CodeForces - 457C:Elections(三分)
You are running for a governor in a small city in Russia. You ran some polls and did some research, and for every person in the city you know whom he will vote for, and how much it will cost to bribe that person to vote for you instead of whomever he wants to vote for right now. You are curious, what is the smallest amount of money you need to spend on bribing to win the elections. To win elections you need to have strictly more votes than any other candidate.
First line contains one integer n (1 ≤ n ≤ 105) — number of voters in the city. Each of the next n lines describes one voter and contains two integers ai and bi (0 ≤ ai ≤ 105; 0 ≤ bi ≤ 104)
— number of the candidate that voter is going to vote for and amount of
money you need to pay him to change his mind. You are the candidate 0 (so if a voter wants to vote for you, ai is equal to zero, in which case bi will also be equal to zero).
Output
Print one integer — smallest amount of money you need to spend to win the elections.
Examples
5
1 2
1 2
1 2
2 1
0 0
3
4
1 2
1 2
2 1
0 0
2
1
100000 0
0
题意:给定N个voter,给个人有自己准备投的人ai,以及费用bi,表示可以用bi元钱去收买他他让改变主意。问0号选手至少花多少钱,使得他的voter最多。
思路:不难想到二分0号选手的voter数,但是二分是WA的,因为单调性有个临界值,小于这个临界值,无法保证0号是最多的。 直接三分。
(注意不要用二分,因为并非是收买的人越多,代价越高。 有可能收买的人越多,可供选择的越多,使得低价的越多,所以必须三分。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int num[maxn],ans=<<,N;
vector<int>G[maxn]; int q[maxn],cnt;
int check(int Mid)
{
int tnum=num[]+Mid,res=,tot=;
rep(i,,){
rep(j,,max(-,num[i]-tnum)) tot++,res+=G[i][j];
}
if(tot>Mid) return <<; cnt=; //不足以成为最多,反正一个极大值
rep(i,,){
rep(j,max(,num[i]-tnum+),num[i]-) q[++cnt]=G[i][j];
}
sort(q+,q+cnt+);
rep(i,,Mid-tot) res+=q[i];
return res;
}
int main()
{
int x,y;
scanf("%d",&N);
rep(i,,N){
scanf("%d%d",&x,&y);
if(x==||y==) num[]++;
else G[x].push_back(y),num[x]++;
}
rep(i,,) sort(G[i].begin(),G[i].end());
int F=true;
rep(i,,N) if(num[i]>=num[]) F=false;
if(F) return puts(""),;
int L=,R=N,Mid1,Mid2,f1,f2;
while(L<=R){
Mid1=L+(R-L)/; f1=check(Mid1);
Mid2=R-(R-L)/; f2=check(Mid2);
if(f1<=f2) R=Mid2-,ans=min(ans,f1);
else L=Mid1+,ans=min(ans,f2);
}
printf("%d\n",ans);
return ;
}
CodeForces - 457C:Elections(三分)的更多相关文章
- Codeforces C. Elections(贪心枚举三分)
题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 458C - Elections
458C - Elections 思路: 三分凹形函数极小值域 代码: #include<bits/stdc++.h> using namespace std; #define ll lo ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- CodeForces - 1059D——二分/三分
题目 题目链接 简单的说,就是作一个圆包含所有的点且与x轴相切,求圆的最小半径 方法一 分析:求最小,对半径而言肯定满足单调性,很容易想到二分.我们二分半径,然后由于固定了与X轴相切,我们对于每一个点 ...
- Codeforces 939E Maximize ( 三分 || 二分 )
题意 : 给出两个操作,① 往一个序列集合(初始为空)里面不降序地添加数字.② 找出当前序列集合的一个子集使得 (子集的最大元素) - (子集的平均数) 最大并且输出这个最大差值 分析 : 首先关注 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp
C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- codeforces 782B The Meeting Place Cannot Be Changed (三分)
The Meeting Place Cannot Be Changed Problem Description The main road in Bytecity is a straight line ...
- 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 ...
- codeforces 578c - weekness and poorness - 三分
2017-08-27 17:24:07 writer:pprp 题意简述: • Codeforces 578C Weakness and poorness• 给定一个序列A• 一个区间的poornes ...
随机推荐
- web前端设计规范
hi,这里写出一点自己对web产品开发的一点粗浅的规范认识,一切为了敏捷开发哈哈. 1.流程. (1) 当产品给出原型和产品文档. (2)设计师更据原型,开始设计产品的效果图. (3)设计师设计完毕后 ...
- English trip V1 - 1.How Do You Feel Now? Teacher:Lamb Key:形容词(Adjectives)
In this lesson you will learn to describe people, things, and feelings.在本课中,您将学习如何描述人,事和感受. STARTER ...
- android--------Android内存分析工具的使用
内存分析(in-memory analytics)是我们编写速度快.效率高的代码必不可少的知识.如果自己编写的代码在内存的分配一无所知,我想这样的程序让你去优化,应该是无从下手的.那么内存分析是什么? ...
- java微信授权登录传参给redirect_uri 接口,回到原页面,传递多个参数
本文背景: 调用微信的授权登录的时候,我们第一步是需要获取用户同意授权的code,这里面有一个redirect_uri参数,当用户同意授权之后,就会回调访问这个redirect_uri,通常这redi ...
- jsp javabean开发模式
JSP&&JavaBean开发模式 在jsp+javabean架构中,jsp负责控制逻辑,表现逻辑,业务对象的调用 jsp+javaBean模式适合开发业务逻辑不复杂的web应用,这种 ...
- learning scala read from console
控制台输入语句: readInt, readDouble, readByte, readShort, readLong, readChar, readBoolean, readLine example ...
- uart boot log
CC开始 xmodem 传输. 按 Ctrl+C 取消. 100% 95 KB 4 KB/s 00:00:23 0 Errors U-Boot SPL 2018.01 (Jan 16 2019 - 1 ...
- vue react自动更新修改的代码
利用node 引入 nodemon,我们项目修改了代码,就不用需要在重新启动 步骤 npm install --save-dev nodemon 执行 nodemon server.js / ...
- U盘安装Ubuntu三步走
需要工具: U盘.Ubuntu的ISO镜像.universal usb installer 1.下载Ubuntu (1) (2)我这里下载14.04版本 (3)我这里下载64位系统 下载后得到的是个t ...
- dubbo集群应用
前面写了一篇dubbo的基础应用篇,单机版 http://www.cnblogs.com/yun965861480/p/6257670.html, 这次将集群的相关配置记录下来. 1.zookeepe ...