题目大意:有$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. jQuery最重要的知识点

    1.各种常见的选择器.2.对于属性的操作.[重点] 2.1)获取或设置属性的值: prop(); 2.2 ) 添加.删除.切换样式: addClass/removeClass/toggleClass ...

  2. C# 多条件拼接sql

    #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneN ...

  3. php post提交xml文件

    <?php header("Content-type: text/xml;"); // xml code demo $xmlData = '<?xml version= ...

  4. Scala语法(三)

    模式匹配 1)match val a = 1 val b=a match { *// a match { }返回值赋予变量 b case 1 => "red" case 2 ...

  5. 廖老师的Python教程——安装Python

    因为Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 要开始学习Python编程,首先 ...

  6. class实现Stack

    基于class实现一个存储string类型的Stack 头文件: //stack.h #include<vector> #include<string> class Stack ...

  7. R语言学习笔记(十六):构建分割点函数

    选取预测概率的分割点 cutoff<- function(n,p){ pp<-1 i<-0 while (pp>=0.02) { model.predfu<-rep(&q ...

  8. Android面试收集录 网络与加密

    1.创建Socket对象需要至少指定哪些信息? IP(或域名)和端口号 Socket socket=new Socket("www.baidu.com",80); 2.如何使用So ...

  9. mysql ON DUPLICATE KEY UPDATE、REPLACE INTO

    INSERT INTO ON DUPLICATE KEY UPDATE 与 REPLACE INTO,两个命令可以处理重复键值问题,在实际上它之间有什么区别呢?前提条件是这个表必须有一个唯一索引或主键 ...

  10. LinqToExcel使用简介一

             最近才看到原来也可以用Linq来访问Excel,功能还挺强大的.要使用这个功能,首先得下载一个LinqToExcel的相关文件,然后就可以调用相关的方法.         使用前面介 ...