zoukankan      html  css  js  c++  java
  • $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
    ",ans) ;
    			return ;
    		}
    	}
    }
    signed main() {
    //  freopen("testdata.txt","w",stdout) ;
        return Ot() , 0 ;
    }
    
    
  • 相关阅读:
    gitlab搭建
    java数组
    安裝nextcloud
    Spring的定时任务@Scheduled(cron = "0 0 1 * * *")
    java内存结构(下)
    java内存结构(上)
    多线程的三个特性
    @RequestBody用法
    SwaggerAPI注解详解(转载)
    在jpanel中添加jbutton并自由设置按钮大小和位置
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10658374.html
Copyright © 2011-2022 走看看