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