Submission #770929

#TimeUsernameProblemLanguageResultExecution timeMemory
770929joshuaZRoad Construction (JOI21_road_construction)C++17
100 / 100
1876 ms15668 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int mxN = 250002;

int n, k;
pll a[mxN];

bool solve(ll d){
    set<pll> st;
    int c = 0;
    for (int i = 1, j = 1; i <= n; i++){
        while (a[i].first - a[j].first > d){
            st.erase({a[j].second, a[j].first});
            j++;
        }
        auto l = st.lower_bound({a[i].second - d, -1e10});
        auto r = st.upper_bound({a[i].second + d, -1e10});
        for (auto it = l; it != r; it++){
            c++;
            if (c >= k) return true;
        }
        st.insert({a[i].second, a[i].first});
    }

    return false;
}

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

    cin >> n >> k;
    //xi + yi - (xj + yj) <= val
    //xi - yi - (xj - yj) <= val
    for (int i = 1; i <= n; i++){
        ll x, y; cin >> x >> y;
        a[i].first = x + y;
        a[i].second = x - y;
    }
    sort(a + 1, a + 1 + n);

    ll lo = 1, hi = 5e9, ans = -1;
    while (lo <= hi){
        ll mid = lo + (hi - lo) / 2;
        if (solve(mid)){
            ans = mid;
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }
    }

    set<pll> st;
    priority_queue<ll> pq;
    for (int i = 1, j = 1; i <= n; i++){
        while (a[i].first - a[j].first > ans){
            st.erase({a[j].second, a[j].first});
            j++;
        }
        auto l = st.lower_bound({a[i].second - ans, -1e10});
        auto r = st.upper_bound({a[i].second + ans, -1e10});
        for (auto it = l; it != r; it++){
            pq.emplace(max(abs((*it).second - a[i].first), abs((*it).first - a[i].second)));
            if (pq.size() > k){
                pq.pop();
            }
        }
        st.insert({a[i].second, a[i].first});
    }

    vector<ll> vec;
    while (!pq.empty()){
        vec.emplace_back(pq.top());
        pq.pop();
    }

    reverse(vec.begin(), vec.end());
    for (int i = 0; i < k; i++){
        cout << vec[i] << '\n';
    }

}

Compilation message (stderr)

road_construction.cpp: In function 'int main()':
road_construction.cpp:66:27: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   66 |             if (pq.size() > k){
      |                 ~~~~~~~~~~^~~
#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...