codeforces 810B
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 kiproducts 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 ki products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·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.
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.
Print a single integer denoting the maximal number of products that shop can sell.
4 2
2 1
3 5
2 3
1 5
10
4 1
0 2
0 3
3 5
0 6
5
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 total1 + 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.
题意:n天里边选f天,将这f天的货物k变为2倍,使得销售的数量最大
分析:货物k如果比销售量l大,则不需要翻倍;如果货物k比销售量l小,则按k翻倍以后增加的销售量从大到小排序。因此现将k比l大的排到前面,然后将剩下的按增加的销售量从大到小排序即可。
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 struct st{ 4 long long k,l; 5 }; 6 st a[100005]; 7 st b[100005]; 8 bool cmp(st a,st b){ 9 return min(a.k*2,a.l)-min(a.k,a.l)>min(b.k*2,b.l)-min(b.k,b.l); 10 } 11 12 int main(){ 13 int n,f; 14 while(cin>>n>>f){ 15 16 long long sum=0; 17 for(int i=1;i<=n;i++){ 18 cin>>a[i].k>>a[i].l; 19 } 20 int ans=1; 21 int ans2=n; 22 for(int i=1;i<=n;i++){ 23 if(a[i].k>=a[i].l){ 24 b[ans].k=a[i].k; 25 b[ans].l=a[i].l; 26 ans++; 27 } 28 else { 29 b[ans2].k=a[i].k; 30 b[ans2].l=a[i].l; 31 ans2--; 32 } 33 } 34 sort(b+ans,b+1+n,cmp); 35 for(int i=1;i<ans;i++){ 36 sum=sum+b[i].l; 37 } 38 for(int i=ans;i<ans+f;i++){ 39 if(i>n) break; 40 sum=sum+min(b[i].k*2,b[i].l); 41 } 42 for(int i=ans+f;i<=n;i++){ 43 sum=sum+min(b[i].k,b[i].l); 44 } 45 cout<<sum<<endl; 46 47 } 48 return 0; 49 }