Submission #114819

# Submission time Handle Problem Language Result Execution time Memory
114819 2019-06-03T04:39:35 Z jwvg0425 산악 구조대 (JOI13_mountain) C++17
0 / 100
1000 ms 384 KB
#include <algorithm>
#include "grader.h"
#define xx first
#define yy second

using namespace std;
using ii = pair<int, int>;

int MinH(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;
		}

		if (x1 > R)
		{
			int g = x1 - R;
			x1 = x2 = R;
			y1 -= g;
			y2 += g;
		}

		if (y1 < 1)
		{
			int g = 1 - y1;
			y1 = y2 = 1;
			x1 -= g;
			x2 += g;
		}

		if (y1 > C)
		{
			int g = y1 - C;
			y1 = y2 = C;
			x1 -= g;
			x2 += g;
		}

		ans = min({ ans, Measure(x1, y1), Measure(x2, y2) });
	}

	return ans;
}

void Find(int R, int C, int RS, int CS, int D, int X)
{
	int sx = RS - D, sy = CS;
	int dx = 1, dy = 1;

	while (true)
	{
		if (sx == RS && sy == CS + D)
			dy = -1;
		
		if (sx == RS + D && sy == CS)
			dx = -1;

		if (sx == RS && sy == CS - D)
			dy = 1;

		if (sx < 1 || sx > R || sy < 1 || sy > C)
		{
			sx += dx;
			sy += dy;
			continue;
		}

		int h = Measure(sx, sy);

		if (h == X)
		{
			Pinpoint(sx, sy);
			return;
		}

		sx += dx;
		sy += dy;
	}
}

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 = 0;

	while (l <= r)
	{
		int mid = (l + r) / 2;

		int nowMin = MinH(R, C, RS, CS, mid);
		int upMin = MinH(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거리에서 탐색
	Find(R, C, RS, CS, ans, X);
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 360 KB Output is correct
2 Incorrect 5 ms 384 KB Wrong Answer [1]
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1085 ms 384 KB Time limit exceeded
2 Halted 0 ms 0 KB -