답안 #604103

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
604103 2022-07-24T18:12:21 Z chuangsheep Balloons (CEOI11_bal) C++11
100 / 100
310 ms 5088 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

/*
 * how long can the radius of the new balloon at position bx be
 * so that it touches the ballon a, which is described by
 * its x position a.first and its radius a.second
 */
ld calc_r(pair<ld, ld> a, ld bx)
{
	return (a.first - bx) * (a.first - bx) / (4 * a.second);
}

int main()
{
	// number of balloons
	int n;
	cin >> n;

	// the radius of each balloon after inflating
	vector<ld> res(n);
	// the balloons we should check when we add a new one
	stack<pair<ld, ld>> to_check;

	// simulate inflating balloons
	for (int i = 0; i < n; i++)
	{
		// the x coordinate and the maximum radius of the current balloon being inflated
		ld x, r;
		cin >> x >> r;

		// the maximum radius of the current balloon
		ld max_r = r;
		/*
		 * as long as the stack is not empty, we want to check
		 * if the last balloon makes the radius of the current balloon smaller
		 */
		while (!to_check.empty())
		{
			pair<ld, ld> last = to_check.top();
			// the radius if the current balloon touches the last balloon
			ld to_last_r = calc_r(last, x);
			/*
			 * the maximum possible radius is the smaller one between current maximum
			 * and the maximum radius so that we touches the last balloon
			 * (by which we have to stop)
			 */
			max_r = min(max_r, to_last_r);

			/*
			 * if current maximum radius >= radius of the last balloon, we can
			 * remove the last balloon since it will never be touched by any
			 * new balloons other than the current one
			 */
			if (max_r >= last.second)
			{
				to_check.pop();
				// check the next balloon saved which will possibly reduce max_r
				continue;
			}
			/*
			 * otherwise, the current balloon is smaller than the last saved balloon
			 * and we can stop checking
			 */
			else
			{
				break;
			}
		}
		/*
		 * save the x coordinate and radius of the current balloon, so that
		 * it can be checked later when a new balloon being inflated
		 */
		to_check.push({x, max_r});

		res[i] = max_r;
	}

	cout << fixed << setprecision(3);
	for (auto &r : res)
	{
		cout << r << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB 10 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB 2 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB 505 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 340 KB 2000 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 676 KB 20000 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 1844 KB 50000 numbers
2 Correct 81 ms 1400 KB 49912 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 2992 KB 100000 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 3396 KB 115362 numbers
2 Correct 190 ms 2872 KB 119971 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 4184 KB 154271 numbers
2 Correct 310 ms 4636 KB 200000 numbers
# 결과 실행 시간 메모리 Grader output
1 Correct 286 ms 5088 KB 200000 numbers
2 Correct 309 ms 4684 KB 199945 numbers