zoukankan      html  css  js  c++  java
  • 2018-2019 ACM-ICPC Brazil Subregional Programming Contest

    A:留坑
    B:二维sg函数,特判边界情况

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=100+10,maxn=200000+10,inf=0x3f3f3f3f;
    
    int sg[N][N],Hash[200];
    int main()
    {
        for(int i=1;i<=100;i++)
        {
            for(int j=1;j<=100;j++)
            {
                if(i==j)continue;
                memset(Hash,0,sizeof Hash);
                for(int k=1;k<i;k++)if(i-k!=j)Hash[sg[i-k][j]]=1;
                for(int k=1;k<j;k++)if(i!=j-k)Hash[sg[i][j-k]]=1;
                for(int k=1;k<min(i,j);k++)if(i-k!=j-k)Hash[sg[i-k][j-k]]=1;
                for(int k=0;;k++)
                {
                    if(!Hash[k])
                    {
                        sg[i][j]=k;
                        break;
                    }
                }
    //            printf("%d ",sg[i][j]);
            }
    //        puts("");
        }
        int n,ans=0;scanf("%d",&n);
        while(n--)
        {
            int x,y;scanf("%d%d",&x,&y);
            if(x==y)return 0*puts("Y");
            ans^=sg[x][y];
        }
        puts(ans?"Y":"N");
        return 0;
    }
    /********************
    
    ********************/
    

    C:bit维护逆序对即可

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    #define base 1000000000000000000
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=4e5+10,maxn=1000000+10,inf=0x3f3f3f3f;
    
    struct bit{
        int sum[N];
        void init(){memset(sum,0,sizeof sum);}
        void update(int i)
        {
            for(;i<N;i+=i&(-i))sum[i]++;
        }
        int query(int i)
        {
            int ans=0;
            for(;i;i-=i&(-i))ans+=sum[i];
            return ans;
        }
    }b;
    pii x[N],y[N];
    int Hash[N],cnt;
    int main()
    {
        int xx,yy,h,v;scanf("%d%d%d%d",&xx,&yy,&h,&v);
        for(int i=1;i<=h;i++)scanf("%d%d",&x[i].fi,&x[i].se),Hash[cnt++]=x[i].fi,Hash[cnt++]=x[i].se;
        sort(x+1,x+1+h);sort(Hash,Hash+cnt);cnt=unique(Hash,Hash+cnt)-Hash;
        for(int i=1;i<=h;i++)
        {
            x[i].fi=lower_bound(Hash,Hash+cnt,x[i].fi)-Hash+1;
            x[i].se=lower_bound(Hash,Hash+cnt,x[i].se)-Hash+1;
        }
        cnt=0;
        for(int i=1;i<=v;i++)scanf("%d%d",&y[i].fi,&y[i].se),Hash[cnt++]=y[i].fi,Hash[cnt++]=y[i].se;
        sort(y+1,y+1+v);sort(Hash,Hash+cnt);cnt=unique(Hash,Hash+cnt)-Hash;
        for(int i=1;i<=v;i++)
        {
            y[i].fi=lower_bound(Hash,Hash+cnt,y[i].fi)-Hash+1;
            y[i].se=lower_bound(Hash,Hash+cnt,y[i].se)-Hash+1;
        }
        ll c=0,d=0;
        for(int i=1;i<=h;i++)
        {
            c+=b.query(N-1)-b.query(x[i].se);
            b.update(x[i].se);
        }
        b.init();
        for(int i=1;i<=v;i++)
        {
            d+=b.query(N-1)-b.query(y[i].se);
            b.update(y[i].se);
        }
        printf("%lld
    ",1ll*(h+1)*(v+1)+c+d);
        return 0;
    }
    /********************
    
    ********************/
    

    D:签到

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    #define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=20200+10,maxn=200000+10,inf=0x3f3f3f3f;
    
    
    int main()
    {
        int _,x,ans=0;
        for(scanf("%d",&_);_;_--)
        {
            scanf("%d",&x);
            if(x!=1)ans++;
        }
        printf("%d
    ",ans);
        return 0;
    }
    /********************
    
    ********************/
    

    E:签到

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    #define C 0.5772156649
    //#define ls l,m,rt<<1
    //#define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=10000+10,maxn=200000+10,inf=0x3f3f3f3f;
    
    char s[N],p[N];
    int main()
    {
        scanf("%s%s",s,p);
        int ls=strlen(s),ps=strlen(p),ans=0;
        for(int i=0;i+ps-1<ls;i++)
        {
            bool f=0;
            for(int j=0;j<ps;j++)if(s[i+j]==p[j]){f=1;break;}
            if(!f)ans++;
        }
        printf("%d
    ",ans);
        return 0;
    }
    /********************
    
    ********************/
    

    F:注意到n很小,开(1<<n)个bit,把区间离散化后按左端点排序,bit维护区间最小值更新即可

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    #define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=2000+10,maxn=100000+10,inf=0x3f3f3f3f;
    
    struct bit{
        int sum[N];
        bit(){memset(sum,-1,sizeof sum);}
        void update(int i,int x)
        {
            for(;i<N;i+=i&(-i))sum[i]=max(sum[i],x);
        }
        int query(int i)
        {
            int ma=-1;
            for(;i;i-=i&(-i))ma=max(ma,sum[i]);
            return ma;
        }
    }b[N];
    struct node{
        int s,t,v,id;
        bool operator <(const node&rhs)const
        {
            return s<rhs.s || s==rhs.s&&t<rhs.t;
        }
    }p;
    vector<node>v;
    int Hash[N],cnt;
    int main()
    {
        b[0].update(1,0);
        int n;scanf("%d",&n);
        for(int i=0,k;i<n;i++)for(scanf("%d",&k);k;k--)
        {
            scanf("%d%d%d",&p.s,&p.t,&p.v),p.id=i,v.pb(p);
            Hash[cnt++]=p.s,Hash[cnt++]=p.t;
        }
        sort(Hash,Hash+cnt);cnt=unique(Hash,Hash+cnt)-Hash;
        sort(v.begin(),v.end());
        for(int i=0;i<v.size();i++)
        {
            p=v[i];
            p.s=lower_bound(Hash,Hash+cnt,p.s)-Hash+1;
            p.t=lower_bound(Hash,Hash+cnt,p.t)-Hash+1;
    //        printf("%d %d %d %d %d
    ",p.s,p.t,p.v,p.id,b[7].query(8));
            for(int j=0;j<(1<<n);j++)
            {
                int x=b[j].query(p.s);
                if(x!=-1)b[j|(1<<p.id)].update(p.t,x+p.v);
    //            if(x!=-1&&(j|(1<<p.id))==7)printf("%d %d %d!!!!
    ",p.s,p.t,x+p.v);
    //            if(j==5&&p.s==7&&p.id==1)printf("%d %d %d***
    ",x+p.v,b[5].query(p.s),b[7].query(p.t));
            }
        }
        printf("%d
    ",b[(1<<n)-1].query(N-1));
        return 0;
    }
    /********************
    
    ********************/
    

    G:二分,加最大流,二分图复杂度完全可以接受

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=2000+10,maxn=50000+10,inf=0x3f3f3f3f;
    
    int p,r,c,d[N],E[N],ans;
    struct node{
        int a,b,c;
        bool operator <(const node&rhs)const
        {
            return c<rhs.c;
        }
    }f[maxn];
    struct edge{
        int to,Next,c;
    }e[maxn];
    int s,t,cnt,head[N],cur[N],dis[N];
    void init(){cnt=0;memset(head,-1,sizeof head);}
    void add(int u,int v,int c)
    {
        e[cnt].to=v;e[cnt].c=c;e[cnt].Next=head[u];head[u]=cnt++;
        e[cnt].to=u;e[cnt].c=0;e[cnt].Next=head[v];head[v]=cnt++;
    }
    bool bfs()
    {
        queue<int>q;
        memset(dis,-1,sizeof dis);
        dis[s]=1;
        q.push(s);
        while(!q.empty())
        {
            int x=q.front();q.pop();
            for(int i=head[x];~i;i=e[i].Next)
            {
                int y=e[i].to;
                if(dis[y]==-1&&e[i].c>0)
                {
                    dis[y]=dis[x]+1;
                    q.push(y);
                }
            }
        }
        return dis[t]!=-1;
    }
    int dfs(int u,int mx)
    {
        if(u==t)return mx;
        int f;
        for(int &i=cur[u];~i;i=e[i].Next)
        {
            int x=e[i].to;
            if(dis[x]==dis[u]+1&&e[i].c>0&&(f=dfs(x,min(mx,e[i].c))))
            {
                e[i].c-=f;
                e[i^1].c+=f;
                return f;
            }
        }
        return 0;
    }
    int maxflow()
    {
        int ans=0,f;
        while(bfs())
        {
            for(int i=0;i<=t;i++)cur[i]=head[i];
            while((f=dfs(s,inf)))ans+=f;
        }
        return ans;
    }
    bool ok(int x)
    {
        init();
        for(int i=1;i<=p;i++)add(i,t,d[i]);
        for(int i=1;i<=r;i++)add(s,p+i,E[i]);
        for(int i=1;i<=c;i++)
        {
            if(f[i].c<=x)add(f[i].b+p,f[i].a,inf);
            else break;
        }
        return maxflow()==ans;
    }
    int main()
    {
        scanf("%d%d%d",&p,&r,&c);
        s=p+r+1,t=p+r+2;
        for(int i=1;i<=p;i++)scanf("%d",&d[i]),ans+=d[i];
        for(int i=1;i<=r;i++)scanf("%d",&E[i]);
        for(int i=1;i<=c;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            f[i]={a,b,c};
        }
        sort(f+1,f+1+c);
        int l=0,r=f[c].c+1;
        while(l<r-1)
        {
            int m=(l+r)>>1;
            if(ok(m))r=m;
            else l=m;
        }
        printf("%d
    ",(r<=f[c].c)?r:-1);
        return 0;
    }
    /********************
    3 2 5
    20 10 10
    25 30
    1 1 3
    2 1 1
    2 2 4
    3 1 2
    3 2 5
    ********************/
    

    H:留坑
    I:按题意模拟即可,bitset优化

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=1000+10,maxn=200000+10,inf=0x3f3f3f3f;
    
    bitset<1010>b[N],c;
    int main()
    {
        int n,m,k,x;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<k;i++)scanf("%d",&x),c.set(x);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            while(k--)scanf("%d",&x),b[i].set(x);
        }
        for(int i=1;i<=n;i++)
        {
            c^=b[i];
            if(c.count()==0)return 0*printf("%d
    ",i);
        }
        for(int i=1;i<=n;i++)
        {
            c^=b[i];
            if(c.count()==0)return 0*printf("%d
    ",i+n);
        }
        puts("-1");
        return 0;
    }
    /********************
    
    ********************/
    

    J:斯坦纳树,spfa更新时,只更新后n-k个点,保证不会出现capitals节点连capitals节点

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=100+10,maxn=200000+10,inf=0x3f3f3f3f;
    
    int x[N],y[N],n,k;
    db dp[N][(1<<10)+10];
    queue<int>q;
    bool vis[N];
    db cal(int a,int b){return sqrt(1.0*(x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));}
    void spfa(int st)
    {
        while(!q.empty())
        {
            int p=q.front();q.pop();
            vis[p]=0;
            for(int i=k;i<n;i++)
            {
                if(i==p)continue;
                db te=cal(p,i);
                if(dp[p][st]+te<dp[i][st])
                {
                    dp[i][st]=dp[p][st]+te;
                    q.push(i);
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d",&n,&k);
        for(int i=0;i<n;i++)for(int j=0;j<(1<<k);j++)dp[i][j]=2e9;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            if(i<k)dp[i][1<<i]=0;
        }
        for(int st=0;st<(1<<k);st++)
        {
            for(int i=0;i<n;i++)
            {
                for(int s=st;s;s=(s-1)&st)
                    if(dp[i][s]+dp[i][st-s]<dp[i][st])
                        dp[i][st]=dp[i][s]+dp[i][st-s];
                if(dp[i][st]<inf)q.push(i),vis[i]=1;
            }
            spfa(st);
        }
        db ans=2e9;
        for(int i=k;i<n;i++)ans=min(ans,dp[i][(1<<k)-1]);
        printf("%.5f
    ",ans);
        return 0;
    }
    /********************
    
    ********************/
    

    K:可以发现xy范围很小,两圆相交要满足R-r<=d,d最大是(sqrt(2)*50),那么我们按r sort之后,暴力枚举满足该条件的圆即可

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=150000+10,maxn=50000+10,inf=0x3f3f3f3f;
    
    struct circle{
        db x,y,r;
        void read(){scanf("%lf%lf%lf",&x,&y,&r);}
        bool operator <(const circle&rhs)const{
            return r<rhs.r;
        }
    }c[N];
    bool ok(int i,int j)
    {
        db d=sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y));
        return c[j].r-c[i].r<d&&d<c[i].r+c[j].r;
    }
    int main()
    {
        int n,ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)c[i].read();
        sort(c+1,c+1+n);
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(c[j].r-c[i].r>sqrt(2)*50)break;
                else if(ok(i,j))
                {
                    ans+=2;
                    if(ans>2*n)return 0*puts("greater");
                }
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    /********************
    
    ********************/
    

    L:树链剖分裸题

    //#pragma GCC optimize(2)
    //#pragma GCC optimize(3)
    //#pragma GCC optimize(4)
    //#pragma GCC optimize("unroll-loops")
    //#pragma comment(linker, "/stack:200000000")
    //#pragma GCC optimize("Ofast,no-stack-protector")
    //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define db double
    #define mp make_pair
    #define pb push_back
    #define pi acos(-1.0)
    #define ll long long
    #define vi vector<int>
    #define mod 1000000007
    #define ld long double
    //#define C 0.5772156649
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define pll pair<ll,ll>
    #define pil pair<int,ll>
    #define pli pair<ll,int>
    #define pii pair<int,int>
    //#define cd complex<double>
    #define ull unsigned long long
    //#define base 1000000000000000000
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    #define fio ios::sync_with_stdio(false);cin.tie(0)
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
    inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
    template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
    template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
    inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
    inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
    
    using namespace std;
    
    const double eps=1e-8;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int N=100000+10,maxn=200000+10,inf=0x3f3f3f3f;
    
    struct edge{
        int to,Next;
    }e[maxn];
    int cnt,head[N];
    int son[N],fa[N],top[N],sz[N],id[N];
    int res,dep[N];
    void add(int u,int v){e[cnt].to=v;e[cnt].Next=head[u];head[u]=cnt++;}
    void init()
    {
        cnt=0;
        memset(head,-1,sizeof head);
        memset(son,-1,sizeof son);
    }
    void dfs1(int u,int f,int de)
    {
        fa[u]=f;sz[u]=1;
        dep[u]=de;
        for(int i=head[u];~i;i=e[i].Next)
        {
            int v=e[i].to;
            if(v!=f)
            {
                dfs1(v,u,de+1);
                sz[u]+=sz[v];
                if(son[u]==-1||sz[v]>sz[son[u]])son[u]=v;
            }
        }
    }
    void dfs2(int u,int f,int tp)
    {
        top[u]=tp;id[u]=++res;
        if(son[u]!=-1)dfs2(son[u],u,tp);
        for(int i=head[u];~i;i=e[i].Next)
        {
            int v=e[i].to;
            if(v!=f&&v!=son[u])dfs2(v,u,v);
        }
    }
    int sum[N<<2],lazy[N<<2];
    void pushdown(int l,int r,int rt)
    {
        if(lazy[rt]!=0)
        {
            int m=(l+r)>>1;
            sum[rt<<1]+=(m-l+1)*lazy[rt];
            sum[rt<<1|1]+=(r-m)*lazy[rt];
            lazy[rt<<1]+=lazy[rt];
            lazy[rt<<1|1]+=lazy[rt];
            lazy[rt]=0;
        }
    }
    void pushup(int rt)
    {
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    void build(int l,int r,int rt)
    {
        lazy[rt]=sum[rt]=0;
        if(l==r)return ;
        int m=(l+r)>>1;
        build(ls);build(rs);
    }
    void update(int L,int R,int c,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            sum[rt]+=(r-l+1)*c;
            lazy[rt]+=c;
            return ;
        }
        pushdown(l,r,rt);
        int m=(l+r)>>1;
        if(L<=m)update(L,R,c,ls);
        if(m<R)update(L,R,c,rs);
        pushup(rt);
    }
    int query1(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)return sum[rt];
        pushdown(l,r,rt);
        int m=(l+r)>>1,ans=0;
        if(L<=m)ans+=query1(L,R,ls);
        if(m<R)ans+=query1(L,R,rs);
        return ans;
    }
    int query(int a,int b)
    {
        int f1=top[a],f2=top[b],ans=0;
        while(f1!=f2)
        {
            if(dep[f1]<dep[f2])swap(f1,f2),swap(a,b);
            ans+=query1(id[f1],id[a],1,res,1);
            a=fa[f1],f1=top[a];
        }
        if(dep[a]>dep[b])swap(a,b);
        ans+=query1(id[a],id[b],1,res,1);
        return ans;
    }
    void change(int a,int b,int c)
    {
        int f1=top[a],f2=top[b];
        while(f1!=f2)
        {
            if(dep[f1]<dep[f2])swap(f1,f2),swap(a,b);
            update(id[f1],id[a],c,1,res,1);
            a=fa[f1],f1=top[a];
        }
        if(dep[a]>dep[b])swap(a,b);
        update(id[a],id[b],c,1,res,1);
    }
    int main()
    {
        int n,q;
        scanf("%d%d",&n,&q);
        init();
        for(int i=1;i<n;i++)
        {
            int a,b;scanf("%d%d",&a,&b);
            add(a,b);add(b,a);
        }
        dfs1(1,-1,1);
        dfs2(1,-1,1);
        build(1,res,1);
        while(q--)
        {
            int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);
            change(a,b,1);
            printf("%d
    ",query(c,d));
            change(a,b,-1);
        }
        return 0;
    }
    /********************
    10 4
    1 4
    4 5
    3 4
    3 2
    7 3
    6 7
    7 8
    10 8
    8 9
    6 10 2 5
    1 9 5 10
    9 10 2 1
    5 10 2 9
    ********************/
    

    M:留坑

  • 相关阅读:
    2018全球最强物联网公司榜单揭晓
    物联网
    kalman滤波
    自动驾驶
    CAN总线基础
    Linux系统下x86和ARM的区别有哪些?
    算法课笔记系列(七)—— 平摊分析Amortized Analysis
    深入理解Linux内存分配
    linux内核--自旋锁的理解
    DMA(直接存储器存取)
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/10046611.html
Copyright © 2011-2022 走看看