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 ...
随机推荐
- uva 10537 Toll! Revisited(优先队列优化dijstra及变形)
Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...
- Disable Oracle Automatic Jobs
By default, Oracle will run some maintance jobs every night. If you don't want to run those jobs, yo ...
- Windows:C++11并发编程-条件变量(condition_variable)详解
<condition_variable >头文件主要包含了与条件变量相关的类和函数.相关的类包括 std::condition_variable和 std::condition_varia ...
- Windows 下Java 连 MYSQL数据库
我们上课的时候在机房是用java连SQLServer2008的.由于这个数据库软件安装太麻烦,最重要的一点是安装后系统应该会变慢.至少开机速度会有影响.至于这个mysql我觉得下载一个绿色版的,然后自 ...
- 2>/dev/null
2>/dev/null是如果你的命今出错的话,错误报告直接输出到黑洞里不会显示在屏幕上. ls -al 1>list.txt 2>list.err #将显示的数据,正确的输出到lis ...
- ControlExtensionTest(一)
#ifndef __CCCONTROLSCENE_H__ #define __CCCONTROLSCENE_H__ #include "cocos2d.h" #include &q ...
- hdoj2111 Saving HDU
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Django 自带密码加密,自定密码加密方式 及自定义验证方式
在django1.6中,默认的加密方式是pbkdf_sha256,具体算法不表,一直以来用django的自带用户验证都十分顺手,今天有需求,需要修改默认加密方式为md5,具体方法为: 在setting ...
- java 多线程8 : synchronized锁机制 之 方法锁
脏读 一个常见的概念.在多线程中,难免会出现在多个线程中对同一个对象的实例变量或者全局静态变量进行并发访问的情况,如果不做正确的同步处理,那么产生的后果就是"脏读",也就是取到的数 ...
- Android开发日记(五)
从服务器端传递多个数据 先在服务器端设置cs文件 using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using Syst ...