HDU 1029 Ignatius and the Princess IV---water problem

Source: Internet
Author: User

HDU 1029

Title: Given the number n (n <= 999999 and n is odd) and the number of N, find at least (n+1)/2 times

How to solve the problem: N number traversal in the past, you can use a map (you can also use an array) to record the number of times each occurrence,

If the number of times is reached (n+1)/2, i.e. output A[I]

Note can appear (n+1)/2 times the number of up to only one

/*HDU 1029 *ignatius and the Princess IV---dp*/#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespacestd;Const intMAXN =1000005;intA[maxn];map<int,int>m;intMain () {#ifdef _local freopen ("D:\\input.txt","R", stdin);#endif    intN; intans;  while(SCANF ("%d", &n) = =1) {m.clear (); //Remember to empty the original stuff.         for(inti =1; I <= N; ++i) {scanf ("%d", A +i); ++M[a[i]]; if(M[a[i]] = = (n +1) /2) {ans=A[i]; }        }//For (i)printf"%d\n", ans); }    return 0;}
View Code

HDU 1029 Ignatius and the Princess IV---water problem

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.