zoukankan      html  css  js  c++  java
  • 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('
    ')
     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;}
  • 相关阅读:
    vue生命周期过程做了什么
    css_css3_实用属性_随时补充更新
    echarts的symbol引用本地图片写法
    无废话设计模式(1)--简单工厂、工厂方法、抽象工厂
    JavaWeb--Maven学习
    SpringCloud Alibaba实战 -引入服务网关Gateway
    从ReentrantLock看AQS (AbstractQueuedSynchronizer) 运行流程 抽象的队列式同步器
    架构的搭建(一)SpringCloud Alibaba
    配置中心之Nacos简介,使用及Go简单集成
    RabbitMQ
  • 原文地址:https://www.cnblogs.com/chxer/p/4676796.html
Copyright © 2011-2022 走看看