头像

lew2018

青岛二中


访客:8361

在线 


活动打卡代码 AcWing 1013. 机器分配

lew2018
20小时前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 11, M = 16;

int n, m;
int w[N][M];
int f[N][M];
int way[N];

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> w[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= m; j++)
            for (int k = 0; k <= j; k++)
                f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]);
    cout << f[n][m] << endl;
    int j = m;
    for (int i = n; i >= 1; i--)
        for (int k = 1; k <= j; k++)
            if (f[i][j] == f[i - 1][j - k] + w[i][k]) {
                way[i] = k;
                j -= k;
                break;
            }
    for (int i = 1; i <= n; i++)
        cout << i << " " << way[i] << endl;
    return 0;
}



lew2018
1天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, m;
int v[N], w[N];
int f[N][N];  //f[i][j]定义为从第i个元素到最后一个元素总容量为j的所有选法

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];
    for (int i = n; i >= 1; i--)
        for (int j = 0; j <= m; j++) {
            f[i][j] = f[i + 1][j];
            if (j >= v[i]) f[i][j] = max(f[i][j], f[i + 1][j - v[i]] + w[i]);
        }
    int j = m;
    for (int i = 1; i <= n; i++)
        if (j >= v[i] && f[i][j] == f[i + 1][j - v[i]] + w[i]) {
            cout << i << " ";
            j -= v[i];
        }
    return 0;
}



lew2018
1天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, m;
int v[N], w[N];
int f[N][N];

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> v[i] >> w[i];
    for (int i = n; i >= 1; i--)
        for (int j = 0; j <= m; j++) {
            f[i][j] = f[i + 1][j];
            if (j >= v[i]) f[i][j] = max(f[i][j], f[i + 1][j - v[i]] + w[i]);
        }
    int j = m;
    for (int i = 1; i <= n; i++)
        if (j >= v[i] && f[i][j] == f[i + 1][j - v[i]] + w[i]) {
            cout << i << " ";
            j -= v[i];
        }
    return 0;
}


活动打卡代码 AcWing 1019. 庆功会

lew2018
1天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 6010;

int n, m;
int f[N];

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        int v, w, s;
        cin >> v >> w >> s;
        for (int j = m; j >= 0; j--)
            for (int k = 0; k <= s && k * v <= j; k++)
                f[j] = max(f[j], f[j - k * v] + k * w);
    }
    cout << f[m] << endl;
    return 0;
}


活动打卡代码 AcWing 278. 数字组合

lew2018
1天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 10010;

int n, m;
int f[N];

int main() {
    cin >> n >> m;
    f[0] = 1;
    for (int i = 0; i < n; i++) {
        int v;
        cin >> v;
        for (int j = m; j >= v; j--)
            f[j] += f[j - v];
    }
    cout << f[m] << endl;
    return 0;
}


活动打卡代码 AcWing 278. 数字组合

lew2018
1天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 10010;

int n, m;
int f[N];

int main() {
    cin >> n >> m;
    f[0] = 1;
    for (int i = 0; i < n; i++) {
        int v;
        cin >> v;
        for (int j = m; j >= v; j--)
            f[j] += f[j - v];
    }
    cout << f[m] << endl;
    return 0;
}


活动打卡代码 AcWing 1020. 潜水员

lew2018
1天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 22, M = 80;

int n, m, K;
int f[N][M];

int main() {
    cin >> n >> m >> K;
    memset(f, 0x3f, sizeof(f));
    f[0][0] = 0;
    while (K--) {
        int v1, v2, w;
        cin >> v1 >> v2 >> w;
        for (int i = n; i >= 0; i--)
            for (int j = m; j >= 0; j--)
                f[i][j] = min(f[i][j], f[max(0, i - v1)][max(0, j - v2)] + w);
    }
    cout << f[n][m] << endl;
    return 0;
}



lew2018
1天前

不优化

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1010, P = 110;

int n, V, M;
int v[N], m[N], w[N];
int f[N][P][P];

int main() {
    cin >> n >> V >> M;
    for (int i = 1; i <= n; i++)
        scanf("%d%d%d", &v[i], &m[i], &w[i]);
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= V; j++)
            for (int k = 0; k <= M; k++) {
                if (j >= v[i] && k >= m[i])
                    f[i][j][k] = max(f[i - 1][j][k], f[i - 1][j - v[i]][k - m[i]] + w[i]);
                else f[i][j][k] = f[i - 1][j][k];
            }
    cout << f[n][V][M] << endl;
    return 0;
}

优化

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 110;

int n, V, M;
int v, m, w;
int f[N][N];

int main() {
    cin >> n >> V >> M;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d%d", &v, &m, &w);
        for (int j = V; j >= v; j--)
            for (int k = M; k >= m; k--)
                f[j][k] = max(f[j][k], f[j - v][k - m] + w);
    }
    cout << f[V][M] << endl;
    return 0;
}



lew2018
2天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 20010;

int n, m;
int f[N], g[N], q[N];

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        int v, w, s;
        cin >> v >> w >> s;
        memcpy(g, f, sizeof(f));
        for (int j = 0; j < v; j++) {
            int hh = 0, tt = -1;
            for (int k = j; k <= m; k += v) {
                if (hh <= tt && q[hh] < k - s * v) hh++;
                while (hh <= tt && g[q[tt]] - (q[tt] - j) / v * w <= g[k] - (k - j) / v * w) tt--;
                q[++tt] = k;
                f[k] = g[q[hh]] + (k - q[hh]) / v * w;
            }
        }
    }
    cout << f[m] << endl;
    return 0;
}


活动打卡代码 AcWing 378. 骑士放置

lew2018
2天前
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define x first
#define y second

typedef pair<int, int> PII;

const int N = 110;

int n, m, k;
PII match[N][N];
bool g[N][N], st[N][N];
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2},
    dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

bool find(int x, int y) {
    for (int i = 0; i < 8; i++) {
        int a = x + dx[i], b = y + dy[i];
        if (a < 1 || a > n || b < 1 || b > m) continue;
        if (g[a][b]) continue;
        if (st[a][b]) continue;
        st[a][b] = true;
        PII t = match[a][b];
        if (t.x == 0 || find(t.x, t.y)) {
            match[a][b] = make_pair(x, y);
            return true;
        }
    }
    return false;
}

int main() {
    cin >> n >> m >> k;
    for (int i = 0; i < k; i++) {
        int x, y;
        cin >> x >> y;
        g[x][y] = true;
    }
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            if (g[i][j] || (i + j) % 2) continue;
            memset(st, 0, sizeof(st));
            if (find(i, j)) res++;
        }
    cout << n * m - k - res << endl;
    return 0;
}