zoukankan      html  css  js  c++  java
  • 二分(哈士奇的手链)

    A - 哈士奇的“手链”

    Description

         哈士奇得到了一条由不同颜色的珍珠串起来的手链,它决定用这条手链当作礼物向它的女神表白,女神收到礼物后决定考验一下哈士奇,如果哈士奇答对了,就接受哈士奇的表白。女神要求哈士奇说出这条手链中由连续的不同颜色珍珠所组成的最长珍珠段的长度。

         女神怕憨憨的哈士奇听不懂她的问题,贴心的举了个栗子:

         用数字代表颜色,例如有这样一个手串12222345,把这个数字串想象成一个首尾相连的珍珠串,那么这条手链中由不同颜色珍珠所组成的最长段为34512(因为首尾相连,所以5后面紧跟着是1),所以答案为5。

         哈士奇听懂了,现在它在思考如何解决这个问题?

    Input

         输入只有一组数据。

         第一行为一个正整数 N (1 <= N <= 500000),表示手链长度。

         第二行包含 N 个范围在 0 到 9 的数字,不同数字代表不同颜色的珍珠。

    Output

         输出为一个整数,表示哈士奇的答案。

    Sample

    Input 

    8
    1 2 2 3 2 3 4 5

    Output 

    5
    #pragma GCC optimize(2)
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<map> 
    #include <math.h>
    #include <math.h>
    using namespace std;
    typedef unsigned long long ull; 
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int maxn=1e6+10;
    const int inf=0x3f3f3f3f;
    const int mod=1e9+7;
    int n;
    int a[maxn];
    int b[10];
    int judge(int x){ 
        int j;
        for(int i=1;i<=2*n-x+1;i++){
            for(int k=0;k<=10;k++){
                b[k]=0;
            }
            for(j=i;j<i+x;j++){ 
                if(b[a[j]]>=1){
                    break;
                }
                else{
                    b[a[j]]++;
                }
            }
            if(j==i+x){
                return 1;
            }
        }
        return 0;
    }
    int main(){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        for(int i=n+1;i<=2*n;i++){
            a[i]=a[i-n];
        }
        int l=1,r=n;
        int ans;
        while(l<=r){
            int mid=(l+r)/2;
            if(judge(mid)){
                l=mid+1;
                ans=mid;
            }
            else{
                r=mid-1;
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    C#开发模式——单例模式
    C#开发模式——单例模式
    Memcached的基础梳理
    Memcached的基础梳理
    Memcached的基础梳理
    Memcached的基础梳理
    13条只有程序员才能懂的内涵段子
    13条只有程序员才能懂的内涵段子
    网页性能优化
    webpack中使用ECharts
  • 原文地址:https://www.cnblogs.com/lipu123/p/13177292.html
Copyright © 2011-2022 走看看