D. PriceFixed(贪心)
题目
题目描述
Lena is the most economicalgirl 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 ii there is a discount for experienced buyers: if you buy bibi 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 ii-th product for 1 ruble!).
Lena needs to buy nn products: she must purchase at least aiai items of the ii-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.
The first line contains a single integer nn (1≤n≤100000) — the number of products.
Each of next nn lines contains a product description. Each description consists of two integers aiai and bibi (1≤ai≤10^14, 1≤bi≤10^14) — the required number of the ii-th product and how many products you need to buy to get the discount on the ii-th product.
The sum of all aiai does not exceed 10^14.
Output the minimum sum that Lena needs to make all purchases.
3 3 4 1 3 1 5
8
5 2 7 2 8 1 2 2 4 1 8
12
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.
题目大意
市场上有 n 种商品,每种商品的价格都是 2。现在你需要买第 i 种商品 a i] 件。但是对于第 i 种商品有一个属性 b ,意味着如果你已经买了 b 件商品(不一定是这一种商品),那么此商品打折,价格会降到 1。
题目解析
这个题是一个贪心,首先满足那些b[i]小的,b]i]小的,打折肯容易,要求数大的肯定难打折,那么贪心的去选取,先对b[i]非递减排个序,然后枚举如果当前的购买数满足打折条件那么就贪心买当前的商品,否则就拿后面的商品和当前的还需多少件商品数,取min,然后修改后面的值,这样能得到最优解
代码
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn=1e5+100; struct node{ ll req,lim; bool friend operator<(node a,node b){ return a.lim<b.lim;//按打折数目从小到大排序 } }a[maxn]; int main(){ int n; cin>>n; ll sum=0; for(int i=1;i<=n;i++){ cin>>a[i].req>>a[i].lim; } sort(a+1,a+n+1); int l=1,r=n; ll ans=0,cnt=0;//贪心主要是让l先打折 while(r>=l){ if(a[l].lim<=cnt){//够打折 cnt+=a[l].req; ans+=a[l].req; l++; } else{ ll m=min(a[r].req,a[l].lim-cnt);//这个a[l].lim-cnt是正好凑够l的打折 cnt+=m; ans+=2*m; a[r].req-=m; if(a[r].req==0){ r--; } } } cout<<ans<<endl; }