Codeforces Round #503 C Elections【优先队列+枚举】

题意:1号选举人想要赢的胜利,可以花钱买票得到最多的票;
分析:
先把vector排下序,再枚举获得的票数,用优先队列维护花费.

#include 
#include 
#include 
#include 
using namespace std;
#define met(s) memset(s, 0, sizeof(s))
#define rep(i, a, b) for(int i = a; i <= b; ++i)
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const LL INF = 1LL << 60;
const int mod = 1e9 + 7;
const int MAXN = 1e4 + 10;
vector G[MAXN];

int main() {
    int n, a, b, m;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
    }
    rep(i, 2, m) sort(G[i].begin(), G[i].end());
    LL ans = INF, sum;
    priority_queuevector, greater > que;
    rep(i, 1, n) {
        int cnt = G[1].size(); sum = 0;
        rep(j, 2, m) {
            int res = G[j].size();
            if(!res) continue;
            rep(p, 0, G[j].size() - 1) {
                if(res < i) que.push(G[j][p]);
                else {
                    res--;
                    cnt++;
                    sum += G[j][p];
                }
            }
        }
        while(cnt < i && !que.empty()) {
            sum += que.top();
            que.pop();
            cnt++;
        }
        while(!que.empty()) que.pop();
        if(cnt >= i) ans = min(ans, sum);
    }
    printf("%lld\n", ans);
    return 0;
}

你可能感兴趣的:(栈和队列,codeforces)