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 ...
随机推荐
- 分享Mvc3+NInject+EF+LigerUI权限系统
分享Mvc3+NInject+EF+LigerUI权限系统 前段时间时不时看到有园友的分享权限系统,于是本人突发奇想,也想写一个玩玩,就利用晚上时间,陆陆续续花了一周多样子,写了如今这个权限系统, ...
- hdu 1998 奇数阶魔方(找规律+模拟)
应该不算太水吧. 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 ...
- Windows系统架构
操作系统模型 大多数操作系统中,都会把应用程序和内核代码分离运行在不同的模式下.内核模式访问系统数据和硬件,应用程序运行在没有特权的模式下(用户模式),只能使用有限的API,且不能直接访问硬件.当 ...
- jvm回收对象
jvm在判断对象死亡之前需要判断对象是否可到达,方法有引用计数算法和可达性分析算法,jvm采用的是后者.首先来了解一下这两种算法. 引用计数算法: 算法定义 为每个对象增加一个字段记录被引用的次数,并 ...
- H3 BPM 跨平台表单发起详解
1.1 目的 为方便技术对接服务,实现流程业务的跨平台对接,给出指引和帮助文档. 2 跨平台以Java为例调用.NET BPM表单流程全过程 2.1 双方系统确定接口协议规则 通信协议:基于soap通 ...
- 学习AngularJs:Directive指令用法(完整版)
这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...
- 微信小程序前置课程:flex布局(二)
原文:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到 ...
- Codeforces Round #386 (Div. 2)
迟到的一次比赛 最近状态很崩溃 网速很慢 然后前面五题看了都有打 但是 只有A B E 是过了的 是时候要反省一下 A.随便判断一下就好 最少的份数嘛 B.画出来之后是一下子左边一下子右边 打一个递归 ...
- SELinux开启和关闭
1.查看SELinux状态 /usr/sbin/sestatus -v 2.关闭SELinux (1)临时关闭(不用重启机器): setenforce 0 #设置SELinux成为permissive ...
- 最全js 放大镜效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...