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 ...
随机推荐
- Exceptionless 本地部署
免费开源分布式系统日志收集框架 Exceptionless 前两天看到了这篇文章,亲身体会了下,确实不错,按照官方的文档试了试本地部署,折腾一番后终于成功,记下心得在此,不敢独享. 本地部署官方wik ...
- js中对象概念的声明
- swift项目中引入OC框架
- 思维固化,addTarget难道就只能给self
使用前提 调用某个对象的,一个无参数的方法 如[self.view resignFirstResponder] 注意 [self.view endEdting:YES]就不行,要是无参数的
- linux的cgroup控制
cgroup:control group-控制群组:将用户(的进程)加入某个群组(又叫控制器controller), 通过 指定群组对资源-cpu 内存 network等的使用,来限制用户对计算机资源 ...
- UVa12726 one Friend at a Time (位 广搜)
题目链接:UVa12726 是个PDF,不好复制进来. 大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友.共有N个人玩QQ,编号为1到N,1是男主 ...
- CF360B Levko and Array (二分查找+DP)
链接:CF360B 题目: B. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【福利将至】iPhone用户可用Siri发微信了
北京时间6月14日,苹果WWDC16开发者大会召开.继2015年3月份春季发布会和9月份的秋季新品发布会,苹果和腾讯联手Apple Watch版微信和微信3DTouch功能之后,双方在今年的WWDC上 ...
- zoj.3865.Superbot(bfs + 多维dp)
Superbot Time Limit: 2 Seconds Memory Limit: 65536 KB Superbot is an interesting game which you ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...