题目链接:
http://codeforces.com/problemset/problem/810/B
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 nnext 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
题目大意:
一个超市要卖n天一种商品,这种商品的保质期只有一天,第二天将不能够上架,然后要求你从这 n 天中任意选择 f 天,这 f 天上架的商品数量是原先的2倍,然后输出最多者n天最多能够卖多少件商品。首先输入n和f,然后n行,每一行两个整数,分别是该天商品上架数量,该天卖出数量。
解题思路:
贪心问题,变量cha存储 增倍前后数量差 ,然后按照差值从大到小排序,然后前 f 天为商品上架数量为原来的两倍的情况下卖出的数量,后几天为原来的上架数量的情况下卖出的数量,计算和即可。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct qq
{
long long k;
long long l;
long long cha; //保存 增倍前后的 数量差
}a[100005];
bool com(qq x,qq y) //按照 数量差 从大到小排序
{
return (x.cha>y.cha);
}
int main()
{
long long n,f;
while(cin>>n>>f)
{
for(int i=0;i<=n-1;i++)
{
cin>>a[i].k>>a[i].l;
a[i].cha=min(2*a[i].k,a[i].l)-min(a[i].k,a[i].l); //计算数量增倍前后的 数量差
}
long long sum=0;
sort(a,a+n,com); //按照 增倍前后数量差 排序
for(int i=0;i<=n-1;i++)
{
if(i+1<=f) //前f天,增倍
sum=sum+min(2*a[i].k,a[i].l);
else //后几天,不增倍
sum=sum+min(a[i].l,a[i].k);
}
cout<<sum<<endl;
}
return 0;
}