当前位置: 代码迷 >> 综合 >> POJ 2533 Longest Ordered Subsequence(最长上升子序列长度、O(nlogn))
  详细解决方案

POJ 2533 Longest Ordered Subsequence(最长上升子序列长度、O(nlogn))

热度:74   发布时间:2023-12-08 10:27:12.0

题目链接:
POJ 2533 Longest Ordered Subsequence
题意;
求最长上升子序列长度。
数据范围: n1000
分析:
直接用 O(nlog(n)) 的方法,在数组中二分查找并替换,使得最长上升子序列的“潜力”更大。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 100010;int arr[MAX_N], data[MAX_N];
int n;int main()
{while(~scanf("%d", &n)) {for (int i = 0; i < n; ++i) {scanf ("%d", &data[i]);}int len = 1;arr[0] = data[0];for(int i = 1; i < n; ++i) {int pos = lower_bound(arr, arr + len, data[i]) - arr;len = max(len, pos + 1);arr[pos] = data[i];}printf("%d\n", len);}return 0;
}
  相关解决方案