HDU 1029 Basic DP

Source: Internet
Author: User

Title Link: Ignatius and the Princess IV

The main idea is to find the only number in the N number that appears at least (n+1)/2 times. 1 <= N <= 999999.

Hash

1 /*2 Test instructions understood. is to find the only number in the N number that appears at least (n+1)/2 times. 1 <= N <= 999999.3 If I use a map tag. Maybe it's just 10^6. 4 However, it is still stuck with CIN and cout. 5  */6 7#include <stdio.h>8#include <string.h>9#include <iostream>Ten#include <map> One using namespacestd; A  -map<int,int>MP; -  the intMain () { -     intN; -     intnum; -     intans; +  -      while(~SCANF ("%d", &N)) { + mp.clear (); A          for(intI=0; i<n; ++i) { atscanf"%d", &num); -mp[num]++; -             if(Mp[num] >= (n+1)/2) {//If you find it, you can't break it. Because you may not have finished typing, -Ans =num; -             } -         } inprintf"%d\n", ans); -     } to     return 0; +}
View Code

Dp:

1 /*2 get to a very ingenious method. Follow test instructions. The number I asked for was the most frequently seen in the numbers,3 Compare the number of occurrences of the current number with the most number in all the preceding numbers at a time. 4 Judging from the process? is the dynamic planning of this problem? 5  */6 7#include <stdio.h>8#include <string.h>9#include <string.h>Ten#include <iostream> One using namespacestd; A  - intMain () { -     intN; the     intnum, CNT, REM; -      while(~SCANF ("%d", &N)) { -CNT =0; -          for(intI=0; i<n; ++i) { +scanf"%d", &num); -             if(CNT = =0) {//Count +REM =num; ACNT =1; at             } -             Else if(num = =REM) { -cnt++; -             } -             Else { -cnt--; in             } -         } toprintf"%d\n", REM); +     } -     return 0; the}
View Code

HDU 1029 Basic DP

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.