跳到正文

题解

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

zhangjiaheng约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年2月12日
收录
2026年2月13日
更新
2026年11月14日
篇幅
约 4 分钟
题目
P15289

版本

  1. v2当前
  2. v1

前言

奶龙题,但是细节有点多。

思路

考虑他会怎么走:

  • 先向右走再走回来再向左走到游戏结束。

  • 先向左走再走回来再向右走到游戏结束。

以下计 sis_i 为能在 ii 这个位置待几回合即 si=⌊bi−22d⌋s_i = \lfloor \cfrac{b_i - 2}{2d} \rfloor。

考虑如何扩展,这里以向右为例,向左同理,首先如果有连着的零肯定就不能往后跳了,可以让答案加上 sis_i。

交之,唉我怎么假了。

考虑以下的情况:

我们需要从一跳回来,但是他跳不回来了,需要特判。

同时还有:

特判之后即可通过本题。

upd:好的如何我又被 hack 了,赛时数据过水。

考虑以下 hack:

所以需要特判 00,11 段的端点是起点,这种情况是可以跳回去的。

代码

C++
signed main(){
    #ifndef ONLINE_JUDGE
        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> a >> d;
    for(int i = 1; i <= n - 1; ++i) cin >> b[i];
    for(int i = 1; i <= n - 1; ++i) s[i] = ((b[i] - 2) / (d << 1));
    int L = 0, R = 0, BackR = 0, BackL = 0;
    int sum = 0, cnt = 0, f = 0;
    for(int i = a; i <= n - 1; ++i){
        R += s[i];
        if(!f) BackR = max(BackR, sum + s[i]);
        sum += s[i];
        if(s[i] == 1 && s[i + 1] == 0 && i != a) f = 1;
        if(s[i] == 1 && s[i - 1] == 0 && i != a + 1 && i != a) f = 1;
        if(s[i] == 0 && s[i + 1] == 0 && i != a) break;
    }
    sum = 0, cnt = 0, f = 0;
    for(int i = a - 1; i >= 1; --i){
        L += s[i];
        if(!f) BackL = max(BackL, sum + s[i]);
        sum += s[i];
        if(s[i] == 1 && s[i - 1] == 0 && i != a) f = 1;
        if(s[i] == 1 && s[i + 1] == 0 && i != a - 1 && i != a) f = 1;
        if(s[i] == 0 && s[i - 1] == 0 && i != a) break;
    }
    if(s[a] == 0 && s[a + 1] == 0) BackL = BackR = 0;
    if(s[a] == 0 && s[a - 1] == 0) BackL = BackR = 0;
    int ans = max(R + BackL, L + BackR);
    cout << ans << '\n';
	return 0;
}
原文