1113 Integer Set Partition (25分)


Given a set of N(>1)N(\gt1) positive integers, you are supposed to partition them into two disjoint sets A1A_1 and A2A_2 of n1n_1 and n2n_2 numbers, respectively. Let S1S_1 and S2S_2 denote the sums of all the numbers in A1A_1 and A2A_2, respectively. You are supposed to make the partition so that n1n2|n_1-n_2| is minimized first, and then S1S2|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(2N105)N(2\le N\le10^5), and then NN positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2312^{31}.

Output Specification:

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

Sample Input 1:

23 8 10 99 46 2333 46 1 666 555

Sample Output 1:

0 3611

Sample Input 2:

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

Sample Output 2:

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);
    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;
发布了150 篇原创文章 · 获赞 66 · 访问量 2万+


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