Revenge of LIS II

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.

题目大意:

    求第二长的绝对递增子序列的长度。

解题思路:

    错误思路:

        求出用于求最长绝对递增子序列的dp数组,sort之后输出dp[N-1]。

        未考虑到dp[N]可以有多种方式构成。eg:1 1 2 就应该输出2。

    正确思路:

        每次求dp[i]的时候,用c[i]记录有多少种情况来构成此最优解。

        求出ans=max(dp[1],dp[2]...dp[N]).

        在求出 sum=sum{ c[i] | dp[i]==ans }

        若sum!=1 说明最优解有多种可能的构成方式。输出ans即可。

        若sum==1 输出ans-1

Code:

 /*************************************************************************
> File Name: BestCode#16_1002.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月01日 星期六 17时44分05秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 5000
using namespace std;
int dp[MAXN];
long long a[MAXN];
int flag[MAXN];
int main()
{
int T;
cin>>T;
while (T--)
{
int N;
cin>>N;
for (int i=;i<=N;i++){
scanf("%I64d",&a[i]);
dp[i]=,flag[i]=;
}
int ans=;
for (int i=;i<=N;i++)
{
for (int j=;j<i;j++)
if (a[j]<a[i])
{
if (dp[i]<dp[j]+)
dp[i]=dp[j]+,flag[i]=flag[j];
else if (dp[i]==dp[j]+)
flag[i]=;
}
if (ans<dp[i]) ans=dp[i];
}
int sum=;
for (int i=;i<=N;i++)
if (ans==dp[i]) sum+=flag[i];
if (sum>)
printf("%d\n",ans);
else
printf("%d\n",ans-);
}
return ;
}

HDU5087——Revenge of LIS II(BestCoder Round #16)的更多相关文章

  1. hdu5087——Revenge of LIS II

    Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

  3. hdu 5086 Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree                                                          Time Limit: 4000/20 ...

  4. hdu5087 Revenge of LIS II (dp)

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

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

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

  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. HDOJ 5087 Revenge of LIS II DP

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

  8. BestCoder Round #16

    BestCoder Round #16 题目链接 这场挫掉了,3挂2,都是非常sb的错误 23333 QAQ A:每一个数字.左边个数乘上右边个数,就是能够组成的区间个数,然后乘的过程注意取模不然会爆 ...

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

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

随机推荐

  1. 虚拟局域网VLAN

    6.5.1配置路由器广域网端口的PPP封装 (1)配置路由器A: Router>enable Router#config Router_config#hostname Router-A Rout ...

  2. HTTP传递数据的几种方法

    Http请求的时候,需要传递参数给后端,一般都是key-value的形式,传递的方法有很多种 例如需要传递的数据是 dict(key1=value1,key2=value2) 1. URL参数 把参数 ...

  3. WPF 实现QQ抖动

    //wpf中实现类似于qq的抖动窗效果 //前段页面 <Window x:Class="WpfApplication4.MainWindow" xmlns="htt ...

  4. VS2010调试入门指南

    1 导言 在软件开发周期中,测试和修正缺陷(defect,defect与bug的区别:Bug是缺陷的一种表现形式,而一个缺陷是可以引起多种Bug的)的时间远多于写代码的时间.通常,debug是指发现缺 ...

  5. .NET中 MEF应用于IOC

    IOC解释 IOC,控制反转的意思.所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓反转,你必须理解如果不反转,会怎么着,因为A必须要有B,才可 ...

  6. 0x03伪指令

    等号伪指令 = 相当于指定常量,由等号定义的符号常量不占用存储空间. count = 1234 可以重复定义多次,EQU则不容许 EQU伪指令 1.常量名 EQU 表达式 NUMBER EQU 10* ...

  7. nodejs redis

    0. install redis library for node npm install redis 1.node command example > var _redis = require ...

  8. 【转】HTTP-only Cookie 脚本获取JSESSIONID的方法

    彻底避免xss攻击的方法. 别人可以通过注入js脚本获取你的session cookie,如果幸运的话还可以获取通过js遍历你的dom树获取你的用户的用户名和密码. 如果只是通过正则表达式验证输入的话 ...

  9. Visual Studio Code asp.net 5环境搭建技能Get

    启动准备阶段 预热 1 VS Code 官方地址:https://www.visualstudio.com/en-us/products/code-vs.aspx 2 安装Node.js :https ...

  10. 剑指offer--面试题6

    题目:由前序.中序遍历序列重建二叉树 虽然思路能想到,但是实际写却无从下手...下面重现作者代码,多多实践... #include<exception> //首先定义二叉树节点 struc ...