답안 #999628

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999628 2024-06-16T01:23:17 Z biank 봉쇄 시간 (IOI23_closing) C++17
17 / 100
106 ms 28516 KB
#include <bits/stdc++.h>

using namespace std;

#define sz(x) int(x.size())
#define all(x) begin(x), end(x)

using ll = long long;
using ii = pair<int, int>;

const ll INF = 1e18;
const int MAX_N = 2e5 + 9;

vector<ii> adj[MAX_N];
int n;

void dfs(int u, vector<ll> &dist, int p = -1, ll d = 0) {
    dist[u] = d;
    for (auto [v, w] : adj[u]) {
        if (v != p) dfs(v, dist, u, d + w);
    }
}

int non_overlaping(ll K, vector<ll> v) {
    sort(all(v));
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (v[i] <= K) {
            K -= v[i];
            ans++;
        }
    }
    return ans;
}

int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
    n = N;
    for (int i = 0; i < n; i++) adj[i].clear(); 
    for (int i = 0; i < n - 1; i++) {
        adj[U[i]].emplace_back(V[i], W[i]);
        adj[V[i]].emplace_back(U[i], W[i]);
    }
    vector<ll> dist1(n), dist2(n);
    dfs(X, dist1), dfs(Y, dist2);
    vector<bool> path(n, false);
    path[Y] = true;
    int u = Y;
    while (u != X) {
        for (auto [v, w] : adj[u]) {
            if (dist1[v] + w == dist1[u]) {
                u = v;
                break;
            }
        }
        path[u] = true;
    }
    for (int i = 0; i < n; i++) {
        if (dist1[i] > dist2[i]) swap(dist1[i], dist2[i]);
    }
    vector<ll> vec1;
    vector<pair<ll, ll>> vec2;
    ll remain = K;
    int curr = 0;
    for (int i = 0; i < n; i++) {
        ll val1 = dist1[i];
        ll val2 = dist2[i] - dist1[i];
        if (path[i]) {
            remain -= val1, curr++;
            vec1.push_back(val2);
        } else if (val1 <= val2) {
            vec1.push_back(val1);
            vec1.push_back(val2);
        } else {
            vec2.emplace_back(val1, val2);
        }
    }
    sort(all(vec1));
    sort(all(vec2), [](const pair<ll, ll> &a, const pair<ll, ll> &b) {
        return a.first + a.second < b.first + b.second;
    });
    int M = sz(vec2);
    vector<ll> prefSum(M + 1);
    prefSum[0] = 0LL;
    for (int i = 0; i < M; i++) {
        prefSum[i + 1] = vec2[i].first + vec2[i].second;
        prefSum[i + 1] += prefSum[i];
    }
    vector<ll> prefMaxi(M + 1);
    prefMaxi[0] = -INF;
    for (int i = 0; i < M; i++) {
        prefMaxi[i + 1] = vec2[i].second;
        prefMaxi[i + 1] = max(prefMaxi[i + 1], prefMaxi[i]);
    }
    vector<ll> suffMini(M + 1);
    suffMini[M] = INF;
    for (int i = M - 1; i >= 0; i--) {
        suffMini[i] = vec2[i].first;
        suffMini[i] = min(suffMini[i], suffMini[i + 1]);
    }
    auto calc = [&]() {
        if (remain < 0) return int(-1e9);
        auto it = upper_bound(all(prefSum), remain);
        assert(it != prefSum.begin()); it--;
        ll diff = remain - *it;
        int idx = int(it - prefSum.begin());
        int cant = curr + 2 * idx;
        if (diff >= suffMini[idx + 1]) return cant + 1;
        it++;
        if (it != prefSum.end()) {
            diff = remain - *it + prefMaxi[idx + 1];
            if (diff >= 0) return cant + 1;
        }
        return cant;
    };
    int ans = max(non_overlaping(K, dist1), calc());
    for (int i = 0; i < sz(vec1); i++) {
        remain -= vec1[i], curr++;
        ans = max(ans, calc());
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 28084 KB Output is correct
2 Correct 102 ms 28516 KB Output is correct
3 Correct 58 ms 7512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '171', found: '172'
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '171', found: '172'
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Incorrect 1 ms 4956 KB 1st lines differ - on the 1st token, expected: '6', found: '7'
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '171', found: '172'
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '171', found: '172'
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '171', found: '172'
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '171', found: '172'
16 Halted 0 ms 0 KB -