제출 #1140058

#제출 시각아이디문제언어결과실행 시간메모리
1140058argon1Swimming competition (LMIO18_plaukimo_varzybos)C++20
10 / 100
246 ms4328 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1000000;
int times[MAX_N+5];
int N, A, B;

bool isFeasible(int maxDiff) {
    int groupCount = 0; // Count of groups formed
    int groupStart = 0; // Index of the start of the current group

    for (int i = 0; i < N; ++i) {
        // If the current swimmer can't be added due to exceeding maxDiff
        if (times[i] - times[groupStart] > maxDiff || i - groupStart + 1 > B) {
            // Check if the previous group size is valid
            if (i - groupStart < A) return false;
            // Start a new group
            groupStart = i;
            ++groupCount;
        }
    }

    // Check the size of the last group
    if (N - groupStart < A) return false;

    return true;
}

int main() {

    cin >> N >> A >> B;

    for (int i = 0; i < N; ++i) {
        cin >> times[i];
    }

    //Sort
    sort(times, times + N);

    //Binary search for the minimum maximum difference
    int low = 0;
    int high = times[N - 1] - times[0];
    int result = high;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        // Check if this mid value is feasible
        if (isFeasible(mid)) {
            result = mid; // Update result and try for a smaller maximum difference
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }

    cout << result << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...