萌新求助,luogu 这题,本机 AC 提交 RE(非玩梗)
查看原帖
萌新求助,luogu 这题,本机 AC 提交 RE(非玩梗)
305121
8atemak1r楼主2022/4/20 17:37
#include<iostream>
#include<queue>
#define int long long
using namespace std;
queue<int> q1, q2;
int read() {
    int num = 0, ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch <= '9' && ch >= '0') num = (num << 3) + (num << 1) +(ch ^ 48), ch = getchar();
    return num;
}
int n, i, tmp, tmpp, sum;
int t[100005];
inline int get() {
	if(q1.empty()) {
		tmp = q2.front(), q2.pop();
		return tmp;
	}
	if(q1.front() < q2.front()) tmp = q1.front(), q1.pop();
	else tmp = q2.front(), q2.pop();
	return tmp;
}
signed main() {
    n = read();
    for(int i = 1; i <= n; ++ i) tmp = read(), ++ t[tmp], tmpp = max(tmp, tmpp);
    for(int i = 1; i <= tmpp; ++ i) while(t[i] --) q1.push(i);
    sum += q1.front(); q1.pop(); sum += q1.front(); q1.pop(); q2.push(sum);
    for(int i = 2, x, y; i < n; ++ i) {
//    	if(q1.front() < q2.front()) x = q1.front(), q1.pop();
//    	else x = q2.front(), q2.pop();
//    	if(q1.front() < q2.front()) y = q1.front(), q1.pop();
//    	else y = q2.front(), q2.pop();
		x = get(); y = get();
    	sum += x + y; q2.push(x + y);
    }
    cout << sum;
    return 0;
}
// 4 1 2 3 4
2022/4/20 17:37
加载中...