codeforces 1165F1/F2 二分好题
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;
}
codeforces 1165F1/F2 二分好题的更多相关文章
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]
补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...
- codeforces#1165 F2. Microtransactions (hard version) (二分+贪心)
题目链接: https://codeforces.com/contest/1165/problem/F2 题意: 需要买$n$种物品,每种物品$k_i$个,每个物品需要两个硬币 每天获得一个硬币 有$ ...
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
- CodeForces 163B Lemmings 二分
Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...
随机推荐
- chrome://inspect调试html页面空白,DOM无法加载的解决方案
chrome://inspect调试html页面空白,DOM无法加载的解决方案 先描述一下问题 有一段时间没碰huilder hybird app 开发了,今天调试的时候 chrome://inspe ...
- X-editable 不能二次初始化的问题解决方案
最近用到了 X-editable 可编辑表格插件,发现了一个头疼的问题,X-editable 不能对同一个 <a> 元素二次初始化. 如下代码举例:在页面加载完成时,用“数组1”填充一个下 ...
- 【C++】去除vector里重复元素的方法比较
背景:构造一个无重复的白名单,之后要在里面进行二分查找.故要求名单有序,且无重复,并且要进行二分查找,所以要采用有:随机访问迭代器类型的容器.这类容器有vector,array,deque.显然要ve ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十二章:几何着色器(The Geometry Shader)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十二章:几何着色器(The Geometry Shader) 代码工 ...
- iOS:学习runtime的理解和心得
http://www.cocoachina.com/ios/20150901/13173.html 作者:兴宇是谁 授权本站转载. Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门 ...
- 2019-9-23-dotnet-判断特定进程存在方法
title author date CreateTime categories dotnet 判断特定进程存在方法 lindexi 2019-09-23 16:20:42 +0800 2019-09- ...
- javascript中数字的一些常规操作
1,禁止输入 - (减号.负号) // html <input type="number" class="no-negative"> // js $ ...
- Linux 中 grep 命令的 12 个实践例子
2017-10-17 Linux小管家 你是否遇到过需要在文件中查找一个特定的字符串或者样式,但是不知道从哪儿开始?那么,就请grep来帮你吧. grep是每个Linux发行版都预装的一个强有力的文件 ...
- QT 开发ros gui过程中遇到:error: catkin_package() include dir 'include' does not exist relative to '/home/jun/catkin_ws/src/qt_ros_test' /opt/ros/kinetic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_p
这是因为在ros工作空间的包中没有include文件夹造成的,所以在该路径下创建include的文件夹,问题就解决了.
- react项目安装及运行
博客地址 :https://www.cnblogs.com/sandraryan/ 安装node ,有就跳过. node.js官网:https://nodejs.org/en/ 终端用node -v ...