PAT1054

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1054

记录爆搜都不行,思路转变下,如果一个数的比例超过一半,那么每次删除2个不一样的树,剩下的就是那个数了,一次遍历即可。

 

 1 #include<cstdio>
 2 
 3 int a[500000];
 4 
 5 int main()
 6 {
 7     int M,N;
 8     scanf("%d%d", &M,&N);
 9     for(int i=0; i<M*N; ++i)
10         scanf("%d", &a[i]);
11     int cad(a[0]), counts(1);
12     /*排除两对不一样的*/
13     for(int i=1; i<M*N; ++i)
14     {
15         if(counts == 0)
16             cad=a[i];
17         else if(a[i] == cad)
18             ++counts;
19         else
20             --counts;
21     }
22     printf("%d\n", cad);
23     return 0;
24 }

 

posted @ 2013-10-26 20:06  coding_monkey  阅读(182)  评论(0编辑  收藏  举报