微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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 = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 500030;
struct node{
    ll x,y;
    ll t;
}pos[maxn];
ll n,m,k,q;
ll mp[510][510];
ll a[510][510],b[510][510];
int cmp(struct node & a,struct node & b){
    return a.t<b.t;
}

int check(ll rr){
    memset(a,0,sizeof(a));
    memset(b,sizeof(b));
    for(int i = 1; pos[i].t<= rr&&i <= q ; i++){
        a[pos[i].x][pos[i].y] = 1;
    }
    for(int i = 1; i <= n ; i++){
        for(int j = 1 ; j <= m ; j++){
            b[i][j] = b[i-1][j]+b[i][j-1]-b[i-1][j-1]+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 1;
        }
    }
    return 0;
}

ll ans = -1;
ll Min = 1e10,Max = -1;

int main(){
    memset(mp,-1,sizeof(mp));
    scanf("%lld %lld %lld %lld",&n,&m,&k,&q);
    for(int i = 1; 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 + 1,pos + 1 + q,cmp);
    ll l = Min,r = Max + 1;
    while(r - l > 0){
        ll mid = (r + l)/2;
        if(check(mid)){
            ans = mid;
            r = mid;
        }else{
            l = mid + 1;
        }
    }
    printf("%lld\n",ans);
    return 0;
}
View Code

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