题目大意:有$n$个人,$m$个政党,每个人都想投一个政党,但可以用一定的钱让他选你想让他选的政党。 现在要$1$号政党获胜,获胜的条件是:票数严格大于其他所有政党。求最小代价

题解:暴力枚举其他政党的最多得票,然后把超过的贪心收买,若$1$号政党得票不够,就继续贪心收买,更新答案,复杂度$O(n^2 \log_2 n)$($n=3000,time\;limit=2s$卡过)

卡点:1.用$pb\_ds$的$priority\_queueTIL$,换成$std:priority\_queueAC$

C++ Code:

#include <cstdio>
#include <ext/pb_ds/priority_queue.hpp>
#define int long long
#define maxn 3010
using namespace std;
int n, m, ans = 0x3f3f3f3f3f3f3f3f;
int p[maxn], c[maxn];
struct cmp {
inline bool operator ()(int a, int b) {return a > b;}
};
//__gnu_pbds::priority_queue<int, cmp, __gnu_pbds::binary_heap_tag> q[maxn], Q;
priority_queue<int, std::vector<int>, cmp > q[3050], Q;
void solve(int mid) {
for (int i = 1; i <= m; i++) while (!q[i].empty()) q[i].pop();
for (int i = 1; i <= n; i++) q[p[i]].push(c[i]);
int res = 0, sz = q[1].size();
for (int i = 2; i <= m; i++) {
while (q[i].size() > mid) {
res += q[i].top();
q[i].pop();
sz++;
}
}
if (sz <= mid) {
int delta = mid - sz + 1;
while (!Q.empty()) Q.pop();
for (int i = 2; i <= m; i++) {
int tmp = 0;
while (!q[i].empty() && tmp < delta) {
Q.push(q[i].top());
q[i].pop();
tmp++;
}
}
while (sz <= mid && !Q.empty()) {
res += Q.top();
Q.pop();
sz++;
}
}
if (res < ans) ans = res;
}
signed main() {
scanf("%I64d%I64d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%I64d%I64d", &p[i], &c[i]);
}
for (int i = n - 1; ~i; i--) solve(i);
printf("%I64d\n", ans);
return 0;
}

  

[CF1019A]Elections的更多相关文章

  1. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections 优先队列

                                                    A. Bear and Elections                               ...

  2. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...

  3. 【CF850E】Random Elections(FWT)

    [CF850E]Random Elections(FWT) 题面 洛谷 CF 题解 看懂题就是一眼题了... 显然三个人是等价的,所以只需要考虑一个人赢了另外两个人就好了. 那么在赢另外两个人的过程中 ...

  4. Codeforces 458C - Elections

    458C - Elections 思路: 三分凹形函数极小值域 代码: #include<bits/stdc++.h> using namespace std; #define ll lo ...

  5. 【CF850E】Random Elections FWT

    [CF850E]Random Elections 题意:有n位选民和3位预选者A,B,C,每个选民的投票方案可能是ABC,ACB,BAC...,即一个A,B,C的排列.现在进行三次比较,A-B,B-C ...

  6. CodeForces - 369C - Valera and Elections

    369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/std ...

  7. CodeForces - 457C:Elections(三分)

    You are running for a governor in a small city in Russia. You ran some polls and did some research, ...

  8. Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索

    题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...

  9. codeforces Codeforces Round #318 div2 A. Bear and Elections 【优先队列】

    A. Bear and Elections time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. jsonp 跨域只能调用一次ajax(无法多次调用或者循环调用)

    jsonp 跨域只能掉用一次ajax(无法多次调用或者循环调用) 百度搜索关键字:jsonp 只能调用一次ajax 解决方法 //回调函数设置,给后台执行        window[callback ...

  2. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column …… 出现这个异常的很大可能性是 数据库是没有问题的 ...

  3. windows 下安装pyspider

    今天主要介绍一下在Windows下安装pyspider,pyspider是一款用python编写的网络爬虫框架,这个框架最好是在linux下运行,Windows下运行可能会出现兼容性问题,如果实在要在 ...

  4. c#学习笔记《1》——regex类

    C#regex是正则表达式类用于string的处理,查找匹配的字符串.1,先看一个例子Regex regex=new Regex(@”OK“)://我们要在目标字符串中找到"OK" ...

  5. 如何导入XML数据 (python3.6.6区别于python2 环境)

    1.在python2中 代码如下图: 放在python3 环境下执行,将出现如下错误: 原因: python2中形如myTree.keys()[0]这样的写法是没有问题的,因为myTree.keys( ...

  6. go学习笔记-常见命令

    常见命令 go 命令 可以在控制台执行go来查看 go Go is a tool for managing Go source code. Usage: go <command> [arg ...

  7. UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

    参考:https://blog.csdn.net/lianai911/article/details/41831645 #include <iostream> #include <c ...

  8. 2648: SJY摆棋子

    2648: SJY摆棋子 https://www.lydsy.com/JudgeOnline/problem.php?id=2648 分析: k-d tree 模板题. 代码: #include< ...

  9. laravel+vue结合使用

        SegmentFault 首页 问答 专栏 讲堂 圈子 发现 搜索 立即登录免费注册 在 SegmentFault,学习技能.解决问题 每个月,我们帮助 1000 万的开发者解决各种各样的技术 ...

  10. 从一个线上服务器警告谈谈backlog

    缘起 双十一如期而至,此时的我因为在处理客户的一个问题已经陷入了忙碌.突然,不断接到驻场实施发来的反馈,都是相同的反馈--"客户端操作缓慢". 我现在负责的服务器是一台接口服务器, ...