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 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
随机推荐
- python之路----面向对象中的内置函数
property属性 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法 ...
- MySQL Crash Course #18# Chapter 26. Managing Transaction Processing
InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...
- 20145318《网络对抗》Web应用
20145318<网络对抗>Web应用 基础问题回答 (1)什么是表单 在网页中负责信息采集,在网页中用户输入信息,通过表单能够提交到后台进行相应的处理 (2)浏览器可以解析运行什么语言 ...
- VC++ 文件和应用程序关联,默认图标不显示问题
- fatal One or more refs for names blocks change upload
前言 今天在提代码时,发现push不到gerrit仓库了,十分的奇怪,和同事沟通后发现,同事可以直接git push origin master而且也可以合并,都是没有问题的,但是就是在gerrit上 ...
- Centos用yum方式安装nodejs和npm
要通过 yum 来安装 nodejs 和 npm 需要先给 yum 添加 epel 源 ##添加 epel 源 rpm -ivh http://download.fedoraproject.org/p ...
- java 如果仅输出一位和要输出多位格式的输出问题,利用boolean值.
package com.ykmimi.testtest; /** * 第七周第二题,来自网络 */ import java.util.Scanner; public class NumberPerfe ...
- Unity3D学习笔记(十七):IK动画、粒子系统和塔防
新动画系统: 反向动力学动画(IK功能): 魔兽世界(头部动画),神秘海域(手部动画),人类一败涂地(手部动画) 如何启用(调整) 1.必须是新动画系统Animator 设置头.手.肘的目标点 2.动 ...
- bugfree 数据库配置 显示No such file or directory
把数据库服务器 localhost 改成127.0.0.1 sudo vi /etc/hosts 增加: 127.0.0.1 localhost 127.0.0.1 你的hostname ...
- WAV文件格式解析及处理
RIFF file format RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构.RIFF文 ...