hdoj 5087 Revenge of LIS II 【第二长单调递增子】
称号:hdoj 5087 Revenge of LIS II
题意:非常easy,给你一个序列,让你求第二长单调递增子序列。
分析:事实上非常easy。不知道比赛的时候为什么那么多了判掉了。
我们用O(n^2)的时间求单调递增子序列的时候,里面在加一层循环维护sum数组。表示前面有几个能够转移当当前,求前面sum的和保存到当前。
最后求最后一个sum【n-1】是否为1就ok。为1的话在最长的基础上减一,否则就是最长的。
AC代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
const long long N = 1100;
const long long Mod = 1000000007;
typedef long long LL;
int a[N],dp[N],sum[N];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ma = 0;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
ma = max(a[i],ma);
}
a[n++] = ma+1;
memset(sum,0,sizeof(sum));
dp[0] = 1;sum[0] = 1;
for(int i=1;i<n;i++)
{
int tmp = 0;
for(int j=i-1;j>=0;j--)
{
if(a[i]>a[j] && dp[j]>tmp)
tmp = dp[j];
}
for(int j=i-1;j>=0;j--)
{
if(dp[j]==tmp && a[j]<a[i])
sum[i]+=sum[j];
}
if(sum[i]==0)
sum[i] = 1;
dp[i] = tmp + 1;
}
int ans = 0;
for(int i=0;i<n;i++)
ans = max(ans,dp[i]);
if(sum[n-1]==1)
ans--;
printf("%d\n",ans-1);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
hdoj 5087 Revenge of LIS II 【第二长单调递增子】的更多相关文章
- HDOJ 5087 Revenge of LIS II DP
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 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 ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
- 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 ...
- HDURevenge of Segment Tree(第二长的递增子序列)
HDURevenge of Segment Tree(第二长的递增子序列) 题目链接 题目大意:这题是求第二长的递增子序列. 解题思路:用n^2的算法来求LIS,可是这里还要记录一下最长的那个序列是否 ...
- [C++] 动态规划之矩阵连乘、最长公共子序列、最大子段和、最长单调递增子序列、0-1背包
一.动态规划的基本思想 动态规划算法通常用于求解具有某种最优性质的问题.在这类问题中,可能会有许多可行解.每一个解都对应于一个值,我们希望找到具有最优值的解. 将待求解问题分解成若干个子问题,先求解子 ...
- HD1160FatMouse's Speed(最长单调递增子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- eclipse调试鼠标放上去显示变量值
在eclipse中调试时,鼠标移动到变量上不显示值,这个原来自己也遇到过,没注意,反正就使用ctrl+shift+i嘛,也可以的,刚查了一下,解决方法如下: Window->Preference ...
- [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...
- Canvas基础知识总结之中的一个
canvas的HTML语法: <canvas> Canvas not supported </canvas> 上面这句代码中内容部分所含的文本,这种文本的叫法"后备内 ...
- Injector Job深入分析 分类: H3_NUTCH 2015-03-10 15:44 334人阅读 评论(0) 收藏
Injector Job的主要功能是根据crawlId在hbase中创建一个表,将将文本中的seed注入表中. (一)命令执行 1.运行命令 [jediael@master local]$ bin/n ...
- GitHub的repository的相关操作
原文地址 https://www.jianshu.com/p/038e8ba10e45 1.准备工作 a.有自己的GitHub账号(https://github.com/)b.在自己本地有安装git软 ...
- [Angular] Configurable NgModules
You probably have seen 'foorRoot()' method a lot inside Angular application. Creating a configurable ...
- ios开发ios9新特性关键字学习:泛型,逆变,协变,__kindof
一:如何去学习?都去学习什么? 1:学习优秀项目的设计思想,多问几个为什么,为什么要这么设计,这么设计的好处是什么,还能不能在优化 ,如何应用到自己的项目中 2:学习优秀项目的代码风格,代码的封装设计 ...
- iOS开发AFN使用二:AFN文件下载与文件上传
#import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...
- [HTML5] Using the tabindex attribute for keyboard accessibility
You can make any element keyboard interactive with the HTML tabindex attribute. But you might need a ...
- 任务调度平台Cuckoo-Schedule
任务调度平台Cuckoo-Schedule 1 概述 1.1 平台概述 Cuckoo-Schedule是基于Quartz-Schedule的轻量级任务调度框架,具有易学习.易 ...