过不了test 2求hack数据
查看原帖
过不了test 2求hack数据
586932
White_Wat楼主2024/7/2 10:29
#include<bits/stdc++.h>

using namespace std;

const int N = 5010;

int T;
int n,a[N];
int cake[N];
int sum[N];
int ans,flag;
typedef pair<int,int> PII;

int main(){
	cin>>T;
	while(T--){
		priority_queue<PII,vector<PII>,greater<PII>> heap;
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++) cake[a[i]]++;
		for(int i=1;i<=n;i++){
			sum[i]=sum[i-1];
			if(cake[i]){
				heap.push({cake[i],i}),sum[i]++;
			}
		}
		ans=0,flag=0;
		for(int i=1;i<=n;i++){
			if(!cake[i]) continue;
			if(!flag){
				ans++,flag^=1;
				/*cout<<i<<' ';*/
				cake[i]=0;
			}
			else{
				i--;
				while(heap.size()&&(!cake[heap.top().second]||sum[heap.top().second-1]-sum[i]<heap.top().first-1)) heap.pop();
				if(!heap.size()){
					i++;
					for(;i<=n;i++) if(cake[i]>0) {/*cout<<i<<':'*/;ans++;}
					break;
				}
				PII tmp=heap.top();
				heap.pop();
				int num=tmp.first,id=tmp.second;
				/*cout<<id<<' ';*/
				cake[id]--,num--;
				if(num!=0) heap.push({num,id});
				else{
					for(int j=id;j<=n;j++) sum[j]--;
				}
				flag^=1;
			}
		}
		for(int i=1;i<=n;i++) cake[i]=0;
		for(int i=1;i<=n;i++) sum[i]=0;
		cout<<ans<<endl;
	}
	return 0;
}
2024/7/2 10:29
加载中...