zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    python 中range函数的用法
    python之字符串中有关%d,%2d,%02d的问题
    大数据面试题
    HDFS的副本存放策略(全)
    hadoop 集群中数据块的副本存放策略
    HDFS 安全模式的理解
    蓝桥杯第十届真题B组(2019年)
    ubuntu下如何编译C语言
    Ubuntu下安装kate编辑器
    Command 'ifconfig' not found, but can be installed with: sudo apt install net-tools
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798780.html
Copyright © 2011-2022 走看看