hdu5087 Revenge of LIS II (dp)
只要理解了LIS,这道题稍微搞一下就行了。
求LIS(最长上升子序列)有两种方法:
1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度。dp[i]最少也得是1,就初始化为1,则dp[i]=max(dp[i],dp[j]+1)(其中j<i且a[j]<a[i])。
int gao()
{
int ans=;
for(int i=;i<n;i++)
{
dp[i]=;
for(int j=;j<i;j++)
{
if(a[j]<a[i])
{
dp[i]=max(dp[i],dp[j]+);
}
}
ans=max(ans,dp[i]);
}
return ans;
}
O(n^2)
2.O(n*lgn)算法:有这样一个性质:如果上升子序列长度相同,那么最末尾最小的那种在之后的比较中最有优势(有点贪心的味道)。根据这个,设dp[i]为长度为i+1的上升子序列中末尾元素的最小值。开始dp全初始化为INF,每次更新时前面的dp都是排好序的,所以就可以二分来找。
int gao()
{
fill(dp,dp+n,INF);
for(int i=;i<n;i++)
{
*lower_bound(dp,dp+n,a[i])=a[i];
}
ans=lower_bound(dp,dp+n,INF)-dp;
}
O(n*lgn)
对于本题,要求输出第二长子序列的长度。那么就找LIS,如果LIS只有一种情况可以得到,那么输出LIS-1,否则输出LIS。
用num数组来记录能达到当前长度的方法数。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
const LL mod=;
int T,n,a[maxn],dp[maxn],num[maxn];
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int ans=;
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<n; i++)
{
dp[i]=;
num[i]=;
for(int j=; j<i; j++)
{
if(a[j]<a[i])
{
if(dp[j]+>dp[i])
{
num[i]=num[j];
dp[i]=dp[j]+;
}
else if(dp[j]+==dp[i])
{
num[i]+=num[j];
}
}
}
if(dp[i]>ans)
{
ans=dp[i];
}
}
int num1=;
for(int i=;i<n;i++)
{
if(dp[i]==ans)
{
num1+=num[i];
}
}
if(num1==)
{
printf("%d\n",ans-);
}
else
{
printf("%d\n",ans);
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
hdu5087 Revenge of LIS II (dp)的更多相关文章
- 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 ...
- 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 (LIS变形)
题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...
- hdu 5087 Revenge of LIS II (DP)
题意: N个数,求第二长上升子序列的长度. 数据范围: 1. 1 <= T <= 1002. 2 <= N <= 10003. 1 <= Ai <= 1 000 0 ...
- hdoj 5087 Revenge of LIS II 【第二长单调递增子】
称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...
- HDU 5078 Revenge of LIS II(dp LIS)
Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- hdu 5087 Revenge of LIS II
http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...
随机推荐
- Redis分布式锁的python实现
案例1: #!/usr/bin/env python # coding=utf-8 import time import redis class RedisLock(object): def __in ...
- 教你在Yii2.0框架中如何创建自定义小部件
本教程将帮助您创建自己的自定义小部件在 yii framework 2.0.部件是可重用的模块和用于视图. 创建一个小部件,需要继承 yii\base\Widget,覆盖重写 yii\base\Wid ...
- 【转】dmesg 时间转换
dmesg 时间转换 dmesg 输出的格式不易查看,可以通过命令进行转换. 记录如下: 时间查看: date -d "1970-01-01 UTC `echo "$(date + ...
- $Android自定义控件在不同状态下的属性
在写代码的时候,有时候需要控件在不同状态下显示不同的外观,比如在按钮按下的时候要变颜色,EditText获取焦点时候边框要变颜色等.那么下面就来梳理一下这些是怎么实现的. (一)按钮按下时候变颜色 1 ...
- 去重除了indexOf的其他方法(使用对象Key的方法)及统计重复次数
1.去重: 法1:使用数组IndexOf去重 法2:使用对象Key: <script> var arr1 = [1,13,24,11,11,14,1,2]; let unique = fu ...
- 主攻ASP.NET.4.5 MVC4.0之重生:二维码生成和谷歌二维码
使用ThoughtWorks.QRCode.Codec 效果图 using ThoughtWorks.QRCode.Codec; 非原创代码 public void code(string id) { ...
- 通过公钥解密密文思路(256bits RSA)
256bit RSA公钥安全系数极低,只需要几分钟即可破解密文,本文综合其他文章记录了一次解密256bits RSA加密的密文的过程,仅作为备忘. 1.分解公钥,分解出n与e: 1.1使用openss ...
- Java 重写equals()与hashCode()方法
List对象的contains方法实际上也是调用的equals()方法来进行逐条对比的. 示例代码: package com.imooc.collection; /** * 课程类 */ public ...
- Java Interface接口
Java 中接口概念 接口可以理解为一种特殊的 类,由 全局常量 和 公共的抽象方法 所组成. 类是一种具体实现体,而接口定义了某一批类所需要遵循的规范,接口不关心这些类的内部数据, 也不关心这些类里 ...
- 让FireFox支持 window.event 全局事件对象
这里比原文稍加改进,让FF也支持 event.srcElement了, 省得每次写兼容代码挺麻烦的: //For firefox window.event if(typeof(window.event ...