标签:
1 /*
2 LIS:应用,nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理。
3 因为是对称的,所以取较小值*2-1再取最大值
4 */
5 /************************************************
6 * Author :Running_Time
7 * Created Time :2015-8-5 21:38:32
8 * File Name :UVA_10534.cpp
9 ************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 1e4 + 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 int a[MAXN];
37 int d[MAXN];
38 int dp[MAXN], dp2[MAXN];
39
40 int main(void) { //UVA 10534 Wavio Sequence
41 int n;
42 while (scanf ("%d", &n) == 1) {
43 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);
44 memset (d, 0, sizeof (d));
45 memset (dp, 0, sizeof (dp));
46 memset (dp2, 0, sizeof (dp2));
47 d[1] = a[1]; int len = 1; dp[1] = 1;
48 for (int i=2; i<=n; ++i) {
49 if (d[len] < a[i]) d[++len] = a[i];
50 else {
51 int j = lower_bound (d+1, d+1+len, a[i]) - d;
52 d[j] = a[i];
53 }
54 dp[i] = len;
55 }
56 d[1] = a[n]; int len2 = 1; dp2[n] = 1;
57 for (int i=n-1; i>=1; --i) {
58 if (d[len2] < a[i]) d[++len2] = a[i];
59 else {
60 int j = lower_bound (d+1, d+1+len2, a[i]) - d;
61 d[j] = a[i];
62 }
63 dp2[i] = len2;
64 }
65 int ans = 0;
66 for (int i=1; i<=n; ++i) {
67 ans = max (ans, min (dp[i], dp2[i]) * 2 - 1);
68 }
69 printf ("%d\n", ans);
70 }
71
72 return 0;
73 }
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4706104.html