Elections CodeForces - 1020C (贪心)
大意: 有n个选民, m个党派, 第i个选民初始投$p_i$一票, 可以花费$c_i$改变投票, 求最少花费使得第一个党派的票数严格最大
假设最终第一个党派得票数$x$, 枚举$x$, 则对于所有票数$\ge x$的党派, 贪心选择尽量小的c改选第一个党派.若最后票数还不够, 再从没改过的人中选择尽量小的改选即可
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const int N = 4e5+10, INF = 0x3f3f3f3f;
int n, m;
vector<pii> s[N];
pii a[N];
int vis[N]; int main() {
scanf("%d%d", &n, &m);
REP(i,1,n) {
int x, y;
scanf("%d%d", &x, &y);
s[x].pb({y,i});
a[i] = {x==1?1e9:y,i};
}
sort(a+1, a+1+n);
REP(i,2,m) sort(s[i].begin(),s[i].end());
ll ans = 1e18;
REP(i,1,n) {
int tot = 0;
ll c = 0;
REP(j,2,m) if (s[j].size()>=i) {
for (int k=0; k<=s[j].size()-i; ++k) {
++tot, c+=s[j][k].x, vis[s[j][k].y]=1;
}
}
REP(j,1,n) {
if (s[1].size()+tot<i&&!vis[a[j].y]) {
++tot, c+=a[j].x;
}
}
if (s[1].size()+tot==i) {
ans = min(ans, c);
}
REP(i,1,n) vis[i]=0;
}
printf("%lld\n", ans);
}
Elections CodeForces - 1020C (贪心)的更多相关文章
- Little Elephant and Elections CodeForces - 258B
Little Elephant and Elections CodeForces - 258B 题意:给出m,在1-m中先找出一个数x,再在剩下数中找出6个不同的数y1,...,y6,使得y1到y6中 ...
- CodeForces - 1020C C - Elections(贪心+枚举)
题目: 党派竞争投票 有n个人,m个党派,这n个人每个人有一个想要投的党派的编号Pi,如果想要这个人改变他的想法,那么就需要花费Ci元钱. 现在你是编号为1的党派,如果你想要赢(你的票数严格大于其他党 ...
- Codeforces Round #503 (by SIS, Div. 2) C. Elections (暴力+贪心)
[题目描述] Elections are coming. You know the number of voters and the number of parties — n and m respe ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
随机推荐
- C++ Word Count 发布程序
前段时间,模仿 Linux 系统下的 wc 程序,在 Windows 系统环境下使用 C/C++ 实现了一个相似的 WC 程序,只不过有针对性,针对的是 C/C++,Java 等风格的源代码文件. 此 ...
- oracle用户名和密码到期后如何处理
原因:确定是由于Oracle11g中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致. 影响: 1.密码过期后,业务进程连接数据库异常,影响业务使用. 2. ...
- USB开发库STSW-STM32121文件分析
hw_config.c: 该文件中包含系统配置的函数. usb_desc.c:各种描述符 usb-endp.c:就两个函数分别处理端点1的IN和端点2的OUT. usb_istr.c: 该文件中只有一 ...
- MIPI接口LCD屏调试心得(转)
源: MIPI接口LCD屏调试心得
- MySQL Crash Course #10# Chapter 19. Inserting Data
INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...
- Elasticsearch 疑难解惑
Elasticsearch是如何实现Master选举的? Elasticsearch的选主是ZenDiscovery模块负责的,主要包含Ping(节点之间通过这个RPC来发现彼此)和Unicast(单 ...
- bzoj 1010 玩具装箱toy -斜率优化
P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具 ...
- 史丰收速算|2014年蓝桥杯B组题解析第四题-fishers
史丰收速算 史丰收速算法的革命性贡献是:从高位算起,预测进位.不需要九九表,彻底颠覆了传统手算! 速算的核心基础是:1位数乘以多位数的乘法. 其中,乘以7是最复杂的,就以它为例. 因为,1/7 是个循 ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- 【Map】Echarts之iphone销量地图的使用以及详细配置
1.引入echarts库文件 <script charset="utf-8" type="text/javascript" language=" ...