题目1533:最长上升子序列
2017-07-01 17:40
274 查看
区间dp,显然二次循环
代码如下
#include <iostream>
#include <queue>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <sstream>
#include <cstring>
using namespace std;
int nums[100001];
int dp[100001];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&nums[i]);
}
int len=0;
for(int i=1;i<=n;i++)
{
if(!len||(nums[i]>dp[len]))
dp[++len]=nums[i];
else
{
int ll=1;
int rr=len;
int mid;
while(ll<rr)
{
mid=(ll+rr)/2;
if(nums[i]>dp[mid])
{
ll=mid+1;
}
else if(nums[i]<dp[mid])
{
rr=mid;
}
else
break;
}
dp[ll]=min(nums[i],dp[ll]);
}
}
printf("%d\n",len);
}
return 0;
}会超时的,引入一个栈,复杂度为n*log(n)。
代码如下
#include <iostream>
#include <queue>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <sstream>
#include <cstring>
using namespace std;
int nums[100001];
int dp[100001];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&nums[i]);
}
int len=0;
for(int i=1;i<=n;i++)
{
if(!len||(nums[i]>dp[len]))
dp[++len]=nums[i];
else
{
int ll=1;
int rr=len;
int mid;
while(ll<rr)
{
mid=(ll+rr)/2;
if(nums[i]>dp[mid])
{
ll=mid+1;
}
else if(nums[i]<dp[mid])
{
rr=mid;
}
else
break;
}
dp[ll]=min(nums[i],dp[ll]);
}
}
printf("%d\n",len);
}
return 0;
}会超时的,引入一个栈,复杂度为n*log(n)。
相关文章推荐
- [poj 1533]最长上升子序列nlogn树状数组
- 题目1533:最长上升子序列-九度
- 题目1533:最长上升子序列 (nlogn | 树状数组)
- 竞赛题目讲解-【Northeastern Europe 2002, Far-Eastern Subregion】最长上升子序列
- YTU OJ 3135: 动态规划基础题目之最长上升子序列(Java解题)
- 最长上升子序列题目大合集
- 九度 1533 最长上升子序列
- wikioi 最长上升子序列题目
- YTU.3135: 动态规划基础题目之最长上升子序列
- 九度 1533:最长上升子序列
- 九度oj-1533 最长上升子序列 (LIS)
- Problem B: 动态规划基础题目之最长上升子序列
- POJ 2533 Longest Ordered Subsequence(最长上升子序列模版)
- 21280 Problem A 最长上升子序列
- 最长上升子序列
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP,LIS最长上升子序列)
- 最长上升子序列(模板)
- 动态规划之一最长上升子序列LIS
- UVA 10635 Prince and Princess (最长公共子序列+最长上升子序列)
- 最长上升子序列