C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

C - Monitor CodeForces - 846D (二维前缀和 + 二分)

#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

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

大佬总结

以上是大佬教程为你收集整理的C - Monitor CodeForces - 846D (二维前缀和 + 二分)全部内容,希望文章能够帮你解决C - Monitor CodeForces - 846D (二维前缀和 + 二分)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: