首页 > 其他分享 >CF #727(div2)D. PriceFixed, 贪心,双指针

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

时间:2023-02-08 16:02:28浏览次数:47  
标签:PriceFixed product 727 items CF item rubles products she


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;
}


标签:PriceFixed,product,727,items,CF,item,rubles,products,she
From: https://blog.51cto.com/gwj1314/6044582

相关文章

  • CF #727(div2)C. Stable Groups,贪心,排序
    problemC.StableGroupstimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputTherearenstudentsnumerated......
  • CF1693 ABCD 题解
    题目链接:https://codeforces.com/contest/1693这场的题都非常好啊……因为现在是从div1开始做了,所以可能刚开始会有点吃力(这场我就会做一个1B呜呜呜)1A先把后缀的极......
  • 每日一道思维题——CF1761C - Set Construction
    题意:存在一个n×n的01矩阵(i,j)处值为1代表Ai 是Aj的真子集,求出这个集合A思路:我们在一开始的时候将每个位置赋初值,若i处的值是j的真子集将i处的值赋值给j代码:#inc......
  • CF1511G Chips on a Board
    CF1511GChipsonaBoard比较有启发性的一道题。询问是最简单的nim游戏,不难发现若一列上有两个棋子,那么这两个棋子对于答案是没有贡献的,因此可以令\(c_i\)表示第\(......
  • CF961E 另解
    一种不用思考怎么树状数组/主席树的笨蛋做法将题目的a[i]视作一个横坐标[i-1,i],纵坐标[0,a[i]]的矩形,我们要统计的是二维平面上同时存在的(x,y)与(y,x)对数。这样的对子......
  • CF14D题解
    CF14DTwoPaths题解题目链接传送门题意简述给定一棵树,找出两条不经过相同点的最长路径,使得他们的长度乘积最大。题目分析首先,如果在一棵树上,两条路径没有共同的点,那......
  • CF1785B Letter Exchange 题解(思维+模拟)
    题目链接难度:绿+。题意给定\(t\)组testcase,每组testcase如下。有\(m\)个长度为3的字符串,每个字符都是\(\text{w}\)、\(\text{i}\)、\(\text{n}\)中的一个,一......
  • [CF1190D] Tokitsukaze and Strange Rectangle
    题目描述Thereare$n$pointsontheplane,the$i$-thofwhichisat$(x_i,y_i)$.Tokitsukazewantstodrawastrangerectangularareaandpickallt......
  • [SA记录] CF1073G Yet Another LCP Problem
    一开始刚看这题时感觉什么思路都没有,不过后来做完P4248[AHOI2013]差异和P7409SvT后再看感觉稍微好一点。这3道题都是SA+单调栈的套路。这一种套路看起来似乎基本都是处......
  • CF1139D Steps to One
    StepstoOne初始给一个空的数列,每次随机从\(\left[1,m\right]\)中选一个整数加入数列末尾,求数列\(\gcd=1\)时的期望长度。这是一个期望加莫反的很有意思的题目......