zoukankan      html  css  js  c++  java
  • [BZOJ1606] [Usaco2008 Dec] Hay For Sale 购买干草 (dp)

    Description

      约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包干草,每一包都有它的体积Vi(l≤Vi≤C).约翰只能整包购买,
      他最多可以运回多少体积的干草呢?

    Input

        第1行输入C和H,之后H行一行输入一个Vi.

    Output

        最多的可买干草体积.

    Sample Input

    7 3 //总体积为7,用3个物品来背包
    2
    6
    5

      The wagon holds 7 volumetric units; three bales are offered for sale with
    volumes of 2, 6, and 5 units, respectively.

    Sample Output

    7 //最大可以背出来的体积

    HINT

      Buying the two smaller bales fills the wagon.

    Source

      Silver

    Solution

      暴力dp可以水过我会乱说?

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int a[5005];
     4 bool f[50005];
     5 int main()
     6 {
     7     int ans, c, h;
     8     scanf("%d%d", &c, &h);
     9     for(int i = 1; i <= h; i++)
    10         scanf("%d", a + i);
    11     f[0] = true;
    12     for(int i = 1; i <= h; i++)
    13         for(int j = c - a[i]; ~j; j--)
    14             if(f[j]) f[j + a[i]] = true;
    15     for(int i = 1; i <= c; i++)
    16         if(f[i]) ans = i;
    17     printf("%d
    ", ans);
    18     return 0;
    19 }
    View Code
  • 相关阅读:
    HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树
    字典树 HDU 1075 What Are You Talking About
    字典树 HDU 1251 统计难题
    最小生成树prim算法 POJ2031
    POJ 1287 Networking 最小生成树
    次小生成树 POJ 2728
    最短路N题Tram SPFA
    poj2236 并查集
    POJ 1611并查集
    Number Sequence
  • 原文地址:https://www.cnblogs.com/CtrlCV/p/5525450.html
Copyright © 2011-2022 走看看