zoukankan      html  css  js  c++  java
  • hdu 4521 小明系列问题——小明序列 线段树+二分

    小明系列问题——小明序列

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)


    Problem Description
      大家都知道小明最喜欢研究跟序列有关的问题了,可是也就因为这样,小明几乎已经玩遍各种序列问题了。可怜的小明苦苦地在各大网站上寻找着新的序列问题,可是找来找去都是自己早已研究过的序列。小明想既然找不到,那就自己来发明一个新的序列问题吧!小明想啊想,终于想出了一个新的序列问题,他欣喜若狂,因为是自己想出来的,于是将其新序列问题命名为“小明序列”。

      提起小明序列,他给出的定义是这样的:
      ①首先定义S为一个有序序列,S={ A1 , A2 , A3 , ... , An },n为元素个数 ;
      ②然后定义Sub为S中取出的一个子序列,Sub={ Ai1 , Ai2 , Ai3 , ... , Aim },m为元素个数 ;
      ③其中Sub满足 Ai1 < Ai2 < Ai3 < ... < Aij-1 < Aij < Aij+1 < ... < Aim ;
      ④同时Sub满足对于任意相连的两个Aij-1与Aij都有 ij - ij-1 > d (1 < j <= m, d为给定的整数);
      ⑤显然满足这样的Sub子序列会有许许多多,而在取出的这些子序列Sub中,元素个数最多的称为“小明序列”(即m最大的一个Sub子序列)。
      例如:序列S={2,1,3,4} ,其中d=1;
      可得“小明序列”的m=2。即Sub={2,3}或者{2,4}或者{1,4}都是“小明序列”。

      当小明发明了“小明序列”那一刻,情绪非常激动,以至于头脑凌乱,于是他想请你来帮他算算在给定的S序列以及整数d的情况下,“小明序列”中的元素需要多少个呢?
     
    Input
      输入数据多组,处理到文件结束;
      输入的第一行为两个正整数 n 和 d;(1<=n<=10^5 , 0<=d<=10^5)
      输入的第二行为n个整数A1 , A2 , A3 , ... , An,表示S序列的n个元素。(0<=Ai<=10^5)
     
    Output
      请对每组数据输出“小明序列”中的元素需要多少个,每组测试数据输出一行。
     
    Sample Input
    2 0 1 2 5 1 3 4 5 1 2 5 2 3 4 5 1 2
     
    Sample Output
    2 2 1
     
    Source
    思路:找前边比其小的数,最大的长度;树状数组或者线段树标记;
       如果标记a[],树状数组即可,标记答案数组加个二分查找;
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll __int64
    #define mod 1000000007
    #define inf 999999999
    //#pragma comment(linker, "/STACK:102400000,102400000")
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF ) return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    struct is
    {
        int l,r;
        int num;
    }tree[100010*3];
    int a[100010];
    int gg[100010];
    void buildtree(int l,int r,int pos)
    {
        tree[pos].l=l;
        tree[pos].r=r;
        if(l==r)
        {
            tree[pos].num=inf;
            return;
        }
        int mid=(l+r)>>1;
        buildtree(l,mid,pos*2);
        buildtree(mid+1,r,pos*2+1);
        tree[pos].num=min(tree[pos*2].num,tree[pos*2+1].num);
    }
    void update(int point,int pos,int change)
    {
        if(tree[pos].l==tree[pos].r&&tree[pos].l==point)
        {
            tree[pos].num=change;
            return;
        }
        int mid=(tree[pos].l+tree[pos].r)>>1;
        if(point<=mid)
        update(point,pos*2,change);
        else
        update(point,pos*2+1,change);
        tree[pos].num=min(tree[pos*2].num,tree[pos*2+1].num);
    }
    int query(int l,int r,int pos)
    {
        if(tree[pos].l==l&&tree[pos].r==r)
        return tree[pos].num;
        int mid=(tree[pos].l+tree[pos].r)>>1;
        if(r<=mid)
        return query(l,r,pos*2);
        else if(l>mid)
        return query(l,r,pos*2+1);
        else
        return min(query(l,mid,pos*2),query(mid+1,r,pos*2+1));
    }
    int main()
    {
        int x,y,z,i,t;
        while(~scanf("%d%d",&x,&y))
        {
            for(i=1;i<=x;i++)
            gg[i]=1;
            buildtree(1,x,1);
            for(i=1;i<=x;i++)
            scanf("%d",&a[i]);
            for(i=y+2;i<=x;i++)
            {
                if(query(gg[i-y-1],gg[i-y-1],1)>a[i-y-1])
                update(gg[i-y-1],1,a[i-y-1]);
                int st=1;
                int en=x,mid;
                if(tree[1].num>=a[i])
                gg[i]=1;
                else
                {
                    while(st<en)
                    {
                    mid=(st+en)>>1;
                    if(query(mid+1,en,1)<a[i])
                    st=mid+1;
                    else
                    en=mid;
                    }
                    gg[i]=st+1;
                }
            }
            int ans=0;
            for(i=1;i<=x;i++)
            ans=max(ans,gg[i]);
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
     
  • 相关阅读:
    [转]GPS原始数据说明
    [转]标准USB,MiniUSB接口定义
    warning C4819: 该文件包含不能在当前代码页(936)中表示的字符
    with用法
    turn out用法
    keep用法
    Stop doing和Stop to do和Stop...from doing有什么不同
    figure用法
    wanna用法
    seem用法
  • 原文地址:https://www.cnblogs.com/jhz033/p/5463861.html
Copyright © 2011-2022 走看看