[CodeForces - 140C New Year Snowmen] 贪心 + STL 优先队列

[CodeForces - 140C New Year Snowmen] 贪心 + STL 优先队列

题意: 给定N个数 ( 1 ≤ n ≤ 105 ),问最多能够选出多少个三元组满足(a, b, c) 满足(a < b < c)。
分析:贪心。每次都是从N个数中选出出现次数最多三个不同的数,一定是最优的。因为这样最多的不先选取会可能取不完。比如下面的测试数据。当大小为 a 数选完了, 便将它从优先队列中删除。
测试数据
10
1 2 2 2 2 3 3 4 4 4

#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
#define lson            l, mid, rt << 1
#define rson            mid + 1, r, rt << 1 | 1

typedef __int64  LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;

const int MAXN = 1e5 + 5;

struct Node {
    int a, b, c;
    Node () {}
    Node (int a, int b, int c) : a (a), b (b), c (c) {}
};
vector<Node> res;
int N;
int R[MAXN];
priority_queue<PII> Q;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    while (~scanf ("%d", &N) ) {
        PII top;
        res.clear();
        for (int i = 0; i < N; i++) {
            scanf ("%d", &R[i]);
        }
        sort (R, R + N);
        for (int i = 0, cnt; i < N; i++) {
            cnt = 1;
            while (i + 1 < N && R[i + 1] == R[i]) cnt ++, i++;
            Q.push (make_pair (cnt, R[i]) );
        }
        PII tp[3];
        int x[3];
        while (!Q.empty() ) {
            bool finish = false;
            for (int i = 0; i < 3; i++) {
                if (Q.empty() ) {
                    finish = true;
                    break;
                }
                tp[i] = Q.top();
                Q.pop();
            }
            if (finish) continue;
            for (int i = 0; i < 3; i++) {
                x[i] = tp[i].snd;
                tp[i].fst --;
                if (tp[i].fst) Q.push (tp[i]);
            }
            sort (x, x + 3);
            res.push_back (Node (x[0], x[1], x[2]) );
        }
        int tot = res.size();
        printf ("%d\n", tot);
        for (int i = 0; i < tot; i++) {
            printf ("%d %d %d\n", res[i].c, res[i].b, res[i].a);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值