1113 Integer Set Partition (25分)

题目

Given a set of $N(\gt1)$ positive integers, you are supposed to partition them into two disjoint sets $A_1$ and $A_2$ of $n_1$ and $n_2$ numbers, respectively. Let $S_1$ and $S_2$ denote the sums of all the numbers in $A_1$ and $A_2$, respectively. You are supposed to make the partition so that $|n_1-n_2|$ is minimized first, and then $|S_1-S_2|$ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer $N(2\le N\le10^5)$, and then $N$ positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than $2^{31}$.

Output Specification:

For each case, print in a line two numbers: $|n_1-n_2|$ and $|S_1-S_2|$, separated by exactly one space.

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

0 3611

Sample Input 2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

1 9359

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
int n, sum1 = 0, sum2 = 0;
vector<int> v;
scanf("%d", &n);
v.resize(n);
for(int i=0; i<n; i++)
scanf("%d", &v[i]);
sort(v.begin(), v.end());
for(int i=0; i<n/2; i++)
sum1 += v[i];
for(int i=n/2; i<n; i++)
sum2 += v[i];
printf("%d %d\n", n%2, sum2-sum1);
return 0;
}


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客