Codeforces 480.E Parking Lot
3 seconds
256 megabytes
standard input
standard output
Petya's been bored at work and he is killing the time by watching the parking lot at the office. The parking lot looks from above like an n × m table (a cell of the table corresponds to a single parking spot). Some spots in the parking lot are taken, others are empty.
Petya watches cars riding into the parking lot one by one. After a car settles down at the parking spot, Petya amuzes himself by counting what maximum square of empty spots (i.e. a square subtable) can be seen on the parking lot if we look at it from above. Also, he takes notes of the square's size (side length) in his notebook.
You task is: given the state of the parking lot at the initial moment of time and the information about where the arriving cars park, restore what Petya wrote in his notebook. It is midday, so nobody leaves the lot.
The first line contains three integers n, m and k — the sizes of the parking lot and the number of arriving cars after Petya started his watch (1 ≤ n, m, k ≤ 2000). Each of the following n lines contains m characters 'X' and '.', where 'X' means a taken spot and '.' means an empty spot. Each of the next k lines contains a pair of integers xi, yi — the number of row and column of the spot the corresponding car takes (1 ≤ xi ≤ n, 1 ≤ yi ≤ m). It is guaranteed that this place was empty. You can assume that a car enters a parking lot only after the previous car successfully finds a spot.
Print k integers — the length of the side of the maximum square of empty spots after the corresponding car has entered the parking lot.
7 8 4
........
X.....X.
........
........
.X......
........
........
1 5
6 4
3 5
4 6
5
4
4
3
大致题意:k次操作,每次删掉一个点,删掉后问最大的不含'X'的正方形边长是多少.
分析:一道挺好的题,get了许多技巧.
首先,题目允许离线,并且是每次删点后询问,删点后求答案比较麻烦,可以考虑时间倒流,从后往前添加点进去.类似于bzoj上一道删点求连通块数目的题. 初始化出删掉所有的k个点以后的最大正方形的边长,这个可以用dp来算.接着维护两个数组:up[i][j]和down[i][j].表示(i,j)这个点分别能往上和往下扩展多少位.这些是预处理操作.
因为每次都往里面添加点,所以答案肯定是递增的,而答案不超过2000,所以可以while判断当前答案ans + 1后行不行,直到退出while循环,当前的ans就是答案.比添加这个点之前更大的矩形肯定包含添加进去的这个点.当判断边长为len的正方形行不行时.将所有包含当前点的正方形给枚举出来,看在这个边上的点向上扩展和向下扩展的最小值分别是多少,如果加起来-1正好 ≥ len,则可行,否则不可行.涉及到区间查询操作,可以用线段树搞一搞.对于每次添加的点,只需要在那一列改一下up和down数组,并在那一行维护一下答案就可以了.
犯了一个错误:m写成了n.在写题的时候一般都用n而不用m,碰到这种m用的多的情况可以考虑把m,n互换.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int inf = 0x3f3f3f3f; int n,m,k,a[][],up[][],down[][],ans[],res,f[][];
int min1[ << ],min2[ << ],min11,min22;
char s[][]; struct node
{
int x,y;
}e[]; void init()
{
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
{
if (a[i][j] == )
up[i][j] = up[i - ][j] + ;
else
up[i][j] = ;
}
for (int i = n; i >= ; i--)
for (int j = ; j <= m; j++)
{
if (a[i][j] == )
down[i][j] = down[i + ][j] + ;
else
down[i][j] = ;
}
} void jisuan()
{
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
{
if (a[i][j] == )
continue;
f[i][j] = min(f[i-][j],min(f[i][j - ],f[i-][j-])) + ;
res = max(res,f[i][j]);
}
} void pushup(int o)
{
min1[o] = min(min1[o * ],min1[o * + ]);
min2[o] = min(min2[o * ],min2[o * + ]);
} void build(int cur,int o,int l,int r)
{
if (l == r)
{
min1[o] = up[cur][l];
min2[o] = down[cur][l];
return;
}
int mid = (l + r) >> ;
build(cur,o * ,l,mid);
build(cur,o * + ,mid + ,r);
pushup(o);
} int query1(int o,int l,int r,int x,int y)
{
if (x <= l && r <= y)
return min1[o];
int mid = (l + r) >> ,temp = inf;
if (x <= mid)
temp = min(temp,query1(o * ,l,mid,x,y));
if (y > mid)
temp = min(temp,query1(o * + ,mid + ,r,x,y));
return temp;
} int query2(int o,int l,int r,int x,int y)
{
if (x <= l && r <= y)
return min2[o];
int mid = (l + r) >> ,temp = inf;
if (x <= mid)
temp = min(temp,query2(o * ,l,mid,x,y));
if (y > mid)
temp = min(temp,query2(o * + ,mid + ,r,x,y));
return temp;
} bool check(int len,int y)
{
if (len > n || len > m)
return false;
for (int i = max(,y - len + ); i <= m - len + ; i++)
{
int temp1 = query1(,,m,i,i + len - );
int temp2 = query2(,,m,i,i + len - );
if (temp1 + temp2 - >= len)
return true;
}
return false;
} void solve()
{
jisuan();
for (int i = k; i >= ; i--)
{
//printf("%d\n",i);
ans[i] = res;
if (i == )
break;
int x = e[i].x,y = e[i].y;
a[x][y] = ;
up[x][y] = up[x - ][y] + ;
for (int j = x + ; j <= n; j++)
{
if (a[j][y] == )
break;
up[j][y] += up[x][y];
}
down[x][y] = down[x + ][y] + ;
for (int j = x - ; j >= ; j--)
{
if (a[j][y] == )
break;
down[j][y] += down[x][y];
}
build(x,,,m);
while (check(res + ,y))
res++;
}
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for (int i = ; i <= n; i++)
{
scanf("%s",s[i] + );
for (int j = ; j <= m; j++)
{
if (s[i][j] == '.')
a[i][j] = ;
else
a[i][j] = ;
}
}
for (int i = ; i <= k; i++)
{
scanf("%d%d",&e[i].x,&e[i].y);
a[e[i].x][e[i].y] = ;
}
init();
solve();
for (int i = ; i <= k; i++)
printf("%d\n",ans[i]); return ;
}
Codeforces 480.E Parking Lot的更多相关文章
- CF 480 E. Parking Lot
CF 480 E. Parking Lot http://codeforces.com/contest/480/problem/E 题意: 给一个n*m的01矩阵,每次可以将一个0修改为1,求最大全0 ...
- #2 codeforces 480 Parcels
题意: 就是有一个用来堆放货物的板,承重力为S.现在有N件货物,每件货物有到达的时间,运走的时间,以及重量,承重,存放盈利.如果这件货物能再运达时间存放,并在指定时间取走的话,就能获得相应的盈利值.货 ...
- Codeforces #480 Tutorial
Problem A,B,C: 简单的模拟,注意A中p mod q时对q=0特殊处理(注意范围) Problem D: Brief Intro: 给定长度为N的数组A,将A中所有连续子序列分成最少的组, ...
- Mistakes(Updating)
1.当调试时发现无法正常调用函数时,检查是否发生爆栈 对于每个栈仅有4MB的空间,开int只能开大约5*10^5. 大数组一定要开全局变量 2.当long long=int*int时会爆int,一定要 ...
- [Atcoder Grand Contest 003] Tutorial
Link: AGC003 传送门 A: 判断如果一个方向有,其相反方向有没有即可 #include <bits/stdc++.h> using namespace std; ]; map& ...
- ●CodeForces 480E Parking Lot
题链: http://codeforces.com/problemset/problem/480/E题解: 单调队列,逆向思维 (在线的话应该是分治做,但是好麻烦..) 离线操作,逆向考虑, 最后的状 ...
- Codeforces 46D Parking Lot
传送门 D. Parking Lot time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
E. Parking Lot time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 219E Parking Lot 线段树
Parking Lot 线段树区间合并一下, 求当前要占的位置, 不包括两端点的写起来方便一点. #include<bits/stdc++.h> #define LL long long ...
随机推荐
- 3星|李开复《AI·未来》:中国创业公司有独特优势,人工智能可能会加剧社会的不平等与不稳定
主要内容:作者对自己一些经历的回顾,对中美两国人工智能行业的回顾与展望. 作者认为中国的创业公司比美国节奏更快工作更拼命,深圳在硬件创新上远远领先于美国,中国创业公司们走出了一条跟美国不同的路. 作者 ...
- 【RL系列】SARSA算法的基本结构
SARSA算法严格上来说,是TD(0)关于状态动作函数估计的on-policy形式,所以其基本架构与TD的$v_{\pi}$估计算法(on-policy)并无太大区别,所以这里就不再单独阐述之.本文主 ...
- 第六次ScrumMeeting博客
第六次ScrumMeeting博客 本次会议于10月31日(二)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 除了汇报任务外,窦鑫泽同学还就前 ...
- Kotlin 学习笔记(一)
(Kotlin 学习笔记的文档结构基本按照 Java 核心技术一书的目录排列) 基本程序设计结构 数据类型 数字 类型 宽度 Double 64 Float 32 Long 64 Int 32 Sho ...
- Android开发第二阶段(3)
今天:对闹钟代码的按钮事件进行了添加和修改.对监听器的相关应用也有了进一步的了解和深入. 明天:对主界面的代码的优化比如对按钮位置的调节等细节处理.
- Alpha 冲刺(4/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前后端接口的开发 测试项目运行的服务器环 ...
- NSURLErrorDomain错误代码
kCFURLErrorUnknown = -998, kCFURLErrorCancelled = -999, kCFURLErrorBadURL = -1000, kCFURLErrorTimedO ...
- c语言的知识与能力自评
知识与能力 C语言最早是由美国Bell实验室设计的,主要用作UNIX系统的工作语言,后来发展成为一种通用语言.C与UNIX有密切的关系,C最早是在PDP机器上用UNIX操作系统上开发的,后来又用C语言 ...
- lintcode-408-二进制求和
408-二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 标签 二进制 字符串处理 脸书 思路 先相加,在处理进位,为了方便操作,将选字符 ...
- 运维学习笔记(七)之T02-01计算机网络 、 数制 、 网络通信参考模型
计算机网络 计算机网络概述 什么是计算机网络 硬件方面:通过线缆将网络设备和计算机连接起来 软件方面:操作系统.应用软件.应用程序通过通信线路互连 实现资源共享.信息传递 功能 数据通信/资源共享/增 ...