Submission #7342776


Source Code Expand

module mod_linked_list
  implicit none

  type t_node
    private
    integer :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type t_node

  type t_linked_list
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: add_first => add_first
    procedure :: add_last => add_last
    procedure :: poll_first => poll_first
    procedure :: poll_last => poll_last
    procedure :: peek_first => peek_first
    procedure :: peek_last => peek_last
    procedure :: clear => clear
    procedure :: get => get
    procedure :: remove => remove
    procedure :: replace => replace
    procedure :: first_index_of => first_index_of
    procedure :: last_index_of => last_index_of
    procedure :: size => size_of
  end type t_linked_list

contains

  function new_node(item) result(node)
    integer, intent(in) :: item
    type(t_node), pointer :: node
    allocate(node)
    node%item = item
  end

  subroutine add_first(this,item)
    class(t_linked_list), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node
    node => new_node(item)
    if (associated(this%tail)) then
      node%next => this%head
      this%head%prev => node
    else
      this%tail => node
    end if
    this%head => node
    this%num = this%num+1
  end

  subroutine add_last(this,item)
    class(t_linked_list), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node
    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end

  function poll_first(this) result(item)
    class(t_linked_list), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node
    item = this%head%item
    node => this%head%next
    deallocate(this%head)
    this%head => node
    if (associated(node)) then
      node%prev => null()
    else
      this%tail => null()
    end if
    this%num = this%num-1
  end

  function poll_last(this) result(item)
    class(t_linked_list), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node
    item = this%tail%item
    node => this%tail%prev
    deallocate(this%tail)
    this%tail => node
    if (associated(node)) then
      node%next => null()
    else
      this%head => null()
    end if
    this%num = this%num-1
  end

  function peek_first(this) result(item)
    class(t_linked_list), intent(in) :: this
    integer :: item
    item = this%head%item
  end

  function peek_last(this) result(item)
    class(t_linked_list), intent(in) :: this
    integer :: item
    item = this%tail%item
  end

  subroutine clear(this)
    class(t_linked_list), intent(inout) :: this
    type(t_node), pointer :: node, next
    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%next))
      next => node%next
      deallocate(node)
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end

  function get(this,i) result(item)
    class(t_linked_list), intent(in) :: this
    integer, intent(in) :: i
    integer :: item, idx
    type(t_node), pointer :: node
    if (i <= (this%num+1)/2) then
      idx = 1
      node => this%head
      do while (idx < i)
        node => node%next
        idx = idx+1
      end do
    else
      idx = this%num
      node => this%tail
      do while (idx > i)
        node => node%prev
        idx = idx-1
      end do
    end if
    item = node%item
  end

  function remove(this,i) result(item)
    class(t_linked_list), intent(inout) :: this
    integer, intent(in) :: i
    integer :: item, idx
    type(t_node), pointer :: node
    if (i == 1) then
      item = poll_first(this)
      return
    end if
    if (i == this%num) then
      item = poll_last(this)
      return
    end if
    if (i <= (this%num+1)/2) then
      idx = 1
      node => this%head
      do while (idx < i)
        node => node%next
        idx = idx+1
      end do
    else
      idx = this%num
      node => this%tail
      do while (idx > i)
        node => node%prev
        idx = idx-1
      end do
    end if
    item = node%item
    node%prev%next => node%next
    node%next%prev => node%prev
    deallocate(node)
    this%num = this%num-1
  end

  subroutine replace(this,i,item)
    class(t_linked_list), intent(inout) :: this
    integer, intent(in) :: i, item
    integer :: idx
    type(t_node), pointer :: node
    if (i <= (this%num+1)/2) then
      idx = 1
      node => this%head
      do while (idx < i)
        node => node%next
        idx = idx+1
      end do
    else
      idx = this%num
      node => this%tail
      do while (idx > i)
        node => node%prev
        idx = idx-1
      end do
    end if
    node%item = item
  end

  subroutine show_all(this)
    type(t_linked_list), intent(in) :: this
    type(t_node), pointer :: node, next
    if (.not.associated(this%head)) return
    node => this%head
    write(*,'(i0)',advance='no') node%item
    do while (associated(node%next))
      node => node%next
      write(*,'(x,i0)',advance='no') node%item
    end do
    write(*,*)
  end

  function first_index_of(this,item) result(idx)
    class(t_linked_list), intent(in) :: this
    integer, intent(in) :: item
    integer :: idx
    type(t_node), pointer :: node
    idx = 1
    node => this%head
    do while (associated(node))
      if (node%item == item) return
      node => node%next
      idx = idx+1
    end do
    idx = -1
  end

  function last_index_of(this,item) result(idx)
    class(t_linked_list), intent(in) :: this
    integer, intent(in) :: item
    integer :: idx
    type(t_node), pointer :: node
    idx = this%num
    node => this%tail
    do while (associated(node))
      if (node%item == item) return
      node => node%prev
      idx = idx-1
    end do
    idx = -1
  end

  integer function size_of(this)
    class(t_linked_list), intent(in) :: this
    size_of = this%num
  end

