結果

問題 No.4 おもりと天秤
ユーザー jjjj
提出日時 2016-07-23 19:19:51
言語 Fortran
(gFortran 13.2.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,484 bytes
コンパイル時間 1,210 ms
コンパイル使用メモリ 28,444 KB
実行使用メモリ 4,552 KB
最終ジャッジ日時 2023-09-08 16:47:22
合計ジャッジ時間 2,093 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 3 ms
4,552 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 6 ms
4,384 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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(0:total/2,N+1))
  dp_memo(:,1:N)=-1
  dp_memo(:,N+1)=0
  dp_memo(0,:)  =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
0