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.
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 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.
5 1 1 2 2 1
1 5
5 1 2 2 3 1
2 3
6 1 2 2 1 1 2
1 5
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
题目的关键:每个位置只能使某个值出现的次数+1;
我的解题思路:对于所有可能出现的值记录其出现的位置,然后在所有可能值中查找。
如果某个值没有出现或是出现的次数少于 题目所需的最多出现次数,那么跳过,求这个值的最后一个和最前一个的距离。进行最短距离的更新。
具体做法: 对于每种值,如果出现次数达到要求,直接求最后一个和最前一个的距离。
看了别人的思路:比我要简单点,1~n出现次数要求最多的那个值,反过来说不管是何值,都要让他出现次数尽可能多,顺次遍历,每到一个点,某个值次数就+1,计算一下,这个值是否是当前出现次数最多的值,如果是或是相等,进行对应判断和更新。
具体做法:对于每个位置记录下当前位置的值的出现次数(这个次数是从左往右的次数)cnt[maxn];
对于每个值,记录下第一次出现的位置。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mp make_pair
#define _f first
#define _s second
using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= 100000+5 ;
const int m= 1000000+5 ;
//const int INF= ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123
int a[maxn];
vector<int >ve[m];
int n;
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=m;i++)
ve[i].clear();
int maxi=1;
FOR1(i,n)
{
scanf("%d",&a[i]);
ve[a[i]].push_back(i);
if(ve[a[i]].size()>maxi)
maxi=ve[a[i]].size();
}
int ans=INF,st,ed;
FOR1(i,m)
{
if(ve[i].size()<maxi) continue;
for(int j=0;j<= ve[i].size()-maxi;j++)
{
if( ve[i][ve[i].size()-1 ]-ve[i][0]+1<ans ) {ans=ve[i][ve[i].size()-1 ]-ve[i][0]+1;st=ve[i][0];ed=ve[i][ve[i].size()-1];}
}
}
printf("%d %d\n",st,ed);
}
return 0;
}