0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AtCoder Beginner Contest159 D - Banned K (ABC159D) をpythonで解く (countが遅すぎる!)

Last updated at Posted at 2020-06-14

普通にpythonのcountメソッドを使ったらTLEしたので、Counterを使ってみたところAC出来ました。

image.png

以下がCounterを使った回答です。

code.py
import sys
from collections import Counter
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
C=Counter(L)           #Lに含まれる{文字:出現回数}の辞書型で帰ってきます
x=0
for a in C.values():   #値(出現回数)のみを取り出します
  x += int(a*(a-1)/2)  
for K in range(N):
  print(x-C[L[K]]+1)   #すべての同じ数の組み合わせからKを含む組み合わせを引きます

以下がTLEした回答です。ちょっといじれば速くなるのでしょうか?

code.py
import sys
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
C=list(set(L))
x=0
for i in range(len(C)):
  a = L.count(C[i])
  x += int(a*(a-1)/2)  
for K in range(N):
  print(x-L.count(L[K])+1)
0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?