Homework Introduction

#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int n, a[N];

int CHECK(double x, double R) {
	int now = n;
	while (1) {
		for (int i = now; i >= 1; i--) {
			if (a[i] <= x) {
				if (x - R <= a[i]) {
					now = i;
				} else {
					x = a[now];
					R -= 1;
					if (R <= 0 || a[now] - R > a[now - 1])
						return 0;
					break;
				}
			}
		}
		if (now == 1)
			return 1;
	}
}

int check(double x) {
	double l = a[1], r = a[n];
	double st = 0;
	while (r - l >= 1e-3) {
		double mid = (l + r) / 2;
		if (CHECK(mid, x)) {
			l = mid;
		} else
			r = mid;
	}
	st = l;
	int now = 1;
	while (1) {
		for (int i = now; i <= n; i++) {
			if (a[i] >= st) {
				if (st + x >= a[i]) {
					now = i;
				} else {
					st = a[now];
					x -= 1;
					if (x <= 0 || a[now + 1] - a[now] > x)
						return 0;
					break;
				}
			}
		}
		if (now == n)
			return 1;
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	double l = 0, r = 1e9;
	while (r - l >= 1e-3) {
		double mid = (l + r) / 2;
		if (check(mid)) {
			r = mid;
		} else
			l = mid;
	}
	cout << fixed << setprecision(1) << l << endl;
	return 0;
}

Problem

Please claim the assignment to see the problems.
Status
Live...
Problem
19
Open Since
2025-8-9 0:00
Deadline
2025-11-30 23:59
Extension
24 hour(s)