Submission #821725

#TimeUsernameProblemLanguageResultExecution timeMemory
821725rembocoderAliens (IOI16_aliens)C++17
60 / 100
2084 ms12736 KiB
#include "aliens.h"
#include <bits/stdc++.h>

using namespace std;

bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.first != b.first) {
        return a.first < b.first;
    }
    return a.second > b.second;
}

const long long inf = 2e18;

long long div_up(long long a, long long b) {
    if (a < 0) {
        return a / b;
    }
    return (a + b - 1) / b;
}

long long take_photos(int n, int m, int k, vector<int> x, vector<int> y) {
    for (int i = 0; i < n; i++) {
        if (x[i] > y[i]) {
            swap(x[i], y[i]);
        }
    }
    vector<pair<int, int>> pairs;
    for (int i = 0; i < n; i++) {
        pairs.push_back({x[i], y[i]});
    }
    sort(pairs.begin(), pairs.end(), cmp);
    vector<pair<int, int>> new_pairs;
    new_pairs.push_back({-1, -1});
    int max_y = -1;
    for (int i = 0; i < n; i++) {
        if (max_y < pairs[i].second) {
            max_y = pairs[i].second;
            new_pairs.push_back(pairs[i]);
        }
    }
    n = new_pairs.size();
    vector<long long> l(n), r(n);
    for (int i = 0; i < n; i++) {
        l[i] = new_pairs[i].first;
        r[i] = new_pairs[i].second + 1;
    }
    k = min(k, n - 1);
    vector<long long> dp(n, inf);
    dp[0] = 0;
    for (int jumps = 0; jumps < k; jumps++) {
        vector<long long> a(n), b(n);
        for (int i = 0; i + 1 < n; i++) {
            a[i] = dp[i] + l[i + 1] * l[i + 1] - max(0LL, r[i] - l[i + 1]) * max(0LL, r[i] - l[i + 1]);
            b[i] = 2 * l[i + 1];
        }
        vector<long long> new_dp(n, inf);
        vector<pair<long long, int>> ch;
        ch.push_back({-1, jumps});
        for (int i = jumps + 1; i < n; i++) {
            int from = ch[upper_bound(ch.begin(), ch.end(), make_pair(r[i], n)) - ch.begin() - 1].second;
            new_dp[i] = r[i] * r[i] + a[from] - b[from] * r[i];
            if (i + 1 == n) {
                break;
            }
            if (jumps == 0) {
                continue;
            }
            while (!ch.empty()) {
                int last = ch.back().second;
                long long x = ch.back().first;
                if (a[last] - b[last] * x < a[i] - b[i] * x) {
                    break;
                }
                ch.pop_back();
            }
            int last = ch.back().second;
            long long x = div_up(a[i] - a[last], b[i] - b[last]);
            ch.push_back({x, i});
        }
        dp = new_dp;
    }
    return dp[n - 1];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...