Hdu1029:ignatius and the Princess IV (find out more than half occurrences of the number in the array)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.