@codeforces - 506C@ Mr. Kitayuta vs. Bamboos
@description@
n 个竹子,第 i 个竹子初始高度 hi,在每天结束时将长高 ai。
一共 m 天,每天可以砍伐 k 次,可以多次砍伐同一个竹子。如果砍伐的竹子当前高度 h,则砍后变为 max(0, h - p)。
问 m 天之后最高的竹子的高度最小是多少。
@solution@
考虑第 i 个竹子如果在第 j 天被砍了 \(c_{i,j}\) 次,则最后剩下的高度应为:
\]
根据砍伐的定义,这是易证的。
我们不妨二分答案 x,前面包含 hi 的式子暴力 O(n) 判断。后面的式子如果暴力判断是 O(nm) 的复杂度。
注意到 k 很小,也就是说总的砍伐数量只有 O(mk) 次,远远小于 O(nm)。
我们不妨只记录对于每棵竹子而言的有效砍伐,使用 m 个队列从后往前进行模拟,就可以做到 O(mk + n) 的判断时间复杂度。
总时间复杂度 O((mk + n)log A)。
@accepted code@
#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
const int MAXM = 5000;
const int MAXN = 100000;
int n, m, k;
ll a[MAXN + 5], h[MAXN + 5], p;
queue<pii>que[MAXM + 5];
bool check(ll x) {
for(int i=1;i<=m;i++) {
while( !que[i].empty() )
que[i].pop();
}
for(int i=1;i<=n;i++) {
ll s = x / a[i] + 1;
if( s <= m ) que[s].push(mp(i, 0));
}
for(int i=1,c=0;i<=m;i++,c+=k) {
while( !que[i].empty() ) {
if( c ) c--; else return false;
pii f = que[i].front(); que[i].pop(); f.se++;
ll s = (f.se*p + x) / a[f.fi] + 1;
if( s <= m ) que[s].push(f);
}
}
ll s = 0;
for(int i=1;i<=n;i++)
s += (max(a[i]*m + h[i] - x, 0LL) + p - 1) / p;
return s <= m*k;
}
int main() {
scanf("%d%d%d%lld", &n, &m, &k, &p);
for(int i=1;i<=n;i++) scanf("%lld%lld", &h[i], &a[i]);
ll le = 0, ri = 0;
for(int i=1;i<=n;i++)
ri = max(ri, h[i] + m*a[i]);
while( le < ri ) {
ll mid = (le + ri) >> 1;
if( check(mid) ) ri = mid;
else le = mid + 1;
}
printf("%lld\n", ri);
}
@details@
一开始本来想写线段树结果发现会 T,最后还是换成了队列模拟。
注意开 long long 的问题。有些地方虽然合法在 int 范围内,但不合法的情况会炸。
@codeforces - 506C@ Mr. Kitayuta vs. Bamboos的更多相关文章
- 506C Mr. Kitayuta vs. Bamboos
分析 代码 #include<bits/stdc++.h> using namespace std; #define int long long ],h[],now[],cnt[]; in ...
- Codeforces 505E - Mr. Kitayuta vs. Bamboos(二分+堆)
题面传送门 首先很显然的一点是,看到类似于"最大值最小"的字眼就考虑二分答案 \(x\)(这点我倒是想到了) 然鹅之后就不会做了/wq/wq/wq 注意到此题正着处理不太方便,故考 ...
- Mr. Kitayuta vs. Bamboos
Mr. Kitayuta vs. Bamboos 题目链接:http://codeforces.com/problemset/problem/505/E 参考:http://blog.csdn.net ...
- 「CF505E」 Mr. Kitayuta vs. Bamboos
「CF505E」 Mr. Kitayuta vs. Bamboos 传送门 如果没有每轮只能进行 \(k\) 次修改的限制或者没有竹子长度必须大于 \(0\) 的限制那么直接贪心就完事了. 但是很遗憾 ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- [Codeforces 505C]Mr. Kitayuta, the Treasure Hunter
Description The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The is ...
- Codeforces 505A Mr. Kitayuta's Gift 暴力
A. Mr. Kitayuta's Gift time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
随机推荐
- STM32 Keil 软件仿真设置
设置 Dialog.DLL 分别为:DARMSTM.DLL和TARMSTM.DLL, Parameter 均为:-pSTM32F103RC,用于设置支持芯片的软硬件仿真
- CF948B Primal Sport
题目链接:http://codeforces.com/contest/948/problem/B 知识点: 素数 解题思路: \(f(x)\) 表示 \(x\) 的最大素因子.不难想到:\(X_1 \ ...
- LightOJ1030 Discovering Gold
题目链接:https://vjudge.net/problem/LightOJ-1030 知识点: 概率与期望 解题思路: 设某一个点 \(i\) 能到达的点的个数为 \(x\),其上有金 \(g\) ...
- HDU1588
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1588 题目大意:g(i)= k * i + b. 给定 k 和 b,求0 <= i < n ...
- Java——关键字和保留字
Java关键字50个 abstract assert boolean break byte case catch char class const continue default do double ...
- get_client_ip()
get_client_ip()获取ip地址,在开启IPv6协议的主机上会全部返回0.0.0.0原因是他会把ipv6地址认为是非法地址而转换成0.0.0.0,而ipv4地址在ipv6主机上用get_cl ...
- debug PHP程序(xdebug、IntelliJ IDEA)
之前写PHP程序的都是echo调试,今天感觉太麻烦了就想起研究一下IntelliJ IDEA如何调试PHP程序. 从网上查找了很多资料,大部分都提到在IDE里开启服务,一下就懵了,怎么启这么多服务呢. ...
- 50个SQL语句(MySQL版) 问题十一
--------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...
- Shell编程案例:修改运维脚本输出效果
1. 需求:每日运维检查脚本dailymonitor.sh显示对服务器测试结果,其中命令 zabbix_get -s 192.168.111.21 -p 10050 -k "net.tcp. ...
- Chisel3 - 字面量(literal)
https://mp.weixin.qq.com/s/uiW4k4DeguvYsG8LhHk2Ug 介绍Chisel3中基本数据类型的字面量的写法,及其背后的实现机制,也就是Scala隐式规则. ...