Codeforces Round #312 (Div. 2) B.Amr and The Large Array

Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.

Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.

Help Amr by choosing the smallest subsegment possible.

Input

The first line contains one number n (1 ≤ n ≤ 105), the size of the array.

The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

Output

Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively.

If there are several possible answers you may output any of them.

Sample test(s)
input
5
1 1 2 2 1
output
1 5
input
5
1 2 2 3 1
output
2 3
input
6
1 2 2 1 1 2
output
1 5
Note

A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1

 

题解:一眼DP,记录一下尾巴就好,要记住窝萌的算法是Online的,还有众数的概念,要不然容易写晕。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(' ')
 8 #define ENT putchar('\n')
 9 using namespace std;
10 const int maxv=1000000+10,inf=-1u>>1;
11 inline int read(){
12     int x=0,sig=1;char ch=getchar();
13     while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
14     while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
15     return x*=sig;
16 }
17 inline void write(int x){
18     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
19     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
20     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
21 }
22 int l[maxv],m[maxv];
23 void init(){
24     int n=read(),ans1,ans2;  
25     int mx=0,mi=inf;
26     for(int i=1;i<=n;i++){
27         int x=read();
28         if(!l[x])l[x]=i;m[x]++;
29         if(m[x]>mx){
30             mx=m[x];ans1=l[x];ans2=i;mi=ans2-ans1;  
31         }else if(m[x]==mx&&i-l[x]<mi){
32             ans1=l[x];ans2=i;mi=ans2-ans1;
33         }
34     }
35     write(ans1);PAU;write(ans2);
36     return;
37 }
38 void work(){
39     return;
40 }
41 void print(){
42     return;
43 }
44 int main(){init();work();print();return 0;}

 

你可能感兴趣的:(codeforces)