답안 #1080701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1080701 2024-08-29T12:59:30 Z shmax 봉쇄 시간 (IOI23_closing) C++17
0 / 100
1000 ms 609156 KB
#include "closing.h"
#include "bits/stdc++.h"

using namespace std;
using i32 = int;
#define int long long
#define len(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define inf 1000'000'000'000'000'000LL
#define bit(x, i) (((x) >> i) & 1)


template<typename T>
using vec = vector<T>;

i32 max_score(i32 N, i32 X, i32 Y, int K, std::vector<i32> U, std::vector<i32> V, std::vector<i32> W) {
    i32 ans = 0;
    vec<vec<pair<int, int>>> g(N);
    for (int i = 0; i < len(U); i++) {
        g[U[i]].emplace_back(V[i], W[i]);
        g[V[i]].emplace_back(U[i], W[i]);
    }
    auto dijkstra = [&](int start) {
        priority_queue<pair<int, int>, vec<pair<int, int>>, greater<>> pq;
        pq.emplace(0, start);
        vec<int> dists(N, inf);
        dists[start] = 0;
        while (!pq.empty()) {
            auto [cost, v] = pq.top();
            pq.pop();
            if (cost != dists[v]) continue;
            for (auto [u, w]: g[v]) {
                if (dists[u] > dists[v] + w) {
                    dists[u] = dists[v] + w;
                    pq.emplace(dists[u], u);
                }
            }
        }
        return dists;
    };
    auto dX = dijkstra(X);
    auto dY = dijkstra(Y);
    vec<int> rg(dX);
    for (auto h: dY) rg.push_back(h);
    sort(all(rg));
    int best = 0;
    int tK = K;
    for (int x: rg) {
        if (tK < x) break;
        tK -= x;
        best++;
    }
    {
        int curK = K;
        int cnt = 0;
        vec<int> to_add;
        for (int i = X; i <= Y; i++) {
            curK -= min(dX[i], dY[i]);
            cnt++;
            to_add.push_back(max(dX[i], dY[i]) - min(dX[i], dY[i]));
        }
        for (int i = 0; i < X; i++) {
            to_add.push_back(min(dX[i], dY[i]));
        }
        for (int i = Y + 1; i < N; i++) {
            to_add.push_back(min(dX[i], dY[i]));
        }
        if (curK >= 0) {
            sort(all(to_add));
            for (int x: to_add) {
                if (curK < x) break;
                curK -= x;
                cnt++;
            }
            best = max(best, cnt);
        }
    }

    {
        int curK = K;
        int cnt = 0;
        vec<int> to_add;
        for (int i = X; i <= Y; i++) {
            curK -= min(dX[i], dY[i]);
            cnt++;
            if (dX[i] >= dY[i]) {
                to_add.push_back(max(dX[i], dY[i]) - min(dX[i], dY[i]));
            } else {
                curK -= max(dX[i], dY[i]) - min(dX[i], dY[i]);
                cnt++;
            }
        }
        for (int i = Y + 1; i < N; i++) {
            to_add.push_back(min(dX[i], dY[i]));
        }
        for (int i = X; i >= 0; i--) {
            int tcnt = cnt;
            int tcurK = curK;
            if (curK < 0) break;
            for (int j = i - 1; j >= 0; j--) {
                to_add.push_back(dX[j]);
            }
            sort(all(to_add));
            for (int x: to_add) {
                if (tcurK < x) break;
                tcurK -= x;
                tcnt++;
            }
            best = max(best, tcnt);
            cnt += 2;
            curK -= dY[i];
        }
        if (curK >= 0) {
            sort(all(to_add));
            for (int x: to_add) {
                if (curK < x) break;
                curK -= x;
                cnt++;
            }
            best = max(best, cnt);
        }
    }
    {
        int curK = K;
        int cnt = 0;
        vec<int> to_add;
        for (int i = X; i <= Y; i++) {
            curK -= min(dX[i], dY[i]);
            cnt++;
            if (dX[i] <= dY[i]) {
                to_add.push_back(max(dX[i], dY[i]) - min(dX[i], dY[i]));
            } else {
                curK -= max(dX[i], dY[i]) - min(dX[i], dY[i]);
                cnt++;
            }
        }
        for (int i = 0; i < X; i++) {
            to_add.push_back(min(dX[i], dY[i]));
        }
        for (int i = Y + 1; i < N; i++) {
            int tcnt = cnt;
            int tcurK = curK;
            if (curK < 0) break;
            for (int j = i + 1; j < N; j++) {
                to_add.push_back(dY[j]);
            }
            sort(all(to_add));
            for (int x: to_add) {
                if (tcurK < x) break;
                tcurK -= x;
                tcnt++;
            }
            best = max(best, tcnt);
            cnt += 2;
            curK -= dX[i];
        }
        if (curK >= 0) {
            sort(all(to_add));
            for (int x: to_add) {
                if (curK < x) break;
                curK -= x;
                cnt++;
            }
            best = max(best, cnt);
        }
    }
    {
        int curK = K;
        int cnt = 0;
        vec<int> to_add;
        for (int i = X; i <= Y; i++) {
            curK -= max(dX[i], dY[i]);
            cnt += 2;
        }
        set<int> used;
        vec<pair<int, int>> tgr;
        for (int i = 0; i < X; i++) {
            tgr.emplace_back(max(dX[i], dY[i]), i);
        }
        for (int i = Y + 1; i < N; i++) {
            tgr.emplace_back(max(dX[i], dY[i]), i);
        }
        sort(all(tgr));
        for (auto [t, id]: tgr) {
            used.insert(id);
            for (int i = 0; i < N; i++) {
                if (i >= X and i <= Y) continue;
                if (used.count(i)) continue;
                to_add.push_back(min(dX[i], dY[i]));
            }

            if (curK >= 0) {
                sort(all(to_add));
                for (int x: to_add) {
                    if (curK < x) break;
                    curK -= x;
                    cnt++;
                }
                best = max(best, cnt);
            }
            curK -= t;
            cnt += 2;
        }
        if (curK >= 0) {
            best = max(best, cnt);
        }
    }

    return best;
}

Compilation message

closing.cpp: In function 'i32 max_score(i32, i32, i32, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:17:9: warning: unused variable 'ans' [-Wunused-variable]
   17 |     i32 ans = 0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1105 ms 609156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '30', found: '33'
4 Halted 0 ms 0 KB -