#include<bits/stdc++.h>#define int unsigned long longusing namespace std;
int n,m;
int ans;
priority_queue<int,vector<int>,greater<int>> q;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;
cin>>x;
q.push(x);
}
while(q.size()>1){
int x=q.top();
q.pop();
int y=q.top();
q.pop();
if(m*x>=y&&m*y>=x)ans++;
q.push(x+y);
}
cout<<ans;
return 0;
}
正确代码:
C++
#include<bits/stdc++.h>#define int unsigned long longusing namespace std;
int n,m;
int ans;
priority_queue<int,vector<int>,greater<int>> q;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;
cin>>x;
q.push(x);
}
while(q.size()>1){
int x=q.top();
q.pop();
int xx=q.top();
q.pop();
int f=0;
if(xx%m==0&&x>=xx/m||xx%m!=0&&x>xx/m)f++;
if(x%m==0&&xx>=x/m||x%m!=0&&xx>x/m)f++;
if(f==2)ans++;
q.push(x+xx);
}
cout<<ans;
return 0;
}
T4
错误原因:没思路
C++
#include<bits/stdc++.h>#define int long longusing namespace std;
int n,m;
int a[200005];
int s[200005];
int d[200005];
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++)cout<<"-1"<<"\n";
return 0;
}
正确代码:
#include<bits/stdc++.h>#define ll long longusing namespace std;
ll n,m;
ll a[200005];
ll b[200005];
ll d[200005];
ll c[200005];
ll v[200005];
ll vis[200005];
struct node{
int t,x,id;
bool operator<(const node &x)const{
return t>x.t;
}
};
priority_queue<node> q;
int main(){
cin>>n>>m;
for(ll i=1;i<=n;i++){
cin>>v[i];
}
for(ll i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
while(!q.empty()){
node now=q.top();
if(now.t>a[i])break;
q.pop();
v[now.id]+=now.x;
}
if(v[b[i]]>=d[i]){
q.push({a[i]+c[i],d[i],b[i]});
cout<<v[b[i]]-d[i]<<"\n";
v[b[i]]-=d[i];
}else{cout<<"-1\n";}
}
return 0;
}
T5
错误原因:方法用错了
C++
#include<bits/stdc++.h>#define int long longusing namespace std;
int t;
int a[100005];
struct node{
int id,dp;
bool operator<(const node &x)const{
return dp<x.dp;
}
};
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
int n;
cin>>n;
priority_queue<node> q;
for(int i=1;i<=n;i++){
int sum=0;
cin>>a[i];
int x=a[i];
while(a[i]>=10){
int v=x%10;
sum+=v;
x/=10;
}
sum+=x;
q.push({i,sum});
}
for(int i=1;i<=n;i++){
node t=q.top();
int u=t.id;
cout<<a[u];
q.pop();
}
cout<<"\n";
}
return 0;
}
正确代码:
C++
#include<bits/stdc++.h>#define int long longusing namespace std;
int t;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
int n;
cin>>n;
priority_queue<string> q;
for(int i=1;i<=n;i++){
string s;
cin>>s;
q.push(s);
}
string ans;
while(!q.empty()){
string ss=q.top();
q.pop();
ans+=ss[0];
if(ss.size()>0)ss.erase(0,1);
if(ss.size())q.push(ss);
}
cout<<ans<<"\n";
}
return 0;
}