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 ...
随机推荐
- Codeforces 847B - Preparing for Merge Sort
847B - Preparing for Merge Sort 思路:前面的排序的最后一个一定大于后面的排序的最后一个.所以判断要不要开始新的排序只要拿当前值和上一个排序最后一个比较就可以了. 代码: ...
- OpenGL入门程序四:颜色模式
1.OpenGL支持两种颜色模式: 1>RGBA颜色模式 ,用 glClearColor 指定清空屏幕后的颜色,即“空颜色” . 2>索引颜色模式,用 glClearIndex 指定清空屏 ...
- yii CComponent组件 实例说明1
yii CComponent组件 实例说明 yii中的module,controller都是CComponent的子类,可以说yii的架构基石就是依托在CCompnent基础上的,这里研究下CComp ...
- Asp.Net MVC中Action跳转
首先action的跳转大致归类: 1跳转到与当前同一控制器内的action和不同控制器内的action. 2带有参数的action跳转和不带参数的action跳转. 3跳转到指定视图,不经过Contr ...
- mtime 的具体解释
find . -mtime +0 # find files modified greater than 24 hours ago find . -mtime 0 # find files modifi ...
- 『科学计算』科学绘图库matplotlib学习之绘制动画
基础 1.matplotlib绘图函数接收两个等长list,第一个作为集合x坐标,第二个作为集合y坐标 2.基本函数: animation.FuncAnimation(fig, update_poin ...
- JS常用操作方法图表
截取字符串方法 方法名 参数 返回值 例子 String.prototype.substr() (indexStart, length) 都可为负数,length为负数时自动转为0,length不传默 ...
- 循环大法——一次性理清forEach/for-in/for/$each
国寿的这个项目写得我基础都忘完了 近期会把vue和基础都并行复习.学习 forEach 适用于调用数组的每个元素,并将元素传递给回调函数,但是空数组是不会执行回调函数的.forEach适用于集合中的对 ...
- O(logn)二叉树中的意义----高性能(四)
转载地址:https://zhidao.baidu.com/question/239708227508660244.html?qbl=relate_question_2&word=%CA%B1 ...
- 自动化测试框架Taffy
Taffy Taffy是基于nosetests的自动化测试框架. Taffy主要用来测试后台服务(包括且不限于Http, Dubbo/hessian, Webservice, Socket等类型接口) ...