結果

問題 No.366 ロボットソート
ユーザー jjjj
提出日時 2016-09-30 23:21:13
言語 Fortran
(gFortran 13.2.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,625 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 32,228 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 15:30:00
合計ジャッジ時間 1,838 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

program main
  implicit none
  integer::N,K,i,j,l,m,total
  logical::sortable
  integer,allocatable::a(:),b(:),sorta(:),sortb(:)
  data total/0/
  read *,N,K
  allocate(a(N),sorta(N),b(N),sortb(N))
  read *,a
  sorta = qsort(a)
  sortable = check_sortable(a,sorta, N, K)
  if(sortable.eqv..false.) then
     print '(i0)',-1
     return
  end if

!  print *, a
!  print *, sorta
  do i=1,MIN(K,N)
     b     = (/(a(j),    j=i,N,K)/)
     sortb = (/(sorta(j),j=i,N,K)/)

!     print *,b
!     print *,sortb
     do j=1, (N+K-1)/K
        do l=j,(N+K-1)/K
           if(sortb(j).eq.b(l))exit
        end do
        do m=l,j+1,-1
           b(m) = b(m-1)
           total = total + 1
        end do
        b(j) = sortb(j)
     end do
  end do
  print '(i0)',total
  return
contains
  function check_sortable(a,sorta, N, K) result(sortable)
    integer,intent(in)::N,K
    integer,allocatable,intent(in)::a(:),sorta(:)
    logical::sortable
    do i=1,N,K
       sortable = .false.
       do j=MOD(i,K),N,K
          if(a(j).eq.sorta(i)) then
             sortable = .true.
             exit
          end if
       end do
       if(sortable .eqv. .false.) then
          return
       end if
    end do
  end function check_sortable
  recursive function qsort(x) result(y)
    integer,intent(in) ::x(:)
    integer,allocatable::y(:)
    integer::pivot,total
    total = size(x)
    if (total <=1) then
       y = x
    else
       pivot = x(total/2)
       y = [qsort(pack(x, x .lt. pivot)), &
            pack(x, x .eq. pivot),        &
            qsort(pack(x, x .gt. pivot))]
    endif
  end function qsort
end program main
0