【PAT】【Advanced Level】1113. Integer Set Partition (25)

本文介绍了一个算法问题,即如何将一组正整数合理地划分为两个子集,使得这两个子集的元素数量之差最小,并在此基础上使元素总和之差最大化。提供了具体的输入输出规范与示例,以及一种基于排序和两端向中间累加的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1113. Integer Set Partition (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
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
原题链接:

https://www.patest.cn/contests/pat-a-practise/1113

思路:

排序,从两边向中间累加前后之差,最后针对中间的元素,使累加的绝对值尽可能的大

对于个数差,奇数为1,偶数为0

CODE:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define N 100010
using namespace std;
int num[N];
int main()
{
	int n;
	scanf("%d",&n);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&num[i]);
	}
	sort(num,num+n);
	int c=0;
	for (int i=0,j=n-1;i<=j;i++,j--)
	{
		c+=(num[j]-num[i]);
	}
	if (n%2==1)
	{
		if (num[n/2]>=0)
		{
			if (c>=0)
			{
				c+=num[n/2];
			}
			else
			{
				c-=num[n/2];
			}
		}
		else
		{
			if (c<0)
			{
				c+=num[n/2];
			}
			else
			{
				c-=num[n/2];
			}
		}
	}	
	if (n%2==1)
	{
		cout<<1<<" ";
	}
	else
	{
		cout<<0<<" ";
	}
	if (c<0)
	{
		cout<<c*-1;
	}
	else
	{
		cout<<c;
	}
	return 0;
}




<think>我们正在讨论Hive动态分区的配置。根据引用内容,用户想配置Hive动态分区,并询问了`set hive.crud.dynamic.partition=true`这个配置。但是,根据引用[1]和引用[2],正确的配置参数是`hive.exec.dynamic.partition`,而不是`hive.crud.dynamic.partition`。因此,我们需要纠正这个参数。 动态分区配置的关键点: 1. 开启动态分区功能:`set hive.exec.dynamic.partition=true;` 2. 设置动态分区模式为非严格模式(允许所有分区字段都是动态的):`set hive.exec.dynamic.partition.mode=nonstrict;` 3. 调整动态分区的相关参数,如最大动态分区数等,以避免超出限制的异常(如引用[2]中提到的)。 另外,引用[1]还提到,动态分区是根据查询语句中最后一个(或几个)字段作为分区字段的,并且分区字段不能是中文,否则可能会有奇怪的bug。 因此,针对用户的问题,我们需要指出正确的配置参数,并给出完整的配置示例。 同时,根据引用[3]我们可以知道如何查看分区,这也是一种验证动态分区是否成功创建的方法。 回答结构: 1. 指出用户提到的配置参数有误,并给出正确的参数。 2. 列出配置动态分区所需的步骤和参数。 3. 给出一个配置示例。 4. 提供验证动态分区是否生效的方法(如查看分区)。 5. 注意事项(如分区字段顺序、中文问题、参数调优等)。 6. 生成相关问题。 注意:用户可能是在Hive或Inceptor中使用,但动态分区的配置在Hive和Inceptor中基本一致。不过,如果用户之前的问题是关于Inceptor事务的,这里我们仍然按照Hive的标准来回答,因为动态分区是Hive和Inceptor共有的功能,且配置相同。</think>### Hive 动态分区配置详解 您提到的 `hive.crud.dynamic.partition` 参数名称有误,正确配置应为 **`hive.exec.dynamic.partition`**。以下是完整配置指南: #### 1. 核心配置参数 ```sql -- 开启动态分区功能(必须) SET hive.exec.dynamic.partition = true; -- 设置动态分区模式(建议非严格模式) SET hive.exec.dynamic.partition.mode = nonstrict; -- strict: 要求至少一个静态分区字段 -- nonstrict: 允许全动态分区 -- 调整分区数量限制(避免报错) SET hive.exec.max.dynamic.partitions = 2048; -- 单语句最大分区数 SET hive.exec.max.dynamic.partitions.pernode = 256; -- 单节点最大分区数 ``` #### 2. 动态分区使用示例 ```sql -- 创建分区表(分区字段必须放在最后) CREATE TABLE user_activity ( user_id BIGINT, action STRING, duration INT ) PARTITIONED BY (dt STRING, country STRING); -- 分区字段 -- 动态插入数据(自动创建分区) INSERT INTO TABLE user_activity PARTITION (dt, country) SELECT user_id, action, duration, event_date AS dt, -- 动态分区字段1 user_country AS country -- 动态分区字段2 FROM raw_events; ``` **关键点**: 1. 查询语句中分区字段必须**放在最后**,且顺序与表定义一致 2. 字段名 `AS` 后的别名必须与分区字段名匹配 3. 系统会根据实际值自动创建分区目录(如 `dt=2023-08-01/country=US`) #### 3. 验证配置 ```sql -- 查看生效配置 SET hive.exec.dynamic.partition; -- 输出: hive.exec.dynamic.partition=true -- 检查分区创建 SHOW PARTITIONS user_activity; -- 输出示例: -- dt=2023-08-01/country=US -- dt=2023-08-01/country=JP ``` #### 4. 注意事项 1. **分区字段顺序** 查询结果列顺序必须与分区字段声明顺序一致,且分区字段在最后 2. **中文分区问题** 避免使用中文分区值(如 `country='中国'`),可能引发编码错误[^1] 3. **小文件问题** 动态分区易产生小文件,建议配合合并: ```sql SET hive.merge.mapfiles = true; SET hive.merge.size.per.task = 256000000; -- 256MB ``` 4. **性能优化** ```sql -- 减少Reducer负载 SET hive.optimize.sort.dynamic.partition = true; -- 控制输出文件数 SET hive.exec.reducers.max = 50; ``` --- ### 常见错误处理 **错误:** `Dynamic partition strict mode requires at least one static partition column` **解决:** 设置 `hive.exec.dynamic.partition.mode=nonstrict` **错误:** `Number of dynamic partitions exceeded` **解决:** 调高 `hive.exec.max.dynamic.partitions` 值[^2] **错误:** 分区目录创建但数据未写入 **解决:** 检查查询字段顺序,确保分区字段在SELECT最后位置 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值