Skip to main content
  1. Posts/

BZOJ 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题(01背包)

·2 mins
Table of Contents
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

3407: [Usaco2009 Oct]Bessie’s Weight Problem 贝茜的体重问题
#

Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 88  Solved: 79 [Submit][Status][Discuss]

Description
#

贝茜像她的诸多姊妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干草.贝茜只能吃一整捆干草;当她开始吃一捆干草的之后就再也停不下来了.她有一个完整

的N(1≤N≤500)捆可以给她当作晚餐的干草的清单.她自然想要尽量吃到更多的干草.很自然地,每捆干草只能被吃一次(即使在列表中相同的重量可能出现2次,但是这表示的是两捆干草,其中每捆干草最多只能被吃掉一次).

给定一个列表表示每捆干草的重量Si(1≤Si≤H),求贝茜不超过节食的限制的前提下可以吃掉多少干草(注意一旦她开始吃一捆干草就会把那一捆干草全部吃完).

Input
#

第1行:两个由空格隔开的整数日和N.

第2到第N+1行:第i+l行是一个单独的整数,表示第i捆干草的重量Si.

Output
#

一个单独的整数表示贝茜在限制范围内最多可以吃多少公斤的干草.

Sample Input
#

56 4 15 19 20 21

Sample Output
#

56

HINT
#

有四捆草,重量分别是15,19,20和21.贝茜在56公斤的限制范围内想要吃多少就可以吃多少.

贝茜可以吃3捆干草(重量分别为15,20,21).恰好达到她的56公斤的限制.

``

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月14日 星期四 19时33分09秒
 4File Name :code/bzoj/3407.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=5E4+7;
34int n,V;
35int a[505];
36int dp[N];
37
38void solve ( int cost,int val)
39{
40    for ( int i = V ; i >= cost ; i--)
41	dp[i] = max(dp[i],dp[i-cost]+val);
42}
43int main()
44{
45	#ifndef  ONLINE_JUDGE
46	freopen("code/in.txt","r",stdin);
47  #endif
48
49	scanf("%d %d",&V,&n);
50	for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
51
52	ms(dp,0);
53	for ( int i = 1 ; i <= n ; i++)
54	{
55	    solve(a[i],a[i]);
56	}
57
58	int ans = 0 ;
59	for ( int i = V ; i >= 0 ; i--) ans = max(ans,dp[i]);
60	printf("%d\n",ans);
61
62  #ifndef ONLINE_JUDGE
63  fclose(stdin);
64  #endif
65    return 0;
66}

Related

BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster (dp,类似01背包)

·3 mins
# Time Limit: 5 Sec Memory Limit: 64 MB Submit: 504 Solved: 265 [Submit][Status][Discuss] Description # The cows are building a roller coaster! They want your help to design as fun a roller coaster as possible, while keeping to the budget. The roller coaster will be built on a long linear stretch of land of length L (1 <= L <= 1,000). The roller coaster comprises a collection of some of the N (1 <= N <= 10,000) different interchangable components. Each component i has a fixed length Wi (1 <= Wi <= L). Due to varying terrain, each component i can be only built starting at location Xi (0 <= Xi <= L-Wi). The cows want to string together various roller coaster components starting at 0 and ending at L so that the end of each component (except the last) is the start of the next component. Each component i has a “fun rating” Fi (1 <= Fi <= 1,000,000) and a cost Ci (1 <= Ci <= 1000). The total fun of the roller coster is the sum of the fun from each component used; the total cost is likewise the sum of the costs of each component used. The cows’ total budget is B (1 <= B <= 1000). Help the cows determine the most fun roller coaster that they can build with their budget.

hdu 1171 Big Event in HDU (母函数,01背包)

·3 mins
**Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26534 Accepted Submission(s): 9332 ** Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002. The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).