POJ 2533 —— Longest Ordered Subsequence 最长上升子序列裸题
2016-02-20 23:49
447 查看
原题:http://poj.org/problem?id=2533
题意:求最长递增子序列
注意:n = 0 n = 1 答案都是1
O(n²)
O(nlogn)
用lower_bound()
题意:求最长递增子序列
注意:n = 0 n = 1 答案都是1
O(n²)
#include<cstdio> const int maxn = 1100; int n; int dp[maxn]; int a[maxn]; int main() { while(~scanf("%d", &n)) { int ans = 0; for(int i = 1;i<=n;i++) { scanf("%d", &a[i]); dp[i] = 1; } if(n == 0 || n == 1) { printf("1\n"); continue; } for(int i = 2;i<=n;i++) { dp[i] = 1; for(int j = 1;j<i;j++) { if(a[i] > a[j] && dp[i] < dp[j] + 1) dp[i] = dp[j] + 1; if(ans < dp[i]) ans = dp[i]; } } printf("%d\n", ans); } return 0; }
O(nlogn)
<span style="font-size:10px;">#include<cstdio> const int maxn = 1100; int stack[maxn]; int n; int main() { while(~scanf("%d", &n)) { int top = 0; stack[0] = -1; for(int i = 1;i<=n;i++) { int temp; scanf("%d", &temp); if(temp > stack[top]) stack[++top] = temp; else { int l = 1, r = top; int mid; while(l <= r) { mid = (l + r) / 2; if(temp > stack[mid]) l = mid + 1; else r = mid - 1; } stack[l] = temp; } } printf("%d\n", top); } return 0; }</span>
用lower_bound()
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 1100; int a[maxn], stack[maxn]; int n; int main() { while(~scanf("%d", &n)) { for(int i = 0;i<n;i++) scanf("%d", &a[i]); int top = 0; stack[0] = -1; for(int i = 0;i<n;i++) { if(a[i] > stack[top]) { stack[++top] = a[i]; continue; } int pos = lower_bound(stack, stack+top, a[i]) - stack; stack[pos] = a[i]; } printf("%d\n", top); } return 0; }
相关文章推荐
- java中continue配合标签进行继续循环
- Handler、Looper和MessageQueue
- 为什么在jsp中写${pageContext.request.contextPath }失效了
- [置顶] NGUI另一种按钮事件触发的方法
- 1007. Maximum Subsequence Sum (25)
- Android Wear 进阶 - 3.1 Creating Custom UIs for Wear Devices-Defining Layouts 创建自定义的手表设备UIs-定义布局
- 【Codeforces#38G】Queue【Splay】【二分】
- [iOS]UIView中的坐标转换
- fixed fluid layout
- iOS-获取UIView的全部层级结构
- UI整理-----part4--UIScrollView
- EasyUI系列学习(十)-Tabs(选项卡)
- 设置UITbaBar和UIBarButtonItem的图片有蓝色图层覆盖
- 百度ueditor学习使用
- LeetCode-334. Increasing Triplet Subsequence
- UINavi中push控制器的时候隐藏TabBar
- leetcode(304) Range Sum Query 2D - Immutable
- 使用 Bluemix™ Live Sync 快速更新 Bluemix 上运行的应用程序实例
- Arduino代码机制-IO
- android小问题--------------------SQLiteDatabase.insert(table, nullColumnHack, values)参数