UVa 497 - Strategic Defense Initiative

  题目大意:就是给出一个序列,求最长递增子序列,并打印方案。

 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAXN 100000
 4 
 5 int a[MAXN], lis[MAXN], pre[MAXN];
 6 
 7 void print_lis(int p)
 8 {
 9     if (pre[p] != -1)  print_lis(pre[p]);
10     printf("%d\n", a[p]);
11 }
12 
13 int main()
14 {
15 #ifdef LOCAL
16     freopen("in", "r", stdin);
17 #endif
18     int T;
19     scanf("%d", &T);
20     getchar();
21     char str[100];
22     gets(str);
23     while (T--)
24     {
25         int n = 0;
26         while (gets(str) && str[0])
27         {
28             sscanf(str, "%d", &a[n]);
29             n++;
30         }
31         memset(pre, -1, sizeof(pre));
32         for (int i = 0; i < n; i++)
33         {
34             lis[i] = 1;
35             for (int j = 0; j < i; j++)
36                 if (a[i] > a[j] && lis[j]+1 > lis[i])
37                 {
38                     lis[i] = lis[j] + 1;
39                     pre[i] = j;
40                 }
41         }
42         int p = 0;
43         for (int i = 1; i < n; i++)
44             if (lis[i] > lis[p])  p = i; 
45         printf("Max hits: %d\n", lis[p]);
46         print_lis(p);
47         if (T)  printf("\n");
48     }
49     return 0;
50 }
View Code

 

posted @ 2013-09-08 16:06  xiaobaibuhei  阅读(578)  评论(0编辑  收藏  举报