何も見ずに解いたもの。
もっともクラスを使えばもっとスマートにかけるのかもしれないですが。
一応辞書という意味ではこんな感じになるのかなと。
N,K = map(int,input().split())
A = dict(input().split() for _ in range(N))
for _ in range(K):
command = input().split()
if command[0] == 'call':
print(A[command[1]])
elif command[0] == 'leave':
del A[command[1]]
elif command[0] == 'join':
A[command[1]] = command[2]
extendとsort,そしてindexというのがポイントですかね
N,X,P = map(int,input().split())
A = [int(input()) for _ in range(N)]
A.extend([X,P])
A.sort(reverse=False)
print(A.index(P)+1)
#上2行を1行にするなら
print(sorted(A).index(P)+1)
たぶん通常ならこれなんですが
queryってことは違うんですよね
現にケース4だと10秒かかってたので
N,K,P = map(int,input().split())
A = [int(input()) for _ in range(N)]
A.append(P)
for _ in range(K):
command = input().split()
if command[0] == 'join':
A.append(int(command[1]))
else:
print(sorted(A).index(P) + 1)
他にいい案も見つからないので答えを見ると
「背の低い順に並ぶとき、順番が変わるのはPaizaくんより背が低い人が入った場合」だということで、そのときだけ順番を+1してやればいいと。
なるほど!
じゃあその視点でやってみます
N,K,P = map(int,input().split())
A = [int(input()) for _ in range(N)]
A.append(P)
#あらかじめ並べて答えを取得しておく
Ans = sorted(A).index(P) + 1
for _ in range(K):
command = input().split()
if command[0] == 'join':
if int(command[1]) < P:
Ans += 1
continue
else:
print(Ans)
こんな感じかな。
これでやってみるとケース4は0.25秒でした。
やったね!
でも上のはifが多すぎるので減らせないか考えます
N,K,P = map(int,input().split())
A = [int(input()) for _ in range(N)]
A.append(P)
#あらかじめ並べて答えを取得しておく
Ans = sorted(A).index(P) + 1
for _ in range(K):
command = input().split()
if command[0] == 'sorting':
print(Ans)
continue
#普通ならelseにするが、continueでスキップさせられる
#これでifを深くしなくてすむ
if int(command[1]) < P:
Ans += 1
ちなみに時間は0.25秒、同じでしたが
シンプルになって読みやすくなりました。
ということで。queryって難しいのかなと思ったのですが、
ちょっと考え方を変えるだけで計算回数も少なくなるよということですね。
下のとあわせて復習しておきたいところですね。
今日は以上です。