Edward's Cola Plan

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status

Description

Edward is a tall, handsome and rich (a.k.a GaoShuaiFuGSF) 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个盖子可以换一个,盖子可以先借用再去换可乐。问:最多能获得盖子数。

 
思路:按Qi-Pi差值排序。临界点是Qi-Pi=M,这时候换不换中奖可乐,最后得到的盖子是一样的。所以二分下临界点的位置。然后答案就是临界点之前的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的更多相关文章

  1. 2014 Super Training #6 H Edward's Cola Plan --排序+二分

    原题: ZOJ 3676  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3676 题意:给每个朋友一瓶可乐,可乐有普通和高 ...

  2. English trip EM2-PE-5A Plan a dinner party Teacher:Lamb

    课上内容(Lesson) # Appetizer   ['æpə'taɪzɚ]  n. 开胃物,开胃食品 spinach salad  菠菜沙拉  # "p" 发b音 gazpac ...

  3. 每日英语: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 ...

  4. 测试计划(Test Plan)

    测试计划(Test Plan) 版权声明:本文为博主原创文章,未经博主允许不得转载. 测试计划的概念: 测试计划是一个文档,描述了进行测试的测试范围,测试策略和方法,测试资源和进度.是对整个测试活动进 ...

  5. SQL Tuning 基础概述02 - Explain plan的使用

    1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...

  6. POJ2175 Evacuation Plan

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4617   Accepted: 1218   ...

  7. New Plan!

    很久无写过blogs,荒废得差不多了,在博客园虽开bolg 5年多,但由于自己工作的问题,从开始的热情记录,到冷却冰冻,再到现在重拾起来,有一番感受:从大学刚毕业的制作网页菜鸟,开始接触DIV,CSS ...

  8. 分析oracle的执行计划(explain plan)并对对sql进行优化实践

    基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分 ...

  9. 【转】Oracle 执行计划(Explain Plan) 说明

    转自:http://blog.chinaunix.net/uid-21187846-id-3022916.html       如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQ ...

随机推荐

  1. Asp.net+jquery+ajaxpro异步仿Facebook纵向时间轴效果

    Asp.net+jquery+ajaxpro异步仿Facebook纵向时间轴效果 在一个项目中,用到了时间轴展示产品的开发进度,为了更好用户体验,想到了Facebook的timeline效果, 搜了一 ...

  2. Http Module 介绍(转载)

    Http Module 介绍 引言 Http 请求处理流程 和 Http Handler 介绍 这两篇文章里,我们首先了解了Http请求在服务器端的处理流程,随后我们知道Http请求最终会由实现了IH ...

  3. linux学习心得之目录树开端与/etc(图文)

    linux学习心得之目录树开端与/etc(图文) linux中“一切皆文件”,学习linux一年了,在学习过程中对目录树的一点心得,分享给大家,有不对的地方敬请斧正. 不多说了,先上图: 根目录: / ...

  4. 为什么不能在子类或外部发布C#事件

    为什么不能在子类或外部发布C#事件 背景 一个朋友问了一个问题:“为什么不能在子类或外部发布C#事件?”,我说我不知道,要看看生产的IL代码,下面我们看看. 测试 代码 1 using System; ...

  5. WCF服务承载

    WCF服务承载(笔记)   自托管(也做自承载) 承载 WCF 服务最灵活.最便捷的方法就是进行自承载.要能够自承载服务,必须满足两个条件.第一,需要 WCF 运行时:第二,需要可以承载 Servic ...

  6. 终于说再见了!Google Reader

    终于说再见了!Google Reader 投递人 itwriter 发布于 2013-07-02 13:28 评论(5) 有760人阅读  原文链接  [收藏]  « » 今天 15:00 左右,Go ...

  7. android 视图设置多个setTag数据

    1)在string.xml 文件中添加 <item tyoe="id" name = "tag_first" /> <item tyoe=&q ...

  8. the selected server is enabled,but is not configured properly.Deployment to it will not be permitted

    用Tomcat添加部署项目的时候报错: the selected server is enabled,but is not configured properly.Deployment to it w ...

  9. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic

    之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...

  10. java ArrayList的序列化分析

    一.绪论 所谓的JAVA序列化与反序列化,序列化就是将JAVA 对象以一种的形式保持,比如存放到硬盘,或是用于传输.反序列化是序列化的一个逆过程. JAVA规定被序列化的对象必须实现java.io.S ...