Submission #1018937

#TimeUsernameProblemLanguageResultExecution timeMemory
1018937coolboy19521Gap (APIO16_gap)C++17
30 / 100
42 ms2328 KiB
#include "bits/stdc++.h"
#include "gap.h"

using namespace std;

const int sz = 1e5 + 5;

long long a[sz];

long long findGap(int T, int N) {
    if (1 == T) {
		long long mn, mx;
		MinMax(0, 1e18, &mn, &mx);

		a[1] = mn;
		a[N] = mx;

		for (int i = 1; i <= (N+1)/2-1; i ++) {
			MinMax(mn+1, mx-1, &mn, &mx);
			a[i+1] = mn;
			a[N - i] = mx;
		}

		long long gp = 0;

		for (int i = 1; i <= N-1; i ++)
			gp = max(gp, a[i+1] - a[i]);

		return gp;
    }
    if (T == 2) {
        long long mn, mx;
        MinMax(0, 1e18, &mn, &mx);
        vector<long long> b = {mn, mx};
        long long k = (mx - mn + N - 2) / (N - 1);

        while (b.size() < N) {
            long long nextMin, nextMax;
            long long rangeStart = mn + 1;
            long long rangeEnd = min(mx - 1, mn + k);

            if (rangeStart > rangeEnd) break;

            MinMax(rangeStart, rangeEnd, &nextMin, &nextMax);

            if (nextMin == -1) { // No numbers found in the range
                mn = rangeEnd + 1;
                continue;
            }

            b.push_back(nextMin);
            if (nextMin != nextMax) {
                b.push_back(nextMax);
                while (nextMin < nextMax && b.size() < N) {
                    // Keep querying within the narrowed range
                    MinMax(nextMin + 1, nextMax - 1, &nextMin, &nextMax);
                    if (nextMin == -1) break; // Exit if no more numbers are found
                    if (nextMin == nextMax) {
                        b.push_back(nextMin);
                        break;
                    } else {
                        b.push_back(nextMin);
                        b.push_back(nextMax);
                    }
                }
            }

            mn = nextMax + 1; // Move to the next range beyond the last found max
        }

        // Sorting the elements to find the maximum gap
        sort(begin(b), end(b));

        long long maxGap = 0;
        for (int i = 0; i < b.size() - 1; i++) {
            maxGap = max(maxGap, b[i + 1] - b[i]);
        }
        return maxGap;
    }
    return 0; // This part should never be reached for T = 2
}

Compilation message (stderr)

gap.cpp: In function 'long long int findGap(int, int)':
gap.cpp:37:25: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |         while (b.size() < N) {
      |                ~~~~~~~~~^~~
gap.cpp:54:54: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   54 |                 while (nextMin < nextMax && b.size() < N) {
      |                                             ~~~~~~~~~^~~
gap.cpp:75:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         for (int i = 0; i < b.size() - 1; i++) {
      |                         ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...