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的更多相关文章

  1. 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 ...

  2. Codeforces Round #560 Div. 3

    题目链接:戳我 于是...风浔凌弱菜又去写了一场div.3 总的来说,真的是比较简单.......就是.......不开long long见祖宗 贴上题解-- A 给定一个数,为01串,每次可以翻转一 ...

  3. Codeforces Round #560 (Div. 3)A-E

    A. Remainder output standard output You are given a huge decimal number consisting of nn digits. It ...

  4. 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 ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. 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 ...

随机推荐

  1. TCP/IP和OSI4层、7层协议介绍

    1.TCP/IP全称:Transmission Control Protocol / Internet Protocol 中文翻译:传输控制协议 / 互联网协议 2.OSI4层.7层模型:

  2. DRF概述

    目录 一. REST 1. 什么是编程? 2. 什么是REST? 二. 知识准备 1. CBV(class based view) 2. 类方法 classmethod和classonlymethod ...

  3. web记住我功能的实现

    在web网页中经常可以看到记住我这样的功能,其实现原理是登陆时候在response中写入cookie,发送请求时,取出cookie判断,如果有则说明已经登陆 写cookie Cookie cookie ...

  4. python学习-31 内置函数

    内置函数 1.abs()  绝对值 2.all()    判断列表里的所有值的布尔值(如果迭代列表里的每个值后都是True 则返回True) '])) 运行结果: True Process finis ...

  5. Scala 安装 Scala for Eclipse安装及运行hello word

    Scala下载安装地址:https://www.scala-lang.org/download/ .windows版本的安装包是scala-2.12.8.msi.直接滑动到网页最下面,下载对应的系统的 ...

  6. Visual Studio 2019激活

    Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT ...

  7. IDEA远程调试Ambari Server

    1.配置端口 Ambari Server默认配置了服务端的debug参数,端口为5005.如果要修改端口,可以在/usr/sbin/ambari_server_main.py文件中对应地方修改,直接改 ...

  8. C# vb .net实现焦距柔化特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的焦距柔化效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  9. MVC学习笔记(二)—用EF创建数据库

    1.创建一个空项目 2.在项目中创建EFCore的类库 3.在NuGut控制台为EFCore项目中安装entity  3.1 命令为:Install-Package EntityFramework(在 ...

  10. python爬虫-有道翻译-js加密破解

    有道翻译-js加密破解 这是本地爬取的网址:http://fanyi.youdao.com/ 一.分析请求 我们在页面中输入:水果,翻译后的英文就是:fruit.请求携带的参数有很多,先将参数数据保存 ...