結果

問題 No.2523 Trick Flower
ユーザー kwm_tkwm_t
提出日時 2023-10-29 19:37:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 4,000 ms
コード長 2,130 bytes
コンパイル時間 4,790 ms
コンパイル使用メモリ 272,208 KB
実行使用メモリ 36,264 KB
最終ジャッジ日時 2023-10-29 19:38:03
合計ジャッジ時間 10,392 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 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 3 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 173 ms
36,264 KB
testcase_16 AC 158 ms
30,456 KB
testcase_17 AC 55 ms
18,256 KB
testcase_18 AC 58 ms
20,424 KB
testcase_19 AC 107 ms
20,936 KB
testcase_20 AC 110 ms
20,932 KB
testcase_21 AC 107 ms
20,940 KB
testcase_22 AC 104 ms
20,936 KB
testcase_23 AC 109 ms
20,932 KB
testcase_24 AC 59 ms
13,500 KB
testcase_25 AC 109 ms
19,068 KB
testcase_26 AC 92 ms
18,324 KB
testcase_27 AC 103 ms
20,148 KB
testcase_28 AC 83 ms
17,180 KB
testcase_29 AC 20 ms
6,624 KB
testcase_30 AC 73 ms
15,328 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 43 ms
10,504 KB
testcase_33 AC 105 ms
20,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint998244353;
//const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
vector<vector<int>> f(vector<vector<int>>&scc, vector<vector<int>>&to) {
	int n = to.size();
	int m = scc.size();
	vector<int>v(n);
	rep(i, m)rep(j, scc[i].size()) v[scc[i][j]] = i;
	vector<vector<int>>res(m);
	rep(i, m) {
		for (int j : scc[i]) {
			for (int k : to[j]) {
				int l = v[k];
				if (i == l)continue;
				res[i].push_back(l);
			}
		}
		std::sort(res[i].begin(), res[i].end());
		res[i].erase(unique(res[i].begin(), res[i].end()), res[i].end());
	}
	return res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	vector<long long>a(n), b(n), c(n);
	rep(i, n)cin >> a[i];
	rep(i, n)cin >> b[i];
	scc_graph g(n);
	vector<vector<int>>to(n);
	rep(i, n) {
		int c; cin >> c; c--;
		to[c].push_back(i);
		g.add_edge(c, i);
	}
	auto scc = g.scc();
	int sz = scc.size();
	to = f(scc, to);
	long long ok = 0;
	long long ng = accumulate(all(a), 0LL) / accumulate(all(b), 0LL) + 1;
	while (1 < abs(ok - ng)) {
		long long mid = (ng + ok) / 2;
		auto check = [&]() {
			vector<long long>dp(sz);
			rrep(i, sz) {
				for (int j : to[i]) if (dp[j] < 0) dp[i] += dp[j], dp[j] = 0;
				for (int j : scc[i]) dp[i] += a[j] - mid * b[j];
			}
			return all_of(all(dp), [](long long x) { return x >= 0; });
		};
		if (check()) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
	return 0;
}
0