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

Input
2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2
Output
8
Input
3 3 2 5
1 2 2
2 2 1
2 3 5
3 2 10
2 1 100
Output
-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 ;
}

一个从很久以前就开始做的梦。

最新文章

  1. 如何预览github中的html页面
  2. 序列的方法(str,list,tuple)
  3. Nginx源码安装及调优配置
  4. Headfirst设计模式的C++实现——适配器(Adapter)
  5. 关于android:focusable属性
  6. Oracle游标cursor1基础和隐式游标
  7. c# ActiveMQ 类
  8. Docker初步了解
  9. Linux 监测命令
  10. python 面向对象终极进阶之开发流程
  11. 【转载】IL指令集
  12. luogu3188/bzoj1190 梦幻岛宝珠 (分层背包dp)
  13. Python这么热,要不要追赶Python学习热潮?
  14. C#获取gif帧数
  15. 验证时出错。HRESULT = &amp;#39;8000000A&amp;#39;
  16. Python动态规划求解最长递增子序列(LIS)
  17. 【Android】7.2 LinearLayout(线性布局)
  18. scrapy连接MySQL
  19. Python开发基础-Day16import模块导入和包的调用
  20. margin 依附与可见的内容,不能为margin而写margin

热门文章

  1. Elasticsearch 使用集群 - 删除索引
  2. poj 3262 Protecting the Flowers 贪心 牛吃花
  3. es6 part 1 //const let
  4. sudo 提权漏洞(CVE-2019-14287)复现 (10.16 第二十二天)
  5. DataStructuresAndAlogorithm--红黑树
  6. redis性能测试方法
  7. 【pwnable.kr】col
  8. Canvas绘制水波进度加载
  9. 关于javascript中this 指向的4种调用模式
  10. Xcode10趟坑之路