Backpack DP Pan Do

DP is too weak, do more.
Ended up doing a bunch of stupid questions. . .

Luogu P2639:

01 Backpack

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define max(a, b) (a < b ? b : a)
const int MAXN = 500;
using namespace std;

inline int read() {
    int x = 0, w = 1;
    char c = ' ';

    while (c < '0' || c > '9') {
        c = getchar();
        if (c == '-') w = -1;
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x * w;
}

int n, m, dp[45000 + 5], a[MAXN + 5];

void init() {
    m = read();
    n = read();

    for (int i = 1; i <= n; ++i) 
        a[i] = read();
}

int main() {
    init();
    
    for (int i = 1; i <= n; ++i)
        for (int j = m; j >= a[i]; --j)
            dp[j] = max(dp[j - a[i]] + a[i], dp[j]);

    printf("%d\n", dp[m]);

    return 0;
}

Luogu P2722

#include <iostream>
#include <cstdio>
const int MAXN = 10000;
using namespace std;

inline int read() {
    int x = 0, w = 1;
    char c = ' ';

    while (c < '0' || c > '9') {
        c = getchar();
        if (c == '-') w = -1;
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x * w;
}

struct Node {
    int num, t;
}a[MAXN + 5];

int n, m, dp[MAXN + 5];

void init() {
    m = read();
    n = read();

    for (int i = 1; i <= n; ++i) {
        a[i].num = read();
        a[i].t = read();
    }
}

int main() {
    init();
 
    for (int i = 1; i <= n; ++i) 
        for (int j = a[i].t; j <= m; ++j)
            dp[j] = max(dp[j - a[i].t] + a[i].num, dp[j]);

    printf("%d\n", dp[m]);

    return 0;
}

Luogu P2347:

Multiple backpacks with bitset water method.

#include <iostream>
#include <cstdio>
#include <bitset>
const int MAXN = 1000;
using namespace std;

inline int read() {
    int x = 0, w = 1;
    char c = ' ';

    while (c < '0' || c > '9') {
        c = getchar();
        if (c == '-') w = -1;
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x * w;
}

int n = 6, m = 1000, dp[MAXN + 5];
int w[MAXN + 5] = {0, 1, 2, 3, 5, 10, 20}, a[MAXN + 5]; 

bitset < 1001 > S;

void init() {
    for (int i = 1; i <= n; ++i)
        a[i] = read();

    S[0] = 1;
}

int main() {
    init();

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= a[i]; ++j)
            S |= S << w[i];
        
    printf("%d\n", S.count() - 1);

    return 0;
}

Luogu P2925:

#include <iostream>
#include <cstdio>
const int MAXN = 50000;
using namespace std;

inline int read() {
    int x = 0, w = 1;
    char c = ' ';

    while (c < '0' || c > '9') {
        c = getchar();
        if (c == '-') w = -1;
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x * w;
}

int n, m, dp[MAXN + 5];
int a[MAXN + 5];

void init() {
    m = read();
    n = read();

    for (int i = 1; i <= n; ++i)
        a[i] = read();
}

int main() {
    init();
    
    for (int i = 1; i <= n; ++i)
        for (int j = m; j >= a[i]; --j)
            dp[j] = max(dp[j - a[i]] + a[i], dp[j]);

    printf("%d\n", dp[m]);

    return 0;
}

Los Valley P1910

#include <iostream>
#include <cstdio>
const int MAXN = 100;
using namespace std;

inline int read() {
    int x = 0, w = 1;
    char c = ' ';

    while (c < '0' || c > '9') {
        c = getchar();
        if (c == '-') w = -1;
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x * w;
}

struct Node {
    int a, b, c;
}a[MAXN + 5];

int dp[1000 + 5][1000 + 5], n, m, x;

void  init() {
    n = read();
    m = read();
    x = read();

    for (int i = 1; i <= n; ++i) {
        a[i].a = read();
        a[i].b = read();
        a[i].c = read();
    }
}

int main() {
    init();

    for (int i = 1; i <= n; ++i)
        for (int j = m; j >= a[i].b; --j)
            for (int k = x; k >= a[i].c; --k)
                dp[j][k] = max(dp[j - a[i].b][k - a[i].c] + a[i].a, dp[j][k]);
    
    printf("%d\n", dp[m][x]);

    return 0;
}

Luogu P1734

#include <iostream>
#include <cstdio>
const int MAXN = 1000;
using namespace std;

inline int read() {
    int x = 0, w = 1;
    char c = ' ';

    while (c < '0' || c > '9') {
        c = getchar();
        if (c == '-') w = -1;
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x * w;
}

int n, a[MAXN + 5], dp[MAXN + 5];

void init() {
    n = read();

    for (int i = 1; i <= n; ++i) 
        for (int j = 1; j < i; ++j)
            if (i % j == 0) a[i] += j;
} 

int main() {
    init();
    
    for (int i = 1; i <= n; ++i)
        for (int j = n; j > i; --j)
            dp[j] = max(dp[j - i] + a[i], dp[j]);

    printf("%d\n", dp[n]);

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325067053&siteId=291194637