codeforces 1020 C Elections(枚举+贪心)
题意:
有 n个人,m个党派,第i个人开始想把票投给党派pi,而如果想让他改变他的想法需要花费ci元。你现在是党派1,问你最少花多少钱使得你的党派得票数大于其它任意党派。
n,m<3000
思路:
枚举“除了党派1之外其他党派最多有的票的数量”,贪心一下即可
坑点:
1.爆int
2.inf太小,要设成0x3f3f3f3f3f3f3f3f
3.sort忘了从第一个开始排(被Wa7支配)
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<list> #define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))
#pragma Gcc optimize(2) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const int maxn = + ;
const int maxm = 5e3 + ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
int scan(){
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
}
//int vis[maxn];
struct vt{
int id;
int p;
ll c;
}v[maxn];
int num[maxn];
int cnt[maxn];
int vis[maxn];
/*bool cmp(vt a, vt b){
if(a.p==1)return false;
if(b.p==1)return true;
if(num[a.id] == num[b.id]){
return a.c < b.c;
}
else return num[a.id]<num[b.id];
}*/
bool cmp(vt a, vt b){
if(a.p==)return false;
if(b.p==)return true;
return a.c < b.c;
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
//mem(vis, 0);
mem(num, );
for(int i = ; i <= n; i++){
int p, c;
scanf("%d %lld",&v[i].p, &v[i].c);
v[i].id = i;
num[v[i].p]++; }
ll ans = 0x3f3f3f3f3f3f3f3f;
sort(v+, v++n, cmp);
for(int i = ; i < n; i++){
ll tmp = ;
mem(vis, );
for(int j = ; j <= m ;j++){
cnt[j] = num[j];
}
for(int j = ; j <= n && v[j].p != ; j++){
if(cnt[v[j].p] > i){
cnt[]++;
cnt[v[j].p]--;
tmp += v[j].c;
vis[j] = ;
} }
if(cnt[] <= i){
for(int j = ; j <= n; j++ ){
if(cnt[]>i)break;
if(!vis[j]){
tmp += v[j].c;
cnt[]++;
}
}
}
if(cnt[]>=i)ans = min(tmp, ans);
//printf("%lld %d\n", tmp, cnt[1]);
}
printf("%lld", ans);
return ;
}
codeforces 1020 C Elections(枚举+贪心)的更多相关文章
- Codeforces #503 C. Elections(贪心,逆向
我的参考的博客地址 题目 逆向考虑. 暴力遍历 k(k是1到n/2+1 范围内的),挑出对于每一个k,记对于党派 i,num[ i ]为其票数.num[ i ]小于k-1的就不用改变投票了(这部分是比 ...
- Codeforces C. Elections(贪心枚举三分)
题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
- 枚举+贪心 HDOJ 4932 Miaomiao's Geometry
题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...
- CodeForces - 1020C C - Elections(贪心+枚举)
题目: 党派竞争投票 有n个人,m个党派,这n个人每个人有一个想要投的党派的编号Pi,如果想要这个人改变他的想法,那么就需要花费Ci元钱. 现在你是编号为1的党派,如果你想要赢(你的票数严格大于其他党 ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- spring boot学习笔记(1)
一.构建springboot项目 可以通过idea的springboot initialization来创建, idea的功能很强大,可以自己选择需要集成的插件. 完整的项目结构是这样的: DemoA ...
- 低功耗蓝牙(BLE)——概述
1. 概述 蓝牙协议是由SIG制定并维护的无线通信协议,蓝牙协议栈是蓝牙协议的具体实现.各厂商都根据蓝牙协议实现了自己的一套函数库--蓝牙协议栈,所以不同厂商的蓝牙协议栈之间虽然存在差别,但是都遵 ...
- MySQL 物理备份工具-xtrabackup
安装 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum -y install perl ...
- cogs 886. [USACO 4.2] 完美的牛栏 二分图 匈牙利算法
886. [USACO 4.2] 完美的牛栏 ★★☆ 输入文件:stall4.in 输出文件:stall4.out 简单对比时间限制:1 s 内存限制:128 MB USACO/sta ...
- Gitlab应用——系统管理
查看linux系统信息 查看日志 创建账号 选择regular,这是一个普通账号,点击“create user”账号创建完成 点击“User”,然后点击“New user”.使 ...
- 1.异常(Error和Exception)
什么是异常 比如上午我们一般是开车去上班,正常情况下,一般都不会迟到,但是今天突然有个问题,车坏了或者限行了,于是乎你改坐公交了,就有可能会迟到,这就属于一种异常的情况.在实际生活中呢,可能会遇到很多 ...
- 公司项目redis 项目报错 记事
异常内容: Timeout performing GET Key_CacheHSCode, inst: 1, mgr: ExecuteSelect, err: never, queue: 2, qu ...
- Miniio安装登陆密码报错问题,注意检查区分带小写!
------------恢复内容开始------------ #创建minio专用文件目录mkdir -p /app/minio/datamkdir -p /app/minio/configchmod ...
- JS-01-js的三种引入方式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ffplay的使用
https://www.cnblogs.com/renhui/p/8458802.html