Codeforces Round #312 (Div. 2) B.Amr and The Large Array
Amr has got a large array of sizen. 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 numbern(1 ≤ n ≤ 105), the size of the array.
The second line containsnintegersai(1 ≤ ai ≤ 106), representing elements of the array.
Output two integersl, 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 subsegmentBof an arrayAfromltoris an array of sizer - l + 1whereBi = Al + i - 1for all1 ≤ 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;}优质内容筛选与推荐>>