message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Note that the order of the points inside the group of three chosen points doesn't matter. Input The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got. It is guaranteed that the coordinates of the points in the input strictly increase. Output Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 1 2 3 4 Output 4 Input 4 2 -3 -2 -1 0 Output 2 Input 5 19 1 10 20 30 50 Output 1 Note In the first sample any group of three points meets our conditions. In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}. In the third sample only one group does: {1, 10, 20}.
instruction
0
273
14
546
Tags: binary search, combinatorics, two pointers Correct Solution: ``` from collections import deque from math import factorial nd = input() nd = nd.split() n = int(nd[0]) d = int(nd[1]) xcorr = list(map(int, input().split())) dq = deque() g = 0 for x in xcorr: dq.append(x) while (x - dq[0]) > d: if len(dq) >= 4: g += (len(dq)-2)*(len(dq)-2-1)/2 #factorial(len(dq)-2)/(factorial(len(dq)-2-2)*2) dq.popleft() if len(dq) >= 3: g += len(dq)*(len(dq)-1)*(len(dq)-2)/2/3 #factorial(len(dq))/(factorial(len(dq)-3)*2*3) print(int(g)) ```
output
1
273
14
547
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Note that the order of the points inside the group of three chosen points doesn't matter. Input The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got. It is guaranteed that the coordinates of the points in the input strictly increase. Output Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 1 2 3 4 Output 4 Input 4 2 -3 -2 -1 0 Output 2 Input 5 19 1 10 20 30 50 Output 1 Note In the first sample any group of three points meets our conditions. In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}. In the third sample only one group does: {1, 10, 20}.
instruction
0
274
14
548
Tags: binary search, combinatorics, two pointers Correct Solution: ``` n, k = list(map(int, input().split())) lis = list(map(int, input().split())) def find(s, lis, a, k): l = s r = len(lis) - 1 while (r - l) > 1: mid = (l + r) // 2 if lis[mid] < a + k: l = mid else: r = mid if lis[r] <= a + k: return r return l nat = 0 def entekhab(y): return int(y * (y - 1) / 2) s = 0 for i in range(n): now = lis[i] loc = find(i, lis, now, k) if now + k >= lis[loc]: nat += entekhab(loc - i) print(int(nat)) ```
output
1
274
14
549
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Note that the order of the points inside the group of three chosen points doesn't matter. Input The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got. It is guaranteed that the coordinates of the points in the input strictly increase. Output Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 1 2 3 4 Output 4 Input 4 2 -3 -2 -1 0 Output 2 Input 5 19 1 10 20 30 50 Output 1 Note In the first sample any group of three points meets our conditions. In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}. In the third sample only one group does: {1, 10, 20}.
instruction
0
275
14
550
Tags: binary search, combinatorics, two pointers Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Mon May 16 17:14:14 2016 @author: Hangell """ from collections import deque str1=input() str2=input() n=int(str1.split()[0]) d=int(str1.split()[1]) numlist=str2.split() newdeque=deque() Sum=0 cha=0 way=0 for i in range(n): numlist[i]=int(numlist[i]) for i in range(n-1): cha=numlist[i+1]-numlist[i] newdeque.append(cha) Sum+=cha while Sum>d and len(newdeque)!=0: Sum-=newdeque.popleft() if len(newdeque)>1: way+=(len(newdeque)-1)*len(newdeque)/2 print(int(way)) ```
output
1
275
14
551
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Note that the order of the points inside the group of three chosen points doesn't matter. Input The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got. It is guaranteed that the coordinates of the points in the input strictly increase. Output Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 1 2 3 4 Output 4 Input 4 2 -3 -2 -1 0 Output 2 Input 5 19 1 10 20 30 50 Output 1 Note In the first sample any group of three points meets our conditions. In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}. In the third sample only one group does: {1, 10, 20}.
instruction
0
277
14
554
Tags: binary search, combinatorics, two pointers Correct Solution: ``` from sys import stdin,stdout,stderr def binary(lo,hi,value): global a ans=lo while lo<=hi: mid=(lo+hi)//2 if a[mid]<=value: ans=mid lo=mid+1 else:hi=mid-1 return ans n,d=map(int,input().split()) a=[int(x)for x in input().split()] ans=0 for i in range(n): lo=binary(i+1,n-1,a[i]+d) lo=(lo-i) ans=ans+lo*(lo-1)/2 print(int(ans)) ```
output
1
277
14
555
Provide tags and a correct Python 3 solution for this coding contest problem. Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: * Each piece should contain at least l numbers. * The difference between the maximal and the minimal number on the piece should be at most s. Please help Alexandra to find the minimal number of pieces meeting the condition above. Input The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105). The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109). Output Output the minimal number of strip pieces. If there are no ways to split the strip, output -1. Examples Input 7 2 2 1 3 1 2 4 1 2 Output 3 Input 7 2 2 1 100 1 100 1 100 1 Output -1 Note For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2]. For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
instruction
0
375
14
750
Tags: binary search, data structures, dp, two pointers Correct Solution: ``` def split(a,n,s,l): pieces = [] i = 1 tmpmin = a[0] tmpmax = a[0] tmppc = [a[0]] while i<n: if abs(a[i]-tmpmin)<=s and abs(a[i]-tmpmax)<=s: tmppc.append(a[i]) if a[i]<tmpmin: tmpmin=a[i] elif a[i]>tmpmax: tmpmax = a[i] else: pieces.append(tmppc) tmppc = [a[i]] tmpmin = a[i] tmpmax = a[i] i += 1 pieces.append(tmppc) fail = False for j in range(len(pieces)): if len(pieces[j])<l: if j>0: prevpc = pieces[j-1] minj = min(pieces[j]) maxj = max(pieces[j]) while len(pieces[j])<l: tmp = prevpc.pop() if abs(tmp-minj)<=s and abs(tmp-maxj)<=s: pieces[j].insert(0,tmp) if tmp<minj: minj=tmp elif tmp>maxj: maxj=tmp else: return -1 if len(prevpc)<l: return -1 else: return -1 return len(pieces) n,s,l = [int(s) for s in input().split()] a = [int(s) for s in input().split()] res = split(a,n,s,l) if res<0: a.reverse() res = split(a,n,s,l) print(res) ```
output
1
375
14
751
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
471
14
942
Tags: brute force, greedy, implementation Correct Solution: ``` n, k = map(int, input().split()) l = list(map(int, input().split())) l = sorted(l, reverse=True) s = 0 for _ in range(n): if l[0] <= 0: break if l[0] <= 2: l[0] -= min(l[0],2) s += 1 l[0] -= min(l[0],4) l = sorted(l, reverse=True) for _ in range(n): if l[0] <= 0: break l[0] -= min(l[0],2) l = sorted(l, reverse=True) for _ in range(n): if l[0] <= 0: break l[0] -= min(l[0],2) l = sorted(l, reverse=True) for _ in range(s): if l[0] <= 0: break l[0] -= min(l[0],1) l = sorted(l, reverse=True) if l[0] <= 0: print("YES") else: print("NO") ```
output
1
471
14
943
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
472
14
944
Tags: brute force, greedy, implementation Correct Solution: ``` n,m=map(int,input().split()) l=list(map(int,input().split())) p=n for i in range(m) : b=l[i]//4 l[i]=l[i]-4*min(p,l[i]//4) p=p-min(p,b) p1=n*2 for i in range(m) : b=l[i]//2 l[i]=l[i]-2*min(p1,l[i]//2) p1=p1-min(p1,b) p2=p+p1 p3=p for i in range(m) : b=l[i]//2 l[i]=l[i]-2*min(p2,l[i]//2) p2=p2-min(p2,b) k=0 p3+=p2 for i in range(m) : k=k+l[i] if k>p3 : print('NO') else : print('YES') ```
output
1
472
14
945
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
473
14
946
Tags: brute force, greedy, implementation Correct Solution: ``` import sys n, k = [ int(x) for x in input().split() ] arr = list( map( int, input().split()) ) cnt_2 = 2 * n cnt_4 = n cnt_1 = 0 i = 0 while(cnt_4 > 0 and i < k): tmp = int(arr[i] / 4) if(tmp > 0 and cnt_4 > 0): arr[i] = arr[i] - 4 * min(tmp, cnt_4) cnt_4 = cnt_4 - min(tmp, cnt_4) i = i + 1 if(cnt_4 > 0): cnt_2 = cnt_2 + cnt_4 cnt_1 = cnt_1 + cnt_4 cnt_4 = 0 i = 0 while(cnt_2 > 0 and i < k): tmp = int(arr[i] / 2) if(tmp > 0 and cnt_2 > 0): arr[i] = arr[i] - 2 * min(tmp, cnt_2) cnt_2 = cnt_2 - min(tmp, cnt_2) i = i + 1 if(cnt_2 > 0): cnt_1 = cnt_1 + cnt_2 cnt_2 = 0 i = 0 while(i < k): tmp = arr[i] if(tmp > cnt_1): print("NO") sys.exit(0) elif(tmp > 0 and tmp <= cnt_1): cnt_1 = cnt_1 - tmp arr[i] = arr[i] - tmp i = i + 1 print("YES") ```
output
1
473
14
947
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
474
14
948
Tags: brute force, greedy, implementation Correct Solution: ``` from math import ceil n,k = map(int, input().split()) a = list(map(int, input().split())) def solve(n,k,a): one,two,four = 0, n*2, n for size in a: while size >= 4: if four > 0: four -= 1 else: two -= 2 size -= 4 while size > 0: if four > 0: four -= 1 if size == 2: one += 1 elif size == 1: two += 1 break elif two > 0: two -= 1 size -= 2 elif one > 0: one -= 1 size -= 1 else: return "NO" return "YES" print(solve(n,k,sorted(a, reverse=True))) ```
output
1
474
14
949
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
475
14
950
Tags: brute force, greedy, implementation Correct Solution: ``` n,k = map(int,input().split(" ")) a = list(map(int,input().split(" "))) count = 0 for i in range(0,len(a)): if a[i]%2 == 0: count = count + 1 if k-count <= n*8-sum(a): if k-count < n and k==n*4: print("NO") else: print('YES') else:print('NO') ```
output
1
475
14
951
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
476
14
952
Tags: brute force, greedy, implementation Correct Solution: ``` def solve(a, n, k): cnt =[0 for i in range(5)] duplas, quads, sozinhos = 2*n, n, 0 for i in a: for j in range(k): while i >= 3: if quads > 0: i -= 4; quads -= 1 elif duplas > 0: i -= 2; duplas -= 1 else: return "no" if i > 0: cnt[i] += 1 while cnt[2]: if duplas > 0: duplas -= 1; cnt[2] -= 1 elif quads > 0: cnt[2] -= 1; quads -= 1; sozinhos += 1 else: cnt[2] -= 1; cnt[1] += 2 if cnt[1] > sozinhos + duplas + 2*quads: return "no" return "yes" n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] print(solve(a, n, k)) # 1522848694321 ```
output
1
476
14
953
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
477
14
954
Tags: brute force, greedy, implementation Correct Solution: ``` n, k = map(int, input().split()) a = list(map(int, input().split())) c = [0, 0, 0, 0] for t in a: c[0] += t//4 if t%4: c[t%4] += 1 c[0] += c[3] c[3] = 0 if c[0] > n: c[2] += 2*(c[0]-n) c[0] = n t = min(n-c[0], c[1], c[2]) c[0] += t c[1] -= t c[2] -= t t = min(n-c[0], (c[1]+1)//2) c[0] += t c[1] -= min(c[1], t*2) t = min(n-c[0], c[2]) c[0] += t c[2] -= min(c[2], t+t//2) c[2] += c[1] c[1] = 0 print("YES" if c[2] <= 2*n else "NO") ```
output
1
477
14
955
Provide tags and a correct Python 3 solution for this coding contest problem. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
instruction
0
478
14
956
Tags: brute force, greedy, implementation Correct Solution: ``` # encoding:utf-8 def main(): n, k = list(map(int, input().split())) nums = list(map(int, input().split())) seat_two = n * 2 seat_four = n seat_one = 0 n_four = sum([x // 4 for x in nums]) nums = [x % 4 for x in nums] n_two = sum([x // 2 for x in nums]) n_one = sum([x % 2 for x in nums]) #print(n_one, n_two, n_four) #print(seat_one, seat_two, seat_four) if seat_four >= n_four: # there is rest of 4 seat seat_four -= n_four n_four = 0 # break seat seat_one and seat_two seat_two += seat_four seat_one += seat_four seat_four = 0 else: # there is rest of 4 people n_four -= seat_four seat_four = 0 # break 4 people to 2, 2 n_two += n_four * 2 n_four = 0 #print(n_one, n_two, n_four) #print(seat_one, seat_two, seat_four) if seat_two >= n_two: seat_two -= n_two n_two = 0 if seat_two + seat_one >= n_one: print("YES") else: print("NO") else: n_two -= seat_two seat_two = 0 n_one += n_two * 2 if seat_one >= n_one: print("YES") else: print("NO") if __name__ == '__main__': main() 1 ```
output
1
478
14
957
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` def solve(n, k, a): fourSeatLeft = n twoSeatLeft = n * 2 for i in range(k): while a[i] >= 4: if fourSeatLeft > 0: fourSeatLeft -= 1 a[i] -= min(a[i], 4) else: break for i in range(k): while a[i] >= 3: if fourSeatLeft > 0: fourSeatLeft -= 1 a[i] -= min(a[i], 4) else: break for i in range(k): while a[i] >= 2: if twoSeatLeft > 0: twoSeatLeft -= 1 a[i] -= min(a[i], 2) else: break for i in range(k): while a[i] >= 1: if twoSeatLeft > 0: twoSeatLeft -= 1 a[i] -= min(a[i], 2) else: break twoSeatLeft += fourSeatLeft oneSeatLeft = fourSeatLeft fourSeatLeft = 0 for i in range(k): while a[i] >= 2: if twoSeatLeft > 0: twoSeatLeft -= 1 a[i] -= min(a[i], 2) else: break oneSeatLeft += twoSeatLeft twoSeatLeft = 0 for i in range(k): while a[i] >= 1: if oneSeatLeft > 0: oneSeatLeft -= 1 a[i] -= min(a[i], 1) else: break if sum(a) > 0: return 'NO' return 'YES' ##if __name__ == '__main__': ## ## import sys ## ## stdin = sys.stdin ## sys.stdin = open('cf839b.txt') ## ## testcaseNo = eval(input()) ## ## for c in range(testcaseNo): ## [n, k] = [eval(v) for v in input().split()] ## a = [eval(v) for v in input().split()] ## ## result = solve(n, k, a) ## ## print('Case#' + str(c + 1) + ':', end=' ') ## print(result) ## ## sys.stdin = stdin [n, k] = [eval(v) for v in input().split()] a = [eval(v) for v in input().split()] print(solve(n, k, a)) ```
instruction
0
479
14
958
Yes
output
1
479
14
959
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` #coding=utf-8 def test(): n, k = map(int, input().split()) a = list(map(int, input().split())) total = n * 8 place = 0 sum = 0 i = 0 temp = 0 flag = 0 while i < k: sum += a[i] if a[i] % 2 is 1: place += 1 else: flag += 1 if a[i] % 4 != 0: temp += 1 i += 1 if sum == total: if place!=0 or (flag%4==0 and n*4==k): print("NO") else: print("YES") else: if (sum + place) > total or (n*4==k and (flag//4)==place ): print("NO") else: print("YES") test() ```
instruction
0
480
14
960
Yes
output
1
480
14
961
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` import sys import os import math import re n,k = map(int,input().split()) sol = list(map(int,input().split())) totalSeats = 8*n middle = n leftRight = 2*n single = 0 for i in range(k): fours = min((sol[i]+1)//4,middle) #take the min of 4s needed + one empty and the middle middle -= fours sol[i] = max(0,sol[i]-4*fours) #soldiers remaining leftRight += middle single += middle for i in range(k): #now deal with the twos twos = min(sol[i]//2, leftRight) leftRight -= twos sol[i] = max(0, sol[i]-2*twos) #soldiers remaining single += leftRight for i in range(k): #all the singles that need a seat single -= sol[i] if single < 0: print('No') else: print('Yes') ```
instruction
0
481
14
962
Yes
output
1
481
14
963
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` n,k=map(int,input().split()) wuya=[int(x) for x in input().split()] n_4=sum([x//4 for x in wuya]) wuya=[x%4 for x in wuya] n_2=sum([x//2 for x in wuya]) n_1=sum([x%2 for x in wuya]) p4=n p4,n_4=p4-min(n_4,p4),n_4-min(n_4,p4) p2=2*n+p4 n_2+=2*n_4 p2,n_2=p2-min(n_2,p2),n_2-min(n_2,p2) p1=p4+p2 n_1+=n_2*2 if p1>=n_1: print('YES') else: print('NO') ```
instruction
0
482
14
964
Yes
output
1
482
14
965
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` debug = 0 read = lambda: map(int, input().split()) k, n = read() a = list(read()) cnt4 = k cnt2 = 2 * k for i in range(n): cnt = min(cnt4, a[i] // 4) a[i] -= cnt * 4 cnt4 -= cnt if debug: print(*a) for i in range(n): cnt = min(cnt2, a[i] // 2) a[i] -= cnt * 2 cnt2 -= cnt if debug: print(' '.join(map(str, a)), ' ', cnt2, cnt4) c = [0] * 20 for i in a: c[min(i, 19)] += 1 d = min(cnt4, c[1], c[2]) c[1] -= d c[2] -= d cnt4 -= d d = min(cnt2, c[2]) c[2] -= d cnt2 -= d d = min(cnt2, c[1]) c[1] -= d cnt2 -= d d = min(cnt4, c[2]) c[2] -= d cnt4 -= d if debug: print('cnt4 = ', cnt4) print('c[1] = ', c[1]) d = min(cnt4, (c[1] + 1) // 2) c[1] -= d * 2 cnt4 -= d print('YES' if sum(c[1:]) == 0 else 'NO') ```
instruction
0
483
14
966
No
output
1
483
14
967
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` n, k = map(int, input().split()) s = list(map(int, input().split())) q = n double = 2*n for i in range(k): if double>=s[i]//2+s[i]%2 : s[i]=0 double-=s[i]//2+s[i]%2 else: s[i]-=double*2 double=0 break for i in range(k): p = min(q, s[i]//4) q -= p s[i] -= (p*4) if q != 0: for i in range(k): if s[i] == 0: continue if s[i] == 1 and q != 0: q -= 0.5 s[i]=0 elif s[i] == 2: if q % 1 == 0.5: q -= 1.5 s[i]=0 elif q > 0: q -= 1 s[i]=0 elif s[i] >= 3 and q >= 1: q -= 1 s[i]=0 if double!=0: for i in range(k): if s[i]!=0: if (double>=s[i]//2+s[i]%2): double-=s[i]//2+s[i]%2 s[i]=0 else: print('NO') exit() for i in s: if i!=0: print('NO') exit() print('YES') ```
instruction
0
484
14
968
No
output
1
484
14
969
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` def main(): n,_=[int(i) for i in input().split(" ")] a=[int(i) for i in input().split(" ")] sides=n*2 mid=n single=0 for g in a: while g>0: if g>=4 and mid>0: mid-=1 g-=4 elif g>=2 and sides>0: sides-=1 g-=2 elif g>=2: #no sides g<4 mid-=1 single+=1 g-=2 elif g==1: if single>0: single-=1 g-=1 elif sides>0: sides-=1 g-=1 elif mid>0: mid-=1 g-=1 else: print("NO") return else: print("NO") return print("YES") main() ```
instruction
0
485
14
970
No
output
1
485
14
971
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}. <image> A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively. The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group. It is guaranteed that a1 + a2 + ... + ak ≤ 8·n. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Examples Input 2 2 5 8 Output YES Input 1 2 7 1 Output NO Input 1 2 4 4 Output YES Input 1 4 2 2 1 2 Output YES Note In the first sample, Daenerys can place the soldiers like in the figure below: <image> In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6). Submitted Solution: ``` def main(): n, k = map(int, input().split()) cnt, a4 = [0] * 4, 0 for a in map(int, input().split()): a4 += a // 4 cnt[a % 4] += 1 _, a1, a2, a3 = cnt if (a3 + a4) * 2 < a2: a4 += a3 a2 -= a4 * 2 n -= ((a2 + 2) // 3) if a2 > a1 * 3 else ((a2 + a1 + 3) // 4) print(("YES", "NO")[n < a4]) else: print(("YES", "NO")[n * 4 < (a4 + a3) * 2 + a2 + a1]) if __name__ == '__main__': main() ```
instruction
0
486
14
972
No
output
1
486
14
973
Provide tags and a correct Python 3 solution for this coding contest problem. A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement. How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7. Input Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk. Output Print the number of possible assignments, modulo 1000000007 = 109 + 7. Examples Input 4 1 5 5 2 3 7 7 3 Output 6 Input 5 1 10 2 10 3 10 4 10 5 5 Output 5 Note These are the possible assignments for the first example: * 1 5 3 7 * 1 2 3 7 * 5 2 3 7 * 1 5 7 3 * 1 2 7 3 * 5 2 7 3
instruction
0
487
14
974
Tags: combinatorics, dfs and similar, dsu, graphs, trees Correct Solution: ``` n = int(input()) m = 2 * n + 1 u = [[] for i in range(m)] v = [0] * m s = [0] * m d = 10 ** 9 + 7 y = 1 for j in range(n): a, b = map(int, input().split()) v[a] = b if a != b: s[b] += 1 u[b].append(a) for b in range(m): if not v[b]: x = 0 p = [b] while p: x += 1 a = p.pop() s[a] = -1 p += u[a] y = (x * y) % d for a in range(m): if s[a] == 0: b = v[a] while s[b] == 1: s[b] = -1 b = v[b] s[b] -= 1 for a in range(m): if s[a] == 1: y = (2 * y) % d while s[a]: s[a] = 0 a = v[a] print(y) ```
output
1
487
14
975
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement. How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7. Input Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk. Output Print the number of possible assignments, modulo 1000000007 = 109 + 7. Examples Input 4 1 5 5 2 3 7 7 3 Output 6 Input 5 1 10 2 10 3 10 4 10 5 5 Output 5 Note These are the possible assignments for the first example: * 1 5 3 7 * 1 2 3 7 * 5 2 3 7 * 1 5 7 3 * 1 2 7 3 * 5 2 7 3 Submitted Solution: ``` import sys input = sys.stdin.readline M = 10**9 + 7 n = int(input()) G = [list() for _ in range(2*n+1)] for _ in range(n): u, v = map(int, input().split()) G[u].append(v) G[v].append(u) visited = [False]*(2*n+1) sys.setrecursionlimit(10**6) E, V, loop = None, None, False def dfs(s): global E, V, loop V.add(s) visited[s] = True if len(G[s]) > 2: loop = True for v in G[s]: E += 1 if not visited[v]: dfs(v) ans = 1 for v in range(2*n+1): if not visited[v]: E, V = 1, set() loop = False dfs(v) cnt = 1 if E < 2*len(V): cnt = len(V) elif not loop: cnt = 2 ans = (ans*cnt) % M print(ans) ```
instruction
0
488
14
976
No
output
1
488
14
977
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement. How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7. Input Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk. Output Print the number of possible assignments, modulo 1000000007 = 109 + 7. Examples Input 4 1 5 5 2 3 7 7 3 Output 6 Input 5 1 10 2 10 3 10 4 10 5 5 Output 5 Note These are the possible assignments for the first example: * 1 5 3 7 * 1 2 3 7 * 5 2 3 7 * 1 5 7 3 * 1 2 7 3 * 5 2 7 3 Submitted Solution: ``` n = int(input()) m = 2 * n + 2 u = [[] for i in range(m)] v = [0] * m s = [0] * m d = 10 ** 9 + 7 y = 1 for j in range(n): a, b = map(int, input().split()) u[b].append(a) v[a] = b s[b] += 1 for b in range(m): if not v[b]: x = 0 p = [b] while p: x += 1 a = p.pop() s[a] = -1 p += u[a] y = (x * y) % d for a in range(m): if s[a] == 0: b = v[a] while s[b] == 1: b = v[b] s[b] -= 1 for a in range(m): if s[a] == 1 and a != v[a]: y = (2 * y) % d while s[a]: a, s[a] = v[a], 0 print(y) ```
instruction
0
489
14
978
No
output
1
489
14
979
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement. How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7. Input Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk. Output Print the number of possible assignments, modulo 1000000007 = 109 + 7. Examples Input 4 1 5 5 2 3 7 7 3 Output 6 Input 5 1 10 2 10 3 10 4 10 5 5 Output 5 Note These are the possible assignments for the first example: * 1 5 3 7 * 1 2 3 7 * 5 2 3 7 * 1 5 7 3 * 1 2 7 3 * 5 2 7 3 Submitted Solution: ``` #!/bin/python3 # template import sys from collections import defaultdict def read_ints(inp = sys.stdin): return list(map(int,next(inp).strip().split())) MOD = 10**9 + 7 def adjacency_lists_directed(e): """ form adjacency lists from sequences of (s,e) edges :warning doesn't handle multi-edges :param e: edge of non-directed graph :return: dict of adjacency vertices for each vertex """ e_al = defaultdict(set) for s,e in e: e_al[s].add(e) return e_al def sol1(edges): #revert edges = [(v, u) for u, v in edges] vs = set(u for u, _ in edges) adj = adjacency_lists_directed(edges) def count(u): # return # of arrangement and visited vertices # 1 if loop # 2 if cycle # nb nodes otherwise visited = set() stack = [u] loop = False cycle = False while stack: cur = stack.pop() ch = adj[cur] if cur in ch: loop = True if cur in visited: cycle = True continue visited.add(cur) stack.extend(ch) return (1 if loop else (2 if cycle else len(visited))), visited res = 1 while vs: # each connected component u = next(iter(vs)) c, vis = count(u) res = (res * c) % MOD vs -= vis return res def main(): # prevent variables at main scope for i in range(1): n, = read_ints() E = [] for _ in range(n): u,v = read_ints() E.append((u,v)) d = sol1(E) print(d) if __name__=='__main__': main() ```
instruction
0
490
14
980
No
output
1
490
14
981
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement. How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7. Input Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk. Output Print the number of possible assignments, modulo 1000000007 = 109 + 7. Examples Input 4 1 5 5 2 3 7 7 3 Output 6 Input 5 1 10 2 10 3 10 4 10 5 5 Output 5 Note These are the possible assignments for the first example: * 1 5 3 7 * 1 2 3 7 * 5 2 3 7 * 1 5 7 3 * 1 2 7 3 * 5 2 7 3 Submitted Solution: ``` n = int(input()) m = 2 * n + 2 u = [[] for i in range(m)] v = [0] * m s = [0] * m d = 10 ** 9 + 7 y = 1 for j in range(n): a, b = map(int, input().split()) u[b].append(a) v[a] = b s[b] += 1 for b in range(m): if not v[b]: x = 0 p = [b] while p: x += 1 a = p.pop() s[a] = -1 p += u[a] y = (x * y) % d for a in range(m): if s[a] == 0: b = v[a] while s[b] == 1: s[b] -= 1 b = v[b] s[b] -= 1 for a in range(m): if s[a] == 1 and a != v[a]: y = (2 * y) % d while s[a]: s[a] = 0 a = v[a] print(y) ```
instruction
0
491
14
982
No
output
1
491
14
983
Provide a correct Python 3 solution for this coding contest problem. $N$ persons visited a restaurant. The restaurant is open from 0 to $T$. The $i$-th person entered the restaurant at $l_i$ and left at $r_i$. Find the maximum number of persons during the business hours. Constraints * $ 1 \leq N \leq 10^5 $ * $ 1 \leq T \leq 10^5 $ * $ 0 \leq l_i < r_i \leq T $ Input The input is given in the following format. $N$ $T$ $l_1$ $r_1$ $l_2$ $r_2$ : $l_N$ $r_N$ Output Print the maximum number of persons in a line. Examples Input 6 10 0 2 1 3 2 6 3 8 4 10 5 10 Output 4 Input 2 2 0 1 1 2 Output 1
instruction
0
789
14
1,578
"Correct Solution: ``` from itertools import accumulate import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) N, T = map(int, input().split()) time = [0] * (T + 2) for _ in range(N): l, r = map(int, input().split()) time[l] += 1 time[r] -= 1 print(max(accumulate(time))) ```
output
1
789
14
1,579
Provide a correct Python 3 solution for this coding contest problem. $N$ persons visited a restaurant. The restaurant is open from 0 to $T$. The $i$-th person entered the restaurant at $l_i$ and left at $r_i$. Find the maximum number of persons during the business hours. Constraints * $ 1 \leq N \leq 10^5 $ * $ 1 \leq T \leq 10^5 $ * $ 0 \leq l_i < r_i \leq T $ Input The input is given in the following format. $N$ $T$ $l_1$ $r_1$ $l_2$ $r_2$ : $l_N$ $r_N$ Output Print the maximum number of persons in a line. Examples Input 6 10 0 2 1 3 2 6 3 8 4 10 5 10 Output 4 Input 2 2 0 1 1 2 Output 1
instruction
0
791
14
1,582
"Correct Solution: ``` #!/usr/bin/env python3 # DSL_5_A: The Maximum Number of Customers def run(): n, t = [int(i) for i in input().split()] sch = [0] * (t+1) for _ in range(n): enter, leave = [int(i) for i in input().split()] sch[enter] += 1 sch[leave] -= 1 c = 0 maxc = 0 for i in sch: c += i if c >= maxc: maxc = c print(maxc) if __name__ == '__main__': run() ```
output
1
791
14
1,583
Provide a correct Python 3 solution for this coding contest problem. $N$ persons visited a restaurant. The restaurant is open from 0 to $T$. The $i$-th person entered the restaurant at $l_i$ and left at $r_i$. Find the maximum number of persons during the business hours. Constraints * $ 1 \leq N \leq 10^5 $ * $ 1 \leq T \leq 10^5 $ * $ 0 \leq l_i < r_i \leq T $ Input The input is given in the following format. $N$ $T$ $l_1$ $r_1$ $l_2$ $r_2$ : $l_N$ $r_N$ Output Print the maximum number of persons in a line. Examples Input 6 10 0 2 1 3 2 6 3 8 4 10 5 10 Output 4 Input 2 2 0 1 1 2 Output 1
instruction
0
792
14
1,584
"Correct Solution: ``` from itertools import accumulate n,t=map(int,input().split()) timeline=[0 for i in range(t+1)] for i in range(n): a,b=map(int,input().split()) timeline[a]+=1 timeline[b]-=1 print(max(accumulate(timeline))) ```
output
1
792
14
1,585
Provide a correct Python 3 solution for this coding contest problem. $N$ persons visited a restaurant. The restaurant is open from 0 to $T$. The $i$-th person entered the restaurant at $l_i$ and left at $r_i$. Find the maximum number of persons during the business hours. Constraints * $ 1 \leq N \leq 10^5 $ * $ 1 \leq T \leq 10^5 $ * $ 0 \leq l_i < r_i \leq T $ Input The input is given in the following format. $N$ $T$ $l_1$ $r_1$ $l_2$ $r_2$ : $l_N$ $r_N$ Output Print the maximum number of persons in a line. Examples Input 6 10 0 2 1 3 2 6 3 8 4 10 5 10 Output 4 Input 2 2 0 1 1 2 Output 1
instruction
0
795
14
1,590
"Correct Solution: ``` def main(): n,t = map(int,input().split()) t+=1 T = [0]*t for _ in range(n): l,r = map(int,input().split()) T[l]+=1 T[r]-=1 num = 0 res = 0 for i in range(t): num += T[i] res = max(num,res) print (res) if __name__ == '__main__': main() ```
output
1
795
14
1,591
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
869
14
1,738
Tags: dfs and similar, graphs, trees Correct Solution: ``` import math as mt import sys,string input=sys.stdin.readline L=lambda : list(map(int,input().split())) Ls=lambda : list(input().split()) M=lambda : map(int,input().split()) I=lambda :int(input()) from collections import defaultdict def dfs(n,dx): vis[n]=1 dis[n]=dx for i in adj[n]: if(vis[i]==0): dfs(i,dx+1) n=I() vis=[0]*(n+1) dis=[0]*(n+1) l=[] adj=[] for i in range(n+1): adj.append([]) for i in range(n): d=I() if(d==-1): l.append(i+1) else: adj[d].append(i+1) for i in l: dfs(i,1) print(max(dis)) ```
output
1
869
14
1,739
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
870
14
1,740
Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys from collections import deque class Graph(object): def __init__(self, n): self.root = [] self.adj = {} self.make_adj(n) def make_adj(self, n): for i in range(1, n+1): self.adj[i] = [] def adj_list(self, u): return self.adj[u] class BFSResult: def __init__(self): self.level = {} self.parent = {} self.max_level = None def bfs(g, s, r): r.parent[s] = None r.level[s] = 0 queue = deque() queue.append(s) while queue: u = queue.popleft() for n in g.adj_list(u): if n not in r.level: r.parent[n] = u r.level[n] = r.level[u] + 1 r.max_level = r.level[u] + 1 queue.append(n) #return r if __name__ == '__main__': n = int(input().rstrip()) g = Graph(n) i = 1 r = BFSResult() for _ in range(n): u = int(input().rstrip()) if u == -1: g.root.append(i) else: g.adj[u].append(i) i+=1 #print(g.root) for s in g.root: bfs(g, s,r) max_l = 0 for i in r.level: if r.level[i]>max_l: max_l = r.level[i] if max_l == 0: print(1) else: print(max_l+1) #print(r.level) ```
output
1
870
14
1,741
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
871
14
1,742
Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys sys.setrecursionlimit(100000000) n=int(input()) d={} de=[-1 for i in range(n+1)] for i in range(1,n+1): d[i]=int(input()) def depth(v): if de[v]!=-1: return de[v] if d[v]==-1: return 0 else: return 1+depth(d[v]) ma=-1 for i in d: t=depth(i) de[i]=t if de[i]>ma: ma=de[i] print(ma+1) ```
output
1
871
14
1,743
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
872
14
1,744
Tags: dfs and similar, graphs, trees Correct Solution: ``` def find(i) : t = 0 while par[i] != 0 : i = par[i] t+=1 return (t) n = int(input()) t = 0 par = [0 for i in range (n+1)] d = {} for _ in range (1,n+1) : x = int(input()) if x != -1 : par[_] = x else : par[_] = 0 #print(par[1:]) ans = 0 for i in range (1,n+1) : r = find(i) t = 0 ans = max(r , ans ) #print(r) print(ans+1) ```
output
1
872
14
1,745
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
873
14
1,746
Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys import threading from collections import defaultdict n=int(input()) adj=defaultdict(list) for i in range(n): adj[int(input())].append(i+1) def fun(root,par,adj): ans=0 for ch in adj.get(root,[]): if ch != par: ans=max(ans,fun(ch,root,adj)) return 1+ans def main(): ans=-1 for i in adj[-1]: ans=max(ans,fun(i,-1,adj)) print(ans) if __name__=="__main__": sys.setrecursionlimit(10**6) threading.stack_size(10**8) t = threading.Thread(target=main) t.start() t.join() ```
output
1
873
14
1,747
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
874
14
1,748
Tags: dfs and similar, graphs, trees Correct Solution: ``` import math import sys p = [-1] * 5000 n = int(input()) f = -999999999 for i in range(0, n): x = int(input()) p[i + 1] = x for i in range(1, n + 1): y = i ans = 1 while(p[y] != -1): ans = ans + 1 y = p[y] f = max(f, ans) print(f) ```
output
1
874
14
1,749
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
875
14
1,750
Tags: dfs and similar, graphs, trees Correct Solution: ``` from collections import deque n = int(input()) g = [[] for i in range(n)] for i in range(n): finish = int(input()) - 1 if finish >= 0: g[i].append(finish) visited2 = [] def dfs(start): visited.append(start) for k in g[start]: if k not in visited: dfs(k) visited2.append(len(visited)) for i in range(n): visited = [] dfs(i) print(max(visited2)) ```
output
1
875
14
1,751
Provide tags and a correct Python 3 solution for this coding contest problem. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5
instruction
0
876
14
1,752
Tags: dfs and similar, graphs, trees Correct Solution: ``` n = int(input()) p=[int(input()) for i in range(n)] g=0 c=0 for i in range(n): c=0 while i>=0: i=p[i]-1 c+=1 g=max(g,c) print(g) ```
output
1
876
14
1,753
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` import sys import math def dfs(graph, start): stack = [start] c = 0 while stack: vertex = stack.pop() c += 1 stack.extend(graph[vertex]) return c n = int(input()) d = [] for i in range(n + 1): d.append(set()) vmax = 0 for i in range(1, n + 1): v = int(input()) if(v != -1): d[i].add(v) for i in range(1, n + 1): k = dfs(d, i) if(k > vmax): vmax = k print(vmax) ```
instruction
0
877
14
1,754
Yes
output
1
877
14
1,755
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` def BFS_level(vertex): s=[(vertex,1)] while(s): n=s.pop(0) for i in range(len(d[n[0]])): s.append((d[n[0]][i],n[1]+1)) if len(s)==0 and len(d[n[0]])==0: return n[1] n=int(input()) d={i+1:[] for i in range(n)} roots=[] for i in range(1,n+1): a=int(input()) if a==-1: roots.append(i) else: d[a].append(i) max_level=0 for i in roots: w=BFS_level(i) if w>max_level: max_level=w print(max_level) ```
instruction
0
878
14
1,756
Yes
output
1
878
14
1,757
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` n=int(input());r=[];m=0 for _ in [0]*n: r.append(int(input())) for i in range(n): a=i;l=0 while r[a]!=-1: a=r[a]-1;l+=1 m=max(m,l) print(m+1) ```
instruction
0
879
14
1,758
Yes
output
1
879
14
1,759
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` n = int(input()) freedom = [] # myBoss = [[] for x in range(n+1)] Imboss = [[] for x in range(n+1)] for i in range(n): inn = int(input()) if inn == -1: # myBoss[i+1] = -1 freedom.append(i+1) else: # myBoss[i+1].append(inn) Imboss[inn].append(i+1) temp = [] group =0 while(len(freedom)!=0): group+=1 for f in freedom: temp = temp + Imboss[f] freedom = temp[::] temp=[] print(group) ```
instruction
0
880
14
1,760
Yes
output
1
880
14
1,761
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` def inc_level_sons(emp): for e in emps[emp]: emps_level[e]+=1 #inc_level_sons(e) n = int(input()) emps = [[]] * 2005 emps_level = [1] * 2005 for i in range(n): e = int(input()) if e != -1: emps[e].append(i+1) emps_level[i+1] = emps_level[e] + 1 #inc_level_sons(i+1) print(str(max(emps_level))) ```
instruction
0
881
14
1,762
No
output
1
881
14
1,763
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` n = int(input()) m = [[0,0]]*n for i in range(n): sup = int(input()) if sup == -1: m[i] = [i+1, 1] else: m[i] = [i+1, m[sup-1][1]+1] max = 0 for i in range(n): if m[i][1]>max: max = m[i][1] print(max) ```
instruction
0
882
14
1,764
No
output
1
882
14
1,765
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` def dfs(graph, node, visited, height, parent): visited[node] = True if parent != -1: height[node] = height[parent] + 1 for i in graph[node]: if not visited[i]: dfs(graph, i, visited, height, node) v = int(input()) graph = [] for i in range(v+1): graph.append([]) edges = [] for i in range(v): edges.append(int(input())) for i in range(len(edges)): if edges[i] != -1: graph[edges[i]].append(i+1) height = [1]*(v+1) visited = [False]*(v+1) for i in range(len(graph)): if not visited[i]: dfs(graph, i, visited, height, -1) print(max(height)) ```
instruction
0
883
14
1,766
No
output
1
883
14
1,767
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true: * Employee A is the immediate manager of employee B * Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B. What is the minimum number of groups that must be formed? Input The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Examples Input 5 -1 1 2 1 -1 Output 3 Note For the first example, three groups are sufficient, for example: * Employee 1 * Employees 2 and 4 * Employees 3 and 5 Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Tue Nov 17 13:44:48 2020 @author: pctab """ from collections import defaultdict graph=defaultdict(set) n=int(input()) for i in range(n): m_i=int(input()) if m_i!=-1: graph[m_i].add(i+1) else: graph[i+1] max_depth=0 def dfs(visited,u,l): global max_depth visited[u]=True max_depth=max(max_depth,l) for v in graph[u]: if not visited[v]: dfs(visited,v,l+1) visited=[False]*(n+1) for i in range(1,n+1): if not visited[i]: dfs(visited,i,1) print(max_depth) ```
instruction
0
884
14
1,768
No
output
1
884
14
1,769
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into n rows, each row consists of m seats. There are k people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (xi, yi), where xi is the row number, and yi is the seat number in this row. It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (x1, y1), then when he comes to the box office, he chooses such empty seat (x2, y2), which satisfies the following conditions: * the value of |x1 - x2| + |y1 - y2| is minimum * if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of x2 is minimum * if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of y2 is minimum Your task is to find the coordinates of a seat for each person. Input The first input line contains three integers n, m, k (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ min(n·m, 105) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next k lines contains two integers xi, yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line). Output Print k lines, each containing a pair of integers. Print on the i-th line xi, yi — the coordinates of the seat, for which the person who stands i-th in the line will buy the ticket. Examples Input 3 4 6 1 1 1 1 1 1 1 2 1 3 1 3 Output 1 1 1 2 2 1 1 3 1 4 2 3 Input 4 3 12 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Output 2 2 1 2 2 1 2 3 3 2 1 1 1 3 3 1 3 3 4 2 4 1 4 3 Submitted Solution: ``` from datetime import * time1 = datetime.now() import sys try: file = open('test.in');fin=file.readline except: fin = sys.stdin.readline n,m,k=map(int,fin().split()) dis=[0]*(m+2)*(n+2) mulm=[(m+2)*i for i in range(n+2)] f1=dict() f2=dict() ansx,ansy=0,0 rangeIJ=((i,j) for j in range(-1,2) for i in range(-1,2)) def renew(x,y,d,i): global ansx,ansy yy = i%(m+2); xx = i//(m+2) if (xx<=n)and(yy>0)and(yy<=m)and(abs(xx-x)+abs(yy-y)<=d): if (xx<ansx)or(xx==ansx)and(yy<ansy): ansx, ansy = xx, yy def findset1(x): if x in f1:f1[x]=findset1(f1[x]) return f1[x] if (x in f1) else x def findset2(x): if x in f2:f2[x]=findset2(f2[x]) return f2[x] if (x in f2) else x def check(x,y,d): global ansx,ansy ansx,ansy=3000,3000 x0 = max(1,x-d);y0 = x-x0+y-d if(y0>0):renew(x,y,d,findset1(mulm[x0]+y0)) y0 = d-x+x0+y if(y0<=m):renew(x,y,d,findset2(mulm[x0]+y0)) if ansx<3000: #print(ansx,ansy) ind=mulm[ansx]+ansy ind1=mulm[ansx+1]+ansy f1[ind] = ind1-1 f2[ind] = ind1+1 return 1 y0 = max(y-d,1); x0 = d+x-y+y0 if(x0<=n):renew(x,y,d,findset2(mulm[x0]+y0)) y0 = min(y+d,m); x0 = d+x-y0+y if(x0<=n):renew(x,y,d,findset1(mulm[x0]+y0)) if ansx<3000: #print(ansx,ansy) ind=mulm[ansx]+ansy ind1=mulm[ansx+1]+ansy f1[ind] = ind1-1 f2[ind] = ind1+1 return 1 return 0 for _ in range(k): x,y=map(int,fin().split()) ind0=mulm[x]+y for i,j in rangeIJ: dis[ind0]=max(dis[ind0],dis[mulm[x+i]+y+j]-abs(i)-abs(j)) while 1: if(check(x,y,dis[ind0])):break dis[ind0]+=1 print(datetime.now()-time1) try:file.close() except:pass ''' import sys import random fout=open('test.in','w') fout.write('2000 2000 100000\n') for i in range(100000): fout.write(str(random.randint(417,419))+' '+str(random.randint(1306,1308))+'\n') ''' ```
instruction
0
1,127
14
2,254
No
output
1
1,127
14
2,255
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into n rows, each row consists of m seats. There are k people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (xi, yi), where xi is the row number, and yi is the seat number in this row. It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (x1, y1), then when he comes to the box office, he chooses such empty seat (x2, y2), which satisfies the following conditions: * the value of |x1 - x2| + |y1 - y2| is minimum * if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of x2 is minimum * if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of y2 is minimum Your task is to find the coordinates of a seat for each person. Input The first input line contains three integers n, m, k (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ min(n·m, 105) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next k lines contains two integers xi, yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line). Output Print k lines, each containing a pair of integers. Print on the i-th line xi, yi — the coordinates of the seat, for which the person who stands i-th in the line will buy the ticket. Examples Input 3 4 6 1 1 1 1 1 1 1 2 1 3 1 3 Output 1 1 1 2 2 1 1 3 1 4 2 3 Input 4 3 12 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Output 2 2 1 2 2 1 2 3 3 2 1 1 1 3 3 1 3 3 4 2 4 1 4 3 Submitted Solution: ``` n, m, k = map(int, input().split()) step = {} seki = set() an = [] def walk(x, y): i, j, k = step.get((x, y), (0, 0, 1)) if k==1: k += 1 else: k -= 1 j += 1 if j>2*i: i += 1 j = 0 while True: while j<=2*i: n_x = j-i m_x = n_x+x if n>=m_x>0: while k<=2: if n_x<0: if k==1: m_y = -n_x-i+y else: m_y = n_x+i+y else: if k==1: m_y = n_x-i+y else: m_y = i-n_x+y if m>=m_y>0 and (m_x, m_y) not in seki: step[(x, y)] = (i, j, k) seki.add((m_x, m_y)) return m_x, m_y k += 1 k = 1 j += 1 j = 0 i += 1 ''' x, y = map(int, input().split()) inps = [[x, y, 1]] for i in range(k-1): x, y = map(int, input().split()) if x!=inps[-1][0] or y!=inps[-1][1]: inps.append([x, y, 1]) else: inps[-1][2]+=1 for i in inps: for j in range(i[2]): an.append(walk(i[0], i[1])) ''' x, y = map(int, input().split()) if x==552 and y==1028: for i in range(60000): an.append(walk(552, 1028)) for i in range(k-60000): x, y = map(int, input().split()) an.append(walk(x, y)) else: an.append(walk(x, y)) for i in range(k-1): x, y = map(int, input().split()) an.append(walk(x, y)) for a in an: print(a[0], a[1]) ```
instruction
0
1,128
14
2,256
No
output
1
1,128
14
2,257
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into n rows, each row consists of m seats. There are k people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (xi, yi), where xi is the row number, and yi is the seat number in this row. It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (x1, y1), then when he comes to the box office, he chooses such empty seat (x2, y2), which satisfies the following conditions: * the value of |x1 - x2| + |y1 - y2| is minimum * if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of x2 is minimum * if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of y2 is minimum Your task is to find the coordinates of a seat for each person. Input The first input line contains three integers n, m, k (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ min(n·m, 105) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next k lines contains two integers xi, yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line). Output Print k lines, each containing a pair of integers. Print on the i-th line xi, yi — the coordinates of the seat, for which the person who stands i-th in the line will buy the ticket. Examples Input 3 4 6 1 1 1 1 1 1 1 2 1 3 1 3 Output 1 1 1 2 2 1 1 3 1 4 2 3 Input 4 3 12 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Output 2 2 1 2 2 1 2 3 3 2 1 1 1 3 3 1 3 3 4 2 4 1 4 3 Submitted Solution: ``` n, m, k = map(int, input().split()) step = {} seki = set() an = [] def walk(x, y): i, j, k = step.get((x, y), (0, 0, 1)) if k==1: k += 1 else: k -= 1 j += 1 if j>2*i: i += 1 j = 0 while True: while j<=2*i: n_x = j-i m_x = n_x+x if n>=m_x>0: while k<=2: if n_x<0: if k==1: m_y = -n_x-i+y else: m_y = n_x+i+y else: if k==1: m_y = n_x-i+y else: m_y = i-n_x+y if m>=m_y>0 and (m_x, m_y) not in seki: step[(x, y)] = (i, j, k) seki.add((m_x, m_y)) return m_x, m_y k += 1 k = 1 j += 1 j = 0 i += 1 for i in range(k): x, y = map(int, input().split()) if x==552 and y==1028: for i in range(k): an.append(walk(552, 1028)) break an.append(walk(x, y)) for a in an: print(a[0], a[1]) ```
instruction
0
1,129
14
2,258
No
output
1
1,129
14
2,259
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into n rows, each row consists of m seats. There are k people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (xi, yi), where xi is the row number, and yi is the seat number in this row. It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (x1, y1), then when he comes to the box office, he chooses such empty seat (x2, y2), which satisfies the following conditions: * the value of |x1 - x2| + |y1 - y2| is minimum * if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of x2 is minimum * if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of y2 is minimum Your task is to find the coordinates of a seat for each person. Input The first input line contains three integers n, m, k (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ min(n·m, 105) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next k lines contains two integers xi, yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line). Output Print k lines, each containing a pair of integers. Print on the i-th line xi, yi — the coordinates of the seat, for which the person who stands i-th in the line will buy the ticket. Examples Input 3 4 6 1 1 1 1 1 1 1 2 1 3 1 3 Output 1 1 1 2 2 1 1 3 1 4 2 3 Input 4 3 12 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Output 2 2 1 2 2 1 2 3 3 2 1 1 1 3 3 1 3 3 4 2 4 1 4 3 Submitted Solution: ``` n,m,k = list(map(int,input().split())) booked_seats = [] for i in range(k): x,y = list(map(int,input().split())) booked_seat = [] if [x,y] not in booked_seats: booked_seat = [x,y] else: delta = 0 while(booked_seat == []): delta += 1 delta_x = delta+1 delta_y = -1 available_seats = [] while((delta_y <= delta) and (delta_x >= 0)): delta_x -= 1 delta_y += 1 x_new = x-delta_x y_new = y-delta_y if ([x_new,y_new] not in booked_seats) and (x_new * y_new > 0) and (x_new <= n) and (y_new <= m): if [x_new,y_new] not in available_seats: available_seats.append([x_new,y_new]) continue x_new = x-delta_x y_new = y+delta_y if ([x_new,y_new] not in booked_seats) and (x_new * y_new > 0) and (x_new <= n) and (y_new <= m): if [x_new,y_new] not in available_seats: available_seats.append([x_new,y_new]) continue x_new = x+delta_x y_new = y-delta_y if ([x_new,y_new] not in booked_seats) and (x_new * y_new > 0) and (x_new <= n) and (y_new <= m): if [x_new,y_new] not in available_seats: available_seats.append([x_new,y_new]) continue x_new = x+delta_x y_new = y+delta_y if ([x_new,y_new] not in booked_seats) and (x_new * y_new > 0) and (x_new <= n) and (y_new <= m): if [x_new,y_new] not in available_seats: available_seats.append([x_new,y_new]) continue if(available_seats != []): booked_seat = min(available_seats) booked_seats.append(booked_seat) print(booked_seat[0],booked_seat[1]) ```
instruction
0
1,130
14
2,260
No
output
1
1,130
14
2,261