跳到正文

题解

P9495 Solution

irris约 8 分钟
原文

这是 2025年10月21日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2023年8月7日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 8 分钟
题目
P9495

版本

  1. v2当前
  2. v1

Preface

感觉给了个大样例的话,就会好做很多啊!

不过贺 Leasier 的 O(n32n)\mathcal O(n^32^n) 暴力对拍,10 分钟就可以调出来,太爽了。

Solution

沿用官方题解关于 A 段,B 段,C 段的定义,因为这一部分是我写的,不过那个时候我并不会这题。

  • 初始设有 cntcnt 个 1\tt 1。
  • 得到 A 段的结束状态:
    • 这时魔王会把所有 11\tt 11 删掉。
    • 从前往后遍历序列,将 11\tt 11 贪心删掉。记我们删掉了 cntAcnt_A 对 11\tt 11,那么 A 段则持续了 cntAcnt_A 个回合。
  • 得到 B 段的结束状态:
    • 这时勇士会尽可能填充更多的 1\tt 1。
    • 在 A 段的基础上,贪心地向里面填入 1\tt 1,但不存在 11\tt 11 子串。
    • 设我们最终总共有 cntBcnt_B 个 1\tt 1,那么 B 段持续了 stepB=⌊cntB−cntA−cnt2⌋step_B = \lfloor\frac{cnt_B - cnt_A - cnt}{2}\rfloor 回合。
    • 上述式子的解释:在 A 段每天增加一个 1\tt 1,B 段每天增加两个 1\tt 1。
  • 得到 C 段的耗时:
    • 这里有个小细节,就是若 cntA+stepBcnt_A + step_B 回合后,恰好 得到了 cntBcnt_B 个 1\tt 1,那么下一回合 依旧属于 B 段,增加两个 1\tt 1;否则下一回合属于 C 段(已经出现 11\tt 11 状物了),增加一个 1\tt 1。
    • 耗时为 (n−cntB)−[cntB=cnt+cntA+2stepB](n - cnt_B) - [cnt_B = cnt + cnt_A + 2step_B]。
  • 输出 cntA+stepB+(n−cntB)−[cntB=cnt+cntA+2stepB]cnt_A + step_B + (n - cnt_B) - [cnt_B = cnt + cnt_A + 2step_B]。

Hack

这一部分特判 尤为重要。

  • cnt=0cnt = 0,若 n≤4n \leq 4 输出 1+[n=4]1 + [n = 4],否则先行模拟一轮(a1,a3,a5←1a_1, a_3, a_5 \gets \tt 1)。
  • 特判所有 n≤3n \leq 3,你可能会输出一些不对的 −1,0,1-1, 0, 1。
  • cnt+cntA>cntBcnt + cnt_A \gt cnt_B,也就是说甚至没有 B 段,直接跳到 C 段。你并不需要关心跳到的这个时刻是在什么时刻,只需输出 n−cntn - cnt 即可(因为每回合增加一个 1\tt 1)。
  • 注意到,若 初始没有 A 段,魔王有可能 无论如何怎么消除都会让 cntBcnt_B 增加 11(例如 01000010\tt{01000010}),如勇士先手,根据定义 cntB=3cnt_B = 3(01001010\tt{0100\color{green}1\color{normal}010});但魔王一定要消除掉一个 1\tt 1,那么这时 cntB=4cnt_B = 4(10101010\tt{\color{red}1\color{normal}0\color{red}1\color{normal}0\color{red}1\color{normal}010})。
    • 如果我们设 a−1=an+2=1a_{-1} = a_{n + 2} = \tt 1,a0=an+1=0a_{0} = a_{n + 1} = \tt 0(相当于规定了边界),那么这里增加的条件可以转化为对于相邻的三个 1\tt 1 出现位置 x<y<zx \lt y \lt z 有 z≡y+1≡x(mod2)z \equiv y + 1\equiv x \pmod 2。
    • 证明一下,相当于 ⌊z−y−22⌋+⌊y−x−22⌋<⌊z−x−22⌋−1\lfloor\frac{z - y - 2}{2}\rfloor + \lfloor\frac{y - x - 2}{2}\rfloor \lt \lfloor\frac{z - x - 2}{2}\rfloor - 1,即 ⌊z−y2⌋+⌊y−x2⌋<⌊z−x2⌋\lfloor\frac{z - y}{2}\rfloor + \lfloor\frac{y - x}{2}\rfloor \lt \lfloor\frac{z - x}{2}\rfloor(这里无视掉 z−y=1z - y = 1 或 y−x=1y - x = 1 的特殊情况),那么显然只有在 z−yz - y,y−xy - x 均为奇数时成立。
    • 显然要求所有的 x,y,zx, y, z 对均满足上述条件才达成了这个特判,这时我们假装模拟了一轮,让 cnt←cnt+2,cntB←cntB+1cnt \gets cnt + 2, cnt_B \gets cnt_B + 1 即可。

Code

给出一个对拍器,这里面有我的代码,改改就能贺了,给大家提供更多样的贺题解选择!

C++
#include <bits/stdc++.h>

#include <chrono>
std::mt19937 eng(std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return std::uniform_int_distribution<int>(l, r)(eng); }

const int N = 10;
int cnt = 0;

#define MAXN 1000001
int a[MAXN], b[MAXN], dp[MAXN]; 

namespace Standard {
	int solve() {
		int cnt = 0, cntA = 0, ext = 0; a[0] = a[N + 1] = 0;
		for (int i = 0; i <= N + 1; ++i) dp[i] = 0;
		for (int i = 1; i <= N; ++i) cnt += a[i];
		if (cnt == 0) {
			if (N < 5) return 1 + (N == 4);
			a[1] = a[3] = a[5] = 1, cnt = 3, ext = 1;
		}
		if (N == 1) return 0;
		if (N <= 3) return cnt != N;
		for (int i = 1; i < N; ++i) if (a[i] == 1 && a[i + 1] == 1) // A 段
			++cntA, a[i] = a[i + 1] = 0;
		for (int i = 1; i <= N; ++i) // B 段结束时的效果
			dp[i] = std::max(dp[i - 2], dp[i - 3]) + (a[i] == 0 && a[i - 1] == 0 && a[i + 1] == 0) + (a[i] == 1);
		int cntB = *std::max_element(dp + 1, dp + N + 1);
		if (cntA == 0) {
			int lst = 1;
			for (int i = 1; i <= N; ++i) if (a[i] == 1) {
				if (((i - lst) & 1) == 0) break; lst = i;
			} else if (i == N && ((N - lst) & 1) == 1) cnt += 2, ++ext, ++cntB;
		}
		if (cnt + cntA > cntB) return N - cnt + ext;
		int stepB = (cntB - cntA - cnt) / 2, finalB = cnt + cntA + stepB * 2;
		return cntA + stepB + (N - cntB) - (finalB == cntB) + ext;
	}
};

namespace MySolve {
	int solve() { return -1; }
};

int main() {
	while (cnt < 10000) {
		for (int i = 1; i <= N; ++i) b[i] = a[i] = rnd(0, 1);
		
		int u = Standard::solve();
		for (int i = 1; i <= N; ++i) a[i] = b[i];
		int v = MySolve::solve();

		if (u != v) {
			printf("WA #%d\n", ++cnt);
			freopen("T4pWA.in", "w", stdout);
			printf("1\n%d\n", N);
			for (int i = 1; i <= N; ++i) printf("%d%c", b[i], " \n"[i == N]);
			freopen("T4pWA.out", "w", stdout);
			printf("%d %d\n", u, v);
			return 0;
		}
		printf("AC #%d\n", ++cnt);
	}
	return 0;
}