Integer Set Partition

本文介绍了一种将正整数集合划分为两个子集的方法,目标是最小化两子集元素数量之差,并在此基础上最大化两子集元素总和之差的绝对值。通过排序并按顺序分配元素到两个子集的方式实现此目标。

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

一道水题

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[100000];
int main() {
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	int sum1=0;
	for(int i=0;i<n/2;i++)
		sum1+=a[i];
	int sum2=0;
	for(int i=n/2;i<n;i++)
		sum2+=a[i];
	printf("%d ",n%2);
	printf("%d",sum2-sum1);
	return 0;
}


public void sendDelay(@Nonnull String topic, String key, Integer partition, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { try { int level = strategy.getTargetDelayLevel(delayMs); String delayTopic = String.format("delay_topic_level_%d", level); event.setTimeStamp(System.currentTimeMillis()); long expirationTime = event.getTimeStamp() + FIXED_DELAY_LEVELS.get(level - 1); DelayEvent delayEvent = new DelayEvent(topic,key,partition,event,level,expirationTime,null); log.info("延迟等级level={}, 到期时间expirationTime={}", level, expirationTime); sendGenerically(delayTopic,delayEvent); } catch (Exception e) { log.error("延迟消息发送失败", e); } } public void sendDelay(@Nonnull String topic, Integer partition, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { sendDelay(topic,null,partition,event,delayMs,strategy); } public void sendDelay(@Nonnull String topic, String key, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { sendDelay(topic,key,null,event,delayMs,strategy); } public void sendDelay(@Nonnull String topic, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { sendDelay(topic,null,null,event,delayMs,strategy); } public<T> void sendDelayGenerically(@Nonnull String topic, String key, Integer partition, @Nonnull T event, long delayMs, @Nonnull DelayQueueStrategy strategy) { try { if (event instanceof Event) { Event thisEvent = (Event) event; int level = strategy.getTargetDelayLevel(delayMs); String delayTopic = String.format("delay_topic_level_%d", level); thisEvent.setTimeStamp(System.currentTimeMillis()); long expirationTime = thisEvent.getTimeStamp() + FIXED_DELAY_LEVELS.get(level - 1); DelayEvent delayEvent = new DelayEvent(topic, key, partition, event, level, expirationTime, SerializeEnum.JSON); log.info("延迟等级level={}, 到期时间expirationTime={}", level, expirationTime); sendGenerically(delayTopic, delayEvent); } else if (event instanceof EventV2) { EventV2 thisEvent = (EventV2) event; int level = strategy.getTargetDelayLevel(delayMs); String delayTopic = String.format("delay_topic_level_%d", level); thisEvent.setTimeStamp(System.currentTimeMillis()); long expirationTime = thisEvent.getTimeStamp() + FIXED_DELAY_LEVELS.get(level - 1); DelayEvent delayEvent = new DelayEvent(topic, key, partition, event, level, expirationTime, SerializeEnum.KRYO); log.info("延迟等级level={}, 到期时间expirationTime={}", level, expirationTime); sendGenerically(delayTopic, delayEvent); }else{ int level = strategy.getTargetDelayLevel(delayMs); String delayTopic = String.format("delay_topic_level_%d", level); long expirationTime = System.currentTimeMillis() + FIXED_DELAY_LEVELS.get(level - 1); DelayEvent delayEvent = new DelayEvent(topic, key, partition, event, level, expirationTime, SerializeEnum.JSON); log.info("延迟等级level={}, 到期时间expirationTime={}", level, expirationTime); sendGenerically(delayTopic, delayEvent); } } catch (Exception e) { log.error("延迟消息发送失败", e); } } public void sendDelayGenerically(@Nonnull String topic, Integer partition, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { sendDelayGenerically(topic,null,partition,event,delayMs,strategy); } public void sendDelayGenerically(@Nonnull String topic, String key, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { sendDelayGenerically(topic,key,null,event,delayMs,strategy); } public void sendDelayGenerically(@Nonnull String topic, @Nonnull Event event, long delayMs, @Nonnull DelayQueueStrategy strategy) { sendDelayGenerically(topic,null,null,event,delayMs,strategy); }请为这些方法写javadoc注释
最新发布
10-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值