Nyoj 456 stamps Half of you

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.