输入总共两行。
第一行一个正整数,代表参加国际交流会的人数(即圆桌上所坐的总人数,不单独对牛牛进行区分)
第二行包含个正整数,第个正整数代表第个人的特征值。
其中
注意:
邻座的定义为: 第人的邻座为,第人的邻座是,第人的邻座是。
邻座的差异值计算方法为。
每对邻座差异值只计算一次。
输出总共两行。
第一行输出最大的差异值。
第二行输出用空格隔开的个数,为重新排列过的特征值。
(注意:不输出编号)
如果最大差异值情况下有多组解,输出任意一组即可。
4 3 6 2 9
20 6 2 9 3
这么坐的话
差异和为为最大的情况。
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } Arrays.sort(nums); long maxSum = 0; int[] result = new int[n]; int i = 0, j = n - 1, idx = 0; while (i < j) { result[idx++] = nums[i++]; result[idx++] = nums[j--]; } if (n % 2 == 1) result[idx] = nums[i]; for (i = 1; i < n; i++) { maxSum += Math.abs(result[i] - result[i - 1]); } maxSum += Math.abs(result[0] - result[n - 1]); System.out.println(maxSum); for (int r : result) { System.out.print(r + " "); } } }