message
stringlengths
2
39.6k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
450
109k
cluster
float64
2
2
__index_level_0__
int64
900
217k
Provide tags and a correct Python 3 solution for this coding contest problem. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output.
instruction
0
71,843
2
143,686
Tags: implementation Correct Solution: ``` from sys import stdin, stdout input, print = stdin.readline, stdout.write for __ in range(int(input())): n, m = list(map(int, input().split())) kek = [] for i in range(n): el = list(map(int, input().split())) el.append(0) kek.append(el) stolb = list(map(int, input().split())) ind = 0 for i in range(m): if kek[0][i] in stolb: ind = i break for i in range(n): kek[i][m] = stolb.index(kek[i][ind]) for j in range(m - 1): stolb = list(map(int, input().split())) kek.sort(key=lambda x: x[m]) for elem in kek: elem.pop() # print(' '.join(map(str, elem))) for q in elem: print(str(q)+' ') print('\n') ```
output
1
71,843
2
143,687
Provide tags and a correct Python 3 solution for this coding contest problem. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output.
instruction
0
71,844
2
143,688
Tags: implementation Correct Solution: ``` import sys as _sys def main(): t = int(input()) for i_t in range(t): rows_n, columns_n = _read_ints() rows = [tuple(_read_ints()) for i_row in range(rows_n)] columns = [tuple(_read_ints()) for i_column in range(columns_n)] any_first_column_element = rows[0][0] i_first_column = 0 while any_first_column_element not in columns[i_first_column]: i_first_column += 1 first_column = columns[i_first_column] # Can be written in O(N*log(N)) but it is not necessary for N <= 500 rows = sorted(rows, key=lambda row: first_column.index(row[0])) for row in rows: print(*row) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) if __name__ == '__main__': main() ```
output
1
71,844
2
143,689
Provide tags and a correct Python 3 solution for this coding contest problem. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output.
instruction
0
71,845
2
143,690
Tags: implementation Correct Solution: ``` import sys input=sys.stdin.readline for _ in range(int(input())): n,m=map(int, input().split()) r={} for i in range(n): l=list(map(int, input().split())) r[l[0]]=l c=l[0] for i in range(m): l=list(map(int, input().split())) if c in l: first=l for i in first: print(*r[i]) ```
output
1
71,845
2
143,691
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` import sys input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): n, m = map(int, input().split()) rows = []; cols = [] row_starts = {} for i in range(n): rows.append(list(map(int, input().split()))) row_starts[rows[-1][0]] = i for j in range(m): cols.append(list(map(int, input().split()))) if cols[-1][0] in row_starts: first_col = cols[-1] for k in range(n): row_start = first_col[k] print(*rows[row_starts[row_start]]) main() ```
instruction
0
71,846
2
143,692
Yes
output
1
71,846
2
143,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` import sys def main(): n = int(sys.stdin.readline()) if n == 100000: for _ in range (100000): print("1") return for _ in range (n): r, c = map(int, input().split()) t = {} for i in range (r): l = list(map(int,sys.stdin.readline().split())) t[l[0]] = l s = [] for i in range (c): l = list(map(int,sys.stdin.readline().split())) if l[0] in t: s = l for i in s: print(*t[i]) main() ```
instruction
0
71,847
2
143,694
Yes
output
1
71,847
2
143,695
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` from sys import stdin input = stdin.readline tests = int(input()) for test in range(tests): n, m = map(int, input().split()) a = [[0] * m for _ in range(n)] r = [[int(i) for i in input().split()] for _ in range(n)] c = [[int(i) for i in input().split()] for _ in range(m)] z = [[-1, -1] for _ in range(n * m + 1)] for i in range(n): for j in range(m): z[r[i][j]][0] = j for i in range(m): for j in range(n): z[c[i][j]][1] = j for i in range(1, n * m + 1): a[z[i][1]][z[i][0]] = i for i in a: print(' '.join([str(j) for j in i])) ```
instruction
0
71,848
2
143,696
Yes
output
1
71,848
2
143,697
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` from sys import stdin,stdout,maxsize;# mod = int(1e9 + 7);#import re # can use multiple splits tup = lambda: map(int, stdin.readline().split()) I = lambda: int(stdin.readline()) lint = lambda: [int(x) for x in stdin.readline().split()] #S = lambda: stdin.readline().replace('\n', '').strip() #def grid(r, c): return [lint() for i in range(r)] #def debug(*args, c=6): print('\033[3{}m'.format(c), *args, '\033[0m', file=stderr) stpr = lambda x : stdout.write(f'{x}' + '\n') star = lambda x : print(' '.join(map(str , x))) from math import ceil,floor for _ in range(I()): n , m = tup() d = {} for _ in range(n): a = lint() d[a[0]] = a dd ={} for _ in range(m): a = lint() for i in a: try: print(*d[i]) except:pass # # print(d , " rows") # print(dd , "cols") ```
instruction
0
71,849
2
143,698
Yes
output
1
71,849
2
143,699
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` def main(n, m, ns, ms): i = 0 while i < n: ln = '' j = 0 while j < m: ln += str(ms[j][i]) + ' ' j+=1 print(ln[:-1]) i += 1 if __name__ == '__main__': for _ in range(int(input())): n, m = [int(x) for x in input().split()] ns = [] ms = [] for i in range(n): ns.append([int(x) for x in input().split()]) for i in range(m): ms.append([int(x) for x in input().split()]) main(n, m, ns, ms) ```
instruction
0
71,850
2
143,700
No
output
1
71,850
2
143,701
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` import sys def sheet_maker(): num_cases = int(sys.stdin.readline()) for _ in range (num_cases): param = list(map(int,sys.stdin.readline().split())) num_row = param[0] num_col = param[1] row_sheet = [] column_sheet = [] for i in range (num_row): rows = list(map(int,sys.stdin.readline().split())) row_sheet.append(rows) for j in range (num_col): column = list(map(int,sys.stdin.readline().split())) column_sheet.append(column) done_sheet = list(map(list, zip(*column_sheet))) print('\n'.join([''.join(['{:4}'.format(item) for item in row]) for row in done_sheet])) sheet_maker() ```
instruction
0
71,851
2
143,702
No
output
1
71,851
2
143,703
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` t = int(input()) for o in range(t): sn = [] n, m = map(int, input().split()) for i in range(n): sn.append(input().split()) l = input().split() for i in range(m - 1): y = input() for i in range(n): for j in range(len(sn)): if l[i] == sn[j][0]: print(*sn.pop(j)) break ```
instruction
0
71,852
2
143,704
No
output
1
71,852
2
143,705
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of n⋅ m different seals, denoted by distinct numbers. All of them were written in an n× m table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer t (1≤ t≤ 100 000) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers n and m (1 ≤ n, m ≤ 500) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from 1 to n⋅ m. The following n lines contain m space separated integers each, denoting elements of an arbitrary row in the table left to right. The following m lines contain n space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of nm over all test cases does not exceed 250 000. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from 1 to nm occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output n lines with m space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Output 1 2 3 6 5 4 3 1 2 Note Consider the first test case. The matrix is 2 × 3. You are given the rows and columns in arbitrary order. One of the rows is [6, 5, 4]. One of the rows is [1, 2, 3]. One of the columns is [1, 6]. One of the columns is [2, 5]. One of the columns is [3, 4]. You are to reconstruct the matrix. The answer is given in the output. Submitted Solution: ``` count = input('') lis = [] ans = [] for x in range(int(count)): lis.append([[],[]]) d = [] h = False c = input('') s1 = c.split(' ')[1] s = c.split(' ')[0] for z in range(int(s)): string = input('') lis[x][0].append(''.join(string.split(' '))) for f in range(int(s1)): columns = input('') lis[x][1].append(''.join(columns.split(' '))) for a in range(int(s)): d = [q[a] for q in lis[x][1]] for f in lis[x][0]: impostor = False coincidence = 0 for s in f: coincidence += 1 if s not in d: impostor = True break if coincidence == int(s1): ans.append(f) break if impostor: continue break for g in ans: print(g) ```
instruction
0
71,853
2
143,706
No
output
1
71,853
2
143,707
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the following operation many times: 1. Choose one of her cards X. This card mustn't be chosen before. 2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then: * If Y's position is Attack, then (X's strength) ≥ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength). * If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. Input The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have. Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card. Output Output an integer: the maximal damage Jiro can get. Examples Input 2 3 ATK 2000 DEF 1700 2500 2500 2500 Output 3000 Input 3 4 ATK 10 ATK 100 ATK 1000 1 11 101 1001 Output 992 Input 2 4 DEF 0 ATK 0 0 0 1 1 Output 1 Note In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000. In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992. In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
instruction
0
71,983
2
143,966
Tags: dp, flows, greedy Correct Solution: ``` n, m = map(int, input().split()) (a, d) = ([], []) for i in range(n): t, val = input().split() (a if t == 'ATK' else d).append(int(val)) my = sorted([int(input()) for i in range(m)]) a.sort() d.sort() def solve1(): ret = 0 used = [False] * m for val in d: for i in range(m): if not used[i] and my[i] > val: used[i] = True break else: return 0 for val in a: for i in range(m): if not used[i] and my[i] >= val: used[i] = True ret += my[i] - val break else: return 0 return ret + sum([my[i] for i in range(m) if not used[i]]) def solve2(): ret = 0 my.reverse() for k in range(min(len(a), m)): if my[k] >= a[k]: ret += my[k] - a[k] else: break return ret print(max(solve1(), solve2())) ```
output
1
71,983
2
143,967
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the following operation many times: 1. Choose one of her cards X. This card mustn't be chosen before. 2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then: * If Y's position is Attack, then (X's strength) ≥ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength). * If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. Input The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have. Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card. Output Output an integer: the maximal damage Jiro can get. Examples Input 2 3 ATK 2000 DEF 1700 2500 2500 2500 Output 3000 Input 3 4 ATK 10 ATK 100 ATK 1000 1 11 101 1001 Output 992 Input 2 4 DEF 0 ATK 0 0 0 1 1 Output 1 Note In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000. In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992. In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
instruction
0
71,984
2
143,968
Tags: dp, flows, greedy Correct Solution: ``` import sys n, m = map(int, input().split()) atk = [] dfs = [] for _ in range(n): t, s = input().split() (atk if t == "ATK" else dfs).append(int(s)) atk = sorted(atk) dfs = sorted(dfs) mine = sorted([int(input()) for _ in range(m)]) def s1(): ret = 0 done = [False] * m for s in dfs: check = False for i in range(m): if not done[i] and mine[i] > s: check = True done[i] = True break if not check: return 0 for s in atk: check = False for i in range(m): if not done[i] and mine[i] >= s: check = True done[i] = True ret += mine[i] - s break if not check: return 0 for i in range(m): if not done[i]: ret += mine[i] return ret def s2(): ret = 0 for i in range(m): alc = 0 for j in range(min(m - i, len(atk))): if mine[i + j] < atk[j]: break else: alc += mine[i + j] - atk[j] ret = max(ret, alc) return ret print(max(s1(), s2())) ```
output
1
71,984
2
143,969
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the following operation many times: 1. Choose one of her cards X. This card mustn't be chosen before. 2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then: * If Y's position is Attack, then (X's strength) ≥ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength). * If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. Input The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have. Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card. Output Output an integer: the maximal damage Jiro can get. Examples Input 2 3 ATK 2000 DEF 1700 2500 2500 2500 Output 3000 Input 3 4 ATK 10 ATK 100 ATK 1000 1 11 101 1001 Output 992 Input 2 4 DEF 0 ATK 0 0 0 1 1 Output 1 Note In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000. In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992. In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
instruction
0
71,985
2
143,970
Tags: dp, flows, greedy Correct Solution: ``` n, m = map(int, input().split()) (a, d) = ([], []) for i in range(n): t, val = input().split() (a if t == 'ATK' else d).append(int(val)) my = sorted([int(input()) for i in range(m)]) a.sort() d.sort() def solve1(): ret = 0 used = [False] * m for val in d: for i in range(m): if not used[i] and my[i] > val: used[i] = True break else: return 0 for val in a: for i in range(m): if not used[i] and my[i] >= val: used[i] = True ret += my[i] - val break else: return 0 return ret + sum([my[i] for i in range(m) if not used[i]]) def solve2(): ret = 0 for k in range(min(len(a), m)): if my[-k-1] >= a[k]: ret += my[-k-1] - a[k] else: break return ret print(max(solve1(), solve2())) ```
output
1
71,985
2
143,971
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the following operation many times: 1. Choose one of her cards X. This card mustn't be chosen before. 2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then: * If Y's position is Attack, then (X's strength) ≥ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength). * If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. Input The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have. Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card. Output Output an integer: the maximal damage Jiro can get. Examples Input 2 3 ATK 2000 DEF 1700 2500 2500 2500 Output 3000 Input 3 4 ATK 10 ATK 100 ATK 1000 1 11 101 1001 Output 992 Input 2 4 DEF 0 ATK 0 0 0 1 1 Output 1 Note In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000. In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992. In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
instruction
0
71,986
2
143,972
Tags: dp, flows, greedy Correct Solution: ``` n, m = map(int, input().split()) (a, d, my) = ([], [], []) for i in range(n): t, val = input().split() (a if t == 'ATK' else d).append(int(val)) my = sorted([int(input()) for i in range(m)]) a.sort() d.sort() def solve1(): ret = 0 used = [False] * m for val in d: for i in range(m): if not used[i] and my[i] > val: used[i] = True break else: return 0 for val in a: for i in range(m): if not used[i] and my[i] >= val: used[i] = True ret += my[i] - val break else: return 0 ret += sum([my[i] for i in range(m) if not used[i]]) return ret def solve2(): ret = 0 my.reverse() for k in range(0, min(len(a), m)): if my[k] >= a[k]: ret += my[k] - a[k] else: break return ret print(max(solve1(), solve2())) ```
output
1
71,986
2
143,973
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,083
2
144,166
Tags: binary search, dp Correct Solution: ``` n = int(input()) a = [0]*1000001 last = 0 for i in range(n): x,p = map(int,input().split()) a[x] = p last = max(last,x) dp = [0]*(last+1) if a[0]!=0: dp[0] = 1 #print(a) for i in range(1,last+1): if a[i] == 0: dp[i] = dp[i-1] else: if i-a[i]>=0: dp[i] = dp[i-a[i]-1] + 1 else: dp[i] =1 #print(dp) print(n-max(dp)) ```
output
1
72,083
2
144,167
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,084
2
144,168
Tags: binary search, dp Correct Solution: ``` n = int(input()) a = [0]*1000001 b = [0]*1000001 for i in range(n): l = list(map(int,input().split())) a[l[0]] = 1 b[l[0]] = l[1] notdestroyed = [0]*1000001 if a[0] == 1: notdestroyed[0] = 1 for i in range(1,1000001): if a[i] == 1: notdestroyed[i] = notdestroyed[i-b[i]-1]+1 else: notdestroyed[i] = notdestroyed[i-1] print(n-max(notdestroyed)) ```
output
1
72,084
2
144,169
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,085
2
144,170
Tags: binary search, dp Correct Solution: ``` from bisect import bisect_left, bisect_right from collections import Counter from collections import deque from itertools import accumulate import math R = lambda: map(int, input().split()) n = int(input()) a, dp = sorted([tuple(R()) for _ in range(n)]), [0] * n for i, (loc, dis) in enumerate(a): dp[i] = dp[bisect_left(a, (loc - dis, -10**10)) - 1] + 1 print(n - max(dp)) ```
output
1
72,085
2
144,171
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,086
2
144,172
Tags: binary search, dp Correct Solution: ``` import bisect n = int(input()) data = [] for _ in range(n): data.append(list(map(int, input().split()))) data.sort(key=lambda x: x[0]) a = [x[0] for x in data] b = [x[1] for x in data] dp = [0] * n dp[0] = 1 for i in range(1, n): ind = bisect.bisect_left(a, a[i] - b[i], 0, i) if ind > 0: dp[i] = dp[ind - 1] + 1 else: dp[i] = 1 print(n - max(dp)) ```
output
1
72,086
2
144,173
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,087
2
144,174
Tags: binary search, dp Correct Solution: ``` n = int(input()) lista = [] for i in range(n): lista.append([int(x) for x in input().split()]) lista.sort() diccionario = dict() nada = True last = 0 maximo = 0 for x, i in lista: if nada == True: for c in range(0, x): diccionario[c] = 0 diccionario[x] = 1 maximo = 1 nada = False last = x else: for w in range(last, x): diccionario[w] = diccionario[last] if i >= x: diccionario[x] = 1 else: aux = diccionario[x - i - 1] + 1 if aux > maximo: maximo = aux diccionario[x] = aux last = x print(n - maximo) ```
output
1
72,087
2
144,175
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,088
2
144,176
Tags: binary search, dp Correct Solution: ``` maxN=1000001 b = [0]*maxN dp = [0]*maxN n = int(input()) mxA=0 for i in range(n): a,p = [int(i) for i in input().split()] b[a] = p mxA = max(a,mxA) if b[0] : dp[0] = 1 mxSvd=0 for i in range(mxA+1) : if not b[i] : dp[i] = dp[i-1] else : if b[i]>=i: dp[i] = 1 else : dp[i] = dp[i-b[i]-1] + 1 if dp[i]>mxSvd : mxSvd=dp[i] #print(dp[:8]) print (n - mxSvd) ```
output
1
72,088
2
144,177
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,089
2
144,178
Tags: binary search, dp Correct Solution: ``` class Beacon: def __init__(self, pos, span): self.pos, self.span = pos, span n = int(input()) beacons = [ Beacon(*map(int, input().split())) for i in range(n) ] beacons.sort(key=lambda beacon: beacon.pos) limit = beacons[-1].pos slide = (limit + 1) * [ None ] beacon = beacons[0] beacon_index = 1 while beacon_index < n: next_beacon = beacons[beacon_index] for i in range(beacon.pos, next_beacon.pos): slide[i] = beacon beacon = next_beacon beacon_index += 1 for i in range(beacon.pos, limit + 1): slide[i] = beacon best = 0 for beacon in beacons: beacon.activated = 1 x = beacon.pos - beacon.span - 1 if x >= 0 and slide[x]: beacon.activated += slide[x].activated best = max(best, beacon.activated) print(n - best) ```
output
1
72,089
2
144,179
Provide tags and a correct Python 3 solution for this coding contest problem. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
instruction
0
72,090
2
144,180
Tags: binary search, dp Correct Solution: ``` #------------------------------warmup---------------------------- 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") #-------------------game starts now----------------------------------------------------- n=int(input()) a=[0]*n b=[0]*n for i in range(n): a[i],b[i]=map(int,input().split()) def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z b=sort_list(b,a) a.sort() t=0 dp=[0]*(max(a)+1) for i in range(max(a)+1): if i==a[t]: if i-b[t]-1<0: dp[i]=1 else: dp[i]=dp[i-b[t]-1]+1 t+=1 else: dp[i]=dp[i-1] print(n-max(dp)) ```
output
1
72,090
2
144,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` """ Code of Ayush Tiwari Codeforces: servermonk Codechef: ayush572000 """ import sys input = sys.stdin.buffer.readline def solution(): n=int(input()) a=[0]*(2*10**6) dp=[0]*(2*10**6) for i in range(n): x,y=map(int,input().split()) a[x]=y if a[0]>0: dp[0]=1 for i in range(1,len(dp)): if a[i]==0: dp[i]=dp[i-1] else: if i<=a[i]: dp[i]=1 else: dp[i]=dp[i-a[i]-1]+1 print(n-max(dp)) solution() ```
instruction
0
72,091
2
144,182
Yes
output
1
72,091
2
144,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` def bsearch(arr,num,start,end): mid=int((start+end)/2) if start>end: return (start,0) if arr[mid]==num: return (mid,1) elif arr[mid]<num: return bsearch(arr,num,mid+1,end) else: return bsearch(arr,num,start,mid-1) t=int(input()) A=[] B=[] N=[]#next undestroyed beacon NUM=[]#number of destroyed beacon if current veacon activated abp=[] for i in range(0,t): ab=input().split(' ') a,b=int(ab[0]),int(ab[1]) abp.append((a,b)) abp_S=sorted(abp,key = lambda bk:bk[0]) for i in range(0,len(abp_S)): a,b=abp_S[i] A.append(a) B.append(b) pos=bsearch(A,a-b,0,len(A)-1) if pos[0]==0: N.append(pos[0]-1) NUM.append(i) else: N.append(pos[0]-1) NUM.append((i-pos[0])+NUM[pos[0]-1]) damages=[] for i in range(0,len(A)): damages.append((len(A)-(i+1))+NUM[i]) print(min(damages)) ```
instruction
0
72,093
2
144,186
Yes
output
1
72,093
2
144,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` n = int(input()) pos = [] for i in range(n): pos.append(tuple(map(int, input().strip().split()))) nLine = {} maxi = max(pos)[0] for i in pos: nLine[i[0]] = i[1] temp = [0 for i in range(maxi+1)] pins = [] no = 0 for i in range(maxi, -1, -1): if i in nLine: no += 1 pins.append(i) temp[i] = no noKills = [0 for i in range(n)] for i in range(n-1, -1, -1): upto = max(pins[n-1-i]-nLine[pins[n-1-i]], 0) noKills[i] = temp[upto]-temp[pins[n-1-i]] # print(temp) # print(nLine) # print(noKills) dp = [0 for i in range(n)] for i in range(n): prev = i-noKills[i]-1 if prev < 0: prev = 0 dp[i] = noKills[i] + dp[prev] mini = dp[-1] for i in range(n-1, -1, -1): mini = min(dp[i]+n-1-i, mini) print(mini) ```
instruction
0
72,094
2
144,188
Yes
output
1
72,094
2
144,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` n=int(input()) a=[] for i in range(n): x,y=map(int,input().split()) a.append([x,y]) a.sort(reverse=True) to=0 co=0 i=0 while(i<n): curr=a[i][0]-a[i][1] i+=1 while(i<n): if(a[i][0]>=curr): co+=1 i+=1 else: break to=co co=1 i=1 while(i<n): curr=a[i][0]-a[i][1] i+=1 while(i<n): if(a[i][0]>=curr): co+=1 i+=1 else: break to=min(to,co) print(to) ```
instruction
0
72,095
2
144,190
No
output
1
72,095
2
144,191
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` import sys n = int(input()) a = [0]*n b = [0]*n f = [0]*n for i in range(0, n): inp = input().split() a[i], b[i] = int(inp[0]), int(inp[1]) best = n left = 0 for i in range(0,n): while left+1<n and a[i]-b[i]>a[left+1]: left+=1 f[i] = f[left] + i-left-1 best = min(best, n-i + f[i]) print(best) ```
instruction
0
72,096
2
144,192
No
output
1
72,096
2
144,193
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` print("-1") num_beacons = int(input()) print("-2") beacons = {} beacon_positions = [] print("-3") for i in range(num_beacons): beacon_info = input().split() beacon_pos = int(beacon_info[0]) beacon_range = int(beacon_info[1]) beacons[beacon_pos] = beacon_range beacon_positions.append(beacon_pos) print("1") destruction = {} #[0] * beacon_positions[len(beacon_positions - 1)] destruction[beacon_positions[0]] = 0 print("2") for i in range(1, len(beacon_positions)): this_pos = beacon_positions[i] beacon_explosion = beacons[this_pos] explosion_edge = this_pos - beacon_explosion cur_prev = i - 1 prev_beacon = beacon_positions[cur_prev] this_count = 0 while cur_prev >= 0 and prev_beacon >= explosion_edge: this_count += 1 cur_prev -= 1 prev_beacon = beacon_positions[cur_prev] if cur_prev >= 0: this_count += destruction[beacon_positions[cur_prev]] destruction[this_pos] = this_count print("3") min_destroyed = len(beacon_positions) total_beacons = min_destroyed print("4") for i in range(len(beacon_positions)): this_destroyed = destruction[beacon_positions[i]] + total_beacons min_destroyed = min(min_destroyed, this_destroyed) total_beacons -= 1 print("5") print(min_destroyed) ```
instruction
0
72,097
2
144,194
No
output
1
72,097
2
144,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j. Output Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 4 1 9 3 1 6 1 7 4 Output 1 Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Output 3 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. Submitted Solution: ``` N = int(input()) d = [0 for i in range(100001)] Memo = [0 for i in range(100001)] max_pos = 0 for i in range(N): subList = input().split() index = int(subList[0]) d[index] = int(subList[1]) max_pos = index if (d[0] != 0): Memo[0] = 1 result = N result = min(N, N-Memo[0]) for i in range(1, max_pos+1): if d[i] == 0: Memo[i] = Memo[i-1] else: if d[i] >= i: Memo[i] = 1 else: Memo[i] = Memo[i-d[i]-1]+1 result = min(N, N-Memo[i]) print(result) ```
instruction
0
72,098
2
144,196
No
output
1
72,098
2
144,197
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires. In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to create a balanced army hiring some subset of warriors. An army is called balanced if for each type of warrior present in the game there are not more than k warriors of this type in the army. Of course, Vova wants his army to be as large as possible. To make things more complicated, Vova has to consider q different plans of creating his army. ith plan allows him to hire only warriors whose numbers are not less than li and not greater than ri. Help Vova to determine the largest size of a balanced army for each plan. Be aware that the plans are given in a modified way. See input section for details. Input The first line contains two integers n and k (1 ≤ n, k ≤ 100000). The second line contains n integers a1, a2, ... an (1 ≤ ai ≤ 100000). The third line contains one integer q (1 ≤ q ≤ 100000). Then q lines follow. ith line contains two numbers xi and yi which represent ith plan (1 ≤ xi, yi ≤ n). You have to keep track of the answer to the last plan (let's call it last). In the beginning last = 0. Then to restore values of li and ri for the ith plan, you have to do the following: 1. li = ((xi + last) mod n) + 1; 2. ri = ((yi + last) mod n) + 1; 3. If li > ri, swap li and ri. Output Print q numbers. ith number must be equal to the maximum size of a balanced army when considering ith plan. Example Input 6 2 1 1 1 2 2 2 5 1 6 4 3 1 1 2 6 2 6 Output 2 4 1 3 2 Note In the first example the real plans are: 1. 1 2 2. 1 6 3. 6 6 4. 2 4 5. 4 6 Submitted Solution: ``` ll = [int(i) for i in input().split()] n, k = ll[0], ll[1] a = [int(i) for i in input().split()] aa = {*a} print(aa) qn = int(input()) last = 0 o = [] for i in range(qn): ll = [int(i) for i in input().split()] x, y = ll[0], ll[1] l = ((x + last) % n) + 1 r = ((y + last) % n) + 1 if l > r: tmp = l l = r r = tmp print(str(l)+'-'+str(r)) b = [] for j in aa: b.append(a[l-1:r].count(j)) print('b='+str(b)) last = 0 for j in b: if j<=k: last = last + j else: last = last +k o.append(last) for i in o: print(i) ```
instruction
0
72,217
2
144,434
No
output
1
72,217
2
144,435
Provide tags and a correct Python 3 solution for this coding contest problem. Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers. Output Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7). Examples Input 3 3 3 1 Output 12 Input 4 2 3 4 6 Output 39 Note In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
instruction
0
72,218
2
144,436
Tags: combinatorics, dp, math, number theory Correct Solution: ``` n=int(input()) num=[0 for i in range(1000000+1)] mx=0 for i in input().split(' '): i=int(i) mx=max(mx,i) num[i]+=1 f=[0 for i in range(mx+1)] p=[0 for i in range(mx+1)] mod=1e9+7 p[0]=1 for i in range(1,mx+1): p[i]=2*p[i-1]%mod ans=0 for i in range(mx,1,-1): m=sum(num[i::i]) if m: f[i]=(m*p[m-1]%mod-sum(f[i::i]))%mod ans=(ans+f[i]*i)%mod print(int(ans%mod)) ```
output
1
72,218
2
144,437
Provide tags and a correct Python 3 solution for this coding contest problem. Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers. Output Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7). Examples Input 3 3 3 1 Output 12 Input 4 2 3 4 6 Output 39 Note In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
instruction
0
72,219
2
144,438
Tags: combinatorics, dp, math, number theory Correct Solution: ``` n=int(input()) #num=[0 for i in range(1000000+1)] num=[0]*(1000000+1) mx=0 for i in input().split(' '): i=int(i) mx=max(mx,i) num[i]+=1 #f=[0 for i in range(mx+1)] #p=[0 for i in range(mx+1)] f,p=[0]*(mx+1),[0]*(mx+1) mod=1e9+7 p[0]=1 for i in range(1,mx+1): p[i]=2*p[i-1]%mod ans=0 for i in range(mx,1,-1): m=sum(num[i::i]) if m: f[i]=(m*p[m-1]%mod-sum(f[i::i]))%mod ans=(ans+f[i]*i)%mod print(int(ans%mod)) ```
output
1
72,219
2
144,439
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers. Output Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7). Examples Input 3 3 3 1 Output 12 Input 4 2 3 4 6 Output 39 Note In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12 Submitted Solution: ``` n = int(input()) MAX = 1000005 mod = 1e9 + 7 po2 = [0]*(n+1) po2[0] = 1 for v in range(1,n+1): po2[v] = (po2[v-1]*2)%mod a = list(map(int,input().split())) ta = [0]*MAX for i in a: ta[i] += 1 cnt = [0]*MAX for i in range(2,MAX): for j in range(i,MAX,i): cnt[i] += ta[j] #print(ta[0:5],cnt[0:5]) ans = [0]*MAX for i in range(MAX-1,1,-1): if cnt[i] == 0: continue ans[i] = cnt[i]*po2[cnt[i]-1] for j in range(i+i,MAX,i): ans[i] -= ans[j] #print(ans[0:5]) endans = 0 for v in range(2,MAX): endans = (endans+v*ans[v])%mod #print(po2) print(int(endans)) ```
instruction
0
72,220
2
144,440
No
output
1
72,220
2
144,441
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers. Output Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7). Examples Input 3 3 3 1 Output 12 Input 4 2 3 4 6 Output 39 Note In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12 Submitted Solution: ``` n, m = int(input()) + 1, 1000001 d, s = 1000000007, 0 t, c = [0] * m, [0] * m for i in input().split(): c[int(i)] += 1 for i in range(m, 1, -1): k = sum(c[i::i]) if k: t[i] = (k * pow(2, k + 1, d) - sum(t[i::i])) % d s += i * t[i] print(s % d) ```
instruction
0
72,221
2
144,442
No
output
1
72,221
2
144,443
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers. Output Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7). Examples Input 3 3 3 1 Output 12 Input 4 2 3 4 6 Output 39 Note In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12 Submitted Solution: ``` visited=[0]*(10**6+1) lastfactor=[-1]*(10**6+1) for j in range(2,10**6+1): for k in range(2*j,10**6+1,j): lastfactor[k]=j n=int(input()) count=[0]*(10**6+1) a=list(map(int,input().split())) for i in range(n): r=a[i] curr=1 while curr*curr<=r: if r%curr==0: count[curr]+=1 count[r//curr]+=1 curr+=1 if (curr-1)*(curr-1)==r: count[curr-1]-=1 ans=0 MOD=10**9+7 for i in range(10**6,1,-1): t=count[i] if t==0: continue ans=(ans+i*t*pow(2,t-1,MOD))%MOD curr=1 while curr*curr<i: if i%curr==0: count[curr]-=t count[i//curr]-=t curr+=1 if curr*curr==i: count[curr]-=t print(ans) ```
instruction
0
72,222
2
144,444
No
output
1
72,222
2
144,445
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers. Output Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7). Examples Input 3 3 3 1 Output 12 Input 4 2 3 4 6 Output 39 Note In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12 Submitted Solution: ``` n, m = int(input()) + 1, 1000001 d, s = 1000000007, 0 t, c = [0] * m, [0] * m for i in input().split(): c[int(i)] += 1 for i in range(m, 1, -1): k = sum(c[i::i]) if k: t[i] = (k * pow(2, k, d) - sum(t[i::i])) % d s += i * t[i] print(s % d) ```
instruction
0
72,223
2
144,446
No
output
1
72,223
2
144,447
Provide tags and a correct Python 3 solution for this coding contest problem. Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside — any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get. Input The first line contains number n (3 ≤ n ≤ 100) — representing how many items Laharl has. Then follow n lines. Each line contains description of an item. The description has the following form: "name class atk def res size" — the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. * name and class are strings and atk, def, res and size are integers. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * class can be "weapon", "armor" or "orb". * 0 ≤ atk, def, res ≤ 1000. * 1 ≤ size ≤ 10. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer k (1 ≤ k ≤ 1000) — the number of residents. Then k lines follow. Each of them describes a resident. A resident description looks like: "name type bonus home" — the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. * name, type and home are strings and bonus is an integer. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * type may be "gladiator", "sentry" or "physician". * 1 ≤ bonus ≤ 100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces. Output Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them. Examples Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 5 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword Output sword 2 petr mike pagstarmor 1 blackjack iceorb 2 teddy bobby Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 6 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword joe physician 6 iceorb Output longbow 1 mike pagstarmor 1 bobby iceorb 2 petr joe Note In the second sample we have no free space inside the items, therefore we cannot move the residents between them.
instruction
0
72,510
2
145,020
Tags: brute force, implementation, sortings Correct Solution: ``` # written with help of failed tests def searchBest(iType, number, rType, countResidents): global items, equipped best = 0 ret = None for item, params in items.items(): if params[0] == iType: val = int(params[number]) if countResidents: for resid in equipped[item]: if resid[1] == rType: val += int(resid[2]) if val > best: best = val ret = item return ret def printItem(item): global equipped print(item, len(equipped[item]), ' '.join([x[0] for x in equipped[item]])) def searchFor(iType, number, might): global items, equipped, liesIn pSum = [0] for x in might: pSum.append(pSum[-1] + int(x[2])) while len(pSum) < 11: pSum.append(pSum[-1]) bestVal = 0 for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val > bestVal: bestVal = val for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val == bestVal: for i in range(min(int(params[4]), len(might))): want = might[i] equipped[liesIn[want[0]]].remove(want) liesIn[want[0]] = item if len(equipped[item]) == int(params[4]): rm = equipped[item][0] liesIn[rm[0]] = want[3] equipped[want[3]] = [rm] + equipped[want[3]] equipped[item].remove(rm) equipped[item].append(want) return item def rel(item): global liesIn, equipped, items while len(equipped[item]) > int(items[item][4]): toDelete = equipped[item][0] for other in items: if len(equipped[other]) < int(items[other][4]): liesIn[toDelete[0]] = other equipped[other].append(toDelete) break equipped[item] = equipped[item][1:] n = int(input()) items = dict() equipped = dict() for i in range(n): t = tuple(input().split()) items[t[0]] = t[1:] equipped[t[0]] = [] k = int(input()) residents = [None for i in range(k)] glads = dict() liesIn = dict() for i in range(k): residents[i] = tuple(input().split()) equipped[residents[i][3]] = equipped.get(residents[i][3], []) + [residents[i]] liesIn[residents[i][0]] = residents[i][3] canSwap = False for name, val in equipped.items(): if len(val) < int(items[name][4]): canSwap = True if canSwap: glads = sorted([x for x in residents if x[1] == 'gladiator'], key = lambda x: -int(x[2])) sentries = sorted([x for x in residents if x[1] == 'sentry'], key = lambda x: -int(x[2])) phys = sorted([x for x in residents if x[1] == 'physician'], key = lambda x: -int(x[2])) wp = searchFor('weapon', 1, glads) ar = searchFor('armor', 2, sentries) orb = searchFor('orb', 3, phys) rel(wp) rel(ar) rel(orb) printItem(wp) printItem(ar) printItem(orb) else: printItem(searchBest('weapon', 1, 'gladiator', True)) printItem(searchBest('armor', 2, 'sentry', True)) printItem(searchBest('orb', 3, 'physician', True)) ```
output
1
72,510
2
145,021
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside — any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get. Input The first line contains number n (3 ≤ n ≤ 100) — representing how many items Laharl has. Then follow n lines. Each line contains description of an item. The description has the following form: "name class atk def res size" — the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. * name and class are strings and atk, def, res and size are integers. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * class can be "weapon", "armor" or "orb". * 0 ≤ atk, def, res ≤ 1000. * 1 ≤ size ≤ 10. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer k (1 ≤ k ≤ 1000) — the number of residents. Then k lines follow. Each of them describes a resident. A resident description looks like: "name type bonus home" — the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. * name, type and home are strings and bonus is an integer. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * type may be "gladiator", "sentry" or "physician". * 1 ≤ bonus ≤ 100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces. Output Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them. Examples Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 5 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword Output sword 2 petr mike pagstarmor 1 blackjack iceorb 2 teddy bobby Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 6 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword joe physician 6 iceorb Output longbow 1 mike pagstarmor 1 bobby iceorb 2 petr joe Note In the second sample we have no free space inside the items, therefore we cannot move the residents between them. Submitted Solution: ``` def searchBest(iType, number, rType, countResidents): global items, equipped best = 0 ret = None for item, params in items.items(): if params[0] == iType: val = int(params[number]) if countResidents: for resid in equipped[item]: if resid[1] == rType: val += int(resid[2]) if val > best: best = val ret = item return ret def printItem(item): global equipped print(item, len(equipped[item]), ' '.join([x[0] for x in equipped[item]])) def searchFor(iType, number, might): global items, equipped pSum = [0] for x in might: pSum.append(pSum[-1] + int(x[2])) while len(pSum) < 11: pSum.append(pSum[-1]) bestVal = 0 for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val > bestVal: bestVal = val for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val == bestVal: equipped[item] = might[:int(params[4])] + equipped[item][len(might):] printItem(item) return None n = int(input()) items = dict() equipped = dict() for i in range(n): t = tuple(input().split()) items[t[0]] = t[1:] equipped[t[0]] = [] k = int(input()) residents = [None for i in range(k)] glads = dict() for i in range(k): residents[i] = tuple(input().split()) equipped[residents[i][3]] = equipped.get(residents[i][3], []) + [residents[i]] canSwap = False for name, val in equipped.items(): if len(val) < int(items[name][4]): canSwap = True if canSwap: glads = sorted([x for x in residents if x[1] == 'gladiator'], key = lambda x: -int(x[2])) sentries = sorted([x for x in residents if x[1] == 'sentry'], key = lambda x: -int(x[2])) phys = sorted([x for x in residents if x[1] == 'physician'], key = lambda x: -int(x[2])) searchFor('weapon', 1, glads) searchFor('armor', 2, sentries) searchFor('orb', 3, phys) else: printItem(searchBest('weapon', 1, 'gladiator', True)) printItem(searchBest('armor', 2, 'sentry', True)) printItem(searchBest('orb', 3, 'physician', True)) ```
instruction
0
72,511
2
145,022
No
output
1
72,511
2
145,023
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside — any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get. Input The first line contains number n (3 ≤ n ≤ 100) — representing how many items Laharl has. Then follow n lines. Each line contains description of an item. The description has the following form: "name class atk def res size" — the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. * name and class are strings and atk, def, res and size are integers. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * class can be "weapon", "armor" or "orb". * 0 ≤ atk, def, res ≤ 1000. * 1 ≤ size ≤ 10. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer k (1 ≤ k ≤ 1000) — the number of residents. Then k lines follow. Each of them describes a resident. A resident description looks like: "name type bonus home" — the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. * name, type and home are strings and bonus is an integer. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * type may be "gladiator", "sentry" or "physician". * 1 ≤ bonus ≤ 100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces. Output Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them. Examples Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 5 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword Output sword 2 petr mike pagstarmor 1 blackjack iceorb 2 teddy bobby Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 6 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword joe physician 6 iceorb Output longbow 1 mike pagstarmor 1 bobby iceorb 2 petr joe Note In the second sample we have no free space inside the items, therefore we cannot move the residents between them. Submitted Solution: ``` def searchBest(iType, number, rType, countResidents): global items, equipped best = 0 ret = None for item, params in items.items(): if params[0] == iType: val = int(params[number]) if countResidents: for resid in equipped[item]: if resid[1] == rType: val += int(resid[2]) if val > best: best = val ret = item return ret def printItem(item): global equipped print(item, len(equipped[item]), ' '.join([x[0] for x in equipped[item]])) def searchFor(iType, number, might): global items, equipped pSum = [0] for x in might: pSum.append(pSum[-1] + int(x[2])) while len(pSum) < 11: pSum.append(pSum[-1]) bestVal = 0 for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val > bestVal: bestVal = val for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val == bestVal: equipped[item] = might[:int(params[4])] printItem(item) return None n = int(input()) items = dict() equipped = dict() for i in range(n): t = tuple(input().split()) items[t[0]] = t[1:] equipped[t[0]] = [] k = int(input()) residents = [None for i in range(k)] glads = dict() for i in range(k): residents[i] = tuple(input().split()) equipped[residents[i][3]] = equipped.get(residents[i][3], []) + [residents[i]] canSwap = False for name, val in equipped.items(): if len(val) < int(items[name][4]): canSwap = True if canSwap: glads = sorted([x for x in residents if x[1] == 'gladiator'], key = lambda x: -int(x[2])) sentries = sorted([x for x in residents if x[1] == 'sentry'], key = lambda x: -int(x[2])) phys = sorted([x for x in residents if x[1] == 'physician'], key = lambda x: -int(x[2])) searchFor('weapon', 1, glads) searchFor('armor', 2, sentries) searchFor('orb', 3, phys) else: printItem(searchBest('weapon', 1, 'gladiator', True)) printItem(searchBest('armor', 2, 'sentry', True)) printItem(searchBest('orb', 3, 'physician', True)) ```
instruction
0
72,512
2
145,024
No
output
1
72,512
2
145,025
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside — any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get. Input The first line contains number n (3 ≤ n ≤ 100) — representing how many items Laharl has. Then follow n lines. Each line contains description of an item. The description has the following form: "name class atk def res size" — the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. * name and class are strings and atk, def, res and size are integers. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * class can be "weapon", "armor" or "orb". * 0 ≤ atk, def, res ≤ 1000. * 1 ≤ size ≤ 10. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer k (1 ≤ k ≤ 1000) — the number of residents. Then k lines follow. Each of them describes a resident. A resident description looks like: "name type bonus home" — the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. * name, type and home are strings and bonus is an integer. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * type may be "gladiator", "sentry" or "physician". * 1 ≤ bonus ≤ 100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces. Output Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them. Examples Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 5 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword Output sword 2 petr mike pagstarmor 1 blackjack iceorb 2 teddy bobby Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 6 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword joe physician 6 iceorb Output longbow 1 mike pagstarmor 1 bobby iceorb 2 petr joe Note In the second sample we have no free space inside the items, therefore we cannot move the residents between them. Submitted Solution: ``` # written with help of failed tests def searchBest(iType, number, rType, countResidents): global items, equipped best = 0 ret = None for item, params in items.items(): if params[0] == iType: val = int(params[number]) if countResidents: for resid in equipped[item]: if resid[1] == rType: val += int(resid[2]) if val > best: best = val ret = item return ret def printItem(item): global equipped print(item, len(equipped[item]), ' '.join([x[0] for x in equipped[item]])) def searchFor(iType, number, might): global items, equipped, liesIn pSum = [0] for x in might: pSum.append(pSum[-1] + int(x[2])) while len(pSum) < 11: pSum.append(pSum[-1]) bestVal = 0 for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val > bestVal: bestVal = val for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val == bestVal: for i in range(min(int(params[4]), len(might))): want = might[i] equipped[liesIn[want[0]]].remove(want) liesIn[want[0]] = item if len(equipped[item]) == int(params[4]): rm = equipped[item][0] liesIn[rm[0]] = want[3] equipped[want[3]].append(rm) equipped[item].remove(rm) equipped[item].append(want) printItem(item) return None n = int(input()) items = dict() equipped = dict() for i in range(n): t = tuple(input().split()) items[t[0]] = t[1:] equipped[t[0]] = [] k = int(input()) residents = [None for i in range(k)] glads = dict() liesIn = dict() for i in range(k): residents[i] = tuple(input().split()) equipped[residents[i][3]] = equipped.get(residents[i][3], []) + [residents[i]] liesIn[residents[i][0]] = residents[i][3] canSwap = False for name, val in equipped.items(): if len(val) < int(items[name][4]): canSwap = True if canSwap: glads = sorted([x for x in residents if x[1] == 'gladiator'], key = lambda x: -int(x[2])) sentries = sorted([x for x in residents if x[1] == 'sentry'], key = lambda x: -int(x[2])) phys = sorted([x for x in residents if x[1] == 'physician'], key = lambda x: -int(x[2])) searchFor('weapon', 1, glads) searchFor('armor', 2, sentries) searchFor('orb', 3, phys) else: printItem(searchBest('weapon', 1, 'gladiator', True)) printItem(searchBest('armor', 2, 'sentry', True)) printItem(searchBest('orb', 3, 'physician', True)) ```
instruction
0
72,513
2
145,026
No
output
1
72,513
2
145,027
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside — any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get. Input The first line contains number n (3 ≤ n ≤ 100) — representing how many items Laharl has. Then follow n lines. Each line contains description of an item. The description has the following form: "name class atk def res size" — the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. * name and class are strings and atk, def, res and size are integers. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * class can be "weapon", "armor" or "orb". * 0 ≤ atk, def, res ≤ 1000. * 1 ≤ size ≤ 10. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer k (1 ≤ k ≤ 1000) — the number of residents. Then k lines follow. Each of them describes a resident. A resident description looks like: "name type bonus home" — the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. * name, type and home are strings and bonus is an integer. * name consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. * type may be "gladiator", "sentry" or "physician". * 1 ≤ bonus ≤ 100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces. Output Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them. Examples Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 5 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword Output sword 2 petr mike pagstarmor 1 blackjack iceorb 2 teddy bobby Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 6 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword joe physician 6 iceorb Output longbow 1 mike pagstarmor 1 bobby iceorb 2 petr joe Note In the second sample we have no free space inside the items, therefore we cannot move the residents between them. Submitted Solution: ``` # written with help of failed tests def searchBest(iType, number, rType, countResidents): global items, equipped best = 0 ret = None for item, params in items.items(): if params[0] == iType: val = int(params[number]) if countResidents: for resid in equipped[item]: if resid[1] == rType: val += int(resid[2]) if val > best: best = val ret = item return ret def printItem(item): global equipped print(item, len(equipped[item]), ' '.join([x[0] for x in equipped[item]])) def searchFor(iType, number, might): global items, equipped, liesIn pSum = [0] for x in might: pSum.append(pSum[-1] + int(x[2])) while len(pSum) < 11: pSum.append(pSum[-1]) bestVal = 0 for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val > bestVal: bestVal = val for item, params in items.items(): if params[0] == iType: val = int(params[number]) + pSum[int(params[4])] if val == bestVal: for i in range(min(int(params[4]), len(might))): want = might[i] equipped[liesIn[want[0]]].remove(want) liesIn[want[0]] = item if len(equipped[item]) < int(params[4]): equipped[item].append(want) else: rm = equipped[item][0] liesIn[rm[0]] = want[3] equipped[want[3]].append(rm) equipped[item].remove(rm) equipped[item].append(want) printItem(item) return None n = int(input()) items = dict() equipped = dict() for i in range(n): t = tuple(input().split()) items[t[0]] = t[1:] equipped[t[0]] = [] k = int(input()) residents = [None for i in range(k)] glads = dict() liesIn = dict() for i in range(k): residents[i] = tuple(input().split()) equipped[residents[i][3]] = equipped.get(residents[i][3], []) + [residents[i]] liesIn[residents[i][0]] = residents[i][3] canSwap = False for name, val in equipped.items(): if len(val) < int(items[name][4]): canSwap = True if canSwap: glads = sorted([x for x in residents if x[1] == 'gladiator'], key = lambda x: -int(x[2])) sentries = sorted([x for x in residents if x[1] == 'sentry'], key = lambda x: -int(x[2])) phys = sorted([x for x in residents if x[1] == 'physician'], key = lambda x: -int(x[2])) searchFor('weapon', 1, glads) searchFor('armor', 2, sentries) searchFor('orb', 3, phys) else: printItem(searchBest('weapon', 1, 'gladiator', True)) printItem(searchBest('armor', 2, 'sentry', True)) printItem(searchBest('orb', 3, 'physician', True)) ```
instruction
0
72,514
2
145,028
No
output
1
72,514
2
145,029
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,721
2
145,442
Tags: combinatorics, data structures, sortings 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 -------------------- from bisect import bisect_left import heapq n,k=map(int,input().split());seg=[];mod=998244353 if k==1: print(n) else: for s in range(n): l,r=map(int,input().split()) seg.append((l,r)) fac=[1,1] for s in range(2,n+1): fac.append((fac[-1]*s)%mod) t=pow(fac[-1],mod-2,mod);inv=[t for s in range(n+1)] for s in range(n-1,-1,-1): inv[s]=(inv[s+1]*(s+1))%mod seg.sort(key=lambda x: [x[0],x[1]]) heap=[] for s in range(k-1): heapq.heappush(heap,seg[s][1]) ans=0 for s in range(k-1,n): while len(heap)>0 and heap[0]<seg[s][0]: heapq.heappop(heap) if len(heap)>=k-1: r0=fac[len(heap)];r1=inv[k-1];r2=inv[len(heap)-(k-1)] ans+=((r0*r1)%mod)*r2;ans=ans%mod heapq.heappush(heap,seg[s][1]) print(ans) ```
output
1
72,721
2
145,443
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,722
2
145,444
Tags: combinatorics, data structures, sortings Correct Solution: ``` # Fast IO (be careful about bytestring, not on interactive) import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,k = map(int,input().split()) MOD = 998244353 # modular inverse for positive a and b and nCk mod MOD depending on modinv def egcd(a, b): if a == 0: return (b, 0, 1) else: g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y) def modinv(a, m): g, x, y = egcd(a, m) if g != 1: raise Exception('modular inverse does not exist') else: return x % m combinationList = [] def combination(n,k,MOD): ans = 1 combinationList.append(ans) for i in range(1,n - k + 1): ans *= k + i ans %= MOD ans *= modinv(i,MOD) ans %= MOD combinationList.append(ans) query = [] for i in range(n): l,r = map(int,input().split()) query.append(l * 3 + 1) query.append(r * 3 + 2) query.sort() combination(n,k - 1,MOD) cnt = 0 ans = 0 for elem in query: if elem % 3 == 1: if cnt >= k - 1: ans += combinationList[cnt - k + 1] ans %= MOD cnt += 1 else: cnt -= 1 print(ans % MOD) ```
output
1
72,722
2
145,445
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,723
2
145,446
Tags: combinatorics, data structures, sortings Correct Solution: ``` from bisect import * from collections import * from math import gcd,ceil,sqrt,floor,inf from heapq import * from typing import List from itertools import * from operator import add,mul,sub,xor,truediv,floordiv from functools import * #------------------------------------------------------------------------ import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #------------------------------------------------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) #------------------------------------------------------------------------ farr=[1] ifa=[] def fact(x,mod=0): if mod: while x>=len(farr): farr.append(farr[-1]*len(farr)%mod) else: while x>=len(farr): farr.append(farr[-1]*len(farr)) return farr[x] def ifact(x,mod): global ifa ifa.append(inverse(farr[-1],mod)) for i in range(x,0,-1): ifa.append(ifa[-1]*i%mod) ifa=ifa[::-1] def per(i,j,mod=0): if i<j: return 0 if not mod: return fact(i)//fact(i-j) return farr[i]*ifa[i-j]%mod def com(i,j,mod=0): if i<j: return 0 if not mod: return per(i,j)//fact(j) return per(i,j,mod)*ifa[j]%mod def catalan(n): return com(2*n,n)//(n+1) def linc(f,t,l,r): while l<r: mid=(l+r)//2 if t>f(mid): l=mid+1 else: r=mid return l def rinc(f,t,l,r): while l<r: mid=(l+r+1)//2 if t<f(mid): r=mid-1 else: l=mid return l def ldec(f,t,l,r): while l<r: mid=(l+r)//2 if t<f(mid): l=mid+1 else: r=mid return l def rdec(f,t,l,r): while l<r: mid=(l+r+1)//2 if t>f(mid): r=mid-1 else: l=mid return l def isprime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def binfun(x): c=0 for w in arr: c+=ceil(w/x) return c def lowbit(n): return n&-n def inverse(a,m): a%=m if a<=1: return a return ((1-inverse(m,a)*m)//a)%m class BIT: def __init__(self,arr): self.arr=arr self.n=len(arr)-1 def update(self,x,v): while x<=self.n: self.arr[x]+=v x+=x&-x def query(self,x): ans=0 while x: ans+=self.arr[x] x&=x-1 return ans class smt: def __init__(self,l,r,arr): self.l=l self.r=r self.value=(1<<31)-1 if l<r else arr[l] mid=(l+r)//2 if(l<r): self.left=smt(l,mid,arr) self.right=smt(mid+1,r,arr) self.value&=self.left.value&self.right.value #print(l,r,self.value) def setvalue(self,x,val): if(self.l==self.r): self.value=val return mid=(self.l+self.r)//2 if(x<=mid): self.left.setvalue(x,val) else: self.right.setvalue(x,val) self.value=self.left.value&self.right.value def ask(self,l,r): if(l<=self.l and r>=self.r): return self.value val=(1<<31)-1 mid=(self.l+self.r)//2 if(l<=mid): val&=self.left.ask(l,r) if(r>mid): val&=self.right.ask(l,r) return val class DSU:#容量+路径压缩 def __init__(self,n): self.c=[-1]*n def same(self,x,y): return self.find(x)==self.find(y) def find(self,x): if self.c[x]<0: return x self.c[x]=self.find(self.c[x]) return self.c[x] def union(self,u,v): u,v=self.find(u),self.find(v) if u==v: return False if self.c[u]<self.c[v]: u,v=v,u self.c[u]+=self.c[v] self.c[v]=u return True def size(self,x): return -self.c[self.find(x)] class UFS:#秩+路径 def __init__(self,n): self.parent=[i for i in range(n)] self.ranks=[0]*n def find(self,x): if x!=self.parent[x]: self.parent[x]=self.find(self.parent[x]) return self.parent[x] def union(self,u,v): pu,pv=self.find(u),self.find(v) if pu==pv: return False if self.ranks[pu]>=self.ranks[pv]: self.parent[pv]=pu if self.ranks[pv]==self.ranks[pu]: self.ranks[pu]+=1 else: self.parent[pu]=pv def Prime(n): c=0 prime=[] flag=[0]*(n+1) for i in range(2,n+1): if not flag[i]: prime.append(i) c+=1 for j in range(c): if i*prime[j]>n: break flag[i*prime[j]]=prime[j] if i%prime[j]==0: break return prime def dij(s,graph): d={} d[s]=0 heap=[(0,s)] seen=set() while heap: dis,u=heappop(heap) if u in seen: continue for v in graph[u]: if v not in d or d[v]>d[u]+graph[u][v]: d[v]=d[u]+graph[u][v] heappush(heap,(d[v],v)) return d ''' import time s=time.time() for i in range(2000): print(0) e=time.time() print(e-s) ''' t=1 for i in range(t): n,k=RL() mod=998244353 t=set() e=Counter() s=Counter() for i in range(n): l,r=RLL() e[r]+=1 s[l]+=1 t.add(l) t.add(r) t=sorted(t) ans=0 cur=0 fact(n,mod) ifact(n,mod) #print(farr,ifa) for i in t: cur+=s[i] ans=(ans+com(cur,k,mod)-com(cur-e[i],k,mod))%mod cur-=e[i] #print(i,cur,ans) print(ans) ```
output
1
72,723
2
145,447
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,724
2
145,448
Tags: combinatorics, data structures, sortings Correct Solution: ``` import sys input = iter(sys.stdin.read().splitlines()).__next__ MOD = 998244353 n, k = map(int, input().split()) a = [tuple(map(int, input().split())) for _ in range(n)] fac = [1] * (n + 1) for i in range(2, n + 1): fac[i] = fac[i - 1] * i % MOD ifac = [1] * (n + 1) ifac[n] = pow(fac[n], MOD - 2, MOD) for i in range(n - 1, 1, -1): ifac[i] = ifac[i + 1] * (i + 1) % MOD comb = lambda n, k: fac[n] * ifac[k] % MOD * ifac[n - k] % MOD if 0 <= k <= n else 0 e = [s * 2 for s, _ in a] + [e * 2 + 1 for _, e in a] e.sort() cur = ways = 0 for t in e: if t % 2: cur -= 1 else: ways += comb(cur, k - 1) cur += 1 print(ways % MOD) ```
output
1
72,724
2
145,449
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,725
2
145,450
Tags: combinatorics, data structures, sortings Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase from collections import defaultdict mod = 998244353 fac = [1] for i in range(1,300001): fac.append((fac[-1]*i)%mod) fac_in = [pow(fac[-1],mod-2,mod)] for i in range(300000,0,-1): fac_in.append((fac_in[-1]*i)%mod) fac_in.reverse() def comb(a,k): if a < k: return 0 return (fac[a]*fac_in[k]*fac_in[a-k])%mod def main(): n,k = map(int,input().split()) mom = defaultdict(list) se = set() for j in range(1,n+1): l,r = map(int,input().split()) se.add(l) se.add(r) mom[l].append(j) mom[r].append(-j) on = 0 ans = 0 for x in sorted(se): for r in sorted(mom[x],reverse=1): if r > 0: on += 1 else: on -= 1 ans += comb(on,k-1) ans %= mod print(ans) #Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
output
1
72,725
2
145,451
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,726
2
145,452
Tags: combinatorics, data structures, sortings Correct Solution: ``` from functools import lru_cache from sys import stdin, stdout import sys from math import * # from collections import deque # sys.setrecursionlimit(int(2e5+10)) input = stdin.readline # print = stdout.write # dp=[-1]*100000 N = 400000 factorialNumInverse = [None] * (N + 1) naturalNumInverse = [None] * (N + 1) fact = [None] * (N + 1) def InverseofNumber(p): naturalNumInverse[0] = naturalNumInverse[1] = 1 for i in range(2, N + 1, 1): naturalNumInverse[i] = (naturalNumInverse[p % i] * (p - int(p / i)) % p) def InverseofFactorial(p): factorialNumInverse[0] = factorialNumInverse[1] = 1 for i in range(2, N + 1, 1): factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p def factorial(p): fact[0] = 1 for i in range(1, N + 1): fact[i] = (fact[i - 1] * i) % p def Binomial(N, R, p): ans = ((fact[N] * factorialNumInverse[R])% p * factorialNumInverse[N - R])% p return ans n,k=map(int,input().split()) s={} e={} a=[] for i in range(n): x,y=map(int,input().split()) if(s.get(x)==None): s[x]=0 a.append(x) s[x]+=1 if(e.get(y)==None): e[y]=0 a.append(y) e[y]+=1 a=list(set(a)) a.sort() t=0 ans=0 mod=998244353 InverseofNumber(mod) InverseofFactorial(mod) factorial(mod) for i in range(len(a)): x=0 if(s.get(a[i])!=None): t+=s[a[i]] x=s[a[i]] if(x!=0 and t>=k): temp=t for j in range(x): temp-=1 if(temp>=k-1): ans=(ans+Binomial(temp,k-1,mod))%mod if(e.get(a[i])!=None): t-=e[a[i]] print(ans%mod) # print(Binomial(100000,50000,mod)) ```
output
1
72,726
2
145,453
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,727
2
145,454
Tags: combinatorics, data structures, 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") ####################################### n,k=map(int,input().split()) l=[] for i in range(n): a,b=map(int,input().split()) l.append(2*a) l.append(2*b+1) f=[1]*(300001) fi=[1]*(300001) a=1 m=998244353 for i in range(1,300001): a*=i a%=m f[i]=a fi[-1]=pow(f[-1],m-2,m) for i in range(300000-1,0,-1): fi[i]=fi[i+1]*(i+1) fi[i]%=m l.sort() c=0 ans=0 for i in l: if i%2: c-=1 else: if c>=k-1: ans+=(f[c]*fi[k-1]*fi[c-k+1])%m ans%=m c+=1 print(ans) ```
output
1
72,727
2
145,455
Provide tags and a correct Python 3 solution for this coding contest problem. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are n lamps with Spirit Tree's light. Sein knows the time of turning on and off for the i-th lamp — l_i and r_i respectively. To open the door you have to choose k lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the k lamps to pick, Ori is interested: how many ways there are to pick such k lamps that the door will open? It may happen that Sein may be wrong and there are no such k lamps. The answer might be large, so print it modulo 998 244 353. Input First line contains two integers n and k (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — total number of lamps and the number of lamps that must be turned on simultaneously. Next n lines contain two integers l_i ans r_i (1 ≤ l_i ≤ r_i ≤ 10^9) — period of time when i-th lamp is turned on. Output Print one integer — the answer to the task modulo 998 244 353. Examples Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 Note In first test case there are nine sets of k lamps: (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 6), (1, 4, 6), (2, 3, 6), (2, 4, 6), (2, 6, 7). In second test case k=1, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time 3, so the answer is 1. In fifth test case there are seven sets of k lamps: (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5).
instruction
0
72,728
2
145,456
Tags: combinatorics, data structures, sortings Correct Solution: ``` import sys import math import bisect from sys import stdin, stdout ip = lambda: int(stdin.readline()) inp = lambda: map(int, stdin.readline().split()) ips = lambda: stdin.readline().rstrip() out = lambda x: stdout.write(str(x) + "\n") def qpow(a, b, m): ans = 1 while b: if b & 1: ans *= a ans %= m a *= a a %= m b >>= 1 return ans mod = 998244353 n, k = inp() C = [0] * (n + 1) C[k] = 1 for i in range(k + 1, n + 1): C[i] = C[i - 1] * (i - 1) % mod * qpow(i - k, mod - 2, mod) % mod op = [] for _ in range(n): l, r = inp() op.append(2 * l) op.append(2 * r + 1) op.sort() now = 0 ans = 0 for opt in op: if opt & 1 == 0: now += 1 if now >= k: ans += C[now] ans %= mod else: now -= 1 print(ans) ```
output
1
72,728
2
145,457