B. Amr and The Large Array
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/558/problem/B
Description
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 Input
5
1 1 2 2 1
Sample Output
1 5
HINT
题意
要求找到一个区间,使得包含同样的数最多的最短的区间……
题意真是蛋疼,不过还好我第一次就读对了
题解:
我们直接对于每个数,统计这个数出现的次数,然后再记录仪下开始位置和结束位置,然后跑一法1e6的复杂度就吼了!
代码
#include<stdio.h> #include<iostream> #include<math.h> #include<algorithm> using namespace std; struct node { int x,y,z; }; node a[1000011]; int main() { for(int i=1;i<=1000001;i++) a[i].x=1000011,a[i].y=-1,a[i].z=0; int n; scanf("%d",&n); int ans=0; for(int i=1;i<=n;i++) { int x; scanf("%d",&x); a[x].x=min(i,a[x].x); a[x].y=max(i,a[x].y); a[x].z++; ans=max(ans,a[x].z); } int ans1=0,ans2=99999999; for(int i=1;i<=1000001;i++) { if(a[i].z==ans) { if(ans2-ans1>a[i].y-a[i].x) ans2=a[i].y,ans1=a[i].x; } } cout<<ans1<<" "<<ans2<<endl; }