LY1380 [ 20231009 NOIP 模拟赛 T1 ] AK 神

发布时间 2023-10-09 19:23:35作者: cxqghzj

题意

给定长度为 \(n\) 的序列 \(S\)

\(A\)\(B\) 两人轮流取连续 \(k\) 个数,保证 \(n \equiv 1\pmod k\)

\(A\) 使最终数字更小,\(B\) 使最终数字更大。

问取到数的和。

Sol

直接考虑每次选哪些数,怎么选显然是不好做的。

不难发现 \(n \equiv 1\pmod k\) 的条件。

题面提示我们要往最终剩下的数字想。

显然最终剩下的数与题目的问题等价。

对于一个序列 \(S_1, S_2, ..., S_i, ..., S_n\) 若其中 \(S_i\) 为最终留下的数。那就说明 \(1 \to i - 1\)\(i + 1 \to n\)\(\equiv 0\pmod k\)

也就是 \(i \equiv 1\pmod k\)

把所有位置排个序,求中位数即可。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <vector>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}

vector <int> isl;
signed main() {
#ifdef ONLINE_JUDGE
	freopen("ak.in", "r", stdin);
	freopen("ak.out", "w", stdout);
#endif
	int n = read(), k = read();
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		int x = read();
		ans += x;
		if (i % k == 1 || k == 1)
			isl.push_back(x);
	}
	sort(isl.begin(), isl.end());
	write(ans - isl[(isl.size()) / 2]), puts("");
	return 0;
}