首页 > 其他 > 详细

PAT_A1113#Integer Set Partition

时间:2019-05-29 23:35:12      阅读:127      评论:0      收藏:0      [点我收藏+]

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:

  • 408的一道真题,最快用O(N)规模完成,思路就是基于快排算法寻找中轴;但这里没卡时间就比较简单了-,-

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 }

 

PAT_A1113#Integer Set Partition

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!