90分#4TLE求助(玄关)
查看原帖
90分#4TLE求助(玄关)
1199534
ycy1124楼主2024/3/25 13:33
#include<bits/stdc++.h>
using namespace std;
queue<long long>a;
queue<long long>b;
long long c[100001];
long long js;
int read(){ 
	int f=1,x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-'){
			f=-1;
			ch=getchar();
		}
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	x*=f;
	return x;
}
int main()
{
	int n,x;
	n=read();
	for(int i=1;i<=n;i++)
	{
		x=read();
		c[x]++;
	}
	for(int i=1;i<=100000;i++)
	{
		while(c[i]>0){
			c[i]--;
			a.push(i);
		}
	}
	long long s1,s2;
	if(n==1)
	{
		printf("0");
		return 0;
	} 
	while(a.size()>=1||b.size()>1)
	{
		if(b.size()>=2&&a.size()>=2){
			if(a.front()<b.front())
			{
				s1=a.front();
				a.pop(); 
			}
			else
			{
				s1=b.front();
				b.pop();
			}
			if(a.front()<b.front())
			{
				s2=a.front();
				a.pop(); 
			}
			else
			{
				s2=b.front();
				b.pop();
			}
		}
		else if(b.empty()&&a.size()>=2)
		{
			s1=a.front();
			a.pop();
			s2=a.front();
			a.pop();
		}
		else if(a.empty()&&b.size()>=2)
		{
			s1=b.front();
			b.pop();
			s2=b.front();
			b.pop();
		}
		else if(a.size()==1&&b.size()==1)
		{
			s1=a.front();
			a.pop();
			s2=b.front();
			b.pop();
		}
		else if(a.size()==1&&b.size()>1)
		{
			if(a.front()<b.front())
			{
				s1=a.front();
				a.pop();
				s2=b.front();
				b.pop(); 
			}
			else
			{
				s1=b.front();
				b.pop();
				if(a.front()<b.front())
				{
					s2=a.front();
					a.pop(); 
				}
				else
				{
					s2=b.front();
					b.pop();
				}
			}
		}
		else if(b.size()==1&&a.size()>1)
		{
			if(a.front()<b.front())
			{
				s1=a.front();
				a.pop();
				if(a.front()<b.front())
				{
					s2=a.front();
					a.pop(); 
				}
				else
				{
					s2=b.front();
					b.pop();
				} 
			}
			else
			{
				s1=b.front();
				b.pop();
				s2=a.front();
				a.pop();
			}
		}
		b.push(s1+s2);
		js+=s1;
		js+=s2;
		//cout<<js<<"\n";
	}
	cout<<js;
	return 0;
}
/*
4
2 2 2 2
*/
2024/3/25 13:33
加载中...