Codeforces Round #727 (Div. 2) D. PriceFixed(贪心)

传送门

题意

市场上有n种商品,每种商品的价格都是2。现在需要买第i种商品ai件。如果你已经买了bi件商品(不一定是这一种商品),那么此商品价格为1,求最小代价;

思路

把b[i]升序排列;
如果可以1价格买下,则直接买完;
如果不可以,则购买b[i]最大的直到可以1价格买下该商品或有别的商品开始打折

这里还采用了双指针,左指针为买到哪个商品,右指针为最大的b[i];

Code

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <stack>
#include <map>
#include <vector>
#include <set>
#include <iomanip>
#define hz020 return
#define mes memset
#define mec memcpy

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pii;

const int N = 200010;
const int null = 0x3f3f3f3f;
const ll mod = 998244353;

int n;

struct node
{
	ll a,b;
}s[N];

int gcd(int a,int b)
{
	return b ? gcd(b,a % b) : a;
}

bool cmp(node x,node y){return x.b < y.b;}

int main()
{
	cin >> n;

	ll qq = 0;
	for(int i = 1;i <= n;i ++) cin >> s[i].a >> s[i].b,qq += s[i].a;
	
	sort(s + 1,s + 1 + n,cmp);
	
	int l = 1,r = n;
	ll cnt = 0,sum = 0;
	while(cnt != qq)
	{
		if(s[l].b <= cnt)
		{
			cnt += s[l].a;
			sum += s[l].a;
			l ++;
		}
		else
		{
			ll idx = min(s[r].a,s[l].b - cnt);
			
			sum += idx * 2;
			s[r].a -= idx;
			cnt += idx;
			
			if(!s[r].a) r --;
		}
		
	}
	
	cout << sum << endl;
	
	hz020 0;
}  ```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半碗无糖蓝莓冻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值