1.题目描述:
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 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 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.
2.题意概述:
每天有ki件物品,你知道每天能卖掉li件; 然后让你选f天; 这f天,可以将ki乘上2; 每天结束之后物品清空; 问你最多能卖多少件物品?
3.解题思路:
如果当前物品ki比当天来访顾客li要大,那么肯定没必要翻倍,因为翻倍照样是卖那么多,对答案没有贡献。
如果当前物品k1比当天来访顾客li要小,考虑贪心,最开始贪心思路想的是对结果min(2k, l)进行排序贪心,但是这种思路是错的,因为如果某天被加倍,它的贡献应该是min(2k, l)-sell,这样才能保证每次贪心选的都是最优的,所以应该先预处理全部为加倍的值,再对那些li>=ki的商品按min(2k, l)-sell差值从大到小贪心地选f个。
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100010
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
ll k, l, ans;
friend bool operator< (node a, node b)
{
return a.ans > b.ans;
}
} p[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, f;
while (~scanf("%d%d", &n, &f))
{
int cnt = 0;
ll sum = 0;
for (int i = 0; i < n; i++)
{
ll l, k;
scanf("%I64d%I64d", &k, &l);
ll sell = min(l, k);
sum += sell;
if (l > k)
{
p[cnt].k = k;
p[cnt].l = l;
p[cnt].ans = 2 * k;
if (p[cnt].ans > l)
p[cnt].ans = l;
p[cnt++].ans -= sell;
}
}
sort(p, p + cnt);
for (int i = 0; i < f; i++)
sum += p[i].ans;
printf("%I64d\n", sum);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}