end module mod_linked_list

program tokaido
  use mod_linked_list
  implicit none
  integer :: n, m, a(200000) = 0, x, s = 0, d, i, j
  integer :: dp(0:1000000) = 0, tmp
  type(t_linked_list) :: deque, tmp1, tmp2
  read(*,*) n
  read(*,*) a(1:n-1)
  if (n > 3) s = sum(a(3:n-1))
  d = a(1)-a(2)
  do i = 0, s-1
    call deque%add_last(i)
  end do
  do i = 3, n-1
    do j = 0, a(i)
      call tmp1%add_last(deque%peek_first())
      call tmp2%add_last(deque%poll_first())
    end do
    do j = 0, a(i)
      call deque%add_first(tmp1%poll_last())
    end do
    tmp = tmp2%poll_first()
    do j = 1, a(i)
      call deque%add_first(tmp2%poll_first())
      tmp = deque%poll_last()
    end do
  end do
  do i = 0, s-1
    dp(i) = deque%poll_first()
  end do
  read(*,*) m
  do i = 1, m
    read(*,*) x
    if (x >= s) then
      write(*,'(i0)') x-s+d
      cycle
    end if
    write(*,'(i0)') dp(x)+d
  end do
end program tokaido

Submission Info

Submission Time
Task H - Tokaido
User ue1221
Language Fortran (gfortran v4.8.4)
Score 0
Code Size 7365 Byte
Status RE
Exec Time 433 ms
Memory 66136 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 0 / 700 0 / 900
Status
AC × 2
AC × 16
RE × 4
AC × 32
RE × 7
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt
dataset2 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 02-15.txt, 02-16.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 252 ms 36608 KB
01-02.txt AC 1 ms 2304 KB
01-03.txt AC 186 ms 35584 KB
01-04.txt AC 224 ms 35968 KB
01-05.txt AC 255 ms 36608 KB
01-06.txt AC 252 ms 36608 KB
01-07.txt AC 211 ms 35712 KB
01-08.txt AC 254 ms 36608 KB
01-09.txt AC 254 ms 36608 KB
01-10.txt AC 195 ms 49280 KB
01-11.txt AC 219 ms 64000 KB
01-12.txt AC 258 ms 59520 KB
01-13.txt AC 257 ms 56576 KB
01-14.txt AC 263 ms 61568 KB
01-15.txt AC 264 ms 61568 KB
01-16.txt RE 245 ms 66136 KB
01-17.txt RE 129 ms 3672 KB
01-18.txt RE 130 ms 3672 KB
01-19.txt RE 130 ms 3676 KB
02-01.txt AC 222 ms 35840 KB
02-02.txt AC 433 ms 38528 KB
02-03.txt AC 388 ms 36992 KB
02-04.txt AC 389 ms 36992 KB
02-05.txt AC 329 ms 36352 KB
02-06.txt AC 388 ms 36992 KB
02-07.txt AC 392 ms 37120 KB
02-08.txt AC 359 ms 56832 KB
02-09.txt AC 363 ms 50304 KB
02-10.txt AC 392 ms 65024 KB
02-11.txt AC 418 ms 61184 KB
02-12.txt AC 417 ms 61568 KB
02-13.txt AC 418 ms 61568 KB
02-14.txt RE 236 ms 66136 KB
02-15.txt RE 130 ms 3672 KB
02-16.txt RE 130 ms 3672 KB
sample-01.txt AC 1 ms 2304 KB
sample-02.txt AC 1 ms 2304 KB