Solve AtCoder Beginner Contest159 D --Banned K (ABC159D) with python (count is too slow!)

When I used the count method of python normally, I did TLE, so when I tried using Counter, I was able to AC.

image.png

Below is the answer using 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)           #Included in L{letter:Number of appearances}I will come back with the dictionary type of
x=0
for a in C.values():   #Extract only the value (number of occurrences)
  x += int(a*(a-1)/2)  
for K in range(N):
  print(x-C[L[K]]+1)   #Subtract the combination containing K from all the same number of combinations

Below is the TLE answer. Will it be faster with a little tinkering?

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)

Recommended Posts

Solve AtCoder Beginner Contest159 D --Banned K (ABC159D) with python (count is too slow!)
Solve AtCoder Beginner Contest 170 D --Not Divisible (ABC170D) with python (Eratosthenes sieve)
Solve AtCoder ABC168 with python (A ~ D)
AtCoder Beginner Contest: D Question Answers Python
Solve "AtCoder version! Ant book (beginner)" with Python!
Solve AtCoder 167 with python
Solve AtCoder Beginner Contest 100-102
Solve AtCoder ABC166 with python
Solve AtCoder ABC 186 with Python
Atcoder Beginner Contest 152 Kiroku (python)
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solve ABC166 A ~ D with Python
Solve AtCoder Problems Recommendation with python (20200517-0523)
AtCoder Beginner Contest 174 C Problem (Python)
[AtCoder] Solve ABC1 ~ 100 A problem with Python
PNG saving is slow with Python imageio.imwrite
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
[At Coder] Beginner Contest 175 ABCD python Solution introduction
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
I wanted to solve NOMURA Contest 2020 with Python
AtCoder Beginner Contest 169, D. Div Game short code
Solve A ~ D of yuki coder 247 with python
AtCoder Beginner Contest 177
AtCoder Beginner Contest 179
AtCoder Beginner Contest 172
AtCoder Beginner Contest 180
AtCoder Beginner Contest 173
Atcoder Beginner Contest 153
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
[Python] [BFS] At Coder Beginner Contest 168-D [.. Double Dots]