Title Link: Http://codeforces.com/problemset/problem/558/B
Title meaning: Give a sequence, and then find the most occurrences, but the interval occupies the shortest interval around the value.
Because it is a side-read-in-edge comparison, the most critical issue is to record where each number first appears, that is, the left value. Because you want to ensure that the number of occurrences is the most, you need a cnt[] array to record the number of occurrences. Then, when the maximum number of occurrences is the same as the current cnt[x], select a shorter interval and then update the left and right interval values.
The short circuit in the game unexpectedly can't come out ~ ~ ~ Tears ~ ~ Tears->_<
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <algorithm>6 using namespacestd;7 8 Const intMAXN = 1e6 +5;9 intCNT[MAXN];Ten intPL[MAXN];//record where each number first appears One A intMain () - { - #ifndef Online_judge theFreopen ("In.txt","R", stdin); - #endif //Online_judge - - intN, A; + while(SCANF ("%d", &n)! =EOF) { -memset (CNT,0,sizeof(CNT)); +memset (PL,0,sizeof(PL)); A intmax_cnt =0; at intMin_dis =0; - intL =0, r =0; - - for(inti =0; I < n; i++) { -scanf"%d", &a); - in if(!Cnt[a]) { -Pl[a] =i; to } +cnt[a]++; - the if(Max_cnt <Cnt[a]) { *max_cnt =Cnt[a]; $L = Pl[a], r =i;Panax NotoginsengMin_dis = i-tl; - } the if(max_cnt = = Cnt[a] && I-pl[a] <Min_dis) { +Min_dis = i-Pl[a]; AL =Pl[a]; theR =i; + } - } $printf"%d%d\n", L +1, r+1); $ } - return 0; -}
Codeforces 558B. AMR and the Large Array problem Solving report