跳到正文

题解

题解:P14440 [JOISC 2013] 山岳救援队 / Mountain Rescue Team

RpInfinity约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年8月21日
收录
2026年8月28日
更新
2026年11月11日
篇幅
约 3 分钟
题目
P14440

版本

  1. v2当前
  2. v1

思路

因为离山顶距离越远高度越小,两维均有单调性,所以原图其实可以以山顶划分为四个杨表,于是直接对于每一个杨表 O(R′+C′)O(R' + C') 查找对应的高度即可。

杨表的食用方式

对于一个 nn 行 mm 列的杨表 AA,不妨设其对于任意位置 (i,j)(i, j):

  • 若 i<ni < n:Ai,j<Ai,j+1A_{i,j}<A_{i,j+1};
  • 若 j<mj < m:Ai,j<Ai+1,jA_{i, j}<A_{i + 1, j}。

首先初始位置应为 (1,m)(1, m),原因是这样子 yy 更小值就会变大,xx 更大值就会更大,可以通过与查找值的大小关系确定应该调整 xx 还是 yy。

设要查找的值为 KK,当前的位置为 (x,y)(x, y)。

  • 若 Ax,y<KA_{x, y} < K,显然的肯定要将 xx 加一;
  • 若 Ax,y=KA_{x, y} = K,已经找到目标;
  • 若 Ax,y>KA_{x, y} > K,大了,于是将 yy 减 11 使值更小。
Code
C++
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

void Rescue(int R, int C, int RS, int CS, int X);
int Measure(int RM, int CM);
void Pinpoint(int RP, int CP);

void Rescue(int R, int C, int RS, int CS, int X) {
    auto check = [&](int sr, int sc) {
        // r = RS + sr * x
        // c = CS + sc * y
        int nr = sr == 1 ? R - RS : RS - 1;
        int nc = sc == 1 ? C - CS : CS - 1;
        int x = 0, y = nc;
        while (x <= nr && y >= 0) {
            int r = RS + sr * x;
            int c = CS + sc * y;
            int h = Measure(r, c);
            if (h == X) {
                Pinpoint(r, c);
                return true;
            }
            if (h > X) ++x;
            else --y;
        }
        return false;
    };
    for (int sr : {-1, 1}) {
        for (int sc : {-1, 1}) {
            if (check(sr, sc))
                return;
        }
    }
}
原文