$P1282 多米诺骨牌$

problem
此题是一道01背包。

关于01 背包 我不想讲了 - > \(MY \ BLOG\)

\[这道题是一道基础的01背包问题\]

\[设f[i][j]=k表示前i张牌构成分值j的最小次数k\]
\[设 dis = a[i]-b[i]\]
//不反转
\[f[i][j+dis+N]=min(f[i][j+dis+N],f[i-1][j+N]);\]
//反转
\[f[i][j+dis+N]=min(f[i][j+dis+N],f[i-1][j+N]+1);\]

所以得出

\[f[i][j+N] = min(f[i-1][j-dis+N] , f[i-1][j+dis+N] + 1) ;\]

#include <bits/stdc++.h>
#define rep(i,j,n) for(register int i=j;i<=n;i++)
#define Rep(i,j,n) for(register int i=j;i>=n;i--)
#define low(x) x&(-x)
using namespace std ;
typedef long long LL ;
const int inf = INT_MAX >> 1 ;
inline LL In() { LL res(0) , f(1) ; register char c ;
#define gc c = getchar()
    while(isspace(gc)) ; c == '-' ? f = - 1 , gc : 0 ;
    while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(gc)) ;
    return res * f ;
#undef gc
}

int n ;
const int Size = 1000 + 5 ;
const int N = 5000 ;
int a[Size] , b[Size] ;
int f[Size][Size * 11] ;
int ans ;
inline void Ot() {
    n = In() ;
    rep(i,1,n) a[i] = In() , b[i] = In() ;
    memset(f,0x7f,sizeof(f)) ;
    f[0][N] = 0 ;
    rep(i,1,n) rep(j,-N,N) {
        int dis = a[i] - b[i] ;
        f[i][j+N] = min(f[i-1][j-dis+N] , f[i-1][j+dis+N] + 1) ;
    }
    rep(i,0,N) {
        int ans = min(f[n][N+i] , f[n][N-i]) ;
        if(ans <= 1000) {
            printf("%d\n",ans) ;
            return ;
        }
    }
}
signed main() {
//  freopen("testdata.txt","w",stdout) ;
    return Ot() , 0 ;
}

猜你喜欢

转载自www.cnblogs.com/qf-breeze/p/10658374.html