题解:P14440 [JOISC 2013] 山岳救援队 / Mountain Rescue Team
RpInfinity · · 题解
思路
因为离山顶距离越远高度越小,两维均有单调性,所以原图其实可以以山顶划分为四个杨表,于是直接对于每一个杨表
:::info[杨表的食用方式]{open}
对于一个
- 若
i < n :A_{i,j}<A_{i,j+1} ; - 若
j < m :A_{i, j}<A_{i + 1, j} 。
首先初始位置应为
设要查找的值为
- 若
A_{x, y} < K ,显然的肯定要将x 加一; - 若
A_{x, y} = K ,已经找到目标; -
若 A_{x, y} > K ,大了,于是将y 减1 使值更小。
:::success[Code]
#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;
}
}
}
:::