2014 Super Training #6 H Edward's Cola Plan --排序+二分
原题: ZOJ 3676 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3676
题意:给每个朋友一瓶可乐,可乐有普通和高级两种,每个朋友只能喝一瓶可乐,喝普通可乐的朋友会给你P个瓶盖,喝高级可乐的朋友会给你Q个瓶盖。问最多能得到多少个瓶盖。瓶盖可以借。
解法:因为瓶盖可以借任意多个,所以按Q-P排序即可,二分临界点Q-P=0的点,即Q-P<m的让他和普通可乐,Q-P>m的喝高级可乐,Q-P=m的无所谓喝什么。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define N 100007 struct node
{
int P,Q;
int dif;
}p[N]; int SP[N],SQ[N]; int cmp(node ka,node kb)
{
return ka.dif < kb.dif;
} int main()
{
int n,m,k;
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
SP[] = SQ[] = ;
for(i=;i<=n;i++)
{
scanf("%d%d",&p[i].P,&p[i].Q);
p[i].dif = p[i].Q-p[i].P;
}
sort(p+,p+n+,cmp);
for(i=;i<=n;i++)
{
SP[i] = SP[i-]+p[i].P;
SQ[i] = SQ[i-]+p[i].Q;
}
for(i=;i<m;i++)
{
scanf("%d",&k);
int low = ;
int high = n;
int res = -;
while(low <= high)
{
int mid = (low+high)/;
if(p[mid].dif < k)
low = mid + ;
else
{
res = mid;
high = mid - ;
}
}
if(res == -) //如果全小于k,则全喝普通的
printf("%d\n",SP[n]);
else
printf("%d\n",SP[res-]+SQ[n]-SQ[res-]-(n-res+)*k);
}
}
return ;
}
2014 Super Training #6 H Edward's Cola Plan --排序+二分的更多相关文章
- 2014 Super Training #3 H Tmutarakan Exams --容斥原理
原题: URAL 1091 http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...
- Edward's Cola Plan
Edward's Cola Plan Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- 2014 Super Training #10 G Nostop --矩阵快速幂
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
- 2014 Super Training #4 D Paint the Grid Again --模拟
原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结 ...
- 2014 Super Training #1 C Ice-sugar Gourd 模拟,扫描线
原题 HDU 3363 http://acm.hdu.edu.cn/showproblem.php?pid=3363 给你一个串,串中有H跟T两种字符,然后切任意刀,使得能把H跟T各自分为原来的一半. ...
- 2014 Super Training #8 B Consecutive Blocks --排序+贪心
当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...
- 2014 Super Training #8 A Gears --并查集
题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性). ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- 2014 Super Training #9 C E - Cup 2 --记忆化搜索
原题:ZOJ 3681 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3681 题意:给一个m,n,m表示m个人,可以把m个 ...
随机推荐
- linux下的inode记录
我们经常在Linux下可以看到inode,都不知道是什么东东,那么我们现在来慢慢了解下. 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做&q ...
- linux下mysql忘记root密码解决方法
如果使用 MySQL 数据库忘记了root账号密码,可以通过调节配置文件,跳过密码的方式登数据库, 在数据库里面修改账号密码,一般默认的账号是 root 1.编辑 MySQL 配置文件 my.cnf ...
- Delphi又要换东家了
前几天听到这个消息,搞个FMX出来,64位还没搞清楚,又开始折腾了!http://www.deltics.co.nz/blog/posts/2371 No Seriously – Let’s Buy ...
- css超出2行部分省略号...
今天做东西,遇到了这个问题,百度后总结得到了这个结果. 首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用 ...
- 2004-输入一个百分制的成绩t,将其转换成对应的等级
,具体转换规则如下: 90~100为A; 80~89为B; 70~79为C; 60~69为D; 0~59为E; 如果输入数据不在0~100范围内,请输出一行:“Score is error!”. 这题 ...
- 调用存储过程从EntityFramework
Prerequisites The prerequisite for running these examples are the following sample tables with test ...
- DAX 2009 for Retail's P job does not work after restoring AX database from another environment.
This time, it's P job. We already re-configured profiles, distribution locations, distribution grou ...
- 公司outing选项
Sign up: 2014 Summer Outing 请您从以下三个方案中选择您最感兴趣的一个项目, 如果您不能参加此次summer outing, 请选择"遗憾放弃"- ...
- 字符串匹配--Karp-Rabin算法
主要特征 1.使用hash函数 2.预处理阶段时间复杂度O(m),常量空间 3.查找阶段时间复杂度O(mn) 4.期望运行时间:O(n+m) 本文地址:http://www.cnblogs.com/a ...
- IOS 杂笔-2(协议)
1.协议的定义 @protocol 协议名称 <NSObject> //方法声明列表 @end; 2.如何遵守协议 (1)类遵守协议 @protocol 类名:父类名 <协议名称1, ...