PAT_A1113#Integer Set Partition

时间:2019-05-29 23:35:12   收藏:0   阅读:129

Source:

PAT A1113 Integer Set Partition (25 分)

Description:

Given a set of N (>) 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 ∣ 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 2?31??.

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

Keys:

Attention:

Code:

 1 /*
 2 Data: 2019-05-29 21:38:41
 3 Problem: PAT_A1113#Integer Set Partition
 4 AC: 08:30
 5 
 6 题目大意:
 7 给定N个整数的集合,把他们分为两个部分,要求两部分和的差最大且元素个数差最小
 8 */
 9 
10 #include<cstdio>
11 #include<algorithm>
12 using namespace std;
13 const int M=1e5+10;
14 int s[M];
15 
16 int main()
17 {
18 #ifdef    ONLINE_JUDGE
19 #else
20     freopen("Test.txt", "r", stdin);
21 #endif
22 
23     int n,sum=0;
24     scanf("%d", &n);
25     for(int i=0; i<n; i++)
26         scanf("%d", &s[i]);
27     sort(s,s+n);
28     for(int i=0; i<n/2; i++)
29         sum+= (s[n-1-i]-s[i]);
30     if(n%2==0)
31         printf("0 ");
32     else{
33         printf("1 ");
34         sum  += s[n/2];
35     }
36     printf("%d\n", sum);
37 
38     return 0;
39 }

 

原文:https://www.cnblogs.com/blue-lin/p/10946599.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!