PAT(A)1113 Integer Set Partition (25分)

在這裏插入圖片描述

Sample Input

10
23 8 10 99 46 2333 46 1 666 555

Sample Output

0 3611

思路:
1、min( |n1 - n2| ) —> 平分整個數組
2、max( |s1 - s2| ) —> 排序一下,大的一半一組,小的一組,最後相減
代碼

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll a[100010];

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i)
        cin >> a[i];

    ll c1, c2 = 0;

    sort(a, a + n);

    if (n & 1) c1 = 1;
    else c1 = 0;

    for (int i = 0; i <= n / 2 - 1; ++i)
        c2 -= a[i];
    for (int i = n / 2; i < n; ++i)
        c2 += a[i];

    cout << c1 << " " << c2;

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章