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. TensorFlow3学习笔记1

    1.简单实例:向量相加 下面我们通过两个向量相加的简单例子来看一下Tensorflow的基本用法. [1. 1. 1. 1.] + [2. 2. 2. 2.] = [3. 3. 3. 3.] impo ...

  2. request header....

    root@xxx# curl -i --get --include 'http://ali-barcode.showapi.com/barcode?code=6938166920785' -H 'Au ...

  3. python之 算法和数据结构

    什么是计算机科学? --首先明确的一点就是计算机科学不仅仅是对计算机的研究,虽然计算机在科学发展的过程中发挥了重大的作用,但是它只是一个工具,一个没有灵魂的工具而已,所谓的计算机科学实际上是对问题,解 ...

  4. HZOJ 辣鸡(ljh)

    题解?noipT1还需要题解?正解就是$n^2$大暴力. 考试的时候打了$n^2$的暴力,也想到了正解的优化,然而觉得它太麻烦了,而且$n^2$怎么优化也过不了50000啊,而且即使不优化前面30分我 ...

  5. pytorch学习笔记(九):PyTorch结构介绍

    PyTorch结构介绍对PyTorch架构的粗浅理解,不能保证完全正确,但是希望可以从更高层次上对PyTorch上有个整体把握.水平有限,如有错误,欢迎指错,谢谢! 几个重要的类型和数值相关的Tens ...

  6. poj 1066 Treasure Hunt (Geometry + BFS)

    1066 -- Treasure Hunt 题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方.可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁.问最少 ...

  7. H3C V.24接口线缆

  8. larave5.6 将Excel文件数据导入数据库代码实例

    <?php namespace App\Admin\Controllers; use App\AdminUser; use Illuminate\Http\Request; use Excel; ...

  9. windows 怎样关闭redis

    安装redis之后在命令行窗口中输入 redis-server redis.windows.conf 启动redis关闭命令行窗口就是关闭 redis.---redis作为windows服务启动方式r ...

  10. JS划重点——类和对象的不正经阐述

    JS划重点--类和对象的不正经阐述 /在JS 类里面函数也是一个对象,那么要创建一个对象就需要一个类,这个类可以由这个对牛逼的对象-函数来实现/ /首先是普罗大众都会的 工厂模式来创建一类/ func ...