zoukankan      html  css  js  c++  java
  • [USACO08DEC]干草出售Hay For Sale

    题目

    Description

    Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his hay inventory, leaving him with nothing to feed the cows. He hitched up his wagon with capacity C (1 <= C <= 50,000) cubic units and sauntered over to Farmer Don's to get some hay before the cows miss a meal.

    Farmer Don had a wide variety of H (1 <= H <= 5,000) hay bales for sale, each with its own volume (1 <= V_i <= C). Bales of hay, you know, are somewhat flexible and can be jammed into the oddest of spaces in a wagon.

    FJ carefully evaluates the volumes so that he can figure out the largest amount of hay he can purchase for his cows.

    Given the volume constraint and a list of bales to buy, what is the greatest volume of hay FJ can purchase? He can't purchase partial bales, of course. Each input line (after the first) lists a single bale FJ can buy.

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

    他最多可以运回多少体积的干草呢?

    Input

    * Line 1: Two space-separated integers: C and H

    * Lines 2..H+1: Each line describes the volume of a single bale: V_i

    Output

    * Line 1: A single integer which is the greatest volume of hay FJ can purchase given the list of bales for sale and constraints.

    Sample Input

    7 3 
    2 
    6 
    5 

    Sample Output

    7 

    思路

    很明显是一道背包题;

    但是直接背包模板是过不了的;会$TLE$;

    只需要一个特判就ok了,如果已经背出了背包的最大容量,那么直接输出最大容量,然后结束程序;

    由于博主蒟蒻,非常喜欢快,所以就加了$bitset$ 优化;

    代码

    #include<bits/stdc++.h>
    #define re register
    typedef long long ll;
    using namespace std;
    inline ll read()
    {
        ll a=0,f=1; char c=getchar();
        while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
        while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
        return a*f;
    }
    ll n,m;
    ll a[6010];
    bitset<50010> dp;//bitset的定义
    int main()
    {
        dp[0]=1;//初始化,没它,你wa
        m=read(); n=read();
        for(re ll i=1;i<=n;i++)
        {
            a[i]=read();
            dp|=(dp<<a[i]);
            if(dp[m])//特判
            {
                printf("%lld
    ",m);
                return 0;
            }
        }
        for(re ll i=m;i>=1;i--)
        if(dp[i])//逆循环,找到最大的权值
        {
            printf("%lld
    ",i);
            break;
        }
            //return 0;
    }
  • 相关阅读:
    Vue基本指令
    C primer plus 6 编程练习答案
    Archlinux安装总结
    xcfe桌面快捷键整理
    openSUSE XFCE桌面 多媒体解码器安装
    Arch i3wm
    arch xfce快捷键
    Archlinux配置~小米笔记本Air 13.3英寸版本
    Archlinux安装指南~小米笔记本Air 13.3英寸版本
    Linux~Archer 进化之路
  • 原文地址:https://www.cnblogs.com/wzx-RS-STHN/p/13446603.html
Copyright © 2011-2022 走看看