zoukankan      html  css  js  c++  java
  • JustOj 2009: P1016 (dp)

    题目描述
      有一个箱子容量为v(正整数,o≤v≤20000),同时有n个物品(o≤n≤30),每个物品有一个体积  (正整数)。要求从  n  个物品中,任取若干个装入箱内,使箱子的剩余空间为最小。 
    输入
    第一行,一个整数,表示箱子容量;  第二行,一个整数,表示有n个物品;  接下来n行,分别表示这n个物品的各自体积。 
    输出
    一个整数,表示箱子剩余空间。
    样例输入
    24
    6
    8
    3
    12
    7
    9
    7
    
    样例输出
    0

    题解:背包问题
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=10005;
    33 const int mod=1e9+7;
    34 int a[31];
    35 int main()
    36 {
    37     std::ios::sync_with_stdio(false);
    38     int v,n,s,i;
    39     cin>>v>>n;
    40     int dp[v+1];
    41     memset(dp,0,sizeof(dp));
    42     for(i=0;i<n;i++){
    43         cin>>a[i];
    44         for(int j=v;j>=a[i];j--){
    45             dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
    46         }
    47     }
    48     cout<<v-dp[v];
    49     return 0;
    50 }
  • 相关阅读:
    "alert(1) to win" writeup
    "CoolShell puzzle game" writeup
    Maximum Subarray(最大连续子序列和)
    hacking 学习站
    爬虫:备份人人网状态
    ichunqiu在线挑战--网站综合渗透实验 writeup
    ichunqiu在线挑战--我很简单,请不要欺负我 writeup
    IDF-CTF-简单的js加密 writeup
    IDF-CTF-cookie欺骗 writeup
    IDF-CTF-不难不易的js加密 writeup
  • 原文地址:https://www.cnblogs.com/shixinzei/p/7281625.html
Copyright © 2011-2022 走看看