【Cf #291 B】R2D2 and Droid Army(二分,线段树)

因为题目中要求使连续死亡的机器人最多,令人联想到二分答案。
考虑如何检验这之中是否存在一段连续的长度为md的区间,其中花最多k步使得它们都死亡。
这个条件等价于区间中m个最大值的和不超过k。
枚举起点,可以用 $ O(mlogn) $ 的时间确定这段区间是否合法,最终check的复杂度是 $ O(nmlogn) $。
总复杂度是 $ O(nmlog^{2}n) $。
$ \bigodot $ 技巧&套路:
- 最大(小)值的问题,可以考虑二分答案。
- check时用线段树优化区间平移,来枚举每一个长度固定的区间。
#include <cstdio>
#include <iostream>
#include <algorithm> const int N = ; int n, m, k, re;
int a[N][], ans[], tmp[]; namespace SE {
int ma[][N << ];
inline void Up(int t) {
for (int i = ; i <= m; ++i) {
ma[i][t] = std::max(ma[i][t << ], ma[i][t << | ]);
}
}
void Build(int t, int l, int r) {
if (l == r) {
for (int i = ; i <= m; ++i) ma[i][t] = a[l][i];
return;
}
int md = (l + r) >> ;
Build(t << , l, md);
Build(t << | , md + , r);
Up(t);
}
int Query(int t, int l, int r, int L, int R, int ty) {
if (L <= l && r <= R) return ma[ty][t];
int md = (l + r) >> , re = ;
if (L <= md) re = std::max(re, Query(t << , l, md, L, R, ty));
if (md < R) re = std::max(re, Query(t << | , md + , r, L, R, ty));
return re;
}
} inline int Check(int md) {
for (int i = ; i + md - <= n; ++i) {
int sum = ;
for (int j = ; j <= m; ++j) {
tmp[j] = SE::Query(, , n, i, i + md - , j);
sum += tmp[j];
if (sum > k) break;
}
if (sum <= k) {
for (int j = ; j <= m; ++j) ans[j] = tmp[j];
return ;
}
}
return ;
} int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
scanf("%d", &a[i][j]);
}
}
SE::Build(, , n);
for (int nl = , nr = n, md; nl <= nr; ) {
md = (nl + nr) >> ;
if (Check(md)) {
re = md; nl = md + ;
} else {
nr = md - ;
}
}
if (re) Check(re);
for (int i = ; i <= m; ++i) {
printf("%d ", (re)? ans[i] : );
} return ;
}
【Cf #291 B】R2D2 and Droid Army(二分,线段树)的更多相关文章
- (困难) CF 484E Sign on Fence,整体二分+线段树
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...
- Codeforces 514 D R2D2 and Droid Army(Trie树)
题目链接 大意是判断所给字符串组中是否存在与查询串仅一字符之差的字符串. 关于字符串查询的题,可以用字典树(Trie树)来解,第一次接触,做个小记.在查询时按题目要求进行查询. 代码: #define ...
- Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]
传送门 D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- R2D2 and Droid Army(多棵线段树)
R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
- Educational Codeforces Round 61 D 二分 + 线段树
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
随机推荐
- iOS分类Category探索
什么是Category? Category是Objective-C 2.0之后添加的语言特性,Category的主要作用是为已经存在的类添加方法,一般称为分类,文件名格式是"NSObject ...
- slotting filter笔记
1.slot filling是为了让用户的意图转化为明确的指令而补全信息的过程. 2.准入条件 从一个开放域转入到封闭域,或者从一个封闭域转入到另一个封闭域,中间的跳转是需要逻辑判断的,而这个逻辑判断 ...
- git 创建管理多用户
<搬运> Windows下Git多账号配置,同一电脑多个ssh-key的管理 一台电脑上的git同时使用两个github账户 git之https或http方式设置记住用户名和密码的方法
- linux压缩相关
tar命令 tar是打包,即把好多东西放在一个大文件里面,之后再压缩:当然也可以解包 tar的几个参数说明: -c 创建一个新的包 -x 将包里的文件还原出来 -t 显示包内文件的列表 -f 指定要处 ...
- 图解Raid5数据存储的原理
- oracle删除死锁进程
在命令行下运行: select SID,SERIAL# from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; alte ...
- SecureCRT SSH连接一直提示密码错误
这是解决方法: http://www.linuxidc.com/Linux/2016-09/134925.htm
- Daily Scrum6 11.10
今日任务: 徐钧鸿:codingcook的sql相关内容,并在进行复查张艺:继续用户管理部分代码黄可嵩:学习搜索的知识,继续进行搜索的移植和响应徐方宇:动态控件和页面间信息传递以及页面响应事件机制试验 ...
- 虚拟机环境下DPDK运行时的一些错误解决
在绑定网卡到DPDK模块时 报错 :is active. Not modifying Routing table indicates that interface 0000:02:01.0 is ac ...
- 《TCP/IP 详解 卷1:协议》第 9 章:广播和本地组播(IGMP 和 MLD)
我已经懒了,卷一已经是去年年底看完的,但怎么说卷一的坑开了就要填完啊-- 广播和本地组播(IGMP 和 MLD) 引言 有 4 种 IP 地址,单播(unicast).任播(anycast).组播(m ...