hdu5087——Revenge of LIS II
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
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.
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
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
1
3
2HintFor 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.
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的更多相关文章
- HDU5087——Revenge of LIS II(BestCoder Round #16)
Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...
- hdu5087 Revenge of LIS II (dp)
只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ...
- HDU5087 Revenge of LIS II (LIS变形)
题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...
- HDOJ 5087 Revenge of LIS II DP
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdoj 5087 Revenge of LIS II 【第二长单调递增子】
称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...
- HDU 5078 Revenge of LIS II(dp LIS)
Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- hdu 5087 Revenge of LIS II
http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
随机推荐
- Linux 共享库(动态库)
Linux 系统上有两类根本不同的 Linux 可执行程序.第一类是静态链接的可执行程序.静态可执行程序包含执行所需的所有函数 — 换句话说,它们是“完整的”.因为这一原因,静态可执行程序不依赖任何外 ...
- Base64转码和解码的帮助类
/** * 将字符串进行Base64编码 * * @param s 被编码的字符串 * @return 编码后的字符串 */ public static String encoderBASE64(St ...
- 递增和递减进度条CCProgressTimer
关于scheduleUpdate看这篇即可 http://www.benmutou.com/blog/archives/56 接下来是示例代码: CCSize size = CCDirector::s ...
- Oracle_字符集问题(数据库与客户端字符集关联关系)
http://blog.163.com/jiankun_liu/blog/static/1863927762013698175289 ********************************* ...
- RSS是什么,RSS怎么玩,RSS原理是什么 (zhuan)
http://www.cjjjs.com/paper/gzsh/201622721397372.aspx *********************************************** ...
- Consumer
Description FJ is going to do some shopping, and before that, he needs some boxes to carry the diffe ...
- js弹出window.open窗口
<html> <head> <SCRIPT LANGUAGE="JavaScript"> function openwin() {OpenWin ...
- .Net应该学什么怎么学(一)
更新时间:2012年06月05日18时21分 来源:传智播客.Net 上篇<学了.Net做什么开发>中我讲到了目前.Net开发主要方向是Web开发,因此在本篇中我将主要讲解做Web开发要学 ...
- 支持移动触摸设备的简洁js幻灯片插件
lory是一款支持移动触摸设备的简洁的js幻灯片插件.该幻灯片插件可以通过纯js调用,也可以将该幻灯片插件作为jQuery插件来使用.该幻灯片的过渡动画具有硬件加速功能,并且可以定制是否使用easin ...
- 适配器模式和外观模式(head first设计模式——6)
为什么要把适配器模式和外观模式放在同一篇文章中,主要是其相对前面的几个模式来讲会简单些并且具有相似之处.下面就分别通过例子来看理解一下两种模式,然后再进行对其进行比较. 一.适配器模式 1.1适配器模 ...