HDUBeauty Of Unimodal Sequence

同NOIP2004合唱队形,原题DP部分可以直接贴。
题意:求一个最长严格先上升后下降子序列,有两问,输出字典序最小解和字典序最大解。
从后往前DP,使用线段树维护,过程中使用后继数组记录转移过程。然后顺着模拟一遍取出来就行了。思路很简单。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=300005;
long long A[MAXN];
struct tnode
{
    int dpval;
    int maxpos;
    int minpos;
    int l,r;
};
tnode operator + (const tnode &A,const tnode &B)
{
    tnode C;
    C.l=A.l;
    C.r=B.r;
    if(A.dpval>B.dpval)
    {
        C.dpval=A.dpval;
        C.maxpos=A.maxpos;
        C.minpos=A.minpos;
    }
    if(A.dpval<B.dpval)
    {
        C.dpval=B.dpval;
        C.maxpos=B.maxpos;
        C.minpos=B.minpos;
    }
    if(A.dpval==B.dpval)
    {
        C.dpval=A.dpval;
        C.maxpos=max(A.maxpos,B.maxpos);
        C.minpos=min(A.minpos,B.minpos);
    }
    return C;
}
struct Segment_Tree
{
    tnode t[4*MAXN];
    int id[MAXN];
    void update (int root)
    {
        int ch=root<<1;
        t[root]=t[ch]+t[ch+1];
    }
    void buildt(int root,int l,int r)
    {
        t[root].l=l;
        t[root].r=r;
        if(l!=r)
        {
            int mid=(l+r)>>1;
            int ch=root<<1;
            buildt(ch,l,mid);
            buildt(ch+1,mid+1,r);
            update(root);
        }
        else
        {
            t[root].dpval=t[root].maxpos=t[root].minpos=-1;
            id[l]=root;
        }
        return;
    }
    void change(int tvpos,int val,int pos)
    {
        int now=id[tvpos];
        if(t[now].dpval<val)
        {
            t[now].dpval=val;
            t[now].minpos=pos;
            t[now].maxpos=pos;
        }
        else if(t[now].dpval==val)
        {
            t[now].minpos=min(t[now].minpos,pos);
            t[now].maxpos=max(t[now].maxpos,pos);
        }
        else
        {
            return;
        }
        now>>=1;
        while(now)
        {
            update(now);
            now>>=1;
        }
        return;
    }
    tnode query(int root,int l,int r)
    {
        if(l>r)
        {
            tnode ret;
            ret.l=ret.r=ret.dpval=ret.maxpos=ret.minpos=-1;
            return ret;
        }
        if(t[root].l==l&&t[root].r==r)
        {
            return t[root];
        }
        int mid=(t[root].l+t[root].r)>>1;
        int ch=root<<1;
        if(r<=mid)return query(ch,l,r);
        else if(l>mid)return query(ch+1,l,r);
        else return query(ch,l,mid)+query(ch+1,mid+1,r);
    }
};
Segment_Tree ST0,ST1;
struct discretization_node
{
    int num;
    int ki;
}disc[MAXN];
bool cmp(discretization_node x,discretization_node y)
{
    return x.num<y.num;
}
int discretization(int a[],int n,discretization_node disc[])
{
    for(int i=1;i<=n;++i)
    {
        disc[i].num=a[i];
        disc[i].ki=i;
    }
    sort(disc+1,disc+1+n,cmp);
    int cnt=0,pre;
    for(int i=1;i<=n;++i)
    {
        if(i==1||a[disc[i].ki]!=pre)++cnt;
        pre=a[disc[i].ki];
        a[disc[i].ki]=cnt;
    }
    return cnt;
}
int n,a[MAXN],nextpos[MAXN][2][2],nextstate[MAXN][2][2],numsize,dp[MAXN][2],anslen,bg0,bg1,s0,s1;
vector<int>A1,A2;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        anslen=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
        }
        numsize=discretization(a,n,disc);
        ST0.buildt(1,1,numsize);
        ST1.buildt(1,1,numsize);
        for(int i=n;i;--i)
        {
            tnode temp1=ST1.query(1,1,a[i]-1);
            if(temp1.dpval!=-1)
            {
                dp[i][1]=temp1.dpval+1;

                nextstate[i][1][0]=1;
                nextpos[i][1][0]=temp1.minpos;

                nextstate[i][1][1]=1;
                nextpos[i][1][1]=temp1.maxpos;
            }
            else
            {
                dp[i][1]=1;

                nextstate[i][1][0]=-1;
                nextpos[i][1][0]=-1;

                nextstate[i][1][1]=-1;
                nextpos[i][1][1]=-1;

            }
            ST1.change(a[i],dp[i][1],i);
            tnode temp0=ST0.query(1,a[i]+1,numsize);
            if(temp1.dpval==-1&&temp0.dpval==-1)
            {
                dp[i][0]=1;

                nextstate[i][0][0]=-1;
                nextpos[i][0][0]=-1;

                nextstate[i][0][1]=-1;
                nextpos[i][0][1]=-1;

                ST0.change(a[i],dp[i][0],i);

                continue;
            }
            if(temp1.dpval>temp0.dpval)
            {
                dp[i][0]=temp1.dpval+1;

                nextstate[i][0][0]=1;
                nextpos[i][0][0]=temp1.minpos;

                nextstate[i][0][1]=1;
                nextpos[i][0][1]=temp1.maxpos;
            }
            else if(temp1.dpval<temp0.dpval)
            {
                dp[i][0]=temp0.dpval+1;

                nextstate[i][0][0]=0;
                nextpos[i][0][0]=temp0.minpos;

                nextstate[i][0][1]=0;
                nextpos[i][0][1]=temp0.maxpos;
            }
            else
            {
                dp[i][0]=temp0.dpval+1;
                if(temp1.minpos<=temp0.minpos)
                {
                    nextstate[i][0][0]=1;
                    nextpos[i][0][0]=temp1.minpos;
                }
                else
                {
                    nextstate[i][0][0]=0;
                    nextpos[i][0][0]=temp0.minpos;
                }
                if(temp0.maxpos>=temp1.maxpos)
                {
                    nextstate[i][0][1]=0;
                    nextpos[i][0][1]=temp0.maxpos;
                }
                else
                {
                    nextstate[i][0][1]=1;
                    nextpos[i][0][1]=temp1.maxpos;
                }
            }
            ST0.change(a[i],dp[i][0],i);
        }
        for(int i=1;i<=n;++i)
        {
            anslen=max(anslen,dp[i][0]);
        }
        bg0=bg1=-1;
        s0=s1=0;
        for(int i=1;i<=n;++i)
        {
            if(dp[i][0]==anslen)
            {
                if(bg0==-1||bg0>i)bg0=i;
                if(bg1==-1||bg1<i)bg1=i;
            }
        }
        A1.clear();
        A2.clear();
        while(s0!=-1)
        {
            A1.push_back(bg0);
            int ts=nextstate[bg0][s0][0];
            int tbg=nextpos[bg0][s0][0];
            bg0=tbg;
            s0=ts;
        }
        while(s1!=-1)
        {
            A2.push_back(bg1);
            int ts=nextstate[bg1][s1][1];
            int tbg=nextpos[bg1][s1][1];
            bg1=tbg;
            s1=ts;
        }
        for(int i=0;i<A1.size();++i)
        {
            printf("%d%c",A1[i],i==A1.size()-1?'\n':' ');
        }
        for(int i=0;i<A2.size();++i)
        {
            printf("%d%c",A2[i],i==A2.size()-1?'\n':' ');
        }
    }
    return 0;
} 

全部评论

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
LZHR:老哥你从投递简历测评完到一面中间隔了多久呀,我这边已经过了五天了仍显示简历筛选中是不是就是挂了
腾讯求职进展汇总
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务