【动态规划】Vijos P1104 采药(NOIP2005普及组第三题)

本文详细解析了01背包问题的经典算法,并通过一个具体案例进行代码实现。使用动态规划解决在有限时间内如何选择物品以获得最大收益的问题,为读者提供了清晰的思路和高效的一维数组解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

  https://vijos.org/p/1104

题目大意:

  T时间,n个物品,每个耗时ti,可获得收益ci,求最大收益。

题目思路:

  【动态规划】

  01背包裸题。一维二维都过了,放个一维吧。

 

 1 //
 2 //by coolxxx
 3 ////<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 //#include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define mem(a,b) memset(a,b,sizeof(a))
22 #define eps (1e-8)
23 #define J 10
24 #define MAX 0x7f7f7f7f
25 #define PI 3.14159265358979323
26 #define N 104
27 #define M 1004
28 using namespace std;
29 typedef long long LL;
30 int cas,cass;
31 int n,m,lll,ans;
32 int v[N],c[N];
33 int f[M];
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37 //    freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j,k,l;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43 //    while(~scanf("%s",s))
44     while(~scanf("%d",&m))
45     {
46         mem(f,0);
47         scanf("%d",&n);
48         for(i=1;i<=n;i++)
49             scanf("%d%d",&v[i],&c[i]);
50         for(i=1;i<=n;i++)
51         {
52             for(j=m;j>=v[i];j--)
53                 f[j]=max(f[j],f[j-v[i]]+c[i]);
54         }
55         printf("%d\n",f[m]);
56     }
57     return 0;
58 }
59 /*
60 //
61 
62 //
63 */
View Code

 

转载于:https://www.cnblogs.com/Coolxxx/p/5773459.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值