sdut 2420 Dining Cows

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2420

代码:

View Code
 1 #include<stdio.h>
 2 struct node
 3 {
 4     int x;
 5     int l1;//记录i点左面1的数目
 6     int l2;//记录i点左面2的数目
 7 }a[30010];
 8 int main()
 9 {
10     int i,n,max,sum;
11     scanf("%d",&n);
12     a[0].l1=0;
13     a[0].l2=0;//空出第0个节点
14     
15     for(i=1;i<=n;i++)
16     {
17         scanf("%d",&a[i].x);
18         if(a[i].x==1)
19         {
20             a[i].l1=a[i-1].l1+1;
21             a[i].l2=a[i-1].l2;
22         }
23         else
24         {
25             a[i].l2=a[i-1].l2+1;
26             a[i].l1=a[i-1].l1;
27         }
28     }
29     max=0;
30     for(i=0;i<=n;i++)
31     {
32         sum=a[i].l1+a[n].l2-a[i].l2;//计算i点左面1和右面二的数目,找出最大
33     
34         if(max<sum)
35             max=sum;
36     }
37     printf("%d\n",n-max);
38 
39     return 0;
40 }
posted @ 2012-08-22 21:10  琳&leen  阅读(137)  评论(0编辑  收藏  举报