D. Divide and Sum
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let’s sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p,q)=∑ni=1|xi−yi|.
Find the sum of f(p,q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1≤n≤150000).
The second line contains 2n integers a1,a2,…,a2n (1≤ai≤109) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
inputCopy
1
1 4
outputCopy
6
inputCopy
2
2 1 2 1
outputCopy
12
inputCopy
3
2 2 2 2 2 2
outputCopy
0
inputCopy
5
13 8 35 94 9284 34 54 69 123 846
outputCopy
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
p=[1], q=[4], then x=[1], y=[4], f(p,q)=|1−4|=3;
p=[4], q=[1], then x=[4], y=[1], f(p,q)=|4−1|=3.
In the second example, there are six valid partitions of the array a:
p=[2,1], q=[2,1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
p=[2,2], q=[1,1];
p=[2,1], q=[1,2] (elements with indices 1 and 4 are selected in the subsequence p);
p=[1,2], q=[2,1];
p=[1,1], q=[2,2];
p=[2,1], q=[2,1] (elements with indices 3 and 4 are selected in the subsequence p).
题意就是给出你一个长度为的数组,然后让你分成和这两个数组,p是单调不减,q是单调不增(注意这个条件解题的重点),然后给出个定义
最后就是求这个式子的值,还有就是对998244353去膜,注意数据量是150000
思路:
首先看下这个式子可以看出其实xi和yi对结果的影响就是,max(xi,yi)-min(xi,yi);也就是加上较大值,减去较小值。然后就是上面我们说的重点定义p是单调不减,q是单调不增看下图:
这个图就是构造出来和然后对应的差,我们可以看出对应的是 ,对应的是。然后我们看出取较大值都是再原数列后个数中找出来的,具体看这个例子吧:
第一个是我们对排序后的数组,下一个是按构造出来的数组,可以看出
- 1->8 对结果的影响+8-1
- 2->7 对结果的影响+7-2
- 3->6 对结果的影响+6-3
可以看出这个结果就是 原数列排序后的后3个和减去前三个3.
再看下面那个图我们将6和3交换(是把6放入p中,把3放入q中)然后再次按要求排序就得出那个图也就是
1, 1->8 对结果的影响+8-1
2, 2->7 对结果的影响+7-2
3, 6->3 对结果的影响+6-3
好像这个例子不够生动形象,那我们把6和2换一下 - 这样就更形象了
- 1->8 对结果的影响+8-1
- 3->7 对结果的影响+7-3
- 6->2 对结果的影响+6-2
这样我们就可以看出其实每次换种顺序对结果并没有影响,所以我们就是找顺序的种数就行。种数就是从中选出个也就是
最后就化成