Stamp your half of the time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
-
Xiao Ke recently collected some stamps, he wants to give some of them to his good friend Xiaoming. Each stamp has a score, they want to divide these stamps into two parts, and make the score and the difference between the two stamps and the smallest (that is, the score and the difference between Xiao Ke and Xiao Ming), now the score of each stamp has been known, they have been divided well, you know the final stamp score and how much difference?
-
input
-
The first line has only one integer m (m<=1000), which indicates the number of test data groups. The
Next has an integer n (n<=1000) that represents the number of stamps. The
then has n integer Vi (vi<=100), which represents the score of the stamp I. The
-
output
-
output difference is one row for each set of outputs.
-
Sample Input
-
252 6 5 8 932 1 5
-
sample output
-
Print
This topic and Nanyang's ZB Birthday ideas, the code is exactly the same as the 01 knapsack problem
#include <stdio.h> #include <string.h># Define Max 101000#define Max (A, B) (a>b?a:b) int main () {int n,m,j,i,s,sum;int price[1100];int dp[max];scanf ("%d", &n), while (n--) {scanf ("%d", &m), memset (Dp,0,sizeof (DP)), Sum=0;for (i=1;i<=m;i++) {scanf ("%d", &price [i]); Sum+=price[i];} S=sum/2;for (i=1;i<=m;i++) {for (j=s;j>=price[i];j--) {Dp[j]=max (dp[j],dp[j-price[i]]+price[i]);}} printf ("%d\n", Sum-2*dp[s]);//Here is the (half-dp[s]) because half of the total may have decimal places in the shaping data will be abandoned, resulting in error}//So the total amount to reduce
To twice times the value of the capacity of s when the result is the same as return 0;}
Nyoj 456 stamps for half of you