message
stringlengths
2
48.6k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
318
108k
cluster
float64
8
8
__index_level_0__
int64
636
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively. Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number: * Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1. * Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1. * Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.) At least how much MP is needed to achieve the objective? Constraints * 3 \leq N \leq 8 * 1 \leq C < B < A \leq 1000 * 1 \leq l_i \leq 1000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A B C l_1 l_2 : l_N Output Print the minimum amount of MP needed to achieve the objective. Examples Input 5 100 90 80 98 40 30 21 80 Output 23 Input 8 100 90 80 100 100 90 90 90 80 80 80 Output 0 Input 8 1000 800 100 300 333 400 444 500 555 600 666 Output 243 Submitted Solution: ``` from itertools import* n,a,b,c,*l=map(int,open(0).read().split()) a=a,b,c m=10**18 for s in product(*[range(4)]*n): if all(i in s for i in(0,1,2)): t,c=[0]*4,[0]*4 for i,v in zip(s,l): t[i]+=v c[i]+=1 m=min(m,sum(abs(x-y)+~-z*10for x,y,z in zip(a,t,c))) print(m) ```
instruction
0
58,725
8
117,450
Yes
output
1
58,725
8
117,451
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively. Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number: * Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1. * Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1. * Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.) At least how much MP is needed to achieve the objective? Constraints * 3 \leq N \leq 8 * 1 \leq C < B < A \leq 1000 * 1 \leq l_i \leq 1000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A B C l_1 l_2 : l_N Output Print the minimum amount of MP needed to achieve the objective. Examples Input 5 100 90 80 98 40 30 21 80 Output 23 Input 8 100 90 80 100 100 90 90 90 80 80 80 Output 0 Input 8 1000 800 100 300 333 400 444 500 555 600 666 Output 243 Submitted Solution: ``` N,A,B,C = map(int, input().split()) l = [int(input()) for _ in range(N)] from itertools import combinations, permutations ans = float('inf') for i in range(3, N+1): for c in combinations(l,i): for l1,l2,*li in permutations(c): l3 = sum(li) for a,b,c in permutations([A,B,C]): cost = (len(li)-1)*10 cost += abs(l1-a) + abs(l2-b) + abs(l3-c) ans = min(ans, cost) print(ans) ```
instruction
0
58,727
8
117,454
No
output
1
58,727
8
117,455
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively. Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number: * Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1. * Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1. * Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.) At least how much MP is needed to achieve the objective? Constraints * 3 \leq N \leq 8 * 1 \leq C < B < A \leq 1000 * 1 \leq l_i \leq 1000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A B C l_1 l_2 : l_N Output Print the minimum amount of MP needed to achieve the objective. Examples Input 5 100 90 80 98 40 30 21 80 Output 23 Input 8 100 90 80 100 100 90 90 90 80 80 80 Output 0 Input 8 1000 800 100 300 333 400 444 500 555 600 666 Output 243 Submitted Solution: ``` import itertools n, a, b, c = map(int, input().split()) L = [int(input())for _ in range(n)] L_perm = list(itertools.permutations(L)) ans = 10**9 for l in L_perm: for i in range(1, n-1): A_tree = l[:i] synthesis_A = 10 * (i-1) fix_A = abs(a-sum(A_tree)) for j in range(i+1, n): B_tree = l[i:j] synthesis_B = 10 * (j-i-1) fix_B = abs(b-sum(B_tree)) for k in range(j+1, n+1): C_tree = l[j:k] synthesis_C = 10 * (k-j-1) fix_C = abs(c-sum(C_tree)) total = fix_A + fix_B + fix_C + synthesis_A + synthesis_B + synthesis_C ans = min(ans, total) print(ans) ```
instruction
0
58,728
8
117,456
No
output
1
58,728
8
117,457
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,872
8
117,744
Tags: implementation, sortings Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations from collections import defaultdict BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n, m = list(map(int, input().split())) streets = [] for i in range(n): streets.append(list(map(int, input().split()))) mem_rows = {} len_row = [] for i in range(n): mem_r = sorted(set(streets[i])) srted = {x: i for i, x in enumerate(mem_r)} ords = [srted[i] for i in streets[i]] mem_rows[i] = ords len_row.append(len(mem_r)) mem_cols = {} len_col = [] for j in range(m): col = [k[j] for k in streets] mem_d = sorted(set(col)) srted = {x: i for i, x in enumerate(mem_d)} ords = [srted[i] for i in col] mem_cols[j] = ords len_col.append(len(mem_d)) for i in range(n): prt = [] for j in range(m): elem = streets[i][j] pos1, pos2 = mem_rows[i][j], mem_cols[j][i] streets_ans = max(pos1, pos2) + max((len_row[i] - pos1), (len_col[j] - pos2)) prt.append(streets_ans) print(*prt) ```
output
1
58,872
8
117,745
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,873
8
117,746
Tags: implementation, sortings Correct Solution: ``` n, m = list(map(int, input().split())) gor = [] ver = [] o = [] for i in range(n): gor.append(list(map(int, input().split()))) o.append([]) for i in range(m): ver.append([]) for i in range(m): for g in gor: ver[i].append(g[i]) ver = list(map(lambda x: sorted(list(set(x))), ver)) def f(vg, vr, o): vr1 = vr.index(o) vg1 = vg.index(o) if vr1 > vg1: return max([len(vr), len(vg) + vr1 - vg1]) return max([len(vg), len(vr) + vg1 - vr1]) for i, vg in enumerate(gor): vg1 = sorted(list(set(vg))) for j, vr in enumerate(vg): o[i].append(f(vg1, ver[j], vr)) for i in o: print(' '.join(list(map(str, i)))) ```
output
1
58,873
8
117,747
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,874
8
117,748
Tags: implementation, sortings Correct Solution: ``` from bisect import bisect_left import sys input = sys.stdin.buffer.readline n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] yoko_less = [[0] * m for i in range(n)] yoko_more = [[0] * m for i in range(n)] for i in range(n): yoko = sorted(set(a[i])) for j in range(m): idx = bisect_left(yoko, a[i][j]) yoko_less[i][j] = idx yoko_more[i][j] = len(yoko) - idx - 1 tate_less = [[0] * m for i in range(n)] tate_more = [[0] * m for i in range(n)] for j in range(m): tate = sorted(set([a[i][j] for i in range(n)])) for i in range(n): idx = bisect_left(tate, a[i][j]) tate_less[i][j] = idx tate_more[i][j] = len(tate) - idx - 1 ans = [[0] * m for i in range(n)] for i in range(n): for j in range(m): less = max(yoko_less[i][j], tate_less[i][j]) more = max(yoko_more[i][j], tate_more[i][j]) ans[i][j] = 1 + more + less for res in ans: print(*res) ```
output
1
58,874
8
117,749
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,875
8
117,750
Tags: implementation, sortings Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ####################################### from bisect import * from collections import * a,b=map(int,input().split()) ans=[] rp=[[0 for i in range(b)] for i in range(a)] for i in range(a): z=list(map(int,input().split())) ans.append(z) t=z.copy() t.sort() al=defaultdict(int) ke=list(set(t)) for t in range(len(ke)): al[ke[t]]=t+1 for j in range(b): rp[i][j]=(al[z[j]],len(ke)) col=[[0 for i in range(a)] for j in range(b)] for i in range(b): for j in range(a): col[i][j]=ans[j][i] # now the first row denotes the first coloumn cp=[[0 for i in range(a)] for j in range(b)] for i in range(b): t=col[i].copy() t.sort() al=defaultdict(int) ke=list(set(t)) for t in range(len(ke)): al[ke[t]]=t+1 for j in range(a): cp[i][j]=(al[col[i][j]],len(ke)) fin=[[0 for i in range(b)] for j in range(a)] for i in range(a): for j in range(b): m=rp[i][j] u=cp[j][i] if(m[0]==u[0]): fin[i][j]=max(u[1],m[1]) if(m[0]>u[0]): fin[i][j]=max(m[1],m[0]+u[1]-u[0]) if(m[0]<u[0]): fin[i][j]=max(u[1],u[0]+m[1]-m[0]) for i in range(a): print(*fin[i]) ```
output
1
58,875
8
117,751
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,876
8
117,752
Tags: implementation, sortings Correct Solution: ``` import sys input=sys.stdin.readline n, m = map(int, input().split()) St = [list(map(int, input().split())) for _ in range(n)] ppr = [sorted(list(set(s))) for s in St] PR = [] for i in range(n): H = dict() for j, v in enumerate(ppr[i], 1): H[v] = j PR.append([H[p] for p in St[i]] + [j]) TSt = list(map(list, zip(*St))) ppc = [sorted(list(set(s))) for s in TSt] PC = [] for i in range(m): H = dict() for j, v in enumerate(ppc[i], 1): H[v] = j PC.append([H[p] for p in TSt[i]] + [j]) for i in range(n): pri = PR[i] prm = PR[i][-1] print(*[max(pri[j], PC[j][i]) + max(prm-pri[j], PC[j][-1]-PC[j][i]) for j in range(m)]) ```
output
1
58,876
8
117,753
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,877
8
117,754
Tags: implementation, sortings Correct Solution: ``` import heapq import sys from collections import defaultdict, Counter from functools import reduce n, m = list(map(int, input().split())) arr = [] for _ in range(n): arr.append(list(map(int, input().split()))) rows = [] for i in range(n): row = set() for j in range(m): row.add(arr[i][j]) rows.append({x: i for i, x in enumerate(sorted(row))}) columns = [] for j in range(m): column = set() for i in range(n): column.add(arr[i][j]) columns.append({x: i for i, x in enumerate(sorted(column))}) def get_answer(i, j): el = arr[i][j] index1 = rows[i][el] index2 = columns[j][el] return max(index1, index2) + max(len(rows[i]) - index1, len(columns[j]) - index2) for i in range(n): answer = [] for j in range(m): answer.append(str(get_answer(i, j))) print(' '.join(answer)) ```
output
1
58,877
8
117,755
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,878
8
117,756
Tags: implementation, sortings Correct Solution: ``` import sys input=sys.stdin.buffer.readline from bisect import bisect_left n,m=map(int,input().split()) arr=[] for i in range(n): arr.append(list(map(int,input().split()))) arr_maxi=[[0 for i in range(m)] for j in range(n)] arr_mini=[[0 for i in range(m)] for j in range(n)] for i in range(n): s=sorted(set(arr[i])) for j in range(m): indx=bisect_left(s,arr[i][j]) arr_maxi[i][j]=len(s)-indx-1 arr_mini[i][j]=indx for j in range(m): s=sorted(set([arr[i][j] for i in range(n)])) for i in range(n): indx=bisect_left(s,arr[i][j]) arr_maxi[i][j]=max(arr_maxi[i][j],len(s)-indx-1) arr_mini[i][j] = max(arr_mini[i][j], indx) for i in range(n): for j in range(m): print(arr_maxi[i][j] +arr_mini[i][j] +1,end=' ') print() ```
output
1
58,878
8
117,757
Provide tags and a correct Python 3 solution for this coding contest problem. Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by n streets along the Eastern direction and m streets across the Southern direction. Naturally, this city has nm intersections. At any intersection of i-th Eastern street and j-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the i-th Eastern and j-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of nm intersections Dora solves an independent problem. She sees n + m - 1 skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer x and assign every skyscraper a height from 1 to x. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible x. For example, if the intersection and the two streets corresponding to it look as follows: <image> Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) <image> The largest used number is 5, hence the answer for this intersection would be 5. Help Dora to compute the answers for each intersection. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1000) β€” the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following n lines contains m integers a_{i,1}, a_{i,2}, ..., a_{i,m} (1 ≀ a_{i,j} ≀ 10^9). The integer a_{i,j}, located on j-th position in the i-th line denotes the height of the skyscraper at the intersection of the i-th Eastern street and j-th Southern direction. Output Print n lines containing m integers each. The integer x_{i,j}, located on j-th position inside the i-th line is an answer for the problem at the intersection of i-th Eastern street and j-th Southern street. Examples Input 2 3 1 2 1 2 1 2 Output 2 2 2 2 2 2 Input 2 2 1 2 3 4 Output 2 3 3 2 Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: * For the intersection of the first line and the first column <image> * For the intersection of the first line and the second column <image> * For the intersection of the second line and the first column <image> * For the intersection of the second line and the second column <image>
instruction
0
58,879
8
117,758
Tags: implementation, sortings Correct Solution: ``` import sys from bisect import bisect_left input=sys.stdin.buffer.readline n,m=map(int,input().split()) a=[list(map(int,input().split())) for i in range(n)] lr_less=[[0]*m for i in range(n)] lr_more=[[0]*m for i in range(n)] for i in range(n): lr=sorted(set(a[i])) for j in range(m): idx=bisect_left(lr,a[i][j]) lr_less[i][j]=idx lr_more[i][j]=len(lr)-1-idx du_less=[[0]*m for i in range(n)] du_more=[[0]*m for i in range(n)] for j in range(m): du=sorted(set([a[i][j] for i in range(n)])) for i in range(n): idx=bisect_left(du,a[i][j]) du_less[i][j]=idx du_more[i][j]=len(du)-1-idx ans=[[0]*m for i in range(n)] for i in range(n): for j in range(m): less=max(lr_less[i][j],du_less[i][j]) more=max(lr_more[i][j],du_more[i][j]) ans[i][j]=less+more+1 for z in ans: print(*z) ```
output
1
58,879
8
117,759
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,930
8
117,860
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` x=int(input()) s=[[0 for n in range(x)] for n in range(x)] v=1 for n in range(x): if n%2==0: for k in range(x): s[k][n]=v v+=1 else: for k in range(x-1,-1,-1): s[k][n]=v v+=1 for n in s: print(*n) ```
output
1
58,930
8
117,861
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,931
8
117,862
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n = int(input()) for i in range(n): ansline = [] for j in range(n): if j % 2 == 0: ansline.append(1 + j*n+i) else: ansline.append(1 + (j+1)*n-i-1) print(*ansline) ```
output
1
58,931
8
117,863
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,932
8
117,864
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- n=int(input()) ans=[[] for s in range(n)] mod1=[s for s in range(n)] mod2=[s for s in range(n-1,-1,-1)] mod=mod1+mod2 for j in range(n**2): ans[mod[j%len(mod)]].append(j+1) for s in range(len(ans)): print(*ans[s]) ```
output
1
58,932
8
117,865
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,933
8
117,866
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n=int(input()) ans=[[] for i in range(n)] flag=0 for i in range(1,(n*n)+1): if flag==0: c=i%n ans[c-1].append(i) if c==0: flag=1 else: c=n-(i%n) if c==n: flag=0 c=0 ans[c].append(i) for op in ans: for a in op: print(a,end=" ") print() ```
output
1
58,933
8
117,867
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,934
8
117,868
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.buffer.readline n = int(input()) res = [[] for _ in range(n)] for i in range(n): for j in range(n): if i % 2: res[n - j - 1].append(i * n + j + 1) else: res[j].append(i * n + j + 1) for r in res: print(*r) ```
output
1
58,934
8
117,869
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,935
8
117,870
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` def create(n): res = [[0] * n for _ in range(n)] cur = 1 for j in range(n): for i in (range(n - 1, -1, -1) if j & 1 else range(n)): res[i][j] = cur cur += 1 return res n = int(input()) res = create(n) for l in res: print(*l) ```
output
1
58,935
8
117,871
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,936
8
117,872
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` N = int(input()) ans = [[] for _ in range(N)] for i in range(N): for j in range(1, N+1): if i%2 == 0: ans[j-1].append(N*i+j) else: ans[N-j].append(N*i+j) for i in range(N): for j in range(N): print(ans[i][j], end=' ') print() ```
output
1
58,936
8
117,873
Provide tags and a correct Python 3 solution for this coding contest problem. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.
instruction
0
58,937
8
117,874
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` import os import sys from io import BytesIO, IOBase from collections import Counter BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return (a * b) / gcd(a, b) def main(): n=int(input()) ans=[[0 for i in range(n)] for j in range(n)] c=1 f=0 for j in range(n): if f==0: for i in range(n): ans[i][j]=c c+=1 else: for i in range(n-1,-1,-1): ans[i][j]=c c+=1 f ^= 1 for i in ans: print(*i) return if __name__ == "__main__": main() ```
output
1
58,937
8
117,875
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` def solve(n): ans = [] for i in range(n): if i % 2 == 0: ans.append(list(range(1 + i*n, (i+1)*n+1))) else: ans.append(list(range(1 + i*n, (i+1)*n+1))[::-1]) # for i in ans: # print(i) for i in range(n): for j in range(i+1, n): ans[i][j], ans[j][i] = ans[j][i], ans[i][j] for i in ans: print(' '.join([str(j) for j in i])) def main(): n = int(input()) solve(n) main() ```
instruction
0
58,938
8
117,876
Yes
output
1
58,938
8
117,877
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n = int(input()) res=[[0]*n for _ in range(n)] cnt=1 for i in range(n): if i%2==0: for j in range(n): res[j][i]=cnt cnt+=1 else: for j in range(n-1,-1,-1): res[j][i]=cnt cnt+=1 for line in res: print(*line) ```
instruction
0
58,939
8
117,878
Yes
output
1
58,939
8
117,879
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n = int(input()) flag = 0 cnt = 1 ans = [] for i in range(n): temp = [] for j in range(n): temp.append(0) ans.append(temp) for i in range(n): for j in range(n): if flag==0: ans[j][i] = cnt else: ans[n-1-j][i] = cnt cnt+=1 if flag==0: flag=1 else: flag=0 #cnt+=1 for i in ans: print(*i) ```
instruction
0
58,940
8
117,880
Yes
output
1
58,940
8
117,881
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n=int(input()) for i in range(n): for j in range(n): if j%2==0: print(j*n+i+1) else: print((j+1)*n-i) ```
instruction
0
58,941
8
117,882
Yes
output
1
58,941
8
117,883
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n = int(input()) nn = [[0] * n for i in range(n)] for i in range(n): for j in range(n): nn[i][j] = n * j + i + 1 for el in nn: print(*el) ```
instruction
0
58,942
8
117,884
No
output
1
58,942
8
117,885
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n = int(input()) print(n*n/2) ```
instruction
0
58,943
8
117,886
No
output
1
58,943
8
117,887
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n = int(input()) mid = n // 2 for g in range(n): for i in range(mid): print(n * i + g + 1, end=' ') for i in range(mid, n): print(n * i + (n - g) + 1, end=' ') print() ```
instruction
0
58,944
8
117,888
No
output
1
58,944
8
117,889
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In order to do some research, n^2 labs are built on different heights of a mountain. Let's enumerate them with integers from 1 to n^2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, …, the lab with the number n^2 is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number u to the lab with the number v if u > v. Now the labs need to be divided into n groups, each group should contain exactly n labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group A to a group B is equal to the number of pairs of labs (u, v) such that the lab with the number u is from the group A, the lab with the number v is from the group B and u > v. Let's denote this value as f(A,B) (i.e. f(A,B) is the sum of units of water that can be sent from a group A to a group B). For example, if n=3 and there are 3 groups X, Y and Z: X = \{1, 5, 6\}, Y = \{2, 4, 9\} and Z = \{3, 7, 8\}. In this case, the values of f are equal to: * f(X,Y)=4 because of 5 β†’ 2, 5 β†’ 4, 6 β†’ 2, 6 β†’ 4, * f(X,Z)=2 because of 5 β†’ 3, 6 β†’ 3, * f(Y,X)=5 because of 2 β†’ 1, 4 β†’ 1, 9 β†’ 1, 9 β†’ 5, 9 β†’ 6, * f(Y,Z)=4 because of 4 β†’ 3, 9 β†’ 3, 9 β†’ 7, 9 β†’ 8, * f(Z,X)=7 because of 3 β†’ 1, 7 β†’ 1, 7 β†’ 5, 7 β†’ 6, 8 β†’ 1, 8 β†’ 5, 8 β†’ 6, * f(Z,Y)=5 because of 3 β†’ 2, 7 β†’ 2, 7 β†’ 4, 8 β†’ 2, 8 β†’ 4. Please, divide labs into n groups with size n, such that the value min f(A,B) over all possible pairs of groups A and B (A β‰  B) is maximal. In other words, divide labs into n groups with size n, such that minimum number of the sum of units of water that can be transported from a group A to a group B for every pair of different groups A and B (A β‰  B) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values f for some division. If there are many optimal divisions, you can find any. Input The only line contains one number n (2 ≀ n ≀ 300). Output Output n lines: In the i-th line print n numbers, the numbers of labs of the i-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Example Input 3 Output 2 8 5 9 3 4 7 6 1 Note In the first test we can divide 9 labs into groups \{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}. From the first group to the second group we can transport 4 units of water (8 β†’ 3, 8 β†’ 4, 5 β†’ 3, 5 β†’ 4). From the first group to the third group we can transport 5 units of water (2 β†’ 1, 8 β†’ 7, 8 β†’ 6, 8 β†’ 1, 5 β†’ 1). From the second group to the first group we can transport 5 units of water (9 β†’ 2, 9 β†’ 8, 9 β†’ 5, 3 β†’ 2, 4 β†’ 2). From the second group to the third group we can transport 5 units of water (9 β†’ 7, 9 β†’ 6, 9 β†’ 1, 3 β†’ 1, 4 β†’ 1). From the third group to the first group we can transport 4 units of water (7 β†’ 2, 7 β†’ 5, 6 β†’ 2, 6 β†’ 5). From the third group to the second group we can transport 4 units of water (7 β†’ 3, 7 β†’ 4, 6 β†’ 3, 6 β†’ 4). The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division. Submitted Solution: ``` n = int(input().strip()) n2 = n**2 L = [x for x in range(1,n2 + 1)] M = [ [] for i in range(n)] print(L) i = 0 t = 0 while i < n2: if t%2 == 0: for j in range(n): M[j].append(str(L[i])) i += 1 else: for j in range(n-1,-1,-1): M[j].append(str(L[i])) i += 1 t += 1 for i in M: print(" ".join(i)) ```
instruction
0
58,945
8
117,890
No
output
1
58,945
8
117,891
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,081
8
118,162
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` import math;import heapq;import sys;input=sys.stdin.readline;S=lambda:input();I=lambda:int(S());M=lambda:map(int,S().split());L=lambda:list(M());H=1000000000+7 for _ in range(I()): s=input() n,k=M() po=L() t=L() h=math.inf a=[h]*n l,r=[0]*n,[0]*n for i in range(k): a[po[i]-1]=t[i] p=h for i in range(n): p=min(p+1,a[i]) l[i]=p p=h for i in range(n-1,-1,-1): p=min(p+1,a[i]) r[i]=p for i in range(n): l[i]=min(l[i],r[i]) print(*l) ```
output
1
59,081
8
118,163
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,082
8
118,164
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## from collections import Counter, defaultdict import math #sys.setrecursionlimit(10**3) for _ in range(int(input())): s=input() n, k = map(int, input().split()) #n=int(input()) #s=input() p=list(map(int, input().split())) t=list(map(int, input().split())) a=[10**12]*n vis=[-1]*n for i in range(k): vis[p[i]-1]=t[i] pos=-n-100 temp=10**12 for i in range(n): if vis[i]!=-1 and temp+(i-pos)>vis[i]: pos=i temp=vis[i] if temp!=10**12: a[i]=min(a[i],temp+(i-pos)) pos =10**6 temp = 10 ** 12 for i in range(n-1,-1,-1): if vis[i] != -1 and temp+(pos-i) > vis[i]: pos = i temp = vis[i] if temp != 10 ** 12: a[i] = min(a[i], temp + (pos-i)) print(*a) ```
output
1
59,082
8
118,165
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,083
8
118,166
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` import sys from collections import deque #Counter #sys.setrecursionlimit(20000) #import itertools def rl(): return sys.stdin.readline().strip() def rl_types(types): str_list = [x for x in sys.stdin.readline().strip().split(' ')] return [types[i](str_list[i]) for i in range(len(str_list))] def pr( something ): sys.stdout.write( str(something) + '\n') def pra( array ): sys.stdout.write( ' '.join([str(x) for x in array]) + '\n') def solve(array): return array if __name__ == '__main__': NT = int( rl() ) for _ in range(NT): rl() n,k = [int(x) for x in rl().split(' ')] locs = [int(x) for x in rl().split(' ')] temps = [int(x) for x in rl().split(' ')] # vals = rl_types( [str,float,float] ) #res = solve(array) acs_raw = sorted([(locs[i]-1,temps[i]) for i in range(k)]) #print(acs) INF = float('inf') fromleft = [INF]*n fromright = [INF]*n #for l,t in acs: #fromleft[l] = t #fromright[l] = t # LEFT acs = deque(acs_raw) ctemp = INF# if acs[0][0]>0 else acs[0].popleft() #lowtemp = ctemp for i in range(n): if len(acs)>0 and acs[0][0]==i: ctemp = min( ctemp,acs.popleft()[1] ) fromleft[i] = ctemp ctemp += 1 # RIGHT acs = deque(acs_raw) ctemp = INF# if acs[0][0]>0 else acs[0].popleft() #lowtemp = ctemp for i in range(n-1,-1,-1): if len(acs)>0 and acs[-1][0]==i: ctemp = min( ctemp,acs.pop()[1] ) fromright[i] = ctemp ctemp += 1 ''' prev_loc,prev_temp = 0,INF next_loc,next_temp = acs[0] for i in range(1,next_loc+1): fromleft[i-1] = INF fromright[i-1] = next_temp+(next_loc-i) for next_loc,next_temp in acs: #print(' ',next_loc,next_temp) fromleft[next_loc-1] = next_temp fromright[next_loc-1] = next_temp for i in range(prev_loc+1,next_loc): fromleft[i-1] = prev_temp+(i-prev_loc) fromright[i-1] = next_temp+(next_loc-i) prev_loc,prev_temp = next_loc,next_temp for i in range(prev_loc+1,n+1): fromright[i-1] = INF fromleft[i-1] = prev_temp+(i-prev_loc) ''' #pr(fromleft) #pr(fromright) #pr('') result = [0]*n for i in range(n): result[i] = min(fromleft[i],fromright[i]) pra(result) ```
output
1
59,083
8
118,167
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,084
8
118,168
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` from bisect import bisect_left from collections import defaultdict as D MAX = 99999999999 for _ in range(int(input())): s = input() n,k = map(int,input().split()) a = list( map( int, input().split() ) ) t = list( map( int, input().split() ) ) d=D(int) for i in range(k): a[i] -=1 d[a[i]] = t[i] ans=[MAX for i in range(n)] r = MAX for i in range(n): if d[i] == 0: d[i] = MAX r = min(r, d[i]) ans[i] = min(ans[i], r) r+=1 for i in range(n-1,-1,-1): if d[i] == 0: d[i] = MAX r = min(r, d[i]) ans[i] = min(ans[i], r) r+=1 print(*ans) ```
output
1
59,084
8
118,169
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,085
8
118,170
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=2*(10**9), func=lambda a, b: min(a,b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = self.getNode() def getNode(self): return TrieNode() def _charToIndex(self, ch): return ord(ch) - ord('a') def insert(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: pCrawl.children[index] = self.getNode() pCrawl = pCrawl.children[index] pCrawl.isEndOfWord = True def search(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: return False pCrawl = pCrawl.children[index] return pCrawl != None and pCrawl.isEndOfWord #-----------------------------------------trie--------------------------------- class Node: def __init__(self, data): self.data = data self.count=0 self.left = None # left node for 0 self.right = None # right node for 1 class BinaryTrie: def __init__(self): self.root = Node(0) def insert(self, pre_xor): self.temp = self.root for i in range(31, -1, -1): val = pre_xor & (1 << i) if val: if not self.temp.right: self.temp.right = Node(0) self.temp = self.temp.right self.temp.count+=1 if not val: if not self.temp.left: self.temp.left = Node(0) self.temp = self.temp.left self.temp.count += 1 self.temp.data = pre_xor def query(self, xor): self.temp = self.root for i in range(31, -1, -1): val = xor & (1 << i) if not val: if self.temp.left and self.temp.left.count>0: self.temp = self.temp.left elif self.temp.right: self.temp = self.temp.right else: if self.temp.right and self.temp.right.count>0: self.temp = self.temp.right elif self.temp.left: self.temp = self.temp.left self.temp.count-=1 return xor ^ self.temp.data # --------------------------------------------------binary----------------------------------- for ik in range(int(input())): line=input() n,k=map(int,input().split()) a=list(map(int,input().split())) t=list(map(int,input().split())) a=[(a[i],t[i])for i in range(k)] a.sort() diff=[0]*k add=[0]*k for i in range(k): diff[i]=a[i][1]-a[i][0] add[i]=a[i][1]+a[i][0] s=SegmentTree(diff) s1=SegmentTree(add) ans=[0]*n j=0 for i in range(n): while(j<k): if a[j][0]<i+1: j+=1 else: break #print(s.query(0,j-1)+i+1,s1.query(j,k-1)-i-1) ans[i]=min(s.query(0,j-1)+i+1,s1.query(j,k-1)-i-1) print(*ans) ```
output
1
59,085
8
118,171
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,086
8
118,172
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` import time from collections import deque def inpt(): return int(input()) def inpl(): return list(map(int,input().split())) def inpm(): return map(int,input().split()) def solve(): n,k = inpm() a = inpl() t = inpl() lans = [1e10 for i in range(n)] for i,j in enumerate(a): lans[j-1] = t[i] for i in range(1,n): lans[i] = min(lans[i-1]+1,lans[i]) rans = [1e10 for i in range(n)] for i,j in enumerate(a): rans[j-1] = t[i] for i in range(n-2,-1,-1): rans[i] = min(rans[i+1]+1,rans[i]) ans = [min(lans[i],rans[i]) for i in range(n)] print(*ans) def main(): #start_time=time.time() m=10**9+7 t = int(input()) while(t): t-=1 input() solve() #print('Time Elapsed = ',time.time()-start_time," seconds") if __name__ == "__main__": main() ```
output
1
59,086
8
118,173
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,087
8
118,174
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` import heapq,math from collections import defaultdict,deque from os import getcwd import sys, os.path #sys.setrecursionlimit(100000000) if(os.path.exists('D:\CP\programs\input.txt')): sys.stdout = open('D:\CP\programs\output.txt', 'w') sys.stdin = open('D:\CP\programs\input.txt', 'r') input=sys.stdin.readline def find(i,pos,temp): res=9999999999 for j in range(len(pos)): res=min(res,(temp[j]+abs(pos[j]-i-1))) return res test=int(input()) for _ in range(test): input() n,k=map(int,input().split()) pos=list(map(int,input().split())) temp=list(map(int,input().split())) res=[0]*n for i in range(k): res[pos[i]-1]=temp[i] left=[i if i!=0 else (10**15)+1 for i in res] right=[i if i!=0 else (10**15) for i in res] #print(left) for i in range(1,n): left[i]=min(left[i-1]+1,left[i]) #print(left) for i in range(n-2,-1,-1): right[i]=min(right[i+1]+1,right[i]) #print(right) for i in range(n): res[i]=min(left[i],right[i]) print(*res) ```
output
1
59,087
8
118,175
Provide tags and a correct Python 3 solution for this coding contest problem. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5
instruction
0
59,088
8
118,176
Tags: data structures, dp, implementation, shortest paths, sortings, two pointers Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase import io from fractions import Fraction import collections from itertools import permutations from collections import defaultdict from collections import deque from collections import Counter import threading #sys.setrecursionlimit(300000) #threading.stack_size(10**8) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- #mod = 9223372036854775807 class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a,b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) class SegmentTree1: def __init__(self, data, default=10**6, func=lambda a, b: min(a,b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) MOD=10**9+7 class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD mod=10**9+7 omod=998244353 #------------------------------------------------------------------------- prime = [True for i in range(10001)] prime[0]=prime[1]=False #pp=[0]*10000 def SieveOfEratosthenes(n=10000): p = 2 c=0 while (p <= n): if (prime[p] == True): c+=1 for i in range(p, n+1, p): #pp[i]=1 prime[i] = False p += 1 #-----------------------------------DSU-------------------------------------------------- class DSU: def __init__(self, R, C): #R * C is the source, and isn't a grid square self.par = range(R*C + 1) self.rnk = [0] * (R*C + 1) self.sz = [1] * (R*C + 1) def find(self, x): if self.par[x] != x: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): xr, yr = self.find(x), self.find(y) if xr == yr: return if self.rnk[xr] < self.rnk[yr]: xr, yr = yr, xr if self.rnk[xr] == self.rnk[yr]: self.rnk[xr] += 1 self.par[yr] = xr self.sz[xr] += self.sz[yr] def size(self, x): return self.sz[self.find(x)] def top(self): # Size of component at ephemeral "source" node at index R*C, # minus 1 to not count the source itself in the size return self.size(len(self.sz) - 1) - 1 #---------------------------------Lazy Segment Tree-------------------------------------- # https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp class LazySegTree: def __init__(self, _op, _e, _mapping, _composition, _id, v): def set(p, x): assert 0 <= p < _n p += _size for i in range(_log, 0, -1): _push(p >> i) _d[p] = x for i in range(1, _log + 1): _update(p >> i) def get(p): assert 0 <= p < _n p += _size for i in range(_log, 0, -1): _push(p >> i) return _d[p] def prod(l, r): assert 0 <= l <= r <= _n if l == r: return _e l += _size r += _size for i in range(_log, 0, -1): if ((l >> i) << i) != l: _push(l >> i) if ((r >> i) << i) != r: _push(r >> i) sml = _e smr = _e while l < r: if l & 1: sml = _op(sml, _d[l]) l += 1 if r & 1: r -= 1 smr = _op(_d[r], smr) l >>= 1 r >>= 1 return _op(sml, smr) def apply(l, r, f): assert 0 <= l <= r <= _n if l == r: return l += _size r += _size for i in range(_log, 0, -1): if ((l >> i) << i) != l: _push(l >> i) if ((r >> i) << i) != r: _push((r - 1) >> i) l2 = l r2 = r while l < r: if l & 1: _all_apply(l, f) l += 1 if r & 1: r -= 1 _all_apply(r, f) l >>= 1 r >>= 1 l = l2 r = r2 for i in range(1, _log + 1): if ((l >> i) << i) != l: _update(l >> i) if ((r >> i) << i) != r: _update((r - 1) >> i) def _update(k): _d[k] = _op(_d[2 * k], _d[2 * k + 1]) def _all_apply(k, f): _d[k] = _mapping(f, _d[k]) if k < _size: _lz[k] = _composition(f, _lz[k]) def _push(k): _all_apply(2 * k, _lz[k]) _all_apply(2 * k + 1, _lz[k]) _lz[k] = _id _n = len(v) _log = _n.bit_length() _size = 1 << _log _d = [_e] * (2 * _size) _lz = [_id] * _size for i in range(_n): _d[_size + i] = v[i] for i in range(_size - 1, 0, -1): _update(i) self.set = set self.get = get self.prod = prod self.apply = apply MIL = 1 << 20 def makeNode(total, count): # Pack a pair into a float return (total * MIL) + count def getTotal(node): return math.floor(node / MIL) def getCount(node): return node - getTotal(node) * MIL nodeIdentity = makeNode(0.0, 0.0) def nodeOp(node1, node2): return node1 + node2 # Equivalent to the following: return makeNode( getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2) ) identityMapping = -1 def mapping(tag, node): if tag == identityMapping: return node # If assigned, new total is the number assigned times count count = getCount(node) return makeNode(tag * count, count) def composition(mapping1, mapping2): # If assigned multiple times, take first non-identity assignment return mapping1 if mapping1 != identityMapping else mapping2 #---------------------------------Pollard rho-------------------------------------------- def memodict(f): """memoization decorator for a function taking a single argument""" class memodict(dict): def __missing__(self, key): ret = self[key] = f(key) return ret return memodict().__getitem__ def pollard_rho(n): """returns a random factor of n""" if n & 1 == 0: return 2 if n % 3 == 0: return 3 s = ((n - 1) & (1 - n)).bit_length() - 1 d = n >> s for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]: p = pow(a, d, n) if p == 1 or p == n - 1 or a % n == 0: continue for _ in range(s): prev = p p = (p * p) % n if p == 1: return math.gcd(prev - 1, n) if p == n - 1: break else: for i in range(2, n): x, y = i, (i * i + 1) % n f = math.gcd(abs(x - y), n) while f == 1: x, y = (x * x + 1) % n, (y * y + 1) % n y = (y * y + 1) % n f = math.gcd(abs(x - y), n) if f != n: return f return n @memodict def prime_factors(n): """returns a Counter of the prime factorization of n""" if n <= 1: return Counter() f = pollard_rho(n) return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f) def distinct_factors(n): """returns a list of all distinct factors of n""" factors = [1] for p, exp in prime_factors(n).items(): factors += [p**i * factor for factor in factors for i in range(1, exp + 1)] return factors def all_factors(n): """returns a sorted list of all distinct factors of n""" small, large = [], [] for i in range(1, int(n**0.5) + 1, 2 if n & 1 else 1): if not n % i: small.append(i) large.append(n // i) if small[-1] == large[-1]: large.pop() large.reverse() small.extend(large) return small #---------------------------------Binary Search------------------------------------------ def binarySearch(arr, n, key): left = 0 right = n-1 mid = 0 res=arr[n-1] while (left <= right): mid = (right + left)//2 if (arr[mid] >= key): res=arr[mid] right = mid-1 else: left = mid + 1 return res def binarySearch1(arr, n, key): left = 0 right = n-1 mid = 0 res=-1 while (left <= right): mid = (right + left)//2 if (arr[mid][0] >= key): right = mid-1 else: res=mid left = mid + 1 return res #---------------------------------running code------------------------------------------ from heapq import heapify,heappop,heappush t=1 t=int(input()) for _ in range (t): #n=int(input()) x=input() n,k=map(int,input().split()) a=list(map(int,input().split())) tp=list(map(int,input().split())) #s=input() b=[[a[i],tp[i]]for i in range (k)] b.sort() #n=len(s) h=[] heapify(h) res=[2*10**9]*n j=0 for i in range (n): if j<k and b[j][0]==i+1: heappush(h,b[j][1]+n-i) j+=1 if not h: continue res[i]=min(res[i],h[0]-n+i) h=[] #print(res) heapify(h) j=k-1 for i in range (n-1,-1,-1): if j>=0 and b[j][0]==i+1: heappush(h,b[j][1]+i) j-=1 if not h: continue res[i]=min(res[i],h[0]-i) print(*res) ```
output
1
59,088
8
118,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` import sys,heapq input = lambda: sys.stdin.readline().rstrip("\r\n") for _ in range(int(input())): input() n,k=[int(x) for x in input().split()] v=[int(x) for x in input().split()] d={} temp=[int(x) for x in input().split()] for i in range(k): d[v[i]]=temp[i] #print(d) h2=[] h1=[] for i in d: heapq.heappush(h2,(d[i]+i,i)) res=[] for i in range(1,n+1): ans=1000000000000000 if i in d: heapq.heappush(h1,d[i]-i) while h2 and h2[0][1]<i: heapq.heappop(h2) if h1: ans=min(ans,h1[0]+i) if h2: ans=min(ans,h2[0][0]-i) res.append(ans) print(*res) ```
instruction
0
59,089
8
118,178
Yes
output
1
59,089
8
118,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` from math import * from collections import * from functools import * from bisect import * from itertools import * from heapq import * from statistics import * inf = float('inf') ninf = -float('inf') ip = input alphal = "abcdefghijklmnopqrstuvwxyz" alphau = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def ipl(): return list(map(int, ip().split())) def ipn(): return int(ip()) def ipf(): return float(ip()) def solve(): n, k = ipl() a, t = ipl(), ipl() c, l, r = [inf]*n, [inf]*n, [inf]*n for i in range(k): c[a[i]-1] = t[i] p = inf for i in range(n): p = min(p+1, c[i]) l[i] = p p = inf for i in range(n-1, -1, -1): p = min(p+1, c[i]) r[i] = p for i in range(n): print(min(l[i], r[i]), end=" ") t = ipn() for _ in range(t): ip() solve() ```
instruction
0
59,090
8
118,180
Yes
output
1
59,090
8
118,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() inf = 10**16 md = 10**9+7 # md = 998244353 def solve(): def toll(air): res = [inf]*n mn = inf for i,t in enumerate(air): if t:mn=min(mn,t-i) res[i]=mn+i return res SI() n, k = LI() aa = LI1() tt = LI() air=[0]*n for a,t in zip(aa,tt): air[a]=t ll = toll(air) rr = toll(air[::-1])[::-1] ans = [] for l, r in zip(ll, rr): ans.append(min(l, r)) print(*ans) for testcase in range(II()): solve() ```
instruction
0
59,091
8
118,182
Yes
output
1
59,091
8
118,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` # TLE FOR LARGER INPUTS """ for _ in range(int(input())): input() n, k = map(int, input().split()) a = list(map(int, input().split())) t = list(map(int, input().split())) ans = [0]*n for i in range(n): minT = 10e10 for ai, ti in zip(a, t): minT = min(minT, ti + abs(i - ai + 1)) ans[i] = minT print(*ans) """ import math for _ in range(int(input())): input() n, k = map(int, input().split()) a = list(map(int, input().split())) t = list(map(int, input().split())) c = [math.inf] * n for i in range(k): c[a[i]-1] = t[i] ans = [0] * n L = [math.inf] * n p = math.inf for i in range(n): p = min(p + 1, c[i]) L[i] = p R = [math.inf] * n p = math.inf for i in range(n-1, -1, -1): p = min(p + 1, c[i]) R[i] = p for i in range(n): ans[i] = min(L[i], R[i]) print(*ans) ```
instruction
0
59,092
8
118,184
Yes
output
1
59,092
8
118,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` import sys input = sys.stdin.readline import math for t in range(int(input())): input() n, k = map(int, input().split()) *pos, = map(int, input().split()) a = [0]*n idx = 0 for i in map(int, input().split()): a[pos[idx]-1] = i idx += 1 forward_dis = [0]*n reverse_dis = [0]*n fp = -math.inf rp = math.inf for i in range(n): if a[i]:fp=i if a[n-1-i]:rp=n-1-i forward_dis[i] = a[fp]+(i-fp) if fp>-math.inf else math.inf reverse_dis[n-1-i] = a[rp]+(rp-(n-1-i)) if rp<math.inf else math.inf print(*[min(forward_dis[i], reverse_dis[i]) for i in range(n)]) ```
instruction
0
59,093
8
118,186
No
output
1
59,093
8
118,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` import sys input = sys.stdin.buffer.readline from collections import deque import bisect def main(): t = int(input()); INF = pow(10,9) + 100 for _ in range(t): dummy = input() n,k = map(int,input().split()) A = [0] + list(map(int,input().split())) + [n+1] AS = set(A) T = [INF] + list(map(int,input().split())) + [INF] dic = {} for aa,tt in zip(A,T): dic[aa] = tt A.sort() #print(A,T) ans = [INF]*n for i in range(1,n+1): #1-index l = bisect.bisect_left(A,i) - 1 r = bisect.bisect_left(A,i) #print(l,r) ans[i-1] = min(dic[A[l]] + abs(A[l]-i), dic[A[r]] + abs(A[r]-i)) print(*ans) if __name__ == '__main__': main() ```
instruction
0
59,094
8
118,188
No
output
1
59,094
8
118,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` q = int(input()) for _ in range(q): input() n,k=map(int,input().split()) a=list(map(int,input().split())) t=list(map(int,input().split())) d = [0] * (n+1) for i in range(len(a)): d[a[i]] = t[i] l=[0]*(n+2);l[0]=10**10 r=[0]*(n+2);r[-1]=10**10 for i in range(1,n+1): if d[i]!=0: l[i]=min(l[i-1]+1,d[i]) else: l[i]=l[i-1]+1 for i in range(n,0,-1): if d[i]!=0: r[i]=min(r[i+1],d[i]) else: r[i]=r[i+1]+1 for i in range(1,n+1): print(min(l[i], r[i]),end=" ") print() ```
instruction
0
59,095
8
118,190
No
output
1
59,095
8
118,191
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≀ a_i ≀ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct). Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i. <image> Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16]. For each cell i (1 ≀ i ≀ n) find it's temperature, that can be calculated by the formula $$$min_{1 ≀ j ≀ k}(t_j + |a_j - i|),$$$ where |a_j - i| denotes absolute value of the difference a_j - i. In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i. Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are: 1. temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15; 2. temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14; 3. temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15; 4. temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16; 5. temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16; 6. temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17. For each cell from 1 to n find the temperature in it. Input The first line contains one integer q (1 ≀ q ≀ 10^4) β€” the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers n (1 ≀ n ≀ 3 β‹… 10^5) and k (1 ≀ k ≀ n) β€” the length of the strip of land and the number of air conditioners respectively. The second line contains k integers a_1, a_2, …, a_k (1 ≀ a_i ≀ n) β€” positions of air conditioners on the strip of land. The third line contains k integers t_1, t_2, …, t_k (1 ≀ t_i ≀ 10^9) β€” temperatures of air conditioners. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case output n integers separated by space: temperatures of air in cells. Example Input 5 6 2 2 5 14 16 10 1 7 30 5 5 3 1 4 2 5 3 1 4 2 5 7 1 1 1000000000 6 3 6 1 3 5 5 5 Output 15 14 15 16 16 17 36 35 34 33 32 31 30 31 32 33 1 2 3 4 5 1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 5 6 5 6 6 5 Submitted Solution: ``` for _ in range(int(input())): input() n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = [-1] * n for i in range(k): x[a[i] - 1] = b[i] pre = [float('inf')] * n start = a[0] - 1 mi = float('inf') mii = -1 for i in range(start, n): if x[i] != -1: if mi > x[i]: mi = x[i] mii = i pre[i] = min(pre[i], mi) mi += 1 suf = [float('inf')] * n start = a[-1] - 1 mi = float('inf') mii = -1 for i in range(start, -1, -1): if x[i] != -1: if mi > x[i]: mi = x[i] mii = i suf[i] = min(suf[i], mi) mi += 1 res = [] for i in range(n): res.append(min(pre[i], suf[i])) print(*res) ```
instruction
0
59,096
8
118,192
No
output
1
59,096
8
118,193
Provide tags and a correct Python 2 solution for this coding contest problem. Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: <image> "Are you kidding me?", asks Chris. For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7. <image> However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y! Input The first line of input contains a single integer n (1 ≀ n ≀ 5Β·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 106), the numbers of the blocks in X. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer m (1 ≀ m ≀ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≀ yi ≀ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi β‰  yj for all i, j (1 ≀ i ≀ n; 1 ≀ j ≀ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. Examples Input 3 1 4 5 Output 2 999993 1000000 Input 1 1 Output 1 1000000
instruction
0
59,193
8
118,386
Tags: greedy, implementation, math Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations import heapq raw_input = stdin.readline pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ # main code n=in_num() s=10**6 d=Counter(in_arr()) c=0 ans=[] for i in range(1,5*10**5+1): if d[i] and d[s-i+1]: c+=1 elif d[i]: ans.append(s-i+1) elif d[s-i+1]: ans.append(i) for i in range(1,5*10**5+1): if d[i] or d[s-i+1]: continue if c==0: break ans.append(i) ans.append(s-i+1) c-=1 pr_num(len(ans)) pr_arr(ans) ```
output
1
59,193
8
118,387
Provide tags and a correct Python 3 solution for this coding contest problem. Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: <image> "Are you kidding me?", asks Chris. For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7. <image> However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y! Input The first line of input contains a single integer n (1 ≀ n ≀ 5Β·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 106), the numbers of the blocks in X. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer m (1 ≀ m ≀ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≀ yi ≀ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi β‰  yj for all i, j (1 ≀ i ≀ n; 1 ≀ j ≀ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. Examples Input 3 1 4 5 Output 2 999993 1000000 Input 1 1 Output 1 1000000
instruction
0
59,194
8
118,388
Tags: greedy, implementation, math Correct Solution: ``` import sys INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br """ Facts and Data representation Constructive? Top bottom up down """ n, = I() a = I() M = 10 ** 6 + 1 take = [0] * M for i in a: take[i] = 1 ans = [] ok = [] cnt = 0 for i in range(1, M // 2 + 1): if take[i] and take[M - i]: cnt += 1 elif not take[i] and not take[M - i]: ok.append((i, M - i)) elif take[i]: ans.append(M - i) else: ans.append(i) for i in range(cnt): ans.append(ok[i][0]) ans.append(ok[i][1]) print(len(ans)) print(*ans) ```
output
1
59,194
8
118,389
Provide tags and a correct Python 3 solution for this coding contest problem. Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: <image> "Are you kidding me?", asks Chris. For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7. <image> However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y! Input The first line of input contains a single integer n (1 ≀ n ≀ 5Β·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 106), the numbers of the blocks in X. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer m (1 ≀ m ≀ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≀ yi ≀ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi β‰  yj for all i, j (1 ≀ i ≀ n; 1 ≀ j ≀ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. Examples Input 3 1 4 5 Output 2 999993 1000000 Input 1 1 Output 1 1000000
instruction
0
59,195
8
118,390
Tags: greedy, implementation, math Correct Solution: ``` import sys def solve(): n = int(input()) s = 1000000 xset = set(map(int, input().split())) res = set() wantother = 0 for i in range(1, s + 1): opposite = s - i + 1 if i in xset: if opposite not in xset: res.add(opposite) else: wantother+=1 wantother /= 2 for i in range(1, s + 1): if wantother == 0: break opposite = s - i + 1 if i not in res and i not in xset and opposite not in xset: res.add(i) res.add(opposite) wantother-=1 print(len(res)) return " ".join(map(str, res)) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") print(solve()) ```
output
1
59,195
8
118,391
Provide tags and a correct Python 3 solution for this coding contest problem. Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: <image> "Are you kidding me?", asks Chris. For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7. <image> However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y! Input The first line of input contains a single integer n (1 ≀ n ≀ 5Β·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 106), the numbers of the blocks in X. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer m (1 ≀ m ≀ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≀ yi ≀ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi β‰  yj for all i, j (1 ≀ i ≀ n; 1 ≀ j ≀ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. Examples Input 3 1 4 5 Output 2 999993 1000000 Input 1 1 Output 1 1000000
instruction
0
59,196
8
118,392
Tags: greedy, implementation, math Correct Solution: ``` import sys from math import gcd,sqrt,ceil from collections import defaultdict,Counter,deque import math # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdout=open("CP1/output.txt",'w') # sys.stdin=open("CP1/input.txt",'r') import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # import sys # import io, os # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_sum(bit,i): s = 0 i+=1 while i>0: s+=bit[i] i-=i&(-i) return s def update(bit,n,i,v): i+=1 while i<=n: bit[i]+=v i+=i&(-i) def modInverse(b,m): g = math.gcd(b, m) if (g != 1): return -1 else: return pow(b, m - 2, m) # print(lo,ha) n = int(input()) l = list(map(int,input().split())) s = 10**6 ans = set() l = set(l) w = 0 for i in range(1,s+1): opp = s+1-i if i in l and opp not in l: ans.add(opp) elif i in l and opp in l: w+=1 w//=2 for i in range(1,s+1): opp = s+1-i if w == 0: break if opp not in ans and i not in ans and i not in l and opp not in l: ans.add(opp) ans.add(i) w-=1 print(len(ans)) print(*ans) ```
output
1
59,196
8
118,393
Provide tags and a correct Python 3 solution for this coding contest problem. Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: <image> "Are you kidding me?", asks Chris. For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7. <image> However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y! Input The first line of input contains a single integer n (1 ≀ n ≀ 5Β·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 106), the numbers of the blocks in X. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer m (1 ≀ m ≀ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≀ yi ≀ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi β‰  yj for all i, j (1 ≀ i ≀ n; 1 ≀ j ≀ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. Examples Input 3 1 4 5 Output 2 999993 1000000 Input 1 1 Output 1 1000000
instruction
0
59,197
8
118,394
Tags: greedy, implementation, math Correct Solution: ``` if __name__ == '__main__': n = int(input()) l = [int(i) for i in list(input().split())] # mask to indicate if a number is taken size = 10**6 mask = [0] * (size+1) for i in l: mask[i] = 1 counter = 0 result = [] for i in range(1, size+1): if mask[i] == 1 and mask[size+1-i] == 0: # symmetric one not taken mask[size + 1 - i] = 2 result.append(size + 1 - i) elif mask[i] == 1 and mask[size+1-i] == 1: # take both one counter += 1 mask[size+1-i] = 2 elif mask[i] == 0 and mask[size+1-i] == 0 and counter > 0: mask[i] = 2 mask[size+1-i] = 2 result.append(i) result.append(size+1-i) counter -= 1 print(len(result)) print(*result) ```
output
1
59,197
8
118,395
Provide tags and a correct Python 3 solution for this coding contest problem. Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: <image> "Are you kidding me?", asks Chris. For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7. <image> However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y! Input The first line of input contains a single integer n (1 ≀ n ≀ 5Β·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 106), the numbers of the blocks in X. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer m (1 ≀ m ≀ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≀ yi ≀ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi β‰  yj for all i, j (1 ≀ i ≀ n; 1 ≀ j ≀ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. Examples Input 3 1 4 5 Output 2 999993 1000000 Input 1 1 Output 1 1000000
instruction
0
59,198
8
118,396
Tags: greedy, implementation, math Correct Solution: ``` import sys def solve(): n = int(input()) s = 1000000 xset = set(map(int, input().split())) res = set() wantother = 0 for i in range(1, s + 1): opposite = s - i + 1 if i in xset: if opposite not in xset: res.add(opposite) else: wantother+=1 wantother /= 2 for i in range(1, s + 1): if wantother == 0: break opposite = s - i + 1 if i not in res and opposite not in res and i not in xset and opposite not in xset: res.add(i) res.add(opposite) wantother-=1 print(len(res)) return " ".join(map(str, res)) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") print(solve()) ```
output
1
59,198
8
118,397
Provide tags and a correct Python 3 solution for this coding contest problem. Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is ai meters high. Vladimir has just planted n bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing. Vladimir wants to check the bamboos each d days (i.e. d days after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than k meters. What is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters of bamboo? Input The first line contains two integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1011) β€” the number of bamboos and the maximum total length of cut parts, in meters. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the required heights of bamboos, in meters. Output Print a single integer β€” the maximum value of d such that Vladimir can reach his goal. Examples Input 3 4 1 3 5 Output 3 Input 3 40 10 30 50 Output 32 Note In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.
instruction
0
59,381
8
118,762
Tags: brute force, data structures, implementation, math, number theory, sortings, two pointers Correct Solution: ``` import itertools unfold = itertools.chain.from_iterable def jumps(a): d = speedup while d < a - 1: c = (a + d - 1) // d d = (a + c - 2) // (c - 1) yield d def calc(d): return sum(d - 1 - (i - 1) % d for i in a) def ans(): for d, pd in zip(D, D[1:]): d -= 1 cd = calc(d) if cd <= k: return d if d == pd: continue cpd = calc(pd) if d - pd >= (cd - k) * (d - pd) / (cd - cpd): return d - (cd - k) * (d - pd) / (cd - cpd) return 1 n, k = map(int, input().split()) a = list(map(int, input().split())) speedup = int(max(a) ** 0.5) D = sorted(set(range(1, speedup + 1)).union([max(a) + k + 1]).union(set( unfold(map(jumps, a)))), reverse=True) print('%d' % ans()) ```
output
1
59,381
8
118,763