60求助,孩子T飞了
查看原帖
60求助,孩子T飞了
400783
Nephren_Sakura楼主2022/10/24 15:22
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,a[10000005],sum[100005];
long long ans;
queue<long long> q1,q2;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1; i<=n; i++)
		cin>>a[i],sum[a[i]]++;
	for(int i=1; i<=100000; i++)
		for(int j=1; j<=sum[i]; j++)
			q1.push(i);
	for(int i=1; i<n; i++){
		long long x,y;
		if(!q1.empty()&&!q2.empty()){
			if(q1.front()>q2.front())
				x=q2.front(),q2.pop();
			else
				x=q1.front(),q1.pop();
		}
		else if(!q1.empty())
			x=q1.front(),q1.pop();
		else
			x=q2.front(),q2.pop();
		if(!q1.empty()&&!q2.empty()){
			if(q1.front()>q2.front())
				y=q2.front(),q2.pop();
			else
				y=q1.front(),q1.pop();
		}
		else if(!q1.empty())
			y=q1.front(),q1.pop();
		else
			y=q2.front(),q2.pop();
		q2.push(x+y);
		ans+=x+y;
	}
	cout<<ans;
	return 0;
}
2022/10/24 15:22
加载中...