text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` import sys from bisect import bisect_left input = sys.stdin.buffer.readline T = int(input()) for _ in range(T): n, b = int(input()), list(map(int, input().split())) setb = set(b) rb = [ u for u in range(1, 2*n+1) if not u in setb ] sm = [0]*(n+2) for i, u in enumerate(rb): p = bisect_left(b, u) p1, p2 = max(p-i, 0), n-1-i p3, p4 = 0+n-i, min(p-1+n-i, n) if p1 <= p2: sm[p1] += 1; sm[p2+1] -= 1 if p3 <= p4: sm[p3] += 1; sm[p4+1] -= 1 for u in range(1, n+2): sm[u] += sm[u-1] cc = sum([ 1 for u in sm if u == n ]) print(cc) ```
87,800
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` t=int(input()) for _ in range(t): n=int(input()) b=list(map(int,input().split())) s=set(b) arr=[1]*(2*n) pos=0 mini=0 for i in range(n): pos=max(pos,b[i]) temp=pos for j in range(pos,2*n): if(((j+1) not in s) and arr[j]==1): mini+=1 arr[j]=0 arr[b[i]-1]=0 pos=j+1 break if(pos==temp): pos=2*n arr=[1]*(2*n) pos=2*n-1 maxi=0 for i in range(n-1,-1,-1): pos=min(pos,b[i]-2) temp=pos for j in range(pos,-1,-1): if(((j+1) not in s) and arr[j]==1): maxi+=1 arr[j]=0 arr[b[i]-1]=0 pos=j-1 break if(pos==temp): pos=-1 maxi=n-maxi print(mini-maxi+1) ```
87,801
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline INF=10**9+1 t=int(input()) for _ in range(t): n=int(input()) b=list(map(int,input().split())) maxofmins=0 minofmaxs=0 maxofmaxs=0 minofmins=0 pointer=1 grid=[-1]*(2*n) for i in b: grid[i-1]=1 tmpmax=0 tmpmin=0 curr=0 for i in range(2*n): curr+=grid[i] tmpmax=max(tmpmax,curr) tmpmin=min(tmpmin,curr) print(n+tmpmin-tmpmax+1) ```
87,802
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) max_force = 0 min_force = 0 legacy = 0 possibility = 0 for i in b: jump = i - legacy - 1 possibility += jump if possibility > 0: possibility -= 1 else: min_force += 1 legacy = i possibility = 0 legacy = 2 * n + 1 for i in reversed(b): jump = legacy - i - 1 possibility += jump if possibility > 0: possibility -= 1 else: max_force += 1 legacy = i print(n - max_force - min_force + 1) ```
87,803
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) used = [0] * (2*n+1) for x in b: used[x] = 1 a = [x for x in range(1, 2*n+1) if used[x]==0] max_ = 0 l, r = 0, len(a) - 1 for x in b[::-1]: if x < a[r]: max_ += 1 r -= 1 else: l += 1 min_ = 0 l, r = 0, len(b) - 1 for x in a[::-1]: if x < b[r]: r -= 1 else: min_ += 1 l += 1 print(max_ - min_ + 1) ```
87,804
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` tests = int(input()) for t in range(tests): n = int(input()) ls = list(map(int, input().split())) if min(ls) == (n+1) or max(ls) == (n): print(1) else: #right to left mini = 0 curr = 2*n idx = n-1 remain = 0 while idx >= 0: remain += curr - ls[idx] if remain > 0: remain -= 1 mini += 1 curr = ls[idx]-1 idx -= 1 #left to right maxi = 0 curr = 1 idx = 0 remain = 0 while idx < n: remain += ls[idx] - curr if remain > 0: remain -= 1 maxi += 1 curr = ls[idx]+1 idx += 1 print(mini - (n-maxi)+1) ```
87,805
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` #! /usr/bin/env python3 import sys t = int(sys.stdin.readline()) for ti in range(t): n = int(sys.stdin.readline()) b = [int(a) for a in sys.stdin.readline().split()] max_min = -1 min_max = n prev = 0 E = n U = 0 for i in range(n): leaved = b[i] - prev - 1 prev = b[i] U = max(0, U - leaved) + 1 E = E - leaved if E > U: max_min = i elif E == U: max_min = i break elif E < U: break E = n U = 0 prev = 2 * n + 1 for i in range(n): leaved = prev - b[n - i - 1] - 1 prev = b[n - i - 1] U = max(0, U - leaved) + 1 E = E - leaved if E > U: min_max = n - i - 1 elif E == U: min_max = n - i - 1 break elif E < U: break print(max_min - min_max + 2) ```
87,806
Provide tags and a correct Python 3 solution for this coding contest problem. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Tags: binary search, constructive algorithms, greedy, two pointers Correct Solution: ``` for _ in range(int(input())): n = int(input()) b = [int(x) for x in input().split()] j, count = 0, 0 bad_left, bad_right = 0, 0 for i in range(1, 2*n+1): if j < n and b[j] == i: j += 1 if count == 0: bad_left += 1 else: count -= 1 else: count += 1 j, count = n-1, 0 for i in range(2*n, 0, -1): if j > -1 and b[j] == i: j -= 1 if count == 0: bad_right += 1 else: count -= 1 else: count += 1 print(n+1 - bad_left - bad_right) ```
87,807
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` import sys def main(): def modst(a, s): ret = 1 while s: if s % 2: ret = ret * a % mod a = a * a % mod s //= 2 return ret def Cnk(n, k): return (k <= n and n >= 0) * ((f[n] * modst((f[k] * f[n - k]) % mod, mod - 2)) % mod) #x, w = map(int, sys.stdin.readline().split()) #a,b,c = map(int, sys.stdin.readline().split()) n = int(sys.stdin.readline().strip()) #a, w = map(int, sys.stdin.readline().split()) q = list(map(int, sys.stdin.readline().split())) #q = sorted(list(map(int, sys.stdin.readline().split())), reverse=True) '''mod = 998244353 f = [1, 1] for i in range(2, 200007): f.append((f[-1] * i) % mod) a = 0 for i in range(2 - n % 2, n + 1, 2): a = (a + Cnk(max(((n - i) // 2) + i - 1, 0), i - 1)) % mod print((a * modst(modst(2, n), mod - 2)) % mod)''' s = set(q) f, ans, res, l, r = 2 * n, 0, 0, 0, 0 for i in range(1, f + 1): if i in s: if l: l -= 1 ans += 1 else: l += 1 for i in range(f, 0, -1): if i in s: if r: r -= 1 res += 1 else: r += 1 print(abs(ans + res - n) + 1) for i in range(int(input())): main() ``` Yes
87,808
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` # Write your code here import sys, math import io, os #data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from bisect import bisect_left as bl, bisect_right as br, insort from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n') def out(var) : sys.stdout.write(str(var)+'\n') from decimal import Decimal #from fractions import Fraction sys.setrecursionlimit(100000) INF = float('inf') mod = 10**9 + 7 for _ in range(int(data())): n=int(data()) b=mdata() l,s,ol,os=0,0,0,0 for i in range(n): if b[i]-i-1>ol: ol+=1 l+=1 if 2*n-b[n-i-1]-i>os: os+=1 s+=1 out(os - n + ol + 1) ``` Yes
87,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n=int(input()) B=list(map(int,input().split())) SETB=set(B) MINOK=[0]*n MAXOK=[0]*n minpair=0 for i in range(n): b=B[i] while minpair<=b or minpair in SETB: minpair+=1 MINOK[i]=minpair minpair+=1 maxpair=n*2 for i in range(n-1,-1,-1): b=B[i] while maxpair>=b or maxpair in SETB: maxpair-=1 MAXOK[i]=maxpair maxpair-=1 #print(MINOK,MAXOK) for i in range(n): if 1<=MINOK[i]<=n*2: MINOK[i]=1 else: MINOK[i]=0 if 1<=MAXOK[i]<=n*2: MAXOK[i]=1 else: MAXOK[i]=0 #print(MINOK,MAXOK) ANS=0 if MAXOK[0]==1: ANS+=1 for i in range(n-1): if MINOK[i]==1 and MAXOK[i+1]==1: ANS+=1 if MINOK[-1]==1: ANS+=1 print(ANS) ``` Yes
87,810
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` def main(n, d): o = [] i = 0 c = d[0] for j in range(1, 2 * n + 1): if i < n and c == j: i += 1 if i == n: continue c = d[i] else: o.append(j) f, e = 0, 0 i = 0 c = o[i] for j in d: if c < j: i += 1 if i == n: break c = o[i] else: f += 1 i = n - 1 c = o[i] for j in range(n - 1, -1, -1): if c > d[j]: i -= 1 if i < 0: break c = o[i] else: e += 1 return n - e - f + 1 t = int(input()) for i in range(t): n =int(input()) *d, = map(int, input().split()) print(main(n, d)) ``` Yes
87,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` from sys import stdin, stdout from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log from bisect import bisect_left as bl, bisect_right as br, bisect mod = pow(10, 9) + 7 mod2 = 998244353 def inp(): return stdin.readline().strip() def out(var, end="\n"): stdout.write(str(var)+"\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def smp(): return map(str, inp().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] def remadd(x, y): return 1 if x%y else 0 def ceil(a,b): return (a+b-1)//b def chkprime(x): if x<=1: return False if x in (2, 3): return True if x%2 == 0: return False for i in range(3, int(sqrt(x))+1, 2): if x%i == 0: return False return True for _ in range(int(inp())): n = int(inp()) arr = lmp() # c1, c2 = 0, 0 # for i in range(n): # if arr[i] <= n: c1+=1 # else: c2 += 1 # print(min(c1, c2)+1) md = {} for i in range(n): if arr[i] not in md: md[arr[i]]=1 c1, c2 = 0, 2*n+1 for i in range(n): if arr[i] <= n: c1 = max(c1, arr[i]) else: c2 = min(c2, arr[i]) i = 1 k1, k2 = 0, 0 while(i<c1): if i not in md: k1 += 1 i += 1 i = 2*n while(i>c2): if i not in md: k2 += 1 i -= 1 print(k1+k2+1) ``` No
87,812
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` #import io,os #input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline T = int(input()) r = 1 while r<=T: n = int(input()) b = list(map(int,input().split())) ans = 0 occu = [False]*(2*n+2) for i in range(n): occu[b[i]] = True s = 1 l = 2*n maxbig = 0 minbig = n for i in range(n): if b[i]>s: s += 1 while occu[s]: s+= 1 maxbig += 1 else: while occu[l]: l -= 1 l -= 1 s += 1 s = 1 l = 2*n for i in range(n-1,-1,-1): if b[i]<l: l -= 1 while occu[l]: l -= 1 minbig -= 1 else: while occu[s]: s += 1 s += 1 l -= 1 # print(minbig) # print(maxbig) print(maxbig-minbig+1) r += 1 ``` No
87,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- # @oj: codeforces # @id: hitwanyang # @email: 296866643@qq.com # @date: 2020/12/18 11:12 # @url: https://codeforc.es/contest/1463/problem/D import sys, os from io import BytesIO, IOBase import collections, itertools, bisect, heapq, math, string from decimal import * # region fastio BUFSIZE = 8192 BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ ## 注意嵌套括号!!!!!! ## 先有思路,再写代码,别着急!!! ## 先有朴素解法,不要有思维定式,试着换思路解决 ## 精度 print("%.10f" % ans) ## sqrt:int(math.sqrt(n))+1 ## 字符串拼接不要用+操作,会超时 ## 二进制转换:bin(1)[2:].rjust(32,'0') ## array copy:cur=array[::] ## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200 ## sqrt:Decimal(x).sqrt()避免精度误差 ## 无穷大表示:float('inf') ## py 10**6 排序+双指针 3秒可能TLE ## 按区间右端点排序,current.left>pre.right,贪心求不相交区间的最大个数 def main(): t = int(input()) for i in range(t): n = int(input()) b = list(map(int, input().split())) sb = sorted(b) x,y=0,0 for j in sb: if j<=n: x+=1 else: y+=1 print (min(n-x,n-y)+1) if __name__ == "__main__": main() ``` No
87,814
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 2n integers 1, 2, ..., 2n. You have to redistribute these 2n elements into n pairs. After that, you choose x pairs and take minimum elements from them, and from the other n - x pairs, you take maximum elements. Your goal is to obtain the set of numbers \\{b_1, b_2, ..., b_n\} as the result of taking elements from the pairs. What is the number of different x-s (0 ≤ x ≤ n) such that it's possible to obtain the set b if for each x you can choose how to distribute numbers into pairs and from which x pairs choose minimum elements? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers b_1, b_2, ..., b_n (1 ≤ b_1 < b_2 < ... < b_n ≤ 2n) — the set you'd like to get. It's guaranteed that the sum of n over test cases doesn't exceed 2 ⋅ 10^5. Output For each test case, print one number — the number of different x-s such that it's possible to obtain the set b. Example Input 3 1 1 5 1 4 5 9 10 2 3 4 Output 1 3 1 Note In the first test case, x = 1 is the only option: you have one pair (1, 2) and choose the minimum from this pair. In the second test case, there are three possible x-s. If x = 1, then you can form the following pairs: (1, 6), (2, 4), (3, 5), (7, 9), (8, 10). You can take minimum from (1, 6) (equal to 1) and the maximum elements from all other pairs to get set b. If x = 2, you can form pairs (1, 2), (3, 4), (5, 6), (7, 9), (8, 10) and take the minimum elements from (1, 2), (5, 6) and the maximum elements from the other pairs. If x = 3, you can form pairs (1, 3), (4, 6), (5, 7), (2, 9), (8, 10) and take the minimum elements from (1, 3), (4, 6), (5, 7). In the third test case, x = 0 is the only option: you can form pairs (1, 3), (2, 4) and take the maximum elements from both of them. Submitted Solution: ``` import sys import math,bisect,operator inf,m = float('inf'),10**9+7 sys.setrecursionlimit(10 ** 5) from itertools import groupby,accumulate from heapq import heapify,heappop,heappush from collections import deque,Counter,defaultdict I = lambda : int(sys.stdin.readline()) neo = lambda : map(int, sys.stdin.readline().split()) Neo = lambda : list(map(int, sys.stdin.readline().split())) for _ in range(I()): n = I() A = Neo() B = [1]*(2*n+1) B[0] = 0 for i in A: B[i] = 0 C = list(accumulate(B[::-1]))[::-1] D = list(accumulate(B)) A.sort() Ans = 0 for i in A: if C[i]-Ans > 0: Ans += 1 t = 0 f = 1 for i in A: if D[i]-t > 0: t += 1 else: f = 0 break if f: Ans += 1 print(Ans) ``` No
87,815
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from collections import Counter dp=Counter() def solve(w,b): if dp[(w,b)]: return dp[(w,b)] if (w==0)|(b<0): return 0 if b==0: return 1 ans=w/(w+b) if b!=1: ans+=b/(w+b)*(b-1)/(w+b-1)*(solve(w-1,b-2)*w/(b+w-2)+solve(w,b-3)*(b-2)/(b+w-2)) dp[(w,b)]=ans return ans def main(): w,b=map(int,input().split()) print(solve(w,b)) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
87,816
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` #!/usr/bin/env python3 # created : 2020. 8. 18. 00:50 import os from sys import stdin, stdout def solve(tc): w, b = map(int, stdin.readline().split()) dp = [[1.0 for j in range(1001)] for i in range(1001)] for i in range(1, 1001): dp[i][0] = 1.0 dp[i][1] = i/(i+1) dp[0][i] = 0.0 dp[1][1] = 0.5 if w == 0: print(0.0) return if b == 0: print(1.0) return elif b == 1: print(dp[w][b]) return for i in range(1, w+1): dp[i][2] = i/(i+2) dp[i][2] += 2/(i+2) * 1/(i+1) for j in range(3, b+1): dp[i][j] = i/(i+j) dp[i][j] += j/(i+j) * (j-1)/(i+j-1) * (j-2)/(i+j-2) * dp[i][j-3] dp[i][j] += j/(i+j) * (j-1)/(i+j-1) * i/(i+j-2) * dp[i-1][j-2] print(dp[w][b]) tcs = 1 tc = 1 while tc <= tcs: solve(tc) tc += 1 ```
87,817
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` # -*- coding:utf-8 -*- """ created by shuangquan.huang at 2/2/20 """ import collections import time import os import sys import bisect import heapq from typing import List from functools import lru_cache @lru_cache(maxsize=None) def solve(w, b): if w <= 0: return 0 if b <= 0: return 1 # current is player 1's turn # win prob ans = w / (w + b) # draw black p = b / (w + b) b -= 1 # probability of continuing after player 2's turn p *= b / (w + b) b -= 1 if p > 1e-13: # mouse jumps is either white or black pblack = solve(w, b-1) * b / (w + b) pwthite = solve(w-1, b) * w / (w + b) ans += p * (pblack + pwthite) return ans w, b = map(int, input().split()) print(solve(w, b)) ```
87,818
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` w, b = map(int, input().split()) cached = [[None]*(b+1) for i in range(w+1)] def cal(w, b): # print(w, b) if cached[w][b] is not None: return cached[w][b] if w == 0: return 0 if b == 0: return 1 total = w + b # white win p = w / total # black, bp = 1 - p # turn to drag dbp = (b-1) / (total - 1) if b == 1: return p if b == 2: if w == 1: return p else: return p + bp*dbp cached[w][b] = p + bp * dbp * (w / (total - 2) * cal(w-1, b-2) + (b - 2) / (total - 2) * cal(w, b - 3)) return cached[w][b] print(cal(w, b)) ```
87,819
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` w, b = map(int, input().split()) k, p, q, s = 0, 0, 1, w + b while q != 0 and k < s: d = q * w / s p += d; q -= d; s -= 1 d = q * w / s q -= d; s -= 1; k += 1 print(p) ```
87,820
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` # three players princess, dragon, chance # there are only 2 possible winners # use 3d array dp[w][b][p] to store the prob of having w, b mice left to the player p # interesting, how do we even implement this?, without recursive? # (w + b) - (wleft + bleft) mod 3 will tell us the player # 0 being princess, 1 being dragon, 2 being chance # so we can just loop through the array, without even worrying for the player at first # all the 'special cases' treatment, is giving me headache. is there a way to nicely sort them out without special cases ? MAX = 1002 w, b = map(int, input().split()) ans = 0 dp = [[0 for _ in range(MAX)] for _ in range(MAX)] end = [[0 for _ in range(MAX)] for _ in range(MAX)] dp[w][b] = 1 for i in range (w, -1, -1): end[i][b+1] = 1 if w > 0: ans += ((w) / (w + b)) * dp[w][b] end[w-1][b] = 1 if b > 0: for j in range(b - 1, -1, -1): dp[w][j] = ((j + 1) / (w + j + 1)) * dp[w][j+1] for i in range(w - 1, -1, -1): for j in range(b - 1, -1, -1): if end[i+1][j] and end[i][j+1]: end[i][j] = 1 else: if not end[i+1][j]: if (w + b - i -j) % 3 == 1: ans += ((i + 1) / (i + j + 1)) * dp[i+1][j] if end[i][j+1]: end[i][j] = 1 elif (w + b - i -j) % 3 == 2: if end[i][j+1]: end[i][j] = 1 else: dp[i][j] += ((i + 1) / (i + j + 1)) * dp[i+1][j] if not end[i][j+1]: dp[i][j] += ((j + 1) / (i + j + 1)) * dp[i][j+1] print(ans) ```
87,821
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` # def fun( num ) : # prob = 1 # if(num>0): # # print( "prob" , prob ) # print( "num" , num ) # prob *= num # prob *= fun( num-1 ) # return prob # print( fun( 8 ) ) save={} def fun( w , b ) : global save key=str(w)+' '+str(b) if(save.get(key)!=None): return save[ key ] prob=0 if (( w>= 0 ) and ( b>=0 ) and ( (w+b)!= 0 ) ) : prob = w / ( w + b ) if( (w+b)>2 ): prob += fun( w-1 , b-2 ) * ( b / ( w+b ) ) * ((b-1) / ( w+b-1 ) ) * ( w / ( w+b-2 )) prob += fun( w , b-3 ) * ( b / ( w+b ) ) * ( (b-1) / ( w+b-1 ) ) * ( (b-2) / ( w+b-2 )) save[ key ]=prob return prob scr = input() mice_w , mice_b =map(int , scr.split()) prob = fun( mice_w , mice_b ) print( prob ) ```
87,822
Provide tags and a correct Python 3 solution for this coding contest problem. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Tags: dp, games, math, probabilities Correct Solution: ``` w, b = map(int, input().split()) k, p, q, s = 0, 0, 1, w + b while q != 0 and k < s: d = q * w / s p += d q -= d s -= 1 d = q * w / s q -= d s -= 1 k += 1 print(p) # Made By Mostafa_Khaled ```
87,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` w, b = map(int, input().split()) k, p, q, s = 0, 0, 1, w + b while q != 0 and k < s: d = q * w / s p += d q -= d s -= 1 d = q * w / s q -= d s -= 1 k += 1 print(p) ``` Yes
87,824
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` n,m=map(int,input().split(" ")) dp=[[[1,0] if j and not i else [0,0] for i in range(m+1)] for j in range(n+1)] for i in range(1,n+1): for j in range(1,m+1): dp[i][j][0]=(i/(i+j)) + (j/(i+j))*dp[i][j-1][1] dp[i][j][1]=(j/(i+j))*(dp[i][j-2][0]*((j-1)/(i+j-1)) + dp[i-1][j-1][0]*((i)/(i+j-1))) print(dp[n][m][0]) ``` Yes
87,825
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` # SHRi GANESHA author: Kunal Verma # import os import sys from bisect import bisect_right from collections import Counter, defaultdict, deque from heapq import * from io import BytesIO, IOBase from math import gcd, inf, sqrt, ceil def lcm(a, b): return (a * b) // gcd(a, b) ''' mod = 10 ** 9 + 7 fac = [1] for i in range(1, 2 * 10 ** 5 + 1): fac.append((fac[-1] * i) % mod) fac_in = [pow(fac[-1], mod - 2, mod)] for i in range(2 * 10 ** 5, 0, -1): fac_in.append((fac_in[-1] * i) % mod) fac_in.reverse() def comb(a, b): if a < b: return 0 return (fac[a] * fac_in[b] * fac_in[a - b]) % mod ''' #MAXN = 10000004 # spf = [0 for i in range(MAXN)] # adj = [[] for i in range(MAXN)] def sieve(): global spf, adj, MAXN spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(2, MAXN): if i * i > MAXN: break if (spf[i] == i): for j in range(i * i, MAXN, i): if (spf[j] == j): spf[j] = i def getdistinctFactorization(n): global adj, spf, MAXN for i in range(1, n + 1): index = 1 x = i if (x != 1): adj[i].append(spf[x]) x = x // spf[x] while (x != 1): if (adj[i][index - 1] != spf[x]): adj[i].append(spf[x]) index += 1 x = x // spf[x] def printDivisors(n): i = 2 z = [1, n] while i <= sqrt(n): if (n % i == 0): if (n / i == i): z.append(i) else: z.append(i) z.append(n // i) i = i + 1 return z def create(n, x, f): pq = len(bin(n)[2:]) if f == 0: tt = min else: tt = max dp = [[inf] * n for _ in range(pq)] dp[0] = x for i in range(1, pq): for j in range(n - (1 << i) + 1): dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))]) return dp def enquiry(l, r, dp, f): if l > r: return inf if not f else -inf if f == 1: tt = max else: tt = min pq1 = len(bin(r - l + 1)[2:]) - 1 return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1]) def SieveOfEratosthenes(n): prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n + 1, p): prime[i] = False p += 1 x = [] for i in range(2, n + 1): if prime[i]: x.append(i) return x cache=defaultdict(lambda :-1) def func(w,b): # print(w,b) if cache[(w,b)]!=-1: return cache[(w,b)] if b < 0 or w == 0: return 0 if b==0: return 1 an=0 if (b==1): an+=(w/(b+w)) else: an+=b/(w+b)*(b-1)/(w+b-1)*(func(w-1,b-2)*w/(b+w-2)) an+=b/(w+b)*(b-1)/(w+b-1)*(func(w,b-3)*(b-2)/(b+w-2)) an+=(w/(b+w)) cache[(w,b)]=an return cache[(w,b)] def main(): w,b=map(int,input().split()) print(func(w,b)) # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == '__main__': main() ``` Yes
87,826
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` w, b = map(int, input().split(' ')) if w==0: print(0) elif b==0: print(1) else: solved = [[-1]*b for i in range(w)] def solve(w, b): if solved[w-1][b-1] != -1: return solved[w-1][b-1] if w==0: ans = 0 solved[w-1][b-1] = ans return ans if b==0: ans = 1 solved[w-1][b-1] = ans return ans if b==1: ans = w/(w+1) solved[w-1][b-1] = ans return ans if b==2: if w==1: ans = 1/3 solved[w-1][b-1] = ans return ans else: ans = w/(w+2) + 2/(w+2) * 1/(w+1) solved[w-1][b-1] = ans return ans x = solve(w-1, b-2) y = solve(w, b-3) ans = w/(w+b) + b/(w+b) * (b-1)/(w+b-1) * (w/(w+b-2) * x + (b-2)/(w+b-2) * y) solved[w-1][b-1] = ans return ans print(solve(w, b)) ``` Yes
87,827
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') range = xrange # not for python 3.0+ # main code w,b=in_arr() dp=[[0 for i in range(w+1)] for i in range(b+1)] vis=[[0 for i in range(w+1)] for j in range(b+1)] dp[b][w]=1 vis[b][w]=1 q=[(b,w)] ans=0 while q: x,y=q.pop(0) if x: if not vis[x-1][y]: q.append((x-1,y)) vis[x-1][y]=1 dp[x-1][y]+=(dp[x][y]*(float(x)/(x+y))) if y: if not vis[x][y-1]: q.append((x,y-1)) vis[x][y-1]=1 pro=dp[x][y]*(float(y)/(x+y)) dis=b+w-x-y if dis%3==0: #print ans,pro,x,y-1 ans+=pro if dis%3==2: dp[x][y-1]+=(pro) print ans ``` Yes
87,828
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` from fractions import Fraction w, b = [int(x) for x in input().split()] def probability(w, b): if w == 0: return 0 if b == 0: return 1 memory = {} def recursive(w, b, t): turn = t % 2 if (w, b, turn) in memory: return memory[(w, b, turn)] # princess turn if turn == 0: answer = Fraction(0) if w != 0: answer = Fraction(w, (b + w)) if b > 1: answer = 1 - (1 - answer) * (1 - recursive(w, b - 1, t + 1)) else: # dragons turn answer = 1 - Fraction(w, (b + w)) if b > 1: answer = answer * (recursive(w - 1, b - 1, t + 1) + recursive(w, b - 2, t + 1)) / 2 # print(w, b, t) # print(answer) memory[(w, b, turn)] = answer return answer return recursive(w, b, 0) answer = probability(w, b) print(float(answer)) ``` No
87,829
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` import sys input=sys.stdin.readline memo={} def prob(w,b): if w==0 or b<=0: return 0 if b==0: return 1 if (w,b) in memo: return memo[(w,b)] ans1=w/(w+b) if b==1: ans2=0 else: ans2=( b/(w+b) )*( (b-1)/(w+b-1) )*( prob(w-1,b-2)*w/(b+w-2) + prob(w,b-3)*(b-2)/(b+w-2)) memo[(w,b)]=ans1+ans2 return ans1+ans2 w,b=map(int,input().split()) print(prob(w,b)) ``` No
87,830
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` from fractions import Fraction w, b = [int(x) for x in input().split()] def probability(w, b): if w == 0: return 0 if b == 0: return 1 if b == 5 and w == 5: return 0.658730159 memory = {} def recursive(w, b, t): turn = t % 2 if (w, b, turn) in memory: return memory[(w, b, turn)] # princess turn if turn == 0: answer = Fraction(0) if w != 0: answer = Fraction(w, (b + w)) if b > 1: answer = 1 - (1 - answer) * (1 - recursive(w, b - 1, t + 1)) else: # dragons turn answer = 1 - Fraction(w, (b + w)) if b > 1: answer = answer * (recursive(w - 1, b - 1, t + 1) + recursive(w, b - 2, t + 1)) / 2 # print(w, b, t) # print(answer) memory[(w, b, turn)] = answer return answer return recursive(w, b, 0) answer = probability(w, b) print(float(answer)) ``` No
87,831
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. Input The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000). Output Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9. Examples Input 1 3 Output 0.500000000 Input 5 5 Output 0.658730159 Note Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. Submitted Solution: ``` from fractions import Fraction w, b = [int(x) for x in input().split()] def probability(w, b): memory = {} def recursive(w, b): if b <= 0: return 1.0 if w <= 0: return 0.0 if (w, b) in memory: return memory[(w, b)] white = w black = b # princess turn # chance to win answer = white / (black + white) # chance to continue the game after choosing black mouse black -= 1 cont_p = (1 - answer) * (black /(black + white)) # it's too small to calc next if cont_p > 1e-13: # dragon chooses black mouse black -= 1 # chance for white mouse pop out white_ch = white / (white + black) # win chances white_p = recursive(white - 1, black) * white_ch black_p = recursive(white, black - 1) * (1 - white_ch) answer += cont_p * (black_p + white_p) memory[(w, b)] = answer return answer return recursive(w, b) answer = probability(w, b) print(answer) ``` No
87,832
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` for _ in range(int(input())) : n=int(input());a=[] l=[[0 for i in range(n)] for j in range(n)] for i in range(n) : s=input() for j in range(n) : if(s[j]=="*") : a.append([i,j]) l[i][j]=s[j] x1=a[0][0];y1=a[0][1] x2=a[1][0];y2=a[1][1] if(x1!=x2 and y1!=y2) : l[x1][y2]="*" l[x2][y1]="*" elif(x1==x2) : if(x1==0 and x2==0) : l[x1+1][y1]="*" l[x2+1][y2]="*" else : l[x1-1][y1]="*" l[x2-1][y2]="*" elif(y1==y2) : if(y1==0 and y2==0) : l[x1][y1+1]="*" l[x2][y2+1]="*" else : l[x1][y1-1]="*" l[x2][y2-1]="*" for i in range(n) : for j in range(n) : print(l[i][j],end="") print() ```
87,833
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` test =int(input()) while test>0: n = int(input()) arr =[] for i in range(n): arr.append(list(input())) x=y=0 x1=y1=0 count=1 for i in range(n): for j in range(n): if count==1 and arr[i][j]=="*": x=j y=i count+=1 elif count==2 and arr[i][j]=="*": x1=j y1=i break if y==y1: arr[(y+1)%n][x1]="*" arr[(y+1)%n][x]="*" elif x1==x: arr[y1][(x1+1)%n]="*" arr[y][(x+1)%n]="*" else: arr[y][x1]="*" arr[y1][x]="*" for s1 in arr: s="" for a in s1: s+=str(a) print(s) test-=1 ```
87,834
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` # cook your dish here for _ in range(int(input())): n = int(input()) arr = [] for i in range(n): temp = list(input()) arr.append(temp) pos1 = -1 pos2 = -1 for i in range(n): for j in range(n): if arr[i][j] == "*": if pos1 == -1: pos1 = [i, j] else: pos2 = [i, j] break if pos1[0] == pos2[0]: if ((pos1[0] + 1) < n): arr[pos1[0]+1][pos1[1]] = "*" arr[pos2[0]+1][pos2[1]] = "*" else: arr[pos1[0]-1][pos1[1]] = "*" arr[pos2[0]-1][pos2[1]] = "*" elif pos1[1] == pos2[1]: if ((pos1[1] + 1) < n): arr[pos1[0]][pos1[1]+1] = "*" arr[pos2[0]][pos2[1]+1] = "*" else: arr[pos1[0]][pos1[1]-1] = "*" arr[pos2[0]][pos2[1]-1] = "*" else: arr[pos1[0]][pos2[1]] = "*" arr[pos2[0]][pos1[1]] = "*" for i in range(n): s = "" print(s.join(arr[i])) ```
87,835
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` import sys import math input = sys.stdin.readline mod=(10**9)+7 ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) for _ in range(inp()): n=inp() l=[] r=-1 c=-1 r1=-1 c1=-1 for i in range(n): a=insr() l.append(a) for j in range(n): if a[j]=="*": if r==-1: r=i c=j continue if r1==-1: r1=i c1=j if r1==r: if r!=n-1: l[r+1][c]="*" l[r1+1][c1]="*" else: l[r-1][c]="*" l[r1-1][c1]="*" elif c1==c: if c!=n-1: l[r][c+1]="*" l[r1][c+1]="*" else: l[r][c-1]="*" l[r1][c-1]="*" else: l[r][c1]="*" l[r1][c]="*" for i in range(n): print("".join(j for j in l[i])) ```
87,836
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` n = int(input()) for z in range(n): t = int(input()) x = [] y = [] l = [list(str(input())) for _ in range(t)] d = {} for i in range(t): for j in range(t): if l[i][j] == '*': x.append(i) y.append(j) k = list(zip(x,y)) ################################################ if k[0][0] != k[1][0]: if k[0][0] > k[1][0] and k[0][1] != k[1][1]: first_x = k[0][0] first_y = k[1][1] second_x = k[1][0] second_y = k[0][1] elif k[0][0] < k[1][0] and k[0][1] != k[1][1]: first_x = k[1][0] first_y = k[0][1] second_x = k[0][0] second_y = k[1][1] if k[0][1] == k[1][1]: if l[k[0][0]][-1] == '*': first_x = k[0][0] first_y = k[0][1] -1 second_x = k[1][0] second_y = k[1][1] -1 else: first_x = k[0][0] first_y = k[0][1] +1 second_x = k[1][0] second_y = k[1][1] +1 if k[0][0] == k[1][0]: if l[0][k[0][1]] == '*': first_x = k[0][0]+1 first_y = k[0][1] second_x = k[1][0]+1 second_y = k[1][1] else: first_x = k[0][0]-1 first_y = k[0][1] second_x = k[1][0]-1 second_y = k[1][1] ############################################## l[first_x][first_y] = '*' l[second_x][second_y] = '*' for i in range(t): print(''.join(l[i])) ```
87,837
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` import sys sys.setrecursionlimit(10**6) def main(t): n = int(input()) matrix = [] row = 0 track = [] for i in range(n): col = 0 k = input() for i in k: if i=='*': track.append(row) track.append(col) col+=1 row+=1 matrix.append(k) r1,c1,r2,c2 = track if r1==r2: if r1+1<n: matrix[r1+1] = matrix[r1] else: matrix[r1-1] = matrix[r1] elif c1==c2: if c1+1<n: k = matrix[r1][:c1+1]+'*'+matrix[r1][c1+2:] else: k = matrix[r1][:c1-1]+'*'+matrix[r1][c1:] matrix[r1] = matrix[r2] = k else: matrix[r1] = matrix[r1][:c2]+'*'+matrix[r1][c2+1:] matrix[r2] = matrix[r1][:c1]+'*'+matrix[r1][c1+1:] print(*matrix,sep='\n') if t>1: main(t-1) main(int(input())) ```
87,838
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` t = int(input()) for x in range(t): n = int(input()) a = [list(input()) for i in range(n)] c = [(i,j) for i in range(n) for j in range(n) if a[i][j]=='*'] # c00 = x0 c01 = y0 c10 = x1 c11 = y1 if c[0][0] == c[1][0]: if c[0][0] < n-1: c.append((c[0][0]+1, c[0][1])) c.append((c[0][0]+1, c[1][1])) else: c.append((c[0][0]-1, c[0][1])) c.append((c[0][0]-1, c[1][1])) elif c[0][1] == c[1][1]: if c[0][1] < n-1: c.append((c[0][0], c[0][1]+1)) c.append((c[1][0], c[1][1]+1)) else: c.append((c[0][0], c[0][1]-1)) c.append((c[1][0], c[1][1]-1)) else: c.append((c[0][0], c[1][1])) c.append((c[1][0], c[0][1])) for i in range(n): for j in range(n): if (i,j) in c: print('*', end='') else: print('.', end='') print('\n', end='') ```
87,839
Provide tags and a correct Python 3 solution for this coding contest problem. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Tags: implementation Correct Solution: ``` def solve(): n = int(input()) #n,m,k = input().split() #n = int(n) #m = int(m) #k = int(k) idx1=-1 idy1=-1 idx2=-1 idy2=-1 mark = True for i in range(n): a = input() for j in range(n): if (a[j]=='*'): if mark : idx1= j idy1= i mark = False else : idx2= j idy2= i if (idx1==idx2): if (idx1==0) : for i in range(n): for j in range(n): if j==idx1 and i==idy1 : print('*',end="") elif j==idx1+1 and i==idy1 : print('*',end="") elif j==idx2 and i==idy2 : print('*',end="") elif j==idx2+1 and i==idy2 : print('*',end="") else : print('.',end="") print('') else : for i in range(n): for j in range(n): if j==idx1 and i==idy1 : print('*',end="") elif j==idx1-1 and i==idy1 : print('*',end="") elif j==idx2 and i==idy2 : print('*',end="") elif j==idx2-1 and i==idy2 : print('*',end="") else : print('.',end="") print('') return 0; if (idy1==idy2): if (idy1==0) : for i in range(n): for j in range(n): if j==idx1 and i==idy1 : print('*',end="") elif j==idx1 and i==idy1+1 : print('*',end="") elif j==idx2 and i==idy2 : print('*',end="") elif j==idx2 and i==idy2+1 : print('*',end="") else : print('.',end="") print('') else : for i in range(n): for j in range(n): if j==idx1 and i==idy1 : print('*',end="") elif j==idx1 and i==idy1-1 : print('*',end="") elif j==idx2 and i==idy2 : print('*',end="") elif j==idx2 and i==idy2-1 : print('*',end="") else : print('.',end="") print('') return 0 for i in range(n): for j in range(n): if j==idx1 and i==idy1 : print('*',end="") elif j==idx2 and i==idy1 : print('*',end="") elif j==idx2 and i==idy2 : print('*',end="") elif j==idx1 and i==idy2 : print('*',end="") else : print('.',end="") print('') return 0 if __name__=="__main__": T = int(input()) for i in range(T): (solve()) ```
87,840
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` import sys import collections from collections import Counter, deque import itertools import math import timeit import random ######################### # imgur.com/Pkt7iIf.png # ######################### def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): divisors = [] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def flin(d, x, default=-1): left = right = -1 for i in range(len(d)): if d[i] == x: if left == -1: left = i right = i if left == -1: return (default, default) else: return (left, right) def ceil(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def prr(a, sep=' '): print(sep.join(map(str, a))) def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def minmax(a, b): return (a, b) if a < b else (b, a) # input = sys.stdin.readline for _ in range(ii()): n = ii() m = [] d = [] for i in range(n): tmp = [] for j, p in enumerate(input()): tmp.append(p) if p == '*': d.append((i, j)) m.append(tmp) a, b = d[0], d[1] if a[0] == b[0]: m[(a[0] + 1) % n][a[1]] = '*' m[(b[0] + 1) % n][b[1]] = '*' elif a[1] == b[1]: m[a[0]][(a[1] + 1) % n] = '*' m[b[0]][(b[1] + 1) % n] = '*' else: m[b[0]][a[1]] = '*' m[a[0]][b[1]] = '*' for i in range(n): print(*m[i], sep='') ``` Yes
87,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` tot = int(input()) for x in range(tot): a = int(input()) # arr = [[] for y in range(a) ] idar = [] for y in range(a): b = input() # arr[y] = list(b) # print(b) if len(idar) < 4: for pp in range(a): if b[pp]=="*": idar.append(y) idar.append(pp) nid = [] if idar[0]==idar[2]: if idar[0]==a-1: nid.append(idar[0]-1) nid.append(idar[1]) nid.append(idar[2]-1) nid.append(idar[3]) else: nid.append(idar[0]+1) nid.append(idar[1]) nid.append(idar[2]+1) nid.append(idar[3]) elif idar[1]==idar[3]: if idar[1]==a-1: nid.append(idar[0]) nid.append(idar[1]-1) nid.append(idar[2]) nid.append(idar[3]-1) else: nid.append(idar[0]) nid.append(idar[1]+1) nid.append(idar[2]) nid.append(idar[3]+1) else: nid.append(idar[0]) nid.append(idar[3]) nid.append(idar[2]) nid.append(idar[1]) pp = idar[0] qq = idar[1] rr = idar[2] ss = idar[3] tt = nid[0] uu = nid[1] vv = nid[2] ww = nid[3] for ff in range(a): for hh in range(a): if (pp==ff and qq==hh) or (rr==ff and ss==hh) or (tt==ff and uu==hh) or (vv==ff and ww==hh): print("*",end="") else: print(".",end="") print() # print(idar) ``` Yes
87,842
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` def main(): t = int(input()) for _ in range(t): n = int(input()) l=[] for i in range(n): arr = list(input()) l.append(arr) ans=[] for i in range(n): for j in range(n): if l[i][j]=="*": ans.append([i,j]) #print(ans) if ans[0][0]==ans[1][0]: if ans[0][0]==0: #print(6) l[1][ans[0][1]]="*" l[1][ans[1][1]]="*" else: l[0][ans[0][1]] = "*" l[0][ans[1][1]] = "*" elif ans[0][1]==ans[1][1]: if ans[0][1]==0: l[ans[0][0]][1]="*" l[ans[1][0]][1]="*" else: l[ans[0][0]][0] = "*" l[ans[1][0]][0] = "*" else: l[ans[0][0]][ans[1][1]]="*" l[ans[1][0]][ans[0][1]]="*" #print(l) for i in range(n): for j in range(n): print(l[i][j],end="") print() if __name__ == '__main__': main() ``` Yes
87,843
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` import sys import math import bisect from sys import stdin, stdout from math import gcd, floor, sqrt, log2, ceil from collections import defaultdict as dd from bisect import bisect_left as bl, bisect_right as br from bisect import insort from collections import Counter from collections import deque from heapq import heappush,heappop,heapify from itertools import permutations,combinations from itertools import accumulate as ac mod = int(1e9)+7 mod = 998244353 ip = lambda : int(stdin.readline()) inp = lambda: map(int,stdin.readline().split()) ips = lambda: stdin.readline().rstrip() out = lambda x : stdout.write(str(x)+"\n") t = ip() for _ in range(t): n = ip() arr = [] for i in range(n): s = ips() arr.append(list(s)) ct = [[0]*n for i in range(n)] ind = [] for i in range(n): for j in range(n): if arr[i][j] == '*': ind.append([i,j]) val1 = ind[0] val2 = ind[1] if val1[0] == val2[0]: if val1[0] == 0: arr[1][val1[1]] = '*' arr[1][val2[1]] = '*' else: arr[0][val1[1]] = '*' arr[0][val2[1]] = '*' elif val1[1] == val2[1]: if val1[1] == 0: arr[val1[0]][1] = '*' arr[val2[0]][1] = '*' else: arr[val1[0]][0] = '*' arr[val2[0]][0] = '*' else: for i in ind: x = i[0] y = i[1] for i in range(n): if y != i: ct[x][i] += 1 if x != i: ct[i][y] += 1 ct[x][y] += 1 ans = [] for i in range(n): for j in range(n): if ct[i][j] == 2: ans.append([i,j]) for i in ans: arr[i[0]][i[1]] = '*' for i in arr: print(''.join(i)) ``` Yes
87,844
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` import sys input=sys.stdin.readline def I():return input().strip() def II():return int(input().strip()) def LI():return [*map(int,input().strip().split())] import string,math,time,functools,random,fractions from heapq import heappush,heappop,heapify from bisect import bisect_left,bisect_right from collections import deque,defaultdict,Counter,OrderedDict from itertools import permutations,combinations,groupby for _ in range(II()): n=II() s=list() for i in range(n): inp=I() for j in range(n): if inp[j]=='*': s.append((i,j)) if s[0][0]==s[1][0]: s.append(((s[0][0]+1)%n,s[0][1])) s.append(((s[1][0]+1)%n,s[1][1])) elif s[0][1]==s[1][1]: s.append((s[0][0],(s[0][1]+1)%n)) s.append((s[1][0],(s[1][1]+1)%n)) else: s.append((s[0][0],s[1][1])) s.append((s[1][0],s[0][1])) for i in range(n): for j in range(n): if (i,j) in s: print('*',end='') else: print('.',end='') print(' ') ``` No
87,845
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` t=int(input()) for _ in range(t): n=int(input()) pl=[] f=0 for i in range(n): s=input() for j in range(n): if f==2: break elif s[j]=='*': pl.append((i,j)) f+=1 if pl[0][0]!=pl[1][0] and pl[0][1]!=pl[1][1]: pl.extend([(pl[0][0],pl[1][1]),(pl[1][0],pl[0][1])]) elif pl[0][0]==pl[1][0] and pl[0][0]==0: pl.extend([(pl[0][0]+1,pl[0][1]),(pl[1][0]+1,pl[1][1])]) elif pl[0][0]==pl[1][0] and pl[0][0]!=0: pl.extend([(pl[0][0]-1,pl[0][1]),(pl[1][0]-1,pl[1][1])]) elif pl[0][1]==pl[1][1] and pl[0][1]==0: pl.extend([(pl[0][0],pl[0][1]+1),(pl[1][0],pl[1][1]+1)]) else: pl.extend([(pl[0][0],pl[0][1]-1),(pl[1][0],pl[1][1]-1)]) for i in range(n): for j in range(n): if (i==pl[0][0] and j==pl[0][1]) or (i==pl[1][0] and j==pl[1][1]) or (i==pl[2][0] and j==pl[2][1]) or (i==pl[3][0] and j==pl[3][1]): print('*',end=' ') else: print('.',end=' ') print() ``` No
87,846
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` """ Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools """ if __name__ == "__main__": # Write your solution here pass ``` No
87,847
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square field of size n × n in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if n=4 and a rectangular field looks like this (there are asterisks in the marked cells): $$$ \begin{matrix} . & . & * & . \\\ . & . & . & . \\\ * & . & . & . \\\ . & . & . & . \\\ \end{matrix} $$$ Then you can mark two more cells as follows $$$ \begin{matrix} * & . & * & . \\\ . & . & . & . \\\ * & . & * & . \\\ . & . & . & . \\\ \end{matrix} $$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 400). Then t test cases follow. The first row of each test case contains a single integer n (2 ≤ n ≤ 400) — the number of rows and columns in the table. The following n lines each contain n characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of n for all test cases do not exceed 400. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output n rows of n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. Submitted Solution: ``` for _ in range(int(input())): n=int(input()) l=[] for i in range(n): l.append(list(input())) x=[] for j in range(n): for k in range(n): if l[j][k]=='*': x.append([j,k]) if x[0][0]==x[1][0]: if x[0][0]==0: l[1][x[0][1]]='*' l[1][x[1][1]]='*' elif x[0][0]==n-1: l[n-2][x[0][1]]='*' l[n-2][x[1][1]]='*' else: l[x[0][0]+1][x[0][1]]='*' l[x[0][0]][x[1][1]]='*' elif x[0][1]==x[1][1]: if x[0][1]==0: l[x[0][0]][1]='*' l[x[1][0]][1]='*' elif x[0][1]==n-1: l[x[0][0]][n-2]='*' l[x[1][0]][n-2]='*' else: l[x[0][0]][x[0][1]+1]='*' l[x[1][0]][x[0][1]+1]='*' else: # if x[0][1]==0: # l[x[0][0]][1]='*' # l[x[0][0]][1]='*' # elif x[0][1]==n-1: # l[x[0][0][n-2]='*' # l[x[0][0]][n-2]='*' # else: l[x[0][0]][x[1][1]]='*' l[x[1][0]][x[0][1]]='*' for ss in range(n): for sss in range(n): print(l[ss][sss],end='') print() ``` No
87,848
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` def median(n,m,li): count = 0 li.sort() while True: if li[(n+1)//2 -1]== m: return count li.append(m) li.sort() n+=1 count+=1 n,m = input().split() li = [int(x) for x in input().split()] print(median(int(n),int(m),li)) ```
87,849
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` def main(): def m(a): return (len(a) + 1) // 2 - 1 from bisect import insort (n, x) = map(int, input().split(' ')) a = list(sorted(list(map(int, input().split(' '))))) if x not in a: insort(a, x) while a[m(a)] != x: if a[m(a)] < x: a = a + [int(1e5)] else: a = [1] + a return len(a) - n print(main()) ```
87,850
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n,x=map(int,input().split()) a=list(map(int,input().split())) s=set(a) count=0 c=0 if x not in s: n+=1 a.append(x) count=1 c=1 a.sort() e=math.floor((n+1)/2) if a[e-1]!=x: l=0 for i in range (n): if a[i]==x: l=i r=n-l-1 if a[e]>x: count+=r-l-1 else: count+=l-r r=0 for i in range (n-1,-1,-1): if a[i]==x: r=n-i-1 l=n-r-1 if a[e]>x: c+=r-l-1 else: c+=l-r count=min(count,c) print(count) ```
87,851
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` import sys import bisect input = lambda: sys.stdin.readline().strip("\r\n") n, x = map(int, input().split()) a = list(map(int, input().split())) a.sort() ans = 0 while a[(len(a)-1)//2] != x: a.append(x) a.sort() ans += 1 print(ans) ```
87,852
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` import math n,x=map(int,input().split()) a=list(map(int,input().split())) new=0 if(a.count(x)==0): a.append(x) new+=1 a.sort() index=0 dest=(n+1)//2 best=1000 val=0 n=len(a) for i in range(n): if(a[i]==x): d=abs(dest-(i+1)) if(d<best): best=d val=i+1 n1=n if(val<=(n+1)//2): while(val!=(n1+1)//2): val+=1 n1+=1 print(n1-n+new) else: while(val!=(n1+1)//2): n1+=1 print(n1-n+new) ```
87,853
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` n,x = map(int,input().split()) a = [int(x) for x in input().split()] ans = 0 if x not in a: ans += 1 a.append(x) a.sort() n = len(a) while x != a[(n+1)//2 - 1]: if x < a[(n+1)//2 - 1]: a.append(1) a.sort() ans+=1 else: a.append(10**5) ans+=1 n = len(a) print(ans) ```
87,854
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` def median(n,m,li): count = 0 while True: li.sort() if li[(n+1)//2 -1]== m: return count li.append(m) n+=1 count+=1 n,m = input().split() li = [int(x) for x in input().split()] print(median(int(n),int(m),li)) ```
87,855
Provide tags and a correct Python 3 solution for this coding contest problem. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Tags: greedy, math, sortings Correct Solution: ``` import bisect I=lambda:map(int,input().split()) n,x=I() a,s=sorted(I()),0 while a[(n-1)//2]!=x: a.insert(bisect.bisect_right(a,x),x) s+=1 n+=1 print(s) ```
87,856
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` import bisect n,x = map(int,input().split()) a = sorted(map(int,input().split())) ans = 0 if a[(n-1)//2] != x: while a[(n-1)//2] != x: a.insert(bisect.bisect_right(a,x),x) ans+=1 n+=1 print(ans) ``` Yes
87,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` n, x = map(int, input().split()) a = list(map(int, input().split())) ans = 0 if (x not in a): a.append(x) ans += 1 n += 1 a.sort() index = 0 middle = (n + 1) // 2 close_dist = n for i in range(n): if (a[i] == x): dist = abs((i + 1) - middle) if (dist < close_dist): index = i + 1 close_dist = dist front = n - index behind = index - 1 ans += abs(front - behind - int(front > behind)) print (ans) ``` Yes
87,858
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` n, x = (int(x) for x in input().split()) lt, eq, gt, val = 0, 0, 0, 0 for ai in (int(x) for x in input().split()): if ai < x: lt += 1 elif ai == x: eq += 1 else: gt += 1 if eq == 0: val = eq = 1 if gt >= lt + eq: val += gt - (lt + eq) else: val += max(0, (lt * 2 + 1) - (n + val)) print(val) ``` Yes
87,859
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` from bisect import bisect_left as bl n,x=map(int,input().split()) cnt=0 l=[int(i) for i in input().split()] l.sort() med=l[n//2-(1-n%2)] if med==x: print(0) exit() if x not in l: l.append(x) cnt=1 l.sort() def is_not_x(l): # print(l) #print(l) length=len(l) if length%2: ind=length//2 else: ind=length//2-1 return l[ind]!=x while is_not_x(l): med=l[len(l)//2-(1-len(l)%2)] if med<x: karma="add" else: karma="remove" if karma=="add": z=10**5 l.append(z) else: l.insert(0,1) cnt+=1 print(cnt) ``` Yes
87,860
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` # Problem: C. Median # Contest: Codeforces - Codeforces Round #113 (Div. 2) # URL: https://codeforces.com/problemset/problem/166/C # Memory Limit: 256 MB # Time Limit: 2000 ms # # KAPOOR'S from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10,9)+7 def OPS(ans): stdout.write(str(ans)+"\n") def OPL(ans): [stdout.write(str(_)+" ") for _ in ans] stdout.write("\n") from bisect import bisect_left if __name__=="__main__": n,x=INL() X=sorted(INL()) m=(n-1)//2 ans=0 if X[m]<x: for _ in range(m,n): if X[_]==x: i=_ break else: i=n ans+=2*(i-m) if n%2==0: ans-=1 elif x<X[m]: for _ in range(m,-1,-1): if X[_]==x: i=_ break else: i=-1 ans+=2*(m-i) if n%2==1: ans-=1 OPS(ans) ``` No
87,861
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` import sys if __name__ == '__main__': #reading input total, required_median = [int(n) for n in input().split()] numbers = [int(n) for n in input().split()] #--------------- bigger = 0 #number of nums that are bigger than the given median smaller = 0 #number of nums that are smaller than the given median same = 0 #number of nums that are equal to the given median found = 0 #flag that shows if expected median was found in initial list. Turns to 1 if found numbers_to_add = 0 #result to be output #iterate through the list of numbers we have for num in numbers: if num > required_median: bigger += 1 elif num < required_median: smaller += 1 elif num == required_median: same += 1 if same == 0: #if no same was found ... we add one therefore adding it to ... numbers_to_add += 1 #... the output total += 1 #... and the total number of elements in our list else: same -= 1 #we remove that element from the number of "same" because this will be our median diff = abs(bigger - smaller) #we calculate the difference we have between the number of nums bigger and smaller than median if bigger > smaller: #if we have more bigger elements than smaller smaller = smaller + min(same, diff) #we will consider the equal elements that we have to be part of the smaller same = same - min(same, diff) #we naturally subtract the number we got from the equal to_add = 0 if bigger > smaller: #if bigger is still bigger than smaller to_add = (bigger - smaller - 1) #I noticed that if the bigger == smaller - 1 AND the total is even, the solution stands. Since this will require the minimum number of moves we will see if this will give a solution. if (total + to_add)%2 == 0: #we check if we add that amount we will get an even total numbers_to_add += to_add #we add the number if that's the case else: numbers_to_add += to_add + 1 #if it's odd we will have to add one more element to what we already wanted to add #same logic here elif bigger < smaller: bigger = bigger + min(same, diff) same = same - min(same, diff) to_add = (smaller - bigger) if (total + to_add) % 2 != 0: numbers_to_add += to_add else: numbers_to_add += to_add - 1 print(numbers_to_add) ``` No
87,862
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` from bisect import bisect_left n, x = list(map(int, input().split())) arr = list(map(int, input().split())) ans = 0 arr.sort() ind = bisect_left(arr, x) if ind == n: ans = n + 1 elif ind == 0: if arr[0] == x: ans = max(0, n - 2) else: ans = n else: if arr[ind] == x: if ind != (n+1)//2: ans = abs(2*ind - n - 2) else: if ind != (n+1)//2: if ind < (n+1)//2: ans += abs((n+1)//2 - ind)*2 + 1 else: ans += abs(2*ind - n - 2) + 1 else: ans = 1 print(ans) ``` No
87,863
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17. We define an expression <image> as the integer part of dividing number a by number b. One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x. Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. Input The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. Output Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x. Examples Input 3 10 10 20 30 Output 1 Input 3 4 1 2 3 Output 4 Note In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <image>, that is, 10. In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. Submitted Solution: ``` n,x = map(int,input().split()) a = [int(x) for x in input().split()] a.sort() k = 0 if x not in a: if x > a[n-1] or x < a[0]: print(n+1) else: for i in reversed(range(n)): if a[i]<x: k = i break if k + 1 == (n+2)//2: print(1) elif k + 1 > (n+1)//2: print(k+1) else: print(n-(k+2)) else: j = a.index(x) if x == a[(n+1)//2]: print(0) if j < (n+1)//2: print((n+1)//2 - (j+1)) else: print(2*(j+1) - 1 - n) ``` No
87,864
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` n = int(input()) arr = list(input().split()) evl = lambda c1, c2: c1[0] == c2[0] or c1[1] == c2[1] vt = set() q = [arr[:]] while q: ar = q.pop() vt.add(''.join(ar)) if len(ar) > 3 and evl(ar[-1], ar[-4]): tmp = ar[:len(ar) - 1] tmp[-3] = ar[-1] if ''.join(tmp) not in vt: q.append(tmp) if len(ar) > 1 and evl(ar[-1], ar[-2]): tmp = ar[:len(ar) - 1] tmp[-1] = ar[-1] if len(tmp) == 1: print('YES') exit(0) elif ''.join(tmp) not in vt: q.append(tmp) print('NO' if n > 1 else 'YES') ```
87,865
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` def f(a, b): return a[0] == b[0] or a[1] == b[1] def h(p): return p[0] == p[2] or p[1] == p[3] def g(p): return p[0] == p[4] or p[1] == p[5] n, t = int(input()), input().split()[:: -1] if n > 2: s = {t[0] + t[1] + t[2]} for k in range(3, n): d = set() for p in s: if f(t[k], p): d.add(p[2: ] + p[: 2]) if h(p): d.add(p[: 2] + p[4: ] + t[k]) s = d print('YES' if any(h(p) and g(p) for p in s) else 'NO') else: print('YES' if n == 1 or f(t[0], t[1]) else 'NO') ```
87,866
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` n=int(input()) v=input() v = v.replace(' ', '') d={'': 0} def rasklad(s): if ( len(s) == 2 ): return 1 if s in d: return d[s] if s[-1] == s[-3] or s[-2] == s[-4]: if rasklad( s[0:-4] + s[-2:] ) : d[s]=1 return 1 if len(s) >= 8 and (s[-1] == s[-7] or s[-2] == s[-8]): if rasklad( s[0:-8] + s[-2:] + s[-6:-2]) : d[s]=1 return 1 d[s]=0 return 0 print("YES" if rasklad(v) else "NO") ```
87,867
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` memo = {} n = int(input()) a = [s for s in input().split()] def ok(c1, c2): return (c1[0] == c2[0]) or (c1[1] == c2[1]) def dfs(i, c1, c2, c3): k = (i, c1, c2, c3) if k in memo: return memo[k] if i < 0: memo[k] = ok(c1, c2) and ok(c1, c3) else: r = False if ok(c1, a[i]): r = r or dfs(i - 1, c2, c3, c1) if ok(c1, c2): r = r or dfs(i - 1, c1, c3, a[i]) memo[k] = r return memo[k] def check(): if n == 1: return True; elif n == 2: return ok(a[1], a[0]) else: return dfs(n - 4, a[-1], a[-2], a[-3]) print('YES' if check() else 'NO') ```
87,868
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` d = dict() def mem(i,a): if(i == 1): print("YES") exit(0) if((i,a) in d): return b = list(a[::]) if(a[i-2][0] == a[i-1][0] or a[i-2][1] == a[i-1][1]): b[i-2] = a[i-1] mem(i-1,tuple(b[:i-1])) b[i-2] = a[i-2] if(i > 3 and (a[i-4][0] == a[i-1][0] or a[i-4][1] == a[i-1][1])): b[i-4] = a[i-1] mem(i-1,tuple(b[:i-1])) d[(i,a)] = 0 n = int(input()) a = list(input().split()) mem(n,tuple(a)) print("NO") ```
87,869
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` memory = {} def main(): number_of_pile = (input()) card_deck = tuple(input().split()) if canComplete(card_deck): print('YES') else: print("NO") def canPutOnTop(pileToPut, pileToPutOn): result = pileToPut[0] == pileToPutOn[0] or pileToPut[1] == pileToPutOn[1] # print(pileToPut, pileToPutOn, result) return result def putOnPileBeforeLast(deck): lastPile = deck[-1] pileBeforeLastPile = deck[-2] if canPutOnTop(lastPile, pileBeforeLastPile): new_deck = deck[:-2] + (deck[-1],) return canComplete(new_deck) return False def putOnThirdPileBeforeLast(deck): if len(deck) >= 4: lastPile = deck[-1] thirdPileBeforeLastPile = deck[-4] if canPutOnTop(lastPile, thirdPileBeforeLastPile): new_deck = deck[:-4] + (deck[-1],) + deck[-3:-1] return canComplete(new_deck) return False def canComplete(card_deck: tuple): if len(card_deck) == 1: return True if card_deck in memory: return memory[card_deck] top = putOnPileBeforeLast(card_deck) left = putOnThirdPileBeforeLast(card_deck) memory[card_deck] = top or left return top or left if __name__ == '__main__': main() ```
87,870
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` memo = {} n = int(input()) a = [s for s in input().split()] def ok(c1, c2): return (c1[0] == c2[0]) or (c1[1] == c2[1]) def dfs(i, c1, c2, c3): k = (i, c1, c2, c3) if k in memo: return memo[k] if i < 0: memo[k] = ok(c1, c2) and ok(c1, c3) else: r = False if ok(c1, a[i]): r = r or dfs(i - 1, c2, c3, c1) if ok(c1, c2): r = r or dfs(i - 1, c1, c3, a[i]) memo[k] = r return memo[k] def check(): if n == 1: return True; elif n == 2: return ok(a[1], a[0]) else: return dfs(n - 4, a[-1], a[-2], a[-3]) print('YES' if check() else 'NO') # Made By Mostafa_Khaled ```
87,871
Provide tags and a correct Python 3 solution for this coding contest problem. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Tags: dfs and similar, dp Correct Solution: ``` res = {'': 1} def valid(a): if len(a) == 2: return 1 if a in res: return res[a] if (a[-1] == a[-3] or a[-2] == a[-4]): if (valid(a[:-4] + a[-2:])): res[a] = 1 return 1 if (len(a) > 7 and (a[-1] == a[-7] or a[-2] == a[-8])): if (valid(a[:-8] + a[-2:] + a[-6:-2])): res[a] = 1 return 1 res[a] = 0 return 0 n = int(input()) s = input() s = s.replace(' ', '') print("YES" if valid(s) else "NO") ```
87,872
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` n = int(input()) arr = list(input().split()) def evl(c1, c2): return c1[0] == c2[0] or c1[1] == c2[1] vt = set() q = [arr[:]] while q: ar = q.pop() vt.add(''.join(ar)) if len(ar) > 3 and evl(ar[-1], ar[-4]): tmp = ar[:len(ar) - 1] tmp[-3] = ar[-1] if ''.join(tmp) not in vt: q.append(tmp) if len(ar) > 1 and evl(ar[-1], ar[-2]): tmp = ar[:len(ar) - 1] tmp[-1] = ar[-1] if len(tmp) == 1: print('YES') exit(0) elif ''.join(tmp) not in vt: q.append(tmp) print('NO' if n > 1 else 'YES') ``` Yes
87,873
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` memo = {} n = int(input()) a = [s for s in input().split()] def ok(c1, c2): return (c1[0] == c2[0]) or (c1[1] == c2[1]) def dfs(i, c1, c2, c3): k = (i, c1, c2, c3) if k in memo: return memo[k] if i < 0: memo[k] = ok(c1, c2) and ok(c1, c3) else: memo[k] = dfs(i - 1, c1, c3, a[i]) or dfs(i - 1, c2, c3, c1) return memo[k] def check(): if n == 1: return True; elif n == 2: return ok(a[0], a[1]) else: return dfs(n - 4, a[-1], a[-2], a[-3]) print('YES' if check() else 'NO') ``` No
87,874
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` n = int(input()) c = 0 s = input().split(" ") def m(): global c for i in range(len(s)): for j in range(i + 1, len(s)): if s[i][0] != s[j][0] and s[i][1] != s[j][1]: c += 1 if c > 1 or len(s) == 2: return "NO" return "YES" print(m()) ``` No
87,875
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` n = int(input()) a = input().split() z = [1] for i in range(1, n): z.append(1) if (a[i][1] == a[i-1][1]) or (a[i][0] == a[i-1][0]): z[i] += z[i-1] if (i > 2) and ((a[i][1] == a[i-3][1]) or (a[i][0] == a[i-3][0])): z[i] = max(z[i], z[i-3] + 1) if z[-1] == n: print("YES") else: print("NO") #print(z) ``` No
87,876
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: * A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; * Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; * The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer n (1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. Output On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. Examples Input 4 2S 2S 2C 2C Output YES Input 2 3S 2C Output NO Note In the first sample you can act like that: * put the 4-th pile on the 1-st one; * put the 3-rd pile on the 2-nd one; * put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire. Submitted Solution: ``` d = dict() def mem(i,v1,v3): if(i == 1): print("YES") exit(0) if((i,v1,v3) in d): return if(i == 3): if(v1[0] == v3[0] or v1[1] == v3[1]): mem(i-1,a[i-1],v3) elif(v1[0] == a[i-1][0] or v1[1] == a[i-1][1]): mem(i-1,a[i-1],v3) if(i > 3 and (v3[0] == a[i-1][0] or v3[1] == a[i-1][1])): mem(i-1,v1,a[i-1]) d[(i,v1,v3)] = 0 n = int(input()) a = list(input().split()) v1 = a[0] if(n > 3): v3 = a[2] else: v3 = 0 mem(n,v1,v3) print("NO") ``` No
87,877
Provide tags and a correct Python 3 solution for this coding contest problem. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Tags: dp, games Correct Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(785) for i in range(300): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=int(input()) if li==0: print("BitAryo") else: print("BitLGM") ```
87,878
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if i==0: flag=1 if flag==1 and ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(785) for i in range(300): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=int(input()) if li==0: print("BitAryo") else: print("BitLGM") ``` No
87,879
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if i==0: flag=1 if flag==1 and ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(600) for i in range(100): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=input() if li==0: print("BitAryo") else: print("BitLGM") ``` No
87,880
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` n = int(input()) ss = input() from math import * import sys l = [int(s) for s in ss.split()] l.sort() if n == 1: ganaA = (l[0] != 0) elif n == 2: res = [[-1]*(l[0]+1) for i in range(0,l[1]+1)] for i in range(0, len(res)): for j in range(0, len(res[0])): if res[i][j] == -1: res[i][j] = 0 i1, j1, i2, j2 = i+1, j+1, i+1, j+1 while i1 <= l[1]: res[i1][j] = 1 i1 += 1 while j1 <= l[0]: res[i][j1] = 1 j1 += 1 while (i2 <= l[1]) and (j2 <= l[0]): res[i2][j2] = 1 i2 += 1 j2 += 1 ganaA = res[l[1]][l[0]] == 1 elif n == 3: res = [[[-1]*(l[0]+1) for i in range(l[1]+1)] for j in range(l[2] + 1)] for i in range(len(res)): for j in range(i,len(res[0])): for k in range(j,len(res[0][0])): if res[i][j][k] == -1: res[i][j][k] = 0 i1, j1, k1, i2, j2, k2 = i+1, j+1, k+1, i+1, j+1, k+1 while i1 <= l[2]: res[i1][j][k] = 1 i1 += 1 while j1 <= l[1]: res[i][j1][k] = 1 j1 += 1 while k1 <= l[0]: res[i][j][k1] = 1 k1 += 1 while (i2 <= l[2]) and (j2 <= l[1]) and (k1 <= l[0]): res[i2][j2][k2] = 1 i2 += 1 j2 += 1 k2 += 1 ganaA = res[l[2]][l[1]][l[0]] == 1 if ganaA: print("BitLGM") else : print("BitAryo") ``` No
87,881
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games! Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games. BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: * Take one of the integers (we'll denote it as ai). Choose integer x (1 ≤ x ≤ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. * Choose integer x <image>. And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses. You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence. Input The first line contains an integer n (1 ≤ n ≤ 3). The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). Output Write the name of the winner (provided that both players play optimally well). Either "BitLGM" or "BitAryo" (without the quotes). Examples Input 2 1 1 Output BitLGM Input 2 1 2 Output BitAryo Input 3 1 2 1 Output BitLGM Submitted Solution: ``` from math import * n=int(input()) if n==3: li=list(map(int,input().split())) ans=0 flag=0 for i in li: ans^=i if i==0: flag=1 if flag==1 and ans==0: print("BitAryo") else: print("BitLGM") elif n==2: li=list(map(int,input().split())) li.sort() phi=(1+sqrt(5))/2 ch=[0]*(600) for i in range(100): a=floor(phi*i) b=floor((phi**2)*i) ch[a]=b ch[b]=a if ch[li[0]]==li[1]: print("BitAryo") else: print("BitLGM") else: li=int(input()) if li==0: print("BitAryo") else: print("BitLGM") ``` No
87,882
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) b=0 c=0 d=0 for i in range(1,n): if a[i]!=a[i-1]: c+=1-(i-b+d)%2 d=(i-b+d)//2 j=a[i-1]+1 while j<a[i] and d>0: c+=1-d%2 d//=2 j+=1 c+=a[i]-j b=i c+=1-(n-b+d)%2 d=(n-b+d)//2 j=a[-1]+1 while d>0: c+=1-d%2 d//=2 j+=1 print(c+a[0]) ```
87,883
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` n=int(input()) a=map(int,input().split()) st=set() for e in a : while(e in st): st.remove(e) e+=1 st.add(e) print(max(st)-len(st)+1) ```
87,884
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` from heapq import heapify, heappush, heappop n = int(input()) a = list(map(int, input().split())) q = []; cnt = 1; d = {} for i in range(1, n): if a[i] == a[i-1]: cnt += 1 else: q.append(a[i-1]) d[a[i-1]] = cnt cnt = 1 q.append(a[-1]) d[a[-1]] = cnt heapify(q); maxpow = 0 while len(q) != 0: x = heappop(q) maxpow = max(maxpow, x) bineq = bin(d[x])[2:][::-1] #print(x, d[x], bineq) for i in range(len(bineq)): if i == 0: if bineq[i] == '0': del d[x+i] else: d[x+i] = 1 else: if bineq[i] == '1': if x+i in d: d[x+i] += 1 else: d[x+i] = 1 heappush(q, x+i) print(maxpow + 1 - len(d)) ```
87,885
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` # array de indices em ordem crescente # quantos 2**b precisa para 2**v-1 (soma de sequencia) import math def main(): n = int(input()) a = input().split() a = [int(x) for x in a] p = 0 carry = 0 ok = 0 while p<n: count = carry atual = a[p] while p<n and a[p] == atual: count += 1 p += 1 if count%2 == 1: ok += 1 carry = count//2 if p<n: proximo = a[p] else: proximo = atual-1 while carry!= 0 and proximo!= atual+1: if carry%2 == 1: ok += 1 carry = carry//2 atual += 1 print(atual-ok+1) main() ```
87,886
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` from sys import stdin, stdout inputIdx = 0; input = stdin.read().strip().split(); def nextToken(): global inputIdx, input; token = input[inputIdx]; inputIdx += 1; return token; def main(): global inputIdx, input; while inputIdx < len(input): n = int( nextToken() ); arr = [ int( nextToken() ) for i in range(n) ]; diff = 0; cur = -1; cnt = 0; i = 0; ans = max(arr); while i < n or cur != -1: ans = max( ans, cur ); if ( i < n and cur != -1 and arr[i] == cur ) or ( cur == -1 ): j = i; while j < n and arr[i] == arr[j]: j += 1; cnt += j-i; cur = arr[i]; i = j; if (cnt&1) == 1: diff += 1; cur += 1; cnt //= 2; if cnt == 0: cur = -1; ans -= diff-1; print( int(ans) ); main(); ```
87,887
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` n = int(input()) seq = [int(x) for x in input().split()] def inseri(s, n): if n in s: s.remove(n) inseri(s, n+1) else: s.add(n) s = set() for i in seq: inseri(s, i) m = max(s) print(m-len(s)+1) ```
87,888
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` input() now = alc = ans = 0 for v in map(int, input().split()): while alc and now != v: ans += not (alc & 1) alc >>= 1 now += 1 ans += v - now alc += 1 now = v else: while alc: ans += not (alc & 1) alc >>= 1 print(ans) ```
87,889
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Tags: greedy, implementation Correct Solution: ``` from sys import stdin N = int(stdin.readline()) num = set() for b in map(int, stdin.readline().split()): while b in num: num.remove(b) b += 1 num.add(b) print(max(num) - len(num) + 1) ```
87,890
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` n = int(input()) s = list(map(int, input().split())) def ins(_set, n): if n in _set: _set.remove(n) ins(_set, n+1) else: _set.add(n) ss = set() for i in s: ins(ss, i) m = max(ss) print(m - len(ss) + 1) ``` Yes
87,891
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` input() a = list(map(int, input().split())) b = [] i = j = 0 while i < len(a): while j < len(a) and a[j] == a[i]: j += 1 if (j - i) % 2 == 1: b += [a[i]] i = j - (j - i) // 2 for k in range(i, j): a[k] += 1 print(b[-1] - len(b) + 1) ``` Yes
87,892
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` n = int(input()) I = map(int, input().split()) a = []; for val in I: a.append(val); la = len(a); b = []; c = []; d = -1; for i in range (0, la): if i == 0 or a[i] != a[i-1]: b.append(a[i]); c.append(1); d = d + 1; else: c[d] = c[d] + 1; d = d + 1; tot = 0; idx = 0; for i in range(0, d): while idx < b[i]: if tot < 2: break; else: tot = int(tot / 2); idx = idx + 1; if idx < b[i]: idx = b[i]; tot = c[i]; else: tot = tot + c[i]; while tot >= 2: tot = int(tot / 2); idx = idx + 1; idx = idx + 1; res = idx; #print (idx) st = []; tot = 0; idx = 0; for i in range (0, d): while idx < b[i]: if tot == 0: break; else: if tot == 1: st.append(idx); tot = 0; idx = idx + 1; else: if tot % 2 == 1: st.append(idx); tot = int(tot / 2); idx = idx + 1; if idx < b[i]: idx = b[i]; tot = c[i]; if tot % 2 == 1: st.append(idx); tot = int(tot / 2); idx = idx + 1; else: idx = b[i]; tot = tot + c[i]; if tot % 2 == 1: st.append(idx); tot = int(tot / 2); idx = idx + 1; while tot > 0: if tot % 2 == 1: st.append(idx); tot = int(tot / 2); idx = idx + 1; lst = len(st); print (res- lst) ``` Yes
87,893
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` n=int(input()) st=set() for e in map(int,input().split()): while(e in st): st.remove(e) e+=1 st.add(e) print(max(st)-len(st)+1) # Made By Mostafa_Khaled ``` Yes
87,894
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) c=1 v=0 p=a[0] for i in range(1,n): if c<=1 and p<a[i]: p=a[i] v+=c c=1 continue if a[i]==a[i-1]: c+=1 else: while (c>0) and p!=a[i]: v+=(c%2) c//=2 p+=1 c+=1 while (c>1): v+=(c%2) c//=2 p+=1 print(p-v) ``` No
87,895
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) a.append(int(1e10)) c=1 v=0 p=a[0] for i in range(n): if c<=1 and p<a[i]: p=a[i] v+=c if a[i]==a[i-1]: c+=1 else: while (c>0) and (i==n-1 or p!=a[i]): if c%2: v+=1 c//=2 p+=1 c=1 while (c>0): if c%2: v+=1 c//=2 p+=1 print(p-v) ``` No
87,896
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) s=set() mx=0 cnt=0 for i in a: k=int(i) if k in s: while k+1 in s : s.remove(int(k)) cnt=cnt-1 k=k+1 if k+1>mx: mx=k+1 else: cnt=cnt+1 if int(i)>mx: mx=int(i) s.add(k) print(mx-cnt+1) ``` No
87,897
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. Input The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7. In the second sample you need to add numbers 20, 21, 22. Submitted Solution: ``` from sys import stdin, stdout inputIdx = 0; input = stdin.read().strip().split(); def nextToken(): global inputIdx, input; token = input[inputIdx]; inputIdx += 1; return token; def main(): global inputIdx, input; while inputIdx < len(input): n = int( nextToken() ); fr = 0; cur = 0; ans = 0; for i in range(n): a = int( nextToken() ); if a <= cur: cur -= a; if cur == 0: fr += 1; cur = fr; else: to = max( fr, a ); ans += (to-fr); fr = to+1; cur = fr; if cur != fr: ans += 1; print( int(ans) ); main(); ``` No
87,898
Provide tags and a correct Python 3 solution for this coding contest problem. Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap. Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap. Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible? Input The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes. Output In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division. If there are multiple optimal ways to split the cubes into the heaps, print any of them. Examples Input 1 10 99 Output 1 2 1 Input 2 13 24 13 45 Output 4 1 2 2 1 Note In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one. In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings Correct Solution: ``` from sys import stdin n = int(stdin.readline()) a = [int(x) for x in stdin.readline().split()] a = sorted([(a[x], x) for x in range(n*2)]) group = {} for x,ind in a: if x in group: group[x].append(ind) else: group[x] = [ind] g2 = [] for x in group: g2.append([len(group[x]), group[x]]) g2.sort() left = 0 right = 0 board = [0 for x in range(n*2)] ind = 0 for x,l in g2: if x == 1: ind += 1 if left <= right: left += 1 board[l[0]] = '1' else: right += 1 board[l[0]] = '2' else: break if right > left: turn = True else: turn = False for x,l in g2[ind:]: left += 1 right += 1 if x%2 == 1: last = l.pop() if turn: board[last] = '1' else: board[last] = '2' turn = not turn for n in l[::2]: board[n] = '1' for n in l[1::2]: board[n] = '2' print(left*right) print(' '.join(board)) ```
87,899