跳到正文

题解

题解:P14353 重排

Ryanhao约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年10月30日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 9 分钟
获赞
1
题目
P14353

版本

  1. v2当前
  2. v1

重排

闲话:赛时不如三岁小孩。

思路

对于三岁小孩问题,最优的方案是,把 aa 排序,从中位数砍开,交错排列,这样会形成一个一上一下的锯齿形结构,其中每个数和前 or 后一个数计算时会对答案共享两次。当然,首尾的多的贡献要减掉。减掉什么好呢?若 nn 为偶数,当然是最中间的两个数的差,否则就是 aa 的中位数和他的前后驱(指排名在它旁边的)的差,取小值。

所以有

g(a)=2[(∑i=1⌊n2⌋ai)−(∑i=⌈n2⌉+1nai)]−{an2+1−an2n≡0(mod2)min⁡{a⌈n2⌉+1−a⌊n2⌋+1,a⌊n2⌋+1−a⌊n2⌋}n≡1(mod2)g(a)=2\left[ \left(\sum_{i=1}^{\left\lfloor\frac{n}{2}\right\rfloor}a_i\right) -\left(\sum_{i=\left\lceil\frac{n}{2}\right\rceil+1}^na_i\right) \right] - \begin{cases} a_{\frac n2+1}-a_{\frac n2} & n \equiv 0\pmod 2 \\ \min\left\{a_{\left\lceil\frac{n}{2}\right\rceil+1}-a_{\left\lfloor\frac{n}{2}\right\rfloor+1}, a_{\left\lfloor\frac{n}{2}\right\rfloor+1}-a_{\left\lfloor\frac{n}{2}\right\rfloor}\right\}& n \equiv 1\pmod 2 \end{cases}

然后回到原题,想想用什么维护。

对于中位数题,对顶堆是一种不错的选择。这东西支持插入一个数,并查询排名为 kk 的数的值。具体的,我们拿一个大根堆,一个小根堆,并让大根堆里的所有数小于小根堆里的所有数。插入时根据堆顶决定插入哪个堆,查询时调整大根堆大小(把大根堆的堆顶塞到小根堆或者把小根堆的堆顶塞到大根堆直到大根堆的大小等于 kk),然后输出大根堆堆顶。

当然这东西直接去挑战平衡树肯定不行,这东西查一次任意 kk 排名最坏情况是 O(nlog⁡2n)O(n\log_2n),但求中位数只有 O(log⁡2n)O(\log_2n)。

以下丢个模板。

C++
class PrioSight { // 乱取的名字
private:
	priority_queue<long long>                                       q1;
	priority_queue<long long,vector<long long>,greater<long long> > q2;
	long long sum1,sum2; int cnt;
	void resiz(int k) { 
		// 调整 q1 大小
		while (q1.size() < k) {
			long long tp = q2.top();
			q2.pop(); q1.push(tp);
		}
		while (q1.size() > k) {
			long long tp = q1.top();
			q1.pop(); q2.push(tp);
		}
	}
public:
	void ins(long long x) {
		// 插入根据堆顶情况决定插入到哪里。
		if (q1.empty()) {
			if (!q2.empty() && q2.top() <= x) {
				q2.push(x);
			} else {
				q1.push(x);
			}
			cnt++; return;
		}
		if (q1.top() > x) {
			q1.push(x);
		} else {
			q2.push(x);
		}
		cnt++;
	} 
	long long get_kth(int k) {
		// 查询排名
		resiz(k); return q1.top();
	}
};

但是本题要维护两个和,加上 sum1,sum2 维护两个堆里的数的和即可。

ACCode

C++
#include <iostream>
#include <queue>
using namespace std;
#define min(x,y) (x<y?x:y)

int readint(); long long readll();  void write(long long x);

class PrioSight {
	// Chinese name "Yes top yes", impossible to find the real name.
private:
	priority_queue<long long>                                       q1;
	priority_queue<long long,vector<long long>,greater<long long> > q2;
	long long sum1,sum2; int cnt;
	void resiz(int k) { 
		// let q1 have k numbers
		while (q1.size() < k) {
			long long tp = q2.top();
			q2.pop(); sum2 -= tp;
			sum1 += tp; q1.push(tp);
		}
		while (q1.size() > k) {
			long long tp = q1.top();
			q1.pop(); sum1 -= tp;
			sum2 += tp; q2.push(tp);
		}
	}
public:
	void ins(long long x) {
		// insert a number
		if (q1.empty()) {
			if (!q2.empty() && q2.top() <= x) {
				q2.push(x);
				sum2 += x;
			} else {
				q1.push(x);
				sum1 += x;
			}
			cnt++; return;
		}
		if (q1.top() > x) {
			q1.push(x);
			sum1 += x;
		} else {
			q2.push(x);
			sum2 += x;
		} // add it to the fit one
		cnt++;
	} 
	long long get_kth(int k) {
		// the k-th smallest number.
		resiz(k); return q1.top();
	}
	long long sum_min(int k) {
		// sum of k of the smallest numbers.
		resiz(k); return sum1;
	}
	long long sum_max(int k) {
		// sum of k of the largest numbers.
		resiz(cnt-k); return sum2;
	}
} GXR;

const int maxn = 3e6+5;
long long b[maxn];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int m = readint(), typ = readint();
	for (int i = 1; i <= m; i++)
		b[i] = readll();
	long long lans = 0, tans = 0;
	for (int n = 1; n <= m; n++) {
		long long nw = b[n] ^ (typ ? lans : 0ll);
		GXR.ins(nw);
		if (n == 1) {
			continue;
		} else if (n & 1) {
			long long A = GXR.sum_min(n/2);
			long long B = GXR.sum_max(n/2);
			long long C = GXR.get_kth(n/2+0);
			long long M = GXR.get_kth(n/2+1);
			long long D = GXR.get_kth(n/2+2);
			lans = 2*(B-A)-min(D-M,M-C);
			tans ^= lans;
		} else {
			long long A = GXR.sum_min(n/2);
			long long B = GXR.sum_max(n/2);
			long long C = GXR.get_kth(n/2+0);
			long long D = GXR.get_kth(n/2+1);
			lans = 2*(B-A)-(D-C);
			tans ^= lans;
		}
	}
	write(tans);
	return 0;
}

// qread
int readint() {
	int x = 0;
	char c = getchar_unlocked();
	while (c < '0' || c > '9') c = getchar_unlocked();
	while (c >= '0' && c <= '9') {
		x = (x<<1)+(x<<3)+(c^'0');
		c = getchar_unlocked();
	}
	return x;
}
long long readll() {
    long long x = 0;
    char c = getchar_unlocked();
    while (c < '0' || c > '9') c = getchar_unlocked();
    while (c >= '0' && c <= '9') {
        x = (x<<1)+(x<<3)+(c^'0');
        c = getchar_unlocked();
    }
	return x;
}
void write(long long x) {
	if (x >= 10) write(x/10);
	putchar((x%10)^'0');
}
原文