【JZOJ3743】【BZOJ5158】Alice and Bob

description


analysis

  • Can restore the original greedy \ (X \) sequence and \ (X \) is the \ (n-\) arrangement; easy to know \ (A \) is not monotonic decreasing from several consecutive segments are put together from section

  • And each section within the range of at most a difference \ (1 \) , probably like \ (1,1,2,2,2,3, ... x, 1,1,1,2 , ..., y ,1,...\)

  • Interval for each segment, the number of the previous block is less than the number in the succeeding block; and each of a decreasing number of optimal fill

  • The smaller the number of the last paragraph is also more excellent filling, consider limiting relationship, from \ (a [i] -1 \ ) last position appears \ (J \) to \ (I \) connected edges, represents \ (a [j ] <a [i] \) forming a tree

  • Since even before the star to the edge of the later of the earlier traversal, we can ensure that the position of the \ (a \) is smaller, \ (the DFS \) after the time stamp is the \ (a \)

  • Fenwick tree and then a final request \ (LIS \) to


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 100005
#define MAXM MAXN*2
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)
#define rep(i,a) for (reg i=las[a];i;i=nex[i])

using namespace std;

ll las[MAXM],nex[MAXM],tov[MAXM];
ll a[MAXN],dfn[MAXN],last[MAXN];
ll tr[MAXN];
ll n,tot,ans;

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
    while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline ll lowbit(ll x){return x&(-x);}
inline ll max(ll x,ll y){return x>y?x:y;}
inline ll min(ll x,ll y){return x<y?x:y;}
inline void dfs(ll x){dfn[x]=++tot;rep(i,x)dfs(tov[i]);}
inline void link(ll x,ll y){nex[++tot]=las[x],las[x]=tot,tov[tot]=y;}
inline void modify(ll x,ll y){while (x<=n)tr[x]=max(tr[x],y),x+=lowbit(x);}
inline ll query(ll x){ll y=0;while (x)y=max(y,tr[x]),x-=lowbit(x);return y;}
int main()
{
    //freopen("alice.in","r",stdin);
    //freopen("alice.out","w",stdout);
    n=read();
    fo(i,1,n)a[i]=read(),link(last[a[i]-1],i),last[a[i]]=i;
    tot=-1,dfs(0);
    fo(i,1,n)printf("%lld ",dfn[i]);
    printf("\n");
    fd(i,n,1)
    {
        ll tmp=query(dfn[i]-1)+1;
        ans+=tmp,modify(dfn[i],tmp);
    }
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/horizonwd/p/12147068.html