zoukankan      html  css  js  c++  java
  • C

    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,0,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
    ",ans);
        return 0;
    }
    View Code

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

  • 相关阅读:
    Java Scanner
    Java 继承
    什么叫异常?什么叫错误? 如何捕获异常? 如何抛出异常? 说说finally和final的区别! 什么是JDK?什么是JRE?说说它们之间的区别? 说说字符常量和字符串常量的区别
    数据分析三剑客之Pandas时间序列
    Css样式布局之Flex弹性盒子布局
    memcached的安装和使用
    Flask 第十八话之Restful API
    Flask 第十七话之信号
    Flask 第十六话之钩子函数
    Flask 第十五话之请求上下文及全局全局存储g对象
  • 原文地址:https://www.cnblogs.com/DreamACMer/p/11182713.html
Copyright © 2011-2022 走看看