Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
import math
def combinations_count(n, r):
    if n >= 2:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        return 0
N = int(input())
A = [int(a) for a in input().split()]
a = [0] * N
for i in A:
    a[i-1] += 1
sum = 0
for i in range(N):
    sum += combinations_count(a[i], 2)
for k in range(N):
    print(sum +  combinations_count(a[A[k]-1]-1, 2) - combinations_count(a[A[k]-1], 2))

Submission status: Time Limit Exceeded

Solution code:
import math
def combinations_count(n, r):
    if n >= 2:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        return 0
N = int(input())
A = [int(a) for a in input().split()]
a = [0] * N
for i in range(N):
    a[A[i]-1] += 1
sum = 0
for i in range(N):
    sum += combinations_count(a[i], 2)
for k in range(N):
    print(sum +  combinations_count(a[A[k]-1]-1, 2) - combinations_count(a[A[k]-1], 2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
B = [int(x) for x in input().split()]
A=Counter(B)
V, C = zip(*A.most_common())
count=0
for x in C:
  if x==1:
    break
  count+=x*(x-1)/2
for x in B:
  ans=count-B.count(x)+1
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
B = [int(x) for x in input().split()]
A=Counter(B)
V, C = zip(*A.most_common())
count=0
for x in C:
  if x==1:
    break
  count+=x*(x-1)/2
for x in B:
  ans=count-C[V.index(x)]+1
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
B = [int(x) for x in input().split()]
A=Counter(B)
V, C = zip(*A.most_common())
count=0
for x in C:
  if x==0:
    break
  count+=x*(x-1)/2
for x in B:
  ans=count-C[V.index(x)]+1
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A_list = list(map(int,input().split()))
for k in range(N):
  count=0
  A_list_k=A_list[:k]+A_list[k+1:]
  for i in range(N-1):
    for j in range(i+1,N-1):     
    	if A_list_k[i]==A_list_k[j]:
          count +=1
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = [int(a) for a in input().split()]
a = [0] * N
for i in range(N):
    a[A[i]-1] += 1
for k in range(N):
    sum = 0
    a[A[k]-1] -= 1
    for i in range(N):
        if a[i] >= 2:
            sum += combinations_count(a[i], 2)
    print(sum)
    a[A[k]-1] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = A
count=0
while len(B)!=0:
  b=B[0]
  C=[i for i in B if i != b]
  count+=(len(B)-len(C))*(len(B)-len(C)-1)/2
  B=C
ans=list(map(lambda x: count-(A.count(x)-1),A))
for x in ans:
  print(int(x))

Submission status: Time Limit Exceeded

Solution code:
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = [int(a) for a in input().split()]

for k in range(N):
    a = [0] * N
    sum = 0
    for i in range(N):
        a[A[i]-1] += 1
    a[A[k]-1] -= 1
    for i in range(N):
        if a[i] >= 2:
            sum += combinations_count(a[i], 2)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math


def comb(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
A = list(map(int, input().split()))

d = {}
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

d2 = {}
for k in range(N):
    a = A[k]

    if a not in d2:
        d2[a] = 0
        for i, n in d.items():
            if a == i:
                d2[a] += comb(n - 1, 2)
            else:
                d2[a] += comb(n, 2)
    print(d2[a])

Submission status: Time Limit Exceeded

Solution code:
import math


def comb(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
A = list(map(int, input().split()))

d = {}
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

for k in range(N):
    ans = 0
    a = A[k]
    for i, n in d.items():
        if a == i:
            ans += comb(n-1, 2)
        else:
            ans += comb(n, 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a_list = list(map(int, input().split()))
c = collections.Counter(a_list)
for k in range(n):
    c[a_list[k]] -= 1
    ans = 0
    for i in c.values():
        ans += i*(i-1)//2
    print(ans)
    c[a_list[k]] += 1

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a_list = list(map(int, input().split()))
for k in range(n):
    a_list_k = a_list.copy()
    del a_list_k[k]
    c = collections.Counter(a_list_k)
    ans = 0
    for i in c.values():
        ans += i*(i-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
#TLE
n = int(input())
a = input().split()
dic = {}
ans = 0
a_copy = a.copy()
while a_copy != []:
    num = a_copy.count(a_copy[0]) - 1
    ans += num * (num+1)
    dic[a_copy[0]] = num
    a_copy = [i for i in a_copy if i != a_copy[0]]
ans /= 2
for i in range(n):
    print(int(ans - dic[a[i]]))

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
ball_number_list = list(map(int, input().split()))
num_list = [0]* n
for i in ball_number_list:
    num_list[i-1] += 1

def choose2(p):
    return (p*(p-1))/2

def findfind(list1,num_list, k):
    c = 0
    list2 = copy.copy(num_list)
    list2[list1[k]-1] -= 1

    for i in list2:
        c += choose2(i)
    return int(c)

for k in range(n):
    print(findfind(ball_number_list,num_list, k))

Submission status: Time Limit Exceeded

Solution code:
#TLE
n = int(input())
a = input().split()
dic = {}
ans = 0
for i in range(n):
    if not a[i] in dic:
        num = a.count(a[i]) - 1
        ans += num * (num+1)
        dic[a[i]] = num
ans /= 2
for i in range(n):
    print(int(ans - dic[a[i]]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
ans = 0
for l in range(N):
    ans += A.count(A[l]) - 1
ans /= 2
for k in range(N):
    print(int(ans - (A.count(A[k]) - 1)))

Submission status: Time Limit Exceeded

Solution code:
#TLE
N = int(input())
A = input().split()
ans = 0
for l in range(N):
    ans += A.count(A[l]) - 1
ans /= 2
for k in range(N):
    answer = ans - (A.count(A[k]) - 1)
    print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
#TLE
N = int(input())
A = list(map(int, input().split()))
ans = 0
for l in range(N):
    ans += A.count(A[l]) - 1
ans /= 2
for k in range(N):
    answer = ans - (A.count(A[k]) - 1)
    print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

def nC2(n):
    return n * (n - 1) // 2

d = {}
for i in a:
    if i in d.keys():
        d[i] += 1
    else:
        d[i] = 1
    
s = sum([nC2(i) for i in d.values()])

for i in a:
    print(s + (1 - d[i]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def select(hashmap, c_b):
    ways = 0
    for k, v in hashmap.items():
        if k == c_b:
            ways += int(((v-1)*(v-2))//2)
        else:
            ways += int((v*(v-1))//2)
    return ways
            
def BannedK():
    N = int(input())
    balls = list(map(int, input().split()))#1 1 2 1 2
    hashmap = Counter(balls)
    for current_ball in balls:
        print(select(hashmap, current_ball))

BannedK()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def calculate(v):
    if v == 2:
        return 1
    ways = 0
    ways = int((v*(v-1))//2)
    return ways

def select(hashmap, c_b):
    ways = 0
    for k, v in hashmap.items():
        if k == c_b:
            ways += calculate(v-1)
        else:
            ways += calculate(v)
    return ways
            
def BannedK():
    N = int(input())
    balls = list(map(int, input().split()))#1 1 2 1 2
    hashmap = Counter(balls)
    for current_ball in balls:
        print(select(hashmap, current_ball))

BannedK()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def calculate(v):
    if v == 2:
        return 1
    first = 1
    ways = 0
    for no in range(v, v-2, -1):
        first *= no
    ways = int(first / 2)
    return ways

def select(hashmap, c_b):
    ways = 0
    for k, v in hashmap.items():
        if k == c_b:
            #if hashmap[c_b]-1 >= v:
            ways += calculate(v-1)
        else:
            #if hashmap[k] >= v:
            ways += calculate(v)

    return ways
            
def BannedK():
    N = int(input())
    balls = list(map(int, input().split()))#1 1 2 1 2
    hashmap = Counter(balls)
    for current_ball in balls:
        print(select(hashmap, current_ball))

BannedK()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def calculate(v):
    first = 1
    ways = 0
    for no in range(v, v-2, -1):
        first *= no
    ways = int(first / 2)
    return ways

def select(hashmap, c_b):
    ways = 0
    for k, v in hashmap.items():
        if k == c_b:
            if hashmap[c_b] >= v:
                ways += calculate(v-1)
        else:
            if hashmap[k] >=v:
                ways += calculate(v)
    return ways
            
def BannedK():
    N = int(input())
    #N = 5
    #balls = [3,3,3,3,3]
    balls = list(map(int, input().split()))#1 1 2 1 2
    hashmap = Counter(balls)
    for current_ball in balls:
        print(select(hashmap, current_ball))

BannedK()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
c = Counter(A)

for k in range(N):
    ans = 0
    res = c.copy()
    res[A[k]] -= 1
    for ri in res.values():
        n = ri
        ans += n * (n-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
lst=input().split()
s=0
dic=collections.Counter(lst)
for i in dic.values():
    s+=(i*i-i)//2
for i in lst:
    print(s+1-lst.count(i))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lst=input().split()
cnt=0
for i in lst:
    for j in lst:
        cnt+=i==j
    s=(cnt-N)//2+1
for i in lst:
    print(s-lst.count(i))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lst=input().split()
for k in range(N):
    lst2=lst[:k]+lst[k+1:]
    count=0
    for i in lst2:
        for j in lst2:
            count+=i==j
    print((count-N+1)//2)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
for k in range(N):
    count=0
    n=A.pop(k)
    for i in range(N-2):
        for j in range(i+1,N-1):
            if A[i]==A[j]:
                count+=1
    print(count)
    A.insert(k,n)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

li = list(map(int, input().split()))

ans = [0]*n
for i in range(n):
    li2 = li.copy()
    del li2[i]
    li3 = list(set(li2))
    for j in range(len(li3)):
        m = li2.count(li3[j])*(li2.count(li3[j])-1)/2
        ans[i] += m
for l in range(len(ans)):
    print(int(ans[l]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

li = list(map(int, input().split()))

ans = [0]*n
for i in range(n):
    li2 = li.copy()
    del li2[i]
    for j in range(n-1):
        for k in range(j+1, n-1):
            if li2[j] == li2[k]:
                ans[i] += 1
for l in range(len(ans)):
    print(ans[l])

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
k = list(map(int, input().split()))

# 書かれている数字のぼーるがいくつあるか、辞書
from collections import Counter
A =Counter(k)

from math import factorial
def nC2(n): # 組み合わせ n「C」r
    if  n<2:
        return 0
    return n*(n-1)//2

# 順番付きの辞書
from collections import OrderedDict
D = OrderedDict()
D2 = OrderedDict()

# 各数字を連続でとる場合の数
futatu_onaji =0

x =[ nC2(v) for i,v in A.items() ]    # n個の中から2つをとる組み合わせ
futatu_onaji = sum(x)

#D =[0]*N
D =[]

D = [ futatu_onaji - (vj -1) for i,v in enumerate(k) for ij,vj in A.items() if v == ij] #vj-1 = 最初のボールと同じ番号を次に取る場合の数
print(*D,sep="\n")

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
k = list(map(int, input().split()))

# 書かれている数字のぼーるがいくつあるか、辞書
from collections import Counter
A =Counter(k)

from math import factorial
def nC2(n): # 組み合わせ n「C」r
    if  n<2:
        return 0
    return n*(n-1)//2

# 順番付きの辞書
from collections import OrderedDict
D = OrderedDict()
D2 = OrderedDict()

# 各数字を連続でとる場合の数
futatu_onaji =0
for i,v in A.items():
    futatu_onaji += nC2(v)


#D =[0]*N
D =[]
for i,v in enumerate(k):
    for ij,vj in A.items():
        if v == ij :
            x = vj-1 #最初のボールと同じ番号を次に取る
            D.append(futatu_onaji - x ) #全体から引いたものをリストに追加
print(*D,sep="\n")

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
k = list(map(int, input().split()))

# 書かれている数字のぼーるがいくつあるか、辞書
from collections import Counter
A =Counter(k)

from math import factorial
def kumiawase_num(n, r): # 組み合わせ n「C」r
    if  n<r:
        return 0
    return factorial(n) // (factorial(n - r) * factorial(r))

# 順番付きの辞書
from collections import OrderedDict
D = OrderedDict()
D2 = OrderedDict()

# 各数字を連続でとる場合の数
futatu_onaji =0
for i,v in A.items():
    futatu_onaji += kumiawase_num(v,2)


#D =[0]*N
D =[]
for i,v in enumerate(k):
    for ij,vj in A.items():
        if v == ij :
            x = kumiawase_num(vj-1,1) #最初のボールと同じ番号を次に取る
            D.append(futatu_onaji - x ) #全体から引いたものをリストに追加
print(*D,sep="\n")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

def nC2(n):
    return n * (n - 1) // 2

d = {i:a.count(i) for i in set(a)}

s = sum([nC2(d[i]) for i in set(a)])

for i in a:
    print(s + (1 - d[i]))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))

dict = {}
for i in range(N):
  if A[i] in dict.keys():
    dict[A[i]] += 1
  else:
    dict[A[i]] = 1

fact = []

fact.append(1)
for i in range(1, N+1):
  fact.append(fact[i-1] * i)

def comb(n,k):
  if n<k:
    return 0
  return int(fact[n] / fact[k] / fact[n-k])

# all
ret = 0
for key in dict.keys():
  val = dict[key]
  ret += comb(val, 2)

# i >= 0
for i in range(N):
  val_n = dict[A[i]]
  next_ret = ret - comb(val_n, 2) + comb(val_n - 1, 2)
  print(next_ret)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))

dict = {}
for i in range(N):
  if A[i] in dict.keys():
    dict[A[i]] += 1
  else:
    dict[A[i]] = 1

fact = []

fact.append(1)
for i in range(1, N):
  fact.append(fact[i-1] * i)
  
for i in range(N):
  ret = 0
  for key in dict.keys():
    val = dict[key]
    if key == A[i]:
      val -= 1
    if val < 2:
      continue
    ret += int(fact[val]/fact[val-2]/fact[2])
  print(ret)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))

Number = [A.count(i+1) for i in range(N)]
Total = sum([Number[i]*(Number[i]-1)/2 for i in range(N)])

for Ak in A:
    print(int(Total - Number[Ak-1]+1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = [0]*n
def choose2(n):
    return n * (n - 1) // 2
total = 0
for i in range(1,n+1):
    total += choose2(a.count(i))
for i in range(n):
    ans[i] = total
    ans[i] -= a.count(a[i]) - 1
print(*ans[:], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# 入力はstringで入る．
# intにしたければ int(input())
# floatで受け取ると 1.0 になる．

# 入力 : N
# 入力 : A1 A2 ... AN
N = int(input())
A = list(map(int, input().split()))

Number = []
Total = 0

Number = [A.count(i+1) for i in range(N)]
Total = sum([Number[i]*(Number[i]-1)/2 for i in range(N)])

for Ak in A:
    print(int(Total - Number[Ak-1]+1))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# 入力はstringで入る．
# intにしたければ int(input())
# floatで受け取ると 1.0 になる．

# 入力 : N
# 入力 : A1 A2 ... AN
N = int(input())
A = list(map(int, input().split()))

Number = []
Total = 0

for i in range(N):
    Number.append(A.count(i+1))
    Total += Number[i]*(Number[i]-1)/2

for Ak in A:
    print(int(Total - Number[Ak-1]+1))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import Counter
n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
def comb(n):
  return int(math.factorial(n)/(math.factorial(n-2)*2))
for k in range(n):
  ans =0
  L=A[:k]+A[k+1:]
  for v in Counter(L).values():
    if v>=2:
      ans +=comb(v)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = [0]*n
for k in range(n):
    b = a[:]
    b.pop(k)
    l = len(b)
    for i in range(1,l):
        if b.count(i) >= 2:
            ans[k] += (b.count(i) * (b.count(i) - 1 )) // 2

print(*ans[:], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = [0]*N
for n in range(N):
  count[A[n]-1] += 1
  
for k in range(N):
  c_all = 0
  for n in range(N):
    c = count[n]
    if A[k]-1 == n:
      c -= 1
    if c > 0: 
      c_all += int(c*(c-1)/2)
  print(c_all)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))

for k in range(N):
  count = [0]*N
  c_all = 0
  for n in range(N):
    if k!=n:
      count[A[n]-1] += 1
  for n in range(N):
    c = count[n]
    if c > 0:
      c_all += int(c*(c-1)/2)
  print(c_all)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

d = {i:a.count(i) for i in set(a)}

def nC2(n):
    return n * (n - 1) // 2

for i in a:
    d2 = d.copy()
    d2[i] -= 1
    print(sum([nC2(i) for i in d2.values()]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)

idx = B[0]
idx_count = 0
idx_set = []
for i in range(N):
  if idx != B[i]:
    tmp1 = int(idx_count*(idx_count-1)/2)		# 同一の整数を持つボールの組合せ数
    tmp2 = int((idx_count-1)*(idx_count-2)/2)	# 同一の整数を持つボールを1つ除いたときの組合せ数
    idx_set.append([idx, tmp1, tmp2])
    idx_count = 0
    idx = B[i]
  idx_count+=1
if idx_count != 0:
  tmp1 = int(idx_count*(idx_count-1)/2)			# 同一の整数を持つボールの組合せ数
  tmp2 = int((idx_count-1)*(idx_count-2)/2)		# 同一の整数を持つボールを1つ除いたときの組合せ数
  idx_set.append([idx, tmp1, tmp2])
#print(idx_set)
idx_sum_set = []
all_c = 0
for i in range(len(idx_set)):
  all_c += idx_set[i][1]
for i in range(len(idx_set)):
  tmp = all_c-idx_set[i][1]+idx_set[i][2]
  idx_sum_set.append([idx_set[i][0], tmp])    

if len(idx_sum_set) == 1:
  for i in range(N):
    print(idx_sum_set[0][1])
elif all_c == 0:
  for i in range(N):
    print(0)
else:
  for i in range(N):
    j = int(len(idx_sum_set)/2)
    while 1:
      if A[i] == idx_sum_set[j][0]:
        print(idx_sum_set[j][1])
        break
      elif A[i] > idx_sum_set[j][0]:
        j = j + int((len(idx_sum_set)-j)/2)
      elif A[i] < idx_sum_set[j][0]:
        j = int(j/2)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# 入力はstringで入る．
# intにしたければ int(input())
# floatで受け取ると 1.0 になる．

# 入力 : N
# 入力 : A1 A2 ... AN
N = int(input())
A = list(map(int, input().split()))

Number = []

for i in range(N):
    Number.append(A.count(i+1))

Total = 0

for i in range(N):
    Total = Total + Number[i]*(Number[i]-1)/2

for k in range(N):
    print(int(Total - Number[A[k]-1]*(Number[A[k]-1]-1)/2 + (Number[A[k]-1]-1)*(Number[A[k]-1]-2)/2))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)

idx = B[0]
idx_count = 0
idx_set = []
for i in range(N):
  if idx != B[i]:
    tmp1 = int(idx_count*(idx_count-1)/2)		# 同一の整数を持つボールの組合せ数
    tmp2 = int((idx_count-1)*(idx_count-2)/2)	# 同一の整数を持つボールを1つ除いたときの組合せ数
    idx_set.append([idx, tmp1, tmp2])
    idx_count = 0
    idx = B[i]
  idx_count+=1
if idx_count != 0:
  tmp1 = int(idx_count*(idx_count-1)/2)			# 同一の整数を持つボールの組合せ数
  tmp2 = int((idx_count-1)*(idx_count-2)/2)		# 同一の整数を持つボールを1つ除いたときの組合せ数
  idx_set.append([idx, tmp1, tmp2])
#print(idx_set)
idx_sum_set = []
all_c = 0
for i in range(len(idx_set)):
  all_c += idx_set[i][1]
for i in range(len(idx_set)):
  tmp = all_c-idx_set[i][1]+idx_set[i][2]
  idx_sum_set.append([idx_set[i][0], tmp])    

if len(idx_sum_set) == 1:
  for i in range(N):
    print(idx_sum_set[0][1])
else:
  for i in range(N):
    j = int(len(idx_sum_set)/2)
    while 1:
      if A[i] == idx_sum_set[j][0]:
        print(idx_sum_set[j][1])
        break
      elif A[i] > idx_sum_set[j][0]:
        j = j + int((len(idx_sum_set)-j)/2)
      elif A[i] < idx_sum_set[j][0]:
        j = int(j/2)

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
#最後の方のテストケースでREになってしまう。時間切れ。
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product

N = int(input())

s = input().split()
p = [int(w) for w in s]
#print('p:{}'.format(p))

number_list = [0 for i in range(N)]
'''
for i in range(N):
    counter = 0
    for j in range(N):
        if(p[j] == i+1):
            counter +=1
    number_list.append(counter)
'''
#print(number_list)

for i in range(N):
    number_list[p[i] - 1] += 1
#print(number_list)



def n_C_2(n):
    if(n == -1):
        return 0
    else:
        return int(n * (n-1) / 2)


full_combi = []
pickup_combi = []
print_combi = []

for i in range(N):
    full_combi.append(n_C_2(number_list[i]))
    pickup_combi.append(n_C_2(number_list[i] - 1))
#print('full_combi:{}'.format(full_combi))
#print('pickup_combi:{}'.format(pickup_combi))

for i in range(N):
    print_combi.append(sum(full_combi) - full_combi[i] + pickup_combi[i])
#print('print_combi:{}'.format(print_combi))

for i in range(N):
    index = p[i] - 1
    print(print_combi[index])

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
#最後の方のテストケースでREになってしまう。時間切れ。
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product

N = int(input())

s = input().split()
p = [int(w) for w in s]
#print('p:{}'.format(p))

number_list = []
'''
for i in range(N):
    counter = 0
    for j in range(N):
        if(p[j] == i+1):
            counter +=1
    number_list.append(counter)
'''
for i in range(N):
    #print(mylist.count("A"))
    number_list.append(p.count(i+1))
#print('number_list:{}'.format(number_list))

def n_C_2(n):
    if(n == -1):
        return 0
    else:
        return int(n * (n-1) / 2)


full_combi = []
pickup_combi = []
print_combi = []

for i in range(N):
    full_combi.append(n_C_2(number_list[i]))
    pickup_combi.append(n_C_2(number_list[i] - 1))
#print('full_combi:{}'.format(full_combi))
#print('pickup_combi:{}'.format(pickup_combi))

for i in range(N):
    print_combi.append(sum(full_combi) - full_combi[i] + pickup_combi[i])
#print('print_combi:{}'.format(print_combi))

for i in range(N):
    index = p[i] - 1
    print(print_combi[index])

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
#最後の方のテストケースでREになってしまう。時間切れ。
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product

N = int(input())

s = input().split()
p = [int(w) for w in s]
#print(p)

number_list = []
for i in range(N):
    counter = 0
    for j in range(N):
        if(p[j] == i+1):
            counter +=1
    number_list.append(counter)

#print(number_list)

def n_C_2(n):
    if(n == -1):
        return 0
    else:
        return int(n * (n-1) / 2)


full_combi = []
pickup_combi = []
print_combi = []

for i in range(N):
    full_combi.append(n_C_2(number_list[i]))
    pickup_combi.append(n_C_2(number_list[i] - 1))
#print(full_combi)
#print(pickup_combi)

for i in range(N):
    print_combi.append(sum(full_combi) - full_combi[i] + pickup_combi[i])
#print(print_combi)

for i in range(N):
    index = p[i] - 1
    print(print_combi[index])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 31 00:21:09 2020

@author: black
"""

import copy
N = int(input())
li = list(map(int, input().split()))
Ans = []
for i in li:
    Li = copy.deepcopy(li)
    Li.remove(i)
    ans = 0
    for j in range(N):
        for k in range(j+1,N-1):
            if Li[j] == Li[k]:
                ans += 1
    Ans.extend([ans])

for k in Ans:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
k = list(map(int, input().split()))

# 書かれている数字のぼーるがいくつあるか、辞書
from collections import Counter
A =Counter(k)

# 各数字を連続でとる場合の数
from math import factorial
def kumiawase_num(n, r): # 組み合わせ n「C」r
    if  n<r:
        return 0
    return factorial(n) // (factorial(n - r) * factorial(r))

# 順番付きの辞書
from collections import OrderedDict
D = OrderedDict()
D2 = OrderedDict()

for i,v in enumerate(k):
    D[i] =[ kumiawase_num(A[ij]-1,2) for ij,vj in A.items() if v == ij ]
    D2[i] = [ kumiawase_num(A[ij] ,2) for ij,vj in A.items() if v != ij ] 

D3 = [ sum(i) + sum(v) for i,v in zip(D.values(),D2.values()) ]      
print(*D3,sep="\n")

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(j) for j in input().split()]
for k in range(N):
  ak=A[k]
  del A[k]
  ans=0
  for i in range(N):
    n=A.count(i+1)
    ans+=n*(n-1)//2
  print(ans)
  A.insert(k,ak)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
for k in range(N):
  ak=A[k]
  del A[k]
  ans=0
  for i in range(N):
    n=A.count(i+1)
    ans+=n*(n-1)//2
  print(ans)
  A.insert(k,ak)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
for k in range(N):
  ak=A[k]
  del A[k]
  AA=sorted(A)
  AA.append(0)
  s=1
  ans=0
  for i in range(N-1):
    if AA[i+1]==AA[i]:
      s+=1
    else:
      ans+=s*(s-1)//2
      s=1
  del AA[N-1]
  print(ans)
  A.insert(k,ak)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
for k in range(N):
  ak=A[k]
  del A[k]
  AA=sorted(A)
  AA.append(0)
  s=1
  ans=0
  for i in range(N-1):
    d=AA[i+1]-AA[i]
    if d == 0:
      s+=1
    else:
      ans+=int(s*(s-1)/2)
      s=1
  del AA[N-1]
  print(ans)
  A.insert(k,ak)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = II()
a = LI()

b = [0]*(n+1)
for i in set(a):
    c = a.count(i)
    b[i] += (c*(c-1)/2)
for i in a:
    print(int(sum(b) - (a.count(i)-1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = II()
a = LI()

b = [0]*(n+1)
for i in set(a):
    c = a.count(i)
    b[i] += (c*(c-1)/2)
sumb = sum(b)
ans = []
for i in a:
    ans.append(int(sumb - (a.count(i)-1)))
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = II()
a = LI()

b = [0]*(n+1)
d = [0]*(n+1)
for i in set(a):
    c = a.count(i)
    b[i] += (c*(c-1)/2)
    d[i] += c
sumb = sum(b)
ans = []
for i in a:
    ans.append(int(sumb - (d[i]-1)))
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = II()
a = LI()

b = [0]*(n+1)
d = [0]*(n+1)
for i in set(a):
    c = a.count(i)
    b[i] += (c*(c-1)/2)
    d[i] += c
sumb = sum(b)
for i in a:
    print(int(sumb - (d[i]-1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = II()
a = LI()

b = []
d = []
for i in range(n+1):
    c = a.count(i)
    b.append(c*(c-1)/2)
    d.append(c)
sumb = sum(b)
for i in a:
    print(int(sumb - (d[i]-1)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * (N+1)
ans = [0] * N
for i in range(N):
  cnt[A[i]] +=1
for i in range(N):
  for j in range(N+1):
    if(j == A[i]):
      ans[i] += (cnt[j]-1) * (cnt[j]-2) // 2
    else:
      ans[i] += cnt[j] * (cnt[j]-1) // 2
for i in range(N):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import math
def has_duplicates(seq):
    return len(seq) != len(set(seq))
def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)
n=int(input())
a=list(map(int, input().split()))
for q in range(n):
    r=list(a)
    del r[q]
    p=0
    if has_duplicates(r):
        m=[x for x in set(r) if r.count(x) > 1]
        for o in m:
            l=r.count(o)
            p=p+permutations_count(l, 2)//2
    print(p)