链接:https://ac.nowcoder.com/acm/contest/883/I
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
Special Judge, 64bit IO Format: %lld
分数:2500
题目描述
JSB has an integer sequence
. He wants to play a game with SHB.
For each , JSB calculates the median of , denoted by bib_ibi. SHB is given the sequence . Can you help him restore the sequence ?
Recall that the median of three numbers is the second largest one of them.
输入描述:
There are multiple cases. The first line of the input contains a single positive integer , indicating the number of cases.
For each case, the first line of the input contains a single integer , the length of the sequence a\ a a. The second line contains integers .
It’s guaranteed that the sum of over all cases does not exceed .
输出描述:
For each case print one line containing
integers, indicating the sequence
. Your output must satisfy
for each
.
If there are multiple valid answers, you may print any of them. If there is no valid answer, print"-1" (without quotes) instead.
示例1
输入
4
5
1 2 3
6
1 2 3 4
6
1 2 4 3
6
1 3 4 2
输出
1 1 3 2 3
1 2 1 4 3 4
1 1 4 2 4 3
-1
题意:
给定
,
表示
的中位数。要求构造出符合条件的序列
,无解输出
题解:
先考虑判定问题。很显然跟一个
有关的中位数
最多只有3个
。
表示当前第
个位置取跟自己有关的第
个中位数,第
个位置取跟其有关的第
个中位数,这样就可以通过查看
的值并且判定一下这三个数的中位数能否等于
即可。
然后答案的话可以通过记录
表示转移前项,来搞定。
#include<bits/stdc++.h>
#define ll long long
#define INF 1999122700
using namespace std;
int n,b[100004];
int vec[100004][3];
int pre[100004][3][3];
bool f[100004][3][3];
bool check(int ti,int j,int k,int l){
int s[4]={vec[ti][j],vec[ti-1][k],vec[ti-2][l]};
sort(s,s+3);
return (s[1]==b[ti-1]);
}
void print(int t,int j,int k){
if(t==1){
printf("%d ",vec[t][j]);
return ;
}
print(t-1,k,pre[t][j][k]);
printf("%d",vec[t][j]);
if(t==n){
puts("");
}
else{
printf(" ");
}
}
int w33ha(){
scanf("%d",&n);
for(int i=2;i<n;i++)scanf("%d",&b[i]);
for(int i=1;i<=n;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
f[i][j][k]=0;
}
}
}
b[n]=b[n-1];b[n+1]=b[n-1];
b[1]=b[2];b[0]=b[2];
for(int i=1;i<=n;i++){
vec[i][0]=b[i-1];
vec[i][1]=b[i];
vec[i][2]=b[i+1];
sort(vec[i],vec[i]+3);
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
f[1][i][j]=1;
f[2][i][j]=1;
}
}
for(int i=3;i<=n;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
for(int l=0;l<3;l++){
if(!f[i-1][k][l])continue;
if(check(i,j,k,l)){
f[i][j][k]=1;
pre[i][j][k]=l;
}
}
}
}
}
bool flag=0;
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if(f[n][j][k]){
flag=1;
print(n,j,k);
break;
}
}
if(flag)break;
}
if(!flag)puts("-1");
return 0;
}
int main(){
int T;scanf("%d",&T);
while(T--)w33ha();
return 0;
}