程序卡常卡不过去,始终600ms左右。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,cc[100010];
ll x[10000010],q[10000010],cnt,ans;
int main() {
scanf("%d",&n);
for(int i=1,a; i<=n; ++i) {
scanf("%d",&a);
cc[a]++;
}
for(int i=1; i<=100000; ++i) {
while(cc[i]--) {
x[++cnt]=i;
}
}
int xh=1,xt=n,qh=1,qt=0;
for(int i=1; i<=n-1; ++i) {
ll w1,w2;
if(xh<=xt&&x[xh]<q[qh]||qh>qt) {
w1=x[xh++];
} else {
w1=q[qh++];
}
if(xh<=xt&&x[xh]<q[qh]||qh>qt) {
w2=x[xh++];
} else {
w2=q[qh++];
}
ans+=w1+w2;
q[++qt]=w1+w2;
}
printf("%lld\n",ans);
return 0;
}
怎么让这个程序变快啊,O2已开。