1113. Integer Set Partition (25)
来源:互联网 发布:c语言计算最大公约数 编辑:程序博客网 时间:2024/05/19 04:07
1113. Integer Set Partition (25)
Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2numbers, 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 |n1 - n2| is minimized first, and then |S1 - S2| is maximized.
Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2 <= N <= 105), 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: |n1 - n2| and |S1 - S2|, separated by exactly one space.
Sample Input 1:1023 8 10 99 46 2333 46 1 666 555Sample Output 1:
0 3611Sample Input 2:
13110 79 218 69 3721 100 29 135 2 6 13 5188 85Sample Output 2:
1 9359
#include<stdio.h>#include<algorithm>using namespace std;int main(){int n,i,sum=0;scanf("%d",&n);int a[n];for(i=0;i<n;i++){scanf("%d",&a[i]);}sort(a,a+n);if(n%2==0){for(i=0;i<n/2;i++){sum=sum-a[i]+a[n-i-1];}printf("%d %d",0,sum);}else{for(i=0;i<n/2;i++){sum=sum-a[i]+a[n-i-1];}sum=sum+a[n/2];printf("%d %d",1,sum);}}
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25)
- 1113. Integer Set Partition
- 1113. Integer Set Partition
- PAT_A 1113. Integer Set Partition (25)
- pat 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25) PAT 甲级
- PAT 甲级 1113. Integer Set Partition (25)
- PAT--1113. Integer Set Partition
- javaweb学习总结(六)——Servlet开发(二)
- ERROR 1010 (HY000): Error dropping database (can't rmdir './myapp', errno: 39)
- 1. 数据预处理-数据归一化和数据规范化
- 测试小故事41:乒乓
- iOS @property 重构set、get方法
- 1113. Integer Set Partition (25)
- Java程序设计基础-第四章
- USACO-Section2.3 Cow Pedigrees【动态规划】
- 机器学习:K-近邻算法原理与Python代码实现
- 数据结构(Java)---队列
- javaweb学习总结(七)——HttpServletResponse对象(一)
- TP5快速使用腾讯短信接口封装的方法
- 属性动画
- JS 获取浏览器窗口大小