結果
| 問題 |
No.1816 MUL-DIV Game
|
| コンテスト | |
| ユーザー |
👑 Kazun
|
| 提出日時 | 2022-01-21 21:29:53 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 560 ms / 2,000 ms |
| コード長 | 3,636 bytes |
| コンパイル時間 | 263 ms |
| コンパイル使用メモリ | 81,880 KB |
| 実行使用メモリ | 100,712 KB |
| 最終ジャッジ日時 | 2024-11-25 22:44:37 |
| 合計ジャッジ時間 | 7,556 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
"""参考元
https://tsubo.hatenablog.jp/entry/2020/06/15/124657
"""
import heapq
class Heap_Dict:
def __init__(self, mode=True):
""" Heap Dict クラスの作成.
Mode: True → 最小値, False → 最大値
"""
self.heap=[]
self.dict={}
self.__mode=bool(mode)
self.__length=0
def __bool__(self):
return bool(self.heap)
def __len__(self):
return self.__length
def __contains__(self, x):
return self.is_exist(x)
def insert(self, x):
""" 要素 x を追加する. """
if self.__mode and not self.is_exist(x):
heapq.heappush(self.heap,x)
elif not self.__mode and not self.is_exist(x):
heapq.heappush(self.heap,-x)
if x in self.dict:
self.dict[x]+=1
else:
self.dict[x]=1
self.__length+=1
def erase(self, x):
""" x を消す (存在しない場合は KeyError) . """
if x not in self:
raise KeyError(x)
self.dict[x]-=1
self.__length-=1
while self.heap:
y=self.heap[0]
if not self.__mode:y=-y
if self.dict[y]==0:
heapq.heappop(self.heap)
else:
break
def discard(self, x):
""" x を消す (存在しない場合は何もしない). """
if x not in self:
return
self.dict[x]-=1
self.__length-=1
while self.heap:
y=self.heap[0]
if not self.__mode:y=-y
if self.dict[y]==0:
heapq.heappop(self.heap)
else:
break
def is_exist(self, x):
""" キューに x が存在するかどうかを判定する. """
return bool(self.dict.get(x,0))
def get_min(self, default=float("inf")):
""" キューにある最小値を返す.
※ Mode=True でないと使えない.
"""
assert self.__mode
if self.heap:
return self.heap[0]
else:
return default
def pop_min(self):
""" キューにある最小値を取り出す.
※ Mode=True でないと使えない.
"""
assert self.__mode and bool(self)
x=self.get_min()
self.erase(x)
return x
def get_max(self, default=-float("inf")):
""" キューにある最大値を返す.
※ Mode=False でないと使えない.
"""
assert not self.__mode
if self.heap:
return -self.heap[0]
else:
return default
def pop_max(self):
""" キューにある最小値を取り出す.
※ Mode = False でないと使えない.
"""
assert (not self.__mode) and bool(self)
x=self.get_max()
self.erase(x)
return x
def count(self, x):
""" x の個数を求める. """
return self.dict.get(x,0)
#==================================================
N=int(input())
A=list(map(int,input().split()))
if N==1:
X=A[0]
elif N%2==1:
X=1
else:
H1=Heap_Dict(1)
H2=Heap_Dict(0)
for a in A:
H1.insert(a)
H2.insert(a)
for i in range(N-1):
if i%2==0:
p=H1.pop_min()
q=H1.pop_min()
H1.insert(p*q)
H2.discard(p); H2.discard(q)
H2.insert(p*q)
else:
p=H2.pop_max()
q=H2.pop_max()
H2.insert(1)
H1.discard(p); H1.discard(q)
H1.insert(1)
X=H1.pop_min()
print(X)
Kazun