3 条题解
-
0
先桶排,然后弄俩队列
然后把所有的数放到第一个队列里面
然后每次选两个队列里面最小的两个数,把他俩合并扔到第二个队列里面,咱俩数合并完的数是显然单增的,所以第二个队列也是单增的。
这样就做完了
code
#include <bits/stdc++.h> using namespace std; bool mlest; double tlest, tleed; inline long long R(){ long long x = 0, f = 1; char ch = getchar(); while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();} while(isdigit(ch)) {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f; } inline void W(long long x){ if(x < 0) {putchar('-'); x = -x;} if(x > 9) W(x/10); putchar(x%10+'0'); } const long long N = 1e5 + 1; long long n, a[N]; queue<long long> q1, q2; inline void read(){ n = R(); for(long long i = 1;i <= n; i++){ long long x = R(); a[x]++; } for(long long i = 1;i <= 100000; i++){ while(a[i]){ q1.push(i); a[i]--; } } } inline void init(){ } inline void compute(){ long long ans = 0; for(long long i = 1;i < n; i++){ long long x, y; if((q1.size() && q2.size() && q1.front() < q2.front()) || (q1.size() && !q2.size())) { x = q1.front(); q1.pop(); } else{ x = q2.front(); q2.pop(); } if((q1.size() && q2.size() && q1.front() < q2.front()) || (q1.size() && !q2.size())) { y = q1.front(); q1.pop(); } else{ y = q2.front(); q2.pop(); } ans += x + y; q2.push(x+y); } W(ans); } inline void clear(){ } void run(){read(); init(); compute(); clear();} bool mleed; void wa(){cout << '\n' << tleed - tlest << "ms\n" << (&mleed-&mlest-1)/1024.0/1024.0 << "MB\n";} void fre(string s){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } int main(){ // fre(""); tlest = clock(); run(); tleed = clock(); // wa(); return 0; }
信息
- ID
- 321
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- (无)
- 递交数
- 23
- 已通过
- 7
- 上传者