답안 #1051076

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1051076 2024-08-09T19:18:34 Z ZanP 봉쇄 시간 (IOI23_closing) C++17
9 / 100
1000 ms 2097152 KB
#include "closing.h"
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;

vector<vector<pair<int, ll>>> pov;
vector<ll> distX, distY, cost1, cost2, q0;
vector<int> level;
vector<pair<ll, int>> q1, q2, q3;

void dfs(int u, int p, ll dist, vector<ll>& distArray)
{
    distArray[u] = dist;
    for (pair<int, ll> pr : pov[u]) {
        int v = pr.first;
        ll w = pr.second;
        if (v == p) { continue; }
        dfs(v, u, dist + w, distArray);
    }
}


int max_score(int N, int X, int Y, ll K, vector <int> U, vector <int> V, vector <int> W)
{
    pov.resize(N);
    distX.resize(N, 0);
    distY.resize(N, 0);
    cost1.resize(N, 0);
    cost2.resize(N, 0);
    level.resize(N, 0);
    q0.resize(N);
    q1.reserve(2 * N);
    q2.reserve(N);
    q3.reserve(N);
    for (int j = 0; j < N - 1; j++)
    {
        pov[U[j]].push_back({ V[j], W[j] });
        pov[V[j]].push_back({ U[j], W[j] });
    }
    dfs(X, X, 0, distX);
    dfs(Y, Y, 0, distY);
    for (int i = 0; i < N; i++)
    {
        cost1[i] = min(distX[i], distY[i]);
        cost2[i] = max(distX[i], distY[i]) - cost1[i];
    }
    // case 1:
    for (int i = 0; i < N; i++) { q0[i] = -cost1[i]; }
    if (q0.size()) { sort(q0.begin(), q0.end()); }
    ll budget = K;
    int ans1 = 0;
    while (q0.size() > 0)
    {
        int p = q0[q0.size() - 1];
        if (p + budget >= 0)
        {
            budget += p;
            q0.pop_back();
            ans1++;
        }
        else { break; }
    }

    // case 2:
    budget = K;
    int ans2 = 0;

    for (int i = 0; i < N; i++) {
        if (distX[i] + distY[i] == distY[X]) {
            budget -= cost1[i];
            if (budget < 0) { return ans1; }
            level[i] = 1;
            q1.push_back({ -(cost2[i]), i });
            ans2++;
        }
        else if (cost1[i] >= cost2[i]) { //MAYBE >=
            q2.push_back({ -(cost1[i] + cost2[i]), i });
            q3.push_back({ -cost1[i], i });
        }
        else {
            q1.push_back({ -(cost1[i]), i });
            q1.push_back({ -(cost2[i]), i });
        }
    }
    if (q1.size())sort(q1.begin(), q1.end());
    if (q2.size())sort(q2.begin(), q2.end());
    if (q3.size())sort(q3.begin(), q3.end());
    while (!q2.empty()) {
        pair<ll,int> p2 = q2[q2.size()-1];
        if (p2.first + budget < 0) { break; }

        if (q1.size() >= 2) {
            pair<ll, int> p1a = q1[q1.size() - 2], p1b = q1[q1.size() - 1];
            if (-p2.first <= -(p1a.first + p1b.first)) {
                budget += p2.first;
                level[p2.second] = 2;
                q2.pop_back();
                ans2 += 2;
                continue;
            }

            budget += p1b.first;
            level[p1b.second]++;
            q1.pop_back();
            ans2 += 1;
            continue;
        }

        budget += p2.first;
        level[p2.second] = 2;
        q2.pop_back();
        ans2 += 2;
        continue;
    }

    while (!q1.empty()) {
        pair<ll, int> p1 = q1[q1.size()-1];
        if (p1.first + budget < 0) { break; }
        budget += p1.first;
        level[p1.second]++;
        q1.pop_back();
        ans2 += 1;
    }

    while (!q3.empty()) {
        pair<ll, int> p3 = q3[q3.size() - 1];
        if (p3.first + budget < 0) { break; }
        if (level[p3.second] != 0) {
            q3.pop_back();
            continue;
        }
        budget += p3.first;
        level[p3.second]++;
        q1.pop_back();
        ans2 += 1;
    }

    return max(ans1, ans2);
}

//  int main() {
//      /*cout << max_score(23, 4, 18, 10, { 0, 1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10, 11, 12, 13, 14, 15, 16,17,18,19,20,21 }, { 3, 3, 3, 4, 5, 8,5,5,14,8,8,10,10,10,18,14,15,15,20,18,22,20 }, { 2, 1, 4, 3, 2, 1,2,2,3,3,1,2,1,6,1,2,1,1,7,2,2,1 });*/

//      //cout << max_score(5, 1, 3, 8,{0,1,2,3}, {1,2,3,4}, {2,2,2,2});
//      cout << max_score(7, 0, 2, 10, { 0, 0, 1, 2, 2, 5 }, { 1, 3, 2, 4, 5, 6 }, { 2, 3, 4, 2, 5, 3 });
//      return 0;
//  }
# 결과 실행 시간 메모리 Grader output
1 Runtime error 910 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 33060 KB Output is correct
2 Correct 91 ms 41096 KB Output is correct
3 Runtime error 938 ms 2097152 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 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 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Execution timed out 1099 ms 348 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Execution timed out 1099 ms 348 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 910 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 910 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 910 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 910 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 910 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -