soj 1862 Choice Pears(优先队列)

@(K ACMer)
题意:
每次取n个堆上最小的一个的贪心策略.
分析
最小堆即可.STL里面的优先队列可过,自己写了仿函数.

code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <string>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 5e3 + 40;
int n, k;
pair<int, int> a[maxn];

class cmp
{
public:
    bool operator() (const pair<int, int>& a, const pair<int, int>& b) {
        return a.first > b.first;
    }
};


int main(void) {
    while (~scanf("%d%d", &n, &k)) {
        priority_queue<pair<int, int>, vector<pair<int, int> >, cmp > que;
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i].first);
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i].second);
        for (int i = 0; i < n; i++)
            que.push(a[i]);
        long long ans = 0;
        while (k--) {
            pair<int, int> temp = que.top();
            ans += temp.first;
            temp.first += temp.second;
            que.pop();
            que.push(temp);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

你可能感兴趣的:(soj 1862 Choice Pears(优先队列))