oj1010: Last counterattack of Warcraft

Topics requirements
legend Terran and Orcs confrontation for a long time, both sides have been hit, Orcs while humans do not have the ability to launch a surprise attack large-scale attack, the human family would like a thorough defeat. Human beings in order to survive, young and old injuries, all war, divided the enemy.
Due to the different physical, we express a person's blood to fighting, and now all you give blood, you put human beings into two parts closest to the fighting. Note to combat closest, otherwise, the Terrans defeat because of you Yo!

Input
first line an integer n (1 <= n <= 36), indicates the total number of people in the family. The following n lines each an integer representing a person's blood mi The (i.e. combat), where. 1 <= mi The <= 400.
The Output
only one line, comprising two numbers, i.e., the total blood volume of each part of the family of soldiers who, a smaller value on the front, two numbers separated by a space.
The Input the Sample
Raw
3
20
32
35
the Sample the Output
Raw
35 52
First find the ideal median mid, re-use knapsack problem dynamic programming ideas, so that bearing the weight of the backpack to the value of mid, constantly looking for the maximum value max side.

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int max(int a, int b)
{
	if (a >= b)
		return a;
	else
		return b;
}
int main()
{
	int dp[1000], num, a[1000], sum = 0, mid;
	memset(a, 0, sizeof(a));
	memset(dp, 0, sizeof(dp));
	cin >> num;
	for (int i = 1; i <= num; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	mid = sum / 2;
	for (int i = 1; i <= sum; i++)
		for (int j = mid; j >= 0; j--)
			if (j >= a[i])
				dp[j] = max(dp[j], dp[j - a[i]] + a[i]);
	cout << dp[mid] << " " << sum - dp[mid] << endl;
	return 0;	
}
Published 38 original articles · won praise 27 · views 3187

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/104865258