zoukankan      html  css  js  c++  java
  • Edward's Cola Plan

     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代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 using namespace std;
     7 struct node
     8 {
     9     int pi,qi;
    10     int cha;
    11 };
    12 
    13 int pisum[100000+5]={0};
    14 int qisum[100000+5]={0};
    15 
    16 bool cmp(node a,node b)
    17 {
    18     return a.cha>b.cha;
    19 }
    20 
    21 node kiss[100000+5];
    22 
    23 int main()
    24 {
    25 //    freopen("input.txt","r",stdin);
    26     int n,T;
    27     while(scanf("%d%d",&n,&T)==2){
    28         for(int i=0;i<n;i++){
    29             scanf("%d%d",&kiss[i].pi,&kiss[i].qi);
    30             kiss[i].cha=kiss[i].qi-kiss[i].pi;
    31         }
    32         sort(kiss,kiss+n,cmp);
    33         pisum[0]=kiss[0].pi;
    34         qisum[0]=kiss[0].qi;
    35         for(int i=1;i<n;i++){
    36             pisum[i]=kiss[i].pi+pisum[i-1];//分别求和
    37             qisum[i]=kiss[i].qi+qisum[i-1];
    38         }
    39         while(T){
    40             int m;
    41             scanf("%d",&m);
    42             int l=0,r=n-1;
    43             int ans=-1;
    44             while(l<=r){//二分查找分界点
    45                 int ss=(l+r)/2;
    46                 if(kiss[ss].cha<m){
    47                     r=ss-1;
    48                 }
    49                 else{
    50                     ans=ss;
    51                     l=ss+1;
    52                     if(kiss[ss].cha==m){
    53                         break;
    54                     }
    55                 }
    56             }
    57             if(ans==-1){//判断输出
    58                 printf("%d
    ",pisum[n-1]);
    59             }
    60             else{
    61                 printf("%d
    ",qisum[ans]+pisum[n-1]-pisum[ans]-(ans+1)*m);
    62             }
    63             T--;
    64         }
    65     }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    80x86的保护模式
    计算机二进制的表示
    操作系统基本知识(一)
    记录一次在安装双系统的过程(先有debian, 后加windows 8.1)
    LitePal + Gson + Volley的ORM框架尝试方案
    如何使用DDMS Heap查看Android应用内存情况
    测试驱动开发的第一个例子---我的毕业设计
    策略模式的孪生兄弟---状态模式
    面试常备---栈和队列总结篇
    面试常备题---二叉树总结篇
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3455121.html
Copyright © 2011-2022 走看看