标签:des style color os strong io for art
Description
Input
Output
Sample Input
2 4 -2 3 3 4 5 8
Sample Output
2
dp问题,dp[k][i] 中i = 第k个砝码挂上后左侧的值-右侧的值,因为可能出现右侧的大,i不能为负值,所以计算的i同意+8000 ;
所以dp[m][8000]是挂上m个砝码后仍然平衡的种类
首先dp[0][8000] = 1 ;其余的都是-1为不可达点
逐个计算,最后得到dp[m][8000]
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int a[22] , b[22] ; int p[22][16001] ; int main() { int i , j , k , n , m , l = -1 , r = -1 ; scanf("%d %d", &n, &m); for(i = 1 ; i <= n ; i++) { scanf("%d", &a[i]); if(l == -1 && a[i] < 0) l = i ; if(r == -1 && a[i] > 0) r = i ; } for(i = 1 ; i <= m ; i++) scanf("%d", &b[i]); memset(p,0,sizeof(p)); p[0][8000] = 1 ; for(i = 1 ; i <= m ; i++) { for(j = 16000 ; j >= 0 ; j--) { for(k = l ; k < r ; k++) { int s = j - a[k]*b[i] ; if( s <= 16000 ) p[i][j] += p[i-1][ s ]; } } for(j = 0 ; j <= 16000 ; j++) { for(k = r ; k <= n ; k++) { int s = j - a[k]*b[i] ; if( s >= 0 ) p[i][j] += p[i-1][ s ] ; } } } printf("%d\n", p[m][8000]); }
测试赛B - Balance(天平的dp问题),布布扣,bubuko.com
标签:des style color os strong io for art
原文地址:http://blog.csdn.net/winddreams/article/details/38300289