Submission #770929

# Submission time Handle Problem Language Result Execution time Memory
770929 2023-07-02T07:08:09 Z joshuaZ Road Construction (JOI21_road_construction) C++17
100 / 100
1876 ms 15668 KB
#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

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 time Memory Grader output
1 Correct 94 ms 6992 KB Output is correct
2 Correct 92 ms 7004 KB Output is correct
3 Correct 87 ms 7048 KB Output is correct
4 Correct 84 ms 7096 KB Output is correct
5 Correct 89 ms 5784 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 9524 KB Output is correct
2 Correct 327 ms 12460 KB Output is correct
3 Correct 91 ms 6940 KB Output is correct
4 Correct 253 ms 12304 KB Output is correct
5 Correct 284 ms 12412 KB Output is correct
6 Correct 277 ms 12608 KB Output is correct
7 Correct 226 ms 12436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 4216 KB Output is correct
2 Correct 253 ms 4212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 105 ms 4304 KB Output is correct
5 Correct 407 ms 4216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 4216 KB Output is correct
2 Correct 253 ms 4212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 105 ms 4304 KB Output is correct
5 Correct 407 ms 4216 KB Output is correct
6 Correct 397 ms 4216 KB Output is correct
7 Correct 349 ms 4216 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 291 ms 4216 KB Output is correct
11 Correct 115 ms 4340 KB Output is correct
12 Correct 405 ms 4220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 6992 KB Output is correct
2 Correct 92 ms 7004 KB Output is correct
3 Correct 87 ms 7048 KB Output is correct
4 Correct 84 ms 7096 KB Output is correct
5 Correct 89 ms 5784 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 862 ms 7980 KB Output is correct
8 Correct 868 ms 9448 KB Output is correct
9 Correct 98 ms 7464 KB Output is correct
10 Correct 410 ms 8840 KB Output is correct
11 Correct 254 ms 9316 KB Output is correct
12 Correct 373 ms 10028 KB Output is correct
13 Correct 409 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 6992 KB Output is correct
2 Correct 92 ms 7004 KB Output is correct
3 Correct 87 ms 7048 KB Output is correct
4 Correct 84 ms 7096 KB Output is correct
5 Correct 89 ms 5784 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 332 ms 9524 KB Output is correct
8 Correct 327 ms 12460 KB Output is correct
9 Correct 91 ms 6940 KB Output is correct
10 Correct 253 ms 12304 KB Output is correct
11 Correct 284 ms 12412 KB Output is correct
12 Correct 277 ms 12608 KB Output is correct
13 Correct 226 ms 12436 KB Output is correct
14 Correct 186 ms 4216 KB Output is correct
15 Correct 253 ms 4212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 105 ms 4304 KB Output is correct
18 Correct 407 ms 4216 KB Output is correct
19 Correct 397 ms 4216 KB Output is correct
20 Correct 349 ms 4216 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 291 ms 4216 KB Output is correct
24 Correct 115 ms 4340 KB Output is correct
25 Correct 405 ms 4220 KB Output is correct
26 Correct 862 ms 7980 KB Output is correct
27 Correct 868 ms 9448 KB Output is correct
28 Correct 98 ms 7464 KB Output is correct
29 Correct 410 ms 8840 KB Output is correct
30 Correct 254 ms 9316 KB Output is correct
31 Correct 373 ms 10028 KB Output is correct
32 Correct 409 ms 9048 KB Output is correct
33 Correct 1876 ms 15388 KB Output is correct
34 Correct 1820 ms 15336 KB Output is correct
35 Correct 1084 ms 14700 KB Output is correct
36 Correct 662 ms 15668 KB Output is correct
37 Correct 720 ms 15560 KB Output is correct
38 Correct 821 ms 14580 KB Output is correct