题目
题目描述
NN 段のピラミッドがあります。 段は上から順に 11 , 22 , … , NN と番号が振られています。 各 1\ \leq\ i\ \leq\ N1 ≤ i ≤ N について、 ii 段目には 2i-12i−1 個のブロックが横一列に並んでいます。 また、各段の中央のブロックに注目すると、これらは縦一列に並んでいます。
N=4N=4 段のピラミッド
すぬけ君は NN 段目のブロックに ( 11 , 22 , … , 2N-12N−1 ) を並べ替えたもの(順列)を書き込みました。 さらに、次のルールに従い、残りすべてのブロックに整数を書き込みました。
あるブロックに書き込まれる整数は、そのブロックの左下、真下、右下のブロックに書き込まれた整数の中央値である。
ブロックに整数を書き込む例
その後、すぬけ君はすべてのブロックに書き込まれた整数を消してしまいました。 すぬけ君は、 NN 段目のブロックに書き込まれた順列が ( a_1a
1
, a_2a
2
, … , a_{2N-1}a
2N−1
) であったことだけを覚えています。
11 段目のブロックに書き込まれた整数を求めてください。
输入格式
The input is given from Standard Input in the following format:
$ N $
$ a_1 $ $ a_2 $ $ … $ $ a_{2N-1} $
输出格式
Print the integer written into the block of step 11 .
题意翻译
给出一个N层的方格金字塔,自顶向下依次标号为第1到第N层。
其中第i(1 \le i \le N1≤i≤N)层有2i − 1个方格。(具体形态见下面的图)
第N层有一个1到2N-1的排列,其他层的数字按以下规则生成:方格b中填写的整数,是方格b正下方,左下方和右下方方格中所写整数的中位数。
现在给出第N层的数字,请你求第一层的数字。
翻译提供者:WAAutoMaton
输入输出样例
输入 #1复制
4
1 6 3 7 4 5 2
输出 #1复制
4
输入 #2复制
2
1 2 3
输出 #2复制
2
说明/提示
制約
2\ \leq\ N\ \leq\ 10^52 ≤ N ≤ 10
5
( a_1a
1
, a_2a
2
, … , a_{2N-1}a
2N−1
) は ( 11 , 22 , … , 2N-12N−1 ) の順列である。
Problem Statement
We have a pyramid with NN steps, built with blocks. The steps are numbered 11 through NN from top to bottom. For each 1\ \leq\ i\ \leq\ N1 ≤ i ≤ N , step ii consists of 2i-12i−1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically.
A pyramid with N=4N=4 steps
Snuke wrote a permutation of ( 11 , 22 , … , 2N-12N−1 ) into the blocks of step NN . Then, he wrote integers into all remaining blocks, under the following rule:
The integer written into a block bb must be equal to the median of the three integers written into the three blocks directly under bb , or to the lower left or lower right of bb .
Writing integers into the blocks
Afterwards, he erased all integers written into the blocks. Now, he only remembers that the permutation written into the blocks of step NN was ( a_1a
1
, a_2a
2
, … , a_{2N-1}a
2N−1
).
Find the integer written into the block of step 11 .
Constraints
2\ \leq\ N\ \leq\ 10^52 ≤ N ≤ 10
5
( a_1a
1
, a_2a
2
, … , a_{2N-1}a
2N−1
) is a permutation of ( 11 , 22 , … , 2N-12N−1 ).
Sample Explanation 1
問題文中の図の例です。
Sample Explanation 3
This case corresponds to the figure in the problem statement.
思路
我们二分塔顶的值,把小于等于这个值的变为1,大于变为0。(有点似曾相识)
我们可以发现如果有多个个1或0连在一起,那么他们就无法被分开,他会一直往上走。
那也就是说,最后那组先走到顶那组就赢了,那就要看那组离中心更近。
那会不会存在两个不同阵营的组距离一样远能,你会发现这是不可能的。
因为,如果距离相等,那么中间一定是奇数个位置,我们用1和0,交替隔开两组,那么最后一个位置肯定会和左边或者右边一样,又形成一个组,所这两个组要么都是1,要么都是0。
解决了这些问题就能愉快的判断了!
代码
#include<bits/stdc++.h>
using namespace std;
int n,a[20000077];
int small(int i,int j,int k)
{
return a[i]<=k && a[j]<=k;
}
int big(int i,int j,int k)
{
return a[i]>k && a[j]>k;
}
int check(int k)
{
for(int i=0; i<n-1; i++)
{
if(big(n+i,n+i+1,k) || big(n-i,n-i-1,k)) return 0;
if(small(n+i,n+i+1,k) || small(n-i,n-i-1,k)) return 1;
}
return small(1,1,k);
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=2*n-1; i++) scanf("%d",&a[i]);
int l=1,r=2*n-1,ans;
while(l<r)
{
int mid=l+r>>1;
if(check(mid))
{
r=mid;
}
else l=mid+1;
}
cout<<r;
}