• [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛B


    【SinGuLaRiTy-1037】 Copyright (c) SinGuLaRiTy 2017. All Rights Reserved.

    Chess

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。现在要问问你,满足要求的方案数是多少。

    Input

    第一行一个正整数T,表示数据组数。对于每组数据:一行,两个正整数N和M(N<=1000,M<=1000)。

    Output

    对于每组数据输出一行,代表方案数模1000000007(1e9+7)。

    Sample Input

    1
    1 1

    Sample Output

    1

    Code

    基本组合数学求组合数

    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    
    #define MAXN 1010
    #define LL long long
    
    using namespace std;
    
    int T;
    
    LL n,m,p=1000000007;
    
    LL quick_mod(LL a,LL b)
    {
        LL ans=1;
        a%=p;
        while(b)
        {
            if(b&1)
            {
                ans=ans*a%p;
                b--;
            }
            b>>=1;
            a=a*a%p;
        }
        return ans;
    }
    
    LL C(LL n,LL m)
    {
        if(m>n)
            return 0;
        LL ans=1;
        for(int i=1;i<=m;i++)
        {
            LL a=(n+i-m)%p;
            LL b=i%p;
            ans=ans*(a*quick_mod(b,p-2)%p)%p;
        }
        return ans;
    }
    
    LL Lucas(LL n,LL m)
    {
        if(m==0)
            return 1;
        return C(n%p,m%p)*Lucas(n/p,m/p)%p;
    }
    
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            cin>>n>>m;
            if(n>m)
                swap(n,m);
            if(n==0||m==0)
            {
                cout<<0;
                continue;
            }
            cout<<Lucas(m,n)<<endl;
        }
        return 0;
    }

    Factory

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)

    Problem Description

    我们将A省简化为由N个城市组成,某些城市之间存在双向道路,而且A省的交通有一个特点就是任意两个城市之间都能通过道路相互到达,且在不重复经过城市的情况下任意两个城市之间的到达方案都是唯一的。聪明的你一定已经发现,这些城市构成了树这样一个结构。现在百度陆续开了许许多多的子公司。每家子公司又会在各城市中不断兴建属于该子公司的办公室。由于各个子公司之间经常有资源的流动,所以公司员工常常想知道,两家子公司间的最小距离。我们可以把子公司看成一个由办公室组成的集合。那么两个子公司A和B的最小距离定义为min(dist(x,y))(x∈A,y∈B)。其中dist(x,y)表示两个办公室之间的最短路径长度。现在共有Q个询问,每次询问分别在两个子公司间的最小距离。

    Input

    第一行一个正整数T,表示数据组数。对于每组数据:第一行两个正整数N和M。城市编号为1至N,子公司编号为1至M。接下来N-1行给定所有道路的两端城市编号和道路长度。接下来M行,依次按编号顺序给出各子公司办公室所在位置,每行第一个整数G,表示办公室数,接下来G个数为办公室所在位置。接下来一个整数Q,表示询问数。接下来Q行,每行两个正整数a,b(a不等于b),表示询问的两个子公司。【数据范围】0<=边权<=100 1<=N,M,Q,工厂总数<=100000

    Output

    对于每个询问,输出一行,表示答案。

    Sample Input

    1
    3 3
    1 2 1
    2 3 1
    2 1 1
    2 2 3
    2 1 3
    3
    1 2
    2 3
    1 3

    Sample Output

    1
    0
    0

    Code

    预处理LCA+容斥原理

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    
    #define inf 0x3f3f3f3f
    
    using namespace std;
    
    int fa[100010],anc[100010][32],deep[100010],dis[100010];
    struct node
    {
        int to;
        int val;
    };
    vector<struct node > edge[100010];
    
    int T,n,m;
    
    vector<int > vec[100010];
    
    void dfs(int x)
    {
        anc[x][0]=fa[x];
        for(int i=1;i<=31;i++)
        {
            anc[x][i]=anc[anc[x][i-1]][i-1];
        }
        int len=edge[x].size();
        for(int i=0;i<len;i++)
        {
            struct node next=edge[x][i];
            if(next.to!=fa[x])
            {
                fa[next.to]=x;
                deep[next.to]=deep[x]+1;
                dis[next.to]=dis[x]+next.val;
                dfs(next.to);
            }
        }
    }
    
    int LCA(int x,int y)
    {
        if(deep[x]<deep[y])
            swap(x,y);
        for(int i=31;i>=0;i--)
        {
            if(deep[y]<=deep[anc[x][i]])
            {
                x=anc[x][i];
            }
        }
        if(x==y)
            return x;
        for(int i=31;i>=0;i--)
        {
            if(anc[x][i]!=anc[y][i])
            {
                x=anc[x][i];
                y=anc[y][i];
            }
        }
        return anc[x][0];
    }
    
    void Init()
    {
        for(int i=0;i<=n;i++)
        {
            edge[i].clear();
            vec[i].clear();
        }
        memset(fa,0,sizeof(fa));
        memset(anc,0,sizeof(anc));
    }
    
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            int u,v,w;
            scanf("%d%d",&n,&m);
            Init();
            for(int i=1;i<n;i++)
            {
                scanf("%d%d%d",&u,&v,&w);
                edge[u].push_back((node){v,w});
                edge[v].push_back((node){u,w});
            }
            for(int i=1;i<=m;i++)
            {
                int x;
                scanf("%d",&x);
                for(int j=1;j<=x;j++)
                {
                    int y;
                    scanf("%d",&y);
                    vec[i].push_back(y);
                }
            }
            fa[1]=1;
            dfs(1);
            int q;
            scanf("%d",&q);
            while(q--)
            {
                int ans=inf;
                scanf("%d%d",&u,&v);
                int l1=vec[u].size();
                int l2=vec[v].size();
                for(int i=0;i<l1;i++)
                {
                    for(int j=0;j<l2;j++)
                    {
                        int lca=LCA(vec[u][i],vec[v][j]);
                        ans=min(ans,dis[vec[u][i]]+dis[vec[v][j]]-dis[lca]*2);
                    }
                }
                printf("%d
    ",ans);
            }
        }
        return 0;
    }

    度度熊的交易计划

    Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题:
    喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区。
    由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但是最多生产b[i]个。
    同样的,由于每个片区的购买能力的区别,第i个片区也能够以c[i]的价格出售最多d[i]个物品。
    由于这些因素,度度熊觉得只有合理的调动物品,才能获得最大的利益。
    据测算,每一个商品运输1公里,将会花费1元。
    那么喵哈哈村最多能够实现多少盈利呢?

    Input

    本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示喵哈哈村由n个片区、m条街道。 接下来n行,每行四个整数a[i],b[i],c[i],d[i]表示的第i个地区,能够以a[i]的价格生产,最多生产b[i]个,以c[i]的价格出售,最多出售d[i]个。 接下来m行,每行三个整数,u[i],v[i],k[i],表示该条公路连接u[i],v[i]两个片区,距离为k[i]
    可能存在重边,也可能存在自环。
    满足: 1<=n<=500, 1<=m<=1000, 1<=a[i],b[i],c[i],d[i],k[i]<=1000, 1<=u[i],v[i]<=n

    Output

    输出最多能赚多少钱。

    Sample Input

    2 1
    5 5 6 1
    3 5 7 7
    1 2 1

    Sample Output

    23

    Code

    最小费用最大流

    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<iostream>
    #include<queue>
    
    #define INF 0x3f3f3f3f
    #define LL long long
    
    using namespace std;
    
    const int M=2010;
    const int N=510;
    
    struct edge
    {
        int to;
        int next;
        int cap;
        int cost;
    } e[11000];
    
    int head[N],tot;
    int d[N], pre[N], path[N];
    bool vis[N];
    
    void init()
    {
        memset(head,-1,sizeof(head));
        tot=0;
    }
    
    void addedge(int s,int t,int cap,int cost)
    {
        e[tot].to=t;
        e[tot].cap=cap;
        e[tot].cost=cost;
        e[tot].next=head[s];
        head[s]=tot++;
        e[tot].to=s;
        e[tot].cap=0;
        e[tot].cost=-cost;
        e[tot].next=head[t];
        head[t]=tot++;
    }
    
    int spfa(int s,int t)
    {
        memset(d,-INF,sizeof(d));
        memset(pre,-1,sizeof(pre));
        memset(path,-1,sizeof(path));
        memset(vis,false,sizeof(vis));
    
        int res=d[0];
        d[s]=0;
        vis[s]=true;
        queue<int>q;
        q.push(s);
        while (!q.empty())
        {
            int u=q.front();
            q.pop();
            vis[u]=false;
            for (int i=head[u];~i;i=e[i].next)
            {
                int v=e[i].to;
                if (d[v]<d[u]+e[i].cost&&e[i].cap>0)
                {
                    d[v]=d[u]+e[i].cost;
                    pre[v]=u;
                    path[v]=i;
                    if(!vis[v])
                    {
                        vis[v]=true;
                        q.push(v);
                    }
                }
            }
        }
        return d[t]!=res;
    }
    
    int MinCostMaxFlow(int s,int t,int &cost)
    {
        int flow;
        flow=cost=0;
        while(spfa(s,t))
        {
            int minn=INF;
            for(int i=t;i!=s&&~i;i=pre[i])
                minn=min(minn,e[path[i]].cap);
            for(int i=t;i!=s&&~i;i=pre[i])
            {
                e[path[i]].cap-=minn;
                e[path[i]^1].cap+=minn;
            }
            if(d[t]<0)
                break;
            flow+=minn;
            cost+=minn*d[t];
        }
        return flow;
    }
    
    int main(void)
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            init();
            int s=0,t=n+1,cost;
            for(int i=1;i<=n;i++)
            {
                int a,b,c,d;
                cin>>a>>b>>c>>d;
                addedge(s,i,b,-a);
                addedge(i,t,d,c);
            }
            while(m--)
            {
                int u,v,k;
                cin>>u>>v>>k;
                addedge(u,v,INF,-k);
                addedge(v,u,INF,-k);
            }
            MinCostMaxFlow(s,t,cost);
            cout<<cost<<endl;
        }
        return 0;
    }

    小小粉丝度度熊

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Problem Description

    度度熊喜欢着喵哈哈村的大明星——星星小姐。为什么度度熊会喜欢星星小姐呢?首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听。但这都不是最重要的,最重要的是,星星小姐拍的一手好代码!于是度度熊关注了星星小姐的贴吧。一开始度度熊决定每天都在星星小姐的贴吧里面签到。但是度度熊是一个非常健忘的孩子,总有那么几天,度度熊忘记签到,于是就断掉了他的连续签到。不过度度熊并不是非常悲伤,因为他有m张补签卡,每一张补签卡可以使得某一忘签到的天,变成签到的状态。那么问题来了,在使用最多m张补签卡的情况下,度度熊最多连续签到多少天呢?

    Input

    本题包含若干组测试数据。第一行两个整数n,m,表示有n个区间,这n个区间内的天数,度度熊都签到了;m表示m张补签卡。接下来n行,每行两个整数(l[i],r[i]),表示度度熊从第l[i]天到第r[i]天,都进行了签到操作。数据范围:1<=n<=100000 0<=m<=10000000000<=l[i]<=r[i]<=1000000000注意,区间可能存在交叉的情况。

    Output

    输出度度熊最多连续签到多少天。

    Sample Input

    2 1
    1 1
    3 3
    1 2
    1 1

    Sample Output

    3
    3

    Code

    尺取法找最大区间

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    
    using namespace std;
    
    typedef long long ll;
    const int maxn = 200000+10;
    
    ll c[maxn];
    
    struct node
    {
        int type;
        int pos;
    }arr[maxn];
    
    struct gg
    {
        int l,r;
    }qj[maxn];
    
    bool cmp(node a,node b)
    {
        if(a.pos==b.pos)
            return a.type<b.type;
        return a.pos<b.pos;
    }
    
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=0;i<n;i++)
            {
                arr[i].type=0;
                arr[i+n].type=1;
                scanf("%d%d",&arr[i].pos,&arr[i+n].pos);
            }
            sort(arr,arr+n*2,cmp);
            int len=0,num=1,spos;
            spos=arr[0].pos;
            for(int i=1;i<n*2;i++)
            {
                if(arr[i].type==0)
                {
                    if(num==0)
                        spos=arr[i].pos;
                    num++;
                }
                if(arr[i].type==1)
                    num--;
                if(num==0)
                {
                    qj[len].l=spos,qj[len].r=arr[i].pos;
                    len++;
                }
            }
            ll ans=0;
            c[0]=0;
            for(int i=0;i<len;i++)
            {
                c[i+1]=c[i]+qj[i].r-qj[i].l+1;
            }
            for(int i=0,j=0;i<len;i++)
            {
                while(j<len&&(qj[j].r-qj[i].l+1-c[j+1]+c[i]<=m))
                {
                    ans=max(ans,(ll)(qj[j].r-qj[i].l+1));
                    j++;
                }
                ll tep=c[j]-c[i];
                ans=max(ans,tep+m);
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }

    Time: 2017-08-15

  • 相关阅读:
    第一次个人编程作业
    第一次软工作业
    [Manacher]最长回文子串
    面向对象程序设计 总结作业
    面向对象程序设计 小黄衫晒单
    面向对象程序设计 作业三
    面向对象程序设计 作业二
    面向对象程序设计 作业一
    SSD/Memory技术学习拼图
    第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/SinGuLaRiTy2001/p/7366112.html
Copyright © 2020-2023  润新知