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. 【Android】3.20 示例20—全景图完整示例

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 1.展示全景图的方式 有以下展示全景图的办法: (1)利用地理坐标展示全景图. (2)利用全景图ID ...

  2. 浅谈CPU,GPU,TPU,DPU,NPU,BPU

    https://www.sohu.com/a/191538165_777155 A12宣传的每秒5万亿次运算,用计算机语言描述就是5Tops. 麒麟970 NPU,根据资料是 1.92Tops. 麒麟 ...

  3. java jvm perf

    http://www.oracle.com/technetwork/java/performance-138178.html http://www.oracle.com/technetwork/jav ...

  4. MySql 触发器同步备份数据表记录

    添加记录到新记录表 DELIMITER $$ USE `DB_Test`$$ CREATE /*!50017 DEFINER = 'root'@'%' */ TRIGGER `InsertOPM_Al ...

  5. Oracle PLSQL Demo - 13.游标的各种属性[Found NotFound ISOpen RowCount CURSOR]

    declare r_emp scott.emp%rowtype; cursor cur_emp is select t.* from scott.emp t; begin open cur_emp; ...

  6. 简单的图形学(二)——材质与反射

    在上一篇[游戏框架系列]简单的图形学(一)文章中,我们讲述了光线追踪的一个最简单的操作--依每个像素延伸出一条追踪光线,光线打到球上(产生交点),就算出这条线的长度,作为最终的灰度,打不到球上,就显示 ...

  7. vue使用sweetalert2弹窗插件

    1). 安装 sweetalert2 npm install sweetalert2@7.15.1 --save 2). 封装 sweetalert2 在 src 新建 plugins 文件夹,然后新 ...

  8. HDS(日立)AMS2000系列存储管理配置方法

    转自:老管网络日志 配置日立存储AMS2000系列时也要和IBM存储一样,首先在客户端或者管理机上安装一个客户端,通过这个客户端再去管理存储. 管理软件名称为Storage Navigator Mod ...

  9. 随机梯度下降 Stochastic gradient descent

    梯度下降法先随机给出参数的一组值,然后更新参数,使每次更新后的结构都能够让损失函数变小,最终达到最小即可. 在梯度下降法中,目标函数其实可以看做是参数的函数,因为给出了样本输入和输出值后,目标函数就只 ...

  10. 【大数据笔记】白话详解Zookeeper的一致性

    下面内容主要摘抄于<<Hadoop实战>>,红色高亮部分是本人添加的白话注释. Zookeeper 是一种高性能.可扩展的服务. Zookeeper 的读写速度非常快,并且读的 ...