v · y "shaped pressure dp"

Always had a fear of like pressure dp

I will not play, will not tone, the key is not transferred

Do these two questions, although they are still not like pressure dp, but better than before a good number

Y

 

 Normal pressure should be like a good fight

Complexity $ O (2 ^ d * n * (n + m)) $

    for(ll i=2;i<=d;i++){
        for(ll state=0;state<=maxn;state++){
            for(ll x=1;x<=n;x++){
                for(ll j=head[x];j;j=nxt[j]){
                    ll y=ver[j];
                    if(!f[i-1][x][state]) continue ;
                    f[i][y][(state<<1)|edg[j]]|=f[i-1][x][state];
                }
            }
        }
    }

So how to optimize,

Binary search, you are determined starting point, enumerated intermediate point, so that the complexity reduces to $ O (2 ^ {\ frac {d} {2}} * n * (m + n) + 2 ^ d * n) $

The final enumeration intermediate point,

It is important to do so thinking

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define A 10101010
bool f1[21][93][1<<12],f2[21][93][1<<12],hash_map[1<<22];
ll nxt[A],ver[A],head[A],edg[A];
ll n,m,q,ans,tot,d;
void add(ll x,ll y,ll z){
    nxt[++tot]=head[x],head[x]=tot,ver[tot]=y,edg[tot]=z;
}
void turn(ll x,ll n)
{
     ll t=x,num=0,xx[100];
     while(x) xx[num++]=x%2,x/=2;    
     for(ll i=num;i<n;i++)printf("0");
     for(ll i=num-1;i>=0;i--)cout<<xx[i];
 //    puts("");
}
int main(){
    scanf("%lld%lld%lld",&n,&m,&d);
    for(ll i=1,a,b,c;i<=m;i++){
        scanf("%lld%lld%lld",&a,&b,&c);
        add(a,b,c);add(b,a,c);
    }
    for(ll i=head[1];i;i=nxt[i]){
        ll y=ver[i];
        f1[1][y][edg[i]]=1;
    }
    ll d1=d/2,d2=d-d1;
    ll maxn1=(1<<(d1))-1,maxn2=(1<<(d2))-1;
//    printf("d1=%lld d2=%lld\n",d1,d2);
    for(ll i=2;i<=d1;i++){
        for(ll state=0;state<=((1<<i)-1);state++){
            for(ll x=1;x<=n;x++){
                for(ll j=head[x];j;j=nxt[j]){
                    ll y=ver[j];
                    if(!f1[i-1][x][state]) continue ;
                    f1[i][y][(state<<1)|edg[j]]|=f1[i-1][x][state];
                }
            }
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=head[i];j;j=nxt[j]){
            ll y=ver[j];
            f2[1][y][edg[j]]=1;
        }
    }
//    printf("f2[1][1][1]=%lld\n",1ll*f2[1][1][1]);
    for(ll i=2;i<=d2;i++){
        for(ll state=0;state<=((1<<i)-1);state++){
            for(ll x=1;x<=n;x++){
                for(ll j=head[x];j;j=nxt[j]){
                    ll y=ver[j];
                    if(!f2[i-1][x][state]) continue ;
                    
//                    printf("f2[%lld][%lld][%lld]=%lld\n",i-1,x,state,1ll*f2[i-1][x][state]);
                    f2[i][y][(state<<1)|edg[j]]|=f2[i-1][x][state];
//                    printf("f2now[%lld][%lld][%lld]=1\n",i,y,(state<<1)|edg[j]);
                }
            }
        }
    }
    for(ll state=0;state<=maxn1;state++){
        for(ll state1=0;state1<=maxn2;state1++)
            for(ll i=1;i<=n;i++){
//                printf("f1[%lld][%lld][%lld]=%lld f2[%lld][%lld][%lld]=%lld state1=%lld\n",d1,i,state,1ll*f1[d1][i][state],d2,i,state1,1ll*f2[d2][i][state1],maxn2);
                if(f1[d1][i][state]&&f2[d2][i][state1]){
                    ll sum=state<<d2|state1;
//                    printf("***** i=%lld state=%lld state2=%lld\n",i,state,state1);
//                    turn(sum,1);
//                    printf("\n");
                    if(!hash_map[sum])
                        hash_map[sum]=1,ans++;
                }
        }
    }
    printf("%lld\n",ans);
}
View Code

v

 

 

answer

Memory search + hash table

About said the number right questions, binary simulated on the line

        st3=(st>>(l-p+1)<<(l-p))|((st&((1<<(l-p))-1)));

First right and then left disappeared this one, and then back intact

Code

#include<bits/stdc++.h>
using namespace std;
#define ll int
#define mod 19260817
struct hash_map{
    ll head[mod],nxt[mod],ver[mod];
    ll cnt;
    short len,L[mod];
    double val[mod];
    double &operator [] (const ll & st){
        int x=1ll*st*len%mod;
        for(ll i=head[x];i;i=nxt[i])
            if(ver[i]==st&&L[i]==len)
                return val[i];
        nxt[++cnt]=head[x],head[x]=cnt,ver[cnt]=st,L[cnt]=len,val[cnt]=-1;
        return val[cnt];
    }
}f;
ll n,k;
char c[33];
double dfs(ll l,ll st){
    if(l==n-k) return 0;
    f.len=l;
    if(f[st]>-0.5) return f[st];
    ll lst[33];
    ll rst=st;
    f[st]=0;
    for(ll i=1;i<=l;i++) lst[i]=rst&1,rst>>=1;
    for(ll i=1;i<=l/2;i++){
        ll j=l-i+1,st1=(st>>(l-i+1)<<(l-i))|(st&((1<<(l-i))-1)),st2=(st>>(l-j+1)<<(l-j))|(st&((1<<(l-j))-1));
        double ans1=dfs(l-1,st1)+lst[j],ans2=dfs(l-1,st2)+lst[i];
        f.len=l;f[st]+=2.0/((double)l)*max(ans1,ans2);
    }
    if(l&1){
        ll p=l/2+1,st3=(st>>(l-p+1)<<(l-p))|((st&((1<<(l-p))-1)));
        double ans3=dfs(l-1,st3)+lst[p];
        f.len=l;f[st]+=1.0/l*ans3;
    }
    return f[st];
}
int main(){
    scanf("%d%d",&n,&k);
    scanf("%s",c+1);
    ll st=0,cnt=0;
    for(ll i=1;i<=n;i++){
        st=((st<<1)|(c[i]=='W'));
        if(c[i]=='W') cnt++;
    }
    if(k==n){
        printf("%d\n",cnt);
        return 0;
    }
    printf("%.8lf\n",dfs(n,st));
}
View Code

 

Guess you like

Origin www.cnblogs.com/znsbc-13/p/11616258.html