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". ...
随机推荐
- POJ 1770 树形DP
咋一看确实想到的是树形DP,但是我一开始也马上想到环的情况,这样应该是不可以进行树形DP的,然后我自以为是地想用有向图代替无向图,而且总是从能量高的指向能量低的,这样自以为消除了环,但是其实是不对滴, ...
- sql server C#操作。原文在收藏页面
C#操作SQL Server数据库 1.概述 2.连接字符串的写法 3.SqlConnection对象 4.SqlCommand对象 5.SqlDataReader对象 6.DataSet对象 7 ...
- ELK 介绍
章节 ELK 介绍 ELK 安装Elasticsearch ELK 安装Kibana ELK 安装Beat ELK 安装Logstash ELK是什么? ELK是3个开源产品的组合: Elastics ...
- NumPy 矩阵库函数
章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...
- MongoDB 教程
版权所有,未经许可,禁止转载 章节 MongoDB 入门 MongoDB 优势 MongoDB 安装 MongoDB 数据建模 MongoDB 创建数据库 MongoDB 删除数据库 MongoDB ...
- mark LINUX_6.8 python_2.6.6 setup版本升级 python 2.7.9 安装 pip 临时使用国内镜像源库 指定模块版本 删除指定模块
简单但却又经常需要使用 网上 贴子也很多 也经常用 所以 做个mark 吧: 1首先下载python2.7.9 源tar包 源码安装 可利用linux自带下载工具wget下载,如下所示: ...
- xml学习-语法规则
XML 指可扩展标记语言(eXtensible Markup Language).XML 被设计用来传输和存储数据. XML 语法规则 XML 文档必须有根元素 XML 必须包含根元素,它是所有其他元 ...
- jquery特效 - 网站水波纹
1,index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 实验吧web-难-认真一点!(布尔盲注,py脚本)
也可用bp进行爆破,这里用py脚本. 打看网页输入1,显示You are in,输入2,显示You are not in,是个布尔注入. 然后看看过滤了什么. sql注入没有过滤:--+.or sql ...
- 用tensorflow求手写数字的识别准确率 (简单版)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = in ...