CodeForces 1539D : PriceFixed 双指针 + 思维

传送门

题目描述

市场上有 n n n 种商品,每种商品的价格都是 2 2 2。现在你需要买第 i i i 种商品 a i a_i ai 件。但是对于第 i i i 种商品有一个属性 b i b_i bi,意味着如果你已经买了 b i b_i bi 件商品(不一定是这一种商品),那么此商品打折,价格会降到 1 1 1

你需要最小化你的代价。

其中 1 ≤ n ≤ 1 × 1 0 5 , 1 ≤ a i , b i ≤ 1 × 1 0 14 1\le n \le 1\times 10^5,1\le a_i,b_i\le 1\times 10^{14} 1n1×105,1ai,bi1×1014

分析

第一反应是按照 b i b_{i} bi从大到小的顺序进行排序,然后发现被样例 h a c k hack hack
因为有可能中途会大于排序末尾的 b b b
所以我们去用双指针求解,正向从大到小,因为他们半价的可能性最低,一旦大于最小的 b i b_{i} bi,就从后往前移动指针

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
	char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
	while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct Node{
	ll a,b;
	bool operator < (Node const &A)const{
		return b > A.b;
	}
}p[N];
int n;


int main() {
	read(n);
	for(int i = 0;i < n;i++) read(p[i].a),read(p[i].b);
	sort(p,p + n);
	int l = 0,r = n - 1;
	ll ans = 0,res = 0;
	while(l <= r){
		if(res + p[l].a <= p[r].b) res += p[l].a,ans += p[l].a * 2,l++;
		else{
			ll x = p[r].b - res;
			res = p[r].b,ans += x * 2;
			p[l].a -= x;
		}
		while(l <= r && p[r].b <= res) res += p[r].a,ans += p[r].a,r--;
	}
	dl(ans);
	return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值