CodeForces 140C New Year Snowm

Posted 梦樱羽

tags:

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

题目链接:http://codeforces.com/contest/140/problem/C

题目大意:

  给定n个数组成的序列(有重复),从中选3个数为一组,使得这三个数严格递增,请问最多能选出多少组,把每组数据输出。

分析:

  很明显是贪心,不过贪心策略有待斟酌。
  一开始我想当然的把数据按大小排序后从小到大贪心,结果就Wa了,很容易找到反例:1 2 3 4 4 4 5 5 5
  如果从小到大贪,那么答案为1,不过这组数据眼睛看看答案都应该是3。造成这种情况的原因是我把数量少的先贪掉了,很多数量多的没得贪。因此贪心策略应该先贪数量多的。

代码如下:

技术图片
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define rep(i,n) for (int i = 0; i < (n); ++i)
  5 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  6 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  7 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  8 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  9  
 10 #define pr(x) cout << #x << " = " << x << "  "
 11 #define prln(x) cout << #x << " = " << x << endl
 12  
 13 #define LOWBIT(x) ((x)&(-x))
 14  
 15 #define ALL(x) x.begin(),x.end()
 16 #define INS(x) inserter(x,x.begin())
 17  
 18 #define ms0(a) memset(a,0,sizeof(a))
 19 #define msI(a) memset(a,inf,sizeof(a))
 20 #define msM(a) memset(a,-1,sizeof(a))
 21  
 22 #define pii pair<int,int> 
 23 #define piii pair<pair<int,int>,int> 
 24 #define mp make_pair
 25 #define pb push_back
 26 #define fi first
 27 #define se second
 28  
 29 inline int gc(){
 30     static const int BUF = 1e7;
 31     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 32      
 33     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 34     return *bg++;
 35 } 
 36  
 37 inline int ri(){
 38     int x = 0, f = 1, c = gc();
 39     for(; c<48||c>57; f = c==-?-1:f, c=gc());
 40     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 41     return x*f;
 42 }
 43  
 44 typedef long long LL;
 45 typedef unsigned long long uLL;
 46 const int inf = 1e9 + 9;
 47 const LL mod = 1e9 + 7;
 48 const int maxN = 1e5 + 7;
 49  
 50 struct Node{
 51     int value, amount = 0;
 52      
 53     inline bool operator < (const Node &x) const {
 54         return amount < x.amount;
 55     }
 56 };
 57  
 58 int n, nlen, ans;
 59 Node nodes[maxN];
 60 unordered_map< int, int > mii;
 61 priority_queue< Node > maxH;
 62 int Ans[maxN][3];
 63  
 64 int main(){
 65     while(cin >> n) {
 66         mii.clear();
 67         nlen = 0;
 68         rep(i, n) {
 69             int t;
 70             scanf("%d", &t);
 71             if(mii.find(t) != mii.end()) {
 72                 ++nodes[mii[t]].amount;
 73             }
 74             else {
 75                 mii[t] = nlen;
 76                 nodes[nlen].value = t;
 77                 nodes[nlen++].amount = 1;
 78             }
 79         }
 80          
 81         while(!maxH.empty()) maxH.pop();
 82         rep(i, nlen) maxH.push(nodes[i]);
 83          
 84         ans = 0;
 85          
 86         while(maxH.size() >= 3) {
 87             Node a = maxH.top();
 88             maxH.pop();
 89             Node b = maxH.top();
 90             maxH.pop();
 91             Node c = maxH.top();
 92             maxH.pop();
 93              
 94             int x = min(min(a.value, b.value), c.value);
 95             int z = max(max(a.value, b.value), c.value);
 96             int y = a.value + b.value + c.value - x - z;
 97             Ans[ans][0] = x;
 98             Ans[ans][1] = y;
 99             Ans[ans++][2] = z;
100              
101             if(--a.amount) maxH.push(a);
102             if(--b.amount) maxH.push(b);
103             if(--c.amount) maxH.push(c);
104         }
105         cout << ans << endl;
106          
107         rep(i, ans) printf("%d %d %d\n", Ans[i][2], Ans[i][1], Ans[i][0]);
108     }
109     return 0;
110 }
View Code

 

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

CodeForces 140C New Year Snowmen(堆)

CodeForces - 140C New Year Snowmen

CF140C New Year Snowmen

CF140C New Year Snowmen

codeforces379F New Year Tree

Codeforces 1090C New Year Presents