【PAT甲级】1113 Integer Set Partition (25分)

题意:

输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[100007];
 5 int main(){
 6     ios::sync_with_stdio(false);
 7     cin.tie(NULL);
 8     cout.tie(NULL);
 9     int n;
10     cin>>n;
11     for(int i=1;i<=n;++i)
12         cin>>a[i];
13     sort(a+1,a+1+n);
14     long long sum=0,sum2=0;
15     for(int i=1;i<=n/2;++i)
16         sum+=a[i];
17     for(int i=n/2+1;i<=n;++i)
18         sum2+=a[i];
19     cout<<n%2<<" "<<sum2-sum;
20     return 0;
21 }

 

posted @ 2020-02-11 14:50  sewage  阅读(128)  评论(0编辑  收藏  举报