BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接: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) 解题报告的更多相关文章
- BestCoder6  1002 Goffi and Squary Partition(hdu 4982)  解题报告
		
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...
 - 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,问是否在每个数 ...
 - hdoj 5087 Revenge of LIS II 【第二长单调递增子】
		
称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...
 - HDOJ 5087 Revenge of LIS II DP
		
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
 - 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
		
Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
 - hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
		
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
 - hdu 5087 Revenge of LIS II
		
http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...
 - hdu 5087 Revenge of LIS II (DP)
		
题意: N个数,求第二长上升子序列的长度. 数据范围: 1. 1 <= T <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 0 ...
 
随机推荐
- Cocos2d-X3.0 刨根问底(二)----- 从HelloWorld开始
			
小鱼习惯直接从代码实例来学习一套成型的引擎库. 运行cpp-empty-test 一个典型的HelloWorld程序翻看代码结构 看到了 main.h与main.cpp文件就从这里开始 #ifndef ...
 - bzoj1069 SCOI2007 最大土地面积
			
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2560 Solved: 983 Description ...
 - 求DAG上两点的最短距离
			
Problem 给出一个不带边权(即边权为1)的有向无环图(unweighted DAG)以及DAG上两点s, t,求s到t的最短距离,如果无法从s走到t,则输出-1. Solution DFS,BF ...
 - HackerRank and MiniMax
			
传送门 Sherlock and MiniMax Authored by darkshadows on May 07 2014 Problem Statement Watson gives Sherl ...
 - 基础总结篇之三:Activity的task相关
			
http://blog.csdn.net/liuhe688/article/details/6761337 古人學問無遺力,少壯工夫老始成.紙上得來終覺淺,絕知此事要躬行.南宋.陸遊<冬夜讀書示 ...
 - 本地与在线图片转Base64及图片预览
			
查看效果:http://sandbox.runjs.cn/show/tgvbo9nq 本地图片转Base64(从而可以预览图片): function localImgLoad() { var src ...
 - CodeForces 703B(容斥定理)
			
题目链接:http://codeforces.com/contest/703/problem/B 解题思路: 第一次写 先求出每个点到其他点的价值,并将其记录 dp[i][j]=1(i<j),然 ...
 - sencha touch list(列表)、 store(数据源)、model(模型)详解
			
//求职 Ext.define('app.model.Staff', { extend: 'Ext.data.Model', config: { fields: [{ name: 'id', type ...
 - C++中的异常处理(三)
			
C++中的异常处理(三) 标签: c++C++异常处理 2012-11-24 23:00 1520人阅读 评论(0) 收藏 举报 分类: 编程常识(2) 版权声明:本文为博主原创文章,未经博主允许 ...
 - 你无法修改 Git 的历史记录
			
转自:http://www.oschina.net/news/26241/you-can-not-change-git-history 有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 ...