hdu--1029--思维题

这题 可能是数据不够大的原因吧  一开始我直接sort一遍也过了...

这题 在一天一道算法题里曾经出现过..

    touch  me

O(n)时间复杂度的思想 很cool

 1 /*
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int arr[1000000];
 6 int main()
 7 {
 8     cin.sync_with_stdio(false);
 9     int n;
10     while( cin >> n )
11     {
12         for( int i = 0 ; i<n ; i++ )
13         {
14             cin >> arr[i];
15         }
16         sort(  arr , arr+n );
17         cout << arr[n/2] << endl;
18     }
19     return 0;
20 }
21 */
22 #include <iostream>
23 using namespace std;
24 
25 int main()
26 {
27     cin.sync_with_stdio(false);
28     int n , num , ans , cnt;
29     while( cin >> n )
30     {
31         cnt = 0;
32         while( n-- )
33         {
34             cin >> num;
35             if( 0 == cnt )
36             {
37                 ans = num;
38                 cnt ++;
39             }
40             else
41             {
42                 ans == num ? cnt++ : cnt--;
43             }
44         }
45         cout << ans << endl;
46     }
47     return 0;
48 }
View Code
posted @ 2014-07-24 20:22  radical  阅读(142)  评论(0编辑  收藏  举报