zoukankan      html  css  js  c++  java
  • E. Coin Troubles 夜

    http://codeforces.com/contest/284/problem/E

    思维很重要呀 原来是一个背包呀

    代码:

    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<algorithm>
    
    #define LL long long
    
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const int MOD=1000000007;
    const int N=305;
    const int M=100005;
    int a[N],next[N];
    bool head[N];
    int dp[M];
    vector< vector<int> >h;
    void packComplete(int cost,int V)
    {
        for(int v=cost;v<=V;++v)
        dp[v]=(dp[v]+dp[v-cost])%MOD;
    }
    int main()
    {
        //freopen("data.in","r",stdin);
        int n,q,t;
        while(cin>>n>>q>>t)
        {
            h.clear();
            memset(next,0,sizeof(next));
            for(int i=1;i<=n;++i)
            cin>>a[i];
            memset(head,true,sizeof(head));
            while(q--)
            {
                int l,r;
                cin>>l>>r;
                head[r]=false;
                next[l]=r;
            }
            int line[N],m;
            for(int i=1;i<=n;++i)
            if(head[i]==true)
            {
                int l=i;
                m=0;
                line[m++]=l;
                while(next[l]!=0)
                {
                    l=next[l];
                    line[m++]=l;
                }
                h.push_back(vector<int>(line,line+m));
            }
            int num=0;
            for(int unsigned i=0;i<h.size();++i)
            num+=h[i].size();
            if(num<n)
            {cout<<"0"<<endl;continue;}
            memset(dp,0,sizeof(dp));
            int V=t;
            int k=0;
            dp[0]=1;
            for(unsigned int i=0;i<h.size();++i)
            {
                int tmp=0;
                for(unsigned int j=0;j<h[i].size();++j)
                {
                    int l=h[i][j];
                    if(k<=V)
                    k=k+(h[i].size()-1-j)*a[l];
                    if(tmp>V) continue;
                    tmp+=a[l];
                    if(tmp<=V)
                    packComplete(tmp,V);
                }
            }
            if(k>V)
            {cout<<"0"<<endl;continue;}
            cout<<dp[V-k]<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    并发解决方案
    主外键
    ms
    mq消息丢失
    五种IO模型
    程序运行时间计算gettimeofday&clock_gettime
    exec与xargs区别
    mysql修改数据存放路径(linux)
    od用不同的编码输出文件内容
    segfault at f0 ip 00007f9954108feb sp 00007fffb5e64fc0 error 4 in libxxx.so[7f99540df000+54000]
  • 原文地址:https://www.cnblogs.com/liulangye/p/2966655.html
Copyright © 2011-2022 走看看