codeforces-140c New Year snowmen (greedy)

Source: Internet
Author: User
Tags min printf cmath

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 R1, R2, ..., RN. To make a snowman, one needs any three snowballs whose radii is pairwise different. For example, the balls with radii 1, 2 and 3 can is used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and he 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 R1, R2, ..., 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 is several solutions, print any of them. Examples input Copy

1 2 3 4 5 6 7
3 2 1
6 5 4
Input Copy
2 2 3

Test instructions: Give n a snowball, each snowball radius of ri, different radius of 3 balls can form a snowman, ask how many snowman can be composed?

Idea: WA after a pitch, think of this and the number of snowballs have a clear relationship, we should in fact priority to use more of that radius of the snowball, because a certain radius, it will have more opportunities to combine with other balls, the sauce will be composed as many snowman as possible. (I used the map and priority queue in the case of the problem)

Of course, the two-part solution is also possible, if you want to make up the mid snowman, then a radius of the snowball we can only select mid (think about it, because the snowman is composed of 3*mid, he can only do the largest or middle big or small), and then the output of the two finished.

Two-minute code:

Dichotomy #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);
	} 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-&GT;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; }

Greedy Code:

Greedy + priority queue #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);  
	} 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.pop ();
		Node B = ();
		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];
		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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.