我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

VIJOS——1303——(导弹防御DP)

Posted on 2008-08-20 21:26 Hero 阅读(315) 评论(1)  编辑 收藏 引用 所属分类: 代码如诗--ACM
 1 //Accepted  100 From 054100526-P1303  CPP
 2 
 3 //一个著名的定理是这样的:最长上升序列的长度等于不上升序列的最小分划
 4 //(即将平面上的点分划成尽可能少的不相交的不上升序列)
 5 
 6 //特殊数据
 7 /*
 8 关键是第二问,如果每次都求最靠前的最长下降序列再剔除,有些数据是过不了的,比如
 9 12 7 11 6 10 5 9
10 这样得出的答案是12 11 10 5、7 6、9(3套系统)
11 正确答案12 11 10 9、7 6 5(2套系统)
12 */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 const int size = 500 ;
18 int data[size] ;
19 
20 int dp[size] ;
21 
22 int fmax( int a, int b ) 
23 {
24     return a > b ? a : b ;
25 }
26 
27 int main()
28 {
29     memset( data, 0sizeof(data) ) ;
30     int cnt = 0 ; char ch ;
31     
32     while( scanf( "%d"&data[++cnt] ) != EOF )
33     {
34         ch = getchar() ; //printf( "ch == %c\n", ch ) ;
35         if'\n' == ch )    break ;
36         if( ch != ',' )        break ;
37     }
38 
39     while0 == data[cnt] ) cnt-- ;
40 
41     dp[1= 1 ;
42     forint i=2; i<=cnt; i++ )
43     {
44         dp[i] = 1 ;
45         forint j=1; j<i; j++ ) 
46         {
47             if( data[j]>=data[i] ) dp[i] = fmax( dp[i], dp[j]+1 ) ;
48         }
49     }//最长不下降序列
50 
51     int maxlen = -1 ;
52 
53     forint i=1; i<=cnt; i++ ) maxlen = fmax( maxlen, dp[i] ) ;
54     printf( "%d,", maxlen ) ;
55 
56     dp[1= 1 ;
57     forint i=2; i<=cnt; i++ )
58     {
59         dp[i] = 1 ;
60         forint j=1; j<i; j++ )
61         {
62             if( data[j]<data[i] ) dp[i] = fmax( dp[i], dp[j]+1 ) ;
63         }
64     }//最长上升序列==不下降序列的最小分划
65 
66     maxlen = -1 ; 
67 
68     forint i=1; i<=cnt; i++ )    maxlen = fmax( maxlen, dp[i] ) ;
69     printf( "%d\n", maxlen-1 ) ;
70 
71     return 0 ;
72 }

Feedback

# re: VIJOS——1303——(导弹防御DP)  回复  更多评论   

2014-08-16 22:15 by 111111
最长上升序列的长度等于不上升序列的最小分划 应该是 最长上升序列的长度等于不上升序列的最小分划 +1 啊

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理