id
stringlengths
24
27
content
stringlengths
37
384k
max_stars_repo_path
stringlengths
51
51
condefects-python_data_1201
X = int(input()) ans = (X+10-1)/10 print(ans) X = int(input()) ans = (X+10-1)//10 print(ans)
ConDefects/ConDefects/Code/abc345_b/Python/54755519
condefects-python_data_1202
n,k,*A = map(int,open(0).read().split()) f = 1 x = 0 for d in range(50): if k>>d&1: g = 0 for a in A: g ^= f<<a f = g g = 0 for i in range(f.bit_length()): if i&1: x ^= (f>>i&1)<<d g ^= (f>>i&1)<<(i>>1) f = g print(x) n,k,*A = map(int,open(0).read().split()) f = 1 x = 0 for d in range(50): if k>>d&1: g = 0 for a in A: g ^= f<<a f = g g = 0 for i in range(f.bit_length()): if (n&1 or not k&-1<<d+1) and i&1: x ^= (f>>i&1)<<d g ^= (f>>i&1)<<(i>>1) f = g print(x)
ConDefects/ConDefects/Code/arc156_d/Python/41134585
condefects-python_data_1203
import numpy as np a, b, d = map(int, input().split()) theta = np.radians(d, dtype=np.float128) c, s = np.cos(theta, dtype=np.float128), np.sin(theta, dtype=np.float128) R = np.array(((c, -s), (s, c)), dtype=np.float128) ret = np.dot(R, np.array((a, b), dtype=np.float128)) print(ret) import numpy as np a, b, d = map(int, input().split()) theta = np.radians(d, dtype=np.float128) c, s = np.cos(theta, dtype=np.float128), np.sin(theta, dtype=np.float128) R = np.array(((c, -s), (s, c)), dtype=np.float128) ret = np.dot(R, np.array((a, b), dtype=np.float128)) print(*ret)
ConDefects/ConDefects/Code/abc259_b/Python/45494335
condefects-python_data_1204
import math a, b, c = map(int, input().split()) x = a * round(math.cos(math.radians(c)), 8) + b * round(math.sin(math.radians(c)), 8) y = a * round(math.sin(math.radians(c)), 8) + b * round(math.cos(math.radians(c)), 8) print(x, y) import math a, b, c = map(int, input().split()) x = a * round(math.cos(math.radians(c)), 8) - b * round(math.sin(math.radians(c)), 8) y = a * round(math.sin(math.radians(c)), 8) + b * round(math.cos(math.radians(c)), 8) print(x, y)
ConDefects/ConDefects/Code/abc259_b/Python/45346319
condefects-python_data_1205
import math a, b, d = map(int, input().split()) sind = math.sin(math.radians(d)) cosd = math.cos(math.radians(d)) ans = [cosd*a-sind*b, sind*a+cosd*b] print(ans) import math a, b, d = map(int, input().split()) sind = math.sin(math.radians(d)) cosd = math.cos(math.radians(d)) ans = [cosd*a-sind*b, sind*a+cosd*b] print(*ans)
ConDefects/ConDefects/Code/abc259_b/Python/45800478
condefects-python_data_1206
N = int(input()) T = input() b = 26 h = 2**61 - 1 H = pow(b,N-1,h) HH = pow(b,-1,h) ans = 0 A = [] for i in range(N): if i == 0: X = 0 Y = 0 Z = 0 for j in range(N,2*N): Z *= b Z += (ord(T[j])-97) Z %= h for j in range(N): n = N - 1 - j Y *= b Y += (ord(T[n])-97) Y %= h if Y == Z: ans = 1 A.append(i) X *= b X %= h X += (ord(T[i])-97) Y -= (ord(T[i])-97) Y *= HH Y %= h Y += (ord(T[i+N])-97)*H Y %= h Z -= (ord(T[i+N])-97)*pow(b,N-1-i,h) Z %= h n = N - 1 - i S = X*pow(b,n,h) % h S += Z S %= h if S == Y: ans = 1 A.append(i+1) if ans == 0: print(-1) exit() i = A.pop() ans = [] for x in range(i): ans.append(T[x]) for x in range(i+N,2*N): ans.append(T[x]) S = ''.join(ans) print(i) print(S) N = int(input()) T = input() b = 26 h = 2**61 - 1 H = pow(b,N-1,h) HH = pow(b,-1,h) ans = 0 A = [] for i in range(N): if i == 0: X = 0 Y = 0 Z = 0 for j in range(N,2*N): Z *= b Z += (ord(T[j])-97) Z %= h for j in range(N): n = N - 1 - j Y *= b Y += (ord(T[n])-97) Y %= h if Y == Z: ans = 1 A.append(i) X *= b X %= h X += (ord(T[i])-97) Y -= (ord(T[i])-97) Y *= HH Y %= h Y += (ord(T[i+N])-97)*H Y %= h Z -= (ord(T[i+N])-97)*pow(b,N-1-i,h) Z %= h n = N - 1 - i S = X*pow(b,n,h) % h S += Z S %= h if S == Y: ans = 1 A.append(i+1) if ans == 0: print(-1) exit() i = A.pop() ans = [] for x in range(i): ans.append(T[x]) for x in range(i+N,2*N): ans.append(T[x]) S = ''.join(ans) print(S) print(i)
ConDefects/ConDefects/Code/abc284_f/Python/45510158
condefects-python_data_1207
import bisect, heapq, sys, math, copy, itertools, decimal from collections import defaultdict, deque sys.setrecursionlimit(10**7) def INT(): return int(input()) def MI(): return map(int, input().split()) def MS(): return map(str, input().split()) def LI(): return list(map(int, input().split())) def LS(): return list(map(str, input().split())) def pr_line(itr): print(*itr, sep='\n') def pr_mtx(matrix): [print(*row) for row in matrix] INF = 1<<62 T = INT() ANS = [] for _ in range(T): A, B = MI() Nset = set() for i in range(1, B+1): if i ** 2 > B: break Nset.add(i) Nset.add(math.ceil(B / i)) ans = INF for n in Nset: k = math.ceil(B / n) x = max(0, math.ceil(B / k) - A) y = (A + x) * k - B ans = min(ans, x + y) ANS.append(ans) pr_line(ANS) import bisect, heapq, sys, math, copy, itertools, decimal from collections import defaultdict, deque sys.setrecursionlimit(10**7) def INT(): return int(input()) def MI(): return map(int, input().split()) def MS(): return map(str, input().split()) def LI(): return list(map(int, input().split())) def LS(): return list(map(str, input().split())) def pr_line(itr): print(*itr, sep='\n') def pr_mtx(matrix): [print(*row) for row in matrix] INF = 1<<62 T = INT() ANS = [] for _ in range(T): A, B = MI() Nset = set() for i in range(1, B+1): if i ** 2 > B: break Nset.add(i) Nset.add(math.ceil(B / i)) ans = INF for n in Nset: k = math.ceil(B / n) x = max(0, math.ceil(B / k) - A) y = (A + x) * k - B ans = min(ans, x + y) ANS.append(ans) pr_line(ANS)
ConDefects/ConDefects/Code/arc150_b/Python/43756504
condefects-python_data_1208
from math import ceil for _ in range(int(input())): a, b = map(int, input().split()) max_k = ceil((b-1)/a) ans = 10**18 for k in range(1, max_k + 1): x = max(ceil(b / k) - a, 0) y = k * (a + x) - b if x+y < ans: ans = x+y print(ans) from math import ceil for _ in range(int(input())): a, b = map(int, input().split()) max_k = ceil(b/a) ans = 10**18 for k in range(1, max_k + 1): x = max(ceil(b / k) - a, 0) y = k * (a + x) - b if x+y < ans: ans = x+y print(ans)
ConDefects/ConDefects/Code/arc150_b/Python/40583945
condefects-python_data_1209
def solve(a,b): sq_b = int(b**.5)+10 ans = a for k in range(1,sq_b): tmp = (k+1)*max(0,((b-1)//k)+1-a)+k*a-b ans = min(ans,tmp) for q in range(1,sq_b): k = (b-1)//(q+1)+1 tmp = (k+1)*max(0,((b-1)//k)+1-a)+k*a-b ans = min(ans,tmp) return ans for _ in range(int(input())): a,b=map(int,input().split()) ans = solve(a,b) print(ans) def solve(a,b): sq_b = int(b**.5)+10 ans = a for k in range(1,sq_b): tmp = (k+1)*max(0,((b-1)//k)+1-a)+k*a-b ans = min(ans,tmp) for q in range(sq_b): k = (b-1)//(q+1)+1 tmp = (k+1)*max(0,((b-1)//k)+1-a)+k*a-b ans = min(ans,tmp) return ans for _ in range(int(input())): a,b=map(int,input().split()) ans = solve(a,b) print(ans)
ConDefects/ConDefects/Code/arc150_b/Python/42077824
condefects-python_data_1210
import math T = int(input()) for _ in range(T): A, B = map(int, input().split()) def f(k): return (k + 1) * max(((B + k - 1) // k), A) - (A + B) bo = math.floor((B - 1) ** 0.5) if B == 1: print(f(1)) continue ans = 10 ** 18 for k in range(1, bo + 1): ans = min(ans, f(k)) for t in range(bo): k = (B - 1) // (t + 1) + 1 ans = min(ans, f(k)) print(ans) import math T = int(input()) for _ in range(T): A, B = map(int, input().split()) def f(k): return (k + 1) * max(((B + k - 1) // k), A) - (A + B) bo = math.floor((B - 1) ** 0.5) if B == 1: print(f(1)) continue ans = 10 ** 18 for k in range(1, bo + 1): ans = min(ans, f(k)) for t in range(bo + 1): k = (B - 1) // (t + 1) + 1 ans = min(ans, f(k)) print(ans)
ConDefects/ConDefects/Code/arc150_b/Python/40524794
condefects-python_data_1211
T = int(input()) for _ in range(T): A, B = map(int, input().split()) if A > B: print(A-B) continue ans = 10**18 for k in range(1, 10**7): if A*k > B: ans = min(ans, A*k-B) break d = B-A*k x = -(-d//k) ans = min(ans, (A+x)*k-B+x) print(ans) T = int(input()) for _ in range(T): A, B = map(int, input().split()) if A > B: print(A-B) continue ans = 10**18 for k in range(1, 10**8): if A*k > B: ans = min(ans, A*k-B) break d = B-A*k x = -(-d//k) ans = min(ans, (A+x)*k-B+x) print(ans)
ConDefects/ConDefects/Code/arc150_b/Python/40750419
condefects-python_data_1212
#まずはリストもmatrixに起こす #h,w = map(int, input().split()) #mtx = [] #for i in range(h+2): # tmp = [0] * (w + 2) # if i != 0 and i != h + 1: # s = input() # for j in range(1, w+1): # if s[j-1] == "#": # tmp[j] = -1 # mtx.append(tmp) #print(mtx) #cnt = 2 #same = set() #for i in range(1,h+1): # for j in range(1,w+1): # box9 = [mtx[i+x][j+y] for x in range(-1,2) for y in range(-1,2)] # target = mtx[i][j] # boxmax = max(box9) # if target == 0: # continue # elif target >= 1: # if boxmax == 1: # for x in range(-1, 2): # for y in range(-1,2): # if mtx[i+x][j+y] == 1: # mtx[i+x][j+y] = cnt # cnt += 1 # elif boxmax >= 2: # for x in range(-1, 2): # for y in range(-1,2): # if mtx[i+x][j+y] == 0: # continue # elif mtx[i+x][j+y] == 1: # mtx[i+x][j+y] = boxmax # elif mtx[i+x][j+y] != boxmax: # same.add((mtx[i+x][j+y],boxmax)) # mtx[i+x][j+y] = boxmax # elif mtx[i+x][j+y] == boxmax: # continue #lstsame = list(same) #lstsame.sort() #print(lstsame) #lstn = [i for i in range(2, cnt)] #for i in range(h+2): # print("".join(map(str, mtx[i]))) #for i in range(len(lstsame)): # a,b = lstsame[i][0], lstsame[i][1] # for j in range(len(lstn)): # if lstn[j] == a: # lstn[j] = b #print(lstn) #print(len(set(lstn))) #結構頑張ったしサンプルデータは通ったがテストデータでWAだった。どこが問題かは分からなかった。 #sameセットの生成、および統合に問題があったんじゃないかとChatGPTには言われた。 #mtxはだいたい生かしてUnion-find木でAC獲るぞ h,w = map(int, input().split()) mtx = [] rank = [1] * w * h for i in range(h): tmp = [-2] * w s = input() for j in range(w): if s[j] == "#": tmp[j] = -1 mtx=mtx+tmp def find(x): global mtx if mtx[x] == -1: return(x) elif mtx[x] == -2: return(-2) else: mtx[x] = find(mtx[x]) return mtx[x] def union(x,y): global mtx,rank x = find(x) y = find(y) if x == y: return else: if rank[x] > rank[y]: x,y = y,x elif rank[x] == rank[y]: rank[y] += 1 mtx[x] = y for i in range(h): for j in range(w): direction=[(-1,-1), (-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] if mtx[i*w+j] != -1: continue for a,b in direction: if 0 <= i+a < h and 0 <= j+b < w: if mtx[(i+a)*w+(j+b)] != -2: union(i*w+j,(i+a)*w+(j+b)) #print(mtx) answer = set() for i in range(h): for j in range(w): if mtx[i*w+j] != -2: answer.add(find(i*w+j)) print(len(answer)) #まずはリストもmatrixに起こす #h,w = map(int, input().split()) #mtx = [] #for i in range(h+2): # tmp = [0] * (w + 2) # if i != 0 and i != h + 1: # s = input() # for j in range(1, w+1): # if s[j-1] == "#": # tmp[j] = -1 # mtx.append(tmp) #print(mtx) #cnt = 2 #same = set() #for i in range(1,h+1): # for j in range(1,w+1): # box9 = [mtx[i+x][j+y] for x in range(-1,2) for y in range(-1,2)] # target = mtx[i][j] # boxmax = max(box9) # if target == 0: # continue # elif target >= 1: # if boxmax == 1: # for x in range(-1, 2): # for y in range(-1,2): # if mtx[i+x][j+y] == 1: # mtx[i+x][j+y] = cnt # cnt += 1 # elif boxmax >= 2: # for x in range(-1, 2): # for y in range(-1,2): # if mtx[i+x][j+y] == 0: # continue # elif mtx[i+x][j+y] == 1: # mtx[i+x][j+y] = boxmax # elif mtx[i+x][j+y] != boxmax: # same.add((mtx[i+x][j+y],boxmax)) # mtx[i+x][j+y] = boxmax # elif mtx[i+x][j+y] == boxmax: # continue #lstsame = list(same) #lstsame.sort() #print(lstsame) #lstn = [i for i in range(2, cnt)] #for i in range(h+2): # print("".join(map(str, mtx[i]))) #for i in range(len(lstsame)): # a,b = lstsame[i][0], lstsame[i][1] # for j in range(len(lstn)): # if lstn[j] == a: # lstn[j] = b #print(lstn) #print(len(set(lstn))) #結構頑張ったしサンプルデータは通ったがテストデータでWAだった。どこが問題かは分からなかった。 #sameセットの生成、および統合に問題があったんじゃないかとChatGPTには言われた。 #mtxはだいたい生かしてUnion-find木でAC獲るぞ h,w = map(int, input().split()) mtx = [] rank = [1] * w * h for i in range(h): tmp = [-2] * w s = input() for j in range(w): if s[j] == "#": tmp[j] = -1 mtx=mtx+tmp def find(x): global mtx if mtx[x] == -1: return(x) elif mtx[x] == -2: return(-2) else: mtx[x] = find(mtx[x]) return mtx[x] def union(x,y): global mtx,rank x = find(x) y = find(y) if x == y: return else: if rank[x] > rank[y]: x,y = y,x elif rank[x] == rank[y]: rank[y] += 1 mtx[x] = y for i in range(h): for j in range(w): direction=[(-1,-1), (-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] if mtx[i*w+j] == -2: continue for a,b in direction: if 0 <= i+a < h and 0 <= j+b < w: if mtx[(i+a)*w+(j+b)] != -2: union(i*w+j,(i+a)*w+(j+b)) #print(mtx) answer = set() for i in range(h): for j in range(w): if mtx[i*w+j] != -2: answer.add(find(i*w+j)) print(len(answer))
ConDefects/ConDefects/Code/abc325_c/Python/54452348
condefects-python_data_1213
import sys readline = sys.stdin.buffer.readline N, K = map(int, readline().split()) A = list(map(int, readline().split())) if K >= 0: print('Yes') A.sort() print(*A) else: if K > sum(A): print('No') else: print('Yes') A.sort(reverse=True) print(*A) import sys readline = sys.stdin.buffer.readline N, K = map(int, readline().split()) A = list(map(int, readline().split())) if K > 0: print('Yes') A.sort() print(*A) else: if K > sum(A): print('No') else: print('Yes') A.sort(reverse=True) print(*A)
ConDefects/ConDefects/Code/arc179_a/Python/54940318
condefects-python_data_1214
# https://atcoder.jp/contests/arc179/tasks/arc179_a # Goal: all values less than K appear before numbers more than K in array's psa # Case 1: K < 0: extra option to make everything in psa >= 0 # General case: sort array for smaller elements to come first from itertools import accumulate import sys def check(psa): # first occurrence of number >=K in psa earliest = 9999999999 for i in range(N + 1): if psa[i] >= K: earliest = i break # check if any number after the first >=K is less than K for i in range(earliest + 1, N + 1): if psa[i] < K: return False return True N, K = map(int, input().split()) arr = list(map(int, input().split())) found = False if check([0] + list(accumulate(sorted(arr)))): found = True arr.sort() if not found and K < 0 and check([0] + list(accumulate(sorted(arr, reverse=True)))): found = True arr.sort(reverse=True) if found: print("Yes") print(*arr) else: print("No") # https://atcoder.jp/contests/arc179/tasks/arc179_a # Goal: all values less than K appear before numbers more than K in array's psa # Case 1: K < 0: extra option to make everything in psa >= 0 # General case: sort array for smaller elements to come first from itertools import accumulate import sys def check(psa): # first occurrence of number >=K in psa earliest = 9999999999 for i in range(N + 1): if psa[i] >= K: earliest = i break # check if any number after the first >=K is less than K for i in range(earliest + 1, N + 1): if psa[i] < K: return False return True N, K = map(int, input().split()) arr = list(map(int, input().split())) found = False if check([0] + list(accumulate(sorted(arr)))): found = True arr.sort() if not found and K <= 0 and check([0] + list(accumulate(sorted(arr, reverse=True)))): found = True arr.sort(reverse=True) if found: print("Yes") print(*arr) else: print("No")
ConDefects/ConDefects/Code/arc179_a/Python/54889248
condefects-python_data_1215
def main(): N,K=map(int,input().split()) A=list(map(int,input().split())) if K>=1: print("Yes") A.sort() print(*A) elif sum(A)>=0: print("Yes") A.sort(reverse=True) print(*A) else: print("No") if __name__=="__main__": main() def main(): N,K=map(int,input().split()) A=list(map(int,input().split())) if K>=1: print("Yes") A.sort() print(*A) elif sum(A)>=K: print("Yes") A.sort(reverse=True) print(*A) else: print("No") if __name__=="__main__": main()
ConDefects/ConDefects/Code/arc179_a/Python/54763731
condefects-python_data_1216
#!/usr/bin/env python3 # 再起関数,Decimal以外は、pypyを推奨(メモリを多く使用する場合遅くなる) # pypyは,numpy使用不可 # pythonの実行時間のオーダーは、10^8まで # 最小値の最大化(最大値の最小化)は二分探索 # O(2^n)はdpが多め # べき乗はpowを使う(modで割る系は平衡二分木が組まれているため) from collections import Counter, deque, defaultdict, OrderedDict from heapq import heapify, heappop, heappush from itertools import accumulate, product, combinations, combinations_with_replacement, permutations from bisect import bisect, bisect_left, bisect_right from functools import lru_cache # Setのように値をO(√n)で見つける、配列のように[0]と[-1]で最小、最大取得はO(1) from sortedcontainers import SortedSet, SortedList, SortedDict from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN, getcontext # 多倍長精度を100桁にする getcontext().prec = 100 import math import sys from copy import deepcopy, copy # 分数モジュール from fractions import Fraction sys.setrecursionlimit(10 ** 7) def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def S(): return input() def SI(): return map(str, input().split()) def LS(): return list(map(str, input().split())) INF = 10**18 MOD = 998244353 # 素数、フェルマーの小定理、平衡二分木 dict = defaultdict(lambda:defaultdict()) n, k = MI() a = LI() if k <= 0: if sum(a) >= 0: print("Yes") print(*sorted(a, reverse=True)) else: print("No") else: print("Yes") print(*sorted(a)) #!/usr/bin/env python3 # 再起関数,Decimal以外は、pypyを推奨(メモリを多く使用する場合遅くなる) # pypyは,numpy使用不可 # pythonの実行時間のオーダーは、10^8まで # 最小値の最大化(最大値の最小化)は二分探索 # O(2^n)はdpが多め # べき乗はpowを使う(modで割る系は平衡二分木が組まれているため) from collections import Counter, deque, defaultdict, OrderedDict from heapq import heapify, heappop, heappush from itertools import accumulate, product, combinations, combinations_with_replacement, permutations from bisect import bisect, bisect_left, bisect_right from functools import lru_cache # Setのように値をO(√n)で見つける、配列のように[0]と[-1]で最小、最大取得はO(1) from sortedcontainers import SortedSet, SortedList, SortedDict from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN, getcontext # 多倍長精度を100桁にする getcontext().prec = 100 import math import sys from copy import deepcopy, copy # 分数モジュール from fractions import Fraction sys.setrecursionlimit(10 ** 7) def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def S(): return input() def SI(): return map(str, input().split()) def LS(): return list(map(str, input().split())) INF = 10**18 MOD = 998244353 # 素数、フェルマーの小定理、平衡二分木 dict = defaultdict(lambda:defaultdict()) n, k = MI() a = LI() if k <= 0: if sum(a) >= k: print("Yes") print(*sorted(a, reverse=True)) else: print("No") else: print("Yes") print(*sorted(a))
ConDefects/ConDefects/Code/arc179_a/Python/55011877
condefects-python_data_1217
n,k=map(int,input().split()) A=list(map(int,input().split())) if k>0: A.sort() print("Yes") print(*A) elif k<sum(A): A.sort(reverse=True) print("Yes") print(*A) else: print("No") n,k=map(int,input().split()) A=list(map(int,input().split())) if k>0: A.sort() print("Yes") print(*A) elif k<=sum(A): A.sort(reverse=True) print("Yes") print(*A) else: print("No")
ConDefects/ConDefects/Code/arc179_a/Python/54967532
condefects-python_data_1218
import math import sys import random from collections import deque from itertools import product debug = lambda name, value, *args, **kwargs: print( f"{name}: {value}", *args, file=sys.stderr, **kwargs) mod = 1000000007 m = random.randint(1,1000000) iinf = 100000000000000005 #1e18 + 5 input = lambda: sys.stdin.readline().strip() #sys.stdin = open('input.txt', 'r') #sys.stdout = open('output.txt', 'w') dr = [1,-1,0,0] dc = [0,0,1,-1] def main(): d = int(input()) res = iinf*10 for x in range(0, int(d**.5) +1): #check flr, cel y = int((d - x*x)**.5) for i in range(max(0,y-5), y+5): res = min(res, abs(x**2 + y**2 - d)) print(int(res)) t=1 #t = int(input()) for _ in range(t): main() import math import sys import random from collections import deque from itertools import product debug = lambda name, value, *args, **kwargs: print( f"{name}: {value}", *args, file=sys.stderr, **kwargs) mod = 1000000007 m = random.randint(1,1000000) iinf = 100000000000000005 #1e18 + 5 input = lambda: sys.stdin.readline().strip() #sys.stdin = open('input.txt', 'r') #sys.stdout = open('output.txt', 'w') dr = [1,-1,0,0] dc = [0,0,1,-1] def main(): d = int(input()) res = iinf*10 for x in range(0, int(d**.5) +1): #check flr, cel y = int((d - x*x)**.5) for i in range(max(0,y-5), y+5): res = min(res, abs(x**2 + i**2 - d)) print(int(res)) t=1 #t = int(input()) for _ in range(t): main()
ConDefects/ConDefects/Code/abc330_c/Python/54910018
condefects-python_data_1219
d=int(input()) ans=float("inf") for x in range(1,d): if x**2-d>=0: ans=min(ans,x**2-d) break else: c=d-x**2 y1=int(c**0.5) y2=y1+1 ans=min(ans,abs(y1**2-c),abs(y2**2-c)) print(ans) d=int(input()) ans=float("inf") for x in range(1,d+1): if x**2-d>=0: ans=min(ans,x**2-d) break else: c=d-x**2 y1=int(c**0.5) y2=y1+1 ans=min(ans,abs(y1**2-c),abs(y2**2-c)) print(ans)
ConDefects/ConDefects/Code/abc330_c/Python/54320173
condefects-python_data_1220
import math def rint(offset=0,base=10): return list(map(lambda x: int(x, base)+offset, input().split())) def full(s, f=int, *args): return [full(s[1:], f) if len(s) > 1 else f(*args) for _ in range(s[0])] def shift(*args,offset=-1): return (a+offset for a in args) D, = rint() D2 = math.floor(math.sqrt(D)) ans = D for x in range(D2+1): y = math.floor(math.sqrt(D-x*x)) ans = min(ans, abs(x*x+y*y-D)) y+1 ans = min(ans, abs(x*x+y*y-D)) print(ans) import math def rint(offset=0,base=10): return list(map(lambda x: int(x, base)+offset, input().split())) def full(s, f=int, *args): return [full(s[1:], f) if len(s) > 1 else f(*args) for _ in range(s[0])] def shift(*args,offset=-1): return (a+offset for a in args) D, = rint() D2 = math.floor(math.sqrt(D)) ans = D for x in range(D2+1): y = math.floor(math.sqrt(D-x*x)) ans = min(ans, abs(x*x+y*y-D)) y+=1 ans = min(ans, abs(x*x+y*y-D)) print(ans)
ConDefects/ConDefects/Code/abc330_c/Python/54990972
condefects-python_data_1221
d = int(input()) x = 0 out = d while 2 * x**2 < d: y = int((d - x**2)**(0.5)) out = min(out, d - x**2 - y**2, x**2 + (y+1)**2 - d) x += 1 print(out) d = int(input()) x = 0 out = d while x**2 < d: y = int((d - x**2)**(0.5)) out = min(out, d - x**2 - y**2, x**2 + (y+1)**2 - d) x += 1 print(out)
ConDefects/ConDefects/Code/abc330_c/Python/54054641
condefects-python_data_1222
D = int(input()) square = [] for i in range(10**6+2): if i**2 > D: break square.append(i**2) ans = 10**12 x = 0 y = len(square)-1 while x < len(square): value = square[x] + square[y] ans = min(ans,abs(value-D)) if value < D: x += 1 elif value > D: y -= 1 elif value == D: break print(ans) D = int(input()) square = [] for i in range(10**7): if i**2 > D: break square.append(i**2) ans = 10**12 x = 0 y = len(square)-1 while x < len(square): value = square[x] + square[y] ans = min(ans,abs(value-D)) if value < D: x += 1 elif value > D: y -= 1 elif value == D: break print(ans)
ConDefects/ConDefects/Code/abc330_c/Python/54071794
condefects-python_data_1223
d=int(input()) ans=1001001001001 for a in range(1,2*10**6+1): b=(abs(d-a**2))**.5 b=int(b) for nb in range(b-1,b+2): ans=min(ans,abs(a**2+b**2-d)) print(ans) d=int(input()) ans=1001001001001 for a in range(1,2*10**6+1): b=(abs(d-a**2))**.5 b=int(b) for nb in range(b-1,b+2): ans=min(ans,abs(a**2+nb**2-d)) print(ans)
ConDefects/ConDefects/Code/abc330_c/Python/54284668
condefects-python_data_1224
import math D = int(input()) n = int(math.sqrt(D)) ans = 10**10 index = 0 for i in range(n, 0, -1): for j in range(index, i): ans = min(ans, abs(D-(i**2 + j**2))) if i**2 + j**2 >= D: index = j break print(ans) import math D = int(input()) n = int(math.sqrt(D)) ans = 10**10 index = 0 for i in range(n, 0, -1): for j in range(index, i+1): ans = min(ans, abs(D-(i**2 + j**2))) if i**2 + j**2 >= D: index = j break print(ans)
ConDefects/ConDefects/Code/abc330_c/Python/54881958
condefects-python_data_1225
N, K = map(int, input().split()) MOD = 998244353 dp = [0]*(K+1) dp[0] = 1 a = (N**2-2*N)%MOD*pow(N, -2, MOD) b = 2*pow(N, -2, MOD) for k in range(1, K+1): dp[k] = (dp[k-1]*a+b)%MOD if N == 1: ans = 1 else: ans = dp[K] + (1-dp[K])*(N+2)//2 print(ans%MOD) N, K = map(int, input().split()) MOD = 998244353 dp = [0]*(K+1) dp[0] = 1 a = (N**2-2*N)%MOD*pow(N, -2, MOD) b = 2*pow(N, -2, MOD) for k in range(1, K+1): dp[k] = (dp[k-1]*a+b)%MOD if N == 1: ans = 1 else: ans = dp[K] + (1-dp[K])*pow(N-1, -1, MOD)*(N+2)*(N-1)//2 print(ans%MOD)
ConDefects/ConDefects/Code/abc360_e/Python/55129894
condefects-python_data_1226
import sys from pprint import pprint sys.setrecursionlimit(10**7) read_int = lambda: int(sys.stdin.readline()) read_ints = lambda: list(map(int, sys.stdin.readline().split())) read_float = lambda: float(sys.stdin.readline()) read_floats = lambda: list(map(float, sys.stdin.readline().split())) def get_logger(debug=True): if not debug: return type("Dummy", (object,), {"debug": lambda self, a: None})() import logging logger = logging.getLogger("") logger.setLevel(logging.DEBUG) handler = logging.StreamHandler(sys.stdout) handler.setFormatter(logging.Formatter("[%(funcName)s:%(lineno)s] %(message)s")) logger.addHandler(handler) return logger def modpow(a: int, n: int, mod: int) -> int: """二分累乗法""" res = 1 while n > 0: if n & 1: res = res * a % mod a = a * a % mod n >>= 1 return res def modinv(a: int, mod: int) -> int: """逆元の計算 オイラーの小定理より 法modにおけるaの逆元a^-1 = a^(mod-2) % mod """ return modpow(a, mod - 2, mod) # ------------------------------- log = get_logger(False) MOD = 998244353 N, K = read_ints() invn2 = modinv(N * N, MOD) p0 = 1 for _ in range(K): p0 = p0 * ((N - 1) ** 2 + 1) + 2 * (1 - p0) p0 = p0 * invn2 % MOD log.debug(p0) ans = p0 + (2 + N) * (1 - p0) // 2 print(ans % MOD) """test cases """ import sys from pprint import pprint sys.setrecursionlimit(10**7) read_int = lambda: int(sys.stdin.readline()) read_ints = lambda: list(map(int, sys.stdin.readline().split())) read_float = lambda: float(sys.stdin.readline()) read_floats = lambda: list(map(float, sys.stdin.readline().split())) def get_logger(debug=True): if not debug: return type("Dummy", (object,), {"debug": lambda self, a: None})() import logging logger = logging.getLogger("") logger.setLevel(logging.DEBUG) handler = logging.StreamHandler(sys.stdout) handler.setFormatter(logging.Formatter("[%(funcName)s:%(lineno)s] %(message)s")) logger.addHandler(handler) return logger def modpow(a: int, n: int, mod: int) -> int: """二分累乗法""" res = 1 while n > 0: if n & 1: res = res * a % mod a = a * a % mod n >>= 1 return res def modinv(a: int, mod: int) -> int: """逆元の計算 オイラーの小定理より 法modにおけるaの逆元a^-1 = a^(mod-2) % mod """ return modpow(a, mod - 2, mod) # ------------------------------- log = get_logger(False) MOD = 998244353 N, K = read_ints() invn2 = modinv(N * N, MOD) p0 = 1 for _ in range(K): p0 = p0 * ((N - 1) ** 2 + 1) + 2 * (1 - p0) p0 = p0 * invn2 % MOD log.debug(p0) ans = p0 + (2 + N) * (1 - p0) * modinv(2, MOD) print(ans % MOD) """test cases """
ConDefects/ConDefects/Code/abc360_e/Python/55140371
condefects-python_data_1227
def calculate_LCP(s1, s2): lcp_length = 0 min_len = min(len(s1), len(s2)) for i in range(min_len): if s1[i] == s2[i]: lcp_length += 1 else: break return lcp_length N = int(input()) L = [] #String, index, LCP for i in range(N): S = input() L.append([S, i, 0]) L = sorted(L, key=lambda x:x[0]) max_lcp = 0 for i in range(N-1): lcp = calculate_LCP(L[i][0], L[i+1][0]) if lcp > max_lcp: L[i][2] = lcp else: L[i][2] = max_lcp max_lcp = lcp L[N-1][2] = L[N-2][2] L = sorted(L, key=lambda x:x[1]) for i in range(N): print(L[i][2]) def calculate_LCP(s1, s2): lcp_length = 0 min_len = min(len(s1), len(s2)) for i in range(min_len): if s1[i] == s2[i]: lcp_length += 1 else: break return lcp_length N = int(input()) L = [] #String, index, LCP for i in range(N): S = input() L.append([S, i, 0]) L = sorted(L, key=lambda x:x[0]) max_lcp = 0 for i in range(N-1): lcp = calculate_LCP(L[i][0], L[i+1][0]) if lcp > max_lcp: L[i][2] = lcp else: L[i][2] = max_lcp max_lcp = lcp L[N-1][2] = calculate_LCP(L[N-1][0], L[N-2][0]) L = sorted(L, key=lambda x:x[1]) for i in range(N): print(L[i][2])
ConDefects/ConDefects/Code/abc287_e/Python/54006112
condefects-python_data_1228
n = int(input()) s = [input() for _ in range(n)] m = 2147483647 # 2**31-1 from collections import defaultdict di = defaultdict(int) for x in s: num = 0 for y in x: y = (ord(y)-ord('a')+1) num = (y+num*100)%m di[num] += 1 for x in s: ans = 0 num = 0 for i, y in enumerate(x): y = (ord(y)-ord('a')+1) num = (y+num*100)%m if di[num] >= 2: ans = max(i+1, ans) print(ans) n = int(input()) s = [input() for _ in range(n)] m = 2**61-1 # 2**31-1 from collections import defaultdict di = defaultdict(int) for x in s: num = 0 for y in x: y = (ord(y)-ord('a')+1) num = (y+num*100)%m di[num] += 1 for x in s: ans = 0 num = 0 for i, y in enumerate(x): y = (ord(y)-ord('a')+1) num = (y+num*100)%m if di[num] >= 2: ans = max(i+1, ans) print(ans)
ConDefects/ConDefects/Code/abc287_e/Python/51264451
condefects-python_data_1229
CHAR_SIZE = 26 class Trie: def __init__(self): self.isLeaf = False self.children = [None] * CHAR_SIZE self.cnt = 0 def insert(self, key): curr = self for i in range(len(key)): index = ord(key[i]) - ord('a') if curr.children[index] is None: curr.children[index] = Trie() curr = curr.children[index] curr.cnt += 1 curr.isLeaf = True def search(self, key): curr = self for c in key: index = ord(c) - ord('a') curr = curr.children[index] if curr is None: return False return curr.cnt N = int(input()) trie = Trie() S = [None]*N for i in range(N): S[i] = input() trie.insert(S[i]) for i in range(N): l, r = 1, len(S[i])+1 while r-l > 1: m = (l+r)//2 if trie.search(S[i][:m]) > 1: l = m else: r = m print(l) CHAR_SIZE = 26 class Trie: def __init__(self): self.isLeaf = False self.children = [None] * CHAR_SIZE self.cnt = 0 def insert(self, key): curr = self for i in range(len(key)): index = ord(key[i]) - ord('a') if curr.children[index] is None: curr.children[index] = Trie() curr = curr.children[index] curr.cnt += 1 curr.isLeaf = True def search(self, key): curr = self for c in key: index = ord(c) - ord('a') curr = curr.children[index] if curr is None: return False return curr.cnt N = int(input()) trie = Trie() S = [None]*N for i in range(N): S[i] = input() trie.insert(S[i]) for i in range(N): l, r = 0, len(S[i])+1 while r-l > 1: m = (l+r)//2 if trie.search(S[i][:m]) > 1: l = m else: r = m print(l)
ConDefects/ConDefects/Code/abc287_e/Python/54495703
condefects-python_data_1230
s = input() S_part = set() for i in range(len(s)): for j in range(i+1,len(s)+1): S_part.add(s[i:j]) print(S_part) s = input() S_part = set() for i in range(len(s)): for j in range(i+1,len(s)+1): S_part.add(s[i:j]) print(len(S_part))
ConDefects/ConDefects/Code/abc347_b/Python/54617710
condefects-python_data_1231
def generate_substrings(s): substrings = [] n = len(s) for length in range(1, n + 1): # 長さ1からnまでの部分文字列を生成する for start in range(n - length + 1): substr = s[start:start + length] substrings.append(substr) return substrings s = input() print(len(generate_substrings(s))) def generate_substrings(s): substrings = [] n = len(s) for length in range(1, n + 1): # 長さ1からnまでの部分文字列を生成する for start in range(n - length + 1): substr = s[start:start + length] substrings.append(substr) return substrings s = input() print(len(set(generate_substrings(s))))
ConDefects/ConDefects/Code/abc347_b/Python/55003393
condefects-python_data_1232
s = input() q = int(input()) def rec(t, k): if t == 0: return k elif k == 0: return ord(s[0]) - ord('A') + t elif k % 2 == 0: return rec(t-1, k//2) + 1 else: return rec(t-1, k//2) + 2 def main(): for _ in range(q): t, k = map(int, input().split()) print(chr(ord('A') + rec(t, k-1)%3)) main() s = input() q = int(input()) def rec(t, k): if t == 0: return ord(s[k]) - ord('A') elif k == 0: return ord(s[0]) - ord('A') + t elif k % 2 == 0: return rec(t-1, k//2) + 1 else: return rec(t-1, k//2) + 2 def main(): for _ in range(q): t, k = map(int, input().split()) print(chr(ord('A') + rec(t, k-1)%3)) main()
ConDefects/ConDefects/Code/abc242_d/Python/45458203
condefects-python_data_1233
S=input() if S=='ACE' or S=='BDF' or S=='CEG' or S=='DFA' or S=='EGB' or S=='FAC' or S=='GBD ': print('Yes') else: print('No') S=input() if S=='ACE' or S=='BDF' or S=='CEG' or S=='DFA' or S=='EGB' or S=='FAC' or S=='GBD': print('Yes') else: print('No')
ConDefects/ConDefects/Code/abc312_a/Python/45974761
condefects-python_data_1234
S=str(input()) if S=='ACE': print('Yes') elif S=='BDF': print('Yes') elif S=='CFG': print('Yes') elif S=='DFA': print('Yes') elif S=='EGB': print('Yes') elif S=='FAC': print('Yes') elif S=='GBD': print('Yes') else: print('No') S=str(input()) if S=='ACE': print('Yes') elif S=='BDF': print('Yes') elif S=='CEG': print('Yes') elif S=='DFA': print('Yes') elif S=='EGB': print('Yes') elif S=='FAC': print('Yes') elif S=='GBD': print('Yes') else: print('No')
ConDefects/ConDefects/Code/abc312_a/Python/46035506
condefects-python_data_1235
S = input() YES = ["ACE", "BDF", "CFG", "DFA", "EGB", "FAC", "GBD"] if S in YES: print("Yes") else: print("No") S = input() YES = ["ACE", "BDF", "CEG", "DFA", "EGB", "FAC", "GBD"] if S in YES: print("Yes") else: print("No")
ConDefects/ConDefects/Code/abc312_a/Python/46185846
condefects-python_data_1236
S = input() if S == 'ACE' or 'BDF' or 'CEG' or 'DFA' or 'EGB' or 'FAC' or 'GBD': print('Yes') else: print('No') S = input() if S == 'ACE' or S == 'BDF' or S == 'CEG' or S == 'DFA' or S == 'EGB' or S == 'FAC' or S == 'GBD': print('Yes') else: print('No')
ConDefects/ConDefects/Code/abc312_a/Python/45765051
condefects-python_data_1237
S = input() Flag = 0 string = ['ACE', 'BDF', 'CEG', 'DFA', 'EGB', 'FAC', 'GBD'] for i in range(len(string)): if S == string[i]: Flag = 1 if Flag == 1: print('Yes') else: print('NO') S = input() Flag = 0 string = ['ACE', 'BDF', 'CEG', 'DFA', 'EGB', 'FAC', 'GBD'] for i in range(len(string)): if S == string[i]: Flag = 1 if Flag == 1: print('Yes') else: print('No')
ConDefects/ConDefects/Code/abc312_a/Python/45975022
condefects-python_data_1238
S = input() curr = ["ACE", "BDF", "CEG", "DFA", "EFA", "FAC", "GBD"] ans = "No" for c in curr: if S == c: ans = "Yes" break print(ans) S = input() curr = ["ACE", "BDF", "CEG", "DFA", "EGB", "FAC", "GBD"] ans = "No" for c in curr: if S == c: ans = "Yes" break print(ans)
ConDefects/ConDefects/Code/abc312_a/Python/45778739
condefects-python_data_1239
tex=['ACE', 'BDF', 'CEG', 'DFA', 'EGB', 'FAC'] s=input() ans=0 for i in range(len(tex)): if s == tex[i]: ans+=1 if ans==0: print("No") else: print("Yes") tex=['ACE', 'BDF', 'CEG', 'DFA', 'EGB', 'FAC', 'GBD'] s=input() ans=0 for i in range(len(tex)): if s == tex[i]: ans+=1 if ans==0: print("No") else: print("Yes")
ConDefects/ConDefects/Code/abc312_a/Python/46047381
condefects-python_data_1240
seikai=["ACE","BDF","CEG","DFA","EGB","FAC","GBD"] S=input() if(S in seikai): print("Yse") else: print("No") seikai=["ACE","BDF","CEG","DFA","EGB","FAC","GBD"] S=input() if(S in seikai): print("Yes") else: print("No")
ConDefects/ConDefects/Code/abc312_a/Python/45893891
condefects-python_data_1241
S = input() list = [ "ACE","BDF","CEG","DFA","EGB","FAC","GBD"] print("YES" if S in list else "NO") S = input() list = [ "ACE","BDF","CEG","DFA","EGB","FAC","GBD"] print("Yes" if S in list else "No")
ConDefects/ConDefects/Code/abc312_a/Python/46206002
condefects-python_data_1242
N, X, Y = map(int, input().split()) G = [[] for _ in range(N+1)] for _ in range(N-1): u, v = map(int, input().split()) G[u].append(v) G[v].append(u) # 深さ優先探索のときはこれをつけるように! # また、PyPy は再帰が遅いので CPython を使うように! import sys sys.setrecursionlimit(10**6) def dfs(v, p, s): s[v] = True for neibor in G[v]: if s[neibor]: continue p[neibor] = v dfs(neibor, p, s) prev = [-1 for _ in range(N+1)] seen = [False for _ in range(N+1)] dfs(X, prev, seen) print(prev) # ゴールから dist の数値を頼りに逆にたどり、最後に配列を反転させて経路を取得する。 root = list() now = Y while (now != -1): root.append(now) now = prev[now] root.reverse() print(*root) N, X, Y = map(int, input().split()) G = [[] for _ in range(N+1)] for _ in range(N-1): u, v = map(int, input().split()) G[u].append(v) G[v].append(u) # 深さ優先探索のときはこれをつけるように! # また、PyPy は再帰が遅いので CPython を使うように! import sys sys.setrecursionlimit(10**6) def dfs(v, p, s): s[v] = True for neibor in G[v]: if s[neibor]: continue p[neibor] = v dfs(neibor, p, s) prev = [-1 for _ in range(N+1)] seen = [False for _ in range(N+1)] dfs(X, prev, seen) #print(prev) # ゴールから dist の数値を頼りに逆にたどり、最後に配列を反転させて経路を取得する。 root = list() now = Y while (now != -1): root.append(now) now = prev[now] root.reverse() print(*root)
ConDefects/ConDefects/Code/abc270_c/Python/54301518
condefects-python_data_1243
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n = int(input()) a = list(map(int,input().split())) hull = [(0, a[0]), (1, a[1])] for i in range(2, n): nextv = (i, a[i]) while True: v1 = hull[-1] v2 = hull[-2] grad1 = (v1[1] - v2[1]) // (v1[0] - v2[0]) grad2 = (nextv[1] - v1[1]) // (nextv[0] - v1[0]) if grad2 <= grad1: hull.pop() if len(hull) < 2: break else: break hull.append(nextv) ans = 0 for i in range(len(hull) - 1): distance = hull[i + 1][0] - hull[i][0] height_difference = abs(hull[i + 1][1] - hull[i][1]) grad_min = height_difference // distance grad_min_dur = height_difference % distance p = min(hull[i][1], hull[i + 1][1]) for j in range(distance + 1): ans += p if j >= grad_min_dur: p += grad_min else: p += grad_min + 1 ans -= hull[i + 1][1] ans += hull[-1][1] print(ans) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n = int(input()) a = list(map(int,input().split())) hull = [(0, a[0]), (1, a[1])] for i in range(2, n): nextv = (i, a[i]) while True: v1 = hull[-1] v2 = hull[-2] grad1 = (v1[1] - v2[1]) // (v1[0] - v2[0]) grad2 = (nextv[1] - v1[1]) // (nextv[0] - v1[0]) if grad2 <= grad1: hull.pop() if len(hull) < 2: break else: break hull.append(nextv) ans = 0 for i in range(len(hull) - 1): distance = hull[i + 1][0] - hull[i][0] height_difference = abs(hull[i + 1][1] - hull[i][1]) grad_min = height_difference // distance grad_min_dur = height_difference % distance p = min(hull[i][1], hull[i + 1][1]) for j in range(distance + 1): ans += p if j < distance - grad_min_dur: p += grad_min else: p += grad_min + 1 ans -= hull[i + 1][1] ans += hull[-1][1] print(ans) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
ConDefects/ConDefects/Code/arc130_f/Python/27576444
condefects-python_data_1244
import sys input = sys.stdin.readline N=int(input()) A=list(map(int,input().split())) def calc(l,r): x=abs(A[r]-A[l]) q=x//(r-l) rr=x%(r-l) if A[l]<A[r]: for i in range(r-l): if (r-(l+i))<=rr-1: A[l+i]=min(A[l+i],rr-(r-(l+i))+A[l]+q*i) else: A[l+i]=min(A[l+i],A[l]+q*i) else: for i in range(r-l): if ((r-i)-l)<=rr-1: A[r-i]=min(A[r-i],rr-((r-i)-l)+A[r]+q*i) else: A[r-i]=min(A[r-i],A[r]+q*i) for tests in range(10): X=[(i,A[i]) for i in range(N)] Q=[] for i,a in X: if len(Q)<=1: Q.append((i,a)) else: while len(Q)>=2: i1,k1=Q[-1] i2,k2=Q[-2] if (k1-k2)*(i-i2)>(a-k2)*(i1-i2): Q.pop() else: break Q.append((i,a)) for i in range(len(Q)-1): calc(Q[i][0],Q[i+1][0]) print(sum(A)) import sys input = sys.stdin.readline N=int(input()) A=list(map(int,input().split())) def calc(l,r): x=abs(A[r]-A[l]) q=x//(r-l) rr=x%(r-l) if A[l]<A[r]: for i in range(r-l): if (r-(l+i))<=rr-1: A[l+i]=min(A[l+i],rr-(r-(l+i))+A[l]+q*i) else: A[l+i]=min(A[l+i],A[l]+q*i) else: for i in range(r-l): if ((r-i)-l)<=rr-1: A[r-i]=min(A[r-i],rr-((r-i)-l)+A[r]+q*i) else: A[r-i]=min(A[r-i],A[r]+q*i) for tests in range(20): X=[(i,A[i]) for i in range(N)] Q=[] for i,a in X: if len(Q)<=1: Q.append((i,a)) else: while len(Q)>=2: i1,k1=Q[-1] i2,k2=Q[-2] if (k1-k2)*(i-i2)>(a-k2)*(i1-i2): Q.pop() else: break Q.append((i,a)) for i in range(len(Q)-1): calc(Q[i][0],Q[i+1][0]) print(sum(A))
ConDefects/ConDefects/Code/arc130_f/Python/27582246
condefects-python_data_1245
h,w=map(int, input().split()) g=[input() for i in range(h)] used=[[1]*w for i in range(h)] def grid(nowh,noww): if(0<=nowh<=h-1 and 0<=noww<=w-1): return 1 else: return 0 st={"#", ">", "v", "<", "^"} for i in range(h): for j in range(w): now=g[i][j] if(now==">"): used[i][j]=0 dw=1 while(1): if(grid(i,j+dw) and g[i][j+dw]=="."): used[i][j+dw]=0 dw+=1 else: break elif(now=="<"): used[i][j]=0 dw=-1 while(1): if(grid(i,j+dw) and g[i][j+dw]=="."): used[i][j+dw]=0 dw+=-1 else: break elif(now=="v"): used[i][j]=0 dh=1 while(1): if(grid(i+dh,j) and g[i+dh][j]=="."): used[i+dh][j]=0 dh+=1 else: break elif(now=="^"): used[i][j]=0 dh=-1 while(1): if(grid(i+dh,j) and g[i+dh][j]=="."): used[i+dh][j]=0 dh+=-1 else: break elif(now=="#"): used[i][j]=0 elif(now=="S"): sh,sw=i,j elif(now=="G"): gh,gw=i,j # for i in used: # print(*i) from collections import deque, defaultdict dir=[(1,0),(-1,0),(0,1),(0,-1)] def bfs(sh,sw): dq=deque() dq.append((sh,sw)) inf=float('inf') dist=[[inf]*w for i in range(h)] dist[sh][sw]=0 used[sh][sw]=0 while(dq): nowh,noww=dq.pop() if (nowh,noww)==(gh,gw): break # print(nowh,noww) for dh,dw in dir: nexth,nextw=nowh+dh,noww+dw if(grid(nexth,nextw) and used[nexth][nextw]): dist[nexth][nextw]=dist[nowh][noww]+1 dq.append((nexth,nextw)) used[nexth][nextw]=0 return dist dist=bfs(sh,sw) inf=float('inf') if(dist[gh][gw]==inf): print(-1) else: print(dist[gh][gw]) h,w=map(int, input().split()) g=[input() for i in range(h)] used=[[1]*w for i in range(h)] def grid(nowh,noww): if(0<=nowh<=h-1 and 0<=noww<=w-1): return 1 else: return 0 st={"#", ">", "v", "<", "^"} for i in range(h): for j in range(w): now=g[i][j] if(now==">"): used[i][j]=0 dw=1 while(1): if(grid(i,j+dw) and g[i][j+dw]=="."): used[i][j+dw]=0 dw+=1 else: break elif(now=="<"): used[i][j]=0 dw=-1 while(1): if(grid(i,j+dw) and g[i][j+dw]=="."): used[i][j+dw]=0 dw+=-1 else: break elif(now=="v"): used[i][j]=0 dh=1 while(1): if(grid(i+dh,j) and g[i+dh][j]=="."): used[i+dh][j]=0 dh+=1 else: break elif(now=="^"): used[i][j]=0 dh=-1 while(1): if(grid(i+dh,j) and g[i+dh][j]=="."): used[i+dh][j]=0 dh+=-1 else: break elif(now=="#"): used[i][j]=0 elif(now=="S"): sh,sw=i,j elif(now=="G"): gh,gw=i,j # for i in used: # print(*i) from collections import deque, defaultdict dir=[(1,0),(-1,0),(0,1),(0,-1)] def bfs(sh,sw): dq=deque() dq.append((sh,sw)) inf=float('inf') dist=[[inf]*w for i in range(h)] dist[sh][sw]=0 used[sh][sw]=0 while(dq): nowh,noww=dq.popleft() if (nowh,noww)==(gh,gw): break # print(nowh,noww) for dh,dw in dir: nexth,nextw=nowh+dh,noww+dw if(grid(nexth,nextw) and used[nexth][nextw]): dist[nexth][nextw]=dist[nowh][noww]+1 dq.append((nexth,nextw)) used[nexth][nextw]=0 return dist dist=bfs(sh,sw) inf=float('inf') if(dist[gh][gw]==inf): print(-1) else: print(dist[gh][gw])
ConDefects/ConDefects/Code/abc317_e/Python/52926276
condefects-python_data_1246
H, W = map(int, input().split()) S = [list(input()) for _ in range(H)] for h in range(H): for w in range(W): if S[h][w] == "S": sh, sw = h, w elif S[h][w] == "G": gh, gw = h, w for h in range(H): w = 0 flg = False while w < W: if S[h][w] not in ("*", "."): flg = False if S[h][w] == ">": flg = True elif flg: S[h][w] = "*" w += 1 w = W - 1 flg = False while w >= 0: if S[h][w] not in ("*", "."): flg = False if S[h][w] == "<": flg = True elif flg: S[h][w] = "*" w -= 1 for w in range(W): h = 0 flg = False while h < H: if S[h][w] not in ("*", "."): flg = False if S[h][w] == "v": flg = True elif flg: S[h][w] = "*" h += 1 h = 0 flg = False while h >= 0: if S[h][w] not in ("*", "."): flg = False if S[h][w] == "^": flg = True elif flg: S[h][w] = "*" h -= 1 def can_move(h, w): return 0 <= h < H and 0 <= w < W and S[h][w] in ("S", "G", ".") dh = (0, 1, 0, -1) dw = (1, 0, -1, 0) from collections import deque que = deque() que.append((sh, sw)) dist = [[-1] * W for _ in range(H)] dist[sh][sw] = 0 while que: fh, fw = que.popleft() for i in range(4): th = fh + dh[i] tw = fw + dw[i] if can_move(th, tw) and dist[th][tw] == -1: dist[th][tw] = dist[fh][fw] + 1 que.append((th, tw)) print(dist[gh][gw]) H, W = map(int, input().split()) S = [list(input()) for _ in range(H)] for h in range(H): for w in range(W): if S[h][w] == "S": sh, sw = h, w elif S[h][w] == "G": gh, gw = h, w for h in range(H): w = 0 flg = False while w < W: if S[h][w] not in ("*", "."): flg = False if S[h][w] == ">": flg = True elif flg: S[h][w] = "*" w += 1 w = W - 1 flg = False while w >= 0: if S[h][w] not in ("*", "."): flg = False if S[h][w] == "<": flg = True elif flg: S[h][w] = "*" w -= 1 for w in range(W): h = 0 flg = False while h < H: if S[h][w] not in ("*", "."): flg = False if S[h][w] == "v": flg = True elif flg: S[h][w] = "*" h += 1 h = H-1 flg = False while h >= 0: if S[h][w] not in ("*", "."): flg = False if S[h][w] == "^": flg = True elif flg: S[h][w] = "*" h -= 1 def can_move(h, w): return 0 <= h < H and 0 <= w < W and S[h][w] in ("S", "G", ".") dh = (0, 1, 0, -1) dw = (1, 0, -1, 0) from collections import deque que = deque() que.append((sh, sw)) dist = [[-1] * W for _ in range(H)] dist[sh][sw] = 0 while que: fh, fw = que.popleft() for i in range(4): th = fh + dh[i] tw = fw + dw[i] if can_move(th, tw) and dist[th][tw] == -1: dist[th][tw] = dist[fh][fw] + 1 que.append((th, tw)) print(dist[gh][gw])
ConDefects/ConDefects/Code/abc317_e/Python/51930031
condefects-python_data_1247
import sys from collections import deque,defaultdict import heapq import math import collections import itertools #sys.setrecursionlimit(10 ** 9) input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) lli = lambda n: [li() for _ in range(n)] N,X = mi() S = input() q = deque() for s in S: if q: if s == "U": q.pop() else: q.append(s) else: q.append(s) for s in q: if s == "U": X = X // 2 elif s == "L": X = 2 *X else: X = 2 * X + 1 print(X) import sys from collections import deque,defaultdict import heapq import math import collections import itertools #sys.setrecursionlimit(10 ** 9) input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) lli = lambda n: [li() for _ in range(n)] N,X = mi() S = input() q = deque() for s in S: if q: if s == "U" and q[-1] != "U": q.pop() else: q.append(s) else: q.append(s) for s in q: if s == "U": X = X // 2 elif s == "L": X = 2 *X else: X = 2 * X + 1 print(X)
ConDefects/ConDefects/Code/abc243_d/Python/45981948
condefects-python_data_1248
N,X = map(int,input().split()) S = input() Move =[] for i in range(N): if len(Move) == 0: Move.append(S[i]) else: if S[i]== "L" or S[i] == "R": Move.append(S[i]) else: if Move[-1]=="L" or Move[-1] =="R": Move.pop() else: Move.append("U") for s in Move: if s == "L": X*=2 elif s == "R": X*=2+1 else: X//=2 print(X) N,X = map(int,input().split()) S = input() Move =[] for i in range(N): if len(Move) == 0: Move.append(S[i]) else: if S[i]== "L" or S[i] == "R": Move.append(S[i]) else: if Move[-1]=="L" or Move[-1] =="R": Move.pop() else: Move.append("U") for s in Move: if s == "L": X*=2 elif s == "R": X=2*X+1 else: X//=2 print(X)
ConDefects/ConDefects/Code/abc243_d/Python/44607569
condefects-python_data_1249
N, M = map(int, input().split()) P = 1 ans = 0 for y in range(2, N + 1): P = P * (N + 1 - y) % M f = P * pow(N, N - y, M) ans += f * (y - 1) * y // 2 ans %= M print(ans * N % M) N, M = map(int, input().split()) P = 1 ans = 0 for y in range(2, N + 1): P = P * (N + 1 - y) % M f = P * pow(N, N - y, M) ans += f * (y - 1) * y // 2 ans %= M print(ans * N % M)
ConDefects/ConDefects/Code/abc284_g/Python/37908236
condefects-python_data_1250
N, T = map(int, input().split(' ')) A = tuple(map(int, input().split(' '))) def main(): rows = [0 for i in range(N)] cols = [0 for i in range(N)] backwards = 0 forwards = 0 for i, a in enumerate(A): cols[(a-1) % N] += 1 if cols[(a-1) % N] == N: # print(cols) return i + 1 rows[(a-1) // N] += 1 if rows[(a-1) // N] == N: # print(rows) return i + 1 if (a - 1) % (N + 1) == 0: backwards += 1 if backwards == N: # print("backwards") return i + 1 if (a - N) % (N - 1) == 0 and a != N ** 2: forwards += 1 if forwards == N: # print("forwards") return i + 1 return -1 print(main()) N, T = map(int, input().split(' ')) A = tuple(map(int, input().split(' '))) def main(): rows = [0 for i in range(N)] cols = [0 for i in range(N)] backwards = 0 forwards = 0 for i, a in enumerate(A): cols[(a-1) % N] += 1 if cols[(a-1) % N] == N: # print(cols) return i + 1 rows[(a-1) // N] += 1 if rows[(a-1) // N] == N: # print(rows) return i + 1 if (a - 1) % (N + 1) == 0: backwards += 1 if backwards == N: # print("backwards") return i + 1 if (a - N) % (N - 1) == 0 and a != N ** 2 and a != 1: forwards += 1 if forwards == N: # print("forwards") return i + 1 return -1 print(main())
ConDefects/ConDefects/Code/abc355_c/Python/55136098
condefects-python_data_1251
N, T = [int(x) for x in input().split()] A = [int(x) - 1 for x in input().split()] boolss = [] for _ in range(N): bools = N * [False] boolss.append(bools) bingo = False for turn, a in enumerate(A): i = a // N j = a % N boolss[i][j] = True if all(boolss[i]): bingo = True vertical = True for n in range(N): if boolss[i][n] == False: vertical = False break if vertical: bingo = True if i == j: diagonal = True for n in range(N): if boolss[n][n] == False: diagonal = False break if diagonal: bingo = True if i == N - j - 1: diagonal = True for n in range(N): if boolss[n][N - n - 1] == False: diagonal = False break if diagonal: bingo = True if bingo: print(turn + 1) break if not bingo: print(-1) N, T = [int(x) for x in input().split()] A = [int(x) - 1 for x in input().split()] boolss = [] for _ in range(N): bools = N * [False] boolss.append(bools) bingo = False for turn, a in enumerate(A): i = a // N j = a % N boolss[i][j] = True if all(boolss[i]): bingo = True vertical = True for n in range(N): if boolss[n][j] == False: vertical = False break if vertical: bingo = True if i == j: diagonal = True for n in range(N): if boolss[n][n] == False: diagonal = False break if diagonal: bingo = True if i == N - j - 1: diagonal = True for n in range(N): if boolss[n][N - n - 1] == False: diagonal = False break if diagonal: bingo = True if bingo: print(turn + 1) break if not bingo: print(-1)
ConDefects/ConDefects/Code/abc355_c/Python/54860926
condefects-python_data_1252
n, t = map(int, input().split()) a = list(map(int, input().split())) r = {} c = {} d = {1: 0, 2: 0} ans = -1 for l in range(t): ai = a[l] i = ai // n j = ai % n if j != 0: if i+1 not in r: r[i+1] = 1 else: r[i+1] += 1 if r[i+1] == n: ans = l + 1 break else: if i not in r: r[i] = 1 else: r[i] += 1 if r[i] == n: ans = l + 1 break if j != 0: if j not in c: c[j] = 1 else: c[j] += 1 if c[j] == n: ans = l + 1 break else: if n not in c: c[n] = 1 else: c[n] += 1 if c[n] == n: ans = l + 1 break if ai % (n+1) == 1: d[1] += 1 if d[1] == n: ans = l + 1 break b = ai - n if b % 2 == 0 and b // 2 >= 0 and b // 2 < n: d[2] += 1 if d[2] == n: ans = l + 1 break # print(ans) # print(r) # print(c) # print(d) print(ans) n, t = map(int, input().split()) a = list(map(int, input().split())) r = {} c = {} d = {1: 0, 2: 0} ans = -1 for l in range(t): ai = a[l] i = ai // n j = ai % n if j != 0: if i+1 not in r: r[i+1] = 1 else: r[i+1] += 1 if r[i+1] == n: ans = l + 1 break else: if i not in r: r[i] = 1 else: r[i] += 1 if r[i] == n: ans = l + 1 break if j != 0: if j not in c: c[j] = 1 else: c[j] += 1 if c[j] == n: ans = l + 1 break else: if n not in c: c[n] = 1 else: c[n] += 1 if c[n] == n: ans = l + 1 break if ai % (n+1) == 1: d[1] += 1 if d[1] == n: ans = l + 1 break b = ai - n if b % (n-1) == 0 and b // (n-1) >= 0 and b // (n-1) < n: d[2] += 1 if d[2] == n: ans = l + 1 break # print(ans) # print(r) # print(c) # print(d) print(ans)
ConDefects/ConDefects/Code/abc355_c/Python/54869003
condefects-python_data_1253
N,T = map(int, input().split()) A = list(map(int, input().split())) horizontal = [[] for _ in range(N)] vertical = [[] for _ in range(N)] diagonal = [[],[]] ans = -1 for i,a in enumerate(A): row = (a-1) // N col = (a-1) % N horizontal[row].append(a) vertical[col].append(a) if row == col: diagonal[0].append(a) if row+col == N-1: diagonal[1].append(a) if (len(horizontal[row]) == N) or (len(vertical[col-1]) == N) or (len(diagonal[0])) == N or (len(diagonal[1])) == N: ans = i+1 break print(ans) N,T = map(int, input().split()) A = list(map(int, input().split())) horizontal = [[] for _ in range(N)] vertical = [[] for _ in range(N)] diagonal = [[],[]] ans = -1 for i,a in enumerate(A): row = (a-1) // N col = (a-1) % N horizontal[row].append(a) vertical[col].append(a) if row == col: diagonal[0].append(a) if row+col == N-1: diagonal[1].append(a) if (len(horizontal[row]) == N) or (len(vertical[col]) == N) or (len(diagonal[0])) == N or (len(diagonal[1])) == N: ans = i+1 break print(ans)
ConDefects/ConDefects/Code/abc355_c/Python/54926937
condefects-python_data_1254
def row(x, n): return (x-1)//n def col(x, n): return (x-1)%n def diag1(x,n, lst1): return (x-1) in lst1 def diag2(x,n, lst2): return (x-1) in lst2 n, t = [int(num) for num in input().split()] lst = [int(num) for num in input().split()] lst1 = [num for num in range(0, n*n, n+1)] lst2 = [num for num in range(n-1, n*(n-1)+1, n-1)] rows = [0 for i in range(n)] cols = [0 for i in range(n)] d1 = 0 d2 = 0 bingo=-1 for i in range(len(lst)): r = row(lst[i], n) c = col(lst[i], n) rows[r]+=1 cols[c]+=1 if r==c: d1+=1 if r+c==n: d2+=1 if rows[r]==n or cols[c]==n or d1==n or d2==n: bingo = i+1 break print(bingo) def row(x, n): return (x-1)//n def col(x, n): return (x-1)%n def diag1(x,n, lst1): return (x-1) in lst1 def diag2(x,n, lst2): return (x-1) in lst2 n, t = [int(num) for num in input().split()] lst = [int(num) for num in input().split()] lst1 = [num for num in range(0, n*n, n+1)] lst2 = [num for num in range(n-1, n*(n-1)+1, n-1)] rows = [0 for i in range(n)] cols = [0 for i in range(n)] d1 = 0 d2 = 0 bingo=-1 for i in range(len(lst)): r = row(lst[i], n) c = col(lst[i], n) rows[r]+=1 cols[c]+=1 if r==c: d1+=1 if r+c==(n-1): d2+=1 if rows[r]==n or cols[c]==n or d1==n or d2==n: bingo = i+1 break print(bingo)
ConDefects/ConDefects/Code/abc355_c/Python/55032844
condefects-python_data_1255
from collections import defaultdict n, t = map(int, input().split()) tate = defaultdict(set) yoko = defaultdict(set) naname = defaultdict(set) for i in range(n): for j in range(n): tmp = n * i + j tate[i].add(tmp) yoko[j].add(tmp) if i == j: naname[0].add(tmp) if i + j == n - 1: naname[1].add(tmp) A = list(map(int, input().split())) cnt = 0 for a in A: a -= 1 i = a // n j = a % n tate[i].discard(a) yoko[j].discard(a) if i == j: naname[0].discard(a) if i + j == n - 1: naname[1].discard(tmp) cnt += 1 if ( len(tate[i]) == 0 or len(yoko[j]) == 0 or len(naname[0]) == 0 or len(naname[1]) == 0 ): exit(print(cnt)) print(-1) from collections import defaultdict n, t = map(int, input().split()) tate = defaultdict(set) yoko = defaultdict(set) naname = defaultdict(set) for i in range(n): for j in range(n): tmp = n * i + j tate[i].add(tmp) yoko[j].add(tmp) if i == j: naname[0].add(tmp) if i + j == n - 1: naname[1].add(tmp) A = list(map(int, input().split())) cnt = 0 for a in A: a -= 1 i = a // n j = a % n tate[i].discard(a) yoko[j].discard(a) if i == j: naname[0].discard(a) if i + j == n - 1: naname[1].discard(a) cnt += 1 if ( len(tate[i]) == 0 or len(yoko[j]) == 0 or len(naname[0]) == 0 or len(naname[1]) == 0 ): exit(print(cnt)) print(-1)
ConDefects/ConDefects/Code/abc355_c/Python/54891333
condefects-python_data_1256
n, t = map(int, input().split()) a = list(map(int, input().split())) b = [0] * (n * 2 + 2) for i, ai in enumerate(a): r = (ai - 1) % n c = (ai - 1) // n b[r] += 1 b[n + c] += 1 if r == c: b[-2] += 1 if r + c == n: b[-1] += 1 if max(b) == n: print(i + 1) exit() print(-1) n, t = map(int, input().split()) a = list(map(int, input().split())) b = [0] * (n * 2 + 2) for i, ai in enumerate(a): r = (ai - 1) % n c = (ai - 1) // n b[r] += 1 b[n + c] += 1 if r == c: b[-2] += 1 if r + c == n - 1: b[-1] += 1 if max(b) == n: print(i + 1) exit() print(-1)
ConDefects/ConDefects/Code/abc355_c/Python/54884677
condefects-python_data_1257
from itertools import permutations as perm from itertools import combinations, product, combinations_with_replacement, groupby, accumulate from fractions import Fraction from collections import * from sys import * from bisect import * from heapq import * #@再起回数の上限を上げる #sys.setrecursionlimit(10**7) # PyPy の再起関数は遅くなるので注意 #import numpy as np # from math import * g = lambda : stdin.readline().strip() #[gl[0], dl[1], ...] gl = lambda : g().split() #gl -> int gil = lambda : [int(var) for var in gl()] #[n] = gil("A") -> n = A #[n,m] = gil("A B") -> n = A, M = B #n=gil("A B C D ...") -> n = [A, B, C, D, ...] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] #[n]= gbs("A") -> n = A # A=[1,2,3,...] -> "1 2 3 ..." 配列で答えをprintする時に使う # -> print(*A) ("1 2 3 ...") # -> print(*A,sep="") ("123...") INF = 10**10 MOD = 7 + 10**9 [N]=gil() H=gil() def main(): A=[] Q=deque() S=0 for i in range(N): cc = 1 while Q and Q[-1][0]<=H[i]: (v,c) = Q.pop() cc += c S -= v*c Q.append((H[i],cc)) S += H[i]*cc A.append(S+1) print(Q) print(*A) if __name__ == "__main__": main() from itertools import permutations as perm from itertools import combinations, product, combinations_with_replacement, groupby, accumulate from fractions import Fraction from collections import * from sys import * from bisect import * from heapq import * #@再起回数の上限を上げる #sys.setrecursionlimit(10**7) # PyPy の再起関数は遅くなるので注意 #import numpy as np # from math import * g = lambda : stdin.readline().strip() #[gl[0], dl[1], ...] gl = lambda : g().split() #gl -> int gil = lambda : [int(var) for var in gl()] #[n] = gil("A") -> n = A #[n,m] = gil("A B") -> n = A, M = B #n=gil("A B C D ...") -> n = [A, B, C, D, ...] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] #[n]= gbs("A") -> n = A # A=[1,2,3,...] -> "1 2 3 ..." 配列で答えをprintする時に使う # -> print(*A) ("1 2 3 ...") # -> print(*A,sep="") ("123...") INF = 10**10 MOD = 7 + 10**9 [N]=gil() H=gil() def main(): A=[] Q=deque() S=0 for i in range(N): cc = 1 while Q and Q[-1][0]<=H[i]: (v,c) = Q.pop() cc += c S -= v*c Q.append((H[i],cc)) S += H[i]*cc A.append(S+1) #print(Q) print(*A) if __name__ == "__main__": main()
ConDefects/ConDefects/Code/abc359_e/Python/55124422
condefects-python_data_1258
def main(): r, c = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(2) ] print(a[c-1][r-1]) if __name__ == '__main__': main() def main(): r, c = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(2) ] print(a[r-1][c-1]) if __name__ == '__main__': main()
ConDefects/ConDefects/Code/abc255_a/Python/44829145
condefects-python_data_1259
import sys input = sys.stdin.readline # Dinic's algorithm from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow n,m=map(int,input().split()) S=[] for i in range(n): s=input().rstrip() S.append(s) idx=[] for i in range(n): l=[[] for _ in range(10)] for j in range(m): v=int(S[i][j]) l[v].append(j) idx.append(l) INF=10**9 ans=INF from collections import defaultdict for i in range(10): flg=True for j in range(n): if len(idx[j][i])==0: flg=False break if not flg: continue ng=0 ok=n*m while ok-ng>1: mid=(ok+ng)//2 inv=defaultdict(list) for j in range(n): cnt=0 le=len(idx[j][i]) while cnt<n: val=idx[j][i][cnt%le]+(cnt//le)*m if val>mid: break inv[val].append(j) cnt+=1 size=len(inv)+n+10 dinic = Dinic(size) for j in range(n): dinic.add_edge(0,j+1,1) fin=size-1 now=n+1 for j in inv: dinic.add_edge(now,fin,1) for k in inv[j]: dinic.add_edge(k+1,now,1) now+=1 fl=dinic.flow(0,fin) if fl>=n: ok=mid else: ng=mid ans=min(ans,ok) if ans==INF: print(-1) else: print(ans) import sys input = sys.stdin.readline # Dinic's algorithm from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow n,m=map(int,input().split()) S=[] for i in range(n): s=input().rstrip() S.append(s) idx=[] for i in range(n): l=[[] for _ in range(10)] for j in range(m): v=int(S[i][j]) l[v].append(j) idx.append(l) INF=10**9 ans=INF from collections import defaultdict for i in range(10): flg=True for j in range(n): if len(idx[j][i])==0: flg=False break if not flg: continue ng=-1 ok=n*m while ok-ng>1: mid=(ok+ng)//2 inv=defaultdict(list) for j in range(n): cnt=0 le=len(idx[j][i]) while cnt<n: val=idx[j][i][cnt%le]+(cnt//le)*m if val>mid: break inv[val].append(j) cnt+=1 size=len(inv)+n+10 dinic = Dinic(size) for j in range(n): dinic.add_edge(0,j+1,1) fin=size-1 now=n+1 for j in inv: dinic.add_edge(now,fin,1) for k in inv[j]: dinic.add_edge(k+1,now,1) now+=1 fl=dinic.flow(0,fin) if fl>=n: ok=mid else: ng=mid ans=min(ans,ok) if ans==INF: print(-1) else: print(ans)
ConDefects/ConDefects/Code/abc320_g/Python/45654657
condefects-python_data_1260
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict as dd INF=10**18 ##二部マッチング ##参考:https://snuke.hatenablog.com/entry/2019/05/07/013609 class Bipartile_Matching: def __init__(self,L,R): ##L2R:Lから見たRのマッチングを記録 ##R2L:Rから見たLのマッチングを記録 ##backpath:L側に逆辺が張られている場合の辿る先 ##root:逆辺を考慮したLの始点を記録 self.L=L self.R=R self.L2R=[-1]*L self.R2L=[-1]*R self.backpath=[-1]*L self.root=[-1]*L self.edge=dd(list) def add_edge(self,s,t): self.edge[s].append(t) def match(self): res=0 f=True while f: f=False q=deque() for i in range(self.L): ##まだマッチング対象が見つかっていなければ ##iを始点としてキューに追加 if self.L2R[i]==-1: self.root[i]=i q.append(i) while q: s=q.popleft() ##逆辺を辿った先がすでに決まっていればcontinue if ~self.L2R[self.root[s]]:continue ##始点から接続されている辺を全探索する for t in self.edge[s]: if self.R2L[t]==-1: ##逆辺が存在する場合は辿っていく while t!=-1: self.R2L[t]=s self.L2R[s],t=t,self.L2R[s] s=self.backpath[s] f=True res+=1 break ##仮のtに対するマッチング候補の情報を更新しキューに追加する temps=self.R2L[t] if ~self.backpath[temps]:continue self.backpath[temps]=s self.root[temps]=self.root[s] q.append(temps) ##更新があれば逆辺・始点情報を初期化する if f: self.backpath=[-1]*self.L self.root=[-1]*self.L return res def judge(mid,j): ##ここに判定条件を書く # 二分マッチングを作成する # 左辺は座標圧縮したリールの停止時間 # 右辺はリール L=len(t2n) G=Bipartile_Matching(L,N) for i in range(N): for k in range(N): t=i2t[i][j][k] # 時間がmid以下ならパスを繋ぐ if t<=mid: # 座標圧縮する n=t2n[t] # 時間とリールでパスを繋ぐ G.add_edge(n,i) # マッチングを解いて数を数える cnt=G.match() if cnt==N:return True else:return False ##ng,okの条件に気をつける def meguru_bisearch(ng, ok ,j): while abs(ok-ng)>1: mid=(ok+ng)//2 if judge(mid,j):ok=mid else:ng=mid return ok # 各数字に対して時間とリールで # マッチングといわれると解けそう N,M=map(int,input().split()) # i2t[i][j]:リールiの数字jが存在する時間をN個まで記録 i2t=[[[] for _ in range(10)] for _ in range(N)] # n2t[j]:数字jがすべてのリール中で出現する時間tを記録する # 後でソートする n2t=[set() for _ in range(10)] # 座標圧縮を行う # 各リールに数字jが出現する場合のインデックスを # まとめてソートする for i in range(N): S=input() for t,j in enumerate(S): j=int(j) if len(i2t[i][j])<N: i2t[i][j].append(t) n2t[j].add(t) # 各数字に対して出現個数がN個に満たない場合は追加する for j in range(10): if len(i2t[i][j]): k=0 while len(i2t[i][j])<N: t=i2t[i][j][k]+M i2t[i][j].append(t) n2t[j].add(t) k+=1 res=INF # 数字jで揃えることを考える for j in range(10): # まずすべてのリールに数字jが出現することを確認する # しなければcontinue f=False for i in range(N): if len(i2t[i][j])==0:f=True if f:continue # 座標圧縮する # t2n[t]:数字jにおいて時間tが出現する順番nを記録 t2n=dd(int) # n2t[j][n]:数字jのn番目に現れる時間tを記録 n2t[j]=sorted(n2t[j]) for n,t in enumerate(n2t[j]):t2n[t]=n # リールi,数字jのk番目の出現時間mに対応する # 圧縮後のインデックスnを確認していく # ここで時間について二分探索しながら # 辺の追加を行ってマッチングが成立する # 最小の時間を求めるのか res=min(res,meguru_bisearch(-1,len(n2t[j]),j)) if res==INF:print(-1) else:print(res) import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict as dd INF=10**18 ##二部マッチング ##参考:https://snuke.hatenablog.com/entry/2019/05/07/013609 class Bipartile_Matching: def __init__(self,L,R): ##L2R:Lから見たRのマッチングを記録 ##R2L:Rから見たLのマッチングを記録 ##backpath:L側に逆辺が張られている場合の辿る先 ##root:逆辺を考慮したLの始点を記録 self.L=L self.R=R self.L2R=[-1]*L self.R2L=[-1]*R self.backpath=[-1]*L self.root=[-1]*L self.edge=dd(list) def add_edge(self,s,t): self.edge[s].append(t) def match(self): res=0 f=True while f: f=False q=deque() for i in range(self.L): ##まだマッチング対象が見つかっていなければ ##iを始点としてキューに追加 if self.L2R[i]==-1: self.root[i]=i q.append(i) while q: s=q.popleft() ##逆辺を辿った先がすでに決まっていればcontinue if ~self.L2R[self.root[s]]:continue ##始点から接続されている辺を全探索する for t in self.edge[s]: if self.R2L[t]==-1: ##逆辺が存在する場合は辿っていく while t!=-1: self.R2L[t]=s self.L2R[s],t=t,self.L2R[s] s=self.backpath[s] f=True res+=1 break ##仮のtに対するマッチング候補の情報を更新しキューに追加する temps=self.R2L[t] if ~self.backpath[temps]:continue self.backpath[temps]=s self.root[temps]=self.root[s] q.append(temps) ##更新があれば逆辺・始点情報を初期化する if f: self.backpath=[-1]*self.L self.root=[-1]*self.L return res def judge(mid,j): ##ここに判定条件を書く # 二分マッチングを作成する # 左辺は座標圧縮したリールの停止時間 # 右辺はリール L=len(t2n) G=Bipartile_Matching(L,N) for i in range(N): for k in range(N): t=i2t[i][j][k] # 時間がmid以下ならパスを繋ぐ if t<=mid: # 座標圧縮する n=t2n[t] # 時間とリールでパスを繋ぐ G.add_edge(n,i) # マッチングを解いて数を数える cnt=G.match() if cnt==N:return True else:return False ##ng,okの条件に気をつける def meguru_bisearch(ng, ok ,j): while abs(ok-ng)>1: mid=(ok+ng)//2 if judge(mid,j):ok=mid else:ng=mid return ok # 各数字に対して時間とリールで # マッチングといわれると解けそう N,M=map(int,input().split()) # i2t[i][j]:リールiの数字jが存在する時間をN個まで記録 i2t=[[[] for _ in range(10)] for _ in range(N)] # n2t[j]:数字jがすべてのリール中で出現する時間tを記録する # 後でソートする n2t=[set() for _ in range(10)] # 座標圧縮を行う # 各リールに数字jが出現する場合のインデックスを # まとめてソートする for i in range(N): S=input() for t,j in enumerate(S): j=int(j) if len(i2t[i][j])<N: i2t[i][j].append(t) n2t[j].add(t) # 各数字に対して出現個数がN個に満たない場合は追加する for j in range(10): if len(i2t[i][j]): k=0 while len(i2t[i][j])<N: t=i2t[i][j][k]+M i2t[i][j].append(t) n2t[j].add(t) k+=1 res=INF # 数字jで揃えることを考える for j in range(10): # まずすべてのリールに数字jが出現することを確認する # しなければcontinue f=False for i in range(N): if len(i2t[i][j])==0:f=True if f:continue # 座標圧縮する # t2n[t]:数字jにおいて時間tが出現する順番nを記録 t2n=dd(int) # n2t[j][n]:数字jのn番目に現れる時間tを記録 n2t[j]=sorted(n2t[j]) for n,t in enumerate(n2t[j]):t2n[t]=n # リールi,数字jのk番目の出現時間mに対応する # 圧縮後のインデックスnを確認していく # ここで時間について二分探索しながら # 辺の追加を行ってマッチングが成立する # 最小の時間を求めるのか res=min(res,meguru_bisearch(-1,max(n2t[j]),j)) if res==INF:print(-1) else:print(res)
ConDefects/ConDefects/Code/abc320_g/Python/45748637
condefects-python_data_1261
import sys read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip() import bisect,string,math,time,functools,random,fractions from bisect import* from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter from itertools import permutations,combinations,groupby rep=range;R=range def I():return int(input()) def LI():return [int(i) for i in input().split()] def SLI():return sorted([int(i) for i in input().split()]) def LI_():return [int(i)-1 for i in input().split()] def S_():return input() def IS():return input().split() def LS():return [i for i in input().split()] def NI(n):return [int(input()) for i in range(n)] def NI_(n):return [int(input())-1 for i in range(n)] def NLI(n):return [[int(i) for i in input().split()] for i in range(n)] def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)] def StoLI():return [ord(i)-97 for i in input()] def ItoS(n):return chr(n+97) def LtoS(ls):return ''.join([chr(i+97) for i in ls]) def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)] def RI(a=1,b=10):return random.randint(a,b) def INP(): N=6 n=random.randint(1,N) mn,mx=0,n a=[random.randint(mn,mx) for i in range(n)] return n,a def Rtest(T): case,err=0,0 for i in range(T): inp=INP() a1=naive(*inp) a2=solve(*inp) if a1!=a2: print(inp) print('naive',a1) print('solve',a2) err+=1 case+=1 print('Tested',case,'case with',err,'errors') def GI(V,E,ls=None,Directed=False,index=1): org_inp=[];g=[[] for i in range(V)] FromStdin=True if ls==None else False for i in range(E): if FromStdin: inp=LI() org_inp.append(inp) else: inp=ls[i] if len(inp)==2:a,b=inp;c=1 else:a,b,c=inp if index==1:a-=1;b-=1 aa=a,c,;bb=b,c,;g[a].append(bb) if not Directed:g[b].append(aa) return g,org_inp def RE(E): rt=[[]for i in range(len(E))] for i in range(len(E)): for nb,d in E[i]: rt[nb]+=(i,d), return rt def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1): #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage mp=[boundary]*(w+2);found={} for i in R(h): s=input() for char in search: if char in s: found[char]=((i+1)*(w+2)+s.index(char)+1) mp_def[char]=mp_def[replacement_of_found] mp+=[boundary]+[mp_def[j] for j in s]+[boundary] mp+=[boundary]*(w+2) return h+2,w+2,mp,found def TI(n):return GI(n,n-1) def accum(ls): rt=[0] for i in ls:rt+=[rt[-1]+i] return rt def bit_combination(n,base=2): rt=[] for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s] return rt def gcd(x,y): if y==0:return x if x%y==0:return y while x%y!=0:x,y=y,x%y return y def RLE(it): rt=[] for i in it: if rt and rt[-1][0]==i:rt[-1][1]+=1 else:rt+=[i,1], return rt def YN(x):print(['NO','YES'][x]) def Yn(x):print(['No','Yes'][x]) def show(*inp,end='\n'): if show_flg:print(*inp,end=end) def gcj(t,*a): print('Case #{}:'.format(t+1),*a) mo=10**9+7 #mo=998244353 inf=float('inf') FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb)) alp=[chr(ord('a')+i)for i in range(26)] #sys.setrecursionlimit(10**7) show_flg=False show_flg=True ans=0 n,d=LI() a=LI_() mo=998244353 def conv(bt): for i in range(2*d+1): 0 D=1<<(2*d+1) dp=[[0]*D for i in range(n+1)] dp[0][0]=1 for i in range(n): c=a[i] for b in range(D): if c<0: # 置くものが決まってない if i-d>=0 and b>>(2*d)==0: # 置くものがi-dに決まる nx=((b|1<<(2*d))<<1)%D #show(i,nx) dp[i+1][nx]+=dp[i][b] dp[i+1][nx]%=mo else: for j in range(2*d+1): if b>>j&1: continue nx=((b|1<<j)<<1)%D dp[i+1][nx]+=dp[i][b] dp[i+1][nx]%=mo else: # 置くものが決まってる if b>>(d+i-c)&1: pass # 遷移なし else: nx=((b|1<<(d+i-c))<<1)%D dp[i+1][nx]+=dp[i][b] dp[i+1][nx]%=mo ans=dp[n][(1<<(2*d+1))-(1<<(d+1))] print(ans) import sys read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip() import bisect,string,math,time,functools,random,fractions from bisect import* from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter from itertools import permutations,combinations,groupby rep=range;R=range def I():return int(input()) def LI():return [int(i) for i in input().split()] def SLI():return sorted([int(i) for i in input().split()]) def LI_():return [int(i)-1 for i in input().split()] def S_():return input() def IS():return input().split() def LS():return [i for i in input().split()] def NI(n):return [int(input()) for i in range(n)] def NI_(n):return [int(input())-1 for i in range(n)] def NLI(n):return [[int(i) for i in input().split()] for i in range(n)] def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)] def StoLI():return [ord(i)-97 for i in input()] def ItoS(n):return chr(n+97) def LtoS(ls):return ''.join([chr(i+97) for i in ls]) def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)] def RI(a=1,b=10):return random.randint(a,b) def INP(): N=6 n=random.randint(1,N) mn,mx=0,n a=[random.randint(mn,mx) for i in range(n)] return n,a def Rtest(T): case,err=0,0 for i in range(T): inp=INP() a1=naive(*inp) a2=solve(*inp) if a1!=a2: print(inp) print('naive',a1) print('solve',a2) err+=1 case+=1 print('Tested',case,'case with',err,'errors') def GI(V,E,ls=None,Directed=False,index=1): org_inp=[];g=[[] for i in range(V)] FromStdin=True if ls==None else False for i in range(E): if FromStdin: inp=LI() org_inp.append(inp) else: inp=ls[i] if len(inp)==2:a,b=inp;c=1 else:a,b,c=inp if index==1:a-=1;b-=1 aa=a,c,;bb=b,c,;g[a].append(bb) if not Directed:g[b].append(aa) return g,org_inp def RE(E): rt=[[]for i in range(len(E))] for i in range(len(E)): for nb,d in E[i]: rt[nb]+=(i,d), return rt def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1): #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage mp=[boundary]*(w+2);found={} for i in R(h): s=input() for char in search: if char in s: found[char]=((i+1)*(w+2)+s.index(char)+1) mp_def[char]=mp_def[replacement_of_found] mp+=[boundary]+[mp_def[j] for j in s]+[boundary] mp+=[boundary]*(w+2) return h+2,w+2,mp,found def TI(n):return GI(n,n-1) def accum(ls): rt=[0] for i in ls:rt+=[rt[-1]+i] return rt def bit_combination(n,base=2): rt=[] for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s] return rt def gcd(x,y): if y==0:return x if x%y==0:return y while x%y!=0:x,y=y,x%y return y def RLE(it): rt=[] for i in it: if rt and rt[-1][0]==i:rt[-1][1]+=1 else:rt+=[i,1], return rt def YN(x):print(['NO','YES'][x]) def Yn(x):print(['No','Yes'][x]) def show(*inp,end='\n'): if show_flg:print(*inp,end=end) def gcj(t,*a): print('Case #{}:'.format(t+1),*a) mo=10**9+7 #mo=998244353 inf=float('inf') FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb)) alp=[chr(ord('a')+i)for i in range(26)] #sys.setrecursionlimit(10**7) show_flg=False show_flg=True ans=0 n,d=LI() a=LI_() mo=998244353 def conv(bt): for i in range(2*d+1): 0 D=1<<(2*d+1) dp=[[0]*D for i in range(n+1)] dp[0][D-(1<<-~d)]=1 for i in range(n): c=a[i] for b in range(D): if c<0: # 置くものが決まってない if i-d>=0 and b>>(2*d)==0: # 置くものがi-dに決まる nx=((b|1<<(2*d))<<1)%D #show(i,nx) dp[i+1][nx]+=dp[i][b] dp[i+1][nx]%=mo else: for j in range(2*d+1): if b>>j&1: continue nx=((b|1<<j)<<1)%D dp[i+1][nx]+=dp[i][b] dp[i+1][nx]%=mo else: # 置くものが決まってる if b>>(d+i-c)&1: pass # 遷移なし else: nx=((b|1<<(d+i-c))<<1)%D dp[i+1][nx]+=dp[i][b] dp[i+1][nx]%=mo ans=dp[n][(1<<(2*d+1))-(1<<(d+1))] print(ans)
ConDefects/ConDefects/Code/arc132_c/Python/31137799
condefects-python_data_1262
n,d = map(int,input().split()) a = [0] + list(map(int,input().split())) dp = [[0] * (1<<(2*d+1)) for _ in range(n+1)] dp[0][0] = 1 mod = 998244353 for i in range(n): for s in range(1<<(2*d+1)): if a[i+1] != -1: if ((s>>1) & (1<<(a[i+1]-(i+1)+d))) == 0: dp[i+1][(s>>1) | (1<<(a[i+1]-(i+1)+d))] += dp[i][s] dp[i+1][(s>>1) | (1<<(a[i+1]-(i+1)+d))] %=mod else: for k in range(-d,d+1): if 1 <= i+1 + k <= n and a[i+1+k] == -1 and ((s>>1) & (1<<(k+d))) ==0: dp[i+1][(s>>1)|(1<<(k+d))] += dp[i][s] dp[i+1][(s>>1)|(1<<(k+d))] %=mod ans = 0 for s in range(1<<(2*d+1)): ans += dp[n][s] ans %=mod print(ans) n,d = map(int,input().split()) a = [0] + list(map(int,input().split())) dp = [[0] * (1<<(2*d+1)) for _ in range(n+1)] dp[0][0] = 1 mod = 998244353 for i in range(n): for s in range(1<<(2*d+1)): if a[i+1] != -1: if ((s>>1) & (1<<(a[i+1]-(i+1)+d))) == 0: dp[i+1][(s>>1) | (1<<(a[i+1]-(i+1)+d))] += dp[i][s] dp[i+1][(s>>1) | (1<<(a[i+1]-(i+1)+d))] %=mod else: for k in range(-d,d+1): if 1 <= i+1 + k <= n and ((s>>1) & (1<<(k+d))) ==0: dp[i+1][(s>>1)|(1<<(k+d))] += dp[i][s] dp[i+1][(s>>1)|(1<<(k+d))] %=mod ans = 0 for s in range(1<<(2*d+1)): ans += dp[n][s] ans %=mod print(ans)
ConDefects/ConDefects/Code/arc132_c/Python/35435888
condefects-python_data_1263
N,D = map(int, input().split()) A =[int(i) for i in input().split()] mod = 998244353 S = 1<<(2*D+2) dp = [[0]*(S) for _ in range(N+1)] dp[0][(1<<(D+1)) - 1] = 1 for i,a in enumerate(A): if a>-1: for s in range(S): if s&1==0: continue t = s>>1 diff = a-(i+1) + D if t>>diff&1: continue dp[i+1][t|(1<<diff)] += dp[i][s] dp[i+1][t|(1<<diff)] %= mod else: for s in range(S): if s&1==0: continue t = s>>1 for diff in range(0,2*D+2): if t>>diff&1: continue dp[i+1][t|(1<<diff)] += dp[i][s] dp[i+1][t|(1<<diff)] %= mod # for i in range(N+1): # print(dp[i][:30]) print(dp[-1][(1<<(D+1))-1]) N,D = map(int, input().split()) A =[int(i) for i in input().split()] mod = 998244353 S = 1<<(2*D+2) dp = [[0]*(S) for _ in range(N+1)] dp[0][(1<<(D+1)) - 1] = 1 for i,a in enumerate(A): if a>-1: for s in range(S): if s&1==0: continue t = s>>1 diff = a-(i+1) + D if t>>diff&1: continue dp[i+1][t|(1<<diff)] += dp[i][s] dp[i+1][t|(1<<diff)] %= mod else: for s in range(S): if s&1==0: continue t = s>>1 for diff in range(0,2*D+1): if t>>diff&1: continue dp[i+1][t|(1<<diff)] += dp[i][s] dp[i+1][t|(1<<diff)] %= mod # for i in range(N+1): # print(dp[i][:30]) print(dp[-1][(1<<(D+1))-1])
ConDefects/ConDefects/Code/arc132_c/Python/31270911
condefects-python_data_1264
import sys;input=sys.stdin.readline def bsearch(mn, mx, func): #func(i)=False を満たす最大のi (mn<=i<mx) idx = (mx + mn)//2 while mx-mn>1: if func(idx): idx, mx = (idx + mn)//2, idx continue idx, mn = (idx + mx)//2, idx return idx def f(k): # print() sm = 0 for i in range(N): a, b = X[i], X[N-1-i] if b-a < k: break print(a,b) sm += b-a-k # print(sm) for i in range(N): a, b = Y[i], Y[N-1-i] if b-a < k: break sm += b-a-k return sm N, K = map(int, input().split()) X = [] Y = [] for _ in range(N): x, y = map(int, input().split()) X.append(x) Y.append(y) X.sort() Y.sort() #print(f(3)) print(bsearch(-1, 10**9*1, lambda x:f(x)<=K)+1) import sys;input=sys.stdin.readline def bsearch(mn, mx, func): #func(i)=False を満たす最大のi (mn<=i<mx) idx = (mx + mn)//2 while mx-mn>1: if func(idx): idx, mx = (idx + mn)//2, idx continue idx, mn = (idx + mx)//2, idx return idx def f(k): # print() sm = 0 for i in range(N): a, b = X[i], X[N-1-i] if b-a < k: break # print(a,b) sm += b-a-k # print(sm) for i in range(N): a, b = Y[i], Y[N-1-i] if b-a < k: break sm += b-a-k return sm N, K = map(int, input().split()) X = [] Y = [] for _ in range(N): x, y = map(int, input().split()) X.append(x) Y.append(y) X.sort() Y.sort() #print(f(3)) print(bsearch(-1, 10**9*1, lambda x:f(x)<=K)+1)
ConDefects/ConDefects/Code/abc330_f/Python/48327716
condefects-python_data_1265
import heapq N,K = map(int, input().split()) X=[] Y=[] for _ in range(N): x,y = map(int, input().split()) X.append(x) Y.append(y) X.sort() Y.sort() def solve(A,d): h=A[:] heapq.heapify(h) for i in range(N): l=heapq.heappushpop(h, A[i]-d) ans=0 for a in A: if a<l: ans+=l-a elif a>l+d: ans+=a-(l+d) return ans #判定用関数 def judge(d): if solve(X,d)+solve(Y,d)<K: return False else: return True #二分探索 def binary(l,r): while r-l>1: mid=(l+r)//2 #print(judge(mid)) if judge(mid): l=mid else: r=mid return l #print(judge(0)) print(binary(-1,max(max(Y)-min(Y)+2,max(X)-min(X)+2))+1) import heapq N,K = map(int, input().split()) X=[] Y=[] for _ in range(N): x,y = map(int, input().split()) X.append(x) Y.append(y) X.sort() Y.sort() def solve(A,d): h=A[:] heapq.heapify(h) for i in range(N): l=heapq.heappushpop(h, A[i]-d) ans=0 for a in A: if a<l: ans+=l-a elif a>l+d: ans+=a-(l+d) return ans #判定用関数 def judge(d): if solve(X,d)+solve(Y,d)<=K: return False else: return True #二分探索 def binary(l,r): while r-l>1: mid=(l+r)//2 #print(judge(mid)) if judge(mid): l=mid else: r=mid return l #print(judge(0)) print(binary(-1,max(max(Y)-min(Y)+2,max(X)-min(X)+2))+1)
ConDefects/ConDefects/Code/abc330_f/Python/48207650
condefects-python_data_1266
N, mod = map(int, input().split()) con = [[0]*N for _ in range(N)] sep = [[0]*(N+1) for _ in range(N)] con[0][0] = 1 sep[0][1] = 1 for n in range(N)[1:]: con[n][0] = 1 for m in range(n+1)[1:]: con[n][m] = con[n-1][m] + con[n-1][m-1]*3 + sep[n-1][m] con[n][m] %= mod for m in range(n+2)[2:]: sep[n][m] = con[n-1][m-2]*2 + sep[n-1][m-1] sep[n][m] %= mod print(*con[-1]) N, mod = map(int, input().split()) con = [[0]*N for _ in range(N)] sep = [[0]*(N+1) for _ in range(N)] con[0][0] = 1 sep[0][1] = 1 for n in range(N)[1:]: con[n][0] = 1 for m in range(n+1)[1:]: con[n][m] = con[n-1][m] + con[n-1][m-1]*3 + sep[n-1][m] con[n][m] %= mod for m in range(n+2)[2:]: sep[n][m] = con[n-1][m-2]*2 + sep[n-1][m-1] sep[n][m] %= mod print(*con[-1][1:])
ConDefects/ConDefects/Code/abc248_f/Python/53199737
condefects-python_data_1267
#@markdown # 04 Tires text=list(input()) lasttext="".join(text[-1:-2]) if lasttext=="er": print("er") else: print("ist") #@markdown # 04 Tires text=list(input()) lasttext="".join(text[-2:]) if lasttext=="er": print("er") else: print("ist")
ConDefects/ConDefects/Code/abc224_a/Python/45979243
condefects-python_data_1268
s=input() if s[-2:]=="er": print("er") if s[-2:]=="ist": print("ist") s=input() if s[-2:]=="er": print("er") if s[-3:]=="ist": print("ist")
ConDefects/ConDefects/Code/abc224_a/Python/45476747
condefects-python_data_1269
s=input() print('er' if s[-2:]=='er' else 'est') s=input() print('er' if s[-2:]=='er' else 'ist')
ConDefects/ConDefects/Code/abc224_a/Python/45312874
condefects-python_data_1270
S = input() if S[-2] == 'er': print('er') else: print('ist') S = input() if S[-1] == 'r': print('er') else: print('ist')
ConDefects/ConDefects/Code/abc224_a/Python/45433181
condefects-python_data_1271
A = input() if A[len(A) - 1] == "r": print("er") else: print("st") A = input() if A[len(A) - 1] == "r": print("er") else: print("ist")
ConDefects/ConDefects/Code/abc224_a/Python/45808243
condefects-python_data_1272
s = input() a = len(s) if s[a - 2 : a - 1] == "er": print("er") else: print("ist") s = input() a = len(s) if s[a - 2 :] == "er": print("er") else: print("ist")
ConDefects/ConDefects/Code/abc224_a/Python/45808674
condefects-python_data_1273
s = input() if s[-2:] == "er": print("er") elif s[-2:] == "ist": print("ist") s = input() if s[-2:] == "er": print("er") else: print("ist")
ConDefects/ConDefects/Code/abc224_a/Python/45545043
condefects-python_data_1274
s=input() if s[-2] == "er": print("er") else: print("ist") s=input() if s[-2:] == "er": print("er") else: print("ist")
ConDefects/ConDefects/Code/abc224_a/Python/46029165
condefects-python_data_1275
def main(): N = int(input()) S = input() S = [S[:N], S[N:2*N], S[2*N:]] print(S) d = [{"A": S[i].count("A"), "B": S[i].count("B"), "C": S[i].count("C")} for i in range(3)] idx = [{"A": 0, "B": 0, "C": 0} for i in range(3)] perm = ["ABC", "ACB", "BCA", "BAC", "CAB", "CBA"] ans = [[0] * N for i in range(3)] for k, p in enumerate(perm): cnt = N for i in range(3): cnt = min(cnt, d[i][p[i]]) print(cnt) for i in range(3): t = cnt for j in range(idx[i][p[i]], N): if t == 0: break idx[i][p[i]] += 1 if S[i][j] == p[i]: ans[i][j] = k+1 d[i][p[i]] -= 1 t -= 1 print("".join(map(str, ans[0] + ans[1] + ans[2]))) if __name__=="__main__": main() def main(): N = int(input()) S = input() S = [S[:N], S[N:2*N], S[2*N:]] d = [{"A": S[i].count("A"), "B": S[i].count("B"), "C": S[i].count("C")} for i in range(3)] idx = [{"A": 0, "B": 0, "C": 0} for i in range(3)] perm = ["ABC", "ACB", "BCA", "BAC", "CAB", "CBA"] ans = [[0] * N for i in range(3)] for k, p in enumerate(perm): cnt = N for i in range(3): cnt = min(cnt, d[i][p[i]]) for i in range(3): t = cnt for j in range(idx[i][p[i]], N): if t == 0: break idx[i][p[i]] += 1 if S[i][j] == p[i]: ans[i][j] = k+1 d[i][p[i]] -= 1 t -= 1 print("".join(map(str, ans[0] + ans[1] + ans[2]))) if __name__=="__main__": main()
ConDefects/ConDefects/Code/agc055_a/Python/37010633
condefects-python_data_1276
from bisect import * N = int(input()) S = input() X = [[] for _ in range(3)] for i, s in enumerate(S): X[ord(s) - 65].append(i) #print(X) ans = ["0"] * (3 * N) n = N for i in range(1, 6): start = 3 * N for j in range(3): if X[j][0] < start: start = X[j][0] c1 = j stop = -1 for j in range(3): if j == c1: continue if X[j][-1] > stop: stop = X[j][-1] c3 = j c2 = (3 ^ c1 ^ c3) for j in range(n): a1 = X[c1][j] a3 = X[c3][-j - 1] d1 = bisect_left(X[c2], a1) d2 = bisect_left(X[c2], a3) if d2 - d1 <= j: j -= 1 break d0 = d1 for k in range(j + 1): ans[X[c1][k]] = str(i) ans[X[c2][d0 + k]] = str(i) ans[X[c3][-k - 1]] = str(i) X[c1] = X[c1][j + 1:] X[c2] = X[c2][:d0] + X[c2][d0 + j + 1:] X[c3] = X[c3][:-j - 1] #print(j + 1, c1, c2, c3, " ", d0, X, ans) n = n - j - 1 if n == 0: break print("".join(ans)) ''' for j in range(1, i + 1): tmp = "" for k in range(3 * N): if ans[k] == str(j): tmp += S[k] print(j, tmp) ''' from bisect import * N = int(input()) S = input() X = [[] for _ in range(3)] for i, s in enumerate(S): X[ord(s) - 65].append(i) #print(X) ans = ["0"] * (3 * N) n = N for i in range(1, 7): start = 3 * N for j in range(3): if X[j][0] < start: start = X[j][0] c1 = j stop = -1 for j in range(3): if j == c1: continue if X[j][-1] > stop: stop = X[j][-1] c3 = j c2 = (3 ^ c1 ^ c3) for j in range(n): a1 = X[c1][j] a3 = X[c3][-j - 1] d1 = bisect_left(X[c2], a1) d2 = bisect_left(X[c2], a3) if d2 - d1 <= j: j -= 1 break d0 = d1 for k in range(j + 1): ans[X[c1][k]] = str(i) ans[X[c2][d0 + k]] = str(i) ans[X[c3][-k - 1]] = str(i) X[c1] = X[c1][j + 1:] X[c2] = X[c2][:d0] + X[c2][d0 + j + 1:] X[c3] = X[c3][:-j - 1] #print(j + 1, c1, c2, c3, " ", d0, X, ans) n = n - j - 1 if n == 0: break print("".join(ans)) ''' for j in range(1, i + 1): tmp = "" for k in range(3 * N): if ans[k] == str(j): tmp += S[k] print(j, tmp) '''
ConDefects/ConDefects/Code/agc055_a/Python/44002402
condefects-python_data_1277
N = int(input()) S = [ord(s) - ord("a") for s in input()] T = [ord(s) - ord("a") for s in input()] Ns, Nt = len(S), len(T) A = [[] for i in range(26)] S = S + S for i in range(2 * Ns): A[S[i]].append(i) for i in range(Nt): if len(A[T[i]]) == 0: print(0) exit() C = [[0] * (2 * Ns) for i in range(26)] Ac = [[0] * (2 * Ns + 1) for i in range(26)] for i in range(2 * Ns): C[S[i]][i] = 1 for c in range(26): for i in range(2 * Ns): Ac[c][i + 1] = Ac[c][i] + C[c][i] cnt = [] for i in range(26): cnt.append(Ac[i][-1]//2) def check(m): now = 0 v = 0 for t in T: q = (m - 1)//cnt[t] r = m - q * cnt[t] v += q * Ns st = Ac[t][now] now = A[t][st + r - 1] + 1 if now >= Ns: v += Ns now -= Ns v += now + 1 return v <= N * Ns def BinarySearch(yes = 10 ** 18, no = -1): while abs(yes - no) != 1: mid = (yes + no)//2 if check(mid): yes = mid else: no = mid return yes yes = 0 no = 10 ** 18 print(BinarySearch(yes, no)) N = int(input()) S = [ord(s) - ord("a") for s in input()] T = [ord(s) - ord("a") for s in input()] Ns, Nt = len(S), len(T) A = [[] for i in range(26)] S = S + S for i in range(2 * Ns): A[S[i]].append(i) for i in range(Nt): if len(A[T[i]]) == 0: print(0) exit() C = [[0] * (2 * Ns) for i in range(26)] Ac = [[0] * (2 * Ns + 1) for i in range(26)] for i in range(2 * Ns): C[S[i]][i] = 1 for c in range(26): for i in range(2 * Ns): Ac[c][i + 1] = Ac[c][i] + C[c][i] cnt = [] for i in range(26): cnt.append(Ac[i][-1]//2) def check(m): now = 0 v = 0 for t in T: q = (m - 1)//cnt[t] r = m - q * cnt[t] v += q * Ns st = Ac[t][now] now = A[t][st + r - 1] + 1 if now >= Ns: v += Ns now -= Ns v += now return v <= N * Ns def BinarySearch(yes = 10 ** 18, no = -1): while abs(yes - no) != 1: mid = (yes + no)//2 if check(mid): yes = mid else: no = mid return yes yes = 0 no = 10 ** 18 print(BinarySearch(yes, no))
ConDefects/ConDefects/Code/abc346_f/Python/54698915
condefects-python_data_1278
N = int(input()) S = input() T = input() sl = len(S) tl = len(T) ind = [[] for i in range(26)] for i in range(2 * sl): ind[ord(S[i % sl]) - ord("a")].append(i) cnt = [0 for i in range(26)] for i in range(26): cnt[i] = len(ind[i]) // 2 import bisect def is_ok(k): itr = -1 # f(S, N)[0, itr)まで見た状況 for i in range(tl): q = itr // sl r = itr % sl a = ord(T[i]) - ord("a") if cnt[a] == 0: return False now = cnt[a] * q + bisect.bisect_right(ind[a], r) next = now + k nq = next // cnt[a] nr = next % cnt[a] if nr == 0: nq -= 1 nr += cnt[a] itr = nq * sl + ind[a][nr - 1] # print(k, itr, now, next) if itr > N * sl: return False return True def m_bisect(ng, ok): while abs(ok - ng) > 1: mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid return ok ok, ng = 0, 10 ** 18 print(m_bisect(ng, ok)) N = int(input()) S = input() T = input() sl = len(S) tl = len(T) ind = [[] for i in range(26)] for i in range(2 * sl): ind[ord(S[i % sl]) - ord("a")].append(i) cnt = [0 for i in range(26)] for i in range(26): cnt[i] = len(ind[i]) // 2 import bisect def is_ok(k): itr = -1 # f(S, N)[0, itr)まで見た状況 for i in range(tl): q = itr // sl r = itr % sl a = ord(T[i]) - ord("a") if cnt[a] == 0: return False now = cnt[a] * q + bisect.bisect_right(ind[a], r) next = now + k nq = next // cnt[a] nr = next % cnt[a] if nr == 0: nq -= 1 nr += cnt[a] itr = nq * sl + ind[a][nr - 1] # print(k, itr, now, next) if itr >= N * sl: return False return True def m_bisect(ng, ok): while abs(ok - ng) > 1: mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid return ok ok, ng = 0, 10 ** 18 print(m_bisect(ng, ok))
ConDefects/ConDefects/Code/abc346_f/Python/53286789
condefects-python_data_1279
n = int(input()) s = input() t = input() # 定义p(a,b,c)为f(s,n)的下标a后的出现的第b个字符c的位置 # 1.如果a>len(s),可以等价为p(a-len(s),b,c)+len(s) # 2.如果s.count(c)<b,可以等价为p(a+len(s),b-s.count(c),c) # 以上两步可以将a,b转为0<=a<len(s), 0<=b<s.count(c) cnt = [0]*26 pos = [[] for _ in range(26)] pre = [[0]*26 for _ in range(len(s)+1)] for i, c in enumerate(s*2): idx = ord(c)-ord('a') pos[idx].append(i) for i in range(26): cnt[i] = len(pos[i])//2 for i in range(1, len(s)+1): pre[i] = pre[i-1][:] pre[i][ord(s[i-1])-ord('a')] += 1 def check(m): it = 0 for i in range(len(t)): d = ord(t[i])-ord('a') if cnt[d] == 0: return False r = (m-1) % cnt[d] + 1 b = (m-r) // cnt[d] it += len(s)*b nx = pos[d][pre[it%len(s)][d]+r-1] it += nx+1-it%len(s) if it > len(s)*n: return False return True ans = 0 l, r = 1, 10**14 while l <= r: mid = (l+r)>>1 if check(mid): ans = mid l = mid+1 else: r = mid-1 print(ans) n = int(input()) s = input() t = input() # 定义p(a,b,c)为f(s,n)的下标a后的出现的第b个字符c的位置 # 1.如果a>len(s),可以等价为p(a-len(s),b,c)+len(s) # 2.如果s.count(c)<b,可以等价为p(a+len(s),b-s.count(c),c) # 以上两步可以将a,b转为0<=a<len(s), 0<=b<s.count(c) cnt = [0]*26 pos = [[] for _ in range(26)] pre = [[0]*26 for _ in range(len(s)+1)] for i, c in enumerate(s*2): idx = ord(c)-ord('a') pos[idx].append(i) for i in range(26): cnt[i] = len(pos[i])//2 for i in range(1, len(s)+1): pre[i] = pre[i-1][:] pre[i][ord(s[i-1])-ord('a')] += 1 def check(m): it = 0 for i in range(len(t)): d = ord(t[i])-ord('a') if cnt[d] == 0: return False r = (m-1) % cnt[d] + 1 b = (m-r) // cnt[d] it += len(s)*b nx = pos[d][pre[it%len(s)][d]+r-1] it += nx+1-it%len(s) if it > len(s)*n: return False return True ans = 0 l, r = 1, 10**18 while l <= r: mid = (l+r)>>1 if check(mid): ans = mid l = mid+1 else: r = mid-1 print(ans)
ConDefects/ConDefects/Code/abc346_f/Python/53131253
condefects-python_data_1280
import bisect N=int(input()) eng="abcdefghijklmnopqrstuvwxyz" engd=dict() for i in range(26) : engd[eng[i]]=i s=input() S=[[]for _ in range(26)] for i in range(len(s)) : S[engd[s[i]]].append(i) t=input() T=[] for i in t : T.append(engd[i]) for i in T: if len(S[i])==0 : exit(print(0)) def can(n): n1,n2=0,-1 for i in T: p=bisect.bisect_left(S[i],n2) p+=n-1 n1+=p//len(S[i]) n2=S[i][p%len(S[i])] return n1<N left=-1 right=2**60 while left!=right: temp=(left+right+1)//2 if can(temp): left=temp else: right=temp-1 print(max(0,left)) import bisect N=int(input()) eng="abcdefghijklmnopqrstuvwxyz" engd=dict() for i in range(26) : engd[eng[i]]=i s=input() S=[[]for _ in range(26)] for i in range(len(s)) : S[engd[s[i]]].append(i) t=input() T=[] for i in t : T.append(engd[i]) for i in T: if len(S[i])==0 : exit(print(0)) def can(n): n1,n2=0,-1 for i in T: p=bisect.bisect_right(S[i],n2) p+=n-1 n1+=p//len(S[i]) n2=S[i][p%len(S[i])] return n1<N left=-1 right=2**60 while left!=right: temp=(left+right+1)//2 if can(temp): left=temp else: right=temp-1 print(max(0,left))
ConDefects/ConDefects/Code/abc346_f/Python/53725760
condefects-python_data_1281
import heapq inf = float("inf") N, K = map(int, input().split()) A = [0] B = [] T = [] for _ in range(N): t, y = map(int, input().split()) if t == 1: A.append(y) B.append(T) T = [] else: T.append(y) B.append(T) ans = -float("inf") hq = [] S = 0 sute = [] while A: a = A.pop() X = B.pop() for x in X: S += x heapq.heappush(hq, x) while len(sute) < K + 100 and hq and hq[0] < 0: t = heapq.heappop(hq) S -= t heapq.heappush(sute, -t) while sute and hq and -sute[0] > hq[0]: a = -heapq.heappop(sute) b = heapq.heappop(hq) S += a - b heapq.heappush(hq, a) heapq.heappush(sute, -b) while len(sute) > K: t = -heapq.heappop(sute) S += t heapq.heappush(hq, t) ans = max(ans, a + S) K -= 1 if K < 0: break print(ans) import heapq inf = float("inf") N, K = map(int, input().split()) A = [0] B = [] T = [] for _ in range(N): t, y = map(int, input().split()) if t == 1: A.append(y) B.append(T) T = [] else: T.append(y) B.append(T) ans = -float("inf") hq = [] S = 0 sute = [] while A: a = A.pop() X = B.pop() for x in X: S += x heapq.heappush(hq, x) while len(sute) < K + 1000 and hq and hq[0] < 0: t = heapq.heappop(hq) S -= t heapq.heappush(sute, -t) while sute and hq and -sute[0] > hq[0]: a = -heapq.heappop(sute) b = heapq.heappop(hq) S += a - b heapq.heappush(hq, a) heapq.heappush(sute, -b) while len(sute) > K: t = -heapq.heappop(sute) S += t heapq.heappush(hq, t) ans = max(ans, a + S) K -= 1 if K < 0: break print(ans)
ConDefects/ConDefects/Code/abc249_f/Python/45465070
condefects-python_data_1282
import bisect import collections import copy import heapq import itertools import math import string import sys def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) n = I() A = LI() m = I() B = set(LI()) x = I() dp = [False for _ in range(x+1)] dp[0] = True for now in range(x+1): if now and (not now in B): for a in A: if now + a < x+1: dp[now+a] = True ans = "Yes" if dp[x] else "No" print(ans) import bisect import collections import copy import heapq import itertools import math import string import sys def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) n = I() A = LI() m = I() B = set(LI()) x = I() dp = [False for _ in range(x+1)] dp[0] = True for now in range(x+1): if dp[now] and (not now in B): for a in A: if now + a < x+1: dp[now+a] = True ans = "Yes" if dp[x] else "No" print(ans)
ConDefects/ConDefects/Code/abc289_d/Python/44672786
condefects-python_data_1283
INT = lambda : int(input()) MI = lambda : map(int, input().split()) MI_DEC = lambda : map(lambda x : int(x)-1, input().split()) LI = lambda : list(map(int, input().split())) LI_DEC = lambda : list(map(lambda x : int(x)-1, input().split())) INF = float('inf') N = INT() A = LI() M = INT() B = LI() X = INT() dp = [False] * (X+1) dp[0] = True cantMove = [False] * (X+1) for i in range(M): cantMove[B[i]] = True for i in range(X): if cantMove[i]: continue for a in A: if i + a <= X: dp[i+a] = True print('Yes' if dp[X] else 'No') INT = lambda : int(input()) MI = lambda : map(int, input().split()) MI_DEC = lambda : map(lambda x : int(x)-1, input().split()) LI = lambda : list(map(int, input().split())) LI_DEC = lambda : list(map(lambda x : int(x)-1, input().split())) INF = float('inf') N = INT() A = LI() M = INT() B = LI() X = INT() dp = [False] * (X+1) dp[0] = True cantMove = [False] * (X+1) for i in range(M): cantMove[B[i]] = True for i in range(X): if cantMove[i] or not dp[i]: continue for a in A: if i + a <= X: dp[i+a] = True print('Yes' if dp[X] else 'No')
ConDefects/ConDefects/Code/abc289_d/Python/46033056
condefects-python_data_1284
N=int(input()) A=list(map(int,input().split())) x=sum(A)//N r=sum(A)%N SUM=[0] for a in A: SUM.append(SUM[-1]+a) DP=[1<<100]*(r+1) # x+1にしたものの個数 DP[0]=0 for i in range(N-1): NDP=[1<<100]*(r+1) for j in range(r+1): if DP[j]==1<<100: continue # index iまでの総和は S=(x+1)*j # x+1がj個 S+=x*(i-j) # xがi-j個 rest=S-SUM[i] # 差分。Sの方がrestだけ多いということは now=A[i]-rest # A[i]はrestだけ小さい # xにするとき NDP[j]=min(NDP[j],DP[j]++abs(now-x)) # x+1にするとき if j+1<r+1: NDP[j+1]=min(NDP[j+1],DP[j]+abs(now-(x+1))) DP=NDP print(DP[r]) N=int(input()) A=list(map(int,input().split())) x=sum(A)//N r=sum(A)%N SUM=[0] for a in A: SUM.append(SUM[-1]+a) DP=[1<<100]*(r+1) # x+1にしたものの個数 DP[0]=0 for i in range(N-1): NDP=[1<<100]*(r+1) for j in range(r+1): if DP[j]==1<<100: continue # index iまでの総和は S=(x+1)*j # x+1がj個 S+=x*(i-j) # xがi-j個 rest=S-SUM[i] # 差分。Sの方がrestだけ多いということは now=A[i]-rest # A[i]はrestだけ小さい # xにするとき NDP[j]=min(NDP[j],DP[j]++abs(now-x)) # x+1にするとき if j+1<r+1: NDP[j+1]=min(NDP[j+1],DP[j]+abs(now-(x+1))) DP=NDP print(min(DP[r-1],DP[r]))
ConDefects/ConDefects/Code/abc307_g/Python/42990129
condefects-python_data_1285
n = int(input()) a = list(map(int,input().split())) pre = a[::1] for i in range(1,n): pre[i] += pre[i-1] sm = sum(a) x = sm//n y = (sm+n-1)//n dp = [[10**18]*(n+1) for i in range(n+1)] dp[0][0] = 0 for i in range(1,n): now = pre[i-1] for j in range(i+1): dif = abs(now - (x*j + y*(i-j))) dp[i][j] = min(dp[i-1][j-1], dp[i-1][j]) + dif ans = 10**15 for i in range(n+1): if i*x + y*(n-i-1) == sm-y or i*x + y*(n-i-1) == sm-x: ans = min(ans, dp[n-1][i]) print(ans) n = int(input()) a = list(map(int,input().split())) pre = a[::1] for i in range(1,n): pre[i] += pre[i-1] sm = sum(a) x = sm//n y = (sm+n-1)//n dp = [[10**18]*(n+1) for i in range(n+1)] dp[0][0] = 0 for i in range(1,n): now = pre[i-1] for j in range(i+1): dif = abs(now - (x*j + y*(i-j))) dp[i][j] = min(dp[i-1][j-1], dp[i-1][j]) + dif ans = 10**18 for i in range(n+1): if i*x + y*(n-i-1) == sm-y or i*x + y*(n-i-1) == sm-x: ans = min(ans, dp[n-1][i]) print(ans)
ConDefects/ConDefects/Code/abc307_g/Python/42981473
condefects-python_data_1286
inf = 10 ** 18 n = int(input()) a = list(map(int,input().split())) dp = [[inf] * (n + 1) for _ in range(n + 1)] dp[0][0] = 0 asumm = 0 s = sum(a) one = s // n for i in range(1, n + 1): asumm += a[i - 1] for j in range(n): if i == j == 0: continue dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j] + abs(asumm - i * one - j)) print(dp[n][s % n]) inf = 10 ** 18 n = int(input()) a = list(map(int,input().split())) dp = [[inf] * (n + 1) for _ in range(n + 1)] dp[0][0] = 0 asumm = 0 s = sum(a) one = s // n for i in range(1, n + 1): asumm += a[i - 1] for j in range(n): if i == j == 0: continue dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j]) + abs(asumm - i * one - j) print(dp[n][s % n])
ConDefects/ConDefects/Code/abc307_g/Python/42993063
condefects-python_data_1287
N = int(input()) A = list(map(int,input().split())) x = sum(A)//N for i in range(N): A[i] -= x S = [A[0]] for i in range(1,N): S.append(S[-1] + A[i]) r = S[-1] dp = [[10**18 for j in range(r+1)] for i in range(N+1)] dp[0][0] = 0 for i in range(N): s = S[i] for rr in range(r+1): dp[i+1][rr] = min(dp[i][rr],dp[i][max(0,rr-1)]) + abs(rr - s) print(min(dp[N])) N = int(input()) A = list(map(int,input().split())) x = sum(A)//N for i in range(N): A[i] -= x S = [A[0]] for i in range(1,N): S.append(S[-1] + A[i]) r = S[-1] dp = [[10**18 for j in range(r+1)] for i in range(N+1)] dp[0][0] = 0 for i in range(N): s = S[i] for rr in range(r+1): dp[i+1][rr] = min(dp[i][rr],dp[i][max(0,rr-1)]) + abs(rr - s) print(dp[N][r])
ConDefects/ConDefects/Code/abc307_g/Python/50088048
condefects-python_data_1288
N = int(input()) A = list(map(int,input().split())) sA = sum(A) one = sA%N zero = N-one for i in range(N): A[i]-=sA//N #one個の1、残り全部0 A_sum = [0] for i in range(N): A_sum.append(A_sum[-1]+A[i]) last = [10**16 for i in range(N)] last[0]=0 for i in range(N): for j in reversed(range(N)): #最後がjの場合 if j==0: last[j]=last[0]+abs(A_sum[i]-j) else: last[j] = min(last[j-1],last[j])+abs(A_sum[i]-j) print(last[one]) N = int(input()) A = list(map(int,input().split())) sA = sum(A) one = sA%N zero = N-one for i in range(N): A[i]-=sA//N #one個の1、残り全部0 A_sum = [0] for i in range(N): A_sum.append(A_sum[-1]+A[i]) last = [10**16 for i in range(N)] last[0]=0 for i in range(1,1+N): for j in reversed(range(N)): #最後がjの場合 if j==0: last[j]=last[0]+abs(A_sum[i]-j) else: last[j] = min(last[j-1],last[j])+abs(A_sum[i]-j) print(last[one])
ConDefects/ConDefects/Code/abc307_g/Python/42982093
condefects-python_data_1289
## https://atcoder.jp/contests/abc307/tasks/abc307_g def main(): N = int(input()) A = list(map(int, input().split())) sum_a = sum(A) if sum_a % N == 0: border_a = sum_a // N answer = 0 for i in range(N - 1): b = border_a - A[i] A[i + 1] -= b answer += abs(b) print(answer) return else: mod = sum_a % N low_border_a = sum_a // N high_border_a = low_border_a + 1 dp = [[None for _ in range(mod + 1)] for _ in range(N)] dp[0][0] = 0 mins = [None for _ in range(mod + 1)] mins[0] = A[0] for i in range(N - 1): new_mins = [None for _ in range(mod + 1)] for m in range(mod + 1): if dp[i][m] is None: continue # low_border_aに合わせる b = low_border_a - mins[m] if dp[i + 1][m] is None: dp[i + 1][m] = float("inf") if dp[i + 1][m] > dp[i][m] + abs(b): dp[i + 1][m] = min(dp[i + 1][m], dp[i][m] + abs(b)) new_mins[m] = A[i + 1] - b # high_border_aに合わせる if m + 1 <= mod: if dp[i + 1][m + 1] is None: dp[i + 1][m + 1] = float("inf") b = high_border_a - mins[m] if dp[i + 1][m + 1] > dp[i][m] + abs(b): dp[i + 1][m + 1] = min(dp[i + 1][m + 1], dp[i][m] + abs(b)) new_mins[m + 1] = A[i + 1] - b mins = new_mins answer = dp[N - 1][mod] print(answer) if __name__ == "__main__": main() ## https://atcoder.jp/contests/abc307/tasks/abc307_g def main(): N = int(input()) A = list(map(int, input().split())) sum_a = sum(A) if sum_a % N == 0: border_a = sum_a // N answer = 0 for i in range(N - 1): b = border_a - A[i] A[i + 1] -= b answer += abs(b) print(answer) return else: mod = sum_a % N low_border_a = sum_a // N high_border_a = low_border_a + 1 dp = [[None for _ in range(mod + 1)] for _ in range(N)] dp[0][0] = 0 mins = [None for _ in range(mod + 1)] mins[0] = A[0] for i in range(N - 1): new_mins = [None for _ in range(mod + 1)] for m in range(mod + 1): if dp[i][m] is None: continue # low_border_aに合わせる b = low_border_a - mins[m] if dp[i + 1][m] is None: dp[i + 1][m] = float("inf") if dp[i + 1][m] > dp[i][m] + abs(b): dp[i + 1][m] = min(dp[i + 1][m], dp[i][m] + abs(b)) new_mins[m] = A[i + 1] - b # high_border_aに合わせる if m + 1 <= mod: if dp[i + 1][m + 1] is None: dp[i + 1][m + 1] = float("inf") b = high_border_a - mins[m] if dp[i + 1][m + 1] > dp[i][m] + abs(b): dp[i + 1][m + 1] = min(dp[i + 1][m + 1], dp[i][m] + abs(b)) new_mins[m + 1] = A[i + 1] - b mins = new_mins answer = min(dp[N - 1][mod], dp[N - 1][mod - 1]) print(answer) if __name__ == "__main__": main()
ConDefects/ConDefects/Code/abc307_g/Python/51711769
condefects-python_data_1290
n = int(input()) aaa = list(map(int, input().split())) x, y = divmod(sum(aaa), n) INF = 1 << 60 dp = [INF] * (y + 1) dp[0] = 0 tmp_sum = 0 for i in range(n): ndp = [INF] * (y + 1) diff = tmp_sum - i * x a = aaa[i] for j in range(min(i, y) + 1): d = diff - j # i+1 への調整残し x0 = x - (a + d) ndp[j] = min(ndp[j], dp[j] + abs(x0)) if j < y: ndp[j + 1] = min(ndp[j], dp[j] + abs(x0 + 1)) tmp_sum += aaa[i] dp = ndp print(dp[y]) n = int(input()) aaa = list(map(int, input().split())) x, y = divmod(sum(aaa), n) INF = 1 << 60 dp = [INF] * (y + 1) dp[0] = 0 tmp_sum = 0 for i in range(n): ndp = [INF] * (y + 1) diff = tmp_sum - i * x a = aaa[i] for j in range(min(i, y) + 1): d = diff - j # i+1 への調整残し x0 = x - (a + d) ndp[j] = min(ndp[j], dp[j] + abs(x0)) if j < y: ndp[j + 1] = min(ndp[j + 1], dp[j] + abs(x0 + 1)) tmp_sum += aaa[i] dp = ndp print(dp[y])
ConDefects/ConDefects/Code/abc307_g/Python/42999980
condefects-python_data_1291
from collections import defaultdict N,M,H,K = map(int,input().split()) S = list(input()) item = defaultdict(int) for i in range(M): x,y = map(int,input().split()) item[(x,y)] = 1 now = [0,0] for i in range(N): move = S[i] if move == "R": now[0] += 1 elif move == "L": now[0] -= 1 elif move == "U": now[1] += 1 elif move == "D": now[1] -= 1 H -= 1 if H < 0: print("No") break if item[(now[0],now[1])] == 1 and H < K: H = K item[(now[0],now[1])] == 0 else: print("Yes") from collections import defaultdict N,M,H,K = map(int,input().split()) S = list(input()) item = defaultdict(int) for i in range(M): x,y = map(int,input().split()) item[(x,y)] = 1 now = [0,0] for i in range(N): move = S[i] if move == "R": now[0] += 1 elif move == "L": now[0] -= 1 elif move == "U": now[1] += 1 elif move == "D": now[1] -= 1 H -= 1 if H < 0: print("No") break if item[(now[0],now[1])] == 1 and H < K: H = K item[(now[0],now[1])] = 0 else: print("Yes")
ConDefects/ConDefects/Code/abc303_c/Python/45491344
condefects-python_data_1292
N, M, H, K = map(int, input().split()) S = input() items = set(tuple(map(int, input().split())) for _ in range(M)) print(items) # シミュレーションする res = True x, y = 0, 0 for c in S: # 移動する if c == 'R': x += 1 elif c == 'L': x -= 1 elif c == 'U': y += 1 else: y -= 1 # 体力を 1 減らす (0 未満になったら倒れる) H -= 1 if H < 0: res = False # アイテムがあって体力が不足していたら回復する if (x, y) in items and H < K: H = K items.remove((x, y)) # アイテムを削除する print("Yes" if res else "No") N, M, H, K = map(int, input().split()) S = input() items = set(tuple(map(int, input().split())) for _ in range(M)) #print(items) # シミュレーションする res = True x, y = 0, 0 for c in S: # 移動する if c == 'R': x += 1 elif c == 'L': x -= 1 elif c == 'U': y += 1 else: y -= 1 # 体力を 1 減らす (0 未満になったら倒れる) H -= 1 if H < 0: res = False # アイテムがあって体力が不足していたら回復する if (x, y) in items and H < K: H = K items.remove((x, y)) # アイテムを削除する print("Yes" if res else "No")
ConDefects/ConDefects/Code/abc303_c/Python/45578518
condefects-python_data_1293
# import math # import sys # sys.setrecursionlimit(10**9) # from itertools import permutations # from itertools import combinations # from functools import lru_cache # import heapq # DIV = 10**9+7 #data(yyyy-mm-dd): #recursion使う時はCpythonで出す.それ以外は基本Pypy def main(): n,m,hit_point,k = map(int,input().split(" ")) s = list(input()) item = dict() for i in range(m): x,y = map(int,input().split(" ")) item[(x,y)] = m # print(item) flag = True x = 0 y = 0 for i in range(n): # print(i,x,y,hit_point,item[x][y]) #移動経路 char = s[i] if char == "R": x += 1 elif char == "L": x -= 1 elif char == "U": y += 1 elif char == "D": y -= 1 #移動する hit_point -= 1 if hit_point < 0: print("No") return 1 if hit_point < k and (x,y) in item: # print("recoverd") if item[(x,y)] > 0: hit_point = k item[(x,y)] -= 1 print("Yes") return 1 if __name__ == "__main__": main() # import math # import sys # sys.setrecursionlimit(10**9) # from itertools import permutations # from itertools import combinations # from functools import lru_cache # import heapq # DIV = 10**9+7 #data(yyyy-mm-dd): #recursion使う時はCpythonで出す.それ以外は基本Pypy def main(): n,m,hit_point,k = map(int,input().split(" ")) s = list(input()) item = dict() for i in range(m): x,y = map(int,input().split(" ")) item[(x,y)] = 1 # print(item) flag = True x = 0 y = 0 for i in range(n): # print(i,x,y,hit_point,item[x][y]) #移動経路 char = s[i] if char == "R": x += 1 elif char == "L": x -= 1 elif char == "U": y += 1 elif char == "D": y -= 1 #移動する hit_point -= 1 if hit_point < 0: print("No") return 1 if hit_point < k and (x,y) in item: # print("recoverd") if item[(x,y)] > 0: hit_point = k item[(x,y)] -= 1 print("Yes") return 1 if __name__ == "__main__": main()
ConDefects/ConDefects/Code/abc303_c/Python/46045444
condefects-python_data_1294
N, M, H, K = map(int, input().split()) S = input() RLUD = {'R': (1, 0), 'L': (-1, 0), 'U': (0, 1), 'D': (0, -1)} healing = set() for _ in range(M): x, y = map(int, input().split()) healing.add((x, y)) x, y = 0, 0 for s in S[:-1]: H -= 1 if H < 0: print('No') exit() x += RLUD[s][0] y += RLUD[s][1] if (x, y) not in healing: continue if H < K: healing.remove((x, y)) H = K print('Yes') N, M, H, K = map(int, input().split()) S = input() RLUD = {'R': (1, 0), 'L': (-1, 0), 'U': (0, 1), 'D': (0, -1)} healing = set() for _ in range(M): x, y = map(int, input().split()) healing.add((x, y)) x, y = 0, 0 for s in S: H -= 1 if H < 0: print('No') exit() x += RLUD[s][0] y += RLUD[s][1] if (x, y) not in healing: continue if H < K: healing.remove((x, y)) H = K print('Yes')
ConDefects/ConDefects/Code/abc303_c/Python/46147196
condefects-python_data_1295
N, Q = map(int, input().split()) X = [] S = {0, N} for _ in range(Q): l, r = map(int, input().split()) l -= 1 S.add(l) S.add(r) X.append((l, r)) SS = sorted(S) D = {a: i for i, a in enumerate(SS)} M = len(SS) X = [(D[l], D[r]) for l, r in X] d = 0 s = 1 while s < M: s *= 2 d += 1 L = [0] * (M + 1) R = [0] * (M + 1) I = [0] * (M + 1) for l, r in X: if l + 1 == r: I[l] += 1 else: L[l] += 1 R[r] += 1 inf = 10 ** 6 Y = [0] for i in range(M - 1): ni = i + 1 nY = [inf] * (i * 2 + 5) for j in range(len(Y)): for dj in ((1, ) if j % 2 else (1, 2)): nj = j + dj if nj >= len(nY): continue if dj == 2: nY[nj] = min(nY[nj], Y[j]) else: if nj % 2: c = I[i] + R[ni] else: c = I[i] + L[i] nY[nj] = min(nY[nj], Y[j] + c) Y = nY print(d, Y[s] * 2 if d else Q) N, Q = map(int, input().split()) X = [] S = {0, N} for _ in range(Q): l, r = map(int, input().split()) l -= 1 S.add(l) S.add(r) X.append((l, r)) SS = sorted(S) D = {a: i for i, a in enumerate(SS)} M = len(SS) X = [(D[l], D[r]) for l, r in X] d = 0 s = 1 while s < M - 1: s *= 2 d += 1 L = [0] * (M + 1) R = [0] * (M + 1) I = [0] * (M + 1) for l, r in X: if l + 1 == r: I[l] += 1 else: L[l] += 1 R[r] += 1 inf = 10 ** 6 Y = [0] for i in range(M - 1): ni = i + 1 nY = [inf] * (i * 2 + 5) for j in range(len(Y)): for dj in ((1, ) if j % 2 else (1, 2)): nj = j + dj if nj >= len(nY): continue if dj == 2: nY[nj] = min(nY[nj], Y[j]) else: if nj % 2: c = I[i] + R[ni] else: c = I[i] + L[i] nY[nj] = min(nY[nj], Y[j] + c) Y = nY print(d, Y[s] * 2 if d else Q)
ConDefects/ConDefects/Code/arc164_e/Python/43438224
condefects-python_data_1296
import sys input = sys.stdin.readline n, q = map(int, input().split()) skips = [0] * (n + 1) for _ in range(q): l, r = map(int, input().split()) skips[l - 1] += 1 skips[r] += 1 skips.pop() skips.pop(0) comp = [v for v in skips if v] #comp.sort() sz = len(comp) + 1 o = 0 while pow(2, o) < sz: o += 1 if o == 0: print(0, 1) sys.exit() take = sz - pow(2, o - 1) INF = 10 ** 6 curr = [INF] * (take + 1) curr[0] = 0 care = curr[:] for v in comp: nex = [INF] for i in range(take): nex.append(curr[i] + v) for i in range(take + 1): curr[i] = min(curr[i], care[i]) care = nex print(o, 2 * min(care[-1], curr[-1])) import sys input = sys.stdin.readline n, q = map(int, input().split()) skips = [0] * (n + 1) for _ in range(q): l, r = map(int, input().split()) skips[l - 1] += 1 skips[r] += 1 skips.pop() skips.pop(0) comp = [v for v in skips if v] #comp.sort() sz = len(comp) + 1 o = 0 while pow(2, o) < sz: o += 1 if o == 0: print(0, q) sys.exit() take = sz - pow(2, o - 1) INF = 10 ** 6 curr = [INF] * (take + 1) curr[0] = 0 care = curr[:] for v in comp: nex = [INF] for i in range(take): nex.append(curr[i] + v) for i in range(take + 1): curr[i] = min(curr[i], care[i]) care = nex print(o, 2 * min(care[-1], curr[-1]))
ConDefects/ConDefects/Code/arc164_e/Python/43525175
condefects-python_data_1297
import sys readline = sys.stdin.readline #n = int(readline()) #*a, = map(int,readline().split()) # b = [list(map(int,readline().split())) for _ in range()] n,Q = map(int,readline().split()) LR = [] s = {0,n} for _ in range(Q): a,b = map(int,readline().split()) a -= 1 LR.append((a,b)) s.add(a) s.add(b) s = sorted(s) x = len(s)-1 d = 0 v = 1 while v < x: v *= 2 d += 1 C = x - 2**(d-1) if x == 1: print(Q) exit() """ x 個の区間の中から C ペア選んで、それぞれに含まれる区間の個数の最小値 """ imos = [0]*(n+1) """ for L,R in LR: imos[L] += 1 imos[R] -= 1 for i in range(1,n): imos[i] += imos[i-1] """ for L,R in LR: imos[L] += 1 imos[R] += 1 #print(LR) #print(s) #print(imos) #print(x,C,[imos[i] for i in s]) INF = 1<<60 dp = [[INF]*(C+1) for _ in range(x+1)] dp[0][0] = 0 for i in range(x): for j in range(C+1): if i+2 <= x and j+1 <= C: dp[i+2][j+1] = min(dp[i+2][j+1], dp[i][j] + imos[s[i+1]]) dp[i+1][j] = min(dp[i+1][j],dp[i][j]) #for i in dp: # print(i) print(d,dp[x][C]*2) import sys readline = sys.stdin.readline #n = int(readline()) #*a, = map(int,readline().split()) # b = [list(map(int,readline().split())) for _ in range()] n,Q = map(int,readline().split()) LR = [] s = {0,n} for _ in range(Q): a,b = map(int,readline().split()) a -= 1 LR.append((a,b)) s.add(a) s.add(b) s = sorted(s) x = len(s)-1 d = 0 v = 1 while v < x: v *= 2 d += 1 C = x - 2**(d-1) if x == 1: print(0,Q) exit() """ x 個の区間の中から C ペア選んで、それぞれに含まれる区間の個数の最小値 """ imos = [0]*(n+1) """ for L,R in LR: imos[L] += 1 imos[R] -= 1 for i in range(1,n): imos[i] += imos[i-1] """ for L,R in LR: imos[L] += 1 imos[R] += 1 #print(LR) #print(s) #print(imos) #print(x,C,[imos[i] for i in s]) INF = 1<<60 dp = [[INF]*(C+1) for _ in range(x+1)] dp[0][0] = 0 for i in range(x): for j in range(C+1): if i+2 <= x and j+1 <= C: dp[i+2][j+1] = min(dp[i+2][j+1], dp[i][j] + imos[s[i+1]]) dp[i+1][j] = min(dp[i+1][j],dp[i][j]) #for i in dp: # print(i) print(d,dp[x][C]*2)
ConDefects/ConDefects/Code/arc164_e/Python/43433582
condefects-python_data_1298
import sys input = sys.stdin.readline from operator import itemgetter N=int(input()) P=[list(map(int,input().split())) for i in range(N)] hosei=10**30 ANS=float("inf") Q=[] for a,b,c in P: a0=hosei*a//c b0=hosei*b//c Q.append((a0,b0)) if a0>b0: ANS=min(ANS,a0) else: ANS=min(ANS,b0) P=list(set(Q)) P.sort(key=itemgetter(1)) # 一番左下の点から始める。 P.sort(key=itemgetter(0)) Q1=[] Q2=[] def outer_product(x,y,z,w): return x*w-y*z for x,y in P: while True: if len(Q1)<2: break s,t=Q1[-1] u,v=Q1[-2] if outer_product(u-s,v-t,x-u,y-v)<0: Q1.pop() else: break Q1.append((x,y)) while True: if len(Q2)<2: break s,t=Q2[-1] u,v=Q2[-2] if outer_product(u-s,v-t,x-u,y-v)>0: Q2.pop() else: break Q2.append((x,y)) Q2.reverse() Q=Q1+Q2[1:] for i in range(len(Q)): a,b=Q[i] c,d=Q[i-1] if (a-c)**2+(b-d)**2<1000: continue if a==c: ANS=min(ANS,a) continue if b==d: ANS=min(ANS,b) continue if ((a-c)*(0-b) - (b-d)*(0-a))*((a-c)*(10**100-b) - (b-d)*(10**100-a))<0: ANS=min(ANS,(a*(b-d)/(a-c)-b)/((b-d)/(a-c)-1)) print(1/ANS*(hosei)) import sys input = sys.stdin.readline from operator import itemgetter N=int(input()) P=[list(map(int,input().split())) for i in range(N)] hosei=10**30 ANS=float("inf") Q=[] for a,b,c in P: a0=hosei*a//c b0=hosei*b//c Q.append((a0,b0)) if a0>b0: ANS=min(ANS,a0) else: ANS=min(ANS,b0) P=list(set(Q)) P.sort(key=itemgetter(1)) # 一番左下の点から始める。 P.sort(key=itemgetter(0)) Q1=[] Q2=[] def outer_product(x,y,z,w): return x*w-y*z for x,y in P: while True: if len(Q1)<2: break s,t=Q1[-1] u,v=Q1[-2] if outer_product(u-s,v-t,x-u,y-v)<0: Q1.pop() else: break Q1.append((x,y)) while True: if len(Q2)<2: break s,t=Q2[-1] u,v=Q2[-2] if outer_product(u-s,v-t,x-u,y-v)>0: Q2.pop() else: break Q2.append((x,y)) Q2.reverse() Q=Q1+Q2[1:] for i in range(len(Q)): a,b=Q[i] c,d=Q[i-1] if (a-c)**2+(b-d)**2<1000: continue if a==c: ANS=min(ANS,a) continue if b==d: ANS=min(ANS,b) continue if (a<b and c>d) or (a>b and c<d): ANS=min(ANS,(a*(b-d)/(a-c)-b)/((b-d)/(a-c)-1)) print(1/ANS*(hosei))
ConDefects/ConDefects/Code/abc275_g/Python/36145190
condefects-python_data_1299
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### ok = 10 ng = 0 n = ni() a,b,c = zip(*[na() for i in range(n)]) eps = 10**(-10) while ok-ng>10**(-8): X = (ok+ng)/2 s = 1 l = 0 r = X for i in range(n): if a[i] == b[i]: if c[i] * a[i] > 0: s = 0 break elif b[i] - a[i] > 0: l = max(l, (c[i]-a[i]*X)/(b[i]-a[i])) else: r = min(r, (c[i]-a[i]*X)/(b[i]-a[i])) if l <= r+eps and s: ok = X else: ng = X print(ok) import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### ok = 10 ng = 0 n = ni() a,b,c = zip(*[na() for i in range(n)]) eps = 10**(-10) while ok-ng>10**(-8): X = (ok+ng)/2 s = 1 l = 0 r = X for i in range(n): if a[i] == b[i]: if c[i] - a[i]*X > 0: s = 0 break elif b[i] - a[i] > 0: l = max(l, (c[i]-a[i]*X)/(b[i]-a[i])) else: r = min(r, (c[i]-a[i]*X)/(b[i]-a[i])) if l <= r+eps and s: ok = X else: ng = X print(ok)
ConDefects/ConDefects/Code/abc275_g/Python/37220565
condefects-python_data_1300
pin = [0] * 7 s = input() p = [[6],[3],[6,1],[4,0],[8,2],[5],[9]] for i in range(len(p)): cnt = 0 for j in range(len(p[i])): if s[p[i][j]] == "1": cnt += 1 if cnt: pin[i] = 1 b = 0 ok = 0 e = 0 for i in range(7): if pin[i] and b == 0: b = 1 if b and pin[i] == 0: ok = 1 if b and ok and pin[i]: e = 1 print("Yes" if e and s[0] == "0" else "No") pin = [0] * 7 s = input() p = [[6],[3],[7,1],[4,0],[8,2],[5],[9]] for i in range(len(p)): cnt = 0 for j in range(len(p[i])): if s[p[i][j]] == "1": cnt += 1 if cnt: pin[i] = 1 b = 0 ok = 0 e = 0 for i in range(7): if pin[i] and b == 0: b = 1 if b and pin[i] == 0: ok = 1 if b and ok and pin[i]: e = 1 print("Yes" if e and s[0] == "0" else "No")
ConDefects/ConDefects/Code/abc267_b/Python/45810314