跳到正文

题解

题解:P15289 「YLLOI-R3-T4」止战之殇

BurnDawn1012约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年2月12日
收录
2026年6月26日
更新
2026年11月8日
篇幅
约 6 分钟
获赞
5
题目
P15289

版本

  1. v2当前
  2. v1

Upd:被 hack 了。

Upd:时隔一万年来修复了。

思路

首先令 cnti=⌊bi−2d−22d⌋+1cnt_i=\lfloor \frac{b_i-2d-2}{2d} \rfloor + 1 表示每个点可以走几次。

令 cnti>1cnt_i > 1 的点为安全点。

这些点是可以重复走的。

发现我们走过的路程要么是先不断地往左走安全点,再向右一路狂奔,或者反过来。

这样就有一个 bfs 错解了。可以得到 30 分。别问我为什么知道。

注意到有一种特殊情况,就是 2 1 1 1 1 1 2 这类数据,你可以 1→3→5→7→6→4→21 \rightarrow 3 \rightarrow 5 \rightarrow 7 \rightarrow 6 \rightarrow 4 \rightarrow 2 这样遍历完。

注意到条件是 cntl>1,cntr>1,cntl⋯r=1cnt_l>1,cnt_r>1,cnt_{l \cdots r}=1。

注意是 cntl>1cnt_l > 1 而不是 cntl=2cnt_l = 2,否则只有 50 分。别问我为什么知道。

记得开 long long,不然只有 10 分,连错解都不如。别问我为什么知道。

还有一个,拿向右狂奔举例,设最左的点为 LL,你需要尝试向 L−1L-1,L−2L-2 扩展。此时扩展条件不再是 cnti>1cnt_i > 1,而是 cnti≥1cnt_i \geq 1。这是因为最左段的点可以只通过一次。

code

C++
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1e6 + 5;

int n, a, d, b[N];
bool vis[N];

signed main(){
	cin >> n >> a >> d;
    for (int i = 1; i < n; i ++ ) cin >> b[i];
    for (int i = 1; i < n; i ++ ){
		if (b[i] >= 2 * d + 2) b[i] = (b[i] - 2 * d - 2) / (2 * d) + 1;
		else b[i] = 0;
	}
    
    int L = a, R = a; queue <int> q;
    
    q.push(a); vis[a] = 1;
    while (q.size()){
		auto t = q.front(); q.pop(); L = min(L, t);
		 
		if (t - 1 > 0 && !vis[t - 1] && b[t - 1] > 1) vis[t - 1] = 1, q.push(t - 1);
		if (t - 2 > 0 && !vis[t - 2] && b[t - 2] > 1) vis[t - 2] = 1, q.push(t - 2);
		
		int x = t - 1;
		while (x > 0 && b[x] == 1) x --;
		if (x > 0 && b[x] >= 2 && !vis[x]){
			vis[x] = 1;
			q.push(x);
		}
	}
	
	memset(vis, 0, sizeof vis);
    q.push(a); vis[a] = 1;
    while (q.size()){
		auto t = q.front(); q.pop(); R = max(R, t);
		if (t + 1 < n && !vis[t + 1] && b[t + 1] > 1) vis[t + 1] = 1, q.push(t + 1);
		if (t + 2 < n && !vis[t + 2] && b[t + 2] > 1) vis[t + 2] = 1, q.push(t + 2);
		
		int x = t + 1;
		while (x < n && b[x] == 1) x ++;
		if (x < n && b[x] >= 2 && !vis[x]){
			vis[x] = 1;
			q.push(x);
		}
	}
	
	int now, ans = 0;
	
	now = 0;
	if (L > 2 && b[L - 2]) L = min(L, L - 2);
	else if (L > 1 && b[L - 1]) L = min(L, L - 1);
	for (int i = L; i < n; i ++ ){
		now += b[i];
		if (i + 2 < n && !b[i + 1] && !b[i + 2]){
			break;
		}
		else if (i + 1 < n && !b[i + 1]) i ++;
	}
	ans = max(ans, now);
	
	now = 0;
	if (R < n - 2 && b[R + 2]) R = max(R, R + 2);
	else if (R < n - 1 && b[R + 1]) R = max(R, R + 1);
	for (int i = R; i >= 1; i -- ){
		now += b[i];
		if (i > 1 && !b[i - 1] && !b[i - 2]){
			break;
		}
		else if (i > 0 && !b[i - 1]) i --;
	}
	ans = max(ans, now);
	
	now = 0;
	for (int i = a; i < n; i ++ ){
		if (b[i]) now ++ ;
		if (i + 2 < n && !b[i + 1] && !b[i + 2]) break;
		else if (i + 1 < n && !b[i +1]) i ++;
	}
	ans = max(ans, now);
	
	now = 0;
	for (int i = a; i > 0; i -- ){
		if (b[i]) now ++ ;
		if (i - 2 > 0 && !b[i - 1] && !b[i - 2]) break;
		else if (i > 0 && !b[i - 1]) i -- ;
	}
	ans = max(ans, now);
	
	cout << ans << "\n";
}

后记

终于 AK 基础赛了!