PAT 甲级 1113 Integer Set Partition

https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016

 

Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A1​​ and A2​​ of n1​​ and n2​​ numbers, respectively. Let S1​​ and S2​​denote the sums of all the numbers in A1​​ and A2​​, respectively. You are supposed to make the partition so that ∣ is minimized first, and then ∣ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2), 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 231​​.

Output Specification:

For each case, print in a line two numbers: ∣ and ∣, separated by exactly one space.

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

Sample Output 1:

0 3611

Sample Input 2:

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

Sample Output 2:

1 9359

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int N;
int num[maxn];

int main() {
    scanf("%d", &N);
    int sum = 0;
    for(int i = 0; i < N; i ++) {
        scanf("%d", &num[i]);
        sum += num[i];
    }

    sort(num, num + N);
    int ans = 0;
    for(int i = 0; i < N / 2; i ++)
        ans += num[i];

    printf("%d %d\n", N % 2, sum - 2 * ans);
    return 0;
}

  好久不写手生生 今天是 Be 主决心减肥的第一天  

 

posted @ 2019-01-21 21:36  丧心病狂工科女  阅读(131)  评论(0编辑  收藏  举报