Solve ABC159-D in Python

Introduction

Solve Boot camp for Beginners.

ABC159-D Banned K

** Thoughts ** It will not pass unless it is suppressed to about $ O (N) $, so it is not enough to calculate the combination for each $ k $. If you think about it for a moment, you can see that the combinations of different types of numbers are independent. For example, when the number of balls removed is 1, the sum of combinations other than 1 does not change. Now let's consider how the combination of the removed numbers changes. The combination of choosing 2 from $ n $ is $ \ frac {n (n-1)} {2} $, and the combination of choosing 2 from $ n-1 $ is $ \ frac {(n-1) (n-) 2)} {2} $, so if you take the difference, it will be $ n-1 $. After that, check the number with Counter and check the whole combination to calculate.

from collections import Counter
n = int(input())
a = list(map(int,input().split()))

c = Counter(a)
key = c.keys()
comb = 0
for i in key:
    comb += (c[i]) * (c[i]-1) // 2 #Examine combinations of non-prohibited states

for i in a:
    ans = comb - (c[i]-1) #a[i]Combination of states where
    print(ans)

Summary

I was able to solve problems that I couldn't solve two months ago, and I felt growth. see you. As an aside, today there is Ada Koda.

Recommended Posts

Solve ABC168D in Python
Solve ABC167-D in Python
Solve ABC159-D in Python
Solve ABC146-C in Python
Solve ABC098-C in Python
Solve ABC169 in Python
Solve ABC160-E in Python
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
[Python] ABC175D
Solve optimization problems in Python
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Solve ABC037 A ~ C in Python
Solve ordinary differential equations in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
[Python] DP ABC184D
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
[Python] UnionFind ABC177D
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Solve ABC175 A, B, C in Python
ABC 157 D --Solve Friend Suggestions in Python!
I wanted to solve ABC159 in Python