UVa10534 Wavio Sequence

原题链接:UVa10534

题目大意:LIS 问题。

LIS 优化

本题的意义大概在于告诉我:LIS 怎么还有优化做法的???结果后来发现其实以前学过,但从来没用到就给忘了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 10010;

int n;
int a[N], q[N];
int f[N], g[N];

int main() {
    while (scanf("%d", &n) != EOF) {
        memset(f, 0, sizeof f);
        memset(g, 0, sizeof g);
        for (int i = 0; i < n; i++) scanf("%d", &a[i]);
        int len = 0;
        q[0] = -2e9;
        for (int i = 0; i < n; i++) {
            int l = 0, r = len;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                if (q[mid] < a[i]) l = mid;
                else r = mid - 1;
            }
            f[i] = r + 1;
            len = max(len, r + 1);
            q[r + 1] = a[i];
        }
        len = 0;
        q[n] = -2e9;
        for (int i = n - 1; i >= 0; i--) {
            int l = 0, r = len;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                if (q[mid] < a[i]) l = mid;
                else r = mid - 1;
            }
            g[i] = r + 1;
            len = max(len, r + 1);
            q[r + 1] = a[i];
        }
        int res = 1;
        for (int i = 1; i <= n; i++) {
            res = max(res, min(f[i], g[i]) * 2 - 1);
        }
        cout << res << endl;
    }
    return 0;
}
updatedupdated2020-05-192020-05-19
加载评论
点击刷新