[动态规划] 洛谷P1616 疯狂采药 (完全背包问题)

题目

LP1616

思路

完全背包裸题

代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define _for(i,a,b) for(int i = (a); i<(b); i++)
#define _rep(i,a,b) for(int i = (a); i<=(b); i++)
using namespace std;

const int maxn = 10000+10;
const int maxx = 100000+10;
int n, TT, d[maxx], V[maxn], W[maxn];
// 码农小技巧:对于多个maxn的时候,经常容易写错(d[maxn]),导致RE,应该极其注意,因为考场难发现。 

int main(){
    scanf("%d%d",&TT,&n);
    _rep(i,1,n) scanf("%d%d",&V[i],&W[i]);

    d[TT] = 0;
    for(int T = TT-1; T>=0; T--)
        _rep(i,1,n)
            if(T+V[i] <= TT)
                d[T] = max(d[T], d[T+V[i]] + W[i]);
    printf("%d\n",d[0]);
    return 0;
}

码农小技巧

1.对于多个maxn的时候,经常容易写错(d[maxn]),导致RE,应该极其注意,因为考场难发现。

猜你喜欢

转载自blog.csdn.net/icecab/article/details/80768367