coderforces 846D Monitor

编程入门 行业动态 更新时间:2024-10-08 02:24:28

<a href=https://www.elefans.com/category/jswz/34/348311.html style=coderforces 846D Monitor"/>

coderforces 846D Monitor

D. Monitor
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

题意:给一个n*m的屏幕,有q个像素会在 t[i]的时间会坏掉, 如果有一个k*k的矩阵区域坏掉的话 ,整个屏幕就会坏掉,问屏幕最少在多少时间会坏掉。如果不会坏输出-1.

分析:可以二分时间T。对于一个时间题 T 如果能找得到全部由会坏掉的像素构成的 k*k的矩阵 且 其中像素中的最大值要小于等于T则 对于T就是满足的。

自己想法:看了大神的代码  才知道还能这样子二分。。。

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
const int maxn = 500;
const int inf = ((1e9) + 10);
typedef long long ll;
using namespace std;
int n,m,k,q;
int g[maxn+5][maxn+5],cnt[maxn+5][maxn+5];
int x[maxn*maxn+5], y[maxn*maxn+5], w[maxn*maxn+5];
void solve()
{int l = -1, r = inf , ans = inf;while(r-l>1){int mid = (l+r)/2;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) g[i][j] = 0;for(int i=1;i<=q;i++) if(w[i]<=mid) g[x[i]][y[i]] = 1;// 对于小于T的q[i] 为1for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) cnt[i][j] =  cnt[i-1][j] + cnt[i][j-1] + g[i][j] - cnt[i-1][j-1]; //求出矩阵的前缀和吧}int ok = 0;for(int i=k;i<=n;i++){for(int j=k;j<=m;j++)if(cnt[i][j]-cnt[i-k][j]-cnt[i][j-k]+cnt[i-k][j-k]==k*k) ok = 1;// 看能否 有k*k的矩阵中满足t[i] 都小于T的}if(ok) r = mid, ans = mid;else l = mid;}if(ans==inf) ans = -1;printf("%d\n",ans);
}
int main()
{while(~scanf("%d %d %d %d",&n,&m,&k,&q)){for(int i=1;i<=q;i++)scanf("%d %d %d",&x[i],&y[i],&w[i]);solve();}return 0;
}


更多推荐

coderforces 846D Monitor

本文发布于:2024-02-19 16:23:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:coderforces   Monitor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!