1113 Integer Set Partition
Given a set of N (>) 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 ∣ 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 231.
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
题意:
将一个给定的数组分成两部分,使两部分元素的个数之差最小,总和之差最大。
思路:
将数组排序,从中间开始划分,前一半的和最小,后一半的和最大,满足题意。
Code:
#include <bits/stdc++.h> using namespace std; int sum(vector<int> v, int s, int e) {
int sum = 0;
for (int i = s; i <= e; ++i) {
sum += v[i];
}
return sum;
} int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
sort(v.begin(), v.end());
int mid = n / 2;
int preSum = sum(v, 0, mid - 1);
int postSum = sum(v, mid, n - 1);
if (v.size() % 2 == 0) {
cout << 0 << " " << postSum - preSum << endl;
} else {
cout << 1 << " " << postSum - preSum << endl;
}
return 0;
}
1113 Integer Set Partition的更多相关文章
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- PAT 甲级 1113 Integer Set Partition
https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...
- 1113. Integer Set Partition (25)
Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...
- PAT 1113 Integer Set Partition
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...
- PAT (Advanced Level) 1113. Integer Set Partition (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 【PAT甲级】1113 Integer Set Partition (25分)
题意: 输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大. AAAAAccepted cod ...
- PAT1113: Integer Set Partition
1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT_A1113#Integer Set Partition
Source: PAT A1113 Integer Set Partition (25 分) Description: Given a set of N (>) positive integer ...
- A1113. Integer Set Partition
Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...
随机推荐
- Linux下搭建ZooKeeper集群
摘要 Zookeeper是针对大型分布式系统的高可靠的协调系统.它基于对Paxos算法的实现,使该框架保证了分布式环境中数据的强一致性,也正是基于这样的特性,使得ZooKeeper解决很多分布式问题. ...
- 心脏滴血(CVE-2014-0160)检测与防御
用Nmap检测 nmap -sV --script=ssl-heartbleed [your ip] -p 443 有心脏滴血漏洞的报告: ➜ ~ nmap -sV --script=ssl-hear ...
- 【Saas-export项目】--项目整合(spring整合MVC)
转: [Saas-export项目]--项目整合(spring整合MVC) 文章目录 Spring整合SpringMVC(export_web_manager子工程) (1)log4j.propert ...
- iOS之CoreBluetooth
思路 手机与设备间的通讯方式CoreBluetooth是比较常见且通用的.在iOS开发中需明晰以下几点 蓝牙4.0最多可联机7个设备,iPhone6以上都是蓝牙4.0 两台iPhone并不直接通过蓝牙 ...
- 最简单的,在win,linux中,用powershell,自动获取Let's Encrypt证书方法
powershell传教士原创 2020-04-12 Let's Encrypt证书有效期3个月,支持泛域名[*.你的网站.net].支持n天内(一般10天内就够用了),用脚本自动续期. 简介: 这个 ...
- HDOJ-1711(KMP算法)
Number Sequence HDOJ-1711 1.这里使用的算法是KMP算法,pi数组就是前缀数组. 2.代码中使用到了一个技巧就是用c数组看成是复合字符串,里面加一个特殊整数位-1000006 ...
- Hadoop的常用命令
注:hadoop的使用命令包含 hadoop fs 开头 or hdfs dfs开头 等多种形式来操作. 这里以hadoo fs的形式来介绍在这些命令 1.列出根目录下所有的目录或文件 hadoop ...
- 数据库事务 ACID属性、数据库并发问题和四种隔离级别
数据库事务 ACID属性.数据库并发问题和四种隔离级别 数据库事务 数据库事务是一组逻辑操作单元,使数据从一种状态变换到另一种状态 一组逻辑操作单元:一个或多个DML操作 事务处理原则 保证所有事务都 ...
- 2020年12月-第01阶段-前端基础-认识HTML
1. HTML 初识 HTML 指的是超文本标记语言 (Hyper Text Markup Language)是用来描述网页的一种语言. HTML 不是一种编程语言,而是一种标记语言 (markup ...
- C#深度复制和浅度复制
C#深度复制和浅度复制 复制一个值变量很简单,新建一个变量然后将原来的变量赋值过去就行,但是复制一个引用变量这种方法是不行的,如果不明白为什么可以先看看这篇解释 引用类型变量和值类型变量在赋值时的不同 ...