結果

問題 No.2523 Trick Flower
ユーザー kotatsugamekotatsugame
提出日時 2023-10-27 22:48:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 4,000 ms
コード長 1,267 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 74,600 KB
実行使用メモリ 12,000 KB
最終ジャッジ日時 2023-10-27 22:48:22
合計ジャッジ時間 2,951 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,572 KB
testcase_01 AC 2 ms
5,572 KB
testcase_02 AC 2 ms
5,572 KB
testcase_03 AC 2 ms
5,572 KB
testcase_04 AC 2 ms
5,572 KB
testcase_05 AC 2 ms
5,572 KB
testcase_06 AC 2 ms
5,572 KB
testcase_07 AC 1 ms
5,580 KB
testcase_08 AC 2 ms
5,596 KB
testcase_09 AC 2 ms
5,592 KB
testcase_10 AC 2 ms
5,584 KB
testcase_11 AC 2 ms
5,584 KB
testcase_12 AC 2 ms
5,584 KB
testcase_13 AC 2 ms
5,592 KB
testcase_14 AC 2 ms
5,588 KB
testcase_15 AC 54 ms
6,664 KB
testcase_16 AC 51 ms
6,664 KB
testcase_17 AC 45 ms
6,188 KB
testcase_18 AC 40 ms
12,000 KB
testcase_19 AC 46 ms
6,576 KB
testcase_20 AC 47 ms
6,576 KB
testcase_21 AC 47 ms
6,576 KB
testcase_22 AC 47 ms
6,576 KB
testcase_23 AC 47 ms
6,576 KB
testcase_24 AC 23 ms
6,208 KB
testcase_25 AC 40 ms
6,488 KB
testcase_26 AC 39 ms
6,464 KB
testcase_27 AC 43 ms
6,540 KB
testcase_28 AC 34 ms
6,432 KB
testcase_29 AC 9 ms
5,864 KB
testcase_30 AC 29 ms
6,376 KB
testcase_31 AC 3 ms
5,680 KB
testcase_32 AC 17 ms
6,064 KB
testcase_33 AC 47 ms
6,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
using namespace std;
int N,A[1<<17],B[1<<17],C[1<<17];
long need[1<<17];
int deg[1<<17];
bool vis[1<<17];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N;
	for(int i=0;i<N;i++)cin>>A[i];
	for(int i=0;i<N;i++)cin>>B[i];
	for(int i=0;i<N;i++)
	{
		cin>>C[i],C[i]--;
		deg[C[i]]++;
	}
	vector<int>Q;
	for(int i=0;i<N;i++)if(!deg[i])Q.push_back(i);
	for(int i=0;i<Q.size();i++)
	{
		int u=Q[i];
		vis[u]=true;
		if(!--deg[C[u]])Q.push_back(C[u]);
	}
	vector<vector<int> >Lp;
	for(int i=0;i<N;i++)if(!vis[i])
	{
		int u=i;
		Lp.push_back(vector<int>());
		while(!vis[u])
		{
			Lp.back().push_back(u);
			vis[u]=true;
			u=C[u];
		}
	}
	long L=0,R=1e15;
	while(R-L>1)
	{
		long k=(L+R)/2;
		bool out=false;
		for(int i=0;i<N;i++)
		{
			if(B[i]>(long)1e15/k)
			{
				out=true;
				break;
			}
			need[i]=k*B[i];
		}
		if(out)
		{
			R=k;
			continue;
		}
		for(int u:Q)
		{
			need[C[u]]+=max(need[u]-A[u],0L);
			if(need[C[u]]>(long)1e15)out=true;
		}
		for(int i=0;i<Lp.size();i++)
		{
			long sN=0,sA=0;
			for(int u:Lp[i])
			{
				sN+=need[u];
				sA+=A[u];
				if(sN>(long)1e15)break;
			}
			if(sN>sA)
			{
				out=true;
				break;
			}
		}
		if(out)R=k;
		else L=k;
	}
	cout<<L<<endl;
}
0