Codeforces Round #503 C. Elections

枚举2~m的最大票数,对于枚举的每个票数i,可以用优先队列以最小的代价构造出1号的胜局,然后在所有最小代价中取最小值即可

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3010;
const ll INF = 1e16;
vector<int> v[maxn];
vector<ll> sum[maxn];
int pos[maxn];
struct Node
{
    int val, id;
    bool operator < (const Node &t) const
    {
        return val > t.val;
    }
} node;
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    int h1 = 0, maxh = 0;
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
        if (x == 1) h1++;
    }
    for (int i = 2; i <= m; i++)
    {
        sort(v[i].begin(), v[i].end());
        ll t = 0;
        for (int j = 0; j < v[i].size(); j++)
        {
            t += v[i][j];
            sum[i].push_back(t);
        }
        maxh = max(maxh, (int)v[i].size());
    }
    if (h1 > maxh)
    {
        printf("0\n");
        return 0;
    }
    ll ans = INF;
    for (int i = h1; i <= maxh; i++)
    {
        for (int j = 2; j <= m; j++) pos[j] = 0;
        int h = h1;
        ll cost = 0;
        for (int j = 2; j <= m; j++)
        {
            if (v[j].size() > i)
            {
                pos[j] = v[j].size()-i;
                h += (v[j].size()-i);
                cost += sum[j][pos[j]-1];
            }
        }
        if (h >= i + 1)
        {
            ans = min(ans, cost);
            continue;
        }
        priority_queue<Node> que;
        for (int j = 2; j <= m; j++) {
            if (pos[j] < v[j].size()) que.push(Node{v[j][pos[j]], j});
        }
        int need = i+1-h;
        while (need)
        {
            if (que.empty()) break;
            node = que.top();
            que.pop();
            cost += node.val;
            int p = node.id;
            pos[p]++;
            if (pos[p] < v[p].size()) que.push(Node{v[p][pos[p]], p});
            need--;
        }
        if (need == 0) ans = min(ans, cost);
    }
    printf("%I64d\n", ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值