text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` N, C = map(int, input().split()) xv = [[int(i) for i in input().split()] for _ in range(N)] av = 0 fa1 = 0 bv = 0 fb1 = 0 fa = [] fb = [] ga = [] gb = [] for i in range(N): ax = xv[i][0] av += xv[i][1] bx = C - xv[-(i+1)][0] bv += xv[-(i+1)][1] fa1 = max(fa1, av-ax) fa.append(fa1) ga.append(av - 2*ax) fb1 = max(fb1, bv-bx) fb.append(fb1) gb.append(bv - 2*bx) p = max(0, fa[-1], fb[-1]) for i in range(N-1): p = max(p, fa[i]+gb[N-i-2], ga[i]+fb[N-i-2]) print(p) ```
105,100
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` n, c = map(int, input().split()) xv = [list(map(int, input().split())) for i in range(n)] dx = [] dy = [] for i in range(n): if i == 0: dx.append(xv[i][0]) dy.append(c - xv[-i-1][0]) else: dx.append(xv[i][0]-xv[i-1][0]) dy.append(-xv[-i-1][0] + xv[-i][0]) right = [0] left = [0] for i in range(n): r = right[-1] + xv[i][1] - dx[i] l = left[-1] + xv[-i-1][1] - dy[i] right.append(r) left.append(l) for i in range(1,n+1): right[i] = max(right[i-1], right[i]) left[i] = max(left[i-1], left[i]) ans = 0 for i in range(1,n+1): a1 = right[i] a2 = right[i] - xv[i-1][0] + left[n-i] a3 = left[i] a4 = left[i] - (c - xv[-i][0]) + right[n-i] ans = max(ans,a1,a2,a3,a4) print(ans) ```
105,101
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` import random def slv(N, C, X, V): ans_l = [(0, 0)] ans_l_ = [0] for x, v in zip(X, V): ans_l_.append(max(ans_l_[-1], ans_l[-1][1] + v - x)) ans_l.append((x, ans_l[-1][1] + v)) ans_l_.pop(0) ans_l.pop(0) ans_r = [(0, 0)] ans_r_ = [0] for x, v in list(zip(X, V))[::-1]: ans_r_.append(max(ans_r_[-1], ans_r[-1][1] + v - (C-x))) ans_r.append((C-x, ans_r[-1][1] + v)) ans_r_.pop(0) ans_r.pop(0) # print('p1') ans_l_r = [] for i, a in enumerate(ans_l[:-1], 1): # print(ans_r_[N-i-1], a[1]-2*a[0]) ans_l_r.append(ans_r_[N-i-1] + a[1]-2*a[0]) # print('p2') ans_r_l = [] for i, a in enumerate(ans_r[:-1], 1): # print(ans_l_[N-i-1], a[1]-2*a[0]) ans_r_l.append(ans_l_[N-i-1] + a[1]-2*a[0]) l = max([x[1]-x[0] for x in ans_l]) r = max([x[1]-x[0] for x in ans_r]) lr = 0 if not ans_l_r else max(ans_l_r) rl = 0 if not ans_r_l else max(ans_r_l) # print(ans_l) # print(ans_l_) # print(ans_r) # print(ans_r_) # print(l, r, lr, rl) return max(l, r, lr, rl) def main(): N, C = list(map(int, input().split())) X = [] V = [] for _ in range(N): x, v = list(map(int, input().split())) X.append(x) V.append(v) # N = 100000 # C = 1000000000000 # X = [1] # V = [100] # for _ in range(N): # X.append(X[-1]+random.randint(1, 10000)) # V.append(random.randint(1, 10000)) print(slv(N, C, X, V)) if __name__ == '__main__': main() ```
105,102
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` n,c=map(int,input().split()) rl=[[0]*2 for _ in range(n+1)] ls,rs=0,0 xv=[[0,0]]+[list(map(int,input().split())) for _ in range(n)] for i in range(1,n+1): j,k=xv[i] rs+=k rl[i][0],rl[i][1]=max(rl[i-1][0],rs-j),max(rl[i-1][1],rs-2*j) ans=rl[n][0] for i in range(n): ls+=xv[n-i][1] ans=max(ans,rl[n-i-1][1]+ls-(c-xv[n-i][0]),rl[n-i-1][0]+ls-(c-xv[n-i][0])*2) print(ans) ```
105,103
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` N, C = [int(_) for _ in input().split()] xv = [[int(_) for _ in input().split()] for i in range(N)] def solve(N, C, xv): from itertools import accumulate xs = [0] + [_[0] for _ in xv] + [C] vs = [0] + [_[1] for _ in xv] + [0] xs_rev = [C - x for x in xs[::-1]] vs_rev = vs[::-1] ts = accumulate(vs) txs = [t - x for x, t in zip(xs, ts)] maxtxs = list(accumulate(txs, max)) ts_rev = accumulate(vs_rev) txs_rev = [t - x for x, t in zip(xs_rev, ts_rev)] maxtxs_rev = list(accumulate(txs_rev, max)) return max( max(tx - xs_rev[i] + maxtxs[N - i] for i, tx in enumerate(txs_rev[:-1])), max(tx - xs[i] + maxtxs_rev[N - i] for i, tx in enumerate(txs[:-1])) ) print(solve(N, C, xv)) ```
105,104
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` N, C = map(int, input().split()) sushi = [] for i in range(N): sushi.append(list(map(int, input().split()))) right_turn_sum = [0] for i in range(N): right_turn_sum.append(sushi[i][1]+right_turn_sum[i]) for i in range(N): right_turn_sum[i+1] -= sushi[i][0] right_turn_to_zero = [0] for i in range(N): right_turn_to_zero.append(right_turn_sum[i+1]-sushi[i][0]) left_turn_sum = [0] for i in range(N): left_turn_sum.append(sushi[-1-i][1]+left_turn_sum[i]) for i in range(N): left_turn_sum[i+1] -= (C - sushi[-1-i][0]) left_turn_to_zero = [0] for i in range(N): left_turn_to_zero.append(left_turn_sum[i+1]-(C-sushi[-1-i][0])) ans = max(right_turn_sum) ans = max(left_turn_sum+[ans]) maxi = 0 for i in range(N+1): if maxi < right_turn_sum[i]: maxi = right_turn_sum[i] right_turn_sum[i] = max(maxi, right_turn_sum[i]) maxi = 0 for i in range(N+1): if maxi < right_turn_to_zero[i]: maxi = right_turn_to_zero[i] right_turn_to_zero[i] = max(maxi, right_turn_to_zero[i]) maxi = 0 for i in range(N+1): if maxi < left_turn_sum[i]: maxi = left_turn_sum[i] left_turn_sum[i] = max(maxi, left_turn_sum[i]) maxi = 0 for i in range(N+1): if maxi < left_turn_to_zero[i]: maxi = left_turn_to_zero[i] left_turn_to_zero[i] = max(maxi, left_turn_to_zero[i]) for i in range(N+1): ans = max(ans, right_turn_to_zero[i]+left_turn_sum[N-i]) for i in range(N+1): ans = max(ans, left_turn_to_zero[i]+right_turn_sum[N-i]) print(ans) ```
105,105
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` import sys read = sys.stdin.buffer.read N,C, *XV = map(int, read().split())#全部 X=XV[::2] V=XV[1::2] m1=[0] m2=[0] s=0 ans=0 for x,v in zip(X,V): s+=v ans=max(ans,s-x) m1.append(max(m1[-1],s-x)) m2.append(max(m2[-1],s-2*x)) s=0 i=0 for x,v in reversed(list(zip(X,V))): s+=v x=C-x ans=max(ans,s-2*x+m1[-2-i],s-x+m2[-2-i]) i+=1 print(ans) ```
105,106
Provide a correct Python 3 solution for this coding contest problem. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 "Correct Solution: ``` # python template for atcoder1 import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline N, C = map(int, input().split()) sushis = [list(map(int, input().split())) for _ in range(N)] # Aを時計回り,Bを反時計回りとする # O->Aと0->A->Oのコストを計算 valA = [] valArev = [] sumV = 0 tmpMax = -float('inf') for d, v in sushis: sumV += v valArev.append(sumV-2*d) tmpMax = max(tmpMax, sumV-d) valA.append(tmpMax) valB = [] valBrev = [] sumV = 0 tmpMax = -float('inf') for d, v in reversed(sushis): sumV += v revD = C-d valBrev.append(sumV-2*revD) tmpMax = max(tmpMax, sumV-revD) valB.append(tmpMax) # 折り返しなしの場合の値 ans = max(valA+valB+[0]) # O->A->O->B for i in range(N-1): ans = max(ans, valArev[i]+valB[N-i-2]) # O->B->O->A for i in range(N-1): ans = max(ans, valBrev[i]+valA[N-i-2]) print(ans) ```
105,107
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` def solve(): N, C = map(int, input().split())# {{{ L = [0]*N R = [0]*N S = [] for n in range(N): x, v = map(int, input().split()) S.append((x,v)) res = 0 # left round pre_x = 0 diff = 0 M = 0 for n in range(N): x, v = S[n] diff -= abs(x - pre_x) diff += v M = max(diff, M) res = max(res, M) L[n] = M pre_x = x # print(L) # right round pre_x = C diff = 0 M = 0 for n in range(1, N+1): x, v = S[-n] diff -= abs(x - pre_x) diff += v M = max(diff, M) res = max(res, M) R[-n] = M pre_x = x # print(R) # }}} # left, right diff = 0 for n in range(N-1): res = max(res, L[n]-S[n][0] + R[n+1]) # right, left diff = 0 for n in range(1, N): res = max(res, R[-n]-(abs(C-S[-n][0])) + L[-n-1]) print(res) if __name__ == "__main__": solve() ``` Yes
105,108
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` n,c = map(int,input().split()) x = [0] v = [0] rf = [0] * (n+2) rg = [0] * (n+2) lf = [0] * (n+2) lg = [0] * (n+2) for i in range(0,n): X,V = map(int,input().split()) x.append(X) v.append(V) for i in range(0,n): rf[i+1] = rf[i] + v[i+1] rg[i+1] = max(rg[i],rf[i+1] - x[i+1]) for i in range(n+1,1,-1): lf[i-1] = lf[i] + v[i-1] lg[i-1] = max(lg[i],lf[i-1] - (c-x[i-1])) ans = max(rg[n],lg[1]) for i in range(1,n): ans = max(ans,rg[i]+lg[i+1] - x[i],rg[i]+lg[i+1] - (c - x[i+1])) print(ans) ``` Yes
105,109
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` N, C = map(int, input().split()) X = []; V = [] for i in range(N): x, v = map(int, input().split()) X.append(x); V.append(v) P0 = []; P1 = [] ma0 = cu0 = ma1 = cu1 = 0 pos = 0 for i in range(N): cu0 += V[i]-(X[i]-pos) cu1 += V[i]-(X[i]-pos)*2 ma0 = max(ma0, cu0) ma1 = max(ma1, cu1) P0.append(ma0) P1.append(ma1) pos = X[i] Q0 = []; Q1 = [] ma0 = cu0 = ma1 = cu1 = 0 pos = C for i in range(N): cu0 += V[-i-1]-(pos-X[-i-1]) cu1 += V[-i-1]-(pos-X[-i-1])*2 ma0 = max(ma0, cu0) ma1 = max(ma1, cu1) Q0.append(ma0) Q1.append(ma1) pos = X[-i-1] ans = max(P0[N-1], Q0[N-1]) for i in range(N-1): ans = max(ans, P0[i] + Q1[-i-2], P1[-i-2] + Q0[i]) print(ans) ``` Yes
105,110
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` import sys def input(): return sys.stdin.readline().strip() N, C = list(map(int, input().split())) sushi = [] for i in range(N): sushi.append(tuple(map(int, input().split()))) sushi_sum = [0] for i in range(N): sushi_sum.append(sushi_sum[-1] + sushi[i][1]) right_max = 0 right_list = [] left_max = 0 left_list = [] for i in range(N): if right_max < sushi_sum[i + 1] - sushi[i][0]: right_max = sushi_sum[i + 1] - sushi[i][0] right_list.append(sushi_sum[i + 1] - sushi[i][0]) if left_max < sushi_sum[-1] - sushi_sum[i] - (C - sushi[i][0]): left_max = sushi_sum[-1] - sushi_sum[i] - (C - sushi[i][0]) left_list.append(sushi_sum[-1] - sushi_sum[i] - (C - sushi[i][0])) for i in range(1, N): right_list[i] = max(right_list[i - 1], right_list[i]) for i in range(N - 2, -1, -1): left_list[i] = max(left_list[i + 1], left_list[i]) right_left_max = 0 for i in range(N - 1): right_left_max = max(right_left_max, sushi_sum[i + 1] - 2*sushi[i][0] + left_list[i + 1]) left_right_max = 0 for i in range(1, N): left_right_max = max(left_right_max, sushi_sum[-1] - sushi_sum[i] - 2*(C - sushi[i][0]) + right_list[i - 1]) print(max(right_max, left_max, right_left_max, left_right_max)) ``` Yes
105,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` n,c1 = map(int, input().split()) g = [] c = [0] d = [0] a = 0 ans=[] for i in range(n): x,v = map(int, input().split()) g.append([x,v]) a+=v c.append(a-x) d.append(a-2*x) b = 0 e = [0] f = [0] for i,j in reversed(g): b+=j e.append(b-(c1-i)) f.append(b-2*(c1-i)) for i in range(1,n+2): ans.append(max(c[:i])+f[-i]) ans.append(max(d[:i])+e[-i]) print(max(ans)) ``` No
105,112
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` from itertools import* n,c=map(int,input().split()) l1=[] l2=[] l3=[] tmp=0 for i in range(n): x,v=map(int,input().split()) l1+=[(x,v+tmp)] l2+=[c-x] l3.append(v) tmp+=v l3=list(zip(l2[::-1],accumulate(l3[::-1])))[::-1] ll=list(map(lambda x:x[1]-x[0],l3)) lr=list(map(lambda x:x[1]-x[0],l1)) #print(l3,l1) #print(ll,lr) ans=0 for i in range(n): c=max(ll[i:]+[0]) d=max(lr[:i]+[0]) if c+d>ans: ans=c+d tmp=i x=max(lr[:tmp]+[0]) y=max(ll[tmp:]+[0]) if x==0 or y==0: print(ans) else: print(ans-min(l1[lr.index(x)][0],l3[ll.index(y)][0])) #print(min(l1[lr.index(max(lr[:tmp]+[0]))][0],l3[ll.index(max(ll[tmp:]+[0]))][0])) #print(tmp) ``` No
105,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` import numpy as np N, C = map(int, input().split()) x_list = [] v_list = [] for i in range(0, N): x, v = map(int, input().split()) x_list.append(x) v_list.append(v) # 右行って左に引き返す a_list = [0] for i in range(0, len(v_list)): if i > 0: l = a_list[i] + v_list[i] - (x_list[i] - x_list[i - 1]) a_list.append(l) elif i == 0: l = v_list[i] - (x_list[i]) a_list.append(l) a_accum_max = [max(a_list[:x+1]) for x in range(0, len(a_list))] b_list = [0] for i in reversed(range(0, len(v_list))): if i == N-1: l = v_list[i] - 2 * (C - x_list[i]) b_list.append(l) elif i < N-1: l = b_list[N - i - 1] + v_list[i] - 2 * (x_list[i+1] -x_list[i]) b_list.append(l) first_list = [] for i in range(0, len(b_list)): first_list.append(a_accum_max[N-i] + b_list[i]) max_fir = max(first_list) # 左行って右に引き返す c_list = [0] for i in range(0, len(v_list)): if i > 0: l = c_list[i] + v_list[i] - 2 * (x_list[i] - x_list[i - 1]) c_list.append(l) elif i == 0: l = v_list[i] - 2 * (x_list[i]) c_list.append(l) c_accum_max = [max(c_list[:x+1]) for x in range(0, len(c_list))] d_list = [0] for i in reversed(range(0, len(v_list))): if i == N-1: l = v_list[i] - (C - x_list[i]) d_list.append(l) elif i < N-1: l = d_list[N - i - 1] + v_list[i] - (x_list[i+1] -x_list[i]) d_list.append(l) second_list = [] for i in range(0, len(d_list)): second_list.append(c_accum_max[N-i] + d_list[i]) max_sec = max(second_list) max_num = max([max_fir, max_sec, 0]) print(max_num) ``` No
105,114
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter. Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories. Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter. Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger. Constraints * 1 ≤ N ≤ 10^5 * 2 ≤ C ≤ 10^{14} * 1 ≤ x_1 < x_2 < ... < x_N < C * 1 ≤ v_i ≤ 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N C x_1 v_1 x_2 v_2 : x_N v_N Output If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c. Examples Input 3 20 2 80 9 120 16 1 Output 191 Input 3 20 2 80 9 1 16 120 Output 192 Input 1 100000000000000 50000000000000 1 Output 0 Input 15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 Output 6500000000 Submitted Solution: ``` import sys from itertools import accumulate def input(): return sys.stdin.readline().strip() sys.setrecursionlimit(20000000) MOD = 10 ** 9 + 7 INF = float("inf") def main(): N, C = map(int, input().split()) X_right = [] X_left = [] V_right = [] for i in range(N): x, v = map(int, input().split()) X_right.append(x) V_right.append(v) X_left.append(C - x) V_left = V_right[::-1] V_right_add = list(accumulate([0] + V_right)) V_left_add = list(accumulate([0] + V_left)) X_left = [0] + X_left[::-1] X_right = [0] + X_right Right = [] answer_right = 0 for i in range(N + 1): if answer_right < V_right_add[i] - X_right[i]: answer_right = V_right_add[i] - X_right[i] Right.append(answer_right) Right = Right[::-1] answer = Right[-1] for i in range(N + 1): if answer < V_left_add[i] - X_left[i] * 2 + Right[i]: answer = max( V_left_add[i] - X_left[i] * 2 + Right[i], V_left_add[i] - X_left[i] + Right[i] - X_right[N - i - 1], answer, ) print(answer) if __name__ == "__main__": main() ``` No
105,115
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` N=int(input()) from collections import defaultdict data=defaultdict(int) data[0]+=1 for d in map(int,input().split()): data[d]+=1 check=[[]] ans=1 for d in data.keys(): if data[d]>2: ans=0 break checkc=check.copy() if data[d]==2: if d==0 or d==12: ans=0 break for c in check: c+=[d,-1*d] elif data[d]==1: if d==0: for c in check: c.append(d) continue check=[] for c in checkc: check.append(c+[d]) check.append(c+[-1*d]) if ans!=0: ans=0 for c in check: c.sort() now=24 for i in range(len(c)): now=min(now,(c[i]-c[i-1])%24) ans=max(now,ans) print(ans) ```
105,116
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` #設定 import sys input = sys.stdin.buffer.readline from collections import defaultdict def getlist(): return list(map(int, input().split())) #処理内容 def main(): N = int(input()) D = getlist() if N >= 24: print(0) elif N >= 12: d = defaultdict(int) judge = "Yes" for i in D: d[i] += 1 if d[0] >= 1: judge = "No" if d[12] >= 2: judge = "No" for i in range(1, 12): if d[i] >= 3: judge = "No" if judge == "No": print(0) else: print(1) else: ans = 0 for i in range(1 << N): L = [0, 24] for j in range(N): if ((i >> j) & 1) == 1: L.append(D[j]) else: L.append(24 - D[j]) L = sorted(L) val = 24 for i in range(N + 1): val = min(val, L[i + 1] - L[i]) ans = max(ans, val) print(ans) if __name__ == '__main__': main() ```
105,117
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) import itertools N = int(input()) D = [int(x) for x in input().split()] counter = [1] + [0] * 12 for x in D: counter[x] += 1 bl = counter[0] > 1 or counter[12] > 1 or any(x >= 3 for x in counter) if bl: print(0) exit() itrs = [[[0]]] for x in range(1,12): if counter[x] == 0: itr = [[]] elif counter[x] == 1: itr = [[x],[-x]] else: itr = [[x,-x]] itrs.append(itr) itrs.append([[]] if counter[12] == 0 else [[12]]) def F(arr): f = lambda x,y: min((x-y)%24, (y-x)%24) return min(f(x,y) for x,y in itertools.combinations(arr,2)) answer = 0 for p in itertools.product(*itrs): arr = [] for x in p: arr += x answer = max(answer, F(arr)) print(answer) ```
105,118
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` import itertools def calc(s): ret = 24 for i in range(1, len(s)): ret = min(ret, s[i] - s[i-1]) return ret def main(): N = int(input()) D = list(map(int, input().split())) if D.count(0) >= 1 or D.count(12) >= 2: print(0) return cnt = [0] * 13 for d in D: cnt[d] += 1 fixed = [0, 24] variable = [] for i in range(1, 12): if cnt[i] >= 3: print(0) return elif cnt[i] == 2: fixed.append(i) fixed.append(24-i) elif cnt[i] == 1: variable.append(i) if cnt[12] == 1: fixed.append(12) ans = 0 for p in itertools.product((False, True), repeat=len(variable)): s = list(fixed) for i in range(len(variable)): if p[i]: s.append(variable[i]) else: s.append(24-variable[i]) s.sort() ans = max(ans, calc(s)) print(ans) if __name__ == "__main__": main() ```
105,119
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) D = list(map(int,input().split())) # from random import randint # n = randint(1, 50) # D = [randint(0, 12) for i in range(n)] C = [0] * 13 C[0] = 1 for i in range(n): C[D[i]] += 1 if C[0] >= 2 or C[12] >=2: print(0) sys.exit() for i in range(13): if C[i] >= 3: print(0) sys.exit() # D = [] # for i in range(13): # for j in range(C[i]): # D.append(i) # n = len(D) # A = [0] * n # print(C) ans0 = 0 for i in range(2**13): zero = 0 ib = format(i, "b").zfill(13) A = [] for j in range(13): if C[j] == 1: A.append(j * (-1) ** int(ib[j])) if C[j] == 2: if int(ib[j]) == 0: A.append(j) A.append(-j) else: zero = 1 if zero == 1: continue # print("not cont") ans = 30 # if len(A)>1: # print(A) # print(ib, A) for j in range(len(A)): for k in range(j+1, len(A)): di = abs(A[k] - A[j]) # print(ans) ans = min(ans, min(di, 24-di)) if ans <= ans0: break if ans <= ans0: break # print(ib, ans0,ans) ans0 = max(ans0, ans) print(ans0) ```
105,120
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` def solve(N, D): fill = [0] * 24 fill[0] = 1 left = False for i in range(N): if D[i] == 0: print(0) return 0 elif D[i] == 12: if fill[12] == 1: print(0) return 0 else: fill[12] = 1 else: if not left: if fill[D[i]] == 0: fill[D[i]] = 1 left = True elif fill[24-D[i]] == 0: fill[24-D[i]] = 1 left = False else: print(0) return 0 else: if fill[24-D[i]] == 0: fill[24-D[i]] = 1 left = False elif fill[D[i]] == 0: fill[D[i]] = 1 left = True else: print(0) return 0 s = 24 cnt = 0 for i in range(1, 24): if i == 23: if fill[i] == 0: cnt += 1 s = min(s, cnt + 1) else: s = min(s, cnt + 1) else: if fill[i] == 0: cnt += 1 else: s = min(s, cnt + 1) cnt = 0 print(s) if __name__ == '__main__': N = int(input()) D = list(map(int, input().split())) solve(N, sorted(D)) ```
105,121
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` from collections import Counter N = int(input()) ds = list(map(int, input().split())) c = Counter(ds) cs = c.most_common() if N >= 24 or cs[0][1] >= 3 or 0 in c: print("0") exit() l = len(cs) k = 0 s = (1 << 0) + (1 << 24) while k < l and cs[k][1] == 2: s += (1 << cs[k][0]) + (1 << (24 - cs[k][0])) k += 1 def process(s, i): if i == l: return min(map(len, bin(s)[2:].split("1")[1:-1])) + 1 return max( process(s + (1 << cs[i][0]), i+1), process(s + (1 << (24 - cs[i][0])), i+1), ) print(process(s, k)) ```
105,122
Provide a correct Python 3 solution for this coding contest problem. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 "Correct Solution: ``` from collections import Counter, defaultdict import sys sys.setrecursionlimit(10 ** 5 + 10) # input = sys.stdin.readline from math import factorial import heapq, bisect import math import itertools import queue from collections import deque def main(): num = int(input()) data = list(map(int, input().split())) count = Counter(data) ans = 0 for i in range(2 ** 11): bin_list = bin(i)[2:] bin_list = bin_list.zfill(11) bin_list = list(map(int, list(bin_list))) now_data = [0, 24] if count[12] >= 2: print(0) sys.exit() elif count[12] == 1: now_data.append(12) if count[0] >= 1: print(0) sys.exit() for j in range(1, 12): if count[j] == 0: continue elif count[j] >= 3: print(0) sys.exit() elif count[j] == 2: now_data.append(j) now_data.append(24 - j) elif bin_list[j - 1]: now_data.append(j) else: now_data.append(24 - j) now_data.sort() kari = 12 # print(now_data) for j in range(len(now_data) - 1): kari = min(kari, now_data[j + 1] - now_data[j]) ans = max(ans, kari) print(ans) if __name__ == '__main__': main() # test() ```
105,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` from collections import Counter import itertools N = int(input()) Ds = list(map(int, input().split())) + [0] cnt = Counter(Ds) if max(cnt.values()) >= 3: print(0) exit() citys = [] rest = [] for k, v in cnt.items(): if v == 2: if k == 0 or k == 12: print(0) exit() citys.append(k) citys.append(24 -k) else: rest.append(k) def check(citys): if len(citys) <= 1: return 24 citys.sort() ans = 24 prev = citys[-1] - 24 for c in citys: ans = min(ans, c - prev) prev = c return ans if check(citys) == 1: print(1) exit() ans = 0 for ops in itertools.product('+-', repeat=len(rest)): citys2 = citys.copy() for i in range(len(rest)): if ops[i] == '+': citys2.append(rest[i]) else: citys2.append(24 - rest[i]) ans = max(ans, check(citys2)) print(ans) ``` Yes
105,124
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` # coding: utf-8 import itertools import collections def main(): n = int(input()) c = collections.Counter(map(int, input().split())) if c[0] > 0: return 0 if c.most_common(1)[0][1] >= 3: return 0 l1 = [] l2 = [] for i, cnt in c.most_common(): if cnt == 2: l2.append(i) elif cnt == 1: l1.append(i) s = 0 for i in range(len(l1) + 1): for invs in itertools.combinations(l1, i): l = [0] for k in l1: l.append(-k if k in invs else k) for k in l2: l.append(k) l.append(-k) l.sort() ss = 24 for j in range(len(l)-1): ss = min(l[j + 1] - l[j], ss) d = l[-1] - l[0] ss = min(min(d, 24-d), ss) s = max(s, ss) return s print(main()) ``` Yes
105,125
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` N = int(input()) D = list(map(int, input().split())) rec = [0] * 13 for i in range(N): rec[D[i]] += 1 if rec[0] >= 1: print(0) exit() ans = 0 for i in range(1 << 12): tmp = [0] for j in range(13): if rec[j] >= 3: print(0) exit() elif rec[j] == 2: tmp.append(j) tmp.append(24 - j) elif rec[j] == 1: if i & 1 << j: tmp.append(j) else: tmp.append(24 - j) tmp = sorted(tmp) pre = tmp[0] num = float("inf") for i in range(1, len(tmp)): num = min(num, tmp[i] - pre) pre = tmp[i] ans = max(min(num, 24 - tmp[-1]), ans) print(ans) ``` Yes
105,126
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` N = int(input()) D = sorted([int(_) for _ in input().split()]) #最小値の最大値 if D[0] == 0: print(0) exit() p = m = 0 a = 99 for d in D: #asign positive e = min(a, d - p, 24 - d - m) #asign negative f = min(a, d - m, 24 - d - p) if e < f: a = f m = d else: a = e p = d print(a) ``` Yes
105,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` n = int(input()) d_l = list(map(int, input().split())) d_l.insert(0, 0) mn = None for i in range(n+1): for j in range(i+1, n+1): ans = max(abs(d_l[i]-d_l[j]), abs(24-d_l[i]-d_l[j])) if mn is None: mn = ans if mn > ans: mn = ans print(mn) ``` No
105,128
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` import collections n = int(input()) inp_list = list(map(int, input().split())) inp_list.append(0) c = collections.Counter(inp_list) a = c.most_common() def ura(time): if time == 12: time = 12 elif time == 0: time = 0 else: time = 24 - time return time member = [] for tup in a: if tup[1] > 1: member.append(tup[0]) member.append(tup[0]) elif tup[1] == 1: member.append(tup[0]) ans = 0 for i in range(2**len(member)): trry = format(i, '0' + str(len(member))+'b') time_diff = 24 for ii in range(len(member)-1): for iii in range(ii+1, len(member)): A = member[ii] if trry[iii] == '0': B = member[iii] else: B = ura(member[iii]) a_b = abs(A-B) b_a = min(A, B)+24-max(A, B) time_diff = min(time_diff, a_b, b_a) ans = max(ans, time_diff) print(ans) ``` No
105,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` import collections import numpy as np N = int(input()) D = list(map(int, input().split())) D.append(0) m = 0 while 1: D.sort() Dsub = [(D[i+1] - D[i]) for i in range(len(D)-1)] Dsub.sort() if m >= Dsub[0]: break else: m = Dsub[0] Dsub1 = [(D[i+1] - D[i]) for i in range(len(D)-1)] index = Dsub1.index(m) D[index] = abs(24 - D[index]) print(m) ``` No
105,130
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi. Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example. Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours. Find the maximum possible value of s. Constraints * 1 \leq N \leq 50 * 0 \leq D_i \leq 12 * All input values are integers. Input Input is given from Standard Input in the following format: N D_1 D_2 ... D_N Output Print the maximum possible value of s. Examples Input 3 7 12 8 Output 4 Input 2 11 11 Output 2 Input 1 0 Output 0 Submitted Solution: ``` from fractions import gcd from datetime import date, timedelta from heapq import* import math from collections import defaultdict, Counter, deque import sys from bisect import * import itertools import copy sys.setrecursionlimit(10 ** 7) MOD = 10 ** 9 + 7 def main(): n = int(input()) d = list(map(int, input().split())) if n == 1: print(min(d[0],24 - d[0])) exit() d2 = [] dc = defaultdict(int) for i in range(n): v = min(d[i], 24 - d[i]) if dc[v] > 2 or (dc[v] == 1 and v == 12) or v == 0: print(0) exit() dc[v] += 1 d2.append(v) dd = [] ddt = [] for i in range(0, 13): if dc[i] == 1: dd.append(i) elif dc[i] == 2: ddt.append(i) ddt.append(24 - i) ans = 0 for i in range(1 << len(dd)): d3 = [] for j in range(len(ddt)): d3.append(ddt[j]) for j in range(len(dd)): if (i & (1 << j)): d3.append(dd[j]) else: d3.append(24 - dd[j]) d3 = sorted(d3) t = float("inf") for i in range(len(d3) - 1): t = min(t, d3[i + 1] - d3[i]) ans = max(t, ans) print(ans) if __name__ == '__main__': main() ``` No
105,131
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` n=int(input()) A=list(map(int,input().split())) C=[0]*9 for i in A: x=i//400 if x<8: C[x]=1 else: C[8]+=1 b=sum(C[:8]) print(str(max(b,1))+' '+str(b+C[8])) ```
105,132
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) cnt=0 l=[] for i in a: p=i//400 if p<=7: l.append(p) else: cnt+=1 d=len(set(l)) if d!=0: print(d,d+cnt) else: print(1,cnt) ```
105,133
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` n=int(input()) A=list(map(int,input().split())) A=[a for a in A if a<3200] free=n-len(A) A=set(map(lambda x: x//400,A)) print(max(1,len(A)),len(A)+free) ```
105,134
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` import math n=input() a= [min(math.floor(int(i)//400),8) for i in input().split()] a7= len(set([i for i in a if i<=7])) a8= len([i for i in a if i==8]) print(max(a7,1),a7+a8) ```
105,135
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) o=0 x=[] for i in range(n): if a[i]>=3200: o+=1 else: x.append(a[i]//400) p=len(set(x)) q=max(1,p) print(q,o+p) ```
105,136
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) c=[0]*8 sums=0 sai=0 for i in a: if i<3200: c[i//400]=1 else: sai+=1 sums=sum(c) print(max(1,sums),sums+sai) ```
105,137
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` n, *a = map(int, open(0).read().split()) l = [i//400 for i in a if i<3200] k = len(set(l)) print(k or 1, n - len(l) + k) ```
105,138
Provide a correct Python 3 solution for this coding contest problem. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 "Correct Solution: ``` v = [0]*9 n = int(input()) a = list(map(int,input().split())) for i in a: idx = i//400 if idx >= 8: idx = 8 v[idx] += 1 r = 0 for i in range(8): if v[i] >= 1: r+= 1 print(max(1,r), r+v[8]) ```
105,139
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` n=int(input()) a=[int(i)//400 for i in input().split()] b=[] c=0 for i in a: if i<8: b.append(i) else: c+=1 b=set(b) print(max(len(b),1),len(b)+c) ``` Yes
105,140
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` input();a=[0]*9 for x in input().split(): x=int(x) if x>3599:x=3200 a[x//400]+=1 b=sum([1 if x else 0 for x in a[:8]]) print(max(1,b),b+a[8]) ``` Yes
105,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) b=[0]*9 for i in a: b[min(i//400,8)]+=1 print(max(8-b[:8].count(0),1),8-b[:8].count(0)+b[8]) ``` Yes
105,142
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) a=list(map(lambda x:x//400,a)) cnt=0 z=[] for i in a: if i>=8: cnt+=1 else: z.append(i) print(max(1,len(set(z))),len(set(z))+cnt) ``` Yes
105,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` N = int(input()) a = list(map(int,input().split())) C = [0 for _ in range(9)] for i in range(N): if a[i] // 400 < 8: C[a[i]//400] = 1 else: C[8] += 1 ans_min = sum(C[:8]) ans_max = min(8,sum(C)) print(ans_min,ans_max) ``` No
105,144
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` n = int(input()) a = [int(i) for i in input().split()] color = [0] * 8 any = 0 for i in a: if 1 <= i <= 399: color[0] = 1 elif 400 <= i <= 799: color[1] = 1 elif 800 <= i <= 1199: color[2] = 1 elif 1200 <= i <= 1599: color[3] = 1 elif 1600 <= i <= 1999: color[4] = 1 elif 2000 <= i <= 2399: color[5] = 1 elif 2400 <= i <= 2799: color[6] = 1 elif 2800 <= i <= 3199: color[7] = 1 else: any += 1 m = max(sum(color), 1) M = min(sum(color) + any, 8) print(m, M) ``` No
105,145
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` N = int(input()) A = list(map(int, input().split())) ans = [0]*9 leng = 0 for i in range(N): if 1 <= A[i] <= 399: ans[0] = 1 elif 400 <= A[i] <= 799: ans[1] = 1 elif 800 <= A[i] <= 1199: ans[2] = 1 elif 1200 <= A[i] <= 1599: ans[3] = 1 elif 1600 <= A[i] <= 1999: ans[4] = 1 elif 2000 <= A[i] <= 2399: ans[5] = 1 elif 2400 <= A[i] <= 2799: ans[6] = 1 elif 2800 <= A[i] <= 3199: ans[7] = 1 else: leng += 1 print(sum(ans), sum(ans)+leng) ``` No
105,146
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: * Rating 1-399 : gray * Rating 400-799 : brown * Rating 800-1199 : green * Rating 1200-1599 : cyan * Rating 1600-1999 : blue * Rating 2000-2399 : yellow * Rating 2400-2799 : orange * Rating 2800-3199 : red Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not. Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i. Find the minimum and maximum possible numbers of different colors of the users. Constraints * 1 ≤ N ≤ 100 * 1 ≤ a_i ≤ 4800 * a_i is an integer. Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. Examples Input 4 2100 2500 2700 2700 Output 2 2 Input 5 1100 1900 2800 3200 3200 Output 3 5 Input 20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 Output 1 1 Submitted Solution: ``` n = int(input()) a = [0]*9 i = input().split() for x in range(n): y = int(i[x]) if 1<= y <=399: a[0] += 1 elif 400<= y <=799: a[1] += 1 elif 800 <= y <= 1199: a[2] += 1 elif 1200 <= y <= 1599: a[3] += 1 elif 1600 <= y <= 1999: a[4] += 1 elif 2000 <= y <= 2399: a[5] += 1 elif 2400 <= y<= 2799: a[6] += 1 elif 2800 <= y <= 3199: a[7] += 1 else : a[8] += 1 count = 0 for x in range(8): if a[x] > 0: count += 1 max = count + a[8] if max > 8: max = 8 if count == 0 and a[8] > 0: count = 1 print(count,max) ``` No
105,147
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` input() sm = de = 0 ans = 10 ** 15 for s in (input() + "-0").split('-'): a = list(map(int, s.split('+'))) if sm: ans = min(ans, sum(a) + de) de += a[0] sm += sum(a) print(sm - 2 * ans) ```
105,148
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` n = int(input()) a = input().split() if(n==1): print(a[0]) exit() a_num = list( map(int, a[::2])) a_op = a[1::2] cumsum = a_num[::] cumsum_op = [0] * n cumsum_op[0] = a_num[0] minus_ind = [] for i in range(1,n): cumsum[i] += cumsum[i-1] if(a_op[i-1] == '+'): cumsum_op[i] = cumsum_op[i-1] + a_num[i] else: cumsum_op[i] = cumsum_op[i-1] - a_num[i] minus_ind.append(i) ans = cumsum_op[-1] if(len(minus_ind)<2): print(ans) exit() for l,r in zip(minus_ind[:-1],minus_ind[1:]): # lまでは符号通り、l,r-1までを減算、r以降を全て加算 tmp = cumsum_op[l-1] - (cumsum[r-1] - cumsum[l-1]) + (cumsum[-1] - cumsum[r-1]) ans = max(ans,tmp) print(ans) ```
105,149
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` import sys input = sys.stdin.readline N = int(input()) terms = [[]] # マイナス区切り op = '+' for x in input().rstrip().split(): if x in '+-': op = x continue x = int(x) if op == '+': terms[-1].append(x) else: terms.append([x]) # 何をマイナスにするか min_minus = 10 ** 18 first_sum = 0 for t in terms[1:]: x = first_sum + sum(t) if x < min_minus: min_minus = x first_sum += t[0] answer = sum(sum(t) for t in terms) if len(terms) > 1: answer -= 2 * min_minus print(answer) ```
105,150
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` n = int(input()) a = list(map(str,input().split())) dp = [[-float("inf")] *4 for i in range(n+1)] dp[0][0] = 0 for i in range(len(a)): b = i//2+1 if a[i] == "-": tmp = [-float("inf"),0,0,0] for j in range(3): tmp[j+1] = dp[b][j] dp[b] = tmp continue elif a[i] == "+": continue for j in range(4): for k in range(3): if j >= k: dp[b][k] = max(dp[b][k],dp[b-1][j]+((-1)**j)*int(a[i])) print(dp[n][0]) ```
105,151
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` def solve(n, inp): if n == 1: return int(inp[0]) INF = -(10 ** 18) dp1, dp2, dp3 = int(inp[0]), INF, INF for op, a in zip(inp[1::2], inp[2::2]): a = int(a) if op == '+': dp1, dp2, dp3 = dp1 + a, max(dp2 - a, dp3 + a), dp3 + a else: dp1, dp2, dp3 = max(dp1 - a, dp2 + a), max(dp1 - a, dp2 + a), max(dp2 + a, dp3 - a) return dp1 n = int(input()) inp = input().split() print(solve(n, inp)) ```
105,152
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` #!/usr/bin/env python3 def solve(n): s = input() sum_a = 0 sum_n = 0 sum_np = 0 min_sum_np = 10 ** 15 j = s.find('-') if j < 0: return sum(list(map(int, s.split(' + ')))) sum_a = sum(list(map(int, s[:j - 1].split(' + ')))) j += 2 s += ' ' len_s = len(s) sign = -1 w = '' while j < len_s: ch = s[j] j += 1 if ch == ' ': if len(w): a = int(w) sum_a += a w = '' if sign < 0: sum_n += a sum_np = 0 else: sum_np += a elif ch == '+': sign = 1 elif ch == '-': min_sum_np = min(min_sum_np, sum_n + sum_np) sign = -1 else: w += ch min_sum_np = min(min_sum_np, sum_n + sum_np) return sum_a - 2 * min_sum_np def main(): n = input() n = int(n) print(solve(n)) if __name__ == '__main__': main() ```
105,153
Provide a correct Python 3 solution for this coding contest problem. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 "Correct Solution: ``` """ https://atcoder.jp/contests/arc066/tasks/arc066_c 最終的な式における寄与を考える -はどういう効果を持っている? 愚直な方法としてdpがある dp[i][mnum] = i番目まで見て、-がmnum個重なってる状態での最大値 → O(N^2) じつは簡単にできる・保持しておくべき(答えになる)状態はもっと少ないのでは? →これ、割と濃厚…か? -の効果は、それ以降の寄与を反転させる なるべく必要のない(無駄な)ところでは戻さない方が最適に近づく 現状 - で、足すやつを寄与+にしたい! → 戻す 現状 + で、足すやつを寄与-にしたい!→そんな場合はない -直後のやつを寄与+にしたい!→ 戻せる+なら直前で-に戻せばいい お??? A-B+C-D+E+F-…とやっていく 一度マイナスのカッコに入ると決めたとする A-(B+C-(D+E+F)-G-(H+I)) すると、次の-以降はすべて+の寄与にできる あとは耳dp! dp[i][state] = i番目まで見て,stateの時の最大値 state = 0 #かっこをつけないで計算するモード state = 1 #全引きモード state = 2 #全足しモード """ N = int(input()) A = input().split() dp = [int(A[0]),float("-inf"),float("-inf")] for i in range(N-1): op = A[i*2+1] a = int(A[i*2+2]) ndp = [float("inf")] * 3 if op == "+": ndp[0] = dp[0] + a ndp[1] = dp[1] - a ndp[2] = dp[2] + a else: ndp[0] = dp[0] - a ndp[1] = dp[0] - a ndp[2] = max(dp[2]+a ,dp[1]+a) dp = ndp print (max(dp)) ```
105,154
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 Submitted Solution: ``` import sys n = int(next(sys.stdin).strip()) items = next(sys.stdin).strip().split() cache = {(i, i + 1): (int(items[2 * i]), int(items[2 * i])) for i in range(n)} def value(l, r): if (l, r) in cache: return cache[(l, r)] lower, upper = float('inf'), float('-inf') for c in range(l + 1, r): ll, lu = value(l, c) rl, ru = value(c, r) op = items[2 * c - 1] if op == '+': lower = min(lower, ll + rl) upper = max(upper, lu + ru) elif op == '-': lower = min(lower, ll - ru) upper = max(upper, lu - rl) cache[(l, r)] = (lower, upper) return lower, upper lower, upper = value(0, n) print(upper) ``` No
105,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 Submitted Solution: ``` #include <bits/stdc++.h> #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using std::cin; using std::cout; using std::lower_bound; using std::string; using std::upper_bound; using std::vector; using vi = vector<long long>; using vii = vector<vi>; using pii = std::pair<long long, long long>; constexpr long long MOD = 1e9 + 7; constexpr long long MAX = 3e6; constexpr long long inf = (1ll << 60); template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <typename T> struct Segment_tree { long long N; T mem; vector<T> node; Segment_tree(vector<T> &X, T m) : mem(m) { long long sz = X.size(); N = 1; while (N < sz) N *= 2; node.resize(2 * N - 1, mem); for (long long i = (long long)(0); i < (long long)(sz); i++) node[N - 1 + i] = X[i]; for (long long i = (long long)(N - 2); (long long)(0) <= i; i--) { node[i] = Compare(node[i * 2 + 1], node[i * 2 + 2]); } } T Compare(T &A, T &B) { return std::max(A, B); } void update(long long X, T val) { X += N - 1; node[X] = val; while (X > 0) { X = (X - 1) / 2; node[X] = Compare(node[X * 2 + 1], node[X * 2 + 2]); } } T Query(long long a, long long b, long long now, long long l, long long r) { if (r < 0) r = N; if (r <= a || b <= l) return mem; if (a <= l && r <= b) return node[now]; auto vl = Query(a, b, now * 2 + 1, l, (l + r) / 2), vr = Query(a, b, now * 2 + 2, (l + r) / 2, r); return Compare(vl, vr); } }; template <typename T> struct lazy_Segment_tree { int N; vector<T> node, lazy; T INF; lazy_Segment_tree(vector<T> X, T Y) : INF(Y) { N = 1; while (X.size() > N) N *= 2; node.resize(2 * N - 1, Y); lazy.resize(2 * N - 1); for (long long i = (long long)(0); i < (long long)(X.size()); i++) node[i + N - 1] = X[i]; for (long long i = (long long)(N - 2); (long long)(0) <= i; i--) node[i] = compare(node[i * 2 + 1], node[i * 2 + 2]); } T compare(T X, T Y) { return std::max(X, Y); } T plus(T X, int l, int r) { return X; } void eval(int now, int l, int r) { if (lazy[now] == 0) return; node[now] += lazy[now]; if (r - l > 1) { lazy[now * 2 + 1] += plus(lazy[now], l, r); lazy[now * 2 + 2] += plus(lazy[now], l, r); } lazy[now] = 0; } void update(int a, int b, T add, int now = 0, int l = 0, int r = -1) { if (r < 0) r = N; eval(now, l, r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[now] += add; eval(now, l, r); } else { update(a, b, add, now * 2 + 1, l, (r + l) / 2); update(a, b, add, now * 2 + 2, (r + l) / 2, r); node[now] = compare(node[now * 2 + 1], node[now * 2 + 2]); } } T Query(int a, int b, int now = 0, int l = 0, int r = -1) { if (r < 0) r = N; eval(now, l, r); if (b <= l || r <= a) return INF; if (a <= l && r <= b) return node[now]; return compare(Query(a, b, now * 2 + 1, l, (r + l) / 2), Query(a, b, now * 2 + 2, (r + l) / 2, r)); } }; struct Tree { int N; vii dp; vi dist; Tree(vii edge) { N = edge.size(); dp.resize(N); dist.resize(N, -1); for (int i = 0; i < N; i++) dp[i].resize(30); dist[0] = dp[0][0] = 0; std::queue<int> que; que.push(0); while (!que.empty()) { int now = que.front(); que.pop(); for (int i = 0; i < edge[now].size(); i++) { int next = edge[now][i]; if (dist[next] == -1) { dist[next] = dist[now] + 1; que.push(next); dp[next][0] = now; } } } for (int i = 1; i < 30; i++) { for (int j = 0; j < N; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1]; } } int LCA(int X, int Y) { if (dist[X] < dist[Y]) std::swap(X, Y); { int Z = dist[X] - dist[Y]; for (int i = 0; i < 30; i++) { if (Z & (1 << i)) { X = dp[X][i]; } } } if (X == Y) return X; for (int i = 29; i >= 0; i--) { if (dp[X][i] != dp[Y][i]) { X = dp[X][i]; Y = dp[Y][i]; } } return dp[X][0]; } }; struct Binary_indexed_tree { int N; vi bit; Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); } void add(int x, long long a) { for (x; x <= N; x += (x & -x)) bit[x] += a; } long long sum(int x) { long long ret = 0; for (x; x > 0; x -= (x & -x)) ret += bit[x]; return ret; } long long lower_bound(long long X) { if (sum(N) < X) return -1; long long ret = 0, memo = 1, sum = 0; while (memo * 2 <= N) memo *= 2; while (memo > 0) { if (memo + ret <= N && sum + bit[memo + ret] < X) { sum += bit[memo + ret]; ret += memo; } memo /= 2; } return ret + 1; } }; struct Union_Find { long long N; vi par; vi siz; Union_Find(int n) : N(n) { par.resize(N); siz.resize(N, 1); for (long long i = (long long)(0); i < (long long)(N); i++) par[i] = i; } long long root(long long X) { if (par[X] == X) return X; return par[X] = root(par[X]); } bool same(long long X, long long Y) { return root(X) == root(Y); } void unite(long long X, long long Y) { X = root(X); Y = root(Y); if (X == Y) return; par[X] = Y; siz[Y] += siz[X]; siz[X] = 0; } long long size(long long X) { return siz[root(X)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vi fac, finv, inv; void COMinit() { fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(long long n, long long r) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } void comp(vi &A) { std::map<long long, long long> memo; for (long long i = (long long)(0); i < (long long)(A.size()); i++) memo[A[i]] = 0; long long cnt = 1; for (auto &p : memo) p.second = cnt++; for (long long i = (long long)(0); i < (long long)(A.size()); i++) A[i] = memo[A[i]]; } void dec(std::map<long long, long long> &mem, long long X) { mem[X]--; if (mem[X] == 0) mem.erase(X); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); long long N; cin >> N; vi A(N); vector<char> op(N); for (long long i = (long long)(0); i < (long long)(N - 1); i++) cin >> A[i] >> op[i]; cin >> A[N - 1]; vii dp(N, vi(3, -inf)); dp[0][0] = A[0]; for (long long i = (long long)(1); i < (long long)(N); i++) { if (op[i - 1] == '+') { dp[i][0] = std::max({dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]}) + A[i]; dp[i][1] = dp[i - 1][1] - A[i]; dp[i][2] = dp[i - 1][2] + A[i]; } else { dp[i][0] = dp[i - 1][0] - A[i]; dp[i][1] = std::max(dp[i - 1][0] - A[i], dp[i - 1][1] + A[i]); dp[i][2] = std::max(dp[i - 1][2] + A[i], dp[i - 1][1] + A[i]); } } long long ans = -inf; for (long long i = (long long)(0); i < (long long)(3); i++) ans = std::max(ans, dp[N - 1][i]); cout << ans << "\n"; } ``` No
105,156
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 Submitted Solution: ``` n = int(input()) ipt = input().split() node = [int(ipt[0])] plus = n-1 while plus > 0 and ipt[2*plus] == '+': node[0] += int(ipt[2*plus+1]) plus -= 1 for i in range(plus): if ipt[2*i+1] == '+': node[-1] += int(ipt[2*i+2]) else: node.append(int(ipt[2*i+2])) if len(node) == 1: print(node[0]) else: print(sum(node) - 2*node[1]) ``` No
105,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N. Here, A_i is an integer, and op_i is an binary operator either `+` or `-`. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints * 1≦N≦10^5 * 1≦A_i≦10^9 * op_i is either `+` or `-`. Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Examples Input 3 5 - 1 - 3 Output 7 Input 5 1 - 2 + 3 - 4 + 5 Output 5 Input 5 1 - 20 - 13 + 14 - 5 Output 13 Submitted Solution: ``` n = int(input()) ipt = input().split() node = [int(ipt[0])] plus = n-1 while plus > 0 and ipt[2*plus-1] == '+': node[0] += int(ipt[2*plus]) plus -= 1 for i in range(plus): if ipt[2*i+1] == '+': node[-1] += int(ipt[2*i+2]) else: node.append(int(ipt[2*i+2])) if len(node) == 1: print(node[0]) else: print(sum(node) - 2*node[1]) ``` No
105,158
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` import bisect N = int(input()) x = list(map(int, input().split())) L = int(input()) dest = [] dest_1 = [0] * N for i in range(N): dest_1[i] = bisect.bisect_left(x, x[i] + L + 0.5) - 1 dest.append(dest_1) for i in range(1, len(bin(N - 1)) - 1): dest_prev = dest[i - 1] dest_i = [0] * N for j in range(N): dest_i[j] = dest_prev[dest_prev[j]] dest.append(dest_i) Q = int(input()) for _ in range(Q): a, b = map(int, input().split()) a -= 1 b -= 1 if a > b: a, b = b, a k = len(dest) - 1 ans = 0 while True: d = dest[k][a] if d >= b: if k > 0: k -= 1 else: ans += 1 break else: ans += 2 ** k a = d print(ans) ```
105,159
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` from bisect import bisect_right import sys input = sys.stdin.readline N = int(input()) x = list(map(int, input().split())) L = int(input()) log = 1 while 1 << log < N: log += 1 doubling = [[0] * (log + 1) for _ in range(N)] for i in range(N): j = bisect_right(x, x[i] + L) - 1 doubling[i][0] = j for l in range(1, log + 1): for i in range(N): doubling[i][l] = doubling[doubling[i][l-1]][l-1] Q = int(input()) for _ in range(Q): a, b = map(int, input().split()) a -= 1 b -= 1 if a > b: a, b = b, a ans = 0 for l in range(log, -1, -1): if doubling[a][l] < b: ans += 1 << l a = doubling[a][l] print(ans + 1) ```
105,160
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` import sys,bisect input=sys.stdin.readline N=int(input()) x=list(map(int,input().split())) L=int(input()) doubling=[[-1 for i in range(N)] for j in range(20)] for i in range(N): npos=x[i]+L index=bisect.bisect_right(x,npos) doubling[0][i]=index-1 for i in range(1,20): for j in range(N): doubling[i][j]=doubling[i-1][doubling[i-1][j]] forward=[[-1 for i in range(N)] for j in range(20)] for i in range(N): forward[0][i]=i for i in range(1,20): for j in range(N): forward[i][j]=doubling[i-1][forward[i-1][j]] for _ in range(int(input())): a,b=map(int,input().split()) a-=1;b-=1 if a>b: a,b=b,a res=0 for i in range(19,-1,-1): if b>forward[i][a]: a=doubling[i][a] res+=2**i print(res) ```
105,161
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n = I() x = LI() f = [[] for i in range(n)] l = I() for i in range(n): xi = x[i] j = bisect.bisect_right(x,l+xi)-1 f[i].append(j) h = int(math.log(n,2))+1 for j in range(h-1): for i in range(n): f[i].append(f[f[i][j]][j]) q = I() for _ in range(q): a,b = LI() a -= 1 b -= 1 if b < a: a,b = b,a ans = 0 for i in range(h)[::-1]: if f[a][i] < b: a = f[a][i] ans += 1<<i print(ans+1) return #Solve if __name__ == "__main__": solve() ```
105,162
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` import bisect import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) d = int(input()) graph = [[0 for i in range(n+1)] for j in range(18)] for i in range(n): x = bisect.bisect_right(a,a[i]+d) graph[0][i+1] = x for j in range(1,18): for i in range(n): t = graph[j-1][i+1] graph[j][i+1] = graph[j-1][t] q = int(input()) for _ in range(q): x,y = map(int,input().split()) x,y = min(x,y),max(x,y) ans = 0 for j in range(18)[::-1]: if graph[j][x] < y: ans += 2**j x = graph[j][x] if j == 0 and x < y: ans += 1 print(ans) ```
105,163
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` import bisect n = int(input()) x = list(map(int, input().split())) l = int(input()) q = int(input()) query = [list(map(int, input().split())) for i in range(q)] log_size = n.bit_length() + 1 double = [[0] * n for i in range(log_size)] for i in range(n): double[0][i] = bisect.bisect_right(x, x[i] + l) - 1 for k in range(1, log_size): for i in range(n): double[k][i] = double[k - 1][double[k - 1][i]] for a, b in query: a -= 1 b -= 1 if a > b: a, b = b, a ans = 10 ** 18 tmp = 0 for k in range(log_size)[::-1]: if double[k][a] >= b: ans = min(ans, 2 ** k + tmp) else: a = double[k][a] tmp += 2 ** k print(ans) ```
105,164
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n = I() x = LI() L = I() q = I() p = [[] for i in range(n)] for i in range(n): xi = x[i] l = i r = n while r-l > 1: m = (l+r) >> 1 xm = x[m] if xm-xi <= L: l = m else: r = m p[i].append(l) N = 20 for j in range(N): for i in range(n): p[i].append(p[p[i][-1]][-1]) for i in range(q): a,b = LI() a -= 1 b -= 1 a,b = min(a,b),max(a,b) ans = 1 for j in range(N)[::-1]: if p[a][j] < b: a = p[a][j] ans += 1<<j print(ans) return #Solve if __name__ == "__main__": solve() ```
105,165
Provide a correct Python 3 solution for this coding contest problem. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 "Correct Solution: ``` n = int(input()) hot = list(map(int,input().split())) n_ = n.bit_length() db = [[n-1]*(n)] L = int(input()) r = 1 for i in range(n-1): while r < n-1 and hot[r+1]-hot[i] <= L: r += 1 db[0][i] = r for j in range(1,n_+1): new = [db[-1][db[-1][i]] for i in range(n)] db.append(new) if new[0] == n-1: break n_ = len(db) def query(s,t): dt = 0 for j in range(n_-1,0,-1): if db[j][s] < t: dt += 2**j s = db[j][s] while s < t: dt += 1 s = db[0][s] return dt q = int(input()) for _ in range(q): s,t = map(int,input().split()) if t < s: s,t = t,s print(query(s-1,t-1)) ```
105,166
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` def examE(): N = I() X = LI() L = I(); Q = I() n = N.bit_length()+1 dp = [[0] * n for _ in range(N)] for i in range(N): dp[i][0] = bisect.bisect_right(X, X[i] + L) - 1 for i in range(1, n): for j in range(N): if dp[j][i - 1] < N: index = dp[dp[j][i - 1]][i - 1] if index == j: dp[j][i] = N else: dp[j][i] = index else: dp[j][i] = N def fast_day(a, b): if a > b: a, b = b, a res = 0 for i in range(n): if a >= b: return res c = max(0, bisect.bisect_left(dp[a], b) - 1) #最低かかる2**?の日数 a = dp[a][c] #そこまで行く res += 2 ** c for _ in range(Q): a, b = map(int, input().split()) print(fast_day(a - 1, b - 1)) # print(dp) import sys,copy,bisect,itertools,heapq,math from heapq import heappop,heappush,heapify from collections import Counter,defaultdict,deque def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) def LS(): return sys.stdin.readline().split() def S(): return sys.stdin.readline().strip() mod = 10**9 + 7 inf = float('inf') examE() ``` Yes
105,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` import sys input = sys.stdin.readline from bisect import bisect N=int(input()) X=[int(i) for i in input().split()] L=int(input()) Q=int(input()) #T=[[int(i) for i in input().split()] for i in range(Q)] T=[] for i in range(Q): a,b=map(int,input().split()) if a>b: a,b=b,a T.append((a-1,b-1)) F=[0]*N for i in range(N): F[i]= bisect(X, X[i] + L) -1 #print(F) dp=[ [N-1] * (N) for i in range(18)] for i in range(N): #dp[0][i]=i dp[0][i]=F[i] for c in range(1,18): for i in range(N): dp[c][i] = dp[c-1][ dp[c-1][i] ] #print(dp) def f(a,b): num=0 t = a while True: for i in range(18): if dp[i][t]>=b: s=i break if s==0: num+=1 break num += pow(2, s - 1) t = dp[s-1][t] print(num) for a,b in T: f(a,b) ``` Yes
105,168
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` #!/usr/bin/python3.6 import bisect n = int(input()) x = [int(item) for item in input().split()] l = int(input()) q = int(input()) ab = [] for i in range(q): a, b = [int(item) for item in input().split()] a -= 1; b -= 1 if a > b: a, b = b, a ab.append((a, b)) dt = [[0] * n for _ in range(18)] for i, item in enumerate(x): dt[0][i] = bisect.bisect_right(x, item + l) - 1 for i in range(1, 18): for j in range(n): dt[i][j] = dt[i-1][dt[i-1][j]] for a, b in ab: days = 0 if a == b: print(days) continue for i in range(17, -1, -1): if dt[i][a] < b: a = dt[i][a] days += 2**i print(days + 1) ``` Yes
105,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` import sys input=sys.stdin.readline from bisect import bisect_right n=int(input()) X=tuple(map(int,input().split())) l=int(input()) q=int(input()) D=[[0]*n for _ in range(18)] for i in range(n): D[0][i]=bisect_right(X,X[i]+l)-1 for k in range(1,18): for i in range(n): D[k][i]=D[k-1][D[k-1][i]] def query(a,b): ret=0 while True: for k in range(18): if D[k][a]>=b: break if k==0: ret+=1 return ret k-=1 ret+=pow(2,k) a=D[k][a] for _ in range(q): a,b=map(int,input().split()) if a>b: a,b=b,a a-=1; b-=1 print(query(a,b)) ``` Yes
105,170
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` N = int(input()) X = list(map(int, input().split(' '))) L = int(input()) Q = int(input()) queries = [tuple(map(lambda s: int(s)-1, input().split(' '))) for _ in range(Q)] j = 0 for i in range(N): bound = X[i] + L try: while X[j] <= bound: j += 1 except IndexError: X[i:] = [N-1]*(N-i) break X[i] = j-1 for a,b in queries: if a > b: a,b = b,a cnt = 0 while a < b: a = X[a] cnt += 1 print(cnt) ``` No
105,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n = int(input()) x = list(map(int, input().split())) l = int(input()) q = int(input()) ps = [None]*n # ps2 = [None]*n j = 0 for i in range(n): if j<i: j = i while j+1<n and x[j+1]-x[i]<=l: j += 1 ps[i] = j # ダブリング def double(ps): # global: n=psのサイズ k = 0 n = len(ps) while pow(2,k)<n: k += 1 prev = [[None]*n for _ in range(k)] # ノードjから2^i個上の上司 for j in range(n): prev[0][j] = ps[j] for i in range(1,k): for j in range(n): p = prev[i-1][j] if p>=0: prev[i][j] = prev[i-1][p] else: prev[i][j] = p return prev dl = double(ps) ans = [None]*q for i in range(q): a,b = map(lambda x: int(x)-1, input().split()) a,b = min(a,b), max(a,b) # print(a,b) res = 0 for k in range(len(dl)-1, -1, -1): if dl[k][a]<b: a = dl[k][a] res += pow(2,k) elif dl[k][a]==b: tmp = res+pow(2,k) break else: tmp = min(tmp, res+pow(2,k)) ans[i] = tmp write("\n".join(map(str, ans))) ``` No
105,172
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` from itertools import count from bisect import bisect_right N = int(input()) X = list(map(int, input().split())) L = int(input()) Q = int(input()) queries = [tuple(map(lambda s: int(s)-1, input().split())) for _ in range(Q)] j = 0 n = 0 for i in range(N): bound = X[i] + L try: while X[j] <= bound: j += 1 except IndexError: X[i:] = [[] for _ in range(N-i)] n = i break X[i] = [j-1] for d in count(0): for i in range(n): t = X[i][d] if t < n: X[i] += [X[t][d]] else: n = i break if n == 0: break for a,b in queries: if a > b: a,b = b,a p = bisect_right(X[a], b) - 1 cnt = 0 while p >= 0: cnt += 2**p a = X[a][p] p = bisect_right(X[a], b) - 1 if a < b: cnt += 1 print(cnt) ``` No
105,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i. Tak the traveler has the following two personal principles: * He never travels a distance of more than L in a single day. * He never sleeps in the open. That is, he must stay at a hotel at the end of a day. You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j. For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles. It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input. Constraints * 2 \leq N \leq 10^5 * 1 \leq L \leq 10^9 * 1 \leq Q \leq 10^5 * 1 \leq x_i < x_2 < ... < x_N \leq 10^9 * x_{i+1} - x_i \leq L * 1 \leq a_j,b_j \leq N * a_j \neq b_j * N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N L Q a_1 b_1 a_2 b_2 : a_Q b_Q Output Print Q lines. The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel. Example Input 9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 Output 4 2 1 2 Submitted Solution: ``` import os import sys import numpy as np if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 N = int(sys.stdin.readline()) X = list(map(int, sys.stdin.readline().split())) L = int(sys.stdin.readline()) Q = int(sys.stdin.readline()) AB = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)] N += 2 X = [X[0]] + X + [X[-1]] X = np.array(X, dtype=int) # rdp[d][i]: i番のホテルから(2^d)日で何個右までいけるか rdp = np.zeros((N.bit_length(), N), dtype=int) rdp[0] = np.searchsorted(X, X + L, side='right') - np.arange(N) - 1 for d in range(1, N.bit_length()): rdp[d] = rdp[d - 1] + rdp[d - 1][np.arange(N) + rdp[d - 1]] X = -X[::-1] # ldp[d][i]: i番のホテルから(2^d)日で何個左までいけるか ldp = np.zeros((N.bit_length(), N), dtype=int) ldp[0] = np.searchsorted(X, X + L, side='right') - np.arange(N) - 1 for d in range(1, N.bit_length()): ldp[d] = ldp[d - 1] + ldp[d - 1][np.arange(N) + ldp[d - 1]] # print(rdp) # print(ldp) def solver(l, r): ret = 0 while l < r: d = np.searchsorted(rdp[:, l], r - l) if rdp[d, l] == r - l: ret += 2 ** d break if d > 0: d -= 1 ret += 2 ** d l += rdp[d, l] return ret def solvel(l, r): r = N - r - 1 l = N - l - 1 ret = 0 while l < r: d = np.searchsorted(ldp[:, l], r - l) if ldp[d, l] == r - l: ret += 2 ** d break if d > 0: d -= 1 ret += 2 ** d l += ldp[d, l] return ret # print(np.searchsorted([1, 2, 5, 8, 9], 3)) # print(np.searchsorted([1, 2, 5, 8, 9], 4)) # print(np.searchsorted([1, 2, 5, 8, 9], 5)) # print(np.searchsorted([1, 2, 5, 8, 9], 6)) # print(np.searchsorted([1, 2, 5, 8, 9], 7)) # print() for a, b in AB: if a < b: r = solver(a, b) else: r = solvel(a, b) print(r) ``` No
105,174
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` import re for _ in [0]*int(input()): print(re.sub(r"Hoshino", "Hoshina", input())) ```
105,175
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` num_data = int(input()) correct_name = "Hoshina" wrong_name = "Hoshino" for _ in range(num_data): print(input().replace(wrong_name,correct_name)) ```
105,176
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` num = int(input()) for i in range(num): sent = input() ans = sent.replace("Hoshino","Hoshina") print(ans) ```
105,177
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` input() try: while True: str = input() print(str.replace("Hoshino","Hoshina")) except EOFError: pass ```
105,178
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` n = int(input()) for i in range(n): data = list(input()) for a in range(len(data)): if a>=6: if data[a-6] == "H" and data[a-5]=="o" and data[a-4]=="s" and data[a-3]=="h" and data[a-2]=="i" and data[a-1]=="n" and data[a]=="o" : data[a] = "a" new = "".join(data) print(new) ```
105,179
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` n = int(input()) for i in range(n): sentence = input() true_sentence = sentence.replace("Hoshino", "Hoshina") print(true_sentence) ```
105,180
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` words=[] num=int(input()) for i in range(num): words.append(i) words[i]=input().replace('Hoshino','Hoshina') for i in range(num): print(words[i]) ```
105,181
Provide a correct Python 3 solution for this coding contest problem. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. "Correct Solution: ``` [print(input().replace("Hoshino","Hoshina")) for i in range(int(input()))] ```
105,182
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` n = int(input()) D = [] for i in range(n): a=str( input()) D.append(a) D_replace=[s.replace('Hoshino' , 'Hoshina') for s in D] print(D_replace[0]) D=[] ``` Yes
105,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` n = int(input()) dataset = [input().replace("Hoshino", "Hoshina") for _ in range(n)] print(*dataset, sep="\n") ``` Yes
105,184
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` # coding=utf-8 ### ### for python program ### import sys import math # math class class mymath: ### pi pi = 3.14159265358979323846264338 ### Prime Number def pnum_eratosthenes(self, n): ptable = [0 for i in range(n+1)] plist = [] for i in range(2, n+1): if ptable[i]==0: plist.append(i) for j in range(i+i, n+1, i): ptable[j] = 1 return plist ### GCD def gcd(self, a, b): if b == 0: return a return self.gcd(b, a%b) ### LCM def lcm(self, a, b): return (a*b)//self.gcd(a,b) ### Mat Multiplication def mul(self, A, B): ans = [] for a in A: c = 0 for j, row in enumerate(a): c += row*B[j] ans.append(c) return ans mymath = mymath() ### output class class output: ### list def list(self, l): l = list(l) #print(" ", end="") for i, num in enumerate(l): print(num, end="") if i != len(l)-1: print(" ", end="") print() output = output() ### input sample #i = input() #N = int(input()) #A, B, C = [x for x in input().split()] #N, K = [int(x) for x in input().split()] #inlist = [int(w) for w in input().split()] #R = float(input()) #A.append(list(map(int,input().split()))) #for line in sys.stdin.readlines(): # x, y = [int(temp) for temp in line.split()] #abc list #abc = [chr(ord('a') + i) for i in range(26)] ### output sample # print("{0} {1} {2:.5f}".format(A//B, A%B, A/B)) # print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi)) # print(" {}".format(i), end="") # def printA(A): # for i, n in enumerate(A): # print(n, end='') # if i != N-1: # print(' ', end='') # print() # リスト内包表記 ifあり # [x-k if x != 0 else x for x in C] # ソート # N = N.sort() # 10000個の素数リスト # P = mymath.pnum_eratosthenes(105000) def get_input(): N = [] while True: try: N.append(input()) #N.append(int(input())) #N.append(float(input())) except EOFError: break return N def is_integer(n): try: float(n) except ValueError: return False else: return float(n).is_integer() def dist(A, B): d = 0 for i in range(len(A)): d += (A[i]-B[i])**2 d = d**(1/2) return d N = int(input()) ans = [] for i in range(N): s = input() ans.append(s.replace('Hoshino', 'Hoshina')) for s in ans: print(s) ``` Yes
105,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` num = int(input()) for i in range(num): box = input() print(box.replace("Hoshino", "Hoshina")) ``` Yes
105,186
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` for i in range(int(input())): ls = list(input().split()) for s in ls: if s == "Hoshino": ls[ls.index(s)] = "Hoshina" print(*ls) ``` No
105,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` # coding:utf-8 inputCount = int(input()) while 0 < inputCount: content = input().split(" ") for index in range(0, len(content)): if content[index] == "Hoshino": content[index] = "Hoshina" print(" ".join(content)) inputCount -= 1 ``` No
105,188
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` N = int(input()) ans = "" for i in range(N): inp = list(map(str, input().split())) tmp = "" for j in range(len(inp)): if inp[j] == "Hoshino": inp[j] = "Hoshina" tmp += inp[j] + " " if i != N-1: ans += tmp + "\n" else: ans += tmp print(ans) ``` No
105,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Example Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa. Submitted Solution: ``` n = int(input()) for i in range(n): l = [s for s in input().split()] for s in l: if 'Hoshino' in s: l[l.index(s)] = s.replace('Hoshino','Hoshina') a = '' for s in l: a += s a += ' ' a.rstrip() print(a) ``` No
105,190
Provide a correct Python 3 solution for this coding contest problem. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 "Correct Solution: ``` import sys sys.setrecursionlimit(1000000) INF = 10 ** 20 def update_state(state, newx): tmp = list(state) tmp[newx] = 1 return tuple(tmp) def get_co(x, y): dc = do = 0 score = mp[y][x] if score < 0: dc = -score else: do = score return dc, do def minimum_cost(x, y, state, ox, goal, dic, w, m): if (x, y, state, ox) in dic: return dic[(x, y, state, ox)] if y == goal: return 0 if ox <= 1: return INF ret = INF if x >= 1: if state[x - 1] == 0: dc, do = get_co(x - 1, y) ret = min(ret, minimum_cost(x - 1, y, update_state(state, x - 1), min(ox + do - 1, m), goal, dic, w, m) + dc) else: ret = min(ret, minimum_cost(x - 1, y, state, ox - 1, goal, dic, w, m)) if x < w - 1: if state[x + 1] == 0: dc, do = get_co(x + 1, y) ret = min(ret, minimum_cost(x + 1, y, update_state(state, x + 1), min(ox + do - 1, m), goal, dic, w, m) + dc) else: ret = min(ret, minimum_cost(x + 1, y, state, ox - 1, goal, dic, w, m)) dc, do = get_co(x, y + 1) ret = min(ret, minimum_cost(x, y + 1, tuple((1 if i == x else 0 for i in range(w))), min(ox + do - 1, m), goal, dic, w, m) + dc) dic[(x, y, state, ox)] = ret return ret while True: w, h = map(int, input().split()) if w == 0: break f, m, o = map(int, input().split()) mp = [list(map(int, input().split())) for _ in range(h)] if o <= 1: print("NA") continue dic = {} ans = INF for i in range(w): dc, do = get_co(i, 0) state = tuple(1 if i == j else 0 for j in range(w)) ans = min(ans, minimum_cost(i, 0, state, min(o + do - 1, m), h - 1, dic, w, m) + dc) if ans > f: print("NA") else: print(ans) ```
105,191
Provide a correct Python 3 solution for this coding contest problem. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 "Correct Solution: ``` import queue di = [0, 1, 0, -1] dj = [1, 0, -1, 0] while True: w,h = map(int,input().split()) if w == 0: break f, m, o = map(int,input().split()) c = [list(map(int,input().split())) for _ in range(h)] d = [[[1e9]*w for x in range(h)] for y in range(m+1)] que = queue.PriorityQueue() for j in range(w): d[o-1][0][j] = -min(0,c[0][j]) que.put((-min(0,c[0][j]),o-1,0,j)) while not que.empty(): p = que.get() ox = p[1] i = p[2] j = p[3] if d[ox][i][j] < p[0] or ox <= 1: continue for k in range(4): ni = i+di[k] nj = j+dj[k] if ni<0 or ni>=h or nj<0 or nj>=w: continue if c[ni][nj] < 0: if d[ox-1][ni][nj] > d[ox][i][j] - c[ni][nj]: d[ox-1][ni][nj] = d[ox][i][j] - c[ni][nj] que.put((d[ox-1][ni][nj],ox-1,ni,nj)) else: nox = min(ox-1+c[ni][nj],m) if d[nox][ni][nj] > d[ox][i][j]: d[nox][ni][nj] = d[ox][i][j]+0 que.put((d[nox][ni][nj],nox,ni,nj)) ans = 1e9 for j in range(w): for ox in range(1,m+1): ans = min(ans, d[ox][h-1][j]) if ans <= f: print(ans) else: print('NA') ```
105,192
Provide a correct Python 3 solution for this coding contest problem. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 "Correct Solution: ``` INF = 10 ** 20 def update_state(state, newx): tmp = list(state) tmp[newx] = 1 return tuple(tmp) def get_co(x, y): dc = do = 0 score = mp[y][x] if score < 0: dc = -score else: do = score return dc, do def minimum_cost(x, y, state, ox, goal, dic, w, m): if (x, y, state, ox) in dic: return dic[(x, y, state, ox)] if y == goal: return 0 if ox <= 1: return INF ret = INF if x >= 1: if state[x - 1] == 0: dc, do = get_co(x - 1, y) ret = min(ret, minimum_cost(x - 1, y, update_state(state, x - 1), min(ox + do - 1, m), goal, dic, w, m) + dc) else: ret = min(ret, minimum_cost(x - 1, y, state, ox - 1, goal, dic, w, m)) if x < w - 1: if state[x + 1] == 0: dc, do = get_co(x + 1, y) ret = min(ret, minimum_cost(x + 1, y, update_state(state, x + 1), min(ox + do - 1, m), goal, dic, w, m) + dc) else: ret = min(ret, minimum_cost(x + 1, y, state, ox - 1, goal, dic, w, m)) dc, do = get_co(x, y + 1) ret = min(ret, minimum_cost(x, y + 1, tuple((1 if i == x else 0 for i in range(w))), min(ox + do - 1, m), goal, dic, w, m) + dc) dic[(x, y, state, ox)] = ret return ret while True: w, h = map(int, input().split()) if w == 0: break f, m, o = map(int, input().split()) mp = [list(map(int, input().split())) for _ in range(h)] if o <= 1: print("NA") continue dic = {} ans = INF for i in range(w): dc, do = get_co(i, 0) state = tuple(1 if i == j else 0 for j in range(w)) ans = min(ans, minimum_cost(i, 0, state, min(o + do - 1, m), h - 1, dic, w, m) + dc) if ans > f: print("NA") else: print(ans) ```
105,193
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 Submitted Solution: ``` INF = 10 ** 20 def update_state(state, newx): tmp = list(state) tmp[newx] = 1 return tuple(tmp) def get_co(x, y): dc = do = 0 score = mp[y][x] if score < 0: dc = -score else: do = score return dc, do def minimum_cost(nowx, nowy, state, ox, goal, dic, w, m): if (nowx, nowy, state, ox) in dic: return dic[(nowx, nowy, state, ox)] if nowy == goal: return 0 if ox <= 1: return INF ret = INF left = right = None for i in range(nowx + 1, w): if state[i] == 0: right = i break for i in range(nowx - 1, -1, -1): if state[i] == 0: left = i break if left != None: dc, do = get_co(left, nowy) ret = min(ret, minimum_cost(left, nowy, update_state(state, left), min(ox + do - 1, m), goal, dic, w, m) + dc) for i in range(left + 1, nowx): ret = min(ret, minimum_cost(i, nowy, state, ox - 1, goal, dic, w, m)) if right != None: dc, do = get_co(right, nowy) ret = min(ret, minimum_cost(right, nowy, update_state(state, right), min(ox + do - 1, m), goal, dic, w, m) + dc) for i in range(nowx + 1, right): ret = min(ret, minimum_cost(i, nowy, state, ox - 1, goal, dic, w, m)) dc, do = get_co(nowx, nowy + 1) ret = min(ret, minimum_cost(nowx, nowy + 1, tuple((1 if i == nowx else 0 for i in range(w))), min(ox + do - 1, m), goal, dic, w, m) + dc) dic[(nowx, nowy, state, ox)] = ret return ret while True: w, h = map(int, input().split()) if w == 0: break f, m, o = map(int, input().split()) if o <= 1: print("NA") continue mp = [list(map(int, input().split())) for _ in range(h)] dic = {} ans = INF for i in range(w): dc, do = get_co(i, 0) ans = min(ans, minimum_cost(i, 0, tuple(1 if i == j else 0 for j in range(w)), min(o + do - 1, m), h - 1, dic, w, m) + dc) if ans > f: print("NA") else: print(ans) ``` No
105,194
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 Submitted Solution: ``` import sys sys.setrecursionlimit(1000000) INF = 10 ** 20 def update_state(state, newx): tmp = list(state) tmp[newx] = 1 return tuple(tmp) def get_co(x, y): dc = do = 0 score = mp[y][x] if score < 0: dc = -score else: do = score return dc, do def minimum_cost(nowx, nowy, state, ox, goal, dic, w, m): if (nowx, nowy, state, ox) in dic: return dic[(nowx, nowy, state, ox)] if nowy == goal: return 0 if ox <= 1: return INF ret = INF left = right = None for i in range(nowx + 1, w): if state[i] == 0: right = i break for i in range(nowx - 1, -1, -1): if state[i] == 0: left = i break if left != None: dc, do = get_co(left, nowy) ret = min(ret, minimum_cost(left, nowy, update_state(state, left), min(ox + do - 1, m), goal, dic, w, m) + dc) for i in range(left + 1, nowx): ret = min(ret, minimum_cost(i, nowy, state, ox - 1, goal, dic, w, m)) if right != None: dc, do = get_co(right, nowy) ret = min(ret, minimum_cost(right, nowy, update_state(state, right), min(ox + do - 1, m), goal, dic, w, m) + dc) for i in range(nowx + 1, right): ret = min(ret, minimum_cost(i, nowy, state, ox - 1, goal, dic, w, m)) dc, do = get_co(nowx, nowy + 1) ret = min(ret, minimum_cost(nowx, nowy + 1, tuple((1 if i == nowx else 0 for i in range(w))), min(ox + do - 1, m), goal, dic, w, m) + dc) dic[(nowx, nowy, state, ox)] = ret return ret while True: w, h = map(int, input().split()) if w == 0: break f, m, o = map(int, input().split()) if o <= 1: print("NA") continue mp = [list(map(int, input().split())) for _ in range(h)] dic = {} ans = INF for i in range(w): dc, do = get_co(i, 0) ans = min(ans, minimum_cost(i, 0, tuple(1 if i == j else 0 for j in range(w)), min(o + do - 1, m), h - 1, dic, w, m) + dc) if ans > f: print("NA") else: print(ans) ``` No
105,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 Submitted Solution: ``` import sys sys.setrecursionlimit(1000000) INF = 10 ** 20 def update_state(state, newx): tmp = list(state) tmp[newx] = 1 return tuple(tmp) def get_co(x, y): dc = do = 0 score = mp[y][x] if score < 0: dc = -score else: do = score return dc, do def minimum_cost(x, y, state, ox, goal, dic, w, m): if (x, y, state, ox) in dic: return dic[(x, y, state, ox)] if y == goal: return 0 if ox <= 1: return INF ret = INF if x >= 1: if state[x - 1] == 0: dc, do = get_co(x - 1, y) ret = min(ret, minimum_cost(x - 1, y, update_state(state, x - 1), min(ox + do - 1, m), goal, dic, w, m) + dc) else: ret = min(ret, minimum_cost(x - 1, y, state, ox - 1, goal, dic, w, m)) if x < w - 1: if state[x + 1] == 0: dc, do = get_co(x + 1, y) ret = min(ret, minimum_cost(x + 1, y, update_state(state, x + 1), min(ox + do - 1, m), goal, dic, w, m) + dc) else: ret = min(ret, minimum_cost(x + 1, y, state, ox - 1, goal, dic, w, m)) """ left = right = None for i in range(x + 1, w): if state[i] == 0: right = i break for i in range(x - 1, -1, -1): if state[i] == 0: left = i break if left != None: dc, do = get_co(left, y) ret = min(ret, minimum_cost(left, y, update_state(state, left), min(ox + do - 1, m), goal, dic, w, m) + dc) for i in range(left + 1, x): ret = min(ret, minimum_cost(i, y, state, ox - (x - i), goal, dic, w, m)) if right != None: dc, do = get_co(right, y) ret = min(ret, minimum_cost(right, y, update_state(state, right), min(ox + do - 1, m), goal, dic, w, m) + dc) for i in range(x + 1, right): ret = min(ret, minimum_cost(i, y, state, ox - (i - x), goal, dic, w, m)) """ dc, do = get_co(x, y + 1) ret = min(ret, minimum_cost(x, y + 1, tuple((1 if i == x else 0 for i in range(w))), min(ox + do - 1, m), goal, dic, w, m) + dc) dic[(x, y, state, ox)] = ret return ret while True: w, h = map(int, input().split()) if w == 0: break f, m, o = map(int, input().split()) if o <= 1: print("NA") continue mp = [list(map(int, input().split())) for _ in range(h)] dic = {} ans = INF for i in range(w): dc, do = get_co(i, 0) state = tuple(1 if i == j else 0 for j in range(w)) ans = min(ans, minimum_cost(i, 0, state, min(o + do - 1, m), h - 1, dic, w, m) + dc) if ans > f: print("NA") else: print(ans) ``` No
105,196
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Aizu has an ancient legend of buried treasure. You have finally found the place where the buried treasure is buried. Since we know the depth of the buried treasure and the condition of the strata to be dug, we can reach the buried treasure at the lowest cost with careful planning. So you decided to create a program that reads the condition of the formation and calculates the route to reach the buried treasure depth at the lowest cost. The state of the formation is represented by cells arranged in a two-dimensional grid, and the position of each cell is represented by the coordinates (x, y). Let the upper left be (1,1), and assume that the x coordinate increases as it goes to the right and the y coordinate increases as it goes deeper down. You choose one of the cells with the smallest y-coordinate and start digging from there, then dig into one of the cells with the largest y-coordinate. There are two types of cells in the formation: 1. A cell filled with soil. There is a fixed cost for each cell to dig. 2. Oxygen-filled cell. There is no need to dig, and each cell can be replenished with a fixed amount of oxygen. The oxygen in the cell that has been replenished with oxygen is exhausted and cannot be replenished again. Also, when you reach this cell, you must replenish oxygen. Only left, right, and down cells can be dug from a cell. Once you have dug a cell, you can move it left or right, but you cannot move it up. You must carry an oxygen cylinder with you when excavating. The moment the oxygen cylinder reaches zero, you will not be able to move, excavate, or replenish oxygen. The remaining amount is decremented by 1 each time you move the cell. Even if the remaining amount of the oxygen cylinder is 0 and the depth of the buried treasure is reached, it is not considered to have been reached. In addition, oxygen can be replenished in cells that have accumulated oxygen, but the excess capacity is discarded. Create a program that inputs the size of the formation, the excavation cost, the capacity of the oxygen cylinder, the amount of oxygen in the initial state, and the information of the formation, and outputs the minimum cost to reach the deepest cell. However, if the minimum cost exceeds the excavation cost, or if you cannot reach the buried treasure no matter how you dig, please output "NA". <image> Input A sequence of multiple datasets is given as input. The end of the input is indicated by two zero lines. Each dataset is given in the following format. W H f m o c1,1 c2,1 ... cW,1 c1,2 c2,2 ... cW,2 ... c1, H c2, H ... cW, H The horizontal size W of the formation and the vertical size H (3 ≤ W, H ≤ 10) are given in the first line. The second line is the integer f (1 ≤ f ≤ 10000) that represents your excavation cost, the integer m (3 ≤ m ≤ 50) that represents the capacity of the oxygen cylinder, and the integer o that represents the amount of oxygen you have in the initial state. o ≤ m) is given. The following H line is given the geological information ci, j. ci, j represents the cell information for coordinates (i, j) and is given in the following format: If the value is negative, the cell is full of soil and the value represents the cost. If the value is positive, it is a cell filled with oxygen, and the value represents the amount of oxygen. However, there are no more than 50 cells in which oxygen has accumulated. The number of datasets does not exceed 50. Output Print the minimum cost or NA on one line for each dataset. Example Input 3 3 100 10 10 -100 -20 -100 -100 -20 -100 -100 -20 -100 3 3 100 10 10 -100 -20 -100 -100 -20 -20 -100 -60 -20 3 3 100 10 3 -100 -20 -100 -20 -20 -20 -20 -100 -20 3 3 100 3 3 -100 -20 -30 -100 -20 2 -100 -20 -20 4 5 1500 5 4 -10 -380 -250 -250 -90 2 -80 8 -250 -130 -330 -120 -120 -40 -50 -20 -250 -10 -20 -150 0 0 Output 60 80 NA 50 390 Submitted Solution: ``` from collections import deque from copy import deepcopy def bfs(inicost, inioxy, inif, m, inio, H): q = deque() anss = [] for i in range(W): cost = deepcopy(inicost) oxy = deepcopy(inioxy) nf = inif + cost[0][i] no = min(inio - 1 + oxy[0][i], m) if nf > 0 and no > 0: cost[0][i] = 0 oxy[0][i] = 0 q.append([0, i, nf, no, cost, oxy, {(0, i): (nf, no)}]) while q: y, x, f, o, cost, oxy, d = q.popleft() if y == H - 1: anss.append(inif - f) continue for dy, dx in [(0, -1), (0, 1), (1, 0)]: ny = y + dy nx = x + dx ncost = deepcopy(cost) noxy = deepcopy(oxy) nd = deepcopy(d) if ny in range(H) and nx in range(W): nf = f + ncost[ny][nx] no = min(o - 1 + noxy[ny][nx], m) if nf > 0 and no > 0 and ((ny, nx) not in nd or nf > nd[ny, nx][0] or no > nd[ny, nx][1]): ncost[ny][nx] = 0 noxy[ny][nx] = 0 nd[ny, nx] = (nf, no) q.append([ny, nx, nf, no, ncost, noxy, nd]) if len(anss) == 0: return 'NA' else: return min(anss) if __name__ == '__main__': while True: W, H = map(int, input().split()) if W == 0: break f, m, o = map(int, input().split()) cost = [] for _ in range(H): l = list(map(int, input().split())) cost.append(l) oxy = deepcopy(cost) for i in range(H): for j in range(W): if cost[i][j] > 0: cost[i][j] = 0 else: oxy[i][j] = 0 print(bfs(cost, oxy, f, m, o, H)) ``` No
105,197
Provide a correct Python 3 solution for this coding contest problem. Playing with Stones Koshiro and Ukiko are playing a game with black and white stones. The rules of the game are as follows: 1. Before starting the game, they define some small areas and place "one or more black stones and one or more white stones" in each of the areas. 2. Koshiro and Ukiko alternately select an area and perform one of the following operations. (a) Remove a white stone from the area (b) Remove one or more black stones from the area. Note, however, that the number of the black stones must be less than or equal to white ones in the area. (c) Pick up a white stone from the stone pod and replace it with a black stone. There are plenty of white stones in the pod so that there will be no shortage during the game. 3. If either Koshiro or Ukiko cannot perform 2 anymore, he/she loses. They played the game several times, with Koshiro’s first move and Ukiko’s second move, and felt the winner was determined at the onset of the game. So, they tried to calculate the winner assuming both players take optimum actions. Given the initial allocation of black and white stones in each area, make a program to determine which will win assuming both players take optimum actions. Input The input is given in the following format. $N$ $w_1$ $b_1$ $w_2$ $b_2$ : $w_N$ $b_N$ The first line provides the number of areas $N$ ($1 \leq N \leq 10000$). Each of the subsequent $N$ lines provides the number of white stones $w_i$ and black stones $b_i$ ($1 \leq w_i, b_i \leq 100$) in the $i$-th area. Output Output 0 if Koshiro wins and 1 if Ukiko wins. Examples Input 4 24 99 15 68 12 90 95 79 Output 0 Input 3 2 46 94 8 46 57 Output 1 "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): g = defaultdict(lambda : None) g[(0,0)] = 0 def grundy(w,b): if g[(w,b)] != None: return g[(w,b)] s = set() if w: s.add(grundy(w-1,b)) if b: s.add(grundy(w+1,b-1)) for i in range(1,min(w,b)+1): s.add(grundy(w,b-i)) i = 0 while i in s: i += 1 g[(w,b)] = i return i n = I() ans = 0 for _ in range(n): w,b = LI() ans ^= grundy(w,b) print(0 if ans else 1) return #Solve if __name__ == "__main__": solve() ```
105,198
Provide a correct Python 3 solution for this coding contest problem. You are working for an administration office of the International Center for Picassonian Cubism (ICPC), which plans to build a new art gallery for young artists. The center is organizing an architectural design competition to find the best design for the new building. Submitted designs will look like a screenshot of a well known game as shown below. <image> This is because the center specifies that the building should be constructed by stacking regular units called pieces, each of which consists of four cubic blocks. In addition, the center requires the competitors to submit designs that satisfy the following restrictions. * All the pieces are aligned. When two pieces touch each other, the faces of the touching blocks must be placed exactly at the same position. * All the pieces are stable. Since pieces are merely stacked, their centers of masses must be carefully positioned so as not to collapse. * The pieces are stacked in a tree-like form in order to symbolize boundless potentiality of young artists. In other words, one and only one piece touches the ground, and each of other pieces touches one and only one piece on its bottom faces. * The building has a flat shape. This is because the construction site is a narrow area located between a straight moat and an expressway where no more than one block can be placed between the moat and the expressway as illustrated below. <image> It will take many days to fully check stability of designs as it requires complicated structural calculation. Therefore, you are asked to quickly check obviously unstable designs by focusing on centers of masses. The rules of your quick check are as follows. Assume the center of mass of a block is located at the center of the block, and all blocks have the same weight. We denote a location of a block by xy-coordinates of its left-bottom corner. The unit length is the edge length of a block. Among the blocks of the piece that touch another piece or the ground on their bottom faces, let xL be the leftmost x-coordinate of the leftmost block, and let xR be the rightmost x-coordinate of the rightmost block. Let the x-coordinate of its accumulated center of mass of the piece be M, where the accumulated center of mass of a piece P is the center of the mass of the pieces that are directly and indirectly supported by P, in addition to P itself. Then the piece is stable, if and only if xL < M < xR. Otherwise, it is unstable. A design of a building is unstable if any of its pieces is unstable. Note that the above rules could judge some designs to be unstable even if it would not collapse in reality. For example, the left one of the following designs shall be judged to be unstable. <image> | | <image> ---|---|--- Also note that the rules judge boundary cases to be unstable. For example, the top piece in the above right design has its center of mass exactly above the right end of the bottom piece. This shall be judged to be unstable. Write a program that judges stability of each design based on the above quick check rules. Input The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset, which represents a front view of a building, is formatted as follows. > w h > p0(h-1)p1(h-1)...p(w-1)(h-1) > ... > p01p11...p(w-1)1 > p00p10...p(w-1)0 > The integers w and h separated by a space are the numbers of columns and rows of the layout, respectively. You may assume 1 ≤ w ≤ 10 and 1 ≤ h ≤ 60. The next h lines specify the placement of the pieces. The character pxy indicates the status of a block at (x,y), either ``.`', meaning empty, or one digit character between ``1`' and ``9`', inclusive, meaning a block of a piece. (As mentioned earlier, we denote a location of a block by xy-coordinates of its left-bottom corner.) When two blocks with the same number touch each other by any of their top, bottom, left or right face, those blocks are of the same piece. (Note that there might be two different pieces that are denoted by the same number.) The bottom of a block at (x,0) touches the ground. You may assume that the pieces in each dataset are stacked in a tree-like form. Output For each dataset, output a line containing a word `STABLE` when the design is stable with respect to the above quick check rules. Otherwise, output `UNSTABLE`. The output should be written with uppercase letters, and should not contain any other extra characters. Sample Input 4 5 ..33 ..33 2222 ..1. .111 5 7 ....1 .1..1 .1..1 11..1 .2222 ..111 ...1. 3 6 .3. 233 23. 22. .11 .11 4 3 2222 ..11 ..11 4 5 .3.. 33.. 322. 2211 .11. 3 3 222 2.1 111 3 4 11. 11. .2. 222 3 4 .11 .11 .2. 222 2 7 11 .1 21 22 23 .3 33 2 3 1. 11 .1 0 0 Output for the Sample Input STABLE STABLE STABLE UNSTABLE STABLE STABLE UNSTABLE UNSTABLE UNSTABLE UNSTABLE Example Input 4 5 ..33 ..33 2222 ..1. .111 5 7 ....1 .1..1 .1..1 11..1 .2222 ..111 ...1. 3 6 .3. 233 23. 22. .11 .11 4 3 2222 ..11 ..11 4 5 .3.. 33.. 322. 2211 .11. 3 3 222 2.1 111 3 4 11. 11. .2. 222 3 4 .11 .11 .2. 222 2 7 11 .1 21 22 23 .3 33 2 3 1. 11 .1 0 0 Output STABLE STABLE STABLE UNSTABLE STABLE STABLE UNSTABLE UNSTABLE UNSTABLE UNSTABLE "Correct Solution: ``` #2006_D """ import sys from collections import defaultdict def dfs(d,y,x,f): global ans if d >= 10: return f_ = defaultdict(int) for i in f.keys(): f_[i] = f[i] for t,s in vr[(y,x)]: if a[t][s] == 3: ans = min(ans,d+1) break elif f[(t,s)]: if s == x+1: break f_[(t,s)] = 0 dfs(d+1,t,s-1,f_) f_[(t,s)] = 1 break for t,s in vl[(y,x)]: if a[t][s] == 3: ans = min(ans,d+1) break elif f[(t,s)]: if s == x-1: break f_[(t,s)] = 0 dfs(d+1,t,s+1,f_) f_[(t,s)] = 1 break for t,s in vd[(y,x)]: if a[t][s] == 3: ans = min(ans,d+1) break elif f[(t,s)]: if t == y+1: break f_[(t,s)] = 0 dfs(d+1,t-1,s,f_) f_[(t,s)] = 1 break for t,s in vu[(y,x)]: if a[t][s] == 3: ans = min(ans,d+1) break elif f[(t,s)]: if t == y-1: break f_[(t,s)] = 0 dfs(d+1,t+1,s,f_) f_[(t,s)] = 1 break return while 1: w,h = map(int, sys.stdin.readline()[:-1].split()) if w == h == 0: break a = [list(map(int, sys.stdin.readline()[:-1].split())) for i in range(h)] vr = defaultdict(list) vl = defaultdict(list) vd = defaultdict(list) vu = defaultdict(list) f = defaultdict(int) for y in range(h): for x in range(w): if a[y][x] == 1: f[(y,x)] = 1 if a[y][x] in [1,3]: for x_ in range(x): vr[(y,x_)].append((y,x)) elif a[y][x] == 2: sy,sx = y,x for y in range(h): for x in range(w)[::-1]: if a[y][x] in (1,3): for x_ in range(x+1,w): vl[(y,x_)].append((y,x)) for x in range(w): for y in range(h): if a[y][x] in (1,3): for y_ in range(y): vd[(y_,x)].append((y,x)) for x in range(w): for y in range(h)[::-1]: if a[y][x] in (1,3): for y_ in range(y+1,h): vu[(y_,x)].append((y,x)) ind = [[[0]*4 for i in range(w)] for j in range(h)] ans = 11 dfs(0,sy,sx,f) ans = ans if ans < 11 else -1 print(ans) """ #2018_D """ import sys from collections import defaultdict sys.setrecursionlimit(1000000) def dfs(d,s,l,v,dic): s_ = tuple(s) if dic[(d,s_)] != None: return dic[(d,s_)] if d == l: dic[(d,s_)] = 1 for x in s: if x > (n>>1): dic[(d,s_)] = 0 return 0 return 1 else: res = 0 i,j = v[d] if s[i] < (n>>1): s[i] += 1 res += dfs(d+1,s,l,v,dic) s[i] -= 1 if s[j] < (n>>1): s[j] += 1 res += dfs(d+1,s,l,v,dic) s[j] -= 1 dic[(d,s_)] = res return res def solve(n): dic = defaultdict(lambda : None) m = int(sys.stdin.readline()) s = [0]*n f = [[1]*n for i in range(n)] for i in range(n): f[i][i] = 0 for i in range(m): x,y = [int(x) for x in sys.stdin.readline().split()] x -= 1 y -= 1 s[x] += 1 f[x][y] = 0 f[y][x] = 0 v = [] for i in range(n): for j in range(i+1,n): if f[i][j]: v.append((i,j)) l = len(v) print(dfs(0,s,l,v,dic)) while 1: n = int(sys.stdin.readline()) if n == 0: break solve(n) """ #2011_D """ import sys def dfs(s,d,f,v): global ans if ans == n-n%2: return if d > ans: ans = d for i in range(n): if s[i] == 0: for j in range(i+1,n): if s[j] == 0: if f[i] == f[j]: s[i] = -1 s[j] = -1 for k in v[i]: s[k] -= 1 for k in v[j]: s[k] -= 1 dfs(s,d+2,f,v) s[i] = 0 s[j] = 0 for k in v[i]: s[k] += 1 for k in v[j]: s[k] += 1 def solve(n): p = [[int(x) for x in sys.stdin.readline().split()] for i in range(n)] v = [[] for i in range(n)] f = [0]*n s = [0]*n for i in range(n): x,y,r,f[i] = p[i] for j in range(i+1,n): xj,yj,rj,c = p[j] if (x-xj)**2+(y-yj)**2 < (r+rj)**2: v[i].append(j) s[j] += 1 dfs(s,0,f,v) print(ans) while 1: n = int(sys.stdin.readline()) ans = 0 if n == 0: break solve(n) """ #2003_D """ import sys def root(x,par): if par[x] == x: return x par[x] = root(par[x],par) return par[x] def unite(x,y,par,rank): x = root(x,par) y = root(y,par) if rank[x] < rank[y]: par[x] = y else: par[y] = x if rank[x] == rank[y]: rank[x] += 1 def solve(n): p = [[float(x) for x in sys.stdin.readline().split()] for i in range(n)] v = [] for i in range(n): for j in range(i): xi,yi,zi,ri = p[i] xj,yj,zj,rj = p[j] d = max(0,((xi-xj)**2+(yi-yj)**2+(zi-zj)**2)**0.5-(ri+rj)) v.append((i,j,d)) par = [i for i in range(n)] rank = [0]*n v.sort(key = lambda x:x[2]) ans = 0 for x,y,d in v: if root(x,par) != root(y,par): unite(x,y,par,rank) ans += d print("{:.3f}".format(round(ans,3))) while 1: n = int(sys.stdin.readline()) if n == 0: break solve(n) """ #2009_D """ import sys from heapq import heappop,heappush from collections import defaultdict def solve(n,m): s,g = [int(x) for x in sys.stdin.readline().split()] s -= 1 g -= 1 e = [[] for i in range(n)] for i in range(m): a,b,d,c = [int(x) for x in sys.stdin.readline().split()] a -= 1 b -= 1 e[a].append((b,d,c)) e[b].append((a,d,c)) dist = defaultdict(lambda : float("inf")) dist[(s,0,-1)] = 0 q = [(0,s,0,-1)] while q: dx,x,v,p = heappop(q) if x == g and v == 1: print(dx) return for i in range(-1,2): v_ = v+i if v_ < 1 :continue for y,d,c in e[x]: if p == y: continue if v_ > c: continue z = d/v_ if dx+z < dist[(y,v_,x)]: dist[(y,v_,x)] = dx+z heappush(q,(dist[(y,v_,x)],y,v_,x)) print("unreachable") return while 1: n,m = [int(x) for x in sys.stdin.readline().split()] if n == 0: break solve(n,m) """ #2016_D """ import sys def solve(n): w = [int(x) for x in sys.stdin.readline().split()] dp = [[0]*(n+1) for i in range(n+1)] for le in range(n+1): for l in range(n): r = l+le if r > n:break if not (r-l)%2 and abs(w[l]-w[r-1]) < 2: if dp[l+1][r-1] == r-l-2: dp[l][r] = r-l continue for k in range(l+1,r): if dp[l][k] + dp[k][r] > dp[l][r]: dp[l][r] = dp[l][k] + dp[k][r] print(dp[0][n]) while 1: n = int(sys.stdin.readline()) if not n: break solve(n) """ #2009_D """ alp = list("abcdefghijklmnopqrstuvwxyz") c = {} key = {} for i in range(25): c[alp[i]] = alp[i+1] key[alp[i]] = i key["z"] = 25 def dfs(i,k,f,n): global ans if i == n: for j in k: if f[key[j]]: break else: ans.append(k) else: dfs(i+1,k+s[i],f,n) if s[i] != "z" and f[key[s[i]]+1]: f[key[s[i]]+1] = 0 dfs(i+1,k+c[s[i]],f,n) f[key[s[i]]+1] = 1 def solve(s): global ans n = len(s) d = {} for i in s: d[i] = 1 f = [1]*26 f[0] = 0 dfs(0,"",f,n) ans.sort() print(len(ans)) if len(ans) < 10: for i in ans: print(i) else: for i in ans[:5]: print(i) for i in ans[-5:]: print(i) while 1: s = input() ans = [] if s == "#": break solve(s) """ #2004_D """ import sys from collections import defaultdict while 1: d = defaultdict(list) d_ = defaultdict(list) n = int(sys.stdin.readline()) if n == 0:break point = [[float(x) for x in sys.stdin.readline().split()] for i in range(n)] ans = 1 for i in range(n): p,q = point[i] for j in range(n): if i == j:continue s,t = point[j] if (p-s)**2+(q-t)**2 > 4: continue d[i].append(j) if j > i: d_[i].append(j) for i in range(n): p,q = point[i] for j in d_[i]: s,t = point[j] v = (t-q,p-s) m = ((p+s)/2,(q+t)/2) l = 0 r = 10000 while r-l > 0.0001: k = (l+r)/2 a,b = m[0]+k*v[0],m[1]+k*v[1] if (p-a)**2+(q-b)**2 < 1: l = k else: r = k ans_ = 2 for l in d[i]: if l == j: continue x,y = point[l] if (x-a)**2+(y-b)**2 < 1: ans_ += 1 if ans_ > ans: ans = ans_ if ans == n:break k = -k a,b = m[0]+k*v[0],m[1]+k*v[1] ans_ = 2 for l in d[i]: if l == j: continue x,y = point[l] if (x-a)**2+(y-b)**2 < 1: ans_ += 1 if ans_ > ans: ans = ans_ if ans == n:break if ans == n:break print(ans) """ #2010_D import sys from collections import defaultdict,deque d = [(1,0),(-1,0),(0,1),(0,-1)] def bfs(b,a,k): q = deque([(b,a)]) bfs_map[b][a] = k g = a+0.5 p = s[b][a] su = 1 while q: y,x = q.popleft() for dy,dx in d: y_ = y+dy x_ = x+dx if 0 <= y_ < h and 0 <= x_ < w: if bfs_map[y_][x_] < 0 and s[y_][x_] == p: bfs_map[y_][x_] = k q.append((y_,x_)) g += x_+0.5 su += 1 f[k] = (g,su) def dfs(x): if g[x] != None: return g[x] g[x] = [f[x][0],f[x][1]] for y in v[x]: g_ = dfs(y) g[x][0] += g_[0] g[x][1] += g_[1] return g[x] while 1: w,h = [int(x) for x in sys.stdin.readline().split()] if w == 0: break s = [sys.stdin.readline()[:-1] for i in range(h)] f = defaultdict(lambda : 0) bfs_map = [[-1]*w for i in range(h)] k = 0 for y in range(h)[::-1]: for x in range(w): if s[y][x] != "." and bfs_map[y][x] < 0: bfs(y,x,k) k += 1 v = [[] for i in range(k)] l = [float("inf")]*k r = [0]*k fl = [1]*k for y in range(h): for x in range(w): if bfs_map[y][x] > -1: b = bfs_map[y][x] if y == h-1 or (bfs_map[y+1][x] != b and bfs_map[y+1][x] > -1): l[b] = min(l[b],x) r[b] = max(r[b],x+1) if y < h-1 and fl[b]: v[bfs_map[y+1][x]].append(b) fl[b] = 0 g = [None]*k for i in range(k): dfs(i) gx = g[i][0]/g[i][1] if gx <= l[i] or r[i] <= gx: print("UNSTABLE") break else: print("STABLE") ```
105,199