• 51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划


    有一个箱子容量为 V(正整数,0<=V<=20000),同时有 n 个物品(0<n<=30),每个物品有一个体积(正整数)。

    现在在 n 个物品中,任取若干个装入箱内,使得箱子的剩余空间为最小。

     收起

    输入

    输入:一个整数v,表示箱子容量
    一个整数n,表示有n个物品
    接下来 n 个整数,分别表示这 n 个物品的各自体积

    输出

    输出:一个整数,表示箱子最小的剩余空间

    输入样例

    24
    6
    8
    3
    12
    7
    9
    7

    输出样例

    0
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<set>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<stack>
    #include<bitset>
    #include<cstdio>
    #include<cstring>
    //---------------------------------Sexy operation--------------------------//
    
    #define cini(n) scanf("%d",&n)
    #define cinl(n) scanf("%lld",&n)
    #define cinc(n) scanf("%c",&n)
    #define cins(s) scanf("%s",s)
    #define coui(n) printf("%d",n)
    #define couc(n) printf("%c",n)
    #define coul(n) printf("%lld",n)
    #define speed ios_base::sync_with_stdio(0)
    #define file  freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
    //-------------------------------Actual option------------------------------//
    
    #define Swap(a,b) a^=b^=a^=b
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a<b?a:b
    #define mem(n,x) memset(n,x,sizeof(n))
    #define mp(a,b) make_pair(a,b)
    //--------------------------------constant----------------------------------//
    
    #define INF  0x3f3f3f3f
    #define maxn  100005
    #define esp  1e-9
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    //------------------------------Dividing Line--------------------------------//
    int v,n;
    int a[maxn];
    int dp[maxn];
    int  main()
    {
        cini(v),cini(n);
        for(int i=0; i<n; i++)
            cini(a[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=v;j>=a[i];j--)
            {
              dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
            }
        }
        cout<<v-dp[v]<<endl;
    }
    
  • 相关阅读:
    Paxos算法理解
    JavaScript笔记
    JVM基础知识(二)
    JVM基础知识(一)
    书单
    centos6.3环境下升级python及MySQLdb的安装
    winform中button的image属性无法更改
    未能找到任何适合于指定的区域性或非特定区域性的资源
    Spring注解开发第十二讲--@Profile注解讲解
    Spring注解驱动第十一讲--引用Spring底层组件
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798780.html
Copyright © 2020-2023  润新知