답안 #842512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
842512 2023-09-03T02:03:49 Z I_love_Hoang_Yen 봉쇄 시간 (IOI23_closing) C++17
43 / 100
164 ms 42880 KB
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Graph = vector<vector<pair<int,ll>>>;

void dfs(int u, int fu, const Graph& g, vector<ll>& dists) {
    for (auto [v, w] : g[u]) {
        if (v == fu) continue;
        assert(dists[v] < 0);

        dists[v] = dists[u] + w;
        dfs(v, u, g, dists);
    }
}

vector<ll> get_dists(int u, const Graph& g) {
    int n = g.size();
    vector<ll> dists(n, -1);
    dists[u] = 0;

    dfs(u, -1, g, dists);
    return dists;
}

int no_common(ll k, const vector<ll>& dx, const vector<ll>& dy) {
    vector<ll> all;
    for (auto d : dx) all.push_back(d);
    for (auto d : dy) all.push_back(d);
    sort(all.begin(), all.end());
    int cnt = 0;
    for (auto d : all) {
        if (d <= k) {
            k -= d;
            ++cnt;
        } else break;
    }
    return cnt;
}

// linear graph
struct Event {
    ll cost;
    int i;
    int typ;  // 1 = SINGLE, 2 = PAIR
};
bool operator < (const Event& a, const Event& b) {
    return a.cost * (3 - a.typ) < b.cost * (3 - b.typ);
}

int sub4(int n, ll k, int x, int y,
        const vector<ll>& dx, const vector<ll>& dy) { // x < y
    int res = no_common(k, dx, dy);

    // 0 .. [xl .. x .. xr]
    //               [yl .. y .. yr] .. n-1
    // Now we only consider the case where [xl, xr] and [yl, yr] overlap
    // They must overlap at least one point between [x, y]

    int cur = 0;
    vector<Event> events;
    // 1. In [0, x], every point can be visited by x then y
    for (int i = 0; i < x; ++i) {
        if (dy[i] - dx[i] >= dx[i]) {
            events.push_back({dx[i], i, 1});
            events.push_back({dy[i] - dx[i], i, 1});
        } else {
            // When dy[i] - dx[i] < dx[i], if we sort normally, we will
            // choose (dy[i] - dx[i]) before dx[i], which is wrong
            // -> we must "bundle" these together
            events.push_back({dy[i], i, 2});
        }
    }
    // 2. In [x, y], every point must be visited by either x or y
    for (int i = x; i <= y; ++i) {
        k -= min(dx[i], dy[i]);
        // Additionally, we can choose to visit from other point
        ++cur;
        events.push_back({llabs(dx[i] - dy[i]), i, 1});
    }
    // 3. In [y, n-1]: same as (1) {{{
    for (int i = y+1; i < n; ++i) {
        if (dx[i] - dy[i] >= dy[i]) {
            events.push_back({dy[i], i, 1});
            events.push_back({dx[i] - dy[i], i, 1});
        } else {
            events.push_back({dx[i], i, 2});
        }
    }
    // }}}

    if (k >= 0) {
        sort(events.begin(), events.end());
        for (auto& event : events) {
            if (event.cost > k) break;
            k -= event.cost;
            cur += event.typ;
            event.typ = 0;
        }

        // Break "bundle"
        set<ll> costs;
        for (auto& event : events) {
            if (event.typ == 2) {
                costs.insert(min(dx[event.i], dy[event.i]));
            }
        }
        for (auto cost : costs) {
            if (k >= cost) {
                k -= cost;
                ++cur;
            }
        }
        res = max(res, cur);
    }
    return res;
}

int max_score(int n, int X, int Y, long long K,
              vector<int> U, vector<int> V, vector<int> W) {
    Graph g(n);
    assert(int(U.size()) == n-1);
    assert(int(V.size()) == n-1);
    assert(int(W.size()) == n-1);
    for (int i = 0; i < n - 1; i++) {
        int u = U[i];
        int v = V[i];
        int w = W[i];
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    if (X > Y) swap(X, Y);

    auto distsX = get_dists(X, g);
    auto distsY = get_dists(Y, g);
    
    return sub4(n, K, X, Y, distsX, distsY);
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 36676 KB Output is correct
2 Correct 142 ms 42880 KB Output is correct
3 Correct 77 ms 2896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 500 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 500 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 856 KB Output is correct
27 Correct 2 ms 856 KB Output is correct
28 Correct 2 ms 856 KB Output is correct
29 Correct 2 ms 856 KB Output is correct
30 Correct 2 ms 1112 KB Output is correct
31 Correct 2 ms 860 KB Output is correct
32 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -