답안 #396366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396366 2021-04-29T20:46:09 Z 4fecta Road Construction (JOI21_road_construction) C++17
5 / 100
10000 ms 1637600 KB
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)

const int MN = 250005;

int n, k, perm[MN], rev[MN];
pii a[MN];

int cost(pii p, pii q) {
    return abs(p.f - q.f) + abs(p.s - q.s);
}

bool cmp(pii p, pii q) {
    return cost(a[p.f], a[p.s]) > cost(a[q.f], a[q.s]);
}

bool cmp1(int p, int q) {
    if (a[p].s != a[q].s) return a[p].s < a[q].s;
    return a[p].f < a[q].f;
}

int32_t main() {
    boost();

    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i].f >> a[i].s, perm[i] = i;
    sort(perm + 1, perm + n + 1, cmp1);
    for (int i = 1; i <= n; i++) rev[perm[i]] = i;
    //for (int i = 1; i <= n; i++) printf("%d\n", perm[i]);
    if (n <= 1000) {
        vector<int> v;
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                v.push_back(cost(a[i], a[j]));
            }
        }
        sort(v.begin(), v.end());
        for (int i = 1; i <= k; i++) printf("%lld\n", v[i - 1]);
        return 0;
    }
    sort(a + 1, a + n + 1);
    priority_queue<pii, vector<pii>, decltype(&cmp)> q(cmp);
    set<pii> vis;
    for (int gap = 1; gap <= 75; gap++) {
        for (int i = gap + 1; i <= n; i++) q.push({i - gap, i}), vis.insert({i - gap, i});
    }
    for (int gap = 1; gap <= 100; gap++) {
        for (int i = gap + 1; i <= n; i++) if (!vis.count({min(perm[i - gap], perm[i]), max(perm[i - gap], perm[i])})) q.push({perm[i - gap], perm[i]}), vis.insert({min(perm[i - gap], perm[i]), max(perm[i - gap], perm[i])});
    }
    while (q.size() && k--) {
        pii i = q.top(); q.pop();
        printf("%lld\n", cost(a[i.f], a[i.s]));
        int id1 = rev[i.f], id2 = rev[i.s];
        if (abs(id1 - id2) == 1 && abs(i.f - i.s) > 1) continue;
        if (i.s < n && !vis.count({i.f, i.s + 1})) q.push({i.f, i.s + 1}), vis.insert({i.f, i.s + 1});
        if (i.f > 1 && !vis.count({i.f - 1, i.s})) q.push({i.f - 1, i.s}), vis.insert({i.f - 1, i.s});
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 6980 KB Output is correct
2 Correct 81 ms 6968 KB Output is correct
3 Correct 57 ms 5048 KB Output is correct
4 Correct 55 ms 5064 KB Output is correct
5 Correct 75 ms 5828 KB Output is correct
6 Correct 24 ms 4548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10221 ms 1637600 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10190 ms 1613576 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10190 ms 1613576 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 6980 KB Output is correct
2 Correct 81 ms 6968 KB Output is correct
3 Correct 57 ms 5048 KB Output is correct
4 Correct 55 ms 5064 KB Output is correct
5 Correct 75 ms 5828 KB Output is correct
6 Correct 24 ms 4548 KB Output is correct
7 Execution timed out 10074 ms 919064 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 6980 KB Output is correct
2 Correct 81 ms 6968 KB Output is correct
3 Correct 57 ms 5048 KB Output is correct
4 Correct 55 ms 5064 KB Output is correct
5 Correct 75 ms 5828 KB Output is correct
6 Correct 24 ms 4548 KB Output is correct
7 Execution timed out 10221 ms 1637600 KB Time limit exceeded
8 Halted 0 ms 0 KB -