Codeforces Round #560 (Div. 3) Microtransactions
Codeforces Round #560 (Div. 3)
F2. Microtransactions (hard version)
题意:
现在有一个人他每天早上获得1块钱,现在有\(n\)种商品,每种商品最后需要\(k_i\)个;现在有\(m\)个打折信息,每个打折信息包含\(d_i,t_i\),表示第\(t_i\)种商品在第\(d_i\)天打折。如果没有打折的话一个商品卖两块钱,否则卖一块钱。
现在他可以在任意一天买任意多个商品,当然钱要够用,问最少需要多少天才能把所有需要的商品买完。
题解:
其实感觉这个没有多难,二分+贪心就好了。
因为可以在任意一天买任意多个商品,那么我们只需要在每个商品打折的最后一天买就行了,这肯定是最优的。
但怎么确定这里的最后一天,二分一下就行了。答案是满足单调性质的。
代码如下:
#include <bits/stdc++.h>
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 4e5 + 5;
int n, m;
int last[N];
vector <pii> v;
vector <int> Need, days[N];
bool check(int x) {
memset(last, 0, sizeof(last)) ;
for(int i = 0; i < m; i++) {
pii now = v[i] ;
if(now.first <= x) last[now.second] = max(last[now.second], now.first) ;
}
for(int i = 0; i <= x; i++) days[i].clear() ;
vector <int> k = Need;
for(int i = 1; i <= n; i++)
if(last[i] != 0) days[last[i]].push_back(i) ;
int money = 0;
for(int i = 1; i <= x; i++) {
money++;
if(i > 200000) {
money += x - i;
break ;
}
for(auto p : days[i]) {
if(money >= k[p]) {
money -= k[p] ;
k[p] = 0;
} else {
k[p] -= money ;
money = 0;
break ;
}
}
}
return accumulate(k.begin(), k.end(), 0) * 2 <= money ;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0) ;
cin >> n >> m;
Need.push_back(0) ;
for(int i = 1; i <= n; i++){
int tmp;
cin >> tmp;
Need.push_back(tmp) ;
}
for(int i = 0; i < m; i++) {
int d, t;
cin >> d >> t;
v.push_back(MP(d, t)) ;
}
int l = 1, r = 400001, mid ;
while(l < r) {
mid = (l + r) >> 1;
if(check(mid)) r = mid ;
else l = mid + 1;
}
cout << l;
return 0;
}
Codeforces Round #560 (Div. 3) Microtransactions的更多相关文章
- A. Remainder Codeforces Round #560 (Div. 3)
A. Remainder Codeforces Round #560 (Div. 3) You are given a huge decimal number consisting of nn dig ...
- Codeforces Round #560 Div. 3
题目链接:戳我 于是...风浔凌弱菜又去写了一场div.3 总的来说,真的是比较简单.......就是.......不开long long见祖宗 贴上题解-- A 给定一个数,为01串,每次可以翻转一 ...
- Codeforces Round #560 (Div. 3)A-E
A. Remainder output standard output You are given a huge decimal number consisting of nn digits. It ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- .net for TCP服务端 && 客户端
关键代码 详细代码请看示例代码 Service //创建套接字 IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ipaddress), port); / ...
- 小技巧——解决Github项目clone慢的问题
设置github的项目git命令走sock代理 git config --global http.https://github.com.proxy socks5://127.0.0.1:1086(so ...
- pdf转换成jpg不清晰怎么办
用Photoshop打开pdf文件,然后用“文件”-“另存为”“JPG”.
- html5 canvas手写字代码(兼容手机端)
html5 canvas手写字代码(兼容手机端) <pre><!DOCTYPE html><html><head> <title>画板实验& ...
- 【剑指offer】平衡二叉树
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 分析:采用后序遍历的方式判断左右子树的高度差是否大于1 class Solution { public: bool flag; int f(T ...
- 第一个python小脚本
第一个python小实验 前言 作为一个工作1年的linux运维搬砖师来说,发现没点开发能力真的是不好混啊.于是下定决心学习python! 直接上刚写的语句(大神莫鄙视) 通过控制台输入一个账号密码, ...
- Windows10下QT5.13.2安装mingw64/MYSQL8.0驱动
开始之前,先将编译器的路径添加到系统环境变量. 我的QT所以sql驱动是在下面这个目录中(大家在自己Qt的安装目录找到对应的文件夹就行,下面的路径也是如此), E:\qt\5.13.2\mingw73 ...
- Python解析 算数表达式求值 栈的使用
使用Python实现一种算数表达式求值的算法,模拟这种使用栈的方式,这是由E.W.Dijkstra在20世纪60年代发明的一种非常简单的算法.代码模拟仅仅表现一种编程思想,代码的逻辑并不完全: if ...
- Expected linebreaks to be 'LF' but found 'CRLF'.
解决方法 在rules中加入 "linebreak-style": [0 ,"error", "windows"], 如果你需要知道原理,请 ...
- C#JsonConvert.DeserializeObject反序列化json字符
需求:需要把第一个id替换掉,在序列化成json dynamic dyn = Newtonsoft.Json.JsonConvert.DeserializeObject(json); foreach ...