HOJ 2085 Wavio Sequence(双lis)
来源:互联网 发布:解除支付宝和淘宝绑定 编辑:程序博客网 时间:2024/05/17 04:40
题目来源:hoj 2085
Wavio Sequence
Problem Description
Wavio is a sequence of integers. It has some interesting properties.
- Wavio is of odd length i.e. L = 2 * n + 1.
- The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.
- The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.
- No two adjacent integers are same in a Wavio sequence.
For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :
Here the longest Wavio sequence is: 1 2 3 4 5 4 3 2 1. So, the output will be 9.
Input
The input file contains multiple test cases. The description of each test case is given below. Input is terminated by end of file.
Each set starts with a postive integer, N(1 ≤ N ≤ 10000). In next few lines there will be N integers.
Output
For each set of input print the length of longest wavio sequence in a line.
Sample Input
101 2 3 4 5 4 3 2 1 10191 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 151 2 3 4 5
Sample Output
991
#include <iostream>#include <stdio.h>using namespace std;int main(){ int n; while(scanf("%d",&n)==1) { int arr[11111],dp1[11111],dp2[11111],brr[11111],crr[11111],l1,l2,i;//dp1保存正向lis结果,dp2保存反向lis结果,brr辅助dp1,crr辅助dp2,事实上可以不用crr。 for(i=1; i<=n; i++) { scanf("%d",&arr[i]); } //以下为正向lis(nlogn) brr[1]=arr[1]; dp1[1]=1; l1=1; int index; for(i=2; i<=n; i++) { if(arr[i]>brr[l1]) { brr[++l1]=arr[i]; dp1[i]=l1; } else { index=lower_bound(brr+1,brr+1+l1,arr[i])-brr;//注意lb函数的用法 brr[index]=arr[i]; dp1[i]=l1; } } //以下为反向lis crr[1]=arr[n]; dp2[n]=1; l2=1; for(i=n-1; i>0; i--) { if(arr[i]>crr[l2]) { crr[++l2]=arr[i]; dp2[i]=l2; } else { index=lower_bound(crr+1,crr+1+l2,arr[i])-crr; crr[index]=arr[i]; dp2[i]=l2; } } //以下求最大值 int max=1; for(i=1; i<=n; i++) { if(max<2*min(dp1[i],dp2[i])-1)//2倍的短的-1 { max=2*min(dp1[i],dp2[i])-1; } } printf("%d\n",max); } return 0;}
- HOJ 2085 Wavio Sequence(双lis)
- UVA10534-Wavio Sequence(LIS)
- UVa 10534 Wavio Sequence (LIS、LDS)
- UVA - 10534Wavio Sequence(LIS)
- UVA 10534Wavio Sequence(LIS)
- UVA 10534 Wavio Sequence(LIS)
- UVA 10534-Wavio Sequence(LIS)
- uva 10534 Wavio Sequence (LIS)
- UVa 10534 - Wavio Sequence LIS
- uva 10534 Wavio Sequence(LIS)
- UVA 10534 - Wavio Sequence LIS
- uva 10534 Wavio Sequence LIS
- UVA 10534 Wavio Sequence 【LIS】
- uva 10534 Wavio Sequence (LIS)
- 【UVA】10534 - Wavio Sequence(LIS最长上升子序列)
- uvaoj 10534 Wavio Sequence 最长上升子序列(LIS)
- uva 10534 - Wavio Sequence(nlgn复杂度LIS)
- UVA10534-----Wavio Sequence-----动态规划之LIS
- ubuntu下安装新版QQ
- background简写属性
- 总结(12.3-12.10)
- codeforces 887B. Cubes for Masha
- Verilog中ISE联合Modelsim仿真,出现蓝线和红线的问题
- HOJ 2085 Wavio Sequence(双lis)
- 哈理工 oj 2188 星际旅行
- 校赛总结
- UI设计新手入门须知
- 语料库之专业名持续更新:【内向即失败--王奕君】
- 卷积函数-atrous_conv2d
- python正则表达式
- java系统属性
- Window 下安装 Memcached