第一次使用博客,一道基础题

1 篇文章 0 订阅
1 篇文章 0 订阅

第一次使用博客

前言

我自己都看不下去自己到底在干什么了所以从今天开始决定每天水一篇博客,虽然不一定是编程的东西但是自己起码保证每日一水!

题目

Problem Description

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 x 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.Important 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.

Input

Multiple test cases and for each case:
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.

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.

Sample Input3

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

Sample Output1

3

题目解释

题目大意:在一个图中每一行都有完好的像素点,现在让我们求像素点能够构成的最大正方形。
解法:由于我们可以看到题目的每一行完好的像素点是连续的,所以我们很容易想到我们可以将矩形的条件直接抽象出来,如果矩形的大小为x,那么上边界的最小值减去下边界的最大值一定要大于或者等于x。所以我们很容易想道我们需要二分矩形的大小。而大小确定之后只需要验证了,我们可以鞎简单的通过rmq得到上边界的最小值和下边界的最大值。所以整道题的复杂度是O(nlog2n)的,虽然不知道有没有更好的写法但是我写的就是这个。区间最值也可以用线段树、树状数组等来维护也可以。

code

#include<bits/stdc++.h>
using namespace std;
int N;
int a[300010],b[300010];
int dp1[300010][22],dp2[300010][22];
void rmq_init1(){    
	for(int i=1;i<=N;i++)        
		dp1[i][0]=a[i];
	for(int j=1;(1<<j)<=N;j++)        
		for(int i=1;i+(1<<j)-1<=N;i++)            
			dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<j-1)][j-1]);
}
void rmq_init2(){    
	for(int i=1;i<=N;i++)        
		dp2[i][0]=b[i];
	for(int j=1;(1<<j)<=N;j++)        
		for(int i=1;i+(1<<j)-1<=N;i++)            
			dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<j-1)][j-1]);
}
int rmq1(int l,int r){
    int k=log2(r-l+1);
    return max(dp1[l][k],dp1[r-(1<<k)+1][k]);
}
int rmq2(int l,int r){
    int k=log2(r-l+1);
    return min(dp2[l][k],dp2[r-(1<<k)+1][k]);
}
bool check(int now){
	for (int i=1;i<=N;i++){
		if (i-now+1>=1){
			int xx1=rmq1(i-now+1,i);
			int xx2=rmq2(i-now+1,i);
			if (xx2-xx1+1>=now) return 1;
		}
	}
	return 0;
}
int main(){
	while (cin>>N){
		for (int i=1;i<=N;i++)
			scanf("%d%d",&a[i],&b[i]);
		rmq_init1();
		rmq_init2();
		int l=0,r=N+1,mid=(l+r)/2;
		while (r-l>1){
			if (check(mid)) l=mid;
			else r=mid;
			mid=(l+r)/2;
		}
		cout<<l<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值