Revenge of LIS II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 444    Accepted Submission(s): 143

Problem Description
In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible.
This subsequence is not necessarily contiguous, or unique.

---Wikipedia



Today, LIS takes revenge on you, again. You mission is not calculating the length of longest increasing subsequence, but the length of the second longest increasing subsequence.

Two subsequence is different if and only they have different length, or have at least one different element index in the same place. And second longest increasing subsequence of sequence S indicates the second largest one while sorting all the increasing subsequences
of S by its length.
 
Input
The first line contains a single integer T, indicating the number of test cases.




Each test case begins with an integer N, indicating the length of the sequence. Then N integer Ai follows, indicating the sequence.



[Technical Specification]

1. 1 <= T <= 100

2. 2 <= N <= 1000

3. 1 <= Ai <= 1 000 000 000
 
Output
For each test case, output the length of the second longest increasing subsequence.
 
Sample Input
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
 
Sample Output
1
3
2
Hint
For the first sequence, there are two increasing subsequence: [1], [1]. So the length of the second longest increasing subsequence is also 1, same with the length of LIS.
 
Source
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  5089 

pid=5088" target="_blank">5088 

pid=5085" target="_blank">5085 5084 5082






这题太坑了,把思路全然引到了求出LIS,然后推断LIS是否唯一上去了

事实上不然。 比方 1 1 2。LIS == 2。可是光这样无法推断出来次大的长度是多少



网上题解是记录每个位置LIS的个数,假设到最后一位LIS仅仅有一个就输出LIS-1,否则去推断LIS上每个位置上LIS是否唯一。不唯一就输出LIS,否则输出LIS - 1

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int dp[1010];
int a[1010];
int num[1010]; int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
memset ( dp, 0, sizeof(dp) );
memset (num, 0, sizeof(num) );
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
dp[i] = 1;
num[i] = 1;
}
num[n + 1] = 1;
dp[n + 1] = 1;
a[n + 1] = 1000000010;
for (int i = 1; i <= n + 1; i++)
{
for (int j = 1; j < i; j++)
{
if (a[i] > a[j] && dp[i] < dp[j] + 1)
{
num[i] = 1;
dp[i] = dp[j] + 1;
}
else if (a[i] > a[j] && dp[i] == dp[j] + 1)
{
num[i]++;
}
}
}
if (num[n + 1] > 1)
{
printf("%d\n", dp[n + 1] - 1);
continue;
}
int k = n + 1, i;
while (k > 0 && num[k] == 1)
{
for (i = k - 1; i >= 1; i--)
{
if (dp[k] == dp[i] + 1 && a[k] > a[i])
{
break;
}
}
k = i;
}
if (k == 0)
{
printf("%d\n", dp[n + 1] - 2);
continue;
}
printf("%d\n", dp[n + 1] - 1);
}
return 0;
}

 

hdu5087——Revenge of LIS II的更多相关文章

  1. HDU5087——Revenge of LIS II(BestCoder Round #16)

    Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...

  2. hdu5087 Revenge of LIS II (dp)

    只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ...

  3. HDU5087 Revenge of LIS II (LIS变形)

    题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...

  4. HDOJ 5087 Revenge of LIS II DP

    DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  5. hdoj 5087 Revenge of LIS II 【第二长单调递增子】

    称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...

  6. HDU 5078 Revenge of LIS II(dp LIS)

    Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...

  7. BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...

  8. hdu 5087 Revenge of LIS II

    http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...

  9. hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

    链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...

随机推荐

  1. nginx检查报错 error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory

    在centos7.3上编译安装nginx-1.12.2 启动测试出错 [root@web02 local]# /usr/local/nginx/sbin/nginx -t /usr/local/ngi ...

  2. 字符设备驱动笔记——中断方式按键驱动之linux异常处理结构(四)

    .中断方式获取按键值 单片机: )按键按下 )cup发生中断,跳转到异常向量入口执行 )b 函数 a.保存被中断的现场 b.执行中断处理函数 c.恢复 linux: )trap_init()函数构造异 ...

  3. mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12\mysql 5.7.13\mysql 5.7.14)(转)

    http://www.jb51.net/article/90302.htm ******************************* 这篇文章主要为大家分享了MySQL 5.7以上缩版本安装配置 ...

  4. Markdown: Basics (快速入门)[转]

    Markdown: Basics (快速入门) / (点击查看完整语法说明) Getting the Gist of Markdown's Formatting Syntax [转自:http://w ...

  5. Makefile学习之路——1

    编写makefile,不是一个猛子扎进去试着写一个规则并对之调试,而应该先采用面向依赖关系的思考方法勾勒出makefile要表达怎样的依赖关系,这一点尤为重要.通过不断地练习这种思考方法,才可能达到流 ...

  6. 纯css3实现的超炫checkbox复选框和radio单选框

    之前为大家分享了好多css3实现的按钮.今天要为大家分享的是纯css3实现的checkbox复选框和radio单选框,效果超级炫.先让我们看看图吧! 在线预览   源码下载 这个实例完全由css3实现 ...

  7. java Web监听器实现定时发送邮件

    首先介绍java定时器(java.util.Timer)有定时执行计划任务的功能,通过设定定时器的间隔时间,会自动在此间隔时间后执行预先安排好的任务(java.util. TimerTask) 由于我 ...

  8. 【WPF/WAF】设置快捷键(Shortcut Key)

    基于WAF框架:WPF Application Framework (WAF) View层XAML中设置热键. <Window.InputBindings> <!--<KeyB ...

  9. 【SpringMVC笔记】第五课 改进Handler处理器和视图解析器

    第四课 已经对注解的映射器和适配器进行了改进. 接下来需要对Handler处理器和视图解析器进行改进. <!-- 配置handler处理器 --> <bean class=" ...

  10. 树莓派 安装 刷Android Things 小结

    一句话说,Android Things就是让开发者可以使用Android开发工具开发嵌入式设备. If you can build an app, you can build a device. 只要 ...