C - Monitor CodeForces - 846D (二维前缀和 + 二分)
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).
Input
The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.
Each of next q lines contain three integer numbers xi, yi, ti (1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 0 ≤ t ≤ 109) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.
We consider that pixel is already broken at moment ti.
Output
Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.
Examples
2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2
8
3 3 2 5
1 2 2
2 2 1
2 3 5
3 2 10
2 1 100
-1
思路:二分时间或者二分会被毁坏的像素点数量也可以吧,后者虽然没写但是我觉得可以。
然后运用二维前缀和,来查找是否存在k*k的像素缺口。
关于二维前缀和 https://blog.csdn.net/yzyyylx/article/details/78298318
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P = ;
const int inf = 0x3f3f3f3f;
const int maxn = ;
struct node{
ll x,y;
ll t;
}pos[maxn];
ll n,m,k,q;
ll mp[][];
ll a[][],b[][];
int cmp(struct node & a, struct node & b){
return a.t<b.t;
} int check(ll rr){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i = ; pos[i].t<= rr&&i <= q ; i++){
a[pos[i].x][pos[i].y] = ;
}
for(int i = ; i <= n ; i++){
for(int j = ; j <= m ; j++){
b[i][j] = b[i-][j]+b[i][j-]-b[i-][j-]+a[i][j];
}
}
for(int i = k ; i <= n ; i++){
for(int j = k; j <= m ; j++){
if(b[i][j]+b[i-k][j-k]-b[i-k][j]-b[i][j-k] == k*k) return ;
}
}
return ;
} ll ans = -;
ll Min = 1e10,Max = -; int main(){
memset(mp,-,sizeof(mp));
scanf("%lld %lld %lld %lld",&n,&m,&k,&q);
for(int i = ; i <= q ; i++){
scanf("%lld %lld %lld",&pos[i].x,&pos[i].y,&pos[i].t);
mp[pos[i].x][pos[i].y] = pos[i].t;
Min = min(Min,pos[i].t);
Max = max(Max,pos[i].t);
}
sort(pos + , pos + + q ,cmp);
ll l = Min , r = Max + ;
while(r - l > ){
ll mid = (r + l)/;
if(check(mid)){
ans = mid;
r = mid;
}else{
l = mid + ;
}
}
printf("%lld\n",ans);
return ;
}
一个从很久以前就开始做的梦。
C - Monitor CodeForces - 846D (二维前缀和 + 二分)的更多相关文章
- [CSP-S模拟测试]:physics(二维前缀和+二分+剪枝)
题目传送门(内部题26) 输入格式 第一行有$3$个整数$n,m,q$.然后有$n$行,每行有一个长度为$m$的字符串,$+$表示正电粒子,$-$表示负电粒子.然后有$q$行,每行$2$个整数$x,y ...
- Codeforces 835C - Star sky - [二维前缀和]
题目链接:http://codeforces.com/problemset/problem/835/C 题意: 在天空上划定一个直角坐标系,有 $n$ 颗星星,每颗星星都有坐标 $(x_i,y_i)$ ...
- HDU-6514 Monitor(二维前缀和+差分)
http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land ...
- Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)
题意是需要求最大的扩散时间,最后输出的是一开始的火源点,那么我们比较容易想到的是二分找最大值,但是我们在这满足这样的点的时候可以发现,在当前扩散时间k下,以这个点为中心的(2k+1)2的正方形块内必 ...
- Codeforces 1080C 题解(思维+二维前缀和)
题面 传送门 题目大意: 有一个黑白的棋盘,现在将棋盘上的一个子矩形全部染成黑色,另一个子矩形全部染成白色 求染完色后黑,白格子的总数 分析 我们可以发现,对于一个(1,1)到(x,y)的矩形,若xy ...
- openjudge1768 最大子矩阵[二维前缀和or递推|DP]
总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...
- COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)
题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...
- poj-3739. Special Squares(二维前缀和)
题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
随机推荐
- 怎么在一个servlet中实现多个功能 ?如何使一个Servlet处理多个请求?
自学javaweb一直不知道一个servelt可以有多个功能!看了别人代码才知道这个可以有! 平时你建立servelt时候你会吧doget和dopost这两个勾上,要想实现多个功能,你不必要勾选dog ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- Mysql:循环结构
循环结构 分类 while loop repeat 循环控制: iterate类似continue ,继续, 结束本次循环,继续下一次 leave 类似于break 跳出 结束当前所 ...
- JS - 使 input 失去焦点
$(document).ready(function(){ $("body").click(function(){ if(!event.srcElement.type) { ...
- java多线程并发(二)--线程的生命周期及方法详解
上篇随笔介绍了线程的相关基础知识以及新启线程的几种方法,本片将继续介绍线程的生命周期及方法详解. 一.线程的生命周期 在Thread代码中,线程的状态被分为6种 public enum State { ...
- window10 家庭版安装docker和php开发环境
前期工作 一.在宿主机创建文件夹 D:\wnmp\mysql57-conf D:\wnmp\mysql57-log D:\wnmp\nginx110-conf D:\wnmp\nginx110-l ...
- OC Swift混编-Swift.h File not found
https://www.jianshu.com/p/f860fe1718ca 2016.09.13 11:53* 字数 266 阅读 1935评论 1喜欢 1 今天碰到个神坑,本人项目是OC项目,最近 ...
- c# 属性 (get、set)
//属性是一种用于访问对象或类的特性的成员.属性可以包括字符串的长度.字体的大小.窗体的标题和客户的名称.属性是成员的自然扩展,二者都是关联类型的命名成员.namespace ConsoleAppli ...
- 51Nod-1072-威佐夫游戏
有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...
- tf.summary可视化参数
1.tf.summary.scalar('accuracy', accuracy) 损失值.准确率随着迭代次数的进行,其指标变化情况:一般在画loss,accuary时会用到这个函数. 2.tenso ...