Submission #1099223

# Submission time Handle Problem Language Result Execution time Memory
1099223 2024-10-10T20:14:47 Z f0rizen Džumbus (COCI19_dzumbus) C++17
110 / 110
61 ms 20976 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;

template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
    for (auto &i : a) {
        is >> i;
    }
    return is;
}

vector<vector<int>> gg;
vector<bool> used;

void dfs(int v) {
    used[v] = 1;
    for (auto u : gg[v]) {
        if (!used[u]) {
            dfs(u);
        }
    }
}

vector<int> a;
vector<vector<int>> g;
vector<int> sz;
vector<vector<vector<ll>>> dp;

void dfs_dp(int v, int p = -1) {
    sz[v] = 1;
    for (auto u : g[v]) {
        if (u != p) {
            dfs_dp(u, v);
            sz[v] += sz[u];
        }
    }
    dp[v].resize(2, vector<ll>(3, infll));
    dp[v][0][0] = 0;
    dp[v][0][1] = a[v];
    int cur = 1;
    for (auto u : g[v]) {
        if (u != p) {
            vector<vector<ll>> dpp(cur + sz[u] + 1, vector<ll>(3, infll));
            for (int i = 0; i <= cur; ++i) {
                for (int j = 0; j <= sz[u]; ++j) {
                    dpp[i + j][0] = min(dpp[i + j][0], dp[v][i][0] + dp[u][j][0]);
                    dpp[i + j][0] = min(dpp[i + j][0], dp[v][i][0] + dp[u][j][1]);
                    dpp[i + j][0] = min(dpp[i + j][0], dp[v][i][0] + dp[u][j][2]);
                    dpp[i + j][1] = min(dpp[i + j][1], dp[v][i][1] + dp[u][j][0]);
                    dpp[i + j][2] = min(dpp[i + j][2], dp[v][i][2] + dp[u][j][0]);
                    if (i + j + 1 <= cur + sz[u]) {
                        dpp[i + j + 1][2] = min(dpp[i + j + 1][2], dp[v][i][2] + dp[u][j][1]);
                    }
                    dpp[i + j][2] = min(dpp[i + j][2], dp[v][i][2] + dp[u][j][2]);
                    if (i + j + 2 <= cur + sz[u]) {
                        dpp[i + j + 2][2] = min(dpp[i + j + 2][2], dp[v][i][1] + dp[u][j][1]);
                    }
                    if (i + j + 1 <= cur + sz[u]) {
                        dpp[i + j + 1][2] = min(dpp[i + j + 1][2], dp[v][i][1] + dp[u][j][2]);
                    }
                }
            }
            dp[v] = dpp;
            cur += sz[u];
        }
    }
}

int32_t main() {
#ifdef LOCAL
    freopen("/tmp/input.txt", "r", stdin);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int n, m;
    cin >> n >> m;
    a.resize(n + 1);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int root = n;
    a[root] = inf;
    gg.resize(n);
    g.resize(n + 1);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        gg[u].push_back(v);
        gg[v].push_back(u);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    used.resize(n);
    for (int i = 0; i < n; ++i) {
        if (!used[i]) {
            dfs(i);
            g[root].push_back(i);
            g[i].push_back(root);
        }
    }
    sz.resize(n + 1);
    dp.resize(n + 1);
    dfs_dp(root);
    vector<ll> val(n + 1);
    for (int i = 0; i <= n; ++i) {
        val[i] = min({dp[root][i][0], dp[root][i][1], dp[root][i][2]});
    }
    for (int i = n - 1; i >= 0; --i) {
        val[i] = min(val[i], val[i + 1]);
    }
    int q;
    cin >> q;
    while (q--) {
        int s;
        cin >> s;
        int l = 0, r = n + 1;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (val[mid] <= s) {
                l = mid;
            } else {
                r = mid;
            }
        }
        cout << l << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 15964 KB Output is correct
2 Correct 35 ms 18516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 15964 KB Output is correct
2 Correct 35 ms 18516 KB Output is correct
3 Correct 61 ms 20976 KB Output is correct
4 Correct 59 ms 16212 KB Output is correct
5 Correct 55 ms 14932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1192 KB Output is correct
2 Correct 27 ms 1112 KB Output is correct
3 Correct 25 ms 792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 15964 KB Output is correct
2 Correct 35 ms 18516 KB Output is correct
3 Correct 61 ms 20976 KB Output is correct
4 Correct 59 ms 16212 KB Output is correct
5 Correct 55 ms 14932 KB Output is correct
6 Correct 24 ms 1192 KB Output is correct
7 Correct 27 ms 1112 KB Output is correct
8 Correct 25 ms 792 KB Output is correct
9 Correct 34 ms 2388 KB Output is correct
10 Correct 41 ms 1948 KB Output is correct
11 Correct 50 ms 1576 KB Output is correct