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 :
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.
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 less than 75 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
10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
Sample Output
9
9
1

问题链接:UVA10534 Wavio Sequence
问题简述:从给定序列中求出最长的2n+1序列,使得其满足前面n个不递减,后面n个不递增。
问题分析:正反各进行一遍LIS,再计算最终结果。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10534 Wavio Sequence */#include <bits/stdc++.h>using namespace std;const int N = 10000;
int a[N], dp[N], ans1[N], ans2[N];int main()
{int n;while(~scanf("%d", &n)) {for(int i = 0; i < n; i++) scanf("%d", &a[i]);memset(dp, 0, sizeof(dp));int len = 0;for(int i = 0; i < n; i++) {if(len == 0 || dp[len - 1] < a[i])dp[len++] = a[i];else {int p = lower_bound(dp, dp + len, a[i]) - dp;dp[p] = a[i];}ans1[i] = len;}len = 0;for(int i = n - 1; i >= 0; i--) {if(len == 0 || dp[len - 1] < a[i])dp[len++] = a[i];else {int p = lower_bound(dp, dp + len, a[i]) - dp;dp[p] = a[i];}ans2[i] = len;}int ans = 0;for(int i = 0; i < n; i++)ans = max(ans, min(ans1[i], ans2[i]));printf("%d\n", ans * 2 - 1);}return 0;
}

UVA10534 Wavio Sequence【LIS+DP】相关推荐

  1. UVA497 Strategic Defense Initiative【LIS+DP】

    "Commander! Commander! Please wake up commander!"     "- mmmph. What time is it?" ...

  2. 【概率DP】$P2059$ 卡牌游戏

    [概率DP]P2059 卡牌游戏 链接 题目描述 N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张 ...

  3. 【数位DP】恨7不成妻

    [数位DP]恨7不成妻 时间限制: 1 Sec  内存限制: 128 MB 提交: 8  解决: 4 [提交] [状态] [命题人:admin] 题目描述 单身! 依然单身! 吉哥依然单身! DS级码 ...

  4. Bailian4004 数字组合【递归+DP】

    4004:数字组合 总时间限制: 1000ms 内存限制: 65536kB 描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: ...

  5. Bailian2755 神奇的口袋【递归+DP】

    2755:神奇的口袋 总时间限制: 10000ms 内存限制: 65536kB 描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得 ...

  6. poj1161Post Office【经典dp】

    题目:poj1161Post Office点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面建p个邮局.村民优先选择去近的邮局.问全部村庄去邮局的最小距离和是多少? 分类:区间d ...

  7. 【背包DP】【2018.9.20普及组模拟】T3(WOJ 3975)保护羊村

    题目(保护羊村): [题目描述] 伟大的Yyz 帮助羊羊们逃出了城堡,可Jack 自然不会善罢甘休."我会诅咒你们的!"杰杰恼羞成怒地喊道.回到羊村后,羊羊们发现羊村地震了.看来J ...

  8. 【线性DP】跳格子问题 + 光签题(取石子游戏)

    Part1跳格子Part\ 1\ 跳格子Part 1 跳格子 LDUOJ 测试平台传送门 问题描述: NikolaNikolaNikola 现在已经成为一个游戏里的重要人物.这个游戏是由一行 NNN ...

  9. 【动态规划dp】青蛙的烦恼(frog)

    青蛙的烦恼(frog) [题目描述] 池塘中有 n 片荷叶恰好围成了一个凸多边形,有一只小青蛙恰好站在 1 号荷叶上,小青蛙想通过 最短的路程遍历所有的荷叶(经过一个荷叶一次且仅一次),小青蛙可以从一 ...

最新文章

  1. 网络推广产品中网站的标题如何设置更利于SEO?
  2. 【Python基础】太香了!推荐6个Python数据分析神器!!
  3. super.getClass()方法调用返回结果的原因
  4. php 隐藏的id,隐藏你的 ID 吧!
  5. linux端口被攻击,Linux 常见攻击端口封杀表
  6. 服务端解决跨域问题的方案(二)
  7. 2019.8.1正则二
  8. 提出问题之后,对于回答问题内容的仔细确认!!!(一个字一个字确认!!)
  9. 32 开漏输出 高电平_关于各种IO输出类型详解
  10. EMNLP'21 Findings|字节火山翻译提出:基于自修正编码器的神经机器翻译
  11. linux 锐捷客户端 脚本,基于802.1x的锐捷linux客户端
  12. keil5兼容keil4操作
  13. 基于阿里云Aliddns动态域名解析的客户端PHP实现与服务器端(包含C与PHP)实现
  14. CentOS8使用network.service服务管理网络
  15. java之Collection
  16. 基于单片机的水壶自动加热系统_一种基于单片机的电热水壶控制系统的制作方法...
  17. 程序员自我提升的5个方法
  18. request.setAttribute的用法
  19. Leetcode 345 Reverse Vowels of a String 字符串处理
  20. 【aspose】aspose-cells-21.1 For Java 除水印

热门文章

  1. JavaWeb——Mybatis进阶mapper代理
  2. 如何使用go读写excel
  3. html5 过渡时间,CSS3 对过渡(transition)进行调速以及延时
  4. 测试学习java_使用Junit测试框架学习Java
  5. 设计模式(一)六大原则
  6. Hive insert into小文件问题优化解决
  7. spark application 同时运行 实行数据共享
  8. scala读取数据从文件或者其他url中
  9. scala构造器的介绍
  10. Git教程——如何创建一个版本库(Repository)