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 二分好题的更多相关文章

  1. Codeforces#441 Div.2 四小题

    Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...

  2. You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

    补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...

  3. codeforces#1165 F2. Microtransactions (hard version) (二分+贪心)

    题目链接: https://codeforces.com/contest/1165/problem/F2 题意: 需要买$n$种物品,每种物品$k_i$个,每个物品需要两个硬币 每天获得一个硬币 有$ ...

  4. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  6. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  7. codeforces 732D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...

  8. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

  9. CodeForces 163B Lemmings 二分

    Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...

随机推荐

  1. Activity基本类分析

    先上一张类图. Android源码分析的文章在网络上已经很多, 有些知识点阅读完之后能够基本理解其框架,但是由于不是这些代码的维护者,所以过一段时间后就忘记的差不多了,又需要反复学习. 所以在读完文章 ...

  2. SSH applicationContext.xml import异常

    近期在项目上,遇到了一个问题.在配置applicationContext.xml使用<import>标签引入其他的xml文件时,导致项目启动时过慢.有时还会引起启动异常.后来查到是xml文 ...

  3. mogodb 修改字段属性

    修改为decimal类型 db.shopgoods.find({'Pricing.Detail':{$type:2}}).forEach(function(x){x.Pricing.Detail=Nu ...

  4. C++笔记:面向对象编程(Handle类)

    句柄类 句柄类的出现是为了解决用户使用指针时须要控制指针的载入和释放的问题. 用指针訪问对象非常easy出现悬垂指针或者内存泄漏的问题. 为了解决这些问题,有很多方法能够使用,句柄类就是当中之中的一个 ...

  5. java基础-内存分配

    1.java运行时的数据区:程序计数器.方法区.虚拟机栈.本地方法栈.堆 ①.程序计数器:一块较小的内存空间,可看作当前线程所执行的字节码的行号指示器 ②.java虚拟机栈:与程序计数器一样,也是线程 ...

  6. oracle函数 sysdate

    [功能]:返回当前日期. [参数]:没有参数,没有括号 [返回]:日期 [示例]select sysdate  hz from dual; 返回:2008-11-5

  7. php实现第三方登录

    1. oAuth2.0原理 网站为了方便用户快速的登录系统,都会提供使用知名的第三方平台账号进行快速登录的功能,第三方登录都是基于oAuth2.0标准来实现的.下面详细分析[基于账号密码授权]和[基于 ...

  8. 在线url网址编码、解码

    >>在线url网址编码.解码<<

  9. 独家 | TensorFlow 2.0将把Eager Execution变为默认执行模式,你该转向动态计算图了

    机器之心报道 作者:邱陆陆 8 月中旬,谷歌大脑成员 Martin Wicke 在一封公开邮件中宣布,新版本开源框架——TensorFlow 2.0 预览版将在年底之前正式发布.今日,在上海谷歌开发者 ...

  10. hdu 1277 全文检索 (直接映射查找 || 自动机)

    Problem - 1277 无聊做水题的时候发现的一道题目.这道题第一反应可以用自动机来解决.当然,条件是各种限制,从而导致可以用直接映射标记的方法来搜索.具体的做法就像RK算法一样,将字符串has ...