题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087

题目意思:找出第二个最长递增子序列,输出长度。就是说,假如序列为 1 1 2,第二长递增子序列是1 2(下标为2 3),而第一长递增子序列也是(下标为 1 3)。

我一开始天真的以为,还是利用求最长递增子序列的算法啦,第二长不就是对dp 数组sort完后(从小到大)的dp[cnt-1] 啦,接着就呵呵啦~~~~= =

题解说,要加多一个 dp 数组,以便对当前下标值为 i 的数 a[i] 为结尾求出第二条dp序列,如果长度一样就直接那个长度了,否则是长度减 1。一直对每个数这样处理,处理到序列最后一个数就是答案了。

以下是看别人的。设 dp[i][0] 表示以a[i]这个数为结尾的最长递增子序列的长度,dp[i][1] 表示以a[i]这个数为结尾的第二长递增子序列的长度(可能为dp[i][0],也可能是dp[i][0]-1)

然后把每个数的两个dp值放进ans数组中,sort之后,答案就为ans[cnt-2]。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int N = + ; int a[N], ans[*N];
int dp[N][]; int main()
{
int T, n;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
memset(dp, , sizeof(dp));
int cnt = ;
for (int i = ; i <= n; i++)
{
dp[i][] = ;
for (int j = ; j < i; j++)
{
if (a[j] < a[i])
{
int x = dp[j][] + ;
int y = dp[j][] + ; if (x > dp[i][])
swap(x, dp[i][]);
dp[i][] = max(x, dp[i][]);
dp[i][] = max(y, dp[i][]);
}
}
ans[cnt++] = dp[i][];
ans[cnt++] = dp[i][];
}
sort(ans, ans+cnt);
printf("%d\n", ans[cnt-]);
}
}
return ;
}

BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告的更多相关文章

  1. BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...

  2. BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...

  3. hdoj 5087 Revenge of LIS II 【第二长单调递增子】

    称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...

  4. HDOJ 5087 Revenge of LIS II DP

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

  5. HDU5087——Revenge of LIS II(BestCoder Round #16)

    Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...

  6. hdu5087——Revenge of LIS II

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

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

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

  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 (DP)

    题意: N个数,求第二长上升子序列的长度. 数据范围: 1. 1 <= T <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 0 ...

随机推荐

  1. BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理

    准备一周多的期末,各种爆炸,回来后状态下滑巨快...调了一晚上+80%下午 2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MB ...

  2. codeforces 720A:Closing ceremony

    Description The closing ceremony of Squanch Code Cup is held in the big hall with n × m seats, arran ...

  3. hdu1695 莫比乌斯反演

    莫比乌斯反演:可参考论文:<POI XIV Stage.1 <Queries>解题报告By Kwc-Oliver> 求莫比乌斯函数mu[i]:(kuangbin模板) http ...

  4. POJ3714 Raid

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10625   Accepted: 3192 Description ...

  5. windows上使用image库

    首先要安装这个库,可以使用pip安装,那么我们要首先安装pip 去https://bootstrap.pypa.io/get-pip.py下载get-pip.py,然后运行python get-pip ...

  6. JS 在线网站

    JS 在线压缩网站 国内压缩js网站http://tool.css-js.com/ uglifyjs 压缩js网站http://lisperator.net/uglifyjs JS在线格式化网站 ht ...

  7. easyUI框架之学习2--添加左侧导航栏

    <head> function addTab(title, url) { if ($('#tableContainer').tabs('exists', title)) { $('#tab ...

  8. chrome 插件开发

    写在开头: 相当有意思的UI界面,编码实现,浏览速度.对于一天浏览器使用超过10个小时的人来说,能够定制自己的浏览器,是相当的具有诱惑力. Getting Started 1:目前不支持标准发布版本的 ...

  9. jQuery类库的设计

    目前为止,jquery是js社区中最活跃.用户最多的前端类库,具有链式操作.兼容性.基于数组的操作.强大的插件机制等特点,也是很多前端入门同学最早接触到的库.但是内部如何实现的,一直吸引着我.因此最近 ...

  10. matlab之矩阵分解

    矩阵分解 矩阵分解 (decomposition, factorization)是将矩阵拆解为数个矩阵的乘积. 1.三角分解法: 要求原矩阵为方阵,将之分解成一个上三角形矩阵(或是排列(permute ...