POJ3903(dp,最长上升子序列,最基础题)
来源:互联网 发布:java jstack 编辑:程序博客网 时间:2024/06/11 19:41
最长上升子序列问题:
定义dp[i]:以 a[i]为末尾的 最长上升子序列 的长度。
递推关系:
dp[i]=max{1,dp[j]+1} 当 i>j且a[i]>a[j]时
边界控制:
d[i]=1;
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int dp[100010];int a[100010];int main(){ int N; while (scanf("%d",&N)!=EOF) { int ans = 0; for (int i = 0; i < N; i++) scanf("%d", &a[i]); for (int i = 0; i < N; i++) { dp[i] = 1; for (int j = 0; j < i; j++) if (a[i] > a[j]) dp[i] = max(dp[i], dp[j] + 1); ans = max(ans, dp[i]); } printf("%d\n", ans); }}
优化:
dp[i]:长度为i+1(数组从0存放)的 上升子序列中 末尾元素的最小值,不存在的话就是INF;
边界控制:dp[i]=INF;
dp[i]=min(dp[i],a[j]) i=0或者dp[i-1]
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define INF 0x3f3f3f3fusing namespace std;int dp[100010];int a[100010];int main(){ int N; while (scanf("%d", &N) != EOF) { int ans = 0; for (int i = 0; i < N; i++) scanf("%d", &a[i]); fill(dp, dp + N, INF); for (int i = 0; i < N; i++) *lower_bound(dp, dp + N, a[i]) = a[i]; printf("%d\n", lower_bound(dp,dp+N,INF)-dp); }}
阅读全文
0 0
- POJ3903(dp,最长上升子序列,最基础题)
- POJ3903(dp最长上升子序列STL)
- POJ3903 Stock Exchange 最长上升子序列DP]
- poj3903 Stock Exchange(最长上升子序列)
- 最长上升子序列--poj2533、poj3903、hdu1025
- poj3903--Stock Exchange--nlogn贪心求最长上升子序列
- poj 2533 poj3903 poj1836 最长上升子序列 LIS
- 最长上升子序列 (dp)
- 最长公共上升子序列 (dp)
- 最长上升子序列(DP)
- HDU1087(最长上升子序列DP)
- 最长上升子序列LIS(dp)
- 最长上升子序列(DP+模板)
- YTU3135--最长上升子序列(dp)
- 最长上升子序列(dp)
- 最长公共上升子序列(dp)
- 最长上升子序列(DP)
- 最长上升子序列(Dp)
- 为当前用户创建cron服务
- Java文件下载选择路径注意事项
- maven debug操作需安装的插件
- Oracle中间件
- cocos2d学习笔记
- POJ3903(dp,最长上升子序列,最基础题)
- python中可变和不可变对象(复值,拷贝,函数值传递)
- PHP问题
- 基础巩固--接口基本特性
- pageEncoding和charset有什么区别
- java jdk版本更换
- 普通程序员如何入门深度学习?
- RecyclerView 模拟数据+长按点击事件+刷新加载
- 归并排序(链表)