CodeForces - 140C New Year Snowmen(贪心)

9 篇文章 0 订阅
9 篇文章 0 订阅

New Year Snowmen

As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 12 and 3 can be used to make a snowman but 223 or 222 cannot. Help Sergey and his twins to determine whatmaximum number of snowmen they can make from those snowballs.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1r2, ...,rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

Output

Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.

Examples
input
Copy
7
1 2 3 4 5 6 7
output
2
3 2 1
6 5 4
input
Copy
3
2 2 3
output
0

题意:给出n个雪球,每个雪球半径为Ri,不同半径的3个小球可以组成一个雪人,问最多可以组成多少个雪人?

思路:wa了一发后,想了想这个跟雪球的数目有明显的关系,我们其实应该优先用多的那个半径的雪球,因为某个半径多的话,它就有更多的机会与别的球结合,酱就会组成尽量多的雪人.(介于题目情况,我用了map和优先队列)

当然,二分求解也是可以的,假设要组成mid个雪人,那么某个半径的雪球我们最多只能选择mid个(想想是吧,因为雪人一共就由3*mid组成,他只能做最大或者中间大或者小的),然后二分完输出就好.

二分代码:

//二分 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath> 
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

int n;
int ans[maxn][3];
map<int,int> mp;

int main()
{
	cin>>n;
	for(int i = 1;i<= n;i++)
	{
		int x;
		scanf("%d",&x);
		mp[x]++;
	}
	
	int l = 1,r = n/3;
	int maxm = 0;
	while(l<= r)
	{
		int mid = (l+r)>>1;
		int tmp = 0;
		
		for(it = mp.begin();it!= mp.end();it++)
			tmp+= min(it->second,mid);
		
		if(tmp>= mid*3)
			l = mid+1,maxm = mid;
		else 
			r = mid-1;
	}
	
	cout<<maxm<<endl;
	int i = 0,j = 0;
	for(it = mp.begin();it!= mp.end();it++)
	{
		int k = min(it->second,maxm);
		while(k--)
		{
			ans[++i][j] = it->first;
			if(i == maxm)
			{
				j++;
				i = 0;
			}
			if(j == 3)
				break;
		}
		if(j == 3)
			break;
	}
	
	for(int i = 1;i<= maxm;i++)
		printf("%d %d %d\n",ans[i][2],ans[i][1],ans[i][0]);
	
	return 0;
}


贪心代码:

//贪心+优先队列 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath> 
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
	int num;
	int sum;
	node (int num = 0,int sum = 0): num(num),sum(sum){}
};
int n,ans[maxn][3];
map<int,int> mp;
bool operator< (node x,node y)
{
	return x.sum< y.sum;  
}

int main()
{
	cin>>n;
	for(int i = 1;i<= n;i++)
	{
		int x;
		scanf("%d",&x);
		mp[x]++;  
	}
	
	priority_queue<node> q;
	for(it = mp.begin();it!= mp.end();it++)
		q.push(node(it->first,it->second));
	
	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();
		
		int tmp[3] = {a.num,b.num,c.num};
		sort(tmp,tmp+3);
		
		ans[++cnt][0] = tmp[2];
		ans[cnt][1] = tmp[1];
		ans[cnt][2] = tmp[0];
		
		a.sum--;
		b.sum--;
		c.sum--;
		if(a.sum)
			q.push(a);
		if(b.sum)
			q.push(b);
		if(c.sum)
			q.push(c);
	}
	
	cout<<cnt<<endl;
	for(int i = 1;i<= cnt;i++)
		printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
	return 0;
}

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值