zoukankan      html  css  js  c++  java
  • Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力

    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;
    }
  • 相关阅读:
    C programming course
    关于时间管理的培训心得
    吴老师,一路好走!
    自己实现Int32Collection(.Net 1.1),以及效率问题的体会
    《C陷阱与缺陷》和《C专家编程》两本书又翻印了
    暂时闲一会,写一点点面试体会吧
    人类没有一件事是值得烦恼的
    EP0N系统中简便可行的光纤保护方法
    越来越不想写代码了
    [转]PON关键技术-通用成帧协议研究
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4650538.html
Copyright © 2011-2022 走看看