【36.86%】【codeforces 558B】Amr and The Large Array

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
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

【题目链接】:http://codeforces.com/contest/558/problem/B

【题解】

找到出现次数最多的数字是哪些(可能有多个);
然后看看哪一个数字(出现次数最多的那些数字)在原序列中出现的最左的位置和最右的位置的差最小;
那个差的最小值就是答案了;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 1e5+10;
const int MAX = 1e6+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

struct abc
{
    int l,r;
};

int n;
map <int,int> dic;
int a[MAXN];
abc b[MAX];

int main()
{
   // freopen("F:\rush.txt","r",stdin);
    int ma = 0;
    rei(n);
    rep1(i,1,n)
    {
        rei(a[i]);
        dic[a[i]]++;
        if (dic[a[i]]>ma)
            ma = dic[a[i]];
    }
    rep1(i,1,n)
    {
        if (dic[a[i]]==ma)
        {
            if (b[a[i]].l==0)
            {
                b[a[i]].l=i;
                b[a[i]].r=i;
            }
            else
                b[a[i]].r = i;
        }
    }
    int qujian = 21e8;
    int al,ar;
    rep1(i,1,1e6)
    if (b[i].l!=0)
    {
        int len = b[i].r-b[i].l+1;
        if (len<qujian)
        {
            al = b[i].l,ar = b[i].r;
            qujian = len;
        }
    }
    pri(al);putchar(' ');pri(ar);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626845.html