CF1012F Passports

CF1012F Passports 

这个题也写了半天,哎。

说起来很简单。

考虑P=1的情况

无非就是在旅行之前办完所有签证。

f[S]表示办完S集合的签证,最早拿回护照的时间

枚举下一个签证进行转移。

P=2

一部分分配给一个护照办签证+旅行

f[S]表示,某个护照办完S集合的签证,并且将要用这个护照去旅行,最早拿回护照的时间。(不算旅行本身。只要保证合法)

枚举下一个签证c进行转移

首先保证S+(1<<c)本身旅行是合法的。

要保证,这个S集合中的trip不能与c办证时间相交。

并且,不能存在某个trip,左端点小于等于c的,但是右端点大于等于c,也就是不能正在旅行

按照ti从小到大枚举c

三指针维护

输出方案随便搞即可。

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('
');}
namespace Modulo{
const int mod=998244353;
il int ad(int x,int y){return x+y>=mod?x+y-mod:x+y;}
il int sub(int x,int y){return ad(x,mod-y);}
il int mul(int x,int y){return (ll)x*y%mod;}
il void inc(int &x,int y){x=ad(x,y);}
il void inc2(int &x,int y){x=mul(x,y);}
il int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
template<class ...Args>il int ad(const int a,const int b,const Args &...args) {return ad(ad(a,b),args...);}
template<class ...Args>il int mul(const int a,const int b,const Args &...args) {return mul(mul(a,b),args...);}
}
//using namespace Modulo;
namespace Miracle{
const int N=22;
const int inf=0x3f3f3f3f;
int n,m;
int f[1<<N];
int len[N],st[N],nd[N],tim[N];
pii pre[1<<N];
int id[N];
int g[1<<N];
bool cmp(int x,int y){
    return tim[x]<tim[y];
}
struct po{
    int p,id;
    bool friend operator <(po a,po b){
        return a.p<b.p;
    }
}p[N];
pii gt[N];
int main(){
    rd(n);rd(m);
    for(reg i=0;i<n;++i){
        rd(st[i]);rd(len[i]);rd(tim[i]);
        nd[i]=st[i]+len[i]-1;
        id[i]=i;
        p[i].p=st[i];
        p[i].id=i;
    }
    sort(id,id+n,cmp);
    sort(p,p+n);
    memset(f,0x3f,sizeof f);
    f[0]=1;
    g[0]=1;
    for(reg s=1;s<(1<<n);++s){
        int now=log2(s&(-s));
        g[s]=g[s^(s&(-s))];
        for(reg i=0;i<n;++i){
            if(i==now) continue;
            if((s>>i)&1){
                if(!((nd[now]<st[i])||(st[now]>nd[i]))) g[s]=0;
            }
        }    
    }
    for(reg s=0;s<(1<<n)-1;++s){
        if(f[s]!=inf){
            int lp=0,ptr=0,mx=0;
            while(lp<n&&((!((s>>p[lp].id)&1))||p[lp].p<f[s])) ++lp;
            while(ptr<n&&(p[ptr].p<=f[s])){
                mx=max(mx,nd[p[ptr].id]);
                ++ptr;
            }
            int t=f[s];
            for(reg o=0;o<n;++o){
                int c=id[o];
                if(!((s>>c)&1)){
                    if(g[s|(1<<c)]){
                        
                        while((lp<n&&p[lp].p<=t+tim[c])||(mx>=t)){
                            if((mx>=t)) t=max(t,mx+1);
                            if((lp<n&&p[lp].p<=t+tim[c])) t=max(t,nd[p[lp].id]+1);
                            while(lp<n&&((!((s>>p[lp].id)&1))||p[lp].p<t)) ++lp;
                            while(ptr<n&&(p[ptr].p<=t)){
                                mx=max(mx,nd[p[ptr].id]);
                                ++ptr;
                            }
                        }
                        if(st[c]>t+tim[c]){
                            if(f[s|(1<<c)]>t+tim[c]){
                                f[s|(1<<c)]=t+tim[c];
                                pre[s|(1<<c)]=mk(t,c);
                            }
                        }
                    }
                }
            }
        }
    }
    int S;
    int ans=inf;
    int up=(1<<n)-1;
    if(m==1) ans=f[up],S=up;
    else
    for(reg s=0;s<(1<<n);++s){
        if(ans>max(f[s],f[up-s])){
            ans=max(f[s],f[up-s]);
            S=s;
        }
    }
    if(ans<inf){
        puts("YES");
        int T=up-S;
        while(S){
            gt[pre[S].se]=mk(1,pre[S].fi);
            S-=(1<<pre[S].se);
        }
        while(T){
            gt[pre[T].se]=mk(2,pre[T].fi);
            T-=(1<<pre[T].se);
        }
        for(reg i=0;i<n;++i){
            printf("%d %d
",gt[i].fi,gt[i].se);
        }
    }else{
        puts("NO");
    }
    return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
*/

状压,思路就是考虑办证的先后顺序,使得最早拿到护照本身。

P=2就分着,然后枚举S1合并。

原文地址:https://www.cnblogs.com/Miracevin/p/11111267.html