hdu 1171 Big Event in HDU
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171
思路:用一个数组将数据存起来,可以将其转化为01背包
1 #include<stdlib.h> 2 #include<time.h> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <cstdlib> 7 #include <ctime> 8 #include <iostream> 9 #include <algorithm> 10 #include <vector> 11 #include <queue> 12 #include <map> 13 #include <set> 14 #include <string> 15 using namespace std; 16 17 #define N 100005 18 int val[N],dp[N]; 19 20 int main() 21 { 22 int Num; 23 int a,b,j,sum; 24 while(cin>>Num && Num>0) 25 { 26 memset(dp,0,sizeof(dp)); 27 memset(val,0,sizeof(val)); 28 29 j=1; 30 sum=0; 31 for(int i=1;i<=Num;i++) 32 { 33 cin>>a>>b; 34 while(b--) 35 { 36 val[j++]=a; 37 sum+=a; 38 } 39 } 40 41 for(int i=1;i<=j;i++) 42 { 43 for(int k=sum/2;k>=val[i];k--) 44 dp[k]=max(dp[k],dp[k-val[i]]+val[i]); 45 } 46 printf("%d %d\n",sum-dp[sum/2],dp[sum/2]); 47 48 } 49 return 0; 50 }