HNU2019 Summer Training 3 E. Blurred Pictures

2023-07-11,,

E. Blurred Pictures

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Damon loves to take photos of the places he visits during his travels, to put them into frames. All of his photos are in a square format of N×N pixels. He brought back beautiful pictures of the many monuments in Paris, such as the Eiffel Tower or the Louvre, but unfortunately, when he got back home, he realized that all his pictures were blurred on the edges. Looking closely, Damon realizes that he can easily distinguish the blurred pixels from the "good" (i.e., non-blurred) ones and that, luckily, all the non-blurred pixels are connected in such a way that any horizontal or vertical line drawn between two non-blurred pixels goes only through non-blurred pixels. In order to get the best from his failed pictures, he decides to cut out the biggest possible picture without any blurred pixel from each of his photos. And since his frames are all squares, for aesthetic reasons, the cut-out pictures have to be squares too. Damon does not want his pictures to be tilted so he wants the sides of the cut-outs to be parallel to the sides of the original picture.

Input

The input comprises several lines, each consisting of integers separated with single spaces:

The first line contains the length N, in pixels, of the input photo;
Each of the next N lines contains two integers ai and bi , the indices of the first (ai)and the last (bi) non-blurred pixel on the i-th line.
0<N≤100000,
0≤ai≤bi<N.

Output

The output should consist of a single line, whose content is an integer, the length of the largest square composed of non-blurred pixels inside the picture.

Examples

input

3
1 1
0 2
1 1

output

1

input

8
2 4
2 4
1 4
0 7
0 3
1 2
1 2
1 1

output

3

Note
In the input picture, each row and each column has at least one non-blurred pixel.
In any two consecutive lines, there are at least two non-blurred pixels in the same column.

代码实现:

#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
const int N=2e5+5;
int l[N],r[N];
signed main(){
int n;
cin>>n;
//题目保证最小为1
int res=1;
//输入每行的左端点和右端点
for(int i=1;i<=n;i++)cin>>l[i]>>r[i];
for(int i=1;i<=n;i++){
while(1){
//如果当前行的长度都小于res,直接换下一行
if(r[i]-l[i]<res)break;
//如果当前行向下越界了,直接返回
if(res+i>n)break;
//记录最终行
int ed=res+i;
//判断第一行和最后一行起始点哪个靠后
int bg=max(l[i],l[ed]);
//如果bg加res小于等第一行右端点且bg加res小于等于最终行右端点,则表示当前行可取,res++,继续往下判断
if(bg+res<=r[i]&&bg+res<=r[ed])res++;
//否则表示当前行已经无法继续向下判断了
else break;
}
}
cout<<res<<endl;
return 0;
}

HNU2019 Summer Training 3 E. Blurred Pictures的相关教程结束。

《HNU2019 Summer Training 3 E. Blurred Pictures.doc》

下载本文的Word格式文档,以方便收藏与打印。