传送门
题目描述
市场上有 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} 1≤n≤1×105,1≤ai,bi≤1×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
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/