CF #727(div2)D. PriceFixed, 贪心,双指针

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF #727(div2)D. PriceFixed, 贪心,双指针相关的知识,希望对你有一定的参考价值。

problem

D. PriceFixed
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store — “PriceFixed”. Here are some rules of that store:

The store has an infinite number of items of every product.
All products have the same price: 2 rubles per item.
For every product i there is a discount for experienced buyers: if you buy bi items of products (of any type, not necessarily type i), then for all future purchases of the i-th product there is a 50% discount (so you can buy an item of the i-th product for 1 ruble!).
Lena needs to buy n products: she must purchase at least ai items of the i-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.

Input
The first line contains a single integer n (1≤n≤100000) — the number of products.

Each of next n lines contains a product description. Each description consists of two integers ai and bi (1≤ai≤1014, 1≤bi≤1014) — the required number of the i-th product and how many products you need to buy to get the discount on the i-th product.

The sum of all ai does not exceed 1014.

Output
Output the minimum sum that Lena needs to make all purchases.

Examples
inputCopy
3
3 4
1 3
1 5
outputCopy
8
inputCopy
5
2 7
2 8
1 2
2 4
1 8
outputCopy
12
Note
In the first example, Lena can purchase the products in the following way:

one item of product 3 for 2 rubles,
one item of product 1 for 2 rubles,
one item of product 1 for 2 rubles,
one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).
In total, she spends 8 rubles. It can be proved that it is impossible to spend less.

In the second example Lena can purchase the products in the following way:

one item of product 1 for 2 rubles,
two items of product 2 for 2 rubles for each,
one item of product 5 for 2 rubles,
one item of product 3 for 1 ruble,
two items of product 4 for 1 ruble for each,
one item of product 1 for 1 ruble.
In total, she spends 12 rubles.

solution

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
struct node{LL x,y;}a[maxn];
bool cmp(node a, node b){return a.y>b.y;}
int main(){
	//ios::sync_with_stdio(false);
	int n;  scanf("%d", &n);//cin>>n;
	for(int i = 1; i <= n; i++){
		//cin>>a[i].x>>a[i].y;
		scanf("%lld%lld", &a[i].x, &a[i].y);
	}
	sort(a+1,a+n+1,cmp);
	int l = 1, r = n;
	LL res = 0, now = 0;
	while(l <= r){
		if(a[r].y > now + a[l].x){
			now += a[l].x;
			res += 2*a[l].x;
			l++;
		}else{
			LL tmp = a[r].y-now;
			res += 2*tmp;
			now += tmp;
			a[l].x -= tmp;
			while(a[r].y<=now && l<=r){
				res += a[r].x;
				now += a[r].x;
				r--;
			}
			if(l>r)break;
		}
	}
	cout<<res<<"\\n";
	return 0;
}


以上是关于CF #727(div2)D. PriceFixed, 贪心,双指针的主要内容,如果未能解决你的问题,请参考以下文章

CF #727(div2)C. Stable Groups,贪心,排序

CF #727(div2)A. Contest Start,找规律,数学

CF 301div2 D. Bad Luck Island

CF#468 div2 D. Peculiar apple-tree(思维)

CF #261 div2 D. Pashmak and Parmida&#39;s problem (树状数组版)

codeforces cf educatonal round 57(div2) D. Easy Problem