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. UVA 10972 - RevolC FaeLoN(边-双连通分量)

    UVA 10972 - RevolC FaeLoN option=com_onlinejudge&Itemid=8&page=show_problem&category=547 ...

  2. TRIZ系列-创新原理-9~11-预先反作用原理、预处理原理、预先防范原理

    一.预先反作用原理表述例如以下: 1)预先给物体施加反作用,以补偿过量的或者不想要的压力. 假设知道系统在执行过程中,会有不利的或者有害的作用(负面作用)产生,则能够预先採取一定的措施来抵消.控制这样 ...

  3. [py]python的继承体系

    python的继承体系 python中一切皆对象 随着类的定义而开辟执行 class Foo(object): print 'Loading...' spam = 'eggs' print 'Done ...

  4. 【Android】7.7 以后改为在Win10下开发了

    分类:C#.Android.VS2015: 创建日期:2016-02-12 修改日期:2016-02-13 一.鼠标点击时千万别一心二用 在Win10升级提醒不厌其烦的持续轰炸下,今天看手机时一不留神 ...

  5. 每日英语:China Underwhelmed After First Apple Event

    Apple's roll-out of its latest iPhones landed with a thud in China, the company's biggest foreign ma ...

  6. Xcode真机调试iOS10中Nslog 打印不出东西

    Xcode真机调试iOS10中Nslog 打印不出东西 解决方案 通过以下途径找到 Product->Scheme->EditScheme ios9以前的 如果不加 1 的那句 在xcod ...

  7. Sql Server数据库自增长字段标识列的插入或更新修改操作办法

    写在前面的话:在日常的Sql server开发中,经常会用到Identity类型的标识列作为一个表结构的自增长编号.比如文章编号.记录编号等等.自增长的标识很大程度上方便了数据库程序的开发,但有时候这 ...

  8. Web攻防系列教程之文件上传攻防解析(转载)

    Web攻防系列教程之文件上传攻防解析: 文件上传是WEB应用很常见的一种功能,本身是一项正常的业务需求,不存在什么问题.但如果在上传时没有对文件进行正确处理,则很可能会发生安全问题.本文将对文件上传的 ...

  9. Python的内存管理 小理解

    请看下面的一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 origin = {'a':100,'b':[1,2,34,5]} obj_copy ={}; ...

  10. 内存单元,CPU对存储器的读写,三大总线

    .存储单元 存储器被分成若干个存储单元,每个存储单元从0开始顺序编号.电子计算机的最小信息单位是bit.8个bit组成一个Byte(虽然现在一个字节可能不是8位,有宽字符出现了,但是一般我们的机器都是 ...