USACO 2.1 Sorting a Three-Valued Sequence (乱搞)

  很不错的一个题。开始自己想了一个策略,然后写了150+代码。。。WA后,发现策略可能有问题,貌似有更简单的办法,自己想的策略是错的,敲的各种if else,想清楚再写啊。。。这个题目应该算是贪心把。

 1 /*
 2  ID: cuizhe
 3  LANG: C++
 4  TASK: sort3
 5 */
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <cmath>
 9 #include <algorithm>
10 using namespace std;
11 int p[1011],o[4];
12 int main()
13 {
14     int i,n,ans,x12,x13,x21,x23,x31,x32;
15     freopen("sort3.in","r",stdin);
16     freopen("sort3.out","w",stdout);
17     scanf("%d",&n);
18     for(i = 1; i <= n; i ++)
19     {
20         scanf("%d",&p[i]);
21         o[p[i]]++;
22     }
23     ans = 0;
24     x12 = x13 = x21 = x23 = x31 = x32 = 0;
25     for(i = 1;i <= o[1];i ++)
26     {
27         if(p[i] == 2)
28         {
29             x12 ++;
30         }
31         else if(p[i] == 3)
32         {
33             x13 ++;
34         }
35     }
36     for(i = o[1]+1;i <= o[1]+o[2];i ++)
37     {
38         if(p[i] == 1)
39         {
40             x21 ++;
41         }
42         else if(p[i] == 3)
43         {
44             x23 ++;
45         }
46     }
47     for(i = o[2]+o[1]+1;i <= n;i ++)
48     {
49         if(p[i] == 1)
50         {
51             x31++;
52         }
53         else if(p[i] == 2)
54         {
55             x32 ++;
56         }
57     }
58     ans = x12+x13+max(x23,x32);
59     printf("%d\n",ans);
60     return 0;
61 }
posted @ 2012-11-06 09:04  Naix_x  阅读(174)  评论(0编辑  收藏  举报