#include <algorithm>
#include "grader.h"
#define xx first
#define yy second
using namespace std;
using ii = pair<int, int>;
int GetH(int R, int C, int RS, int CS, int D)
{
int ans = 1000000001;
if (D < 0)
return ans;
ii pos[] = {
{RS - D, CS },
{RS + D, CS },
{RS, CS - D },
{RS, CS + D }
};
for (int i = 0; i < 4; i++)
{
if (pos[i].xx >= 1 && pos[i].xx <= R && pos[i].yy >= 1 && pos[i].yy <= C)
{
ans = min(ans, Measure(pos[i].xx, pos[i].yy));
continue;
}
//젤 먼 두 개로 분할
int x1 = pos[i].xx, y1 = pos[i].yy;
int x2 = x1, y2 = y1;
if (x1 < 1)
{
int g = 1 - x1;
x1 = x2 = 1;
y1 -= g;
y2 += g;
}
else if (x1 > R)
{
int g = x1 - R;
x1 = x2 = R;
y1 -= g;
y2 += g;
}
else if (y1 < 1)
{
int g = 1 - y1;
y1 = y2 = 1;
x1 -= g;
x2 += g;
}
else if (y1 > C)
{
int g = y1 - C;
y1 = y2 = C;
x1 -= g;
x2 += g;
}
if (x1 >= 1 && x1 <= R && y1 >= 1 && y1 <= C)
ans = min(ans, Measure(x1, y1));
if (x2 >= 1 && x2 <= R && y2 >= 1 && y2 <= C)
ans = min(ans, Measure(x2, y2));
}
return ans;
}
bool Find(int R, int C, int RS, int CS, int D, int X)
{
if (D == 0)
{
int h = Measure(RS, CS);
if (h == X)
{
Pinpoint(RS, CS);
return true;
}
return false;
}
int sx = RS - D, sy = CS;
int dx[] = { 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, -1, -1, 1 };
int di = 0;
for(int i = 0; i < 4 * D; i++, sx += dx[di], sy += dy[di])
{
if (i % D == 0)
di++;
if (sx < 1 || sx > R || sy < 1 || sy > C)
continue;
int h = Measure(sx, sy);
if (h == X)
{
Pinpoint(sx, sy);
return true;
}
}
return false;
}
void Rescue(int R, int C, int RS, int CS, int X) {
// 모서리 4개, 가운데 4개로 max, min을 특정할 수 있다.
// 속하는 영역을 이분 탐색으로 찾은 후 거기 밀기
int l = 0, r = max(R - RS, RS - 1) + max(C - CS, CS - 1);
int ans = r;
while (l <= r)
{
int mid = (l + r) / 2;
int nowMin = GetH(R, C, RS, CS, mid);
int upMin = GetH(R, C, RS, CS, mid - 1);
if (nowMin <= X && X <= upMin)
{
ans = mid;
break;
}
if (nowMin < X)
r = mid - 1;
else
l = mid + 1;
}
// ans거리에서 탐색
if (!Find(R, C, RS, CS, ans, X))
Find(R, C, RS, CS, ans - 1, X);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
304 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
256 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
256 KB |
Output is correct |
24 |
Correct |
5 ms |
256 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
256 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
368 KB |
Output is correct |
30 |
Correct |
5 ms |
256 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
256 KB |
Output is correct |
33 |
Correct |
5 ms |
304 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
256 KB |
Output is correct |
37 |
Correct |
5 ms |
256 KB |
Output is correct |
38 |
Correct |
5 ms |
256 KB |
Output is correct |
39 |
Correct |
5 ms |
256 KB |
Output is correct |
40 |
Correct |
5 ms |
256 KB |
Output is correct |
41 |
Correct |
5 ms |
256 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
256 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
256 KB |
Output is correct |
47 |
Correct |
5 ms |
308 KB |
Output is correct |
48 |
Correct |
5 ms |
256 KB |
Output is correct |
49 |
Correct |
5 ms |
256 KB |
Output is correct |
50 |
Correct |
4 ms |
256 KB |
Output is correct |
51 |
Correct |
5 ms |
384 KB |
Output is correct |
52 |
Correct |
5 ms |
256 KB |
Output is correct |
53 |
Correct |
5 ms |
304 KB |
Output is correct |
54 |
Correct |
5 ms |
256 KB |
Output is correct |
55 |
Correct |
5 ms |
256 KB |
Output is correct |
56 |
Incorrect |
5 ms |
256 KB |
Wrong Answer [5] |
57 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
696 KB |
Wrong Answer [5] |
2 |
Halted |
0 ms |
0 KB |
- |