题目大意:有$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. 【Commare中关于理论范畴和技术常用的技术术语】

    1:地址掩码,分类2:OSI/TCP-IP层,归属,作用3:debugg,dispaly具体配置,排错,现象4:原理5:术语 | | = 局域网,广域网 | | = ( (OSPF,RIP,ISIS, ...

  2. PHP如何实现99乘法表?

    看到这个问题,可能大家更多的是考虑到用for循环,个人觉得使用for循环太影响程序性能.推荐使用递归处理.  /** * Title : 递归实现99乘法表 * Author : Bruceqi * ...

  3. @staticmethod怎么用?

    早上起来写个小demo, 类中写了个方法, pycharm给这个方法加上了莫名其妙的波浪线, 对于一个有代码洁癖的人来说, 完全不能忍, 来看看为什么. 问题重现 pycharm的提示 上面说了, 这 ...

  4. Linux : centOS 与 Ubuntu 安装 Nginx

    源码下载: wget http://nginx.org/download/nginx-1.14.0.tar.gz 解压:tar –zxvf xxx 安装依赖: yum -y install  open ...

  5. python七类之字典详解

    一.字典 一.关键字:dict 1.字典是唯一的键值对数据,其表现形式:   dic  =  {键:值},​字典里的键必须保证是唯一的 2.键必须是不可变的数据类型: ​a.故列表是不能当键的 b.所 ...

  6. 黑洞数--python

    黑洞数:黑洞数又称陷阱数,是类具有奇特转换特性的整数.任何一个数字不全相同整数,经有限“重排求差”操作,总会得某一个或一些数,这些数即为黑洞数.“重排求差”操作即把组成该数的数字重排后得到的最大数减去 ...

  7. Git 克隆指定分支代码

    git clone 指定分支 拉代码 1.git clone 不指定分支 git clone  http://10.1.1.11/service/sz-service.git 2.git clone ...

  8. intellij idea之git执行打标签(tag)和删除标签

    intellij idea 版本为2017.2.6 进入Version Control-->log 1.在之前版本中,右键,新建标签 2.输入标签名称,建议输入版本号的方式 3.push标签 由 ...

  9. 虚拟接VMnet1 和VMnet8的区别

    vmnet1是host-only,也就是说,选择用vmnet1的话就相当于VMware给你提供了一个虚拟交换机,仅将虚拟机和真实系统连上了,虚拟机可以与真实系统相互共享文件,但是虚拟机无法访问外部互联 ...

  10. ant-design 实现一个登陆窗口

    前提:已经完成项目实战(https://ant.design/docs/react/practical-projects-cn#定义-Model) 如果要想实现一个登陆窗口,首先得有一个ui,想到的是 ...