답안 #590822

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
590822 2022-07-06T11:46:09 Z nguyen31hoang08minh2003 Džumbus (COCI19_dzumbus) C++14
0 / 110
32 ms 2316 KB
/*
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|
| / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / |
|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |  /|\  |
| \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ | / | \ |
|  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|/  |  \|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
*/
#include <bits/stdc++.h>
#define fore(i, a, b) for (signed i = (a), i##_last = (b); i < i##_last; ++i)
#define fort(i, a, b) for (signed i = (a), i##_last = (b); i <= i##_last; ++i)
#define ford(i, a, b) for (signed i = (a), i##_last = (b); i >= i##_last; --i)
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double;

template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;};

typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

const int maxN = 1005;
const ll inf = 0x3f3f3f3f3f3f3f3f;

vi adj[maxN];
ll cost[maxN];
bool seen[maxN];
int n, m, r, q, s, d[maxN], a[maxN], b[maxN];
set<ii> p;
set<array<int, 3> > pq;

void expand(const vi &v) {
    cost[r + sz(v)] = cost[r];
    r += sz(v);
    for (const int &u : v) {
        seen[u] = true;
        p.erase({d[u], u});
        cost[r] += d[u];
    }
    for (const int &x : v)
        for (const int &y : adj[x]) {
            if (seen[y])
                continue;
            p.insert({d[y], y});
        }
}

array<int, 3> getData() {
    array<int, 3> res;
    auto x = p.begin();
    res[0] = x -> first;
    res[1] = x -> second;
    ++x;
    res[0] += (x -> first);
    res[2] = x -> second;
    return res;
}

void solve() {
    cin >> n >> m;
    fort(i, 1, n) {
        cin >> d[i];
        cost[i] = inf;
    }
    fort(i, 1, m) {
        cin >> a[i] >> b[i];
        pq.insert({d[a[i]] + d[b[i]], a[i], b[i]});
        adj[a[i]].pb(b[i]);
        adj[b[i]].pb(a[i]);
    }
    cost[n + 1] = inf;
    while (!pq.empty() || p.size() >= 2) {
        if (pq.empty()) {
            const auto [w, u, v] = getData();
            expand({u, v});
        } else {
            const auto [w1, x, y] = *pq.begin();
            if (seen[x] || seen[y]) {
                pq.erase(pq.begin());
                continue;
            }
            if (p.size() <= 1) {
                pq.erase(pq.begin());
                expand({x, y});
            } else {
                const auto [w2, u, v] = getData();
                if (w1 <= w2) {
                    pq.erase(pq.begin());
                    expand({x, y});
                } else
                    expand({u, v});
            }
        }
        if (!p.empty())
            mini(cost[r + 1], cost[r] + (p.begin() -> fi));
    }
    ford(i, n, 0)
        mini(cost[i], cost[i + 1]);
    cin >> q;
    for (int lo, hi, mid; q--;) {
        cin >> s;
        lo = 0;
        hi = n + 1;
        while (lo + 1 < hi) {
            mid = lo + hi >> 1;
            if (cost[mid] <= s)
                lo = mid;
            else
                hi = mid;
        }
        cout << lo << '\n';
    }
}

int main() {
    #ifdef LOCAL
        freopen("input.INP", "r", stdin);
    #endif // LOCAL
    cin.tie(0) -> sync_with_stdio(0);
    cout.tie(0);
    solve();
    return 0;
}

Compilation message

dzumbus.cpp: In function 'void solve()':
dzumbus.cpp:88:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   88 |             const auto [w, u, v] = getData();
      |                        ^
dzumbus.cpp:91:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |             const auto [w1, x, y] = *pq.begin();
      |                        ^
dzumbus.cpp:100:28: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  100 |                 const auto [w2, u, v] = getData();
      |                            ^
dzumbus.cpp:119:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  119 |             mid = lo + hi >> 1;
      |                   ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 32 ms 2316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -