結果

問題 No.4 おもりと天秤
ユーザー jjjj
提出日時 2016-07-23 19:13:57
言語 Fortran
(gFortran 13.2.0)
結果
WA  
実行時間 -
コード長 1,547 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 33,612 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 07:50:50
合計ジャッジ時間 1,265 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

! 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
0