problem_id
stringlengths
3
7
contestId
stringclasses
660 values
problem_index
stringclasses
27 values
programmingLanguage
stringclasses
3 values
testset
stringclasses
5 values
incorrect_passedTestCount
float64
0
146
incorrect_timeConsumedMillis
float64
15
4.26k
incorrect_memoryConsumedBytes
float64
0
271M
incorrect_submission_id
stringlengths
7
9
incorrect_source
stringlengths
10
27.7k
correct_passedTestCount
float64
2
360
correct_timeConsumedMillis
int64
30
8.06k
correct_memoryConsumedBytes
int64
0
475M
correct_submission_id
stringlengths
7
9
correct_source
stringlengths
28
21.2k
contest_name
stringclasses
664 values
contest_type
stringclasses
3 values
contest_start_year
int64
2.01k
2.02k
time_limit
float64
0.5
15
memory_limit
float64
64
1.02k
title
stringlengths
2
54
description
stringlengths
35
3.16k
input_format
stringlengths
67
1.76k
output_format
stringlengths
18
1.06k
interaction_format
null
note
stringclasses
840 values
examples
stringlengths
34
1.16k
rating
int64
800
3.4k
tags
stringclasses
533 values
testset_size
int64
2
360
official_tests
stringlengths
44
19.7M
official_tests_complete
bool
1 class
input_mode
stringclasses
1 value
generated_checker
stringclasses
231 values
executable
bool
1 class
347/B
347
B
Python 3
TESTS
17
310
6,758,400
39214212
n=int(input()) a=[int(num) for num in input().split()] max=0 flag=0 for i in range(0,n): if a[i]==i: max=max+1 elif a[a[i]]==i: temp=a[i] a[i]=a[temp] a[temp]=temp max=max+1 flag=1 elif flag!=1: flag=2 if flag==2: max=max+1 print(max)
19
124
7,782,400
4929723
n=int(input()) a=list(map(int,input().split())) b=0; c=0; for i,j in enumerate(a): if i==j: b+=1 elif a[j]==i: c=2 elif c==0: c=1 print(b+c)
Codeforces Round 201 (Div. 2)
CF
2,013
2
256
Fixed Points
A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not. A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0, a1, ..., an - 1 if and only if ai = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points. You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.
The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.
Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.
null
null
[{"input": "5\n0 1 3 4 2", "output": "3"}]
1,100
["brute force", "implementation", "math"]
19
[{"input": "5\r\n0 1 3 4 2\r\n", "output": "3\r\n"}, {"input": "10\r\n6 9 4 7 8 2 3 5 0 1\r\n", "output": "2\r\n"}, {"input": "100\r\n99 5 40 32 4 31 38 57 94 47 26 16 89 72 9 80 55 86 78 90 42 41 46 74 56 97 21 48 66 27 93 85 88 59 64 95 10 45 12 22 84 60 8 98 62 51 14 65 39 30 11 71 92 19 76 43 87 54 15 53 37 6 25 18 96 35 13 91 2 3 0 23 1 7 49 75 81 33 50 52 63 44 69 36 17 61 24 20 68 34 73 29 70 83 58 79 82 28 77 67\r\n", "output": "3\r\n"}, {"input": "3\r\n0 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1 0\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 0\r\n", "output": "1\r\n"}, {"input": "1\r\n0\r\n", "output": "1\r\n"}, {"input": "5\r\n0 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "4\r\n"}, {"input": "7\r\n0 1 2 4 3 6 5\r\n", "output": "5\r\n"}, {"input": "6\r\n0 1 2 3 5 4\r\n", "output": "6\r\n"}]
false
stdio
null
true
347/B
347
B
Python 3
TESTS
17
436
16,588,800
89457733
n = int(input()) lst = list(map(int,input().split(' '))) maxed = 0 c = 0 maps = {} for i in range(len(lst)): maps[lst[i]] = i for i in range(len(lst)): if lst[i] == i: continue if lst[i] == maps[i]: temp = lst[i] lst[i] = i lst[maps[i]] = temp c+=1 elif c == 0 and i == len(lst)-1: temp = lst[i] lst[i] = i for i in range(len(lst)): if i == lst[i]: maxed+=1 print(maxed)
19
154
13,107,200
183383962
n = int(input()) a = list(map(int, input().split())) cnt = 0 for i in range(len(a)): if i == a[i]: cnt +=1 ok2 = False ok1 = False for i in range(len(a)): if (i == a[i]): continue ok1 = True if (i == a[a[i]]): ok2 = True if ok2 == True: cnt+= 2 elif ok1 == True: cnt+= 1 print(cnt)
Codeforces Round 201 (Div. 2)
CF
2,013
2
256
Fixed Points
A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not. A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0, a1, ..., an - 1 if and only if ai = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points. You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.
The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.
Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.
null
null
[{"input": "5\n0 1 3 4 2", "output": "3"}]
1,100
["brute force", "implementation", "math"]
19
[{"input": "5\r\n0 1 3 4 2\r\n", "output": "3\r\n"}, {"input": "10\r\n6 9 4 7 8 2 3 5 0 1\r\n", "output": "2\r\n"}, {"input": "100\r\n99 5 40 32 4 31 38 57 94 47 26 16 89 72 9 80 55 86 78 90 42 41 46 74 56 97 21 48 66 27 93 85 88 59 64 95 10 45 12 22 84 60 8 98 62 51 14 65 39 30 11 71 92 19 76 43 87 54 15 53 37 6 25 18 96 35 13 91 2 3 0 23 1 7 49 75 81 33 50 52 63 44 69 36 17 61 24 20 68 34 73 29 70 83 58 79 82 28 77 67\r\n", "output": "3\r\n"}, {"input": "3\r\n0 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1 0\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 0\r\n", "output": "1\r\n"}, {"input": "1\r\n0\r\n", "output": "1\r\n"}, {"input": "5\r\n0 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "4\r\n"}, {"input": "7\r\n0 1 2 4 3 6 5\r\n", "output": "5\r\n"}, {"input": "6\r\n0 1 2 3 5 4\r\n", "output": "6\r\n"}]
false
stdio
null
true
780/D
780
D
Python 3
PRETESTS
3
46
4,915,200
25241863
n = int(input()) blocked = {} blocked_name = {} was = {} for i in range(n): arr = input().split() name, city = arr[0], arr[1] first = name[:3] second = name[:2] + '_' + city[:1] try: v = blocked[first] v1 = blocked[first[:2] + '_' + first[2:]] try: vv = blocked[second] print('NO') exit(0) except KeyError: blocked[first] = i blocked[second] = i except KeyError: try: v1 = blocked[first[:2] + '_' + first[2:]] try: vv = blocked[second] print('NO') exit(0) except KeyError: blocked[second] = i except KeyError: blocked[first] = i blocked[second] = i values = set() values_ok = [] for i in range(n): for val in blocked: if blocked[val] == i: if '_' in val: values.add(''.join(map(str, val.split('_')))) values_ok.append(''.join(map(str, val.split('_')))) else: values.add(val) values_ok.append(val) break if len(values) == n: print('YES') for i in range(n): print(values_ok[i]) else: print('NO')
41
124
409,600
49508210
n = int(input()) ans = 0 o = [] p = [] for i in range(n): s, g = [str(j) for j in input().split()] ss = s[0:3] gg = s[0:2] + g[0] flag = True if ss in o: flag = False o.append(ss) if gg in p and ss not in p and flag: p.append(ss) elif gg not in p: p.append(gg) else: ans = -1 if ans < 0: print('NO') else: print('YES') for i in p: print(i)
Технокубок 2017 - Финал (только для онсайт-финалистов)
CF
2,017
2
256
Innokenty and a Football League
Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters. Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that: 1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN", 2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB". Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name. Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of clubs in the league. Each of the next n lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.
It it is not possible to choose short names and satisfy all constraints, print a single line "NO". Otherwise, in the first line print "YES". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input. If there are multiple answers, print any of them.
null
In the first sample Innokenty can choose first option for both clubs. In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs. In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club. In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different.
[{"input": "2\nDINAMO BYTECITY\nFOOTBALL MOSCOW", "output": "YES\nDIN\nFOO"}, {"input": "2\nDINAMO BYTECITY\nDINAMO BITECITY", "output": "NO"}, {"input": "3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP", "output": "YES\nPLM\nPLS\nGOG"}, {"input": "3\nABC DEF\nABC EFG\nABD OOO", "output": "YES\nABD\nABE\nABO"}]
1,900
["2-sat", "graphs", "greedy", "implementation", "shortest paths", "strings"]
41
[{"input": "2\r\nDINAMO BYTECITY\r\nFOOTBALL MOSCOW\r\n", "output": "YES\r\nDIN\r\nFOO\r\n"}, {"input": "2\r\nDINAMO BYTECITY\r\nDINAMO BITECITY\r\n", "output": "NO\r\n"}, {"input": "3\r\nPLAYFOOTBALL MOSCOW\r\nPLAYVOLLEYBALL SPB\r\nGOGO TECHNOCUP\r\n", "output": "YES\r\nPLM\r\nPLS\r\nGOG\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABD OOO\r\n", "output": "YES\r\nABD\r\nABE\r\nABO\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABC EEEEE\r\n", "output": "NO\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABD CABA\r\n", "output": "YES\r\nABD\r\nABE\r\nABC\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABD EABA\r\n", "output": "NO\r\n"}, {"input": "1\r\nAAA AAA\r\n", "output": "YES\r\nAAA\r\n"}, {"input": "1\r\nAAAAAAAAAAAAAAAAAAAA ZZZZZZZZZZZZZZZZZZZZ\r\n", "output": "YES\r\nAAA\r\n"}, {"input": "5\r\nADAC BABC\r\nABB DCB\r\nABB BCDC\r\nDBAC BAC\r\nDBBC DBC\r\n", "output": "YES\r\nADA\r\nABD\r\nABB\r\nDBA\r\nDBB\r\n"}, {"input": "5\r\nIAH HJIE\r\nIAH FJK\r\nIAH BIAA\r\nIAH AFG\r\nIAH DEFF\r\n", "output": "YES\r\nIAH\r\nIAF\r\nIAB\r\nIAA\r\nIAD\r\n"}, {"input": "10\r\nIJGDI KHB\r\nHBI CKKCG\r\nFHE GCAA\r\nEDCGH HHICE\r\nGFH AIHD\r\nHED KIK\r\nDCK BCFIJ\r\nFFIHE FDB\r\nJGB AKKI\r\nIJD CAG\r\n", "output": "YES\r\nIJG\r\nHBI\r\nFHE\r\nEDC\r\nGFH\r\nHED\r\nDCK\r\nFFI\r\nJGB\r\nIJD\r\n"}, {"input": "10\r\nEDBG IGGAC\r\nEDBG GIKAG\r\nEDBG IKGEI\r\nEDBG AJEG\r\nEDBG HAD\r\nEDBG ACKK\r\nEDBG FEDE\r\nEDBG DAB\r\nEDBG CCJBD\r\nEDBG KKGFB\r\n", "output": "NO\r\n"}, {"input": "10\r\nADE GBH\r\nJJDGJ AAF\r\nJJDGJ BBKG\r\nADE FKH\r\nADE CIA\r\nAIE JCBJ\r\nAIE BBJB\r\nEBAK JDB\r\nJJDGJ IDBG\r\nCEJE FIG\r\n", "output": "YES\r\nADG\r\nJJA\r\nJJB\r\nADF\r\nADC\r\nAIJ\r\nAIB\r\nEBA\r\nJJI\r\nCEJ\r\n"}, {"input": "4\r\nABA DEF\r\nABB DEF\r\nABC DEF\r\nABE DEF\r\n", "output": "YES\r\nABA\r\nABB\r\nABC\r\nABE\r\n"}, {"input": "2\r\nABC CCC\r\nABE CCC\r\n", "output": "YES\r\nABC\r\nABE\r\n"}, {"input": "2\r\nABS SSS\r\nABD SSD\r\n", "output": "YES\r\nABS\r\nABD\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) clubs = [] for _ in range(n): team, hometown = f.readline().strip().split() clubs.append((team, hometown)) ai_list = [] bi_list = [] for team, hometown in clubs: ai = team[:3] bi = team[:2] + hometown[0] ai_list.append(ai) bi_list.append(bi) with open(submission_output_path) as f: lines = [line.strip() for line in f.readlines()] if not lines: print(0) return first_line = lines[0].upper() if first_line == "NO": is_possible = is_solvable(ai_list, bi_list, clubs) print(1 if not is_possible else 0) elif first_line == "YES": if len(lines) != n + 1: print(0) return submission_names = lines[1:] valid = True for i in range(n): if submission_names[i] not in (ai_list[i], bi_list[i]): valid = False break if not valid or len(set(submission_names)) != n: print(0) return forced_ais = set() for i in range(n): if submission_names[i] == bi_list[i]: forced_ais.add(ai_list[i]) for ai in forced_ais: for j in range(n): if ai_list[j] == ai and submission_names[j] != bi_list[j]: print(0) return print(1) else: print(0) def is_solvable(ai_list, bi_list, clubs): n = len(ai_list) forced = [False] * n freq = {} for ai in ai_list: freq[ai] = freq.get(ai, 0) + 1 q = [] for i in range(n): if freq[ai_list[i]] > 1: if not forced[i]: forced[i] = True q.append(i) from collections import deque q = deque(q) while q: i = q.popleft() current_bi = bi_list[i] for j in range(n): if ai_list[j] == current_bi and not forced[j]: forced[j] = True q.append(j) forced_names = [bi_list[i] for i in range(n) if forced[i]] unforced_ais = [ai_list[i] for i in range(n) if not forced[i]] forced_ais_set = {ai_list[i] for i in range(n) if forced[i]} if len(set(forced_names)) != len(forced_names): return False for ai in unforced_ais: if ai in forced_ais_set: return False if len(unforced_ais) != len(set(unforced_ais)): return False return True if __name__ == "__main__": input_path, output_path, submission_output_path = sys.argv[1:4] main(input_path, output_path, submission_output_path)
true
416/B
416
B
Python 3
TESTS
5
561
6,963,200
38436552
#416B arr = list(map(int, input().split(" "))) m = arr[0] n = arr[1] time = [] timesum = [0]*m for i in range(m): if n == 1: arr = list(map(int, input().split(" "))) time.append([0, arr[0]]) else: arr = list(map(int, input().split(" "))) a = 0 for i in range(n-1): a += arr[i] time.append([a, arr[n-1]]) timesum[0] = time[0][0] for i in range(1, m): timesum[i] = timesum[i-1] + time[i][0] lastpainter = [0] * m lastpainter[0] = time[0][0] + time[0][1] print(lastpainter[0], end = " ") for i in range(1, m): k = max(lastpainter[i-1], timesum[i]) lastpainter[i] = k + time[i][1] print(lastpainter[i], end = " ")
26
327
12,185,600
69259619
def arr_inp(): return [int(x) for x in stdin.readline().split()] def get_col(arr, i): return [row[i] for row in arr] def arr_sum(ar): ar.insert(0, 0) return list(accumulate(ar, lambda x, y: x + y)) from itertools import accumulate from sys import stdin m, n = arr_inp() arr = [arr_inp() for i in range(m)] ans = arr_sum(get_col(arr, 0))[1:] if m == 1: print(sum(arr[0])) elif n == 1: print(*ans) else: for i in range(1, n): free = 0 for j in range(m): ans[j] = max(free, ans[j]) + arr[j][i] free = ans[j] print(*ans)
Codeforces Round 241 (Div. 2)
CF
2,014
1
256
Art Union
A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows. Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter tij units of time. Order is important everywhere, so the painters' work is ordered by the following rules: - Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n); - each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on; - each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest; - as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter. Given that the painters start working at time 0, find for each picture the time when it is ready for sale.
The first line of the input contains integers m, n (1 ≤ m ≤ 50000, 1 ≤ n ≤ 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers ti1, ti2, ..., tin (1 ≤ tij ≤ 1000), where tij is the time the j-th painter needs to work on the i-th picture.
Print the sequence of m integers r1, r2, ..., rm, where ri is the moment when the n-th painter stopped working on the i-th picture.
null
null
[{"input": "5 1\n1\n2\n3\n4\n5", "output": "1 3 6 10 15"}, {"input": "4 2\n2 5\n3 1\n5 3\n10 1", "output": "7 8 13 21"}]
1,300
["brute force", "dp", "implementation"]
26
[{"input": "5 1\r\n1\r\n2\r\n3\r\n4\r\n5\r\n", "output": "1 3 6 10 15 "}, {"input": "4 2\r\n2 5\r\n3 1\r\n5 3\r\n10 1\r\n", "output": "7 8 13 21 "}, {"input": "1 1\r\n66\r\n", "output": "66 "}, {"input": "2 2\r\n1 1\r\n1 1\r\n", "output": "2 3 "}, {"input": "2 2\r\n10 1\r\n10 1\r\n", "output": "11 21 "}, {"input": "1 5\r\n1 95 44 14 35\r\n", "output": "189 "}, {"input": "7 1\r\n80\r\n92\r\n24\r\n88\r\n40\r\n45\r\n7\r\n", "output": "80 172 196 284 324 369 376 "}, {"input": "1 2\r\n51 44\r\n", "output": "95 "}, {"input": "2 1\r\n19\r\n4\r\n", "output": "19 23 "}, {"input": "2 2\r\n1 10\r\n1 1\r\n", "output": "11 12 "}, {"input": "3 3\r\n3 9 4\r\n5 10 8\r\n4 4 7\r\n", "output": "16 30 37 "}, {"input": "10 3\r\n6 10 3\r\n2 7 9\r\n10 4 7\r\n6 3 4\r\n6 2 6\r\n8 4 4\r\n5 9 8\r\n6 9 7\r\n2 7 10\r\n2 6 2\r\n", "output": "19 32 39 43 49 53 61 68 78 80 "}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
10
154
0
193216835
n = int(input()) pairs = [] for i in range(n): pairs.append(tuple(map(int, input().split(" ")))) pairs.sort(key=lambda x: 1000 * x[0] + x[1]) pairsY = sorted(pairs, key=lambda y: 1000 * y[1] + y[0]) validX = set() validY = set() for i in range(1, n-1): if(pairs[i-1][0] == pairs[i][0] and pairs[i+1][0] == pairs[i][0]): validX.add(pairs[i]) if(pairsY[i-1][1] == pairsY[i][1] and pairsY[i+1][1] == pairsY[i][1]): validY.add(pairsY[i]) print(len(validX.intersection(validY)))
26
62
0
172527676
dx={} dy={} pts=[] n=int(input()) for i in range(n): pt=input().split() x=int(pt[0]) y=int(pt[1]) pts.append([x,y]) if x in dx: dx[x].append(y) else: dx[x] = [y] if y in dy: dy[y].append(x) else: dy[y] = [x] res=0 for x,y in pts: if x <max(dy[y]) and x >min(dy[y]) and y<max(dx[x]) and y>min(dx[x]): res+=1 print(res)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
7
122
0
116209539
n=int(input()) x,y=[],[] for i in range(n): a,b=map(int,input().split()) x.append(a) y.append(b) a=0 for i in range(n): # for k in range(n): newy=[y[k] for k in range(n) if x[k]==x[i] and k!=i] if len(newy)!=0: if y[i]<max(newy) and y[i]>min(newy) : # print(x[i],y[i]) for j in range(i,-1,-1): # for s in range (i,-1,-1): newx=[x[s] for s in range(n) if y[s]==y[j] and s!=j] if len(newx)!=0: if x[j]<max(newx) and x[j]>min(newx) : a+=1 break else: break print(a)
26
62
0
200942614
n = int(input()) dx,dy,points = {},{},[] for i in range(n): a,b=map(int,input().split()) points.append((a,b)) if a not in dx: dx[a] = [b] else: dx[a].append(b) if b not in dy: dy[b] = [a] else: dy[b].append(a) # print(dx,dy) num=0 for pt in points: if pt[1] > min(dx[pt[0]]) and pt[1] < max(dx[pt[0]]): if pt[0] > min(dy[pt[1]]) and pt[0] < max(dy[pt[1]]): num+=1 print(num)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
169/A
169
A
Python 3
TESTS
7
109
0
43412425
a=list(map(int, input().split())) b=list(map(int, input().split())) if a[1]<=a[2]: h=sorted(b,reverse=True) else: h=sorted(b,reverse=False) t=len(h)//2 print (abs(h[t-1]-h[t]))
29
46
0
144335879
i=lambda:map(int,input().split()) *_,b=i() c=sorted(i()) print(c[b]-c[b-1])
VK Cup 2012 Round 2 (Unofficial Div. 2 Edition)
CF
2,012
2
256
Chores
Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter — its complexity. The complexity of the i-th chore equals hi. As Petya is older, he wants to take the chores with complexity larger than some value x (hi > x) to leave to Vasya the chores with complexity less than or equal to x (hi ≤ x). The brothers have already decided that Petya will do exactly a chores and Vasya will do exactly b chores (a + b = n). In how many ways can they choose an integer x so that Petya got exactly a chores and Vasya got exactly b chores?
The first input line contains three integers n, a and b (2 ≤ n ≤ 2000; a, b ≥ 1; a + b = n) — the total number of chores, the number of Petya's chores and the number of Vasya's chores. The next line contains a sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 109), hi is the complexity of the i-th chore. The numbers in the given sequence are not necessarily different. All numbers on the lines are separated by single spaces.
Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.
null
In the first sample the possible values of x are 3, 4 or 5. In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4.
[{"input": "5 2 3\n6 2 3 100 1", "output": "3"}, {"input": "7 3 4\n1 1 9 1 1 1 1", "output": "0"}]
800
["sortings"]
29
[{"input": "5 2 3\r\n6 2 3 100 1\r\n", "output": "3\r\n"}, {"input": "7 3 4\r\n1 1 9 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n10 2\r\n", "output": "8\r\n"}, {"input": "2 1 1\r\n7 7\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n1 1000000000\r\n", "output": "999999999\r\n"}, {"input": "3 1 2\r\n6 5 5\r\n", "output": "1\r\n"}, {"input": "3 2 1\r\n10 10 8\r\n", "output": "2\r\n"}, {"input": "8 3 5\r\n42 55 61 72 83 10 22 33\r\n", "output": "6\r\n"}, {"input": "10 5 5\r\n1 2 3 4 5 999999999 999999998 999999997 999999996 999999995\r\n", "output": "999999990\r\n"}, {"input": "4 1 3\r\n10 8 7 3\r\n", "output": "2\r\n"}, {"input": "4 2 2\r\n402 10 10 402\r\n", "output": "392\r\n"}, {"input": "4 1 3\r\n10 402 402 10\r\n", "output": "0\r\n"}, {"input": "4 3 1\r\n100 100 200 200\r\n", "output": "0\r\n"}, {"input": "102 101 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}]
false
stdio
null
true
754/D
754
D
PyPy 3-64
TESTS
2
109
8,806,400
175544051
''' https://codeforces.com/problemset/problem/754/D 输入 n, k (1≤k≤n≤3e5) 和 n 个闭区间,区间左右端点在 [-1e9,1e9] 内,区间的编号从 1 开始。 请你选择 k 个区间,使得这 k 个区间的交集的大小尽量大(只考虑整数), 输出这个最大值,以及对应的区间的编号。 思考题:如果改成并集呢? 输入 4 2 1 100 40 70 120 130 125 180 输出 31 1 2 输入 3 2 1 12 15 20 25 30 输出 0 1 2 输入 5 2 1 10 5 15 14 50 30 70 99 100 输出 21 3 4 https://codeforces.com/problemset/submission/754/174797228 Python https://codeforces.com/problemset/submission/754/174840684 提示 1:按照区间左端点排序。 提示 2:遍历区间,考虑把第 i 个区间的左端点当作交集的左端点,那么 i 左边这些区间都是可以选的,贪心来说,选其中第 k 大的右端点当作交集的右端点。 提示 3:用最小堆维护第 k 大的右端点。 提示 4:更新答案时,记录对应的左端点,从而知道并集的左右端点。 最后再次遍历区间,输出 k 个包含并集的区间编号。 ''' from itertools import * from collections import * from math import * from heapq import * from functools import * from bisect import * from random import * import random import sys import os from io import BytesIO, IOBase from copy import deepcopy import threading BUFSIZE = 4096 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 I(): return input() def II(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(int, input().split())) def GMI(): return map(lambda x: int(x) - 1, input().split()) def LGMI(): return list(map(lambda x: int(x) - 1, input().split())) def solve(): n, k = MI() arr = [] for _ in range(n): l, r = MI() arr.append((l, r)) arr.sort() res = 0 # 滑动窗口 h = [r for l, r in arr[:k]] L, R = inf, -inf for i in range(k - 1, n): l, r = arr[i] heappush(h, r) if h[0] - l > R - L: L, R = l, h[0] heappop(h) print(max(0,R-L+1)) res=[] for i in range(n): l,r=arr[i] if l<=L and r>=R: res.append(i+1) if len(res)==k:break print(*res) solve()
77
2,121
61,337,600
174828075
import sys from heapq import heappush,heappop n,m=map(int,input().split()) arr=sorted([*map(int,sys.stdin.readline().split()),i] for i in range(1,n+1)) q,res,idx,poped,areas=[],0,0,[],set() for i in range(n): heappush(q,(arr[i][1],i)) if len(q)<m:continue lr=arr[q[0][1]][1]-arr[i][0]+1 if lr>res: res=lr areas|={arr[j][2] for j in range(idx,i+1)} idx=i+1 areas-=set(poped) poped.clear() poped.append(arr[heappop(q)[1]][2]) print(res) print(*areas) if res>0 else print(*list(range(1,m+1)))
Codeforces Round 390 (Div. 2)
CF
2,017
4
256
Fedor and coupons
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly k coupons with him. Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose. Each of the next n lines contains two integers li and ri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-th coupon. The coupons can be equal.
In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted. In the second line print k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose. If there are multiple answers, print any of them.
null
In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total. In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
[{"input": "4 2\n1 100\n40 70\n120 130\n125 180", "output": "31\n1 2"}, {"input": "3 2\n1 12\n15 20\n25 30", "output": "0\n1 2"}, {"input": "5 2\n1 10\n5 15\n14 50\n30 70\n99 100", "output": "21\n3 4"}]
2,100
["binary search", "data structures", "greedy", "sortings"]
77
[{"input": "4 2\r\n1 100\r\n40 70\r\n120 130\r\n125 180\r\n", "output": "31\r\n1 2 \r\n"}, {"input": "3 2\r\n1 12\r\n15 20\r\n25 30\r\n", "output": "0\r\n1 2 \r\n"}, {"input": "5 2\r\n1 10\r\n5 15\r\n14 50\r\n30 70\r\n99 100\r\n", "output": "21\r\n3 4 \r\n"}, {"input": "7 6\r\n-8 6\r\n7 9\r\n-10 -5\r\n-6 10\r\n-7 -3\r\n5 8\r\n4 10\r\n", "output": "0\r\n1 2 3 4 5 6 \r\n"}, {"input": "9 6\r\n-7 -3\r\n-3 10\r\n-6 1\r\n-1 8\r\n-9 4\r\n-7 -6\r\n-5 -3\r\n-10 -2\r\n3 4\r\n", "output": "1\r\n1 2 3 5 7 8 \r\n"}, {"input": "7 7\r\n9 10\r\n-5 3\r\n-6 2\r\n1 6\r\n-9 6\r\n-10 7\r\n-7 -5\r\n", "output": "0\r\n1 2 3 4 5 6 7 \r\n"}, {"input": "23 2\r\n-629722518 -626148345\r\n739975524 825702590\r\n-360913153 -208398929\r\n76588954 101603025\r\n-723230356 -650106339\r\n-117490984 -101920679\r\n-39187628 -2520915\r\n717852164 720343632\r\n-611281114 -579708833\r\n-141791522 -122348148\r\n605078929 699430996\r\n-873386085 -820238799\r\n-922404067 -873522961\r\n7572046 13337057\r\n975081176 977171682\r\n901338407 964254238\r\n325388219 346712972\r\n505189756 516497863\r\n-425326983 -422098946\r\n520670681 522544433\r\n-410872616 -367919621\r\n359488350 447471156\r\n-566203447 -488202136\r\n", "output": "0\r\n1 2 \r\n"}, {"input": "24 21\r\n240694945 246896662\r\n240694930 246896647\r\n240695065 246896782\r\n240695050 246896767\r\n240695080 246896797\r\n240694960 246896677\r\n240694975 246896692\r\n240694825 246896542\r\n240694900 246896617\r\n240694915 246896632\r\n240694885 246896602\r\n240694855 246896572\r\n240694870 246896587\r\n240694795 246896512\r\n240695095 246896812\r\n240695125 246896842\r\n240695005 246896722\r\n240694990 246896707\r\n240695140 246896857\r\n240695020 246896737\r\n240695035 246896752\r\n240694840 246896557\r\n240694810 246896527\r\n240695110 246896827\r\n", "output": "6201418\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 20 21 22 23 \r\n"}, {"input": "1 1\r\n2 2\r\n", "output": "1\r\n1 \r\n"}, {"input": "1 1\r\n-1000000000 1000000000\r\n", "output": "2000000001\r\n1 \r\n"}, {"input": "2 1\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "1\r\n1 \r\n"}, {"input": "7 3\r\n3 3\r\n-6 -1\r\n6 7\r\n2 8\r\n3 10\r\n-8 0\r\n-3 10\r\n", "output": "6\r\n4 5 7 \r\n"}, {"input": "5 4\r\n4 7\r\n-4 2\r\n-7 -7\r\n-5 -2\r\n-8 -8\r\n", "output": "0\r\n1 2 3 4 \r\n"}, {"input": "7 7\r\n0 7\r\n9 9\r\n-10 -7\r\n5 8\r\n-10 4\r\n-7 0\r\n-3 5\r\n", "output": "0\r\n1 2 3 4 5 6 7 \r\n"}, {"input": "9 2\r\n5 10\r\n-10 -10\r\n0 10\r\n-6 3\r\n-8 7\r\n6 10\r\n-8 1\r\n5 7\r\n2 2\r\n", "output": "10\r\n5 7 \r\n"}, {"input": "9 5\r\n-2 1\r\n-6 9\r\n-7 -2\r\n5 7\r\n-10 -7\r\n-9 -2\r\n1 4\r\n-1 10\r\n4 8\r\n", "output": "0\r\n1 2 3 4 5 \r\n"}, {"input": "54 7\r\n-98 -39\r\n14 60\r\n-23 -5\r\n58 75\r\n14 16\r\n-40 20\r\n-6 10\r\n11 60\r\n-47 54\r\n-71 -17\r\n-48 -25\r\n-87 -46\r\n-10 99\r\n-97 -88\r\n-14 94\r\n-25 29\r\n-96 -92\r\n68 75\r\n-75 2\r\n12 84\r\n-47 3\r\n-88 49\r\n-37 88\r\n-61 -25\r\n36 67\r\n30 54\r\n12 31\r\n-71 60\r\n-18 -15\r\n-61 -47\r\n-51 -41\r\n-67 51\r\n26 37\r\n18 94\r\n-67 52\r\n-16 56\r\n-5 26\r\n27 57\r\n36 91\r\n-61 61\r\n71 86\r\n27 73\r\n-57 -39\r\n54 71\r\n-16 14\r\n-97 81\r\n-32 49\r\n-18 50\r\n-63 93\r\n51 70\r\n8 66\r\n43 45\r\n-2 99\r\n11 98\r\n", "output": "111\r\n22 28 32 35 40 46 49 \r\n"}, {"input": "52 18\r\n-50 54\r\n35 65\r\n67 82\r\n-87 -10\r\n-39 4\r\n-55 -18\r\n-27 90\r\n-42 73\r\n18 43\r\n70 85\r\n-85 -22\r\n-1 60\r\n-89 23\r\n-78 -75\r\n-14 69\r\n-69 50\r\n-93 74\r\n-10 45\r\n-81 -72\r\n-24 86\r\n-89 100\r\n25 70\r\n-65 -61\r\n-45 100\r\n-49 -23\r\n-74 -59\r\n-81 -15\r\n-58 47\r\n-65 -58\r\n-47 16\r\n-22 91\r\n-85 19\r\n-81 77\r\n79 87\r\n-31 88\r\n26 32\r\n11 90\r\n7 46\r\n64 83\r\n-51 -20\r\n-76 44\r\n-22 75\r\n45 84\r\n-98 46\r\n-20 78\r\n-88 -47\r\n-41 65\r\n2 93\r\n-66 69\r\n-73 94\r\n-85 -44\r\n-65 -23\r\n", "output": "67\r\n1 7 8 16 17 20 21 24 28 31 33 35 41 42 44 47 49 50 \r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path) as f: n, k = map(int, f.readline().split()) coupons = [] for _ in range(n): l, r = map(int, f.readline().split()) coupons.append((l, r)) # Read reference output (ans_ref) with open(output_path) as f: ans_ref = int(f.readline().strip()) # Read submission output with open(submission_path) as f: try: ans_sub = int(f.readline().strip()) if ans_sub != ans_ref: print(0) return selected = list(map(int, f.readline().strip().split())) except: print(0) return # Check selected coupons if len(selected) != k: print(0) return seen = set() for p in selected: if p < 1 or p > n or p in seen: print(0) return seen.add(p) # Compute intersection max_l = -float('inf') min_r = float('inf') for idx in selected: l, r = coupons[idx - 1] max_l = max(max_l, l) min_r = min(min_r, r) intersection = max(0, min_r - max_l + 1) print(1 if intersection == ans_ref else 0) if __name__ == "__main__": main()
true
730/G
730
G
Python 3
TESTS
6
62
204,800
21808523
n=int(input()) p=1 L=[] m,M=0,0 for j in range(n): ch=input().split() s,d=int(ch[0]),int(ch[1]) if j==0: m=s M=s+d-1 print(s,d+s-1) L.append([s,s+d-1]) else: B=True for i in L: if i[1]>=s>=i[0] or i[0]<=(s+d-1)<=i[1] or (s<=i[0] and (s+d-1)>=i[1]): B=False break if B: print(s,s+d-1) if s<m: m=s if (s+d-1)>M: M=s+d-1 L.append([s,s+d-1]) else: if p+d-1<m: print(p,p+d-1) L.append([p,p+d-1]) p=p+d else: print(M+1,M+d) L.append([M+1,M+d]) M=M+d
28
140
1,433,600
21712976
#!/usr/bin/env python3 def main(): try: while True: n = int(input()) req = [tuple(map(int, input().split())) for i in range(n)] used = [(req[0][0], req[0][0] + req[0][1])] print(used[0][0], used[0][1] - 1) for start, dur in req[1:]: last = 1 for a, b in used: if last <= start and start + dur <= a: used.append((start, start + dur)) used.sort() print(start, start + dur - 1) break last = b else: if start >= used[-1][1]: used.append((start, start + dur)) used.sort() print(start, start + dur - 1) else: last = 1 for a, b in used: if a - last >= dur: used.append((last, last + dur)) used.sort() break last = b else: used.append((last, last + dur)) # used.sort() print(last, last + dur - 1) except EOFError: pass main()
2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,016
2
512
Car Repair Shop
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came. The i-th request is characterized by two values: si — the day when a client wants to start the repair of his car, di — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows: - If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client. - Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + di - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + di - 1]. It is possible that the day x when repair is scheduled to start will be less than si. Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients. The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≤ si ≤ 109, 1 ≤ di ≤ 5·106), where si is the preferred time to start repairing the i-th car, di is the number of days to repair the i-th car. The requests should be processed in the order they are given in the input.
Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.
null
null
[{"input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7"}, {"input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000"}]
1,600
["implementation"]
28
[{"input": "3\r\n9 2\r\n7 3\r\n2 4\r\n", "output": "9 10\r\n1 3\r\n4 7\r\n"}, {"input": "4\r\n1000000000 1000000\r\n1000000000 1000000\r\n100000000 1000000\r\n1000000000 1000000\r\n", "output": "1000000000 1000999999\r\n1 1000000\r\n100000000 100999999\r\n1000001 2000000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 1\r\n"}, {"input": "1\r\n1000000000 1\r\n", "output": "1000000000 1000000000\r\n"}, {"input": "1\r\n1000000000 5000000\r\n", "output": "1000000000 1004999999\r\n"}, {"input": "5\r\n6 2\r\n10 1\r\n10 2\r\n9 2\r\n5 1\r\n", "output": "6 7\r\n10 10\r\n1 2\r\n3 4\r\n5 5\r\n"}, {"input": "10\r\n1 3\r\n77 8\r\n46 5\r\n83 4\r\n61 7\r\n8 4\r\n54 7\r\n80 7\r\n33 7\r\n13 4\r\n", "output": "1 3\r\n77 84\r\n46 50\r\n4 7\r\n61 67\r\n8 11\r\n54 60\r\n12 18\r\n33 39\r\n19 22\r\n"}, {"input": "10\r\n588 12\r\n560 10\r\n593 14\r\n438 15\r\n761 11\r\n984 6\r\n503 2\r\n855 19\r\n538 2\r\n650 7\r\n", "output": "588 599\r\n560 569\r\n1 14\r\n438 452\r\n761 771\r\n984 989\r\n503 504\r\n855 873\r\n538 539\r\n650 656\r\n"}, {"input": "20\r\n360 26\r\n475 17\r\n826 12\r\n815 23\r\n567 28\r\n897 26\r\n707 20\r\n1000 9\r\n576 5\r\n16 5\r\n714 16\r\n630 17\r\n426 26\r\n406 23\r\n899 25\r\n102 22\r\n896 8\r\n320 27\r\n964 25\r\n932 18\r\n", "output": "360 385\r\n475 491\r\n826 837\r\n1 23\r\n567 594\r\n897 922\r\n707 726\r\n1000 1008\r\n24 28\r\n29 33\r\n34 49\r\n630 646\r\n426 451\r\n50 72\r\n73 97\r\n102 123\r\n124 131\r\n320 346\r\n964 988\r\n932 949\r\n"}, {"input": "30\r\n522692116 84\r\n589719489 488\r\n662495181 961\r\n915956552 470\r\n683572975 271\r\n498400137 480\r\n327010963 181\r\n200704287 367\r\n810826488 54\r\n978100746 208\r\n345455616 986\r\n106372142 876\r\n446972337 42\r\n309349333 200\r\n93462198 543\r\n167946793 318\r\n325598940 427\r\n121873339 459\r\n174934933 598\r\n279521023 655\r\n739750520 3\r\n870850765 192\r\n622303167 400\r\n471234786 63\r\n805952711 18\r\n349834333 857\r\n804873364 302\r\n512746562 39\r\n533285962 561\r\n996718586 494\r\n", "output": "522692116 522692199\n589719489 589719976\n662495181 662496141\n915956552 915957021\n683572975 683573245\n498400137 498400616\n327010963 327011143\n200704287 200704653\n810826488 810826541\n978100746 978100953\n345455616 345456601\n106372142 106373017\n446972337 446972378\n309349333 309349532\n93462198 93462740\n167946793 167947110\n325598940 325599366\n121873339 121873797\n174934933 174935530\n279521023 279521677\n739750520 739750522\n870850765 870850956\n622303167 622303566\n471234786 471234848\n805952711 805952728\n349834333 349835189\n804873364 804873665\n512746562 512746600\n533285962 533286522\n996718586 996719079\n"}, {"input": "2\r\n10 3\r\n9 2\r\n", "output": "10 12\r\n1 2\r\n"}, {"input": "1\r\n1 5000000\r\n", "output": "1 5000000\r\n"}]
false
stdio
null
true
913/G
913
G
Python 3
TESTS
8
1,730
6,041,600
34039412
import sys import math from decimal import * line = lambda: list(int(x) for x in input().split()) def pow(n, k, p): if k == 0: return 1 % p if k % 2 == 1: return pow(n, k - 1, p) * n % p t = pow(n, k / 2, p) return t * t % p; test = int(input()) for i in range(0, test): x = int(input()) x = x * 10 ** 6 x += (2 ** 16 - x % 2 ** 16) % 2 ** 16 if x % 5 == 0: x += 2 ** 16; res = 0 for i in range(1, 16 + 1): while pow(2, res, 5 ** i) != x % 5 ** i: if i == 1: res += 1 else: res += 4 * 5 ** (i - 2) res += 4 * 5 ** 15 print(res)
31
140
7,168,000
199889664
L = 11; L_ = 32 P2 = 2**L_ P5 = 5**(L_//2) P2P5 = P2 * P5 P5_ = 5**L_ P10 = 10**L_ P10_ = 10**(L_-L) T = P5 // 5 * 4 T_ = P5_ // 5 * 4 def gcd_(a, b): if b == 0: return (1, 0) else: (x_, y_) = gcd_(b, a % b) return (y_, x_ - a // b * y_) def inv(a, md): (x_, y_) = gcd_(a, md) if x_ < 0: x_ += md return x_ def init(): a = 2; p = 1; k = T while k: if (k & 1): p = p * a % P5_ a = a * a % P5_ k >>= 1 return (inv(P2, P5) * P2, inv(p // P5, P5)) (X, Y) = init() t = int(input()) while t > 0: t -= 1 n = int(input()) n *= P10_ n = ((n - X + P2P5 - 1) // P2P5 * P2P5 + X) % P5_ print((n // P5 * Y % P5 * T) % T_ + T_)
Hello 2018
CF
2,018
2
256
Power Substring
You are given n positive integers a1, a2, ..., an. For every ai you need to find a positive integer ki such that the decimal notation of 2ki contains the decimal notation of ai as a substring among its last min(100, length(2ki)) digits. Here length(m) is the length of the decimal notation of m. Note that you don't have to minimize ki. The decimal notations in this problem do not contain leading zeros.
The first line contains a single integer n (1 ≤ n ≤ 2 000) — the number of integers ai. Each of the next n lines contains a positive integer ai (1 ≤ ai < 1011).
Print n lines. The i-th of them should contain a positive integer ki such that the last min(100, length(2ki)) digits of 2ki contain the decimal notation of ai as a substring. Integers ki must satisfy 1 ≤ ki ≤ 1050. It can be shown that the answer always exists under the given constraints. If there are multiple answers, print any of them.
null
null
[{"input": "2\n8\n2", "output": "3\n1"}, {"input": "2\n3\n4857", "output": "5\n20"}]
3,200
["math", "number theory"]
31
[{"input": "2\r\n8\r\n2\r\n", "output": "3\r\n1\r\n"}, {"input": "2\r\n3\r\n4857\r\n", "output": "5\r\n20\r\n"}, {"input": "7\r\n1\r\n7\r\n9\r\n5\r\n6\r\n10\r\n4\r\n", "output": "9\r\n17\r\n13\r\n21\r\n4\r\n42\r\n2\r\n"}, {"input": "10\r\n384\r\n179\r\n982\r\n466\r\n646\r\n226\r\n759\r\n798\r\n291\r\n852\r\n", "output": "14\r\n493\r\n230\r\n150\r\n66\r\n2050\r\n762\r\n454\r\n129\r\n187\r\n"}, {"input": "10\r\n677\r\n958\r\n20\r\n169\r\n441\r\n752\r\n24\r\n809\r\n41\r\n96\r\n", "output": "869\r\n394\r\n43\r\n372\r\n44\r\n101\r\n10\r\n412\r\n44\r\n12\r\n"}, {"input": "10\r\n31\r\n362\r\n396\r\n661\r\n314\r\n803\r\n366\r\n124\r\n748\r\n875\r\n", "output": "49\r\n390\r\n55\r\n289\r\n770\r\n405\r\n446\r\n251\r\n359\r\n501\r\n"}, {"input": "50\r\n415546\r\n6493493\r\n5136540\r\n6965643\r\n8466744\r\n3329383\r\n9711334\r\n9543203\r\n4478668\r\n375580\r\n1919986\r\n1906588\r\n2651062\r\n6256271\r\n1372732\r\n5775823\r\n7301593\r\n4894751\r\n189902\r\n5406338\r\n8281625\r\n3220812\r\n7228487\r\n965119\r\n6142296\r\n3854700\r\n685373\r\n9076805\r\n5770856\r\n9690466\r\n6965459\r\n6120825\r\n2706148\r\n7123642\r\n8323362\r\n4901105\r\n4138289\r\n5897583\r\n6454782\r\n1044857\r\n9693372\r\n9648933\r\n672868\r\n6725325\r\n5179595\r\n3710846\r\n4844826\r\n5742620\r\n1917887\r\n2746786\r\n", "output": "99999\r\n3787651\r\n2509597\r\n3516786\r\n96025\r\n6352070\r\n126398\r\n921114\r\n85899\r\n182804\r\n1075899\r\n4859997\r\n6250277\r\n71876\r\n533344\r\n6651457\r\n25579\r\n6121058\r\n1161796\r\n2314211\r\n111608\r\n21607\r\n74715\r\n787759\r\n300906\r\n2682678\r\n173998\r\n1416485\r\n6607691\r\n208322\r\n5388255\r\n2425068\r\n4583381\r\n2514335\r\n7727484\r\n6713746\r\n655121\r\n1441264\r\n2956660\r\n2630768\r\n657344\r\n4219039\r\n1536881\r\n2699630\r\n1313340\r\n1775060\r\n3852150\r\n3643597\r\n4979359\r\n1055884\r\n"}, {"input": "1\r\n94109029405\r\n", "output": "114306868781\r\n"}, {"input": "100\r\n822\r\n991\r\n907\r\n729\r\n120\r\n707\r\n197\r\n280\r\n444\r\n45\r\n582\r\n951\r\n338\r\n740\r\n502\r\n345\r\n969\r\n240\r\n710\r\n476\r\n530\r\n674\r\n327\r\n187\r\n890\r\n530\r\n626\r\n767\r\n178\r\n831\r\n819\r\n406\r\n413\r\n553\r\n855\r\n17\r\n17\r\n460\r\n708\r\n30\r\n364\r\n974\r\n32\r\n615\r\n231\r\n758\r\n39\r\n731\r\n288\r\n955\r\n897\r\n897\r\n365\r\n246\r\n209\r\n17\r\n836\r\n496\r\n916\r\n610\r\n961\r\n181\r\n619\r\n256\r\n965\r\n697\r\n480\r\n62\r\n876\r\n522\r\n467\r\n706\r\n271\r\n114\r\n996\r\n724\r\n415\r\n434\r\n525\r\n297\r\n156\r\n281\r\n342\r\n44\r\n884\r\n335\r\n192\r\n397\r\n957\r\n483\r\n688\r\n564\r\n612\r\n634\r\n39\r\n762\r\n825\r\n342\r\n704\r\n413\r\n", "output": "190\r\n742\r\n417\r\n32\r\n2105\r\n317\r\n969\r\n2405\r\n291\r\n28\r\n130\r\n142\r\n2330\r\n183\r\n110\r\n328\r\n172\r\n1705\r\n442\r\n415\r\n1310\r\n1670\r\n1282\r\n357\r\n2210\r\n1310\r\n550\r\n382\r\n1930\r\n842\r\n13\r\n106\r\n1049\r\n16\r\n2202\r\n80\r\n80\r\n223\r\n379\r\n22\r\n31\r\n258\r\n5\r\n1102\r\n1842\r\n94\r\n73\r\n349\r\n19\r\n241\r\n260\r\n260\r\n1809\r\n466\r\n312\r\n80\r\n35\r\n52\r\n395\r\n1510\r\n364\r\n389\r\n413\r\n8\r\n2309\r\n60\r\n905\r\n50\r\n215\r\n790\r\n297\r\n750\r\n2442\r\n270\r\n255\r\n451\r\n602\r\n1070\r\n109\r\n160\r\n475\r\n484\r\n70\r\n18\r\n471\r\n1902\r\n13\r\n1969\r\n2269\r\n345\r\n99\r\n431\r\n207\r\n1570\r\n73\r\n1390\r\n308\r\n70\r\n82\r\n1049\r\n"}, {"input": "100\r\n638\r\n53\r\n413\r\n417\r\n71\r\n817\r\n523\r\n512\r\n700\r\n782\r\n505\r\n830\r\n169\r\n235\r\n356\r\n133\r\n922\r\n497\r\n670\r\n381\r\n784\r\n139\r\n144\r\n16\r\n499\r\n521\r\n354\r\n896\r\n49\r\n1000\r\n661\r\n949\r\n500\r\n910\r\n937\r\n20\r\n774\r\n498\r\n962\r\n885\r\n633\r\n265\r\n366\r\n801\r\n970\r\n660\r\n482\r\n527\r\n911\r\n240\r\n468\r\n747\r\n365\r\n565\r\n637\r\n516\r\n745\r\n892\r\n179\r\n658\r\n23\r\n860\r\n836\r\n163\r\n469\r\n841\r\n396\r\n383\r\n155\r\n51\r\n878\r\n362\r\n487\r\n781\r\n933\r\n534\r\n544\r\n251\r\n809\r\n846\r\n340\r\n711\r\n393\r\n570\r\n251\r\n471\r\n177\r\n675\r\n816\r\n290\r\n234\r\n749\r\n411\r\n126\r\n334\r\n523\r\n993\r\n812\r\n243\r\n393\r\n", "output": "14\r\n16\r\n1049\r\n380\r\n69\r\n280\r\n165\r\n9\r\n403\r\n430\r\n488\r\n122\r\n372\r\n81\r\n375\r\n2149\r\n1790\r\n360\r\n382\r\n1389\r\n74\r\n273\r\n18\r\n4\r\n253\r\n324\r\n870\r\n92\r\n52\r\n5005\r\n289\r\n1229\r\n503\r\n242\r\n500\r\n43\r\n458\r\n230\r\n1890\r\n1909\r\n196\r\n248\r\n446\r\n304\r\n2410\r\n123\r\n690\r\n1782\r\n2042\r\n1705\r\n499\r\n237\r\n1809\r\n309\r\n669\r\n95\r\n228\r\n267\r\n493\r\n630\r\n65\r\n23\r\n35\r\n285\r\n1329\r\n444\r\n55\r\n1622\r\n341\r\n9\r\n174\r\n390\r\n1182\r\n889\r\n1149\r\n198\r\n78\r\n109\r\n412\r\n366\r\n383\r\n1542\r\n156\r\n1410\r\n109\r\n442\r\n440\r\n401\r\n84\r\n710\r\n570\r\n229\r\n189\r\n486\r\n398\r\n165\r\n256\r\n107\r\n425\r\n156\r\n"}, {"input": "8\r\n99999999999\r\n1000817304\r\n16153741376\r\n99973050183\r\n5299397471\r\n60086000371\r\n25955597485\r\n32561727234\r\n", "output": "61035156266\r\n3077084936\r\n99939683987\r\n89222838971\r\n1854981735\r\n33985564103\r\n120149453377\r\n379527675\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: lines = f.read().splitlines() n = int(lines[0]) ais = lines[1:1+n] with open(submission_path) as f: submission = f.read().splitlines() if len(submission) != n: print(0) return for ai, ki_line in zip(ais, submission): if not ki_line.isdigit(): print(0) return if len(ki_line) > 1 and ki_line[0] == '0': print(0) return try: ki = int(ki_line) except: print(0) return if ki < 1 or ki > 10**50: print(0) return if ki >= 329: L = 100 else: power = pow(2, ki) len_2ki = len(str(power)) L = min(100, len_2ki) modulus = 10 ** L last_L_digits = pow(2, ki, modulus) s = str(last_L_digits).zfill(L) if ai not in s: print(0) return print(1) if __name__ == "__main__": main()
true
493/A
493
A
PyPy 3
TESTS
8
108
20,172,800
127315833
homeTeam = input() awayTeam = input() n = int(input()) result=[] awayMap={} homeMap={} for _ in range(n): a,b,c,d = map(str,input().split()) if b=='a': if c in awayMap: if awayMap[c][0]>=2: pass else: if d=='y': awayMap[c][0]+=1 else: awayMap[c][0]+=2 awayMap[c][1] = a else: if(d=='r'): awayMap[c] = [2,a] else: awayMap[c] = [1,a] else: if c in homeMap: if homeMap[c][0]>=2: pass else: if d=='y': homeMap[c][0]+=1 else: homeMap[c][0]+=2 homeMap[c][1] = a else: if(d=='r'): homeMap[c] = [2,a] else: homeMap[c] = [1,a] for i in homeMap: if homeMap[i][0]>=2: result.append([homeMap[i][1],homeTeam,i]) for i in awayMap: if awayMap[i][0]>=2: result.append([awayMap[i][1],awayTeam,i]) result.sort() for i in result: print(i[1],i[2],i[0])
18
61
0
10381058
h = input() a = input() n = int(input()) d = dict() for i in range(n): arr = input().split() mark = (h if arr[1] == 'h' else a) + ' ' + arr[2] if mark not in d: d[mark] = 0 if d[mark] < 2: d[mark] += 1 if arr[3] == 'y' else 2 if d[mark] >= 2: print(mark, arr[0])
Codeforces Round 281 (Div. 2)
CF
2,014
2
256
Vasya and Football
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card. Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct. Next follows number n (1 ≤ n ≤ 90) — the number of fouls. Each of the following n lines contains information about a foul in the following form: - first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs; - then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player; - then goes the player's number m (1 ≤ m ≤ 99); - then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given. The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
For each event when a player received his first red card in a chronological order print a string containing the following information: - The name of the team to which the player belongs; - the player's number in his team; - the minute when he received the card. If no player received a card, then you do not need to print anything. It is possible case that the program will not print anything to the output (if there were no red cards).
null
null
[{"input": "MC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r", "output": "MC 25 70\nMC 42 82\nCSKA 13 90"}]
1,300
["implementation"]
18
[{"input": "MC\r\nCSKA\r\n9\r\n28 a 3 y\r\n62 h 25 y\r\n66 h 42 y\r\n70 h 25 y\r\n77 a 4 y\r\n79 a 25 y\r\n82 h 42 r\r\n89 h 16 y\r\n90 a 13 r\r\n", "output": "MC 25 70\r\nMC 42 82\r\nCSKA 13 90\r\n"}, {"input": "REAL\r\nBARCA\r\n3\r\n27 h 7 y\r\n44 a 10 y\r\n87 h 3 r\r\n", "output": "REAL 3 87\r\n"}, {"input": "MASFF\r\nSAFBDSRG\r\n5\r\n1 h 1 y\r\n15 h 1 r\r\n27 a 1 y\r\n58 a 1 y\r\n69 h 10 y\r\n", "output": "MASFF 1 15\r\nSAFBDSRG 1 58\r\n"}, {"input": "ARMENIA\r\nBULGARIA\r\n12\r\n33 h 17 y\r\n42 h 21 y\r\n56 a 17 y\r\n58 a 6 y\r\n61 a 7 y\r\n68 a 10 y\r\n72 h 13 y\r\n73 h 21 y\r\n74 a 8 r\r\n75 a 4 y\r\n77 a 10 y\r\n90 a 23 y\r\n", "output": "ARMENIA 21 73\r\nBULGARIA 8 74\r\nBULGARIA 10 77\r\n"}, {"input": "PORTUGAL\r\nNETHERLANDS\r\n16\r\n2 a 18 y\r\n7 a 3 y\r\n20 h 18 y\r\n31 h 6 y\r\n45 h 6 y\r\n50 h 8 y\r\n59 a 5 y\r\n60 h 7 y\r\n63 a 3 y\r\n72 a 20 y\r\n73 h 20 y\r\n74 a 10 y\r\n75 h 1 y\r\n76 h 14 y\r\n78 h 20 y\r\n90 a 5 y\r\n", "output": "PORTUGAL 6 45\r\nNETHERLANDS 3 63\r\nPORTUGAL 20 78\r\nNETHERLANDS 5 90\r\n"}, {"input": "TANC\r\nXNCOR\r\n2\r\n15 h 27 r\r\n28 h 27 r\r\n", "output": "TANC 27 15\r\n"}, {"input": "ASGDFJH\r\nAHGRSDXGER\r\n3\r\n23 h 15 r\r\n68 h 15 y\r\n79 h 15 y\r\n", "output": "ASGDFJH 15 23\r\n"}, {"input": "ASFSHDSG\r\nADGYRTJNG\r\n5\r\n1 h 1 y\r\n2 h 1 y\r\n3 h 1 y\r\n4 h 1 r\r\n5 h 1 y\r\n", "output": "ASFSHDSG 1 2\r\n"}, {"input": "A\r\nB\r\n42\r\n5 a 84 y\r\n8 h 28 r\r\n10 a 9 r\r\n11 h 93 y\r\n13 a 11 r\r\n15 h 3 r\r\n20 a 88 r\r\n23 a 41 y\r\n25 a 14 y\r\n27 a 38 r\r\n28 a 33 y\r\n29 h 66 r\r\n31 a 16 r\r\n32 a 80 y\r\n34 a 54 r\r\n35 a 50 y\r\n36 a 9 y\r\n39 a 22 y\r\n42 h 81 y\r\n43 a 10 y\r\n44 a 27 r\r\n47 h 39 y\r\n48 a 80 y\r\n50 h 5 y\r\n52 a 67 y\r\n54 h 63 y\r\n56 h 7 y\r\n57 h 44 y\r\n58 h 41 y\r\n61 h 32 y\r\n64 h 91 y\r\n67 a 56 y\r\n69 h 83 y\r\n71 h 59 y\r\n72 a 76 y\r\n75 h 41 y\r\n76 a 49 r\r\n77 a 4 r\r\n78 a 69 y\r\n79 a 96 r\r\n80 h 81 y\r\n86 h 85 r\r\n", "output": "A 28 8\r\nB 9 10\r\nB 11 13\r\nA 3 15\r\nB 88 20\r\nB 38 27\r\nA 66 29\r\nB 16 31\r\nB 54 34\r\nB 27 44\r\nB 80 48\r\nA 41 75\r\nB 49 76\r\nB 4 77\r\nB 96 79\r\nA 81 80\r\nA 85 86\r\n"}, {"input": "ARM\r\nAZE\r\n45\r\n2 a 13 r\r\n3 a 73 r\r\n4 a 10 y\r\n5 h 42 y\r\n8 h 56 y\r\n10 h 15 y\r\n11 a 29 r\r\n13 a 79 y\r\n14 a 77 r\r\n18 h 7 y\r\n20 a 69 r\r\n22 h 19 y\r\n25 h 88 r\r\n26 a 78 y\r\n27 a 91 r\r\n28 h 10 r\r\n30 h 13 r\r\n31 a 26 r\r\n33 a 43 r\r\n34 a 91 y\r\n40 h 57 y\r\n44 h 18 y\r\n46 a 25 r\r\n48 a 29 y\r\n51 h 71 y\r\n57 a 16 r\r\n58 h 37 r\r\n59 h 92 y\r\n60 h 11 y\r\n61 a 88 y\r\n64 a 28 r\r\n65 h 71 r\r\n68 h 39 y\r\n70 h 8 r\r\n71 a 10 y\r\n72 a 32 y\r\n73 h 95 r\r\n74 a 33 y\r\n75 h 48 r\r\n78 a 44 y\r\n79 a 22 r\r\n80 h 50 r\r\n84 a 50 y\r\n88 a 90 y\r\n89 h 42 r\r\n", "output": "AZE 13 2\r\nAZE 73 3\r\nAZE 29 11\r\nAZE 77 14\r\nAZE 69 20\r\nARM 88 25\r\nAZE 91 27\r\nARM 10 28\r\nARM 13 30\r\nAZE 26 31\r\nAZE 43 33\r\nAZE 25 46\r\nAZE 16 57\r\nARM 37 58\r\nAZE 28 64\r\nARM 71 65\r\nARM 8 70\r\nAZE 10 71\r\nARM 95 73\r\nARM 48 75\r\nAZE 22 79\r\nARM 50 80\r\nARM 42 89\r\n"}, {"input": "KASFLS\r\nASJBGGDLJFDDFHHTHJH\r\n42\r\n2 a 68 y\r\n4 h 64 r\r\n5 a 24 y\r\n6 h 20 r\r\n8 a 16 r\r\n9 a 96 y\r\n10 h 36 r\r\n12 a 44 y\r\n13 h 69 r\r\n16 a 62 r\r\n18 a 99 r\r\n20 h 12 r\r\n21 a 68 y\r\n25 h 40 y\r\n26 h 54 r\r\n28 h 91 r\r\n29 a 36 r\r\n33 a 91 y\r\n36 h 93 r\r\n37 h 60 r\r\n38 a 82 r\r\n41 a 85 y\r\n42 a 62 r\r\n46 a 22 r\r\n48 a 88 r\r\n49 a 8 r\r\n51 h 45 y\r\n54 a 84 y\r\n57 a 8 y\r\n59 h 24 y\r\n61 h 22 r\r\n64 h 11 r\r\n69 a 89 y\r\n72 h 44 r\r\n75 h 57 r\r\n76 h 80 y\r\n77 h 54 r\r\n79 a 1 y\r\n81 a 31 r\r\n82 h 8 y\r\n83 a 28 r\r\n86 h 56 y\r\n", "output": "KASFLS 64 4\r\nKASFLS 20 6\r\nASJBGGDLJFDDFHHTHJH 16 8\r\nKASFLS 36 10\r\nKASFLS 69 13\r\nASJBGGDLJFDDFHHTHJH 62 16\r\nASJBGGDLJFDDFHHTHJH 99 18\r\nKASFLS 12 20\r\nASJBGGDLJFDDFHHTHJH 68 21\r\nKASFLS 54 26\r\nKASFLS 91 28\r\nASJBGGDLJFDDFHHTHJH 36 29\r\nKASFLS 93 36\r\nKASFLS 60 37\r\nASJBGGDLJFDDFHHTHJH 82 38\r\nASJBGGDLJFDDFHHTHJH 22 46\r\nASJBGGDLJFDDFHHTHJH 88 48\r\nASJBGGDLJFDDFHHTHJH 8 49\r\nKASFLS 22 61\r\nKASFLS 11 64\r\nKASFLS 44 72\r\nKASFLS 57 75\r\nASJBGGDLJFDDFHHTHJH 31 81\r\nASJBGGDLJFDDFHHTHJH 28 83\r\n"}, {"input": "A\r\nAA\r\n2\r\n1 a 1 y\r\n2 h 1 y\r\n", "output": ""}, {"input": "AB\r\nBC\r\n3\r\n1 h 1 y\r\n2 h 1 y\r\n3 h 1 r\r\n", "output": "AB 1 2\r\n"}]
false
stdio
null
true
165/A
165
A
PyPy 3-64
TESTS
9
124
0
167721025
import sys input = sys.stdin.readline test = False mod1, mod2 = 10 ** 9 + 7, 998244353 inf = 10 ** 18 + 5 lim = 2 * 10 ** 5 + 5 def test_case(): n = int(input()) coor = [] x_coor = [[] for _ in range(1005)] y_coor = [[] for _ in range(1005)] for i in range(n): cur = list(map(int, input().split())) coor.append(cur) x_coor[cur[0]].append(cur[1]) y_coor[cur[1]].append(cur[0]) cnt = 0 for x, y in coor: neighbours = [0, 0, 0, 0] for xx in y_coor[y]: if xx < x: neighbours[0] = 1 if xx > x: neighbours[1] = 1 for yy in x_coor[x]: if yy < y: neighbours[2] = 1 if yy > y: neighbours[3] = 1 cnt += (sum(neighbours) == 4) print(cnt) t = 1 if test: t = int(input()) for _ in range(t): # print(f"Case #{_+1}: ", end='') test_case()
26
92
0
137067791
n=int(input()) a=[] for i in range(n): a.append([int(i) for i in input().split()]) a=sorted(a) ans=0 for i in range(n): con=[False,False,False,False] x0=a[i][0] y0=a[i][1] for j in range(0,len(a)): if x0==a[j][0]: if y0<a[j][1]: con[2]=True elif y0>a[j][1]: con[3]=True if y0==a[j][1]: if x0<a[j][0]: con[1]=True elif x0>a[j][0]: con[0]=True if con==[True,True,True,True]: ans+=1 break print(ans)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
294/A
294
A
PyPy 3-64
TESTS
30
92
0
213798124
n = int(input()) array = input().split() array = [int(x) for x in array] m = int(input()) x = [] y = [] for i in range(m): a, b = input().split() x.append(int(a)) y.append(int(b)) if n == 1: array[0] = 0 else: for i in range(m): if x[i] == 1: array[0+1] += array[0] - y[i] array[0] = 0 elif x[i] == n: array[n-1-1] += y[i] - 1 array[n-1] = 0 else: array[x[i]-1-1] += y[i] - 1 array[x[i]-1+1] += array[x[i]-1] - y[i] array[x[i]-1] = 0 for i in range(n): print(array[i])
31
62
0
145704448
n = int(input()) A = list(map(int, input().split())) m = int(input()) for i in range(m): x, y = map(int, input().split()); x -= 1; y -= 1 if x > 0: A[x-1] += y if x < n-1: A[x+1] += A[x]-y-1 A[x] = 0 for a in A: print(a)
Codeforces Round 178 (Div. 2)
CF
2,013
2
256
Shaass and Oskols
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.
On the i-th line of the output print the number of birds on the i-th wire.
null
null
[{"input": "5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "output": "0\n12\n5\n0\n16"}, {"input": "3\n2 4 1\n1\n2 2", "output": "3\n0\n3"}]
800
["implementation", "math"]
31
[{"input": "5\r\n10 10 10 10 10\r\n5\r\n2 5\r\n3 13\r\n2 12\r\n1 13\r\n4 6\r\n", "output": "0\r\n12\r\n5\r\n0\r\n16\r\n"}, {"input": "3\r\n2 4 1\r\n1\r\n2 2\r\n", "output": "3\r\n0\r\n3\r\n"}, {"input": "5\r\n58 51 45 27 48\r\n5\r\n4 9\r\n5 15\r\n4 5\r\n5 8\r\n1 43\r\n", "output": "0\r\n66\r\n57\r\n7\r\n0\r\n"}, {"input": "10\r\n48 53 10 28 91 56 81 2 67 52\r\n2\r\n2 40\r\n6 51\r\n", "output": "87\r\n0\r\n23\r\n28\r\n141\r\n0\r\n86\r\n2\r\n67\r\n52\r\n"}, {"input": "2\r\n72 45\r\n6\r\n1 69\r\n2 41\r\n1 19\r\n2 7\r\n1 5\r\n2 1\r\n", "output": "0\r\n0\r\n"}, {"input": "10\r\n95 54 36 39 98 30 19 24 14 12\r\n3\r\n9 5\r\n8 15\r\n7 5\r\n", "output": "95\r\n54\r\n36\r\n39\r\n98\r\n34\r\n0\r\n28\r\n13\r\n21\r\n"}, {"input": "1\r\n100\r\n1\r\n1 100\r\n", "output": "0\r\n"}, {"input": "1\r\n100\r\n1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1\r\n1 25\r\n", "output": "0\r\n"}, {"input": "2\r\n50 0\r\n1\r\n1 1\r\n", "output": "0\r\n49\r\n"}, {"input": "1\r\n10\r\n0\r\n", "output": "10\r\n"}]
false
stdio
null
true
294/C
294
C
Python 3
TESTS
4
109
307,200
68315251
def fac(x): pr = 1 for i in range(2, x + 1): pr *= i return pr n, m = map(int, input().split()) a = list(map(int, input().split())) x = a[0] - 1 y = n - a[-1] mn1 = 1 mn2 = 1 for i in range(1, m): mn1 *= fac(a[i] - a[i - 1] - 1) mn2 *= (2 ** (a[i] - a[i - 1] - 2)) ans = fac(n - m) // (fac(x) * fac(y) * mn1) * mn2 print(ans % 1000000007)
30
108
307,200
102414504
from sys import stdin, stdout def main(): p = 1000000007 # Constante brindada por el problema n, m = readline() on = sorted(readline()) # Se ordena de menor a mayor los elementos del conjunto de luces encendidas off = [] # Conjunto de cardinalidades de los segmentos de luces apagadas sum_ = 0 for i in range(1, m): # Se analizan todos los segmentos interiores c = on[i] - on[i - 1] - 1 if c > 0: off.append(c) sum_ += c factorial = [1] * (on[0] + sum_ + n - on[-1]) for i in range(2, len(factorial)): # Factoriales modulo p hasta la cantidad de luces apagadas factorial[i] = i * factorial[i - 1] % p answer = pow(2, sum_ - len(off), p) * factorial[n - m] % p # Se calcula el numerador de la expresion off.append(on[0] - 1) # Se agregan los segmentos exteriores off.append(n - on[-1]) for x in off: # Se agrega al calculo los elementos del denominador answer = answer * pow(factorial[x], p - 2, p) % p # Se utiliza el inverso multiplicativo return answer def readline(): # Metodo para leer una linea completa, dividirla en elementos y convertirlos en numeros enteros return map(int, stdin.readline().strip().split()) if __name__ == '__main__': stdout.write(str(main()) + '\n')
Codeforces Round 178 (Div. 2)
CF
2,013
1
256
Shaass and Lights
There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (109 + 7).
The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on.
In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (109 + 7).
null
null
[{"input": "3 1\n1", "output": "1"}, {"input": "4 2\n1 4", "output": "2"}, {"input": "11 2\n4 8", "output": "6720"}]
1,900
["combinatorics", "number theory"]
30
[{"input": "3 1\r\n1\r\n", "output": "1\r\n"}, {"input": "4 2\r\n1 4\r\n", "output": "2\r\n"}, {"input": "11 2\r\n4 8\r\n", "output": "6720\r\n"}, {"input": "4 2\r\n1 3\r\n", "output": "2\r\n"}, {"input": "4 4\r\n1 2 3 4\r\n", "output": "1\r\n"}, {"input": "4 2\r\n1 3\r\n", "output": "2\r\n"}, {"input": "4 4\r\n1 2 3 4\r\n", "output": "1\r\n"}, {"input": "1000 3\r\n100 900 10\r\n", "output": "727202008\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "68 37\r\n1 2 3 6 7 8 10 11 12 14 16 18 22 23 24 26 30 31 32 35 37 39 41 42 45 47 50 51 52 54 58 59 61 62 63 64 68\r\n", "output": "867201120\r\n"}, {"input": "132 48\r\n6 7 8 12 15 17 18 19 22 24 25 26 30 33 35 38 40 43 46 49 50 51 52 54 59 60 66 70 76 79 87 89 91 92 94 98 99 101 102 105 106 109 113 115 116 118 120 129\r\n", "output": "376947760\r\n"}, {"input": "36 24\r\n1 7 8 10 11 12 13 14 15 16 17 19 21 22 25 26 27 28 29 30 31 32 35 36\r\n", "output": "63866880\r\n"}, {"input": "100 2\r\n11 64\r\n", "output": "910895596\r\n"}, {"input": "1000 1\r\n35\r\n", "output": "253560421\r\n"}, {"input": "1000 2\r\n747 798\r\n", "output": "474746180\r\n"}, {"input": "1000 3\r\n804 811 984\r\n", "output": "600324842\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}]
false
stdio
null
true
294/A
294
A
Python 3
TESTS
30
218
307,200
93028191
I = input n = int(I()) a = list(map(int, I().split())) m = int(I()) if n==1: print(0) else: for i in range(m): x, y = map(int, I().split()) if x==1: a[x]+=(a[x-1]-y) a[x-1]=0 elif x==n: a[x-2]+=(y-1) a[x-1]=0 else: a[x-2]+=(y-1) a[x]+=(a[x-1]-y) a[x-1]=0 for i in range(n): print(a[i])
31
62
0
148939018
n = int(input()) a = [int(a) for a in input().split()[:n]] m = int(input()) for line in range(m): x,y = [int(x) for x in input().split()] down = a[x-1] - y up = y-1 a[x-1] = 0 if x < len(a): a[x] = a[x] + down if x > 1: a[x-2] = a[x-2] + up for i in a: print(i)
Codeforces Round 178 (Div. 2)
CF
2,013
2
256
Shaass and Oskols
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.
On the i-th line of the output print the number of birds on the i-th wire.
null
null
[{"input": "5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "output": "0\n12\n5\n0\n16"}, {"input": "3\n2 4 1\n1\n2 2", "output": "3\n0\n3"}]
800
["implementation", "math"]
31
[{"input": "5\r\n10 10 10 10 10\r\n5\r\n2 5\r\n3 13\r\n2 12\r\n1 13\r\n4 6\r\n", "output": "0\r\n12\r\n5\r\n0\r\n16\r\n"}, {"input": "3\r\n2 4 1\r\n1\r\n2 2\r\n", "output": "3\r\n0\r\n3\r\n"}, {"input": "5\r\n58 51 45 27 48\r\n5\r\n4 9\r\n5 15\r\n4 5\r\n5 8\r\n1 43\r\n", "output": "0\r\n66\r\n57\r\n7\r\n0\r\n"}, {"input": "10\r\n48 53 10 28 91 56 81 2 67 52\r\n2\r\n2 40\r\n6 51\r\n", "output": "87\r\n0\r\n23\r\n28\r\n141\r\n0\r\n86\r\n2\r\n67\r\n52\r\n"}, {"input": "2\r\n72 45\r\n6\r\n1 69\r\n2 41\r\n1 19\r\n2 7\r\n1 5\r\n2 1\r\n", "output": "0\r\n0\r\n"}, {"input": "10\r\n95 54 36 39 98 30 19 24 14 12\r\n3\r\n9 5\r\n8 15\r\n7 5\r\n", "output": "95\r\n54\r\n36\r\n39\r\n98\r\n34\r\n0\r\n28\r\n13\r\n21\r\n"}, {"input": "1\r\n100\r\n1\r\n1 100\r\n", "output": "0\r\n"}, {"input": "1\r\n100\r\n1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1\r\n1 25\r\n", "output": "0\r\n"}, {"input": "2\r\n50 0\r\n1\r\n1 1\r\n", "output": "0\r\n49\r\n"}, {"input": "1\r\n10\r\n0\r\n", "output": "10\r\n"}]
false
stdio
null
true
294/A
294
A
Python 3
TESTS
30
218
307,200
69433370
n=int(input()) a=list(map(int,input().split())) m=int(input()) l=[] for i in range(m): k=list(map(int,input().split())) l.append(k) if n==1: print(0) else: for i in range(m): if l[i][0]-1==0: a[l[i][0]]+= a[l[i][0]-1]-l[i][1] a[l[i][0]-1]=0 elif l[i][0]-1==n-1: a[l[i][0]-2]+=l[i][1]-1 a[l[i][0]-1]=0 else: a[l[i][0]]+= a[l[i][0]-1]-l[i][1] a[l[i][0]-2]+=l[i][1]-1 a[l[i][0]-1]=0 for i in range(len(a)): print(a[i])
31
62
0
149637842
n = int(input()) a = [int(i) for i in input().split()] m = int(input()) for i in range(m): wire , bird = [int(j) for j in input().split()] if wire < n: a[wire] += a[wire - 1] - bird if wire - 2 >= 0: a[wire - 2] += bird - 1 a[wire - 1] = 0 for j in range(n): print(a[j])
Codeforces Round 178 (Div. 2)
CF
2,013
2
256
Shaass and Oskols
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.
On the i-th line of the output print the number of birds on the i-th wire.
null
null
[{"input": "5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "output": "0\n12\n5\n0\n16"}, {"input": "3\n2 4 1\n1\n2 2", "output": "3\n0\n3"}]
800
["implementation", "math"]
31
[{"input": "5\r\n10 10 10 10 10\r\n5\r\n2 5\r\n3 13\r\n2 12\r\n1 13\r\n4 6\r\n", "output": "0\r\n12\r\n5\r\n0\r\n16\r\n"}, {"input": "3\r\n2 4 1\r\n1\r\n2 2\r\n", "output": "3\r\n0\r\n3\r\n"}, {"input": "5\r\n58 51 45 27 48\r\n5\r\n4 9\r\n5 15\r\n4 5\r\n5 8\r\n1 43\r\n", "output": "0\r\n66\r\n57\r\n7\r\n0\r\n"}, {"input": "10\r\n48 53 10 28 91 56 81 2 67 52\r\n2\r\n2 40\r\n6 51\r\n", "output": "87\r\n0\r\n23\r\n28\r\n141\r\n0\r\n86\r\n2\r\n67\r\n52\r\n"}, {"input": "2\r\n72 45\r\n6\r\n1 69\r\n2 41\r\n1 19\r\n2 7\r\n1 5\r\n2 1\r\n", "output": "0\r\n0\r\n"}, {"input": "10\r\n95 54 36 39 98 30 19 24 14 12\r\n3\r\n9 5\r\n8 15\r\n7 5\r\n", "output": "95\r\n54\r\n36\r\n39\r\n98\r\n34\r\n0\r\n28\r\n13\r\n21\r\n"}, {"input": "1\r\n100\r\n1\r\n1 100\r\n", "output": "0\r\n"}, {"input": "1\r\n100\r\n1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1\r\n1 25\r\n", "output": "0\r\n"}, {"input": "2\r\n50 0\r\n1\r\n1 1\r\n", "output": "0\r\n49\r\n"}, {"input": "1\r\n10\r\n0\r\n", "output": "10\r\n"}]
false
stdio
null
true
294/A
294
A
PyPy 3
TESTS
30
248
0
106993025
numWires = int(input()) a = list(map(int, input().split())) numShots = int(input()) if len(a) == 1: a = [0] else: for i in range(numShots): shot = list(map(int, input().split())) if shot[0] == len(a): a[shot[0] - 2] += shot[1] - 1 elif shot[0] == 1: a[shot[0]] += a[shot[0] - 1] - shot[1] else: a[shot[0] - 2] += shot[1] - 1 a[shot[0]] += a[shot[0] - 1] - shot[1] a[shot[0] - 1] = 0 for i in a: print(i)
31
62
0
174729836
import sys from math import gcd def inp() : return sys.stdin.readline().strip() def get_ints(): return map(int, inp().split()) def get_arr(): return list(map(int, inp().split())) def get_int(): return int(inp()) n=get_int() os=get_arr() t=get_int() for _ in range(t): x,y=get_ints() x-=1 if(x-1>=0 and x+1<n): os[x-1]+=(y-1) os[x+1]+=(os[x]-y) elif(x-1<0 and x+1<n): os[x+1]+=(os[x]-y) elif(x-1>=0 and x+1>=n): os[x-1]+=(y-1) os[x]=0 for i in os: print(i)
Codeforces Round 178 (Div. 2)
CF
2,013
2
256
Shaass and Oskols
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.
On the i-th line of the output print the number of birds on the i-th wire.
null
null
[{"input": "5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "output": "0\n12\n5\n0\n16"}, {"input": "3\n2 4 1\n1\n2 2", "output": "3\n0\n3"}]
800
["implementation", "math"]
31
[{"input": "5\r\n10 10 10 10 10\r\n5\r\n2 5\r\n3 13\r\n2 12\r\n1 13\r\n4 6\r\n", "output": "0\r\n12\r\n5\r\n0\r\n16\r\n"}, {"input": "3\r\n2 4 1\r\n1\r\n2 2\r\n", "output": "3\r\n0\r\n3\r\n"}, {"input": "5\r\n58 51 45 27 48\r\n5\r\n4 9\r\n5 15\r\n4 5\r\n5 8\r\n1 43\r\n", "output": "0\r\n66\r\n57\r\n7\r\n0\r\n"}, {"input": "10\r\n48 53 10 28 91 56 81 2 67 52\r\n2\r\n2 40\r\n6 51\r\n", "output": "87\r\n0\r\n23\r\n28\r\n141\r\n0\r\n86\r\n2\r\n67\r\n52\r\n"}, {"input": "2\r\n72 45\r\n6\r\n1 69\r\n2 41\r\n1 19\r\n2 7\r\n1 5\r\n2 1\r\n", "output": "0\r\n0\r\n"}, {"input": "10\r\n95 54 36 39 98 30 19 24 14 12\r\n3\r\n9 5\r\n8 15\r\n7 5\r\n", "output": "95\r\n54\r\n36\r\n39\r\n98\r\n34\r\n0\r\n28\r\n13\r\n21\r\n"}, {"input": "1\r\n100\r\n1\r\n1 100\r\n", "output": "0\r\n"}, {"input": "1\r\n100\r\n1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1\r\n1 25\r\n", "output": "0\r\n"}, {"input": "2\r\n50 0\r\n1\r\n1 1\r\n", "output": "0\r\n49\r\n"}, {"input": "1\r\n10\r\n0\r\n", "output": "10\r\n"}]
false
stdio
null
true
730/G
730
G
PyPy 3
TESTS
6
155
4,812,800
160374723
from bisect import * from collections import * import sys import io, os import math import random from heapq import * gcd = math.gcd sqrt = math.sqrt def ceil(a, b): a = -a k = a // b k = -k return k # arr=list(map(int, input().split())) input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def strinp(testcases): k = 5 if (testcases == -1 or testcases == 1): k = 1 f = str(input()) f = f[2:len(f) - k] return f def main(): n=int(input()) s=[0]*n d=[0]*n for i in range(n): s[i],d[i]=map(int,input().split()) sch=[] sch.append([s[0],s[0]+d[0]-1,0]) heapify(sch) for i in range(1,n): st=s[i] ed=s[i]+d[i]-1 trig=True tsch=[] heapify(tsch) for j in range(i): g=heappop(sch) heappush(tsch,g) l=g[0] r=g[1] if((l>=st and l<=ed) or (r>=st and r<=ed)): trig=False if(trig): heappush(tsch,[st,ed,i]) sch=tsch continue cs=1 sch=tsch tsch=[] heapify(tsch) for j in range(i-1): g=heappop(sch) heappush(tsch,g) l=g[0] r=g[1] if(l-cs>=d[i]): break cs=r+1 while(sch!=[]): heappush(tsch,heappop(sch)) heappush(tsch,[cs,cs+d[i]-1,i]) sch=tsch ans=[0]*n for i in range(n): g=heappop(sch) ans[g[2]]=[g[0],g[1]] for i in range(n): print(*ans[i]) main()
28
280
307,200
101603018
n = int(input()) l, r = [0] * n, [0] * n f = lambda x, y: all(x > r[j] or y < l[j] for j in range(i)) for i in range(n): x, d = map(int, input().split()) y = x + d - 1 if not f(x, y): k = min(r[j] for j in range(i + 1) if f(r[j] + 1, r[j] + d)) x, y = k + 1, k + d l[i], r[i] = x, y print(x, y)
2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,016
2
512
Car Repair Shop
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came. The i-th request is characterized by two values: si — the day when a client wants to start the repair of his car, di — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows: - If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client. - Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + di - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + di - 1]. It is possible that the day x when repair is scheduled to start will be less than si. Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients. The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≤ si ≤ 109, 1 ≤ di ≤ 5·106), where si is the preferred time to start repairing the i-th car, di is the number of days to repair the i-th car. The requests should be processed in the order they are given in the input.
Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.
null
null
[{"input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7"}, {"input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000"}]
1,600
["implementation"]
28
[{"input": "3\r\n9 2\r\n7 3\r\n2 4\r\n", "output": "9 10\r\n1 3\r\n4 7\r\n"}, {"input": "4\r\n1000000000 1000000\r\n1000000000 1000000\r\n100000000 1000000\r\n1000000000 1000000\r\n", "output": "1000000000 1000999999\r\n1 1000000\r\n100000000 100999999\r\n1000001 2000000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 1\r\n"}, {"input": "1\r\n1000000000 1\r\n", "output": "1000000000 1000000000\r\n"}, {"input": "1\r\n1000000000 5000000\r\n", "output": "1000000000 1004999999\r\n"}, {"input": "5\r\n6 2\r\n10 1\r\n10 2\r\n9 2\r\n5 1\r\n", "output": "6 7\r\n10 10\r\n1 2\r\n3 4\r\n5 5\r\n"}, {"input": "10\r\n1 3\r\n77 8\r\n46 5\r\n83 4\r\n61 7\r\n8 4\r\n54 7\r\n80 7\r\n33 7\r\n13 4\r\n", "output": "1 3\r\n77 84\r\n46 50\r\n4 7\r\n61 67\r\n8 11\r\n54 60\r\n12 18\r\n33 39\r\n19 22\r\n"}, {"input": "10\r\n588 12\r\n560 10\r\n593 14\r\n438 15\r\n761 11\r\n984 6\r\n503 2\r\n855 19\r\n538 2\r\n650 7\r\n", "output": "588 599\r\n560 569\r\n1 14\r\n438 452\r\n761 771\r\n984 989\r\n503 504\r\n855 873\r\n538 539\r\n650 656\r\n"}, {"input": "20\r\n360 26\r\n475 17\r\n826 12\r\n815 23\r\n567 28\r\n897 26\r\n707 20\r\n1000 9\r\n576 5\r\n16 5\r\n714 16\r\n630 17\r\n426 26\r\n406 23\r\n899 25\r\n102 22\r\n896 8\r\n320 27\r\n964 25\r\n932 18\r\n", "output": "360 385\r\n475 491\r\n826 837\r\n1 23\r\n567 594\r\n897 922\r\n707 726\r\n1000 1008\r\n24 28\r\n29 33\r\n34 49\r\n630 646\r\n426 451\r\n50 72\r\n73 97\r\n102 123\r\n124 131\r\n320 346\r\n964 988\r\n932 949\r\n"}, {"input": "30\r\n522692116 84\r\n589719489 488\r\n662495181 961\r\n915956552 470\r\n683572975 271\r\n498400137 480\r\n327010963 181\r\n200704287 367\r\n810826488 54\r\n978100746 208\r\n345455616 986\r\n106372142 876\r\n446972337 42\r\n309349333 200\r\n93462198 543\r\n167946793 318\r\n325598940 427\r\n121873339 459\r\n174934933 598\r\n279521023 655\r\n739750520 3\r\n870850765 192\r\n622303167 400\r\n471234786 63\r\n805952711 18\r\n349834333 857\r\n804873364 302\r\n512746562 39\r\n533285962 561\r\n996718586 494\r\n", "output": "522692116 522692199\n589719489 589719976\n662495181 662496141\n915956552 915957021\n683572975 683573245\n498400137 498400616\n327010963 327011143\n200704287 200704653\n810826488 810826541\n978100746 978100953\n345455616 345456601\n106372142 106373017\n446972337 446972378\n309349333 309349532\n93462198 93462740\n167946793 167947110\n325598940 325599366\n121873339 121873797\n174934933 174935530\n279521023 279521677\n739750520 739750522\n870850765 870850956\n622303167 622303566\n471234786 471234848\n805952711 805952728\n349834333 349835189\n804873364 804873665\n512746562 512746600\n533285962 533286522\n996718586 996719079\n"}, {"input": "2\r\n10 3\r\n9 2\r\n", "output": "10 12\r\n1 2\r\n"}, {"input": "1\r\n1 5000000\r\n", "output": "1 5000000\r\n"}]
false
stdio
null
true
45/C
45
C
PyPy 3-64
TESTS
10
77
3,072,000
182407187
import sys input = sys.stdin.readline from heapq import heappop,heappush n=int(input()) S=input().strip() A=list(map(int,input().split())) LEFT=[i-1 for i in range(n)] RIGHT=[i+1 for i in range(n)] USE=[0]*n H=[] for i in range(n-1): if S[i]!=S[i+1]: heappush(H,(abs(A[i]-A[i+1]),i)) H.sort() ANS=[] while H: #print(H) x,ind=heappop(H) if USE[ind]==1 or USE[RIGHT[ind]]==1: continue ANS.append((ind+1,RIGHT[ind]+1)) USE[ind]=1 USE[RIGHT[ind]]=1 r=RIGHT[RIGHT[ind]] l=LEFT[ind] if 0<=l<n and 0<=r<n and USE[l]==0 and USE[r]==0 and S[l]!=S[r]: RIGHT[l]=r LEFT[r]=l heappush(H,(abs(A[l]-A[r]),l)) print(len(ANS)) for x,y in ANS: print(x,y)
31
1,106
68,505,600
216640543
from heapq import heappush, heappop, heapify def improve_arrangement(n, b, a): c = [] d = [0] * n e = [] ahead = [0] + [i for i in range(n)] after = [i + 1 for i in range(n)] + [0] num = 0 for i in range(n - 1): x = i y = i + 1 if b[x] != b[y]: c.append((abs(a[x] - a[y]), x, y)) heapify(c) while c: skill, cp1, cp2 = heappop(c) if d[cp1] + d[cp2] == 0: d[cp1], d[cp2] = 1, 1 e.append(str(cp1 + 1) + " " + str(cp2 + 1)) num += 1 if cp1 > 0 and cp2 < n - 1: x = ahead[cp1] y = after[cp2] ahead[y] = x after[x] = y if b[x] != b[y]: heappush(c, (abs(a[x] - a[y]), x, y)) return num, e if __name__ == "__main__": n = int(input()) b = list(input()) a = [int(i) for i in input().split()] num, e = improve_arrangement(n, b, a) print(num) print('\n'.join(e))
School Team Contest 3 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Dancing Lessons
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
The first line contains an integer n (1 ≤ n ≤ 2·105) — the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the dancing skill. People are specified from left to right in the order in which they lined up.
Print the resulting number of couples k. Then print k lines containing two numerals each — the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
null
null
[{"input": "4\nBGBG\n4 2 4 3", "output": "2\n3 4\n1 2"}, {"input": "4\nBBGG\n4 6 1 5", "output": "2\n2 3\n1 4"}, {"input": "4\nBGBB\n1 1 2 3", "output": "1\n1 2"}]
1,900
["data structures"]
31
[{"input": "4\r\nBGBG\r\n4 2 4 3\r\n", "output": "2\r\n3 4\r\n1 2\r\n"}, {"input": "4\r\nBBGG\r\n4 6 1 5\r\n", "output": "2\r\n2 3\r\n1 4\r\n"}, {"input": "4\r\nBGBB\r\n1 1 2 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "1\r\nB\r\n490297\r\n", "output": "0\r\n"}, {"input": "2\r\nBB\r\n2518190 6313112\r\n", "output": "0\r\n"}, {"input": "3\r\nBBB\r\n3146681 8599398 1571560\r\n", "output": "0\r\n"}, {"input": "10\r\nGBBBBGBBGG\r\n9448959 6234076 8008159 4440374 4736443 255932 6568554 4544108 54187 1105310\r\n", "output": "4\r\n1 2\r\n5 6\r\n8 9\r\n7 10\r\n"}, {"input": "10\r\nBGGBBGBGBG\r\n9892816 3514007 5425956 5241945 9171176 3351177 2772494 2891569 1510552 8471969\r\n", "output": "5\r\n7 8\r\n3 4\r\n6 9\r\n5 10\r\n1 2\r\n"}, {"input": "10\r\nGGGGBGGGBB\r\n2853026 3310290 2843753 8559868 1089557 6446421 8976433 3755381 2966918 3322276\r\n", "output": "3\r\n8 9\r\n5 6\r\n7 10\r\n"}, {"input": "10\r\nGBBGBBBGBB\r\n9251505 1388373 4869655 8551146 5931052 358636 416134 8756343 8254852 661072\r\n", "output": "3\r\n8 9\r\n4 5\r\n1 2\r\n"}]
false
stdio
null
true
452/F
452
F
Python 3
TESTS
0
31
0
177090959
# LUOGU_RID: 90672285 from random import choice a = choice(["YES", "NO"]) print(a)
100
374
46,694,400
122667287
n = int(input()) v = list(map(int, input().split())) ans = "NO" p = [] for i in range(n + 1): p.append(-1) for i in range(n): p[v[i]] = i for i in range(n - 1): for j in range(i + 1, min(n, i + 6)): if v[i] * 2 - v[j] >= 1 and v[i] * 2 - v[j] <= n and p[v[i] * 2 - v[j]] < i: ans = "YES" break if v[j] * 2 - v[i] >= 1 and v[j] * 2 - v[i] <= n and p[v[j] * 2 - v[i]] > j: ans = "YES" break if ans == "YES": break print(ans)
MemSQL Start[c]UP 2.0 - Round 1
CF
2,014
1
256
Permutation
You are given a permutation of numbers from 1 to n. Determine whether there's a pair of integers a, b (1 ≤ a, b ≤ n; a ≠ b) such that the element $${ \frac { ( a + b ) } { 2 } }$$ (note, that it is usual division, not integer one) is between a and b in this permutation.
First line consists of a single integer n (1 ≤ n ≤ 300000) — the size of permutation. Second line contains n integers — the permutation itself.
Print "YES", if such a pair exists, "NO" otherwise (in both cases without quotes, the answer is case insensitive).
null
In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5.
[{"input": "4\n1 3 4 2", "output": "NO"}, {"input": "5\n1 5 2 4 3", "output": "YES"}]
2,700
["data structures", "divide and conquer", "hashing"]
100
[{"input": "4\r\n1 3 4 2\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 5 2 4 3\r\n", "output": "YES\r\n"}, {"input": "100\r\n17 41 19 23 46 16 10 31 82 12 77 32 11 71 83 25 98 18 34 59 13 73 80 65 37 22 6 2 24 5 94 42 51 63 52 92 97 26 93 38 36 87 64 70 14 43 68 85 33 44 74 89 56 1 69 88 20 49 48 21 84 90 7 47 39 55 81 86 76 57 3 62 15 78 100 60 61 66 91 30 58 35 99 96 54 27 79 9 29 50 45 72 75 4 67 40 8 53 95 28\r\n", "output": "YES\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n22 78 19 70 81 33 15 72 40 51 18 62 36 24 37 20 97 65 89 75 14 55 23 53 1 67 50 99 54 76 41 16 44 60 2 90 7 28 79 43 47 64 71 27 25 8 46 92 95 80 31 100 42 96 86 66 52 63 98 4 56 91 34 83 85 3 84 17 94 11 73 29 6 58 61 68 57 88 10 32 82 93 26 13 87 39 30 5 12 59 9 48 69 35 77 21 45 38 49 74\r\n", "output": "YES\r\n"}, {"input": "100\r\n75 28 8 98 60 16 40 89 90 39 44 88 51 9 95 42 27 63 92 15 67 3 19 81 54 2 97 61 45 93 58 84 70 83 79 78 21 12 94 87 64 11 56 4 10 49 25 1 33 86 62 72 69 74 96 48 6 46 29 66 23 73 50 37 5 18 41 34 22 7 82 99 35 57 38 26 31 100 55 17 91 85 24 71 43 13 52 65 76 77 36 47 53 32 14 30 20 68 59 80\r\n", "output": "YES\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 2 95 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 2 31 66 95 34 63 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "YES\r\n"}, {"input": "100\r\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 2 31 95 66 34 63 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64\r\n", "output": "YES\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "NO\r\n"}, {"input": "5\r\n3 5 4 1 2\r\n", "output": "NO\r\n"}, {"input": "14\r\n7 11 9 13 10 3 1 14 12 5 2 6 4 8\r\n", "output": "NO\r\n"}, {"input": "6\r\n6 2 4 1 3 5\r\n", "output": "YES\r\n"}]
false
stdio
null
true
297/A
297
A
PyPy 3
TESTS
31
310
0
84234237
a=input() b=input() coa=0 cob=0 for i in a: if i=='1': coa+=1 for i in b: if i=='1': cob+=1 if coa%2==1: print('YES') else: if cob>coa: print('NO') else: print('YES')
79
122
4,608,000
29355698
ax, bx = 0, 0 for c in input(): if c == '1': ax += 1 for c in input(): if c == '1': bx += 1 print("YES" if bx <= ax + ax % 2 else "NO")
Codeforces Round 180 (Div. 1)
CF
2,013
1
256
Parity Game
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: - Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$. - Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b? The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
null
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
1,700
["constructive algorithms"]
79
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
false
stdio
null
true
547/D
547
D
PyPy 3-64
TESTS
0
46
0
224154830
n = int(input()) points = [] for _ in range(n): x, y = map(int, input().split()) points.append((x, y)) # Sort the points by x-coordinates and then by y-coordinates points.sort() colors = [None] * n red_count = 0 blue_count = 0 for i in range(n): x, y = points[i] if red_count <= blue_count: # Color the point red if there are fewer red points colors[i] = 'r' red_count += 1 else: # Color the point blue if there are fewer blue points colors[i] = 'b' blue_count += 1 print("".join(colors))
50
889
112,640,000
224152629
from sys import stdin, stdout from collections import defaultdict time = 0 c = 2*10**5 n = 4*10**5+2 col = 0 finished= [0]*n for_node = [0]*n f_range=range f_len=len def dfs_euler_tour(node: int, graph): stack = [] global colour global col global time stack.append((node, -1)) while stack: s, ind = stack.pop() if ind > -1: if ind < f_len(colour): colour[ind] = col col = col ^ 1 index = for_node[s] while(index < f_len(graph[s])): v,i=graph[s][index] if not visited[i]: stack.append((v, i)) visited[i] = True index += 1 for_node[s] = index break index += 1 finished[s]+1 m = int(stdin.readline()) graph =defaultdict(list) edges=[] debug=[] for i in f_range(m): u, v = stdin.readline().split() u, v = (int(u)-1, int(v)-1 + c+1) edges.append((u, v)) graph[u].append((v, i)) graph[v].append((u, i)) if u == 30630-1 and m == 199809: debug.append(i) colour = [-1]*m odds= [i for i in graph.keys() if f_len(graph[i]) % 2] if odds: for i in f_range(f_len(odds)): u = odds[i] ind=f_len(edges) if u<c: v = n-1 else: v = c edges.append((u, v)) graph[u].append((v, ind)) graph[v].append((u, ind)) if f_len(graph[n-1]) % 2: u=n-1 v=c ind = f_len(edges) edges.append((u, v)) graph[u].append((v, ind)) graph[v].append((u, ind)) visited = [False]*f_len(edges) for i in graph.keys(): if not finished[i]: dfs_euler_tour(i, graph) sol = ''.join(['b' if i == 1 else 'r' for i in colour]) sol += '\n' stdout.write(sol)
Codeforces Round 305 (Div. 1)
CF
2,015
3
256
Mike and Fish
As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange thing about him is he has an infinite number of blue and red fish. He has marked n distinct points in the plane. i-th point is point (xi, yi). He wants to put exactly one fish in each of these points such that the difference between the number of red fish and the blue fish on each horizontal or vertical line is at most 1. He can't find a way to perform that! Please help him.
The first line of input contains integer n (1 ≤ n ≤ 2 × 105). The next n lines contain the information about the points, i-th line contains two integers xi and yi (1 ≤ xi, yi ≤ 2 × 105), the i-th point coordinates. It is guaranteed that there is at least one valid answer.
Print the answer as a sequence of n characters 'r' (for red) or 'b' (for blue) where i-th character denotes the color of the fish in the i-th point.
null
null
[{"input": "4\n1 1\n1 2\n2 1\n2 2", "output": "brrb"}, {"input": "3\n1 1\n1 2\n2 1", "output": "brr"}]
2,600
["constructive algorithms", "dfs and similar", "graphs"]
50
[{"input": "4\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "brrb\r\n"}, {"input": "3\r\n1 1\r\n1 2\r\n2 1\r\n", "output": "brr\r\n"}, {"input": "3\r\n157210 22861\r\n175396 39466\r\n40933 17093\r\n", "output": "rrr\r\n"}, {"input": "5\r\n55599 84144\r\n169207 98421\r\n1909 186625\r\n31525 147710\r\n7781 82078\r\n", "output": "rrrrr\r\n"}, {"input": "15\r\n44249 54630\r\n165741 91307\r\n49455 83026\r\n52521 88269\r\n39286 65158\r\n38282 73821\r\n96608 30032\r\n155832 122920\r\n22021 13546\r\n161556 192797\r\n168062 8224\r\n161221 155335\r\n5670 180269\r\n89163 128733\r\n151226 75254\r\n", "output": "rrrrrrrrrrrrrrr\r\n"}, {"input": "9\r\n95316 68815\r\n95316 23738\r\n60801 169893\r\n84639 68815\r\n109462 87456\r\n22940 37614\r\n172202 151462\r\n84639 23738\r\n109462 151462\r\n", "output": "rbrbrrrrb\r\n"}, {"input": "2\r\n196356 153892\r\n134153 153892\r\n", "output": "rb\r\n"}, {"input": "10\r\n4126 18194\r\n143965 18194\r\n32687 18194\r\n118527 18194\r\n186573 18194\r\n97223 18194\r\n179697 18194\r\n175536 18194\r\n107767 18194\r\n127019 18194\r\n", "output": "bbbbrbrrrr\r\n"}, {"input": "1\r\n1 1\r\n", "output": "r\r\n"}, {"input": "1\r\n1000 3434\r\n", "output": "r\r\n"}, {"input": "1\r\n200000 200000\r\n", "output": "r\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "rr\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "br\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) points = [] for _ in range(n): x, y = map(int, f.readline().split()) points.append((x, y)) with open(submission_path, 'r') as f: s = f.read().strip() if len(s) != n: print(0) return for c in s: if c not in ('r', 'b'): print(0) return x_counts = {} y_counts = {} for (x, y), color in zip(points, s): if x not in x_counts: x_counts[x] = {'r': 0, 'b': 0} x_counts[x][color] += 1 if y not in y_counts: y_counts[y] = {'r': 0, 'b': 0} y_counts[y][color] += 1 for x in x_counts: r = x_counts[x]['r'] b = x_counts[x]['b'] if abs(r - b) > 1: print(0) return for y in y_counts: r = y_counts[y]['r'] b = y_counts[y]['b'] if abs(r - b) > 1: print(0) return print(1) if __name__ == "__main__": main()
true
58/C
58
C
PyPy 3
TESTS
5
280
0
91777469
n=int(input()) l=list(map(int,input().split())) x=n-1 ans=float('inf') for i in range((n>>1)): j=i k=i-1 front=l[i] back=l[i] c=0 while j<(n>>1): if l[j]==front: if l[x-j]!=front: c+=1 else: c+=1 if l[x-j]!=front: c+=1 j+=1 front+=1 while k>=0: if l[k]==back: if l[x-k]!=back: c+=1 else: c+=1 if l[x-k]!=back: c+=1 back-=1 k-=1 if n&1: if l[n>>1]!=front: c+=1 ans=min(ans,c) print(ans)
40
342
7,577,600
188863469
# LUOGU_RID: 99389245 n = int(input()) s = 0 t = [0]*100005 ms = list(map(int,input().split())) a = 0 for i in ms: a += 1 i -= min(a,n-a+1) if i >= 0: t[i-1] += 1 s = max(s,t[i-1]) print(n-s)
Codeforces Beta Round 54 (Div. 2)
CF
2,011
2
256
Trees
On Bertown's main street n trees are growing, the tree number i has the height of ai meters (1 ≤ i ≤ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should be equal to each other, the heights of the 2-nd and the (n - 1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not. Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful.
The first line contains integer n (1 ≤ n ≤ 105) which is the number of trees. The second line contains integers ai (1 ≤ ai ≤ 105) which are the heights of the trees.
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
null
null
[{"input": "3\n2 2 2", "output": "1"}, {"input": "4\n1 2 2 1", "output": "0"}]
1,800
["brute force"]
40
[{"input": "3\r\n2 2 2\r\n", "output": "1\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n61452 50974 73849\r\n", "output": "2\r\n"}, {"input": "4\r\n86002 1199 86003 86002\r\n", "output": "1\r\n"}, {"input": "5\r\n92605 92606 41969 98774 92605\r\n", "output": "2\r\n"}, {"input": "10\r\n1 1 2 3 4 4 3 2 1 10\r\n", "output": "9\r\n"}, {"input": "10\r\n73905 73906 73907 85732 73909 73909 73908 73907 73906 73905\r\n", "output": "1\r\n"}, {"input": "10\r\n60718 99414 65042 65043 65044 38495 95782 65042 65041 65040\r\n", "output": "4\r\n"}, {"input": "7\r\n1 2 3 7 6 5 4\r\n", "output": "3\r\n"}, {"input": "5\r\n4 5 6 5 1\r\n", "output": "1\r\n"}, {"input": "1\r\n100000\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "7\r\n1 2 3 7 6 5 4\r\n", "output": "3\r\n"}, {"input": "1\r\n2727\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 2 4\r\n", "output": "1\r\n"}, {"input": "9\r\n100 12 13 14 15 14 13 12 11\r\n", "output": "1\r\n"}, {"input": "5\r\n2 4 5 4 2\r\n", "output": "2\r\n"}, {"input": "5\r\n3 3 4 3 2\r\n", "output": "1\r\n"}]
false
stdio
null
true
730/G
730
G
Python 3
TESTS
6
46
204,800
21708503
import sys lines = iter(sys.stdin.read().splitlines()) next(lines) s,d = map(int,next(lines).split()) dates = [[0,0],[s, s + d -1],[1000000001,1000000001]] res = [[s, s + d -1]] for line in lines: s,d = map(int,line.split()) nhueco = True for i in range(len(dates)): if s > dates[i][1] and s+d-1 < dates[i+1][0]: dates.insert(i+1,[s, s + d -1]) res.append([s, s + d -1]) break elif nhueco and dates[i][1] + dates[i+1][0] -1 >= d: nhueco = False ld = dates[i][1] + 1 li = i+1 else: dates.insert(li,[ld, ld + d -1]) res.append([ld, ld + d -1]) for date in res: print(" ".join(map(str,date)))
28
1,232
0
49084217
n = int(input()) cur = [] def good(s, e): if s < 1: return False assert s <= e for l, r in cur: if max(l, s) <= min(r, e): return False return True for i in range(n): s, d = map(int, input().split()) e = s+d-1 if not good(s, e): s = int(2e9) if good(1, d): s = 1 for l, r in cur: if good(l-d, l-1): s = min(s, l-d) if good(r+1, r+d): s = min(s, r+1) cur.append((s, s+d-1)) cur.sort() print(s, s+d-1)
2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,016
2
512
Car Repair Shop
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came. The i-th request is characterized by two values: si — the day when a client wants to start the repair of his car, di — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows: - If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client. - Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + di - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + di - 1]. It is possible that the day x when repair is scheduled to start will be less than si. Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients. The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≤ si ≤ 109, 1 ≤ di ≤ 5·106), where si is the preferred time to start repairing the i-th car, di is the number of days to repair the i-th car. The requests should be processed in the order they are given in the input.
Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.
null
null
[{"input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7"}, {"input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000"}]
1,600
["implementation"]
28
[{"input": "3\r\n9 2\r\n7 3\r\n2 4\r\n", "output": "9 10\r\n1 3\r\n4 7\r\n"}, {"input": "4\r\n1000000000 1000000\r\n1000000000 1000000\r\n100000000 1000000\r\n1000000000 1000000\r\n", "output": "1000000000 1000999999\r\n1 1000000\r\n100000000 100999999\r\n1000001 2000000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 1\r\n"}, {"input": "1\r\n1000000000 1\r\n", "output": "1000000000 1000000000\r\n"}, {"input": "1\r\n1000000000 5000000\r\n", "output": "1000000000 1004999999\r\n"}, {"input": "5\r\n6 2\r\n10 1\r\n10 2\r\n9 2\r\n5 1\r\n", "output": "6 7\r\n10 10\r\n1 2\r\n3 4\r\n5 5\r\n"}, {"input": "10\r\n1 3\r\n77 8\r\n46 5\r\n83 4\r\n61 7\r\n8 4\r\n54 7\r\n80 7\r\n33 7\r\n13 4\r\n", "output": "1 3\r\n77 84\r\n46 50\r\n4 7\r\n61 67\r\n8 11\r\n54 60\r\n12 18\r\n33 39\r\n19 22\r\n"}, {"input": "10\r\n588 12\r\n560 10\r\n593 14\r\n438 15\r\n761 11\r\n984 6\r\n503 2\r\n855 19\r\n538 2\r\n650 7\r\n", "output": "588 599\r\n560 569\r\n1 14\r\n438 452\r\n761 771\r\n984 989\r\n503 504\r\n855 873\r\n538 539\r\n650 656\r\n"}, {"input": "20\r\n360 26\r\n475 17\r\n826 12\r\n815 23\r\n567 28\r\n897 26\r\n707 20\r\n1000 9\r\n576 5\r\n16 5\r\n714 16\r\n630 17\r\n426 26\r\n406 23\r\n899 25\r\n102 22\r\n896 8\r\n320 27\r\n964 25\r\n932 18\r\n", "output": "360 385\r\n475 491\r\n826 837\r\n1 23\r\n567 594\r\n897 922\r\n707 726\r\n1000 1008\r\n24 28\r\n29 33\r\n34 49\r\n630 646\r\n426 451\r\n50 72\r\n73 97\r\n102 123\r\n124 131\r\n320 346\r\n964 988\r\n932 949\r\n"}, {"input": "30\r\n522692116 84\r\n589719489 488\r\n662495181 961\r\n915956552 470\r\n683572975 271\r\n498400137 480\r\n327010963 181\r\n200704287 367\r\n810826488 54\r\n978100746 208\r\n345455616 986\r\n106372142 876\r\n446972337 42\r\n309349333 200\r\n93462198 543\r\n167946793 318\r\n325598940 427\r\n121873339 459\r\n174934933 598\r\n279521023 655\r\n739750520 3\r\n870850765 192\r\n622303167 400\r\n471234786 63\r\n805952711 18\r\n349834333 857\r\n804873364 302\r\n512746562 39\r\n533285962 561\r\n996718586 494\r\n", "output": "522692116 522692199\n589719489 589719976\n662495181 662496141\n915956552 915957021\n683572975 683573245\n498400137 498400616\n327010963 327011143\n200704287 200704653\n810826488 810826541\n978100746 978100953\n345455616 345456601\n106372142 106373017\n446972337 446972378\n309349333 309349532\n93462198 93462740\n167946793 167947110\n325598940 325599366\n121873339 121873797\n174934933 174935530\n279521023 279521677\n739750520 739750522\n870850765 870850956\n622303167 622303566\n471234786 471234848\n805952711 805952728\n349834333 349835189\n804873364 804873665\n512746562 512746600\n533285962 533286522\n996718586 996719079\n"}, {"input": "2\r\n10 3\r\n9 2\r\n", "output": "10 12\r\n1 2\r\n"}, {"input": "1\r\n1 5000000\r\n", "output": "1 5000000\r\n"}]
false
stdio
null
true
730/G
730
G
Python 3
TESTS
6
62
204,800
21808751
n=int(input()) p=1 L=[] m,M=0,0 for j in range(n): ch=input().split() s,d=int(ch[0]),int(ch[1]) if j==0: m=s M=s+d-1 print(s,d+s-1) L.append([s,s+d-1]) L.sort() else: B=True C=True for i in range(len(L)): if i<(len(L)-1) and s>L[i][1] and s+d-1<L[i+1][0]: print(s,s+d-1) L.append([s,s+d-1]) L.sort() C=False break if L[i][1]>=s>=L[i][0] or L[i][0]<=(s+d-1)<=L[i][1] or (s<=L[i][0] and (s+d-1)>=L[i][1]): B=False break if B and C: print(s,s+d-1) if s<m: m=s if (s+d-1)>M: M=s+d-1 L.append([s,s+d-1]) L.sort() elif not B and C: if p+d-1<m: print(p,p+d-1) L.append([p,p+d-1]) L.sort() p=p+d else: print(M+1,M+d) L.append([M+1,M+d]) L.sort() M=M+d
28
77
307,200
21705313
from bisect import bisect_left, insort_left a = [] n = int(input()) for _ in range(n): #print(a) s, d = map(int, input().split()) if len(a) == 0: print(s, s+d - 1) a.append((s, s + d - 1)) continue p = bisect_left(a, (s, s + d - 1)) #print('p', p) ok = True if p > 0 and a[p-1][1] >= s: ok = False if p < len(a) and a[p][0] <= s + d - 1: ok = False if ok: insort_left(a, (s, s + d - 1)) print(s, s + d - 1) else: ok = False for i in range(len(a)): if i == 0: if a[0][0] > d: print(1,d) a = [(1, d)] + a ok = True break else: if a[i - 1][1] + d < a[i][0]: print(a[i - 1][1] + 1, a[i - 1][1] + d) insort_left(a, (a[i - 1][1] + 1, a[i - 1][1] + d)) ok = True break if not ok: print(a[-1][1] + 1, a[-1][1] + d) insort_left(a, (a[-1][1] + 1, a[-1][1] + d))
2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,016
2
512
Car Repair Shop
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came. The i-th request is characterized by two values: si — the day when a client wants to start the repair of his car, di — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows: - If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client. - Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + di - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + di - 1]. It is possible that the day x when repair is scheduled to start will be less than si. Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients. The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≤ si ≤ 109, 1 ≤ di ≤ 5·106), where si is the preferred time to start repairing the i-th car, di is the number of days to repair the i-th car. The requests should be processed in the order they are given in the input.
Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.
null
null
[{"input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7"}, {"input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000"}]
1,600
["implementation"]
28
[{"input": "3\r\n9 2\r\n7 3\r\n2 4\r\n", "output": "9 10\r\n1 3\r\n4 7\r\n"}, {"input": "4\r\n1000000000 1000000\r\n1000000000 1000000\r\n100000000 1000000\r\n1000000000 1000000\r\n", "output": "1000000000 1000999999\r\n1 1000000\r\n100000000 100999999\r\n1000001 2000000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 1\r\n"}, {"input": "1\r\n1000000000 1\r\n", "output": "1000000000 1000000000\r\n"}, {"input": "1\r\n1000000000 5000000\r\n", "output": "1000000000 1004999999\r\n"}, {"input": "5\r\n6 2\r\n10 1\r\n10 2\r\n9 2\r\n5 1\r\n", "output": "6 7\r\n10 10\r\n1 2\r\n3 4\r\n5 5\r\n"}, {"input": "10\r\n1 3\r\n77 8\r\n46 5\r\n83 4\r\n61 7\r\n8 4\r\n54 7\r\n80 7\r\n33 7\r\n13 4\r\n", "output": "1 3\r\n77 84\r\n46 50\r\n4 7\r\n61 67\r\n8 11\r\n54 60\r\n12 18\r\n33 39\r\n19 22\r\n"}, {"input": "10\r\n588 12\r\n560 10\r\n593 14\r\n438 15\r\n761 11\r\n984 6\r\n503 2\r\n855 19\r\n538 2\r\n650 7\r\n", "output": "588 599\r\n560 569\r\n1 14\r\n438 452\r\n761 771\r\n984 989\r\n503 504\r\n855 873\r\n538 539\r\n650 656\r\n"}, {"input": "20\r\n360 26\r\n475 17\r\n826 12\r\n815 23\r\n567 28\r\n897 26\r\n707 20\r\n1000 9\r\n576 5\r\n16 5\r\n714 16\r\n630 17\r\n426 26\r\n406 23\r\n899 25\r\n102 22\r\n896 8\r\n320 27\r\n964 25\r\n932 18\r\n", "output": "360 385\r\n475 491\r\n826 837\r\n1 23\r\n567 594\r\n897 922\r\n707 726\r\n1000 1008\r\n24 28\r\n29 33\r\n34 49\r\n630 646\r\n426 451\r\n50 72\r\n73 97\r\n102 123\r\n124 131\r\n320 346\r\n964 988\r\n932 949\r\n"}, {"input": "30\r\n522692116 84\r\n589719489 488\r\n662495181 961\r\n915956552 470\r\n683572975 271\r\n498400137 480\r\n327010963 181\r\n200704287 367\r\n810826488 54\r\n978100746 208\r\n345455616 986\r\n106372142 876\r\n446972337 42\r\n309349333 200\r\n93462198 543\r\n167946793 318\r\n325598940 427\r\n121873339 459\r\n174934933 598\r\n279521023 655\r\n739750520 3\r\n870850765 192\r\n622303167 400\r\n471234786 63\r\n805952711 18\r\n349834333 857\r\n804873364 302\r\n512746562 39\r\n533285962 561\r\n996718586 494\r\n", "output": "522692116 522692199\n589719489 589719976\n662495181 662496141\n915956552 915957021\n683572975 683573245\n498400137 498400616\n327010963 327011143\n200704287 200704653\n810826488 810826541\n978100746 978100953\n345455616 345456601\n106372142 106373017\n446972337 446972378\n309349333 309349532\n93462198 93462740\n167946793 167947110\n325598940 325599366\n121873339 121873797\n174934933 174935530\n279521023 279521677\n739750520 739750522\n870850765 870850956\n622303167 622303566\n471234786 471234848\n805952711 805952728\n349834333 349835189\n804873364 804873665\n512746562 512746600\n533285962 533286522\n996718586 996719079\n"}, {"input": "2\r\n10 3\r\n9 2\r\n", "output": "10 12\r\n1 2\r\n"}, {"input": "1\r\n1 5000000\r\n", "output": "1 5000000\r\n"}]
false
stdio
null
true
493/A
493
A
Python 3
TESTS
8
109
0
61548468
team = {"h" : input(), "a" : input()} n = int(input()) d = {} for x in range(n): t, ha, num, card = input().split() if (ha,num) not in d: d[(ha,num)] = (t,card) else: if d[(ha,num)][1] == "y": d[(ha,num)] = (t,"r") lis = d.items() nlis = [x for x in lis if x[1][1] == "r"] nlis.sort(key=lambda x: x[1][0]) for x in nlis: print(team[x[0][0]], x[0][1], x[1][0])
18
61
0
16709746
teams = dict() teams['h'] = input() teams['a'] = input() n = int(input()) data = {} report = [] for _ in range(n): time, team, num, card = input().split() name = team+num if name not in data: data[name] = card if card == 'r': report.append("{} {} {}".format(teams[team], num, time)) elif data[name] == 'y': report.append("{} {} {}".format(teams[team], num, time)) data[name] = 'r' print('\n'.join(report))
Codeforces Round 281 (Div. 2)
CF
2,014
2
256
Vasya and Football
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card. Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct. Next follows number n (1 ≤ n ≤ 90) — the number of fouls. Each of the following n lines contains information about a foul in the following form: - first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs; - then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player; - then goes the player's number m (1 ≤ m ≤ 99); - then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given. The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
For each event when a player received his first red card in a chronological order print a string containing the following information: - The name of the team to which the player belongs; - the player's number in his team; - the minute when he received the card. If no player received a card, then you do not need to print anything. It is possible case that the program will not print anything to the output (if there were no red cards).
null
null
[{"input": "MC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r", "output": "MC 25 70\nMC 42 82\nCSKA 13 90"}]
1,300
["implementation"]
18
[{"input": "MC\r\nCSKA\r\n9\r\n28 a 3 y\r\n62 h 25 y\r\n66 h 42 y\r\n70 h 25 y\r\n77 a 4 y\r\n79 a 25 y\r\n82 h 42 r\r\n89 h 16 y\r\n90 a 13 r\r\n", "output": "MC 25 70\r\nMC 42 82\r\nCSKA 13 90\r\n"}, {"input": "REAL\r\nBARCA\r\n3\r\n27 h 7 y\r\n44 a 10 y\r\n87 h 3 r\r\n", "output": "REAL 3 87\r\n"}, {"input": "MASFF\r\nSAFBDSRG\r\n5\r\n1 h 1 y\r\n15 h 1 r\r\n27 a 1 y\r\n58 a 1 y\r\n69 h 10 y\r\n", "output": "MASFF 1 15\r\nSAFBDSRG 1 58\r\n"}, {"input": "ARMENIA\r\nBULGARIA\r\n12\r\n33 h 17 y\r\n42 h 21 y\r\n56 a 17 y\r\n58 a 6 y\r\n61 a 7 y\r\n68 a 10 y\r\n72 h 13 y\r\n73 h 21 y\r\n74 a 8 r\r\n75 a 4 y\r\n77 a 10 y\r\n90 a 23 y\r\n", "output": "ARMENIA 21 73\r\nBULGARIA 8 74\r\nBULGARIA 10 77\r\n"}, {"input": "PORTUGAL\r\nNETHERLANDS\r\n16\r\n2 a 18 y\r\n7 a 3 y\r\n20 h 18 y\r\n31 h 6 y\r\n45 h 6 y\r\n50 h 8 y\r\n59 a 5 y\r\n60 h 7 y\r\n63 a 3 y\r\n72 a 20 y\r\n73 h 20 y\r\n74 a 10 y\r\n75 h 1 y\r\n76 h 14 y\r\n78 h 20 y\r\n90 a 5 y\r\n", "output": "PORTUGAL 6 45\r\nNETHERLANDS 3 63\r\nPORTUGAL 20 78\r\nNETHERLANDS 5 90\r\n"}, {"input": "TANC\r\nXNCOR\r\n2\r\n15 h 27 r\r\n28 h 27 r\r\n", "output": "TANC 27 15\r\n"}, {"input": "ASGDFJH\r\nAHGRSDXGER\r\n3\r\n23 h 15 r\r\n68 h 15 y\r\n79 h 15 y\r\n", "output": "ASGDFJH 15 23\r\n"}, {"input": "ASFSHDSG\r\nADGYRTJNG\r\n5\r\n1 h 1 y\r\n2 h 1 y\r\n3 h 1 y\r\n4 h 1 r\r\n5 h 1 y\r\n", "output": "ASFSHDSG 1 2\r\n"}, {"input": "A\r\nB\r\n42\r\n5 a 84 y\r\n8 h 28 r\r\n10 a 9 r\r\n11 h 93 y\r\n13 a 11 r\r\n15 h 3 r\r\n20 a 88 r\r\n23 a 41 y\r\n25 a 14 y\r\n27 a 38 r\r\n28 a 33 y\r\n29 h 66 r\r\n31 a 16 r\r\n32 a 80 y\r\n34 a 54 r\r\n35 a 50 y\r\n36 a 9 y\r\n39 a 22 y\r\n42 h 81 y\r\n43 a 10 y\r\n44 a 27 r\r\n47 h 39 y\r\n48 a 80 y\r\n50 h 5 y\r\n52 a 67 y\r\n54 h 63 y\r\n56 h 7 y\r\n57 h 44 y\r\n58 h 41 y\r\n61 h 32 y\r\n64 h 91 y\r\n67 a 56 y\r\n69 h 83 y\r\n71 h 59 y\r\n72 a 76 y\r\n75 h 41 y\r\n76 a 49 r\r\n77 a 4 r\r\n78 a 69 y\r\n79 a 96 r\r\n80 h 81 y\r\n86 h 85 r\r\n", "output": "A 28 8\r\nB 9 10\r\nB 11 13\r\nA 3 15\r\nB 88 20\r\nB 38 27\r\nA 66 29\r\nB 16 31\r\nB 54 34\r\nB 27 44\r\nB 80 48\r\nA 41 75\r\nB 49 76\r\nB 4 77\r\nB 96 79\r\nA 81 80\r\nA 85 86\r\n"}, {"input": "ARM\r\nAZE\r\n45\r\n2 a 13 r\r\n3 a 73 r\r\n4 a 10 y\r\n5 h 42 y\r\n8 h 56 y\r\n10 h 15 y\r\n11 a 29 r\r\n13 a 79 y\r\n14 a 77 r\r\n18 h 7 y\r\n20 a 69 r\r\n22 h 19 y\r\n25 h 88 r\r\n26 a 78 y\r\n27 a 91 r\r\n28 h 10 r\r\n30 h 13 r\r\n31 a 26 r\r\n33 a 43 r\r\n34 a 91 y\r\n40 h 57 y\r\n44 h 18 y\r\n46 a 25 r\r\n48 a 29 y\r\n51 h 71 y\r\n57 a 16 r\r\n58 h 37 r\r\n59 h 92 y\r\n60 h 11 y\r\n61 a 88 y\r\n64 a 28 r\r\n65 h 71 r\r\n68 h 39 y\r\n70 h 8 r\r\n71 a 10 y\r\n72 a 32 y\r\n73 h 95 r\r\n74 a 33 y\r\n75 h 48 r\r\n78 a 44 y\r\n79 a 22 r\r\n80 h 50 r\r\n84 a 50 y\r\n88 a 90 y\r\n89 h 42 r\r\n", "output": "AZE 13 2\r\nAZE 73 3\r\nAZE 29 11\r\nAZE 77 14\r\nAZE 69 20\r\nARM 88 25\r\nAZE 91 27\r\nARM 10 28\r\nARM 13 30\r\nAZE 26 31\r\nAZE 43 33\r\nAZE 25 46\r\nAZE 16 57\r\nARM 37 58\r\nAZE 28 64\r\nARM 71 65\r\nARM 8 70\r\nAZE 10 71\r\nARM 95 73\r\nARM 48 75\r\nAZE 22 79\r\nARM 50 80\r\nARM 42 89\r\n"}, {"input": "KASFLS\r\nASJBGGDLJFDDFHHTHJH\r\n42\r\n2 a 68 y\r\n4 h 64 r\r\n5 a 24 y\r\n6 h 20 r\r\n8 a 16 r\r\n9 a 96 y\r\n10 h 36 r\r\n12 a 44 y\r\n13 h 69 r\r\n16 a 62 r\r\n18 a 99 r\r\n20 h 12 r\r\n21 a 68 y\r\n25 h 40 y\r\n26 h 54 r\r\n28 h 91 r\r\n29 a 36 r\r\n33 a 91 y\r\n36 h 93 r\r\n37 h 60 r\r\n38 a 82 r\r\n41 a 85 y\r\n42 a 62 r\r\n46 a 22 r\r\n48 a 88 r\r\n49 a 8 r\r\n51 h 45 y\r\n54 a 84 y\r\n57 a 8 y\r\n59 h 24 y\r\n61 h 22 r\r\n64 h 11 r\r\n69 a 89 y\r\n72 h 44 r\r\n75 h 57 r\r\n76 h 80 y\r\n77 h 54 r\r\n79 a 1 y\r\n81 a 31 r\r\n82 h 8 y\r\n83 a 28 r\r\n86 h 56 y\r\n", "output": "KASFLS 64 4\r\nKASFLS 20 6\r\nASJBGGDLJFDDFHHTHJH 16 8\r\nKASFLS 36 10\r\nKASFLS 69 13\r\nASJBGGDLJFDDFHHTHJH 62 16\r\nASJBGGDLJFDDFHHTHJH 99 18\r\nKASFLS 12 20\r\nASJBGGDLJFDDFHHTHJH 68 21\r\nKASFLS 54 26\r\nKASFLS 91 28\r\nASJBGGDLJFDDFHHTHJH 36 29\r\nKASFLS 93 36\r\nKASFLS 60 37\r\nASJBGGDLJFDDFHHTHJH 82 38\r\nASJBGGDLJFDDFHHTHJH 22 46\r\nASJBGGDLJFDDFHHTHJH 88 48\r\nASJBGGDLJFDDFHHTHJH 8 49\r\nKASFLS 22 61\r\nKASFLS 11 64\r\nKASFLS 44 72\r\nKASFLS 57 75\r\nASJBGGDLJFDDFHHTHJH 31 81\r\nASJBGGDLJFDDFHHTHJH 28 83\r\n"}, {"input": "A\r\nAA\r\n2\r\n1 a 1 y\r\n2 h 1 y\r\n", "output": ""}, {"input": "AB\r\nBC\r\n3\r\n1 h 1 y\r\n2 h 1 y\r\n3 h 1 r\r\n", "output": "AB 1 2\r\n"}]
false
stdio
null
true
298/B
298
B
Python 3
TESTS
30
404
1,126,400
15184842
from functools import reduce from operator import * from math import * from sys import * from string import * from collections import * setrecursionlimit(10**7) dX= [-1, 1, 0, 0,-1, 1,-1, 1] dY= [ 0, 0,-1, 1, 1,-1,-1, 1] RI=lambda: list(map(int,input().split())) RS=lambda: input().rstrip().split() ################################################# t,sx,sy,ex,ey=RI() needed={c:0 for c in 'NSEW'} if sx<ex: needed['E']=ex-sx else: needed['W']=sx-ex if sy<ey: needed['N']=ey-sy else: needed['S']=sy-ey cnt={c:0 for c in 'NSEW'} s=RS()[0] for i in range(t): flag=1 if needed[s[i]]: cnt[s[i]]+=1 for c in cnt: if cnt[c]!=needed[c]: flag=0 if flag: print(i+1) exit(0) print(-1)
43
122
102,400
205879989
m,a,ay,b,by=map(int,input().split());s=input()+'0' x='W'if a>b else 'E' y='S'if ay>by else 'N' c,d=abs(a-b),abs(ay-by) for i in range(m): if s[i]==x and c>0:c-=1 if s[i]==y and d>0:d-=1 if c==d==0:print(i+1);break else:print(-1)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
429/B
429
B
PyPy 3
TESTS
2
140
22,630,400
115454700
from collections import deque, defaultdict from math import sqrt, ceil, factorial, floor, inf, log2, sqrt import bisect import copy from itertools import combinations import sys def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() arr=[] n,m=get_ints() for i in range(n): a=get_array() arr.append(a) ##starting from (1,1) dp=[[0 for i in range(m)]for j in range(n)] ##starting from (1,n) dp1=[[0 for i in range(m)]for j in range(n)] dp2=[[0 for i in range(m)]for j in range(n)] dp3=[[0 for i in range(m)]for j in range(n)] ## startin from top left dp[0][0]=arr[0][0] for i in range(1,m): dp[0][i]+=dp[0][i-1]+arr[0][i] for j in range(1,n): dp[j][0]+=dp[j-1][0]+arr[j][0] for i in range(1,n): for j in range(1,m): dp[i][j]+=max(dp[i-1][j],dp[i][j-1])+arr[i][j] ##starting from bottom left dp1[n-1][0]=arr[n-1][0] for i in range(1,m): dp1[n-1][i]+=dp1[n-1][i-1]+arr[n-1][i] for i in range(n-2,-1,-1): dp1[i][0]+=dp1[i+1][0]+arr[i][0] for i in range(n-2,-1,-1): for j in range(1,m): dp1[i][j]+=max(dp1[i+1][j],dp1[i][j-1])+arr[i][j] ##starting from bottom right(for A) dp2[n-1][m-1]=arr[n-1][m-1] for i in range(m-2,-1,-1): dp2[n-1][i]+=dp2[n-1][i+1]+arr[n-1][i] for i in range(n-2,-1,-1): dp2[i][m-1]+=dp2[i+1][m-1]+arr[i][m-1] for i in range(n-2,-1,-1): for j in range(m-2,-1,-1): dp2[i][j]=max(dp2[i+1][j],dp2[i][j+1]) dp3[0][m-1]=arr[0][m-1] ## starting from top right.(for B) for i in range(m-2,-1,-1): dp3[0][i]+=dp3[0][i+1]+arr[0][i] for i in range(1,n): dp3[i][m-1]+=dp3[i-1][m-1]+arr[i][m-1] for i in range(1,n): for j in range(m-2,-1,-1): dp3[i][j]=max(dp3[i-1][j],dp3[i][j+1])+arr[i][j] ans=-1 for i in range(n): for j in range(m): maxi,maxi1=-1,-1 ##A top and B left if i-1>=0 and j-1>=0 and i+1<n and j+1<m: maxi, maxi1 = -1, -1 maxi=max(maxi,dp[i-1][j]+dp1[i][j-1]) maxi1 = max(maxi1, dp2[i + 1][j] + dp3[i][j + 1]) ans=max(ans,maxi+maxi1) if i+1<n and j-1>=0 and j+1<m and i-1>=0: ## A left and B bottom maxi, maxi1 = -1, -1 maxi=max(maxi,dp[i][j-1]+dp1[i+1][j]) maxi1 = max(maxi1, dp2[i][j + 1] + dp3[i - 1][j]) ans=max(ans,maxi+maxi1) ##print(i,j,maxi,maxi1) ##ans=max(ans,maxi+maxi1) print(ans)
30
327
65,536,000
199930474
import sys input = sys.stdin.readline def f(i1, i2, i3, j1, j2, j3): w = [[0]*(m+2) for _ in range(n+2)] for i in range(i1, i2, i3): for j in range(j1, j2, j3): w[i][j] = max(w[i-i3][j], w[i][j-j3]) + g[i-1][j-1] return w n, m = map(int, input().split()) g = [list(map(int, input().split())) for _ in range(n)] a1 = f(1, n+1, 1, 1, m+1, 1) a1, a2, b1, b2 = f(1, n+1, 1, 1, m+1, 1), f(n, 0, -1, m, 0, -1), f(n, 0, -1, 1, m+1, 1), f(1, n+1, 1, m, 0, -1) c = 0 q = [[0]*(m+2) for _ in range(n+2)] for i in range(2, n): for j in range(2, m): c = max(c, a1[i-1][j] + a2[i+1][j] + b1[i][j-1] + b2[i][j+1], a1[i][j-1] + a2[i][j+1] + b1[i+1][j] + b2[i-1][j]) print(c)
Codeforces Round 245 (Div. 1)
CF
2,014
2
256
Working out
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column. Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout. If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
The output contains a single number — the maximum total gain possible.
null
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
[{"input": "3 3\n100 100 100\n100 1 100\n100 100 100", "output": "800"}]
1,600
["dp"]
30
[{"input": "3 3\r\n100 100 100\r\n100 1 100\r\n100 100 100\r\n", "output": "800"}, {"input": "4 5\r\n87882 40786 3691 85313 46694\r\n28884 16067 3242 97367 78518\r\n4250 35501 9780 14435 19004\r\n64673 65438 56977 64495 27280\r\n", "output": "747898"}, {"input": "3 3\r\n3 1 2\r\n3 2 0\r\n2 3 2\r\n", "output": "16"}, {"input": "3 3\r\n1 10 1\r\n1 10 1\r\n1 10 1\r\n", "output": "26"}, {"input": "3 3\r\n0 0 0\r\n0 10000 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n1 1 1\r\n0 10000 0\r\n1 1 1\r\n", "output": "6"}, {"input": "3 3\r\n9 0 9\r\n0 9 9\r\n9 9 9\r\n", "output": "54"}, {"input": "3 3\r\n0 0 0\r\n0 100 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n100000 100000 100000\r\n1 100000 100000\r\n1 1 100000\r\n", "output": "500003"}, {"input": "3 3\r\n100 0 100\r\n1 100 100\r\n0 100 100\r\n", "output": "501"}]
false
stdio
null
true
298/B
298
B
PyPy 3-64
TESTS
15
124
1,843,200
216531827
t, x, y, xx, yy = map(int, input().split()) string = input() count = 0 for i in string: if xx > x: if i == "E": x+=1 count += 1 elif xx < x: if i == "W": x-=1 count += 1 elif yy < y: if i == "S": y -= 1 count += 1 elif yy > y: if i == "N": y += 1 count += 1 # print((x, y), " ", (xx, yy)) if x==xx and yy == y:print(count) else: print(-1)
43
122
102,400
221732850
def solve(): t,s,s1,e,e1=map(int,input().split()) di=input() to=0 for i in di: if(s==e and s1==e1): print(to) return if(s>e): if i=='W': s-=1 elif(s<e): if i=='E': s+=1 if(s1>e1): if i=='S': s1-=1 elif s1<e1: if i=='N': s1+=1 to+=1 print(to) if(s==e and s1==e1) else print(-1) if __name__ == '__main__': solve()
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
297/A
297
A
Python 3
TESTS
9
218
0
42464327
a=input() b=input() acount=0 bcount=0 for i in range(len(b), 0, -1): if(b[i-1]=='0'): bcount+=1 else: break if (bcount != 0): b=b[:-bcount] for i in range(len(a), 0, -1): if(a[i-1]=='0'): acount+=1 else: break if (acount != 0): a=a[:-acount] if (b == ''): print('YES') elif(b.count('1')%2==0): if(b==(a[-len(b):]) or b[:-1]==(a[-len(b)-1:])): print('YES') else: print('NO') else: if(b==(a[-len(b):]) and (acount==bcount)): print('YES') else: print('NO')
79
124
0
11884627
a = (input().count('1') + 1) // 2 b = (input().count('1') + 1) // 2 print('YES' if a >= b else 'NO')
Codeforces Round 180 (Div. 1)
CF
2,013
1
256
Parity Game
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: - Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$. - Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b? The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
null
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
1,700
["constructive algorithms"]
79
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
false
stdio
null
true
984/A
984
A
PyPy 3-64
TESTS
5
46
1,638,400
220942154
a = int(input()) d = input().split() c = 0 p = a while a>1: n = max(d) d.remove(n) c+=1 a-=1 if c!=p-1: l = min(d) d.remove(l) c+=1 a-=1 print(*d)
35
46
0
135926021
n = int(input()) x = list(map(int, input().split())) x = sorted(x) if n % 2 == 1: print(x[n//2]) elif n % 2 == 0: print(x[n//2-1])
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns. The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it. You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "output": "124"}, {"input": "2\r\n2 1\r\n", "output": "1"}, {"input": "3\r\n1 1 1000\r\n", "output": "1"}, {"input": "2\r\n322 322\r\n", "output": "322"}, {"input": "3\r\n9 92 12\r\n", "output": "12"}, {"input": "3\r\n1 2 2\r\n", "output": "2"}]
false
stdio
null
true
298/B
298
B
Python 3
TESTS
30
466
204,800
44844213
BLOW = {'E': [1, 0], 'S': [0, -1], 'W': [-1, 0], 'N': [0, 1]} t, x, y, a, b = map(int, input().split()) wind = input() go = [] if x < a: go.append('E') if x > a: go.append('W') if y < b: go.append('N') if y > b: go.append('S') position = [x, y] found = False for i in range(t): if wind[i] in go: position = list(map(sum, zip(position, BLOW[wind[i]]))) if position == [a, b]: found = True print(i + 1) break if not found: print(-1)
43
122
921,600
219378432
t, sx, sy, ex, ey = map(int,input().split()) l = list(input()) x = ex - sx y = ey - sy xP = "" yP = "" if x > 0: xP = "E" else: x = abs(x) xP = "W" if y > 0: yP = "N" else: y = abs(y) yP = "S" loc = -1 for i,a in enumerate(l): if a == xP and x > 0 : x -= 1 if a == yP and y > 0 : y -= 1 if x == 0 and y == 0 : loc = i + 1 break print(loc)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
297/A
297
A
PyPy 3-64
TESTS
23
186
512,000
142825339
a=input() b=input() l=len(b) a1=a.count('1') b1=b.count('1') r=0 c=False for i in b: if(i=='0'): if((a1+r)%2==0): l-=1 else: a1-=1 l-=1 if(a1==0 and r!=b1): c=True break else: if((a1+r)%2==0): a1-=1 r+=1 l-=1 if(a1==0 and r!=b1): c=True break else: r+=1 l-=1 if(c): print("NO") quit() if(a1>=0 and l==0): print("YES") else: print("NO")
79
124
0
18871802
def main(): a, b = (input().count('1') for _ in "ab") print(("NO", "YES")[a + (a & 1) >= b]) if __name__ == '__main__': main()
Codeforces Round 180 (Div. 1)
CF
2,013
1
256
Parity Game
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: - Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$. - Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b? The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
null
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
1,700
["constructive algorithms"]
79
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
false
stdio
null
true
298/B
298
B
PyPy 3-64
TESTS
15
124
1,638,400
209295662
n,sx,sy,ex,ey=list(map(int,input().split())) r=input() a,b=sx-ex,sy-ey flag1,flag2,flag3,flag4=0,0,0,0 if a==0: flag1=1 if b==0:flag2=1 if a>0:flag3=1 if b>0 :flag4=1 i=0 if flag1 and flag2: print(0) elif flag1 and flag4: while i<n and b!=0: if r[i]=="S": b-=1 i+=1 elif flag1 and not flag4: while i<n and b!=0: if r[i]=="N": b+=1 i+=1 elif flag2 and flag3: while i<n and a!=0: if r[i]=="W": a-=1 i+=1 elif flag2 and not flag3: while i<n and a!=0: if r[i]=="E": a+=1 i+=1 elif flag3 and flag4: while i<n and a+b!=0: if a>0 and r[i]=="W": a-=1 elif b>0 and r[i]=="S": b-=1 i+=1 elif flag3 and not flag4: while i<n and a>-1 and b<1: if a>0 and r[i]=="W": a-=1 elif b<0 and r[i]=="N": b-=1 i+=1 elif flag4 and not flag3: while i<n and a<1 and b>-1: if a<1 and r[i]=="E": a+=1 elif b>0 and r[i]=="S": b-=1 i+=1 else: while i<n and a+b!=0: if a<0 and r[i]=="E": a+=1 elif b<0 and r[i]=="N": b+=1 i+=1 if a+b!=0: print(-1) else: print(i)
43
124
102,400
220329146
def timing(): t, sx, sy, ex, ey = map(int, input().split()) s = input() for x in range(len(s)): if s[x] == "E" and ex > sx: sx = sx + 1 sy = sy if s[x] == "S" and ey < sy: sx = sx sy = sy - 1 if s[x] == "W" and ex < sx: sx = sx - 1 sy = sy if s[x] == "N" and ey > sy: sx = sx sy = sy + 1 if ex == sx and ey == sy: return x + 1 return -1 print(timing())
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
297/A
297
A
Python 3
TESTS
10
216
0
42463964
a=input() b=input() count=0 for i in range(len(b), 0, -1): if(b[i-1]=='0'): count+=1 else: break if (count != 0): b=b[:-count] count=0 for i in range(len(a), 0, -1): if(a[i-1]=='0'): count+=1 else: break if (count != 0): a=a[:-count] if (b == ''): print('YES') elif(b.count('1')%2==0): if(b==(a[-len(b):]) or b[:-1]==(a[-len(b)-1:])): print('YES') else: print('NO') else: if(b==(a[-len(b):])): print('YES') else: print('NO')
79
124
0
30093905
a=input() print('YES'if a.count('1')+(a.count('1')&1)>=input().count('1')else'NO')
Codeforces Round 180 (Div. 1)
CF
2,013
1
256
Parity Game
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: - Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$. - Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b? The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
null
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
1,700
["constructive algorithms"]
79
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
false
stdio
null
true
429/B
429
B
Python 3
TESTS
2
109
0
45491862
n, m = map(int, input().split()) a = [[int(x) for x in input().split()] for _ in range(n)] tmp1 = [[None] * m for _ in range(n)] tmp1[n - 1][m - 1] = a[n - 1][m - 1] for i in range(n - 2, -1, -1): tmp1[i][m - 1] = a[i][m - 1] + tmp1[i + 1][m - 1] for j in range(m - 2, -1, -1): tmp1[n - 1][j] = a[n - 1][j] + tmp1[n - 1][j + 1] for i in range(n - 2, -1, -1): for j in range(m - 2, -1, -1): tmp1[i][j] = max(tmp1[i + 1][j], tmp1[i][j + 1]) + a[i][j] tmp2 = [[None] * m for _ in range(n)] tmp2[0][0] = a[0][0] for i in range(1, n): tmp2[i][0] = a[i][0] + tmp2[i - 1][0] for j in range(1, m): tmp2[0][j] = a[0][j] + tmp2[0][j - 1] for i in range(1, n): for j in range(1, m): tmp2[i][j] = max(tmp2[i - 1][j], tmp2[i][j - 1]) + a[i][j] tmp3 = [[None] * m for _ in range(n)] tmp3[n - 1][0] = a[n - 1][0] for i in range(n - 2, -1, -1): tmp3[i][0] = a[i][0] + tmp3[i + 1][0] for j in range(1, m): tmp3[n - 1][j] = a[n - 1][j] + tmp3[n - 1][j - 1] for i in range(n - 2, -1, -1): for j in range(1, m): tmp3[i][j] = max(tmp3[i + 1][j], tmp3[i][j - 1]) + a[i][j] tmp4 = [[None] * m for _ in range(n)] tmp4[0][m - 1] = a[0][m - 1] for i in range(1, n): tmp4[i][m - 1] = a[i][m - 1] + tmp4[i - 1][m - 1] for j in range(m - 2, -1, -1): tmp4[0][j] = a[0][j] + tmp4[0][j + 1] for i in range(1, n): for j in range(m - 2, -1, -1): tmp4[i][j] = max(tmp4[i - 1][j], tmp4[i][j + 1]) + a[i][j] res = 0 for i in range(1, n - 1): for j in range(1, m - 1): res1 = tmp2[i - 1][j] + \ tmp3[i][j - 1] + \ tmp1[i + 1][j] + \ tmp4[i][j + 1] res2 = tmp2[i][j - 1] + \ tmp3[n - 1][0] + \ tmp1[i][j + 1] + \ tmp4[i - 1][j] res = max(res, res1, res2) print(res)
30
358
122,777,600
146702661
import os, sys from io import BytesIO, IOBase from math import log2, ceil, sqrt, gcd from _collections import deque import heapq as hp from bisect import bisect_left, bisect_right from math import cos, sin from itertools import permutations from operator import itemgetter sys.setrecursionlimit(5 * 10 ** 4) 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") pi = 3.1415926535 mod = 10 ** 9 + 7 n,m = list(map(int,input().split())) a = [list(map(int,input().split())) for _ in range(n)] dp1 = [[0] * (m + 2) for _ in range(n + 2)] dp2 = [[0] * (m + 2) for _ in range(n + 2)] dp3 = [[0] * (m + 2) for _ in range(n + 2)] dp4 = [[0] * (m + 2) for _ in range(n + 2)] for i in range(1, n + 1): for j in range(1, m + 1): dp1[i][j] = a[i - 1][j - 1] + max(dp1[i - 1][j], dp1[i][j - 1]) for i in range(n, 0, -1): for j in range(m, 0, -1): dp2[i][j] = a[i - 1][j - 1] + max(dp2[i + 1][j], dp2[i][j + 1]) for i in range(n, 0, -1): for j in range(1, m + 1): dp3[i][j] = a[i - 1][j - 1] + max(dp3[i + 1][j], dp3[i][j - 1]) for i in range(1, n + 1): for j in range(m, 0, -1): dp4[i][j] = a[i - 1][j - 1] + max(dp4[i - 1][j], dp4[i][j + 1]) ans = 0 for i in range(2, n): for j in range(2, m): ans = max(ans, max(dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1] , dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) ) print(ans)
Codeforces Round 245 (Div. 1)
CF
2,014
2
256
Working out
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column. Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout. If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
The output contains a single number — the maximum total gain possible.
null
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
[{"input": "3 3\n100 100 100\n100 1 100\n100 100 100", "output": "800"}]
1,600
["dp"]
30
[{"input": "3 3\r\n100 100 100\r\n100 1 100\r\n100 100 100\r\n", "output": "800"}, {"input": "4 5\r\n87882 40786 3691 85313 46694\r\n28884 16067 3242 97367 78518\r\n4250 35501 9780 14435 19004\r\n64673 65438 56977 64495 27280\r\n", "output": "747898"}, {"input": "3 3\r\n3 1 2\r\n3 2 0\r\n2 3 2\r\n", "output": "16"}, {"input": "3 3\r\n1 10 1\r\n1 10 1\r\n1 10 1\r\n", "output": "26"}, {"input": "3 3\r\n0 0 0\r\n0 10000 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n1 1 1\r\n0 10000 0\r\n1 1 1\r\n", "output": "6"}, {"input": "3 3\r\n9 0 9\r\n0 9 9\r\n9 9 9\r\n", "output": "54"}, {"input": "3 3\r\n0 0 0\r\n0 100 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n100000 100000 100000\r\n1 100000 100000\r\n1 1 100000\r\n", "output": "500003"}, {"input": "3 3\r\n100 0 100\r\n1 100 100\r\n0 100 100\r\n", "output": "501"}]
false
stdio
null
true
298/B
298
B
PyPy 3-64
TESTS
15
124
1,945,600
216540243
t, x, y, xx, yy = map(int, input().split()) string = input() count = 0 time = 0 n = 0 for i in (string): if xx > x: if i == "E": x+=1 count += 1 elif xx < x: if i == "W": x-=1 count += 1 elif yy < y: if i == "S": y -= 1 count += 1 elif yy > y: if i == "N": y += 1 count += 1 time += 1 n = time + 1 if x == xx and yy == y: break # print((x, y), " ", (xx, yy)) if x==xx and yy == y:print(time) else: print(-1)
43
124
102,400
228184516
t,sx,sy,ex,ey=map(int,input().split()) s=input() h,v="Z","Z" if(sx>ex):h="W" elif(sx<ex):h="E" if(sy>ey):v="S" elif(sy<ey):v="N" a,b=abs(sx-ex),abs(sy-ey);c=0 for i in s: if(a or b):c+=1 if(h==i and a>0): a-=1 if(v==i and b>0): b-=1 if(a!=0 or b!=0):print(-1) else:print(c)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
298/B
298
B
PyPy 3
TESTS
15
154
2,048,000
142587099
t, sx, sy, ex, ey = [int(x) for x in input().split()] wind_direction = input() distance_x = ex - sx distance_y = ey - sy only_east = distance_x > 0 only_north = distance_y > 0 time = 0 for direction in wind_direction: if distance_x == 0 and distance_y == 0: break dx = 0 dy = 0 if distance_x != 0: if direction == 'E' and only_east: dx = 1 elif direction == 'W' and not only_east: dx = -1 elif distance_y != 0: if direction == 'N' and only_north: dy = 1 elif direction == 'S' and not only_north: dy = -1 distance_x -= dx distance_y -= dy time += 1 print(time if distance_x == 0 and distance_y == 0 else -1)
43
124
102,400
231316668
t, sx, sy, ex, ey = list(map(int, input().split())) s = input() for _ in range(len(s)): i = s[_] if i == 'E': if sx < ex: sx += 1 elif i == 'S': if sy > ey: sy -= 1 elif i == 'W': if sx > ex: sx -= 1 else: if sy < ey: sy += 1 if sx == ex and sy == ey: print(_ + 1) break else: print(-1)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
803/F
803
F
PyPy 3
TESTS
10
170
1,740,800
59779640
# 803F import math import collections def do(): n = int(input()) nums = map(int, input().split(" ")) count = collections.defaultdict(int) for num in nums: for i in range(1, int(math.sqrt(num))+1): cp = num // i if num % i == 0: count[i] += 1 if cp != i and num % cp == 0: count[cp] += 1 maxk = max(count.keys()) freq = {k: (1 << count[k]) - 1 for k in count} for k in sorted(count.keys(), reverse=True): for kk in range(k << 1, maxk+1, k): freq[k] -= freq[kk] if kk in freq else 0 return freq[1] print(do())
35
124
14,950,400
220022104
mod = 1000000007 N = 100001 is_prime = [True] * N primes = [] mu = [0] * N mu[1] = 1 pow2 = [0] * N pow2[0] = 1 pow2[1] = 2 for i in range(2, N): if is_prime[i]: primes.append(i) mu[i] = -1 for p in primes: t = i * p if t >= N: break is_prime[t] = False if i % p == 0: mu[t] = 0 break mu[t] = -mu[i] pow2[i] = (pow2[i - 1] << 1) % mod n = int(input()) maxa = 0 cnt = [0] * N for a in map(int, input().split()): maxa = max(maxa, a) cnt[a] += 1 ans = pow2[n] - 1 for i in range(2, maxa + 1): if mu[i] == 0: continue s = 0 for j in range(i, maxa + 1, i): s = (s + cnt[j]) % mod ans = (ans + mu[i] * (pow2[s] - 1)) % mod print(ans)
Educational Codeforces Round 20
ICPC
2,017
2
256
Coprime Subsequences
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109 + 7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1].
The first line contains one integer number n (1 ≤ n ≤ 100000). The second line contains n integer numbers a1, a2... an (1 ≤ ai ≤ 100000).
Print the number of coprime subsequences of a modulo 109 + 7.
null
In the first example coprime subsequences are: 1. 1 2. 1, 2 3. 1, 3 4. 1, 2, 3 5. 2, 3 In the second example all subsequences are coprime.
[{"input": "3\n1 2 3", "output": "5"}, {"input": "4\n1 1 1 1", "output": "15"}, {"input": "7\n1 3 5 15 3 105 35", "output": "100"}]
2,000
["bitmasks", "combinatorics", "number theory"]
35
[{"input": "3\r\n1 2 3\r\n", "output": "5\r\n"}, {"input": "4\r\n1 1 1 1\r\n", "output": "15\r\n"}, {"input": "7\r\n1 3 5 15 3 105 35\r\n", "output": "100\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n100000\r\n", "output": "0\r\n"}, {"input": "5\r\n10 8 6 4 6\r\n", "output": "0\r\n"}, {"input": "5\r\n5 1 3 5 4\r\n", "output": "26\r\n"}, {"input": "5\r\n5 1 6 6 6\r\n", "output": "23\r\n"}, {"input": "10\r\n9 6 8 5 5 2 8 9 2 2\r\n", "output": "951\r\n"}, {"input": "10\r\n2 2 16 16 14 1 9 12 15 13\r\n", "output": "953\r\n"}, {"input": "50\r\n17 81 20 84 6 86 11 33 19 46 70 79 23 64 40 99 78 70 3 10 32 42 18 73 35 36 69 90 81 81 8 25 87 23 76 100 53 11 36 19 87 89 53 65 97 67 3 65 88 87\r\n", "output": "896338157\r\n"}, {"input": "50\r\n166 126 98 42 179 166 99 192 1 185 114 173 152 187 57 21 132 88 152 55 110 51 1 30 147 153 34 115 59 3 78 16 19 136 188 134 28 48 54 120 97 74 108 54 181 79 143 187 51 4\r\n", "output": "763698643\r\n"}, {"input": "100\r\n154 163 53 13 186 87 143 114 17 111 143 108 102 111 158 171 69 74 67 18 87 43 80 104 63 109 19 113 86 52 119 91 15 154 9 153 140 91 19 19 191 193 76 84 50 128 173 27 120 83 6 59 65 5 135 59 162 121 15 110 146 107 137 99 55 189 2 118 55 27 4 198 23 79 167 125 72 30 74 163 44 184 166 43 198 116 68 5 47 138 121 146 98 103 89 75 137 36 146 195\r\n", "output": "363088732\r\n"}, {"input": "100\r\n881 479 355 759 257 497 690 598 275 446 439 787 257 326 584 713 322 5 253 781 434 307 164 154 241 381 38 942 680 906 240 11 431 478 628 959 346 74 493 964 455 746 950 41 585 549 892 687 264 41 487 676 63 453 861 980 477 901 80 907 285 506 619 748 773 743 56 925 651 685 845 313 419 504 770 324 2 559 405 851 919 128 318 698 820 409 547 43 777 496 925 918 162 725 481 83 220 203 609 617\r\n", "output": "934190491\r\n"}]
false
stdio
null
true
984/A
984
A
Python 3
TESTS
5
46
0
167451595
n = int(input()) arr = list(input().split(" ")) arr.sort() if(n % 2 == 0): x = int(n/2) print(arr[x-1]) else: x = int((n+1)/2) print(arr[x-1])
35
46
0
135985301
n=int(input()) arr=list(map(int,input().split())) arr.sort() ans=arr[(n-1)//2] print(ans)
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns. The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it. You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "output": "124"}, {"input": "2\r\n2 1\r\n", "output": "1"}, {"input": "3\r\n1 1 1000\r\n", "output": "1"}, {"input": "2\r\n322 322\r\n", "output": "322"}, {"input": "3\r\n9 92 12\r\n", "output": "12"}, {"input": "3\r\n1 2 2\r\n", "output": "2"}]
false
stdio
null
true
984/A
984
A
Python 3
TESTS
5
46
204,800
106509779
c=int(input()) ar=(input().split())[:c] ##print(ar) arr_sorted=sorted(ar) ##print(arr_sorted) mid=int(c/2) ##print(*arr_sorted[mid],sep='') if(c%2!=0): print(*arr_sorted[mid],sep='') else: print(*arr_sorted[mid-1],sep='')
35
46
0
136023668
x = int(input()) arr = [int(q) for q in input().split()] arr = sorted(arr) while len(arr) != 1: arr.pop(len(arr)-1) if len(arr) != 1: arr.pop(0) print(arr[0])
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns. The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it. You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "output": "124"}, {"input": "2\r\n2 1\r\n", "output": "1"}, {"input": "3\r\n1 1 1000\r\n", "output": "1"}, {"input": "2\r\n322 322\r\n", "output": "322"}, {"input": "3\r\n9 92 12\r\n", "output": "12"}, {"input": "3\r\n1 2 2\r\n", "output": "2"}]
false
stdio
null
true
429/B
429
B
PyPy 3
TESTS
2
77
409,600
215672038
from collections import defaultdict import math import sys from bisect import bisect_right mod = pow(10,8) def clc(): n,m = map(int,input().split()) a = [] for _ in range(n): x = list(map(int,input().split())) a.append(x) b = [[0 for i in range(m)] for j in range(n)] c = [[0 for i in range(m)] for j in range(n)] d = [[0 for i in range(m)] for j in range(n)] e = [[0 for i in range(m)] for j in range(n)] for i in range(0,n): for j in range(0,m): temp= 0 if j>0: temp = b[i][j-1] if i>0: temp = max(temp,b[i-1][j]) b[i][j] = temp+a[i][j] for i in range(0,n): for j in range(m-1,-1,-1): temp= 0 if j+1<m: temp = c[i][j+1] if i>0: temp = max(temp,c[i-1][j]) c[i][j] = temp+a[i][j] for i in range(n-1,-1,-1): for j in range(0,m): temp = 0 if j>0: temp = d[i][j-1] if i+1<n: temp = max(temp,d[i+1][j]) d[i][j] = temp+a[i][j] for i in range(n-1,-1,-1): for j in range(m-1,-1,-1): temp = 0 if j+1<m: temp = e[i][j+1] if i+1<n: temp = max(temp,e[i+1][j]) e[i][j] = temp+a[i][j] ans = 0 for i in range(1,n-1): for j in range(1,m-1): ans = b[i - 1][j] + e[i + 1][j] + c[i][j + 1] + d[i][j - 1] ans = max(ans,b[i][j - 1] + e[i][j + 1] + c[i - 1][j] + d[i + 1][j]) print(ans) return True ans =clc()
30
389
46,592,000
163195251
import sys input = sys.stdin.readline M = int(1e9) + 7 n, m = map(int, input().split()) grid = [list(map(int, input().split())) for _ in range(n)] dp11 = [[0 for _ in range(m+2)] for _ in range(n+2)] for i in range(1, n+1): for j in range(1, m+1): dp11[i][j] = grid[i-1][j-1] + max(dp11[i-1][j], dp11[i][j-1]) dp1m = [[0 for _ in range(m+2)] for _ in range(n+2)] for i in range(1, n+1): for j in range(m, 0, -1): dp1m[i][j] = grid[i-1][j-1] + max(dp1m[i-1][j], dp1m[i][j+1]) dpn1 = [[0 for _ in range(m+2)] for _ in range(n+2)] for i in range(n, 0, -1): for j in range(1, m+1): dpn1[i][j] = grid[i-1][j-1] + max(dpn1[i+1][j], dpn1[i][j-1]) dpnm = [[0 for _ in range(m+2)] for _ in range(n+2)] for i in range(n, 0, -1): for j in range(m, 0, -1): dpnm[i][j] = grid[i-1][j-1] + max(dpnm[i+1][j], dpnm[i][j+1]) ans = -1 for x in range(2, n): for y in range(2, m): case1 = dp11[x-1][y] + dpnm[x+1][y] + dpn1[x][y-1] + dp1m[x][y+1] case2 = dp11[x][y-1] + dpnm[x][y+1] + dpn1[x+1][y] + dp1m[x-1][y] ans = max(ans, case1, case2) print(ans)
Codeforces Round 245 (Div. 1)
CF
2,014
2
256
Working out
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column. Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout. If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
The output contains a single number — the maximum total gain possible.
null
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
[{"input": "3 3\n100 100 100\n100 1 100\n100 100 100", "output": "800"}]
1,600
["dp"]
30
[{"input": "3 3\r\n100 100 100\r\n100 1 100\r\n100 100 100\r\n", "output": "800"}, {"input": "4 5\r\n87882 40786 3691 85313 46694\r\n28884 16067 3242 97367 78518\r\n4250 35501 9780 14435 19004\r\n64673 65438 56977 64495 27280\r\n", "output": "747898"}, {"input": "3 3\r\n3 1 2\r\n3 2 0\r\n2 3 2\r\n", "output": "16"}, {"input": "3 3\r\n1 10 1\r\n1 10 1\r\n1 10 1\r\n", "output": "26"}, {"input": "3 3\r\n0 0 0\r\n0 10000 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n1 1 1\r\n0 10000 0\r\n1 1 1\r\n", "output": "6"}, {"input": "3 3\r\n9 0 9\r\n0 9 9\r\n9 9 9\r\n", "output": "54"}, {"input": "3 3\r\n0 0 0\r\n0 100 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n100000 100000 100000\r\n1 100000 100000\r\n1 1 100000\r\n", "output": "500003"}, {"input": "3 3\r\n100 0 100\r\n1 100 100\r\n0 100 100\r\n", "output": "501"}]
false
stdio
null
true
298/B
298
B
PyPy 3
TESTS
30
310
22,118,400
88882456
time, x1, y1, x2, y2 = map(int, input().split()) word = input() up = True east = True required = abs(x2 - x1) + abs(y2 - y1) if x2 - x1 < 0: east = False if y2 - y1 < 0: up = False if x2 - x1 == 0: if up: for spend, val in enumerate(word): if val == 'N': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) if not up: for spend, val in enumerate(word): if val == 'S': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) elif y2 - y1 == 0: if east: for spend, val in enumerate(word): if val == 'E': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) else: for spend, val in enumerate(word): if val == 'W': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) else: if up and east: for spend, val in enumerate(word): if val == 'N' or val == 'E': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) elif up and not east: for spend, val in enumerate(word): if val == 'N' or val == 'W': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) elif not up and not east: for spend, val in enumerate(word): if val == 'S' or val == 'W': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1) elif not up and east: for spend, val in enumerate(word): if val == 'S' or val == 'E': required -= 1 if required == 0: break else: print(-1) exit() print(spend + 1)
43
124
614,400
108562921
t,x1,y1,x2,y2 = map(int,input().split()) s = str(input()) a = x2 - x1 b = y2 - y1 from itertools import islice def nth_index(iterable, value, n): matches = (idx for idx, val in enumerate(iterable) if val == value) return next(islice(matches, n-1, n), None) if a > 0 and b > 0: f = nth_index(s,'E',a) l = nth_index(s,'N',b) if s.count('E') < abs(a) or s.count('N') < abs(b): final = -1 else: final = max(f,l)+1 elif a > 0 and b < 0 : f = nth_index(s,'E',abs(a)) l = nth_index(s,'S',abs(b)) if s.count('E') < abs(a) or s.count('S') < abs(b): final = -1 else: final = max(f,l)+1 elif a < 0 and b > 0: f = nth_index(s,'W',abs(a)) l = nth_index(s,'N',abs(b)) if s.count('W') < abs(a) or s.count('N') < abs(b): final = -1 else: final = max(f,l)+1 elif a < 0 and b < 0: f = nth_index(s,'W',abs(a)) l = nth_index(s,'S',abs(b)) if s.count('W') < abs(a) or s.count('S') < abs(b): final = -1 else: final = max(f,l)+1 elif a > 0 and b == 0: f = nth_index(s,'E',abs(a)) if s.count('E') < abs (a): final = -1 else: final = f+1 elif a < 0 and b == 0: f = nth_index(s,'W',abs(a)) if s.count('W') < abs (a): final = -1 else: final = f+1 elif a == 0 and b > 0: f = nth_index(s,'N',abs(b)) if s.count('N') < abs (b): final = -1 else: final = f+1 elif a == 0 and b < 0: f = nth_index(s,'S',abs(b)) if s.count('S') < abs (b): final = -1 else: final = f+1 print(final)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
984/A
984
A
Python 3
TESTS
7
124
409,600
71547259
def test_0(): # A - import math from collections import Counter n = list(map(int, input().split(" ")))[0] data = list(map(int, input().split(" "))) data.sort() if n == 1: print(data[0]) # print(data) k = n//2-1 if n %2==0 else n//2 print(data[k]) test_0()
35
46
0
136385941
n = int(input()) nums = list(map(int,input().split())) nums.sort() print(nums[(len(nums)-1)//2])
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns. The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it. You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "output": "124"}, {"input": "2\r\n2 1\r\n", "output": "1"}, {"input": "3\r\n1 1 1000\r\n", "output": "1"}, {"input": "2\r\n322 322\r\n", "output": "322"}, {"input": "3\r\n9 92 12\r\n", "output": "12"}, {"input": "3\r\n1 2 2\r\n", "output": "2"}]
false
stdio
null
true
984/A
984
A
Python 3
TESTS
5
31
0
166401416
n=int(input()) s=[] f=[] u=input() s=u.split() s=sorted(s) for _ in range(1): if(len(s)%2==1): y=len(s)//2 f.append(s[y]) else: z=(len(s)//2)-1 f.append(s[z]) print(*f)
35
46
0
140945136
n = int(input()) a = [int(i) for i in input().split()] a.sort() print(a[(n//2-1 if n % 2 == 0 else n//2)])
Codeforces Round 483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
CF
2,018
2
256
Game
Two players play a game. Initially there are $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns. The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it. You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.
The first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print one number that will be left on the board.
null
In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
[{"input": "3\n2 1 3", "output": "2"}, {"input": "3\n2 2 2", "output": "2"}]
800
["sortings"]
35
[{"input": "3\r\n2 1 3\r\n", "output": "2"}, {"input": "3\r\n2 2 2\r\n", "output": "2"}, {"input": "9\r\n44 53 51 80 5 27 74 79 94\r\n", "output": "53"}, {"input": "10\r\n38 82 23 37 96 4 81 60 67 86\r\n", "output": "60"}, {"input": "10\r\n58 26 77 15 53 81 68 48 22 65\r\n", "output": "53"}, {"input": "1\r\n124\r\n", "output": "124"}, {"input": "2\r\n2 1\r\n", "output": "1"}, {"input": "3\r\n1 1 1000\r\n", "output": "1"}, {"input": "2\r\n322 322\r\n", "output": "322"}, {"input": "3\r\n9 92 12\r\n", "output": "12"}, {"input": "3\r\n1 2 2\r\n", "output": "2"}]
false
stdio
null
true
298/B
298
B
PyPy 3
TESTS
30
310
24,678,400
80546323
f_line = input().split() t = int(f_line[0]) sx = int(f_line[1]) sy = int(f_line[2]) ex = int(f_line[3]) ey = int(f_line[4]) dx = ex-sx dy = ey-sy tx = 0 ty = 0 dir_lst = list(input()) min_time = 0 for i in dir_lst: if dx == tx and dy == ty: break else: min_time += 1 if i == 'E': if dx <= 0: continue else: tx += 1 elif i == 'S': if dy >= 0: continue else: ty -= 1 elif i == 'W': if dx >= 0: continue else: tx -= 1 elif i == 'N': if dy <= 0: continue else: ty += 1 if dx == 0 and dy == 0: print(0) elif dx != tx or dy != ty: print(-1) else: print(min_time)
43
124
2,048,000
168725046
allnums = input() allnumslist = allnums.split() x1 = int(allnumslist[1]) y1 = int(allnumslist[2]) x2 = int(allnumslist[3]) y2 = int(allnumslist[4]) count = 0 direction = input() for i in range(len(direction)): if x1 != x2 or y1 != y2: count += 1 if x1 < x2: if direction[i] == 'E': x1 += 1 if y1 < y2: if direction[i] == 'N': y1 += 1 if x1 > x2: if direction[i] == 'W': x1 -= 1 if y1 > y2: if direction[i] == 'S': y1 -= 1 if x1 != x2 or y1 != y2: print('-1') if x1 == x2 and y1 == y2: print(count)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
490/B
490
B
PyPy 3-64
TESTS
2
46
0
185035822
left_by_right = dict() right_by_left = dict() n = int(input()) for _ in range(n): a, b = map(int, input().split()) right_by_left[a] = b left_by_right[b] = a res = [''] * n pref = 0 for i in range(1, n, 2): next_man = right_by_left[pref] res[i] = str(next_man) pref = next_man pref = 0 for i in range(n - 2, -1, -2): next_man = left_by_right[pref] res[i] = str(next_man) pref = next_man print(' '.join(res))
61
608
92,364,800
215124672
import sys input = sys.stdin.readline n = int(input()) graph = dict(input()[:-1].split() for _ in range(n)) ans = [None] * n values = set(graph.values()) ans[0] = next(k for k in graph if k not in values) ans[1] = graph['0'] for i in range(2, n): ans[i] = graph[ans[i - 2]] print(*ans)
Codeforces Round 279 (Div. 2)
CF
2,014
2
256
Queue
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1). After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task. Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
null
The picture illustrates the queue for the first sample.
[{"input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141"}]
1,500
["dsu", "implementation"]
61
[{"input": "4\r\n92 31\r\n0 7\r\n31 0\r\n7 141\r\n", "output": "92 7 31 141 \r\n"}, {"input": "2\r\n0 1\r\n2 0\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 2\r\n1 3\r\n2 0\r\n", "output": "1 2 3 \r\n"}, {"input": "4\r\n101 0\r\n0 102\r\n102 100\r\n103 101\r\n", "output": "103 102 101 100 \r\n"}, {"input": "5\r\n0 1\r\n1 4\r\n4 0\r\n3 2\r\n5 3\r\n", "output": "5 1 3 4 2 \r\n"}, {"input": "6\r\n10001 0\r\n0 10005\r\n10003 10001\r\n10002 10000\r\n10005 10002\r\n10004 10003\r\n", "output": "10004 10005 10003 10002 10001 10000 \r\n"}, {"input": "3\r\n0 743259\r\n72866 70294\r\n743259 0\r\n", "output": "72866 743259 70294 \r\n"}, {"input": "4\r\n263750 0\r\n513707 263750\r\n0 718595\r\n718595 148112\r\n", "output": "513707 718595 263750 148112 \r\n"}, {"input": "5\r\n645873 145459\r\n638930 82975\r\n0 645873\r\n82975 389665\r\n145459 0\r\n", "output": "638930 645873 82975 145459 389665 \r\n"}, {"input": "6\r\n341637 51795\r\n0 809471\r\n51795 0\r\n244669 341637\r\n852537 508622\r\n809471 852537\r\n", "output": "244669 809471 341637 852537 51795 508622 \r\n"}, {"input": "7\r\n111283 0\r\n496010 510417\r\n423431 921854\r\n510417 111283\r\n0 496010\r\n758535 423431\r\n921854 59208\r\n", "output": "758535 496010 423431 510417 921854 111283 59208 \r\n"}, {"input": "8\r\n611412 115521\r\n114290 712424\r\n115521 242491\r\n242491 0\r\n0 114290\r\n712424 282922\r\n282922 589147\r\n359823 611412\r\n", "output": "359823 114290 611412 712424 115521 282922 242491 589147 \r\n"}, {"input": "9\r\n308992 348750\r\n0 6496\r\n487447 676506\r\n874677 985199\r\n260782 487447\r\n985199 260782\r\n348750 0\r\n570981 308992\r\n6496 570981\r\n", "output": "874677 6496 985199 570981 260782 308992 487447 348750 676506 \r\n"}, {"input": "10\r\n419946 201769\r\n245945 0\r\n842799 113073\r\n836998 245945\r\n0 794376\r\n692107 836998\r\n113073 904403\r\n904403 987165\r\n201769 692107\r\n794376 842799\r\n", "output": "419946 794376 201769 842799 692107 113073 836998 904403 245945 987165 \r\n"}, {"input": "10\r\n189071 852255\r\n227133 652124\r\n329720 4848\r\n652124 329720\r\n0 72517\r\n943168 0\r\n72517 544697\r\n4848 943168\r\n538963 189071\r\n544697 538963\r\n", "output": "227133 72517 652124 544697 329720 538963 4848 189071 943168 852255 \r\n"}, {"input": "2\r\n0 300000\r\n1000000 0\r\n", "output": "1000000 300000 \r\n"}]
false
stdio
null
true
490/B
490
B
Python 3
TESTS
2
46
102,400
179043482
def solution(): from collections import defaultdict n = int(input()) lst = [0 for _ in range(n)] front = defaultdict(int) back = defaultdict(int) start = 0 end = 0 unvisited = [] for _ in range(n): front_p,back_p = input().split() if front_p == "0": start = back_p continue if back_p == "0": end = front_p continue front[back_p] = front_p back[front_p] = back_p unvisited.append(back_p) unvisited.append(front_p) lst[1] = start lst[n-2] = end #print(front) #print(back) for i in range(n): if lst[i] and (lst[i] in back) and (i+2 < n): lst[i+2] = back[lst[i]] if lst[i] and (lst[i] in front) and (i-2) >=0: lst[i-2] = front[lst[i]] for i in range(n-1,1,-1): if lst[i] and (lst[i] in front) and i-2 >= 0: lst[i-2] = front[lst[i]] if lst[i] and (lst[i] in back) and (i+2 < n): lst[i+2] = back[lst[i]] #print(lst) #for this test case only 3 """ 0 2 1 3 2 0 """ for i in unvisited: if i not in front: if lst[-1] == 0: lst[-1] = i for i in unvisited: if i not in back: if lst[0] == 0: lst[0] = 0 lst = list(map(str,lst)) print(" ".join(lst)) solution()
61
732
48,537,600
202532657
import sys input = lambda: sys.stdin.readline().rstrip() from collections import defaultdict N = int(input()) P = defaultdict(int) ins = defaultdict(int) A = set() ans = [0]*N for _ in range(N): a,b = map(int, input().split()) if a==0: ans[1] = b continue elif b==0: ans[-2]=a continue A.add(a) A.add(b) ins[b]+=1 P[a]=b for a in A: if ins[a]==0: if a==ans[1]: idx = 1 while P[a]>0: idx+=2 ans[idx] = P[a] a = P[a] else: idx = 0 while a>0: ans[idx] = a a = P[a] idx+=2 print(*ans)
Codeforces Round 279 (Div. 2)
CF
2,014
2
256
Queue
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1). After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task. Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
null
The picture illustrates the queue for the first sample.
[{"input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141"}]
1,500
["dsu", "implementation"]
61
[{"input": "4\r\n92 31\r\n0 7\r\n31 0\r\n7 141\r\n", "output": "92 7 31 141 \r\n"}, {"input": "2\r\n0 1\r\n2 0\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 2\r\n1 3\r\n2 0\r\n", "output": "1 2 3 \r\n"}, {"input": "4\r\n101 0\r\n0 102\r\n102 100\r\n103 101\r\n", "output": "103 102 101 100 \r\n"}, {"input": "5\r\n0 1\r\n1 4\r\n4 0\r\n3 2\r\n5 3\r\n", "output": "5 1 3 4 2 \r\n"}, {"input": "6\r\n10001 0\r\n0 10005\r\n10003 10001\r\n10002 10000\r\n10005 10002\r\n10004 10003\r\n", "output": "10004 10005 10003 10002 10001 10000 \r\n"}, {"input": "3\r\n0 743259\r\n72866 70294\r\n743259 0\r\n", "output": "72866 743259 70294 \r\n"}, {"input": "4\r\n263750 0\r\n513707 263750\r\n0 718595\r\n718595 148112\r\n", "output": "513707 718595 263750 148112 \r\n"}, {"input": "5\r\n645873 145459\r\n638930 82975\r\n0 645873\r\n82975 389665\r\n145459 0\r\n", "output": "638930 645873 82975 145459 389665 \r\n"}, {"input": "6\r\n341637 51795\r\n0 809471\r\n51795 0\r\n244669 341637\r\n852537 508622\r\n809471 852537\r\n", "output": "244669 809471 341637 852537 51795 508622 \r\n"}, {"input": "7\r\n111283 0\r\n496010 510417\r\n423431 921854\r\n510417 111283\r\n0 496010\r\n758535 423431\r\n921854 59208\r\n", "output": "758535 496010 423431 510417 921854 111283 59208 \r\n"}, {"input": "8\r\n611412 115521\r\n114290 712424\r\n115521 242491\r\n242491 0\r\n0 114290\r\n712424 282922\r\n282922 589147\r\n359823 611412\r\n", "output": "359823 114290 611412 712424 115521 282922 242491 589147 \r\n"}, {"input": "9\r\n308992 348750\r\n0 6496\r\n487447 676506\r\n874677 985199\r\n260782 487447\r\n985199 260782\r\n348750 0\r\n570981 308992\r\n6496 570981\r\n", "output": "874677 6496 985199 570981 260782 308992 487447 348750 676506 \r\n"}, {"input": "10\r\n419946 201769\r\n245945 0\r\n842799 113073\r\n836998 245945\r\n0 794376\r\n692107 836998\r\n113073 904403\r\n904403 987165\r\n201769 692107\r\n794376 842799\r\n", "output": "419946 794376 201769 842799 692107 113073 836998 904403 245945 987165 \r\n"}, {"input": "10\r\n189071 852255\r\n227133 652124\r\n329720 4848\r\n652124 329720\r\n0 72517\r\n943168 0\r\n72517 544697\r\n4848 943168\r\n538963 189071\r\n544697 538963\r\n", "output": "227133 72517 652124 544697 329720 538963 4848 189071 943168 852255 \r\n"}, {"input": "2\r\n0 300000\r\n1000000 0\r\n", "output": "1000000 300000 \r\n"}]
false
stdio
null
true
298/B
298
B
Python 3
TESTS
30
404
614,400
69844636
t, sx, sy, ex, ey = [int(x) for x in input().split()] dir = input() allow = [] if ex - sx > 0: allow.append('E') elif ex - sx < 0: allow.append('W') if ey - sy > 0: allow.append('N') elif ey - sy < 0: allow.append('S') curr = [sx, sy] end = [ex, ey] mov = {'E':(1, 0), 'W':(-1, 0), 'N':(0, 1), 'S':(0, -1)} ans = 0 for t, d in enumerate(dir): ans += 1 if d in allow: curr[0] += mov[d][0] curr[1] += mov[d][1] if curr == end: break if curr == end: print(ans) else: print(-1)
43
124
2,252,800
178420251
def find(): time_counter = 0 xtravel = ex-sx ytravel = ey-sy for i in inp2: if i == "N" and ytravel > 0: ytravel -= 1 if i == "E" and xtravel > 0: xtravel -= 1 if i == "S" and ytravel < 0: ytravel += 1 if i == "W" and xtravel < 0: xtravel += 1 time_counter += 1 if xtravel == 0 and ytravel == 0: return(time_counter) return(-1) inp1 = input() inp2 = input() split_list = inp1.split() t = int(split_list[0]) sx = int(split_list[1]) sy = int(split_list[2]) ex = int(split_list[3]) ey = int(split_list[4]) if sx == ex and sy == ey: print(0) else: print(find())
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
33/A
33
A
Python 3
TESTS
8
218
0
81854070
n,m,k=map(int,input().split()) tot=0; t={} for i in range(n): r,c=map(int,input().split()) t[r]=min(t.get(r,1000001),c) for i in range(1,m+1): tot+=min(t[i],k) k-=t[i] print(tot)
31
92
0
151265763
n, m, k = [int(item) for item in input().split(' ')] my_dict = dict() for i in range(n): r, health = [int(item) for item in input().split(' ')] if r in my_dict and my_dict[r] > health or r not in my_dict: my_dict[r] = health summary = sum(my_dict.values()) print(summary if summary <= k else k)
Codeforces Beta Round 33 (Codeforces format)
CF
2,010
2
256
What is for dinner?
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are "relaxing". For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap). It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again. Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative. As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner. We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one.
The first line contains three integers n, m, k (1 ≤ m ≤ n ≤ 1000, 0 ≤ k ≤ 106) — total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 ≤ r ≤ m) — index of the row, where belongs the corresponding tooth, and c (0 ≤ c ≤ 106) — its residual viability. It's guaranteed that each tooth row has positive amount of teeth.
In the first line output the maximum amount of crucians that Valerie can consume for dinner.
null
null
[{"input": "4 3 18\n2 3\n1 2\n3 6\n2 3", "output": "11"}, {"input": "2 2 13\n1 13\n2 12", "output": "13"}]
1,200
["greedy", "implementation"]
31
[{"input": "4 3 18\r\n2 3\r\n1 2\r\n3 6\r\n2 3\r\n", "output": "11\r\n"}, {"input": "2 2 13\r\n1 13\r\n2 12\r\n", "output": "13\r\n"}, {"input": "5 4 8\r\n4 6\r\n4 5\r\n1 3\r\n2 0\r\n3 3\r\n", "output": "8\r\n"}, {"input": "1 1 0\r\n1 3\r\n", "output": "0\r\n"}, {"input": "7 1 30\r\n1 8\r\n1 15\r\n1 5\r\n1 17\r\n1 9\r\n1 16\r\n1 16\r\n", "output": "5\r\n"}, {"input": "4 2 8\r\n1 9\r\n1 10\r\n1 4\r\n2 6\r\n", "output": "8\r\n"}, {"input": "10 4 14\r\n2 6\r\n1 5\r\n2 8\r\n2 6\r\n2 5\r\n4 1\r\n4 0\r\n2 4\r\n3 4\r\n1 0\r\n", "output": "8\r\n"}, {"input": "1 1 1000000\r\n1 1000000\r\n", "output": "1000000\r\n"}, {"input": "4 3 181818\r\n3 1299\r\n1 1694\r\n3 1164\r\n2 1278\r\n", "output": "4136\r\n"}, {"input": "19 12 199\r\n7 1\r\n8 6\r\n6 14\r\n1 7\r\n4 1\r\n6 6\r\n3 4\r\n1 5\r\n9 2\r\n5 3\r\n11 3\r\n9 4\r\n1 12\r\n4 7\r\n7 3\r\n12 14\r\n2 1\r\n10 8\r\n6 12\r\n", "output": "54\r\n"}]
false
stdio
null
true
449/B
449
B
PyPy 3-64
TESTS
4
1,326
131,584,000
188171661
import gc import heapq import itertools import math from collections import Counter, deque, defaultdict from sys import stdout import time from math import factorial, log, gcd import sys from decimal import Decimal import threading from heapq import * from fractions import Fraction import bisect def S(): return sys.stdin.readline().split() def I(): return [int(i) for i in sys.stdin.readline().split()] def II(): return int(sys.stdin.readline()) def IS(): return sys.stdin.readline().replace('\n', '') def main(): n, m, k = I() graph = [[] for _ in range(n)] edges, train_edges = [], [] inf = float('inf') for _ in range(m): u, v, w = I() graph[u - 1].append((v - 1, w)) graph[v - 1].append((u - 1, w)) edges.append((u - 1, v - 1, w)) for i in range(k): v, w = I() graph[0].append((v - 1, w)) graph[v - 1].append((0, w)) edges.append((0, v - 1, w)) train_edges.append((v - 1, w)) d = [inf for _ in range(n)] d[0] = 0 queue = [] heapq.heappush(queue, (0, 0)) while queue: r, v = heapq.heappop(queue) for u, w in graph[v]: if d[u] > r + w: d[u] = r + w heapq.heappush(queue, (d[u], u)) new_graph = [0] * n for u, v, w in edges: if d[u] + w == d[v]: new_graph[v] += 1 if d[v] + w == d[u]: new_graph[u] += 1 ans = 0 for i in range(k): v, w = train_edges[i] if d[v] < w or new_graph[v] > 1: new_graph[v] -= 1 ans += 1 print(ans) if __name__ == '__main__': # for _ in range(II()): # main() main()
45
1,886
133,222,400
202495416
import os import sys import threading from io import BytesIO, IOBase from heapq import heappush, heappop, heapify from collections import defaultdict, deque, Counter from bisect import bisect_left as bl from bisect import bisect_right as br # threading.stack_size(10**8) # sys.setrecursionlimit(10**6) def ri(): return int(input()) def rs(): return input() def rl(): return list(map(int, input().split())) def rls(): return list(input().split()) def main(): n,m,k=rl() g=[[] for i in range(n+1)] for _ in range(m): u,v,x=rl() g[u].append((v,x,1)) g[v].append((u,x,1)) t=[[] for i in range(n+1)] for _ in range(k): s,y=rl() t[s].append(y) g[1].append((s,y,2)) g[s].append((1,y,2)) dis=[float('inf') for i in range(n+1)] dis[1]=0 vis=[False for i in range(n+1)] q=[] heappush(q,(0,1)) while q: cd,cn=heappop(q) if vis[cn]:continue vis[cn]=True for (nn,nw,ty) in g[cn]: nd=nw+dis[cn] if nd<dis[nn] and not vis[nn]: dis[nn]=nd heappush(q,(dis[nn],nn)) res=0 for i in range(2,n+1): gr=0 gt=0 for (nn,nw,ty) in g[i]: if dis[nn]+nw==dis[i]: if ty==2:gt+=1 else:gr+=1 if gr>0:res+=len(t[i]) else:res+=len(t[i])-1 print(res) pass BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() # threading.Thread(target=main).start()
Codeforces Round 257 (Div. 1)
CF
2,014
2
256
Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi. Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output a single integer representing the maximum number of the train routes which can be closed.
null
null
[{"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "output": "2"}, {"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3", "output": "2"}]
2,000
["graphs", "greedy", "shortest paths"]
45
[{"input": "5 5 3\r\n1 2 1\r\n2 3 2\r\n1 3 3\r\n3 4 4\r\n1 5 5\r\n3 5\r\n4 5\r\n5 5\r\n", "output": "2\r\n"}, {"input": "2 2 3\r\n1 2 2\r\n2 1 3\r\n2 1\r\n2 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "5 4 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "3 2 5\r\n1 2 2\r\n2 3 4\r\n3 5\r\n3 5\r\n3 5\r\n3 6\r\n3 7\r\n", "output": "4\r\n"}, {"input": "5 5 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 3 1000000000\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "2 1 5\r\n1 2 4\r\n2 3\r\n2 5\r\n2 4\r\n2 4\r\n2 5\r\n", "output": "4\r\n"}, {"input": "3 3 6\r\n1 2 499999999\r\n2 3 500000000\r\n1 3 999999999\r\n2 499999999\r\n2 500000000\r\n2 499999999\r\n3 999999999\r\n3 1000000000\r\n3 1000000000\r\n", "output": "6\r\n"}, {"input": "2 1 1\r\n1 2 1\r\n2 1000000000\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 4\r\n2 3 4\r\n2 2\r\n3 6\r\n", "output": "1\r\n"}, {"input": "5 5 2\r\n1 2 100\r\n2 3 100\r\n3 4 100\r\n4 5 20\r\n2 5 5\r\n5 50\r\n4 1\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 100\r\n2 3 1\r\n2 1\r\n3 3\r\n", "output": "1\r\n"}]
false
stdio
null
true
690/C1
690
C1
PyPy 3
TESTS
14
217
4,915,200
118715248
def dfs(start,vis,end,ans): global graph #print(start,end) vis[start] = True if start == end: ans[0] = True return for node in graph[start]: if node not in vis: dfs(node,vis,end,ans) from collections import defaultdict n,m = map(int,input().split()) graph = defaultdict(list) arr = [] flag = True for i in range(m): u,v = map(int,input().split()) if u in graph and v in graph: vis = {} ans = [False] dfs(u,vis,v,ans) if ans[0]: flag = False graph[u].append(v) graph[v].append(u) if not flag: print("no") else: print("yes")
18
62
307,200
19010610
import sys sys.setrecursionlimit(1000000) def dfs(v, pr): global used global p global f if not f: return None if used[v]: f = False used[v] = True for i in range(len(p[v])): if p[v][i] != pr: dfs(p[v][i], v) n, m = map(int, input().split()) p = [] for i in range(n): p.append([]) for i in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 p[a].append(b) p[b].append(a) used = [False] * n f = True for i in range(n): if i != 0 and not used[i]: f = False break if not used[i]: dfs(i, -1) if f: print('yes') else: print('no')
Helvetic Coding Contest 2016 online mirror (teams, unrated)
ICPC
2,016
2
256
Brain Network (easy)
One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated – it consists of n brains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly: 1. It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains). 2. There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false. If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid.
The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 ≤ a, b ≤ n, a ≠ b).
The output consists of one line, containing either yes or no depending on whether the nervous system is valid.
null
null
[{"input": "4 4\n1 2\n2 3\n3 1\n4 1", "output": "no"}, {"input": "6 5\n1 2\n2 3\n3 4\n4 5\n3 6", "output": "yes"}]
1,300
[]
18
[{"input": "4 4\r\n1 2\r\n2 3\r\n3 1\r\n4 1\r\n", "output": "no\r\n"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n3 6\r\n", "output": "yes\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "yes\r\n"}, {"input": "3 3\r\n2 1\r\n1 3\r\n3 2\r\n", "output": "no\r\n"}, {"input": "3 2\r\n1 2\r\n2 3\r\n", "output": "yes\r\n"}, {"input": "9 8\r\n1 2\r\n2 3\r\n3 4\r\n4 1\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n", "output": "no\r\n"}, {"input": "8 7\r\n6 2\r\n1 5\r\n4 8\r\n4 7\r\n6 7\r\n8 3\r\n8 1\r\n", "output": "yes\r\n"}, {"input": "200 5\r\n93 101\r\n199 164\r\n14 94\r\n115 61\r\n106 156\r\n", "output": "no\r\n"}, {"input": "10 9\r\n6 5\r\n9 2\r\n4 7\r\n2 3\r\n7 3\r\n3 4\r\n10 6\r\n1 2\r\n5 8\r\n", "output": "no\r\n"}, {"input": "10 9\r\n2 3\r\n6 8\r\n10 1\r\n1 8\r\n6 7\r\n8 7\r\n10 5\r\n7 10\r\n2 5\r\n", "output": "no\r\n"}, {"input": "10 9\r\n3 2\r\n4 1\r\n6 1\r\n7 1\r\n9 2\r\n6 9\r\n5 2\r\n7 9\r\n3 7\r\n", "output": "no\r\n"}]
false
stdio
null
true
260/C
260
C
Python 3
TESTS
3
93
307,200
76331305
import sys lines = sys.stdin.readlines() (n,x) = map(int, lines[0].strip().split(" ")) nums = list(map(int, lines[1].strip().split(" "))) minSoFar = min(nums) index = -1 for i in range(x+n-2, x-2, -1): if nums[i%n] == minSoFar: index = i%n; break tmp = nums[index] num = tmp * n + (x-1+n-index) % n for i in range(n): nums[i] -= tmp for i in range(1, (x-1 + n - index) % n+1): nums[(i+index)%n] -= 1 nums[index] = num print(" ".join(map(str, nums)))
38
108
15,462,400
215078802
n, x = list(map(int, input().split())) A = list(map(int, input().split())) m = min(A) mi = A.index(m) for i in range(n): A[i] -= m j = x - 1 val = n * m while(A[j]): A[j] -= 1 val += 1 j = ((j - 1) % n + n) % n A[j] = val print(*A)
Codeforces Round 158 (Div. 2)
CF
2,012
1
256
Balls and Boxes
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right. Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on. For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one. At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls. He asks you to help to find the initial arrangement of the balls in the boxes.
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
null
null
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
1,700
["constructive algorithms", "greedy", "implementation"]
38
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
false
stdio
null
true
429/B
429
B
Python 3
TESTS
2
46
6,963,200
125012463
n,m = map(int,input().split()) matrix =[] for i in range(n): arr = list(map(int,input().split())) matrix.append(arr) dp1 =[[0]*(m+2) for ele in range(n+2)] dp2 =[[0]*(m+2) for ele in range(n+2)] dp3 =[[0]*(m+2) for ele in range(n+2)] dp4 =[[0]*(m+2) for ele in range(n+2)] for i in range(1,n+1): for j in range(1,m+1): dp1[i][j]=matrix[i-1][j-1]+max(dp1[i][j-1],dp1[i-1][j]) for i in range(n,0,-1): for j in range(1,m+1): dp2[i][j]=matrix[i-1][j-1]+max(dp2[i][j-1],dp2[i+1][j]) for i in range(n,0,-1): for j in range(m,0,-1): dp3[i][j]=matrix[i-1][j-1]+max(dp3[i][j+1],dp3[i+1][j]) for i in range(1,n+1): for j in range(m,0,-1): dp4[i][j]=matrix[i-1][j-1]+max(dp4[i][j+1],dp4[i-1][j]) ans =0 for i in range(2,n): for j in range(2,m): ans = max(ans,dp1[i-1][j]+dp3[i+1][j]+dp2[i][j-1]+dp4[i][j+1],dp1[i][j-1],dp3[i][j+1]+dp2[i+1][j]+dp4[i-1][j]) print(ans)
30
405
52,736,000
169404412
from collections import defaultdict as dd, deque as dq, Counter as ctr from itertools import accumulate from re import findall import re import sys input = lambda: sys.stdin.buffer.readline().decode('utf-8').rstrip('\r\n') from bisect import bisect_left as bl from bisect import bisect_right as br from array import array inp = lambda: int(input()) mi = lambda x=int : map(x, input().split()) arr= lambda d='i',x=int: array(d,mi(x)) li = lambda x=int: list(mi(x)) lb = lambda x=int: list(map(x, input())) ls = lambda: list(input()) bi = lambda n: bin(n).replace("0b", "") def sbsq(a,s): i=0 for x in s: if i>=len(a): break if x==a[i]: i+=1 return i==len(a) yn = ['No', 'Yes'] YN = ['NO', 'YES'] YY = "YES" NN = "NO" yy = "Yes" nn = "No" alp='abcdefghijklmnopqrstuvwxyz' inf= sys.maxsize mod=1000000007 import heapq as hq # print =lambda x: sys.stdout.buffer.write(x.encode('utf-8')) mod=998244353 # from math import comb from math import ceil,sqrt,gcd def main(kase): n,m=mi() grd=[li() for i in range(n)] dpnm=[[0]*m for i in range(n)] dp0m=[[0]*m for i in range(n)] dpn0=[[0]*m for i in range(n)] dp00=[[0]*m for i in range(n)] for i in range(n): for j in range(m): dpnm[i][j]=grd[i][j] dp00[-i-1][-j-1]=grd[-i-1][-j-1] if i>0: dpnm[i][j]=dpnm[i-1][j]+grd[i][j] dp00[-i-1][-1-j]=dp00[-i][-1-j]+grd[-i-1][-1-j] if j>0: dpnm[i][j]=max(dpnm[i][j],dpnm[i][j-1]+grd[i][j]) dp00[-i-1][-1-j]=max(dp00[-i-1][-1-j],dp00[-i-1][-j]+grd[-i-1][-1-j]) for i in range(n-1,-1,-1): for j in range(m): dp0m[i][j]=grd[i][j] dpn0[-i-1][-j-1]=grd[-i-1][-j-1] if i<n-1: dp0m[i][j]=dp0m[i+1][j]+grd[i][j] dpn0[-i-1][-j-1]=dpn0[-i-2][-j-1]+grd[-i-1][-j-1] if j>0: dp0m[i][j]=max(dp0m[i][j],dp0m[i][j-1]+grd[i][j]) dpn0[-i-1][-j-1]=max(dpn0[-i-1][-j-1],dpn0[-i-1][-j]+grd[-i-1][-j-1]) ans=0 # for x in [dp00,dp0m,dpn0,dpnm]: # for y in x: # print(y) for i in range(1,n-1): for j in range(1,m-1): ans=max(ans,dpn0[i-1][j]+dp0m[i+1][j]+dp00[i][j+1]+dpnm[i][j-1],dpn0[i][j+1]+dp0m[i][j-1]+dp00[i+1][j]+dpnm[i-1][j]) print(ans) pass if __name__ == "__main__": test_Cases=1 # test_Cases=inp() for i in range(test_Cases): main(i)
Codeforces Round 245 (Div. 1)
CF
2,014
2
256
Working out
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column. Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout. If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
The output contains a single number — the maximum total gain possible.
null
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
[{"input": "3 3\n100 100 100\n100 1 100\n100 100 100", "output": "800"}]
1,600
["dp"]
30
[{"input": "3 3\r\n100 100 100\r\n100 1 100\r\n100 100 100\r\n", "output": "800"}, {"input": "4 5\r\n87882 40786 3691 85313 46694\r\n28884 16067 3242 97367 78518\r\n4250 35501 9780 14435 19004\r\n64673 65438 56977 64495 27280\r\n", "output": "747898"}, {"input": "3 3\r\n3 1 2\r\n3 2 0\r\n2 3 2\r\n", "output": "16"}, {"input": "3 3\r\n1 10 1\r\n1 10 1\r\n1 10 1\r\n", "output": "26"}, {"input": "3 3\r\n0 0 0\r\n0 10000 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n1 1 1\r\n0 10000 0\r\n1 1 1\r\n", "output": "6"}, {"input": "3 3\r\n9 0 9\r\n0 9 9\r\n9 9 9\r\n", "output": "54"}, {"input": "3 3\r\n0 0 0\r\n0 100 0\r\n0 0 0\r\n", "output": "0"}, {"input": "3 3\r\n100000 100000 100000\r\n1 100000 100000\r\n1 1 100000\r\n", "output": "500003"}, {"input": "3 3\r\n100 0 100\r\n1 100 100\r\n0 100 100\r\n", "output": "501"}]
false
stdio
null
true
449/B
449
B
Python 3
TESTS
4
1,559
63,283,200
52225606
import sys from heapq import heappush, heappop input=sys.stdin.readline def main(): n,m,k = map(int,input().split()) g=[[] for _ in range(1+n)] for _ in range(m): u,v,x = map(int,input().split()) g[u].append((x,v,-1)) g[v].append((x,u,-1)) for _ in range(k): s,y=map(int,input().split()) g[1].append((y,s,s)) def dijkstra(): inf=float('inf') dis=[inf]*(n+1) t=[-1]*(n+1) vis=[False]*(n+1) dis[1]=0 pq=[] heappush(pq,(0,1,-1)) while pq: d,v,b = heappop(pq) if vis[v]: continue vis[v]=True for x,u,bb in g[v]: if x+d < dis[u]: dis[u]=d + x t[u]=max(t[v],bb) heappush(pq,(dis[u],u,t[u])) elif x+d==dis[u] : if max(t[v],bb)==-1 : t[u]=-1 t=set(t) return len(t)-1 if -1 in t else len(t) print(k-dijkstra()) main()
45
1,871
131,072,000
167802107
import sys import math import collections from heapq import heappush, heappop input = sys.stdin.readline ints = lambda: list(map(int, input().split())) n, m, k = ints() graph = [[] for _ in range(n + 1)] for _ in range(m): a, b, c = ints() graph[a].append((b, c, 0)) graph[b].append((a, c, 0)) trains = [[] for _ in range(n + 1)] for _ in range(k): s, y = ints() trains[s].append(y) graph[1].append((s, y, 1)) graph[s].append((1, y, 1)) distances = [math.inf for _ in range(n + 1)] distances[1] = 0 visited = [False for _ in range(n + 1)] q = [] heappush(q, (0, 1)) while q: d, node = heappop(q) if visited[node]: continue visited[node] = True for (nb, c, t) in graph[node]: total = c + distances[node] if total < distances[nb] and not visited[nb]: distances[nb] = total heappush(q, (distances[nb], nb)) ans = 0 for i in range(2, n + 1): g_r = 0 g_t = 0 for (nb, c, t) in graph[i]: if distances[nb] + c == distances[i]: if t: g_t += 1 else: g_r += 1 if g_r > 0: ans += len(trains[i]) else: ans += len(trains[i]) - 1 print(ans)
Codeforces Round 257 (Div. 1)
CF
2,014
2
256
Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi. Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output a single integer representing the maximum number of the train routes which can be closed.
null
null
[{"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "output": "2"}, {"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3", "output": "2"}]
2,000
["graphs", "greedy", "shortest paths"]
45
[{"input": "5 5 3\r\n1 2 1\r\n2 3 2\r\n1 3 3\r\n3 4 4\r\n1 5 5\r\n3 5\r\n4 5\r\n5 5\r\n", "output": "2\r\n"}, {"input": "2 2 3\r\n1 2 2\r\n2 1 3\r\n2 1\r\n2 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "5 4 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "3 2 5\r\n1 2 2\r\n2 3 4\r\n3 5\r\n3 5\r\n3 5\r\n3 6\r\n3 7\r\n", "output": "4\r\n"}, {"input": "5 5 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 3 1000000000\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "2 1 5\r\n1 2 4\r\n2 3\r\n2 5\r\n2 4\r\n2 4\r\n2 5\r\n", "output": "4\r\n"}, {"input": "3 3 6\r\n1 2 499999999\r\n2 3 500000000\r\n1 3 999999999\r\n2 499999999\r\n2 500000000\r\n2 499999999\r\n3 999999999\r\n3 1000000000\r\n3 1000000000\r\n", "output": "6\r\n"}, {"input": "2 1 1\r\n1 2 1\r\n2 1000000000\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 4\r\n2 3 4\r\n2 2\r\n3 6\r\n", "output": "1\r\n"}, {"input": "5 5 2\r\n1 2 100\r\n2 3 100\r\n3 4 100\r\n4 5 20\r\n2 5 5\r\n5 50\r\n4 1\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 100\r\n2 3 1\r\n2 1\r\n3 3\r\n", "output": "1\r\n"}]
false
stdio
null
true
298/B
298
B
Python 3
TESTS
30
278
716,800
104419424
from collections import * t, sx, sy, ex, ey = map(int, input().split()) s = input() cnte = 0 cntw = 0 cntn = 0 cnts = 0 if sx == ex and sy == ey: print(0) elif sx == ex: if ey > sy: diff = abs(ey - sy) for i in range(t): if s[i] == "N": cntn += 1 if cntn == diff: print(i + 1) exit(0) print(-1) else: diff = abs(ey - sy) for i in range(t): if s[i] == "S": cntn += 1 if cntn == diff: print(i + 1) exit(0) print(-1) elif sy == ey: if ex > sx: diff = abs(ex - sx) for i in range(t): if s[i] == "E": cntn += 1 if cntn == diff: print(i + 1) exit(0) print(-1) else: diff = abs(ex - sx) for i in range(t): if s[i] == "W": cntn += 1 if cntn == diff: print(i + 1) exit(0) print(-1) else: ex -= sx ey -= sy sx = 0 sy = 0 d = defaultdict(int) for i in range(t): d[s[i]] += 1 if ex > 0 and ey > 0: cnte = ex cntn = ey if d["E"] < cnte or d["N"] < cntn: print(-1) exit(0) else: e = 0 n = 0 for i in range(t): if s[i] == "E": e += 1 if s[i] == "N": n += 1 if e == cnte and n == cntn: print(i + 1) exit(0) elif ex > 0 and ey < 0: cnte = ex cntn = abs(ey) if d["E"] < cnte or d["S"] < cntn: print(-1) exit(0) else: e = 0 n = 0 for i in range(t): if s[i] == "E": e += 1 if s[i] == "S": n += 1 if e == cnte and n == cntn: print(i + 1) exit(0) elif ex < 0 and ey > 0: cnte = abs(ex) cntn = ey if d["W"] < cnte or d["N"] < cntn: print(-1) exit(0) else: e = 0 n = 0 for i in range(t): if s[i] == "W": e += 1 if s[i] == "N": n += 1 if e == cnte and n == cntn: print(i + 1) exit(0) else: cnte = abs(ex) cntn = abs(ey) if d["W"] < cnte or d["S"] < cntn: print(-1) exit(0) else: e = 0 n = 0 for i in range(t): if s[i] == "W": e += 1 if s[i] == "S": n += 1 if e == cnte and n == cntn: print(i + 1) exit(0)
43
124
2,355,200
193611395
l=list(map(int,input().split())) t,x,y,p,q=l[0],l[1],l[2],l[3],l[4] s,n,e,w=0,0,0,0 babe=input() if p-x>=0: e=p-x else: w=x-p if q-y>=0: n=q-y else: s=y-q if (p-x==0) and (q-y==0): print(0) else: for i in range(t): if babe[i]=="S": if s>0: s-=1 elif babe[i]=="N": if n>0: n-=1 elif babe[i]=="E": if e>0: e-=1 elif babe[i]=="W": if w>0: w-=1 if s==e==n==w==0: print(i+1) break else: print(-1)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Sail
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). - If the wind blows to the east, the boat will move to (x + 1, y). - If the wind blows to the south, the boat will move to (x, y - 1). - If the wind blows to the west, the boat will move to (x - 1, y). - If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105, - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
null
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
[{"input": "5 0 0 1 1\nSESNW", "output": "4"}, {"input": "10 5 3 3 6\nNENSWESNEE", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
43
[{"input": "5 0 0 1 1\r\nSESNW\r\n", "output": "4\r\n"}, {"input": "10 5 3 3 6\r\nNENSWESNEE\r\n", "output": "-1\r\n"}, {"input": "19 -172106364 -468680119 -172106365 -468680119\r\nSSEEESSSESESWSEESSS\r\n", "output": "13\r\n"}, {"input": "39 -1000000000 -1000000000 -999999997 -1000000000\r\nENEENWSWSSWESNSSEESNSESWSWNSWESNENWNWEE\r\n", "output": "4\r\n"}, {"input": "41 -264908123 -86993764 -264908123 -86993723\r\nNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\r\n", "output": "41\r\n"}, {"input": "34 -1000000000 -1000000000 -999999983 -1000000000\r\nEEEEESSEWNSSSESWEWSWESEWSEESNEWEEE\r\n", "output": "-1\r\n"}, {"input": "1 0 0 0 -1\r\nS\r\n", "output": "1\r\n"}, {"input": "1 5 5 5 6\r\nE\r\n", "output": "-1\r\n"}, {"input": "15 1 1 1 2\r\nNNNNNNNNNNNNNNN\r\n", "output": "1\r\n"}, {"input": "11 1 1 1 2\r\nNNNNNNNNNNN\r\n", "output": "1\r\n"}]
false
stdio
null
true
74/A
74
A
Python 3
TESTS
7
92
6,963,200
115764899
import sys def get_single_int (): return int (sys.stdin.readline ().strip ()) def get_string (): return sys.stdin.readline ().strip () def get_ints (): return map (int, sys.stdin.readline ().strip ().split ()) def get_list (): return list (map (str, sys.stdin.readline ().strip ().split ())) #code starts here n = get_single_int () ar = [] for i in range (n): ar.append (get_list ()) ans = 0 result = '' for i in range (n): name = ar [i] [0] sh = int (ar [i] [1])*100 ush = int (ar [i] [2])*50 summ = 0 for j in range (5): summ+= int (ar [ i] [j + 3]) summ += (sh - ush) if (summ > ans): result = name ans = summ if (summ > ans): result = name print (result)
32
92
0
162484826
def room_leader(): a = int(input()) tabela_participantes = [input().split() for _ in range(a)] scores = [] for p in tabela_participantes: scores.append(calcula_pontuacao(p)) scores.sort(reverse=True, key=lambda y: y[1]) print(scores[0][0]) def calcula_pontuacao(participante): atual = 0 nome = "" for count,part in enumerate(participante): if(count>0): part = int(part) if(count==0): nome = part elif(count==1): atual += 100*part elif(count==2): atual -= 50*part else: atual += part return (nome, atual) room_leader()
Codeforces Beta Round 68
CF
2,011
2
256
Room Leader
Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document. In the beginning of the round the contestants are divided into rooms. Each room contains exactly n participants. During the contest the participants are suggested to solve five problems, A, B, C, D and E. For each of these problem, depending on when the given problem was solved and whether it was solved at all, the participants receive some points. Besides, a contestant can perform hacks on other contestants. For each successful hack a contestant earns 100 points, for each unsuccessful hack a contestant loses 50 points. The number of points for every contestant is represented by the sum of points he has received from all his problems, including hacks. You are suggested to determine the leader for some room; the leader is a participant who has maximum points.
The first line contains an integer n, which is the number of contestants in the room (1 ≤ n ≤ 50). The next n lines contain the participants of a given room. The i-th line has the format of "handlei plusi minusi ai bi ci di ei" — it is the handle of a contestant, the number of successful hacks, the number of unsuccessful hacks and the number of points he has received from problems A, B, C, D, E correspondingly. The handle of each participant consists of Latin letters, digits and underscores and has the length from 1 to 20 characters. There are the following limitations imposed upon the numbers: - 0 ≤ plusi, minusi ≤ 50; - 150 ≤ ai ≤ 500 or ai = 0, if problem A is not solved; - 300 ≤ bi ≤ 1000 or bi = 0, if problem B is not solved; - 450 ≤ ci ≤ 1500 or ci = 0, if problem C is not solved; - 600 ≤ di ≤ 2000 or di = 0, if problem D is not solved; - 750 ≤ ei ≤ 2500 or ei = 0, if problem E is not solved. All the numbers are integer. All the participants have different handles. It is guaranteed that there is exactly one leader in the room (i.e. there are no two participants with the maximal number of points).
Print on the single line the handle of the room leader.
null
The number of points that each participant from the example earns, are as follows: - Petr — 3860 - tourist — 4140 - Egor — 4030 - c00lH4x0R —  - 350 - some_participant — 2220 Thus, the leader of the room is tourist.
[{"input": "5\nPetr 3 1 490 920 1000 1200 0\ntourist 2 0 490 950 1100 1400 0\nEgor 7 0 480 900 950 0 1000\nc00lH4x0R 0 10 150 0 0 0 0\nsome_participant 2 1 450 720 900 0 0", "output": "tourist"}]
1,000
["implementation"]
32
[{"input": "5\r\nPetr 3 1 490 920 1000 1200 0\r\ntourist 2 0 490 950 1100 1400 0\r\nEgor 7 0 480 900 950 0 1000\r\nc00lH4x0R 0 10 150 0 0 0 0\r\nsome_participant 2 1 450 720 900 0 0\r\n", "output": "tourist"}, {"input": "1\r\nA 0 0 200 0 0 0 0\r\n", "output": "A"}, {"input": "2\r\n12345678901234567890 1 0 200 0 0 0 0\r\n_ 1 0 201 0 0 0 0\r\n", "output": "_"}, {"input": "5\r\nAb 0 0 481 900 1200 1600 2000\r\nCd 0 0 480 899 1200 1600 2000\r\nEf 0 0 480 900 1200 1600 2000\r\ngH 0 0 480 900 1200 1599 2000\r\nij 0 0 480 900 1199 1600 2001\r\n", "output": "Ab"}, {"input": "4\r\nF1 0 0 150 0 0 0 0\r\nF2 0 1 0 0 0 0 0\r\nF3 0 2 0 0 0 0 0\r\nF4 0 3 0 0 0 0 0\r\n", "output": "F1"}, {"input": "2\r\nA87h 5 0 199 0 0 0 0\r\nBcfg 7 0 0 0 0 0 0\r\n", "output": "Bcfg"}, {"input": "2\r\nminus_one 0 4 199 0 0 0 0\r\nminus_two 0 4 198 0 0 0 0\r\n", "output": "minus_one"}, {"input": "1\r\nZ 0 0 0 0 0 0 0\r\n", "output": "Z"}, {"input": "3\r\nAbcd 0 4 189 0 0 0 0\r\nDefg 0 5 248 0 0 0 0\r\nGhh 1 3 0 0 0 0 0\r\n", "output": "Defg"}, {"input": "3\r\ndf 0 6 0 0 0 0 0\r\njnm 1 8 300 0 0 0 0\r\n_ub_ 3 20 300 310 0 0 0\r\n", "output": "jnm"}, {"input": "1\r\njhgcyt 0 50 0 0 0 0 0\r\n", "output": "jhgcyt"}, {"input": "2\r\njhv 0 50 500 1000 1500 2000 2500\r\nPetr 2 1 489 910 1100 1300 1000\r\n", "output": "jhv"}, {"input": "3\r\nufu 0 50 0 0 0 0 0\r\nhzEr65f 1 50 0 0 0 0 0\r\nytdttjfhfd 0 50 150 0 0 0 0\r\n", "output": "ytdttjfhfd"}, {"input": "5\r\nufuf 0 50 0 0 0 0 0\r\nyfycy 50 0 500 1000 1500 2000 2500\r\n__u77 6 7 490 999 1456 1976 1356\r\n0 1 2 0 0 0 0 2452\r\ngu7fF 50 0 500 1000 1500 2000 2499\r\n", "output": "yfycy"}, {"input": "2\r\nhfy 0 50 0 0 0 0 2500\r\nugug 0 50 0 0 0 0 2499\r\n", "output": "hfy"}, {"input": "8\r\nA 0 0 0 0 0 0 0\r\nb 0 0 0 0 0 0 0\r\nc 0 0 0 0 0 0 0\r\nD 0 0 0 0 0 0 0\r\nE 1 0 0 0 0 0 0\r\nF 0 0 0 0 0 0 0\r\ng 0 0 0 0 0 0 0\r\nH 0 0 0 0 0 0 0\r\n", "output": "E"}, {"input": "2\r\nyyyc 50 50 0 0 0 0 0\r\nydd 0 0 0 0 0 0 2499\r\n", "output": "yyyc"}, {"input": "2\r\ntom 0 2 0 0 0 0 0\r\nmac 0 1 0 0 0 0 0\r\n", "output": "mac"}, {"input": "1\r\ncool 0 10 0 0 0 0 0\r\n", "output": "cool"}]
false
stdio
null
true
490/B
490
B
PyPy 3-64
TESTS
2
62
0
232687168
import sys def main() -> None: read = sys.stdin.readline n = int(read()) l_neigh = {} r_neigh = {} left_pairings, right_pairings = [None] * n, [None] * n left_most, right_most = None, None for _ in range(n): left, right = (int(i) for i in read().split()) if left == 0: left_most = right elif right == 0: right_most = left l_neigh[right] = left r_neigh[left] = right left_pairings[1] = left_most right_pairings[-2] = right_most for i in range(3, n, 2): left_pairings[i] = r_neigh[left_pairings[i - 2]] for i in range(n - 4, -1, -2): right_pairings[i] = l_neigh[right_pairings[i + 2]] final_output = [] for i in range(0, n): left = left_pairings[i] right = right_pairings[i] final_output.append(str(left if left is not None else right)) print(' '.join(final_output)) if __name__ == '__main__': main() # 92 7 31 141
61
748
42,496,000
177473864
import sys input = sys.stdin.readline from collections import deque n = int(input()) l = dict() r = dict() x = -1 for i in range(n): a, b = map(int, input().split()) l[b] = a r[a] = b if n % 2 == 0: if x == -1: x = a else: if a == 0: x = b if n % 2 == 0: d = deque([x]) a = x while a in r: a = r[a] d.append(a) a = x while a in l: a = l[a] d.appendleft(a) for i in range(n//2): print(d[i], end=' ') print(d[n//2+1+i], end=' ') else: d = [x] a = x s = {0, x} while a in r: if r[a] in s: break a = r[a] d.append(a) s.add(a) for i in l: if i not in s: x = i break d1 = deque([x]) a = x while a in r: a = r[a] d1.append(a) a = x while a in l: a = l[a] d1.appendleft(a) for i in range(n//2): print(d1[i], end=' ') print(d[i], end=' ') print(d1[-1])
Codeforces Round 279 (Div. 2)
CF
2,014
2
256
Queue
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1). After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task. Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
null
The picture illustrates the queue for the first sample.
[{"input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141"}]
1,500
["dsu", "implementation"]
61
[{"input": "4\r\n92 31\r\n0 7\r\n31 0\r\n7 141\r\n", "output": "92 7 31 141 \r\n"}, {"input": "2\r\n0 1\r\n2 0\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 2\r\n1 3\r\n2 0\r\n", "output": "1 2 3 \r\n"}, {"input": "4\r\n101 0\r\n0 102\r\n102 100\r\n103 101\r\n", "output": "103 102 101 100 \r\n"}, {"input": "5\r\n0 1\r\n1 4\r\n4 0\r\n3 2\r\n5 3\r\n", "output": "5 1 3 4 2 \r\n"}, {"input": "6\r\n10001 0\r\n0 10005\r\n10003 10001\r\n10002 10000\r\n10005 10002\r\n10004 10003\r\n", "output": "10004 10005 10003 10002 10001 10000 \r\n"}, {"input": "3\r\n0 743259\r\n72866 70294\r\n743259 0\r\n", "output": "72866 743259 70294 \r\n"}, {"input": "4\r\n263750 0\r\n513707 263750\r\n0 718595\r\n718595 148112\r\n", "output": "513707 718595 263750 148112 \r\n"}, {"input": "5\r\n645873 145459\r\n638930 82975\r\n0 645873\r\n82975 389665\r\n145459 0\r\n", "output": "638930 645873 82975 145459 389665 \r\n"}, {"input": "6\r\n341637 51795\r\n0 809471\r\n51795 0\r\n244669 341637\r\n852537 508622\r\n809471 852537\r\n", "output": "244669 809471 341637 852537 51795 508622 \r\n"}, {"input": "7\r\n111283 0\r\n496010 510417\r\n423431 921854\r\n510417 111283\r\n0 496010\r\n758535 423431\r\n921854 59208\r\n", "output": "758535 496010 423431 510417 921854 111283 59208 \r\n"}, {"input": "8\r\n611412 115521\r\n114290 712424\r\n115521 242491\r\n242491 0\r\n0 114290\r\n712424 282922\r\n282922 589147\r\n359823 611412\r\n", "output": "359823 114290 611412 712424 115521 282922 242491 589147 \r\n"}, {"input": "9\r\n308992 348750\r\n0 6496\r\n487447 676506\r\n874677 985199\r\n260782 487447\r\n985199 260782\r\n348750 0\r\n570981 308992\r\n6496 570981\r\n", "output": "874677 6496 985199 570981 260782 308992 487447 348750 676506 \r\n"}, {"input": "10\r\n419946 201769\r\n245945 0\r\n842799 113073\r\n836998 245945\r\n0 794376\r\n692107 836998\r\n113073 904403\r\n904403 987165\r\n201769 692107\r\n794376 842799\r\n", "output": "419946 794376 201769 842799 692107 113073 836998 904403 245945 987165 \r\n"}, {"input": "10\r\n189071 852255\r\n227133 652124\r\n329720 4848\r\n652124 329720\r\n0 72517\r\n943168 0\r\n72517 544697\r\n4848 943168\r\n538963 189071\r\n544697 538963\r\n", "output": "227133 72517 652124 544697 329720 538963 4848 189071 943168 852255 \r\n"}, {"input": "2\r\n0 300000\r\n1000000 0\r\n", "output": "1000000 300000 \r\n"}]
false
stdio
null
true
925/B
925
B
PyPy 3
TESTS
4
186
1,228,800
100875333
import bisect n,x1,x2=map(int,input().split()) cc=sorted(enumerate(list(map(int,input().split()))),key=lambda x:x[1]) c=[] real_inds=[] for inda,num in cc: c.append(num) real_inds.append(inda+1) flag=0 if x2<x1: flag=1 x1,x2=x2,x1 k=1 rr=n while k<n: w=x1/k start=bisect.bisect_left(c,w) taken=start+k if taken>n: k+=1 else: rr=n-k+1 ll=0 while rr-ll>1: # print("ll,rr are",ll,rr) k2=(rr+ll)//2 w2=x2/k2 start2=bisect.bisect_left(c,w2) free=n-k2 if free<taken: free-=k if free>=start2: rr=k2 else: ll=k2 # print(k,ll,rr,free) if rr-ll==0 or rr>n-k: k+=1 else: break if k==n: print("No") else: print("Yes") w=x1/k start=bisect.bisect_left(c,w) taken=start+k first=real_inds[start:taken] k2=rr if flag: print(k2,k) else: print(k,k2) w2=x2/k2 start2=bisect.bisect_left(c,w2) free=n-start2 needed=n-k2 sec=[] if needed>=taken: sec=real_inds[needed:] else: need_more=taken-needed sec=real_inds[start-need_more:start]+real_inds[taken:] if flag: print(*sec) print(*first) else: print(*first) print(*sec)
40
748
42,700,800
159295631
I = lambda:list(map(int, input().split())) n,x1,x2 = I() servers = I() idx = list(range(n)) idx.sort(key = lambda i : servers[i]) def solve(a, b, rev): j = len(idx)-1 while j >= 0 and (n-j)*servers[idx[j]] < b: j -= 1 if j < 0: return i = j-1 while i >= 0 and (j-i)*servers[idx[i]] < a: i -= 1 if i < 0: return print("Yes") if not rev: print(j-i, n-j) print(" ".join(map(lambda x:str(x+1),idx[i:j]))) print(" ".join(map(lambda x:str(x+1), idx[j:]))) else: print(n-j, j-i) print(" ".join(map(lambda x:str(x+1), idx[j:]))) print(" ".join(map(lambda x:str(x+1),idx[i:j]))) exit() solve(x1,x2,False) solve(x2,x1,True) print("No")
VK Cup 2018 - Round 3
CF
2,018
2
256
Resource Distribution
One department of some software company has $$$n$$$ servers of different specifications. Servers are indexed with consecutive integers from $$$1$$$ to $$$n$$$. Suppose that the specifications of the $$$j$$$-th server may be expressed with a single integer number $$$c_j$$$ of artificial resource units. In order for production to work, it is needed to deploy two services $$$S_1$$$ and $$$S_2$$$ to process incoming requests using the servers of the department. Processing of incoming requests of service $$$S_i$$$ takes $$$x_i$$$ resource units. The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $$$S_i$$$ is deployed using $$$k_i$$$ servers, then the load is divided equally between these servers and each server requires only $$$x_i / k_i$$$ (that may be a fractional number) resource units. Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides. Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.
The first line contains three integers $$$n$$$, $$$x_1$$$, $$$x_2$$$ ($$$2 \leq n \leq 300\,000$$$, $$$1 \leq x_1, x_2 \leq 10^9$$$) — the number of servers that the department may use, and resource units requirements for each of the services. The second line contains $$$n$$$ space-separated integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$) — the number of resource units provided by each of the servers.
If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes). Otherwise print the word "Yes" (without the quotes). In the second line print two integers $$$k_1$$$ and $$$k_2$$$ ($$$1 \leq k_1, k_2 \leq n$$$) — the number of servers used for each of the services. In the third line print $$$k_1$$$ integers, the indices of the servers that will be used for the first service. In the fourth line print $$$k_2$$$ integers, the indices of the servers that will be used for the second service. No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.
null
In the first sample test each of the servers 1, 2 and 6 will will provide $$$8 / 3 = 2.(6)$$$ resource units and each of the servers 5, 4 will provide $$$16 / 2 = 8$$$ resource units. In the second sample test the first server will provide $$$20$$$ resource units and each of the remaining servers will provide $$$32 / 3 = 10.(6)$$$ resource units.
[{"input": "6 8 16\n3 5 2 9 8 7", "output": "Yes\n3 2\n1 2 6\n5 4"}, {"input": "4 20 32\n21 11 11 12", "output": "Yes\n1 3\n1\n2 3 4"}, {"input": "4 11 32\n5 5 16 16", "output": "No"}, {"input": "5 12 20\n7 8 4 11 9", "output": "No"}]
1,700
["binary search", "implementation", "sortings"]
40
[{"input": "6 8 16\r\n3 5 2 9 8 7\r\n", "output": "Yes\r\n4 2\r\n3 1 2 6\r\n5 4\r\n"}, {"input": "4 20 32\r\n21 11 11 12\r\n", "output": "Yes\r\n1 3\r\n1\r\n2 3 4\r\n"}, {"input": "4 11 32\r\n5 5 16 16\r\n", "output": "No\r\n"}, {"input": "5 12 20\r\n7 8 4 11 9\r\n", "output": "No\r\n"}, {"input": "2 1 1\r\n1 1\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 1\r\n1 1000000\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 1\r\n1000000000 1000000000\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 2\r\n1 1\r\n", "output": "No\r\n"}, {"input": "15 250 200\r\n71 2 77 69 100 53 54 40 73 32 82 58 24 82 41\r\n", "output": "Yes\r\n11 3\r\n13 10 8 15 6 7 12 4 1 9 3\r\n11 14 5\r\n"}, {"input": "4 12 11\r\n4 4 6 11\r\n", "output": "Yes\r\n3 1\r\n1 2 3\r\n4\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, x1, x2 = map(int, f.readline().split()) c = list(map(int, f.readline().split())) with open(output_path) as f: ref_lines = [line.strip() for line in f] with open(submission_path) as f: sub_lines = [line.strip() for line in f] non_empty_ref = [line for line in ref_lines if line] non_empty_sub = [line for line in sub_lines if line] if not non_empty_ref: print(0) return ref_first = non_empty_ref[0] if ref_first == 'No': if len(non_empty_sub) == 1 and non_empty_sub[0] == 'No': print(1) else: print(0) return else: if len(non_empty_sub) != 4 or non_empty_sub[0] != 'Yes': print(0) return try: k1, k2 = map(int, non_empty_sub[1].split()) except: print(0) return if k1 <= 0 or k2 <= 0 or k1 + k2 > n: print(0) return try: s1 = list(map(int, non_empty_sub[2].split())) s2 = list(map(int, non_empty_sub[3].split())) except: print(0) return if len(s1) != k1 or len(s2) != k2: print(0) return all_indices = set() for s in s1: if not (1 <= s <= n) or s in all_indices: print(0) return all_indices.add(s) for s in s2: if not (1 <= s <= n) or s in all_indices: print(0) return all_indices.add(s) for s in s1: if c[s-1] * k1 < x1: print(0) return for s in s2: if c[s-1] * k2 < x2: print(0) return print(1) return if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
925/B
925
B
PyPy 3-64
TESTS
6
1,387
47,001,600
213797987
import sys import math import collections from heapq import heappush, heappop input = sys.stdin.readline ints = lambda: list(map(int, input().split())) n, x1, x2 = ints() a = ints() for i in range(n): a[i] = [a[i], i] a.sort() j = n for i in range(n): k = math.ceil(x1/a[i][0]) while j - i > k: j -= 1 if j != n: k2 = math.ceil(x2/a[j][0]) if n - j >= k2: r1 = [a[x][1]+1 for x in range(i, j)] r2 = [a[x][1]+1 for x in range(j, n)] print("Yes") print(len(r1), len(r2)) print(*r1) print(*r2) sys.exit() j = n for i in range(n): k = math.ceil(x2/a[i][0]) while j - i > k: j -= 1 if j != n: k2 = math.ceil(x1/a[j][0]) if n - j >= k2: r1 = [a[x][1]+1 for x in range(i, j)] r2 = [a[x][1]+1 for x in range(j, n)] print("Yes") print(len(r2), len(r1)) print(*r2) print(*r1) sys.exit() print("No")
40
795
50,995,200
37714382
n, a, b = [int(x) for x in input().split()] hs = [int(x) for x in input().split()] hs = sorted(enumerate(hs), key=lambda x: x[1]) for i in range(1, n+1): if hs[-i][1] * i >= a: break else: print('No') exit() for j in range(i+1, n+1): if hs[-j][1] * (j - i) >= b: print('Yes') print(i, j - i) print(" ".join(map(str, [index+1 for index,_ in hs[-i:]]))) print(" ".join(map(str, [index+1 for index,_ in hs[-j:-i]]))) break else: for i in range(1, n+1): if hs[-i][1] * i >= b: break else: print('No') exit() for j in range(i+1, n+1): if hs[-j][1] * (j - i) >= a: print('Yes') print(j - i, i) print(" ".join(map(str, [index+1 for index,_ in hs[-j:-i]]))) print(" ".join(map(str, [index+1 for index,_ in hs[-i:]]))) break else: print('No')
VK Cup 2018 - Round 3
CF
2,018
2
256
Resource Distribution
One department of some software company has $$$n$$$ servers of different specifications. Servers are indexed with consecutive integers from $$$1$$$ to $$$n$$$. Suppose that the specifications of the $$$j$$$-th server may be expressed with a single integer number $$$c_j$$$ of artificial resource units. In order for production to work, it is needed to deploy two services $$$S_1$$$ and $$$S_2$$$ to process incoming requests using the servers of the department. Processing of incoming requests of service $$$S_i$$$ takes $$$x_i$$$ resource units. The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $$$S_i$$$ is deployed using $$$k_i$$$ servers, then the load is divided equally between these servers and each server requires only $$$x_i / k_i$$$ (that may be a fractional number) resource units. Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides. Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.
The first line contains three integers $$$n$$$, $$$x_1$$$, $$$x_2$$$ ($$$2 \leq n \leq 300\,000$$$, $$$1 \leq x_1, x_2 \leq 10^9$$$) — the number of servers that the department may use, and resource units requirements for each of the services. The second line contains $$$n$$$ space-separated integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$) — the number of resource units provided by each of the servers.
If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes). Otherwise print the word "Yes" (without the quotes). In the second line print two integers $$$k_1$$$ and $$$k_2$$$ ($$$1 \leq k_1, k_2 \leq n$$$) — the number of servers used for each of the services. In the third line print $$$k_1$$$ integers, the indices of the servers that will be used for the first service. In the fourth line print $$$k_2$$$ integers, the indices of the servers that will be used for the second service. No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.
null
In the first sample test each of the servers 1, 2 and 6 will will provide $$$8 / 3 = 2.(6)$$$ resource units and each of the servers 5, 4 will provide $$$16 / 2 = 8$$$ resource units. In the second sample test the first server will provide $$$20$$$ resource units and each of the remaining servers will provide $$$32 / 3 = 10.(6)$$$ resource units.
[{"input": "6 8 16\n3 5 2 9 8 7", "output": "Yes\n3 2\n1 2 6\n5 4"}, {"input": "4 20 32\n21 11 11 12", "output": "Yes\n1 3\n1\n2 3 4"}, {"input": "4 11 32\n5 5 16 16", "output": "No"}, {"input": "5 12 20\n7 8 4 11 9", "output": "No"}]
1,700
["binary search", "implementation", "sortings"]
40
[{"input": "6 8 16\r\n3 5 2 9 8 7\r\n", "output": "Yes\r\n4 2\r\n3 1 2 6\r\n5 4\r\n"}, {"input": "4 20 32\r\n21 11 11 12\r\n", "output": "Yes\r\n1 3\r\n1\r\n2 3 4\r\n"}, {"input": "4 11 32\r\n5 5 16 16\r\n", "output": "No\r\n"}, {"input": "5 12 20\r\n7 8 4 11 9\r\n", "output": "No\r\n"}, {"input": "2 1 1\r\n1 1\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 1\r\n1 1000000\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 1\r\n1000000000 1000000000\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 2\r\n1 1\r\n", "output": "No\r\n"}, {"input": "15 250 200\r\n71 2 77 69 100 53 54 40 73 32 82 58 24 82 41\r\n", "output": "Yes\r\n11 3\r\n13 10 8 15 6 7 12 4 1 9 3\r\n11 14 5\r\n"}, {"input": "4 12 11\r\n4 4 6 11\r\n", "output": "Yes\r\n3 1\r\n1 2 3\r\n4\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, x1, x2 = map(int, f.readline().split()) c = list(map(int, f.readline().split())) with open(output_path) as f: ref_lines = [line.strip() for line in f] with open(submission_path) as f: sub_lines = [line.strip() for line in f] non_empty_ref = [line for line in ref_lines if line] non_empty_sub = [line for line in sub_lines if line] if not non_empty_ref: print(0) return ref_first = non_empty_ref[0] if ref_first == 'No': if len(non_empty_sub) == 1 and non_empty_sub[0] == 'No': print(1) else: print(0) return else: if len(non_empty_sub) != 4 or non_empty_sub[0] != 'Yes': print(0) return try: k1, k2 = map(int, non_empty_sub[1].split()) except: print(0) return if k1 <= 0 or k2 <= 0 or k1 + k2 > n: print(0) return try: s1 = list(map(int, non_empty_sub[2].split())) s2 = list(map(int, non_empty_sub[3].split())) except: print(0) return if len(s1) != k1 or len(s2) != k2: print(0) return all_indices = set() for s in s1: if not (1 <= s <= n) or s in all_indices: print(0) return all_indices.add(s) for s in s2: if not (1 <= s <= n) or s in all_indices: print(0) return all_indices.add(s) for s in s1: if c[s-1] * k1 < x1: print(0) return for s in s2: if c[s-1] * k2 < x2: print(0) return print(1) return if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
44/A
44
A
Python 3
TESTS
28
62
0
144856923
n=int(input()) d=set() for i in range(n): s,k=map(str,input().split()) d.add(s+k) print(len(d))
29
62
0
144809605
lst = [] for i in range(int(input())): input2 = input().split() lst.append(input2[0]+" "+input2[1]) print(len(set(lst)))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
69/E
69
E
PyPy 3
TESTS
12
233
6,144,000
78369401
from collections import defaultdict, deque from bisect import bisect import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from sys import stdout print = stdout.write n, k = map(int, input().split()) a = deque() count = defaultdict(int) te = [] for _ in range(k): x = int(input()) a.append(x) count[x] += 1 for key, value in count.items(): if value == 1: te.append(key) ans = [] te.sort() try: ans.append(te[-1]) except: ans.append('Nothing') for i in range(k,n): x = int(input()) t = a.popleft() a.append(x) if t == x: continue count[t] -= 1 count[x] += 1 y, z = count[t], count[x] if z == 1: te.insert(bisect(te, x), x) elif z == 2: del te[bisect(te, x)-1] if y == 0: del te[bisect(te, t)-1] elif y == 1: te.insert(bisect(te, t), t) try: ans.append(te[-1]) except: ans.append('Nothing') for i in ans: print(str(i) + '\n')
49
529
17,100,800
206692985
# for I/O for local system import sys from os import path if(path.exists('Input.txt')): sys.stdin = open("Input.txt","r") sys.stdout = open("Output.txt","w") # For fast I/O input = sys.stdin.buffer.readline # input = sys.stdin.readline print = sys.stdout.write # Import libraries here whenever required from heapq import * from random import randint # Use this because normal dict can sometimes give TLE class mydict: def __init__(self, func=lambda: 0): self.random = randint(0, 1 << 32) self.default = func self.dict = {} def __getitem__(self, key): mykey = self.random ^ key if mykey not in self.dict: self.dict[mykey] = self.default() return self.dict[mykey] def get(self, key, default): mykey = self.random ^ key if mykey not in self.dict: return default return self.dict[mykey] def __setitem__(self, key, item): mykey = self.random ^ key self.dict[mykey] = item def getkeys(self): return [self.random ^ i for i in self.dict] def __str__(self): return f'{[(self.random ^ i, self.dict[i]) for i in self.dict]}' # Solver function def solve(): n, k = map(int, input().split()) a = [] for i in range(n): val = int(input()) a.append(val) d = mydict() heap = [] heapify(heap) numbers = n - k + 1 for i in range(n - numbers + 1): d[a[i]] += 1 ans = -10 ** 18 for i in d.getkeys(): if(d[i] == 1): heappush(heap, -i) ans = max(ans, i) if(ans == -10 ** 18): print("Nothing\n") else: print(str(ans) + "\n") l = 0 for i in range(n - numbers + 1, n): d[a[l]] -= 1 d[a[i]] += 1 if(d[a[i]] == 1): heappush(heap, -a[i]) if(d[a[l]] == 1): heappush(heap, -a[l]) l += 1 if(len(heap) == 0): print("Nothing\n") continue ans = -10 ** 18 while heap: val = -heappop(heap) if(d[val] != 1): continue ans = val heappush(heap, -ans) break if(ans == -10 ** 18): print("Nothing\n") else: print(str(ans) + "\n") # Main for _ in range(1): solve()
Codeforces Beta Round 63 (Div. 2)
CF
2,011
1
256
Subsegments
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in $${\mathcal {O}}(\log n)$$, which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
The first line contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n) — the number of array elements and the length of the segment. Then follow n lines: the i-th one contains a single number ai ( - 109 ≤ ai ≤ 109).
Print n–k + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 … ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
null
null
[{"input": "5 3\n1\n2\n2\n3\n3", "output": "1\n3\n2"}, {"input": "6 4\n3\n3\n3\n4\n4\n2", "output": "4\nNothing\n3"}]
1,800
["data structures", "implementation"]
49
[{"input": "5 3\r\n1\r\n2\r\n2\r\n3\r\n3\r\n", "output": "1\r\n3\r\n2\r\n"}, {"input": "6 4\r\n3\r\n3\r\n3\r\n4\r\n4\r\n2\r\n", "output": "4\r\nNothing\r\n3\r\n"}, {"input": "10 3\r\n-55\r\n-35\r\n-80\r\n91\r\n-96\r\n-93\r\n-39\r\n-77\r\n4\r\n29\r\n", "output": "-35\r\n91\r\n91\r\n91\r\n-39\r\n-39\r\n4\r\n29\r\n"}, {"input": "10 3\r\n-13\r\n26\r\n-97\r\n-38\r\n43\r\n-12\r\n80\r\n3\r\n8\r\n45\r\n", "output": "26\r\n26\r\n43\r\n43\r\n80\r\n80\r\n80\r\n45\r\n"}, {"input": "10 3\r\n-84\r\n25\r\n-25\r\n8\r\n60\r\n-74\r\n-98\r\n48\r\n-55\r\n38\r\n", "output": "25\r\n25\r\n60\r\n60\r\n60\r\n48\r\n48\r\n48\r\n"}, {"input": "10 3\r\n-62\r\n-81\r\n46\r\n22\r\n-84\r\n19\r\n-86\r\n44\r\n-84\r\n-73\r\n", "output": "46\r\n46\r\n46\r\n22\r\n19\r\n44\r\n44\r\n44\r\n"}, {"input": "10 3\r\n-6\r\n2\r\n79\r\n-49\r\n86\r\n13\r\n-31\r\n-71\r\n57\r\n93\r\n", "output": "79\r\n79\r\n86\r\n86\r\n86\r\n13\r\n57\r\n93\r\n"}, {"input": "10 3\r\n-38\r\n68\r\n-77\r\n57\r\n-35\r\n28\r\n-61\r\n-9\r\n3\r\n60\r\n", "output": "68\r\n68\r\n57\r\n57\r\n28\r\n28\r\n3\r\n60\r\n"}, {"input": "10 3\r\n2\r\n-100\r\n50\r\n-85\r\n-48\r\n68\r\n-96\r\n-31\r\n85\r\n-29\r\n", "output": "50\r\n50\r\n50\r\n68\r\n68\r\n68\r\n85\r\n85\r\n"}, {"input": "10 3\r\n-20\r\n-63\r\n-64\r\n45\r\n-84\r\n-13\r\n79\r\n-31\r\n70\r\n-100\r\n", "output": "-20\r\n45\r\n45\r\n45\r\n79\r\n79\r\n79\r\n70\r\n"}]
false
stdio
null
true
501/D
501
D
PyPy 3
TESTS
4
124
0
96055495
def sum(BIT, i): s = 0 while i > 0: s += BIT[i] i -= i & (-i) return s def update(BIT, i, v): while i < len(BIT): BIT[i] += v i += i & (-i) def find(fen, k): curr = 0 ans = 0 prevsum = 0 for i in range(19, -1, -1): if ((curr + (1 << i) < n) and fen[curr + (1 << i)] + prevsum < k): ans = curr + (1 << i) curr = ans prevsum += fen[curr] return ans + 1 def Rank(x,BIT) : return sum(BIT,x) n = int(input()) p = list(map(int, input().split())) q = list(map(int, input().split())) factp = [] factq = [] BIT = [0] * (n + 1) for j in range(n): update(BIT,j+1,1) for val in p: factp.append(Rank(val+1,BIT)-1) update(BIT,val+1,-1) BIT = [0] * (n + 1) for j in range(n): update(BIT,j+1,1) for val in q: factq.append(Rank(val+1,BIT)-1) update(BIT,val+1,-1) carry = 0 for i in range(n - 1, -1, -1): radix = n - i factp[i] = factp[i] + factq[i] + carry if factp[i] < radix: carru = 0 else: carry = 1 factp[i] -= radix BIT = [0] * (n + 1) for j in range(n): update(BIT,j+1,1) res=[] for i in range(n): k = factp[i]+1 res.append(find(BIT,k)-1) update(BIT,res[-1]+1,-1) print(*res)
44
811
27,340,800
68433253
import sys class SegmTree(): def __init__(self, array=None): size = len(array) N = 1 while N < size: N <<= 1 self.N = N self.tree = [0] * (2*self.N) for i in range(size): self.tree[i+self.N] = array[i] self.build() def build(self): for i in range(self.N - 1, 0, -1): self.tree[i] = self.tree[i<<1] + self.tree[i<<1|1] def add(self, i, value=1): i += self.N while i > 0: self.tree[i] += value i >>= 1 def get_sum(self, l, r): N = self.N l += N r += N result = 0 while l < r: if l & 1: result += self.tree[l] l += 1 if r & 1: r -= 1 result += self.tree[r] l >>= 1 r >>= 1 return result def find_kth_nonzero(self, k): i = 1 if k < 1 or k > self.tree[1]: return -1 while i < self.N: i <<= 1 if self.tree[i] < k: k -= self.tree[i] i |= 1 return i - self.N reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ n = int(input()) p = list(map(int, input().split())) q = list(map(int, input().split())) ord_p = [0] * n ord_q = [0] * n st = SegmTree([1] * n) for i, val in enumerate(p): ord_p[i] = st.get_sum(0, val) st.add(val, -1) st = SegmTree([1] * n) for i, val in enumerate(q): ord_q[i] = st.get_sum(0, val) st.add(val, -1) transfer = 0 for i in range(n-1, -1, -1): radix = n-i ord_p[i] = ord_p[i] + ord_q[i] + transfer if ord_p[i] < radix: transfer = 0 else: transfer = 1 ord_p[i] -= radix st = SegmTree([1] * n) for i in range(n): k = ord_p[i] + 1 ord_q[i] = st.find_kth_nonzero(k) st.add(ord_q[i], -1) print(*ord_q)
Codeforces Round 285 (Div. 2)
CF
2,015
2
256
Misha and Permutations Summation
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation $${\it Perm}((\mathit{Ord}(p)+\mathit{Ord}(q))\bmod n!)$$, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order. For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0) Misha has two permutations, p and q. Your task is to find their sum. Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
The first line contains an integer n (1 ≤ n ≤ 200 000). The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p. The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
null
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0). In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is $$Perm((0+0) \bmod 2) = Perm(0) = (0,1)$$. In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is $${ \it Perm}((0+1){\bmod {2}})={\it Perm}(1)=(1,0)$$. Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0). In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is $$Perm((3+5) \bmod 6) = Perm(2) = (1,0,2)$$.
[{"input": "2\n0 1\n0 1", "output": "0 1"}, {"input": "2\n0 1\n1 0", "output": "1 0"}, {"input": "3\n1 2 0\n2 1 0", "output": "1 0 2"}]
2,000
["data structures"]
44
[{"input": "2\r\n0 1\r\n0 1\r\n", "output": "0 1\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "1 0\r\n"}, {"input": "3\r\n1 2 0\r\n2 1 0\r\n", "output": "1 0 2\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "1 0\r\n"}, {"input": "5\r\n2 1 3 0 4\r\n2 0 4 3 1\r\n", "output": "4 2 0 3 1\r\n"}, {"input": "3\r\n0 2 1\r\n1 0 2\r\n", "output": "1 2 0\r\n"}, {"input": "4\r\n2 0 1 3\r\n0 2 1 3\r\n", "output": "2 1 0 3\r\n"}, {"input": "1\r\n0\r\n0\r\n", "output": "0\r\n"}, {"input": "75\r\n71 69 34 23 13 68 19 45 40 6 74 11 53 24 27 7 50 5 70 47 4 21 25 54 62 30 17 33 52 16 67 15 14 57 38 18 48 29 58 1 8 36 2 35 56 43 44 39 20 10 0 64 3 61 32 22 37 28 26 55 63 60 49 42 59 51 66 46 73 41 9 65 12 72 31\r\n48 2 4 57 73 15 60 32 66 19 21 68 31 10 59 20 16 14 34 51 37 58 28 49 35 46 1 23 74 42 62 72 45 30 11 13 71 12 22 65 55 7 36 26 39 33 44 53 69 52 25 56 54 17 41 70 8 0 3 67 9 64 40 27 6 61 63 5 24 38 18 47 29 43 50\r\n", "output": "44 72 38 6 13 10 5 3 33 28 22 8 14 39 16 31 66 26 34 27 48 2 55 35 24 74 21 57 54 62 60 17 65 15 51 40 49 43 73 69 64 41 36 53 9 70 7 12 11 61 32 46 59 0 68 4 42 20 23 45 67 52 1 56 58 30 47 50 18 71 25 19 29 63 37\r\n"}, {"input": "84\r\n83 4 68 34 24 2 48 38 22 51 5 62 31 67 66 53 49 70 9 71 46 41 30 8 50 17 28 79 15 80 32 43 14 74 29 42 81 60 56 65 23 0 77 76 58 78 1 11 37 27 75 35 18 73 54 20 57 33 36 6 61 69 64 55 39 10 3 45 13 26 59 82 21 25 63 52 16 44 47 72 19 12 7 40\r\n63 41 80 52 36 45 17 69 22 66 37 21 46 44 64 9 48 74 58 81 10 32 0 78 68 35 26 83 14 25 79 33 13 29 75 61 6 11 49 1 31 71 59 47 62 54 2 55 30 3 53 4 16 34 77 12 43 8 28 56 18 42 5 76 82 73 27 20 70 40 23 51 38 39 7 67 50 19 60 72 24 65 57 15\r\n", "output": "62 46 66 3 61 47 68 21 44 30 41 0 78 27 45 65 13 56 70 64 58 80 31 4 32 54 57 77 28 20 24 81 29 17 22 19 6 75 15 69 55 74 52 39 40 49 1 67 76 33 43 34 26 23 50 35 12 38 71 53 82 16 79 59 36 5 14 72 2 83 7 37 51 60 73 25 42 63 10 48 8 9 18 11\r\n"}, {"input": "9\r\n8 5 0 1 6 7 4 2 3\r\n6 5 0 8 7 1 4 3 2\r\n", "output": "6 2 1 0 7 3 5 8 4\r\n"}, {"input": "10\r\n1 7 8 0 2 5 4 6 3 9\r\n0 8 3 7 1 6 2 4 5 9\r\n", "output": "2 6 0 8 3 1 5 7 4 9\r\n"}, {"input": "5\r\n4 3 0 1 2\r\n2 4 3 1 0\r\n", "output": "2 3 4 1 0\r\n"}, {"input": "8\r\n5 2 4 6 1 0 3 7\r\n7 4 3 0 2 6 1 5\r\n", "output": "5 0 1 6 4 7 2 3\r\n"}, {"input": "7\r\n6 0 3 1 5 4 2\r\n6 0 2 4 3 5 1\r\n", "output": "5 0 4 6 2 1 3\r\n"}, {"input": "10\r\n5 2 9 1 8 6 7 4 3 0\r\n7 4 8 9 6 3 2 1 0 5\r\n", "output": "2 8 7 1 9 4 5 0 6 3\r\n"}, {"input": "10\r\n0 1 7 3 2 5 8 6 9 4\r\n9 5 2 7 1 4 0 6 8 3\r\n", "output": "9 5 8 7 1 4 6 0 2 3\r\n"}, {"input": "8\r\n2 3 0 5 4 7 6 1\r\n6 3 2 5 0 4 7 1\r\n", "output": "0 6 4 1 5 3 2 7\r\n"}, {"input": "10\r\n7 4 6 1 0 9 2 8 5 3\r\n4 7 0 5 2 8 9 6 1 3\r\n", "output": "2 1 7 6 4 8 0 5 9 3\r\n"}, {"input": "10\r\n4 2 3 9 8 0 7 5 6 1\r\n7 3 1 2 9 8 6 4 0 5\r\n", "output": "1 6 5 2 9 0 7 8 4 3\r\n"}, {"input": "10\r\n3 5 7 0 2 8 9 6 1 4\r\n4 3 8 7 9 6 0 5 2 1\r\n", "output": "7 9 3 8 1 5 0 4 6 2\r\n"}, {"input": "10\r\n1 2 0 3 4 8 6 5 7 9\r\n5 2 9 1 6 0 4 7 3 8\r\n", "output": "6 3 9 1 5 7 4 2 0 8\r\n"}]
false
stdio
null
true
456/A
456
A
Python 3
TESTS
39
218
9,932,800
219594024
n = int(input()) prices = [] quality = [] for i in range(n): data = input() price, qual = map(int, data.split()) prices.append(price) quality.append(qual) alex = False for i in range(1, n): if ((prices[i - 1] < prices[i]) and (quality[i - 1] > quality[i])): alex = True break elif ((prices[i - 1] > prices[i]) and (quality[i] > quality[i - 1])): alex = True break else: continue if (alex): print("Happy Alex") else: print("Poor Alex")
46
124
0
205306972
a="Poor" for i in range(int(input())): b,c=input().split() if b!=c:a="Happy" print(a,"Alex")
Codeforces Round 260 (Div. 2)
CF
2,014
1
256
Laptops
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop. Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.
The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops. Next n lines contain two integers each, ai and bi (1 ≤ ai, bi ≤ n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality). All ai are distinct. All bi are distinct.
If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).
null
null
[{"input": "2\n1 2\n2 1", "output": "Happy Alex"}]
1,100
["sortings"]
46
[{"input": "2\r\n1 2\r\n2 1\r\n", "output": "Happy Alex\r\n"}, {"input": "2\r\n1 1\r\n2 2\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n2 2\r\n3 3\r\n1 1\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n3 3\r\n1 2\r\n2 1\r\n", "output": "Happy Alex\r\n"}, {"input": "1\r\n1 1\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n2 3\r\n1 1\r\n3 2\r\n", "output": "Happy Alex\r\n"}, {"input": "4\r\n4 1\r\n3 2\r\n2 3\r\n1 4\r\n", "output": "Happy Alex\r\n"}, {"input": "2\r\n2 1\r\n1 2\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n3 2\r\n1 1\r\n2 3\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 1\r\n3 3\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "Happy Alex\r\n"}]
false
stdio
null
true
456/A
456
A
Python 3
TESTS
39
124
0
226489744
from sys import stdin stream = None try: stream = open('file.txt', 'r') except: stream = stdin n = int(stream.readline()) curr_price = 0 curr_quality = 0 result = False for i in range(n): price, quality = [int(i) for i in stream.readline().split()] if (curr_price > price and curr_quality < quality) or (curr_price < price and curr_quality > quality): result = True break curr_price = price curr_quality = quality print('Happy Alex' if result else 'Poor Alex')
46
124
0
209949482
for _ in range(int(input())): i,j=input().split() if i!=j: print('Happy Alex') exit() print('Poor Alex')
Codeforces Round 260 (Div. 2)
CF
2,014
1
256
Laptops
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop. Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.
The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops. Next n lines contain two integers each, ai and bi (1 ≤ ai, bi ≤ n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality). All ai are distinct. All bi are distinct.
If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).
null
null
[{"input": "2\n1 2\n2 1", "output": "Happy Alex"}]
1,100
["sortings"]
46
[{"input": "2\r\n1 2\r\n2 1\r\n", "output": "Happy Alex\r\n"}, {"input": "2\r\n1 1\r\n2 2\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n2 2\r\n3 3\r\n1 1\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n3 3\r\n1 2\r\n2 1\r\n", "output": "Happy Alex\r\n"}, {"input": "1\r\n1 1\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n2 3\r\n1 1\r\n3 2\r\n", "output": "Happy Alex\r\n"}, {"input": "4\r\n4 1\r\n3 2\r\n2 3\r\n1 4\r\n", "output": "Happy Alex\r\n"}, {"input": "2\r\n2 1\r\n1 2\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n3 2\r\n1 1\r\n2 3\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 1\r\n3 3\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "Happy Alex\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
92
0
210752767
T=int(input()) lis=[] for _ in range(T): s=input() s=s.replace(" ","") if(s not in lis): lis.append(s) print(len(lis))
29
62
0
144856262
ans=set() for _ in range(int(input())): a=input() ans.add(a) print(len(ans))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
92
0
204007070
n=int(input()) t=[] count=0 for i in range(n): spe,col=input().split() a=spe+col if a not in t: t.append(a) count+=1 print(count)
29
62
0
144857118
n=int(input()) d=set() for i in range(n): s,k=map(str,input().split()) j=s+"-"+k d.add(j) print(len(d))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
92
0
203972022
n=int(input()) l=[] count=0 for i in range(n): fruit,colour=input().split() if l.count(fruit+colour)==0: l.append(fruit+colour) count+=1 print(count)
29
62
0
144857960
n=int(input()) l=[] for i in range(0,n): s=input() l.append(s) l=set(l) print(len(l))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
154/A
154
A
PyPy 3
TESTS
4
154
307,200
186979529
from collections import defaultdict from sys import stdin input = stdin.readline s = input().strip() k = int(input()) mp = defaultdict(set) for _ in range(k): t = input().strip() mp[t[0]].add(t[1]) mp[t[1]].add(t[0]) ans = 0; last = '' for c in s: if c in mp[last]: ans += 1 else: last = c print(ans)
42
216
4,300,800
218083045
s = input() k = int(input()) res = 0 for _ in range(k): f = input() ins = False first, second = 0, 0 for i in s: if not ins and i != f[0] and i != f[1]: continue if ins and i != f[0] and i != f[1]: ins = False MIN = min(first, second) res += MIN first, second = 0, 0 elif i == f[0] or i == f[1]: ins = True if i == f[0]: first += 1 elif i == f[1]: second += 1 if ins: MIN = min(first, second) res += MIN print(res)# 1691619870.475087
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input": "eepeeeeppppppeepeppe\r\n1\r\npe\r\n", "output": "10\r\n"}, {"input": "vefneyamdzoytemupniw\r\n13\r\nve\r\nfg\r\noi\r\nan\r\nck\r\nwx\r\npq\r\nml\r\nbt\r\nud\r\nrz\r\nsj\r\nhy\r\n", "output": "1\r\n"}, {"input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\r\n13\r\nld\r\nac\r\nnp\r\nrv\r\nmo\r\njh\r\ngb\r\nuw\r\nfq\r\nst\r\nkx\r\nzy\r\nei\r\n", "output": "11\r\n"}, {"input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\r\n4\r\nli\r\nqh\r\nad\r\nbp\r\n", "output": "4\r\n"}, {"input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\r\n5\r\nmb\r\nho\r\nxu\r\njv\r\nyp\r\n", "output": "37\r\n"}, {"input": "z\r\n0\r\n", "output": "0\r\n"}, {"input": "t\r\n13\r\nzx\r\nig\r\nyq\r\nbd\r\nph\r\nar\r\nne\r\nwo\r\ntk\r\njl\r\ncv\r\nfs\r\nmu\r\n", "output": "0\r\n"}, {"input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\r\n13\r\njr\r\nnf\r\nyk\r\ntq\r\nwe\r\nil\r\ngu\r\npb\r\naz\r\nxm\r\nhc\r\nvd\r\nso\r\n", "output": "0\r\n"}, {"input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\r\n1\r\nni\r\n", "output": "28\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n1\r\nab\r\n", "output": "75\r\n"}]
false
stdio
null
true
995/A
995
A
PyPy 3
TESTS
3
124
0
61332619
n,k=map(int,input().split()) was=int(k) m=[] for i in range(4): m.append(list(map(int,input().split()))) res=[] for i in range(n): if m[1][i] != 0: if m[1][i] == m[0][i]: res.append([m[1][i],1,i+1]) m[1][i] = 0 k-=1 for i in range(n): if m[2][i] != 0: if m[2][i] == m[3][i]: res.append([m[2][i],4,i+1]) m[2][i] = 0 k-=1 if True: while True: for i in range(n): if m[1][i] != 0: if i != 0: if m[1][i-1] == 0: res.append([m[1][i],2,i]) m[1][i-1] = m[1][i] m[1][i] = 0 if m[0][i-1] == m[1][i-1]: res.append([m[1][i-1],1,i]) m[0][i-1] = m[1][i-1] m[1][i-1] = 0 k-=1 elif i == 0: if m[2][i] == 0: res.append([m[1][i],3,i+1]) m[2][i] = m[1][i] m[1][i] = 0 if m[3][i] == m[2][i]: res.append([m[2][i],4,i+1]) m[3][i] = m[2][i] m[2][i] = 0 k-=1 for i in range(n): if m[2][i] != 0: if i != n-1: if m[2][i+1] == 0: res.append([m[2][i],3,i+2]) m[2][i+1] = m[2][i] m[2][i] = 0 if m[3][i+1] == m[2][i+1]: res.append([m[2][i+1],4,i+2]) m[3][i+1] = m[2][i+1] m[2][i+1] = 0 k-=1 else: if m[1][i] == 0: res.append([m[2][i],1,i+1]) m[1][i] = m[2][i] m[2][i] = 0 if m[0][i] == m[1][i]: res.append([m[1][i],0,i]) m[0][i]= m[1][i] m[1][i]=0 k-=1 if k <= 0: break if k == was: print(-1) exit(0) else: print(-1) exit(0) print(len(res)) for i in res: print(*i)
57
202
3,276,800
39642428
k, n = [int(x) for x in input().split()] a = [] for i in range(4): a.append([int(x) - 1 for x in input().split()]) pos = [] for i in range(k): pos.append([1, i]) for i in range(k): pos.append([2, k - i - 1]) lft = n ans = [] for i in range(2 * k): for j in range(2 * k): if (a[pos[j][0]][pos[j][1]] != -1) and (a[pos[j][0]][pos[j][1]] == a[pos[j][0] ^ 1][pos[j][1]]): lft -= 1 ans.append([a[pos[j][0]][pos[j][1]], pos[j][0] ^ 1, pos[j][1]]) a[pos[j][0]][pos[j][1]] = -1 if (lft == 0): break fst = -1 for j in range(2 * k): if (a[pos[j][0]][pos[j][1]] != -1) and (a[pos[(j + 1) % (2 * k)][0]][pos[(j + 1) % (2 * k)][1]] == -1): fst = j break if (fst == -1): print(-1) exit(0) for j in range(2 * k - 1): if (a[pos[(fst - j) % (2 * k)][0]][pos[(fst - j) % (2 * k)][1]] != -1): a[pos[(fst - j + 1) % (2 * k)][0]][pos[(fst - j + 1) % (2 * k)][1]] = a[pos[(fst - j) % (2 * k)][0]][pos[(fst - j) % (2 * k)][1]] a[pos[(fst - j) % (2 * k)][0]][pos[(fst - j) % (2 * k)][1]] = -1 ans.append([a[pos[(fst - j + 1) % (2 * k)][0]][pos[(fst - j + 1) % (2 * k)][1]], pos[(fst - j + 1) % (2 * k)][0], pos[(fst - j + 1) % (2 * k)][1]]) print(len(ans)) for i in ans: print(' '.join([str(x + 1) for x in i]))
Codeforces Round 492 (Div. 1) [Thanks, uDebug!]
CF
2,018
3
256
Tesla
Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem. Allen's future parking lot can be represented as a rectangle with $$$4$$$ rows and $$$n$$$ ($$$n \le 50$$$) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having $$$k$$$ ($$$k \le 2n$$$) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places. Illustration to the first example. However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space. Allen knows he will be a very busy man, and will only have time to move cars at most $$$20000$$$ times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.
The first line of the input contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 50$$$, $$$1 \le k \le 2n$$$), representing the number of columns and the number of cars, respectively. The next four lines will contain $$$n$$$ integers each between $$$0$$$ and $$$k$$$ inclusive, representing the initial state of the parking lot. The rows are numbered $$$1$$$ to $$$4$$$ from top to bottom and the columns are numbered $$$1$$$ to $$$n$$$ from left to right. In the first and last line, an integer $$$1 \le x \le k$$$ represents a parking spot assigned to car $$$x$$$ (you can only move this car to this place), while the integer $$$0$$$ represents a empty space (you can't move any car to this place). In the second and third line, an integer $$$1 \le x \le k$$$ represents initial position of car $$$x$$$, while the integer $$$0$$$ represents an empty space (you can move any car to this place). Each $$$x$$$ between $$$1$$$ and $$$k$$$ appears exactly once in the second and third line, and exactly once in the first and fourth line.
If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $$$20000$$$ car moves, then print $$$m$$$, the number of moves, on the first line. On the following $$$m$$$ lines, print the moves (one move per line) in the format $$$i$$$ $$$r$$$ $$$c$$$, which corresponds to Allen moving car $$$i$$$ to the neighboring space at row $$$r$$$ and column $$$c$$$. If it is not possible for Allen to move all the cars to the correct spaces with at most $$$20000$$$ car moves, print a single line with the integer $$$-1$$$.
null
In the first sample test case, all cars are in front of their spots except car $$$5$$$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $$$20000$$$ will be accepted. In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.
[{"input": "4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3", "output": "6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2"}, {"input": "1 2\n1\n2\n1\n2", "output": "-1"}, {"input": "1 2\n1\n1\n2\n2", "output": "2\n1 1 1\n2 4 1"}]
2,100
["constructive algorithms", "implementation"]
57
[{"input": "4 5\r\n1 2 0 4\r\n1 2 0 4\r\n5 0 0 3\r\n0 5 0 3\r\n", "output": "6\r\n1 1 1\r\n2 1 2\r\n4 1 4\r\n3 4 4\r\n5 3 2\r\n5 4 2\r\n"}, {"input": "1 2\r\n1\r\n2\r\n1\r\n2\r\n", "output": "-1\r\n"}, {"input": "1 2\r\n1\r\n1\r\n2\r\n2\r\n", "output": "2\r\n1 1 1\r\n2 4 1\r\n"}, {"input": "2 2\r\n1 0\r\n0 2\r\n0 1\r\n0 2\r\n", "output": "7\r\n2 2 1\r\n1 2 2\r\n2 3 1\r\n1 2 1\r\n2 3 2\r\n1 1 1\r\n2 4 2\r\n"}, {"input": "7 14\r\n2 11 1 14 9 8 5\r\n12 6 7 1 10 2 3\r\n14 13 9 8 5 4 11\r\n13 6 4 3 12 7 10\r\n", "output": "-1\r\n"}, {"input": "10 20\r\n18 7 3 16 5 8 19 2 20 12\r\n15 16 7 11 14 3 12 4 8 10\r\n19 18 20 1 17 9 5 2 6 13\r\n11 15 13 17 6 9 14 1 10 4\r\n", "output": "200\n9 4 6\n5 3 6\n2 3 7\n6 3 8\n13 3 9\n10 3 10\n8 2 10\n4 2 9\n12 2 8\n3 2 7\n14 2 6\n11 2 5\n7 2 4\n16 2 3\n15 2 2\n19 2 1\n18 3 1\n20 3 2\n1 3 3\n17 3 4\n5 3 5\n2 3 6\n6 3 7\n13 3 8\n17 4 4\n10 3 9\n8 3 10\n4 2 10\n12 2 9\n3 2 8\n14 2 7\n11 2 6\n7 2 5\n16 2 4\n15 2 3\n19 2 2\n18 2 1\n20 3 1\n1 3 2\n5 3 4\n2 3 5\n6 3 6\n13 3 7\n10 4 9\n16 1 4\n18 1 1\n8 3 9\n4 3 10\n12 2 10\n3 2 9\n14 2 8\n11 2 7\n7 2 6\n15 2 4\n19 2 3\n20 2 1\n1 3 1\n5 3 3\n2 3 4\n6 3 5\n13 3 6\n4 4 10\n6 4 5\n12 1 10\n8 3 8\n3 2 10\n14 2 9\n11 2 8\n7 2 7\n15 2 5\n19 2 4\n20 2 2\n1 2 1\n5 3 2\n2 3 3\n13 3 5\n8 3 7\n3 3 10\n14 2 10\n11 2 9\n7 2 8\n15 2 6\n19 2 5\n20 2 3\n1 2 2\n5 3 1\n2 3 2\n13 3 4\n8 3 6\n3 3 9\n14 3 10\n11 2 10\n7 2 9\n15 2 7\n19 2 6\n20 2 4\n1 2 3\n5 2 1\n2 3 1\n13 3 3\n13 4 3\n8 3 5\n3 3 8\n14 3 9\n11 3 10\n7 2 10\n15 2 8\n19 2 7\n20 2 5\n1 2 4\n5 2 2\n2 2 1\n19 1 7\n8 3 4\n3 3 7\n14 3 8\n11 3 9\n7 3 10\n15 2 9\n20 2 6\n1 2 5\n5 2 3\n2 2 2\n8 3 3\n3 3 6\n14 3 7\n11 3 8\n7 3 9\n15 2 10\n20 2 7\n1 2 6\n5 2 4\n2 2 3\n14 4 7\n8 3 2\n3 3 5\n11 3 7\n7 3 8\n15 3 10\n20 2 8\n1 2 7\n5 2 5\n2 2 4\n5 1 5\n8 3 1\n3 3 4\n11 3 6\n7 3 7\n15 3 9\n20 2 9\n1 2 8\n2 2 5\n20 1 9\n8 2 1\n3 3 3\n11 3 5\n7 3 6\n15 3 8\n1 2 9\n2 2 6\n8 2 2\n3 3 2\n11 3 4\n7 3 5\n15 3 7\n1 2 10\n2 2 7\n8 2 3\n3 3 1\n11 3 3\n7 3 4\n15 3 6\n1 3 10\n2 2 8\n2 1 8\n8 2 4\n3 2 1\n11 3 2\n7 3 3\n15 3 5\n1 3 9\n8 2 5\n3 2 2\n11 3 1\n7 3 2\n15 3 4\n1 3 8\n1 4 8\n11 4 1\n8 2 6\n3 2 3\n7 3 1\n15 3 3\n3 1 3\n8 1 6\n7 2 1\n15 3 2\n15 4 2\n7 2 2\n7 1 2\n"}, {"input": "2 1\r\n0 0\r\n0 0\r\n0 1\r\n0 1\r\n", "output": "1\r\n1 4 2\r\n"}, {"input": "2 3\r\n0 2\r\n0 1\r\n3 2\r\n3 1\r\n", "output": "7\r\n1 2 1\r\n2 2 2\r\n3 4 1\r\n1 3 1\r\n2 1 2\r\n1 3 2\r\n1 4 2\r\n"}, {"input": "8 12\r\n9 7 10 5 0 0 8 0\r\n11 6 5 4 1 10 2 0\r\n0 8 0 7 0 3 9 12\r\n6 4 1 2 0 11 12 3\r\n", "output": "127\n8 3 1\n7 3 3\n7 3 2\n3 3 5\n3 3 4\n3 3 3\n9 3 6\n9 3 5\n9 3 4\n12 3 7\n12 3 6\n12 3 5\n2 2 8\n10 2 7\n1 2 6\n4 2 5\n5 2 4\n6 2 3\n11 2 2\n8 2 1\n7 3 1\n3 3 2\n9 3 3\n12 3 4\n5 1 4\n2 3 8\n10 2 8\n1 2 7\n4 2 6\n6 2 4\n11 2 3\n8 2 2\n7 2 1\n3 3 1\n9 3 2\n12 3 3\n2 3 7\n10 3 8\n1 2 8\n4 2 7\n6 2 5\n11 2 4\n8 2 3\n7 2 2\n3 2 1\n9 3 1\n12 3 2\n7 1 2\n2 3 6\n10 3 7\n1 3 8\n4 2 8\n6 2 6\n11 2 5\n8 2 4\n3 2 2\n9 2 1\n12 3 1\n9 1 1\n2 3 5\n10 3 6\n1 3 7\n4 3 8\n6 2 7\n11 2 6\n8 2 5\n3 2 3\n12 2 1\n2 3 4\n10 3 5\n1 3 6\n4 3 7\n6 2 8\n11 2 7\n8 2 6\n3 2 4\n12 2 2\n2 4 4\n10 3 4\n1 3 5\n4 3 6\n6 3 8\n11 2 8\n8 2 7\n3 2 5\n12 2 3\n8 1 7\n10 3 3\n1 3 4\n4 3 5\n6 3 7\n11 3 8\n3 2 6\n12 2 4\n10 3 2\n1 3 3\n4 3 4\n6 3 6\n11 3 7\n3 2 7\n12 2 5\n1 4 3\n10 3 1\n4 3 3\n6 3 5\n11 3 6\n3 2 8\n12 2 6\n11 4 6\n10 2 1\n4 3 2\n6 3 4\n3 3 8\n12 2 7\n3 4 8\n4 4 2\n10 2 2\n6 3 3\n12 2 8\n10 2 3\n6 3 2\n12 3 8\n10 1 3\n6 3 1\n12 3 7\n6 4 1\n12 4 7\n"}, {"input": "1 1\r\n0\r\n1\r\n0\r\n1\r\n", "output": "2\r\n1 3 1\r\n1 4 1\r\n"}, {"input": "2 4\r\n3 4\r\n2 1\r\n3 4\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "3 5\r\n2 1 5\r\n5 3 2\r\n4 0 1\r\n0 4 3\r\n", "output": "18\r\n4 3 2\r\n5 3 1\r\n3 2 1\r\n2 2 2\r\n1 2 3\r\n4 4 2\r\n5 3 2\r\n3 3 1\r\n2 2 1\r\n1 2 2\r\n5 3 3\r\n3 3 2\r\n2 1 1\r\n1 1 2\r\n5 2 3\r\n3 3 3\r\n5 1 3\r\n3 4 3\r\n"}, {"input": "8 15\r\n15 13 0 14 2 7 4 9\r\n11 5 14 2 15 12 10 13\r\n1 9 7 4 3 8 0 6\r\n3 1 12 6 10 11 8 5\r\n", "output": "134\n6 3 7\n13 3 8\n10 2 8\n12 2 7\n15 2 6\n2 2 5\n14 2 4\n5 2 3\n11 2 2\n1 2 1\n9 3 1\n7 3 2\n4 3 3\n3 3 4\n8 3 5\n6 3 6\n2 1 5\n14 1 4\n13 3 7\n10 3 8\n12 2 8\n15 2 7\n5 2 4\n11 2 3\n1 2 2\n9 2 1\n7 3 1\n4 3 2\n3 3 3\n8 3 4\n6 3 5\n13 3 6\n10 3 7\n12 3 8\n15 2 8\n5 2 5\n11 2 4\n1 2 3\n9 2 2\n7 2 1\n4 3 1\n3 3 2\n8 3 3\n6 3 4\n6 4 4\n13 3 5\n10 3 6\n12 3 7\n15 3 8\n5 2 6\n11 2 5\n1 2 4\n9 2 3\n7 2 2\n4 2 1\n3 3 1\n8 3 2\n3 4 1\n13 3 4\n10 3 5\n12 3 6\n15 3 7\n5 2 7\n11 2 6\n1 2 5\n9 2 4\n7 2 3\n4 2 2\n8 3 1\n10 4 5\n13 3 3\n12 3 5\n15 3 6\n5 2 8\n11 2 7\n1 2 6\n9 2 5\n7 2 4\n4 2 3\n8 2 1\n13 3 2\n12 3 4\n15 3 5\n5 3 8\n11 2 8\n1 2 7\n9 2 6\n7 2 5\n4 2 4\n8 2 2\n5 4 8\n13 3 1\n12 3 3\n15 3 4\n11 3 8\n1 2 8\n9 2 7\n7 2 6\n4 2 5\n8 2 3\n7 1 6\n12 4 3\n13 2 1\n15 3 3\n11 3 7\n1 3 8\n9 2 8\n4 2 6\n8 2 4\n9 1 8\n13 2 2\n15 3 2\n11 3 6\n1 3 7\n4 2 7\n8 2 5\n4 1 7\n11 4 6\n13 1 2\n15 3 1\n1 3 6\n8 2 6\n15 2 1\n1 3 5\n8 2 7\n15 1 1\n1 3 4\n8 2 8\n1 3 3\n8 3 8\n1 3 2\n8 3 7\n1 4 2\n8 4 7\n"}, {"input": "8 14\r\n12 7 0 5 4 3 13 6\r\n6 9 7 0 4 12 2 14\r\n10 8 13 1 5 0 11 3\r\n2 0 8 10 9 14 1 11\r\n", "output": "163\n4 2 4\n12 2 5\n2 2 6\n14 2 7\n11 3 6\n3 3 7\n14 2 8\n2 2 7\n12 2 6\n4 2 5\n7 2 4\n9 2 3\n6 2 2\n10 2 1\n8 3 1\n13 3 2\n1 3 3\n5 3 4\n11 3 5\n3 3 6\n4 1 5\n14 3 8\n2 2 8\n12 2 7\n7 2 5\n9 2 4\n6 2 3\n10 2 2\n8 2 1\n13 3 1\n1 3 2\n5 3 3\n11 3 4\n3 3 5\n14 3 7\n2 3 8\n12 2 8\n7 2 6\n9 2 5\n6 2 4\n10 2 3\n8 2 2\n13 2 1\n1 3 1\n5 3 2\n11 3 3\n3 3 4\n14 3 6\n2 3 7\n12 3 8\n7 2 7\n9 2 6\n6 2 5\n10 2 4\n8 2 3\n13 2 2\n1 2 1\n5 3 1\n11 3 2\n3 3 3\n14 4 6\n2 3 6\n12 3 7\n7 2 8\n9 2 7\n6 2 6\n10 2 5\n8 2 4\n13 2 3\n1 2 2\n5 2 1\n11 3 1\n3 3 2\n2 3 5\n12 3 6\n7 3 8\n9 2 8\n6 2 7\n10 2 6\n8 2 5\n13 2 4\n1 2 3\n5 2 2\n11 2 1\n3 3 1\n2 3 4\n12 3 5\n7 3 7\n9 3 8\n6 2 8\n10 2 7\n8 2 6\n13 2 5\n1 2 4\n5 2 3\n11 2 2\n3 2 1\n6 1 8\n2 3 3\n12 3 4\n7 3 6\n9 3 7\n10 2 8\n8 2 7\n13 2 6\n1 2 5\n5 2 4\n11 2 3\n3 2 2\n5 1 4\n2 3 2\n12 3 3\n7 3 5\n9 3 6\n10 3 8\n8 2 8\n13 2 7\n1 2 6\n11 2 4\n3 2 3\n13 1 7\n2 3 1\n12 3 2\n7 3 4\n9 3 5\n10 3 7\n8 3 8\n1 2 7\n11 2 5\n3 2 4\n2 4 1\n9 4 5\n12 3 1\n7 3 3\n10 3 6\n8 3 7\n1 2 8\n11 2 6\n3 2 5\n12 2 1\n7 3 2\n10 3 5\n8 3 6\n1 3 8\n11 2 7\n3 2 6\n3 1 6\n12 1 1\n7 3 1\n10 3 4\n8 3 5\n1 3 7\n11 2 8\n1 4 7\n10 4 4\n7 2 1\n8 3 4\n11 3 8\n11 4 8\n7 2 2\n8 3 3\n7 1 2\n8 4 3\n"}, {"input": "10 1\r\n0 0 1 0 0 0 0 0 0 0\r\n0 0 1 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "1\r\n1 1 3\r\n"}, {"input": "10 10\r\n0 2 0 9 0 10 6 0 0 0\r\n0 9 2 0 0 0 4 0 6 0\r\n0 0 10 0 7 1 5 8 3 0\r\n1 5 3 4 7 0 8 0 0 0\r\n", "output": "106\n9 2 1\n2 2 2\n4 2 6\n4 2 5\n4 2 4\n4 2 3\n6 2 8\n6 2 7\n6 2 6\n6 2 5\n6 2 4\n10 3 2\n10 3 1\n7 3 4\n7 3 3\n7 3 2\n1 3 5\n1 3 4\n1 3 3\n5 3 6\n5 3 5\n5 3 4\n8 3 7\n8 3 6\n8 3 5\n3 3 8\n3 3 7\n3 3 6\n2 1 2\n6 2 5\n4 2 4\n9 2 2\n10 2 1\n7 3 1\n1 3 2\n5 3 3\n8 3 4\n3 3 5\n6 2 6\n4 2 5\n9 2 3\n10 2 2\n7 2 1\n1 3 1\n5 3 2\n8 3 3\n3 3 4\n1 4 1\n5 4 2\n6 2 7\n4 2 6\n9 2 4\n10 2 3\n7 2 2\n8 3 2\n3 3 3\n3 4 3\n6 1 7\n9 1 4\n4 2 7\n10 2 4\n7 2 3\n8 3 1\n4 2 8\n10 2 5\n7 2 4\n8 2 1\n4 2 9\n10 2 6\n7 2 5\n8 2 2\n10 1 6\n4 2 10\n7 2 6\n8 2 3\n4 3 10\n7 2 7\n8 2 4\n4 3 9\n7 2 8\n8 2 5\n4 3 8\n7 2 9\n8 2 6\n4 3 7\n7 2 10\n8 2 7\n4 3 6\n7 3 10\n8 2 8\n4 3 5\n7 3 9\n8 2 9\n4 3 4\n7 3 8\n8 2 10\n4 4 4\n7 3 7\n8 3 10\n7 3 6\n8 3 9\n7 3 5\n8 3 8\n7 4 5\n8 3 7\n8 4 7\n"}, {"input": "50 1\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n", "output": "68\n1 2 17\n1 2 16\n1 2 15\n1 2 14\n1 2 13\n1 2 12\n1 2 11\n1 2 10\n1 2 9\n1 2 8\n1 2 7\n1 2 6\n1 2 5\n1 2 4\n1 2 3\n1 2 2\n1 2 1\n1 2 2\n1 2 3\n1 2 4\n1 2 5\n1 2 6\n1 2 7\n1 2 8\n1 2 9\n1 2 10\n1 2 11\n1 2 12\n1 2 13\n1 2 14\n1 2 15\n1 2 16\n1 2 17\n1 2 18\n1 2 19\n1 2 20\n1 2 21\n1 2 22\n1 2 23\n1 2 24\n1 2 25\n1 2 26\n1 2 27\n1 2 28\n1 2 29\n1 2 30\n1 2 31\n1 2 32\n1 2 33\n1 2 34\n1 2 35\n1 2 36\n1 2 37\n1 2 38\n1 2 39\n1 2 40\n1 2 41\n1 2 42\n1 2 43\n1 2 44\n1 2 45\n1 2 46\n1 2 47\n1 2 48\n1 2 49\n1 2 50\n1 3 50\n1 4 50\n"}, {"input": "40 80\r\n38 45 18 59 53 44 49 27 46 63 42 61 26 39 29 7 52 79 11 73 24 69 55 43 20 32 37 25 57 19 1 54 4 22 36 16 71 15 65 12\r\n46 1 52 54 27 3 40 10 8 41 72 17 11 44 28 73 55 65 60 13 12 43 16 26 34 53 50 15 62 35 33 48 58 42 57 80 21 51 64 74\r\n22 29 4 18 69 36 31 68 77 61 37 6 70 59 78 19 25 71 79 56 30 38 66 2 32 7 47 75 67 39 9 76 49 23 63 24 5 45 20 14\r\n33 5 50 8 13 17 14 74 10 66 34 58 41 72 2 60 51 77 21 56 70 40 9 35 64 78 68 6 47 23 75 80 28 30 3 76 67 48 62 31\r\n", "output": "3120\n56 4 20\n30 3 20\n38 3 21\n66 3 22\n2 3 23\n32 3 24\n7 3 25\n47 3 26\n75 3 27\n67 3 28\n39 3 29\n9 3 30\n76 3 31\n49 3 32\n23 3 33\n63 3 34\n24 3 35\n5 3 36\n45 3 37\n20 3 38\n14 3 39\n74 3 40\n64 2 40\n51 2 39\n21 2 38\n80 2 37\n57 2 36\n42 2 35\n58 2 34\n48 2 33\n33 2 32\n35 2 31\n62 2 30\n15 2 29\n50 2 28\n53 2 27\n34 2 26\n26 2 25\n16 2 24\n43 2 23\n12 2 22\n13 2 21\n60 2 20\n65 2 19\n55 2 18\n73 2 17\n28 2 16\n44 2 15\n11 2 14\n17 2 13\n72 2 12\n41 2 11\n8 2 10\n10 2 9\n40 2 8\n3 2 7\n27 2 6\n54 2 5\n52 2 4\n1 2 3\n46 2 2\n22 2 1\n29 3 1\n4 3 2\n18 3 3\n69 3 4\n36 3 5\n31 3 6\n68 3 7\n77 3 8\n61 3 9\n37 3 10\n6 3 11\n70 3 12\n59 3 13\n78 3 14\n19 3 15\n25 3 16\n71 3 17\n79 3 18\n30 3 19\n38 3 20\n66 3 21\n2 3 22\n32 3 23\n7 3 24\n47 3 25\n75 3 26\n67 3 27\n39 3 28\n9 3 29\n76 3 30\n49 3 31\n23 3 32\n63 3 33\n24 3 34\n5 3 35\n45 3 36\n20 3 37\n14 3 38\n74 3 39\n64 3 40\n51 2 40\n21 2 39\n80 2 38\n57 2 37\n42 2 36\n58 2 35\n48 2 34\n33 2 33\n35 2 32\n62 2 31\n15 2 30\n50 2 29\n53 2 28\n34 2 27\n26 2 26\n16 2 25\n43 2 24\n12 2 23\n13 2 22\n60 2 21\n65 2 20\n55 2 19\n73 2 18\n28 2 17\n44 2 16\n11 2 15\n17 2 14\n72 2 13\n41 2 12\n8 2 11\n10 2 10\n40 2 9\n3 2 8\n27 2 7\n54 2 6\n52 2 5\n1 2 4\n46 2 3\n22 2 2\n29 2 1\n4 3 1\n18 3 2\n69 3 3\n36 3 4\n31 3 5\n68 3 6\n77 3 7\n61 3 8\n37 3 9\n6 3 10\n70 3 11\n59 3 12\n78 3 13\n19 3 14\n25 3 15\n71 3 16\n79 3 17\n30 3 18\n38 3 19\n66 3 20\n2 3 21\n32 3 22\n7 3 23\n47 3 24\n75 3 25\n67 3 26\n39 3 27\n9 3 28\n76 3 29\n49 3 30\n23 3 31\n63 3 32\n24 3 33\n5 3 34\n45 3 35\n20 3 36\n14 3 37\n43 1 24\n74 3 38\n64 3 39\n51 3 40\n21 2 40\n80 2 39\n57 2 38\n42 2 37\n58 2 36\n48 2 35\n33 2 34\n35 2 33\n62 2 32\n15 2 31\n50 2 30\n53 2 29\n34 2 28\n26 2 27\n16 2 26\n12 2 24\n13 2 23\n60 2 22\n65 2 21\n55 2 20\n73 2 19\n28 2 18\n44 2 17\n11 2 16\n17 2 15\n72 2 14\n41 2 13\n8 2 12\n10 2 11\n40 2 10\n3 2 9\n27 2 8\n54 2 7\n52 2 6\n1 2 5\n46 2 4\n22 2 3\n29 2 2\n4 2 1\n18 3 1\n69 3 2\n36 3 3\n31 3 4\n68 3 5\n77 3 6\n61 3 7\n37 3 8\n6 3 9\n70 3 10\n59 3 11\n78 3 12\n19 3 13\n25 3 14\n71 3 15\n79 3 16\n30 3 17\n38 3 18\n66 3 19\n2 3 20\n32 3 21\n7 3 22\n47 3 23\n75 3 24\n67 3 25\n39 3 26\n9 3 27\n76 3 28\n49 3 29\n23 3 30\n63 3 31\n24 3 32\n5 3 33\n45 3 34\n20 3 35\n14 3 36\n23 4 30\n27 1 8\n74 3 37\n64 3 38\n51 3 39\n21 3 40\n80 2 40\n57 2 39\n42 2 38\n58 2 37\n48 2 36\n33 2 35\n35 2 34\n62 2 33\n15 2 32\n50 2 31\n53 2 30\n34 2 29\n26 2 28\n16 2 27\n12 2 25\n13 2 24\n60 2 23\n65 2 22\n55 2 21\n73 2 20\n28 2 19\n44 2 18\n11 2 17\n17 2 16\n72 2 15\n41 2 14\n8 2 13\n10 2 12\n40 2 11\n3 2 10\n54 2 8\n52 2 7\n1 2 6\n46 2 5\n22 2 4\n29 2 3\n4 2 2\n18 2 1\n69 3 1\n36 3 2\n31 3 3\n68 3 4\n77 3 5\n61 3 6\n37 3 7\n6 3 8\n70 3 9\n59 3 10\n78 3 11\n19 3 12\n25 3 13\n71 3 14\n79 3 15\n30 3 16\n38 3 17\n66 3 18\n2 3 19\n32 3 20\n7 3 21\n47 3 22\n75 3 23\n67 3 24\n39 3 25\n9 3 26\n76 3 27\n49 3 28\n63 3 30\n24 3 31\n5 3 32\n45 3 33\n20 3 34\n14 3 35\n73 1 20\n74 3 36\n64 3 37\n51 3 38\n21 3 39\n80 3 40\n57 2 40\n42 2 39\n58 2 38\n48 2 37\n33 2 36\n35 2 35\n62 2 34\n15 2 33\n50 2 32\n53 2 31\n34 2 30\n26 2 29\n16 2 28\n12 2 26\n13 2 25\n60 2 24\n65 2 23\n55 2 22\n28 2 20\n44 2 19\n11 2 18\n17 2 17\n72 2 16\n41 2 15\n8 2 14\n10 2 13\n40 2 12\n3 2 11\n54 2 9\n52 2 8\n1 2 7\n46 2 6\n22 2 5\n29 2 4\n4 2 3\n18 2 2\n69 2 1\n36 3 1\n31 3 2\n68 3 3\n77 3 4\n61 3 5\n37 3 6\n6 3 7\n70 3 8\n59 3 9\n78 3 10\n19 3 11\n25 3 12\n71 3 13\n79 3 14\n30 3 15\n38 3 16\n66 3 17\n2 3 18\n32 3 19\n7 3 20\n47 3 21\n75 3 22\n67 3 23\n39 3 24\n9 3 25\n76 3 26\n49 3 27\n63 3 29\n24 3 30\n5 3 31\n45 3 32\n20 3 33\n14 3 34\n74 3 35\n64 3 36\n51 3 37\n21 3 38\n80 3 39\n57 3 40\n42 2 40\n58 2 39\n48 2 38\n33 2 37\n35 2 36\n62 2 35\n15 2 34\n50 2 33\n53 2 32\n34 2 31\n26 2 30\n16 2 29\n12 2 27\n13 2 26\n60 2 25\n65 2 24\n55 2 23\n28 2 21\n44 2 20\n11 2 19\n17 2 18\n72 2 17\n41 2 16\n8 2 15\n10 2 14\n40 2 13\n3 2 12\n54 2 10\n52 2 9\n1 2 8\n46 2 7\n22 2 6\n29 2 5\n4 2 4\n18 2 3\n69 2 2\n36 2 1\n31 3 1\n68 3 2\n77 3 3\n61 3 4\n37 3 5\n6 3 6\n70 3 7\n59 3 8\n78 3 9\n19 3 10\n25 3 11\n71 3 12\n79 3 13\n30 3 14\n38 3 15\n66 3 16\n2 3 17\n32 3 18\n7 3 19\n47 3 20\n75 3 21\n67 3 22\n39 3 23\n9 3 24\n76 3 25\n49 3 26\n63 3 28\n24 3 29\n5 3 30\n45 3 31\n20 3 32\n14 3 33\n11 1 19\n18 1 3\n55 1 23\n74 3 34\n64 3 35\n51 3 36\n21 3 37\n80 3 38\n57 3 39\n42 3 40\n58 2 40\n48 2 39\n33 2 38\n35 2 37\n62 2 36\n15 2 35\n50 2 34\n53 2 33\n34 2 32\n26 2 31\n16 2 30\n12 2 28\n13 2 27\n60 2 26\n65 2 25\n28 2 22\n44 2 21\n17 2 19\n72 2 18\n41 2 17\n8 2 16\n10 2 15\n40 2 14\n3 2 13\n54 2 11\n52 2 10\n1 2 9\n46 2 8\n22 2 7\n29 2 6\n4 2 5\n69 2 3\n36 2 2\n31 2 1\n68 3 1\n77 3 2\n61 3 3\n37 3 4\n6 3 5\n70 3 6\n59 3 7\n78 3 8\n19 3 9\n25 3 10\n71 3 11\n79 3 12\n30 3 13\n38 3 14\n66 3 15\n2 3 16\n32 3 17\n7 3 18\n47 3 19\n75 3 20\n67 3 21\n39 3 22\n9 3 23\n76 3 24\n49 3 25\n63 3 27\n24 3 28\n5 3 29\n45 3 30\n20 3 31\n14 3 32\n9 4 23\n74 3 33\n64 3 34\n51 3 35\n21 3 36\n80 3 37\n57 3 38\n42 3 39\n58 3 40\n48 2 40\n33 2 39\n35 2 38\n62 2 37\n15 2 36\n50 2 35\n53 2 34\n34 2 33\n26 2 32\n16 2 31\n12 2 29\n13 2 28\n60 2 27\n65 2 26\n28 2 23\n44 2 22\n17 2 20\n72 2 19\n41 2 18\n8 2 17\n10 2 16\n40 2 15\n3 2 14\n54 2 12\n52 2 11\n1 2 10\n46 2 9\n22 2 8\n29 2 7\n4 2 6\n69 2 4\n36 2 3\n31 2 2\n68 2 1\n77 3 1\n61 3 2\n37 3 3\n6 3 4\n70 3 5\n59 3 6\n78 3 7\n19 3 8\n25 3 9\n71 3 10\n79 3 11\n30 3 12\n38 3 13\n66 3 14\n2 3 15\n32 3 16\n7 3 17\n47 3 18\n75 3 19\n67 3 20\n39 3 21\n76 3 23\n49 3 24\n63 3 26\n24 3 27\n5 3 28\n45 3 29\n20 3 30\n14 3 31\n2 4 15\n46 1 9\n74 3 32\n64 3 33\n51 3 34\n21 3 35\n80 3 36\n57 3 37\n42 3 38\n58 3 39\n48 3 40\n33 2 40\n35 2 39\n62 2 38\n15 2 37\n50 2 36\n53 2 35\n34 2 34\n26 2 33\n16 2 32\n12 2 30\n13 2 29\n60 2 28\n65 2 27\n28 2 24\n44 2 23\n17 2 21\n72 2 20\n41 2 19\n8 2 18\n10 2 17\n40 2 16\n3 2 15\n54 2 13\n52 2 12\n1 2 11\n22 2 9\n29 2 8\n4 2 7\n69 2 5\n36 2 4\n31 2 3\n68 2 2\n77 2 1\n61 3 1\n37 3 2\n6 3 3\n70 3 4\n59 3 5\n78 3 6\n19 3 7\n25 3 8\n71 3 9\n79 3 10\n30 3 11\n38 3 12\n66 3 13\n32 3 15\n7 3 16\n47 3 17\n75 3 18\n67 3 19\n39 3 20\n76 3 22\n49 3 23\n63 3 25\n24 3 26\n5 3 27\n45 3 28\n20 3 29\n14 3 30\n74 3 31\n64 3 32\n51 3 33\n21 3 34\n80 3 35\n57 3 36\n42 3 37\n58 3 38\n48 3 39\n33 3 40\n35 2 40\n62 2 39\n15 2 38\n50 2 37\n53 2 36\n34 2 35\n26 2 34\n16 2 33\n12 2 31\n13 2 30\n60 2 29\n65 2 28\n28 2 25\n44 2 24\n17 2 22\n72 2 21\n41 2 20\n8 2 19\n10 2 18\n40 2 17\n3 2 16\n54 2 14\n52 2 13\n1 2 12\n22 2 10\n29 2 9\n4 2 8\n69 2 6\n36 2 5\n31 2 4\n68 2 3\n77 2 2\n61 2 1\n37 3 1\n6 3 2\n70 3 3\n59 3 4\n78 3 5\n19 3 6\n25 3 7\n71 3 8\n79 3 9\n30 3 10\n38 3 11\n66 3 12\n32 3 14\n7 3 15\n47 3 16\n75 3 17\n67 3 18\n39 3 19\n76 3 21\n49 3 22\n63 3 24\n24 3 25\n5 3 26\n45 3 27\n20 3 28\n14 3 29\n15 1 38\n74 3 30\n64 3 31\n51 3 32\n21 3 33\n80 3 34\n57 3 35\n42 3 36\n58 3 37\n48 3 38\n33 3 39\n35 3 40\n62 2 40\n50 2 38\n53 2 37\n34 2 36\n26 2 35\n16 2 34\n12 2 32\n13 2 31\n60 2 30\n65 2 29\n28 2 26\n44 2 25\n17 2 23\n72 2 22\n41 2 21\n8 2 20\n10 2 19\n40 2 18\n3 2 17\n54 2 15\n52 2 14\n1 2 13\n22 2 11\n29 2 10\n4 2 9\n69 2 7\n36 2 6\n31 2 5\n68 2 4\n77 2 3\n61 2 2\n37 2 1\n6 3 1\n70 3 2\n59 3 3\n78 3 4\n19 3 5\n25 3 6\n71 3 7\n79 3 8\n30 3 9\n38 3 10\n66 3 11\n32 3 13\n7 3 14\n47 3 15\n75 3 16\n67 3 17\n39 3 18\n76 3 20\n49 3 21\n63 3 23\n24 3 24\n5 3 25\n45 3 26\n20 3 27\n14 3 28\n48 4 38\n74 3 29\n64 3 30\n51 3 31\n21 3 32\n80 3 33\n57 3 34\n42 3 35\n58 3 36\n33 3 38\n35 3 39\n62 3 40\n50 2 39\n53 2 38\n34 2 37\n26 2 36\n16 2 35\n12 2 33\n13 2 32\n60 2 31\n65 2 30\n28 2 27\n44 2 26\n17 2 24\n72 2 23\n41 2 22\n8 2 21\n10 2 20\n40 2 19\n3 2 18\n54 2 16\n52 2 15\n1 2 14\n22 2 12\n29 2 11\n4 2 10\n69 2 8\n36 2 7\n31 2 6\n68 2 5\n77 2 4\n61 2 3\n37 2 2\n6 2 1\n70 3 1\n59 3 2\n78 3 3\n19 3 4\n25 3 5\n71 3 6\n79 3 7\n30 3 8\n38 3 9\n66 3 10\n32 3 12\n7 3 13\n47 3 14\n75 3 15\n67 3 16\n39 3 17\n76 3 19\n49 3 20\n63 3 22\n24 3 23\n5 3 24\n45 3 25\n20 3 26\n14 3 27\n66 4 10\n74 3 28\n64 3 29\n51 3 30\n21 3 31\n80 3 32\n57 3 33\n42 3 34\n58 3 35\n33 3 37\n35 3 38\n62 3 39\n50 2 40\n53 2 39\n34 2 38\n26 2 37\n16 2 36\n12 2 34\n13 2 33\n60 2 32\n65 2 31\n28 2 28\n44 2 27\n17 2 25\n72 2 24\n41 2 23\n8 2 22\n10 2 21\n40 2 20\n3 2 19\n54 2 17\n52 2 16\n1 2 15\n22 2 13\n29 2 12\n4 2 11\n69 2 9\n36 2 8\n31 2 7\n68 2 6\n77 2 5\n61 2 4\n37 2 3\n6 2 2\n70 2 1\n59 3 1\n78 3 2\n19 3 3\n25 3 4\n71 3 5\n79 3 6\n30 3 7\n38 3 8\n32 3 11\n7 3 12\n47 3 13\n75 3 14\n67 3 15\n39 3 16\n76 3 18\n49 3 19\n63 3 21\n24 3 22\n5 3 23\n45 3 24\n20 3 25\n14 3 26\n16 1 36\n62 4 39\n80 4 32\n74 3 27\n64 3 28\n51 3 29\n21 3 30\n57 3 32\n42 3 33\n58 3 34\n33 3 36\n35 3 37\n50 3 40\n53 2 40\n34 2 39\n26 2 38\n12 2 35\n13 2 34\n60 2 33\n65 2 32\n28 2 29\n44 2 28\n17 2 26\n72 2 25\n41 2 24\n8 2 23\n10 2 22\n40 2 21\n3 2 20\n54 2 18\n52 2 17\n1 2 16\n22 2 14\n29 2 13\n4 2 12\n69 2 10\n36 2 9\n31 2 8\n68 2 7\n77 2 6\n61 2 5\n37 2 4\n6 2 3\n70 2 2\n59 2 1\n78 3 1\n19 3 2\n25 3 3\n71 3 4\n79 3 5\n30 3 6\n38 3 7\n32 3 10\n7 3 11\n47 3 12\n75 3 13\n67 3 14\n39 3 15\n76 3 17\n49 3 18\n63 3 20\n24 3 21\n5 3 22\n45 3 23\n20 3 24\n14 3 25\n52 1 17\n74 3 26\n64 3 27\n51 3 28\n21 3 29\n57 3 31\n42 3 32\n58 3 33\n33 3 35\n35 3 36\n50 3 39\n53 3 40\n34 2 40\n26 2 39\n12 2 36\n13 2 35\n60 2 34\n65 2 33\n28 2 30\n44 2 29\n17 2 27\n72 2 26\n41 2 25\n8 2 24\n10 2 23\n40 2 22\n3 2 21\n54 2 19\n1 2 17\n22 2 15\n29 2 14\n4 2 13\n69 2 11\n36 2 10\n31 2 9\n68 2 8\n77 2 7\n61 2 6\n37 2 5\n6 2 4\n70 2 3\n59 2 2\n78 2 1\n19 3 1\n25 3 2\n71 3 3\n79 3 4\n30 3 5\n38 3 6\n32 3 9\n7 3 10\n47 3 11\n75 3 12\n67 3 13\n39 3 14\n76 3 16\n49 3 17\n63 3 19\n24 3 20\n5 3 21\n45 3 22\n20 3 23\n14 3 24\n74 3 25\n64 3 26\n51 3 27\n21 3 28\n57 3 30\n42 3 31\n58 3 32\n33 3 34\n35 3 35\n50 3 38\n53 3 39\n34 3 40\n26 2 40\n12 2 37\n13 2 36\n60 2 35\n65 2 34\n28 2 31\n44 2 30\n17 2 28\n72 2 27\n41 2 26\n8 2 25\n10 2 24\n40 2 23\n3 2 22\n54 2 20\n1 2 18\n22 2 16\n29 2 15\n4 2 14\n69 2 12\n36 2 11\n31 2 10\n68 2 9\n77 2 8\n61 2 7\n37 2 6\n6 2 5\n70 2 4\n59 2 3\n78 2 2\n19 2 1\n25 3 1\n71 3 2\n79 3 3\n30 3 4\n38 3 5\n32 3 8\n7 3 9\n47 3 10\n75 3 11\n67 3 12\n39 3 13\n76 3 15\n49 3 16\n63 3 18\n24 3 19\n5 3 20\n45 3 21\n20 3 22\n14 3 23\n29 1 15\n74 3 24\n64 3 25\n51 3 26\n21 3 27\n57 3 29\n42 3 30\n58 3 31\n33 3 33\n35 3 34\n50 3 37\n53 3 38\n34 3 39\n26 3 40\n12 2 38\n13 2 37\n60 2 36\n65 2 35\n28 2 32\n44 2 31\n17 2 29\n72 2 28\n41 2 27\n8 2 26\n10 2 25\n40 2 24\n3 2 23\n54 2 21\n1 2 19\n22 2 17\n4 2 15\n69 2 13\n36 2 12\n31 2 11\n68 2 10\n77 2 9\n61 2 8\n37 2 7\n6 2 6\n70 2 5\n59 2 4\n78 2 3\n19 2 2\n25 2 1\n71 3 1\n79 3 2\n30 3 3\n38 3 4\n32 3 7\n7 3 8\n47 3 9\n75 3 10\n67 3 11\n39 3 12\n76 3 14\n49 3 15\n63 3 17\n24 3 18\n5 3 19\n45 3 20\n20 3 21\n14 3 22\n59 1 4\n64 4 25\n74 3 23\n51 3 25\n21 3 26\n57 3 28\n42 3 29\n58 3 30\n33 3 32\n35 3 33\n50 3 36\n53 3 37\n34 3 38\n26 3 39\n12 2 39\n13 2 38\n60 2 37\n65 2 36\n28 2 33\n44 2 32\n17 2 30\n72 2 29\n41 2 28\n8 2 27\n10 2 26\n40 2 25\n3 2 24\n54 2 22\n1 2 20\n22 2 18\n4 2 16\n69 2 14\n36 2 13\n31 2 12\n68 2 11\n77 2 10\n61 2 9\n37 2 8\n6 2 7\n70 2 6\n78 2 4\n19 2 3\n25 2 2\n71 2 1\n79 3 1\n30 3 2\n38 3 3\n32 3 6\n7 3 7\n47 3 8\n75 3 9\n67 3 10\n39 3 11\n76 3 13\n49 3 14\n63 3 16\n24 3 17\n5 3 18\n45 3 19\n20 3 20\n14 3 21\n74 3 22\n51 3 24\n21 3 25\n57 3 27\n42 3 28\n58 3 29\n33 3 31\n35 3 32\n50 3 35\n53 3 36\n34 3 37\n26 3 38\n12 2 40\n13 2 39\n60 2 38\n65 2 37\n28 2 34\n44 2 33\n17 2 31\n72 2 30\n41 2 29\n8 2 28\n10 2 27\n40 2 26\n3 2 25\n54 2 23\n1 2 21\n22 2 19\n4 2 17\n69 2 15\n36 2 14\n31 2 13\n68 2 12\n77 2 11\n61 2 10\n37 2 9\n6 2 8\n70 2 7\n78 2 5\n19 2 4\n25 2 3\n71 2 2\n79 2 1\n30 3 1\n38 3 2\n32 3 5\n7 3 6\n47 3 7\n75 3 8\n67 3 9\n39 3 10\n76 3 12\n49 3 13\n63 3 15\n24 3 16\n5 3 17\n45 3 18\n20 3 19\n14 3 20\n12 1 40\n74 3 21\n51 3 23\n21 3 24\n57 3 26\n42 3 27\n58 3 28\n33 3 30\n35 3 31\n50 3 34\n53 3 35\n34 3 36\n26 3 37\n13 2 40\n60 2 39\n65 2 38\n28 2 35\n44 2 34\n17 2 32\n72 2 31\n41 2 30\n8 2 29\n10 2 28\n40 2 27\n3 2 26\n54 2 24\n1 2 22\n22 2 20\n4 2 18\n69 2 16\n36 2 15\n31 2 14\n68 2 13\n77 2 12\n61 2 11\n37 2 10\n6 2 9\n70 2 8\n78 2 6\n19 2 5\n25 2 4\n71 2 3\n79 2 2\n30 2 1\n38 3 1\n32 3 4\n7 3 5\n47 3 6\n75 3 7\n67 3 8\n39 3 9\n76 3 11\n49 3 12\n63 3 14\n24 3 15\n5 3 16\n45 3 17\n20 3 18\n14 3 19\n74 3 20\n51 3 22\n21 3 23\n57 3 25\n42 3 26\n58 3 27\n33 3 29\n35 3 30\n50 3 33\n53 3 34\n34 3 35\n26 3 36\n13 3 40\n60 2 40\n65 2 39\n28 2 36\n44 2 35\n17 2 33\n72 2 32\n41 2 31\n8 2 30\n10 2 29\n40 2 28\n3 2 27\n54 2 25\n1 2 23\n22 2 21\n4 2 19\n69 2 17\n36 2 16\n31 2 15\n68 2 14\n77 2 13\n61 2 12\n37 2 11\n6 2 10\n70 2 9\n78 2 7\n19 2 6\n25 2 5\n71 2 4\n79 2 3\n30 2 2\n38 2 1\n32 3 3\n7 3 4\n47 3 5\n75 3 6\n67 3 7\n39 3 8\n76 3 10\n49 3 11\n63 3 13\n24 3 14\n5 3 15\n45 3 16\n20 3 17\n14 3 18\n38 1 1\n61 1 12\n65 1 39\n74 3 19\n51 3 21\n21 3 22\n57 3 24\n42 3 25\n58 3 26\n33 3 28\n35 3 29\n50 3 32\n53 3 33\n34 3 34\n26 3 35\n13 3 39\n60 3 40\n28 2 37\n44 2 36\n17 2 34\n72 2 33\n41 2 32\n8 2 31\n10 2 30\n40 2 29\n3 2 28\n54 2 26\n1 2 24\n22 2 22\n4 2 20\n69 2 18\n36 2 17\n31 2 16\n68 2 15\n77 2 14\n37 2 12\n6 2 11\n70 2 10\n78 2 8\n19 2 7\n25 2 6\n71 2 5\n79 2 4\n30 2 3\n32 3 2\n7 3 3\n47 3 4\n75 3 5\n67 3 6\n39 3 7\n76 3 9\n49 3 10\n63 3 12\n24 3 13\n5 3 14\n45 3 15\n20 3 16\n14 3 17\n74 3 18\n51 3 20\n21 3 21\n57 3 23\n42 3 24\n58 3 25\n33 3 27\n35 3 28\n50 3 31\n53 3 32\n34 3 33\n26 3 34\n13 3 38\n60 3 39\n28 2 38\n44 2 37\n17 2 35\n72 2 34\n41 2 33\n8 2 32\n10 2 31\n40 2 30\n3 2 29\n54 2 27\n1 2 25\n22 2 23\n4 2 21\n69 2 19\n36 2 18\n31 2 17\n68 2 16\n77 2 15\n37 2 13\n6 2 12\n70 2 11\n78 2 9\n19 2 8\n25 2 7\n71 2 6\n79 2 5\n30 2 4\n32 3 1\n7 3 2\n47 3 3\n75 3 4\n67 3 5\n39 3 6\n76 3 8\n49 3 9\n63 3 11\n24 3 12\n5 3 13\n45 3 14\n20 3 15\n14 3 16\n74 3 17\n51 3 19\n21 3 20\n57 3 22\n42 3 23\n58 3 24\n33 3 26\n35 3 27\n50 3 30\n53 3 31\n34 3 32\n26 3 33\n13 3 37\n60 3 38\n28 2 39\n44 2 38\n17 2 36\n72 2 35\n41 2 34\n8 2 33\n10 2 32\n40 2 31\n3 2 30\n54 2 28\n1 2 26\n22 2 24\n4 2 22\n69 2 20\n36 2 19\n31 2 18\n68 2 17\n77 2 16\n37 2 14\n6 2 13\n70 2 12\n78 2 10\n19 2 9\n25 2 8\n71 2 7\n79 2 6\n30 2 5\n32 2 1\n7 3 1\n47 3 2\n75 3 3\n67 3 4\n39 3 5\n76 3 7\n49 3 8\n63 3 10\n24 3 11\n5 3 12\n45 3 13\n20 3 14\n14 3 15\n74 3 16\n51 3 18\n21 3 19\n57 3 21\n42 3 22\n58 3 23\n33 3 25\n35 3 26\n50 3 29\n53 3 30\n34 3 31\n26 3 32\n13 3 36\n60 3 37\n28 2 40\n44 2 39\n17 2 37\n72 2 36\n41 2 35\n8 2 34\n10 2 33\n40 2 32\n3 2 31\n54 2 29\n1 2 27\n22 2 25\n4 2 23\n69 2 21\n36 2 20\n31 2 19\n68 2 18\n77 2 17\n37 2 15\n6 2 14\n70 2 13\n78 2 11\n19 2 10\n25 2 9\n71 2 8\n79 2 7\n30 2 6\n32 2 2\n7 2 1\n47 3 1\n75 3 2\n67 3 3\n39 3 4\n76 3 6\n49 3 7\n63 3 9\n24 3 10\n5 3 11\n45 3 12\n20 3 13\n14 3 14\n21 4 19\n74 3 15\n51 3 17\n57 3 20\n42 3 21\n58 3 22\n33 3 24\n35 3 25\n50 3 28\n53 3 29\n34 3 30\n26 3 31\n13 3 35\n60 3 36\n28 3 40\n44 2 40\n17 2 38\n72 2 37\n41 2 36\n8 2 35\n10 2 34\n40 2 33\n3 2 32\n54 2 30\n1 2 28\n22 2 26\n4 2 24\n69 2 22\n36 2 21\n31 2 20\n68 2 19\n77 2 18\n37 2 16\n6 2 15\n70 2 14\n78 2 12\n19 2 11\n25 2 10\n71 2 9\n79 2 8\n30 2 7\n32 2 3\n7 2 2\n47 2 1\n75 3 1\n67 3 2\n39 3 3\n76 3 5\n49 3 6\n63 3 8\n24 3 9\n5 3 10\n45 3 11\n20 3 12\n14 3 13\n51 4 17\n69 1 22\n74 3 14\n57 3 19\n42 3 20\n58 3 21\n33 3 23\n35 3 24\n50 3 27\n53 3 28\n34 3 29\n26 3 30\n13 3 34\n60 3 35\n28 3 39\n44 3 40\n17 2 39\n72 2 38\n41 2 37\n8 2 36\n10 2 35\n40 2 34\n3 2 33\n54 2 31\n1 2 29\n22 2 27\n4 2 25\n36 2 22\n31 2 21\n68 2 20\n77 2 19\n37 2 17\n6 2 16\n70 2 15\n78 2 13\n19 2 12\n25 2 11\n71 2 10\n79 2 9\n30 2 8\n32 2 4\n7 2 3\n47 2 2\n75 2 1\n67 3 1\n39 3 2\n76 3 4\n49 3 5\n63 3 7\n24 3 8\n5 3 9\n45 3 10\n20 3 11\n14 3 12\n35 4 24\n74 3 13\n57 3 18\n42 3 19\n58 3 20\n33 3 22\n50 3 26\n53 3 27\n34 3 28\n26 3 29\n13 3 33\n60 3 34\n28 3 38\n44 3 39\n17 2 40\n72 2 39\n41 2 38\n8 2 37\n10 2 36\n40 2 35\n3 2 34\n54 2 32\n1 2 30\n22 2 28\n4 2 26\n36 2 23\n31 2 22\n68 2 21\n77 2 20\n37 2 18\n6 2 17\n70 2 16\n78 2 14\n19 2 13\n25 2 12\n71 2 11\n79 2 10\n30 2 9\n32 2 5\n7 2 4\n47 2 3\n75 2 2\n67 2 1\n39 3 1\n76 3 3\n49 3 4\n63 3 6\n24 3 7\n5 3 8\n45 3 9\n20 3 10\n14 3 11\n54 1 32\n74 3 12\n57 3 17\n42 3 18\n58 3 19\n33 3 21\n50 3 25\n53 3 26\n34 3 27\n26 3 28\n13 3 32\n60 3 33\n28 3 37\n44 3 38\n17 3 40\n72 2 40\n41 2 39\n8 2 38\n10 2 37\n40 2 36\n3 2 35\n1 2 31\n22 2 29\n4 2 27\n36 2 24\n31 2 23\n68 2 22\n77 2 21\n37 2 19\n6 2 18\n70 2 17\n78 2 15\n19 2 14\n25 2 13\n71 2 12\n79 2 11\n30 2 10\n32 2 6\n7 2 5\n47 2 4\n75 2 3\n67 2 2\n39 2 1\n76 3 2\n49 3 3\n63 3 5\n24 3 6\n5 3 7\n45 3 8\n20 3 9\n14 3 10\n1 1 31\n74 3 11\n57 3 16\n42 3 17\n58 3 18\n33 3 20\n50 3 24\n53 3 25\n34 3 26\n26 3 27\n13 3 31\n60 3 32\n28 3 36\n44 3 37\n17 3 39\n72 3 40\n41 2 40\n8 2 39\n10 2 38\n40 2 37\n3 2 36\n22 2 30\n4 2 28\n36 2 25\n31 2 24\n68 2 23\n77 2 22\n37 2 20\n6 2 19\n70 2 18\n78 2 16\n19 2 15\n25 2 14\n71 2 13\n79 2 12\n30 2 11\n32 2 7\n7 2 6\n47 2 5\n75 2 4\n67 2 3\n39 2 2\n76 3 1\n49 3 2\n63 3 4\n24 3 5\n5 3 6\n45 3 7\n20 3 8\n14 3 9\n74 3 10\n57 3 15\n42 3 16\n58 3 17\n33 3 19\n50 3 23\n53 3 24\n34 3 25\n26 3 26\n13 3 30\n60 3 31\n28 3 35\n44 3 36\n17 3 38\n72 3 39\n41 3 40\n8 2 40\n10 2 39\n40 2 38\n3 2 37\n22 2 31\n4 2 29\n36 2 26\n31 2 25\n68 2 24\n77 2 23\n37 2 21\n6 2 20\n70 2 19\n78 2 17\n19 2 16\n25 2 15\n71 2 14\n79 2 13\n30 2 12\n32 2 8\n7 2 7\n47 2 6\n75 2 5\n67 2 4\n39 2 3\n76 2 1\n49 3 1\n63 3 3\n24 3 4\n5 3 5\n45 3 6\n20 3 7\n14 3 8\n74 3 9\n57 3 14\n42 3 15\n58 3 16\n33 3 18\n50 3 22\n53 3 23\n34 3 24\n26 3 25\n13 3 29\n60 3 30\n28 3 34\n44 3 35\n17 3 37\n72 3 38\n41 3 39\n8 3 40\n10 2 40\n40 2 39\n3 2 38\n22 2 32\n4 2 30\n36 2 27\n31 2 26\n68 2 25\n77 2 24\n37 2 22\n6 2 21\n70 2 20\n78 2 18\n19 2 17\n25 2 16\n71 2 15\n79 2 14\n30 2 13\n32 2 9\n7 2 8\n47 2 7\n75 2 6\n67 2 5\n39 2 4\n76 2 2\n49 2 1\n63 3 2\n24 3 3\n5 3 4\n45 3 5\n20 3 6\n14 3 7\n14 4 7\n74 3 8\n57 3 13\n42 3 14\n58 3 15\n33 3 17\n50 3 21\n53 3 22\n34 3 23\n26 3 24\n13 3 28\n60 3 29\n28 3 33\n44 3 34\n17 3 36\n72 3 37\n41 3 38\n8 3 39\n10 3 40\n40 2 40\n3 2 39\n22 2 33\n4 2 31\n36 2 28\n31 2 27\n68 2 26\n77 2 25\n37 2 23\n6 2 22\n70 2 21\n78 2 19\n19 2 18\n25 2 17\n71 2 16\n79 2 15\n30 2 14\n32 2 10\n7 2 9\n47 2 8\n75 2 7\n67 2 6\n39 2 5\n76 2 3\n49 2 2\n63 3 1\n24 3 2\n5 3 3\n45 3 4\n20 3 5\n28 4 33\n74 4 8\n57 3 12\n42 3 13\n58 3 14\n33 3 16\n50 3 20\n53 3 21\n34 3 22\n26 3 23\n13 3 27\n60 3 28\n44 3 33\n17 3 35\n72 3 36\n41 3 37\n8 3 38\n10 3 39\n40 3 40\n3 2 40\n22 2 34\n4 2 32\n36 2 29\n31 2 28\n68 2 27\n77 2 26\n37 2 24\n6 2 23\n70 2 22\n78 2 20\n19 2 19\n25 2 18\n71 2 17\n79 2 16\n30 2 15\n32 2 11\n7 2 10\n47 2 9\n75 2 8\n67 2 7\n39 2 6\n76 2 4\n49 2 3\n63 2 1\n24 3 1\n5 3 2\n45 3 3\n20 3 4\n5 4 2\n22 1 34\n57 3 11\n42 3 12\n58 3 13\n33 3 15\n50 3 19\n53 3 20\n34 3 21\n26 3 22\n13 3 26\n60 3 27\n44 3 32\n17 3 34\n72 3 35\n41 3 36\n8 3 37\n10 3 38\n40 3 39\n3 3 40\n4 2 33\n36 2 30\n31 2 29\n68 2 28\n77 2 27\n37 2 25\n6 2 24\n70 2 23\n78 2 21\n19 2 20\n25 2 19\n71 2 18\n79 2 17\n30 2 16\n32 2 12\n7 2 11\n47 2 10\n75 2 9\n67 2 8\n39 2 7\n76 2 5\n49 2 4\n63 2 2\n24 2 1\n45 3 2\n20 3 3\n4 1 33\n57 3 10\n42 3 11\n58 3 12\n33 3 14\n50 3 18\n53 3 19\n34 3 20\n26 3 21\n13 3 25\n60 3 26\n44 3 31\n17 3 33\n72 3 34\n41 3 35\n8 3 36\n10 3 37\n40 3 38\n3 3 39\n36 2 31\n31 2 30\n68 2 29\n77 2 28\n37 2 26\n6 2 25\n70 2 24\n78 2 22\n19 2 21\n25 2 20\n71 2 19\n79 2 18\n30 2 17\n32 2 13\n7 2 12\n47 2 11\n75 2 10\n67 2 9\n39 2 8\n76 2 6\n49 2 5\n63 2 3\n24 2 2\n45 3 1\n20 3 2\n58 4 12\n79 1 18\n57 3 9\n42 3 10\n33 3 13\n50 3 17\n53 3 18\n34 3 19\n26 3 20\n13 3 24\n60 3 25\n44 3 30\n17 3 32\n72 3 33\n41 3 34\n8 3 35\n10 3 36\n40 3 37\n3 3 38\n36 2 32\n31 2 31\n68 2 30\n77 2 29\n37 2 27\n6 2 26\n70 2 25\n78 2 23\n19 2 22\n25 2 21\n71 2 20\n30 2 18\n32 2 14\n7 2 13\n47 2 12\n75 2 11\n67 2 10\n39 2 9\n76 2 7\n49 2 6\n63 2 4\n24 2 3\n45 2 1\n20 3 1\n37 1 27\n57 3 8\n42 3 9\n33 3 12\n50 3 16\n53 3 17\n34 3 18\n26 3 19\n13 3 23\n60 3 24\n44 3 29\n17 3 31\n72 3 32\n41 3 33\n8 3 34\n10 3 35\n40 3 36\n3 3 37\n36 2 33\n31 2 32\n68 2 31\n77 2 30\n6 2 27\n70 2 26\n78 2 24\n19 2 23\n25 2 22\n71 2 21\n30 2 19\n32 2 15\n7 2 14\n47 2 13\n75 2 12\n67 2 11\n39 2 10\n76 2 8\n49 2 7\n63 2 5\n24 2 4\n45 2 2\n20 2 1\n45 1 2\n49 1 7\n57 3 7\n42 3 8\n33 3 11\n50 3 15\n53 3 16\n34 3 17\n26 3 18\n13 3 22\n60 3 23\n44 3 28\n17 3 30\n72 3 31\n41 3 32\n8 3 33\n10 3 34\n40 3 35\n3 3 36\n36 2 34\n31 2 33\n68 2 32\n77 2 31\n6 2 28\n70 2 27\n78 2 25\n19 2 24\n25 2 23\n71 2 22\n30 2 20\n32 2 16\n7 2 15\n47 2 14\n75 2 13\n67 2 12\n39 2 11\n76 2 9\n63 2 6\n24 2 5\n20 2 2\n57 3 6\n42 3 7\n33 3 10\n50 3 14\n53 3 15\n34 3 16\n26 3 17\n13 3 21\n60 3 22\n44 3 27\n17 3 29\n72 3 30\n41 3 31\n8 3 32\n10 3 33\n40 3 34\n3 3 35\n36 2 35\n31 2 34\n68 2 33\n77 2 32\n6 2 29\n70 2 28\n78 2 26\n19 2 25\n25 2 24\n71 2 23\n30 2 21\n32 2 17\n7 2 16\n47 2 15\n75 2 14\n67 2 13\n39 2 12\n76 2 10\n63 2 7\n24 2 6\n20 2 3\n3 4 35\n7 1 16\n36 1 35\n57 3 5\n42 3 6\n33 3 9\n50 3 13\n53 3 14\n34 3 15\n26 3 16\n13 3 20\n60 3 21\n44 3 26\n17 3 28\n72 3 29\n41 3 30\n8 3 31\n10 3 32\n40 3 33\n31 2 35\n68 2 34\n77 2 33\n6 2 30\n70 2 29\n78 2 27\n19 2 26\n25 2 25\n71 2 24\n30 2 22\n32 2 18\n47 2 16\n75 2 15\n67 2 14\n39 2 13\n76 2 11\n63 2 8\n24 2 7\n20 2 4\n57 3 4\n42 3 5\n33 3 8\n50 3 12\n53 3 13\n34 3 14\n26 3 15\n13 3 19\n60 3 20\n44 3 25\n17 3 27\n72 3 28\n41 3 29\n8 3 30\n10 3 31\n40 3 32\n31 2 36\n68 2 35\n77 2 34\n6 2 31\n70 2 30\n78 2 28\n19 2 27\n25 2 26\n71 2 25\n30 2 23\n32 2 19\n47 2 17\n75 2 16\n67 2 15\n39 2 14\n76 2 12\n63 2 9\n24 2 8\n20 2 5\n39 1 14\n57 3 3\n42 3 4\n33 3 7\n50 3 11\n53 3 12\n34 3 13\n26 3 14\n13 3 18\n60 3 19\n44 3 24\n17 3 26\n72 3 27\n41 3 28\n8 3 29\n10 3 30\n40 3 31\n31 2 37\n68 2 36\n77 2 35\n6 2 32\n70 2 31\n78 2 29\n19 2 28\n25 2 27\n71 2 26\n30 2 24\n32 2 20\n47 2 18\n75 2 17\n67 2 16\n76 2 13\n63 2 10\n24 2 9\n20 2 6\n63 1 10\n57 3 2\n42 3 3\n33 3 6\n50 3 10\n53 3 11\n34 3 12\n26 3 13\n13 3 17\n60 3 18\n44 3 23\n17 3 25\n72 3 26\n41 3 27\n8 3 28\n10 3 29\n40 3 30\n31 2 38\n68 2 37\n77 2 36\n6 2 33\n70 2 32\n78 2 30\n19 2 29\n25 2 28\n71 2 27\n30 2 25\n32 2 21\n47 2 19\n75 2 18\n67 2 17\n76 2 14\n24 2 10\n20 2 7\n25 1 28\n57 3 1\n42 3 2\n33 3 5\n50 3 9\n53 3 10\n34 3 11\n26 3 12\n13 3 16\n60 3 17\n44 3 22\n17 3 24\n72 3 25\n41 3 26\n8 3 27\n10 3 28\n40 3 29\n31 2 39\n68 2 38\n77 2 37\n6 2 34\n70 2 33\n78 2 31\n19 2 30\n71 2 28\n30 2 26\n32 2 22\n47 2 20\n75 2 19\n67 2 18\n76 2 15\n24 2 11\n20 2 8\n19 1 30\n34 4 11\n57 2 1\n42 3 1\n33 3 4\n50 3 8\n53 3 9\n26 3 11\n13 3 15\n60 3 16\n44 3 21\n17 3 23\n72 3 24\n41 3 25\n8 3 26\n10 3 27\n40 3 28\n31 2 40\n68 2 39\n77 2 38\n6 2 35\n70 2 34\n78 2 32\n71 2 29\n30 2 27\n32 2 23\n47 2 21\n75 2 20\n67 2 19\n76 2 16\n24 2 12\n20 2 9\n60 4 16\n57 2 2\n42 2 1\n33 3 3\n50 3 7\n53 3 8\n26 3 10\n13 3 14\n44 3 20\n17 3 22\n72 3 23\n41 3 24\n8 3 25\n10 3 26\n40 3 27\n31 3 40\n68 2 40\n77 2 39\n6 2 36\n70 2 35\n78 2 33\n71 2 30\n30 2 28\n32 2 24\n47 2 22\n75 2 21\n67 2 20\n76 2 17\n24 2 13\n20 2 10\n31 4 40\n57 2 3\n42 2 2\n33 3 2\n50 3 6\n53 3 7\n26 3 9\n13 3 13\n44 3 19\n17 3 21\n72 3 22\n41 3 23\n8 3 24\n10 3 25\n40 3 26\n68 3 40\n77 2 40\n6 2 37\n70 2 36\n78 2 34\n71 2 31\n30 2 29\n32 2 25\n47 2 23\n75 2 22\n67 2 21\n76 2 18\n24 2 14\n20 2 11\n57 2 4\n42 2 3\n33 3 1\n50 3 5\n53 3 6\n26 3 8\n13 3 12\n44 3 18\n17 3 20\n72 3 21\n41 3 22\n8 3 23\n10 3 24\n40 3 25\n68 3 39\n77 3 40\n6 2 38\n70 2 37\n78 2 35\n71 2 32\n30 2 30\n32 2 26\n47 2 24\n75 2 23\n67 2 22\n76 2 19\n24 2 15\n20 2 12\n32 1 26\n33 4 1\n57 2 5\n42 2 4\n50 3 4\n53 3 5\n26 3 7\n13 3 11\n44 3 17\n17 3 19\n72 3 20\n41 3 21\n8 3 22\n10 3 23\n40 3 24\n68 3 38\n77 3 39\n6 2 39\n70 2 38\n78 2 36\n71 2 33\n30 2 31\n47 2 25\n75 2 24\n67 2 23\n76 2 20\n24 2 16\n20 2 13\n57 2 6\n42 2 5\n50 3 3\n53 3 4\n26 3 6\n13 3 10\n44 3 16\n17 3 18\n72 3 19\n41 3 20\n8 3 21\n10 3 22\n40 3 23\n68 3 37\n77 3 38\n6 2 40\n70 2 39\n78 2 37\n71 2 34\n30 2 32\n47 2 26\n75 2 25\n67 2 24\n76 2 21\n24 2 17\n20 2 14\n50 4 3\n57 2 7\n42 2 6\n53 3 3\n26 3 5\n13 3 9\n44 3 15\n17 3 17\n72 3 18\n41 3 19\n8 3 20\n10 3 21\n40 3 22\n68 3 36\n77 3 37\n6 3 40\n70 2 40\n78 2 38\n71 2 35\n30 2 33\n47 2 27\n75 2 26\n67 2 25\n76 2 22\n24 2 18\n20 2 15\n40 4 22\n57 2 8\n42 2 7\n53 3 2\n26 3 4\n13 3 8\n44 3 14\n17 3 16\n72 3 17\n41 3 18\n8 3 19\n10 3 20\n68 3 35\n77 3 36\n6 3 39\n70 3 40\n78 2 39\n71 2 36\n30 2 34\n47 2 28\n75 2 27\n67 2 26\n76 2 23\n24 2 19\n20 2 16\n57 2 9\n42 2 8\n53 3 1\n26 3 3\n13 3 7\n44 3 13\n17 3 15\n72 3 16\n41 3 17\n8 3 18\n10 3 19\n68 3 34\n77 3 35\n6 3 38\n70 3 39\n78 2 40\n71 2 37\n30 2 35\n47 2 29\n75 2 28\n67 2 27\n76 2 24\n24 2 20\n20 2 17\n71 1 37\n57 2 10\n42 2 9\n53 2 1\n26 3 2\n13 3 6\n44 3 12\n17 3 14\n72 3 15\n41 3 16\n8 3 17\n10 3 18\n68 3 33\n77 3 34\n6 3 37\n70 3 38\n78 3 40\n30 2 36\n47 2 30\n75 2 29\n67 2 28\n76 2 25\n24 2 21\n20 2 18\n24 1 21\n57 2 11\n42 2 10\n53 2 2\n26 3 1\n13 3 5\n44 3 11\n17 3 13\n72 3 14\n41 3 15\n8 3 16\n10 3 17\n68 3 32\n77 3 33\n6 3 36\n70 3 37\n78 3 39\n30 2 37\n47 2 31\n75 2 30\n67 2 29\n76 2 26\n20 2 19\n13 4 5\n72 4 14\n57 2 12\n42 2 11\n53 2 3\n26 2 1\n44 3 10\n17 3 12\n41 3 14\n8 3 15\n10 3 16\n68 3 31\n77 3 32\n6 3 35\n70 3 36\n78 3 38\n30 2 38\n47 2 32\n75 2 31\n67 2 30\n76 2 27\n20 2 20\n42 1 11\n57 2 13\n53 2 4\n26 2 2\n44 3 9\n17 3 11\n41 3 13\n8 3 14\n10 3 15\n68 3 30\n77 3 31\n6 3 34\n70 3 35\n78 3 37\n30 2 39\n47 2 33\n75 2 32\n67 2 31\n76 2 28\n20 2 21\n41 4 13\n57 2 14\n53 2 5\n26 2 3\n44 3 8\n17 3 10\n8 3 13\n10 3 14\n68 3 29\n77 3 30\n6 3 33\n70 3 34\n78 3 36\n30 2 40\n47 2 34\n75 2 33\n67 2 32\n76 2 29\n20 2 22\n53 1 5\n57 2 15\n26 2 4\n44 3 7\n17 3 9\n8 3 12\n10 3 13\n68 3 28\n77 3 29\n6 3 32\n70 3 33\n78 3 35\n30 3 40\n47 2 35\n75 2 34\n67 2 33\n76 2 30\n20 2 23\n57 2 16\n26 2 5\n44 3 6\n17 3 8\n8 3 11\n10 3 12\n68 3 27\n77 3 28\n6 3 31\n70 3 32\n78 3 34\n30 3 39\n47 2 36\n75 2 35\n67 2 34\n76 2 31\n20 2 24\n68 4 27\n57 2 17\n26 2 6\n44 3 5\n17 3 7\n8 3 10\n10 3 11\n77 3 27\n6 3 30\n70 3 31\n78 3 33\n30 3 38\n47 2 37\n75 2 36\n67 2 35\n76 2 32\n20 2 25\n20 1 25\n57 2 18\n26 2 7\n44 3 4\n17 3 6\n8 3 9\n10 3 10\n77 3 26\n6 3 29\n70 3 30\n78 3 32\n30 3 37\n47 2 38\n75 2 37\n67 2 36\n76 2 33\n17 4 6\n57 2 19\n26 2 8\n44 3 3\n8 3 8\n10 3 9\n77 3 25\n6 3 28\n70 3 29\n78 3 31\n30 3 36\n47 2 39\n75 2 38\n67 2 37\n76 2 34\n6 4 28\n10 4 9\n57 2 20\n26 2 9\n44 3 2\n8 3 7\n77 3 24\n70 3 28\n78 3 30\n30 3 35\n47 2 40\n75 2 39\n67 2 38\n76 2 35\n57 2 21\n26 2 10\n44 3 1\n8 3 6\n77 3 23\n70 3 27\n78 3 29\n30 3 34\n47 3 40\n75 2 40\n67 2 39\n76 2 36\n30 4 34\n57 2 22\n26 2 11\n44 2 1\n8 3 5\n77 3 22\n70 3 26\n78 3 28\n47 3 39\n75 3 40\n67 2 40\n76 2 37\n57 2 23\n26 2 12\n44 2 2\n8 3 4\n77 3 21\n70 3 25\n78 3 27\n47 3 38\n75 3 39\n67 3 40\n76 2 38\n8 4 4\n57 2 24\n26 2 13\n44 2 3\n77 3 20\n70 3 24\n78 3 26\n47 3 37\n75 3 38\n67 3 39\n76 2 39\n26 1 13\n78 4 26\n57 2 25\n44 2 4\n77 3 19\n70 3 23\n47 3 36\n75 3 37\n67 3 38\n76 2 40\n57 2 26\n44 2 5\n77 3 18\n70 3 22\n47 3 35\n75 3 36\n67 3 37\n76 3 40\n67 4 37\n77 4 18\n57 2 27\n44 2 6\n70 3 21\n47 3 34\n75 3 35\n76 3 39\n44 1 6\n70 4 21\n57 2 28\n47 3 33\n75 3 34\n76 3 38\n57 2 29\n47 3 32\n75 3 33\n76 3 37\n57 1 29\n47 3 31\n75 3 32\n76 3 36\n76 4 36\n47 3 30\n75 3 31\n75 4 31\n47 3 29\n47 4 29\n"}, {"input": "40 77\r\n60 31 50 41 4 12 27 6 65 11 0 34 44 13 42 18 64 15 76 59 36 69 70 71 66 57 37 25 26 2 23 24 45 55 67 29 75 49 33 40\r\n11 14 65 44 74 51 55 16 19 29 75 41 27 35 69 10 70 2 73 58 45 61 0 7 30 6 23 25 66 63 28 62 24 77 20 43 0 18 50 52\r\n54 64 60 57 31 8 72 26 76 0 71 48 32 17 12 39 15 67 1 68 36 40 46 49 4 21 56 33 47 3 59 34 9 22 38 53 13 5 37 42\r\n51 52 30 9 20 62 14 74 38 21 48 0 16 28 43 10 47 72 56 5 17 58 61 53 77 63 0 7 39 54 22 19 3 1 68 46 73 32 8 35\r\n", "output": "2992\n7 2 23\n30 2 24\n6 2 25\n23 2 26\n25 2 27\n66 2 28\n63 2 29\n28 2 30\n62 2 31\n24 2 32\n77 2 33\n20 2 34\n43 2 35\n18 2 37\n18 2 36\n50 2 38\n50 2 37\n52 2 39\n52 2 38\n71 3 10\n48 3 11\n32 3 12\n17 3 13\n12 3 14\n39 3 15\n15 3 16\n67 3 17\n1 3 18\n68 3 19\n36 3 20\n40 3 21\n46 3 22\n49 3 23\n4 3 24\n21 3 25\n56 3 26\n33 3 27\n47 3 28\n3 3 29\n59 3 30\n34 3 31\n9 3 32\n22 3 33\n38 3 34\n53 3 35\n13 3 36\n5 3 37\n37 3 38\n42 3 39\n24 1 32\n48 4 11\n52 2 39\n50 2 38\n18 2 37\n43 2 36\n20 2 35\n77 2 34\n62 2 32\n28 2 31\n63 2 30\n66 2 29\n25 2 28\n23 2 27\n6 2 26\n30 2 25\n7 2 24\n61 2 23\n45 2 22\n58 2 21\n73 2 20\n2 2 19\n70 2 18\n10 2 17\n69 2 16\n35 2 15\n27 2 14\n41 2 13\n75 2 12\n29 2 11\n19 2 10\n16 2 9\n55 2 8\n51 2 7\n74 2 6\n44 2 5\n65 2 4\n14 2 3\n11 2 2\n54 2 1\n64 3 1\n60 3 2\n57 3 3\n31 3 4\n8 3 5\n72 3 6\n26 3 7\n76 3 8\n71 3 9\n32 3 11\n17 3 12\n12 3 13\n39 3 14\n15 3 15\n67 3 16\n1 3 17\n68 3 18\n36 3 19\n40 3 20\n46 3 21\n49 3 22\n4 3 23\n21 3 24\n56 3 25\n33 3 26\n47 3 27\n3 3 28\n59 3 29\n34 3 30\n9 3 31\n22 3 32\n38 3 33\n53 3 34\n13 3 35\n5 3 36\n37 3 37\n42 3 38\n25 1 28\n52 2 40\n50 2 39\n18 2 38\n43 2 37\n20 2 36\n77 2 35\n62 2 33\n28 2 32\n63 2 31\n66 2 30\n23 2 28\n6 2 27\n30 2 26\n7 2 25\n61 2 24\n45 2 23\n58 2 22\n73 2 21\n2 2 20\n70 2 19\n10 2 18\n69 2 17\n35 2 16\n27 2 15\n41 2 14\n75 2 13\n29 2 12\n19 2 11\n16 2 10\n55 2 9\n51 2 8\n74 2 7\n44 2 6\n65 2 5\n14 2 4\n11 2 3\n54 2 2\n64 2 1\n60 3 1\n57 3 2\n31 3 3\n8 3 4\n72 3 5\n26 3 6\n76 3 7\n71 3 8\n32 3 10\n17 3 11\n12 3 12\n39 3 13\n15 3 14\n67 3 15\n1 3 16\n68 3 17\n36 3 18\n40 3 19\n46 3 20\n49 3 21\n4 3 22\n21 3 23\n56 3 24\n33 3 25\n47 3 26\n3 3 27\n59 3 28\n34 3 29\n9 3 30\n22 3 31\n38 3 32\n53 3 33\n13 3 34\n5 3 35\n37 3 36\n42 3 37\n22 4 31\n52 3 40\n50 2 40\n18 2 39\n43 2 38\n20 2 37\n77 2 36\n62 2 34\n28 2 33\n63 2 32\n66 2 31\n23 2 29\n6 2 28\n30 2 27\n7 2 26\n61 2 25\n45 2 24\n58 2 23\n73 2 22\n2 2 21\n70 2 20\n10 2 19\n69 2 18\n35 2 17\n27 2 16\n41 2 15\n75 2 14\n29 2 13\n19 2 12\n16 2 11\n55 2 10\n51 2 9\n74 2 8\n44 2 7\n65 2 6\n14 2 5\n11 2 4\n54 2 3\n64 2 2\n60 2 1\n57 3 1\n31 3 2\n8 3 3\n72 3 4\n26 3 5\n76 3 6\n71 3 7\n32 3 9\n17 3 10\n12 3 11\n39 3 12\n15 3 13\n67 3 14\n1 3 15\n68 3 16\n36 3 17\n40 3 18\n46 3 19\n49 3 20\n4 3 21\n21 3 22\n56 3 23\n33 3 24\n47 3 25\n3 3 26\n59 3 27\n34 3 28\n9 3 29\n38 3 31\n53 3 32\n13 3 33\n5 3 34\n37 3 35\n42 3 36\n60 1 1\n52 3 39\n50 3 40\n18 2 40\n43 2 39\n20 2 38\n77 2 37\n62 2 35\n28 2 34\n63 2 33\n66 2 32\n23 2 30\n6 2 29\n30 2 28\n7 2 27\n61 2 26\n45 2 25\n58 2 24\n73 2 23\n2 2 22\n70 2 21\n10 2 20\n69 2 19\n35 2 18\n27 2 17\n41 2 16\n75 2 15\n29 2 14\n19 2 13\n16 2 12\n55 2 11\n51 2 10\n74 2 9\n44 2 8\n65 2 7\n14 2 6\n11 2 5\n54 2 4\n64 2 3\n57 2 1\n31 3 1\n8 3 2\n72 3 3\n26 3 4\n76 3 5\n71 3 6\n32 3 8\n17 3 9\n12 3 10\n39 3 11\n15 3 12\n67 3 13\n1 3 14\n68 3 15\n36 3 16\n40 3 17\n46 3 18\n49 3 19\n4 3 20\n21 3 21\n56 3 22\n33 3 23\n47 3 24\n3 3 25\n59 3 26\n34 3 27\n9 3 28\n38 3 30\n53 3 31\n13 3 32\n5 3 33\n37 3 34\n42 3 35\n52 3 38\n50 3 39\n18 3 40\n43 2 40\n20 2 39\n77 2 38\n62 2 36\n28 2 35\n63 2 34\n66 2 33\n23 2 31\n6 2 30\n30 2 29\n7 2 28\n61 2 27\n45 2 26\n58 2 25\n73 2 24\n2 2 23\n70 2 22\n10 2 21\n69 2 20\n35 2 19\n27 2 18\n41 2 17\n75 2 16\n29 2 15\n19 2 14\n16 2 13\n55 2 12\n51 2 11\n74 2 10\n44 2 9\n65 2 8\n14 2 7\n11 2 6\n54 2 5\n64 2 4\n57 2 2\n31 2 1\n8 3 1\n72 3 2\n26 3 3\n76 3 4\n71 3 5\n32 3 7\n17 3 8\n12 3 9\n39 3 10\n15 3 11\n67 3 12\n1 3 13\n68 3 14\n36 3 15\n40 3 16\n46 3 17\n49 3 18\n4 3 19\n21 3 20\n56 3 21\n33 3 22\n47 3 23\n3 3 24\n59 3 25\n34 3 26\n9 3 27\n38 3 29\n53 3 30\n13 3 31\n5 3 32\n37 3 33\n42 3 34\n23 1 31\n52 3 37\n50 3 38\n18 3 39\n43 3 40\n20 2 40\n77 2 39\n62 2 37\n28 2 36\n63 2 35\n66 2 34\n6 2 31\n30 2 30\n7 2 29\n61 2 28\n45 2 27\n58 2 26\n73 2 25\n2 2 24\n70 2 23\n10 2 22\n69 2 21\n35 2 20\n27 2 19\n41 2 18\n75 2 17\n29 2 16\n19 2 15\n16 2 14\n55 2 13\n51 2 12\n74 2 11\n44 2 10\n65 2 9\n14 2 8\n11 2 7\n54 2 6\n64 2 5\n57 2 3\n31 2 2\n8 2 1\n72 3 1\n26 3 2\n76 3 3\n71 3 4\n32 3 6\n17 3 7\n12 3 8\n39 3 9\n15 3 10\n67 3 11\n1 3 12\n68 3 13\n36 3 14\n40 3 15\n46 3 16\n49 3 17\n4 3 18\n21 3 19\n56 3 20\n33 3 21\n47 3 22\n3 3 23\n59 3 24\n34 3 25\n9 3 26\n38 3 28\n53 3 29\n13 3 30\n5 3 31\n37 3 32\n42 3 33\n31 1 2\n65 1 9\n70 1 23\n52 3 36\n50 3 37\n18 3 38\n43 3 39\n20 3 40\n77 2 40\n62 2 38\n28 2 37\n63 2 36\n66 2 35\n6 2 32\n30 2 31\n7 2 30\n61 2 29\n45 2 28\n58 2 27\n73 2 26\n2 2 25\n10 2 23\n69 2 22\n35 2 21\n27 2 20\n41 2 19\n75 2 18\n29 2 17\n19 2 16\n16 2 15\n55 2 14\n51 2 13\n74 2 12\n44 2 11\n14 2 9\n11 2 8\n54 2 7\n64 2 6\n57 2 4\n8 2 2\n72 2 1\n26 3 1\n76 3 2\n71 3 3\n32 3 5\n17 3 6\n12 3 7\n39 3 8\n15 3 9\n67 3 10\n1 3 11\n68 3 12\n36 3 13\n40 3 14\n46 3 15\n49 3 16\n4 3 17\n21 3 18\n56 3 19\n33 3 20\n47 3 21\n3 3 22\n59 3 23\n34 3 24\n9 3 25\n38 3 27\n53 3 28\n13 3 29\n5 3 30\n37 3 31\n42 3 32\n56 4 19\n69 1 22\n52 3 35\n50 3 36\n18 3 37\n43 3 38\n20 3 39\n77 3 40\n62 2 39\n28 2 38\n63 2 37\n66 2 36\n6 2 33\n30 2 32\n7 2 31\n61 2 30\n45 2 29\n58 2 28\n73 2 27\n2 2 26\n10 2 24\n35 2 22\n27 2 21\n41 2 20\n75 2 19\n29 2 18\n19 2 17\n16 2 16\n55 2 15\n51 2 14\n74 2 13\n44 2 12\n14 2 10\n11 2 9\n54 2 8\n64 2 7\n57 2 5\n8 2 3\n72 2 2\n26 2 1\n76 3 1\n71 3 2\n32 3 4\n17 3 5\n12 3 6\n39 3 7\n15 3 8\n67 3 9\n1 3 10\n68 3 11\n36 3 12\n40 3 13\n46 3 14\n49 3 15\n4 3 16\n21 3 17\n33 3 19\n47 3 20\n3 3 21\n59 3 22\n34 3 23\n9 3 24\n38 3 26\n53 3 27\n13 3 28\n5 3 29\n37 3 30\n42 3 31\n52 3 34\n50 3 35\n18 3 36\n43 3 37\n20 3 38\n77 3 39\n62 2 40\n28 2 39\n63 2 38\n66 2 37\n6 2 34\n30 2 33\n7 2 32\n61 2 31\n45 2 30\n58 2 29\n73 2 28\n2 2 27\n10 2 25\n35 2 23\n27 2 22\n41 2 21\n75 2 20\n29 2 19\n19 2 18\n16 2 17\n55 2 16\n51 2 15\n74 2 14\n44 2 13\n14 2 11\n11 2 10\n54 2 9\n64 2 8\n57 2 6\n8 2 4\n72 2 3\n26 2 2\n76 2 1\n71 3 1\n32 3 3\n17 3 4\n12 3 5\n39 3 6\n15 3 7\n67 3 8\n1 3 9\n68 3 10\n36 3 11\n40 3 12\n46 3 13\n49 3 14\n4 3 15\n21 3 16\n33 3 18\n47 3 19\n3 3 20\n59 3 21\n34 3 22\n9 3 23\n38 3 25\n53 3 26\n13 3 27\n5 3 28\n37 3 29\n42 3 30\n11 1 10\n44 1 13\n52 3 33\n50 3 34\n18 3 35\n43 3 36\n20 3 37\n77 3 38\n62 3 40\n28 2 40\n63 2 39\n66 2 38\n6 2 35\n30 2 34\n7 2 33\n61 2 32\n45 2 31\n58 2 30\n73 2 29\n2 2 28\n10 2 26\n35 2 24\n27 2 23\n41 2 22\n75 2 21\n29 2 20\n19 2 19\n16 2 18\n55 2 17\n51 2 16\n74 2 15\n14 2 12\n54 2 10\n64 2 9\n57 2 7\n8 2 5\n72 2 4\n26 2 3\n76 2 2\n71 2 1\n32 3 2\n17 3 3\n12 3 4\n39 3 5\n15 3 6\n67 3 7\n1 3 8\n68 3 9\n36 3 10\n40 3 11\n46 3 12\n49 3 13\n4 3 14\n21 3 15\n33 3 17\n47 3 18\n3 3 19\n59 3 20\n34 3 21\n9 3 22\n38 3 24\n53 3 25\n13 3 26\n5 3 27\n37 3 28\n42 3 29\n52 3 32\n50 3 33\n18 3 34\n43 3 35\n20 3 36\n77 3 37\n62 3 39\n28 3 40\n63 2 40\n66 2 39\n6 2 36\n30 2 35\n7 2 34\n61 2 33\n45 2 32\n58 2 31\n73 2 30\n2 2 29\n10 2 27\n35 2 25\n27 2 24\n41 2 23\n75 2 22\n29 2 21\n19 2 20\n16 2 19\n55 2 18\n51 2 17\n74 2 16\n14 2 13\n54 2 11\n64 2 10\n57 2 8\n8 2 6\n72 2 5\n26 2 4\n76 2 3\n71 2 2\n32 3 1\n17 3 2\n12 3 3\n39 3 4\n15 3 5\n67 3 6\n1 3 7\n68 3 8\n36 3 9\n40 3 10\n46 3 11\n49 3 12\n4 3 13\n21 3 14\n33 3 16\n47 3 17\n3 3 18\n59 3 19\n34 3 20\n9 3 21\n38 3 23\n53 3 24\n13 3 25\n5 3 26\n37 3 27\n42 3 28\n47 4 17\n53 4 24\n52 3 31\n50 3 32\n18 3 33\n43 3 34\n20 3 35\n77 3 36\n62 3 38\n28 3 39\n63 3 40\n66 2 40\n6 2 37\n30 2 36\n7 2 35\n61 2 34\n45 2 33\n58 2 32\n73 2 31\n2 2 30\n10 2 28\n35 2 26\n27 2 25\n41 2 24\n75 2 23\n29 2 22\n19 2 21\n16 2 20\n55 2 19\n51 2 18\n74 2 17\n14 2 14\n54 2 12\n64 2 11\n57 2 9\n8 2 7\n72 2 6\n26 2 5\n76 2 4\n71 2 3\n32 2 1\n17 3 1\n12 3 2\n39 3 3\n15 3 4\n67 3 5\n1 3 6\n68 3 7\n36 3 8\n40 3 9\n46 3 10\n49 3 11\n4 3 12\n21 3 13\n33 3 15\n3 3 17\n59 3 18\n34 3 19\n9 3 20\n38 3 22\n13 3 24\n5 3 25\n37 3 26\n42 3 27\n2 1 30\n45 1 33\n52 3 30\n50 3 31\n18 3 32\n43 3 33\n20 3 34\n77 3 35\n62 3 37\n28 3 38\n63 3 39\n66 3 40\n6 2 38\n30 2 37\n7 2 36\n61 2 35\n58 2 33\n73 2 32\n10 2 29\n35 2 27\n27 2 26\n41 2 25\n75 2 24\n29 2 23\n19 2 22\n16 2 21\n55 2 20\n51 2 19\n74 2 18\n14 2 15\n54 2 13\n64 2 12\n57 2 10\n8 2 8\n72 2 7\n26 2 6\n76 2 5\n71 2 4\n32 2 2\n17 2 1\n12 3 1\n39 3 2\n15 3 3\n67 3 4\n1 3 5\n68 3 6\n36 3 7\n40 3 8\n46 3 9\n49 3 10\n4 3 11\n21 3 12\n33 3 14\n3 3 16\n59 3 17\n34 3 18\n9 3 19\n38 3 21\n13 3 23\n5 3 24\n37 3 25\n42 3 26\n52 3 29\n50 3 30\n18 3 31\n43 3 32\n20 3 33\n77 3 34\n62 3 36\n28 3 37\n63 3 38\n66 3 39\n6 2 39\n30 2 38\n7 2 37\n61 2 36\n58 2 34\n73 2 33\n10 2 30\n35 2 28\n27 2 27\n41 2 26\n75 2 25\n29 2 24\n19 2 23\n16 2 22\n55 2 21\n51 2 20\n74 2 19\n14 2 16\n54 2 14\n64 2 13\n57 2 11\n8 2 9\n72 2 8\n26 2 7\n76 2 6\n71 2 5\n32 2 3\n17 2 2\n12 2 1\n39 3 1\n15 3 2\n67 3 3\n1 3 4\n68 3 5\n36 3 6\n40 3 7\n46 3 8\n49 3 9\n4 3 10\n21 3 11\n33 3 13\n3 3 15\n59 3 16\n34 3 17\n9 3 18\n38 3 20\n13 3 22\n5 3 23\n37 3 24\n42 3 25\n52 3 28\n50 3 29\n18 3 30\n43 3 31\n20 3 32\n77 3 33\n62 3 35\n28 3 36\n63 3 37\n66 3 38\n6 2 40\n30 2 39\n7 2 38\n61 2 37\n58 2 35\n73 2 34\n10 2 31\n35 2 29\n27 2 28\n41 2 27\n75 2 26\n29 2 25\n19 2 24\n16 2 23\n55 2 22\n51 2 21\n74 2 20\n14 2 17\n54 2 15\n64 2 14\n57 2 12\n8 2 10\n72 2 9\n26 2 8\n76 2 7\n71 2 6\n32 2 4\n17 2 3\n12 2 2\n39 2 1\n15 3 1\n67 3 2\n1 3 3\n68 3 4\n36 3 5\n40 3 6\n46 3 7\n49 3 8\n4 3 9\n21 3 10\n33 3 12\n3 3 14\n59 3 15\n34 3 16\n9 3 17\n38 3 19\n13 3 21\n5 3 22\n37 3 23\n42 3 24\n21 4 10\n52 3 27\n50 3 28\n18 3 29\n43 3 30\n20 3 31\n77 3 32\n62 3 34\n28 3 35\n63 3 36\n66 3 37\n6 3 40\n30 2 40\n7 2 39\n61 2 38\n58 2 36\n73 2 35\n10 2 32\n35 2 30\n27 2 29\n41 2 28\n75 2 27\n29 2 26\n19 2 25\n16 2 24\n55 2 23\n51 2 22\n74 2 21\n14 2 18\n54 2 16\n64 2 15\n57 2 13\n8 2 11\n72 2 10\n26 2 9\n76 2 8\n71 2 7\n32 2 5\n17 2 4\n12 2 3\n39 2 2\n15 2 1\n67 3 1\n1 3 2\n68 3 3\n36 3 4\n40 3 5\n46 3 6\n49 3 7\n4 3 8\n33 3 11\n3 3 13\n59 3 14\n34 3 15\n9 3 16\n38 3 18\n13 3 20\n5 3 21\n37 3 22\n42 3 23\n52 3 26\n50 3 27\n18 3 28\n43 3 29\n20 3 30\n77 3 31\n62 3 33\n28 3 34\n63 3 35\n66 3 36\n6 3 39\n30 3 40\n7 2 40\n61 2 39\n58 2 37\n73 2 36\n10 2 33\n35 2 31\n27 2 30\n41 2 29\n75 2 28\n29 2 27\n19 2 26\n16 2 25\n55 2 24\n51 2 23\n74 2 22\n14 2 19\n54 2 17\n64 2 16\n57 2 14\n8 2 12\n72 2 11\n26 2 10\n76 2 9\n71 2 8\n32 2 6\n17 2 5\n12 2 4\n39 2 3\n15 2 2\n67 2 1\n1 3 1\n68 3 2\n36 3 3\n40 3 4\n46 3 5\n49 3 6\n4 3 7\n33 3 10\n3 3 12\n59 3 13\n34 3 14\n9 3 15\n38 3 17\n13 3 19\n5 3 20\n37 3 21\n42 3 22\n5 4 20\n52 3 25\n50 3 26\n18 3 27\n43 3 28\n20 3 29\n77 3 30\n62 3 32\n28 3 33\n63 3 34\n66 3 35\n6 3 38\n30 3 39\n7 3 40\n61 2 40\n58 2 38\n73 2 37\n10 2 34\n35 2 32\n27 2 31\n41 2 30\n75 2 29\n29 2 28\n19 2 27\n16 2 26\n55 2 25\n51 2 24\n74 2 23\n14 2 20\n54 2 18\n64 2 17\n57 2 15\n8 2 13\n72 2 12\n26 2 11\n76 2 10\n71 2 9\n32 2 7\n17 2 6\n12 2 5\n39 2 4\n15 2 3\n67 2 2\n1 2 1\n68 3 1\n36 3 2\n40 3 3\n46 3 4\n49 3 5\n4 3 6\n33 3 9\n3 3 11\n59 3 12\n34 3 13\n9 3 14\n38 3 16\n13 3 18\n37 3 20\n42 3 21\n64 1 17\n52 3 24\n50 3 25\n18 3 26\n43 3 27\n20 3 28\n77 3 29\n62 3 31\n28 3 32\n63 3 33\n66 3 34\n6 3 37\n30 3 38\n7 3 39\n61 3 40\n58 2 39\n73 2 38\n10 2 35\n35 2 33\n27 2 32\n41 2 31\n75 2 30\n29 2 29\n19 2 28\n16 2 27\n55 2 26\n51 2 25\n74 2 24\n14 2 21\n54 2 19\n57 2 16\n8 2 14\n72 2 13\n26 2 12\n76 2 11\n71 2 10\n32 2 8\n17 2 7\n12 2 6\n39 2 5\n15 2 4\n67 2 3\n1 2 2\n68 2 1\n36 3 1\n40 3 2\n46 3 3\n49 3 4\n4 3 5\n33 3 8\n3 3 10\n59 3 11\n34 3 12\n9 3 13\n38 3 15\n13 3 17\n37 3 19\n42 3 20\n12 1 6\n52 3 23\n50 3 24\n18 3 25\n43 3 26\n20 3 27\n77 3 28\n62 3 30\n28 3 31\n63 3 32\n66 3 33\n6 3 36\n30 3 37\n7 3 38\n61 3 39\n58 2 40\n73 2 39\n10 2 36\n35 2 34\n27 2 33\n41 2 32\n75 2 31\n29 2 30\n19 2 29\n16 2 28\n55 2 27\n51 2 26\n74 2 25\n14 2 22\n54 2 20\n57 2 17\n8 2 15\n72 2 14\n26 2 13\n76 2 12\n71 2 11\n32 2 9\n17 2 8\n39 2 6\n15 2 5\n67 2 4\n1 2 3\n68 2 2\n36 2 1\n40 3 1\n46 3 2\n49 3 3\n4 3 4\n33 3 7\n3 3 9\n59 3 10\n34 3 11\n9 3 12\n38 3 14\n13 3 16\n37 3 18\n42 3 19\n52 3 22\n50 3 23\n18 3 24\n43 3 25\n20 3 26\n77 3 27\n62 3 29\n28 3 30\n63 3 31\n66 3 32\n6 3 35\n30 3 36\n7 3 37\n61 3 38\n58 3 40\n73 2 40\n10 2 37\n35 2 35\n27 2 34\n41 2 33\n75 2 32\n29 2 31\n19 2 30\n16 2 29\n55 2 28\n51 2 27\n74 2 26\n14 2 23\n54 2 21\n57 2 18\n8 2 16\n72 2 15\n26 2 14\n76 2 13\n71 2 12\n32 2 10\n17 2 9\n39 2 7\n15 2 6\n67 2 5\n1 2 4\n68 2 3\n36 2 2\n40 2 1\n46 3 1\n49 3 2\n4 3 3\n33 3 6\n3 3 8\n59 3 9\n34 3 10\n9 3 11\n38 3 13\n13 3 15\n37 3 17\n42 3 18\n52 3 21\n50 3 22\n18 3 23\n43 3 24\n20 3 25\n77 3 26\n62 3 28\n28 3 29\n63 3 30\n66 3 31\n6 3 34\n30 3 35\n7 3 36\n61 3 37\n58 3 39\n73 3 40\n10 2 38\n35 2 36\n27 2 35\n41 2 34\n75 2 33\n29 2 32\n19 2 31\n16 2 30\n55 2 29\n51 2 28\n74 2 27\n14 2 24\n54 2 22\n57 2 19\n8 2 17\n72 2 16\n26 2 15\n76 2 14\n71 2 13\n32 2 11\n17 2 10\n39 2 8\n15 2 7\n67 2 6\n1 2 5\n68 2 4\n36 2 3\n40 2 2\n46 2 1\n49 3 1\n4 3 2\n33 3 5\n3 3 7\n59 3 8\n34 3 9\n9 3 10\n38 3 12\n13 3 14\n37 3 16\n42 3 17\n52 3 20\n50 3 21\n18 3 22\n43 3 23\n20 3 24\n77 3 25\n62 3 27\n28 3 28\n63 3 29\n66 3 30\n6 3 33\n30 3 34\n7 3 35\n61 3 36\n58 3 38\n73 3 39\n10 2 39\n35 2 37\n27 2 36\n41 2 35\n75 2 34\n29 2 33\n19 2 32\n16 2 31\n55 2 30\n51 2 29\n74 2 28\n14 2 25\n54 2 23\n57 2 20\n8 2 18\n72 2 17\n26 2 16\n76 2 15\n71 2 14\n32 2 12\n17 2 11\n39 2 9\n15 2 8\n67 2 7\n1 2 6\n68 2 5\n36 2 4\n40 2 3\n46 2 2\n49 2 1\n4 3 1\n33 3 4\n3 3 6\n59 3 7\n34 3 8\n9 3 9\n38 3 11\n13 3 13\n37 3 15\n42 3 16\n77 4 25\n52 3 19\n50 3 20\n18 3 21\n43 3 22\n20 3 23\n62 3 26\n28 3 27\n63 3 28\n66 3 29\n6 3 32\n30 3 33\n7 3 34\n61 3 35\n58 3 37\n73 3 38\n10 2 40\n35 2 38\n27 2 37\n41 2 36\n75 2 35\n29 2 34\n19 2 33\n16 2 32\n55 2 31\n51 2 30\n74 2 29\n14 2 26\n54 2 24\n57 2 21\n8 2 19\n72 2 18\n26 2 17\n76 2 16\n71 2 15\n32 2 13\n17 2 12\n39 2 10\n15 2 9\n67 2 8\n1 2 7\n68 2 6\n36 2 5\n40 2 4\n46 2 3\n49 2 2\n4 2 1\n33 3 3\n3 3 5\n59 3 6\n34 3 7\n9 3 8\n38 3 10\n13 3 12\n37 3 14\n42 3 15\n52 3 18\n50 3 19\n18 3 20\n43 3 21\n20 3 22\n62 3 25\n28 3 26\n63 3 27\n66 3 28\n6 3 31\n30 3 32\n7 3 33\n61 3 34\n58 3 36\n73 3 37\n10 3 40\n35 2 39\n27 2 38\n41 2 37\n75 2 36\n29 2 35\n19 2 34\n16 2 33\n55 2 32\n51 2 31\n74 2 30\n14 2 27\n54 2 25\n57 2 22\n8 2 20\n72 2 19\n26 2 18\n76 2 17\n71 2 16\n32 2 14\n17 2 13\n39 2 11\n15 2 10\n67 2 9\n1 2 8\n68 2 7\n36 2 6\n40 2 5\n46 2 4\n49 2 3\n4 2 2\n33 3 2\n3 3 4\n59 3 5\n34 3 6\n9 3 7\n38 3 9\n13 3 11\n37 3 13\n42 3 14\n38 4 9\n73 4 37\n52 3 17\n50 3 18\n18 3 19\n43 3 20\n20 3 21\n62 3 24\n28 3 25\n63 3 26\n66 3 27\n6 3 30\n30 3 31\n7 3 32\n61 3 33\n58 3 35\n10 3 39\n35 2 40\n27 2 39\n41 2 38\n75 2 37\n29 2 36\n19 2 35\n16 2 34\n55 2 33\n51 2 32\n74 2 31\n14 2 28\n54 2 26\n57 2 23\n8 2 21\n72 2 20\n26 2 19\n76 2 18\n71 2 17\n32 2 15\n17 2 14\n39 2 12\n15 2 11\n67 2 10\n1 2 9\n68 2 8\n36 2 7\n40 2 6\n46 2 5\n49 2 4\n4 2 3\n33 3 1\n3 3 3\n59 3 4\n34 3 5\n9 3 6\n13 3 10\n37 3 12\n42 3 13\n29 1 36\n63 4 26\n75 1 37\n52 3 16\n50 3 17\n18 3 18\n43 3 19\n20 3 20\n62 3 23\n28 3 24\n66 3 26\n6 3 29\n30 3 30\n7 3 31\n61 3 32\n58 3 34\n10 3 38\n35 3 40\n27 2 40\n41 2 39\n19 2 36\n16 2 35\n55 2 34\n51 2 33\n74 2 32\n14 2 29\n54 2 27\n57 2 24\n8 2 22\n72 2 21\n26 2 20\n76 2 19\n71 2 18\n32 2 16\n17 2 15\n39 2 13\n15 2 12\n67 2 11\n1 2 10\n68 2 9\n36 2 8\n40 2 7\n46 2 6\n49 2 5\n4 2 4\n33 2 1\n3 3 2\n59 3 3\n34 3 4\n9 3 5\n13 3 9\n37 3 11\n42 3 12\n35 4 40\n55 1 34\n76 1 19\n52 3 15\n50 3 16\n18 3 17\n43 3 18\n20 3 19\n62 3 22\n28 3 23\n66 3 25\n6 3 28\n30 3 29\n7 3 30\n61 3 31\n58 3 33\n10 3 37\n27 3 40\n41 2 40\n19 2 37\n16 2 36\n51 2 34\n74 2 33\n14 2 30\n54 2 28\n57 2 25\n8 2 23\n72 2 22\n26 2 21\n71 2 19\n32 2 17\n17 2 16\n39 2 14\n15 2 13\n67 2 12\n1 2 11\n68 2 10\n36 2 9\n40 2 8\n46 2 7\n49 2 6\n4 2 5\n33 2 2\n3 3 1\n59 3 2\n34 3 3\n9 3 4\n13 3 8\n37 3 10\n42 3 11\n4 1 5\n9 4 4\n52 3 14\n50 3 15\n18 3 16\n43 3 17\n20 3 18\n62 3 21\n28 3 22\n66 3 24\n6 3 27\n30 3 28\n7 3 29\n61 3 30\n58 3 32\n10 3 36\n27 3 39\n41 3 40\n19 2 38\n16 2 37\n51 2 35\n74 2 34\n14 2 31\n54 2 29\n57 2 26\n8 2 24\n72 2 23\n26 2 22\n71 2 20\n32 2 18\n17 2 17\n39 2 15\n15 2 14\n67 2 13\n1 2 12\n68 2 11\n36 2 10\n40 2 9\n46 2 8\n49 2 7\n33 2 3\n3 2 1\n59 3 1\n34 3 2\n13 3 7\n37 3 9\n42 3 10\n57 1 26\n52 3 13\n50 3 14\n18 3 15\n43 3 16\n20 3 17\n62 3 20\n28 3 21\n66 3 23\n6 3 26\n30 3 27\n7 3 28\n61 3 29\n58 3 31\n10 3 35\n27 3 38\n41 3 39\n19 2 39\n16 2 38\n51 2 36\n74 2 35\n14 2 32\n54 2 30\n8 2 25\n72 2 24\n26 2 23\n71 2 21\n32 2 19\n17 2 18\n39 2 16\n15 2 15\n67 2 14\n1 2 13\n68 2 12\n36 2 11\n40 2 10\n46 2 9\n49 2 8\n33 2 4\n3 2 2\n59 2 1\n34 3 1\n13 3 6\n37 3 8\n42 3 9\n7 4 28\n52 3 12\n50 3 13\n18 3 14\n43 3 15\n20 3 16\n62 3 19\n28 3 20\n66 3 22\n6 3 25\n30 3 26\n61 3 28\n58 3 30\n10 3 34\n27 3 37\n41 3 38\n19 2 40\n16 2 39\n51 2 37\n74 2 36\n14 2 33\n54 2 31\n8 2 26\n72 2 25\n26 2 24\n71 2 22\n32 2 20\n17 2 19\n39 2 17\n15 2 16\n67 2 15\n1 2 14\n68 2 13\n36 2 12\n40 2 11\n46 2 10\n49 2 9\n33 2 5\n3 2 3\n59 2 2\n34 2 1\n13 3 5\n37 3 7\n42 3 8\n43 4 15\n52 3 11\n50 3 12\n18 3 13\n20 3 15\n62 3 18\n28 3 19\n66 3 21\n6 3 24\n30 3 25\n61 3 27\n58 3 29\n10 3 33\n27 3 36\n41 3 37\n19 3 40\n16 2 40\n51 2 38\n74 2 37\n14 2 34\n54 2 32\n8 2 27\n72 2 26\n26 2 25\n71 2 23\n32 2 21\n17 2 20\n39 2 18\n15 2 17\n67 2 16\n1 2 15\n68 2 14\n36 2 13\n40 2 12\n46 2 11\n49 2 10\n33 2 6\n3 2 4\n59 2 3\n34 2 2\n13 3 4\n37 3 6\n42 3 7\n52 3 10\n50 3 11\n18 3 12\n20 3 14\n62 3 17\n28 3 18\n66 3 20\n6 3 23\n30 3 24\n61 3 26\n58 3 28\n10 3 32\n27 3 35\n41 3 36\n19 3 39\n16 3 40\n51 2 39\n74 2 38\n14 2 35\n54 2 33\n8 2 28\n72 2 27\n26 2 26\n71 2 24\n32 2 22\n17 2 21\n39 2 19\n15 2 18\n67 2 17\n1 2 16\n68 2 15\n36 2 14\n40 2 13\n46 2 12\n49 2 11\n33 2 7\n3 2 5\n59 2 4\n34 2 3\n13 3 3\n37 3 5\n42 3 6\n15 1 18\n71 1 24\n52 3 9\n50 3 10\n18 3 11\n20 3 13\n62 3 16\n28 3 17\n66 3 19\n6 3 22\n30 3 23\n61 3 25\n58 3 27\n10 3 31\n27 3 34\n41 3 35\n19 3 38\n16 3 39\n51 2 40\n74 2 39\n14 2 36\n54 2 34\n8 2 29\n72 2 28\n26 2 27\n32 2 23\n17 2 22\n39 2 20\n67 2 18\n1 2 17\n68 2 16\n36 2 15\n40 2 14\n46 2 13\n49 2 12\n33 2 8\n3 2 6\n59 2 5\n34 2 4\n13 3 2\n37 3 4\n42 3 5\n52 3 8\n50 3 9\n18 3 10\n20 3 12\n62 3 15\n28 3 16\n66 3 18\n6 3 21\n30 3 22\n61 3 24\n58 3 26\n10 3 30\n27 3 33\n41 3 34\n19 3 37\n16 3 38\n51 3 40\n74 2 40\n14 2 37\n54 2 35\n8 2 30\n72 2 29\n26 2 28\n32 2 24\n17 2 23\n39 2 21\n67 2 19\n1 2 18\n68 2 17\n36 2 16\n40 2 15\n46 2 14\n49 2 13\n33 2 9\n3 2 7\n59 2 6\n34 2 5\n13 3 1\n37 3 3\n42 3 4\n52 3 7\n50 3 8\n18 3 9\n20 3 11\n62 3 14\n28 3 15\n66 3 17\n6 3 20\n30 3 21\n61 3 23\n58 3 25\n10 3 29\n27 3 32\n41 3 33\n19 3 36\n16 3 37\n51 3 39\n74 3 40\n14 2 38\n54 2 36\n8 2 31\n72 2 30\n26 2 29\n32 2 25\n17 2 24\n39 2 22\n67 2 20\n1 2 19\n68 2 18\n36 2 17\n40 2 16\n46 2 15\n49 2 14\n33 2 10\n3 2 8\n59 2 7\n34 2 6\n13 2 1\n37 3 2\n42 3 3\n26 1 29\n61 4 23\n52 3 6\n50 3 7\n18 3 8\n20 3 10\n62 3 13\n28 3 14\n66 3 16\n6 3 19\n30 3 20\n58 3 24\n10 3 28\n27 3 31\n41 3 32\n19 3 35\n16 3 36\n51 3 38\n74 3 39\n14 2 39\n54 2 37\n8 2 32\n72 2 31\n32 2 26\n17 2 25\n39 2 23\n67 2 21\n1 2 20\n68 2 19\n36 2 18\n40 2 17\n46 2 16\n49 2 15\n33 2 11\n3 2 9\n59 2 8\n34 2 7\n13 2 2\n37 3 1\n42 3 2\n28 4 14\n52 3 5\n50 3 6\n18 3 7\n20 3 9\n62 3 12\n66 3 15\n6 3 18\n30 3 19\n58 3 23\n10 3 27\n27 3 30\n41 3 31\n19 3 34\n16 3 35\n51 3 37\n74 3 38\n14 2 40\n54 2 38\n8 2 33\n72 2 32\n32 2 27\n17 2 26\n39 2 24\n67 2 22\n1 2 21\n68 2 20\n36 2 19\n40 2 18\n46 2 17\n49 2 16\n33 2 12\n3 2 10\n59 2 9\n34 2 8\n13 2 3\n37 2 1\n42 3 1\n52 3 4\n50 3 5\n18 3 6\n20 3 8\n62 3 11\n66 3 14\n6 3 17\n30 3 18\n58 3 22\n10 3 26\n27 3 29\n41 3 30\n19 3 33\n16 3 34\n51 3 36\n74 3 37\n14 3 40\n54 2 39\n8 2 34\n72 2 33\n32 2 28\n17 2 27\n39 2 25\n67 2 23\n1 2 22\n68 2 21\n36 2 20\n40 2 19\n46 2 18\n49 2 17\n33 2 13\n3 2 11\n59 2 10\n34 2 9\n13 2 4\n37 2 2\n42 2 1\n58 4 22\n52 3 3\n50 3 4\n18 3 5\n20 3 7\n62 3 10\n66 3 13\n6 3 16\n30 3 17\n10 3 25\n27 3 28\n41 3 29\n19 3 32\n16 3 33\n51 3 35\n74 3 36\n14 3 39\n54 2 40\n8 2 35\n72 2 34\n32 2 29\n17 2 28\n39 2 26\n67 2 24\n1 2 23\n68 2 22\n36 2 21\n40 2 20\n46 2 19\n49 2 18\n33 2 14\n3 2 12\n59 2 11\n34 2 10\n13 2 5\n37 2 3\n42 2 2\n19 4 32\n36 1 21\n52 3 2\n50 3 3\n18 3 4\n20 3 6\n62 3 9\n66 3 12\n6 3 15\n30 3 16\n10 3 24\n27 3 27\n41 3 28\n16 3 32\n51 3 34\n74 3 35\n14 3 38\n54 3 40\n8 2 36\n72 2 35\n32 2 30\n17 2 29\n39 2 27\n67 2 25\n1 2 24\n68 2 23\n40 2 21\n46 2 20\n49 2 19\n33 2 15\n3 2 13\n59 2 12\n34 2 11\n13 2 6\n37 2 4\n42 2 3\n52 4 2\n50 3 2\n18 3 3\n20 3 5\n62 3 8\n66 3 11\n6 3 14\n30 3 15\n10 3 23\n27 3 26\n41 3 27\n16 3 31\n51 3 33\n74 3 34\n14 3 37\n54 3 39\n8 2 37\n72 2 36\n32 2 31\n17 2 30\n39 2 28\n67 2 26\n1 2 25\n68 2 24\n40 2 22\n46 2 21\n49 2 20\n33 2 16\n3 2 14\n59 2 13\n34 2 12\n13 2 7\n37 2 5\n42 2 4\n20 4 5\n34 1 12\n50 3 1\n18 3 2\n62 3 7\n66 3 10\n6 3 13\n30 3 14\n10 3 22\n27 3 25\n41 3 26\n16 3 30\n51 3 32\n74 3 33\n14 3 36\n54 3 38\n8 2 38\n72 2 37\n32 2 32\n17 2 31\n39 2 29\n67 2 27\n1 2 26\n68 2 25\n40 2 23\n46 2 22\n49 2 21\n33 2 17\n3 2 15\n59 2 14\n13 2 8\n37 2 6\n42 2 5\n50 2 1\n18 3 1\n62 3 6\n66 3 9\n6 3 12\n30 3 13\n10 3 21\n27 3 24\n41 3 25\n16 3 29\n51 3 31\n74 3 32\n14 3 35\n54 3 37\n8 2 39\n72 2 38\n32 2 33\n17 2 32\n39 2 30\n67 2 28\n1 2 27\n68 2 26\n40 2 24\n46 2 23\n49 2 22\n33 2 18\n3 2 16\n59 2 15\n13 2 9\n37 2 7\n42 2 6\n62 4 6\n50 2 2\n18 2 1\n66 3 8\n6 3 11\n30 3 12\n10 3 20\n27 3 23\n41 3 24\n16 3 28\n51 3 30\n74 3 31\n14 3 34\n54 3 36\n8 2 40\n72 2 39\n32 2 34\n17 2 33\n39 2 31\n67 2 29\n1 2 28\n68 2 27\n40 2 25\n46 2 24\n49 2 23\n33 2 19\n3 2 17\n59 2 16\n13 2 10\n37 2 8\n42 2 7\n50 2 3\n18 2 2\n66 3 7\n6 3 10\n30 3 11\n10 3 19\n27 3 22\n41 3 23\n16 3 27\n51 3 29\n74 3 30\n14 3 33\n54 3 35\n8 3 40\n72 2 40\n32 2 35\n17 2 34\n39 2 32\n67 2 30\n1 2 29\n68 2 28\n40 2 26\n46 2 25\n49 2 24\n33 2 20\n3 2 18\n59 2 17\n13 2 11\n37 2 9\n42 2 8\n50 1 3\n18 2 3\n66 3 6\n6 3 9\n30 3 10\n10 3 18\n27 3 21\n41 3 22\n16 3 26\n51 3 28\n74 3 29\n14 3 32\n54 3 34\n8 3 39\n72 3 40\n32 2 36\n17 2 35\n39 2 33\n67 2 31\n1 2 30\n68 2 29\n40 2 27\n46 2 26\n49 2 25\n33 2 21\n3 2 19\n59 2 18\n13 2 12\n37 2 10\n42 2 9\n8 4 39\n18 2 4\n66 3 5\n6 3 8\n30 3 9\n10 3 17\n27 3 20\n41 3 21\n16 3 25\n51 3 27\n74 3 28\n14 3 31\n54 3 33\n72 3 39\n32 2 37\n17 2 36\n39 2 34\n67 2 32\n1 2 31\n68 2 30\n40 2 28\n46 2 27\n49 2 26\n33 2 22\n3 2 20\n59 2 19\n13 2 13\n37 2 11\n42 2 10\n18 2 5\n66 3 4\n6 3 7\n30 3 8\n10 3 16\n27 3 19\n41 3 20\n16 3 24\n51 3 26\n74 3 27\n14 3 30\n54 3 32\n72 3 38\n32 2 38\n17 2 37\n39 2 35\n67 2 33\n1 2 32\n68 2 31\n40 2 29\n46 2 28\n49 2 27\n33 2 23\n3 2 21\n59 2 20\n13 2 14\n37 2 12\n42 2 11\n10 4 16\n13 1 14\n59 1 20\n18 2 6\n66 3 3\n6 3 6\n30 3 7\n27 3 18\n41 3 19\n16 3 23\n51 3 25\n74 3 26\n14 3 29\n54 3 31\n72 3 37\n32 2 39\n17 2 38\n39 2 36\n67 2 34\n1 2 33\n68 2 32\n40 2 30\n46 2 29\n49 2 28\n33 2 24\n3 2 22\n37 2 13\n42 2 12\n18 2 7\n66 3 2\n6 3 5\n30 3 6\n27 3 17\n41 3 18\n16 3 22\n51 3 24\n74 3 25\n14 3 28\n54 3 30\n72 3 36\n32 2 40\n17 2 39\n39 2 37\n67 2 35\n1 2 34\n68 2 33\n40 2 31\n46 2 30\n49 2 29\n33 2 25\n3 2 23\n37 2 14\n42 2 13\n54 4 30\n67 1 35\n18 2 8\n66 3 1\n6 3 4\n30 3 5\n27 3 16\n41 3 17\n16 3 21\n51 3 23\n74 3 24\n14 3 27\n72 3 35\n32 3 40\n17 2 40\n39 2 38\n1 2 35\n68 2 34\n40 2 32\n46 2 31\n49 2 30\n33 2 26\n3 2 24\n37 2 15\n42 2 14\n18 2 9\n66 2 1\n6 3 3\n30 3 4\n27 3 15\n41 3 16\n16 3 20\n51 3 22\n74 3 23\n14 3 26\n72 3 34\n32 3 39\n17 3 40\n39 2 39\n1 2 36\n68 2 35\n40 2 33\n46 2 32\n49 2 31\n33 2 27\n3 2 25\n37 2 16\n42 2 15\n42 1 15\n18 2 10\n66 2 2\n6 3 2\n30 3 3\n27 3 14\n41 3 15\n16 3 19\n51 3 21\n74 3 22\n14 3 25\n72 3 33\n32 3 38\n17 3 39\n39 2 40\n1 2 37\n68 2 36\n40 2 34\n46 2 33\n49 2 32\n33 2 28\n3 2 26\n37 2 17\n30 4 3\n32 4 38\n18 2 11\n66 2 3\n6 3 1\n27 3 13\n41 3 14\n16 3 18\n51 3 20\n74 3 21\n14 3 24\n72 3 32\n17 3 38\n39 3 40\n1 2 38\n68 2 37\n40 2 35\n46 2 34\n49 2 33\n33 2 29\n3 2 27\n37 2 18\n18 2 12\n66 2 4\n6 2 1\n27 3 12\n41 3 13\n16 3 17\n51 3 19\n74 3 20\n14 3 23\n72 3 31\n17 3 37\n39 3 39\n1 2 39\n68 2 38\n40 2 36\n46 2 35\n49 2 34\n33 2 30\n3 2 28\n37 2 19\n18 2 13\n66 2 5\n6 2 2\n27 3 11\n41 3 12\n16 3 16\n51 3 18\n74 3 19\n14 3 22\n72 3 30\n17 3 36\n39 3 38\n1 2 40\n68 2 39\n40 2 37\n46 2 36\n49 2 35\n33 2 31\n3 2 29\n37 2 20\n18 2 14\n66 2 6\n6 2 3\n27 3 10\n41 3 11\n16 3 15\n51 3 17\n74 3 18\n14 3 21\n72 3 29\n17 3 35\n39 3 37\n1 3 40\n68 2 40\n40 2 38\n46 2 37\n49 2 36\n33 2 32\n3 2 30\n37 2 21\n18 2 15\n66 2 7\n6 2 4\n27 3 9\n41 3 10\n16 3 14\n51 3 16\n74 3 17\n14 3 20\n72 3 28\n17 3 34\n39 3 36\n1 3 39\n68 3 40\n40 2 39\n46 2 38\n49 2 37\n33 2 33\n3 2 31\n37 2 22\n18 2 16\n66 2 8\n6 2 5\n27 3 8\n41 3 9\n16 3 13\n51 3 15\n74 3 16\n14 3 19\n72 3 27\n17 3 33\n39 3 35\n1 3 38\n68 3 39\n40 2 40\n46 2 39\n49 2 38\n33 2 34\n3 2 32\n37 2 23\n16 4 13\n18 1 16\n40 1 40\n49 1 38\n66 2 9\n6 2 6\n27 3 7\n41 3 8\n51 3 14\n74 3 15\n14 3 18\n72 3 26\n17 3 32\n39 3 34\n1 3 37\n68 3 38\n46 2 40\n33 2 35\n3 2 33\n37 2 24\n66 2 10\n6 2 7\n27 3 6\n41 3 7\n51 3 13\n74 3 14\n14 3 17\n72 3 25\n17 3 31\n39 3 33\n1 3 36\n68 3 37\n46 3 40\n33 2 36\n3 2 34\n37 2 25\n66 2 11\n6 2 8\n27 3 5\n41 3 6\n51 3 12\n74 3 13\n14 3 16\n72 3 24\n17 3 30\n39 3 32\n1 3 35\n68 3 36\n46 3 39\n33 2 37\n3 2 35\n37 2 26\n6 1 8\n66 2 12\n27 3 4\n41 3 5\n51 3 11\n74 3 12\n14 3 15\n72 3 23\n17 3 29\n39 3 31\n1 3 34\n68 3 35\n46 3 38\n33 2 38\n3 2 36\n37 2 27\n1 4 34\n37 1 27\n68 4 35\n66 2 13\n27 3 3\n41 3 4\n51 3 10\n74 3 11\n14 3 14\n72 3 22\n17 3 28\n39 3 30\n46 3 37\n33 2 39\n3 2 37\n33 1 39\n66 2 14\n27 3 2\n41 3 3\n51 3 9\n74 3 10\n14 3 13\n72 3 21\n17 3 27\n39 3 29\n46 3 36\n3 2 38\n39 4 29\n46 4 36\n66 2 15\n27 3 1\n41 3 2\n51 3 8\n74 3 9\n14 3 12\n72 3 20\n17 3 26\n3 2 39\n66 2 16\n27 2 1\n41 3 1\n51 3 7\n74 3 8\n14 3 11\n72 3 19\n17 3 25\n3 2 40\n74 4 8\n66 2 17\n27 2 2\n41 2 1\n51 3 6\n14 3 10\n72 3 18\n17 3 24\n3 3 40\n72 4 18\n66 2 18\n27 2 3\n41 2 2\n51 3 5\n14 3 9\n17 3 23\n3 3 39\n66 2 19\n27 2 4\n41 2 3\n51 3 4\n14 3 8\n17 3 22\n3 3 38\n66 2 20\n27 2 5\n41 2 4\n51 3 3\n14 3 7\n17 3 21\n3 3 37\n14 4 7\n17 4 21\n41 1 4\n66 2 21\n27 2 6\n51 3 2\n3 3 36\n66 2 22\n27 2 7\n51 3 1\n3 3 35\n27 1 7\n51 4 1\n66 2 23\n3 3 34\n66 2 24\n3 3 33\n3 4 33\n66 2 25\n66 1 25\n"}, {"input": "50 1\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "34\r\n1 3 27\r\n1 3 28\r\n1 3 29\r\n1 3 30\r\n1 3 31\r\n1 3 32\r\n1 3 33\r\n1 3 34\r\n1 3 35\r\n1 3 36\r\n1 3 37\r\n1 3 38\r\n1 3 39\r\n1 3 40\r\n1 3 41\r\n1 3 42\r\n1 3 43\r\n1 3 44\r\n1 3 45\r\n1 3 46\r\n1 3 47\r\n1 3 48\r\n1 3 49\r\n1 3 50\r\n1 2 50\r\n1 2 49\r\n1 2 48\r\n1 2 47\r\n1 2 46\r\n1 2 45\r\n1 2 44\r\n1 2 43\r\n1 2 42\r\n1 1 42\r\n"}, {"input": "37 22\r\n0 18 0 0 0 16 0 0 0 0 1 21 0 0 0 4 0 15 0 8 0 0 0 0 0 0 0 9 14 0 0 0 22 0 0 3 0\r\n0 0 0 0 0 21 0 0 2 0 0 0 0 0 0 13 0 0 0 0 0 0 22 12 9 15 11 8 0 16 0 0 0 0 0 0 0\r\n0 3 1 0 0 0 0 14 0 20 0 7 0 0 0 4 0 6 0 0 5 0 18 0 17 10 0 0 0 0 19 0 0 0 0 0 0\r\n13 0 2 19 10 0 0 17 0 0 20 0 0 5 11 0 0 6 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0\r\n", "output": "1064\n21 2 5\n21 2 4\n21 2 3\n21 2 2\n21 2 1\n2 2 8\n2 2 7\n2 2 6\n2 2 5\n2 2 4\n2 2 3\n2 2 2\n13 2 15\n13 2 14\n13 2 13\n13 2 12\n13 2 11\n13 2 10\n13 2 9\n13 2 8\n13 2 7\n13 2 6\n13 2 5\n13 2 4\n13 2 3\n22 2 22\n22 2 21\n22 2 20\n22 2 19\n22 2 18\n22 2 17\n22 2 16\n22 2 15\n22 2 14\n22 2 13\n22 2 12\n22 2 11\n22 2 10\n22 2 9\n22 2 8\n22 2 7\n22 2 6\n22 2 5\n22 2 4\n12 2 23\n12 2 22\n12 2 21\n12 2 20\n12 2 19\n12 2 18\n12 2 17\n12 2 16\n12 2 15\n12 2 14\n12 2 13\n12 2 12\n12 2 11\n12 2 10\n12 2 9\n12 2 8\n12 2 7\n12 2 6\n12 2 5\n9 2 24\n9 2 23\n9 2 22\n9 2 21\n9 2 20\n9 2 19\n9 2 18\n9 2 17\n9 2 16\n9 2 15\n9 2 14\n9 2 13\n9 2 12\n9 2 11\n9 2 10\n9 2 9\n9 2 8\n9 2 7\n9 2 6\n15 2 25\n15 2 24\n15 2 23\n15 2 22\n15 2 21\n15 2 20\n15 2 19\n15 2 18\n15 2 17\n15 2 16\n15 2 15\n15 2 14\n15 2 13\n15 2 12\n15 2 11\n15 2 10\n15 2 9\n15 2 8\n15 2 7\n11 2 26\n11 2 25\n11 2 24\n11 2 23\n11 2 22\n11 2 21\n11 2 20\n11 2 19\n11 2 18\n11 2 17\n11 2 16\n11 2 15\n11 2 14\n11 2 13\n11 2 12\n11 2 11\n11 2 10\n11 2 9\n11 2 8\n8 2 27\n8 2 26\n8 2 25\n8 2 24\n8 2 23\n8 2 22\n8 2 21\n8 2 20\n8 2 19\n8 2 18\n8 2 17\n8 2 16\n8 2 15\n8 2 14\n8 2 13\n8 2 12\n8 2 11\n8 2 10\n8 2 9\n16 2 29\n16 2 28\n16 2 27\n16 2 26\n16 2 25\n16 2 24\n16 2 23\n16 2 22\n16 2 21\n16 2 20\n16 2 19\n16 2 18\n16 2 17\n16 2 16\n16 2 15\n16 2 14\n16 2 13\n16 2 12\n16 2 11\n16 2 10\n3 3 1\n1 3 2\n14 3 7\n14 3 6\n14 3 5\n14 3 4\n14 3 3\n20 3 9\n20 3 8\n20 3 7\n20 3 6\n20 3 5\n20 3 4\n7 3 11\n7 3 10\n7 3 9\n7 3 8\n7 3 7\n7 3 6\n7 3 5\n4 3 15\n4 3 14\n4 3 13\n4 3 12\n4 3 11\n4 3 10\n4 3 9\n4 3 8\n4 3 7\n4 3 6\n6 3 17\n6 3 16\n6 3 15\n6 3 14\n6 3 13\n6 3 12\n6 3 11\n6 3 10\n6 3 9\n6 3 8\n6 3 7\n5 3 20\n5 3 19\n5 3 18\n5 3 17\n5 3 16\n5 3 15\n5 3 14\n5 3 13\n5 3 12\n5 3 11\n5 3 10\n5 3 9\n5 3 8\n18 3 22\n18 3 21\n18 3 20\n18 3 19\n18 3 18\n18 3 17\n18 3 16\n18 3 15\n18 3 14\n18 3 13\n18 3 12\n18 3 11\n18 3 10\n18 3 9\n17 3 24\n17 3 23\n17 3 22\n17 3 21\n17 3 20\n17 3 19\n17 3 18\n17 3 17\n17 3 16\n17 3 15\n17 3 14\n17 3 13\n17 3 12\n17 3 11\n17 3 10\n10 3 25\n10 3 24\n10 3 23\n10 3 22\n10 3 21\n10 3 20\n10 3 19\n10 3 18\n10 3 17\n10 3 16\n10 3 15\n10 3 14\n10 3 13\n10 3 12\n10 3 11\n19 3 30\n19 3 29\n19 3 28\n19 3 27\n19 3 26\n19 3 25\n19 3 24\n19 3 23\n19 3 22\n19 3 21\n19 3 20\n19 3 19\n19 3 18\n19 3 17\n19 3 16\n19 3 15\n19 3 14\n19 3 13\n19 3 12\n16 2 11\n8 2 10\n11 2 9\n15 2 8\n9 2 7\n12 2 6\n22 2 5\n13 2 4\n2 2 3\n21 2 2\n3 2 1\n1 3 1\n14 3 2\n20 3 3\n7 3 4\n4 3 5\n6 3 6\n5 3 7\n18 3 8\n17 3 9\n10 3 10\n19 3 11\n16 2 12\n8 2 11\n11 2 10\n15 2 9\n9 2 8\n12 2 7\n22 2 6\n13 2 5\n2 2 4\n21 2 3\n3 2 2\n1 2 1\n14 3 1\n20 3 2\n7 3 3\n4 3 4\n6 3 5\n5 3 6\n18 3 7\n17 3 8\n10 3 9\n19 3 10\n17 4 8\n16 2 13\n8 2 12\n11 2 11\n15 2 10\n9 2 9\n12 2 8\n22 2 7\n13 2 6\n2 2 5\n21 2 4\n3 2 3\n1 2 2\n14 2 1\n20 3 1\n7 3 2\n4 3 3\n6 3 4\n5 3 5\n18 3 6\n10 3 8\n19 3 9\n16 2 14\n8 2 13\n11 2 12\n15 2 11\n9 2 10\n12 2 9\n22 2 8\n13 2 7\n2 2 6\n21 2 5\n3 2 4\n1 2 3\n14 2 2\n20 2 1\n7 3 1\n4 3 2\n6 3 3\n5 3 4\n18 3 5\n10 3 7\n19 3 8\n16 2 15\n8 2 14\n11 2 13\n15 2 12\n9 2 11\n12 2 10\n22 2 9\n13 2 8\n2 2 7\n21 2 6\n3 2 5\n1 2 4\n14 2 3\n20 2 2\n7 2 1\n4 3 1\n6 3 2\n5 3 3\n18 3 4\n10 3 6\n19 3 7\n16 2 16\n8 2 15\n11 2 14\n15 2 13\n9 2 12\n12 2 11\n22 2 10\n13 2 9\n2 2 8\n21 2 7\n3 2 6\n1 2 5\n14 2 4\n20 2 3\n7 2 2\n4 2 1\n6 3 1\n5 3 2\n18 3 3\n10 3 5\n19 3 6\n10 4 5\n16 2 17\n8 2 16\n11 2 15\n15 2 14\n9 2 13\n12 2 12\n22 2 11\n13 2 10\n2 2 9\n21 2 8\n3 2 7\n1 2 6\n14 2 5\n20 2 4\n7 2 3\n4 2 2\n6 2 1\n5 3 1\n18 3 2\n19 3 5\n16 2 18\n8 2 17\n11 2 16\n15 2 15\n9 2 14\n12 2 13\n22 2 12\n13 2 11\n2 2 10\n21 2 9\n3 2 8\n1 2 7\n14 2 6\n20 2 5\n7 2 4\n4 2 3\n6 2 2\n5 2 1\n18 3 1\n19 3 4\n19 4 4\n16 2 19\n8 2 18\n11 2 17\n15 2 16\n9 2 15\n12 2 14\n22 2 13\n13 2 12\n2 2 11\n21 2 10\n3 2 9\n1 2 8\n14 2 7\n20 2 6\n7 2 5\n4 2 4\n6 2 3\n5 2 2\n18 2 1\n16 2 20\n8 2 19\n11 2 18\n15 2 17\n9 2 16\n12 2 15\n22 2 14\n13 2 13\n2 2 12\n21 2 11\n3 2 10\n1 2 9\n14 2 8\n20 2 7\n7 2 6\n4 2 5\n6 2 4\n5 2 3\n18 2 2\n18 1 2\n16 2 21\n8 2 20\n11 2 19\n15 2 18\n9 2 17\n12 2 16\n22 2 15\n13 2 14\n2 2 13\n21 2 12\n3 2 11\n1 2 10\n14 2 9\n20 2 8\n7 2 7\n4 2 6\n6 2 5\n5 2 4\n8 1 20\n15 1 18\n21 1 12\n16 2 22\n11 2 20\n9 2 18\n12 2 17\n22 2 16\n13 2 15\n2 2 14\n3 2 12\n1 2 11\n14 2 10\n20 2 9\n7 2 8\n4 2 7\n6 2 6\n5 2 5\n1 1 11\n16 2 23\n11 2 21\n9 2 19\n12 2 18\n22 2 17\n13 2 16\n2 2 15\n3 2 13\n14 2 11\n20 2 10\n7 2 9\n4 2 8\n6 2 7\n5 2 6\n16 2 24\n11 2 22\n9 2 20\n12 2 19\n22 2 18\n13 2 17\n2 2 16\n3 2 14\n14 2 12\n20 2 11\n7 2 10\n4 2 9\n6 2 8\n5 2 7\n16 2 25\n11 2 23\n9 2 21\n12 2 20\n22 2 19\n13 2 18\n2 2 17\n3 2 15\n14 2 13\n20 2 12\n7 2 11\n4 2 10\n6 2 9\n5 2 8\n16 2 26\n11 2 24\n9 2 22\n12 2 21\n22 2 20\n13 2 19\n2 2 18\n3 2 16\n14 2 14\n20 2 13\n7 2 12\n4 2 11\n6 2 10\n5 2 9\n16 2 27\n11 2 25\n9 2 23\n12 2 22\n22 2 21\n13 2 20\n2 2 19\n3 2 17\n14 2 15\n20 2 14\n7 2 13\n4 2 12\n6 2 11\n5 2 10\n16 2 28\n11 2 26\n9 2 24\n12 2 23\n22 2 22\n13 2 21\n2 2 20\n3 2 18\n14 2 16\n20 2 15\n7 2 14\n4 2 13\n6 2 12\n5 2 11\n16 2 29\n11 2 27\n9 2 25\n12 2 24\n22 2 23\n13 2 22\n2 2 21\n3 2 19\n14 2 17\n20 2 16\n7 2 15\n4 2 14\n6 2 13\n5 2 12\n16 2 30\n11 2 28\n9 2 26\n12 2 25\n22 2 24\n13 2 23\n2 2 22\n3 2 20\n14 2 18\n20 2 17\n7 2 16\n4 2 15\n6 2 14\n5 2 13\n16 2 31\n11 2 29\n9 2 27\n12 2 26\n22 2 25\n13 2 24\n2 2 23\n3 2 21\n14 2 19\n20 2 18\n7 2 17\n4 2 16\n6 2 15\n5 2 14\n4 1 16\n16 2 32\n11 2 30\n9 2 28\n12 2 27\n22 2 26\n13 2 25\n2 2 24\n3 2 22\n14 2 20\n20 2 19\n7 2 18\n6 2 16\n5 2 15\n9 1 28\n16 2 33\n11 2 31\n12 2 28\n22 2 27\n13 2 26\n2 2 25\n3 2 23\n14 2 21\n20 2 20\n7 2 19\n6 2 17\n5 2 16\n16 2 34\n11 2 32\n12 2 29\n22 2 28\n13 2 27\n2 2 26\n3 2 24\n14 2 22\n20 2 21\n7 2 20\n6 2 18\n5 2 17\n16 2 35\n11 2 33\n12 2 30\n22 2 29\n13 2 28\n2 2 27\n3 2 25\n14 2 23\n20 2 22\n7 2 21\n6 2 19\n5 2 18\n16 2 36\n11 2 34\n12 2 31\n22 2 30\n13 2 29\n2 2 28\n3 2 26\n14 2 24\n20 2 23\n7 2 22\n6 2 20\n5 2 19\n16 2 37\n11 2 35\n12 2 32\n22 2 31\n13 2 30\n2 2 29\n3 2 27\n14 2 25\n20 2 24\n7 2 23\n6 2 21\n5 2 20\n16 3 37\n11 2 36\n12 2 33\n22 2 32\n13 2 31\n2 2 30\n3 2 28\n14 2 26\n20 2 25\n7 2 24\n6 2 22\n5 2 21\n16 3 36\n11 2 37\n12 2 34\n22 2 33\n13 2 32\n2 2 31\n3 2 29\n14 2 27\n20 2 26\n7 2 25\n6 2 23\n5 2 22\n22 1 33\n16 3 35\n11 3 37\n12 2 35\n13 2 33\n2 2 32\n3 2 30\n14 2 28\n20 2 27\n7 2 26\n6 2 24\n5 2 23\n16 3 34\n11 3 36\n12 2 36\n13 2 34\n2 2 33\n3 2 31\n14 2 29\n20 2 28\n7 2 27\n6 2 25\n5 2 24\n14 1 29\n16 3 33\n11 3 35\n12 2 37\n13 2 35\n2 2 34\n3 2 32\n20 2 29\n7 2 28\n6 2 26\n5 2 25\n16 3 32\n11 3 34\n12 3 37\n13 2 36\n2 2 35\n3 2 33\n20 2 30\n7 2 29\n6 2 27\n5 2 26\n16 3 31\n11 3 33\n12 3 36\n13 2 37\n2 2 36\n3 2 34\n20 2 31\n7 2 30\n6 2 28\n5 2 27\n16 3 30\n11 3 32\n12 3 35\n13 3 37\n2 2 37\n3 2 35\n20 2 32\n7 2 31\n6 2 29\n5 2 28\n16 3 29\n11 3 31\n12 3 34\n13 3 36\n2 3 37\n3 2 36\n20 2 33\n7 2 32\n6 2 30\n5 2 29\n3 1 36\n16 3 28\n11 3 30\n12 3 33\n13 3 35\n2 3 36\n20 2 34\n7 2 33\n6 2 31\n5 2 30\n16 3 27\n11 3 29\n12 3 32\n13 3 34\n2 3 35\n20 2 35\n7 2 34\n6 2 32\n5 2 31\n16 3 26\n11 3 28\n12 3 31\n13 3 33\n2 3 34\n20 2 36\n7 2 35\n6 2 33\n5 2 32\n16 3 25\n11 3 27\n12 3 30\n13 3 32\n2 3 33\n20 2 37\n7 2 36\n6 2 34\n5 2 33\n16 3 24\n11 3 26\n12 3 29\n13 3 31\n2 3 32\n20 3 37\n7 2 37\n6 2 35\n5 2 34\n16 3 23\n11 3 25\n12 3 28\n13 3 30\n2 3 31\n20 3 36\n7 3 37\n6 2 36\n5 2 35\n16 3 22\n11 3 24\n12 3 27\n13 3 29\n2 3 30\n20 3 35\n7 3 36\n6 2 37\n5 2 36\n16 3 21\n11 3 23\n12 3 26\n13 3 28\n2 3 29\n20 3 34\n7 3 35\n6 3 37\n5 2 37\n7 4 35\n16 3 20\n11 3 22\n12 3 25\n13 3 27\n2 3 28\n20 3 33\n6 3 36\n5 3 37\n16 3 19\n11 3 21\n12 3 24\n13 3 26\n2 3 27\n20 3 32\n6 3 35\n5 3 36\n16 3 18\n11 3 20\n12 3 23\n13 3 25\n2 3 26\n20 3 31\n6 3 34\n5 3 35\n16 3 17\n11 3 19\n12 3 22\n13 3 24\n2 3 25\n20 3 30\n6 3 33\n5 3 34\n16 3 16\n11 3 18\n12 3 21\n13 3 23\n2 3 24\n20 3 29\n6 3 32\n5 3 33\n16 3 15\n11 3 17\n12 3 20\n13 3 22\n2 3 23\n20 3 28\n6 3 31\n5 3 32\n16 3 14\n11 3 16\n12 3 19\n13 3 21\n2 3 22\n20 3 27\n6 3 30\n5 3 31\n12 4 19\n16 3 13\n11 3 15\n13 3 20\n2 3 21\n20 3 26\n6 3 29\n5 3 30\n11 4 15\n16 3 12\n13 3 19\n2 3 20\n20 3 25\n6 3 28\n5 3 29\n16 3 11\n13 3 18\n2 3 19\n20 3 24\n6 3 27\n5 3 28\n16 3 10\n13 3 17\n2 3 18\n20 3 23\n6 3 26\n5 3 27\n16 3 9\n13 3 16\n2 3 17\n20 3 22\n6 3 25\n5 3 26\n16 3 8\n13 3 15\n2 3 16\n20 3 21\n6 3 24\n5 3 25\n16 3 7\n13 3 14\n2 3 15\n20 3 20\n6 3 23\n5 3 24\n16 3 6\n13 3 13\n2 3 14\n20 3 19\n6 3 22\n5 3 23\n16 3 5\n13 3 12\n2 3 13\n20 3 18\n6 3 21\n5 3 22\n16 3 4\n13 3 11\n2 3 12\n20 3 17\n6 3 20\n5 3 21\n16 3 3\n13 3 10\n2 3 11\n20 3 16\n6 3 19\n5 3 20\n16 3 2\n13 3 9\n2 3 10\n20 3 15\n6 3 18\n5 3 19\n6 4 18\n16 3 1\n13 3 8\n2 3 9\n20 3 14\n5 3 18\n16 2 1\n13 3 7\n2 3 8\n20 3 13\n5 3 17\n16 2 2\n13 3 6\n2 3 7\n20 3 12\n5 3 16\n16 2 3\n13 3 5\n2 3 6\n20 3 11\n5 3 15\n20 4 11\n16 2 4\n13 3 4\n2 3 5\n5 3 14\n5 4 14\n16 2 5\n13 3 3\n2 3 4\n16 2 6\n13 3 2\n2 3 3\n2 4 3\n16 1 6\n13 3 1\n13 4 1\n"}, {"input": "37 5\r\n0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 5 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "299\n3 2 12\n3 2 11\n3 2 10\n3 2 9\n3 2 8\n3 2 7\n3 2 6\n3 2 5\n3 2 4\n3 2 3\n3 2 2\n3 2 1\n5 2 24\n5 2 23\n5 2 22\n5 2 21\n5 2 20\n5 2 19\n5 2 18\n5 2 17\n5 2 16\n5 2 15\n5 2 14\n5 2 13\n5 2 12\n5 2 11\n5 2 10\n5 2 9\n5 2 8\n5 2 7\n5 2 6\n5 2 5\n5 2 4\n5 2 3\n5 2 2\n4 3 7\n4 3 6\n4 3 5\n4 3 4\n4 3 3\n4 3 2\n4 3 1\n1 3 20\n1 3 19\n1 3 18\n1 3 17\n1 3 16\n1 3 15\n1 3 14\n1 3 13\n1 3 12\n1 3 11\n1 3 10\n1 3 9\n1 3 8\n1 3 7\n1 3 6\n1 3 5\n1 3 4\n1 3 3\n1 3 2\n2 3 24\n2 3 23\n2 3 22\n2 3 21\n2 3 20\n2 3 19\n2 3 18\n2 3 17\n2 3 16\n2 3 15\n2 3 14\n2 3 13\n2 3 12\n2 3 11\n2 3 10\n2 3 9\n2 3 8\n2 3 7\n2 3 6\n2 3 5\n2 3 4\n2 3 3\n5 2 3\n3 2 2\n4 2 1\n1 3 1\n2 3 2\n5 2 4\n3 2 3\n4 2 2\n1 2 1\n2 3 1\n5 2 5\n3 2 4\n4 2 3\n1 2 2\n2 2 1\n5 2 6\n3 2 5\n4 2 4\n1 2 3\n2 2 2\n5 2 7\n3 2 6\n4 2 5\n1 2 4\n2 2 3\n5 2 8\n3 2 7\n4 2 6\n1 2 5\n2 2 4\n5 2 9\n3 2 8\n4 2 7\n1 2 6\n2 2 5\n5 2 10\n3 2 9\n4 2 8\n1 2 7\n2 2 6\n5 2 11\n3 2 10\n4 2 9\n1 2 8\n2 2 7\n5 2 12\n3 2 11\n4 2 10\n1 2 9\n2 2 8\n2 1 8\n5 2 13\n3 2 12\n4 2 11\n1 2 10\n5 2 14\n3 2 13\n4 2 12\n1 2 11\n5 2 15\n3 2 14\n4 2 13\n1 2 12\n5 2 16\n3 2 15\n4 2 14\n1 2 13\n5 2 17\n3 2 16\n4 2 15\n1 2 14\n5 2 18\n3 2 17\n4 2 16\n1 2 15\n5 2 19\n3 2 18\n4 2 17\n1 2 16\n5 2 20\n3 2 19\n4 2 18\n1 2 17\n5 2 21\n3 2 20\n4 2 19\n1 2 18\n5 2 22\n3 2 21\n4 2 20\n1 2 19\n5 2 23\n3 2 22\n4 2 21\n1 2 20\n5 2 24\n3 2 23\n4 2 22\n1 2 21\n5 2 25\n3 2 24\n4 2 23\n1 2 22\n5 2 26\n3 2 25\n4 2 24\n1 2 23\n5 2 27\n3 2 26\n4 2 25\n1 2 24\n5 2 28\n3 2 27\n4 2 26\n1 2 25\n5 2 29\n3 2 28\n4 2 27\n1 2 26\n5 2 30\n3 2 29\n4 2 28\n1 2 27\n5 2 31\n3 2 30\n4 2 29\n1 2 28\n5 2 32\n3 2 31\n4 2 30\n1 2 29\n5 2 33\n3 2 32\n4 2 31\n1 2 30\n5 2 34\n3 2 33\n4 2 32\n1 2 31\n5 2 35\n3 2 34\n4 2 33\n1 2 32\n5 2 36\n3 2 35\n4 2 34\n1 2 33\n5 2 37\n3 2 36\n4 2 35\n1 2 34\n5 3 37\n3 2 37\n4 2 36\n1 2 35\n1 1 35\n5 3 36\n3 3 37\n4 2 37\n5 3 35\n3 3 36\n4 3 37\n5 3 34\n3 3 35\n4 3 36\n5 3 33\n3 3 34\n4 3 35\n5 3 32\n3 3 33\n4 3 34\n5 3 31\n3 3 32\n4 3 33\n5 3 30\n3 3 31\n4 3 32\n5 3 29\n3 3 30\n4 3 31\n5 3 28\n3 3 29\n4 3 30\n5 3 27\n3 3 28\n4 3 29\n5 3 26\n3 3 27\n4 3 28\n5 3 25\n3 3 26\n4 3 27\n5 3 24\n3 3 25\n4 3 26\n4 4 26\n5 3 23\n3 3 24\n5 3 22\n3 3 23\n5 3 21\n3 3 22\n5 3 20\n3 3 21\n5 3 19\n3 3 20\n5 3 18\n3 3 19\n5 4 18\n3 3 18\n3 3 17\n3 3 16\n3 3 15\n3 3 14\n3 3 13\n3 4 13\n"}, {"input": "48 17\r\n0 0 0 0 0 0 14 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 16 0 0 0 1 0 0 0 3 0 0 15 0 0 0 0 0 0 0 11\r\n0 0 0 0 0 0 0 0 0 0 17 0 0 0 6 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 13 0 3 10 11 0 0 0 0 0 0 0 0 0\r\n0 0 0 2 0 0 0 0 0 0 0 0 0 0 4 0 15 0 0 0 0 0 0 0 0 0 0 9 0 0 16 0 0 12 0 0 0 0 0 0 5 0 0 0 0 0 7 14\r\n0 0 5 13 0 0 0 10 0 0 0 0 17 0 0 0 0 0 0 12 0 0 0 7 0 0 0 0 0 0 9 0 0 0 0 0 6 0 0 0 0 0 4 0 0 0 0 0\r\n", "output": "1254\n17 2 10\n17 2 9\n17 2 8\n17 2 7\n17 2 6\n17 2 5\n17 2 4\n17 2 3\n17 2 2\n17 2 1\n6 2 14\n6 2 13\n6 2 12\n6 2 11\n6 2 10\n6 2 9\n6 2 8\n6 2 7\n6 2 6\n6 2 5\n6 2 4\n6 2 3\n6 2 2\n8 2 18\n8 2 17\n8 2 16\n8 2 15\n8 2 14\n8 2 13\n8 2 12\n8 2 11\n8 2 10\n8 2 9\n8 2 8\n8 2 7\n8 2 6\n8 2 5\n8 2 4\n8 2 3\n1 2 32\n1 2 31\n1 2 30\n1 2 29\n1 2 28\n1 2 27\n1 2 26\n1 2 25\n1 2 24\n1 2 23\n1 2 22\n1 2 21\n1 2 20\n1 2 19\n1 2 18\n1 2 17\n1 2 16\n1 2 15\n1 2 14\n1 2 13\n1 2 12\n1 2 11\n1 2 10\n1 2 9\n1 2 8\n1 2 7\n1 2 6\n1 2 5\n1 2 4\n13 2 34\n13 2 33\n13 2 32\n13 2 31\n13 2 30\n13 2 29\n13 2 28\n13 2 27\n13 2 26\n13 2 25\n13 2 24\n13 2 23\n13 2 22\n13 2 21\n13 2 20\n13 2 19\n13 2 18\n13 2 17\n13 2 16\n13 2 15\n13 2 14\n13 2 13\n13 2 12\n13 2 11\n13 2 10\n13 2 9\n13 2 8\n13 2 7\n13 2 6\n13 2 5\n3 2 36\n3 2 35\n3 2 34\n3 2 33\n3 2 32\n3 2 31\n3 2 30\n3 2 29\n3 2 28\n3 2 27\n3 2 26\n3 2 25\n3 2 24\n3 2 23\n3 2 22\n3 2 21\n3 2 20\n3 2 19\n3 2 18\n3 2 17\n3 2 16\n3 2 15\n3 2 14\n3 2 13\n3 2 12\n3 2 11\n3 2 10\n3 2 9\n3 2 8\n3 2 7\n3 2 6\n10 2 37\n10 2 36\n10 2 35\n10 2 34\n10 2 33\n10 2 32\n10 2 31\n10 2 30\n10 2 29\n10 2 28\n10 2 27\n10 2 26\n10 2 25\n10 2 24\n10 2 23\n10 2 22\n10 2 21\n10 2 20\n10 2 19\n10 2 18\n10 2 17\n10 2 16\n10 2 15\n10 2 14\n10 2 13\n10 2 12\n10 2 11\n10 2 10\n10 2 9\n10 2 8\n10 2 7\n11 2 38\n11 2 37\n11 2 36\n11 2 35\n11 2 34\n11 2 33\n11 2 32\n11 2 31\n11 2 30\n11 2 29\n11 2 28\n11 2 27\n11 2 26\n11 2 25\n11 2 24\n11 2 23\n11 2 22\n11 2 21\n11 2 20\n11 2 19\n11 2 18\n11 2 17\n11 2 16\n11 2 15\n11 2 14\n11 2 13\n11 2 12\n11 2 11\n11 2 10\n11 2 9\n11 2 8\n2 3 3\n2 3 2\n2 3 1\n4 3 14\n4 3 13\n4 3 12\n4 3 11\n4 3 10\n4 3 9\n4 3 8\n4 3 7\n4 3 6\n4 3 5\n4 3 4\n4 3 3\n4 3 2\n15 3 16\n15 3 15\n15 3 14\n15 3 13\n15 3 12\n15 3 11\n15 3 10\n15 3 9\n15 3 8\n15 3 7\n15 3 6\n15 3 5\n15 3 4\n15 3 3\n9 3 27\n9 3 26\n9 3 25\n9 3 24\n9 3 23\n9 3 22\n9 3 21\n9 3 20\n9 3 19\n9 3 18\n9 3 17\n9 3 16\n9 3 15\n9 3 14\n9 3 13\n9 3 12\n9 3 11\n9 3 10\n9 3 9\n9 3 8\n9 3 7\n9 3 6\n9 3 5\n9 3 4\n16 3 30\n16 3 29\n16 3 28\n16 3 27\n16 3 26\n16 3 25\n16 3 24\n16 3 23\n16 3 22\n16 3 21\n16 3 20\n16 3 19\n16 3 18\n16 3 17\n16 3 16\n16 3 15\n16 3 14\n16 3 13\n16 3 12\n16 3 11\n16 3 10\n16 3 9\n16 3 8\n16 3 7\n16 3 6\n16 3 5\n12 3 33\n12 3 32\n12 3 31\n12 3 30\n12 3 29\n12 3 28\n12 3 27\n12 3 26\n12 3 25\n12 3 24\n12 3 23\n12 3 22\n12 3 21\n12 3 20\n12 3 19\n12 3 18\n12 3 17\n12 3 16\n12 3 15\n12 3 14\n12 3 13\n12 3 12\n12 3 11\n12 3 10\n12 3 9\n12 3 8\n12 3 7\n12 3 6\n5 3 40\n5 3 39\n5 3 38\n5 3 37\n5 3 36\n5 3 35\n5 3 34\n5 3 33\n5 3 32\n5 3 31\n5 3 30\n5 3 29\n5 3 28\n5 3 27\n5 3 26\n5 3 25\n5 3 24\n5 3 23\n5 3 22\n5 3 21\n5 3 20\n5 3 19\n5 3 18\n5 3 17\n5 3 16\n5 3 15\n5 3 14\n5 3 13\n5 3 12\n5 3 11\n5 3 10\n5 3 9\n5 3 8\n5 3 7\n7 3 46\n7 3 45\n7 3 44\n7 3 43\n7 3 42\n7 3 41\n7 3 40\n7 3 39\n7 3 38\n7 3 37\n7 3 36\n7 3 35\n7 3 34\n7 3 33\n7 3 32\n7 3 31\n7 3 30\n7 3 29\n7 3 28\n7 3 27\n7 3 26\n7 3 25\n7 3 24\n7 3 23\n7 3 22\n7 3 21\n7 3 20\n7 3 19\n7 3 18\n7 3 17\n7 3 16\n7 3 15\n7 3 14\n7 3 13\n7 3 12\n7 3 11\n7 3 10\n7 3 9\n7 3 8\n14 3 47\n14 3 46\n14 3 45\n14 3 44\n14 3 43\n14 3 42\n14 3 41\n14 3 40\n14 3 39\n14 3 38\n14 3 37\n14 3 36\n14 3 35\n14 3 34\n14 3 33\n14 3 32\n14 3 31\n14 3 30\n14 3 29\n14 3 28\n14 3 27\n14 3 26\n14 3 25\n14 3 24\n14 3 23\n14 3 22\n14 3 21\n14 3 20\n14 3 19\n14 3 18\n14 3 17\n14 3 16\n14 3 15\n14 3 14\n14 3 13\n14 3 12\n14 3 11\n14 3 10\n14 3 9\n11 2 9\n10 2 8\n3 2 7\n13 2 6\n1 2 5\n8 2 4\n6 2 3\n17 2 2\n2 2 1\n4 3 1\n15 3 2\n9 3 3\n16 3 4\n12 3 5\n5 3 6\n7 3 7\n14 3 8\n11 2 10\n10 2 9\n3 2 8\n13 2 7\n1 2 6\n8 2 5\n6 2 4\n17 2 3\n2 2 2\n4 2 1\n15 3 1\n9 3 2\n16 3 3\n12 3 4\n5 3 5\n7 3 6\n14 3 7\n11 2 11\n10 2 10\n3 2 9\n13 2 8\n1 2 7\n8 2 6\n6 2 5\n17 2 4\n2 2 3\n4 2 2\n15 2 1\n9 3 1\n16 3 2\n12 3 3\n5 3 4\n7 3 5\n14 3 6\n11 2 12\n10 2 11\n3 2 10\n13 2 9\n1 2 8\n8 2 7\n6 2 6\n17 2 5\n2 2 4\n4 2 3\n15 2 2\n9 2 1\n16 3 1\n12 3 2\n5 3 3\n7 3 4\n14 3 5\n5 4 3\n11 2 13\n10 2 12\n3 2 11\n13 2 10\n1 2 9\n8 2 8\n6 2 7\n17 2 6\n2 2 5\n4 2 4\n15 2 3\n9 2 2\n16 2 1\n12 3 1\n7 3 3\n14 3 4\n11 2 14\n10 2 13\n3 2 12\n13 2 11\n1 2 10\n8 2 9\n6 2 8\n17 2 7\n2 2 6\n4 2 5\n15 2 4\n9 2 3\n16 2 2\n12 2 1\n7 3 2\n14 3 3\n11 2 15\n10 2 14\n3 2 13\n13 2 12\n1 2 11\n8 2 10\n6 2 9\n17 2 8\n2 2 7\n4 2 6\n15 2 5\n9 2 4\n16 2 3\n12 2 2\n7 3 1\n14 3 2\n11 2 16\n10 2 15\n3 2 14\n13 2 13\n1 2 12\n8 2 11\n6 2 10\n17 2 9\n2 2 8\n4 2 7\n15 2 6\n9 2 5\n16 2 4\n12 2 3\n7 2 1\n14 3 1\n11 2 17\n10 2 16\n3 2 15\n13 2 14\n1 2 13\n8 2 12\n6 2 11\n17 2 10\n2 2 9\n4 2 8\n15 2 7\n9 2 6\n16 2 5\n12 2 4\n7 2 2\n14 2 1\n11 2 18\n10 2 17\n3 2 16\n13 2 15\n1 2 14\n8 2 13\n6 2 12\n17 2 11\n2 2 10\n4 2 9\n15 2 8\n9 2 7\n16 2 6\n12 2 5\n7 2 3\n14 2 2\n11 2 19\n10 2 18\n3 2 17\n13 2 16\n1 2 15\n8 2 14\n6 2 13\n17 2 12\n2 2 11\n4 2 10\n15 2 9\n9 2 8\n16 2 7\n12 2 6\n7 2 4\n14 2 3\n11 2 20\n10 2 19\n3 2 18\n13 2 17\n1 2 16\n8 2 15\n6 2 14\n17 2 13\n2 2 12\n4 2 11\n15 2 10\n9 2 9\n16 2 8\n12 2 7\n7 2 5\n14 2 4\n2 1 12\n11 2 21\n10 2 20\n3 2 19\n13 2 18\n1 2 17\n8 2 16\n6 2 15\n17 2 14\n4 2 12\n15 2 11\n9 2 10\n16 2 9\n12 2 8\n7 2 6\n14 2 5\n11 2 22\n10 2 21\n3 2 20\n13 2 19\n1 2 18\n8 2 17\n6 2 16\n17 2 15\n4 2 13\n15 2 12\n9 2 11\n16 2 10\n12 2 9\n7 2 7\n14 2 6\n11 2 23\n10 2 22\n3 2 21\n13 2 20\n1 2 19\n8 2 18\n6 2 17\n17 2 16\n4 2 14\n15 2 13\n9 2 12\n16 2 11\n12 2 10\n7 2 8\n14 2 7\n14 1 7\n11 2 24\n10 2 23\n3 2 22\n13 2 21\n1 2 20\n8 2 19\n6 2 18\n17 2 17\n4 2 15\n15 2 14\n9 2 13\n16 2 12\n12 2 11\n7 2 9\n11 2 25\n10 2 24\n3 2 23\n13 2 22\n1 2 21\n8 2 20\n6 2 19\n17 2 18\n4 2 16\n15 2 15\n9 2 14\n16 2 13\n12 2 12\n7 2 10\n11 2 26\n10 2 25\n3 2 24\n13 2 23\n1 2 22\n8 2 21\n6 2 20\n17 2 19\n4 2 17\n15 2 16\n9 2 15\n16 2 14\n12 2 13\n7 2 11\n11 2 27\n10 2 26\n3 2 25\n13 2 24\n1 2 23\n8 2 22\n6 2 21\n17 2 20\n4 2 18\n15 2 17\n9 2 16\n16 2 15\n12 2 14\n7 2 12\n11 2 28\n10 2 27\n3 2 26\n13 2 25\n1 2 24\n8 2 23\n6 2 22\n17 2 21\n4 2 19\n15 2 18\n9 2 17\n16 2 16\n12 2 15\n7 2 13\n11 2 29\n10 2 28\n3 2 27\n13 2 26\n1 2 25\n8 2 24\n6 2 23\n17 2 22\n4 2 20\n15 2 19\n9 2 18\n16 2 17\n12 2 16\n7 2 14\n11 2 30\n10 2 29\n3 2 28\n13 2 27\n1 2 26\n8 2 25\n6 2 24\n17 2 23\n4 2 21\n15 2 20\n9 2 19\n16 2 18\n12 2 17\n7 2 15\n11 2 31\n10 2 30\n3 2 29\n13 2 28\n1 2 27\n8 2 26\n6 2 25\n17 2 24\n4 2 22\n15 2 21\n9 2 20\n16 2 19\n12 2 18\n7 2 16\n8 1 26\n11 2 32\n10 2 31\n3 2 30\n13 2 29\n1 2 28\n6 2 26\n17 2 25\n4 2 23\n15 2 22\n9 2 21\n16 2 20\n12 2 19\n7 2 17\n11 2 33\n10 2 32\n3 2 31\n13 2 30\n1 2 29\n6 2 27\n17 2 26\n4 2 24\n15 2 23\n9 2 22\n16 2 21\n12 2 20\n7 2 18\n11 2 34\n10 2 33\n3 2 32\n13 2 31\n1 2 30\n6 2 28\n17 2 27\n4 2 25\n15 2 24\n9 2 23\n16 2 22\n12 2 21\n7 2 19\n11 2 35\n10 2 34\n3 2 33\n13 2 32\n1 2 31\n6 2 29\n17 2 28\n4 2 26\n15 2 25\n9 2 24\n16 2 23\n12 2 22\n7 2 20\n11 2 36\n10 2 35\n3 2 34\n13 2 33\n1 2 32\n6 2 30\n17 2 29\n4 2 27\n15 2 26\n9 2 25\n16 2 24\n12 2 23\n7 2 21\n11 2 37\n10 2 36\n3 2 35\n13 2 34\n1 2 33\n6 2 31\n17 2 30\n4 2 28\n15 2 27\n9 2 26\n16 2 25\n12 2 24\n7 2 22\n1 1 33\n11 2 38\n10 2 37\n3 2 36\n13 2 35\n6 2 32\n17 2 31\n4 2 29\n15 2 28\n9 2 27\n16 2 26\n12 2 25\n7 2 23\n11 2 39\n10 2 38\n3 2 37\n13 2 36\n6 2 33\n17 2 32\n4 2 30\n15 2 29\n9 2 28\n16 2 27\n12 2 26\n7 2 24\n3 1 37\n11 2 40\n10 2 39\n13 2 37\n6 2 34\n17 2 33\n4 2 31\n15 2 30\n9 2 29\n16 2 28\n12 2 27\n7 2 25\n11 2 41\n10 2 40\n13 2 38\n6 2 35\n17 2 34\n4 2 32\n15 2 31\n9 2 30\n16 2 29\n12 2 28\n7 2 26\n16 1 29\n11 2 42\n10 2 41\n13 2 39\n6 2 36\n17 2 35\n4 2 33\n15 2 32\n9 2 31\n12 2 29\n7 2 27\n11 2 43\n10 2 42\n13 2 40\n6 2 37\n17 2 36\n4 2 34\n15 2 33\n9 2 32\n12 2 30\n7 2 28\n11 2 44\n10 2 43\n13 2 41\n6 2 38\n17 2 37\n4 2 35\n15 2 34\n9 2 33\n12 2 31\n7 2 29\n11 2 45\n10 2 44\n13 2 42\n6 2 39\n17 2 38\n4 2 36\n15 2 35\n9 2 34\n12 2 32\n7 2 30\n11 2 46\n10 2 45\n13 2 43\n6 2 40\n17 2 39\n4 2 37\n15 2 36\n9 2 35\n12 2 33\n7 2 31\n11 2 47\n10 2 46\n13 2 44\n6 2 41\n17 2 40\n4 2 38\n15 2 37\n9 2 36\n12 2 34\n7 2 32\n11 2 48\n10 2 47\n13 2 45\n6 2 42\n17 2 41\n4 2 39\n15 2 38\n9 2 37\n12 2 35\n7 2 33\n11 1 48\n10 2 48\n13 2 46\n6 2 43\n17 2 42\n4 2 40\n15 2 39\n9 2 38\n12 2 36\n7 2 34\n10 3 48\n13 2 47\n6 2 44\n17 2 43\n4 2 41\n15 2 40\n9 2 39\n12 2 37\n7 2 35\n15 1 40\n10 3 47\n13 2 48\n6 2 45\n17 2 44\n4 2 42\n9 2 40\n12 2 38\n7 2 36\n10 3 46\n13 3 48\n6 2 46\n17 2 45\n4 2 43\n9 2 41\n12 2 39\n7 2 37\n10 3 45\n13 3 47\n6 2 47\n17 2 46\n4 2 44\n9 2 42\n12 2 40\n7 2 38\n10 3 44\n13 3 46\n6 2 48\n17 2 47\n4 2 45\n9 2 43\n12 2 41\n7 2 39\n10 3 43\n13 3 45\n6 3 48\n17 2 48\n4 2 46\n9 2 44\n12 2 42\n7 2 40\n10 3 42\n13 3 44\n6 3 47\n17 3 48\n4 2 47\n9 2 45\n12 2 43\n7 2 41\n10 3 41\n13 3 43\n6 3 46\n17 3 47\n4 2 48\n9 2 46\n12 2 44\n7 2 42\n10 3 40\n13 3 42\n6 3 45\n17 3 46\n4 3 48\n9 2 47\n12 2 45\n7 2 43\n10 3 39\n13 3 41\n6 3 44\n17 3 45\n4 3 47\n9 2 48\n12 2 46\n7 2 44\n10 3 38\n13 3 40\n6 3 43\n17 3 44\n4 3 46\n9 3 48\n12 2 47\n7 2 45\n10 3 37\n13 3 39\n6 3 42\n17 3 43\n4 3 45\n9 3 47\n12 2 48\n7 2 46\n10 3 36\n13 3 38\n6 3 41\n17 3 42\n4 3 44\n9 3 46\n12 3 48\n7 2 47\n10 3 35\n13 3 37\n6 3 40\n17 3 41\n4 3 43\n9 3 45\n12 3 47\n7 2 48\n4 4 43\n10 3 34\n13 3 36\n6 3 39\n17 3 40\n9 3 44\n12 3 46\n7 3 48\n10 3 33\n13 3 35\n6 3 38\n17 3 39\n9 3 43\n12 3 45\n7 3 47\n10 3 32\n13 3 34\n6 3 37\n17 3 38\n9 3 42\n12 3 44\n7 3 46\n6 4 37\n10 3 31\n13 3 33\n17 3 37\n9 3 41\n12 3 43\n7 3 45\n10 3 30\n13 3 32\n17 3 36\n9 3 40\n12 3 42\n7 3 44\n10 3 29\n13 3 31\n17 3 35\n9 3 39\n12 3 41\n7 3 43\n10 3 28\n13 3 30\n17 3 34\n9 3 38\n12 3 40\n7 3 42\n10 3 27\n13 3 29\n17 3 33\n9 3 37\n12 3 39\n7 3 41\n10 3 26\n13 3 28\n17 3 32\n9 3 36\n12 3 38\n7 3 40\n10 3 25\n13 3 27\n17 3 31\n9 3 35\n12 3 37\n7 3 39\n10 3 24\n13 3 26\n17 3 30\n9 3 34\n12 3 36\n7 3 38\n10 3 23\n13 3 25\n17 3 29\n9 3 33\n12 3 35\n7 3 37\n10 3 22\n13 3 24\n17 3 28\n9 3 32\n12 3 34\n7 3 36\n10 3 21\n13 3 23\n17 3 27\n9 3 31\n12 3 33\n7 3 35\n9 4 31\n10 3 20\n13 3 22\n17 3 26\n12 3 32\n7 3 34\n10 3 19\n13 3 21\n17 3 25\n12 3 31\n7 3 33\n10 3 18\n13 3 20\n17 3 24\n12 3 30\n7 3 32\n10 3 17\n13 3 19\n17 3 23\n12 3 29\n7 3 31\n10 3 16\n13 3 18\n17 3 22\n12 3 28\n7 3 30\n10 3 15\n13 3 17\n17 3 21\n12 3 27\n7 3 29\n10 3 14\n13 3 16\n17 3 20\n12 3 26\n7 3 28\n10 3 13\n13 3 15\n17 3 19\n12 3 25\n7 3 27\n10 3 12\n13 3 14\n17 3 18\n12 3 24\n7 3 26\n10 3 11\n13 3 13\n17 3 17\n12 3 23\n7 3 25\n10 3 10\n13 3 12\n17 3 16\n12 3 22\n7 3 24\n7 4 24\n10 3 9\n13 3 11\n17 3 15\n12 3 21\n10 3 8\n13 3 10\n17 3 14\n12 3 20\n10 4 8\n12 4 20\n13 3 9\n17 3 13\n17 4 13\n13 3 8\n13 3 7\n13 3 6\n13 3 5\n13 3 4\n13 4 4\n"}, {"input": "22 2\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0\r\n0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 0\r\n", "output": "65\r\n2 2 13\r\n1 3 21\r\n2 2 12\r\n1 3 22\r\n2 2 11\r\n1 2 22\r\n2 2 10\r\n1 2 21\r\n2 2 9\r\n1 2 20\r\n2 2 8\r\n1 2 19\r\n2 2 7\r\n1 2 18\r\n2 2 6\r\n1 2 17\r\n2 2 5\r\n1 2 16\r\n2 2 4\r\n1 2 15\r\n2 2 3\r\n1 2 14\r\n2 2 2\r\n1 2 13\r\n2 2 1\r\n1 2 12\r\n2 3 1\r\n1 2 11\r\n2 3 2\r\n1 2 10\r\n2 3 3\r\n1 2 9\r\n2 3 4\r\n1 2 8\r\n2 3 5\r\n1 2 7\r\n2 3 6\r\n1 2 6\r\n2 3 7\r\n1 2 5\r\n2 3 8\r\n1 2 4\r\n2 3 9\r\n1 2 3\r\n2 3 10\r\n1 2 2\r\n2 3 11\r\n1 2 1\r\n2 3 12\r\n1 3 1\r\n2 3 13\r\n1 3 2\r\n2 3 14\r\n1 3 3\r\n2 3 15\r\n1 3 4\r\n2 3 16\r\n1 3 5\r\n2 3 17\r\n1 3 6\r\n2 3 18\r\n1 3 7\r\n2 4 18\r\n1 3 8\r\n1 4 8\r\n"}, {"input": "12 3\r\n0 0 0 0 0 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0\r\n2 0 0 0 0 3 0 0 0 1 0 0\r\n0 0 0 0 0 0 0 1 3 0 2 0\r\n", "output": "38\r\n1 3 11\r\n3 3 7\r\n2 3 2\r\n1 3 12\r\n3 3 8\r\n2 3 3\r\n1 2 12\r\n3 3 9\r\n2 3 4\r\n1 2 11\r\n3 4 9\r\n2 3 5\r\n1 2 10\r\n2 3 6\r\n1 2 9\r\n2 3 7\r\n1 2 8\r\n2 3 8\r\n1 2 7\r\n2 3 9\r\n1 2 6\r\n2 3 10\r\n1 2 5\r\n2 3 11\r\n1 2 4\r\n2 4 11\r\n1 2 3\r\n1 2 2\r\n1 2 1\r\n1 3 1\r\n1 3 2\r\n1 3 3\r\n1 3 4\r\n1 3 5\r\n1 3 6\r\n1 3 7\r\n1 3 8\r\n1 4 8\r\n"}, {"input": "10 20\r\n18 9 4 5 12 14 16 1 15 20\r\n11 13 16 6 18 5 20 17 4 3\r\n12 9 15 14 8 10 2 19 1 7\r\n6 11 13 2 7 19 10 3 8 17\r\n", "output": "-1\r\n"}, {"input": "10 20\r\n1 12 11 7 4 2 13 10 20 9\r\n18 9 1 5 16 15 8 20 7 13\r\n2 10 4 12 14 19 3 11 17 6\r\n3 18 8 6 15 19 16 14 17 5\r\n", "output": "200\n17 4 9\n19 4 6\n3 3 6\n11 3 7\n6 3 9\n6 3 8\n13 3 10\n7 2 10\n20 2 9\n8 2 8\n15 2 7\n16 2 6\n5 2 5\n1 2 4\n9 2 3\n18 2 2\n2 2 1\n10 3 1\n4 3 2\n12 3 3\n14 3 4\n3 3 5\n11 3 6\n6 3 7\n20 1 9\n13 3 9\n7 3 10\n8 2 9\n15 2 8\n16 2 7\n5 2 6\n1 2 5\n9 2 4\n18 2 3\n2 2 2\n10 2 1\n4 3 1\n12 3 2\n14 3 3\n3 3 4\n11 3 5\n6 3 6\n13 3 8\n7 3 9\n8 2 10\n15 2 9\n16 2 8\n5 2 7\n1 2 6\n9 2 5\n18 2 4\n2 2 3\n10 2 2\n4 2 1\n12 3 1\n14 3 2\n3 3 3\n11 3 4\n6 3 5\n13 3 7\n7 3 8\n8 3 10\n15 2 10\n16 2 9\n5 2 8\n1 2 7\n9 2 6\n18 2 5\n2 2 4\n10 2 3\n4 2 2\n12 2 1\n14 3 1\n3 3 2\n11 3 3\n6 3 4\n6 4 4\n13 3 6\n7 3 7\n8 3 9\n15 3 10\n16 2 10\n5 2 9\n1 2 8\n9 2 7\n18 2 6\n2 2 5\n10 2 4\n4 2 3\n12 2 2\n14 2 1\n3 3 1\n11 3 2\n3 4 1\n12 1 2\n13 3 5\n7 3 6\n8 3 8\n15 3 9\n16 3 10\n5 2 10\n1 2 9\n9 2 8\n18 2 7\n2 2 6\n10 2 5\n4 2 4\n14 2 2\n11 3 1\n2 1 6\n13 3 4\n7 3 5\n8 3 7\n15 3 8\n16 3 9\n5 3 10\n1 2 10\n9 2 9\n18 2 8\n10 2 6\n4 2 5\n14 2 3\n11 2 1\n4 1 5\n5 4 10\n13 3 3\n7 3 4\n8 3 6\n15 3 7\n16 3 8\n1 3 10\n9 2 10\n18 2 9\n10 2 7\n14 2 4\n11 2 2\n9 1 10\n13 3 2\n7 3 3\n8 3 5\n15 3 6\n16 3 7\n1 3 9\n18 2 10\n10 2 8\n14 2 5\n11 2 3\n10 1 8\n11 1 3\n16 4 7\n13 3 1\n7 3 2\n8 3 4\n15 3 5\n1 3 8\n18 3 10\n14 2 6\n15 4 5\n13 2 1\n7 3 1\n8 3 3\n1 3 7\n18 3 9\n14 2 7\n8 4 3\n13 2 2\n7 2 1\n1 3 6\n18 3 8\n14 2 8\n13 2 3\n7 2 2\n1 3 5\n18 3 7\n14 2 9\n13 2 4\n7 2 3\n1 3 4\n18 3 6\n14 2 10\n13 2 5\n7 2 4\n1 3 3\n18 3 5\n14 3 10\n7 1 4\n13 2 6\n1 3 2\n18 3 4\n14 3 9\n13 2 7\n1 3 1\n18 3 3\n14 3 8\n13 1 7\n14 4 8\n1 2 1\n18 3 2\n1 1 1\n18 4 2\n"}, {"input": "15 30\r\n20 24 17 13 26 8 5 6 27 14 18 22 25 2 15\r\n4 12 6 25 3 5 28 11 15 21 9 26 7 17 13\r\n19 20 24 16 2 23 8 29 22 30 1 27 10 14 18\r\n9 29 3 7 12 28 10 16 23 19 21 1 30 11 4\r\n", "output": "-1\r\n"}, {"input": "40 20\r\n15 0 0 0 0 0 0 0 7 3 0 0 18 0 0 0 4 0 1 0 0 0 11 0 0 0 0 0 0 0 0 0 5 0 0 14 2 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 12 18 17 0 0 0 0 20 0 6 0 0 14 13 0 19 2 0 0 4 0 0 0 0\r\n15 0 0 0 0 0 9 0 7 0 0 16 8 5 0 0 0 0 0 0 0 0 0 0 0 10 0 0 11 0 0 0 0 0 0 3 0 0 0 0\r\n0 16 0 0 0 0 0 17 0 0 0 0 6 0 0 0 8 0 0 0 0 0 0 0 13 10 0 0 19 0 0 0 0 12 9 20 0 0 0 0\r\n", "output": "1005\n1 2 11\n1 2 10\n1 2 9\n1 2 8\n1 2 7\n1 2 6\n1 2 5\n1 2 4\n1 2 3\n1 2 2\n1 2 1\n12 2 16\n12 2 15\n12 2 14\n12 2 13\n12 2 12\n12 2 11\n12 2 10\n12 2 9\n12 2 8\n12 2 7\n12 2 6\n12 2 5\n12 2 4\n12 2 3\n12 2 2\n18 2 17\n18 2 16\n18 2 15\n18 2 14\n18 2 13\n18 2 12\n18 2 11\n18 2 10\n18 2 9\n18 2 8\n18 2 7\n18 2 6\n18 2 5\n18 2 4\n18 2 3\n17 2 18\n17 2 17\n17 2 16\n17 2 15\n17 2 14\n17 2 13\n17 2 12\n17 2 11\n17 2 10\n17 2 9\n17 2 8\n17 2 7\n17 2 6\n17 2 5\n17 2 4\n20 2 23\n20 2 22\n20 2 21\n20 2 20\n20 2 19\n20 2 18\n20 2 17\n20 2 16\n20 2 15\n20 2 14\n20 2 13\n20 2 12\n20 2 11\n20 2 10\n20 2 9\n20 2 8\n20 2 7\n20 2 6\n20 2 5\n6 2 25\n6 2 24\n6 2 23\n6 2 22\n6 2 21\n6 2 20\n6 2 19\n6 2 18\n6 2 17\n6 2 16\n6 2 15\n6 2 14\n6 2 13\n6 2 12\n6 2 11\n6 2 10\n6 2 9\n6 2 8\n6 2 7\n6 2 6\n14 2 28\n14 2 27\n14 2 26\n14 2 25\n14 2 24\n14 2 23\n14 2 22\n14 2 21\n14 2 20\n14 2 19\n14 2 18\n14 2 17\n14 2 16\n14 2 15\n14 2 14\n14 2 13\n14 2 12\n14 2 11\n14 2 10\n14 2 9\n14 2 8\n14 2 7\n13 2 29\n13 2 28\n13 2 27\n13 2 26\n13 2 25\n13 2 24\n13 2 23\n13 2 22\n13 2 21\n13 2 20\n13 2 19\n13 2 18\n13 2 17\n13 2 16\n13 2 15\n13 2 14\n13 2 13\n13 2 12\n13 2 11\n13 2 10\n13 2 9\n13 2 8\n19 2 31\n19 2 30\n19 2 29\n19 2 28\n19 2 27\n19 2 26\n19 2 25\n19 2 24\n19 2 23\n19 2 22\n19 2 21\n19 2 20\n19 2 19\n19 2 18\n19 2 17\n19 2 16\n19 2 15\n19 2 14\n19 2 13\n19 2 12\n19 2 11\n19 2 10\n19 2 9\n2 2 32\n2 2 31\n2 2 30\n2 2 29\n2 2 28\n2 2 27\n2 2 26\n2 2 25\n2 2 24\n2 2 23\n2 2 22\n2 2 21\n2 2 20\n2 2 19\n2 2 18\n2 2 17\n2 2 16\n2 2 15\n2 2 14\n2 2 13\n2 2 12\n2 2 11\n2 2 10\n4 2 35\n4 2 34\n4 2 33\n4 2 32\n4 2 31\n4 2 30\n4 2 29\n4 2 28\n4 2 27\n4 2 26\n4 2 25\n4 2 24\n4 2 23\n4 2 22\n4 2 21\n4 2 20\n4 2 19\n4 2 18\n4 2 17\n4 2 16\n4 2 15\n4 2 14\n4 2 13\n4 2 12\n4 2 11\n9 3 6\n9 3 5\n9 3 4\n9 3 3\n9 3 2\n7 3 8\n7 3 7\n7 3 6\n7 3 5\n7 3 4\n7 3 3\n16 3 11\n16 3 10\n16 3 9\n16 3 8\n16 3 7\n16 3 6\n16 3 5\n16 3 4\n8 3 12\n8 3 11\n8 3 10\n8 3 9\n8 3 8\n8 3 7\n8 3 6\n8 3 5\n5 3 13\n5 3 12\n5 3 11\n5 3 10\n5 3 9\n5 3 8\n5 3 7\n5 3 6\n10 3 25\n10 3 24\n10 3 23\n10 3 22\n10 3 21\n10 3 20\n10 3 19\n10 3 18\n10 3 17\n10 3 16\n10 3 15\n10 3 14\n10 3 13\n10 3 12\n10 3 11\n10 3 10\n10 3 9\n10 3 8\n10 3 7\n11 3 28\n11 3 27\n11 3 26\n11 3 25\n11 3 24\n11 3 23\n11 3 22\n11 3 21\n11 3 20\n11 3 19\n11 3 18\n11 3 17\n11 3 16\n11 3 15\n11 3 14\n11 3 13\n11 3 12\n11 3 11\n11 3 10\n11 3 9\n11 3 8\n3 3 35\n3 3 34\n3 3 33\n3 3 32\n3 3 31\n3 3 30\n3 3 29\n3 3 28\n3 3 27\n3 3 26\n3 3 25\n3 3 24\n3 3 23\n3 3 22\n3 3 21\n3 3 20\n3 3 19\n3 3 18\n3 3 17\n3 3 16\n3 3 15\n3 3 14\n3 3 13\n3 3 12\n3 3 11\n3 3 10\n3 3 9\n4 2 12\n2 2 11\n19 2 10\n13 2 9\n14 2 8\n6 2 7\n20 2 6\n17 2 5\n18 2 4\n12 2 3\n1 2 2\n15 2 1\n9 3 1\n7 3 2\n16 3 3\n8 3 4\n5 3 5\n10 3 6\n11 3 7\n3 3 8\n15 1 1\n4 2 13\n2 2 12\n19 2 11\n13 2 10\n14 2 9\n6 2 8\n20 2 7\n17 2 6\n18 2 5\n12 2 4\n1 2 3\n9 2 1\n7 3 1\n16 3 2\n8 3 3\n5 3 4\n10 3 5\n11 3 6\n3 3 7\n16 4 2\n4 2 14\n2 2 13\n19 2 12\n13 2 11\n14 2 10\n6 2 9\n20 2 8\n17 2 7\n18 2 6\n12 2 5\n1 2 4\n9 2 2\n7 2 1\n8 3 2\n5 3 3\n10 3 4\n11 3 5\n3 3 6\n4 2 15\n2 2 14\n19 2 13\n13 2 12\n14 2 11\n6 2 10\n20 2 9\n17 2 8\n18 2 7\n12 2 6\n1 2 5\n9 2 3\n7 2 2\n8 3 1\n5 3 2\n10 3 3\n11 3 4\n3 3 5\n4 2 16\n2 2 15\n19 2 14\n13 2 13\n14 2 12\n6 2 11\n20 2 10\n17 2 9\n18 2 8\n12 2 7\n1 2 6\n9 2 4\n7 2 3\n8 2 1\n5 3 1\n10 3 2\n11 3 3\n3 3 4\n4 2 17\n2 2 16\n19 2 15\n13 2 14\n14 2 13\n6 2 12\n20 2 11\n17 2 10\n18 2 9\n12 2 8\n1 2 7\n9 2 5\n7 2 4\n8 2 2\n5 2 1\n10 3 1\n11 3 2\n3 3 3\n4 1 17\n2 2 17\n19 2 16\n13 2 15\n14 2 14\n6 2 13\n20 2 12\n17 2 11\n18 2 10\n12 2 9\n1 2 8\n9 2 6\n7 2 5\n8 2 3\n5 2 2\n10 2 1\n11 3 1\n3 3 2\n2 2 18\n19 2 17\n13 2 16\n14 2 15\n6 2 14\n20 2 13\n17 2 12\n18 2 11\n12 2 10\n1 2 9\n9 2 7\n7 2 6\n8 2 4\n5 2 3\n10 2 2\n11 2 1\n3 3 1\n2 2 19\n19 2 18\n13 2 17\n14 2 16\n6 2 15\n20 2 14\n17 2 13\n18 2 12\n12 2 11\n1 2 10\n9 2 8\n7 2 7\n8 2 5\n5 2 4\n10 2 3\n11 2 2\n3 2 1\n2 2 20\n19 2 19\n13 2 18\n14 2 17\n6 2 16\n20 2 15\n17 2 14\n18 2 13\n12 2 12\n1 2 11\n9 2 9\n7 2 8\n8 2 6\n5 2 5\n10 2 4\n11 2 3\n3 2 2\n18 1 13\n2 2 21\n19 2 20\n13 2 19\n14 2 18\n6 2 17\n20 2 16\n17 2 15\n12 2 13\n1 2 12\n9 2 10\n7 2 9\n8 2 7\n5 2 6\n10 2 5\n11 2 4\n3 2 3\n7 1 9\n2 2 22\n19 2 21\n13 2 20\n14 2 19\n6 2 18\n20 2 17\n17 2 16\n12 2 14\n1 2 13\n9 2 11\n8 2 8\n5 2 7\n10 2 6\n11 2 5\n3 2 4\n2 2 23\n19 2 22\n13 2 21\n14 2 20\n6 2 19\n20 2 18\n17 2 17\n12 2 15\n1 2 14\n9 2 12\n8 2 9\n5 2 8\n10 2 7\n11 2 6\n3 2 5\n2 2 24\n19 2 23\n13 2 22\n14 2 21\n6 2 20\n20 2 19\n17 2 18\n12 2 16\n1 2 15\n9 2 13\n8 2 10\n5 2 9\n10 2 8\n11 2 7\n3 2 6\n2 2 25\n19 2 24\n13 2 23\n14 2 22\n6 2 21\n20 2 20\n17 2 19\n12 2 17\n1 2 16\n9 2 14\n8 2 11\n5 2 10\n10 2 9\n11 2 8\n3 2 7\n2 2 26\n19 2 25\n13 2 24\n14 2 23\n6 2 22\n20 2 21\n17 2 20\n12 2 18\n1 2 17\n9 2 15\n8 2 12\n5 2 11\n10 2 10\n11 2 9\n3 2 8\n2 2 27\n19 2 26\n13 2 25\n14 2 24\n6 2 23\n20 2 22\n17 2 21\n12 2 19\n1 2 18\n9 2 16\n8 2 13\n5 2 12\n10 2 11\n11 2 10\n3 2 9\n2 2 28\n19 2 27\n13 2 26\n14 2 25\n6 2 24\n20 2 23\n17 2 22\n12 2 20\n1 2 19\n9 2 17\n8 2 14\n5 2 13\n10 2 12\n11 2 11\n3 2 10\n1 1 19\n3 1 10\n2 2 29\n19 2 28\n13 2 27\n14 2 26\n6 2 25\n20 2 24\n17 2 23\n12 2 21\n9 2 18\n8 2 15\n5 2 14\n10 2 13\n11 2 12\n2 2 30\n19 2 29\n13 2 28\n14 2 27\n6 2 26\n20 2 25\n17 2 24\n12 2 22\n9 2 19\n8 2 16\n5 2 15\n10 2 14\n11 2 13\n2 2 31\n19 2 30\n13 2 29\n14 2 28\n6 2 27\n20 2 26\n17 2 25\n12 2 23\n9 2 20\n8 2 17\n5 2 16\n10 2 15\n11 2 14\n2 2 32\n19 2 31\n13 2 30\n14 2 29\n6 2 28\n20 2 27\n17 2 26\n12 2 24\n9 2 21\n8 2 18\n5 2 17\n10 2 16\n11 2 15\n2 2 33\n19 2 32\n13 2 31\n14 2 30\n6 2 29\n20 2 28\n17 2 27\n12 2 25\n9 2 22\n8 2 19\n5 2 18\n10 2 17\n11 2 16\n2 2 34\n19 2 33\n13 2 32\n14 2 31\n6 2 30\n20 2 29\n17 2 28\n12 2 26\n9 2 23\n8 2 20\n5 2 19\n10 2 18\n11 2 17\n2 2 35\n19 2 34\n13 2 33\n14 2 32\n6 2 31\n20 2 30\n17 2 29\n12 2 27\n9 2 24\n8 2 21\n5 2 20\n10 2 19\n11 2 18\n2 2 36\n19 2 35\n13 2 34\n14 2 33\n6 2 32\n20 2 31\n17 2 30\n12 2 28\n9 2 25\n8 2 22\n5 2 21\n10 2 20\n11 2 19\n2 2 37\n19 2 36\n13 2 35\n14 2 34\n6 2 33\n20 2 32\n17 2 31\n12 2 29\n9 2 26\n8 2 23\n5 2 22\n10 2 21\n11 2 20\n2 1 37\n19 2 37\n13 2 36\n14 2 35\n6 2 34\n20 2 33\n17 2 32\n12 2 30\n9 2 27\n8 2 24\n5 2 23\n10 2 22\n11 2 21\n19 2 38\n13 2 37\n14 2 36\n6 2 35\n20 2 34\n17 2 33\n12 2 31\n9 2 28\n8 2 25\n5 2 24\n10 2 23\n11 2 22\n14 1 36\n19 2 39\n13 2 38\n6 2 36\n20 2 35\n17 2 34\n12 2 32\n9 2 29\n8 2 26\n5 2 25\n10 2 24\n11 2 23\n11 1 23\n19 2 40\n13 2 39\n6 2 37\n20 2 36\n17 2 35\n12 2 33\n9 2 30\n8 2 27\n5 2 26\n10 2 25\n19 3 40\n13 2 40\n6 2 38\n20 2 37\n17 2 36\n12 2 34\n9 2 31\n8 2 28\n5 2 27\n10 2 26\n19 3 39\n13 3 40\n6 2 39\n20 2 38\n17 2 37\n12 2 35\n9 2 32\n8 2 29\n5 2 28\n10 2 27\n19 3 38\n13 3 39\n6 2 40\n20 2 39\n17 2 38\n12 2 36\n9 2 33\n8 2 30\n5 2 29\n10 2 28\n19 3 37\n13 3 38\n6 3 40\n20 2 40\n17 2 39\n12 2 37\n9 2 34\n8 2 31\n5 2 30\n10 2 29\n19 3 36\n13 3 37\n6 3 39\n20 3 40\n17 2 40\n12 2 38\n9 2 35\n8 2 32\n5 2 31\n10 2 30\n19 3 35\n13 3 36\n6 3 38\n20 3 39\n17 3 40\n12 2 39\n9 2 36\n8 2 33\n5 2 32\n10 2 31\n19 3 34\n13 3 35\n6 3 37\n20 3 38\n17 3 39\n12 2 40\n9 2 37\n8 2 34\n5 2 33\n10 2 32\n5 1 33\n19 3 33\n13 3 34\n6 3 36\n20 3 37\n17 3 38\n12 3 40\n9 2 38\n8 2 35\n10 2 33\n19 3 32\n13 3 33\n6 3 35\n20 3 36\n17 3 37\n12 3 39\n9 2 39\n8 2 36\n10 2 34\n20 4 36\n19 3 31\n13 3 32\n6 3 34\n17 3 36\n12 3 38\n9 2 40\n8 2 37\n10 2 35\n19 3 30\n13 3 31\n6 3 33\n17 3 35\n12 3 37\n9 3 40\n8 2 38\n10 2 36\n19 3 29\n13 3 30\n6 3 32\n17 3 34\n12 3 36\n9 3 39\n8 2 39\n10 2 37\n19 4 29\n13 3 29\n6 3 31\n17 3 33\n12 3 35\n9 3 38\n8 2 40\n10 2 38\n13 3 28\n6 3 30\n17 3 32\n12 3 34\n9 3 37\n8 3 40\n10 2 39\n12 4 34\n13 3 27\n6 3 29\n17 3 31\n9 3 36\n8 3 39\n10 2 40\n13 3 26\n6 3 28\n17 3 30\n9 3 35\n8 3 38\n10 3 40\n9 4 35\n13 3 25\n6 3 27\n17 3 29\n8 3 37\n10 3 39\n13 4 25\n6 3 26\n17 3 28\n8 3 36\n10 3 38\n6 3 25\n17 3 27\n8 3 35\n10 3 37\n6 3 24\n17 3 26\n8 3 34\n10 3 36\n6 3 23\n17 3 25\n8 3 33\n10 3 35\n6 3 22\n17 3 24\n8 3 32\n10 3 34\n6 3 21\n17 3 23\n8 3 31\n10 3 33\n6 3 20\n17 3 22\n8 3 30\n10 3 32\n6 3 19\n17 3 21\n8 3 29\n10 3 31\n6 3 18\n17 3 20\n8 3 28\n10 3 30\n6 3 17\n17 3 19\n8 3 27\n10 3 29\n6 3 16\n17 3 18\n8 3 26\n10 3 28\n6 3 15\n17 3 17\n8 3 25\n10 3 27\n6 3 14\n17 3 16\n8 3 24\n10 3 26\n10 4 26\n6 3 13\n17 3 15\n8 3 23\n6 4 13\n17 3 14\n8 3 22\n17 3 13\n8 3 21\n17 3 12\n8 3 20\n17 3 11\n8 3 19\n17 3 10\n8 3 18\n17 3 9\n8 3 17\n8 4 17\n17 3 8\n17 4 8\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, k = map(int, f.readline().split()) grid = [] for _ in range(4): grid.append(list(map(int, f.readline().split()))) initial = {} target = {} for row_idx in [1, 2]: for col_idx in range(n): x = grid[row_idx][col_idx] if x != 0: initial[x] = (row_idx + 1, col_idx + 1) for row_idx in [0, 3]: for col_idx in range(n): x = grid[row_idx][col_idx] if x != 0: target[x] = (row_idx + 1, col_idx + 1) with open(output_path) as f_ref: ref_line = f_ref.readline().strip() if ref_line == '-1': with open(submission_path) as f_sub: sub_line = f_sub.readline().strip() rest = f_sub.read().strip() print(1 if sub_line == '-1' and rest == '' else 0) return else: try: with open(submission_path) as f_sub: lines = f_sub.read().splitlines() if not lines: print(0) return m_line = lines[0].strip() if m_line == '-1': print(0) return m = int(m_line) if m < 0 or m > 20000: print(0) return moves = lines[1:1+m] if len(moves) != m: print(0) return current = [[0]*n for _ in range(4)] for x in initial: pr, pc = initial[x] current[pr-1][pc-1] = x for move in moves: parts = move.strip().split() if len(parts) != 3: print(0) return i, r, c = map(int, parts) if i < 1 or i > k: print(0) return found = False current_pr = current_pc = None for row in range(4): for col in range(n): if current[row][col] == i: current_pr, current_pc = row + 1, col + 1 found = True break if found: break if not found: print(0) return dr = abs(r - current_pr) dc = abs(c - current_pc) if dr + dc != 1: print(0) return dest_r, dest_c = r - 1, c - 1 if not (0 <= dest_r < 4 and 0 <= dest_c < n): print(0) return if current[dest_r][dest_c] != 0: print(0) return if r in (1, 4): t_r, t_c = target.get(i, (None, None)) if (r, c) != (t_r, t_c): print(0) return current[current_pr - 1][current_pc - 1] = 0 current[dest_r][dest_c] = i for x in target: t_r, t_c = target[x] if current[t_r - 1][t_c - 1] != x: print(0) return print(1) except: print(0) return if __name__ == '__main__': input_path, output_path, submission_path = sys.argv[1:4] main(input_path, output_path, submission_path)
true
456/A
456
A
Python 3
TESTS
39
124
0
225696213
prev = ["0","0"] for i in range(int(input())): t = input().split() if (t[0]<prev[0]) ^ (t[1]<prev[1]): print("Happy Alex") break prev = t else: print("Poor Alex")
46
124
0
214022873
a="Poor" for i in range(int(input())): b,c=input().split() if b!=c:a="Happy" print(a,"Alex")
Codeforces Round 260 (Div. 2)
CF
2,014
1
256
Laptops
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop. Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.
The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops. Next n lines contain two integers each, ai and bi (1 ≤ ai, bi ≤ n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality). All ai are distinct. All bi are distinct.
If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes).
null
null
[{"input": "2\n1 2\n2 1", "output": "Happy Alex"}]
1,100
["sortings"]
46
[{"input": "2\r\n1 2\r\n2 1\r\n", "output": "Happy Alex\r\n"}, {"input": "2\r\n1 1\r\n2 2\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n2 2\r\n3 3\r\n1 1\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n3 3\r\n1 2\r\n2 1\r\n", "output": "Happy Alex\r\n"}, {"input": "1\r\n1 1\r\n", "output": "Poor Alex\r\n"}, {"input": "3\r\n2 3\r\n1 1\r\n3 2\r\n", "output": "Happy Alex\r\n"}, {"input": "4\r\n4 1\r\n3 2\r\n2 3\r\n1 4\r\n", "output": "Happy Alex\r\n"}, {"input": "2\r\n2 1\r\n1 2\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n3 2\r\n1 1\r\n2 3\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 1\r\n3 3\r\n", "output": "Happy Alex\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "Happy Alex\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
62
0
147786053
v=[] n=int(input()) for i in range(n): tree,color=map(str,input().split()) leaf=tree+color v.append(leaf) k=set(v) print(len(k))
29
62
0
144971447
n = int(input()) data = [] for i in range(n): I = input().split() data.append((I[0], I[1])) results = [] for i in data: if i not in results: results.append(i) print(len(results))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
92
0
223827159
lst = [] for i in range(int(input())): x, y = input().split() z = x + y lst.append(z) print(len(set(lst)))
29
62
0
147785727
t=int(input()) k1={} for i in range(t): s=input() k1[s]=1 print(len(k1))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
62
0
147787621
n=int(input()) l=[] for i in range(n): s=input().split() s1="".join(s) l.append(s1) print(len(set(l)))
29
62
0
147786010
lst1=[] lst2=[] test=int(input()) for i in range(test): x=input() if x not in lst1: lst1.append(x) else: lst2.append(x) print(len(lst1))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
62
0
147785792
s=set() for i in range(int(input())): x,y=map(str,input().split()) s.add(x+y) print(len(s))
29
62
0
147786611
a=[] for i in range(int(input())): p=input() a.append(p) print(len(list(set(a))))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
690/C1
690
C1
PyPy 3-64
TESTS
11
93
1,228,800
140691425
import sys n, m = map(int, sys.stdin.readline().split()) if m == n - 1: graph = [[] for i in range(n + 1)] for _ in range(m): a = list(map(int, sys.stdin.readline().split())) graph[a[0]].append(a[1]) graph[a[1]].append(a[0]) found = {} unvisited = [-1 for i in range(n + 1)] def go(x): for _ in graph[x]: if unvisited[_] == -1: unvisited[_] = 1 go(_) go(1) unvisited[0] = 1 b = True for _ in unvisited: if unvisited[_] == -1: sys.stdout.write('no') b = False break if b: sys.stdout.write('yes') else: sys.stdout.write('no') # check m = n - 1, then check sammanhängande
18
62
614,400
18999611
l = [] were = [] def dfs(v): global l, were if not were: were = len(l) * [False] were[v] = True for i in l[v]: if not were[i]: dfs(i) n, m = map(int, input().split()) if m != n - 1: print("no") else: l = [[] for i in range(n)] for i in range(m): a, b = map(int, input().split()) l[a - 1].append(b - 1) l[b - 1].append(a - 1) dfs(0) f = True for i in were: f = f and i if f: print("yes") else: print("no")
Helvetic Coding Contest 2016 online mirror (teams, unrated)
ICPC
2,016
2
256
Brain Network (easy)
One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated – it consists of n brains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly: 1. It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains). 2. There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false. If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid.
The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 ≤ a, b ≤ n, a ≠ b).
The output consists of one line, containing either yes or no depending on whether the nervous system is valid.
null
null
[{"input": "4 4\n1 2\n2 3\n3 1\n4 1", "output": "no"}, {"input": "6 5\n1 2\n2 3\n3 4\n4 5\n3 6", "output": "yes"}]
1,300
[]
18
[{"input": "4 4\r\n1 2\r\n2 3\r\n3 1\r\n4 1\r\n", "output": "no\r\n"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n3 6\r\n", "output": "yes\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "yes\r\n"}, {"input": "3 3\r\n2 1\r\n1 3\r\n3 2\r\n", "output": "no\r\n"}, {"input": "3 2\r\n1 2\r\n2 3\r\n", "output": "yes\r\n"}, {"input": "9 8\r\n1 2\r\n2 3\r\n3 4\r\n4 1\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n", "output": "no\r\n"}, {"input": "8 7\r\n6 2\r\n1 5\r\n4 8\r\n4 7\r\n6 7\r\n8 3\r\n8 1\r\n", "output": "yes\r\n"}, {"input": "200 5\r\n93 101\r\n199 164\r\n14 94\r\n115 61\r\n106 156\r\n", "output": "no\r\n"}, {"input": "10 9\r\n6 5\r\n9 2\r\n4 7\r\n2 3\r\n7 3\r\n3 4\r\n10 6\r\n1 2\r\n5 8\r\n", "output": "no\r\n"}, {"input": "10 9\r\n2 3\r\n6 8\r\n10 1\r\n1 8\r\n6 7\r\n8 7\r\n10 5\r\n7 10\r\n2 5\r\n", "output": "no\r\n"}, {"input": "10 9\r\n3 2\r\n4 1\r\n6 1\r\n7 1\r\n9 2\r\n6 9\r\n5 2\r\n7 9\r\n3 7\r\n", "output": "no\r\n"}]
false
stdio
null
true
69/E
69
E
Python 3
TESTS
13
155
6,758,400
33465402
from sys import * from bisect import * from collections import * p = list(map(int, stdin.read().split())) n, k = p[0], p[1] d = Counter(p[2:2 + k]) t = sorted(q for q in d if d[q] < 2) g = lambda: print(t[-1] if t else 'Nothing') g() for a, b in zip(p[2:], p[2 + k:]): for q in [a, b]: if d[q] == 1: t.pop(bisect(t, q) - 1) d[b] += 1 d[a] -= 1 for q in [a, b]: if d[q] == 1: insort(t, q) g()
49
623
17,510,400
129463203
from sys import * from bisect import * from collections import * #using inbuit libraries p = list(map(int, stdin.read().split())) n, k = p[0], p[1] s, d = [], Counter(p[2:2 + k]) t = sorted(q for q in d if d[q] == 1) def sub(q): if d[q] == 1: t.pop(bisect(t, q) - 1) def add(q): if d[q] == 1: insort(t, q) def get(): s.append(str(t[-1]) if t else 'Nothing') get() for a, b in zip(p[2:], p[2 + k:]): if a != b: sub(a), sub(b) d[b] += 1 d[a] -= 1 add(a), add(b) get() print('\n'.join(s))
Codeforces Beta Round 63 (Div. 2)
CF
2,011
1
256
Subsegments
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in $${\mathcal {O}}(\log n)$$, which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
The first line contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n) — the number of array elements and the length of the segment. Then follow n lines: the i-th one contains a single number ai ( - 109 ≤ ai ≤ 109).
Print n–k + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 … ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
null
null
[{"input": "5 3\n1\n2\n2\n3\n3", "output": "1\n3\n2"}, {"input": "6 4\n3\n3\n3\n4\n4\n2", "output": "4\nNothing\n3"}]
1,800
["data structures", "implementation"]
49
[{"input": "5 3\r\n1\r\n2\r\n2\r\n3\r\n3\r\n", "output": "1\r\n3\r\n2\r\n"}, {"input": "6 4\r\n3\r\n3\r\n3\r\n4\r\n4\r\n2\r\n", "output": "4\r\nNothing\r\n3\r\n"}, {"input": "10 3\r\n-55\r\n-35\r\n-80\r\n91\r\n-96\r\n-93\r\n-39\r\n-77\r\n4\r\n29\r\n", "output": "-35\r\n91\r\n91\r\n91\r\n-39\r\n-39\r\n4\r\n29\r\n"}, {"input": "10 3\r\n-13\r\n26\r\n-97\r\n-38\r\n43\r\n-12\r\n80\r\n3\r\n8\r\n45\r\n", "output": "26\r\n26\r\n43\r\n43\r\n80\r\n80\r\n80\r\n45\r\n"}, {"input": "10 3\r\n-84\r\n25\r\n-25\r\n8\r\n60\r\n-74\r\n-98\r\n48\r\n-55\r\n38\r\n", "output": "25\r\n25\r\n60\r\n60\r\n60\r\n48\r\n48\r\n48\r\n"}, {"input": "10 3\r\n-62\r\n-81\r\n46\r\n22\r\n-84\r\n19\r\n-86\r\n44\r\n-84\r\n-73\r\n", "output": "46\r\n46\r\n46\r\n22\r\n19\r\n44\r\n44\r\n44\r\n"}, {"input": "10 3\r\n-6\r\n2\r\n79\r\n-49\r\n86\r\n13\r\n-31\r\n-71\r\n57\r\n93\r\n", "output": "79\r\n79\r\n86\r\n86\r\n86\r\n13\r\n57\r\n93\r\n"}, {"input": "10 3\r\n-38\r\n68\r\n-77\r\n57\r\n-35\r\n28\r\n-61\r\n-9\r\n3\r\n60\r\n", "output": "68\r\n68\r\n57\r\n57\r\n28\r\n28\r\n3\r\n60\r\n"}, {"input": "10 3\r\n2\r\n-100\r\n50\r\n-85\r\n-48\r\n68\r\n-96\r\n-31\r\n85\r\n-29\r\n", "output": "50\r\n50\r\n50\r\n68\r\n68\r\n68\r\n85\r\n85\r\n"}, {"input": "10 3\r\n-20\r\n-63\r\n-64\r\n45\r\n-84\r\n-13\r\n79\r\n-31\r\n70\r\n-100\r\n", "output": "-20\r\n45\r\n45\r\n45\r\n79\r\n79\r\n79\r\n70\r\n"}]
false
stdio
null
true
260/C
260
C
PyPy 3-64
TESTS
3
62
28,979,200
133295991
list1 = list(map(int, input().split())) n = list1[0] x = list1[1] boxes = list(map(int, input().split())) x = x - 1 balls = 0 #while (boxes[(x % n)] != 0): # boxes[x % n] -= 1 # balls += 1 # x -= 1 #boxes[x % n] = balls min1 = [boxes[0] , 0] for i in range(n) : if boxes[i] < min1[0] or (boxes[i] == min1[0] and (x - i < x - min1[1])) : min1[0] = boxes[i] min1[1] = i balls = min1[0] * n els = 0 for i in range(n): boxes[i] -= min1[0] t = min1[1] j = x -t if j < 0: j = j + n for y in range(j): balls += 1 boxes[(min1[1] + 1) % n] -= 1 min1[1] += 1 boxes[t] = balls for i in boxes: print(i, end=' ')
38
109
15,155,200
199574332
n,x = map(int, input().split()) a = list(map(int, input().split())) m = min(a) a = [i-m for i in a] x = x-1 d = m*n while a[x]>0: d+=1 a[x]-=1 x-=1 if x<0: x=n-1 a[x]=d print(*a)
Codeforces Round 158 (Div. 2)
CF
2,012
1
256
Balls and Boxes
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right. Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on. For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one. At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls. He asks you to help to find the initial arrangement of the balls in the boxes.
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
null
null
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
1,700
["constructive algorithms", "greedy", "implementation"]
38
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
false
stdio
null
true
260/C
260
C
Python 3
TESTS
10
31
0
172832133
n, x = map(int, input().split()) x -= 1 A = [int(t) for t in input().split()] i = A.index(min(A)) ans = [0] * len(A) if all([t == A[0] for t in A]): ans[x] = n*A[0] print(' '.join([str(t) for t in ans])) exit() if x >= i: for ind, a in enumerate(A): if ind == i: ans[ind] = x-i+n*A[i] elif i < ind <= x: ans[ind] = A[ind] - A[i] - 1 else: ans[ind] = A[ind] - A[i] else: for ind, a in enumerate(A): if ind == i: ans[ind] = n*A[i]+n-(i-x) elif x < ind < i: ans[ind] = A[ind] - A[i] else: ans[ind] = A[ind] - A[i] - 1 print(' '.join([str(t) for t in ans]))
38
171
14,028,800
28818655
from math import inf n, x = map(int, input().split()) a = list(map(int, input().split())) x -= 1 mx = inf mi = None for i in range(x, x - n, -1): if a[i] < mx: mx = a[i] mi = i mi %= n for i in range(n): a[i] -= mx a[mi] = mx * n + (x - mi) % n for i in range(mi + 1 - (n if x < mi else 0), x + 1): a[i] -= 1 print(' '.join(map(str, a)))
Codeforces Round 158 (Div. 2)
CF
2,012
1
256
Balls and Boxes
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right. Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on. For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one. At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls. He asks you to help to find the initial arrangement of the balls in the boxes.
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
null
null
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
1,700
["constructive algorithms", "greedy", "implementation"]
38
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
false
stdio
null
true
439/B
439
B
Python 3
TESTS
5
124
0
41611565
n=input().split() n[0]=int(n[0]) n[1]=int(n[1]) l=input().split() l.sort() p=0 for x in range(n[0]): if n[1]==1: p+=int(l[x]) else: p+=n[1]*int(l[x]) n[1]=n[1]-1 print(p)
31
93
11,366,400
168477571
n, m = map(int, input().split()) res = 0 l = sorted([int(i) for i in input().split()]) for i in range(n): if m == 1: res += l[i] else: res += l[i] * m m -= 1 print(res)
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu, the Dumb Guy
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously. Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours. Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour. You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy. Please be careful that answer might not fit in 32 bit data type.
The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105). The next line will contain n space separated integers: c1, c2, ..., cn (1 ≤ ci ≤ 105).
Output a single integer representing the answer to the problem.
null
Look at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours. Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 × 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours. So overall, minimum of both the cases is 11 hours. Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours.
[{"input": "2 3\n4 1", "output": "11"}, {"input": "4 2\n5 1 2 1", "output": "10"}, {"input": "3 3\n1 1 1", "output": "6"}]
1,200
["implementation", "sortings"]
31
[{"input": "2 3\r\n4 1\r\n", "output": "11\r\n"}, {"input": "4 2\r\n5 1 2 1\r\n", "output": "10\r\n"}, {"input": "3 3\r\n1 1 1\r\n", "output": "6\r\n"}, {"input": "20 4\r\n1 1 3 5 5 1 3 4 2 5 2 4 3 1 3 3 3 3 4 3\r\n", "output": "65\r\n"}, {"input": "20 10\r\n6 6 1 2 6 4 5 3 6 5 4 5 6 5 4 6 6 2 3 3\r\n", "output": "196\r\n"}, {"input": "1 1\r\n9273\r\n", "output": "9273\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "2\r\n"}, {"input": "1 2\r\n2\r\n", "output": "4\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
690/B1
690
B1
Python 3
TESTS
7
93
7,065,600
37352938
n = int(input()) grid = [] flag = True ans = -1 while(n): n-=1 grid.append(str(int(input()))) for i in grid: if(i!='0'): if(ans==-1 or len(i)==ans): ans = len(i) else: flag = False if(flag): print('Yes') else: print('No')
21
140
21,401,600
84934143
n = int(input()) A = [input() for i in range(n)] def early_exit(): print("No") exit() if n < 3: early_exit() # first find the corner corner_row = [] for i in range(n): if '1' in A[i]: corner_row.append(i) if len(corner_row) != 2: early_exit() # now for each of the corner row, find the corresponding column # check everything before the first index def check_zero(x): for i in x: if i != '0': return False return True for i in range(corner_row[0]): if not check_zero(A[i]): early_exit() for i in range(corner_row[1]+1, n): if not check_zero(A[i]): early_exit() #find first non-zero for the two corner row for j in range(n): if A[corner_row[0]][j] != '0': left_corner = j break for j in range(n-1,-1,-1): if A[corner_row[0]][j] != '0': right_corner = j break if A[corner_row[0]][left_corner] != '1' or A[corner_row[0]][right_corner] != '1' or left_corner == right_corner: early_exit() for j in range(n): if A[corner_row[1]][j] != '0': left2_corner = j break for j in range(n-1,-1,-1): if A[corner_row[1]][j] != '0': right2_corner = j break if A[corner_row[1]][left2_corner] != '1' or A[corner_row[1]][right2_corner] != '1' or left_corner != left2_corner or right_corner != right2_corner: early_exit() # by now we have find the row interval to check # check the first and last row for i in corner_row: for j in range(left_corner+1, right_corner): if A[i][j] != '2': early_exit() # check zero blocks in between for i in range(corner_row[0]+1, corner_row[1]): if not check_zero(A[i][:left_corner]): early_exit() if not check_zero(A[i][right_corner+1:]): early_exit() if A[i][left_corner] != '2' or A[i][right_corner] != '2': early_exit() for j in range(left_corner+1, right_corner): if A[i][j] != '4': early_exit() print("Yes")
Helvetic Coding Contest 2016 online mirror (teams, unrated)
ICPC
2,016
2
256
Recover Polygon (easy)
The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good. Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle. As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides).
The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4. Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates (1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1).
The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.
null
The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the input.
[{"input": "6\n000000\n000000\n012100\n024200\n012100\n000000", "output": "Yes"}]
1,700
[]
21
[{"input": "6\r\n000000\r\n000000\r\n012100\r\n024200\r\n012100\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n012210\r\n024420\r\n012210\r\n000000\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000100\r\n001210\r\n002420\r\n001210\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "10\r\n0000000000\r\n0122210000\r\n0244420100\r\n0122210000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n", "output": "No\r\n"}, {"input": "10\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0012100000\r\n0024200000\r\n0012100000\r\n0000000000\r\n", "output": "Yes\r\n"}, {"input": "9\r\n000000000\r\n000000000\r\n012221000\r\n024442000\r\n012221000\r\n000000000\r\n000000000\r\n000000010\r\n000000000\r\n", "output": "No\r\n"}, {"input": "9\r\n000000000\r\n012222100\r\n024444200\r\n024444200\r\n024444200\r\n024444200\r\n024444200\r\n012222100\r\n000000000\r\n", "output": "Yes\r\n"}, {"input": "8\r\n00000000\r\n00001210\r\n00002420\r\n00002020\r\n00001210\r\n00000000\r\n00000000\r\n00000000\r\n", "output": "No\r\n"}, {"input": "8\r\n00000000\r\n00000000\r\n01210000\r\n02420000\r\n01210000\r\n00000000\r\n00000000\r\n00000000\r\n", "output": "Yes\r\n"}, {"input": "7\r\n0000000\r\n0000000\r\n0000000\r\n1122210\r\n0244420\r\n0122210\r\n0000000\r\n", "output": "No\r\n"}, {"input": "7\r\n0000000\r\n0012210\r\n0024420\r\n0012210\r\n0000000\r\n0000000\r\n0000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n000000\r\n001100\r\n001200\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n000000\r\n002200\r\n002200\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n000000\r\n003300\r\n003300\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n001100\r\n013310\r\n013310\r\n001100\r\n000000\r\n", "output": "No\r\n"}]
false
stdio
null
true
490/B
490
B
PyPy 3-64
TESTS
2
78
0
177470631
import sys input = sys.stdin.readline from collections import defaultdict n = int(input()) d = defaultdict(list) for i in range(n): a, b = map(int, input().split()) if a == 0: x = b elif b == 0: y = a else: d[a].append((0, b)) d[b].append((1, a)) l = [0, x] r = [0, y] while 1: for i, j in d[x]: if i == 0: l.append(j) x = j break else: break while 1: for i, j in d[y]: if i == 1: r.append(j) y = j break else: break r = r[1:][::-1] l = l[1:] for i in range(n//2): print(r[i], end=' ') print(l[i], end=' ')
61
795
44,851,200
216194526
n = int(input()) A, B = set(), set() d = {} for i in range(n): a, b = map(int, input().split()) d[a] = b A.add(a) B.add(b) sol = [0] * n sol[0] = (A - B).pop() sol[1] = d[0] for i in range(2, n): sol[i] = d[sol[i - 2]] print(*sol)
Codeforces Round 279 (Div. 2)
CF
2,014
2
256
Queue
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1). After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task. Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
null
The picture illustrates the queue for the first sample.
[{"input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141"}]
1,500
["dsu", "implementation"]
61
[{"input": "4\r\n92 31\r\n0 7\r\n31 0\r\n7 141\r\n", "output": "92 7 31 141 \r\n"}, {"input": "2\r\n0 1\r\n2 0\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 2\r\n1 3\r\n2 0\r\n", "output": "1 2 3 \r\n"}, {"input": "4\r\n101 0\r\n0 102\r\n102 100\r\n103 101\r\n", "output": "103 102 101 100 \r\n"}, {"input": "5\r\n0 1\r\n1 4\r\n4 0\r\n3 2\r\n5 3\r\n", "output": "5 1 3 4 2 \r\n"}, {"input": "6\r\n10001 0\r\n0 10005\r\n10003 10001\r\n10002 10000\r\n10005 10002\r\n10004 10003\r\n", "output": "10004 10005 10003 10002 10001 10000 \r\n"}, {"input": "3\r\n0 743259\r\n72866 70294\r\n743259 0\r\n", "output": "72866 743259 70294 \r\n"}, {"input": "4\r\n263750 0\r\n513707 263750\r\n0 718595\r\n718595 148112\r\n", "output": "513707 718595 263750 148112 \r\n"}, {"input": "5\r\n645873 145459\r\n638930 82975\r\n0 645873\r\n82975 389665\r\n145459 0\r\n", "output": "638930 645873 82975 145459 389665 \r\n"}, {"input": "6\r\n341637 51795\r\n0 809471\r\n51795 0\r\n244669 341637\r\n852537 508622\r\n809471 852537\r\n", "output": "244669 809471 341637 852537 51795 508622 \r\n"}, {"input": "7\r\n111283 0\r\n496010 510417\r\n423431 921854\r\n510417 111283\r\n0 496010\r\n758535 423431\r\n921854 59208\r\n", "output": "758535 496010 423431 510417 921854 111283 59208 \r\n"}, {"input": "8\r\n611412 115521\r\n114290 712424\r\n115521 242491\r\n242491 0\r\n0 114290\r\n712424 282922\r\n282922 589147\r\n359823 611412\r\n", "output": "359823 114290 611412 712424 115521 282922 242491 589147 \r\n"}, {"input": "9\r\n308992 348750\r\n0 6496\r\n487447 676506\r\n874677 985199\r\n260782 487447\r\n985199 260782\r\n348750 0\r\n570981 308992\r\n6496 570981\r\n", "output": "874677 6496 985199 570981 260782 308992 487447 348750 676506 \r\n"}, {"input": "10\r\n419946 201769\r\n245945 0\r\n842799 113073\r\n836998 245945\r\n0 794376\r\n692107 836998\r\n113073 904403\r\n904403 987165\r\n201769 692107\r\n794376 842799\r\n", "output": "419946 794376 201769 842799 692107 113073 836998 904403 245945 987165 \r\n"}, {"input": "10\r\n189071 852255\r\n227133 652124\r\n329720 4848\r\n652124 329720\r\n0 72517\r\n943168 0\r\n72517 544697\r\n4848 943168\r\n538963 189071\r\n544697 538963\r\n", "output": "227133 72517 652124 544697 329720 538963 4848 189071 943168 852255 \r\n"}, {"input": "2\r\n0 300000\r\n1000000 0\r\n", "output": "1000000 300000 \r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
62
0
148184816
import sys t=int(input()) l=[] for i in range(t): n=input() x=n.split() p=x[0]+x[1] l.append(p) l=set(l) print(len(l))
29
62
0
147786794
t = int(input()) l = [] for i in range(t): a = input() l.append(a) print(len(set(l)))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
56/B
56
B
PyPy 3
TESTS
7
184
0
106491210
n = int(input()) arr = list(map(int, input().split())) l, r = -1, -1 i = 0 while(i+1<n and arr[i]+1==arr[i+1]): i+=1 l = i i+=1 while(i+1<n and arr[i]-1==arr[i+1]): i+=1 r = i i+=1 while(i+1<n and arr[i]+1==arr[i+1]): i+=1 if(r != -1 and i>= n-1): print(l+2, r+1) else: print(0, 0)
33
92
0
144285648
n = int(input()) s = input().split() s = [int(i) for i in s] sorted_s = sorted(s) i = 0 flag = 0 while(i<n): if(s[i] != sorted_s[i]): flag = 1 sub1 = s[:i] sub2 = s[i:s[i]][::-1] sub3 = s[s[i]:] tot = sub1 + sub2 + sub3 #print(tot) if(tot == sorted_s): print(i+1,s[i]) break else: print(0,0) break i += 1 if(flag == 0): print(0,0)
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
false
stdio
null
true
44/A
44
A
Python 3
TESTS
28
92
0
172926703
t = int(input()) lst = [] for i in range(t): a, b = input().split() lst.append(a + b) print(len(set(lst)))
29
62
0
147786951
x = int(input()) lst = [] for i in range(x): s = input() lst.append(s) lst = list(set(lst)) print(len(lst))
School Team Contest 2 (Winter Computer School 2010/11)
ICPC
2,010
2
256
Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.
Output the single number — the number of Alyona's leaves.
null
null
[{"input": "5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "output": "4"}, {"input": "3\noak yellow\noak yellow\noak yellow", "output": "1"}]
900
["implementation"]
29
[{"input": "5\r\nbirch yellow\r\nmaple red\r\nbirch yellow\r\nmaple yellow\r\nmaple green\r\n", "output": "4\r\n"}, {"input": "3\r\noak yellow\r\noak yellow\r\noak yellow\r\n", "output": "1\r\n"}, {"input": "5\r\nxbnbkzn hp\r\nkaqkl vrgzbvqstu\r\nj aqidx\r\nhos gyul\r\nwefxmh tygpluae\r\n", "output": "5\r\n"}, {"input": "1\r\nqvwli hz\r\n", "output": "1\r\n"}, {"input": "4\r\nsrhk x\r\nsrhk x\r\nqfoe vnrjuab\r\nqfoe vnrjuab\r\n", "output": "2\r\n"}, {"input": "4\r\nsddqllmmpk syded\r\nfprsq fnenjnaz\r\nn hdej\r\nsddqllmmpk syded\r\n", "output": "3\r\n"}, {"input": "17\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\njtbctslqq tosqzw\r\n", "output": "1\r\n"}, {"input": "18\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nrfdb esp\r\nb d\r\nb d\r\nrfdb esp\r\nrfdb esp\r\n", "output": "2\r\n"}, {"input": "13\r\nsvpzwtwn rykzfdce\r\nqweiyeck jkreouy\r\nhk nnli\r\ntwxrnbbdt vtuv\r\nxokqjtylly sz\r\nesdt dbfidjslq\r\ng ybqgomvw\r\nxcpfjmf zcqvz\r\nifajadhj invzueip\r\nvdemdnxifb hckwebmi\r\nsdpnhipam wvowzavh\r\nuqdlfskhgo vunbpghae\r\ne dtigwnb\r\n", "output": "13\r\n"}, {"input": "20\r\nm vkfh\r\nvptikamead mvx\r\nitu mf\r\nklruxckw aqega\r\nekqkd enjllwol\r\ncc uybfdh\r\nimrfdngvo u\r\ne uh\r\ntwt jsslcfuogk\r\nbljwqsag tuqbdn\r\nqcv q\r\nasx gzhvwwmajj\r\nqcv q\r\nekqkd enjllwol\r\nasx gzhvwwmajj\r\nks vv\r\nkzyfi cn\r\ncc uybfdh\r\nitu mf\r\ncjbjhtbyvk vatwfmux\r\n", "output": "15\r\n"}, {"input": "2\r\nab ab\r\na bab\r\n", "output": "2\r\n"}]
false
stdio
null
true
56/B
56
B
Python 3
TESTS
14
92
0
209778092
import sys n = int(input()) a = list(map(int, input().split())) check = [i+1 for i in range(n)] if a == check: print(0, 0) sys.exit(0) l,r = 0, 0 for i in range(1, n): if a[i] > a[i-1]: l = i break for i in range(n-1,-1,-1): if a[i] < a[i-1]: r = i break c1,c2 = l,r while l <= r: a[l],a[r] = a[r], a[l] l += 1 r -= 1 if a == check: print(c1+1, c2+1) else: print(0, 0)
33
92
0
145116479
n = int(input()) data = input().split() data2 = [] for i in range(n): if str(i+1) != data[i]: data2.append((data[i], i)) data2.reverse() if data2: o = list(range(1, n+1)) can = True for i in range(len(data2)): if o.index(int(data2[i][0])) != int(data2[-i-1][1]): can = False break if can: print(str(data2[-1][1] + 1) + " " + str(data2[0][1] + 1)) else: print("0 0") else: print("0 0")
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
false
stdio
null
true
154/A
154
A
PyPy 3
TESTS
4
216
0
114251403
def f(a,bad): i=0 cnt=0 while a and i+1<len(a): if bad.get(a[i],None)==a[i+1]: cnt+=1 a.pop(i+1) else: i+=1 return cnt a=list(input()) n=int(input()) bad={} for i in range(n): l=list(input()) bad[l[0]]=l[1] bad[l[1]]=l[0] print(f(a,bad))
42
218
2,457,600
116908726
import sys input=sys.stdin.readline s=input().rstrip() k=int(input()) cannot=[input().rstrip() for i in range(k)] ans=0 for t in cannot: a,b=0,0 for i in range(len(s)): if s[i]==t[0]: a+=1 elif s[i]==t[1]: b+=1 else: ans+=min(a,b) a=b=0 ans+=min(a,b) print(ans)
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input": "eepeeeeppppppeepeppe\r\n1\r\npe\r\n", "output": "10\r\n"}, {"input": "vefneyamdzoytemupniw\r\n13\r\nve\r\nfg\r\noi\r\nan\r\nck\r\nwx\r\npq\r\nml\r\nbt\r\nud\r\nrz\r\nsj\r\nhy\r\n", "output": "1\r\n"}, {"input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\r\n13\r\nld\r\nac\r\nnp\r\nrv\r\nmo\r\njh\r\ngb\r\nuw\r\nfq\r\nst\r\nkx\r\nzy\r\nei\r\n", "output": "11\r\n"}, {"input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\r\n4\r\nli\r\nqh\r\nad\r\nbp\r\n", "output": "4\r\n"}, {"input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\r\n5\r\nmb\r\nho\r\nxu\r\njv\r\nyp\r\n", "output": "37\r\n"}, {"input": "z\r\n0\r\n", "output": "0\r\n"}, {"input": "t\r\n13\r\nzx\r\nig\r\nyq\r\nbd\r\nph\r\nar\r\nne\r\nwo\r\ntk\r\njl\r\ncv\r\nfs\r\nmu\r\n", "output": "0\r\n"}, {"input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\r\n13\r\njr\r\nnf\r\nyk\r\ntq\r\nwe\r\nil\r\ngu\r\npb\r\naz\r\nxm\r\nhc\r\nvd\r\nso\r\n", "output": "0\r\n"}, {"input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\r\n1\r\nni\r\n", "output": "28\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n1\r\nab\r\n", "output": "75\r\n"}]
false
stdio
null
true
696/A
696
A
PyPy 3-64
TESTS
2
77
3,891,200
178250499
q=int(input()) eta={} for j in range(q): c=[int(k) for k in input().split()] if c[0]==1: a, b=bin(c[1])[2:], bin(c[2])[2:] #print(a, b) zeta=len(a) for k in range(min(len(a), len(b))): if a[k]==b[k]: zeta-=1 for k in range(zeta): if (c[1]//2, c[1]) in eta: eta[(c[1]//2, c[1])]+=c[3] else: eta[(c[1]//2, c[1])]=c[3] c[1]//=2 rho=int(b[:len(a)-zeta], 2) #print(rho) for k in range(len(a)-zeta, len(b)): if b[k]=="1": if (rho, 2*rho+1) in eta: eta[(rho, 2*rho+1)]+=c[3] else: eta[(rho, 2*rho+1)]=c[3] rho=2*rho+1 else: if (rho, 2*rho) in eta: eta[(rho, 2*rho)]+=c[3] else: eta[(rho, 2*rho)]=c[3] rho=2*rho elif c[0]==2: res=0 a, b=bin(c[1])[2:], bin(c[2])[2:] zeta=len(a) for k in range(min(len(a), len(b))): if a[k]==b[k]: zeta-=1 for k in range(zeta): if (c[1]//2, c[1]) in eta: res+=eta[(c[1]//2, c[1])] c[1]//=2 rho=int(b[:len(a)-zeta], 2) for k in range(len(a)-zeta, len(b)): if b[k]=="1": if (rho, 2*rho+1) in eta: res+=eta[(rho, 2*rho+1)] rho=2*rho+1 else: if (rho, 2*rho) in eta: res+=eta[(rho, 2*rho)] rho*=2 print(res)
49
140
11,366,400
198925231
d={} def pro(x,y,w): res=0 while x!=y: if x<y: x,y=y,x d[x]=d.get(x,0)+w res+=d[x]; x//=2 return res q=int(input()) while q>0: q-=1 s=list(map(int,input().split())) if s[0]==1: pro(s[1],s[2],s[3]) else: print(pro(s[1],s[2],0))
Codeforces Round 362 (Div. 1)
CF
2,016
1
256
Lorenzo Von Matterhorn
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line.
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
null
In the example testcase: Here are the intersections used: 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
[{"input": "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4", "output": "94\n0\n32"}]
1,500
["brute force", "data structures", "implementation", "trees"]
49
[{"input": "7\r\n1 3 4 30\r\n1 4 1 2\r\n1 3 6 8\r\n2 4 3\r\n1 6 1 40\r\n2 3 7\r\n2 2 4\r\n", "output": "94\r\n0\r\n32\r\n"}, {"input": "1\r\n2 666077344481199252 881371880336470888\r\n", "output": "0\r\n"}, {"input": "10\r\n1 1 63669439577744021 396980128\r\n1 2582240553355225 63669439577744021 997926286\r\n1 2582240553355225 1 619026011\r\n1 1 4 231881718\r\n2 63669439577744021 3886074192977\r\n2 4 63669439577744021\r\n2 124354374175272 10328962213420903\r\n1 10328962213420903 3886074192977 188186816\r\n1 124354374175272 31088593543820 705639304\r\n2 2582240553355225 254677758310976084\r\n", "output": "19528689796\r\n80417520800\r\n140119493557\r\n179078288337\r\n"}, {"input": "10\r\n1 1 399719082491 159376944\r\n1 186 1 699740230\r\n2 410731850987390 1\r\n1 410731850987390 399719082491 699271234\r\n1 1 186 255736462\r\n1 1 186 544477714\r\n1 399719082491 410731850987390 366708275\r\n2 1 186\r\n2 410731850987390 1\r\n2 399719082491 186\r\n", "output": "6013820218\r\n11615319450\r\n55320479319\r\n37986050043\r\n"}, {"input": "10\r\n2 37526406560905229 37526426361107171\r\n2 37526424114740747 18763396439955441\r\n2 300485276957081578 301492476099962199\r\n1 75035386466351570 441803674395985082 642312512\r\n2 300197522144700185 220954108245114486\r\n1 150105696341181576 559187296 100113944\r\n1 300197522135707767 150242638470761995 170574370\r\n2 150105691058036871 220954108245108400\r\n2 37560659619635168 150070774425697078\r\n2 18780329809814344 300222324900057526\r\n", "output": "0\r\n0\r\n0\r\n13488562752\r\n14270974176\r\n13899046930\r\n5418394872\r\n"}, {"input": "1\r\n2 1 343417335313797025\r\n", "output": "0\r\n"}, {"input": "2\r\n1 562949953421312 562949953421311 1\r\n2 562949953421312 562949953421311\r\n", "output": "97\r\n"}, {"input": "2\r\n1 100 50 1\r\n2 4294967396 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 4294967298 4294967299 10\r\n2 2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n1 500000000000 250000000000 1\r\n2 1783793664 891896832\r\n", "output": "0\r\n"}, {"input": "2\r\n1 100000000000000 200000000000000 1\r\n2 276447232 552894464\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2147540141 4295080282 1\r\n2 1 112986\r\n", "output": "0\r\n"}, {"input": "2\r\n1 239841676148963 1 20\r\n2 2112405731 1\r\n", "output": "20\r\n"}]
false
stdio
null
true
70/B
70
B
PyPy 3
TESTS
10
93
20,172,800
130632694
n = int(input()) s = input() a = [""] for c in s: if a[-1] == "" and c == ' ': continue a[-1] += c; if c == '?' or c == '!' or c == '.': a.append(""); ans = 0 last = 0 for i in range(len(a)-1): if len(a[i]) > n: print('Impossible') exit(0) elif i == 0 or (last + len(a[i]) + 1) >= n: ans += 1 last = len(a[i]) else: last += len(a[i]) + 1 print(ans)
48
171
1,331,200
78864704
import sys n = int(input()) s = str(input()) m = len(s) cnt = 0 gd = False ans = 0 lst = 0 end = ['.', '?', '!'] rem = 0 for i in range(m): cnt += 1 if(s[i] in end): gd = True lst = cnt # store the last good one if(cnt == n): if(not gd): print("Impossible") exit(0) else: cnt = cnt - lst - 1 gd = False ans += 1 rem = i if(s[m - 1] not in end): print("Impossible") exit(0) elif(rem != (m - 1)): ans += 1 print(ans)
Codeforces Beta Round 64
CF
2,011
1
256
Text Messaging
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split! Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space). The little walrus's text looks in the following manner: SPACE stands for the symbol of a space. So, how many messages did Fangy send?
The first line contains an integer n, which is the size of one message (2 ≤ n ≤ 255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.
On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.
null
Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
[{"input": "25\nHello. I am a little walrus.", "output": "2"}, {"input": "2\nHow are you?", "output": "Impossible"}, {"input": "19\nHello! Do you like fish? Why?", "output": "3"}]
1,600
["expression parsing", "greedy", "strings"]
48
[{"input": "25\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "2\r\nHow are you?\r\n", "output": "Impossible\r\n"}, {"input": "19\r\nHello! Do you like fish? Why?\r\n", "output": "3\r\n"}, {"input": "4\r\na. A.\r\n", "output": "2\r\n"}, {"input": "146\r\niIQVkDsPqzAJyBrtHk EhBSN gzDoigItCMzETerb cIbXhTPbKYMdMoYqyFTEN. qcrrseVwmaZEiQUQoGT SUyErST lJDejKkjqTdoUrHR tsZYDyI? pmuNNHYqQUISxdZfWOB XdEECvNz hnNmVfODaIC qjhRIEPAlEsQBxo apZ! gCpqoiUFIwWLBOmYubywj qJojFVhLd dCpovICpXHfgihydEOoij?\r\n", "output": "2\r\n"}, {"input": "118\r\ngweVo bjbEKaZQw PpSi AWOYt sQSvAHNRswh vUaGuLbtExNECz! USsQxMCjaGOmUESwHvyY SshkERibaWkmNLSZOtWZy FFTUWQgekPRjLRetAdSFt! sIhcimZTisFvndrYioLF HetLn wjoaDUKfbkagupl QdYb fFiV GNqBygStKQw. XLiYZEOGnTLSHmCwktEr pVBePMoRGopNt LdEujxuxzmlbycQdR?\r\n", "output": "4\r\n"}, {"input": "16\r\nAbacaba. Abacaba. abacaba. abacab.\r\n", "output": "3\r\n"}, {"input": "21\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "16\r\nAbacaba. Abacab. abacaba. abacaba.\r\n", "output": "3\r\n"}, {"input": "10\r\nhello! how are you?\r\n", "output": "Impossible\r\n"}, {"input": "5\r\nabc. abcd.\r\n", "output": "2\r\n"}, {"input": "16\r\nabacaba. abacab. Abacaba. Abacaba.\r\n", "output": "3\r\n"}, {"input": "5\r\na. b. c. d.\r\n", "output": "2\r\n"}, {"input": "8\r\nabc! ab.\r\n", "output": "1\r\n"}, {"input": "2\r\na. b.\r\n", "output": "2\r\n"}]
false
stdio
null
true
455/B
455
B
PyPy 3-64
TESTS
2
61
0
206137785
import sys from collections import defaultdict input = sys.stdin.readline n, k = map(int, input().split()) class BorNode: def __init__(self, prefix=''): self.prefix = prefix self.children = defaultdict(int) self.is_terminal = False self.is_win = None self.is_lose = None def check_win(self): if self.is_win != None: return self.is_win if len(self.children) == 0: result = False else: result = False for child in self.children: if not self.children[child].check_win(): result = True self.is_win = result return result def check_lose(self): if self.is_win != None: return self.is_win if len(self.children) == 0: result = True else: result = False for child in self.children: if not self.children[child].check_win(): result = True self.is_win = result return result class Bor: def __init__(self): self.root = BorNode() def append(self, word): current = self.root for i in range(len(word)): letter = word[i] if letter not in current.children: prefix = word[0: i + 1] current.children[letter] = BorNode(prefix) current = current.children[letter] current.is_terminal = True ''' def find(self, word): current = self.root for char in word: if char not in current.children: return None current = current.children[char] if current.is_word: return current def __child_words_for(self, node, words): if node.is_word: words.append(node.text) for letter in node.children: self.__child_words_for(node.children[letter], words) def starts_with(self, prefix): words = list() current = self.root for char in prefix: if char not in current.children: return list() current = current.children[char] self.__child_words_for(current, words) return words ''' bor = Bor() for i in range(n): w = input() bor.append(w) win = bor.root.check_win() lose = bor.root.check_lose() if not w: print("Second") elif lose: print("First") else: if k % 2 == 1: print("First") else: print("Second")
75
920
6,451,200
7386129
""" Codeforces Contest 260 Div 1 Problem B Author : chaotic_iak Language: Python 3.3.4 """ def main(): n,k = read() s = set() for i in range(n): s.add(read(0)) s = list(s) s.sort() s = treeify(s) res = solve(s) if res == 0: # neither: second player win print("Second") if res == 1: # odd: first player win if k is odd print("First" if k % 2 else "Second") if res == 2: # even: second player win print("Second") if res == 3: # both: first player win print("First") def treeify(s): res = [[] for _ in range(26)] for i in s: if i: res[ord(i[0]) - 97].append(i[1:]) fin = [] for i in range(26): if res[i]: fin.append(treeify(res[i])) return fin def solve(s, parity=2): for i in range(len(s)): if isinstance(s[i], list): s[i] = solve(s[i], 3-parity) if not s: return parity # no possible move: current parity if 0 in s: return 3 # any neither: both if 1 in s and 2 in s: return 3 # any odd and any even: both if 1 in s: return 1 # any odd: odd if 2 in s: return 2 # any even: even return 0 # all both: neither ################################### NON-SOLUTION STUFF BELOW def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return map(int, inputs.split()) def write(s="\n"): if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") main()
Codeforces Round 260 (Div. 1)
CF
2,014
1
256
A Lot of Games
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move. Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109). Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
null
null
[{"input": "2 3\na\nb", "output": "First"}, {"input": "3 1\na\nb\nc", "output": "First"}, {"input": "1 2\nab", "output": "Second"}]
1,900
["dfs and similar", "dp", "games", "implementation", "strings", "trees"]
75
[{"input": "2 3\r\na\r\nb\r\n", "output": "First\r\n"}, {"input": "3 1\r\na\r\nb\r\nc\r\n", "output": "First\r\n"}, {"input": "1 2\r\nab\r\n", "output": "Second\r\n"}, {"input": "5 6\r\nabas\r\ndsfdf\r\nabacaba\r\ndartsidius\r\nkolobok\r\n", "output": "Second\r\n"}, {"input": "4 2\r\naaaa\r\nbbbb\r\nccccc\r\ndumbavumba\r\n", "output": "First\r\n"}, {"input": "3 8\r\nso\r\nbad\r\ntest\r\n", "output": "First\r\n"}, {"input": "5 2\r\nwelcome\r\nto\r\nthe\r\nmatrix\r\nneo\r\n", "output": "First\r\n"}, {"input": "6 4\r\ndog\r\ncat\r\ncow\r\nhot\r\nice\r\nlol\r\n", "output": "Second\r\n"}, {"input": "4 8\r\nla\r\na\r\nz\r\nka\r\n", "output": "First\r\n"}, {"input": "3 2\r\nop\r\nhop\r\ncop\r\n", "output": "First\r\n"}, {"input": "3 3\r\nabasdfabab\r\nabaaasdfdsf\r\nasdfaba\r\n", "output": "Second\r\n"}, {"input": "2 2\r\naba\r\naa\r\n", "output": "Second\r\n"}, {"input": "4 1\r\naa\r\naba\r\nba\r\nbba\r\n", "output": "Second\r\n"}, {"input": "1 3\r\nab\r\n", "output": "Second\r\n"}, {"input": "3 3\r\naa\r\nabb\r\ncc\r\n", "output": "Second\r\n"}]
false
stdio
null
true
56/B
56
B
Python 3
TESTS
14
154
0
117491308
import math n=int(input()) lst = list(map(int, input().strip().split(' '))) l=-1 r=-1 k=0 if n%2==0: k=n//2 else: k=n//2 + 1 for i in range(k): if l==-1 and lst[i]!=i+1: l=i if r==-1 and lst[n-i-1]!=n-i: r=n-i-1 if l!=-1 and r!=-1: break if l==-1 and r==-1: print('0 0') else: f=0 p=r+1 for j in range(l,r+1): if lst[j]==p: p-=1 else: f=1 break if f==1: print('0 0') else: print(l+1,r+1,end=" ")
33
92
0
153768422
N=int(input()) A=list(map(int,input().split())) mn,mx=N+1,-1 for i in range(N): if(i+1!=A[i]): mn=min(mn,i) mx=max(mx,i) if(mx==-1): print('0 0') else: #print('/yiw') A=A[:mn]+A[mn:(mx+1)][::-1]+A[(mx+1):] #print('/kk') #sorted(A)==A #print('>') if(sorted(A)==A): print(mn+1,mx+1) else: print('0 0')
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
false
stdio
null
true
439/B
439
B
Python 3
TESTS
6
62
6,758,400
122937082
n, t = map(int, input().split()) a = sorted(map(int, input().split())) count = 0 for i in range(0, n): if t > 1: count += t*a[i] t -= 1 else: count += a[i] print(count%1000001)
31
93
11,366,400
224691998
n,x = map(int,input().split()) l = [int(x) for x in input().split()] l.sort() # pre = [0]*n # pre[0]=l[0] # for i in range(1,n): # pre[i]=pre[i-1]+l[i] t = 0 for j in range(n): t+=(l[j]*x) if x>1: x-=1 print(t)
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu, the Dumb Guy
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously. Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours. Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour. You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy. Please be careful that answer might not fit in 32 bit data type.
The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105). The next line will contain n space separated integers: c1, c2, ..., cn (1 ≤ ci ≤ 105).
Output a single integer representing the answer to the problem.
null
Look at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours. Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 × 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours. So overall, minimum of both the cases is 11 hours. Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours.
[{"input": "2 3\n4 1", "output": "11"}, {"input": "4 2\n5 1 2 1", "output": "10"}, {"input": "3 3\n1 1 1", "output": "6"}]
1,200
["implementation", "sortings"]
31
[{"input": "2 3\r\n4 1\r\n", "output": "11\r\n"}, {"input": "4 2\r\n5 1 2 1\r\n", "output": "10\r\n"}, {"input": "3 3\r\n1 1 1\r\n", "output": "6\r\n"}, {"input": "20 4\r\n1 1 3 5 5 1 3 4 2 5 2 4 3 1 3 3 3 3 4 3\r\n", "output": "65\r\n"}, {"input": "20 10\r\n6 6 1 2 6 4 5 3 6 5 4 5 6 5 4 6 6 2 3 3\r\n", "output": "196\r\n"}, {"input": "1 1\r\n9273\r\n", "output": "9273\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "2\r\n"}, {"input": "1 2\r\n2\r\n", "output": "4\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
154/A
154
A
Python 3
TESTS
4
124
0
9284800
import sys import math s = sys.stdin.readline() k = int(sys.stdin.readline()) v = {} for i in range(k): st = sys.stdin.readline() v[st[0]] = st[1] v[st[1]] = st[0] i = 1 tt = s[0] res = 0 while(s[i] != '\n'): if s[i] in v: if(tt == v[s[i]]): res += 1 else: tt = s[i] else: tt = s[i] i += 1 print(res)
42
248
512,000
5544564
p, t = {}, input() a, n = False, len(t) x = y = s = 0 q = [input() for i in range(int(input()))] for a, b in q: p[a], p[b] = b, a for i in t: if a: if i == a: x += 1 elif i == b: y += 1 else: s += min(x, y) if i in p: a, b = i, p[i] x, y = 1, 0 else: a = False elif i in p: a, b = i, p[i] x, y = 1, 0 if a: s += min(x, y) print(s)
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input": "eepeeeeppppppeepeppe\r\n1\r\npe\r\n", "output": "10\r\n"}, {"input": "vefneyamdzoytemupniw\r\n13\r\nve\r\nfg\r\noi\r\nan\r\nck\r\nwx\r\npq\r\nml\r\nbt\r\nud\r\nrz\r\nsj\r\nhy\r\n", "output": "1\r\n"}, {"input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\r\n13\r\nld\r\nac\r\nnp\r\nrv\r\nmo\r\njh\r\ngb\r\nuw\r\nfq\r\nst\r\nkx\r\nzy\r\nei\r\n", "output": "11\r\n"}, {"input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\r\n4\r\nli\r\nqh\r\nad\r\nbp\r\n", "output": "4\r\n"}, {"input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\r\n5\r\nmb\r\nho\r\nxu\r\njv\r\nyp\r\n", "output": "37\r\n"}, {"input": "z\r\n0\r\n", "output": "0\r\n"}, {"input": "t\r\n13\r\nzx\r\nig\r\nyq\r\nbd\r\nph\r\nar\r\nne\r\nwo\r\ntk\r\njl\r\ncv\r\nfs\r\nmu\r\n", "output": "0\r\n"}, {"input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\r\n13\r\njr\r\nnf\r\nyk\r\ntq\r\nwe\r\nil\r\ngu\r\npb\r\naz\r\nxm\r\nhc\r\nvd\r\nso\r\n", "output": "0\r\n"}, {"input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\r\n1\r\nni\r\n", "output": "28\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n1\r\nab\r\n", "output": "75\r\n"}]
false
stdio
null
true
56/B
56
B
Python 3
TESTS
7
124
307,200
108974240
n=int(input()) ls=list(map(int,input().split())) flag=0 if ls==ls[::-1] and len(ls)!=1: print("1"+" "+str(n)) else: ans=list(range(1,n+1)) for i in range(n-1): for j in range(i+1,n): if ls[:i]+ls[j:i-1:-1]+ls[(j+1):]==ans: print(str(i+1)+" "+str(j+1)) flag=1 break if flag==0: print("0 0")
33
92
0
183313085
n=int(input()) a=list(map(int,input().split())) l,r=-1,-1 for i in range(n): if a[i]!=i+1: l=i break for i in range(n-1,-1,-1): if a[i]!=i+1: r=i break j=r+1 for i in range(l,r+1): if a[i]==j: j-=1 continue else: print(0,0) exit(0) print(l+1,r+1)
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
false
stdio
null
true
739/A
739
A
PyPy 3
TESTS
4
77
20,172,800
127795929
starts=[] ans=[] mn=10**18 n,m=map(int,input().split()) for i in range(m): x,y=map(int,input().split()) mn=min(mn,y-x+1) starts.append(x) i=0 num=0 counter=0 while(i<n): i+=1 if(starts[counter]==i): counter+=1 counter=min(counter,m-1) num=0 else: num+=1 ans.append(num) print(mn) print(*ans)
69
374
36,659,200
124730404
import sys input=sys.stdin.readline n,m=map(int,input().split()) seg=[list(map(int,input().split())) for i in range(m)] min_l=10**18 for l,r in seg: min_l=min(min_l,r-l+1) ans=[i for i in range(min_l)]*((n+min_l-1)//min_l) ans=ans[:n] print(min_l) print(*ans)
Codeforces Round 381 (Div. 1)
CF
2,016
2
256
Alyona and mex
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri]. Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible. The mex of a set S is a minimum possible non-negative integer that is not in S.
The first line contains two integers n and m (1 ≤ n, m ≤ 105). The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
In the first line print single integer — the maximum possible minimum mex. In the second line print n integers — the array a. All the elements in a should be between 0 and 109. It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109. If there are multiple solutions, print any of them.
null
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
[{"input": "5 3\n1 3\n2 5\n4 5", "output": "2\n1 0 2 1 0"}, {"input": "4 2\n1 4\n2 4", "output": "3\n5 2 0 1"}]
1,700
["constructive algorithms", "greedy"]
69
[{"input": "5 3\r\n1 3\r\n2 5\r\n4 5\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "4 2\r\n1 4\r\n2 4\r\n", "output": "3\r\n0 1 2 0\r\n"}, {"input": "1 1\r\n1 1\r\n", "output": "1\r\n0\r\n"}, {"input": "2 1\r\n2 2\r\n", "output": "1\r\n0 0\r\n"}, {"input": "5 6\r\n2 4\r\n2 3\r\n1 4\r\n3 4\r\n2 5\r\n1 3\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "8 3\r\n2 3\r\n2 8\r\n3 6\r\n", "output": "2\r\n0 1 0 1 0 1 0 1\r\n"}, {"input": "10 10\r\n1 9\r\n4 8\r\n4 8\r\n5 9\r\n1 9\r\n3 8\r\n1 6\r\n1 9\r\n1 6\r\n6 9\r\n", "output": "4\r\n0 1 2 3 0 1 2 3 0 1\r\n"}, {"input": "3 6\r\n1 3\r\n1 3\r\n1 1\r\n1 1\r\n3 3\r\n3 3\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "3 3\r\n1 3\r\n2 2\r\n1 3\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "6 8\r\n3 5\r\n3 6\r\n4 6\r\n2 5\r\n2 5\r\n1 3\r\n3 6\r\n3 5\r\n", "output": "3\r\n0 1 2 0 1 2\r\n"}, {"input": "10 4\r\n4 10\r\n4 6\r\n6 8\r\n1 10\r\n", "output": "3\r\n0 1 2 0 1 2 0 1 2 0\r\n"}, {"input": "9 1\r\n1 1\r\n", "output": "1\r\n0 0 0 0 0 0 0 0 0\r\n"}, {"input": "3 8\r\n2 3\r\n1 3\r\n1 2\r\n2 3\r\n1 3\r\n2 2\r\n1 2\r\n1 2\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "3 8\r\n1 2\r\n1 2\r\n1 1\r\n2 3\r\n2 3\r\n1 1\r\n1 3\r\n1 3\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "7 3\r\n7 7\r\n3 7\r\n5 7\r\n", "output": "1\r\n0 0 0 0 0 0 0\r\n"}, {"input": "9 9\r\n4 5\r\n5 8\r\n1 8\r\n4 8\r\n3 4\r\n7 8\r\n1 4\r\n7 8\r\n6 7\r\n", "output": "2\r\n0 1 0 1 0 1 0 1 0\r\n"}, {"input": "10 10\r\n1 5\r\n7 10\r\n2 10\r\n2 5\r\n2 9\r\n5 9\r\n3 10\r\n4 9\r\n6 9\r\n2 7\r\n", "output": "4\r\n0 1 2 3 0 1 2 3 0 1\r\n"}, {"input": "8 7\r\n5 8\r\n3 7\r\n1 8\r\n3 4\r\n2 8\r\n2 7\r\n4 6\r\n", "output": "2\r\n0 1 0 1 0 1 0 1\r\n"}, {"input": "10 3\r\n2 4\r\n8 10\r\n4 6\r\n", "output": "3\r\n0 1 2 0 1 2 0 1 2 0\r\n"}, {"input": "5 8\r\n3 4\r\n1 5\r\n3 4\r\n3 5\r\n3 4\r\n1 4\r\n1 5\r\n2 5\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "9 4\r\n5 9\r\n3 8\r\n2 8\r\n1 4\r\n", "output": "4\r\n0 1 2 3 0 1 2 3 0\r\n"}, {"input": "7 7\r\n2 4\r\n1 4\r\n3 6\r\n2 6\r\n5 7\r\n3 6\r\n1 4\r\n", "output": "3\r\n0 1 2 0 1 2 0\r\n"}, {"input": "6 2\r\n4 6\r\n2 5\r\n", "output": "3\r\n0 1 2 0 1 2\r\n"}, {"input": "7 9\r\n6 7\r\n1 2\r\n2 5\r\n4 7\r\n1 7\r\n5 6\r\n2 3\r\n6 7\r\n1 2\r\n", "output": "2\r\n0 1 0 1 0 1 0\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path, 'r') as f: n, m = map(int, f.readline().split()) subarrays = [tuple(map(int, line.strip().split())) for line in f] # Read reference output (k_ref) with open(output_path, 'r') as f: k_ref = int(f.readline().strip()) # Read submission output with open(submission_path, 'r') as f: lines = f.readlines() if len(lines) < 2: print(0) return try: k_sub = int(lines[0].strip()) array_sub = list(map(int, lines[1].strip().split())) except: print(0) return # Check k_sub equals k_ref if k_sub != k_ref: print(0) return # Check array validity if len(array_sub) != n: print(0) return for num in array_sub: if not (0 <= num <= 10**9): print(0) return required = set(range(k_sub)) found_k_sub = False for l, r in subarrays: start = l - 1 end = r sub = array_sub[start:end] elements = set(sub) # Check all required elements are present if not required.issubset(elements): print(0) return # Check if current subarray has mex exactly k_sub if k_sub not in elements: found_k_sub = True if not found_k_sub: print(0) return print(100) if __name__ == "__main__": main()
true
490/B
490
B
PyPy 3-64
TESTS
3
46
0
179040721
no_stu = int(input()) pairs = {} queue = [0]*no_stu back_set = set() for i in range(no_stu): front, back = list(map(int, input().split())) if front==0: queue[1]=back elif back==0: queue[-2]=front else: pairs[front] = back back_set.add(back) for i in pairs: if i not in back_set: queue[0]=i break for i in range(len(queue)-2): queue[i+2] = pairs[queue[i]] for i in range(len(queue)-1): print(queue[i], end=" ") print(queue[-1])
61
888
29,491,200
165675179
n = int(input()) cd = set() trace = {} nodes = set() for _ in range(n): a, b = input().split() if a == '0': a = 'start' if b == '0': b = 'end' cd.add(b) nodes.add(a) nodes.add(b) trace[a] = b first, second = -1, trace.get('start') for nd in nodes: if nd not in cd and nd != 'start': first = nd while first or second: if first and first != 'end': print(first, end=" ") if second and second != 'end': print(second, end=" ") first = trace.get(first) second = trace.get(second)
Codeforces Round 279 (Div. 2)
CF
2,014
2
256
Queue
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1). After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task. Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
null
The picture illustrates the queue for the first sample.
[{"input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141"}]
1,500
["dsu", "implementation"]
61
[{"input": "4\r\n92 31\r\n0 7\r\n31 0\r\n7 141\r\n", "output": "92 7 31 141 \r\n"}, {"input": "2\r\n0 1\r\n2 0\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 2\r\n1 3\r\n2 0\r\n", "output": "1 2 3 \r\n"}, {"input": "4\r\n101 0\r\n0 102\r\n102 100\r\n103 101\r\n", "output": "103 102 101 100 \r\n"}, {"input": "5\r\n0 1\r\n1 4\r\n4 0\r\n3 2\r\n5 3\r\n", "output": "5 1 3 4 2 \r\n"}, {"input": "6\r\n10001 0\r\n0 10005\r\n10003 10001\r\n10002 10000\r\n10005 10002\r\n10004 10003\r\n", "output": "10004 10005 10003 10002 10001 10000 \r\n"}, {"input": "3\r\n0 743259\r\n72866 70294\r\n743259 0\r\n", "output": "72866 743259 70294 \r\n"}, {"input": "4\r\n263750 0\r\n513707 263750\r\n0 718595\r\n718595 148112\r\n", "output": "513707 718595 263750 148112 \r\n"}, {"input": "5\r\n645873 145459\r\n638930 82975\r\n0 645873\r\n82975 389665\r\n145459 0\r\n", "output": "638930 645873 82975 145459 389665 \r\n"}, {"input": "6\r\n341637 51795\r\n0 809471\r\n51795 0\r\n244669 341637\r\n852537 508622\r\n809471 852537\r\n", "output": "244669 809471 341637 852537 51795 508622 \r\n"}, {"input": "7\r\n111283 0\r\n496010 510417\r\n423431 921854\r\n510417 111283\r\n0 496010\r\n758535 423431\r\n921854 59208\r\n", "output": "758535 496010 423431 510417 921854 111283 59208 \r\n"}, {"input": "8\r\n611412 115521\r\n114290 712424\r\n115521 242491\r\n242491 0\r\n0 114290\r\n712424 282922\r\n282922 589147\r\n359823 611412\r\n", "output": "359823 114290 611412 712424 115521 282922 242491 589147 \r\n"}, {"input": "9\r\n308992 348750\r\n0 6496\r\n487447 676506\r\n874677 985199\r\n260782 487447\r\n985199 260782\r\n348750 0\r\n570981 308992\r\n6496 570981\r\n", "output": "874677 6496 985199 570981 260782 308992 487447 348750 676506 \r\n"}, {"input": "10\r\n419946 201769\r\n245945 0\r\n842799 113073\r\n836998 245945\r\n0 794376\r\n692107 836998\r\n113073 904403\r\n904403 987165\r\n201769 692107\r\n794376 842799\r\n", "output": "419946 794376 201769 842799 692107 113073 836998 904403 245945 987165 \r\n"}, {"input": "10\r\n189071 852255\r\n227133 652124\r\n329720 4848\r\n652124 329720\r\n0 72517\r\n943168 0\r\n72517 544697\r\n4848 943168\r\n538963 189071\r\n544697 538963\r\n", "output": "227133 72517 652124 544697 329720 538963 4848 189071 943168 852255 \r\n"}, {"input": "2\r\n0 300000\r\n1000000 0\r\n", "output": "1000000 300000 \r\n"}]
false
stdio
null
true
501/C
501
C
PyPy 3-64
TESTS
2
46
0
178272399
n=int(input()) w=[] eta=[] for j in range(n): w.append([int(k) for k in input().split()]) for j in range(n): if w[j][0]==1: eta.append([j, w[j][1]]) w[w[j][1]][1]=w[w[j][1]][1]^j w[w[j][1]][0]-=1 w[j][0]=0 w[j][1]=0 iota=w[j][1] while w[iota][0]==1: eta.append([iota, w[iota][1]]) w[w[iota][1]][1]=w[w[iota][1]][1]^iota w[w[iota][1]][0]-=1 w[iota][0]=0 w[iota][1]=0 iota=w[iota][1] print(len(eta)) for j in eta: print(" ".join([str(k) for k in j]))
49
233
26,009,600
188040560
import sys def read(T): return [T(i) for i in sys.stdin.readline().split()] def main(): n=read(int)[0] a=[read(int) for i in range(n)] stack,res=[],[] for i in range(n): if a[i][0]==1: stack.append(i) while len(stack): cur=stack.pop() if not a[cur][0]: continue res.append([cur,a[cur][1]]) a[cur][0]-=1 a[a[cur][1]][0]-=1 if a[a[cur][1]][0]==1: stack.append(a[cur][1]) a[a[cur][1]][1]^=cur a[cur][1]=0 print(len(res)) for edge in res: print(edge[0],edge[1]) main()
Codeforces Round 285 (Div. 2)
CF
2,015
1
256
Misha and Forest
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.
The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space.
In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order.
null
The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
[{"input": "3\n2 3\n1 0\n1 0", "output": "2\n1 0\n2 0"}, {"input": "2\n1 1\n1 0", "output": "1\n0 1"}]
1,500
["constructive algorithms", "data structures", "greedy", "sortings", "trees"]
49
[{"input": "3\r\n2 3\r\n1 0\r\n1 0\r\n", "output": "2\r\n1 0\r\n2 0\r\n"}, {"input": "2\r\n1 1\r\n1 0\r\n", "output": "1\r\n0 1\r\n"}, {"input": "10\r\n3 13\r\n2 6\r\n1 5\r\n3 5\r\n1 3\r\n2 2\r\n2 6\r\n1 6\r\n1 3\r\n2 3\r\n", "output": "9\r\n2 5\r\n4 3\r\n7 6\r\n8 3\r\n5 0\r\n6 1\r\n3 9\r\n1 0\r\n9 0\r\n"}, {"input": "10\r\n1 2\r\n1 7\r\n1 0\r\n1 8\r\n0 0\r\n1 9\r\n0 0\r\n1 1\r\n1 3\r\n1 5\r\n", "output": "4\r\n0 2\r\n1 7\r\n3 8\r\n5 9\r\n"}, {"input": "5\r\n1 1\r\n2 2\r\n2 2\r\n2 6\r\n1 3\r\n", "output": "4\r\n0 1\r\n4 3\r\n1 2\r\n3 2\r\n"}, {"input": "10\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "11\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n1 8\r\n1 7\r\n0 0\r\n0 0\r\n", "output": "1\r\n7 8\r\n"}, {"input": "12\r\n0 0\r\n1 3\r\n0 0\r\n1 1\r\n0 0\r\n1 7\r\n0 0\r\n1 5\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "2\r\n1 3\r\n5 7\r\n"}, {"input": "13\r\n2 7\r\n0 0\r\n0 0\r\n2 11\r\n2 7\r\n2 14\r\n2 3\r\n2 1\r\n1 11\r\n3 15\r\n1 6\r\n2 11\r\n1 9\r\n", "output": "10\r\n8 11\r\n10 6\r\n12 9\r\n11 3\r\n6 9\r\n3 0\r\n9 5\r\n0 4\r\n5 7\r\n4 7\r\n"}, {"input": "14\r\n1 10\r\n1 9\r\n3 4\r\n1 2\r\n0 0\r\n1 11\r\n1 12\r\n1 10\r\n1 10\r\n2 10\r\n3 15\r\n3 14\r\n2 4\r\n0 0\r\n", "output": "10\r\n0 10\r\n1 9\r\n3 2\r\n5 11\r\n6 12\r\n7 10\r\n8 10\r\n9 11\r\n12 2\r\n11 2\r\n"}, {"input": "15\r\n0 0\r\n1 6\r\n3 2\r\n1 13\r\n2 15\r\n2 5\r\n1 1\r\n2 1\r\n1 4\r\n1 2\r\n0 0\r\n1 14\r\n0 0\r\n1 3\r\n2 9\r\n", "output": "9\r\n1 6\r\n3 13\r\n8 4\r\n9 2\r\n11 14\r\n4 7\r\n14 2\r\n7 5\r\n2 5\r\n"}, {"input": "16\r\n1 10\r\n2 13\r\n1 13\r\n2 1\r\n1 3\r\n2 2\r\n1 14\r\n0 0\r\n1 1\r\n1 14\r\n1 0\r\n0 0\r\n0 0\r\n1 2\r\n2 15\r\n0 0\r\n", "output": "8\r\n0 10\r\n2 13\r\n4 3\r\n6 14\r\n8 1\r\n9 14\r\n3 5\r\n1 5\r\n"}, {"input": "17\r\n0 0\r\n2 6\r\n0 0\r\n2 11\r\n0 0\r\n1 13\r\n1 3\r\n1 10\r\n0 0\r\n1 1\r\n1 7\r\n0 0\r\n0 0\r\n3 9\r\n0 0\r\n2 12\r\n0 0\r\n", "output": "7\r\n5 13\r\n6 3\r\n7 10\r\n9 1\r\n3 13\r\n1 15\r\n13 15\r\n"}, {"input": "18\r\n0 0\r\n0 0\r\n2 19\r\n1 2\r\n2 29\r\n0 0\r\n1 7\r\n1 6\r\n1 12\r\n1 13\r\n0 0\r\n1 12\r\n4 23\r\n1 9\r\n0 0\r\n0 0\r\n2 14\r\n1 4\r\n", "output": "9\r\n3 2\r\n6 7\r\n8 12\r\n9 13\r\n11 12\r\n17 4\r\n2 16\r\n4 12\r\n16 12\r\n"}, {"input": "19\r\n1 13\r\n0 0\r\n1 9\r\n1 11\r\n1 11\r\n2 3\r\n2 30\r\n0 0\r\n1 5\r\n1 2\r\n0 0\r\n5 29\r\n1 6\r\n2 11\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n2 13\r\n", "output": "10\r\n0 13\r\n2 9\r\n3 11\r\n4 11\r\n8 5\r\n12 6\r\n13 11\r\n5 11\r\n6 18\r\n11 18\r\n"}, {"input": "20\r\n0 0\r\n2 15\r\n0 0\r\n2 7\r\n1 1\r\n0 0\r\n0 0\r\n0 0\r\n1 9\r\n2 11\r\n0 0\r\n1 1\r\n0 0\r\n0 0\r\n1 3\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "5\r\n4 1\r\n8 9\r\n11 1\r\n14 3\r\n9 3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "0\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] # not used submission_path = sys.argv[3] # Read input with open(input_path) as f: lines = [line.strip() for line in f if line.strip()] n = int(lines[0]) degrees = [] s_list = [] for line in lines[1:n+1]: d, s = map(int, line.split()) degrees.append(d) s_list.append(s) # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f if line.strip()] if not sub_lines: print(0) return try: m = int(sub_lines[0]) except: print(0) return edges = [] for line in sub_lines[1:]: parts = line.split() if len(parts) != 2: print(0) return try: a = int(parts[0]) b = int(parts[1]) except: print(0) return edges.append( (a, b) ) # Check sum_deg is even and m matches sum_deg = sum(degrees) if sum_deg % 2 != 0: print(0) return if m != sum_deg // 2: print(0) return # Check edges count if len(edges) != m: print(0) return # Check each edge is valid and no duplicates seen = set() for a, b in edges: if a == b: print(0) return if not (0 <= a < n) or not (0 <= b < n): print(0) return u, v = min(a, b), max(a, b) if (u, v) in seen: print(0) return seen.add( (u, v) ) # Build adjacency list adj = [[] for _ in range(n)] for a, b in edges: adj[a].append(b) adj[b].append(a) # Check degrees and XOR sums for v in range(n): if len(adj[v]) != degrees[v]: print(0) return xor_sum = 0 for nei in adj[v]: xor_sum ^= nei if xor_sum != s_list[v]: print(0) return # Check for cycles using Union-Find parent = list(range(n)) def find(u): while parent[u] != u: parent[u] = parent[parent[u]] u = parent[u] return u for a, b in edges: root_a = find(a) root_b = find(b) if root_a == root_b: print(0) return parent[root_a] = root_b # All checks passed print(1) if __name__ == "__main__": main()
true
56/B
56
B
Python 3
TESTS
7
218
6,963,200
86098035
n=int(input()) a=list(map(int,input().split())) L=0 if a[L]==1: while L<n-1: if a[L+1]-a[L]!=1: break L+=1 if L==n-1: print(0,0) exit() L+=1 R=a.index(L+1) a=a[:L]+a[R:L-1:-1]+a[R+1:] b=[x for x in range(1,n+1)] if a==b: print(L+1,R+1) else: print(0,0)
33
92
4,505,600
161868235
n = int(input()) arr = list(map(int,input().split())) l,r = -1,-1 for i in range(n): if arr[i] != i+1: l = i break for i in range(n-1,-1,-1): if arr[i] != i+1: r = i break s = r+1 for i in range(l,s): if arr[i]==s: s -= 1 continue else: print(0,0) exit(0) print(l+1,r+1)
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
false
stdio
null
true
56/B
56
B
PyPy 3
TESTS
7
218
23,040,000
20031253
def s(): input() a = list(map(int,input().split())) b = list(range(1,len(a)+1)) l,r = 0,0 for i,v in enumerate(a): if v != b[i]: l = i break for i,v in reversed(list(enumerate(a))): if v != b[i]: r = i break a[l:r+1] = a[r:l-1:-1] if a == b: print(l+1,r+1) else: print(0,0) s()
33
122
4,505,600
135321089
import sys n = int(sys.stdin.readline()) seq = sys.stdin.readline().split(' ') left = 0 right = 0 isPermFound = False for i in range(1, n): if int(seq[i]) - int(seq[i-1]) > 1: if left == 0: left = i + 1 elif isPermFound: print("0 0") exit(0) else: isPermFound = True right = i if int(seq[i-1]) - int(seq[i]) > 1: print("0 0") exit(0) if int(seq[i-1]) > int(seq[i]): left = i if left == 0 else left right = i + 1 print("{} {}".format(left, right))
Codeforces Beta Round 52 (Div. 2)
CF
2,011
2
256
Spoilt Permutation
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
null
null
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
1,300
["implementation"]
33
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
false
stdio
null
true
698/B
698
B
PyPy 3-64
TESTS
4
61
0
189687081
n=int(input()) arr=[int(h) for h in input().split()] visited=[0]*n change=[] root=[] for i in range(1,n+1): if arr[i-1]==i: root.append(i) j=i if visited[i-1]==1: continue h=set() while j not in h: h.add(j) visited[j-1]=1 j=arr[j-1] if j in h: change.append(j) change=list(set(change)) if len(root)==0: print(len(change)) for f in change: arr[f-1]=change[0] print(*arr) else: print(len(change)-1) for f in change: arr[f-1]=root[0] print(*arr)
101
405
26,521,600
36753020
n = int(input()) arr = list(map(int, input().split(' '))) root=-1 for i,a in enumerate(arr) : if i == a-1 : root = i break v = [False]*len(arr) if root>-1 : v[root]=True ans = 0 for i,a in enumerate(arr) : if v[i] : continue v[i]= True l=[i] a-=1 while not v[a] : l.append(a) v[a]=True a=arr[a]-1 if a in l: #new cycle if root==-1: arr[a]=a+1 root=a ans+=1 else : arr[a]=root+1 ans+=1 print(ans) print(' '.join(map(str,arr)))
Codeforces Round 363 (Div. 1)
CF
2,016
2
256
Fix a Tree
A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent). For this rooted tree the array p is [2, 3, 3, 2]. Given a sequence p1, p2, ..., pn, one is able to restore a tree: 1. There must be exactly one index r that pr = r. A vertex r is a root of the tree. 2. For all other n - 1 vertices i, there is an edge between vertex i and vertex pi. A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid. You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
In the first line print the minimum number of elements to change, in order to get a valid sequence. In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
null
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because p4 = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red. In the second sample, the given sequence is already valid.
[{"input": "4\n2 3 3 4", "output": "1\n2 3 4 4"}, {"input": "5\n3 2 2 5 3", "output": "0\n3 2 2 5 3"}, {"input": "8\n2 3 5 4 1 6 6 7", "output": "2\n2 3 7 8 1 6 6 7"}]
1,700
["constructive algorithms", "dfs and similar", "dsu", "graphs", "trees"]
101
[{"input": "4\r\n2 3 3 4\r\n", "output": "1\r\n2 3 4 4 \r\n"}, {"input": "5\r\n3 2 2 5 3\r\n", "output": "0\r\n3 2 2 5 3 \r\n"}, {"input": "8\r\n2 3 5 4 1 6 6 7\r\n", "output": "2\r\n2 3 7 8 1 6 6 7\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1\r\n2 2 \r\n"}, {"input": "7\r\n4 3 2 6 3 5 2\r\n", "output": "1\r\n4 3 3 6 3 5 2 \r\n"}, {"input": "6\r\n6 2 6 2 4 2\r\n", "output": "0\r\n6 2 6 2 4 2 \r\n"}, {"input": "7\r\n1 6 4 4 5 6 7\r\n", "output": "4\r\n7 6 4 7 7 7 7 \r\n"}, {"input": "7\r\n7 5 3 1 2 1 5\r\n", "output": "1\r\n7 5 3 1 3 1 5 \r\n"}, {"input": "7\r\n1 2 3 4 5 6 7\r\n", "output": "6\r\n7 7 7 7 7 7 7 \r\n"}, {"input": "18\r\n2 3 4 5 2 7 8 9 10 7 11 12 14 15 13 17 18 18\r\n", "output": "5\r\n2 18 4 5 2 7 18 9 10 7 18 18 18 15 13 17 18 18 \r\n"}, {"input": "8\r\n2 1 2 2 6 5 6 6\r\n", "output": "2\r\n1 1 2 2 1 5 6 6 \r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "1\r\n1 1 1 \r\n"}]
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_output_path): # Read input with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) assert len(a) == n # Read submission's output with open(submission_output_path) as f: k_sub_line = f.readline().strip() p_line = f.readline().strip() k_sub = int(k_sub_line) p = list(map(int, p_line.split())) assert len(p) == n # Compute K_min cycles = compute_cycles(a) c = len(cycles) has_self_loop = any(len(cycle) == 1 for cycle in cycles) if has_self_loop: k_min = c - 1 else: k_min = c # Check submission's K_sub equals k_min if k_sub != k_min: print(0) return # Check validity of p if not is_valid(p): print(0) return # Check number of changes between a and p is K_sub changes = sum(1 for i in range(n) if a[i] != p[i]) if changes != k_sub: print(0) return print(1) def compute_cycles(a): n = len(a) visited = [False] * (n + 1) cycles = [] for i in range(1, n+1): if not visited[i]: path = [] current = i while True: if visited[current]: if current in path: idx = path.index(current) cycle = path[idx:] cycles.append(cycle) break visited[current] = True path.append(current) current = a[current-1] return cycles def is_valid(p): n = len(p) # Check exactly one root roots = [i+1 for i in range(n) if p[i] == i+1] if len(roots) != 1: return False r = roots[0] # Build reverse adjacency list reverse_adj = [[] for _ in range(n+1)] for j in range(n): i = j + 1 parent = p[j] reverse_adj[parent].append(i) # BFS to count reachable nodes from r visited = [False] * (n + 1) queue = deque([r]) visited[r] = True count = 1 while queue: u = queue.popleft() for v in reverse_adj[u]: if not visited[v]: visited[v] = True count += 1 queue.append(v) return count == n if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true