dp - P2663 more the more the team

Title Description

Class comprehensive ability to organize a competition, the class (N (less than 100), N is an even number) divided into two teams compete against each other. The teacher found more and more to a whole class the more the more comprehensive ability test results, asking him to elect half (he was also likely to be selected) from the class, the students and asked them comprehensive ability test scores of and without more than half of the class as much as possible to achieve the highest score. Such divided between the two teams is the most average. The more the smile on his face piled found you, you help him write a program it.

Input Format

The first line: the number of students N; N lines that begin with the second lines each student a comprehensive ability test scores.

Output Format

The output of a number: the highest overall capacity of N / 2 test scores of students and without more than half of the class as much as possible out of reach.

 

First, find out all the comprehensive ability and / 2, because they require the comprehensive capabilities of the selected students' test results only and in no more than half the total score of the class

So every time the for loop from ave (half of the total score of the class) to start each - 1, that is, after each election to ensure students do not exceed a ave

Because students require the selected maximum and comprehensive results, two of the most average, so dp [j] = max (dp [j], dp [ja [i]] + a [i]) takes a large.

1  for(int i=1;i<=n;i++) {
2         for(int j=ave;j>=a[i];j--) {
3             dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
4             tmp=max(tmp,dp[j]);
5         }
6     }

 

Where DP [j] j represents the score line, the upper limit of the sum of selected students

Each time is running out in greater than or equal current students score before choosing a large value

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 int n,a[110];
 6 int dp[11000];
 7 int main()
 8 {
 9     scanf ("%d",&n);
10     int ans=0;
11     for (int i = 1;i <= n;i++)
12     {
13         scanf ("%d",&a[i]);
14         ans+=a[i];
15     }
16     int ave=ans/2;
17     int tmp=0;
18      for(int i=1;i<=n;i++) {
19         for(int j=ave;j>=a[i];j--) {
20             dp[j]=0;
21           
22         }
23     }
24    for(int i=1;i<=n;i++) {
25         for(int j=ave;j>=a[i];j--) {
26             dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
27             tmp=max(tmp,dp[j]);
28         }
29     }
30     cout<<tmp<<endl;
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/very-beginning/p/11952833.html