前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Codeforces Round #710 (Div. 3) D. Epic Transformation(优先队列、贪心)

Codeforces Round #710 (Div. 3) D. Epic Transformation(优先队列、贪心)

作者头像
glm233
发布2021-04-01 17:01:44
发布2021-04-01 17:01:44
34400
代码可运行
举报
运行总次数:0
代码可运行

这题首先可以先找每个数出现次数,然后开一个优先队列,从大到小,最正确的贪心策略是每次最大数和次大数减去(次大数与第三大数差+1)---(可以证明)

代码语言:javascript
代码运行次数:0
复制
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+10;
ll t,a[N];

int main(){
	cin>>t;
	while(t--){
		int x;
		map<int,int>p;
		cin>>x;
		for(int i=1;i<=x;i++)cin>>a[i],p[a[i]]++;
		priority_queue<int,vector<int>,less<int> >q;
		for(auto it:p)q.push(it.second);
		if(q.size()==1){
			cout<<q.top()<<endl;
		}
		else if(q.size()==2){
			int a=q.top();q.pop();
			cout<<a-q.top()<<endl;
		}
		else{
            int flag=0;
			while(q.size()>1){
				int a=q.top();q.pop();
				int b=q.top();q.pop();
				if(q.empty()){
                        flag=1;
                    cout<<a-b<<endl;
                    break;
				}
				int c=q.top();
				//cout<<a<<" "<<b<<endl;
				int sub=b-c+1;
				if(a-sub!=0)q.push(a-sub);
				if(b-sub!=0)q.push(b-sub);
			}
			if(!flag)cout<<q.top()<<endl;
		}


	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档