• [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;
    }
  • 相关阅读:
    Java 中的 volatile 关键字
    Java 隐式锁
    导致并发程序出问题的根本原因是什么?
    BZOJ_2434_[Noi2011]阿狸的打字机_AC自动机+出栈入栈序+树状数组
    BZOJ_5368_[Pkusc2018]真实排名_组合数
    CF上的3道小题(2)
    BZOJ_4199_[Noi2015]品酒大会_后缀自动机
    BZOJ_4566_[Haoi2016]找相同字符_后缀自动机
    BZOJ_3172_[Tjoi2013]单词_后缀自动机
    BZOJ_3998_[TJOI2015]弦论_后缀自动机
  • 原文地址:https://www.cnblogs.com/wzx-RS-STHN/p/13446603.html
Copyright © 2020-2023  润新知