Luogu 1282 domino

Links: https://www.luogu.org/problem/P1282

Ideas:

Taking into account $ a, b $ minimal, backpack practices may be employed.

Due to the number of points a card from top to bottom and is constant, so if they know the current number of a row, you can know another line.

Provided $ f [i] [j] $ $ I $ represents the front card, the minimum number of points and the first row is inverted to the $ $ J.

Finally, enumerate and to the first row.

Note that all the states of the former backpack (except initialize) To set the maximum value.

Code:

#include <bits/stdc++.h>
const int INF = 1 << 30;
const int MAXN = 1050;
using namespace std;
int n, sum, res, Minx = INF, ans = INF, a[MAXN], b[MAXN], f[MAXN][MAXN * 6 + 5];
int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        sum += a[i] + b[i];
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= 6 * n; j++)
            f[i][j] = INF;
    }
    f[1][a[1]] = 0;
    f[1][b[1]] = 1;
    for(int i = 2; i <= n; i++) {
        for(int j = 0; j <= 6 * n; j++) {
            if(j - a[i] >= 0)
                f[i][j] = min(f[i][j], f[i - 1][j - a[i]]);
            if(j - b[i] >= 0)
                f[i][j] = min(f[i][j], f[i - 1][j - b[i]] + 1);
        }
    }
    for(int i = 0; i <= sum; i++) {
        if(f[n][i] == INF)
            continue;
        int res = abs(i - (sum - i));
        if(res < Minx) {
            Minx = Res; 
            years = f [n] [i]; 
        } 
        If (res == Minx) 
            years = min (years, f [n] [i]); 
    } 
    Cout << age << endl;
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/BeyondLimits/p/11609419.html