Hdu1029:ignatius and the Princess IV
Kuangbin Topic 12 Basic DP:B problem
Time limit:2000/1000 MS (java/others) Memory limit:65536/32767 K (java/others)
Total submission (s): 35107 Accepted Submission (s): 15340
Problem Description "OK, you aren't too bad, em ... But can never pass the next test. "Feng5166 says.
"I'll tell you a odd number N, and then N integers. There'll be a special integer among them and you've to tell me which an integer is the Special one after I tell you all the integers. "feng5166 says.
"What's the characteristic of the special integer?" Ignatius asks.
"The integer would appear at least (n+1)/2 times. If you can ' t find the right integer, I'll kill the princess, and you'll be my dinner, too. Hahahaha ... "feng5166 says.
Can you find the special integer for Ignatius?
Inputthe input contains several test cases. Each test case contains the lines. The first line consists of an odd integer N (1<=n<=999999) which indicate the number of the integers feng5166 would te ll our hero. The second line contains the N integers. The input is terminated by the end of file.
Outputfor each of the test case and you had to output only one line which contains the special number you had found.
Sample Input51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1
Sample Output351 Test Instructions: Give odd numbers, there will be a number of more than half, ask this number is what the idea: the same number is always greater than the sum of the number of different numbers, so encountered the same number sum + + number of various numbers sum--sum==0 When you change the number, the last number must be the one with the most occurrences of the/*s. The main element is the element that appears more than N/2 in the number of columns
It's easy to see that if you remove 2 different elements in a sequence,
So many elements in the original sequence, in the new sequence, are still many elements,
So all we have to do is scan the sequence sequentially and assign T to result first.
Add a counter, CNT = 1, then right-scan,
If the same as result, then cnt++, different, then CNT--,
This is really what we draw from the above conclusion, once CNT = = 0,
Then must c is not multi-element, this time the t assignment is result,cnt = 1;
Repeat the process, knowing the end, this time, result is a multi-element,
The time complexity of this is N, which can be used to save each element in an array,
And then recursion the above process, but, with the array hyper memory,
Therefore, we can directly follow the above process calculation */
#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;intMain () {intN; intsum; intnum; intNew_num; while(~SCANF ("%d", &N)) { for(intI=1; I<=n; i++) {scanf ("%d", &num); if(i==1) {New_num=num; Sum=1; } if(num = =new_num) {Sum++ ; } Else if(Sum >0) {sum-- ; } Else if(Sum = =0) {New_num=num; }} printf ("%d\n", New_num); } return 0 ; }
Hdu1029:ignatius and the Princess IV (find out more than half occurrences of the number in the array)