手写队列求调全WA,样例过了
查看原帖
手写队列求调全WA,样例过了
947029
Chen_Three楼主2024/12/10 22:23
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll q1[10000005], q2[10000005], dui[100005];
ll head1 = 1, head2 = 1, tail1 = 1, tail2 = 1, tired;
template <typename T> void read(T &x){
	x = 0;
	char ch = getchar();
	ll fh = 1;
	while (ch < '0' || ch > '9'){ 
		if (ch == '-') fh = -1; 
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
	x *= fh;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
int main(){
	ll n, t;
	read(n);
	for(ll i = 1; i <= n; i++){
		read(t);
		dui[t]++; 
	}
	for(ll i = 1; i <= 100000; i++){
		for(ll j = 1; j <= dui[i]; j++){
			q1[tail1++] = i;
		}
	}
	tail1--;
	for(ll i = 1; i < n; i++){
		ll x, y;
		//cout << head1 <<' ' <<head2 <<' ' << tail1 << ' ' << tail2 << '\n';
		if((q1[head1] < q2[head2] && head1 != tail1) || head2 == tail2) x = q1[head1++];
		else x = q2[head2++];
		if((q1[head1] < q2[head2] && head1 != tail1) || head2 == tail2) y = q1[head1++];
		else y = q2[head2++];
		q2[tail2++] = x + y;
		tired += x + y;
		//cout << x << ' ' << y << "\n";
	}
	write(tired);
	return 0;
}
2024/12/10 22:23
加载中...