1113 Integer Set Partition (25 分) 排序

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A​1 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 ∣n​1−n​2
​∣ is minimized first, and then ∣S​1−S​2∣ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤10^5), 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 2 ^​31.

Output Specification:

For each case, print in a line two numbers: ∣n1​​ −n2∣ and ∣S​1−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

题意:输入n个数,然后将这些数划分为两个子集,然后求n1-n2的绝对值的最小值和s1-s2的绝对值的最大值
思路:排序一下即可

#include<iostream>
#include<cstdio>
#include<stack> 
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<cctype>

using namespace std;

int main() {
	int n;
	scanf("%d",&n);
	vector<int> v(n);
	for(int i = 0;i < n;i++) {
		scanf("%d",&v[i]);
	}
	sort(v.begin(),v.end());
	int sum = 0;
	for(int i = 0;i < n;i++) {
		sum = sum + v[i];
	}
	int sum1 = 0;
	for(int i = 0;i < n / 2;i++) {
		sum1 = sum1 + v[i];
	}
	printf("%d %d\n",n % 2,sum - 2 * sum1);
	return 0;
}
### 回答1: 好的,我可以回答这个问题。以下是我的回答: 使用Java可以使用Random类生成不含重复的整数,然后使用快速排序算法对其进行排序。对于有序、逆序和随机排序三种情况,快速排序的时间复杂度别为O(n^2)、O(nlogn)和O(nlogn)。因此,随机排序的情况下,快速排序的运行时间最短。 具体实现可以参考以下代码: ```java import java.util.*; public class QuickSort { public static void main(String[] args) { int[] arr = generateRandomArray(10000); long startTime = System.currentTimeMillis(); quickSort(arr, , arr.length - 1); long endTime = System.currentTimeMillis(); System.out.println("Random order: " + (endTime - startTime) + "ms"); arr = generateSortedArray(10000); startTime = System.currentTimeMillis(); quickSort(arr, , arr.length - 1); endTime = System.currentTimeMillis(); System.out.println("Sorted order: " + (endTime - startTime) + "ms"); arr = generateReverseArray(10000); startTime = System.currentTimeMillis(); quickSort(arr, , arr.length - 1); endTime = System.currentTimeMillis(); System.out.println("Reverse order: " + (endTime - startTime) + "ms"); } public static void quickSort(int[] arr, int left, int right) { if (left < right) { int pivotIndex = partition(arr, left, right); quickSort(arr, left, pivotIndex - 1); quickSort(arr, pivotIndex + 1, right); } } public static int partition(int[] arr, int left, int right) { int pivot = arr[right]; int i = left - 1; for (int j = left; j < right; j++) { if (arr[j] < pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, right); return i + 1; } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static int[] generateRandomArray(int n) { Set<Integer> set = new HashSet<>(); Random random = new Random(); while (set.size() < n) { set.add(random.nextInt(n)); } int[] arr = new int[n]; int i = ; for (int num : set) { arr[i++] = num; } return arr; } public static int[] generateSortedArray(int n) { int[] arr = new int[n]; for (int i = ; i < n; i++) { arr[i] = i; } return arr; } public static int[] generateReverseArray(int n) { int[] arr = new int[n]; for (int i = ; i < n; i++) { arr[i] = n - i - 1; } return arr; } } ``` ### 回答2: 首先,使用Java的Random类生成10000个不重复的整数。可以通过创建一个HashSet来存储生成的整数,因为HashSet不允许重复值。循环生成整数,将每个生成的整数添加到HashSet中,直到HashSet的大小达到10000为止。 接下来,使用Java的快速排序算法对生成的10000个整数进行排序。可以创建一个名为quickSort的函数,该函数采用数组以及数组的开始索引和结束索引,以对数组的一部进行排序。在实现快速排序算法时,可以使用递归的方法,将数组不断地为两部,直到每个部只包含一个元素为止。然后,通过比较和交换元素的位置,将数组的元素按照从小到大的顺序排列。 别对于有序、逆序和随机排序的情况,可以进行如下操作: 1. 有序 生成有序的10000个整数,可以直接使用循环生成,从1开始递增直到10000。然后调用quickSort函数对生成的整数进行排序,并记录运行时间。 2. 逆序 生成逆序的10000个整数,可以使用循环生成,从10000开始递减直到1。然后调用quickSort函数对生成的整数进行排序,并记录运行时间。 3. 随机排序 生成随机顺序的10000个整数,可以使用Random类生成。通过循环生成10000个随机整数,并且每次生成的整数不能重复。然后调用quickSort函数对生成的整数进行排序,并记录运行时间。 最后,别得到三种情况下快速排序的运行时间,并进行比较。 ### 回答3: 首先,我们需要使用Java的Random类来生成10000个不重复的整数。为了实现这一点,我们可以创建一个HashSet来存储已经生成的数,并确保在生成新的数之前检查HashSet中是否已经存在该数。当HashSet的大小达到10000之后,我们就可以停止生成新的数。 接下来,我们需要实现快速排序算法来对生成的整数数组进行排序。快速排序是一种常用的排序算法,它的平均时间复杂度为O(nlogn)。快速排序的基本思想是选择一个基准元素,通过一趟排序将待排序割成独立的两部,其中一部的所有元素都比基准元素小,另一部都比基准元素大。然后继续对这两部重复执行快速排序,直到整个序列有序。 对于有序和逆序情况,我们可以通过对生成的整数数组进行排序后再反转来实现。对于随机排序情况,我们可以直接对生成的整数数组进行排序。 最后,我们可以使用System.currentTimeMillis()方法来获取排序前和排序后的时间,然后计算它们之间的差值得到运行时间。 以下是实现代码的示例: ```java import java.util.*; public class Main { public static void main(String[] args) { // 生成10000个不重复的整数 HashSet<Integer> set = new HashSet<>(); Random random = new Random(); while (set.size() < 10000) { int num = random.nextInt(10000); set.add(num); } Integer[] nums = set.toArray(new Integer[0]); // 有序情况 Arrays.sort(nums); long startTime = System.currentTimeMillis(); quickSort(nums, 0, nums.length - 1); long endTime = System.currentTimeMillis(); System.out.println("有序情况下的运行时间:" + (endTime - startTime) + "毫秒"); // 逆序情况 Arrays.sort(nums, Collections.reverseOrder()); startTime = System.currentTimeMillis(); quickSort(nums, 0, nums.length - 1); endTime = System.currentTimeMillis(); System.out.println("逆序情况下的运行时间:" + (endTime - startTime) + "毫秒"); // 随机排序情况 shuffleArray(nums); startTime = System.currentTimeMillis(); quickSort(nums, 0, nums.length - 1); endTime = System.currentTimeMillis(); System.out.println("随机排序情况下的运行时间:" + (endTime - startTime) + "毫秒"); } // 快速排序算法 public static void quickSort(Integer[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } public static int partition(Integer[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } // 随机打乱数组 public static void shuffleArray(Integer[] arr) { Random random = new Random(); for (int i = arr.length - 1; i > 0; i--) { int index = random.nextInt(i + 1); int temp = arr[index]; arr[index] = arr[i]; arr[i] = temp; } } } ``` 以上是用Java随机生成10000个不含重复整数,并使用快速排序算法对其进行排序的方法。对于三种情况(有序,逆序,随机排序),我们都计算了排序的运行时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值