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

题目链接:

  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
",f[m]);
56     }
57     return 0;
58 }
59 /*
60 //
61 
62 //
63 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5773459.html