Submission #1275151

#TimeUsernameProblemLanguageResultExecution timeMemory
1275151vk3601hSirni (COCI17_sirni)C++20
Compilation error
0 ms0 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N;
    cin >> N;
    vector<int> P(N);
    for (int i = 0; i < N; ++i) {
        cin >> P[i];
    }
    sort(P.begin(), P.end());

    set<int> visited;
    visited.insert(P[0]);
    long long total = 0;

    for (int i = 1; i < N; ++i) {
        int x = P[i];
        bool found = false;
        // Check for divisors in visited
        int limit = sqrt(x);
        for (int d = 1; d <= limit; ++d) {
            if (x % d == 0) {
                if (visited.find(d) != visited.end()) {
                    found = true;
                    break;
                }
                if (d != x / d && visited.find(x / d) != visited.end()) {
                    found = true;
                    break;
                }
            }
        }
        if (found) {
            visited.insert(x);
            continue;
        }

        // Find the largest element in visited less than x
        auto it = visited.upper_bound(x);
        int min_weight = INT_MAX;
        if (it != visited.begin()) {
            --it;
            int candidate = *it;
            min_weight = x % candidate;
        }

        // Check intervals [x/(k+1), x/k]
        for (int k = 1; k <= limit; ++k) {
            int L = x / (k + 1);
            int R = x / k;
            if (L > R) continue;
            auto it = visited.upper_bound(R);
            if (it != visited.begin()) {
                --it;
                if (*it >= L) {
                    int candidate = *it;
                    int weight = x % candidate;
                    if (weight < min_weight) {
                        min_weight = weight;
                    }
                }
            }
        }

        total += min_weight;
        visited.insert(x);
    }

    cout << total << endl;
    return 0;
}

Compilation message (stderr)

sirni.cpp: In function 'int main()':
sirni.cpp:48:26: error: 'INT_MAX' was not declared in this scope
   48 |         int min_weight = INT_MAX;
      |                          ^~~~~~~
sirni.cpp:6:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    5 | #include <cmath>
  +++ |+#include <climits>
    6 | using namespace std;