Hdu-1029-ignatius and the Princess IV (c + + && tips)

Source: Internet
Author: User

Ignatius and the Princess IVTime limit:2000/1000 MS (java/others) Memory limit:65536/32767 K (java/others)
Total submission (s): 20489 Accepted Submission (s): 8441


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 Input
51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1

Sample Output
351

Authorignatius.l
Recommendwe carefully selected several similar problems for you:1040 1074 1003 1028 1171
The main meaning of the topic is: the first line read into a n,n is odd, and then the next line read N number, constitute a set of series, and then your goal is to find out the number of times in this series of numbers is at least (n+1)/2 times, it is worth noting that the test data is huge, accidentally will time out!
Look at the two algorithms, the first is the use of array storage, and then directly n+1/2 the next number, output! Although can live, but personally think the topic and no other conditions, this is too water ...

The second algorithm is as follows: (Code source Network, not my original)
#include <iostream> #include <cstdio>using namespace Std;int main () {    int nums;    while (cin>>nums)    {        int cur,max=0,count=0;        for (int i=0;i<nums;i++)        {            cin>>cur;            if (count==0)            {                max=cur;                count++;            }            else            {                if (Cur!=max)                {                    count--;                }                else                {                    count++;}}        }        cout<<max<<endl;    }    return 0;}

The idea of this method is this, read the number, encounter the same count++, the difference is count--, once the count==0 is replaced by a number as Max, because N is an odd number, requires that one count appear at least (n+1)/2 times. Use count to record the number of occurrences, the correct number of occurrences must be no less than 1. Why is it? Because the correct solution appears at least (n+1)/2 times, even if the last n+1/2-1 number is not the same as the previous number, count will not be 0. For example: 11
3 3 3 3 3 3 9 5 1 2 7 when max = 3, count=6, even if the last count=1 is not 0,max=3! Will not enter (count==0) This module!

Hdu-1029-ignatius and the Princess IV (c + + && tips)

Related Article

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.