Edward's Cola Plan
Description
Edward is a tall, handsome and rich (a.k.a GaoShuaiFu, GSF) student who studys in Zhejiang University. He also takes part in many competition of ACM-ICPC, and he is so smart that he almost wins the championship of World Finals recently! Unfortunately, he doesn't spent time in studying, so when the result of the final examination is published, he finds that he can't pass Mathematical Analysis! What a sad thing it is! But luckily, he still pass General Physics, so he decides to treat his N friends some Coca-Cola. The Coca-Cola is cheap - it only costs 3 yuan to buy one bottle.
What's more, he knows that the Cola company is holding a promotional activity again that customers can exchange one bottle of cola withM caps. And, because his friends are all good guys, they will give him some caps after drinking the Cola. After he deliberate for a long time, he thinks exactly one Coca-Cola for each person is enough. He is so happy!
But it's not so easy. The Cola company prints some signs on the Cola which is exchanged by caps, such as "Free!", "Gifts!", "Let's black someone!", and so on. We can call it Gift Cola. His friends have different attitude toward to different Cola. To more specific, if Edwardgives the ith friend a bottle of Normal Cola, he can get Pi caps back; and if he gives a bottle of Gift Cola, he can get Qi caps back.
Edward loves caps and he wants to get the most caps he can! However, the Cola Company often changes the variable M. So Edward asks you how many caps he can get at most in the given M after he treats all his friends.
Remember, Edward is GSF, so he has enough money to buy N bottles of cola, But he can not take the caps and then give the Cola to his friends; Moreover, he can not buy the Cola for himself, too. And he can borrow unlimited caps from someone (such as Dai), too. Of course, he needs to return all the caps he has borrowed at last.
Input
The input contains no more than 30 test cases. Please notice that there's no empty line between each test cases.
For each test case, first line has two integers N (1 ≤ N ≤ 100000) - the number of his friends and T (1 ≤ T ≤ 10000) - the number of the queries Edward asks.
The following N lines, each line contains two integers Pi and Qi (0 ≤ Pi, Qi ≤ 1000), which shows the ith friend will give back Pi caps forNormal Cola and Qi caps for Gift Cola.
The following T lines, each line contains an integer M (1 ≤ M ≤ 1000), which means Edward can use M caps to exchange for one Gift Cola.
Process to END_OF_FILE.
Output
For each test case, you should output T lines, each line has an integer - the maximal number of caps Edward can get after he treats all his friends.
Sample Input
2 1
2 0
0 2
2
Sample Output
2
题意:你给第i个人喝普通可乐他能给你Pi个盖子,给他中奖可乐他能给你Qi个盖子。中奖可乐用M个盖子可以换一个,盖子可以先借用再去换可乐。问:最多能获得盖子数。
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct node
{
int pi,qi;
int cha;
}; int pisum[+]={};
int qisum[+]={}; bool cmp(node a,node b)
{
return a.cha>b.cha;
} node kiss[+]; int main()
{
// freopen("input.txt","r",stdin);
int n,T;
while(scanf("%d%d",&n,&T)==){
for(int i=;i<n;i++){
scanf("%d%d",&kiss[i].pi,&kiss[i].qi);
kiss[i].cha=kiss[i].qi-kiss[i].pi;
}
sort(kiss,kiss+n,cmp);
pisum[]=kiss[].pi;
qisum[]=kiss[].qi;
for(int i=;i<n;i++){
pisum[i]=kiss[i].pi+pisum[i-];//分别求和
qisum[i]=kiss[i].qi+qisum[i-];
}
while(T){
int m;
scanf("%d",&m);
int l=,r=n-;
int ans=-;
while(l<=r){//二分查找分界点
int ss=(l+r)/;
if(kiss[ss].cha<m){
r=ss-;
}
else{
ans=ss;
l=ss+;
if(kiss[ss].cha==m){
break;
}
}
}
if(ans==-){//判断输出
printf("%d\n",pisum[n-]);
}
else{
printf("%d\n",qisum[ans]+pisum[n-]-pisum[ans]-(ans+)*m);
}
T--;
}
}
return ;
}
Edward's Cola Plan的更多相关文章
- 2014 Super Training #6 H Edward's Cola Plan --排序+二分
原题: ZOJ 3676 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3676 题意:给每个朋友一瓶可乐,可乐有普通和高 ...
- English trip EM2-PE-5A Plan a dinner party Teacher:Lamb
课上内容(Lesson) # Appetizer ['æpə'taɪzɚ] n. 开胃物,开胃食品 spinach salad 菠菜沙拉 # "p" 发b音 gazpac ...
- 每日英语:Google Scraps Plan to Build Hong Kong Data Center
Internet giant Google Inc. has scrapped a plan to build its own data center in Hong Kong and will in ...
- 测试计划(Test Plan)
测试计划(Test Plan) 版权声明:本文为博主原创文章,未经博主允许不得转载. 测试计划的概念: 测试计划是一个文档,描述了进行测试的测试范围,测试策略和方法,测试资源和进度.是对整个测试活动进 ...
- SQL Tuning 基础概述02 - Explain plan的使用
1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...
- POJ2175 Evacuation Plan
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4617 Accepted: 1218 ...
- New Plan!
很久无写过blogs,荒废得差不多了,在博客园虽开bolg 5年多,但由于自己工作的问题,从开始的热情记录,到冷却冰冻,再到现在重拾起来,有一番感受:从大学刚毕业的制作网页菜鸟,开始接触DIV,CSS ...
- 分析oracle的执行计划(explain plan)并对对sql进行优化实践
基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分 ...
- 【转】Oracle 执行计划(Explain Plan) 说明
转自:http://blog.chinaunix.net/uid-21187846-id-3022916.html 如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQ ...
随机推荐
- window.parent与window.opener的区别
有这样一个需求,弹出一个新窗口 并从该新页面的select选择框中选择需要的类别,再返回到之前的父窗口页面的某个文本框中.这里就要用到window.parent和window.opener 如题两种方 ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC
使用 NPC,NPCManager 在 XNA 中创建 NPC 平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐 ...
- Entity Framework 之 Code First
使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First [前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费 ...
- QQ空间如何显示相片
QQ空间如何显示相片 前言 此篇文章估计不会太长,有移除首页的风险,但是老夫(称老夫是因为我们真正的叶小钗其实都100多岁啦)是不会怕滴.所以,我来了哟! 题外话:今天我们一起还看了一道前端的面试题, ...
- Arduino 串口篇 Arduino发送二进制 send binary via RS232-to-USB to PC
有用的链接在这里:http://hi.baidu.com/mizuda/item/06b7fdc1d0e45a0ec710b2dd 更加详细的内容请查阅Arduino官方:http://arduino ...
- Linux kernel中网络设备的管理
kernel中使用net_device结构来描述网络设备,这个结构是网络驱动及接口层中最重要的结构.该结构不仅描述了接口方面的信息,还包括硬件信息,致使该结构很大很复杂.通过这个结构,内核在底层的网络 ...
- 通用高性能 Windows Socket 组件 HP-Socket v2.2.2 更新发布
HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...
- form表单重复提交,type=“button”和type=“submit”区别
公司测试提了一个项目后台在IE浏览器下(360,firefox就没问题)出现数据重复的问题,调试了好久终于发现问题所在,也不知道是谁写的代码,醉醉的.... 错误地点: <input type= ...
- 微信公众号开发之网页中及时获取当前用户Openid及注意事项
目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 (四)微信公众号开发之网页授权获取用户基本信息 (五)微信公众号开发之网页中及 ...
- windows环境下解决web服务假死的问题
最近在windows系统在部署web服务器,发现很不稳定.web服务有容易假死,改过配置换过各种web软件,如apache.nginx都不管用. 所以干脆做个简易的定时检测Web服务状态的软件.一旦w ...