結果

問題 No.2523 Trick Flower
ユーザー KKT89KKT89
提出日時 2023-10-27 22:11:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 4,000 ms
コード長 2,305 bytes
コンパイル時間 2,848 ms
コンパイル使用メモリ 225,408 KB
実行使用メモリ 13,244 KB
最終ジャッジ日時 2023-10-27 22:11:18
合計ジャッジ時間 5,457 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 104 ms
12,828 KB
testcase_16 AC 105 ms
12,828 KB
testcase_17 AC 50 ms
13,244 KB
testcase_18 AC 46 ms
12,448 KB
testcase_19 AC 80 ms
12,984 KB
testcase_20 AC 92 ms
12,984 KB
testcase_21 AC 84 ms
12,984 KB
testcase_22 AC 80 ms
12,984 KB
testcase_23 AC 84 ms
12,984 KB
testcase_24 AC 44 ms
9,080 KB
testcase_25 AC 73 ms
12,096 KB
testcase_26 AC 69 ms
11,568 KB
testcase_27 AC 79 ms
12,572 KB
testcase_28 AC 64 ms
10,936 KB
testcase_29 AC 14 ms
5,264 KB
testcase_30 AC 55 ms
10,116 KB
testcase_31 AC 5 ms
4,348 KB
testcase_32 AC 30 ms
7,380 KB
testcase_33 AC 82 ms
12,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> a(n), b(n), c(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
    }
    vector<vector<int>> g(n);
    vector<int> deg(n);
    for (int i = 0; i < n; ++i) {
        cin >> c[i]; c[i] -= 1;
        deg[c[i]] += 1;
        g[i].push_back(c[i]);
    }

    vector<int> odr, cyc;
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (deg[i] == 0) {
            q.push(i);
        }
    }
    while (q.size()) {
        int s = q.front(); q.pop();
        odr.push_back(s);
        for (int t : g[s]) {
            deg[t] -= 1;
            if (deg[t] == 0) {
                q.push(t);
            }
        }
    }

    auto check = [&](ll x) -> bool {
        auto cp = a;
        vector<bool> used(n);
        for (int i : odr) {
            used[i] = true;
            if (b[i] > 1e17/x) return false;
            ll need = b[i]*x;
            if (cp[i] >= need) continue;
            ll dif = need - cp[i];
            cp[c[i]] -= dif;
            if (cp[c[i]] < -(ll)(2e9)*(ll)n) return false;
        }
        for (int i = 0; i < n; ++i) {
            if (used[i]) continue;
            ll sa = 0, sb = 0;
            int j = i;
            while (!used[j]) {
                used[j] = true;
                sa += cp[j]; sb += b[j];
                j = c[j];
            }
//            cout << x << " " << sa << " " << sb << endl;
            if (sa < 0) return false;
            if (sb == 0) continue;
            if (sa/sb < x) return false;
        }
        return true;
    };

    ll l = 0, r = 1e18;
    while (r-l > 1) {
        ll mid = (l+r)/2;
        if (check(mid)) {
            l = mid;
        }
        else {
            r = mid;
        }
    }
    cout << l << endl;
}
0