贪心加排序

来源:互联网 发布:instagram软件下载 编辑:程序博客网 时间:2024/06/03 04:41
原题链接
B. Summer sell-off

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant.

Shop, where Noora is working, has a plan on the following n days. For each day sales manager knows exactly, that in i-th day ki products will be put up for sale and exactly li clients will come to the shop that day. Also, the manager is sure, that everyone, who comes to the shop, buys exactly one product or, if there aren't any left, leaves the shop without buying anything. Moreover, due to the short shelf-life of the products, manager established the following rule: if some part of the products left on the shelves at the end of the day, that products aren't kept on the next day and are sent to the dump.

For advertising purposes manager offered to start a sell-out in the shop. He asked Noora to choose any f days from n next for sell-outs. On each of f chosen days the number of products were put up for sale would be doubled. Thus, if on i-th day shop planned to put up for sale kiproducts and Noora has chosen this day for sell-out, shelves of the shop would keep ki products. Consequently, there is an opportunity to sell two times more products on days of sell-out.

Noora's task is to choose f days to maximize total number of sold products. She asks you to help her with such a difficult problem.

Input

The first line contains two integers n and f (1 ≤ n ≤ 105, 0 ≤ f ≤ n) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out.

Each line of the following n subsequent lines contains two integers ki, li (0 ≤ ki, li ≤ 109) denoting the number of products on the shelves of the shop on the i-th day and the number of clients that will come to the shop on i-th day.

Output

Print a single integer denoting the maximal number of products that shop can sell.

Examples
input
4 22 13 52 31 5
output
10
input
4 10 20 33 50 6
output
5
Note

In the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2, 6, 2, 2] respectively. So on the first day shop will sell 1 product, on the second — 5, on the third — 2, on the fourth — 2. In total 1 + 5 + 2 + 2 = 10 product units.

In the second example it is possible to sell 5 products, if you choose third day for sell-out.


这个题目什么意思呢,看了我三遍,TNND的,哎,还是要多做阅读理解呀哭

题意:

因为暑假来了嘛,有个叫Noora的小女孩吧,想去赚点钱,所以呢,就去找了份暑假工,在店里当了个售货员,这个时候,他老板就厉害啦,那个老板(叫manager吧)知道每天有多少产品摆出来,而且,还TM的还晓得每天会来好多顾客,WOCAO你说牛不牛逼,题目的那个例子就是说,第 i 天有Ki个商品摆出来卖,然后那个老板还知道在这天会有Li个顾客会来,而且每个顾客来了,都会只买一个商品,当然咯,如果东西卖完了,那顾客就没办法啦,就不会买任何东西,因为卖光了嘛,还有就是由于保质期的问题,如果当天的商品没卖完,不能留到第二天再去卖,只能扔了,第二天那个老板又会拿出一些商品给小女孩Noora来卖,然后那个老板就让Noora在接下来的n天里任意选f天,(f<=n),就是说在f天的这几天里,卖的商品Ki会翻倍,也就是2*Ki个商品了,这样如果这天顾客比较多的话,这就是个好机会去多卖出一些商品嘛,然后你的任务就是去如何选择这f天,让卖出的商品尽可能的多。
样例1
Input
4 2
2 1
3 5
2 3
1 5
output
10

4 代表 Noora总共要卖4天的商品,2就是Noora可以在这四天内任意选择两天,在这两天内商品的数量可以比原来翻倍。

goods         clients
第一天2件商品      1个人   --》  卖1
第二天3商品      5个人   --》  卖3
第三天2商品      3个人   --》  卖2
第四天1商品      5个人   --》  卖1
思路:     假如我们先不翻倍,看看能卖出多少商品,这时就是1+3+2+1=7件,可以卖出7件,然后我们把商品都翻倍,人不会翻倍哦

翻倍后:
goods         clients
第一天4件商品      1个人  --》  卖1
第一天6件商品      5个人  --》  卖5
第三天4件商品      3个人  --》  卖3
第三天2件商品      5个人  --》  卖2

然后我们可以看出,第一天翻倍后多卖0件,第二天多卖了2件,第三天多卖了1件,第四天多卖了1件,然后我们把多卖出的数量用个数组存起来,再sort一下从大到小排序取出前f个再加上不翻倍的时候能卖出去的商品数,也就是7+2+1=10   所以答案就是10嘛

代码如下:
#include <bits/stdc++.h>using namespace std;typedef long long LL;int main(){    LL n,f;    LL goods[100009];    LL client[100009];    LL more[100009];    LL this_[100009];    LL other[100009];    while(cin>>n>>f)    {        LL sum=0;        for(LL i=0;i<n;i++)        {            cin>>goods[i]>>client[i];            this_[i]=min(goods[i],client[i]);            sum+=this_[i];        }        for(LL i=0;i<n;i++)//翻倍后            goods[i]*=2;        for(LL i=0;i<n;i++)        {            other[i]=min(goods[i],client[i]);        }        for(LL i=0;i<n;i++)        {            more[i]=other[i]-this_[i];        }        sort(more,more+n,greater<LL>());//greater<LL>()  就是从大到小排列 ,当然你自己也可以定义一个cmp ,我比较懒,hh        for(LL i=0;i<f;i++)            sum+=more[i];                        cout<<sum<<endl;    }    return 0;}
// 还有不懂的可以私聊哦  QQ1305703713






原创粉丝点击