桶排快读普通队列60pts求优化
查看原帖
桶排快读普通队列60pts求优化
431095
Druid楼主2024/11/8 12:38
#include <bits/stdc++.h>
using namespace std;
int n,a[11451419],tong[114514],l,r;
long long ans, x;
long long b[11451419];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n;i++)
	{
		cin >> x;
		tong[x]++;
	}
	int k=0;
	for(int i=0;i<=100000;i++)
		for(int j=1;j<=tong[i];j++)
			a[k++]=i;
	int i = 0;
	while(1)
	{
		if(i>=n)//当原序列中没有数时
		{
			if(l+1==r)
				break;
			x = b[l++];
			ans += x + b[l];
			b[r++]=(x + b[l++]);
		}
		else
		{
			if(l!=r and b[l]<=a[i])
				x = b[l++];
			else
				x = a[i++];
			if(l!=r and (i>=n or b[l]<=a[i]))
			{
			    ans += x + b[l];
			    b[r++]=(x + b[l++]);
			}
			else
			{
			    ans += x + a[i];
			    b[r++]=(x + a[i++]);
			}
		}
	}
	cout << ans;
	return 0;
}
2024/11/8 12:38
加载中...