forked from gitter-badger/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF558B.cpp
More file actions
56 lines (53 loc) · 1.32 KB
/
CF558B.cpp
File metadata and controls
56 lines (53 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100100;
int a[maxn];
map<int, int> mp;
map<int, int> first, last;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (mp.find(a[i]) == mp.end()) {
mp[a[i]] = 1;
} else {
mp[a[i]] += 1;
}
}
for (int i = 0; i < n; i++) {
if (first.find(a[i]) == first.end())
first[a[i]] = i;
}
for (int i = n - 1; i >= 0; i--) {
if (last.find(a[i]) == last.end())
last[a[i]] = i;
}
map<int, int>::iterator it;
int maxNum = 0;
vector<int> num;
for (it = mp.begin(); it != mp.end(); it++) {
if (maxNum < it->second) {
maxNum = it->second;
num.clear();
num.push_back(it->first);
} else if (maxNum == it->second) {
num.push_back(it->first);
}
}
int ans = maxn;
int ansl = 0, ansr = 0;
for (int i = 0; i < num.size(); i++) {
if (last[num[i]] - first[num[i]] < ans) {
ans = last[num[i]] - first[num[i]];
ansl = first[num[i]];
ansr = last[num[i]];
}
}
printf("%d %d\n", ansl + 1, ansr + 1);
}