input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import itertools n = int(eval(input())) L = list(map(int, input().split())) def is_rectangle(a,b,c): if a + b <= c: return False if b + c <= a: return False if c + a <= b: return False else: return True x = [is_rectangle(a,b,c) for a, b, c in itertools.combinations(L, r=3)] print((sum(x)))
#提出 #8086687 import bisect n = int(eval(input())) l = list(map(int,input().split())) l.sort() ans = 0 for b in range(n): for a in range(b): ans += bisect.bisect_left(l,l[a]+l[b])-(b+1) print(ans)
p02888
n=int(eval(input())) l=list(map(int,input().split())) l.sort() cnt=0 for ind_a,a in enumerate(l[:-2]): for ind_b,b in enumerate(l[ind_a+1:-1]): for ind_c,c in enumerate(l[ind_a+ind_b+2:]): #print(a,b,c) if a+b<=c: cnt+=ind_c break if a+b>c: cnt+=ind_c+1 print(cnt)
n=int(eval(input())) l=list(map(int,input().split())) l.sort() l.append(10**9) cnt=0 i=0 #print(l) while i<n-2: j=i+1 while j<n-1: LL=j+1 RR=n-1 #print("#",l[i],l[j],l[LL],l[RR]) while LL<=RR: k=(LL+RR)//2 if k==n: break #print(l) #print(i,j,LL,RR,k) #print(l[i]+l[j],l[k],l[k+1]) if l[i]+l[j]>l[k] and l[i]+l[j]<=l[k+1]: cnt+=k-j break elif LL==RR: break elif l[i]+l[j]<=l[k]: RR=k elif l[i]+l[j]>l[k]: LL=k+1 elif l[i]+l[j]<l[k] and k==j+1: break j+=1 i+=1 print(cnt)
p02888
N = int(eval(input())) L = list(map(int,input().split(' '))) L.sort() #print(L) cnt = 0 for i in range(N-2): highJ = 0 for j in range(i+1,N-1): tmphighJ = max(j,highJ) for k in range(max(j+1,highJ+1),N): if L[k] < L[i]+L[j]: tmphighJ = k else: break highJ = max(j,tmphighJ) cnt+= tmphighJ - j #print(i,j,highJ,cnt) print(cnt)
N = int(eval(input())) L = list(map(int,input().split(' '))) L.sort() #print(L) cnt = 0 order =0 for i in range(N-2): highJ = 0 for j in range(i+1,N-1): tmphighJ = max(j,highJ) for k in range(max(j+1,highJ+1),N): order+=1 if L[k] < L[i]+L[j]: tmphighJ = k else: break highJ = max(j,tmphighJ) cnt+= highJ - j #print(i,j,highJ,cnt) print(cnt) #print(order)
p02888
#!/usr/bin/env python3 import sys from bisect import bisect_left n = int(sys.stdin.readline().strip()) l = [ int(x) for x in sys.stdin.readline().strip().split() ] l.sort() c = 0 n1 = n - 1 for i in range(n-2): li = l[i] for j in range(i+1,n1): k = bisect_left(l,l[j]+li,lo=j) c += k-j-1 # print(c)
#!/usr/bin/env python3 import sys from bisect import bisect_left n = int(sys.stdin.readline().strip()) l = list(map(int,sys.stdin.readline().strip().split())) l.sort() c = 0 n1 = n - 1 for i in range(n-2): li = l[i] for j in range(i+1,n1): k = bisect_left(l,l[j]+li,lo=j) c += k-j-1 # print(c)
p02888
n = int(eval(input())) s = list(map(int, input().split())) from collections import defaultdict d = defaultdict(int) for i in range(n): d[s[i]]+= 1 r = 0 key = sorted(list(d.keys())) ng = len(key) for i in range(ng)[::-1]: ki = key[i] for j in range(i+1)[::-1]: kj = key[j] if kj <= ki / 2: continue for k in range(j+1)[::-1]: ti = 0 kk = key[k] if kj + kk <= ki: break if ki != kj and kj != kk and ki != kk: ti = d[ki] * d[kj] * d[kk] elif ki == kj and kj == kk: if d[ki] < 3: continue else: ti = d[ki] * (d[ki]-1) * (d[ki]-2) // 6 elif ki == kj: if d[ki] < 2: continue else: ti = d[ki] * (d[ki]-1) * d[kk] // 2 elif kj == kk: if d[kk] < 2: continue else: ti = d[j] * (d[kj]-1) * d[ki] // 2 elif ki == kk: if d[ki] < 2: continue else: ti = d[ki] * (d[ki]-1) * d[kj] // 2 r+=ti print(r)
n = int(eval(input())) s = list(map(int, input().split())) import bisect s = sorted(s) res = 0 for i in range(n)[::-1]: for j in range(i)[::-1]: if s[j] <= s[i] / 2: break k = bisect.bisect_left(s, s[i]-s[j]+1) res += max(0, j-k) print(res)
p02888
n = int(eval(input())) l = list(map(int, input().split())) l.sort() ans = 0 for i in range(n-2): for j in range(i+1,n-1): for k in range(j+1,n): if l[k]<l[j]+l[i]: ans += 1 print(ans)
n = int(eval(input())) l = list(map(int, input().split())) l.sort(reverse=True) ans = 0 for i in range(n-2): j = i+1 k = n-1 while j < k: if l[i]<l[j]+l[k]: ans += k-j j += 1 else: k -= 1 print(ans)
p02888
import copy import bisect def main(N, L): L.sort(reverse=True) triangles = 0 for i, P in enumerate(L): Q_list = copy.copy(L) del Q_list[:i + 1] for j, Q in enumerate(Q_list): if Q < P / 2: break minR = P - Q + 1 R_list = copy.copy(Q_list) del R_list[:j + 1] R_list.reverse() triangles += len(R_list) - bisect.bisect_left(R_list, minR) print(triangles) if __name__ == '__main__': N = int(eval(input())) L = list(map(int, input().split())) main(N, L)
import copy import bisect N = int(eval(input())) L = sorted(list(map(int, input().split()))) triangles = 0 for i in range(N - 2): P = L[i] for j in range(i + 1, N - 1): Q = L[j] if P + Q <= L[j + 1]: continue maxR = P + Q - 1 triangles += bisect.bisect_right(L, maxR) - (j + 1) print(triangles)
p02888
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() res = 0 for i in range(N-2): idx = (N-1) - i max_, second_max = L[idx], L[idx-1] for tmp_idx in range(1, idx): second_max = L[tmp_idx] tidx = bisect.bisect_right(L, max_ - second_max) if len(L[tidx:tmp_idx]) > 0: res += len(L[tidx:tmp_idx]) print(res)
from bisect import bisect_left N = int(eval(input())) l = list(map(int, input().split())) l.sort() res = 0 for i in range(N-2): for j in range(i+1, N-1): t = l[i] + l[j] idx = bisect_left(l, t) res += idx-j-1 print(res)
p02888
n = int(eval(input())) l = list(map(int, input().split())) l.sort() ans = 0 for i in range(n-2): for j in range(i+1, n-1): for k in range(j+1, n): if l[k] >= l[j] + l[i]: break if l[j] < l[k] + l[i] and l[i] < l[k] + l[j]: ans += 1 print(ans)
n = int(eval(input())) l = list(map(int, input().split())) l.sort() ans = 0 for i in range(n): k = i+1 for j in range(i+1, n): while k < n and l[k] < l[i] + l[j]: k += 1 ans += k - (j+1) print(ans)
p02888
from itertools import permutations n=int(eval(input())) li=[int(i) for i in input().split()] ans=0 for i in range(n-2): for j in range(i+1,n-1): for k in range(j+1,n): a=li[i] b=li[j] c=li[k] if a<b+c and b<a+c and c<a+b: ans+=1 print(ans)
import bisect n=int(eval(input())) li=[int(i) for i in input().split()] li.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): a=li[i] b=li[j] c=bisect.bisect_left(li,a+b) ans+=c-j-1 print(ans)
p02888
import bisect n = int(eval(input())) l = list(map(int,input().split())) l.sort() count = 0 for i in range(n-2): for j in range(i+1, n-1): short_2hen = l[i] + l[j] l_long = l[j+1:n] bis = bisect.bisect_right(l_long, short_2hen-1) count += bis print(count)
import bisect n = int(eval(input())) l = list(map(int,input().split())) l.sort() count = 0 for i in range(n-2): for j in range(i+1, n-1): short_2hen = l[i] + l[j] bis = bisect.bisect_left(l, short_2hen) count += bis - j - 1 print(count)
p02888
from bisect import bisect_left, bisect_right N = int(eval(input())) L = list(map(int, input().split())) L.sort() count = 0 for i, l1 in enumerate(L): for j, l2 in enumerate(L): if i < j: s = bisect_right(L[j+1:], abs(l1-l2)) e = bisect_left(L[j+1:], l1+l2) # print((i, j), (l1, l2), (abs(l1-l2), l1+l2), (s, e)) count += max(0, e-s) # abs(l1-l2) <= m <= l1+l2 print(count)
from bisect import bisect_left, bisect_right N = int(eval(input())) L = list(map(int, input().split())) L.sort() count = 0 for i, l1 in enumerate(L): for j, l2 in enumerate(L): if i < j: s = max(j+1, bisect_right(L, abs(l1-l2))) e = max(j+1, bisect_left(L, l1+l2)) # print((i, j), (l1, l2), (abs(l1-l2), l1+l2), (s, e)) count += max(0, e-s) # abs(l1-l2) <= m <= l1+l2 print(count)
p02888
from bisect import bisect_left N = int(eval(input())) L = list(map(int, input().split())) L.sort() answer = 0 for i in range(N): for j in range(i+1, N): answer += bisect_left(L[j+1:], L[i]+L[j]) print(answer)
N = int(eval(input())) L = list(map(int, input().split())) L.sort() answer = 0 for i in range(N-1, 1, -1): # iは辺の最大値 s = 0 # aは1番短い辺 l = i-1 while l > s: if L[s] + L[l] > L[i]: answer += l - s l -= 1 else: s += 1 print(answer)
p02888
N = int(eval(input())) L = list(map(int, input().split())) L.sort() answer = 0 for i in range(N-1, 1, -1): # iは辺の最大値 s = 0 # aは1番短い辺 l = i-1 while l > s: if L[s] + L[l] > L[i]: answer += l - s l -= 1 else: s += 1 print(answer)
N = int(eval(input())) L = list(map(int, input().split())) L.sort() answer = 0 for i in range(N-1, 1, -1): # iは辺の最大値 s = 0 # sは1番短い辺 l = i-1 while l > s: if L[s] + L[l] > L[i]: answer += l - s l -= 1 else: s += 1 print(answer)
p02888
import sys from bisect import bisect_left sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N = ir() L = lr() L.sort() answer = 0 for i in range(N-2): for j in range(i+1, N-1): candidate = L[j+1:] index = bisect_left(candidate, (L[i]+L[j])) answer += index print(answer)
import sys from bisect import bisect_left sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N = ir() L = lr() L.sort() answer = 0 for i in range(N-2): for j in range(i+1, N-1): index = bisect_left(L, (L[i]+L[j])) answer += index - j - 1 print(answer)
p02888
import sys from bisect import bisect_left sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N = ir() L = lr() L.sort() answer = 0 for i in range(N-2): for j in range(i+1, N-1): index = bisect_left(L, (L[i]+L[j])) answer += index - j - 1 print(answer)
import sys sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N = ir() L = lr() L.sort() answer = 0 for i in range(N-1, -1, -1): # iが一番大きい辺 x = L[i] left = 0 right = i - 1 while left < right: if L[left] + L[right] <= x: left += 1 continue answer += (right - left) right -= 1 print(answer)
p02888
from bisect import bisect n = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(n): for j in range(i+1, n): for k in range(j+1, bisect(L, L[i]+L[j]-1)): ans+=1 print(ans)
from bisect import bisect n = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(n): for j in range(i+1, n): ans+=bisect(L, L[i]+L[j]-1)-j-1 print(ans)
p02888
import itertools N = int(eval(input())) L = tuple(map(int, input().split())) list = list(itertools.combinations(L, 3)) count = 0 for i in list: if i[0] < i[1] + i[2]: if i[1] < i[0] + i[2]: if i[2] < i[1] + i[0]: count += 1 print(count)
import itertools N = int(eval(input())) L = tuple(map(int, input().split())) count = 0 for i in itertools.combinations(L, 3): if i[0] < i[1] + i[2]: if i[1] < i[0] + i[2]: if i[2] < i[1] + i[0]: count += 1 print(count)
p02888
from itertools import accumulate N, *L = list(map(int, open(0).read().split())) A = [0] * (2 * 10 ** 3 + 7) for l in L: A[l] += 1 S = list(accumulate(A)) ans = 0 for i in range(N): b = L[i] for j in range(i + 1, N): c = L[j] l, r = abs(b - c), b + c ans += S[r - 1] - S[l] - (l < b < r) - (l < c < r) print((ans // 3))
from itertools import accumulate, combinations N, *L = list(map(int, open(0).read().split())) A = [0] * (2 * 10 ** 3 + 7) for l in L: A[l] += 1 S = list(accumulate(A)) ans = 0 for b, c in combinations(L, 2): l, r = abs(b - c), b + c ans += S[r - 1] - S[l] - (l < b < r) - (l < c < r) print((ans // 3))
p02888
from itertools import combinations from bisect import bisect_right def main(): N, *L = list(map(int, open(0).read().split())) L.sort() print((sum(bisect_right(L, L[i] + L[j] - 1, lo=j + 1) - (j + 1) for i, j in combinations(list(range(N)), 2)))) if __name__ == '__main__': main()
from itertools import combinations from bisect import bisect_right def main(): N, *L = list(map(int, open(0).read().split())) L.sort() print((sum(bisect_right(L, L[i] + L[j] - 1, j + 1) - (j + 1) for i, j in combinations(list(range(N)), 2)))) if __name__ == '__main__': main()
p02888
import bisect N = int(eval(input())) L = sorted(map(int, input().split(' '))) ans = 0 for i in range(len(L)): for j in range(i + 1, len(L)): a = L[i] b = L[j] ans += bisect.bisect_right(L[j + 1:], a + b - 1) print(ans)
import bisect N = int(eval(input())) L = sorted(map(int, input().split(' '))) ans = 0 for i in range(len(L)): for j in range(i + 1, len(L)): a = L[i] b = L[j] ans += max(0, bisect.bisect_right(L, a + b - 1) - (j + 1)) print(ans)
p02888
import bisect N=int(eval(input())) L=list(map(int,input().split())) L.sort() ans=0 def find_gt(a,x): i = bisect.bisect_right(a,x) if i != len(a): return i return(-1) for i in range(N-1,-1,-1): for j in range(i-1,-1,-1): l=L[:j] if len(l)>=1: num=find_gt(l,L[i]-L[j]) if num!=-1: ans+=len(l)-num else: continue print(ans)
import bisect N=int(eval(input())) L=list(map(int,input().split())) L.sort() count=0 for i in range(N): for j in range(i+1,N): T=bisect.bisect_left(L,L[i]+L[j]) count+=T-j-1 print(count)
p02888
import bisect N = int(eval(input())) L = list(map(int,input().split())) L.sort() count = 0 for a in range(N): for b in range(a+1, N): Ll = L[b+1:N] la = L[a] lb = L[b] ab = la + lb sa = abs(la-lb) first = bisect.bisect_left(Ll, ab) -1 second = bisect.bisect_right(Ll, sa) count += first - second +1 print(count)
import bisect N = int(eval(input())) L = list(map(int,input().split())) L.sort() count = 0 total = [] for a in range(N-2): for b in range(a+1, N-1): la = L[a] lb = L[b] ab = la + lb sa = abs(la-lb) first = bisect.bisect_left(L, ab) -1 second = bisect.bisect_right(L, sa) if first > second and first >= b+1: count += first - max(second, b+1) +1 print(count)
p02888
import bisect N = int(eval(input())) A = list(map(int,input().split())) A.sort() SUM = 0 for i,a in enumerate(A): for j,b in enumerate(A[i+1:]): SUM += bisect.bisect_left(A[i+j+2:], a+b) print(SUM)
N = int(eval(input())) A = list(map(int,input().split())) A.sort() count = 0 for i in range(N-1,0,-1): l = 0 r = i -1 while(l< r): if (A[l] + A[r] >A[i]): count += (r-l) r -= 1 else: l += 1 print(count)
p02888
import bisect import itertools N = int(eval(input())) L = list(map(int, input().split())) cnt = 0 for i in range(N): L_t = L[i+1:N] for v in itertools.combinations(L_t, 2): a = int(L[i]) b = int(v[0]) c = int(v[1]) if a < b + c and b < a + c and c < a + b: cnt += 1 print(cnt)
import bisect N = int(eval(input())) L = sorted(map(int, input().split())) cnt = 0 for i in range(N-2): for j in range(i+1, N-1): ab = L[i] + L[j] c_index = bisect.bisect_left(L, ab) cnt += c_index - 1 - j print(cnt)
p02888
#import heapq import itertools #import math #import numpy as np #import sys N = int(eval(input())) L = list(map(int, input().split())) #S = [input() for _ in range(H)] #T = [list(map(int,input().split())) for _ in range(N)] #sys.exit() L = sorted(L) a = [(x, y, z) for (x, y, z) in itertools.combinations(L, 3) if ((x+y) > z) and ((x+z) > y) and ((y+z) > x)] print((len(a)))
#import heapq import itertools #import math #import numpy as np #import sys N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) #print(L) C = itertools.combinations(list(range(0,N-1)), 2) ans = 0 i = 1 for c in C : #print(c) while True : #print(L[c[1] + i]) if L[c[0]] + L[c[1]] > L[c[1] + i] : ans += 1 i += 1 else : i = 1 break if c[1] + i > N - 1: i = 1 break print(ans) #a = [(x, y, z) for (x, y, z) in itertools.combinations(L, 3) if ((x+y) > z) and ((x+z) > y) and ((y+z) > x)] #print(len(a))
p02888
#import heapq import itertools #import math #import numpy as np #import sys N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) #print(L) C = itertools.combinations(list(range(0,N-1)), 2) ans = 0 i = 1 for c in C : #print(c) while True : #print(L[c[1] + i]) if L[c[0]] + L[c[1]] > L[c[1] + i] : ans += 1 i += 1 else : i = 1 break if c[1] + i > N - 1: i = 1 break print(ans) #a = [(x, y, z) for (x, y, z) in itertools.combinations(L, 3) if ((x+y) > z) and ((x+z) > y) and ((y+z) > x)] #print(len(a))
from bisect import bisect_left N = int(eval(input())) L = list(map(int, input().split())) L.sort() result = 0 for i in range(N - 2): for j in range(i + 1, N - 1): result += bisect_left(L, L[i] + L[j]) - j - 1 print(result)
p02888
N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 ans2 = 0 for k in range(N-2): for j in range(k+1, N-1): cnt = 0 for l in range(N-1, j, -1): if L[k] + L[j] <= L[l]: cnt += 1 continue else: ans += N-1-j-cnt break print(ans)
# Binary Indexed Tree (Fenwick Tree) class BIT: def __init__(self, n): self.n = n self.data = [0]*(n+1) self.el = [0]*(n+1) def sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): # assert i > 0 self.el[i] += x while i <= self.n: self.data[i] += x i += i & -i def get(self, i, j=None): if j is None: return self.el[i] return self.sum(j) - self.sum(i) N = int(eval(input())) L = list(map(int, input().split())) L.sort() bit = BIT(L[-1]*2+1) bit.add(L[0]+L[1], 1) total = 1 ans = 0 for k in range(2, N): ans += total - bit.sum(L[k]) for j in range(k): bit.add(L[j]+L[k], 1) total += 1 print(ans)
p02888
# -*- coding: utf-8 -*- """ Created on Wed Sep 9 20:56:06 2020 @author: liang """ N = int(eval(input())) L = [int(x) for x in input().split()] L.sort() ans = 0 #print(L) for i in range(N-2): #print("1:",i) p = i+2 #以下O(2?N) 定数倍が遅い for j in range(i+1,N-1): #print("2",i,j) while p < N and L[p] < L[i] + L[j] : p += 1 #print("up") #print(p) #ans += len(L[j+1:p]) ans += max(0, p - j - 1) print(ans)
# -*- coding: utf-8 -*- """ Created on Wed Sep 9 21:39:55 2020 @author: liang """ # -*- coding: utf-8 -*- """ Created on Wed Sep 9 20:56:06 2020 @author: liang """ N = int(eval(input())) L = [int(x) for x in input().split()] from bisect import bisect_left L.sort() ans = 0 #print(L) for i in range(N-2): #print("1:",i) #p = i+2 #以下O(2N) 定数倍が遅い for j in range(i+1,N-1): #print("2",i,j) t = L[i] + L[j] #while p < N and L[p] < L[i] + L[j] : # p += 1 p = bisect_left(L,t) #print("up") #print(p) #ans += len(L[j+1:p]) ans += max(0, p - j -1) print(ans)
p02888
N = int(eval(input())) L = list(map(int, input().split())) from itertools import combinations comb = combinations(L, 3) ans = 0 for c in comb: if 2*max(c)<sum(c): ans+=1 print(ans)
N = int(eval(input())) L = list(map(int, input().split())) from bisect import bisect_right L = sorted(L, reverse=True) L = [-L[i] for i in range(N)] cnt=0 for i in range(N-2): for j in range(i+1, N-1): diff = L[i]-L[j] idx = bisect_right(L, diff-1, j+1, N) cnt+=(idx-j)-1 print(cnt)
p02888
n=int(eval(input())) l=list(map(int,input().split())) l.sort() count=0 for i in range(n-2): for j in range(i+1,n-1): left=j+1 right=n-1 while left<=right: center=(right+left)//2 if center==n-1: if l[i]+l[j]>l[center]: count+=center-j break elif l[center+1]>l[i]+l[j]>l[center] : count+=center-j break elif l[i]+l[j]>l[center]: left=center+1 else: right=center-1 print(count)
n=int(eval(input())) l=list(map(int,input().split())) l.sort() l.reverse() count=0 for i in range(n-2): j=i+1 k=n-1 while j<k: if l[i]<l[j]+l[k]: count+=k-j j+=1 else: k-=1 print(count)
p02888
import bisect def main(): N = int(eval(input())) L = [int(i) for i in input().split()] L.sort() ans = 0 for i in range(N-2): for j in range(i+1,N-1): tmp = L[i] + L[j] insert_index = bisect.bisect_left(L[j+1:], tmp) #print(insert_index) ans += insert_index print(ans) if __name__ == "__main__": main()
import bisect import time def main(): N = int(eval(input())) L = [int(i) for i in input().split()] #N = 2000 #L = [int(i) for i in range(N)] L.sort() ans = 0 for i in range(N-2): for j in range(i+1,N-1): #ans += bisect.bisect_left(L[j+1:], L[i]+L[j]) index = bisect.bisect_left(L, L[i]+L[j]) ans += (index - (j+1)) print(ans) if __name__ == "__main__": #start = time.time() main() #elapsed_time = time.time() - start #print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
p02888
import bisect import time def main(): N = int(eval(input())) L = [int(i) for i in input().split()] #N = 2000 #L = [int(i) for i in range(N)] L.sort() ans = 0 for i in range(N-2): for j in range(i+1,N-1): #ans += bisect.bisect_left(L[j+1:], L[i]+L[j]) index = bisect.bisect_left(L, L[i]+L[j]) ans += (index - (j+1)) print(ans) if __name__ == "__main__": #start = time.time() main() #elapsed_time = time.time() - start #print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
from bisect import bisect_left import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines def main(): N = int(readline()) L = [int(i) for i in readline().split()] L.sort() ans = 0 for i in range(N-1): L_i = L[i] for j in range(i+1, N): idx = bisect_left(L, L_i + L[j]) ans += idx - (j + 1) print(ans) if __name__ == "__main__": main()
p02888
import bisect N = int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for i in range(N-2): for j in range(i+1,N-1): a = L[i]+L[j] ind = bisect.bisect_left(L[j+1:],a) cnt += ind print(cnt)
import bisect N = int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for i in range(N-2): for j in range(i+1,N-1): a = L[i]+L[j] ind = bisect.bisect_left(L,a) cnt += ind-j-1 print(cnt)
p02888
from bisect import bisect_left,bisect_right from itertools import combinations N = int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for x in combinations(list(range(N)),2): i,j = x[0],x[1] if j<i: i,j = j,i indr = bisect_left(L,L[i]+L[j]) indl = bisect_right(L,abs(L[i]-L[j])) if j<indl or i>indr: cnt += indr-indl elif indr>j>=indl and i<indl: cnt += indr-indl-1 elif indr>j and i>=indl: cnt += indr-indl-2 elif j>=indr and indr>i>=indl: cnt += indr-indl-1 print((cnt//3))
from bisect import bisect_right,bisect_left N = int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for i in range(N-1): for j in range(i+1,N): a = L[i] b = L[j] indU = bisect_left(L,a+b) indL = bisect_right(L,b-a) if j<indU and i>= indL: cnt += indU-indL-2 elif indL<=i<indU and j>=indU or i<indL and indL<=j<indU: cnt += indU-indL-1 elif j<indL or i>=indU: cnt += indU-indL print((cnt//3))
p02888
from bisect import bisect_right,bisect_left N = int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for i in range(N-1): for j in range(i+1,N): a = L[i] b = L[j] indU = bisect_left(L,a+b) indL = bisect_right(L,b-a) if j<indU and i>= indL: cnt += indU-indL-2 elif indL<=i<indU and j>=indU or i<indL and indL<=j<indU: cnt += indU-indL-1 elif j<indL or i>=indU: cnt += indU-indL print((cnt//3))
from bisect import bisect_left N = int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for i in range(N-2): for j in range(i+1,N-1): a = L[i] b = L[j] ind = bisect_left(L,a+b) cnt += ind-j-1 print(cnt)
p02888
from bisect import bisect_left N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i, a in enumerate(L[:N-2]): for j, b in enumerate(L[i+1:N-1]): over_index = bisect_left(L[i+j+2:],a+b) ans += over_index print(ans)
from bisect import bisect_left N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(N-2): for j in range(i+1, N-1): ans += bisect_left(L, L[i]+L[j]) - (j + 1) print(ans)
p02888
n = int(eval(input())) l = list(map(int, input().split())) l.sort() s = 0 for i in range(n-2): for j in range(i+1,n-1): ab = abs(l[i]-l[j]) for k in range(j+1,n): if l[k] >= l[i] + l[j]: break if ab < l[k]: s += 1 print(s)
n = int(eval(input())) l = list(map(int, input().split())) l.sort() s = 0 for i in range(n-2): for j in range(i+1,n-1): high = n-1 low = j+1 ab = l[i] + l[j] if l[j+1] >= ab: continue while high - low > 1: mid = (high+low)//2 if l[mid] < ab: low = mid else: high = mid if l[high] < ab: s += high - j else: s += low - j print(s)
p02888
import collections n = int(eval(input())) L = sorted(map(int,input().split())) ans = [] for i in range(n): for j in range(i+1,n): for k in range(j+1,n): if L[i] < L[j] + L[k] and L[j] < L[k] + L[i] and L[k] < L[i] + L[j]: A = [i,j,k] A.sort() s = ''.join([str(n) for n in A]) ans.append(s) cnt = set(ans) print((len(cnt)))
N = int(eval(input())) A = list(map(int,input().split())) A.sort() count = 0 for i in range(N-1,0,-1): l = 0 r = i -1 while(l< r): if (A[l] + A[r] >A[i]): count += (r-l) r -= 1 else: l += 1 print(count)
p02888
import bisect def f(L): n = len(L) L.sort() count = 0 # a=3 b=4 c=2, c < a + b => 2 < 3+4 # a < b + c => a - b < c => c > a - b # b < c + a => b - a < c => c > b - a # c < a + b for i in range(n): for j in range(i + 1, n): a, b = L[i], L[j] start = bisect.bisect_left(L, max(a-b, b-a)) end = bisect.bisect_left(L, a + b) for k in range(start, end): assert L[k] < a + b assert L[k] > a - b # assert L[k] > b - a if k != i and k != j and L[k] < a + b: # other two checks if L[k] < a + b and a < b + L[k] and b < L[k] + a: count += 1 return count // 3 assert f([3, 4, 2, 1]) == 1 assert f([1, 1000, 1]) == 0 assert f([218, 786, 704, 233, 645, 728, 389]) == 23 _N = eval(input()) L = list(map(int, input().split())) ans = f(L) print(ans)
import bisect def f(L): n = len(L) L.sort() count = 0 for i in range(n): for j in range(i + 1, n): a = L[i] b = L[j] start = max(a - b, b - a) end = a + b si = bisect.bisect_right(L, start) ei = bisect.bisect_left(L, end) zx = ei - si if i >= si and i < ei: zx -= 1 if j >= si and j < ei: zx -= 1 count += zx return count // 3 assert f([3, 4, 2, 1]) == 1 assert f([1, 1000, 1]) == 0 assert f([218, 786, 704, 233, 645, 728, 389]) == 23 _N = eval(input()) L = list(map(int, input().split())) ans = f(L) print(ans)
p02888
import sys import itertools input = sys.stdin.readline def main(): n = int(eval(input())) l = list(map(int, input().split())) # 種類組み合わせ・条件 l = sorted(l) hoge = [] for a, b in itertools.combinations(l, 2): target = l[:] target.remove(a) target.remove(b) hoge += [sorted([a, b, c]) for c in [x for x in target if b - a < x < a + b]] print((len(set(["{}{}{}".format(a, b, c) for a, b, c in hoge])))) # sum([str(i) for i in if __name__ == "__main__": main()
import sys import bisect input = sys.stdin.readline def main(): n = int(eval(input())) l = list(map(int, input().split())) # 種類組み合わせ・条件 l = sorted(l) hoge = 0 for i, a in enumerate(l[:-2], start=1): for j, b in enumerate(l[i:-1], start=i): index = bisect.bisect_left(l, a + b) hoge += index - j - 1 print(hoge) if __name__ == "__main__": main()
p02888
n = int(eval(input())) l = list(map(int,input().split())) l.sort() ans = 0 for i in range(n-2): for j in range(i + 1,n-1): flag = True for k in range(j + 1,n): if l[i] + l[j] > l[k] : ans += 1 else: break print(ans)
import bisect n = int(eval(input())) l = list(map(int,input().split())) l.sort() ans = 0 for i in range(n-2): for j in range(i + 1,n-1): ans += bisect.bisect_left(l,l[i] + l[j]) - j - 1 print(ans)
p02888
def bin_search1(L1,L2,l,N):#長すぎる辺の個数を返す low = 0 high = N-1 while high-low>1: mid = (low+high)//2 if (L1+L2<=l[mid]):#長すぎ high = mid else: low = mid if L1+L2>l[high]:#全部ok return 0 elif L1+L2<=l[low]:#全部ng return N else: return N-high def bin_search2(L1,L2,l,N):#短すぎる辺の個数を返す low = 0 high = N-1 while high-low>1: mid = (low+high)//2 if (abs(L1-L2)>=l[mid]): low = mid else: high = mid if abs(L1-L2)<l[low]: return 0 elif abs(L1-L2)>=l[high]: return N else: return high N = int(eval(input())) L = list(map(int,input().split())) L.sort() ans = 0 for i in range(N): for j in range(N): if i!=j: L_temp = L#修正面倒なため a = L[i] b = L[j] if i<j: L_temp.pop(j) L_temp.pop(i) ans += (N-2-bin_search1(a,b,L_temp,N-2)-bin_search2(a,b,L_temp,N-2)) L.insert(i,a) L.insert(j,b) else: L_temp.pop(i) L_temp.pop(j) ans += (N-2-bin_search1(a,b,L_temp,N-2)-bin_search2(a,b,L_temp,N-2)) L.insert(j,b) L.insert(i,a) ans = ans//6 print(ans)
from bisect import bisect_left N = int(input()) L = list(map(int,input().split())) L.sort() ans = 0 for i in range(N-1): for j in range(i+1,N): ans += bisect_left(L,L[i]+L[j])-(j+1) print(ans)
p02888
import itertools N=int(eval(input())) L=list(map(int,input().split())) C=list(itertools.combinations(L,3)) ans=0 for i in range(len(C)): if C[i][0]<C[i][1]+C[i][2] and C[i][1]<C[i][0]+C[i][2] and C[i][2]<C[i][1]+C[i][0]: ans+=1 print(ans)
import itertools N=int(eval(input())) L=list(map(int,input().split())) C=[x for x in itertools.combinations(L,3) if x[0]<x[1]+x[2] and x[1]<x[0]+x[2] and x[2]<x[0]+x[1]] print((len(C)))
p02888
import itertools N=int(eval(input())) L=list(map(int,input().split())) ans=0 for x in itertools.combinations(L,3): if x[0]<x[1]+x[2] and x[1]<x[0]+x[2] and x[2]<x[0]+x[1]: ans+=1 print(ans)
import bisect N=int(eval(input())) L=sorted(list(map(int,input().split()))) ans=0 for i in range(N-2): for j in range(N-i-2): ans+=min(N-i-j-2,N-bisect.bisect_left(L,L[-j-1]-L[i]+1)-j-1) print(ans)
p02888
import itertools import bisect N = int(eval(input())) L = list(map(int, input().split())) ans = 0 LS = sorted(L) l = len(LS) for i in range(l): for j in range(l): if i < j: k = bisect.bisect_left(LS,LS[i]+LS[j]) ans += k-j-1 print(ans)
import sys import bisect N = int(eval(input())) L = list(map(int, input().split())) LS = sorted(L) ans = 0 for i in range(N): for j in range(N): if i < j: index = bisect.bisect_left(LS,LS[i]+LS[j]) ans += index-j-1 print(ans)
p02888
from itertools import combinations N=int(eval(input())) triangle=[] count=0 L=list(map(int,input().split())) for i in combinations(L,3): triangle.append(list(i)) for i in triangle: #print(i) if i[0]<i[1]+i[2] and i[1]<i[2]+i[0] and i[2]<i[1]+i[0]: count+=1 print(count)
from bisect import bisect_left N=int(eval(input())) triangle=[] count=0 L=list(map(int,input().split())) L.sort() #print(L) for a in range(N): for b in range(a+1,N): c=L[a]+L[b] zahyo=bisect_left(L,c) count+=max(0,zahyo-b-1) print(count)
p02888
import bisect def main(): n = int(eval(input())) l_lst = list(map(int, input().split())) l_lst.sort() count = 0 for i in range(n - 1): for j in range(i + 1, n): lst = l_lst[j + 1:] a = l_lst[i] b = l_lst[j] count += bisect.bisect_left(lst, a + b) print(count) if __name__ == '__main__': main()
import bisect def main(): n = int(eval(input())) l_lst = list(map(int, input().split())) l_lst.sort() count = 0 for i in range(n - 1): for j in range(i + 1, n): a = l_lst[i] b = l_lst[j] tmp = bisect.bisect_left(l_lst, a + b) tmp -= j + 1 tmp = max(0, tmp) count += tmp print(count) if __name__ == '__main__': main()
p02888
import bisect N = int(eval(input())) L = sorted(list(map(int,input().split()))) ans = 0 for i in range(N-2): for j in range(i+1,N-1): idx = bisect.bisect(L,L[i]+L[j]-1) ans += idx-j-1 print(ans)
import bisect N = int(eval(input())) L = sorted(list(map(int,input().split()))) ans = 0 for bi in range(1,N-1): for ai in range(0,bi): ci = bisect.bisect(L,L[bi]+L[ai]-1) - 1 if bi<ci: ans += ci-bi print(ans)
p02888
n=int(eval(input())) l=list(map(int,input().split())) l.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): k=j+1 while k<n: if l[i]+l[j]>l[k]: k+=1 ans+=1 else: break print(ans)
n=int(eval(input())) p=list(map(int,input().split())) p.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): if j<n-2: l=j+1 r=n-1 while r-l>1: c=(l+r)//2 if p[c]>=p[i]+p[j]: r=c else: l=c if p[l]<p[i]+p[j]: ans+=l-j if p[r]<p[i]+p[j]: ans+=1 if j==n-2: if p[i]+p[n-2]>p[n-1]: ans+=1 print(ans)
p02888
from bisect import bisect_left N = int(input()) L = list(map(int, input().split())) L.sort() result = 0 for i, a in enumerate(L, 1): for j, b in enumerate (L[i:], i+1): k = bisect_left(L[j:], a + b) result += k print(result)
from bisect import bisect_left N = int(input()) L = list(map(int, input().split())) L.sort() result = 0 for i in range(N-2): a = L[i] for j in range(i+1, N-1): b = L[j] k = bisect_left(L, a + b, j + 1) result += k - (j + 1) print(result)
p02888
import sys input = sys.stdin.readline from itertools import combinations N=int(input().rstrip('\n')) ls = [int(i) for i in input().rstrip('\n').split()] ls = sorted(ls) ok = [x for x in combinations(ls,3) if x[0]+x[1]>x[2]] print((len(ok)))
import sys input = sys.stdin.readline N=int(input().rstrip('\n')) ls = [int(i) for i in input().rstrip('\n').split()] ls=sorted(ls,reverse = True) res = 0 for x in range(N-2): a = ls[x] for y in range(x+1,N-1): b = ls[y] if a>(b+ls[y+1]): break else: for z in range(y+1,N): if a<(b+ls[z]): res += 1 else: break print(res)
p02888
from itertools import combinations n = int(eval(input())) l = list(int(i) for i in input().split()) cnt = 0 for bars in combinations(l, 3): a,b,c = bars[0], bars[1], bars[2] if a < b + c and b < a + c and c < b + a: cnt+=1 print(cnt)
import bisect def main(): n = int(eval(input())) l = sorted(list(int(i) for i in input().split())) cnt = 0 for i in range(n - 2): a = l[i] for j in range(i + 1, n-1): b = l[j] cnt += bisect.bisect_left(l, a+b)-(j+1) print(cnt) if __name__ == "__main__": main()
p02888
from bisect import bisect_left, bisect_right n = int(eval(input())) stick = list(map(int, input().split())) stick.sort() count = 0 for i in range(n): for j in range(i + 1, n): l = bisect_left(stick, stick[j] - stick[i] + 1) r = bisect_right(stick, stick[i] + stick[j] - 1) - 1 if r < 0 or l >= n: break count += r - l + 1 if l <= i <= r: count -= 1 if l <= j <= r: count -= 1 print((count // 3))
from bisect import bisect_left, bisect_right n = int(eval(input())) stick = list(map(int, input().split())) stick.sort() count = 0 for i in range(n - 1, -1, -1): for j in range(i - 1, -1, -1): l = bisect_left(stick, stick[i] - stick[j] + 1) r = bisect_right(stick, stick[i] + stick[j] - 1) - 1 if j <= l or l > r: break count += min(r + 1, j) - l print(count)
p02888
from bisect import bisect_left n = int(eval(input())) l = sorted(list(map(int, input().split()))) answer = 0 for i in range(n - 2): for j in range(i + 1, n - 1): lij = l[i] + l[j] r = bisect_left(l, lij) answer += max(0, r - j - 1) print(answer)
from bisect import bisect_left n = int(eval(input())) l = sorted(list(map(int, input().split()))) answer = 0 for i in range(n - 2): for j in range(i + 1, n - 1): lij = l[i] + l[j] r = bisect_left(l, lij) answer += r - j - 1 print(answer)
p02888
n = int(eval(input())) l = list(map(int, input().split())) l.sort() triangle = 0 while len(l) >= 3: max_l = l.pop() for i in range(1, len(l)): rest_l = max_l - l[i] inf = -1 sup = i while sup - inf > 1: x = (inf + sup) // 2 if l[x] > rest_l: sup = x else: inf = x triangle += i - sup print(triangle)
from bisect import bisect_left n = int(eval(input())) l = list(map(int, input().split())) l.sort() triangle = 0 for i in range(n-2): for j in range(i+1, n-1): index = bisect_left(l, l[i] + l[j], j + 1, n) - 1 triangle += index - j print(triangle)
p02888
N=int(eval(input())) S=list(map(int,input().split( ))) S = sorted(S) tri_num=0 for i in range(N-2): k = i+2 for j in range(i+1,N-1): while k < N: if S[i] + S[j] <= S[k]: tri_num = tri_num + k-j-1 if k == i+2: k+=1 break if k == N-1: tri_num = tri_num + k-j break k+=1 print(tri_num)
N=int(eval(input())) S=list(map(int,input().split( ))) S = sorted(S,reverse=True) tri_num=0 for i in range(N-2): j = i+1 k = N-1 while k > j: if S[i] < S[j] + S[k]: tri_num += k-j j+=1 else: k-=1 print(tri_num)
p02888
import bisect import copy def two_int(): N, K = list(map(int, input().split())) return N,K def one_int(): return int(eval(input())) def one_str(): return eval(input()) def many_int(): return list(map(int, input().split())) N=one_int() L=many_int() L=sorted(L) sankaku_dict={} count=0 for i in range(len(L)-1): low=L[i] Lcopy = copy.deepcopy(L) Lcopy.remove(low) for j in range(i+1, len(L)): #すでに探索済みか判定 high = L[j] Lcopy2 = Lcopy[j:] #下限を取得 # under_index = bisect.bisect_right(Lcopy2,high-low) #上限を取得 high_index = bisect.bisect_left(Lcopy2,high+low) # for va in Lcopy2[under_index:high_index]: # temp = sorted([high,low, va]) # temp = (temp[0]*10**8) + (temp[1]*10**4) + temp[2] # sankaku_dict[temp]=1 count+=len(Lcopy2[:high_index]) # len(sankaku_dict) print(count) # print(len(sankaku_dict.keys()))
import bisect import copy def two_int(): N, K = list(map(int, input().split())) return N,K def one_int(): return int(eval(input())) def one_str(): return eval(input()) def many_int(): return list(map(int, input().split())) N=one_int() L=many_int() L=sorted(L) sankaku_dict={} count=0 for i in range(len(L)-1): low=L[i] for j in range(i+1, len(L)): #すでに探索済みか判定 high = L[j] #上限を取得 high_index = bisect.bisect_left(L[j+1:], high+low) count+=high_index#len(L[j+1:high_index]) # len(sankaku_dict) print(count) # print(len(sankaku_dict.keys()))
p02888
N = int(eval(input())) L = list(map(int, input().split())) total = 0 L.sort(reverse=True) for i in range(N-2): a = L[i] for j in range(i+1, N-1): b = L[j] for k in range(j+1, N): if a - b < L[k]: total += 1 print(total)
N = int(eval(input())) L = list(map(int, input().split())) L.sort(reverse=True) total = 0 for i in range(N-2): a = L[i] for j in range(i+1, N-1): b = L[j] # j以降の項で、初めてL[k] < a-bとなるkを二分探索する if L[-1] > a - b: # 一番最後の項でもOKな場合(j+1以降全部OK) total += N - j - 1 elif L[j+1] <= a - b: # 一番最初の項からNGな場合 continue else: head = j+1 # head はL[head] > a - bを満たす(OK側) tail = N-1 # tail はL[tail] <= a - bを満たす(NG側) while head+1 != tail: if L[(head + tail)//2] > a - b: # 中間地点はOK側 head = (head + tail) // 2 else: # 中間地点はNG側 tail = (head + tail) // 2 total += head - j print(total)
p02888
import itertools n = int(eval(input())) L = list(map(int, input().split())) ans = 0 L.sort() for i in range(n-2): for j in range(i+1, n-1): for k in range(j+1, n): if L[k] < L[i]+L[j]: ans += 1 else: break print(ans)
import bisect n = int(eval(input())) L = list(map(int, input().split())) ans = 0 L.sort() for i in range(n - 2): for j in range(i + 1, n - 1): idx = bisect.bisect_left(L, L[i] + L[j]) ans += max(0, idx - j - 1) print(ans)
p02888
import bisect n=int(eval(input())) l=sorted(map(int,input().split())) t=0 for i in range(n): for j in range(i+1,n): a = j+1 b = bisect.bisect_left(l,l[i]+l[j]) t += b-a if b>a else 0 print(t)
import bisect n=int(eval(input())) l=sorted(map(int,input().split())) t=0 for i in range(n): for j in range(i+1,n): t += bisect.bisect_left(l,l[i]+l[j]) - j - 1 print(t)
p02888
import itertools def triangleChecker(combi): a, b, c = combi if a >= b + c : return False else: if b >= c + a: return False else : if c >= a + b: return False else : return True N = int(eval(input())) numbers = [int(i) for i in input().split()] combinations = list(itertools.combinations(numbers, 3)) counter = 0 for combi in combinations: if triangleChecker(combi): # print(combi) counter += 1 print(counter)
def triangleChecker(a, b, c): if a >= b + c : return False else: if b >= c + a: return False else : if c >= a + b: return False else : return True N = int(eval(input())) numbers = [int(i) for i in input().split()] counter = 0 for a in range(len(numbers)-2): for b in range(a+1, len(numbers)-1): for c in range(b+1, len(numbers)): if triangleChecker(numbers[a], numbers[b], numbers[c]): # print(combi) counter += 1 print(counter)
p02888
n=int(eval(input())) l=list(map(int,input().split())) def quicksort(a, left: int, right: int): #a[left]〜a[right]をクイックソート pl=left pr=right pivot= a[(left +right)//2] #print(f'a[{left}]~a[{right}] : ', *a[left : right +1]) while pl<=pr: while a[pl] <pivot: pl+=1 while a[pr] >pivot: pr-=1 if pl <= pr: a[pl], a[pr]= a[pr], a[pl] pl+=1 pr-=1 if left < pr: quicksort(a, left, pr) if pl < right: quicksort(a, pl, right) def quick_sort(a): quicksort(a, 0, len(a)-1) quick_sort(l) count=0 for i in range(n-2): for j in range(i+1,n-1): x=l[i]+l[j] for k in range(j+1,n): if x<=l[k]: break count+=1 print(count)
n=int(eval(input())) l=list(map(int,input().split())) from bisect import bisect_left l.sort() count=0 for a in range(n-2): for b in range(a+1,n-1): idx=bisect_left(l,l[a]+l[b],lo=b) count+=idx-(b+1) print(count)
p02888
import itertools N = int(eval(input())) L = list(map(int,input().split())) tri = list(itertools.combinations(L,3)) cnt = 0 for a,b,c in tri: if a < (b+c): if b < c+a: if c < a+b: cnt += 1 print(cnt)
import itertools N = int(eval(input())) L = list(map(int,input().split())) L.sort() tri = list(itertools.combinations(L,3)) cnt = 0 for a,b,c in tri: if a < (b+c) and b < (c+a) and c < (a+b): cnt += 1 print(cnt)
p02888
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() buff = [] ll = [] for i in range(N): l_b = [] for j in range(i+1, N): l_b.append(L[i]+L[j]) ll.append(L[i]+L[j]) if i != N-1: buff.append(l_b) # buff.sort() # print(L) # print(buff) ans = 0 _len = len(buff) for i in range(_len): for j in range(len(buff[i])): index = bisect.bisect_left(L[i+j+1+1:], buff[i][j]) # print(L[i+j+1+1:]) # print(index, buff[i][j]) ans += index print(ans) # for i in range(N): # index = bisect.bisect_left(buff, L[i]) # print(index) # ans += index # print(ans)
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() buff = [] # ll = [] for i in range(N): l_b = [] for j in range(i+1, N): l_b.append(L[i]+L[j]) # ll.append(L[i]+L[j]) if i != N-1: buff.append(l_b) # buff.sort() # print(L) # print(buff) ans = 0 _len = len(buff) for i in range(_len): for j in range(len(buff[i])): index = bisect.bisect_left(L[i+j+1+1:], buff[i][j]) # print(L[i+j+1+1:]) # print(index, buff[i][j]) ans += index print(ans) # for i in range(N): # index = bisect.bisect_left(buff, L[i]) # print(index) # ans += index # print(ans)
p02888
N = int(eval(input())) L = list(map(int,input().split())) L = sorted(L) cnt = 0 for a in range(N-2): for b in range(a+1,N-1): for c in range(b+1,N): if L[c] < L[a] + L[b]: cnt += 1 print(cnt)
N = int(eval(input())) L = list(map(int,input().split())) L = sorted(L) def binary_search(a,b): top = N bottom = b c = (top + bottom)//2 while top - bottom > 1: #print(a,b,":",top,bottom,c) if L[c] < L[a] + L[b]: bottom = c else: top = c c = (top + bottom)//2 return top - 1 cnt = 0 for a in range(N-2): for b in range(a+1,N-1): cnt += binary_search(a,b) - b print(cnt)
p02888
from collections import deque def hoge(A,B): sum = 0 for i in range(B): sum += A A -= 1 return sum N = int(eval(input())) L = list(map(int, input().split())) L.sort(reverse=True) List = deque() Sum = 0 for i in range(1, N - 1): for j in range(i + 1, N): List.append(L[i] + L[j]) for i in range(N - 2): for j in range(hoge(N-2,i),len(List)): if List[j] > L[i]: Sum += 1 print(Sum)
import bisect N = int(eval(input())) L = list(map(int, input().split())) Sum = 0 L.sort(reverse=False) for i in reversed(list(range(2,N))): for j in reversed(list(range(1,i))): hoge = 0 hoge = j - bisect.bisect(L, L[i] - L[j]) if hoge < 0: hoge = 0 Sum += hoge print(Sum)
p02888
n = int(eval(input())) l = list(map(int, input().split())) l.sort() count = 0 for i in range(n - 2): for j in range(i + 1, n - 1): for k in range(j + 1, n): if l[k] < l[i] + l[j]: count += 1 else: break print(count)
import bisect n = int(eval(input())) l = list(map(int, input().split())) l.sort() count = 0 for i in range(n - 1): for j in range(i + 1, n): itr = bisect.bisect_left(l, l[i] + l[j]) count += itr - 1 - j print(count)
p02888
import itertools n = int(eval(input())) l = list(map(int, input().split())) count = 0 for a, b, c in itertools.combinations(l, 3): if a < b+c and b < a+c and c < a+b: count += 1 print(count)
import bisect n = int(eval(input())) l = list(map(int, input().split())) l.sort() count = 0 for i in range(n-2): for j in range(i+1, n-1): a = l[i]+l[j] index = bisect.bisect_left(l, a) count += index - j -1 print(count)
p02888
import itertools N=int(eval(input())) L=list(map(int, input().split())) S=0 C=list(itertools.combinations(L,3)) for i in range(len(C)): if C[i][0]+C[i][1]>C[i][2] and C[i][1]+C[i][2]>C[i][0] and C[i][2]+C[i][0]>C[i][1]: S+=1 print(S)
import itertools N=int(eval(input())) L=list(map(int, input().split())) S=0 C=list(itertools.combinations(L,3)) for i in range(len(C)): if max(C[i])*2<sum(C[i]): S+=1 print(S)
p02888
import itertools N = int(eval(input())) L = list(map(int, input().split())) L = list(itertools.combinations(L, 3)) cnt = 0 for l in L: l = list(l) m = max(l) l.remove(m) if m < sum(l): cnt += 1 print(cnt)
N = int(eval(input())) L = list(map(int, input().split())) L.sort(reverse=True) cnt = 0 for i in range(N-2): a = L[i] for j in range(i+1, N-1): b = L[j] for k in range(j+1, N): c = L[k] if b > c and a < b+c: cnt += 1 print(cnt)
p02888
import math from bisect import bisect_right from bisect import bisect_left n = int(eval(input())) L = list(map(int,input().split())) L.sort() c = 0 for i in range(n-2): for j in range(i+1,n-1): lg = L[i] + L[j] st = L[j] - L[i] if lg > L[j+1] and st < L[n-1]: if lg > L[n-1]: m1 = n - (j+1) else: m1 = bisect_left(L[j+1:],lg) if st > L[j+1]: m2 = 0 else: m2 = bisect_right(L[j+1:],st) c += max(0,m1-m2) print (c)
import math from bisect import bisect_right from bisect import bisect_left n = int(eval(input())) L = list(map(int,input().split())) L.sort() c = 0 for i in range(n-2): for j in range(i+1,n-1): lg = L[i] + L[j] st = L[j] - L[i] if lg > L[j+1] and st < L[n-1]: if lg > L[n-1]: m1 = n - (j+1) else: m1 = bisect_left(L[j+1:],lg) c += max(0,m1) print (c)
p02888
import bisect N = int(eval(input())) L = sorted(list(map(int, input().split(" ")))) cnt = 0 for i in range(0, len(L)-2): l1 = L[i] rest = L[i+1:] for i2 in range(0, len(rest)-1): l2 = rest[i2] if l2 < l1 / 2: break if l2 < l1 / 2: break rest2 = rest[i2+1:] cnt += bisect.bisect_left(rest2, l2+l1) - bisect.bisect_left(rest2, l2-l1) print(cnt)
from bisect import bisect_right N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(N): for j in range(i): m = L[i]-L[j] k = bisect_right(L, m) n = max(0, j-k) ans+=n print(ans)
p02888
n=int(eval(input())) l=list(map(int,input().split())) d={} for li in l: if li in d: d[li]+=1 else: d[li]=1 l=list(set(l)) l.sort() ans=0 for i in range(len(l)): for j in range(i+1): ls=[li for li in l[:j+1] if li>l[i]-l[j]] for lsi in ls: if l[i]==l[j] and l[j]==lsi: ans+=d[l[i]]*(d[l[i]]-1)*(d[l[i]]-2)//6 #print(str(ans)+' '+str(l[i])+' '+str(l[j])+' '+str(lsi)) elif l[i]==l[j]: ans+=d[l[i]]*(d[l[i]]-1)*d[lsi]//2 elif l[j]==lsi: ans+=d[l[i]]*d[l[j]]*(d[l[j]]-1)//2 else: ans+=d[l[i]]*d[l[j]]*d[lsi] print(ans)
n=int(eval(input())) l=sorted(list(map(int,input().split()))) import bisect ans=0 for i in range(n): for j in range(i): index=bisect.bisect_right(l,l[i]-l[j])#このindexよりも右側のものでかつjより左のものが当てはまるもの ans += max(j - index,0) print(ans)
p02888
N = int(eval(input())) L = list(map(int, input().split())) L.sort() cnt = 0 for i in range(N-2): for j in range(i+1, N-1): for k in range(j+1, N): if L[k] >= L[i]+L[j]: break else: cnt += 1 print(cnt)
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() cnt = 0 for i in range(N-2): for j in range(i+1, N-1): cnt += bisect.bisect_left(L, L[i]+L[j]) - (j+1) print(cnt)
p02888
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(N): for j in range(i+1, N): lower_bound = bisect.bisect_left(L, L[i]+L[j]) for k in range(lower_bound): if k == i or k == j: continue if L[i]+L[j] > L[k] and L[i]+L[k] > L[j] and L[j]+L[k] > L[i]: ans += 1 # print(L[i], L[j]) # print(bisect.bisect_left(L, L[i]+L[j])-2) # ans += max(bisect.bisect_left(L, L[i]+L[j])-2, 0) print((ans//3))
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(N): for j in range(i+1, N): lower_bound_i_plus_j = bisect.bisect_left(L, L[i]+L[j]) ans += lower_bound_i_plus_j - (j+1) print(ans)
p02888
import bisect N=int(eval(input())) A=list(map(int,input().split())) A.sort() ans=0 for i in range(N-2): for j in range(i+1,N-1): p = A[i]+A[j] ans += bisect.bisect_left(A[j+1:],p) print(ans)
from bisect import bisect_left N = int(eval(input())) L = [int(x) for x in input().split()] L.sort() ans = 0 for i in range(N-2): for j in range(i+1,N-1): ans += bisect_left(L, L[i]+L[j])-j-1 print(ans)
p02888
from itertools import combinations n = int(eval(input())) ls = list(map(int, input().split())) ls.sort() count = 0 # for a, b, c in combinations(ls, 3): # if a < b + c and b < c + a and c < a + b: # count += 1 for j in range(n - 1, 1, -1): last = ls[j] for a, b in combinations(ls[:j], 2): if a + b > last: count += 1 print(count)
# from itertools import combinations from bisect import bisect_left n = int(eval(input())) ls = list(map(int, input().split())) ls.sort() count = 0 # for a, b, c in combinations(ls, 3): # if a < b + c and b < c + a and c < a + b: # count += 1 # for j in range(n - 1, 1, -1): # last = ls[j] # for a, b in combinations(ls[:j], 2): # if a + b > last: # count += 1 for i in range(n - 1, 1, -1): for j in range(i - 1, 0, -1): idx = bisect_left(ls[:j], ls[i] - ls[j] + 1) if idx < j: count += j - idx print(count)
p02888
N=int(eval(input())) L=list(map(int,input().split())) ans=0 c=[0]*1001 for i in L:c[i]+=1 for i in range(1000):c[i+1]=c[i]+c[i+1] for i in range(N): for j in range(N): if i!=j: l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000) s=c[r]-c[l] if l<L[i] and L[i]<=r:s-=1 if l<L[j] and L[j]<=r:s-=1 ans+=max(s,0) print((ans//6))
N=int(eval(input())) L=list(map(int,input().split())) ans=0 c=[0]*1001 for i in L:c[i]+=1 for i in range(1000):c[i+1]=c[i]+c[i+1] for i in range(N): for j in range(i+1,N): l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000) s=c[r]-c[l] if l<L[i] and L[i]<=r:s-=1 if l<L[j] and L[j]<=r:s-=1 ans+=max(s,0) print((ans//3))
p02888
N=int(eval(input())) L=list(map(int,input().split())) ans=0 c=[0]*1001 for i in L:c[i]+=1 for i in range(1000):c[i+1]=c[i]+c[i+1] for i in range(N-1): for j in range(i+1,N): l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000) s=c[r]-c[l] if l<L[i] and L[i]<=r:s-=1 if l<L[j] and L[j]<=r:s-=1 ans+=max(s,0) print((ans//3))
N=int(eval(input())) L=list(map(int,input().split())) ans=0 c=[0]*1001 for i in L:c[i]+=1 for i in range(1000):c[i+1]=c[i]+c[i+1] for i in range(N-1): for j in range(i+1,N): l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000) ans+=c[r]-c[l] if l<L[i] and L[i]<=r:ans-=1 if l<L[j] and L[j]<=r:ans-=1 print((ans//3))
p02888
from itertools import combinations n = int(eval(input())) l = list(map(int, input().split())) ans = 0 combi = list(combinations(l, 3)) for i in range(len(combi)): max_num = max(combi[i]) if max_num < (sum(combi[i]) - max_num): ans += 1 print(ans)
n = int(eval(input())) l = list(map(int, input().split())) l.sort(reverse = True) ans = 0 for i in range(n): for j in range(i+1, n): for k in range(j+1, n): max_num = max(l[i], l[j], l[k]) if max_num < ((l[i]+l[j]+l[k]) - max_num): ans += 1 else: break print(ans)
p02888
import bisect import copy ans = 0 n = int(eval(input())) l = [int(x) for x in input().split()] l.sort() for i in range(n-2): ln = copy.copy(l) del ln[:i+1] for j in range(i+1, n-1): a = l[i] b = l[j] del ln[0] k = bisect.bisect_right(ln, b - a) m = bisect.bisect_left(ln, a + b) ans += (m - k) print(ans)
import bisect import copy ans = 0 n = int(eval(input())) l = [int(x) for x in input().split()] l.sort() for i in range(n-2): for j in range(i+1, n-1): a = l[i] b = l[j] m = bisect.bisect_left(l, a + b) ans += (m - j - 1) print(ans)
p02888
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(N): for j in range(i + 1, N): l = L[j] - L[i] r = L[j] + L[i] ans += bisect.bisect_left(L[j+1:], r) - bisect.bisect_right(L[j+1:], l) print(ans)
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(N): for j in range(i + 1, N): r = L[j] + L[i] ans += bisect.bisect_left(L[j+1:], r) print(ans)
p02888
import heapq N = int(eval(input())) L_list = list(map(int,input().split())) L_list.sort() ans = 0 list = [] M = 0 for i in range(N-1): for j in range(N-i-1): list.append((L_list[i]+L_list[j+i+1],i,j+i+1)) M += 1 for k in range(N): for z in range(M): if L_list[k] < list[z][0]: if (k > list[z][2]): ans += 1 print(ans)
import bisect N = int(eval(input())) l = list(map(int, input().split())) l.sort() ans = 0 for i in range(N - 2): for j in range(i + 1, N - 1): insert_index = bisect.bisect_left(l, l[i] + l[j],j) #l[i] + l[j]を挿入できる点を探す。同じ者がlにすでにある時は、一番左になるようにする ans += (insert_index - j - 1) print(ans)
p02888
import itertools n=int(eval(input())) l=list(map(int,input().split())) def triangle(num): a,b,c=num if a<(b+c) and b<(a+c) and c<(a+b): return True else: return False ans=0 for num in itertools.combinations(l,3): ans+=triangle(num) print(ans)
from bisect import bisect_left n=int(eval(input())) l=list(map(int,input().split())) l.sort() ans=0 for a in range(n-2): for b in range(a+1,n-1): ans+=max(0,bisect_left(l, l[a] + l[b])-b-1) print(ans)
p02888
n = int(eval(input())) l = list(map(int,input().split())) l.sort(reverse = 1) ans = 0 flg = 0 for i in range(n-2): for j in range(i+1,n-1): indL = j indR = len(l)-1 while 1: if indL == indR: flg = 1 break if l[i] < l[indL] + l[indR]: ans += (indR - indL) break else: indR -= 1 if flg == 1: flg = 0 break print(ans)
n = int(eval(input())) l = list(map(int,input().split())) l.sort(reverse = 1) ans = 0 for i in range(n-2): indL = i+1 indR = len(l)-1 while 1: if indL == indR: break if l[i] < l[indL] + l[indR]: ans += (indR - indL) indL += 1 else: indR -= 1 print(ans)
p02888
n = int(eval(input())) L = list(map(int, input().split())) cnt = n * (n - 1) * (n - 2) / 6 jud = [] for i in range(n): for j in range(n): if i > j: jud.append(L[i] + L[j]) for p in range(len(jud)): for m in range(n): if L[m] >= jud[p]: cnt -= 1 if cnt < 0: print((0)) else: print((int(cnt)))
from _bisect import bisect_left from _bisect import bisect_right n = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) cnt = 0 for a in range(n): for b in range(n): if a < b: c = bisect_left(L, L[a] + L[b], b) cnt += c - b - 1 print(cnt)
p02888
from itertools import combinations n=int(eval(input())) l=[int(i)for i in input().split()] l2=list(combinations(l, 3)) sum=0 for i in range(len(l2)): a=l2[i][0] b=l2[i][1] c=l2[i][2] if(a<b+c and b<c+a and c<a+b): sum+=1 print(sum)
n=int(eval(input())) l=sorted([int(i)for i in input().split()]) sum=0 for i in range(n-2): for j in range(i+1, n-1): for k in range(j+1, n): if l[i]+l[j]>l[k]: sum+=1 else: break print(sum)
p02888
from bisect import* n,*l=list(map(int,open(0).read().split())) l.sort() A=0 for i in range(n): a=l[i] for j in range(i+1,n): b=l[j] k1=max(j+1,bisect(l,b-a)) k2=bisect_left(l,a+b) A+=max(0,k2-k1) print(A)
from bisect import* n,*l=list(map(int,open(0).read().split())) l.sort() print((sum(~j+bisect_left(l,l[i]+l[j],j+1)for i in range(n)for j in range(i+1,n))))
p02888
from bisect import* n,*l=list(map(int,open(0).read().split())) l.sort() print((sum(~j+bisect_left(l,l[i]+l[j],j+1)for i in range(n)for j in range(i+1,n))))
from bisect import* _,l=open(0) l=sorted(map(int,l.split())) e=enumerate print((sum(j-bisect(l,a-b,0,j)for i,a in e(l)for j,b in e(l[:i]))))
p02888
from bisect import* _,l=open(0) l=sorted(map(int,l.split())) e=enumerate print((sum(j-bisect(l,a-b,0,j)for i,a in e(l)for j,b in e(l[:i]))))
from bisect import* _,l=open(0) l=sorted(map(int,l.split())) *e,=enumerate(l) print((sum(j-bisect(l,a-b,0,j)for i,a in e for j,b in e[:i])))
p02888
n = int(eval(input())) ls = list(map(int, input().split())) from itertools import combinations p = list(combinations(ls, 3)) result = 0 for a, b, c in p: if a < (b+c) and b < (c+a) and c < (a+b): result += 1 print(result)
n = int(eval(input())) ls = list(map(int, input().split())) from itertools import combinations p = combinations(ls, 3) result = 0 for a, b, c in p: if a < (b+c) and b < (c+a) and c < (a+b): result += 1 print(result)
p02888
#!/usr/bin/python3 # -*- coding: utf-8 -*- from bisect import bisect_left,bisect_right ## input N = int(eval(input())) Ln = list(map(int, input().split())) Ln = sorted(Ln) ret = 0 for a in range(N-1,1,-1): for b in range(a-1,0,-1): if Ln[a] >= Ln[b] + Ln[b-1]: continue else: c = b-1 while Ln[a]<Ln[b]+Ln[c] and c>=0: ret += 1 c -= 1 print(ret)
#!/usr/bin/python3 # -*- coding: utf-8 -*- ## Input N = int(eval(input())) Ln = list(map(int, input().split())) Ln = sorted(Ln) ## Solve ret = 0 for a in range(2,N): c = 0 for b in range(a-1,0,-1): if c >= b: continue else: while (c<b and Ln[a]>=Ln[b]+Ln[c]): c += 1 ret += max(b-c,0) print(ret)
p02888
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) import bisect def main(): N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 for i in range(N-2): for j in range(N-1): if j > i: ans += bisect.bisect_left(L[j+1:], L[i] + L[j]) - bisect.bisect_left(L[j+1:], L[j] - L[i]) print(ans) if __name__ == '__main__': main()
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) import bisect def main(): N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 for i in range(N-2): for j in range(N-1): if j > i: ans += bisect.bisect_left(L[j+1:], L[i] + L[j]) print(ans) if __name__ == '__main__': main()
p02888
import bisect n = int(eval(input())) l_li = [int(x) for x in input().split()] l_li.sort() count = 0 for i in range(n): for j in range(i+1,n): wa = l_li[i] + l_li[j] target = l_li[j+1:] count += bisect.bisect_left(target, wa) print(count)
import bisect n = int(eval(input())) l_li = [int(x) for x in input().split()] l_li.sort() count = 0 for i in range(n): for j in range(i+1,n): wa = l_li[i] + l_li[j] count += bisect.bisect_left(l_li, wa) - j - 1 print(count)
p02888
import sys import bisect n = int(eval(input())) *l, = list(map(int, input().split())) l = sorted(l) count = 0 for i in range(n-2): for j in range(i+1, n-1): c = bisect.bisect_left(l[j+1:], l[i]+l[j]) count += c print(count)
import sys import bisect n = int(eval(input())) *l, = list(map(int, input().split())) l.sort() ans = 0 for i in range(n-1): for j in range(i+1, n): c = bisect.bisect_left(l, l[i] + l[j], j+1) ans += c - j - 1 print(ans)
p02888
import bisect n = int(eval(input())) *l, = sorted(map(int, input().split())) ans = 0 for i in range(n): for j in range(i+1, n): bc = l[i]+l[j] index = bisect.bisect_left(l, bc) ans += len(l[j+1:index]) print(ans)
import bisect n = int(eval(input())) *l, = sorted(map(int, input().split())) ans = 0 for i in range(n): for j in range(i+1, n): bc = l[i]+l[j] ans += bisect.bisect_left(l, bc)-(j+1) print(ans)
p02888
N = int(eval(input())) L = list(map(int, input().split())) L.sort(reverse=True) ans = 0 for i in range(0, N-2): a = L[i] for j in range(i+1, N-1): b = L[j] if L[-1] > a - b: ans += len(L[j+1:]) else: for k in range(j+1, N): c = L[k] if c > a - b: ans += 1 else: break print(ans)
import bisect n = int(eval(input())) l = list(map(int, input().split())) l.sort() ''' リストをソートし、a, b <= cとなるようにa, bを取り出せば、 a < b + c, b < c + a という条件は満たすので、 a, bを取り出したとき、c < a + b を満たすcを二分探索で探す ''' ans = 0 for i in range(n-2): for j in range(i+1, n-1): ans += bisect.bisect_left(l, l[i]+l[j]) - j - 1 print(ans)
p02888
import bisect N = int(eval(input())) L = list(map(int,input().split())) L.sort() ans = 0 for i in range(N-2): a = L[i] for j in range(i+1,N-1): b = L[j] c = bisect.bisect_left(L,a+b) ans += c-j-1 print(ans)
n = int(eval(input())) l = list(map(int,input().split())) l.sort() from bisect import bisect_left ans = 0 for i in range(n): a = l[i] for j in range(i+1,n): b = l[j] ans += bisect_left(l,a+b)-j-1 print(ans)
p02888
N=int(eval(input())) L= [int(i) for i in input().split()] L.sort() diff_L=[] for i in range(1,N-1): diff=[] for j in range(i+1,N): diff.append(L[j]-L[i]) diff_L.append(diff) total=0 for i in range(N-2): for j in range(i,N-2): for elem in diff_L[j]: if elem < L[i]: total += 1 else: break print(total)
N=int(eval(input())) L= [int(i) for i in input().split()] L.sort() larger=[N] prev=1 for i in range(N): larger= larger +[N-i]*(L[i]-prev) prev=L[i] total=0 for i in range(N-2): low=L[:i+1] high=L[i+1:] for j in range(len(high)-1): diff = high[j+1]-high[0] if diff >= len(larger): count = 0 else: count = max(larger[diff] - (N-i-1),0) total += count print(total)
p02888