結果
問題 | No.1027 U+1F4A0 |
ユーザー |
|
提出日時 | 2020-04-17 21:47:26 |
言語 | Fortran (gFortran 14.2.0) |
結果 |
AC
|
実行時間 | 1 ms / 2,000 ms |
コード長 | 33,007 bytes |
コンパイル時間 | 2,144 ms |
コンパイル使用メモリ | 39,936 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-03 11:58:40 |
合計ジャッジ時間 | 2,971 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
module module_sortimplicit noneinterface sortmodule procedure int_heap_sort, real_heap_sortend interfacecontainssubroutine int_heap_down(a,node,las)implicit noneinteger(8) a(*),xinteger(8) node,par,ch1,ch2,laspar = nodex = a(par); ch1 = par*2do while(ch1 <= las)ch2 = ch1+1if(ch2 <= las .and. a(ch1) < a(ch2)) ch1 = ch2if(a(ch1) > x) thena(par) = a(ch1); a(ch1) = xelseexitend ifpar = ch1; ch1 = par*2end doend subroutine int_heap_downsubroutine int_heap_sort(a,n)implicit noneinteger(8) a(*),xinteger(8) n,ido i = n/2, 1, -1call int_heap_down(a,i,n)end dodo i=n,2,-1x = a(i); a(i) = a(1); a(1) = xcall int_heap_down(a,1_8,i-1)end doend subroutine int_heap_sortsubroutine real_heap_down(a,node,las)implicit nonereal a(*), xinteger(8) node,par,ch1,ch2,laspar = nodex = a(par); ch1 = par*2do while(ch1 <= las)ch2 = ch1+1if(ch2 <= las .and. a(ch1) < a(ch2)) ch1 = ch2if(a(ch1) > x) thena(par) = a(ch1); a(ch1) = xelseexitend ifpar = ch1; ch1 = par*2end doend subroutine real_heap_downsubroutine real_heap_sort(a,n)implicit nonereal a(*),xinteger(8) n,ido i = n/2, 1, -1call real_heap_down(a,i,n)end dodo i=n,2,-1x = a(i); a(i) = a(1); a(1) = xcall real_heap_down(a,1_8,i-1)end doend subroutine real_heap_sortend module module_sortmodule module_dequeimplicit noneprivatetype deque_nodetype(deque_node) ,pointer:: prev => null(),next => null()integer(8) valend type deque_nodetype,public:: dequetype(deque_node) ,pointer:: first => null(),last => null(), ptr => null()integer(8):: size=0containsprocedure:: push_back => deque_push_backprocedure:: push_front => deque_push_frontprocedure:: pop_back => deque_pop_backprocedure:: pop_front => deque_pop_frontend type dequecontainssubroutine deque_push_back(self,val)class(deque)::selfinteger(8)::valif(self%size == 0) thenallocate(self%first)self%last => self%firstself%first%val = valself%size = 1elseallocate(self%last%next)self%last%next%prev => self%lastself%last => self%last%nextself%last%val = valself%size = self%size+1end ifend subroutine deque_push_backsubroutine deque_push_front(self,val)class(deque)::selfinteger(8)::valif(self%size == 0) thenallocate(self%first)self%last => self%firstself%first%val = valself%size = 1elseallocate(self%first%prev)self%first%prev%next => self%firstself%first => self%first%prevself%first%val = valself%size = self%size+1end ifend subroutine deque_push_frontsubroutine deque_pop_back(self)class(deque)::selfif(self%size == 0) thenreturnelse if(self%size == 1) thenself%size = 0deallocate(self%last)self%last => null(); self%first => null()elseself%last => self%last%prevdeallocate(self%last%next)self%last%next => null()self%size = self%size-1end ifend subroutine deque_pop_backsubroutine deque_pop_front(self)class(deque)::selfif(self%size == 0) thenreturnelse if(self%size == 1) thenself%size = 0deallocate(self%first)self%last => null(); self%first => null()elseself%first => self%first%nextdeallocate(self%first%prev)self%first%prev => null()self%size = self%size-1end ifend subroutine deque_pop_frontend module module_dequemodule module_RedBlackTreeimplicit noneprivateinteger(8),parameter :: red = 1,black = 0type RBT_Int_nodetype(RBT_Int_node),pointer :: par=>null(), left => null(),right => null()integer(8) :: keyinteger(8) :: val=0integer(8) :: color=redend type RBT_Int_nodetype RBT_Char_nodetype(RBT_Char_node),pointer :: par=>null(), left => null(),right => null()character(100) :: keyinteger(8) :: val=0integer(8) :: color=redend type RBT_Char_nodetype(RBT_Int_node) ,pointer,save :: rbt_int_niltype(RBT_Char_node) ,pointer,save :: rbt_char_niltype,public:: RedBlackTree_Inttype(RBT_Int_node),pointer :: root => null()integer(8) :: size = 0containsprocedure:: insert => Int_Insertprocedure:: get_val => Int_Getprocedure:: find => Int_Findprocedure:: add => Int_Addprocedure:: FixUp => Int_FixUpend type RedBlackTree_Inttype,public:: RedBlackTree_Chartype(RBT_Char_node),pointer :: root => null()integer(8) :: size = 0containsprocedure:: insert => Char_Insertprocedure:: get_val => Char_Getprocedure:: find => Char_Findprocedure:: add => Char_Addprocedure:: FixUp => Char_FixUpend type RedBlackTree_Charcontainssubroutine Int_Init()implicit noneif(.not.associated(rbt_int_nil)) thenallocate(rbt_int_nil)rbt_int_nil%color = blackend ifend subroutine Int_Initrecursive function Int_Get(self,key) result(val)implicit noneclass(RedBlackTree_Int), intent(in) :: selfinteger(8) :: keyinteger(8) :: valtype(RBT_Int_node),pointer :: uif(.not.associated(rbt_int_nil)) thenallocate(rbt_int_nil)rbt_int_nil%color = blackend ifif(self%size == 0) thenval = -1returnend ifu => Int_SearchTree(self%root,key)val = u%valreturnend function Int_Getrecursive function Int_Find(self,key) result(res)implicit noneclass(RedBlackTree_Int), intent(in) :: selfinteger(8) :: keylogical :: restype(RBT_Int_node),pointer :: uif(.not.associated(rbt_int_nil)) thenallocate(rbt_int_nil)rbt_int_nil%color = blackend ifif(self%size == 0) thenres = .false.returnend ifu => Int_SearchTree(self%root,key)res = (.not.associated(u,rbt_int_nil))returnend function Int_Findrecursive subroutine Int_Add(self,key,val)implicit noneclass(RedBlackTree_Int), intent(inout) :: selfinteger(8) :: keytype(RBT_Int_node),pointer :: uinteger(8) :: valif(.not.associated(rbt_int_nil)) thenallocate(rbt_int_nil)rbt_int_nil%color = blackend ifif(self%size == 0) thencall self%insert(key,val)returnend ifu => Int_SearchTree(self%root,key)if (associated(u,rbt_int_nil)) thencall self%insert(key,val)elseu%val = u%val+valend ifreturnend subroutine Int_Addrecursive function Int_SearchTree(u,key) result(res)implicit nonetype(RBT_Int_node), pointer :: u,resinteger(8) :: keyif(associated(u,rbt_int_nil)) thenres => rbt_int_nilreturnend ifif(key < u%key) thenres => Int_SearchTree(u%left,key)returnelse if(key > u%key) thenres => Int_SearchTree(u%right,key)returnelseres => ureturnend ifend function Int_SearchTreesubroutine Int_Insert(self,key,val)implicit noneclass(RedBlackTree_Int),intent(inout):: selfinteger(8),intent(in) :: keyinteger(8),intent(in) :: valtype(RBT_Int_node),pointer,save :: now, uif(.not.associated(rbt_int_nil)) thenallocate(rbt_int_nil)rbt_int_nil%color = blackend if!allocate new RBT_Int_nodeallocate(u)u%key = key; u%val = valu%left => rbt_int_nil; u%right => rbt_int_nil!insert new RBT_Int_nodeif(self%size == 0) thenu%left => rbt_int_nil; u%right => rbt_int_nil; u%par => rbt_int_nilself%root => uself%root%color = blackself%size = 1returnelsenow => self%rootif(.not.Insert_RBT_Int_node(u,now)) returnend if!Fix Treecall self%FixUp(u)self%size = self%size+1end subroutine Int_Insertrecursive function Insert_RBT_Int_node(u,now) result(added)implicit nonetype(RBT_Int_node), pointer:: u,nowlogical :: addedif(u%key < now%key) thenif(associated(now%left,rbt_int_nil)) thennow%left => uu%par => nowadded = .true.elsenow => now%leftadded = Insert_RBT_Int_node(u,now)end ifreturnelse if(u%key > now%key) thenif(associated(now%right,rbt_int_nil)) thennow%right => uu%par => nowadded = .true.elsenow => now%rightadded = Insert_RBT_Int_node(u,now)end ifreturnelseadded = .false.returnend ifend function Insert_RBT_Int_nodesubroutine Int_FixUp(self,u)implicit noneclass(RedBlackTree_Int),intent(inout) :: selftype(RBT_Int_node),pointer,intent(inout) :: utype(RBT_Int_node),pointer :: w,gif(.not.associated(rbt_int_nil)) thenallocate(rbt_int_nil)rbt_int_nil%color = blackend ifnullify(w,g)do while(u%color == red)if(u%key == self%root%key) thenu%color = blackreturnend ifw => u%parif(w%left%color == black) thencall Int_FripLeft(w,self%root)u => ww => u%parend ifif(w%color == black) returng => w%parif(g%right%color == black) thencall Int_FripRight(g,self%root)returnelsecall Int_PushBlack(g)u => gend ifend doend subroutine Int_FixUpsubroutine Int_PushBlack(u)implicit nonetype(RBT_Int_node), pointer:: uu%color = red; u%left%color = black; u%right%color = black;end subroutine Int_PushBlacksubroutine Int_PullBlack(u)implicit nonetype(RBT_Int_node), pointer:: uu%color = black; u%left%color = red; u%right%color = red;end subroutine Int_PullBlacksubroutine Int_FripLeft(u,root)implicit nonetype(RBT_Int_node), pointer, intent(inout) :: roottype(RBT_Int_node), pointer :: u,winteger(8) :: tmptmp = u%color; u%color = u%right%color; u%right%color = tmpw => u%rightw%par => u%parif(.not.associated(u%par,rbt_int_nil)) thenif(associated(w%par%left,u)) thenw%par%left=>welsew%par%right=>wend ifend ifu%right => w%leftif(.not.associated(u%right,rbt_int_nil))u%right%par => uu%par => ww%left => uif(associated(u,root)) thenroot => wroot%par => rbt_int_nilend ifend subroutine Int_FripLeftsubroutine Int_FripRight(u,root)implicit nonetype(RBT_Int_node), pointer,intent(inout):: roottype(RBT_Int_node), pointer :: u,winteger(8) :: tmptmp = u%color; u%color = u%left%color; u%left%color = tmpw => u%leftw%par => u%parif(.not.associated(u%par,rbt_int_nil)) thenif(associated(w%par%left,u)) thenw%par%left=>welsew%par%right=>wend ifend ifu%left => w%rightif(.not.associated(u%left,rbt_int_nil))u%left%par => uu%par => ww%right => uif(associated(u,root)) thenroot => wroot%par => rbt_int_nilend ifend subroutine Int_FripRightsubroutine Char_Init()implicit noneif(.not.associated(rbt_char_nil)) thenallocate(rbt_char_nil)rbt_char_nil%color = blackend ifend subroutine Char_Initrecursive function Char_Get(self,key) result(val)implicit noneclass(RedBlackTree_Char), intent(in) :: selfCharacter(*) :: keyinteger(8) :: valtype(RBT_Char_node),pointer :: uif(.not.associated(rbt_char_nil)) thenallocate(rbt_char_nil)rbt_char_nil%color = blackend ifif(self%size == 0) thenval = -1returnend ifu => Char_SearchTree(self%root,key)val = u%valreturnend function Char_Getrecursive function Char_Find(self,key) result(res)implicit noneclass(RedBlackTree_Char), intent(in) :: selfCharacter(*) :: keylogical :: restype(RBT_Char_node),pointer :: uif(.not.associated(rbt_char_nil)) thenallocate(rbt_char_nil)rbt_char_nil%color = blackend ifif(self%size == 0) thenres = .false.returnend ifu => Char_SearchTree(self%root,key)res = (.not.associated(u,rbt_char_nil))returnend function Char_Findrecursive subroutine Char_Add(self,key,val)implicit noneclass(RedBlackTree_Char), intent(inout) :: selfCharacter(*) :: keytype(RBT_Char_node),pointer :: uinteger(8) :: valif(.not.associated(rbt_char_nil)) thenallocate(rbt_char_nil)rbt_char_nil%color = blackend ifif(self%size == 0) thencall self%insert(key,val)returnend ifu => Char_SearchTree(self%root,key)if (associated(u,rbt_char_nil)) thencall self%insert(key,val)elseu%val = u%val+valend ifreturnend subroutine Char_Addrecursive function Char_SearchTree(u,key) result(res)implicit nonetype(RBT_Char_node), pointer :: u,resCharacter(*) :: keyif(associated(u,rbt_char_nil)) thenres => rbt_char_nilreturnend ifif(key < u%key) thenres => Char_SearchTree(u%left,key)returnelse if(key > u%key) thenres => Char_SearchTree(u%right,key)returnelseres => ureturnend ifend function Char_SearchTreesubroutine Char_Insert(self,key,val)implicit noneclass(RedBlackTree_Char),intent(inout) :: selfCharacter(*),intent(in) :: keyinteger(8),intent(in) :: valtype(RBT_Char_node),pointer,save :: now, uif(.not.associated(rbt_char_nil)) thenallocate(rbt_char_nil)rbt_char_nil%color = blackend if!allocate new RBT_Char_nodeallocate(u)u%key = key; u%val = valu%left => rbt_char_nil; u%right => rbt_char_nil!insert new RBT_Char_nodeif(.not. associated(self%root)) thenu%left => rbt_char_nil; u%right => rbt_char_nil; u%par => rbt_char_nilself%root => uself%root%color = blackself%size = 1returnelsenow => self%rootif(.not.Insert_RBT_Char_node(u,now)) returnend if!Fix Treecall self%FixUp(u)self%size = self%size+1end subroutine Char_Insertrecursive function Insert_RBT_Char_node(u,now) result(added)implicit nonetype(RBT_Char_node), pointer:: u,nowlogical :: addedif(u%key < now%key) thenif(associated(now%left,rbt_char_nil)) thennow%left => uu%par => nowadded = .true.elsenow => now%leftadded = Insert_RBT_Char_node(u,now)end ifreturnelse if(u%key > now%key) thenif(associated(now%right,rbt_char_nil)) thennow%right => uu%par => nowadded = .true.elsenow => now%rightadded = Insert_RBT_Char_node(u,now)end ifreturnelseadded = .false.returnend ifend function Insert_RBT_Char_nodesubroutine Char_FixUp(self,u)implicit noneclass(RedBlackTree_Char),intent(inout) :: selftype(RBT_Char_node),pointer,intent(inout) :: utype(RBT_Char_node),pointer :: w,gif(.not.associated(rbt_char_nil)) thenallocate(rbt_char_nil)rbt_char_nil%color = blackend ifnullify(w,g)do while(u%color == red)if(u%key == self%root%key) thenu%color = blackreturnend ifw => u%parif(w%left%color == black) thencall Char_FripLeft(w,self%root)u => ww => u%parend ifif(w%color == black) returng => w%parif(g%right%color == black) thencall Char_FripRight(g,self%root)returnelsecall Char_PushBlack(g)u => gend ifend doend subroutine Char_FixUpsubroutine Char_PushBlack(u)implicit nonetype(RBT_Char_node), pointer:: uu%color = red; u%left%color = black; u%right%color = black;end subroutine Char_PushBlacksubroutine Char_PullBlack(u)implicit nonetype(RBT_Char_node), pointer:: uu%color = black; u%left%color = red; u%right%color = red;end subroutine Char_PullBlacksubroutine Char_FripLeft(u,root)implicit nonetype(RBT_Char_node), pointer, intent(inout) :: roottype(RBT_Char_node), pointer :: u,winteger(8) :: tmptmp = u%color; u%color = u%right%color; u%right%color = tmpw => u%rightw%par => u%parif(.not.associated(u%par,rbt_char_nil)) thenif(associated(w%par%left,u)) thenw%par%left=>welsew%par%right=>wend ifend ifu%right => w%leftif(.not.associated(u%right,rbt_char_nil))u%right%par => uu%par => ww%left => uif(associated(u,root)) thenroot => wroot%par => rbt_char_nilend ifend subroutine Char_FripLeftsubroutine Char_FripRight(u,root)implicit nonetype(RBT_Char_node), pointer,intent(inout):: roottype(RBT_Char_node), pointer :: u,winteger(8) :: tmptmp = u%color; u%color = u%left%color; u%left%color = tmpw => u%leftw%par => u%parif(.not.associated(u%par,rbt_char_nil)) thenif(associated(w%par%left,u)) thenw%par%left=>welsew%par%right=>wend ifend ifu%left => w%rightif(.not.associated(u%left,rbt_char_nil))u%left%par => uu%par => ww%right => uif(associated(u,root)) thenroot => wroot%par => rbt_char_nilend ifend subroutine Char_FripRightend module module_RedBlackTreemodule module_MinHeapimplicit noneprivatetype MH_nodetype(MH_node),pointer :: par=>null(), left => null(),right => null()integer(8) :: key,valinteger(8) :: sizeend type MH_nodetype(MH_node) ,pointer,save :: heap_niltype,public:: MinHeaptype(MH_node),pointer :: root,lastinteger(8) :: size = 0containsprocedure:: push => MinHeap_Insert_MH_nodeprocedure:: pop => MinHeap_Pop_MH_nodeend type MinHeapcontainssubroutine MinHeap_Up(root,u)implicit nonetype(MH_node),pointer :: root,uinteger(8) :: tmpdo while(.not.associated(root,u))if(u%key < u%par%key) thentmp = u%keyu%key = u%par%keyu%par%key = tmptmp = u%valu%val = u%par%valu%par%val = tmpu => u%parelsereturnend ifend doreturnend subroutine MinHeap_Upsubroutine MinHeap_Down(root)implicit nonetype(MH_node),pointer:: root,uinteger(8) :: tmpu => rootdo while(.not.associated(u%left,heap_nil))if(.not.associated(u%right,heap_nil)) thenif (u%right%key < u%left%key .and. u%right%key < u%key) thentmp = u%keyu%key = u%right%keyu%right%key = tmptmp = u%valu%val = u%right%valu%right%val = tmpu => u%rightcycleend ifend ifif(u%left%key < u%key) thentmp = u%keyu%key = u%left%keyu%left%key = tmptmp = u%valu%val = u%left%valu%left%val = tmpu => u%leftcycleend ifexitend doreturnend subroutine MinHeap_Downsubroutine Update_Size(root,u,is_add)implicit nonetype(MH_node),pointer :: root,ulogical :: is_adddo while(.not.associated(root,u))if(is_add) thenu%size = u%size+1elseu%size = u%size-1end ifu => u%parend doif(is_add) thenu%size = u%size+1elseu%size = u%size-1end ifend subroutine Update_sizesubroutine MinHeap_Insert_MH_node(self,key,val)implicit noneclass(MinHeap) :: selftype(MH_node),pointer :: u,nowinteger(8) :: key,valif(.not.associated(heap_nil)) allocate(heap_nil)u => null()allocate(u)u%key = keyu%val = valu%left => heap_nil; u%right => heap_nilu%size = 1if(self%size == 0) thenself%root => uself%root%par => heap_nilself%size = 1returnend ifnow => self%rootdonow%size = now%size+1if(.not.associated(now%left,heap_nil)) thenif(.not.associated(now%right,heap_nil)) thenif(now%right%size < now%left%size) thennow => now%right; cycleelsenow => now%left; cycleend ifelsenow%right => uu%par => nowexitend ifelsenow%left => uu%par => nowexitend ifend docall MinHeap_Up(self%root,u)self%size = self%size+1end subroutine MinHeap_Insert_MH_nodesubroutine MinHeap_Pop_MH_node(self)implicit noneclass(MinHeap) :: selftype(MH_node),pointer :: u,nowinteger(8) :: tmpif(.not.associated(heap_nil)) allocate(heap_nil)if(self%size == 0) returnnow => self%rootdonow%size = now%size-1if(.not.associated(now%left,heap_nil)) thenif(.not.associated(now%right,heap_nil)) thenif(now%right%size >= now%left%size) thennow => now%right; cycleelsenow => now%left; cycleend ifelseu => now%lefttmp = u%keyu%key = self%root%keyself%root%key = tmptmp = u%valu%val = self%root%valself%root%val = tmpnow%left => heap_nilexitend ifelse if(associated(now,self%root)) thendeallocate(self%root)self%size = 0returnelseu => nowtmp = u%keyu%key = self%root%keyself%root%key = tmptmp = u%valu%val = self%root%valself%root%val = tmpif(associated(now%par%left,now)) thennow%par%left => heap_nilelsenow%par%right => heap_nilend ifexitend ifend dodeallocate(u)self%size = self%size-1call MinHeap_Down(self%root)end subroutine MinHeap_Pop_MH_nodeend module module_MinHeaprecursive function gcd(a,b) result(res)implicit noneinteger(8) :: a,b,resif(a < b) thenres = gcd(b,a)returnend ifif(mod(a,b) == 0) thenres = breturnelseres = gcd(b,mod(a,b))returnend ifend function gcdrecursive function mod_pow(a,b,modulo) result(res)implicit noneinteger(8) :: a,b,modulo,resif(b == 0) thenres = 1returnend ifif(mod(b,2) == 1) thenres = mod(a*mod_pow(a,b-1,modulo),modulo)elseres = mod(mod_pow(a,b/2,modulo)**2,modulo)end ifreturnend function mod_powsubroutine compress(lis,size)use module_sortuse module_RedBlackTreeimplicit noneinteger(8) :: size,resinteger(8) :: lis(*),tmp_lis(size)integer(8) :: itype(RedBlackTree_Int) :: mapdo i = 1, sizecall map%insert(lis(i),0_8)end dotmp_lis(1:size) = lis(1:size)call sort(tmp_lis,size)res = 0call map%add(tmp_lis(1),res)do i = 2, sizeif(tmp_lis(i) /= tmp_lis(i-1)) thenres = res+1call map%add(tmp_lis(i),res)end ifend dodo i = 1, sizelis(i) = map%get_val(lis(i))enddoend subroutine compressmodule globaluse module_sortuse module_dequeuse module_RedBlackTreeuse module_MinHeapimplicit noneend module globalprogram mainuse globalimplicit noneinteger(8) :: D1,D2,ans = 0integer(8) :: x,yread *, D1, D2if(D1 > D2) thenans = 0else if(D1 == D2) thenans = 4else if (D1*2 > D2) thenans = 8else if(D1*2 == D2) thenans = 4elseans = 0end ifprint '(i0)', ansend program main