D. Monitor
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题目链接:CF 846D

显然二分一下时间,然后把小于等于二分时间t的坏点坐标加入到二维矩阵中,然后暴力枚举k*k矩阵的右下角端点看是否存在矩阵的和刚好为k*k,若存在则说明这一块全坏了,看范围懂做法的水题。。。

代码:

#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 510;
int T[N][N];
struct info
{
int x, y, t;
bool operator<(const info &rhs)const
{
return t < rhs.t;
}
};
inline int lowbit(const int &x)
{
return x & (-x);
}
info arr[N * N];
int n, m, k, q;
int kk; inline void add(int x, int y)
{
for (int i = x; i < N; i += lowbit(i))
for (int j = y; j < N; j += lowbit(j))
T[i][j] += 1;
}
inline sum(int x, int y)
{
int r = 0;
for (int i = x; i > 0; i -= lowbit(i))
for (int j = y; j > 0; j -= lowbit(j))
r += T[i][j];
return r;
}
inline bool check(int t)
{
CLR(T, 0);
int i, j;
for (i = 0; i < q; ++i)
{
if (arr[i].t <= t)
{
add(arr[i].x, arr[i].y);
}
else
break;
}
for (i = k; i <= n; ++i)
{
for (j = k; j <= m; ++j)
{
int area = sum(i, j) - sum(i - k, j) - sum(i, j - k) + sum(i - k, j - k);
if (area == kk)
return 1;
}
}
return 0;
}
int main(void)
{
int i;
while (~scanf("%d%d%d%d", &n, &m, &k, &q))
{
int L = 0, R = 0;
kk = k * k;
for (i = 0; i < q; ++i)
{
scanf("%d%d%d", &arr[i].x, &arr[i].y, &arr[i].t);
R = max(R, arr[i].t);
}
sort(arr, arr + q);
int ans = -1;
while (L <= R)
{
int mid = MID(L, R);
if (check(mid))
{
ans = mid;
R = mid - 1;
}
else
L = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. SSH免手动输入密码和设置代理
  2. ajax处理缓冲问题
  3. My Game --文件读取数据
  4. Android开机启动Activity或者Service方法
  5. 文本框只读属性,disabled不能提交
  6. JavaScript基础18——js的Array对象
  7. webstorm下搭建编译less环境
  8. CI中REST URL含有中文怎么处理(报错:The URI you submitted has disallowed characters)
  9. UNICODE字符集(20140520)
  10. ubuntu13 安装并配置ssh,交换密钥
  11. Java heap dump触发和分析(转)
  12. 转:sql语句中GROUP BY 和 HAVING和使用 count()
  13. [转]【Android】9-patch图片以及例子说明
  14. NancyFx 2.0的开源框架的使用-Stateless
  15. JavaWeb笔记一、Servlet 详解
  16. git学习(持续踩坑中&#129315;)
  17. linux用户、文件权限相关命令
  18. pynlpir 报错 Cannot Save user dictionary 原因与解决方法
  19. ELK 5.6.8 安装部署
  20. unity中实现三个Logo图片进行若隐若现的切换并有延时切换图片的效果

热门文章

  1. Python线程间事件通知
  2. 易语言调用C++写的DLL
  3. Linux添加swap分区
  4. Linux下常用压缩 解压命令与压缩比率对比
  5. 搭建MQTT代理服务器
  6. linux socketpair
  7. 调整图像的亮度和对比度—opencv
  8. Kali2017 Metasploit连接postgresql数据库
  9. linux命令随身记
  10. java程序——随机数求和