Codeforces Round #484 (Div. 2)- Shark(暴力)

D. Shark
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.

Max is a young biologist. For nn days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer kk that if the shark in some day traveled the distance strictly less than kk, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to kk; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least kk.

The shark never returned to the same location after it has moved from it. Thus, in the sequence of nn days we can find consecutive nonempty segments when the shark traveled the distance less than kk in each of the days: each such segment corresponds to one location. Max wants to choose such kk that the lengths of all such segments are equal.

Find such integer kk, that the number of locations is as large as possible. If there are several such kk, print the smallest one.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of days.

The second line contains nn distinct positive integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the distance traveled in each of the day.

Output

Print a single integer kk, such that

  1. the shark was in each location the same number of days,
  2. the number of locations is maximum possible satisfying the first condition,
  3. kk is smallest possible satisfying the first and second conditions.
Examples
input
Copy
8
1 2 7 3 4 8 5 6
output
Copy
7
input
Copy
6
25 1 2 3 14 36
output
Copy
2
Note

In the first example the shark travels inside a location on days 11 and 22 (first location), then on 44-th and 55-th days (second location), then on 77-th and 88-th days (third location). There are three locations in total.

In the second example the shark only moves inside a location on the 22

-nd day, so there is only one location

题意:给你一个长为n的数组.,你可以任意选定一个数字k,使得大于等于k的数变成0,其他数变成1,

然后要满足所有为1的块的大小一样,并且你要保证为1的块尽可能多,问你满足条件的最小的k为多少?

题解:一个结论就是这个k一定是原数组中的某个数+1,证明就免了,然后我们考虑暴力k的值时多少,但是这样的话复杂度不是n^2嘛。你可以考虑标记每个为1的块的左右端点,然后标记每个块的大小,编号,以及每种大小的块的数量,然后粉丝中情况合并为1的块。这样每次就能O(1)搞定更新和查询答案。总复杂度为O(n)(不考虑排序的话)。

#include<map>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100005
struct node
{                                                                 
	int x,y;                      
}a[maxn];                                                         
int n,l[maxn],r[maxn],k,sum[maxn];
int p[maxn],num,b[maxn],res,ans,q,nn;
bool comp(node a,node b)
{
	return a.x<b.x;
}
int main(void)
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i].x),a[i].y=i;
	sort(a+1,a+n+1,comp);
	for(int i=1;i<=n;i++)
	{
		b[a[i].y]=1;
		if(b[a[i].y-1]==0 && b[a[i].y+1]==0)
		{
			if(sum[1]==0) num++;
			sum[1]++;res++;p[a[i].y]=res;
			l[res]=a[i].y;r[res]=a[i].y;
			nn=sum[1];
		}
		else if(b[a[i].y-1] && b[a[i].y+1]==0)
		{
			p[a[i].y]=p[a[i].y-1];
			int t=p[a[i].y];
			int tt=r[t]-l[t]+1;sum[tt]--;
			if(sum[tt]==0) num--;
			r[t]=a[i].y;tt=r[t]-l[t]+1;
			if(sum[tt]==0) num++;sum[tt]++;
			nn=sum[tt];
		}
		else if(b[a[i].y-1]==0 && b[a[i].y+1])
		{
			p[a[i].y]=p[a[i].y+1];
			int t=p[a[i].y];
			int tt=r[t]-l[t]+1;sum[tt]--;
			if(sum[tt]==0) num--;
			l[t]=a[i].y;tt=r[t]-l[t]+1;
			if(sum[tt]==0) num++;sum[tt]++;
			nn=sum[tt];
		}
		else
		{
			int t1=p[a[i].y+1];
			p[a[i].y]=p[a[i].y-1];
			int t=p[a[i].y];
			int tt=r[t]-l[t]+1;sum[tt]--;
			if(sum[tt]==0) num--;
			tt=r[t1]-l[t1]+1;sum[tt]--;
			if(sum[tt]==0) num--;
			r[t]=r[t1];tt=r[t]-l[t]+1;
			if(sum[tt]==0) num++;sum[tt]++;
			nn=sum[tt];p[r[p[a[i].y+1]]]=p[a[i].y];
			//printf("%d\n",r[t1]);
		}
		//printf("%d\n",sum[2]);
		//for(int j=1;j<=n;j++) printf("%d ",sum[j]);
		//printf("%d\n",nn);;
		if(num==1 && nn>q) q=nn,ans=a[i].x+1;
		//printf("%d\n",ans);
	}
	printf("%d\n",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值