机器分配

https://www.acwing.com/problem/content/1015/

dp输出方案

分组背包

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 20;
int f[N][N];
int a[N][N];
int cnt[N];

int main() {
    //freopen("in.txt", "r", stdin);
    IO;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) cin >> a[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] + a[i][k]);
    
    cout << f[n][m] << '\n';
    
    int j = m, now = f[n][m];
    for (int i = n; i; --i)
        for (int k = 0; k <= j; ++k)
            if (f[i - 1][k] + a[i][j - k] == now) {
                cnt[i] = j - k;
                now = f[i - 1][k];
                j = k;
                break;
            }

    for (int i = 1; i <= n; ++i) cout <<  i << ' ' << cnt[i] << '\n';
    return 0;
}


posted @ 2021-04-13 20:31  phr2000  阅读(41)  评论(0编辑  收藏  举报