题目链接:codeforces558B
用cnt[ ]记录出现次数,当最大次数相等时比较最短距离,更新区间。
<pre name="code" class="cpp">#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int cnt[1000005];
int sp[1000005];
int main(){
int n;
while (scanf("%d", &n) != EOF){
int max = 0;
int dis = 0;
int a, r = 0, l = 0;
memset(cnt, 0, sizeof(cnt));
memset(sp, 0, sizeof(sp));
for (int i = 0; i < n; i++){
scanf("%d", &a);
if (!cnt[a])
sp[a] = i;
cnt[a]++;
if (max < cnt[a]){
max = cnt[a];
l = sp[a], r = i;
dis = i - l;
}
else if (max == cnt[a] && i - sp[a] < dis){
dis = i - sp[a];
l = sp[a];
r = i;
}
}
printf("%d %d\n", l + 1, r + 1);
}
return 0;
}