codeforces 846D (二分+二维前缀和)Monitor
2017-10-20 00:31
585 查看
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 × kconsisting 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 qpixels 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.
Example
Input
Output
Input
Output
-1
考虑到坏了的就是坏了的,时间的符合性具有单调性,所以可以二分时间,找到符合的结果,在找
k*k的矩形里面用到了二维前缀和,sum记录 0,0到i,j的值,然后利用树状数组求对应矩形的做法就好,因为没有修改操作所以不需要后缀数组。
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct node
{
int x,y,t;
}s[500000];
int sum[510][510];
int mian[510][510];
int cmp(node a,node b)
{
return a.t<b.t;
}
int check(int num)
{
memset(mian,0,sizeof(mian));
memset(sum,0,sizeof(sum));
for(int i=1;i<=num;i++)
mian[s[i].x][s[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]+mian[i][j];
for(int i=k;i<=n;i++)
{
for(int j=k;j<=m;j++)
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()
{
int q;
scanf("%d%d%d%d",&n,&m,&k,&q);
for(int i=1;i<=q;i++)
scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t);
sort(s+1,s+q+1,cmp);
int l=1,r=q;
int ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))
{
ans=s[mid].t;
r=mid-1;
}
else l=mid+1;
}
printf("%d\n",ans );
}
moment when it contains a square k × kconsisting 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 qpixels 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.
Example
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的矩形里面用到了二维前缀和,sum记录 0,0到i,j的值,然后利用树状数组求对应矩形的做法就好,因为没有修改操作所以不需要后缀数组。
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct node
{
int x,y,t;
}s[500000];
int sum[510][510];
int mian[510][510];
int cmp(node a,node b)
{
return a.t<b.t;
}
int check(int num)
{
memset(mian,0,sizeof(mian));
memset(sum,0,sizeof(sum));
for(int i=1;i<=num;i++)
mian[s[i].x][s[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]+mian[i][j];
for(int i=k;i<=n;i++)
{
for(int j=k;j<=m;j++)
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()
{
int q;
scanf("%d%d%d%d",&n,&m,&k,&q);
for(int i=1;i<=q;i++)
scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t);
sort(s+1,s+q+1,cmp);
int l=1,r=q;
int ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))
{
ans=s[mid].t;
r=mid-1;
}
else l=mid+1;
}
printf("%d\n",ans );
}
相关文章推荐
- Codeforces 846 D Monitor(类似求面积的前缀和)
- Monitor(二分,二维前缀和板子记录)
- Codeforces 479E Riding in a Lift【Dp+前缀和优化+二分】好题~
- Codeforces 846 C Four Segments(前缀和+枚举)
- Codeforces 611C:New Year and Domino 二维前缀和
- codeforces 676C Vasya and String 前缀数组+二分查找
- Codeforces 106D Treasure Island【思维+二维前缀和】
- Poj 1358 Housing Complexes【二分匹配+二维前缀和预处理建图】
- codeforces 16C C. Monitor(二分)
- Codeforces 846D Monitor(二维前缀和)
- Codeforces 676C - Vasya and String 详解(二分前缀和+尺取两种写法)
- Codeforces 460C Present 二分+前缀和
- CodeForces 460C——二分+前缀和—— Present
- [CodeForces 677D] Vanya and Treasure (二维树状数组+静态前缀最小值)
- codeForces 660C.Hard Process(二分+前缀和)
- codeforces 835C(二维前缀和)
- Codeforces 838A. Binary Blocks (区间筛+二维前缀和, IndiaHacks 2nd Elimination 2017 )
- Codeforces 835C Star sky【思维+暴力预处理二维前缀和】
- Codeforces 611C:New Year and Domino 二维前缀和
- Codeforces 645C:Enduring Exodus (二分+前缀和)