因为题目中要求使连续死亡的机器人最多,令人联想到二分答案。

考虑如何检验这之中是否存在一段连续的长度为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(二分,线段树)的更多相关文章

  1. (困难) CF 484E Sign on Fence,整体二分+线段树

    Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...

  2. Codeforces 514 D R2D2 and Droid Army(Trie树)

    题目链接 大意是判断所给字符串组中是否存在与查询串仅一字符之差的字符串. 关于字符串查询的题,可以用字典树(Trie树)来解,第一次接触,做个小记.在查询时按题目要求进行查询. 代码: #define ...

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

  4. R2D2 and Droid Army(多棵线段树)

    R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. HDU4614 Vases and Flowers 二分+线段树

    分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...

  6. J - Joseph and Tests Gym - 102020J (二分+线段树)

    题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...

  7. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...

  8. 【BZOJ-3110】K大数查询 整体二分 + 线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Sta ...

  9. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  10. K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)

    大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...

随机推荐

  1. Socket之简单的Unity3D聊天室__TCP协议

    服务器端程序 using System; using System.Collections.Generic; using System.Linq; using System.Net; using Sy ...

  2. Java线程wait和sleep的区别

    Java中调用wait方法或者sleep方法都可以让线程进入waitint或者time-waiting状态,但是它们还是 有所不同的: wait是Object中的方法,而sleep则是Thread中的 ...

  3. python函数之format

    自python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串,它增强了字符串格式化的功能. 基本语法是通过{}和:来代替%.format函数可以接受不限 ...

  4. SQL Server复制

    SQL Server复制的阶梯:级别1-SQL Server复制介绍 By Sebastian Meine, 2012/12/26 原文链接:http://www.sqlservercentral.c ...

  5. Netty源码分析第6章(解码器)---->第1节: ByteToMessageDecoder

    Netty源码分析第六章: 解码器 概述: 在我们上一个章节遗留过一个问题, 就是如果Server在读取客户端的数据的时候, 如果一次读取不完整, 就触发channelRead事件, 那么Netty是 ...

  6. 最新Microsoft Edge!使用chromium内核

    2018年11月,微软宣布其Edge浏览器将采用Chromium引擎,意味着微软的Edge浏览器以失败告终. 但令人振奋的是,新版Edge也许会“死而复生”.在使用了Chromium内核后,Edge各 ...

  7. Mysql Order By注入总结

    何为order by 注入 本文讨论的内容指可控制的位置在order by子句后,如下order参数可控"select * from goods order by $_GET['order' ...

  8. uniq命令详解

    基础命令学习目录首页 原文链接:http://man.linuxde.net/uniq 删除重复行: uniq file.txt sort file.txt | uniq sort -u file.t ...

  9. SecureCRT SSH连接一直提示密码错误

    这是解决方法:  http://www.linuxidc.com/Linux/2016-09/134925.htm

  10. 第10章 系统级I/O(下)

    10.7  I/O重定向 Unix外壳提供了I/O重定向操作符,允许用户将磁盘文件和标准输出输入联系起来. 例如:unix>ls>foo.txt,使得外壳加载和执行ls程序,将标准输出重定 ...