"Problem solution": a sequence

. A: sequence d

Time limit: 1 Sec   Memory Limit: 512 MB

Face questions


He declined to publicly face the question.

answer


Simple configuration problem. Think positive solutions (no brainer good cutting) However deceived points blinded when the game ......

In fact after the game to change the topic, I found that my thinking is too simple, even if can not afford to think positive solution also cut during the Games.

inf decision: The hands may find this play four.

 

if(a+b>n+1){puts("No");continue;}
if(a*b<n){puts("No");continue;}
if(a>n||b>n){puts("No");continue;}
if(a==0||b==0){puts("No");continue;}

 

For the data of $ 20% $, $ n \ leq 5 $, $ next-permutation $ directly to violence.

$ $ For the data of 30%, to ensure $ N = A * B $, is the real part of this solution to give positive tips brought portion. Play with the hand can be found, we can $ N $ elements $ B $ bisected into groups, within each group monotonically increasing, monotonically decreasing between the groups, must be constructed so that a legitimate program.

For data guaranteed $ 20% $ $ B \ leq 2 $, so that the case can be divided directly play hand. Where $ B = 1 $ is very simple, if there is a legitimate solution at this time, must satisfy $ A = n $. $ B = 2 $ circumstances of the way I write is very sb not elaborate. Details look at the code.

Data for $ 100% $ of positive solutions from the above $ 30% $ data from the promotion. We still $ B $ can be divided into groups, the groups monotonously increasing monotonically decreased groups, then we need to ensure that only the largest group of elements in a $ A $, and the number of elements remaining group not exceeded $ A $ It can be.

This is still a good implementation. But I thought too rigid, has been using a number of methods weird, try using the four operations $ O (1) $ obtained a legal decomposition scheme. However, not so easy to find. Finally opened the array to maintain the size of each group.

Code:

 

#include<bits/stdc++.h>
#define lowbit(A) A&-A
#define rint register int
using namespace std;
int T,n,a,b,pl[100005],t[15],dp[15],up,dn,tim,len[100005];
bool vis[100005],is_ok;
inline void update(int x,int dat)
{
    for(;x<=n;x+=lowbit(x))
        t[x]=max(t[x],dat);
    return ;
}
inline int query(int x)
{
    int res=0;
    for(;x;x-=lowbit(x))
        res=max(res,t[x]);
    return res;
}
int main ()
{
    scanf("%d",&T);
    while(T--)
    {
        is_ok=0;
        scanf("%d %d %d",&n,&a,&b);
        if(a+b>n+1){puts("No");continue;}
        if(a*b<n){puts("No");continue;}
        if(a>n||b>n){puts("No");continue;}
        if(a==0||b==0){puts("No");continue;}
        if(n<=5)
        {
            for(rint i=1;i<=n;++i)pl[i]=i;
            do{
                for(rint i=1;i<=n;++i)t[i]=0,dp[i]=1;up=1;
                for(rint i=1;i<=n;++i)
                {
                    dp[i]=query(pl[i]-1)+1;
                    up=max(up,dp[i]);
                    update(pl[i],dp[i]);
                }
                if(up!=a)continue;
                for(rint i=1;i<=n;++i)t[i]=0,dp[i]=1;dn=1;
                for(rint i=n;i>=1;--i)
                {
                    dp[i]=query(pl[i]-1)+1;
                    dn=max(dn,dp[i]);
                    update(pl[i],dp[i]);
                }
                if(dn==b){is_ok=1;break;}
            }while(next_permutation(pl+1,pl+n+1));
            if(!is_ok)puts("No");
            else
            {
                puts("Yes");
                for(rint i=1;i<=n;++i)
                    printf("%d ",pl[i]);
                puts("");
            }
            continue;
        }
        else if(a*b==n)
        {
            puts("Yes");
            for(rint i=1;i<=a;++i)
            {
                for(rint j=b*i;j>b*(i-1);--j)
                    printf("%d ",j);
            }
            puts("");
            continue;
        }
        else if(b==1)
        {
            puts("Yes");
            for(rint i=1;i<=n;++i)printf("%d ",i);puts("");
            continue;
        }
        else if(b==2)
        {
            int lin=n/2;
            int bro=a-lin-(n&1);puts("Yes");
            for(rint i=1;i<=2*bro;++i)
                printf("%d ",i);
            for(rint i=bro+1;i<=lin;++i)
                printf("%d %d ",2*i,2*i-1);
            if(n&1)printf("%d",n);
            puts("");
            continue;
        }
        puts("Yes");
        int zh=n;
        memset(len,0,sizeof(len));
        for(rint j=zh-a+1;j<=zh;++j)printf("%d ",j);zh=n-a;
        for(rint i=1;i<=n-a;++i)
            len[i%(b-1)+1]++;
        for(rint i=1;i<=b-1;++i)
        {
            for(rint j=zh-len[i]+1;j<=zh;++j)printf("%d ",j);
            zh = zh- len [i];
        }
        puts("");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/xingmi-weiyouni/p/11696205.html