Monitor

https://codeforces.com/contest/846/problem/D

问题描述

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.

问题分析

问题大意是 给定一个n*m的显示器,每到一定的时间其上的点(Xi,Yi)会坏掉,当正好有k*k个坏掉的点组成方格时,意味着整个显示器坏掉。求解整个显示器坏掉的最小的时刻。
该问题的解法主要是先用二分时间,然后判断时间是否满足条件。判断时间是否满足条件的过程中可以用简单的动态规划求解(这里是该解法的一大亮点)。状态转移方程为:dp[i][j] = min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1])) + 1;

AC代码


/*
author:Manson
date:8.16.2018
theme:二分 
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 515;

int used[N][N],dp[N][N];
int n,m,q,k;
int x[N*N],y[N*N],t[N*N];

int judge(int key){
    memset(used,0,sizeof(used));
    //满足时间条件时置为 1 
    for(int i = 0;i < q;i++){
        if(t[i] <= key){
            used[x[i]][y[i]] = 1;
        }
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(used[i][j]){
                //状态转移方程 
                dp[i][j] = min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]))+1;
                if(dp[i][j] >= k)
                return 1;
            }
            else{
                dp[i][j] = 0;
            }

        }
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>m>>k>>q;
    for(int i = 0;i < q;i++){
        cin>>x[i]>>y[i]>>t[i];
    }
    int l = 0,r = 1000000000,mid;
    while(l < r){
        mid = (l+r)/2;
        if(judge(mid)){
            r = mid;
        }
        else{
            l = mid+1;
        }
    }
    if(r == 1000000000 && !judge(r)){
        cout<<"-1"<<endl;
    }
    else{
        cout<<r<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值