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;
    }
  • 相关阅读:
    Aseprite+Cocos:打包像素画图,导入到cocos里并动起来
    自定义博客园个人皮肤
    埃航和737MAX坠毁:软件优先级问题
    淘宝网——软件质量属性场景分析
    王概凯《架构漫谈》阅读笔记
    2965 -- The Pilots Brothers' refrigerator
    UVa10082 -- WERTYU
    1753 -- Flip Game
    1083 -- Moving Tables
    2159 -- Ancient Cipher
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4650538.html
Copyright © 2011-2022 走看看