[2016-03-11][POJ][2533][Longest Ordered Subsequence]
[2016-03-11][POJ][2533][Longest Ordered Subsequence]
| Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) 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.
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input
7 1 7 3 5 9 4 8
Sample Output
4
- 时间:2016-03-11 09:54:02 星期五
- 题目编号:POJ 2533
- 题目大意:给定一个序列,求最长上升子序列的长度
- 输入:序列的长度 序列
- 输出:最大长度
- 分析:最长上升子序列,DP
- 方法:
- 方法1.DP,dp[i]表示以第i个字符结尾的子序列的长度,dp[i] = max(dp[i],dp[j] + 1),j = 1 ,2,3..,i a[i] > a[j]
- 方法2,贪心?,nlogn,从开头扫向结尾,每次读取到一个新的,更大的值就把它入栈,每次读取到的值比栈顶的元素要小,就把栈里面第一个不大于它元素替换掉,最后计算栈里面有多少元素
//dp #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; typedef long long LL; #define CLR(x,y) memset((x),(y),sizeof((x))) #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x)) #define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x)) #define FOR2(x,y,z) int (x);for((x)=(y);(x)<(z);++(x)) #define FORD2(x,y,z) int (x);for((x)=(y);(x)>=(z);--(x)) const int maxn = 1000 + 100; int a[maxn],dp[maxn]; int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; scanf("%d",&n); FOR(i,0,n){ scanf("%d",a+i); } for(int i = 0; i < n ;++i){ dp[i] = 1; for(int j = 0 ; j < n ;++j){ if(a[i] > a[j]){ dp[i] = max(dp[i],dp[j] + 1); } } } int ans = 0; for(int i = 0;i < n;++i){ ans = max(ans,dp[i]); } printf("%d\n",ans); return 0; } |
//贪心? #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; typedef long long LL; #define CLR(x,y) memset((x),(y),sizeof((x))) #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x)) #define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x)) #define FOR2(x,y,z) int (x);for((x)=(y);(x)<(z);++(x)) #define FORD2(x,y,z) int (x);for((x)=(y);(x)>=(z);--(x)) const int maxn = 1000 + 100; int a[maxn],stk[maxn]; int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,pcur = 0;//pcur,栈顶的位置 scanf("%d",&n); FOR(i,0,n){ scanf("%d",a+i); } for(int i = 0;i < n;++i){ if(!pcur){ stk[pcur++] = a[i]; }else { if(a[i] > stk[pcur - 1]){ stk[pcur++] = a[i]; }else if(a[i] < stk[pcur - 1]){ int pos = lower_bound(stk,stk + pcur,a[i]) - stk; stk[pos] = a[i]; } } } printf("%d\n",pcur); return 0; } |
浙公网安备 33010602011771号