pku3670 Eating Together

http://poj.org/problem?id=3670

DP,最长不降子序列,O(n*logn)解法

 1 #include <stdio.h>
 2 #define N 30030
 3 
 4 int n, a[N], dp[N];
 5 const int maxint = (1<<31)-1;
 6 
 7 int bs(int l ,int r, int x)
 8 {
 9     int m;
10     while(l < r)
11     {
12         m = (l + r)>>1;
13         if(dp[m] <= x)
14         {
15             l = m + 1;
16         }
17         else
18         {
19             r = m;
20         }
21     }
22     return l;
23 }
24 
25 int LIS()
26 {
27     int i, j, maxn = 1;
28     dp[0] = 0;
29     dp[1] = maxint;
30     for(i=1; i<=n; i++)
31     {
32         j = bs(0, maxn+1, a[i]);
33         dp[j] = a[i];
34         if(j > maxn)
35         {
36             maxn ++;
37             dp[maxn+1] = maxint;
38         }
39     }
40     return maxn;
41 }
42 
43 int main()
44 {
45     int t, i, j;
46     int r1, r2;
47     while(~scanf("%d", &n))
48     {
49         for(i=1; i<=n && scanf("%d", a+i); i++);
50         r1 = LIS();
51         for(i=1; i<=n/2; i++)
52         {
53             j = n-i+1;
54             //printf("%d %d\n", i, j);
55             a[i] ^= a[j], a[j] ^= a[i], a[i] ^= a[j];
56         }
57         r2 = LIS();
58         printf("%d\n", n - (r1>r2? r1: r2));
59     }
60     return 0;
61 }

 

posted @ 2013-07-27 19:30  Yuan1991  阅读(177)  评论(0编辑  收藏  举报