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

二维前缀和
题意:
给定一个n×m的矩阵,其中有q个点是坏掉的,当 整 个 矩 阵 中 存 在 某 个 k × k 的 子 矩 阵 中 的 点 都 坏 了 , 那 么 整 个 矩 阵 就 会 损 坏 。q 个 点 按 照 一 定 的 时 间 顺 序 损 坏 ,判 断 最 早 在 何 时 , 整 个 矩 阵 会 损 坏 , 即 出 现 k × k 个 子 矩 阵 内 的 点 均 损 坏 的 情 况 。若 不 存 在 整 个 矩 阵 损 坏 的 情 况 , 输 出 − 1 。
题解思路:
给定每个点损坏的时间,那么时间呈线性单调性,可以利用二分时间来减小时间复杂度。
首先我们对q个点,按照时间顺序从小到大排序,将时间进行二分,对 于 每 一 个 时 间 m i d , 我 们 用 二 维 数 组 mp , 将 所 有 在 m i d 时 间 以 内 损 坏 的 点 上 累 加 1 , 然 后 对 数 组 a 求 一 遍 前 缀 和 , 然后对数组a求一遍前缀和,最 后 全 图 枚 举 k × k 的 子 矩 阵 , 查 询 子 矩 阵 中 损 坏 点 的 数 量 , 若 数 量 等 于 k × k , 则 说 明 m i d 可 行 。

AC代码:
PS:注意q的数据范围是n*m

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define debug(a) cout<<#a<<"="<<a<<endl;
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
#define ll long long
#define mod 998244353
using namespace std;
const double PI = acos(-1.0);
const int maxn = 5e2 + 10;
const int INF = 0x3f3f3f3f;
const double esp_0 = 1e-6;
ll gcd(ll x, ll y) {
    
    
	return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
    
    
	return x * y / gcd(x, y);
}
ll extends_gcd(ll a, ll b, ll& x, ll& y) {
    
    
	if (b == 0) {
    
    
		x = 1;
		y = 0;
		return a;
	}
	ll gcdd=extends_gcd(b, a % b, x, y);
	ll temp = x;
	x = y;
	y = temp - (a / b) * y;
	return gcdd;
}
struct node {
    
    
	int x, y, t;
	friend bool operator<(const node& a, const node& b) {
    
    
		return a.t < b.t;
	}
}a[maxn * maxn];
int mp[maxn][maxn];
int sum[maxn][maxn];
int n, m, k, q;
int judge(int time) {
    
    
	mem(mp, 0);
	mem(sum, 0);
	for (int i = 0; i < q; ++i) {
    
    
		if (a[i].t <= time)mp[a[i].x][a[i].y] = 1;
	}
	for (int i = 1; i <= n; ++i) {
    
    
		for (int j = 1; j <= m; ++j) {
    
    
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mp[i][j];//求二维前缀和
		}
	}
	for (int i = k; i <= n; ++i) {
    
    
		for (int j = k; j <= m; ++j) {
    
    
		//判断每一个k*k的子矩阵,注意下标是K
			if (sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] == k * k) {
    
    
				return 1;
			}
		}
	}
	return 0;
}
int main(){
    
    
	scanf("%d%d%d%d", &n, &m, &k, &q);
	int left = 0, right = 0, mid = 0;
	for (int i = 0; i < q; ++i) {
    
    
		scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].t);
		right = max(right, a[i].t);
	}
	sort(a, a + q);
	int ans = INF;
	while (left <= right) {
    
    
		mid = (left + right) >> 1;
		if (judge(mid)) {
    
    
			ans = mid;
			right = mid - 1;
		}
		else
			left = mid + 1;
	}
	if (ans == INF) {
    
    
		ans = -1;
	}
	printf("%d\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40924271/article/details/109911905