zoukankan      html  css  js  c++  java
  • 【HDU2019多校】Beauty Of Unimodal Sequence (LIS + 思维)

    You are given an array of nn integers a1,a2,...,ana1,a2,...,an. We define that a sequence p1,p2,...,pk(k[1,n])p1,p2,...,pk(k∈[1,n]) is beautiful if and only if these conditions are met: 

     ∙ 1p1<p2<<pkn.1≤p1<p2<⋯<pk≤n. 

     ∙ There exists t(t[1,k])t(t∈[1,k]) satisfying ap1<ap2<<aptap1<ap2<⋯<apt and apt>apt+1>>apkapt>apt+1>⋯>apk. 

    You need to find all the longest beautiful sequences, and output the lexicographically smallest one and the lexicographically largest one in them. 

    Check the examples below for better understanding.

    InputThere are multiple test cases. 

    Each case starts with a line containing a positive integer n(n3×105)n(n≤3×105). 

    The second line contains nn integers a1,a2,...,an(1ai109)a1,a2,...,an(1≤ai≤109). 

    It is guaranteed that the sum of NNs in all test cases is no larger than 106106.
    OutputFor each test case, output two lines, the first of which depicts the lexicographically smallest one in longest beautiful sequences and the second of which depicts the lexicographically largest one in longest beautiful sequences.Sample Input

    7
    1 4 2 5 7 4 6
    3
    1 2 3
    3
    3 2 1

    Sample Output

    1 2 4 5 6
    1 3 4 5 7
    1 2 3
    1 2 3
    1 2 3
    1 2 3

    SOLUTION:


    设两个数组 f1 i 表示从i位向后的最长下降子序列
    f2 i 表示从i位向后的最长单峰子序列
    通过这两个函数我们可以从前到后,为了保证字典序最小,那就能取就取。贪心的构造
    对于字典序最大,就把原数组倒过来在做一次就行了


    CODE:
    #include<bits/stdc++.h>
    using namespace std;
    const int N=3e5+5;
    int a[N],b[N],pre[N][2],suf[N][2];
    int lowbit(int x){return x&(-x);}
    int asc[N],desc[N];
    void addasc(int x,int v)
    {
        for(int i=x;i<N;i+=lowbit(i))
            asc[i]=max(asc[i],v);
    }
    int queryasc(int x)
    {
        int ans=0;
        for(int i=x;i;i-=lowbit(i))
            ans=max(ans,asc[i]);
        return ans;
    }
    void adddesc(int x,int v)
    {
        for(int i=x;i;i-=lowbit(i))
            desc[i]=max(desc[i],v);
    }
    int querydesc(int x)
    {
        int ans=0;
        for(int i=x;i<N;i+=lowbit(i))
            ans=max(ans,desc[i]);
        return ans;
    }
    struct node
    {
        int id,p;
        bool operator< (const node& aa)const
        {
            if(p!=aa.p)
                return p<aa.p;
            return id<aa.id;
        }
    };
    vector<node>vec;
    int num[N],ans[N][2];
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            memset(asc,0,sizeof(asc));
            memset(desc,0,sizeof(desc));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]),b[i]=a[i];
                for(int j=0;j<=1;j++)
                    pre[i][j]=suf[i][j]=ans[i][j]=0;
            }
            sort(b+1,b+1+n);
            int all=unique(b+1,b+1+n)-b-1;
            for(int i=1;i<=n;i++)
                a[i]=lower_bound(b+1,b+1+all,a[i])-b;
            for(int i=1;i<=n;i++)
            {
                int k=queryasc(a[i]-1);
                pre[i][0]=k+1;
                k=querydesc(a[i]+1);
                pre[i][1]=max(pre[i][0],k+1);
                addasc(a[i],pre[i][0]),adddesc(a[i],max(pre[i][1],pre[i][0]));
            }
            memset(asc,0,sizeof(asc));
            memset(desc,0,sizeof(desc));
            int mx=0;
            for(int i=n;i>=1;i--)
            {
                int k=queryasc(a[i]-1);
                suf[i][0]=k+1;
                k=querydesc(a[i]+1);
                suf[i][1]=max(suf[i][0],k+1);
                addasc(a[i],suf[i][0]),adddesc(a[i],max(suf[i][1],suf[i][0]));
    
                num[i]=max(pre[i][0]+suf[i][0]-1,max(pre[i][1]+suf[i][0]-1,pre[i][0]+suf[i][1]-1));
                mx=max(num[i],mx);
            }
            int f=0;
            int cnt=0;
            int now=-1;
            for(int i=1;i<=n;i++)
            {
                if(!f)
                {
                    if(a[i]>now && suf[i][1]+cnt==mx)
                   {
                        if(suf[i][0]+cnt == mx)f=1;
                        now=a[i];ans[++cnt][0]=i;
    
                   }
                }
                else
                {
                   if(a[i]<now && suf[i][0]+cnt==mx)
                   {
                       now=a[i];ans[++cnt][0]=i; f=1;
                   }
                }
    
    
    
            }
            cnt=f=0;
            for(int i=n;i>=1;i--)
            {
                if(num[i]==mx)
                {
                    if(f)
                    {
                        if(cnt+pre[i][0]==mx)
                            ans[mx-cnt][1]=i,cnt++;
                    }
                    else
                    {
                        if(cnt+1==suf[i][0])
                            ans[mx-cnt][1]=i,cnt++;
                        else if(pre[i][0]+cnt==mx)
                            ans[mx-cnt][1]=i,cnt++;
                    }
                }
            }
            for(int i=1;i<=mx;i++)
                printf("%d%c",ans[i][0],i==mx?'
    ':' ');
            for(int i=1;i<=mx;i++)
                printf("%d%c",ans[i][1],i==mx?'
    ':' ');
        }
        return 0;
    }
    /*//////
    10
    10 4 5 8 4 6 8 5 3 10
    
    */
    

      











  • 相关阅读:
    必会重构技巧(二):使用多态替换条件
    必会重构技巧(五):划分职责
    Flickr 网站架构分析
    必会重构技巧(三):提取接口
    WCF 一步一步 发布 WCF服务 到 IIS (图)
    LINQ 图解
    在Silverlight中如何创建WCF Service
    必会重构技巧(四):提取工厂类
    技术汇总:第十四章:电脑端生成支付宝二维码支付
    HDU 4463 Outlets 2012年亚洲区域赛杭州赛区现场赛K题
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11297612.html
Copyright © 2011-2022 走看看