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] 为1
        for(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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值