来自官方题解:
AC代码:
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 using namespace std; 15 #define ll long long 16 #define eps 1e-10 17 #define MOD 1000000007 18 #define N 26 19 #define M 2006 20 #define inf 1e12 21 int n,a[N]; 22 int dp[M]; 23 int main() 24 { 25 int t; 26 scanf("%d",&t); 27 while(t--){ 28 memset(dp,0,sizeof(dp)); 29 scanf("%d",&n); 30 int sum=0; 31 for(int i=0;i<n;i++){ 32 scanf("%d",&a[i]); 33 sum+=a[i]; 34 } 35 dp[0]=1; 36 for(int i=0;i<n;i++){ 37 for(int j=sum;j>=a[i];j--){ 38 dp[j]|=dp[j-a[i]]; 39 } 40 } 41 42 for(int i=0;i<n;i++){ 43 for(int j=0;j+a[i]<=sum;j++){ 44 dp[j]|=dp[j+a[i]]; 45 } 46 } 47 48 int m; 49 scanf("%d",&m); 50 while(m--){ 51 int weight; 52 scanf("%d",&weight); 53 if(dp[weight]){ 54 printf("YES\n"); 55 }else{ 56 printf("NO\n"); 57 } 58 } 59 60 } 61 return 0; 62 }
hdu 5616 Jam's balance(dp 正反01背包)
时间: 2024-10-16 18:50:24