PAT_A 1113. Integer Set Partition (25)
来源:互联网 发布:宿迁装修公司知乎 编辑:程序博客网 时间:2024/05/17 01:02
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 n2 numbers, 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:
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
分析
先排序,在计算差值的问题code
#include<iostream>#include<algorithm>#include<vector>using namespace std;int main(){ int n; cin>>n; vector<int>d; int m; for(int i=0;i<n;i++) { cin>>m; d.push_back(m); } sort(d.begin(),d.begin()+n); if(n%2) cout<<1; else cout<<0; cout<<" "; int sum=0; int half=n/2; for(int i=n-1;i>=0;i--) { if(i>=half) sum+=d.at(i); else sum-=d.at(i); } cout<<sum<<endl; return 0;}
- PAT_A 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 (25)
- 1113. Integer Set Partition
- 1113. Integer Set Partition
- pat 1113. Integer Set Partition (25)
- 1113. Integer Set Partition (25) PAT 甲级
- PAT 甲级 1113. Integer Set Partition (25)
- PAT--1113. Integer Set Partition
- MATLAB中文显示乱码问题
- iOS音频播放编程简介
- 【35.53%】【POJ 2912】Rochambeau
- hdu---2016做题笔记
- 第六周【项目二-建立链队算法库】
- PAT_A 1113. Integer Set Partition (25)
- c++知识复习8.0
- storm对网站有关数据的统计以及多线程问题探讨
- 设置Eclipse中Maven编译的JDK版本
- Spark性能优化指南——基础篇
- 链表节点以及数组元素的删除
- 51nod 1002 数塔取数问题
- 微信精选阅读器
- JUnit4