Submission #1208485

#TimeUsernameProblemLanguageResultExecution timeMemory
1208485NeltClosing Time (IOI23_closing)C++20
75 / 100
1068 ms2162688 KiB
#include "closing.h"
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const ll N = 2e5 + 5;
vector<pair<ll, ll>> g[N];
void dfs(ll v, ll par, vector<ll> &depth)
{
    for (auto [to, w] : g[v]) 
        if (to != par)
        {
            depth[to] = depth[v] + w;
            dfs(to, v, depth);
        }
}
vector<ll> getpath(ll x, ll y, vector<ll> &d)
{
    vector<ll> path;
    while (x != y and path.size() <= d.size())
    {
        path.push_back(x);
        for (auto [i, w] : g[x]) if (d[i] < d[x])
        {
            x = i;
            break;
        }
    }
    path.push_back(y);
    return path;
}
ll n;
int max_score(int N, int X, int Y, long long k,
              std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
    n = N;
    for (ll i = 0; i < n; i++) g[i].clear();
    for (ll i = 0; i + 1 < n; i++) g[U[i]].push_back(make_pair(V[i], W[i])), g[V[i]].push_back(make_pair(U[i], W[i]));
    vector<ll> a(n), b(n);
    dfs(X, -1, a);
    dfs(Y, -1, b);
    ll ans = 0, ptr = n - 1, sum = 0;
    vector<ll> path = getpath(X, Y, b);
    a.insert(a.begin(), 0);
    b.insert(b.begin(), 0);
    bool onpath[n + 1];
    for (ll i = 1; i <= n; i++) onpath[i] = 0;
    for (ll i : path) i++, onpath[i] = 1, k -= min(a[i], b[i]);
    ll dp[n + 1][2 * n + 1];
    for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= 2 * n; j++) dp[i][j] = 1e18 + 1;
    dp[0][0] = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 0; j <= 2 * n; j++)
            dp[i][j] = min(dp[i][j], dp[i - 1][j]);
        for (ll j = 0; j <= 2 * n; j++)
        {
            if (j + 1 <= 2 * n)
                dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + (onpath[i] ? 0 : min(a[i], b[i])));
            if (j + 2 <= 2 * n)
                dp[i][j + 2] = min(dp[i][j + 2], dp[i - 1][j] + (onpath[i] ? abs(a[i] - b[i]) : max(a[i], b[i])));
        }
    }
    for (ll j = 0; j <= 2 * n; j++) if (dp[n][j] <= k) ans = j;
    a.erase(a.begin());
    b.erase(b.begin());
    for (ll i = 0; i < n; i++) sum += b[i];
    for (ll i : path) k += min(a[i], b[i]);
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (sum <= k) ans = max(ans, n);
    for (ll i = 0; i < n; i++)
    {
        sum += a[i];
        while (ptr >= 0 and sum > k) sum -= b[ptr--];
        if (sum <= k) ans = max(ans, ptr + 1 + i + 1);
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...