• USACO Money Systems Dp 01背包


    一道经典的Dp..01背包

    定义dp[i] 为需要构造的数字为i 的所有方法数

    一开始的时候是这么想的

        for(i = 1; i <= N; ++i){
            for(j = 1; j <= V; ++j){
                if(i - a[j] > 0){
                    dp[i] += dp[i - a[j]];
                }
            }
        }
    

    状态存在冗余, 输出的时候答案肯定不对

    但只需要改一下两个for循环的顺序即可。

    Source Code:

    /*
    ID: wushuai2
    PROG: money
    LANG: C++
    */
    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-7      ;
    const int M = 660000         ;
    const ll P = 10000000097ll   ;
    const int INF = 0x3f3f3f3f   ;
    const int MAX_N = 20         ;
    const int MAXSIZE = 101000000;
    
    ofstream fout ("money.out");
    ifstream fin ("money.in");
    
    int V, N, a[30];
    ll dp[11000];
    
    int main(){
        int i, j, k, l, m, n, t, s, c, w, q, num;
        fin >> V >> N;
        for(i = 1; i <= V; ++i){
            fin >> a[i];
        }
        dp[0] = 1;
        for(i = 1; i <= V; ++i){
            for(j = a[i]; j <= N; ++j){
                if(j - a[i] >= 0){
                    dp[j] += dp[j - a[i]];
                }
            }
        }
        /*
        for(i = 1; i <= N; ++i){
            for(j = 1; j <= V; ++j){
                if(i - a[j] > 0){
                    dp[i] += dp[i - a[j]];
                }
            }
        }
        */
        fout << dp[N] << endl;
    
        fin.close();
        fout.close();
        return 0;
    }
  • 相关阅读:
    Python学习 5day__基础知识
    pycharm 2018.1 专业版激活 亲测可用!!!
    JQuery 中 find() 和 filter() 的区别
    React 事件处理
    React 数据传递
    js操作cookie的一些注意项
    html5的技术要点
    css背景设置,让套图中某张图片居中显示的例子
    js对象封装内部图片的相关代码,采用base64图片串
    针对网上很多抱怨的言论,写了一个EF中update对象时,通用的遍历赋值方法,以供参考
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4297245.html
Copyright © 2020-2023  润新知