• 《算法竞赛》$0×00$基本算法


    0×02 枚举,模拟,递推

    $Poj1958 Stange Towers of Hanoi$

    递推

    #include<iostream>
    #include<cstdio>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    #define inf 2100000000
    using namespace std;
    int n,d[20],f[20];
    int main()
    {
        d[1]=f[1]=1;printf("1
    ");
        go(i,2,12)f[i]=inf;
        go(n,2,12)
        {
            d[n]=2*d[n-1]+1;
            go(i,1,n-1)f[n]=min(f[n],2*f[i]+d[n-i]);
            printf("%d
    ",f[n]);
        }
        return 0;
    }
    View Code

    $BZOJ2018 激光炸弹$

    前缀和

     1 #include<iostream>
     2 #include<cstdio>
     3 #define il inline
     4 #define Rg register
     5 #define go(i,a,b) for(Rg int i=a;i<=b;i++)
     6 #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
     7 using namespace std;
     8 il int read()
     9 {
    10     int x=0,y=1;char c=getchar();
    11     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    12     while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    13     return x*y;
    14 }
    15 int n,r,l1,l2,ans,f[5002][5002];
    16 int main()
    17 {
    18     n=read(),r=read();
    19     go(i,1,n)
    20     {
    21         int x=read()+1,y=read()+1;
    22         l1=max(l1,x),l2=max(l2,y);
    23         f[x][y]=read();
    24     }
    25     go(i,1,l1)
    26         go(j,1,l2)
    27     {
    28         f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+f[i][j];
    29         int h=max(i-r,0),k=max(j-r,0);
    30         ans=max(ans,f[i][j]-f[h][j]-f[i][k]+f[h][k]);
    31     }
    32     printf("%d
    ",ans);
    33     return 0;
    34 }
    View Code

    $Poj3263 Tallest Cow$

    只讲一下优化:把对一个区间的操作转化为左,右两个端点上的操作,再通过前缀和得到原问题的解。

    #include<iostream>
    #include<cstdio>
    #include<map>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    map<pair<int,int>,bool> f;
    int N,I,H,R,A,B,a[10001],b[10001];
    int main()
    {
        N=read(),I=read(),H=read(),R=read();
        go(i,1,R)
        {
            A=read(),B=read();if(A>B)swap(A,B);
            if(f[make_pair(A,B)])continue;
            a[A+1]-=1;a[B]+=1;f[make_pair(A,B)]=1;
        }
        go(i,1,N){b[i]=b[i-1]+a[i];printf("%d
    ",H+b[i]);}
        return 0;
    }
    View Code

    0×03 递归

    $Poj1845 Sumdiv$

    分治!

     1 #include<iostream>
     2 #include<cstdio>
     3 #define il inline
     4 #define Rg register
     5 #define go(i,a,b) for(Rg int i=a;i<=b;i++)
     6 #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
     7 #define ll long long
     8 using namespace std;
     9 il int read()
    10 {
    11     int x=0,y=1;char c=getchar();
    12     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    13     while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    14     return x*y;
    15 }
    16 const int N=20,mod=9901;
    17 int n,m,ct,a[N],b[N];
    18 ll ans=1;
    19 il void brk(int x)
    20 {
    21     for(Rg int i=2;i*i<=x;i++)
    22     {
    23         if(x%i==0)a[++ct]=i;
    24         while(x%i==0)x/=i,b[ct]++;
    25     }
    26     if(x>1)a[++ct]=x,b[ct]++;
    27 }
    28 il ll ksm(int x,int y)
    29 {
    30     ll ret=1;x%=mod;//remember to mod !!!!!!!
    31     while(y){if(y&1)ret=ret*x%mod;x=x*x%mod;y>>=1;}
    32     return ret;
    33 }
    34 il ll sum(int p,int c)
    35 {
    36     if(!c)return 1;
    37     if(c&1)return (ksm(p,(c+1)>>1)+1)*sum(p,(c-1)/2)%mod;
    38     else return ((ksm(p,c>>1)+1)*sum(p,(c/2-1))%mod+ksm(p,c))%mod;
    39 }
    40 int main()
    41 {
    42     n=read(),m=read();
    43     brk(n);
    44     go(i,1,ct){ans=(ans*sum(a[i],b[i]*m))%mod;}
    45     printf("%lld
    ",ans);
    46     return 0;
    47 }
    View Code

    $Poj3889 Fractal Streets$

    递归 $remember to be careful!$

     #include<iostream>
    #include<cstdio>
    #include<cmath>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    #define ll long long
    #define db double
    using namespace std;
    int T,n;
    ll a,b;
    il ll s(ll x){return x*x;}
    il pair<ll,ll> calc(int n,ll x)
    {
        if(n==0)return make_pair(1,1);
        ll c=1LL<<(2*n-2),d=(x-1)/c,e=x%c,len=1LL<<(n-1);
        if(!e)e=c;
        pair<ll,ll> tmp=calc(n-1,e);
        ll f=tmp.first,g=tmp.second;
        if(d==0)return make_pair(g,f);
        if(d==1)return make_pair(f,g+len);
        if(d==2)return make_pair(f+len,g+len);
        return make_pair(len*2-g+1,len-f+1);
    }
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%lld%lld",&n,&a,&b);
            pair<ll,ll> aa,bb;
            aa=calc(n,a);bb=calc(n,b);
            db ans=sqrt((db)(s(aa.first-bb.first)+s(aa.second-bb.second)))*10;
            printf("%.0lf
    ",ans);
        }
        return 0;
    }
    View Code

    0×04 二分

    $Poj2018 Best Cow Fances$

     1 #include<iostream>
     2 #include<cstdio>
     3 #define il inline
     4 #define Rg register
     5 #define go(i,a,b) for(Rg int i=a;i<=b;i++)
     6 #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
     7 #define db double
     8 #define eps 1e-7
     9 #define inf 2100000000
    10 using namespace std;
    11 il int read()
    12 {
    13     int x=0,y=1;char c=getchar();
    14     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    15     while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    16     return x*y;
    17 }
    18 int n,f;
    19 db l=0,r=inf,mid,a[100001],b[100001],s[100001];
    20 il bool ck()
    21 {
    22     go(i,1,n)b[i]=a[i]-mid;
    23     go(i,1,n)s[i]=s[i-1]+b[i];
    24     db mins=inf,maxs=-inf;
    25     go(i,f,n)
    26     {
    27         mins=min(mins,s[i-f]);
    28         maxs=max(maxs,s[i]-mins);
    29     }
    30     if(maxs>=0)return 1;return 0;
    31 }
    32 int main()
    33 {
    34     n=read(),f=read();
    35     go(i,1,n)scanf("%lf",&a[i]),l=min(l,a[i]),r=max(r,a[i]);
    36     while(r-l>eps)
    37     {
    38         mid=(l+r)*1.0/2;
    39         if(ck())l=mid;
    40         else r=mid;
    41     }
    42     printf("%d
    ",(int)(r*1000));
    43     return 0;
    44 }
    View Code

    0×05 排序

    $CF670C Cinema$

    离散化或者$map$

    View Code

    $CH0501 货仓选址$

    $BZOJ3032 七夕祭$

    $Poj2784 Running Median$

    $TJ$

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    int t,t1,n,nw;
    priority_queue<int>q1;
    priority_queue<int,vector<int>,greater<int> >q2;
    int main()
    {
        t=read();
        while(t--)
        {
            t1=read(),n=read();printf("%d %d
    ",t1,n/2+1);
            while(q1.size()>0)q1.pop();
            while(q2.size()>0)q2.pop();
            go(i,1,n)
            {
                int x=read();
                if(i==1){nw=x;q2.push(x);}
                else
                {
                    if(x<nw)q1.push(x);
                    else q2.push(x);
                }
                while((int)q1.size()>(int)q2.size()){int y=q1.top();q1.pop();q2.push(y);}
                if((int)q2.size()-(int)q1.size()>1){int y=q2.top();q2.pop();q1.push(y);}
                nw=q2.top();
                if(i&1)printf("%d ",nw);
                if((i&1)&&(i/2+1)%10==0)printf("
    ");
            }
            if((n/2+1)%10)printf("
    ");
        }
        return 0;
    }
    View Code

    $Poj2299 Ultra-QuikSort$

    #include<iostream>
    #include<cstdio>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    #define ll long long
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    const int N=500001;
    int n,a[N],b[N];
    ll as;
    il void sol(int l,int r)
    {
        if(l==r)return;
        int mid=(l+r)>>1;
        sol(l,mid);sol(mid+1,r);
        Rg int i=l,j=mid+1;
        go(k,l,r)
        {
            if((a[i]<a[j]&&i<=mid) || j>r)b[k]=a[i++];
            else b[k]=a[j++],as+=mid-i+1;
        }
        go(k,l,r)a[k]=b[k];
    }
    int main()
    {
        while(1)
        {
            n=read();as=0;
            if(!n)break;
            go(i,1,n)a[i]=read();
            sol(1,n);
            printf("%lld
    ",as);
        }
        return 0;
    }
    View Code

    0×06 倍增

    $CH0601 Genius ACM$

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    #define ll long long
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    int T,n,m,as,a[500001],b[500001],c[500001];
    ll K;
    il bool ck(int l,int r,int md)
    {
        go(i,md,r)b[i]=a[i];
        sort(b+md,b+r+1);
        int i=l,j=md;
        go(k,l,r)
            if((i<=md-1 && b[i]<b[j]) || j>r)c[k]=b[i++];
            else c[k]=b[j++];
        i=l,j=r;int t=min(m,(r-l+1)/2);ll nw=0;
        while(t--)
            nw+=1LL*(c[i]-c[j])*(c[i]-c[j]),i++,j--;
        if(nw<=K)
        {
            go(i,l,r)b[i]=c[i];
            return 1;
        }
        return 0;
    }
    int main()
    {
        T=read();
        while(T--)
        {
            n=read(),m=read();scanf("%lld",&K);as=0;
            go(i,1,n)a[i]=read();
            int l=1,r=1,p=1;
            b[1]=a[1];
            while(l<=n)
            {
                if(r+p<=n && ck(l,r+p,r+1))r+=p,p*=2;
                else p/=2;
                if(!p || r==n){as++,l=++r,p=1,b[l]=a[l];}
            }
            printf("%d
    ",as);
        }
        return 0;
    }
    View Code

    0×07 贪心

    $Poj3614 Sunscreen$

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i--)
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    int n,m,as,b[1001];
    struct node{int l,r;}a[2501];
    il bool cmp(node x,node y){return x.l>y.l;}
    int main()
    {
        n=read(),m=read();
        go(i,1,n)a[i].l=read(),a[i].r=read();
        go(i,1,m){int x=read(),y=read();b[x]+=y;}
        sort(a+1,a+n+1,cmp);
        go(i,1,n)
        {
            int l=a[i].l,r=a[i].r;
            yes(j,r,l)if(b[j]>0){b[j]--;as++;break;}
        }
        printf("%d
    ",as);
        return 0;
    }
    View Code

    $Poj3190 Stall Reservation$

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #define il inline
    #define Rg register
    #define pr pair<int,int>
    #define mp make_pair
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    int n,cl[50001],ct;
    struct node{int l,r,pos;}a[50001];
    priority_queue<pr,vector<pr>,greater<pr> >q;
    il bool cmp(node x,node y){return x.l<y.l;}
    int main()
    {
        n=read();
        go(i,1,n)a[i].l=read(),a[i].r=read(),a[i].pos=i;;
        sort(a+1,a+n+1,cmp);
        q.push(mp(a[1].r,a[1].pos));cl[a[1].pos]=++ct;
        go(i,2,n)
        {
            pr t=q.top();
            if(a[i].l>t.first){q.pop();q.push(mp(a[i].r,a[i].pos));cl[a[i].pos]=cl[t.second];}
            else{q.push(mp(a[i].r,a[i].pos));cl[a[i].pos]=++ct;}
        }
        printf("%d
    ",ct);
        go(i,1,n)printf("%d
    ",cl[i]);
        return 0;
    }
    View Code

    $Poj1328 Radar Installation$

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i--)
    #define ll long long
    #define db double
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    int n,d,as;
    db nw;
    struct node{db l,r;}a[1001];
    il bool cmp(node x,node y){return x.l<y.l;}
    int main()
    {
        n=read(),d=read();nw=-1e9-1e9;
        go(i,1,n)
        {
            int x,y;x=read(),y=read();
            if(y>d){printf("-1
    ");return 0;}
            db r=sqrt((db)(d*d-y*y));
            a[i].l=x-r,a[i].r=x+r;
        }
        sort(a+1,a+n+1,cmp);
        go(i,1,n)
        {
            if(a[i].l<=nw){nw=min(nw,a[i].r);}
            else {as++,nw=a[i].r;}
        }
        printf("%d
    ",as);
        return 0;
    }
    View Code

    $Poj2054 Color a Tree$

    #include<iostream>
    #include<cstdio>
    #define il inline
    #define Rg register
    #define go(i,a,b) for(Rg int i=a;i<=b;i++)
    #define yes(i,a,b) for(Rg int i=a;i>=b;i++)
    #define db double
    #define ll long long
    using namespace std;
    il int read()
    {
        int x=0,y=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        return x*y;
    }
    int n,rt,a[1001],fa[1001],s[1001];
    ll as;
    int main()
    {
        while(1)
        {
            n=read(),rt=read();as=0;
            if(!n && !rt)return 0;
            go(i,1,n)a[i]=read(),s[i]=1;
            go(i,1,n-1){int x=read(),y=read();fa[y]=x;}
            go(T,1,n-1)
            {
                db maxs=0;int pos;
                go(i,1,n)
                    if(i!=rt && 1.0*a[i]/s[i]>=maxs)maxs=1.0*a[i]/s[i],pos=i;
                go(i,1,n)
                    if(fa[i]==pos)fa[i]=fa[pos];
                as+=a[pos]*s[fa[pos]];
                s[fa[pos]]+=s[pos];
                a[fa[pos]]+=a[pos];
                a[pos]=0;
            }
            as+=a[rt];
            printf("%lld
    ",as);
        }
        return 0;
    }
    View Code
    光伴随的阴影
  • 相关阅读:
    threadlocal 变量 跟synchronized 关键字的关系
    Android媒体扫描详细解析之一(MediaScanner & MediaProvider)
    创建视图全文搜索[完整版]
    海量小文件问题综述
    内存拷贝探究
    case功能菜单选项
    linux case ${variable} in
    attack source code
    ftps加密服务器
    vim编程设置
  • 原文地址:https://www.cnblogs.com/forward777/p/11233957.html
Copyright © 2020-2023  润新知