称号: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 【第二长单调递增子】的更多相关文章

  1. HDOJ 5087 Revenge of LIS II DP

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

  2. hdu 5087 Revenge of LIS II

    http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...

  3. hdu 5087 Revenge of LIS II (DP)

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

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

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

  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. HDURevenge of Segment Tree(第二长的递增子序列)

    HDURevenge of Segment Tree(第二长的递增子序列) 题目链接 题目大意:这题是求第二长的递增子序列. 解题思路:用n^2的算法来求LIS,可是这里还要记录一下最长的那个序列是否 ...

  8. [C++] 动态规划之矩阵连乘、最长公共子序列、最大子段和、最长单调递增子序列、0-1背包

    一.动态规划的基本思想 动态规划算法通常用于求解具有某种最优性质的问题.在这类问题中,可能会有许多可行解.每一个解都对应于一个值,我们希望找到具有最优值的解. 将待求解问题分解成若干个子问题,先求解子 ...

  9. HD1160FatMouse's Speed(最长单调递增子序列)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. Classification and Representation

    Classification To attempt classification, one method is to use linear regression and map all predict ...

  2. 【24.17%】【codeforces 721D】Maxim and Array

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. GoJS超详细入门(插件使用无非:引包、初始化、配参数(json)、引数据(json)四步)

    GoJS超详细入门(插件使用无非:引包.初始化.配参数(json).引数据(json)四步) 一.总结 一句话总结:插件使用无非:引包.初始化.配参数(json).引数据(json)四步. 1.goj ...

  4. Android动态修改图片颜色的实现方式分析

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.修改色相.饱和度.亮度 参看:http://blog.csdn.NET/sjf0115/article/details/7267063 2.使 ...

  5. MySQL参数文件位置

    对于linux/unix: mysql --help|grep my.cnf   /etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.m ...

  6. Make chrome extension

    How to Make a Chrome Extension. https://robots.thoughtbot.com/how-to-make-a-chrome-extension Skip to ...

  7. 《从零開始学Swift》学习笔记(Day 71)——Swift与C/C++混合编程之数据类型映射

    原创文章.欢迎转载.转载请注明:关东升的博客 posted @ 2017-07-21 13:23 zhchoutai 阅读(...) 评论(...) 编辑 收藏

  8. CF 559B(Equivalent Strings-构造法)

    B. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. linux 安装scrt

    http://www.vandyke.com/products/securecrt/  wget http://download.boll.me/securecrt_linux_crack.pl pe ...

  10. NOIP模拟 cube - 数学

    题目原文: 豆豆还是觉得自己智商太低了,就又去做数学题了.一看到题,他就觉得自己可能真的一点智商都没有.便哭着跑来像 dalao 求教:如果存在正整数 A,B ,满足 A3 - B3 = x ,则称质 ...