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". ...
随机推荐
- 四十八、在SAP中函数参数的使用
一.不带参数的函数定义如下 二.我们把函数内部会变化的变量以参数的形式定义,其中USING和CHANGING分别表示变量和返回值,因为so_car表示的是工作区,所以后面加上[]符号. 三.我们把2个 ...
- 043-PHP简单获得一个类对应的反射信息
<?php // 简单获得一个类对应的反射信息 class demo{ CONST CON_STR = '123456'; public $str_1; private $str_2; prot ...
- 125-PHP类__set()魔术方法
<?php class ren{ //定义一个人类 //定义成员属性 private $name='tom'; private $age=15; //定义__set()魔术方法 public f ...
- XPath--快速获取XML数据的节点或属性
转载自 XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识.示例Xml: <?xml version="1.0" ...
- 二、【未来】React环境安装:npx
搭建React的开发环境的第二种方法(新-未来推荐): https://reactjs.org/docs/create-a-new-react-app.html 一. npx简介: 1. npm v5 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 类 & 对象
C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计.类是 C++ 的核心特性,通常被称为用户定义的类型. 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法.类中的 ...
- 将已有微信小程序转换为多端应用
文档地址 https://nervjs.github.io/taro/
- UVA - 11093 Just Finish it up(环形跑道)(模拟)
题意:环形跑道上有n(n <= 100000)个加油站,编号为1~n.第i个加油站可以加油pi加仑.从加油站i开到下一站需要qi加仑汽油.你可以选择一个加油站作为起点,起始油箱为空(但可以立即加 ...
- BlackArch Linux 2019.06.01 宣布发布
导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...
- Yota Phone宣告破产
作为双面屏手机的开山鼻祖,Yota Phone已经消失在大家的视线中. 据外媒报道称,开曼群岛最高法院裁定在开曼群岛注册的YotaPhone手机生产商Yota Devices公司破产.法院的相关裁定被 ...