codeforces 1165F1/F2 二分好题

时间:2023-03-08 21:37:17

Codeforces 1165F1/F2 二分好题

传送门:https://codeforces.com/contest/1165/problem/F2

题意:

有n种物品,你对于第i个物品,你需要买\(k_i\)个,每个物品在非打折日买是2块钱,在打折日买是1块钱,每天你可以赚一块钱,现在告诉你一共有m个打折日,在第\(d_i\)天第\(t_i\)种物品打折,问你你最少需要多少天可以买完你需要的物品

题解:

二分

思路是这样的

根据题目的题意,你最多打工4e5天就可以买完所有的物品,所以我们可以二分天数然后check当前天数是否能买完所有的物品即可

怎么check呢

对于打折的日子,我们记下每种物品可以在mid天内可以打折的日子的最大值,然后在最后的这个物品的打折日将这个物品买

这样是一个小小的贪心

因为对于一个可以买的物品,我们不会花费更多的钱去买他

最后记录一个花费,和打工这么多天赚的钱比较一下即可

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int k[maxn];
int need[maxn];
vector<int> vec;
pair<int, int> q[maxn];
int n, m;
bool check(int day) {
vec.resize(n, -1);
for (int i = 0; i < m; ++i) {
if (q[i].first <= day) {
vec[q[i].second] = max(vec[q[i].second], q[i].first);
}
}
vector<vector<int>> offer(200001);
for (int i = 0; i < n; ++i) {
if (vec[i] != -1) {
offer[vec[i]].push_back(i);
}
}
for(int i = 0; i < n; i++) {
need[i] = k[i];
}
int cur_money = 0;
for (int i = 0; i <= day; ++i) {
++cur_money;
if (i > 200000) continue;
for (auto it : offer[i]) {
if (cur_money >= need[it]) {
cur_money -= need[it];
need[it] = 0;
} else {
need[it] -= cur_money;
cur_money = 0;
break;
}
}
}
int tot = 0;
for(int i = 0; i < n; i++) {
tot += need[i];
}
return tot * 2 <= cur_money; }
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d%d", &n, &m);
int sum = 0;
for(int i = 0; i < n; i++) {
// scanf("%d", &k[i]);
cin >> k[i];
sum += k[i];
}
for(int i = 0; i < m; i++) {
scanf("%d%d", &q[i].first, &q[i].second);
--q[i].first;
--q[i].second;
}
int L = 0, R = 400000;
int ans = 0;
while(L <= R) {
int mid = (L + R) >> 1;
// debug3(L, R, mid);
if(check(mid)) {
ans = mid;
R = mid - 1;
} else {
L = mid + 1;
}
// debug1(ans);
}
printf("%d\n", ans + 1);
return 0;
}