結果
問題 | No.518 ローマ数字の和 |
ユーザー |
![]() |
提出日時 | 2022-09-22 06:56:32 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 41 ms / 2,000 ms |
コード長 | 2,478 bytes |
コンパイル時間 | 318 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 12,160 KB |
最終ジャッジ日時 | 2024-12-22 04:42:02 |
合計ジャッジ時間 | 2,034 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import bisectimport copyimport decimalimport fractionsimport heapqimport itertoolsimport mathimport randomimport sysimport timefrom collections import Counter,deque,defaultdictfrom functools import lru_cache,reducefrom heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_maxdef _heappush_max(heap,item):heap.append(item)heapq._siftdown_max(heap, 0, len(heap)-1)def _heappushpop_max(heap, item):if heap and item < heap[0]:item, heap[0] = heap[0], itemheapq._siftup_max(heap, 0)return itemfrom math import gcd as GCDread=sys.stdin.readreadline=sys.stdin.readlinereadlines=sys.stdin.readlineswrite=sys.stdout.writeN=int(readline())num=0for R in readline().split():while R:if len(R)>=2 and R[:2] in ("IV","IX","XL","XC","CD","CM"):if R[:2]=="IV":num+=4elif R[:2]=="IX":num+=9elif R[:2]=="XL":num+=40elif R[:2]=="XC":num+=90elif R[:2]=="CD":num+=400elif R[:2]=="CM":num+=900R=R[2:]else:for i in range(len(R)):if R[0]!=R[i]:breakelse:i=len(R)if R[0]=="M":num+=1000*ielif R[0]=="D":num+=500*ielif R[0]=="C":num+=100*ielif R[0]=="L":num+=50*ielif R[0]=="X":num+=10*ielif R[0]=="V":num+=5*ielif R[0]=="I":num+=iR=R[i:]ans_lst=[]if num>=4000:ans_lst.append("ERROR")else:x=num//1000num%=1000if x:ans_lst.append("M"*x)x=num//100num%=100if x==9:ans_lst.append("CM")elif 5<=x<=8:ans_lst.append("D"+"C"*(x-5))elif x==4:ans_lst.append("CD")elif x:ans_lst.append("C"*x)x=num//10num%=10if x==9:ans_lst.append("XC")elif 5<=x<=8:ans_lst.append("L"+"X"*(x-5))elif x==4:ans_lst.append("XL")elif x:ans_lst.append("X"*x)x=numif x==9:ans_lst.append("IX")elif 5<=x<=8:ans_lst.append("V"+"I"*(x-5))elif x==4:ans_lst.append("IV")elif x:ans_lst.append("I"*x)print(*ans_lst,sep="")