CodeForces - 140C New Year Snowmen

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 140C New Year Snowmen相关的知识,希望对你有一定的参考价值。

CodeForces - 140C New Year Snowmen

题意:

现在来做雪人,每个雪人由三个不同大小的雪球构成:一个大的,一个中等的,一个小的。现在有 n 个雪球半径分别为 r1, r2, …, rn. 为了做雪人,三个雪球的大小必须两两不同。例如,半径分别为 1,2,3 的雪球可以做成雪人,但 2, 2, 3 或 2, 2, 2 不行。现在需要尽可能做更多雪人。

题解:

直接贪心肯定不行,比如7,5,2,2,2,1,1,如果贪心,上来会选7,5,2,然后就没有其他答案了。最佳答案应该是尽可能用最多的,上面这个例子中,2最多,尽可能用2,然后是1最多,然后是5,一轮用完后,这几个数的数量都要减一,然后再找数量最多的三个,每次都要找最多的三个,因此我们要用到优先队列,按照顺序从大到小,然后再按半径从大到小,每次选顶上三个,依次进行即可

代码:

#include<bits/stdc++.h>
#define debug(a,b) printf("%s = %d\\n",a,b);
typedef long long ll;
using namespace std;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
   return s*w;
}
map<int,int>mp;
const int maxn=1e5+9;
struct node{
	int n;
	int num;
	node(int a=0,int b=0):n(a),num(b){
	}
	bool operator <(const node a)const{
		if(a.num!=num)
			return num<a.num;
		else 
			return n<a.n;
	}
};
int tmp[4]; 
int A[maxn],B[maxn],C[maxn]; 
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		mp[x]++;
	}
	priority_queue<node>q;
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		q.push(node(it->first,it->second));
	}
	int sum=0;
	int cnt=0;
	while(q.size()>=3){
		node a=q.top();
		q.pop();
		node b=q.top();
		q.pop();
		node c=q.top();
		q.pop();
		sum++;
		tmp[1]=a.n;
		tmp[2]=b.n;
		tmp[3]=c.n;
		sort(tmp+1,tmp+4);
		A[++cnt]=tmp[3];
		B[cnt]=tmp[2];
		C[cnt]=tmp[1];
		a.num--;
		b.num--;
		c.num--;
		if(a.num>0)
			q.push(node(a.n,a.num));
		if(b.num>0)
			q.push(node(b.n,b.num));
		if(c.num>0)
			q.push(node(c.n,c.num));	
	}
	cout<<sum<<endl;
	for(int i=1;i<=cnt;i++){
		printf("%d %d %d\\n",A[i],B[i],C[i]);
	}
	return 0;
}

以上是关于CodeForces - 140C New Year Snowmen的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 140C New Year Snowmen(堆)

CodeForces - 140C New Year Snowmen

CF140C New Year Snowmen

CF140C New Year Snowmen

codeforces379F New Year Tree

[2016-04-01][codeforces][659E][New Reform]