50pts 求调
查看原帖
50pts 求调
752977
yuzhongbutong123楼主2024/11/24 16:35
#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int N=3e5+100;

int T,n;

ll v[N];

bool cmp(ll x,ll y)
{
	return x>y;
}

int main()
{
//	freopen("card4.in","r",stdin); 
//	freopen("card4.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--)
	{
		ll ans=0;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>v[i];
		sort(v+1,v+1+n,cmp);
		ll tp=n;
		for(int i=1;i<=tp;i++)
		{
			ll ti=v[i]/3;
			ans+=ti;
//			cout<<1<<" "<<ans<<endl;
			v[i]=v[i]%3;
			if(v[i]<=ti)
				ti-=v[i],v[i]=0;
			if(v[i]>0)
			{
				v[i]=0,ans++;
//				cout<<2<<" "<<ans<<endl;
			}
			if(ti>0)
			{
				while(ti>0)
				{
					ll k=min(v[tp],ti);
					v[tp]-=k;
					ti-=k;
					if(v[tp]==0)
						tp--;
					if(tp==0)
						break;
				}
				if(ti>0)
				{
//					cout<<3<<" "<<ans<<endl;
					ans-=ti,ti*=3;
					ll k=ti%4;
					ans+=ti/4;
					if(k==3)
						ans+=2;
					if(k==2||k==1)
						ans+=1;
				}
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}
2024/11/24 16:35
加载中...