链接: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 a1,a2,…,ana_1, a_2, \dots, a_na1​,a2​,…,an​​. He wants to play a game with SHB.

For each 1≤i≤n−21 \le i \le n-21≤i≤n−2, JSB calculates the median of {ai,ai+1,ai+2}\{a_i, a_{i+1}, a_{i+2}\}{ai​,ai+1​,ai+2​}, denoted by bib_ibi​. SHB is given the sequence b1,b2,…,bn−2b_1, b_2, \dots, b_{n-2}b1​,b2​,…,bn−2​​. Can you help him restore the sequence aaa ?

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 TTT, indicating the number of cases.

For each case, the first line of the input contains a single integer n(3≤n≤105)nn\ (3 \le n \le 10^5)nn (3≤n≤105)n, the length of the sequence a\ a a. The second line contains n−2\ n-2 n−2 integers b1,b2,…,bn−2(0≤bi≤109)b_1, b_2, \dots, b_{n-2}\ (0 \le b_i \le 10^9)b1​,b2​,…,bn−2​ (0≤bi​≤109).

It’s guaranteed that the sum of nnn over all cases does not exceed 10610^6106.

输出描述:
For each case print one line containing nnn integers, indicating the sequence a1,a2,…,ana_1, a_2, \dots, a_na1​,a2​,…,an​. Your output must satisfy 0≤ai≤1090 \le a_i \le 10^90≤ai​≤109 for each 1≤i≤n1 \le i \le n1≤i≤n.

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

题意:
给定b[1],b[2]…b[n−2]b[1],b[2] \dots b[n-2]b[1],b[2]…b[n−2],b[i]b[i]b[i]表示a[i],a[i+1],a[i+2]{a[i],a[i+1],a[i+2]}a[i],a[i+1],a[i+2]的中位数。要求构造出符合条件的序列{an}\{a_n\}{an​},无解输出−1-1−1

题解:
先考虑判定问题。很显然跟一个a[i]a[i]a[i]有关的中位数b[i]b[i]b[i]最多只有3个b[i−2],b[i−1],b[i]b[i-2],b[i-1],b[i]b[i−2],b[i−1],b[i]。
f[i][j][k]f[i][j][k]f[i][j][k]表示当前第iii个位置取跟自己有关的第j+1j+1j+1个中位数,第i−1i-1i−1个位置取跟其有关的第k+1k+1k+1个中位数,这样就可以通过查看f[i−1][k][l]f[i-1][k][l]f[i−1][k][l]的值并且判定一下这三个数的中位数能否等于b[i−1]b[i-1]b[i−1]即可。
然后答案的话可以通过记录pre[i][j][k]pre[i][j][k]pre[i][j][k]表示转移前项,来搞定。

#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;
}

[2019牛客多校训练第3场]Median相关推荐

  1. 2019牛客多校训练第十场F Popping Balloons

    2019牛客多校训练第十场F Popping Balloons 题意:二维平面内给你若干个点,然后你可以在x轴和y轴分别射三枪(每一枪的间隔是R),问最多能射掉多少气球. 题解:贪心.这个应该只能算作 ...

  2. 2019牛客多校第一场

    2019牛客多校第一场 题号 题目 知识点 A Monotonic Matrix B Symmetric Matrix C Fluorescent 2 D Two Graphs E Removal F ...

  3. 2019牛客多校第九场AThe power of Fibonacci(广义BM)

    2019牛客多校第九场AThe power of Fibonacci(广义BM) 题目大意 求斐波那契数列m次方的前n项和 解题思路 显然,斐波那契的m次方前缀和依然是线性递推,因此考虑用exBM求解 ...

  4. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  5. Knapsack Cryptosystem(2019牛客多校折半查询)

    链接:https://ac.nowcoder.com/acm/contest/889/D 来源:牛客网 Amy asks Mr. B problem D. Please help Mr. B to s ...

  6. Quadratic equation(二次剩余)2019牛客多校第九场

    链接:https://ac.nowcoder.com/acm/contest/889/B 来源:牛客网 题目描述 Amy asks Mr. B problem B. Please help Mr. B ...

  7. 2019牛客多校训练营第一场 H题 HOR 题解

    题目描述: 输入描述: 输出描述: 示例1: 题解: 更多问题可关注牛客竞赛区,一个刷题.比赛.分享的社区. 传送门:https://ac.nowcoder.com/acm/contest/discu ...

  8. 2019牛客多校 第七场 B Irreducible Polynomial 多项式因式分解判断

    链接:https://ac.nowcoder.com/acm/contest/887/B 来源:牛客网 Irreducible Polynomial 时间限制:C/C++ 1秒,其他语言2秒 空间限制 ...

  9. 2019牛客多校训练营第一场 E题 ABBA 题解

    问题描述: 输入描述: 输出描述: 示例1: 题解: 更多问题可关注牛客竞赛区,一个刷题.比赛.分享的社区. 传送门:https://ac.nowcoder.com/acm/contest/discu ...

最新文章

  1. SecureCRT 绝佳配色方案, 保护你的眼睛
  2. 京东数科首次公开:强一致、高性能分布式事务中间件JDTX
  3. python详细安装教程3.7.4-python 3.7.4下载与安装的问题
  4. 7. U9成本核算基本流程概述
  5. less 函数_Python中的函数式编程教程,学会用一行代码搞定所有内容
  6. MAPREDUCE的实战案例
  7. 三十四、R语言数据分析实战
  8. 柱坐标系下的ns方程_麦克斯韦方程组小结
  9. 哪位大兄弟有用 cMake 开发Android ndk的
  10. server2003-多域间林之间信任配置方法详解(附图)
  11. mysql 连接 110 超时_Nginx和mysql上行超时超时(110:连接超时)
  12. 乱斗西游2服务器修改,为我们的友谊干杯《乱斗西游2》转服功能邀你面基
  13. 6.世界坐标观察模式
  14. 超全Inventor 3d模型素材网站整理
  15. 最厉害的象棋软件_太牛了!象棋史上最厉害人机高手竟弃车跟软件对杀,就问你敢不敢...
  16. windows安装虚拟机(VMware)
  17. Springcloudalibaba整合es!实现相关性排序,集成完代码真香
  18. 关于动态抽样(Dynamic Sampling)
  19. 2021年在vue中使用 Google Map
  20. 网站建设规划 如何做网站

热门文章

  1. 不做ui了 转行做什么_ui设计师是做什么的 想转行可以吗
  2. android自带的webview有广告,android webview 拦截广告
  3. java图片改变分辨率并保存
  4. 工程硕士计算机专业开题报告,计算机技术工程硕士论文
  5. 小程序源码:全网独家小程序版本独立微信社群人脉系统社群空间站最新源码开源+详细教程
  6. [ATPG]解读report_nonscan_cells -summary得到的report
  7. 狄利克雷卷积_算法学习笔记(35): 狄利克雷卷积
  8. 计算机教师帮扶记录,教师结对帮扶工作总结范文(通用6篇)
  9. 力扣1024视频拼接
  10. html添加一条虚线垂直的,【html问题】在网页中添加垂直分割线