結果
問題 |
No.4 おもりと天秤
|
ユーザー |
![]() |
提出日時 | 2016-07-23 19:13:57 |
言語 | Fortran (gFortran 14.2.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,547 bytes |
コンパイル時間 | 449 ms |
コンパイル使用メモリ | 33,920 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-06 15:10:34 |
合計ジャッジ時間 | 1,404 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 WA * 13 |
ソースコード
! dynamic programing with memo ! recursive for remain_capacity ! dp_memo(cap, indx) recursive function rc_dp(W, capacity, indx, N, dp_memo) result(total_weight) implicit none integer,intent(in) ::W(:) integer::total_weight,capacity,indx, N integer,allocatable::dp_memo(:,:) if(dp_memo(capacity, indx).ne.-1) then total_weight = dp_memo(capacity, indx) else if(capacity.lt.W(indx)) then total_weight = rc_dp(W, capacity, indx+1, N, dp_memo) dp_memo(capacity, indx) = total_weight else total_weight = MAX(rc_dp(W, capacity , indx+1, N, dp_memo), & rc_dp(W, capacity-W(indx), indx+1, N, dp_memo)+W(indx) ) dp_memo(capacity, indx) = total_weight end if end function rc_dp program main implicit none interface recursive function rc_dp(W, capacity, indx, N, dp_memo) result(total_weight) implicit none integer,intent(in) ::W(:) integer::total_weight, capacity,indx, N integer,allocatable::dp_memo(:,:) end function rc_dp end interface integer::N,total,i,j,k, total_weight integer,allocatable::W(:) integer,allocatable::dp_memo(:,:) read *, N allocate(W(N)) read *, W(1:N) total = SUM(W) if(MOD(total,2).eq.1) then print '(a)','impossible' return end if allocate(dp_memo(total/2,N+1)) dp_memo(:,1:N)=-1 dp_memo(:,N+1)=0 total_weight = rc_dp(W, total/2, 1, N, dp_memo) if(total_weight.eq.total/2) then print '(a)','possible' else print '(a)','impossible' end if end program main