为什么下面这份代码在本题提交就能 AC ,在 P1090 提交就会 WA ?
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,t,m=0,s[111111]={},ans=0,a,b;
queue<ll> p,q;
inline ll read(){
ll re=0;
char ch=getchar();
while(ch>='0'&&ch<='9') re=(re*10)+ch-48,ch=getchar();
return re;
}
int main(){
n=read();
for(ll i=1;i<=n;i++){
t=read();
m=max(m,t);
s[t]++;
}
for(ll i=1;i<=m;i++)
for(ll j=1;j<=s[i];j++)
p.push(i);
while(--n){
if(q.empty()||(!p.empty()&&p.front()<q.front())) a=p.front(),p.pop();
else a=q.front(),q.pop();
if(q.empty()||(!p.empty()&&p.front()<q.front())) b=p.front(),p.pop();
else b=q.front(),q.pop();
q.push(a+b);
ans+=a+b;
}
printf("%lld\n",ans);
return 0;
}
本题提交记录
P1090 提交记录