LIS UVA 10534 Wavio Sequence
题意:找对称的,形如:123454321 子序列的最长长度
分析:LIS的nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理。因为是对称的,所以取较小值*2-1再取最大值
代码:
/************************************************ * Author :Running_Time * Created Time :2015-8-5 21:38:32 * File Name :UVA_10534.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1e4 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int a[MAXN]; int d[MAXN]; int dp[MAXN], dp2[MAXN]; int main(void) { //UVA 10534 Wavio Sequence int n; while (scanf ("%d", &n) == 1) { for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); memset (d, 0, sizeof (d)); memset (dp, 0, sizeof (dp)); memset (dp2, 0, sizeof (dp2)); d[1] = a[1]; int len = 1; dp[1] = 1; for (int i=2; i<=n; ++i) { if (d[len] < a[i]) d[++len] = a[i]; else { int j = lower_bound (d+1, d+1+len, a[i]) - d; d[j] = a[i]; } dp[i] = len; } d[1] = a[n]; int len2 = 1; dp2[n] = 1; for (int i=n-1; i>=1; --i) { if (d[len2] < a[i]) d[++len2] = a[i]; else { int j = lower_bound (d+1, d+1+len2, a[i]) - d; d[j] = a[i]; } dp2[i] = len2; } int ans = 0; for (int i=1; i<=n; ++i) { ans = max (ans, min (dp[i], dp2[i]) * 2 - 1); } printf ("%d\n", ans); } return 0; }
编译人生,运行世界!