AGC018C Coins (set)
题目大意:
给出n个人,每个人手里都有xi个金牌,yi个银牌,ci个铜牌。
你需要选出X个人,拿走他们手里的金牌,选出Y个人,拿走他们手里的银牌,选出Z个人,拿走他们手里的铜牌
X+Y+Z = n。并且选的这X,Y,Z个人里不能有重复的。
题解:
不妨先考虑只有金牌和银牌怎么做。
如果只有两种,就变成很简单的贪心了,依据每个人手中的金牌数x减去银牌数y然后排序,按差值选即可
现在又多了一个铜牌,我们还是按照这个思路
依据每个人手中的金牌数x减去银牌数y然后排序。
然后我们考虑如何分配我们的X和Y。
注意到,排完序以后,这里有一个奇妙的性质出现了
这个性质简单来说就是,银牌从排序靠前的元素选,金牌从排序靠后的元素选,这样做会更优
或者这样说,银牌和金牌的选择是互不交叉的。(考虑如果有交叉,那么相互交换一下会更优)
那么我们就可以这样求出答案
枚举一个k,从前k个元素中选出Y个银牌,从后n-k个元素中选出X个金牌,其他都是选铜牌
这样我们只需要把k从Y枚举到n-X就可以了
那铜牌的影响怎么处理呢?
就让金牌数和银牌数直接减去铜牌数,然后最后的答案加上铜牌数,这样所有的铜牌数就变成了0
就可以不考虑它的影响了。
对每一个k,答案就是前k个元素中最大的Y个银牌数和,加上后n-k个元素中最大的X个银牌数和
这个显然可以用set来维护。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
const int maxn = 1e5 + ;
typedef pair<int, int> PII;
bool cmp(const PII &A, const PII &B){
return A.fi - A.se < B.fi - B.se;
}
PII a[maxn];
set<int> S[];
map<int, int> H[];
int ssz[], sz[];
long long ans = , temp, ans1[maxn], ans2[maxn];
void Erase(int x, int ty){
if(H[ty][x] == ) S[ty].erase(x);
H[ty][x]--;
ans -= x;
ssz[ty]--;
}
void Insert(int x, int ty){
if(H[ty][x] == ) S[ty].insert(x);
H[ty][x]++;
ans += x;
ssz[ty]++;
} void S_Insert(int x, int ty){
if(ssz[ty] == sz[ty]){
int y = *S[ty].begin();
if(y > x) return;
Erase(y, ty);
}
Insert(x, ty);
} int main()
{
int X, Y, Z, x, y, z;
cin>>X>>Y>>Z;
int n = X+Y+Z;
for(int i = ; i <= n; i++){
scanf("%d %d %d", &x, &y, &z);
x -= z;
y -= z;
ans += z;
a[i].fi = x;
a[i].se = y;
}
temp = ans;
ans = ;
sort(a+, a++n, cmp);
sz[] = Y; sz[] = X;
for(int i = ; i <= Y; i++) S_Insert(a[i].se, );
ans1[Y] = ans;
for(int i = Y+; i <= n-X; i++){
S_Insert(a[i].se, );
ans1[i] = ans;
}
ans = ;
for(int i = n; i >= n-X+; i--) S_Insert(a[i].fi, );
ans2[n-X+] = ans;
for(int i = n-X; i >= Y+; i--){
S_Insert(a[i].fi, );
ans2[i] = ans;
}
ans = -1e18;
for(int i = Y; i <= n-X; i++) ans = max(ans, ans1[i]+ans2[i+]);
cout<<ans+temp<<endl;
return ;
}
AGC018C Coins (set)的更多相关文章
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- csuoj 1119: Collecting Coins
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec Memo ...
- Coins
Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hi ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
随机推荐
- linux系统基础之---账号管理(基于centos7.4 1708)
- git stash应用
今天在看一个bug,之前一个分支的版本是正常的,在新的分支上上加了很多日志没找到原因,希望回溯到之前的版本,确定下从哪个提交引入的问题,但是还不想把现在的修改提交,也不希望在Git上看到当前修改的版本 ...
- DevOps - 项目构建 - Maven
Maven介绍Apache Maven是一个创新的软件项目管理和综合工具.Maven提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文件.Ma ...
- java程序执行系统命令
String cmd="orakill orcl 1233";//解锁数据库表 Process proc = Runtime.getRuntime().exec(cmd);
- json_decode结果为null的几种原因
值只能是UTF-8编码,元素最后不能有逗号,元素不能使用单引号,元素值中间不能有空格和n.
- linux 用户 用户组
useradd -m -G sudo zhangxiao passwd zhangxiao
- redis搭建
redis 1.简介.安装 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.redis 和 Memcached 缓存服务很像 ...
- hive 学习系列五(hive 和elasticsearch 的交互,很详细哦,我又来吹liubi了)
hive 操作elasticsearch 一,从hive 表格向elasticsearch 导入数据 1,首先,创建elasticsearch 索引,索引如下 curl -XPUT '10.81.17 ...
- 分布式系统的CAP(Redis)
CAP理论就是说在分布式存储系统中,最多只能实现上面的两点.而由于当前的网络硬件肯定会出现延迟丢包等问题,所以 分区容忍性是我们必须需要实现的. 所以我们只能在一致性和可用性之间进行权衡,没有NoSQ ...
- [BZOJ1455]罗马游戏(左偏树)
用并查集和左偏树维护士兵的关系 Code #include <cstdio> #include <algorithm> #define N 1000010 using name ...