POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 38980 | Accepted: 17119 |
Description
be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence
(1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
Output
Sample Input
- 7
- 1 7 3 5 9 4 8
Sample Output
- 4
Source
- #include<iostream>
- #include<algorithm>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<queue>
- using namespace std;
- int n;
- int a[1001];
- int dp[1010];
- int main()
- {
- while(scanf("%d",&n)!=EOF)
- {
- int maxx = -1;
- 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[j]+1>dp[i])
- {
- dp[i] = dp[j] + 1;
- }
- }
- if(maxx < dp[i])
- {
- maxx = dp[i];
- }
- }
- printf("%d\n",maxx);
- }
- return 0;
- }
- #include<iostream>
- #include<algorithm>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define inf 999999
- using namespace std;
- int n;
- int dp[1010];
- int a[1010];
- int res(int len,int num)
- {
- int l = 0,r = len;
- while(l!=r)
- {
- int mid = (l+r)>>1;
- if(dp[mid] == num)
- {
- return mid;
- }
- else if(dp[mid]<num)
- {
- l = mid + 1;
- }
- else if(dp[mid]>num)
- {
- r = mid;
- }
- }
- return l;
- }
- int main()
- {
- while(scanf("%d",&n)!=EOF)
- {
- for(int i=1;i<=n;i++)
- {
- scanf("%d",&a[i]);
- }
- int len = 1;
- dp[0] = -1;
- for(int i=1;i<=n;i++)
- {
- dp[i] = inf;
- int k = res(len,a[i]);
- if(k == len)
- {
- len++;
- }
- dp[k] = a[i];
- }
- printf("%d\n",len-1);
- }
- return 0;
- }
POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)的更多相关文章
- 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...
- POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)
传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...
- POJ - 2533 Longest Ordered Subsequence(最长上升子序列)
d.最长上升子序列 s.注意是严格递增 c.O(nlogn) #include<iostream> #include<stdio.h> using namespace std; ...
- POJ2533 Longest Ordered Subsequence —— DP 最长上升子序列(LIS)
题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- poj 2533 Longest Ordered Subsequence(dp)
题目:http://poj.org/problem?id=2533 题意:最长上升子序列.... 以前做过,课本上的思想 #include<iostream> #include<cs ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- POJ 2533 Longest Ordered Subsequence(裸LIS)
传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- 【POJ - 2533】Longest Ordered Subsequence (最长上升子序列 简单dp)
Longest Ordered Subsequence 搬中文 Descriptions: 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序 ...
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
随机推荐
- HTML学习笔记 基础标签及css引用案例 第一节 (原创)参考使用表
<!DOCTYPE html><!--头文件 不是标签 也没有结束,这是声明该文件为HTML5--><html lang="en"><!- ...
- 享受Python和PHP动态类型检查语言的快感
前言 写这文章的时候特地查了资料,以确保我没有说错关于Python和PHP的类型机制. 所以这里放一张图,关于强弱类型与动态/静态类型检查的区分 从分类上看,PHP属于弱类型语言,而Python属于强 ...
- .Net Core2.0秒杀CMS部署到Centos7.3遇到的坑,酸爽呀
一.Centos7.3的安装 打开VirtualBox,点击新建,如下图所示: 点击“下一步”,弹出下面的对话框,调整内存大小,建议设置为2G,这样操作更流畅点 设置好,点击“OK”,再点击“启动”, ...
- C#提取字符串中的数字字符串
1 }
- [转载] FreeMarker教程
转载自http://www.blogjava.net/freeman1984/archive/2010/11/04/337239.html FreeMarker是一个模板引擎,一个基于模板生成文本输出 ...
- [转]我在面试.NET/C#程序员时会提出的问题
http://blog.zhaojie.me/2011/03/my-interview-questions-for-dotnet-programmers.html 说起来我也面试过相当数量的.NET( ...
- java 之 简单工厂模式(大话设计模式)
以前只是看设计模式,每次看完都去理解一次,并没有手动去写代码,所以理解的还不是很深刻,最近查看框架源码,发现很多地方用到的都是设计模式,因为对设计模式理解的不够深刻,所以源码查看进度很慢!现在决定来温 ...
- Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)
来源自:http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Most network-connected ...
- unity3d资源打包总结
http://www.manew.com/blog-33734-12973.html unity 打包的时候会把下面几个文件资源打进apk或者ipa包里面 1. Asset下的所有脚本文件 2. As ...
- 史上最全常用正则表达式(Javascript公众号推文)
2017-04-13 zxin JavaScript很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下 ...