poj 3670(LIS)
1 // File Name: 3670.cpp 2 // Author: Missa_Chen 3 // Created Time: 2013年07月08日 星期一 21时15分34秒 4 5 #include <iostream> 6 #include <string> 7 #include <algorithm> 8 #include <cstdio> 9 #include <cstring> 10 #include <cmath> 11 #include <queue> 12 #include <map> 13 #include <stack> 14 #include <set> 15 #include <cstdlib> 16 #include <vector> 17 #include <time.h> 18 19 using namespace std; 20 21 #define LL long long 22 const int inf = 0x3f3f3f3f; 23 const int maxn = 3e4 + 5; 24 int a[maxn], dp[maxn], D[maxn], len, n; 25 //dp[i]表示以第i个元素为子序列最后一个元素的LIS长度。 26 //D[len]表示上升子序列长度为len的最后一个元素。 27 int LIS() 28 { 29 len = 0; 30 memset(dp, 0, sizeof(dp)); 31 memset(D, 0, sizeof(D)); 32 int ret = 0; 33 for (int i = 1; i <= n; ++i) 34 { 35 if (a[i] >= D[len]) 36 { 37 dp[i] = len + 1; 38 D[++len] = a[i]; 39 } 40 else 41 { 42 int low = 1, high = len, tmp = 0; 43 while (low <= high) 44 { 45 int mid = (low + high) >> 1; 46 if (D[mid] <= a[i]) 47 { 48 tmp = mid; 49 low = mid + 1; 50 } 51 else 52 high = mid - 1; 53 } 54 dp[i] = tmp + 1; 55 D[tmp + 1] = a[i]; 56 } 57 ret = max(ret, dp[i]); 58 } 59 return ret; 60 } 61 int main() 62 { 63 while (~scanf("%d",&n)) 64 { 65 for (int i = 1; i <= n; ++i) 66 scanf("%d", &a[i]); 67 int ans = LIS(); 68 reverse(a + 1, a + n + 1); 69 ans = max(ans, LIS()); 70 printf("%d\n", n - ans); 71 } 72 return 0; 73 }