POJ 2533 Longest Ordered Subsequence
2015-01-27 21:46
176 查看
Longest Ordered Subsequence
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.
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
Sample Output
Source
Northeastern Europe 2002, Far-Eastern Subregion
解题报告:求最长上升子序列有几种方法,LIS的O(n^2) 、排序+LCS O(n^2) LIS的O(nlogn)。
首先是LIS的O(n^2)是最普遍的做法。
dp
表示前n个数字能组成的最长上升子序列个数。
只需要知道dp[1] ~dp[n-1]中的最大值 ,加上1就是当前的dp
。
另外一种n^2的排序+LCS做法。
只要理解:最大单调递增子序列一定是排序后的串的子序列。
所以最大上升子序列,实质就是原串和排序后的串的最大公共子序列。
注意!!!有序串需要去重,这样求出的最大公共子序列才是严格单调递增的。
最后一种做法就是把其中的一个n变成logn,用的是二分的思想。具体这个博客上写的很清楚:
http://blog.csdn.net/dongmianshu/article/details/5954992
核心是:数组d[i] = j 表示长度为i的子序列最小末尾是j,虽然长度为i的序列的末尾不一定是j,但是j的话是满足的,用的是贪心。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 35275 | Accepted: 15481 |
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.
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
Source
Northeastern Europe 2002, Far-Eastern Subregion
解题报告:求最长上升子序列有几种方法,LIS的O(n^2) 、排序+LCS O(n^2) LIS的O(nlogn)。
首先是LIS的O(n^2)是最普遍的做法。
dp
表示前n个数字能组成的最长上升子序列个数。
只需要知道dp[1] ~dp[n-1]中的最大值 ,加上1就是当前的dp
。
#include <cstdio> #define MAXN 1005 #define max(a, b) (a > b ? a : b) int arr[MAXN], dp[MAXN]; int main() { int n; int ans = 1; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", arr + i); dp[i] = 1; for (int j = 0; j < i; ++j) { if (arr[i] > arr[j]) { dp[i] = max(dp[i], dp[j] + 1); } } ans = max(dp[i], ans); } printf("%d\n", ans); return 0; }
另外一种n^2的排序+LCS做法。
只要理解:最大单调递增子序列一定是排序后的串的子序列。
所以最大上升子序列,实质就是原串和排序后的串的最大公共子序列。
注意!!!有序串需要去重,这样求出的最大公共子序列才是严格单调递增的。
#include <stdio.h> #include <algorithm> using namespace std; #define MAXN 1005 int n, i , j; int arr[MAXN], sortArr[MAXN], dp[MAXN][MAXN]; int main() { scanf("%d", &n); for (i = 0; i < n; i ++) { scanf("%d", arr + i); sortArr[i] = arr[i]; } sort(sortArr, sortArr + n); int sortLen = unique(sortArr, sortArr + n) - sortArr; for (i = 1; i <= n; i ++) { for (j = 1; j <= sortLen; j ++) { if (arr[i-1] == sortArr[j-1]) { dp[i][j] = dp[i-1][j-1] + 1; } else { dp[i][j] = max(dp[i][j-1], dp[i-1][j]); } } } printf("%d\n", dp [sortLen]); return 0; }
最后一种做法就是把其中的一个n变成logn,用的是二分的思想。具体这个博客上写的很清楚:
http://blog.csdn.net/dongmianshu/article/details/5954992
核心是:数组d[i] = j 表示长度为i的子序列最小末尾是j,虽然长度为i的序列的末尾不一定是j,但是j的话是满足的,用的是贪心。
#include <stdio.h> #include <algorithm> using namespace std; #define MAXN 1005 int n, ans, cur; int arr[MAXN], b[MAXN]; //inline int biSearch(int d) //{ // int left = 0, right = ans, mid; // while(left < right) // { // mid = (left + right) >> 1; // if(b[mid] < d) // { // left = mid + 1; // } // else // { // right = mid; // } // } // return left; //} int main() { scanf("%d", &n); for (int i = 0; i < n; i ++) { scanf("%d", arr + i); } ans = 1; b[0] = arr[0]; for (int i = 1; i < n; i ++) { cur = lower_bound(b, b + ans, arr[i]) - b; // cur = biSearch(arr[i]); b[cur] = arr[i]; if (cur == ans) ans ++; } printf("%d\n", ans); return 0; }
相关文章推荐
- POJ - 2533 Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence 二分查找(LIS nlogn算法)
- Longest Ordered Subsequence - POJ 2533 最长上升子序列
- POJ 2533 Longest Ordered Subsequence
- poj 2533 Longest Ordered Subsequence(LIS(最长上升子序列))
- poj 2533 Longest Ordered Subsequence(LIS)
- poj 2533 Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence (LIS)
- POJ 2533 - Longest Ordered Subsequence(dp 最长递增子序列)
- POJ 2533 Longest Ordered Subsequence
- 【POJ 2533】Longest Ordered Subsequence(最长上升子序列LIS)
- POJ 2533 Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence(LIS)
- poj 2533 Longest Ordered Subsequence(dp)
- POJ 2533 Longest Ordered Subsequence(最长递增子序列)
- POJ 2533 Longest Ordered Subsequence - from lanshui_Yang
- 【poj 2533】Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence
- POJ-2533-Longest Ordered Subsequence