题目大意:有$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. 实现php Curl 调用不同项目中方法

    之前为了实现跨项目调用方法,遇到的一些问题和解决方法总结. 话不多说,直接复制代码先跑了再说! jq代码. $.ajax({ type: "post", dataType: &qu ...

  2. vue $set修改数组

    看了别人写的,自己简单写一下自己的理解. 因为 JavaScript 的限制,Vue.js 不能检测到下面数组变化,所以,想要正常是不能通过操作数组来渲染dom的,解决的方法是通过set方法, 在组件 ...

  3. python实现归并排序,归并排序的详细分析

    python实现归并排序,归并排序的详细分析.   学习归并排序的过程是十分痛苦的.它并不常用,看起来时间复杂度好像是几种排序中最低的,比快排的时间复杂度还要低,但是它的执行速度不是最快的.很多朋友不 ...

  4. JS如何给ul下的所有li绑定点击事件,点击使其弹出下标和内容

    这是一个非常常见的面试题,出题方式多样,但考察点相同,下面我们来看看这几种方法:方法一: var itemli = document.getElementsByTagName("li&quo ...

  5. pyqt5--学习资料

    http://zetcode.com/gui/pyqt5/ http://www.thehackeruniversity.com/2014/01/23/pyqt5-beginner-tutorial/ ...

  6. 吐血分享:QQ群霸屏技术教程(接单篇)

    在文章<QQ群霸屏技术教程(利润篇)>中,阿力推推提及到QQ群霸屏技术变现的方式,稍显粗略,这里详尽介绍下(老鸟漂过). 资本 资本之上,才谈得上接单,没技能,接个毛线. 1擅长点. 建议 ...

  7. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO

    你有碰上过这样的提示吗? Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in t ...

  8. mysql 筛选重复名称

    CREATE TABLE `blur_article` ( `id` ) NOT NULL, `name` ) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=In ...

  9. (数据科学学习手册28)SQL server 2012中的查询语句汇总

    一.简介 数据库管理系统(DBMS)最重要的功能就是提供数据查询,即用户根据实际需求对数据进行筛选,并以特定形式进行显示.在Microsoft SQL Serve 2012 中,可以使用通用的SELE ...

  10. 牛客网暑期ACM多校训练营(第四场) F

    参考:http://www.cnblogs.com/Jadon97/p/9383027.html #include <iostream> #include <cstdio> # ...