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
489/A
489
A
PyPy 3-64
TESTS
5
46
0
227825926
n = int(input()) arr = list(map(int,input().split())) swaps = [] for i in range(n): curr_min = min(arr[i:]) curr_min_ind = arr.index(curr_min) if arr[i] != curr_min: arr[curr_min_ind] = arr[i] arr[i] = curr_min swaps.append([i, curr_min_ind]) print(len(swaps)) for s in swaps: print(*s)
22
93
5,939,200
203206293
import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) n = int(input()) lst = [x for x in get_ints()] cnt = 0 r = [] for i in range(len(lst)): j = i for t in range(j,len(lst)): if lst[t] < lst[j]: j = t if i == j: continue a = lst[j] lst[j] = lst[i] lst[i] = a if i < j: r.append([i,j]) else: r.append([j,i]) cnt += 1 print(cnt) for x in r: print(f"{x[0]} {x[1]}")
Codeforces Round 277.5 (Div. 2)
CF
2,014
1
256
SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
null
null
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
1,200
["greedy", "implementation", "sortings"]
22
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) arr = list(map(int, f.readline().split())) with open(sub_path) as f: lines = f.readlines() if not lines: print(0) return try: k = int(lines[0].strip()) swaps = [] for line in lines[1:1 + k]: i, j = map(int, line.strip().split()) swaps.append((i, j)) except (ValueError, IndexError): print(0) return if not (0 <= k <= n): print(0) return for i, j in swaps: if not (0 <= i < n and 0 <= j < n): print(0) return current = arr.copy() for i, j in swaps: current[i], current[j] = current[j], current[i] sorted_arr = sorted(arr) if current == sorted_arr: print(1) else: print(0) if __name__ == "__main__": main()
true
149/C
149
C
PyPy 3-64
TESTS
1
30
512,000
153604948
# @Chukamin ICPC_TRAIN def main(): n = int(input()) data = list(map(int, input().split())) numlist = [[i + 1, 0] for i in range(n)] for i in range(n): numlist[i][1] = data[i] numlist.sort(key = lambda x: x[1]) p = n >> 1 q = n - p print(p) for i in range(0, n, 2): print(numlist[i][0], end = ' ') print() print(q) for j in range(1, n, 2): print(numlist[j][0], end = ' ') if __name__ == '__main__': main()
47
343
19,353,600
204888678
n=int(input()) st=list(map(int,input().split())) b=[] for i in range(0,n): b.append([st[i],i+1]) b.sort() x,y=[],[] for i in range(0,n): if i%2==0: x.append(b[i][1]) else: y.append(b[i][1]) print(len(x)) print(*x) print(len(y)) print(*y)
Codeforces Round 106 (Div. 2)
CF
2,012
1
256
Division into Teams
Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers). The key in football is to divide into teams fairly before the game begins. There are n boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristic ai (the larger it is, the better the boy plays). Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team as qi. Division n boys into two teams is considered fair if three conditions are fulfilled: - Each boy plays for exactly one team (x + y = n). - The sizes of teams differ in no more than one (|x - y| ≤ 1). - The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally: $$\left| \sum_{i=1}^{x} a_{p_i} - \sum_{i=1}^{y} a_{q_i} \right| \leq \max_{i=1}^{n} a_i$$ Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.
The first line contains the only integer n (2 ≤ n ≤ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 ≤ ai ≤ 104), the i-th number represents the i-th boy's playing skills.
On the first line print an integer x — the number of boys playing for the first team. On the second line print x integers — the individual numbers of boys playing for the first team. On the third line print an integer y — the number of boys playing for the second team, on the fourth line print y integers — the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions: x + y = n, |x - y| ≤ 1, and the condition that limits the total skills. If there are multiple ways to solve the problem, print any of them. The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.
null
Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 ≤ 2) is fulfilled.
[{"input": "3\n1 2 1", "output": "2\n1 2\n1\n3"}, {"input": "5\n2 3 3 1 1", "output": "3\n4 1 3\n2\n5 2"}]
1,500
["greedy", "math", "sortings"]
47
[{"input": "3\r\n1 2 1\r\n", "output": "2\r\n1 2 \r\n1\r\n3 \r\n"}, {"input": "5\r\n2 3 3 1 1\r\n", "output": "3\r\n4 1 3 \r\n2\r\n5 2 \r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 1 2 2\r\n", "output": "5\r\n8 2 4 6 9 \r\n5\r\n1 3 5 7 10 \r\n"}, {"input": "10\r\n2 3 3 1 3 1 1 1 2 2\r\n", "output": "5\r\n4 7 1 10 3 \r\n5\r\n6 8 9 2 5 \r\n"}, {"input": "10\r\n2 3 2 3 3 1 1 3 1 1\r\n", "output": "5\r\n6 9 1 2 5 \r\n5\r\n7 10 3 4 8 \r\n"}, {"input": "11\r\n1 3 1 2 1 2 2 2 1 1 1\r\n", "output": "6\r\n1 5 10 4 7 2 \r\n5\r\n3 9 11 6 8 \r\n"}, {"input": "11\r\n54 83 96 75 33 27 36 35 26 22 77\r\n", "output": "6\r\n10 6 8 1 11 3 \r\n5\r\n9 5 7 4 2 \r\n"}, {"input": "11\r\n1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "6\r\n1 3 5 7 9 11 \r\n5\r\n2 4 6 8 10 \r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n1 \r\n1\r\n2 \r\n"}, {"input": "2\r\n35 36\r\n", "output": "1\r\n1 \r\n1\r\n2 \r\n"}, {"input": "25\r\n1 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 1 2 2 2 1 1 2 2 1\r\n", "output": "13\r\n1 10 17 22 2 5 7 9 13 15 18 20 24 \r\n12\r\n4 11 21 25 3 6 8 12 14 16 19 23 \r\n"}, {"input": "27\r\n2 1 1 3 1 2 1 1 3 2 3 1 3 2 1 3 2 3 2 1 2 3 2 2 1 2 1\r\n", "output": "14\r\n2 5 8 15 25 1 10 17 21 24 4 11 16 22 \r\n13\r\n3 7 12 20 27 6 14 19 23 26 9 13 18 \r\n"}, {"input": "30\r\n2 2 2 3 4 3 4 4 3 2 3 2 2 4 1 4 2 4 2 2 1 4 3 2 1 3 1 1 4 3\r\n", "output": "15\r\n15 25 28 2 10 13 19 24 6 11 26 5 8 16 22 \r\n15\r\n21 27 1 3 12 17 20 4 9 23 30 7 14 18 29 \r\n"}, {"input": "100\r\n3 4 8 10 8 6 4 3 7 7 6 2 3 1 3 10 1 7 9 3 5 5 2 6 2 9 1 7 4 2 4 1 6 1 7 10 2 5 3 7 6 4 6 2 8 8 8 6 6 10 3 7 4 3 4 1 7 9 3 6 3 6 1 4 9 3 8 1 10 1 4 10 7 7 9 5 3 8 10 2 1 10 8 7 10 8 5 3 1 2 1 10 6 1 5 3 3 5 7 2\r\n", "output": "50\r\n14 27 34 63 70 89 94 23 30 44 90 1 13 20 51 59 66 88 97 7 31 53 64 21 38 87 98 11 33 43 49 62 9 18 35 52 73 84 3 45 47 78 86 26 65 4 36 69 79 85 \r\n50\r\n17 32 56 68 81 91 12 25 37 80 100 8 15 39 54 61 77 96 2 29 42 55 71 22 76 95 6 24 41 48 60 93 10 28 40 57 74 99 5 46 67 83 19 58 75 16 50 72 82 92 \r\n"}, {"input": "100\r\n85 50 17 89 65 89 5 20 86 26 16 21 85 14 44 31 87 31 6 2 48 67 8 80 79 1 48 36 97 1 5 30 79 50 78 12 2 55 76 100 54 40 26 81 97 96 68 56 87 14 51 17 54 37 52 33 69 62 38 63 74 15 62 78 9 19 67 2 60 58 93 60 18 96 55 48 34 7 79 82 32 58 90 67 20 50 27 15 7 89 98 10 11 15 99 49 4 51 77 52\r\n", "output": "50\r\n26 20 68 7 19 89 65 93 14 62 94 3 73 8 12 43 32 18 56 28 59 15 27 96 34 51 55 41 38 48 82 72 63 5 67 47 61 99 64 33 24 80 13 17 4 90 71 74 45 95 \r\n50\r\n30 37 97 31 78 23 92 36 50 88 11 52 66 85 10 87 16 81 77 54 42 21 76 2 86 98 100 53 75 70 69 58 60 22 84 57 39 35 25 79 44 1 9 49 6 83 46 29 91 40 \r\n"}, {"input": "100\r\n2382 7572 9578 1364 2325 2929 7670 5574 2836 2440 6553 1751 929 8785 6894 9373 9308 7338 6380 9541 9951 6785 8993 9942 5087 7544 6582 7139 8458 7424 9759 8199 9464 8817 7625 6200 4955 9373 9500 3062 849 4210 9337 5466 2190 8150 4971 3145 869 5675 1975 161 1998 378 5229 9000 8958 761 358 434 7636 8295 4406 73 375 812 2473 3652 9067 3052 5287 2850 6987 5442 2625 8894 8733 791 9763 5258 8259 9530 2050 7334 2118 2726 8221 5527 8827 1585 8334 8898 6399 6217 7400 2576 5164 9063 6247 9433\r\n", "output": "50\r\n64 59 54 58 66 49 4 12 53 85 5 10 96 86 72 70 48 42 37 25 55 71 44 8 36 99 93 27 15 28 18 30 2 61 46 87 62 29 14 89 92 23 98 17 16 100 39 20 31 24 \r\n50\r\n52 65 60 78 41 13 90 51 83 45 1 67 75 9 6 40 68 63 47 97 80 74 88 50 94 19 11 22 73 84 95 26 35 7 32 81 91 77 34 76 57 56 69 43 38 33 82 3 79 21 \r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "3\r\n10 10 10\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "3\r\n5 10 10\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "5\r\n6 1 1 1 1\r\n", "output": "3\r\n2 4 1 \r\n2\r\n3 5 \r\n"}, {"input": "5\r\n1 100 2 200 3\r\n", "output": "3\r\n1 5 4 \r\n2\r\n3 2 \r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n = int(f.readline().strip()) a = list(map(int, f.readline().split())) max_a = max(a) with open(submission_path) as f: lines = [line.strip() for line in f.readlines()] if len(lines) != 4: print(0) return try: x = int(lines[0]) p = list(map(int, lines[1].split())) y = int(lines[2]) q = list(map(int, lines[3].split())) except: print(0) return if x + y != n or abs(x - y) > 1: print(0) return if len(p) != x or len(q) != y: print(0) return all_indices = set(p) | set(q) if len(all_indices) != x + y or any(i < 1 or i > n for i in all_indices): print(0) return sum_p = sum(a[i-1] for i in p) sum_q = sum(a[i-1] for i in q) if abs(sum_p - sum_q) > max_a: print(0) return print(1) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
487/B
487
B
PyPy 3-64
TESTS
25
187
14,745,600
229957657
n, s, l = map(int, input().split()) a = list(map(int, input().split())) from collections import deque mx_queue = deque() mi_queue = deque() left = 0 f = deque([(0,0)]) for i in range(n): while mx_queue and a[mx_queue[-1]] < a[i]: mx_queue.pop() mx_queue.append(i) while mi_queue and a[mi_queue[-1]] > a[i]: mi_queue.pop() mi_queue.append(i) while a[mx_queue[0]] - a[mi_queue[0]] > s: left += 1 if mx_queue[0] < left: mx_queue.popleft() if mi_queue[0] < left: mi_queue.popleft() if f and f[0][1] < left: f.popleft() if not f or i - f[0][1] + 1 < l: continue res = f[0][0] + 1 while f and f[-1][0] > res: f.pop() f.append((res, i + 1)) print(-1 if not f else f[-1][0])
35
264
29,184,000
134849349
from collections import deque # f[i] = min(f[k]) + 1 for g[i] - 1 <= k <= i - l # [g[i], i] is a valid seg # sliding window of max/min elements def solve(N, S, L, A): qmax, qmin = deque(), deque() F = [N + 1] * N q = deque() j = 0 for i, x in enumerate(A): while qmax and A[qmax[-1]] <= x: qmax.pop() qmax.append(i) while qmin and A[qmin[-1]] >= x: qmin.pop() qmin.append(i) # increment j until max-min <= S while j <= i and qmax and qmin and A[qmax[0]] - A[qmin[0]] > S: while qmax and qmax[0] <= j: qmax.popleft() while qmin and qmin[0] <= j: qmin.popleft() j += 1 # [j,i] is the longest segment now # j - 1 <= k <= i - L if i >= L: # insert i-L while q and q[-1][1] >= F[i - L]: q.pop() q.append((i - L, F[i - L])) if j == 0 and i - j + 1 >= L: F[i] = 1 else: # remove elements before j-1 while q and q[0][0] < j - 1: q.popleft() if q: F[i] = min(F[i], q[0][1] + 1) return F[-1] if F[-1] <= N else -1 if __name__ == "__main__": N, S, L = (int(_) for _ in input().split()) A = [int(_) for _ in input().split()] print(solve(N, S, L, A))
Codeforces Round 278 (Div. 1)
CF
2,014
1
256
Strip
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: - Each piece should contain at least l numbers. - The difference between the maximal and the minimal number on the piece should be at most s. Please help Alexandra to find the minimal number of pieces meeting the condition above.
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105). The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output the minimal number of strip pieces. If there are no ways to split the strip, output -1.
null
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2]. For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
[{"input": "7 2 2\n1 3 1 2 4 1 2", "output": "3"}, {"input": "7 2 2\n1 100 1 100 1 100 1", "output": "-1"}]
2,000
["binary search", "data structures", "dp", "two pointers"]
35
[{"input": "7 2 2\r\n1 3 1 2 4 1 2\r\n", "output": "3\r\n"}, {"input": "7 2 2\r\n1 100 1 100 1 100 1\r\n", "output": "-1\r\n"}, {"input": "1 0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "6 565 2\r\n31 76 162 -182 -251 214\r\n", "output": "1\r\n"}, {"input": "1 0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "1 0 1\r\n-1000000000\r\n", "output": "1\r\n"}, {"input": "1 100 2\r\n42\r\n", "output": "-1\r\n"}, {"input": "2 1000000000 1\r\n-1000000000 1000000000\r\n", "output": "2\r\n"}, {"input": "2 1000000000 2\r\n-1000000000 1000000000\r\n", "output": "-1\r\n"}, {"input": "10 3 3\r\n1 1 1 1 1 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "10 3 3\r\n1 1 1 2 2 5 6 7 8 9\r\n", "output": "3\r\n"}]
false
stdio
null
true
696/A
696
A
PyPy 3-64
TESTS
2
62
3,686,400
211394913
import sys from collections import defaultdict input = sys.stdin.readline q = int(input()) d = defaultdict(int) for i in range(q): l = list(map(int, input().split())) if(l[0] == 1): u, v, w = min(l[1], l[2]), max(l[1], l[2]), l[3] while(v != 1 and v != u): d[v] += w v //= 2 if(v == u): continue while(u != 1): d[u] += w u //= 2 else: u, v = min(l[1], l[2]), max(l[1], l[2]) s = 0 while(v != 1 and v != u): s += d[v] v //= 2 if(v == u): print(s) continue while(u != 1): s += d[u] u //= 2 print(s)
49
171
7,884,800
19181795
d = {} def lca(u,v,w): res = 0 while u!=v: if u < v: u,v = v,u d[u] = d.get(u,0) +w res += d[u] u //=2 return res q = int(input()) for i in range(q): a = list(map(int,input().split())) if a[0]==1: lca(a[1],a[2],a[3]) else: print(lca(a[1],a[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
696/A
696
A
PyPy 3-64
TESTS
2
108
10,240,000
153311240
from math import ceil from typing import Set class AutoMapping(dict): def __getitem__(self, key: int) -> int: try: return super().__getitem__(key) except KeyError: super().__setitem__(key, 0) return 0 fees = AutoMapping() def path(i: int, j: int) -> Set[int]: result = set() assert i <= j while i < j: result.add(j) j = ceil(j / 2) - 1 while i != j: result.add(i) result.add(j) i = ceil(i / 2) - 1 j = ceil(j / 2) - 1 return result for _ in range(int(input())): event = input().split() if event[0] == "1": _, v, u, w = map(int, event) if u > v: u, v = v, u v -= 1 u -= 1 for index in path(u, v): fees[index] += w elif event[0] == "2": _, v, u = map(int, event) if u > v: u, v = v, u u -= 1 v -= 1 result = 0 for index in path(u, v): result += fees[index] print(result)
49
171
7,987,200
20225084
d = {} def lca(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 a = list(map(int, input().split())) if a[0] == 1: lca(a[1], a[2], a[3]) else: (print(lca(a[1], a[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
336/C
336
C
PyPy 3
TESTS
5
420
27,340,800
107367044
from sys import stdin,stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) for _ in range(1):#nmbr()): n=nmbr() a=lst() bits=[[0 for _ in range(31)] for _ in range(n)] for i in range(n): for j in range(31): bits[i][30-j]=(a[i]>>j)&1 # print(*bits,sep='\n') ch=bit=mx=0 for bp in range(31): ch=0 for i in range(n): if bits[i][30-bp]: ch+=1 if ch!=0: bit=bp mx=ch if mx==0: print(-1) else: ans=[] print(mx) for i in range(n): if bits[i][30-bit]: ans+=[a[i]] print(*ans)
45
358
52,326,400
127858869
def process(A): d = {} for x in A: i = 0 i1 = 1 while i1 <= x: if i1&x==i1: if i not in d: d[i] = [None, []] if d[i][0] is None: d[i][0] = x else: d[i][0] = (d[i][0] & x) d[i][1].append(x) i1 = 2*i1 i = i+1 answer =[-1, None, A] for x in d: if d[x][0] % (2**x)==0: answer = max(answer, [x]+d[x]) return answer[2] n = int(input()) A = [int(x) for x in input().split()] answer = process(A) print(len(answer)) print(' '.join(map(str, answer)))
Codeforces Round 195 (Div. 2)
CF
2,013
1
256
Vasily the Bear and Sequence
Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 and ... and bk is divisible by number 2v without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b1 and b2 and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and".
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).
In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk — the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.
null
null
[{"input": "5\n1 2 3 4 5", "output": "2\n4 5"}, {"input": "3\n1 2 4", "output": "1\n4"}]
1,800
["brute force", "greedy", "implementation", "number theory"]
45
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "2\r\n4 5\r\n"}, {"input": "3\r\n1 2 4\r\n", "output": "1\r\n4\r\n"}, {"input": "3\r\n1 20 22\r\n", "output": "2\r\n20 22\r\n"}, {"input": "10\r\n109070199 215498062 361633800 406156967 452258663 530571268 670482660 704334662 841023955 967424642\r\n", "output": "6\r\n361633800 406156967 452258663 530571268 841023955 967424642\r\n"}, {"input": "30\r\n61 65 67 71 73 75 77 79 129 131 135 137 139 141 267 520 521 522 524 526 1044 1053 6924600 32125372 105667932 109158064 192212084 202506108 214625360 260071380\r\n", "output": "8\r\n520 521 522 524 526 109158064 202506108 260071380\r\n"}, {"input": "40\r\n6 7 10 11 18 19 33 65 129 258 514 515 1026 2049 4741374 8220406 14324390 17172794 17931398 33354714 34796238 38926670 39901570 71292026 72512934 77319030 95372470 102081830 114152702 120215390 133853238 134659386 159128594 165647058 219356350 225884742 236147130 240926050 251729234 263751314\r\n", "output": "13\r\n2049 4741374 8220406 17172794 17931398 38926670 39901570 77319030 134659386 159128594 219356350 225884742 240926050\r\n"}, {"input": "1\r\n536870912\r\n", "output": "1\r\n536870912\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n536870911\r\n", "output": "1\r\n536870911\r\n"}, {"input": "2\r\n536870911 536870912\r\n", "output": "1\r\n536870912\r\n"}, {"input": "38\r\n37750369 37750485 37750546 37751012 37751307 37751414 37751958 37751964 37752222 37752448 75497637 75497768 75497771 75498087 75498145 75498177 75498298 75498416 75498457 150994987 150994994 150994999 150995011 150995012 150995015 150995016 150995023 150995040 150995053 805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\r\n", "output": "9\r\n805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\r\n"}, {"input": "39\r\n37749932 37750076 37750391 37750488 37750607 37750812 37750978 37751835 37752173 37752254 75497669 75497829 75497852 75498044 75498061 75498155 75498198 75498341 75498382 75498465 150994988 150994989 150995009 150995019 150995024 150995030 150995031 150995069 150995072 805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\r\n", "output": "10\r\n805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\r\n"}]
false
stdio
null
true
651/B
651
B
PyPy 3
TESTS
5
61
17,715,200
161984121
n=int(input()) a=[int(x)for x in input().split()] a.sort(reverse=True) ans=0 li=[] for i in range(1,n): if a[i]<a[i-1]: ans+=1 else: li.append(a[i-1]) if li: if li[0]>a[i-1]: ans+=1 li.pop() print(ans)
31
46
0
132051076
import sys n = int(input()) l = [int(x) for x in sys.stdin.readline().split()] s = [0]*1001 for i in l: s[i]+=1 print(n-max(s))
Codeforces Round 345 (Div. 2)
CF
2,016
1
256
Beautiful Paintings
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one. We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
null
In the first sample, the optimal order is: 10, 20, 30, 40, 50. In the second sample, the optimal order is: 100, 200, 100, 200.
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
1,200
["greedy", "sortings"]
31
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
false
stdio
null
true
652/B
652
B
PyPy 3-64
TESTS
4
46
28,979,200
161892063
def yes(): print("YES") def no(): print("NO") n = int(input()) lis = list(map(int, input().split())) lis.sort() if n == 2: print(lis[0], lis[1], end=" ") elif n == 3: print(lis[0], lis[2], lis[1], end=" ") else: for i in range(0, int(n/2)): if n % 2: print(lis[i], lis[i+3], end=" ") else: print(lis[i], lis[i+2], end=" ") if n % 2: print(lis[int(n/2)])
16
31
0
180647211
n = int(input()) l = sorted(list(map(int, input().split()))) for i in range(1,n): if(i%2 and i!=n-1): l[i], l[i+1] = l[i+1],l[i] print(*l)
Educational Codeforces Round 10
ICPC
2,016
1
256
z-sort
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: 1. ai ≥ ai - 1 for all even i, 2. ai ≤ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted. Can you make the array z-sorted?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
null
null
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
1,000
["sortings"]
16
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\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 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": "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 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"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\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 = int(f.readline().strip()) a = list(map(int, f.readline().split())) a_sorted = sorted(a) # Read reference output with open(output_path) as f: ref_lines = [line.strip() for line in f] ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible') # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f] if not sub_lines: print(0) return sub_output = sub_lines[0].split() sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible') # Check if submission matches the reference's impossible status if ref_is_impossible: print(1 if sub_is_impossible else 0) return else: if sub_is_impossible: print(0) return # Check if the submission is a valid permutation and meets conditions if len(sub_output) != n: print(0) return try: sub_array = list(map(int, sub_output)) except: print(0) return if sorted(sub_array) != a_sorted: print(0) return # Check z-sort conditions valid = True for i in range(1, n): if (i + 1) % 2 == 0: # 1-based even index if sub_array[i] < sub_array[i - 1]: valid = False break else: # 1-based odd index > 1 if sub_array[i] > sub_array[i - 1]: valid = False break print(1 if valid else 0) if __name__ == "__main__": main()
true
651/B
651
B
Python 3
TESTS
5
62
0
109992992
n=int(input()) arr=sorted(list(map(int,input().split()))) t=0 i=0 j=0 while i<len(arr) and j<len(arr): if arr[i]<arr[j]: i+1 t+=1 j+=1 elif arr[i]==arr[j]: j+=1 print(t)
31
46
0
177094296
n = int(input()) a = list(map(int, input().split(" "))) c = {} for b in a: if b in c: c[b] += 1 else: c[b] = 1 occs = list(c.values()) print(sum(occs) - max(occs))
Codeforces Round 345 (Div. 2)
CF
2,016
1
256
Beautiful Paintings
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one. We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
null
In the first sample, the optimal order is: 10, 20, 30, 40, 50. In the second sample, the optimal order is: 100, 200, 100, 200.
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
1,200
["greedy", "sortings"]
31
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
false
stdio
null
true
489/A
489
A
PyPy 3-64
TESTS
5
46
0
201907969
size = int(input()) nums = list(map(int, input().split())) sortedNums = [] swaps = [] for i, num in enumerate(nums): nums[i] = [num, i] nums.sort() seen = set() ans = [] for i, num in enumerate(nums): if num[1] == i: continue if num[1] not in seen: ans.append([i, num[1]]) seen.add(num[1]) seen.add(i) else: break print(len(ans)) for a in ans: print(*a)
22
109
5,939,200
196159170
n = int(input()) a = list(map(int, input().split())) swaps = [] for i in range(n): mn_index = i for j in range(i + 1, n): if a[j] < a[mn_index]: mn_index = j if i != mn_index: swaps.append((i, mn_index)) a[mn_index], a[i] = a[i], a[mn_index] print(len(swaps)) for i, j in swaps: print(i, j)
Codeforces Round 277.5 (Div. 2)
CF
2,014
1
256
SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
null
null
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
1,200
["greedy", "implementation", "sortings"]
22
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) arr = list(map(int, f.readline().split())) with open(sub_path) as f: lines = f.readlines() if not lines: print(0) return try: k = int(lines[0].strip()) swaps = [] for line in lines[1:1 + k]: i, j = map(int, line.strip().split()) swaps.append((i, j)) except (ValueError, IndexError): print(0) return if not (0 <= k <= n): print(0) return for i, j in swaps: if not (0 <= i < n and 0 <= j < n): print(0) return current = arr.copy() for i, j in swaps: current[i], current[j] = current[j], current[i] sorted_arr = sorted(arr) if current == sorted_arr: print(1) else: print(0) if __name__ == "__main__": main()
true
985/C
985
C
Python 3
TESTS
7
202
7,168,000
38547825
(n, k, l) = [int(i) for i in input().split()] lengths = input().split() for i in range(n * k): lengths[i] = int(lengths[i]) lengths.sort() smallest = lengths[0] biggest = smallest + l stop = len(lengths) for i in range(len(lengths)): if lengths[i] > biggest: stop = i break if stop < n: print(0) else: pos = 0 i = 1 possible = stop ans = 0 todo = n while possible > todo: ans += lengths[pos] pos += k todo -= 1 possible -= k for i in range(todo): ans += lengths[pos] pos += 1 print(ans)
50
108
13,721,600
193545323
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### n,k,l = na() a = na() a = sorted(a) m = l + a[0] s = 0 ans = 0 for i in range(n*k-1,-1,-1): s += 1 if a[i] <= m and s >= k: ans += a[i] s -= k if s: print(0) else: print(ans)
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
2
256
Liebig's Barrels
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel. Let volume vj of barrel j be equal to the length of the minimal stave in it. You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n. Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109). The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
null
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3]. In the second example you can form the following barrels: [10], [10]. In the third example you can form the following barrels: [2, 5]. In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
1,500
["greedy"]
50
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
489/D
489
D
Python 3
TESTS
1
46
307,200
215824180
from collections import deque def calc(v, n, g): res = 0 dist = [(0, 0) for _ in range(n + 1)] dist[v] = (0, 0) q = deque() q.append(v) while q: v = q.popleft() for i in g[v]: if dist[v][0] == 0: dist[i] = (1, 0) q.append(i) else: dist[i] = (dist[i][0], dist[i][1] + 1) for i in range(1, n + 1): if i != v: res += dist[i][1] * (dist[i][1] - 1) // 2 return res if __name__ == "__main__": n, m = map(int, input().split()) g = [[] for _ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) g[u].append(v) ans = 0 for i in range(1, n + 1): ans += calc(i, n, g) print(ans)
31
967
86,528,000
216377649
import sys input = lambda: sys.stdin.readline().rstrip() from collections import defaultdict import math N,M = map(int, input().split()) P = [[] for _ in range(N)] for _ in range(M): a,b = map(int, input().split()) a-=1;b-=1 P[a].append(b) lib = defaultdict(int) def deal(idx): v = [(idx,-1,0)] while v: i,p,d = v.pop() if d==2: lib[(idx,i)]+=1 continue for j in P[i]: if j==p:continue v.append((j,i,d+1)) for i in range(N): deal(i) ans = 0 for v in lib.values(): if v>1: ans+=math.comb(v,2) print(ans)
Codeforces Round 277.5 (Div. 2)
CF
2,014
1
256
Unbearable Controversy of Being
Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different! Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c and d, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below: Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him. Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city. When rhombi are compared, the order of intersections b and d doesn't matter.
The first line of the input contains a pair of integers n, m (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000) — the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi) — the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions. It is not guaranteed that you can get from any intersection to any other one.
Print the required number of "damn rhombi".
null
null
[{"input": "5 4\n1 2\n2 3\n1 4\n4 3", "output": "1"}, {"input": "4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3", "output": "12"}]
1,700
["brute force", "combinatorics", "dfs and similar", "graphs"]
31
[{"input": "5 4\r\n1 2\r\n2 3\r\n1 4\r\n4 3\r\n", "output": "1\r\n"}, {"input": "4 12\r\n1 2\r\n1 3\r\n1 4\r\n2 1\r\n2 3\r\n2 4\r\n3 1\r\n3 2\r\n3 4\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "12\r\n"}, {"input": "1 0\r\n", "output": "0\r\n"}, {"input": "10 20\r\n6 10\r\n4 2\r\n1 5\r\n6 1\r\n8 9\r\n1 3\r\n2 6\r\n9 7\r\n4 5\r\n3 7\r\n9 2\r\n3 9\r\n4 8\r\n1 10\r\n6 9\r\n8 5\r\n7 6\r\n1 8\r\n8 10\r\n5 6\r\n", "output": "3\r\n"}, {"input": "3000 0\r\n", "output": "0\r\n"}, {"input": "1 0\r\n", "output": "0\r\n"}, {"input": "2 0\r\n", "output": "0\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "2 2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3 0\r\n", "output": "0\r\n"}, {"input": "3 6\r\n1 2\r\n1 3\r\n2 1\r\n2 3\r\n3 1\r\n3 2\r\n", "output": "0\r\n"}, {"input": "4 10\r\n1 2\r\n1 3\r\n1 4\r\n2 1\r\n2 3\r\n2 4\r\n3 1\r\n3 2\r\n3 4\r\n4 1\r\n", "output": "5\r\n"}, {"input": "4 9\r\n1 2\r\n1 4\r\n2 1\r\n2 3\r\n3 1\r\n3 2\r\n3 4\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}, {"input": "4 11\r\n1 2\r\n1 3\r\n1 4\r\n2 1\r\n2 4\r\n3 1\r\n3 2\r\n3 4\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "8\r\n"}, {"input": "5 20\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 1\r\n2 3\r\n2 4\r\n2 5\r\n3 1\r\n3 2\r\n3 4\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n4 5\r\n5 1\r\n5 2\r\n5 3\r\n5 4\r\n", "output": "60\r\n"}, {"input": "6 30\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n2 1\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n3 1\r\n3 2\r\n3 4\r\n3 5\r\n3 6\r\n4 1\r\n4 2\r\n4 3\r\n4 5\r\n4 6\r\n5 1\r\n5 2\r\n5 3\r\n5 4\r\n5 6\r\n6 1\r\n6 2\r\n6 3\r\n6 4\r\n6 5\r\n", "output": "180\r\n"}, {"input": "7 42\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n2 1\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n2 7\r\n3 1\r\n3 2\r\n3 4\r\n3 5\r\n3 6\r\n3 7\r\n4 1\r\n4 2\r\n4 3\r\n4 5\r\n4 6\r\n4 7\r\n5 1\r\n5 2\r\n5 3\r\n5 4\r\n5 6\r\n5 7\r\n6 1\r\n6 2\r\n6 3\r\n6 4\r\n6 5\r\n6 7\r\n7 1\r\n7 2\r\n7 3\r\n7 4\r\n7 5\r\n7 6\r\n", "output": "420\r\n"}, {"input": "8 56\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n2 1\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n2 7\r\n2 8\r\n3 1\r\n3 2\r\n3 4\r\n3 5\r\n3 6\r\n3 7\r\n3 8\r\n4 1\r\n4 2\r\n4 3\r\n4 5\r\n4 6\r\n4 7\r\n4 8\r\n5 1\r\n5 2\r\n5 3\r\n5 4\r\n5 6\r\n5 7\r\n5 8\r\n6 1\r\n6 2\r\n6 3\r\n6 4\r\n6 5\r\n6 7\r\n6 8\r\n7 1\r\n7 2\r\n7 3\r\n7 4\r\n7 5\r\n7 6\r\n7 8\r\n8 1\r\n8 2\r\n8 3\r\n8 4\r\n8 5\r\n8 6\r\n8 7\r\n", "output": "840\r\n"}, {"input": "5 10\r\n3 4\r\n4 3\r\n3 2\r\n5 1\r\n2 4\r\n1 4\r\n5 4\r\n5 3\r\n2 3\r\n3 1\r\n", "output": "2\r\n"}]
false
stdio
null
true
384/B
384
B
PyPy 3
TESTS
0
61
0
138639667
n, m, k = list(map(int,input().split())) itog = set() if k == 0: for _ in range(n): mass = list(map(int,input().split())) mass1 = mass[:] mass1.sort() for i in range(m): if mass[i] != mass1[i]: x = mass.index(mass1[i]) + 1 d = () if x >= i + 1: d = (i + 1, x) else: d = (x, i + 1) itog.add(d) else: for _ in range(n): mass = list(map(int,input().split())) mass1 = mass[:] mass1 = list(reversed(list(sorted(mass1)))) for i in range(m): if mass[i] != mass1[i]: x = mass.index(mass1[i]) + 1 d = () if x >= i + 1: d = (i + 1, x) else: d = (x, i + 1) itog.add(d) print(len(itog)) for i in itog: print(*i)
31
61
0
162726764
n, m, k = map(int, input().split()) print(m * (m - 1) // 2) for i in range(1, m): for j in range(i + 1, m + 1): if k == 0: print (i,j) else: print(j,i) # Mon Jul 04 2022 09:29:45 GMT+0000 (Coordinated Universal Time)
Codeforces Round 225 (Div. 2)
CF
2,014
1
256
Multitasking
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers. Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j. Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $$\frac{m(m-1)}{2}$$ (at most $$\frac{m(m-1)}{2}$$ pairs). Help Iahub, find any suitable array.
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
On the first line of the output print an integer p, the size of the array (p can be at most $$\frac{m(m-1)}{2}$$). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices. If there are multiple correct answers, you can print any.
null
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
[{"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5", "output": "3\n2 4\n2 3\n4 5"}, {"input": "3 2 1\n1 2\n2 3\n3 4", "output": "1\n2 1"}]
1,500
["greedy", "implementation", "sortings", "two pointers"]
31
[{"input": "2 5 0\r\n1 3 2 5 4\r\n1 4 3 2 5\r\n", "output": "3\r\n2 4\r\n2 3\r\n4 5\r\n"}, {"input": "3 2 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 5 0\r\n836096 600367 472071 200387 79763\r\n714679 505282 233544 157810 152591\r\n", "output": "10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 3\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n"}, {"input": "2 5 1\r\n331081 525217 574775 753333 840639\r\n225591 347017 538639 620341 994088\r\n", "output": "10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n3 2\r\n4 2\r\n5 2\r\n4 3\r\n5 3\r\n5 4\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 0\r\n1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2 1\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 2 0\r\n2 1\r\n3 1\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 0\r\n2 1\r\n1 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 1\r\n2 1\r\n3 1\r\n", "output": "1\r\n2 1\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, m, k = map(int, lines[0].split()) arrays = [list(map(int, line.split())) for line in lines[1:n+1]] try: with open(submission_path) as f: sub_lines = f.read().splitlines() except: print(0) return if not sub_lines: print(0) return try: p = int(sub_lines[0].strip()) if p < 0 or p > m * (m-1) // 2: print(0) return except: print(0) return swaps = [] for line in sub_lines[1:p+1]: parts = line.strip().split() if len(parts) != 2: print(0) return try: i = int(parts[0]) j = int(parts[1]) except: print(0) return if i < 1 or i > m or j < 1 or j > m or i == j: print(0) return swaps.append( (i-1, j-1) ) for arr in arrays: current = arr.copy() for i, j in swaps: if current[i] > current[j]: current[i], current[j] = current[j], current[i] valid = True if k == 0: for x in range(m-1): if current[x] > current[x+1]: valid = False break else: for x in range(m-1): if current[x] < current[x+1]: valid = False break if not valid: print(0) return print(1) if __name__ == "__main__": main()
true
877/E
877
E
Python 3
TESTS
6
108
307,200
53209586
class Node: def __init__(self): self.parent = None self.on = False self.children = [] self.count = -1 def toggle(self): self.on = not self.on count = 0 for child in self.children: child.toggle() count += child.count self.count = count+1 if self.on else count def calc_count(self): if self.count != -1: return self.count count = 0 for child in self.children: child.calc_count() count += child.count self.count = count+1 if self.on else count nodes = [] v = int(input()) for i in range(v): nodes.append(Node()) p = list(map(int, input().split())) for i in range(len(p)): nodes[i+1].parent = nodes[p[i]-1] nodes[p[i]-1].children.append(nodes[i+1]) t = list(map(int, input().split())) for i in range(len(t)): nodes[i].on = t[i] == 1 for i in range(v): nodes[i].calc_count() q = int(input()) for i in range(q): command, v = input().split() v = int(v) - 1 if command == 'pow': nodes[v].toggle() if command == 'get': print(nodes[v].count)
57
1,902
63,692,800
104860740
class LazySegTree: def __init__(self, init_val, seg_ide, lazy_ide, f, g, h): self.n = len(init_val) self.num = 2**(self.n-1).bit_length() self.seg_ide = seg_ide self.lazy_ide = lazy_ide self.f = f #(seg, seg) -> seg self.g = g #(seg, lazy, size) -> seg self.h = h #(lazy, lazy) -> lazy self.seg = [seg_ide]*2*self.num for i in range(self.n): self.seg[i+self.num] = init_val[i] for i in range(self.num-1, 0, -1): self.seg[i] = self.f(self.seg[2*i], self.seg[2*i+1]) self.size = [0]*2*self.num for i in range(self.n): self.size[i+self.num] = 1 for i in range(self.num-1, 0, -1): self.size[i] = self.size[2*i] + self.size[2*i+1] self.lazy = [lazy_ide]*2*self.num def update(self, i, x): i += self.num self.seg[i] = x while i: i = i >> 1 self.seg[i] = self.f(self.seg[2*i], self.seg[2*i+1]) def calc(self, i): return self.g(self.seg[i], self.lazy[i], self.size[i]) def calc_above(self, i): i = i >> 1 while i: self.seg[i] = self.f(self.calc(2*i), self.calc(2*i+1)) i = i >> 1 def propagate(self, i): self.seg[i] = self.g(self.seg[i], self.lazy[i], self.size[i]) self.lazy[2*i] = self.h(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.h(self.lazy[2*i+1], self.lazy[i]) self.lazy[i] = self.lazy_ide def propagate_above(self, i): H = i.bit_length() for h in range(H, 0, -1): self.propagate(i >> h) def query(self, l, r): l += self.num r += self.num lm = l // (l & -l) rm = r // (r & -r) -1 self.propagate_above(lm) self.propagate_above(rm) al = self.seg_ide ar = self.seg_ide while l < r: if l & 1: al = self.f(al, self.calc(l)) l += 1 if r & 1: r -= 1 ar = self.f(self.calc(r), ar) l = l >> 1 r = r >> 1 return self.f(al, ar) def oprerate_range(self, l, r, a): l += self.num r += self.num lm = l // (l & -l) rm = r // (r & -r) -1 self.propagate_above(lm) self.propagate_above(rm) while l < r: if l & 1: self.lazy[l] = self.h(self.lazy[l], a) l += 1 if r & 1: r -= 1 self.lazy[r] = self.h(self.lazy[r], a) l = l >> 1 r = r >> 1 self.calc_above(lm) self.calc_above(rm) f = lambda x, y: x+y g = lambda x, a, s: s-x if a%2 == 1 else x h = lambda a, b: a+b def EulerTour(g, root): n = len(g) root = root g = g tank = [root] eulerTour = [] left = [0]*n right = [-1]*n depth = [-1]*n parent = [-1]*n child = [[] for i in range(n)] eulerNum = -1 de = -1 while tank: v = tank.pop() if v >= 0: eulerNum += 1 eulerTour.append(v) left[v] = eulerNum right[v] = eulerNum tank.append(~v) de += 1 depth[v] = de for u in g[v]: if parent[v] == u: continue tank.append(u) parent[u] = v child[v].append(u) else: de -= 1 if ~v != root: eulerTour.append(parent[~v]) eulerNum += 1 right[parent[~v]] = eulerNum return eulerTour, left, right import bisect import sys import io, os input = sys.stdin.readline #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def main(): n = int(input()) P = list(map(int, input().split())) T = list(map(int, input().split())) P = [p-1 for p in P] P = [-1]+P edge = [[] for i in range(n)] for i, p in enumerate(P): if p != -1: edge[p].append(i) et, left, right = EulerTour(edge, 0) d = {} B = sorted(left) #print(B) #print(right) for i, b in enumerate(B): d[b] = i A = [0]*n for i in range(n): A[d[left[i]]] = T[i] seg = LazySegTree(A, 0, 0, f, g, h) q = int(input()) for i in range(q): query = list(map(str, input().split())) if query[0] == 'get': u = int(query[1])-1 l = d[left[u]] r = bisect.bisect_right(B, right[u]) print(seg.query(l, r)) else: u = int(query[1])-1 l = d[left[u]] r = bisect.bisect_right(B, right[u]) seg.oprerate_range(l, r, 1) if __name__ == '__main__': main()
Codeforces Round 442 (Div. 2)
CF
2,017
2
256
Danil and a Part-time Job
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on. Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages. There are two types of tasks: 1. pow v describes a task to switch lights in the subtree of vertex v. 2. get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages. A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v. Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i. The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and 0 otherwise. The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks. The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
null
The tree before the task pow 1. The tree after the task pow 1.
[{"input": "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4", "output": "2\n0\n0\n1\n2\n1\n1\n0"}]
2,000
["bitmasks", "data structures", "trees"]
57
[{"input": "4\r\n1 1 1\r\n1 0 0 1\r\n9\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\npow 1\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\n", "output": "2\r\n0\r\n0\r\n1\r\n2\r\n1\r\n1\r\n0\r\n"}, {"input": "1\r\n\r\n1\r\n4\r\npow 1\r\nget 1\r\npow 1\r\nget 1\r\n", "output": "0\r\n1\r\n"}, {"input": "10\r\n1 2 1 3 4 5 6 8 5\r\n1 0 0 0 1 0 0 0 1 0\r\n10\r\npow 10\r\npow 4\r\npow 10\r\npow 7\r\npow 10\r\npow 10\r\npow 4\r\npow 8\r\npow 7\r\npow 1\r\n", "output": ""}, {"input": "10\r\n1 2 3 4 2 4 1 7 8\r\n1 1 0 1 1 0 0 0 1 1\r\n10\r\npow 1\r\nget 2\r\npow 2\r\npow 8\r\nget 6\r\npow 6\r\npow 10\r\nget 6\r\npow 8\r\npow 3\r\n", "output": "3\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 1 4 5 3 5 6 3\r\n0 0 0 0 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 4\r\nget 7\r\nget 3\r\npow 2\r\npow 5\r\npow 2\r\nget 7\r\npow 6\r\nget 10\r\n", "output": "0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n"}, {"input": "10\r\n1 1 3 1 3 1 4 6 3\r\n0 1 1 1 1 1 1 1 0 0\r\n10\r\nget 9\r\nget 10\r\nget 4\r\nget 5\r\nget 5\r\nget 5\r\nget 10\r\nget 7\r\nget 5\r\nget 2\r\n", "output": "0\r\n0\r\n2\r\n1\r\n1\r\n1\r\n0\r\n1\r\n1\r\n1\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 8\r\n0 0 0 0 1 1 1 1 0 0\r\n10\r\npow 3\r\nget 1\r\npow 9\r\nget 1\r\nget 1\r\nget 8\r\npow 8\r\npow 4\r\nget 10\r\npow 2\r\n", "output": "4\r\n3\r\n3\r\n1\r\n0\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 9\r\n1 1 0 1 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 6\r\nget 4\r\nget 2\r\nget 1\r\nget 2\r\nget 6\r\nget 9\r\nget 10\r\nget 7\r\n", "output": "3\r\n0\r\n1\r\n3\r\n4\r\n3\r\n0\r\n0\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 3 3 5 5 7 7 9\r\n0 0 1 1 0 0 1 1 0 0\r\n10\r\npow 8\r\npow 4\r\npow 8\r\npow 5\r\npow 1\r\npow 2\r\npow 6\r\npow 6\r\npow 3\r\npow 2\r\n", "output": ""}, {"input": "10\r\n1 1 2 2 3 3 5 5 6\r\n1 1 1 1 0 0 1 1 0 0\r\n10\r\nget 2\r\nget 8\r\nget 10\r\nget 5\r\nget 5\r\npow 10\r\nget 10\r\nget 1\r\nget 7\r\npow 4\r\n", "output": "3\r\n1\r\n0\r\n1\r\n1\r\n1\r\n7\r\n1\r\n"}, {"input": "10\r\n1 1 2 2 3 3 4 4 5\r\n1 1 0 1 0 0 0 0 0 0\r\n10\r\nget 2\r\nget 5\r\npow 2\r\npow 4\r\nget 2\r\nget 4\r\npow 7\r\nget 10\r\npow 5\r\nget 6\r\n", "output": "2\r\n0\r\n3\r\n1\r\n1\r\n0\r\n"}]
false
stdio
null
true
985/C
985
C
Python 3
TESTS
7
249
7,168,000
38547475
(n, k, l) = [int(i) for i in input().split()] lengths = input().split() for i in range(n * k): lengths[i] = int(lengths[i]) smallest = min(lengths) biggest = smallest + l M = 0 for i in lengths: if smallest <= i <= biggest: M += 1 ans = 0 if M < n: print(ans) else: if k != 1: n1 = (M - n) // (k - 1) n2 = n - n1 else: n1 = n n2 = 0 import heapq heapq.heapify(lengths) for i in range(n1): size = heapq.heappop(lengths) ans += size for i in range(k - 1): heapq.heappop(lengths) for i in range(n2): size = heapq.heappop(lengths) ans += size print(ans)
50
109
14,540,800
229431587
n, k, l = map(int, input().split()) a = [0] a += list(map(int, input().split())) a.sort() mb = [] ans = 0 last = 1 for i in range(2, n * k + 1): if a[i] - a[1] <= l: last = i if last < n: print(0) else: t = (n - 1) * k + 1 for i in range(last, last - (n - 1), -1): ans += a[min(i, t)] t -= k ans += a[1] print(ans)# 1698068168.8804967
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
2
256
Liebig's Barrels
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel. Let volume vj of barrel j be equal to the length of the minimal stave in it. You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n. Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109). The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
null
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3]. In the second example you can form the following barrels: [10], [10]. In the third example you can form the following barrels: [2, 5]. In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
1,500
["greedy"]
50
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
489/A
489
A
PyPy 3-64
TESTS
0
31
0
214606231
def swap_sort(arr): n = len(arr) swaps = [] for i in range(n): for j in range(n - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] swaps.append((j, j + 1)) return swaps n = int(input()) arr = list(map(int, input().split())) swaps = swap_sort(arr) print(len(swaps)) for swap in swaps: print(swap[0], swap[1])
22
124
3,993,600
219501482
n = int(input()) a = list(map(int,input().split())) d = sorted(a) x = [] c = 0 for i in range(n - 1): if a[i] == d[i]: continue for j in range(i + 1,n): if a[j] == d[i]: a[j] = a[i] x.append([i, j]) c = c + 1 break print(c) for i in x: print(*i)
Codeforces Round 277.5 (Div. 2)
CF
2,014
1
256
SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
null
null
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
1,200
["greedy", "implementation", "sortings"]
22
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) arr = list(map(int, f.readline().split())) with open(sub_path) as f: lines = f.readlines() if not lines: print(0) return try: k = int(lines[0].strip()) swaps = [] for line in lines[1:1 + k]: i, j = map(int, line.strip().split()) swaps.append((i, j)) except (ValueError, IndexError): print(0) return if not (0 <= k <= n): print(0) return for i, j in swaps: if not (0 <= i < n and 0 <= j < n): print(0) return current = arr.copy() for i, j in swaps: current[i], current[j] = current[j], current[i] sorted_arr = sorted(arr) if current == sorted_arr: print(1) else: print(0) if __name__ == "__main__": main()
true
989/D
989
D
Python 3
TESTS
0
61
0
39177263
from bisect import bisect_left, bisect_right n, l, w = [int(item) for item in input().split()] clouds = [] for i in range(n): clouds.append([int(item) for item in input().split()]) answer = 0 m_array = sorted([item[0] for item in clouds if item[1] == -1]) p_array = sorted([item[0] for item in clouds if item[1] == 1]) # print(m_array) # print(p_array) for xp in p_array: mn = -float("inf") if w != 1: mn = max(xp - 2 * (xp + l) / (w - 1), mn) else: mn = float("inf") if xp >= 0 else mn mn = max(xp - 2 * (xp + l) / (w + 1), mn) # mn = max(xp - l - 2 * xp / (w + 1), xp - l) # if (w != 1): # mn = max(xp - l + 2 * (xp + l) / (w - 1), mn) # else: # mn = float("inf") if (l + xp) > 0 else mn mn = max(mn, xp - l) #print(mn) last = bisect_right(m_array, mn) answer += len(m_array) - last print(answer)
30
841
6,860,800
39193022
# Codeforces Round #487 (Div. 2)import collections from functools import cmp_to_key #key=cmp_to_key(lambda x,y: 1 if x not in y else -1 ) import sys def getIntList(): return list(map(int, input().split())) import bisect N,L,WM = getIntList() z = {} z[-1] = {1:[], -1:[]} z[0] = {1:[], -1:[]} z[1] = {1:[], -1:[]} for i in range(N): x0,v = getIntList() t = (x0,v) if x0+L <=0: z[-1][v].append(t) elif x0>=0: z[1][v].append(t) else: z[0][v].append(t) res = 0 res += len(z[-1][1] ) * len(z[ 1][-1] ) res += len(z[0][1] ) * len(z[ 1][-1] ) res += len(z[-1][1] ) * len(z[ 0][-1] ) if WM==1: print(res) sys.exit() z[1][-1].sort() z[-1][1].sort() #print(z[-1][1]) tn = len(z[1][-1]) for t in z[1][1]: g = (-WM-1) * t[0] / (-WM+1) - L g = max(g, t[0]+ 0.5) p = bisect.bisect_right(z[1][-1], (g,2) ) res += tn-p tn = len(z[-1][1]) for t in z[-1][-1]: g = (WM+1) * (t[0] + L)/ (WM-1) g = min(g, t[0] - 0.1) p = bisect.bisect_left(z[-1][1], (g,-2) ) res += p print(res)
Codeforces Round 487 (Div. 2)
CF
2,018
2
256
A Shade of Moonlight
The sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is $$$0$$$. There are $$$n$$$ clouds floating in the sky. Each cloud has the same length $$$l$$$. The $$$i$$$-th initially covers the range of $$$(x_i, x_i + l)$$$ (endpoints excluded). Initially, it moves at a velocity of $$$v_i$$$, which equals either $$$1$$$ or $$$-1$$$. Furthermore, no pair of clouds intersect initially, that is, for all $$$1 \leq i \lt j \leq n$$$, $$$\lvert x_i - x_j \rvert \geq l$$$. With a wind velocity of $$$w$$$, the velocity of the $$$i$$$-th cloud becomes $$$v_i + w$$$. That is, its coordinate increases by $$$v_i + w$$$ during each unit of time. Note that the wind can be strong and clouds can change their direction. You are to help Mino count the number of pairs $$$(i, j)$$$ ($$$i < j$$$), such that with a proper choice of wind velocity $$$w$$$ not exceeding $$$w_\mathrm{max}$$$ in absolute value (possibly negative and/or fractional), the $$$i$$$-th and $$$j$$$-th clouds both cover the moon at the same future moment. This $$$w$$$ doesn't need to be the same across different pairs.
The first line contains three space-separated integers $$$n$$$, $$$l$$$, and $$$w_\mathrm{max}$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq l, w_\mathrm{max} \leq 10^8$$$) — the number of clouds, the length of each cloud and the maximum wind speed, respectively. The $$$i$$$-th of the following $$$n$$$ lines contains two space-separated integers $$$x_i$$$ and $$$v_i$$$ ($$$-10^8 \leq x_i \leq 10^8$$$, $$$v_i \in \{-1, 1\}$$$) — the initial position and the velocity of the $$$i$$$-th cloud, respectively. The input guarantees that for all $$$1 \leq i \lt j \leq n$$$, $$$\lvert x_i - x_j \rvert \geq l$$$.
Output one integer — the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity $$$w$$$.
null
In the first example, the initial positions and velocities of clouds are illustrated below. The pairs are: - $$$(1, 3)$$$, covering the moon at time $$$2.5$$$ with $$$w = -0.4$$$; - $$$(1, 4)$$$, covering the moon at time $$$3.5$$$ with $$$w = -0.6$$$; - $$$(1, 5)$$$, covering the moon at time $$$4.5$$$ with $$$w = -0.7$$$; - $$$(2, 5)$$$, covering the moon at time $$$2.5$$$ with $$$w = -2$$$. Below is the positions of clouds at time $$$2.5$$$ with $$$w = -0.4$$$. At this moment, the $$$1$$$-st and $$$3$$$-rd clouds both cover the moon. In the second example, the only pair is $$$(1, 4)$$$, covering the moon at time $$$15$$$ with $$$w = 0$$$. Note that all the times and wind velocities given above are just examples among infinitely many choices.
[{"input": "5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1", "output": "4"}, {"input": "4 10 1\n-20 1\n-10 -1\n0 1\n10 -1", "output": "1"}]
2,500
["binary search", "geometry", "math", "sortings", "two pointers"]
30
[{"input": "5 1 2\r\n-2 1\r\n2 1\r\n3 -1\r\n5 -1\r\n7 -1\r\n", "output": "4\r\n"}, {"input": "4 10 1\r\n-20 1\r\n-10 -1\r\n0 1\r\n10 -1\r\n", "output": "1\r\n"}, {"input": "1 100000000 98765432\r\n73740702 1\r\n", "output": "0\r\n"}, {"input": "10 2 3\r\n-1 -1\r\n-4 1\r\n-6 -1\r\n1 1\r\n10 -1\r\n-8 -1\r\n6 1\r\n8 1\r\n4 -1\r\n-10 -1\r\n", "output": "5\r\n"}, {"input": "3 100000000 100000000\r\n-100000000 1\r\n100000000 1\r\n0 -1\r\n", "output": "1\r\n"}, {"input": "9 25000000 989\r\n-100000000 -1\r\n-75000000 1\r\n75000000 1\r\n50000000 -1\r\n-50000000 1\r\n0 1\r\n25000000 1\r\n-25000000 -1\r\n100000000 -1\r\n", "output": "11\r\n"}, {"input": "2 5 1\r\n-2 1\r\n5 -1\r\n", "output": "1\r\n"}, {"input": "2 5 1\r\n-9 -1\r\n-2 1\r\n", "output": "0\r\n"}, {"input": "3 4 5\r\n9 1\r\n-4 1\r\n-8 -1\r\n", "output": "0\r\n"}, {"input": "5 1 1\r\n-6 1\r\n15 1\r\n-7 1\r\n-13 -1\r\n12 -1\r\n", "output": "2\r\n"}, {"input": "50 1 19\r\n-5213 -1\r\n2021 -1\r\n-4479 1\r\n1569 -1\r\n1618 1\r\n-8318 1\r\n3854 1\r\n8190 -1\r\n9162 1\r\n8849 1\r\n-5545 -1\r\n-7898 -1\r\n728 1\r\n-2175 -1\r\n6453 -1\r\n2999 1\r\n4716 1\r\n-2192 -1\r\n7938 -1\r\n1910 -1\r\n-6863 -1\r\n5230 -1\r\n-2782 -1\r\n-2587 -1\r\n-3389 1\r\n-332 -1\r\n5915 1\r\n-2604 1\r\n-8907 1\r\n-2019 1\r\n2992 1\r\n-3279 -1\r\n6720 1\r\n4332 1\r\n8789 -1\r\n2003 1\r\n-8046 -1\r\n-594 -1\r\n-4133 -1\r\n-7954 -1\r\n-6270 -1\r\n4042 -1\r\n3650 1\r\n-8569 1\r\n2529 -1\r\n266 -1\r\n3405 -1\r\n-9753 1\r\n1205 -1\r\n6437 -1\r\n", "output": "262\r\n"}, {"input": "50 100 40\r\n4843 -1\r\n7653 1\r\n5391 1\r\n-1651 1\r\n-8530 -1\r\n9770 1\r\n2721 1\r\n7321 1\r\n-3636 -1\r\n-1525 -1\r\n-3060 1\r\n1877 -1\r\n3771 -1\r\n-7651 1\r\n581 -1\r\n1127 -1\r\n6966 -1\r\n-6089 1\r\n1465 -1\r\n3147 -1\r\n-6927 -1\r\n4477 1\r\n-6535 1\r\n5991 -1\r\n-2740 1\r\n5021 1\r\n-7761 -1\r\n4626 1\r\n9958 1\r\n4275 1\r\n5695 1\r\n8835 -1\r\n7791 -1\r\n189 -1\r\n-170 1\r\n-4468 -1\r\n-708 1\r\n34 -1\r\n-9068 1\r\n6424 -1\r\n-2066 -1\r\n-7367 1\r\n6224 1\r\n3329 1\r\n-1809 -1\r\n7105 1\r\n-4607 -1\r\n-3174 -1\r\n-9782 -1\r\n1350 -1\r\n", "output": "253\r\n"}]
false
stdio
null
true
652/B
652
B
Python 3
TESTS
5
93
0
41616536
x = int(input()) L = [i for i in input().split()] L.sort() L2 = [] for i in range(len(L)): if i%2 == 0: L2.append(L[0]) L.pop(0) else: L2.append(L[-1]) L.pop(-1) S = " ".join(L2) print(S)
16
31
0
212178239
itr = int(input()) l=sorted(list(map(int,input().split()))) k=len(l) while len(l): if k%2==0: print(l[0],l[-1],end=' ') del(l[0]) del(l[-1]) else: if len(l)>2: print(l[0],l[-1],end=' ') del(l[0]) del(l[-1]) else: print(l[0],end=' ') del(l[0])
Educational Codeforces Round 10
ICPC
2,016
1
256
z-sort
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: 1. ai ≥ ai - 1 for all even i, 2. ai ≤ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted. Can you make the array z-sorted?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
null
null
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
1,000
["sortings"]
16
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\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 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": "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 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"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\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 = int(f.readline().strip()) a = list(map(int, f.readline().split())) a_sorted = sorted(a) # Read reference output with open(output_path) as f: ref_lines = [line.strip() for line in f] ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible') # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f] if not sub_lines: print(0) return sub_output = sub_lines[0].split() sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible') # Check if submission matches the reference's impossible status if ref_is_impossible: print(1 if sub_is_impossible else 0) return else: if sub_is_impossible: print(0) return # Check if the submission is a valid permutation and meets conditions if len(sub_output) != n: print(0) return try: sub_array = list(map(int, sub_output)) except: print(0) return if sorted(sub_array) != a_sorted: print(0) return # Check z-sort conditions valid = True for i in range(1, n): if (i + 1) % 2 == 0: # 1-based even index if sub_array[i] < sub_array[i - 1]: valid = False break else: # 1-based odd index > 1 if sub_array[i] > sub_array[i - 1]: valid = False break print(1 if valid else 0) if __name__ == "__main__": main()
true
741/C
741
C
Python 3
TESTS
1
31
102,400
232515056
# LUOGU_RID: 134829911 def read_input(): """ Read the input from the user. Returns: - n: The number of pairs of guests. - pairs: A list of pairs of integers, where each pair represents the chairs on which a boy and his girlfriend are sitting. """ n = int(input()) pairs = [list(map(int, input().split())) for _ in range(n)] return n, pairs def assign_food(n, pairs): """ Assign food to each guest. Parameters: - n: The number of pairs of guests. - pairs: A list of pairs of integers, where each pair represents the chairs on which a boy and his girlfriend are sitting. Returns: - food: A list of integers, where each integer represents the type of food assigned to the guest sitting on the corresponding chair. """ if n == 1: return [0,1,2] food = [0] * (2*n + 1) inverse = [0] * (2*n + 1) tag =[[0,0,0] for i in range(2*n+5)] f = [0] * (2*n+1) for i in pairs: f[i[0]]=i[1] f[i[1]]=i[0] def friend(ind): return f[ind] # for i,j in enumerate(pairs): # inverse[j[0]] = i # inverse[j[1]] = i # def friend(ind): # pair_ind = inverse[ind] # if pairs[pair_ind][0] != ind: # return pairs[pair_ind][0] # else: # return pairs[pair_ind][1] def serve_ppp(ind, previous_2, previous_1): if ind == 2*n + 1: return not ((food[2*n] == food[1] and food[1] == food[2]) or (food[2*n] == food[1] and food[1] == food[2*n - 1])) if food[ind]: if previous_1 == previous_2 and previous_1 == food[ind]: return False else: return serve(ind+1,previous_1,food[ind]) if previous_2 == previous_1: if previous_1 == 1: food[ind] = 2 else: food[ind] = 1 food[friend(ind)] = previous_1 if serve(ind+1, previous_1, food[ind]): return True else: food[ind] = 0 return False else: food[ind] = 1 food[friend(ind)] = 2 if serve(ind+1, previous_1, 1): return True food[ind] = 2 food[friend(ind)] = 1 if serve(ind+1, previous_1, 2): return True else: food[ind] = 0 return False def serve(ind, previous_2, previous_1): if ind == 2*n + 1: return not ((food[2*n] == food[1] and food[1] == food[2]) or (food[2*n] == food[1] and food[1] == food[2*n - 1])) for i in range(1,3): if tag[ind][i]: continue food[ind] = i tag[friend(ind)][i]+=1 if previous_1==i: tag[ind+1][i]+=1 if serve(ind+1,previous_1,i): return True tag[friend(ind)][i]-=1 if previous_1==i: tag[ind+1][i]-=1 return False if serve_ppp(1,0,-1): return food else: return False def print_food(pairs, food): """ Print the type of food assigned to each pair of guests. Parameters: - pairs: A list of pairs of integers, where each pair represents the chairs on which a boy and his girlfriend are sitting. - food: A list of integers, where each integer represents the type of food assigned to the guest sitting on the corresponding chair. """ for a, b in pairs: print(food[a], food[b]) if __name__ == "__main__": n, pairs = read_input() food = assign_food(n, pairs) if food == False: print(-1) else: print_food(pairs, food)
94
717
9,523,200
23044496
import sys n = int(input()) A = [0]*(2*n) B = [] for line in sys.stdin: x, y = [int(x)-1 for x in line.split()] A[x] = y A[y] = x B.append(x) C = [0]*(2*n) for i in range(2*n): while not C[i]: C[i] = 1 C[i^1] = 2 i = A[i^1] for x in B: print(C[x], C[A[x]])
Codeforces Round 383 (Div. 1)
CF
2,016
1
256
Arpa’s overnight party and Mehrdad’s silent entering
Note that girls in Arpa’s land are really attractive. Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs were numbered 1 through 2n in clockwise direction. There was exactly one person sitting on each chair. There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that: - Each person had exactly one type of food, - No boy had the same type of food as his girlfriend, - Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive. Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.
The first line contains an integer n (1 ≤ n ≤ 105) — the number of pairs of guests. The i-th of the next n lines contains a pair of integers ai and bi (1 ≤ ai, bi ≤ 2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.
If there is no solution, print -1. Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2. If there are multiple solutions, print any of them.
null
null
[{"input": "3\n1 4\n2 5\n3 6", "output": "1 2\n2 1\n1 2"}]
2,600
["constructive algorithms", "dfs and similar", "graphs"]
94
[{"input": "3\r\n1 4\r\n2 5\r\n3 6\r\n", "output": "1 2\r\n2 1\r\n1 2\r\n"}, {"input": "6\r\n3 2\r\n5 11\r\n7 12\r\n6 9\r\n8 4\r\n1 10\r\n", "output": "1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "19\r\n30 27\r\n6 38\r\n10 28\r\n20 5\r\n14 18\r\n32 2\r\n36 29\r\n12 1\r\n31 24\r\n15 4\r\n35 11\r\n3 7\r\n21 17\r\n25 19\r\n16 8\r\n23 22\r\n37 33\r\n13 9\r\n34 26\r\n", "output": "1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n"}, {"input": "4\r\n4 2\r\n6 8\r\n5 1\r\n3 7\r\n", "output": "1 2\r\n1 2\r\n2 1\r\n2 1\r\n"}, {"input": "17\r\n11 12\r\n17 22\r\n34 7\r\n3 1\r\n5 24\r\n18 20\r\n27 30\r\n16 33\r\n23 21\r\n19 4\r\n2 15\r\n29 28\r\n9 8\r\n13 25\r\n6 10\r\n32 26\r\n31 14\r\n", "output": "1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n"}, {"input": "19\r\n10 7\r\n9 17\r\n21 30\r\n36 8\r\n14 11\r\n25 24\r\n1 23\r\n38 33\r\n4 20\r\n3 37\r\n27 5\r\n28 19\r\n22 2\r\n6 34\r\n12 15\r\n31 32\r\n35 13\r\n16 29\r\n18 26\r\n", "output": "1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n"}, {"input": "17\r\n17 31\r\n11 23\r\n34 22\r\n24 8\r\n4 1\r\n7 14\r\n20 27\r\n3 19\r\n12 26\r\n32 25\r\n28 18\r\n16 29\r\n21 9\r\n6 2\r\n33 30\r\n5 13\r\n10 15\r\n", "output": "1 2\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n"}, {"input": "6\r\n2 7\r\n5 9\r\n12 8\r\n1 4\r\n3 6\r\n10 11\r\n", "output": "2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n"}, {"input": "8\r\n10 3\r\n2 16\r\n14 13\r\n5 15\r\n1 7\r\n11 8\r\n6 4\r\n12 9\r\n", "output": "1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n"}, {"input": "4\r\n2 8\r\n3 5\r\n4 7\r\n1 6\r\n", "output": "2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "2\r\n2 3\r\n1 4\r\n", "output": "2 1\r\n1 2\r\n"}, {"input": "15\r\n16 22\r\n4 17\r\n27 3\r\n23 24\r\n18 20\r\n15 21\r\n9 7\r\n2 28\r\n29 19\r\n8 30\r\n14 10\r\n6 26\r\n25 11\r\n12 1\r\n13 5\r\n", "output": "2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n"}, {"input": "10\r\n19 6\r\n8 2\r\n15 18\r\n17 14\r\n16 7\r\n20 10\r\n5 1\r\n13 3\r\n9 12\r\n11 4\r\n", "output": "2 1\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "9\r\n12 7\r\n10 15\r\n16 14\r\n2 4\r\n1 17\r\n6 9\r\n8 3\r\n13 5\r\n11 18\r\n", "output": "1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n2 1\r\n"}, {"input": "7\r\n3 14\r\n7 4\r\n13 10\r\n11 8\r\n6 1\r\n5 9\r\n2 12\r\n", "output": "2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n"}, {"input": "6\r\n2 11\r\n7 1\r\n12 8\r\n4 10\r\n3 9\r\n5 6\r\n", "output": "2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "8\r\n13 6\r\n10 5\r\n1 12\r\n11 15\r\n7 16\r\n4 14\r\n9 2\r\n8 3\r\n", "output": "1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n"}, {"input": "8\r\n16 5\r\n10 15\r\n8 11\r\n2 14\r\n6 4\r\n7 3\r\n1 13\r\n9 12\r\n", "output": "1 2\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n"}, {"input": "7\r\n10 14\r\n4 6\r\n1 11\r\n7 2\r\n9 8\r\n5 13\r\n3 12\r\n", "output": "2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n"}, {"input": "5\r\n2 5\r\n10 9\r\n1 6\r\n3 8\r\n4 7\r\n", "output": "2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n"}, {"input": "8\r\n14 2\r\n7 9\r\n15 6\r\n13 11\r\n12 16\r\n10 5\r\n8 1\r\n3 4\r\n", "output": "1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n"}, {"input": "5\r\n4 6\r\n5 1\r\n2 3\r\n7 8\r\n9 10\r\n", "output": "2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "23\r\n46 21\r\n17 3\r\n27 38\r\n34 43\r\n7 6\r\n8 37\r\n22 4\r\n16 42\r\n36 32\r\n12 9\r\n10 45\r\n26 2\r\n13 24\r\n23 29\r\n18 15\r\n33 30\r\n31 5\r\n11 25\r\n1 14\r\n44 39\r\n19 20\r\n35 28\r\n41 40\r\n", "output": "2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n"}, {"input": "26\r\n8 10\r\n52 21\r\n2 33\r\n18 34\r\n30 51\r\n5 19\r\n22 32\r\n36 28\r\n42 16\r\n13 49\r\n11 17\r\n31 39\r\n43 37\r\n50 15\r\n29 20\r\n35 46\r\n47 23\r\n3 1\r\n44 7\r\n9 27\r\n6 48\r\n40 24\r\n26 14\r\n45 4\r\n12 25\r\n41 38\r\n", "output": "2 1\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n"}, {"input": "20\r\n34 12\r\n9 6\r\n5 3\r\n13 26\r\n18 15\r\n16 22\r\n7 14\r\n17 37\r\n38 40\r\n4 2\r\n11 23\r\n21 8\r\n10 36\r\n30 33\r\n28 19\r\n29 31\r\n39 20\r\n35 24\r\n25 32\r\n1 27\r\n", "output": "1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "17\r\n3 14\r\n34 22\r\n24 9\r\n16 17\r\n6 30\r\n33 12\r\n5 10\r\n21 8\r\n32 2\r\n26 23\r\n31 27\r\n19 15\r\n29 4\r\n7 18\r\n25 13\r\n20 28\r\n1 11\r\n", "output": "1 2\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n"}, {"input": "24\r\n30 4\r\n41 1\r\n2 11\r\n22 42\r\n29 43\r\n7 14\r\n16 6\r\n40 5\r\n27 34\r\n46 33\r\n17 10\r\n21 39\r\n28 31\r\n19 32\r\n23 20\r\n25 48\r\n12 9\r\n47 37\r\n38 3\r\n44 8\r\n36 18\r\n13 26\r\n24 15\r\n45 35\r\n", "output": "1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n2 1\r\n2 1\r\n1 2\r\n"}, {"input": "15\r\n21 14\r\n25 5\r\n7 28\r\n2 6\r\n8 27\r\n29 18\r\n9 15\r\n4 26\r\n12 1\r\n19 16\r\n17 20\r\n24 10\r\n11 23\r\n13 22\r\n30 3\r\n", "output": "1 2\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input with open(input_path) as f: n = int(f.readline()) pairs = [tuple(map(int, f.readline().split())) for _ in range(n)] # Read reference output with open(output_path) as f: ref_lines = [line.strip() for line in f.readlines()] # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f.readlines()] # Check if reference is -1 if ref_lines and ref_lines[0].strip() == '-1': if len(sub_lines) != 1 or sub_lines[0].strip() != '-1': print(0) return else: print(1) return else: if len(sub_lines) == 1 and sub_lines[0].strip() == '-1': print(0) return if len(sub_lines) != n: print(0) return chair_food = {} for i in range(n): line = sub_lines[i] parts = line.split() if len(parts) != 2: print(0) return try: a, b = map(int, parts) except: print(0) return if a not in (1, 2) or b not in (1, 2) or a == b: print(0) return ai, bi = pairs[i] chair_food[ai] = a chair_food[bi] = b all_chairs = set() for a, b in pairs: all_chairs.add(a) all_chairs.add(b) for chair in all_chairs: if chair not in chair_food: print(0) return for i in range(1, 2 * n + 1): next_i = i + 1 if i < 2 * n else 1 nextnext_i = next_i + 1 if next_i < 2 * n else 1 a = chair_food.get(i, 0) b = chair_food.get(next_i, 0) c = chair_food.get(nextnext_i, 0) if a == b == c: print(0) return print(1) 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
384/B
384
B
PyPy 3-64
TESTS
2
62
1,638,400
187169642
n,m,k= map(int,input().split()) b=[] c=[] for i in range(n): a=list(map(int,input().split())) b.append(a) if not k: for i in range(1,m+1): for j in range(i+1,m+1): for t in range(n): if any(b[t][i-1]>b[t][j-1]for t in range(n)): c.append([i,j]) else: for i in range(m,0,-1): for j in range(i-1,0,-1): if any(b[t][i-1]>b[t][j-1]for t in range(n)): c.append([i,j]) ## ##d=[] ##print(c) ##for i in c: ## if i in d: ## c.remove(i) ## d.append(i) print(len(c)) for i in c: print(*i)
31
61
204,800
205083812
n,m,k = map(int, input().split()) print (m*(m-1)//2) for i in range(m): for j in range(i+1, m): print (j+1, i+1) if k else print (i+1, j+1)
Codeforces Round 225 (Div. 2)
CF
2,014
1
256
Multitasking
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers. Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j. Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $$\frac{m(m-1)}{2}$$ (at most $$\frac{m(m-1)}{2}$$ pairs). Help Iahub, find any suitable array.
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
On the first line of the output print an integer p, the size of the array (p can be at most $$\frac{m(m-1)}{2}$$). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices. If there are multiple correct answers, you can print any.
null
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
[{"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5", "output": "3\n2 4\n2 3\n4 5"}, {"input": "3 2 1\n1 2\n2 3\n3 4", "output": "1\n2 1"}]
1,500
["greedy", "implementation", "sortings", "two pointers"]
31
[{"input": "2 5 0\r\n1 3 2 5 4\r\n1 4 3 2 5\r\n", "output": "3\r\n2 4\r\n2 3\r\n4 5\r\n"}, {"input": "3 2 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 5 0\r\n836096 600367 472071 200387 79763\r\n714679 505282 233544 157810 152591\r\n", "output": "10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 3\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n"}, {"input": "2 5 1\r\n331081 525217 574775 753333 840639\r\n225591 347017 538639 620341 994088\r\n", "output": "10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n3 2\r\n4 2\r\n5 2\r\n4 3\r\n5 3\r\n5 4\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 0\r\n1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2 1\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 2 0\r\n2 1\r\n3 1\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 0\r\n2 1\r\n1 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 1\r\n2 1\r\n3 1\r\n", "output": "1\r\n2 1\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, m, k = map(int, lines[0].split()) arrays = [list(map(int, line.split())) for line in lines[1:n+1]] try: with open(submission_path) as f: sub_lines = f.read().splitlines() except: print(0) return if not sub_lines: print(0) return try: p = int(sub_lines[0].strip()) if p < 0 or p > m * (m-1) // 2: print(0) return except: print(0) return swaps = [] for line in sub_lines[1:p+1]: parts = line.strip().split() if len(parts) != 2: print(0) return try: i = int(parts[0]) j = int(parts[1]) except: print(0) return if i < 1 or i > m or j < 1 or j > m or i == j: print(0) return swaps.append( (i-1, j-1) ) for arr in arrays: current = arr.copy() for i, j in swaps: if current[i] > current[j]: current[i], current[j] = current[j], current[i] valid = True if k == 0: for x in range(m-1): if current[x] > current[x+1]: valid = False break else: for x in range(m-1): if current[x] < current[x+1]: valid = False break if not valid: print(0) return print(1) if __name__ == "__main__": main()
true
612/F
612
F
PyPy 3-64
TESTS
6
62
614,400
170881266
from collections import defaultdict import sys readline=sys.stdin.readline N,s=map(int,readline().split()) s-=1 A=list(map(int,readline().split())) idx=defaultdict(list) inf=1<<30 for i,a in enumerate(A): idx[a].append(i) dp=[inf]*N sorted_A=sorted(list(idx.keys())) le=len(sorted_A) i=s lst=idx[sorted_A[0]] if A[s]==sorted_A[0]: dp[s]=N if len(lst)==1: j=lst[0] dp[j]=min((i-j)%N,(j-i)%N) else: for j in range(len(lst)): x,y=lst[j],lst[(j+1)%len(lst)] if (y-x)%N>=(i-x)%N: if i!=y: dp[y]=min(dp[y],(i-y)%N) if i!=x: dp[x]=min(dp[x],(x-i)%N) else: dp[x]=min(dp[x],(i-y)%N*2+(x-i)%N) dp[y]=min(dp[y],(x-i)%N*2+(i-y)%N) for a,aa in zip(sorted_A[:-1],sorted_A[1:]): lst0=idx[a] lst1=idx[aa] for i in lst0: if len(lst1)==1: j=lst1[0] dp[j]=min(dp[j],dp[i]+min((i-j)%N,(j-i)%N)) else: for j in range(len(lst1)): x,y=lst1[j],lst1[(j+1)%len(lst1)] if (y-x)%N>=(i-x)%N: if i!=y: dp[y]=min(dp[y],dp[i]+(i-y)%N) if i!=x: dp[x]=min(dp[x],dp[i]+(x-i)%N) else: dp[x]=min(dp[x],dp[i]+(i-y)%N*2+(x-i)%N) dp[y]=min(dp[y],dp[i]+(x-i)%N*2+(i-y)%N) ans=min(dp[i] for i in idx[sorted_A[-1]]) print(ans) for cur in idx[sorted_A[-1]]: if dp[cur]==ans: break ans_lst=[] for a,aa in zip(sorted_A[:-1][::-1],sorted_A[1:][::-1]): lst0=idx[a] lst1=idx[aa] for i in lst0: if len(lst1)==1: j=lst1[0] ans_lst.append(j) else: for j in range(len(lst1)): x,y=lst1[j],lst1[(j+1)%len(lst1)] if (y-x)%N>=(i-x)%N: if i!=y: if y==cur and dp[y]==dp[i]+(i-y)%N: while cur!=i: if A[cur]==aa: ans_lst.append(cur) cur+=1 cur%=N break if i!=x: if x==cur and dp[x]==dp[i]+(x-i)%N: while cur!=i: if A[cur]==aa: ans_lst.append(cur) cur-=1 cur%=N break else: if x==cur and dp[x]==dp[i]+(i-y)%N*2+(x-i)%N: while cur!=y: if A[cur]==aa: ans_lst.append(cur) cur-=1 cur%=N ans_lst.append(y) cur=i break elif y==cur and dp[y]==dp[i]+(x-i)%N*2+(i-y)%N: while cur!=x: if A[cur]==aa: ans_lst.append(cur) cur+=1 cur%=N ans_lst.append(x) cur=i break i=s lst=idx[sorted_A[0]] if len(lst)==1: j=lst[0] ans_lst.append(j) else: for j in range(len(lst)): x,y=lst[j],lst[(j+1)%len(lst)] aa=sorted_A[0] if (y-x)%N>=(i-x)%N: if i!=y: if y==cur and dp[y]==(i-y)%N: while cur!=i: if A[cur]==aa: ans_lst.append(cur) cur+=1 cur%=N break if i!=x: if x==cur and dp[x]==(x-i)%N: while cur!=i: if A[cur]==aa: ans_lst.append(cur) cur-=1 cur%=N break else: if x==cur and dp[x]==(i-y)%N*2+(x-i)%N: while cur!=y: if A[cur]==aa: ans_lst.append(cur) cur-=1 cur%=N ans_lst.append(y) cur=i break elif y==cur and dp[y]==(x-i)%N*2+(i-y)%N: while cur!=x: if A[cur]==aa: ans_lst.append(cur) cur+=1 cur%=N ans_lst.append(x) cur=i break ans_lst.append(s) if s==sorted_A[0]: ans_lst.append(s) ans_lst=ans_lst[::-1] le=len(ans_lst) for i in range(le-1): x,y=ans_lst[i],ans_lst[i+1] if (x-y)%N<(y-x)%N: print("-",(x-y)%N,sep="") else: print("+",(y-x)%N,sep="")
27
140
8,704,000
165028661
import os from re import M import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def solve(): n, s = map(int, input().split()) A = list(map(int, input().split())) s -= 1 se = set(A) dic = {l:i for i, l in enumerate(sorted(se))} le = len(se) A = [dic[a] for a in A] lst = [[] for _ in range(le + 1)] for i, a in enumerate(A): lst[a + 1].append(i) lst[0] = [s] dp = [[] for _ in range(le + 1)] bef = [[] for _ in range(le + 1)] dp[0] = [0] inf = 1 << 30 for ii in range(le): l1 = lst[ii] l2 = lst[ii + 1] le1 = len(l1) le2 = len(l2) bdp = dp[ii] ndp = [inf] * le2 bef_ = [None] * le2 if le2 == 1: for i in range(le1): tmp = abs(l1[i] - l2[0]) tmp = min(tmp, n - tmp) d = bdp[i] + tmp if d < ndp[0]: ndp[0] = d bef_[0] = i else: for i in range(le1): for j in range(le2): tmp = abs(l1[i] - l2[j]) tmp = min(tmp, n - tmp) d = bdp[i] + tmp if j == 0: dd = d + l2[le2 - 1] - l2[j] if dd < ndp[le2 - 1]: ndp[le2 - 1] = dd bef_[le2 - 1] = (i, 0) else: dd = d + n - (l2[j] - l2[j - 1]) if dd < ndp[j - 1]: ndp[j - 1] = dd bef_[j - 1] = (i, 0) if j == le2 - 1: dd = d + l2[j] - l2[0] if dd < ndp[0]: ndp[0] = dd bef_[0] = (i, 1) else: dd = d + n - (l2[j + 1] - l2[j]) if dd < ndp[j + 1]: ndp[j + 1] = dd bef_[j + 1] = (i, 1) dp[ii + 1] = ndp bef[ii + 1] = bef_ min_ = 1 << 30 t = -1 for i, d in enumerate(dp[-1]): if d < min_: min_ = d t = i print(min_) ans = [] for i in range(le, 0, -1): if type(bef[i][t]) == int: j = bef[i][t] tmp = lst[i][t] - lst[i - 1][j] if tmp < 0: tmp += n if tmp <= n - tmp: ans.append(f"+{tmp}") else: ans.append(f"-{n - tmp}") t = j else: j, k = bef[i][t] l = len(lst[i]) if k == 1: r = t + 1 if r == l: r = 0 for _ in range(l - 1): d = lst[i][r] - lst[i][t] if d < 0: d += n ans.append(f"-{d}") t = r r = t + 1 if r == l: r = 0 else: r = t - 1 if r == -1: r = l - 1 for _ in range(l - 1): d = lst[i][t] - lst[i][r] if d < 0: d += n ans.append(f"+{d}") t = r r = t - 1 if r == -1: r = l - 1 tmp = lst[i][t] - lst[i - 1][j] if tmp < 0: tmp += n if tmp <= n - tmp: ans.append(f"+{tmp}") else: ans.append(f"-{n - tmp}") t = j print(*ans[::-1], sep="\n") for _ in range(1): solve()
Educational Codeforces Round 4
ICPC
2,015
1
256
Simba on the Circle
You are given a circular array with n elements. The elements are numbered from some element with values from 1 to n in clockwise order. The i-th cell contains the value ai. The robot Simba is in cell s. Each moment of time the robot is in some of the n cells (at the begin he is in s). In one turn the robot can write out the number written in current cell or move to the adjacent cell in clockwise or counterclockwise direction. To write out the number from the cell Simba doesn't spend any time, but to move to adjacent cell Simba spends one unit of time. Simba wants to write the number from each cell one time, so the numbers will be written in a non decreasing order. Find the least number of time units to write out all numbers.
The first line contains two integers n and s (1 ≤ s ≤ n ≤ 2000) — the number of cells in the circular array and the starting position of Simba. The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the number written in the i-th cell. The numbers are given for cells in order from 1 to n. Some of numbers ai can be equal.
In the first line print the number t — the least number of time units. Each of the next n lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of cells should be printed in the form of +x in case of clockwise movement and -x in case of counterclockwise movement to x cells (0 ≤ x ≤ n - 1). Note that the sum of absolute values of x should be equal to t.
null
null
[{"input": "9 1\n0 1 2 2 2 1 0 1 1", "output": "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1"}, {"input": "8 1\n0 1 0 1 0 1 0 1", "output": "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2"}, {"input": "8 1\n1 2 3 4 5 6 7 8", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1"}, {"input": "8 1\n0 0 0 0 0 0 0 0", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1"}]
2,600
["dp"]
27
[{"input": "9 1\r\n0 1 2 2 2 1 0 1 1\r\n", "output": "12\r\n+0\r\n-3\r\n-1\r\n+2\r\n+1\r\n+2\r\n+1\r\n+1\r\n+1\r\n"}, {"input": "8 1\r\n0 1 0 1 0 1 0 1\r\n", "output": "13\r\n+0\r\n+2\r\n+2\r\n+2\r\n-1\r\n+2\r\n+2\r\n+2\r\n"}, {"input": "8 1\r\n1 2 3 4 5 6 7 8\r\n", "output": "7\r\n+0\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n"}, {"input": "8 1\r\n0 0 0 0 0 0 0 0\r\n", "output": "7\r\n+0\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n"}, {"input": "8 1\r\n0 1 2 2 1 0 1 1\r\n", "output": "11\r\n+0\r\n-3\r\n-1\r\n+2\r\n+1\r\n+2\r\n+1\r\n+1\r\n"}, {"input": "1 1\r\n4\r\n", "output": "0\r\n+0\r\n"}, {"input": "10 1\r\n-1 0 1 0 -1 1 0 0 1 -1\r\n", "output": "22\r\n+0\r\n-1\r\n-5\r\n-3\r\n+2\r\n+3\r\n+1\r\n+1\r\n-3\r\n-3\r\n"}, {"input": "20 7\r\n0 6 0 0 0 -7 -8 9 -7 4 7 2 -4 4 -5 2 6 8 -2 -7\r\n", "output": "83\r\n+0\r\n+2\r\n-3\r\n-6\r\n-5\r\n-2\r\n+6\r\n+2\r\n+2\r\n+1\r\n+1\r\n+7\r\n+4\r\n-2\r\n-4\r\n-8\r\n-5\r\n-6\r\n+7\r\n+10\r\n"}, {"input": "30 13\r\n68 50 99 23 84 23 24 -42 82 36 -10 -51 -96 96 19 -4 4 -41 74 92 13 58 26 79 -11 38 -80 -38 73 -21\r\n", "output": "238\r\n+0\r\n+14\r\n+15\r\n-4\r\n+10\r\n+10\r\n+2\r\n-5\r\n-14\r\n+5\r\n+1\r\n+4\r\n-6\r\n-11\r\n+2\r\n+1\r\n-14\r\n-13\r\n-14\r\n+6\r\n-10\r\n+9\r\n-2\r\n-10\r\n+5\r\n+15\r\n-4\r\n+15\r\n-6\r\n-11\r\n"}, {"input": "40 1\r\n886 -661 499 -14 -101 660 -259 -499 -766 155 -120 -112 -922 979 36 528 593 653 409 -476 -125 183 -817 59 353 16 525 -43 -388 989 306 -145 935 -712 -243 460 -861 339 347 -445\r\n", "output": "437\r\n+12\r\n-16\r\n-14\r\n-14\r\n-15\r\n+8\r\n+6\r\n+12\r\n+20\r\n-11\r\n+18\r\n-12\r\n-3\r\n-11\r\n-10\r\n+1\r\n-7\r\n-17\r\n+16\r\n-18\r\n-11\r\n+9\r\n-14\r\n+12\r\n+9\r\n+7\r\n+1\r\n-14\r\n-6\r\n+17\r\n+7\r\n-16\r\n-11\r\n+1\r\n+1\r\n-12\r\n-5\r\n-8\r\n-19\r\n+16\r\n"}, {"input": "50 32\r\n2624 -8355 -5993 -1 8197 382 -9197 -5078 -7 -1021 -4419 8918 -7114 5016 1912 -8436 -1217 2178 -6513 -9910 -1695 7501 7028 -6171 9063 9112 9063 -1886 9156 -7256 8871 -6855 7059 -5209 2308 5964 -4283 2248 1790 -6658 2906 -478 -5663 -9250 4355 1099 1468 -3051 -9353 -5717\r\n", "output": "601\r\n-12\r\n-21\r\n-5\r\n+13\r\n+9\r\n-14\r\n-22\r\n-17\r\n+19\r\n+8\r\n-21\r\n+5\r\n-21\r\n-3\r\n-7\r\n-9\r\n+24\r\n+3\r\n-24\r\n+11\r\n-20\r\n-7\r\n-4\r\n-7\r\n-18\r\n+17\r\n-5\r\n+2\r\n-10\r\n+1\r\n-8\r\n-24\r\n+3\r\n+20\r\n-3\r\n+16\r\n-10\r\n+4\r\n+19\r\n+22\r\n-13\r\n+10\r\n-11\r\n-17\r\n-24\r\n-19\r\n+13\r\n+2\r\n-1\r\n+3\r\n"}, {"input": "60 32\r\n58726 58267 -31806 44691 -52713 -11475 61179 83630 93772 48048 -64921 -16810 -16172 -30820 30109 -81876 -27921 -69676 -28393 -45495 6588 -30154 21312 50563 22336 -37995 -31034 -30980 -72408 -29962 -4891 24299 8648 -69415 -62580 95513 -13691 -92575 -10376 40008 2041 -24616 -6934 -42025 68949 -87961 -91709 -46669 -36624 -75601 -83110 43195 86628 53287 -14813 -7263 -20579 -51021 37654 -13428\r\n", "output": "884\r\n+6\r\n+9\r\n-1\r\n+5\r\n+25\r\n-26\r\n-21\r\n-11\r\n+16\r\n-23\r\n+24\r\n+30\r\n-7\r\n-10\r\n-28\r\n+24\r\n-18\r\n+23\r\n+14\r\n+24\r\n+1\r\n-14\r\n+8\r\n+8\r\n-11\r\n-2\r\n+25\r\n+15\r\n+15\r\n+1\r\n-18\r\n-18\r\n+23\r\n+6\r\n-27\r\n+17\r\n-13\r\n-12\r\n+10\r\n-20\r\n+12\r\n-10\r\n+2\r\n+7\r\n-17\r\n-16\r\n-19\r\n+12\r\n+12\r\n+6\r\n+14\r\n+30\r\n+8\r\n-1\r\n+6\r\n-22\r\n+23\r\n-15\r\n+16\r\n+27\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: lines = f.read().splitlines() n, s = map(int, lines[0].split()) a = list(map(int, lines[1].split())) # Read reference output with open(output_path) as f: ref_lines = f.read().splitlines() ref_t = int(ref_lines[0]) # Read submission output with open(submission_path) as f: sub_lines = f.read().splitlines() if not sub_lines: print(0) return try: sub_t = int(sub_lines[0]) except: print(0) return if sub_t != ref_t: print(0) return sub_steps = sub_lines[1:] if len(sub_steps) != n: print(0) return # Check each step format and collect x sum sum_x = 0 steps = [] for line in sub_steps: line = line.strip() if not (line.startswith('+') or line.startswith('-')): print(0) return try: sign = line[0] x = int(line[1:]) except: print(0) return if x < 0 or x >= n: print(0) return steps.append((sign, x)) sum_x += x if sum_x != sub_t: print(0) return # Simulate the steps current_pos = s visited = set() prev_num = None for sign, x in steps: pos = current_pos - 1 if sign == '+': pos = (pos + x) % n else: pos = (pos - x) % n new_pos = pos + 1 if new_pos < 1 or new_pos > n: print(0) return if new_pos in visited: print(0) return visited.add(new_pos) current_num = a[new_pos - 1] if prev_num is not None and current_num < prev_num: print(0) return prev_num = current_num current_pos = new_pos # Check all cells are visited if len(visited) != n or any((i + 1) not in visited for i in range(n)): print(0) return print(1) if __name__ == "__main__": main()
true
384/B
384
B
Python 3
TESTS
0
46
0
18816464
def main(): n, m, k = map(int, input().split()) l, res = [], [0] for _ in range(n): row = list(map(int, input().split())) for i, j in enumerate(sorted(range(m), key=row.__getitem__, reverse=k)): row[j] = i l.append(row) for i in range(m - 1): s = set() for row in l: if row[i] != i: j = row.index(i) s.add(j) row[j] = row[i] pref = '\n' + str(i + 1) + ' ' for j in sorted(s): res.append(pref) res.append(str(j + 1)) res[0] = str(len(res) // 2) print(''.join(res)) if __name__ == '__main__': main()
31
77
5,120,000
25095447
#in the name of god #Mr_Rubick n,m,k=map(int,input().split()) print(str(m*(m-1)//2)) for i in range(1,m): for j in range(i+1,m+1): if k==0: print(str(i)+" "+str(j)) else: print(str(j)+" "+str(i))
Codeforces Round 225 (Div. 2)
CF
2,014
1
256
Multitasking
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers. Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j. Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $$\frac{m(m-1)}{2}$$ (at most $$\frac{m(m-1)}{2}$$ pairs). Help Iahub, find any suitable array.
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
On the first line of the output print an integer p, the size of the array (p can be at most $$\frac{m(m-1)}{2}$$). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices. If there are multiple correct answers, you can print any.
null
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
[{"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5", "output": "3\n2 4\n2 3\n4 5"}, {"input": "3 2 1\n1 2\n2 3\n3 4", "output": "1\n2 1"}]
1,500
["greedy", "implementation", "sortings", "two pointers"]
31
[{"input": "2 5 0\r\n1 3 2 5 4\r\n1 4 3 2 5\r\n", "output": "3\r\n2 4\r\n2 3\r\n4 5\r\n"}, {"input": "3 2 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 5 0\r\n836096 600367 472071 200387 79763\r\n714679 505282 233544 157810 152591\r\n", "output": "10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 3\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n"}, {"input": "2 5 1\r\n331081 525217 574775 753333 840639\r\n225591 347017 538639 620341 994088\r\n", "output": "10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n3 2\r\n4 2\r\n5 2\r\n4 3\r\n5 3\r\n5 4\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 0\r\n1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2 1\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 2 0\r\n2 1\r\n3 1\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 0\r\n2 1\r\n1 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 1\r\n2 1\r\n3 1\r\n", "output": "1\r\n2 1\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, m, k = map(int, lines[0].split()) arrays = [list(map(int, line.split())) for line in lines[1:n+1]] try: with open(submission_path) as f: sub_lines = f.read().splitlines() except: print(0) return if not sub_lines: print(0) return try: p = int(sub_lines[0].strip()) if p < 0 or p > m * (m-1) // 2: print(0) return except: print(0) return swaps = [] for line in sub_lines[1:p+1]: parts = line.strip().split() if len(parts) != 2: print(0) return try: i = int(parts[0]) j = int(parts[1]) except: print(0) return if i < 1 or i > m or j < 1 or j > m or i == j: print(0) return swaps.append( (i-1, j-1) ) for arr in arrays: current = arr.copy() for i, j in swaps: if current[i] > current[j]: current[i], current[j] = current[j], current[i] valid = True if k == 0: for x in range(m-1): if current[x] > current[x+1]: valid = False break else: for x in range(m-1): if current[x] < current[x+1]: valid = False break if not valid: print(0) return print(1) if __name__ == "__main__": main()
true
652/B
652
B
Python 3
TESTS
5
62
0
110487377
n=int(input()) array=list(input().split()) array.sort() ans=[] for i in range(n//2): ans.append(array[i]) ans.append(array[n-i-1]) if len(array)%2!=0: ans.append(array[n//2]) print(" ".join(ans))
16
31
0
212178658
def solve(): print(l[0],l[-1],end=' ') del(l[0]) del(l[-1]) itr = int(input()) l=sorted(list(map(int,input().split()))) while len(l): if itr%2==0:solve() else: if len(l)>2:solve() else:print(l[0],end=' ');del(l[0])
Educational Codeforces Round 10
ICPC
2,016
1
256
z-sort
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: 1. ai ≥ ai - 1 for all even i, 2. ai ≤ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted. Can you make the array z-sorted?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
null
null
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
1,000
["sortings"]
16
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\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 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": "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 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"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\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 = int(f.readline().strip()) a = list(map(int, f.readline().split())) a_sorted = sorted(a) # Read reference output with open(output_path) as f: ref_lines = [line.strip() for line in f] ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible') # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f] if not sub_lines: print(0) return sub_output = sub_lines[0].split() sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible') # Check if submission matches the reference's impossible status if ref_is_impossible: print(1 if sub_is_impossible else 0) return else: if sub_is_impossible: print(0) return # Check if the submission is a valid permutation and meets conditions if len(sub_output) != n: print(0) return try: sub_array = list(map(int, sub_output)) except: print(0) return if sorted(sub_array) != a_sorted: print(0) return # Check z-sort conditions valid = True for i in range(1, n): if (i + 1) % 2 == 0: # 1-based even index if sub_array[i] < sub_array[i - 1]: valid = False break else: # 1-based odd index > 1 if sub_array[i] > sub_array[i - 1]: valid = False break print(1 if valid else 0) if __name__ == "__main__": main()
true
877/E
877
E
PyPy 3
TESTS
6
108
20,172,800
125777086
import sys input = sys.stdin.readline n = int(input()) # directed tree G = [[] for _ in range(n)] parent = list(map(int,input().split())) for i in range(n-1): G[parent[i]-1].append(i+1) # start and end points S = [-1]*n E = [-1]*n T = 0 def time(node=0): global T,S,E S[node] = T T += 1 for node0 in G[node]: time(node0) E[node] = T-1 time() sz = 1 while sz < n: sz *= 2 tree = [0]*(1+2*sz) lazy = [0]*(1+2*sz) def query(node,l,r,ql,qr): if l>qr or r<ql: return 0 if lazy[node]: tree[node] = r-l+1 - tree[node] if l != r: lazy[2*node] = (1+lazy[2*node])%2 lazy[2*node+1] = (1+lazy[2*node+1])%2 lazy[node] = 0 if ql<=l and r<=qr: return tree[node] mi = (l+r)//2 return query(2*node,l,mi,ql,qr) + query(2*node+1,mi+1,r,ql,qr) def update(node,l,r,ul,ur): if l>ur or r<ul: return if lazy[node]: tree[node] = r-l+1 - tree[node] if l != r: lazy[2*node] = (1+lazy[2*node])%2 lazy[2*node+1] = (1+lazy[2*node+1])%2 lazy[node] = 0 if ul<=l and r<=ur: tree[node] = r-l+1 - tree[node] if l != r: lazy[2*node] = (1+lazy[2*node])%2 lazy[2*node+1] = (1+lazy[2*node+1])%2 return mi = (l+r)//2 update(2*node,l,mi,ul,ur) update(2*node+1,mi+1,r,ul,ur) return light = list(map(int,input().split())) for i in range(n): if light[i]: tree[sz+S[i]] = 1 for i in range(sz-1,0,-1): tree[i] = tree[2*i] + tree[2*i+1] for _ in range(int(input())): s,x = input().split() if s == "get": x = int(x)-1 ql = S[x] qr = E[x] sys.stdout.write(str(query(1,0,sz-1,ql,qr)) + "\n") else: x = int(x)-1 ul = S[x] ur = E[x] update(1,0,sz-1,ul,ur)
57
1,544
90,316,800
209145397
import math import sys from bisect import * from collections import * from functools import * from heapq import * from itertools import * from random import * from string import * from types import GeneratorType # region fastio input = lambda: sys.stdin.readline().rstrip() sint = lambda: int(input()) mint = lambda: map(int, input().split()) ints = lambda: list(map(int, input().split())) # print = lambda d: sys.stdout.write(str(d) + "\n") # endregion fastio # # region interactive # def printQry(a, b) -> None: # sa = str(a) # sb = str(b) # print(f"? {sa} {sb}", flush = True) # def printAns(ans) -> None: # s = str(ans) # print(f"! {s}", flush = True) # # endregion interactive # # region dfsconvert # def bootstrap(f, stack=[]): # def wrappedfunc(*args, **kwargs): # if stack: # return f(*args, **kwargs) # else: # to = f(*args, **kwargs) # while True: # if type(to) is GeneratorType: # stack.append(to) # to = next(to) # else: # stack.pop() # if not stack: # break # to = stack[-1].send(to) # return to # return wrappedfunc # # endregion dfsconvert # MOD = 998244353 # MOD = 10 ** 9 + 7 # DIR = ((-1, 0), (0, 1), (1, 0), (0, -1)) def dfs(graph, start=0): n = len(graph) dfn = [-1] * n sz = [1] * n visited, finished = [False] * n, [False] * n time = n - 1 stack = [start] while stack: start = stack[-1] # push unvisited children into stack if not visited[start]: visited[start] = True for child in graph[start]: if not visited[child]: stack.append(child) else: stack.pop() dfn[start] = time time -= 1 # update with finished children for child in graph[start]: if finished[child]: sz[start] += sz[child] finished[start] = True return dfn, sz class SegTree: def __init__(self, nums: list) -> None: n = len(nums) self.n = n self.tree = [0] * (4 * n) self.lazy = [0] * (4 * n) self.build(nums, 1, 1, n) def build(self, nums: list, o: int, l: int, r: int) -> None: if l == r: self.tree[o] = nums[l - 1] return m = (l + r) >> 1 self.build(nums, o * 2, l, m) self.build(nums, o * 2 + 1, m + 1, r) self.pushUp(o) def xor(self, o: int, l: int, r: int) -> None: self.tree[o] = r - l + 1 - self.tree[o] # 异或反转 self.lazy[o] ^= 1 def do(self, o: int, l: int, r: int, val: int) -> None: self.tree[o] = r - l + 1 - self.tree[o] # 异或反转 self.lazy[o] ^= 1 def op(self, a: int, b: int) -> int: # + - * / max min return a + b def maintain(self, o: int, l: int, r: int) -> None: self.tree[o] = self.op(self.tree[l], self.tree[r]) self.lazy[o] ^= 1 def pushUp(self, o: int) -> None: self.tree[o] = self.tree[o * 2] + self.tree[o * 2 + 1] def pushDown(self, o: int, l: int, r: int) -> None: m = (l + r) >> 1 self.do(o * 2, l, m, self.lazy[o]) # 调用相应的更新操作方法 self.do(o * 2 + 1, m + 1, r, self.lazy[o]) self.lazy[o] = 0 def update(self, o: int, l: int, r: int, L: int, R: int, val: int) -> None: if L <= l and r <= R: # 当前区间已完全包含在更新区间,不需要继续往下更新,存在lazy self.xor(o, l, r) return if self.lazy[o]: # 当前lazyd存在更新,往下传递 self.pushDown(o, l, r) m = (l + r) >> 1 if m >= L: # 左节点在更新区间 self.update(o * 2, l, m, L, R, val) if m < R: # 右节点在更新区间 self.update(o * 2 + 1, m + 1, r, L, R, val) self.pushUp(o) # 从左右节点更新当前节点值 def query(self, o: int, l: int, r: int, L: int, R: int) -> int: if R < l or L > r: return 0 if L <= l and r <= R: return self.tree[o] if self.lazy[o]: # 当前lazyd存在更新,往下传递 self.pushDown(o, l, r) m = (l + r) >> 1 res = 0 if m >= L: # 左节点在查询区间 res += self.query(o * 2, l, m, L, R) if m < R: # 右节点在查询区间 res += self.query(o * 2 + 1, m + 1, r, L, R) return res def solve() -> None: n = sint() p = ints() # 建图 g = [[] for _ in range(n)] for i, u in enumerate(p, 1): g[u - 1].append(i) # DFN序 dfn, sz = dfs(g) # print(dfn) # print(sz) t = ints() nums = [0] * n for i, v in enumerate(t): nums[dfn[i]] = v # print(nums) st = SegTree(nums) for _ in range(sint()): qry = input().split() u = int(qry[1]) - 1 if qry[0] == "get": print(st.query(1, 1, n, dfn[u] + 1, dfn[u] + sz[u])) else: st.update(1, 1, n, dfn[u] + 1, dfn[u] + sz[u], 1) # for _ in range(int(input())): solve()
Codeforces Round 442 (Div. 2)
CF
2,017
2
256
Danil and a Part-time Job
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on. Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages. There are two types of tasks: 1. pow v describes a task to switch lights in the subtree of vertex v. 2. get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages. A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v. Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i. The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and 0 otherwise. The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks. The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
null
The tree before the task pow 1. The tree after the task pow 1.
[{"input": "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4", "output": "2\n0\n0\n1\n2\n1\n1\n0"}]
2,000
["bitmasks", "data structures", "trees"]
57
[{"input": "4\r\n1 1 1\r\n1 0 0 1\r\n9\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\npow 1\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\n", "output": "2\r\n0\r\n0\r\n1\r\n2\r\n1\r\n1\r\n0\r\n"}, {"input": "1\r\n\r\n1\r\n4\r\npow 1\r\nget 1\r\npow 1\r\nget 1\r\n", "output": "0\r\n1\r\n"}, {"input": "10\r\n1 2 1 3 4 5 6 8 5\r\n1 0 0 0 1 0 0 0 1 0\r\n10\r\npow 10\r\npow 4\r\npow 10\r\npow 7\r\npow 10\r\npow 10\r\npow 4\r\npow 8\r\npow 7\r\npow 1\r\n", "output": ""}, {"input": "10\r\n1 2 3 4 2 4 1 7 8\r\n1 1 0 1 1 0 0 0 1 1\r\n10\r\npow 1\r\nget 2\r\npow 2\r\npow 8\r\nget 6\r\npow 6\r\npow 10\r\nget 6\r\npow 8\r\npow 3\r\n", "output": "3\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 1 4 5 3 5 6 3\r\n0 0 0 0 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 4\r\nget 7\r\nget 3\r\npow 2\r\npow 5\r\npow 2\r\nget 7\r\npow 6\r\nget 10\r\n", "output": "0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n"}, {"input": "10\r\n1 1 3 1 3 1 4 6 3\r\n0 1 1 1 1 1 1 1 0 0\r\n10\r\nget 9\r\nget 10\r\nget 4\r\nget 5\r\nget 5\r\nget 5\r\nget 10\r\nget 7\r\nget 5\r\nget 2\r\n", "output": "0\r\n0\r\n2\r\n1\r\n1\r\n1\r\n0\r\n1\r\n1\r\n1\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 8\r\n0 0 0 0 1 1 1 1 0 0\r\n10\r\npow 3\r\nget 1\r\npow 9\r\nget 1\r\nget 1\r\nget 8\r\npow 8\r\npow 4\r\nget 10\r\npow 2\r\n", "output": "4\r\n3\r\n3\r\n1\r\n0\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 9\r\n1 1 0 1 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 6\r\nget 4\r\nget 2\r\nget 1\r\nget 2\r\nget 6\r\nget 9\r\nget 10\r\nget 7\r\n", "output": "3\r\n0\r\n1\r\n3\r\n4\r\n3\r\n0\r\n0\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 3 3 5 5 7 7 9\r\n0 0 1 1 0 0 1 1 0 0\r\n10\r\npow 8\r\npow 4\r\npow 8\r\npow 5\r\npow 1\r\npow 2\r\npow 6\r\npow 6\r\npow 3\r\npow 2\r\n", "output": ""}, {"input": "10\r\n1 1 2 2 3 3 5 5 6\r\n1 1 1 1 0 0 1 1 0 0\r\n10\r\nget 2\r\nget 8\r\nget 10\r\nget 5\r\nget 5\r\npow 10\r\nget 10\r\nget 1\r\nget 7\r\npow 4\r\n", "output": "3\r\n1\r\n0\r\n1\r\n1\r\n1\r\n7\r\n1\r\n"}, {"input": "10\r\n1 1 2 2 3 3 4 4 5\r\n1 1 0 1 0 0 0 0 0 0\r\n10\r\nget 2\r\nget 5\r\npow 2\r\npow 4\r\nget 2\r\nget 4\r\npow 7\r\nget 10\r\npow 5\r\nget 6\r\n", "output": "2\r\n0\r\n3\r\n1\r\n1\r\n0\r\n"}]
false
stdio
null
true
877/E
877
E
PyPy 3
TESTS
6
140
0
80641150
n=int(input()) par=[int(i)-1 for i in input().split()] t=list(map(int,input().split())) qq=int(input()) tr=[[] for i in range(n)] for i in range(n-1): tr[par[i]].append(i+1) size=[1 for i in range(n)] on=[t[i] for i in range(n)] pos=[-1 for i in range(n)] m=[] i=0 s=[0] while s: x=s.pop() m.append(x) pos[x]=i i+=1 for y in tr[x]: if pos[y]!=-1:continue s.append(y) for i in range(n-1,0,-1): j=m[i] size[par[j-1]]+=size[j] on[par[j-1]]+=on[j] it=[0 for i in range(2*n)] def u(i,v): i+=n it[i]+=v while i>0: i>>=1 it[i]=it[i<<1]+it[i<<1|1] def q(i): l=n r=i+n+1 ans=0 while l<r: if l&1: ans+=it[l] l+=1 if r&1: r-=1 ans+=it[r] r>>=1 l>>=1 return(ans) for _ in range(qq): s=str(input()) v=int(s[-1])-1 i=pos[v] if s[0]=='g': if q(i)%2==0: print(on[v]) else: print(size[v]-on[v]) else: j=i+size[v] u(i,1) if j<n: u(j,-1)
57
1,902
63,692,800
104860740
class LazySegTree: def __init__(self, init_val, seg_ide, lazy_ide, f, g, h): self.n = len(init_val) self.num = 2**(self.n-1).bit_length() self.seg_ide = seg_ide self.lazy_ide = lazy_ide self.f = f #(seg, seg) -> seg self.g = g #(seg, lazy, size) -> seg self.h = h #(lazy, lazy) -> lazy self.seg = [seg_ide]*2*self.num for i in range(self.n): self.seg[i+self.num] = init_val[i] for i in range(self.num-1, 0, -1): self.seg[i] = self.f(self.seg[2*i], self.seg[2*i+1]) self.size = [0]*2*self.num for i in range(self.n): self.size[i+self.num] = 1 for i in range(self.num-1, 0, -1): self.size[i] = self.size[2*i] + self.size[2*i+1] self.lazy = [lazy_ide]*2*self.num def update(self, i, x): i += self.num self.seg[i] = x while i: i = i >> 1 self.seg[i] = self.f(self.seg[2*i], self.seg[2*i+1]) def calc(self, i): return self.g(self.seg[i], self.lazy[i], self.size[i]) def calc_above(self, i): i = i >> 1 while i: self.seg[i] = self.f(self.calc(2*i), self.calc(2*i+1)) i = i >> 1 def propagate(self, i): self.seg[i] = self.g(self.seg[i], self.lazy[i], self.size[i]) self.lazy[2*i] = self.h(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.h(self.lazy[2*i+1], self.lazy[i]) self.lazy[i] = self.lazy_ide def propagate_above(self, i): H = i.bit_length() for h in range(H, 0, -1): self.propagate(i >> h) def query(self, l, r): l += self.num r += self.num lm = l // (l & -l) rm = r // (r & -r) -1 self.propagate_above(lm) self.propagate_above(rm) al = self.seg_ide ar = self.seg_ide while l < r: if l & 1: al = self.f(al, self.calc(l)) l += 1 if r & 1: r -= 1 ar = self.f(self.calc(r), ar) l = l >> 1 r = r >> 1 return self.f(al, ar) def oprerate_range(self, l, r, a): l += self.num r += self.num lm = l // (l & -l) rm = r // (r & -r) -1 self.propagate_above(lm) self.propagate_above(rm) while l < r: if l & 1: self.lazy[l] = self.h(self.lazy[l], a) l += 1 if r & 1: r -= 1 self.lazy[r] = self.h(self.lazy[r], a) l = l >> 1 r = r >> 1 self.calc_above(lm) self.calc_above(rm) f = lambda x, y: x+y g = lambda x, a, s: s-x if a%2 == 1 else x h = lambda a, b: a+b def EulerTour(g, root): n = len(g) root = root g = g tank = [root] eulerTour = [] left = [0]*n right = [-1]*n depth = [-1]*n parent = [-1]*n child = [[] for i in range(n)] eulerNum = -1 de = -1 while tank: v = tank.pop() if v >= 0: eulerNum += 1 eulerTour.append(v) left[v] = eulerNum right[v] = eulerNum tank.append(~v) de += 1 depth[v] = de for u in g[v]: if parent[v] == u: continue tank.append(u) parent[u] = v child[v].append(u) else: de -= 1 if ~v != root: eulerTour.append(parent[~v]) eulerNum += 1 right[parent[~v]] = eulerNum return eulerTour, left, right import bisect import sys import io, os input = sys.stdin.readline #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def main(): n = int(input()) P = list(map(int, input().split())) T = list(map(int, input().split())) P = [p-1 for p in P] P = [-1]+P edge = [[] for i in range(n)] for i, p in enumerate(P): if p != -1: edge[p].append(i) et, left, right = EulerTour(edge, 0) d = {} B = sorted(left) #print(B) #print(right) for i, b in enumerate(B): d[b] = i A = [0]*n for i in range(n): A[d[left[i]]] = T[i] seg = LazySegTree(A, 0, 0, f, g, h) q = int(input()) for i in range(q): query = list(map(str, input().split())) if query[0] == 'get': u = int(query[1])-1 l = d[left[u]] r = bisect.bisect_right(B, right[u]) print(seg.query(l, r)) else: u = int(query[1])-1 l = d[left[u]] r = bisect.bisect_right(B, right[u]) seg.oprerate_range(l, r, 1) if __name__ == '__main__': main()
Codeforces Round 442 (Div. 2)
CF
2,017
2
256
Danil and a Part-time Job
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on. Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages. There are two types of tasks: 1. pow v describes a task to switch lights in the subtree of vertex v. 2. get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages. A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v. Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i. The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and 0 otherwise. The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks. The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
null
The tree before the task pow 1. The tree after the task pow 1.
[{"input": "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4", "output": "2\n0\n0\n1\n2\n1\n1\n0"}]
2,000
["bitmasks", "data structures", "trees"]
57
[{"input": "4\r\n1 1 1\r\n1 0 0 1\r\n9\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\npow 1\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\n", "output": "2\r\n0\r\n0\r\n1\r\n2\r\n1\r\n1\r\n0\r\n"}, {"input": "1\r\n\r\n1\r\n4\r\npow 1\r\nget 1\r\npow 1\r\nget 1\r\n", "output": "0\r\n1\r\n"}, {"input": "10\r\n1 2 1 3 4 5 6 8 5\r\n1 0 0 0 1 0 0 0 1 0\r\n10\r\npow 10\r\npow 4\r\npow 10\r\npow 7\r\npow 10\r\npow 10\r\npow 4\r\npow 8\r\npow 7\r\npow 1\r\n", "output": ""}, {"input": "10\r\n1 2 3 4 2 4 1 7 8\r\n1 1 0 1 1 0 0 0 1 1\r\n10\r\npow 1\r\nget 2\r\npow 2\r\npow 8\r\nget 6\r\npow 6\r\npow 10\r\nget 6\r\npow 8\r\npow 3\r\n", "output": "3\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 1 4 5 3 5 6 3\r\n0 0 0 0 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 4\r\nget 7\r\nget 3\r\npow 2\r\npow 5\r\npow 2\r\nget 7\r\npow 6\r\nget 10\r\n", "output": "0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n"}, {"input": "10\r\n1 1 3 1 3 1 4 6 3\r\n0 1 1 1 1 1 1 1 0 0\r\n10\r\nget 9\r\nget 10\r\nget 4\r\nget 5\r\nget 5\r\nget 5\r\nget 10\r\nget 7\r\nget 5\r\nget 2\r\n", "output": "0\r\n0\r\n2\r\n1\r\n1\r\n1\r\n0\r\n1\r\n1\r\n1\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 8\r\n0 0 0 0 1 1 1 1 0 0\r\n10\r\npow 3\r\nget 1\r\npow 9\r\nget 1\r\nget 1\r\nget 8\r\npow 8\r\npow 4\r\nget 10\r\npow 2\r\n", "output": "4\r\n3\r\n3\r\n1\r\n0\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 9\r\n1 1 0 1 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 6\r\nget 4\r\nget 2\r\nget 1\r\nget 2\r\nget 6\r\nget 9\r\nget 10\r\nget 7\r\n", "output": "3\r\n0\r\n1\r\n3\r\n4\r\n3\r\n0\r\n0\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 3 3 5 5 7 7 9\r\n0 0 1 1 0 0 1 1 0 0\r\n10\r\npow 8\r\npow 4\r\npow 8\r\npow 5\r\npow 1\r\npow 2\r\npow 6\r\npow 6\r\npow 3\r\npow 2\r\n", "output": ""}, {"input": "10\r\n1 1 2 2 3 3 5 5 6\r\n1 1 1 1 0 0 1 1 0 0\r\n10\r\nget 2\r\nget 8\r\nget 10\r\nget 5\r\nget 5\r\npow 10\r\nget 10\r\nget 1\r\nget 7\r\npow 4\r\n", "output": "3\r\n1\r\n0\r\n1\r\n1\r\n1\r\n7\r\n1\r\n"}, {"input": "10\r\n1 1 2 2 3 3 4 4 5\r\n1 1 0 1 0 0 0 0 0 0\r\n10\r\nget 2\r\nget 5\r\npow 2\r\npow 4\r\nget 2\r\nget 4\r\npow 7\r\nget 10\r\npow 5\r\nget 6\r\n", "output": "2\r\n0\r\n3\r\n1\r\n1\r\n0\r\n"}]
false
stdio
null
true
39/B
39
B
Python 3
TESTS
0
30
0
207123055
n = int(input()) l = list(map(int,input().split())) lji = [] t = 0 for i in range(n): if l[i] == t + 1: t += 1 lji.append(2000+i+1) if lji: print(*lji) else: print(0)
35
92
0
206831016
input() t = [] for y, d in enumerate(map(int, input().split()), 2001): if d == len(t) + 1: t.append(y) print(len(t), *t)
School Team Contest 1 (Winter Computer School 2010/11)
ICPC
2,010
2
64
Company Income Growth
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 — to a2 billion, ..., and in the current (2000 + n)-th year — an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year — 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the company’s losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth. Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 — 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers ai ( - 100 ≤ ai ≤ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output k — the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
null
null
[{"input": "10\n-2 1 1 3 2 3 4 -10 -2 5", "output": "5\n2002 2005 2006 2007 2010"}, {"input": "3\n-1 -2 -3", "output": "0"}]
1,300
["greedy"]
35
[{"input": "10\r\n-2 1 1 3 2 3 4 -10 -2 5\r\n", "output": "5\r\n2002 2005 2006 2007 2010 "}, {"input": "3\r\n-1 -2 -3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 1\r\n", "output": "1\r\n2002 "}, {"input": "2\r\n-1 1\r\n", "output": "1\r\n2002 "}, {"input": "2\r\n-2 0\r\n", "output": "0\r\n"}, {"input": "2\r\n3 -3\r\n", "output": "0\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n2001 "}, {"input": "3\r\n-2 -2 1\r\n", "output": "1\r\n2003 "}, {"input": "4\r\n-4 2 3 -1\r\n", "output": "0\r\n"}, {"input": "5\r\n-3 -3 -4 2 -2\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n2001 "}, {"input": "2\r\n1 2\r\n", "output": "2\r\n2001 2002 "}, {"input": "100\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 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 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 3 1 3 3 2 1 2 1 2\r\n", "output": "2\r\n2003 2006 "}, {"input": "15\r\n4 1 4 6 3 2 1 1 3 2 4 4 1 4 1\r\n", "output": "4\r\n2002 2006 2009 2011 "}, {"input": "15\r\n3 3 3 2 2 2 1 1 1 2 2 2 4 4 4\r\n", "output": "2\r\n2007 2010 "}, {"input": "15\r\n6 5 2 3 4 1 3 2 4 5 1 2 6 4 4\r\n", "output": "2\r\n2006 2008 "}]
false
stdio
null
true
985/C
985
C
PyPy 3
TESTS
7
249
10,649,600
94202583
import sys n, k, l = map(int, input().split()) a = sorted(map(int, input().split())) ans = a[0] pair, right = 1, 0 for i in range(n*k-1, 0, -1): if right < k - 1: right += 1 continue if a[i] - a[0] <= l: ans += a[i] right -= 1 pair += 1 else: right += 1 if pair < n: print(0) else: print(ans)
50
124
17,817,600
209233530
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor N,M,K = map(int,input().split()) A = sorted(list(map(int,input().split()))) l,r = [],[] num = min(A)+K for x in range(N*M): if A[x]<=num: l.append(A[x]) else: r.append(A[x]) l = l ansi = 0 ans = [] num = len(r) for _ in range(N): numi = 1 if l==[]: ansi=0 break k = [l.pop()] for _ in range(M-1): if num==0:break k.append(r.pop()) numi+=1 num-=1 for _ in range(M-numi): k.append(l.pop()) ansi += min(k) print(ansi)
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
2
256
Liebig's Barrels
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel. Let volume vj of barrel j be equal to the length of the minimal stave in it. You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n. Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109). The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
null
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3]. In the second example you can form the following barrels: [10], [10]. In the third example you can form the following barrels: [2, 5]. In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
1,500
["greedy"]
50
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
985/C
985
C
Python 3
TESTS
7
295
7,168,000
38547657
(n, k, l) = [int(i) for i in input().split()] lengths = input().split() for i in range(n * k): lengths[i] = int(lengths[i]) smallest = min(lengths) biggest = smallest + l M = 0 for i in lengths: if smallest <= i <= biggest: M += 1 ans = 0 if M < n: print(0) else: i = 1 import heapq heapq.heapify(lengths) while (n - i) <= (M - k * i) and (n - i) > 0: ans += heapq.heappop(lengths) for j in range(k - 1): heapq.heappop(lengths) i += 1 for j in range(n - i + 1): ans += heapq.heappop(lengths) print(ans)
50
140
17,305,600
217517683
import sys input = sys.stdin.buffer.readline def process(n, k, l, A): A.sort() my_min = A[0] spare = [] answer = [] while len(A) > 0 and A[-1] > A[0]+l: spare.append(A.pop()) i = 0 while len(A) > 0: if len(spare) >= k-1: L = [A.pop()] for j in range(k-1): L.append(spare.pop()) answer.append(L) else: spare.append(A.pop()) if len(answer) >= n: answer = sum([x[0] for x in answer]) sys.stdout.write(f'{answer}\n') return else: sys.stdout.write(f'0\n') return n, k, l = [int(x) for x in input().split()] A = [int(x) for x in input().split()] process(n, k, l, A)
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
2
256
Liebig's Barrels
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel. Let volume vj of barrel j be equal to the length of the minimal stave in it. You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n. Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109). The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
null
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3]. In the second example you can form the following barrels: [10], [10]. In the third example you can form the following barrels: [2, 5]. In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
1,500
["greedy"]
50
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
732/D
732
D
Python 3
TESTS
3
77
1,536,000
200341009
import sys input = sys.stdin.readline n, m = map(int, input().split()) sched = list(map(int, input().split())) time = list(map(int, input().split())) l = 0 r = n - 1 ans = -1 def check(n): global sched, time, m days = 0 passed = {} pass_num = 0 for i in range(n-1,-1,-1): if sched[i] > 0: if passed.get(sched[i], 0) == 0: passed[sched[i]] = 1 pass_num += 1 days += time[sched[i]-1] else: days -= 1 else: days -= 1 if pass_num < m or days > 0: return False return True while l <= r: mid = (l+r)//2 if check(mid): ans = mid r = mid - 1 else: l = mid + 1 print(ans)
53
155
8,601,600
199733920
n,m = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) sums = sum(B)+len(B) last = [-1] * m cou = 0 ans = 0 per = 0 for j in range(n): if A[j] == 0: cou +=1 else: if last[A[j]-1] == -1: if j >= B[A[j]-1]: ans += 1 last[A[j]-1] = 0 if ans == m: if (j+1) >= sums: per=1 print(j+1) break if per == 0: print(-1)
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Exams
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects. The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i. The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
null
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
[{"input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5"}, {"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9"}, {"input": "5 1\n1 1 1 1 1\n5", "output": "-1"}]
1,700
["binary search", "greedy", "sortings"]
53
[{"input": "7 2\r\n0 1 0 2 1 0 2\r\n2 1\r\n", "output": "5\r\n"}, {"input": "10 3\r\n0 0 1 2 3 0 2 0 1 2\r\n1 1 4\r\n", "output": "9\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n5\r\n", "output": "-1\r\n"}, {"input": "100 10\r\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\r\n2 4 10 11 5 2 6 7 2 15\r\n", "output": "74\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n0 0 0\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n0 1 0 2\r\n1 1\r\n", "output": "4\r\n"}, {"input": "10 1\r\n0 1 0 0 0 0 0 0 0 1\r\n1\r\n", "output": "2\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n1\r\n", "output": "5\r\n"}, {"input": "7 2\r\n0 0 0 0 0 1 2\r\n1 1\r\n", "output": "7\r\n"}, {"input": "10 3\r\n0 0 1 2 2 0 2 0 1 3\r\n1 1 4\r\n", "output": "10\r\n"}, {"input": "6 2\r\n1 1 1 1 1 2\r\n1 1\r\n", "output": "6\r\n"}, {"input": "6 2\r\n1 0 0 0 0 2\r\n1 1\r\n", "output": "-1\r\n"}]
false
stdio
null
true
187/A
187
A
Python 3
TESTS
5
218
3,072,000
57770285
import sys N = int(input()) current = input().split(" ") real = input().split(" ") positions = [-1 for i in range(int(2e5))] for i, val in enumerate(real): positions[int(val)-1] = i # print(positions[:N]) last_pos = -1 for i, val in enumerate(current): # print(val, positions[int(val)-1] - i) if positions[int(val)-1] - i < 0: print(N - i) sys.exit() elif positions[int(val) - 1] - i > 0: if last_pos == -1: last_pos = i else: if last_pos != -1: print(N - i) sys.exit() print(0)
58
746
17,305,600
140347188
input() perm_target = list(map(int, input().split())) perm_given = list(map(int, input().split())) cursor = 0 move = 0 for val in perm_given: if val == perm_target[cursor]: cursor += 1 else: move += 1 print(move)
Codeforces Round 119 (Div. 1)
CF
2,012
2
256
Permutations
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation. Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations. Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once. Next line describe the second permutation in the same format.
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
null
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. In the second sample, he removes number 5 and inserts it after 1. In the third sample, the sequence of changes are like this: - 1 5 2 3 4 - 1 4 5 2 3 - 1 3 4 5 2 - 1 2 3 4 5
[{"input": "3\n3 2 1\n1 2 3", "output": "2"}, {"input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1"}, {"input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3"}]
1,500
["greedy"]
58
[{"input": "3\r\n3 2 1\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 5 2 3 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 2 3 4\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "7\r\n6 1 7 3 4 5 2\r\n6 1 7 3 4 5 2\r\n", "output": "0\r\n"}, {"input": "10\r\n5 8 1 10 3 6 2 9 7 4\r\n4 2 6 3 1 9 10 5 8 7\r\n", "output": "8\r\n"}, {"input": "10\r\n1 6 10 3 4 9 2 5 8 7\r\n7 5 1 6 10 3 4 8 9 2\r\n", "output": "3\r\n"}, {"input": "10\r\n2 1 10 3 7 8 5 6 9 4\r\n6 9 2 4 1 10 3 7 8 5\r\n", "output": "3\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "0\r\n"}, {"input": "20\r\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\r\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1\r\n", "output": "19\r\n"}]
false
stdio
null
true
390/A
390
A
Python 3
TESTS
14
857
9,113,600
58496768
n = int(input()) v = 1000 g = 1000 vertical = n gorizontal = n a = [] ax = [] ay = [] for i in range(n): s = input() a1 = list(map(int, s.split())) a.append(a1) for i in range(n): ax.append(a[i][0]) ay.append(a[i][1]) for i in range(101): if ax.count(i) > 0 or ay.count(i) > 0: if ax.count(i) > 0: vertical -= ax.count(i) vertical += 1 else: gorizontal -= ay.count(i) gorizontal += 1 else: continue print(min(gorizontal, vertical))
19
140
0
220411674
# print(max(0 , minimum)) n = int(input()) x , y = set() , set() for i in range(n): input_ = input().split() x.add(input_[0]) y.add(input_[1]) print(min(len(x) , len(y)))
Codeforces Round 229 (Div. 2)
CF
2,014
1
256
Inna and Alarm Clock
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square. The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game: - First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal. - Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off. Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100). Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
null
In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments. In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.
[{"input": "4\n0 0\n0 1\n0 2\n1 0", "output": "2"}, {"input": "4\n0 0\n0 1\n1 0\n1 1", "output": "2"}, {"input": "4\n1 1\n1 2\n2 3\n3 3", "output": "3"}]
null
["implementation"]
19
[{"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 0\r\n", "output": "2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1\r\n1 2\r\n2 3\r\n3 3\r\n", "output": "3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "1\r\n"}, {"input": "42\r\n28 87\r\n26 16\r\n59 90\r\n47 61\r\n28 83\r\n36 30\r\n67 10\r\n6 95\r\n9 49\r\n86 94\r\n52 24\r\n74 9\r\n86 24\r\n28 51\r\n25 99\r\n40 98\r\n57 33\r\n18 96\r\n43 36\r\n3 79\r\n4 86\r\n38 61\r\n25 61\r\n6 100\r\n58 81\r\n28 19\r\n64 4\r\n3 40\r\n2 56\r\n41 49\r\n97 100\r\n86 34\r\n42 36\r\n44 40\r\n14 85\r\n21 60\r\n76 99\r\n64 47\r\n69 13\r\n49 37\r\n97 37\r\n3 70\r\n", "output": "31\r\n"}, {"input": "21\r\n54 85\r\n69 37\r\n42 87\r\n53 18\r\n28 22\r\n13 3\r\n62 97\r\n38 91\r\n67 19\r\n100 79\r\n29 18\r\n48 40\r\n68 84\r\n44 20\r\n37 34\r\n73 53\r\n21 5\r\n20 73\r\n24 94\r\n23 52\r\n7 55\r\n", "output": "20\r\n"}, {"input": "19\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n1 11\r\n1 12\r\n1 13\r\n1 14\r\n1 15\r\n1 16\r\n1 17\r\n1 18\r\n1 19\r\n", "output": "1\r\n"}, {"input": "12\r\n1 1\r\n1 3\r\n1 5\r\n2 1\r\n2 2\r\n2 4\r\n3 1\r\n3 3\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}]
false
stdio
null
true
652/B
652
B
Python 3
TESTS
5
109
0
43898571
n = int(input()) array = input().split(" ") # поскольку вводится все в строку # то мы делаем переход к массиву этим методом (просто делим строку на список по пробелам) MaxArray = [] # массив с максимальными элементами из array for i in range(n // 2): # цикл, в котором вырываются максимальные эл из array # и загоняются в MaxArray (меня интересует только половина таких) MaxArray.append(max(array)) array.remove(MaxArray[i]) if n % 2 != 0: print(array[0], end = ' ') array.remove(array[0]) for i in range(n // 2): print(MaxArray[(n // 2) - 1 - i], array[i], end = ' ') else: for i in range(n // 2): print(array[i], MaxArray[(n // 2) - 1 - i], end = ' ')
16
31
0
214427385
from itertools import zip_longest n, a = int(input()), sorted(int(i) for i in input().split()) mid = (n + 1)//2 res = [i for x, y in zip_longest(a[:mid], a[mid:][::-1], fillvalue=0) for i in (x, y)] if n & 1 == 1: res.pop() print(*res)
Educational Codeforces Round 10
ICPC
2,016
1
256
z-sort
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: 1. ai ≥ ai - 1 for all even i, 2. ai ≤ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted. Can you make the array z-sorted?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
null
null
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
1,000
["sortings"]
16
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\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 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": "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 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"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\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 = int(f.readline().strip()) a = list(map(int, f.readline().split())) a_sorted = sorted(a) # Read reference output with open(output_path) as f: ref_lines = [line.strip() for line in f] ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible') # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f] if not sub_lines: print(0) return sub_output = sub_lines[0].split() sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible') # Check if submission matches the reference's impossible status if ref_is_impossible: print(1 if sub_is_impossible else 0) return else: if sub_is_impossible: print(0) return # Check if the submission is a valid permutation and meets conditions if len(sub_output) != n: print(0) return try: sub_array = list(map(int, sub_output)) except: print(0) return if sorted(sub_array) != a_sorted: print(0) return # Check z-sort conditions valid = True for i in range(1, n): if (i + 1) % 2 == 0: # 1-based even index if sub_array[i] < sub_array[i - 1]: valid = False break else: # 1-based odd index > 1 if sub_array[i] > sub_array[i - 1]: valid = False break print(1 if valid else 0) if __name__ == "__main__": main()
true
653/D
653
D
Python 3
TESTS
3
93
409,600
101018122
import sys from random import randint from math import * class Graph: verticies = {} nodesCount = 0 class Vertex: def __init__(self, label, endPoint=None): self.label = label self.edges = [] self.visitedToken = 0 self.endPoint = endPoint class Edge: residual = None def __init__(self, from_, to_, isResidual, maxCapacity): self.from_ = from_ self.to_ = to_ self.isResidual = isResidual self.capacity = maxCapacity self.flow = 0 def augment(self, bootleneck): self.flow += bootleneck self.residual.flow -= bootleneck def remainingCapacity(self): return self.capacity - self.flow def addEdge(self, from_, to_, capacity): from_ = self.verticies[from_] to_ = self.verticies[to_] if from_.endPoint and from_.endPoint != to_: from_ = from_.endPoint main = self.Edge(from_, to_, False, capacity) residual = self.Edge(to_, from_, True, 0) main.residual = residual residual.residual = main from_.edges.append(main) to_.edges.append(residual) def addVertex(self, label, *args): self.nodesCount += 1 if args: capacity = args[0] key = str(randint(0, sys.maxsize)) + '--' + str(label) endPoint = self.Vertex(key) self.verticies[key] = endPoint self.verticies[label] = self.Vertex(label, endPoint=endPoint) self.addEdge(label, key, capacity) else: self.verticies[label] = self.Vertex(label) def maxFlow(self, f, t): f = self.verticies[f] t = self.verticies[t] visitedToken = 1 flow = 0 def dfs(node, bootleneck=sys.maxsize): node.visitedToken = visitedToken bootleneck_backup = bootleneck if node == t: return bootleneck for edge in node.edges: if edge.remainingCapacity() == 0 or edge.to_.visitedToken == visitedToken: continue bootleneck = dfs(edge.to_, min( bootleneck, edge.remainingCapacity())) if bootleneck: edge.augment(bootleneck) return bootleneck else: bootleneck = bootleneck_backup return 0 while True: bootleneck = dfs(f) if not bootleneck: break flow += bootleneck visitedToken += 1 return flow n, m, x = map(int, input().split()) matrix = [[0 for _ in range(n+1)] for _ in range(n+1)] g = Graph() for i in range(1, n+1): g.addVertex(i) for i in range(m): a, b, c = map(int, input().split()) g.addEdge(a, b, c) # matrix[a][b] = c flow = g.maxFlow(1, n) bpf = x/flow paths = [] for e in g.verticies[1].edges: if not e.isResidual: paths.append(e.flow) ans = max(paths)/(max(paths) * int(bpf) + (int(bpf) != bpf))*x print(round(ans*10**10)/10**10)
47
577
716,800
101171262
from collections import deque class Dinic(): def __init__(self, listEdge, s, t): self.s = s self.t = t self.graph = {} self.maxCap = 1000000 # dict các node lân cận # e[0]: from, e[1]: to, e[2]: dung luong for e in listEdge: if e[0] not in self.graph: self.graph[e[0]] = [] if e[1] not in self.graph: self.graph[e[1]] = [] #to #cap #reveser edge self.graph[e[0]].append([e[1], e[2], len(self.graph[e[1]])]) self.graph[e[1]].append([e[0], 0, len(self.graph[e[0]])-1]) self.N = len(self.graph.keys()) def bfs(self): self.dist = {} self.dist[self.s] = 0 self.curIter = {node:[] for node in self.graph} Q = deque([self.s]) while(len(Q) > 0): cur = Q.popleft() for index,e in enumerate(self.graph[cur]): # Chỉ add vào các node kế tiếp nếu dung lượng cạnh > 0 và chưa được visit trước đấy if e[1] > 0 and e[0] not in self.dist: self.dist[e[0]] = self.dist[cur] + 1 # add vào danh sách node kế tiếp của node hiện tại self.curIter[cur].append(index) Q.append(e[0]) def findPath(self, cur, f): if cur == self.t: return f while len(self.curIter[cur]) > 0: indexEdge = self.curIter[cur][-1] nextNode = self.graph[cur][indexEdge][0] remainCap = self.graph[cur][indexEdge][1] indexPreEdge = self.graph[cur][indexEdge][2] if remainCap > 0 and self.dist[nextNode] > self.dist[cur]: #self.next[cur] = indexEdge flow = self.findPath(nextNode, min(f, remainCap)) if flow > 0: self.path.append(cur) self.graph[cur][indexEdge][1] -= flow self.graph[nextNode][indexPreEdge][1] += flow #if cur == self.s: # print(self.path, flow) return flow #else: #self.path.pop() self.curIter[cur].pop() return 0 def maxFlow(self): maxflow = 0 flow = [] while(True): self.bfs() if self.t not in self.dist: break while(True): self.path = [] f = self.findPath(self.s, self.maxCap) #print('iter', self.curIter) if f == 0: break flow.append(f) maxflow += f return maxflow # Tìm tập node thuộc S và T # sau khi đã tìm được max flow def residualBfs(self): Q = deque([self.s]) side = {self.s:'s'} while(len(Q) > 0): cur = Q.popleft() for index,e in enumerate(self.graph[cur]): if e[1] > 0 and e[0] not in side: Q.append(e[0]) side[e[0]] = 's' S = [] T = [] for x in self.graph: if x in side: S.append(x) else: T.append(x) return set(S), set(T) n, m, X = map(int, input().split()) edge = [list(map(int, input().split())) for _ in range(m)] l, r = 0, 1000000 while r-l>1e-9: #print(l, r) md = (r+l) / 2 edge_ = [[u, v, w // md] for u, v, w in edge] g = Dinic(edge_, 1, n) maxflow = g.maxFlow() if maxflow >= X: l=md else: r=md print(X*r)
IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)
CF
2,016
2
256
Delivery Bears
Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city. In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with n nodes and m edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node n. The total weight that travels across a particular edge must not exceed the weight capacity of that edge. Niwel has exactly x bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like. Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight.
The first line contains three integers n, m and x (2 ≤ n ≤ 50, 1 ≤ m ≤ 500, 1 ≤ x ≤ 100 000) — the number of nodes, the number of directed edges and the number of bears, respectively. Each of the following m lines contains three integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1 000 000). This represents a directed edge from node ai to bi with weight capacity ci. There are no self loops and no multiple edges from one city to the other city. More formally, for each i and j that i ≠ j it's guaranteed that ai ≠ aj or bi ≠ bj. It is also guaranteed that there is at least one path from node 1 to node n.
Print one real value on a single line — the maximum amount of weight Niwel can deliver if he uses exactly x bears. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample, Niwel has three bears. Two bears can choose the path $$1 \rightarrow 3 \rightarrow 4$$, while one bear can choose the path $$1 \rightarrow 2 \rightarrow 4$$. Even though the bear that goes on the path $$1 \rightarrow 2 \rightarrow 4$$ can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.
[{"input": "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2", "output": "1.5000000000"}, {"input": "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30", "output": "10.2222222222"}]
2,200
["binary search", "flows", "graphs"]
47
[{"input": "4 4 3\r\n1 2 2\r\n2 4 1\r\n1 3 1\r\n3 4 2\r\n", "output": "1.5000000000\n"}, {"input": "5 11 23\r\n1 2 3\r\n2 3 4\r\n3 4 5\r\n4 5 6\r\n1 3 4\r\n2 4 5\r\n3 5 6\r\n1 4 2\r\n2 5 3\r\n1 5 2\r\n3 2 30\r\n", "output": "10.2222222222\n"}, {"input": "10 16 63\r\n1 2 1\r\n2 10 1\r\n1 3 1\r\n3 10 1\r\n1 4 1\r\n4 10 1\r\n1 5 1\r\n5 10 1\r\n1 6 1\r\n6 10 1\r\n1 7 1\r\n7 10 1\r\n1 8 1\r\n8 10 1\r\n1 9 1\r\n9 10 1\r\n", "output": "7.8750000000\n"}, {"input": "2 1 3\r\n1 2 301\r\n", "output": "301.0000000000\n"}, {"input": "2 2 1\r\n1 2 48\r\n2 1 39\r\n", "output": "48.0000000000\n"}, {"input": "5 9 5\r\n3 2 188619\r\n4 2 834845\r\n2 4 996667\r\n1 2 946392\r\n2 5 920935\r\n2 3 916558\r\n1 5 433923\r\n4 5 355150\r\n3 5 609814\r\n", "output": "1182990.0000000000\n"}, {"input": "7 15 10\r\n1 3 776124\r\n6 7 769968\r\n2 1 797048\r\n4 3 53774\r\n2 7 305724\r\n4 1 963904\r\n4 6 877656\r\n4 5 971901\r\n1 4 803781\r\n3 1 457050\r\n3 7 915891\r\n1 7 8626\r\n5 7 961155\r\n3 4 891456\r\n5 4 756977\r\n", "output": "1552248.0000000000\n"}, {"input": "3 2 100000\r\n1 2 1\r\n2 3 1\r\n", "output": "1.0000000000\n"}, {"input": "3 2 100000\r\n1 2 1\r\n2 3 1000000\r\n", "output": "1.0000000000\n"}, {"input": "2 1 100000\r\n1 2 1\r\n", "output": "1.0000000000\n"}, {"input": "3 2 100000\r\n1 2 1\r\n2 3 100000\r\n", "output": "1.0000000000\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] correct_output_path = sys.argv[2] submission_output_path = sys.argv[3] # Read correct output with open(correct_output_path, 'r') as f: correct_line = f.readline().strip() correct = float(correct_line.split()[0]) # Read submission output with open(submission_output_path, 'r') as f: sub_line = f.readline().strip() try: sub = float(sub_line) except: print(0) return # Compute the error abs_diff = abs(sub - correct) denominator = max(1, correct) if abs_diff <= 1e-6 * denominator: print(1) else: print(0) if __name__ == '__main__': main()
true
989/D
989
D
Python 3
TESTS
3
93
0
39241036
def bin_search(a, left, right, threshold): while right - left - 1 > 0: m = int((left + right) / 2) if a[m] < threshold: left = m else: right = m return right def main(): n, l, w = [int(x) for x in input().split()] u, v = [], [] for i in range(n): x, vel = [int(x) for x in input().split()] if vel > 0: u.append(x) else: v.append(x) u = sorted(u) v = sorted(v) ans = 0 if w == 1: neg_u = [x for x in u if x < 0] for x in v: r1 = bin_search(u, 0, len(u), min(-(x + l), x)) l2 = bin_search(neg_u, 0, len(neg_u), -(x + l)) r2 = bin_search(neg_u, 0, len(neg_u), x) if (x + l) * (w + 1) > 0: ans += r1 ans += max(0, (r2 - l2)) print(ans) return for x in v: threshold = min((x + l) * (w + 1) / (w - 1), -(x + l), x) r1 = bin_search(u, 0, len(u), threshold) threshold = min((x + l) * (w - 1) / (w + 1), x) r2 = bin_search(u, 0, len(u), threshold) l2 = bin_search(u, 0, len(u), -(x + l)) if l2 <= r1: ans += r2 else: ans += r1 ans += max(0, r2 - l2) print(ans) if __name__ == '__main__': main()
30
841
6,860,800
39193022
# Codeforces Round #487 (Div. 2)import collections from functools import cmp_to_key #key=cmp_to_key(lambda x,y: 1 if x not in y else -1 ) import sys def getIntList(): return list(map(int, input().split())) import bisect N,L,WM = getIntList() z = {} z[-1] = {1:[], -1:[]} z[0] = {1:[], -1:[]} z[1] = {1:[], -1:[]} for i in range(N): x0,v = getIntList() t = (x0,v) if x0+L <=0: z[-1][v].append(t) elif x0>=0: z[1][v].append(t) else: z[0][v].append(t) res = 0 res += len(z[-1][1] ) * len(z[ 1][-1] ) res += len(z[0][1] ) * len(z[ 1][-1] ) res += len(z[-1][1] ) * len(z[ 0][-1] ) if WM==1: print(res) sys.exit() z[1][-1].sort() z[-1][1].sort() #print(z[-1][1]) tn = len(z[1][-1]) for t in z[1][1]: g = (-WM-1) * t[0] / (-WM+1) - L g = max(g, t[0]+ 0.5) p = bisect.bisect_right(z[1][-1], (g,2) ) res += tn-p tn = len(z[-1][1]) for t in z[-1][-1]: g = (WM+1) * (t[0] + L)/ (WM-1) g = min(g, t[0] - 0.1) p = bisect.bisect_left(z[-1][1], (g,-2) ) res += p print(res)
Codeforces Round 487 (Div. 2)
CF
2,018
2
256
A Shade of Moonlight
The sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is $$$0$$$. There are $$$n$$$ clouds floating in the sky. Each cloud has the same length $$$l$$$. The $$$i$$$-th initially covers the range of $$$(x_i, x_i + l)$$$ (endpoints excluded). Initially, it moves at a velocity of $$$v_i$$$, which equals either $$$1$$$ or $$$-1$$$. Furthermore, no pair of clouds intersect initially, that is, for all $$$1 \leq i \lt j \leq n$$$, $$$\lvert x_i - x_j \rvert \geq l$$$. With a wind velocity of $$$w$$$, the velocity of the $$$i$$$-th cloud becomes $$$v_i + w$$$. That is, its coordinate increases by $$$v_i + w$$$ during each unit of time. Note that the wind can be strong and clouds can change their direction. You are to help Mino count the number of pairs $$$(i, j)$$$ ($$$i < j$$$), such that with a proper choice of wind velocity $$$w$$$ not exceeding $$$w_\mathrm{max}$$$ in absolute value (possibly negative and/or fractional), the $$$i$$$-th and $$$j$$$-th clouds both cover the moon at the same future moment. This $$$w$$$ doesn't need to be the same across different pairs.
The first line contains three space-separated integers $$$n$$$, $$$l$$$, and $$$w_\mathrm{max}$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq l, w_\mathrm{max} \leq 10^8$$$) — the number of clouds, the length of each cloud and the maximum wind speed, respectively. The $$$i$$$-th of the following $$$n$$$ lines contains two space-separated integers $$$x_i$$$ and $$$v_i$$$ ($$$-10^8 \leq x_i \leq 10^8$$$, $$$v_i \in \{-1, 1\}$$$) — the initial position and the velocity of the $$$i$$$-th cloud, respectively. The input guarantees that for all $$$1 \leq i \lt j \leq n$$$, $$$\lvert x_i - x_j \rvert \geq l$$$.
Output one integer — the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity $$$w$$$.
null
In the first example, the initial positions and velocities of clouds are illustrated below. The pairs are: - $$$(1, 3)$$$, covering the moon at time $$$2.5$$$ with $$$w = -0.4$$$; - $$$(1, 4)$$$, covering the moon at time $$$3.5$$$ with $$$w = -0.6$$$; - $$$(1, 5)$$$, covering the moon at time $$$4.5$$$ with $$$w = -0.7$$$; - $$$(2, 5)$$$, covering the moon at time $$$2.5$$$ with $$$w = -2$$$. Below is the positions of clouds at time $$$2.5$$$ with $$$w = -0.4$$$. At this moment, the $$$1$$$-st and $$$3$$$-rd clouds both cover the moon. In the second example, the only pair is $$$(1, 4)$$$, covering the moon at time $$$15$$$ with $$$w = 0$$$. Note that all the times and wind velocities given above are just examples among infinitely many choices.
[{"input": "5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1", "output": "4"}, {"input": "4 10 1\n-20 1\n-10 -1\n0 1\n10 -1", "output": "1"}]
2,500
["binary search", "geometry", "math", "sortings", "two pointers"]
30
[{"input": "5 1 2\r\n-2 1\r\n2 1\r\n3 -1\r\n5 -1\r\n7 -1\r\n", "output": "4\r\n"}, {"input": "4 10 1\r\n-20 1\r\n-10 -1\r\n0 1\r\n10 -1\r\n", "output": "1\r\n"}, {"input": "1 100000000 98765432\r\n73740702 1\r\n", "output": "0\r\n"}, {"input": "10 2 3\r\n-1 -1\r\n-4 1\r\n-6 -1\r\n1 1\r\n10 -1\r\n-8 -1\r\n6 1\r\n8 1\r\n4 -1\r\n-10 -1\r\n", "output": "5\r\n"}, {"input": "3 100000000 100000000\r\n-100000000 1\r\n100000000 1\r\n0 -1\r\n", "output": "1\r\n"}, {"input": "9 25000000 989\r\n-100000000 -1\r\n-75000000 1\r\n75000000 1\r\n50000000 -1\r\n-50000000 1\r\n0 1\r\n25000000 1\r\n-25000000 -1\r\n100000000 -1\r\n", "output": "11\r\n"}, {"input": "2 5 1\r\n-2 1\r\n5 -1\r\n", "output": "1\r\n"}, {"input": "2 5 1\r\n-9 -1\r\n-2 1\r\n", "output": "0\r\n"}, {"input": "3 4 5\r\n9 1\r\n-4 1\r\n-8 -1\r\n", "output": "0\r\n"}, {"input": "5 1 1\r\n-6 1\r\n15 1\r\n-7 1\r\n-13 -1\r\n12 -1\r\n", "output": "2\r\n"}, {"input": "50 1 19\r\n-5213 -1\r\n2021 -1\r\n-4479 1\r\n1569 -1\r\n1618 1\r\n-8318 1\r\n3854 1\r\n8190 -1\r\n9162 1\r\n8849 1\r\n-5545 -1\r\n-7898 -1\r\n728 1\r\n-2175 -1\r\n6453 -1\r\n2999 1\r\n4716 1\r\n-2192 -1\r\n7938 -1\r\n1910 -1\r\n-6863 -1\r\n5230 -1\r\n-2782 -1\r\n-2587 -1\r\n-3389 1\r\n-332 -1\r\n5915 1\r\n-2604 1\r\n-8907 1\r\n-2019 1\r\n2992 1\r\n-3279 -1\r\n6720 1\r\n4332 1\r\n8789 -1\r\n2003 1\r\n-8046 -1\r\n-594 -1\r\n-4133 -1\r\n-7954 -1\r\n-6270 -1\r\n4042 -1\r\n3650 1\r\n-8569 1\r\n2529 -1\r\n266 -1\r\n3405 -1\r\n-9753 1\r\n1205 -1\r\n6437 -1\r\n", "output": "262\r\n"}, {"input": "50 100 40\r\n4843 -1\r\n7653 1\r\n5391 1\r\n-1651 1\r\n-8530 -1\r\n9770 1\r\n2721 1\r\n7321 1\r\n-3636 -1\r\n-1525 -1\r\n-3060 1\r\n1877 -1\r\n3771 -1\r\n-7651 1\r\n581 -1\r\n1127 -1\r\n6966 -1\r\n-6089 1\r\n1465 -1\r\n3147 -1\r\n-6927 -1\r\n4477 1\r\n-6535 1\r\n5991 -1\r\n-2740 1\r\n5021 1\r\n-7761 -1\r\n4626 1\r\n9958 1\r\n4275 1\r\n5695 1\r\n8835 -1\r\n7791 -1\r\n189 -1\r\n-170 1\r\n-4468 -1\r\n-708 1\r\n34 -1\r\n-9068 1\r\n6424 -1\r\n-2066 -1\r\n-7367 1\r\n6224 1\r\n3329 1\r\n-1809 -1\r\n7105 1\r\n-4607 -1\r\n-3174 -1\r\n-9782 -1\r\n1350 -1\r\n", "output": "253\r\n"}]
false
stdio
null
true
484/D
484
D
PyPy 3-64
TESTS
7
61
0
229108932
n=int(input()) lst=list(map(int,input().split())) freq=[0 for i in range(n)] for i in range(1,len(lst)): ans=0 for j in range(i-1,-1,-1): if(i==0): ans=max(ans,abs(lst[i]-lst[j])) else: ans=max(ans,abs(lst[i]-lst[j])+freq[j-1]) freq[i]=ans print(freq[-1])
71
373
5,427,200
223452331
import sys inf = float('inf') def read_int(): res = b'' while True: d = sys.stdin.buffer.read(1) if d == b'-' or d.isdigit(): res += d elif res: break return int(res) n = read_int() dp, neg, pos = 0, -inf, -inf for _ in range(n): v = read_int() dp, neg, pos = max(dp, neg + v, pos - v), max(neg, dp - v), max(pos, dp + v) print(dp)
Codeforces Round 276 (Div. 1)
CF
2,014
2
256
Kindergarten
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero). The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106). The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Print the maximum possible total sociability of all groups.
null
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1. In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
[{"input": "5\n1 2 3 1 2", "output": "3"}, {"input": "3\n3 3 3", "output": "0"}]
2,400
["data structures", "dp", "greedy"]
71
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n3 3 3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1000000000 1000000000\r\n", "output": "2000000000\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "4\r\n"}, {"input": "4\r\n23 5 7 1\r\n", "output": "24\r\n"}, {"input": "4\r\n23 7 5 1\r\n", "output": "22\r\n"}, {"input": "8\r\n23 2 7 5 15 8 4 10\r\n", "output": "37\r\n"}, {"input": "8\r\n4 5 3 6 2 7 1 8\r\n", "output": "16\r\n"}]
false
stdio
null
true
484/B
484
B
PyPy 3-64
TESTS
37
592
26,624,000
195237787
import fractions import gc import heapq import itertools from itertools import combinations, permutations import math import random 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 from bisect import bisect_left, bisect_right 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 = II() a = I() a.sort() ans = 0 for i in range(n - 1): for j in range(max(i + 1, n - 100), n): ans = max(ans, a[j] % a[i]) print(ans) if __name__ == '__main__': mod = 998244353 EPS = 10 ** -6 # for _ in range(II()): # main() main()
45
342
40,652,800
130048879
import bisect n = int(input()) arr = set(map(int,input().split())) ls = sorted(arr) ans = 0 mx = max(arr) for c in ls[::-1]: if c-1<ans: break kc = c+c while kc<=2*mx: ind = bisect.bisect_left(ls,kc) ans = max(ans,ls[ind-1]%c) kc+=c print(ans)
Codeforces Round 276 (Div. 1)
CF
2,014
1
256
Maximum Value
You are given a sequence a consisting of n integers. Find the maximum possible value of $$a_i \bmod a_j$$ (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105). The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).
Print the answer to the problem.
null
null
[{"input": "3\n3 4 5", "output": "2"}]
2,100
["binary search", "math", "sortings", "two pointers"]
45
[{"input": "3\r\n3 4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n1 2 4\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000 999999\r\n", "output": "1\r\n"}, {"input": "12\r\n4 4 10 13 28 30 41 43 58 61 70 88\r\n", "output": "30\r\n"}, {"input": "7\r\n2 13 22 32 72 91 96\r\n", "output": "27\r\n"}, {"input": "5\r\n5 11 12 109 110\r\n", "output": "10\r\n"}]
false
stdio
null
true
651/B
651
B
Python 3
TESTS
5
46
307,200
219543990
from collections import defaultdict d=defaultdict(lambda:0) n=int(input()) a=list(map(int,input().split())) for i in a: d[i]+=1 x=list(d.values()) s=sum(x)-x[0] ans=0 for i in x: if s-i<=0: ans+=s break ans+=i s-=i print(ans)
31
46
0
199701606
n = int(input()) a = list(map(int, input().split())) arr = [0] * 10001 ck = 0 for item in a: arr[item] += 1 ck = max(arr[item], ck) print(n - ck)
Codeforces Round 345 (Div. 2)
CF
2,016
1
256
Beautiful Paintings
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one. We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
null
In the first sample, the optimal order is: 10, 20, 30, 40, 50. In the second sample, the optimal order is: 100, 200, 100, 200.
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
1,200
["greedy", "sortings"]
31
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
false
stdio
null
true
615/B
615
B
PyPy 3-64
TESTS
42
1,715
17,203,200
139011356
n, m = map(int, input().split()) adj_list = [[] for i in range(n+1)] for i in range(m): a,b = map(int, input().split()) adj_list[a].append(b) adj_list[b].append(a) max_len = [0 for i in range(n+1)] for i in range(2, n+1): for neighbor in adj_list[i]: if neighbor < i: max_len[i] = max(max_len[i], max_len[neighbor] + 1) ans = 0 for i in range(2, n+1): ans = max(ans, (max_len[i] + 1)*len(adj_list[i])) print(ans)
60
311
19,660,800
209700728
import sys input = lambda: sys.stdin.readline().rstrip() N,M = map(int, input().split()) P = [[] for _ in range(N)] for _ in range(M): u,v = map(int, input().split()) u-=1;v-=1 P[u].append(v) P[v].append(u) cnt = [1]*N for i in range(N): for j in P[i]: if j>i: cnt[j] = max(cnt[j], cnt[i]+1) ans = 0 for i in range(N): ans = max(ans, cnt[i]*len(P[i])) print(ans)
Codeforces Round 338 (Div. 2)
CF
2,016
3
256
Longtail Hedgehog
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: 1. Only segments already presented on the picture can be painted; 2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 3. The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get. Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
First line of the input contains two integers n and m(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively. Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
Print the maximum possible value of the hedgehog's beauty.
null
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.
[{"input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7", "output": "9"}, {"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "12"}]
1,600
["dp", "graphs"]
60
[{"input": "8 6\r\n4 5\r\n3 5\r\n2 5\r\n1 2\r\n2 8\r\n6 7\r\n", "output": "9\r\n"}, {"input": "4 6\r\n1 2\r\n1 3\r\n1 4\r\n2 3\r\n2 4\r\n3 4\r\n", "output": "12\r\n"}, {"input": "5 7\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n", "output": "9\r\n"}, {"input": "5 9\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n1 5\r\n2 5\r\n", "output": "16\r\n"}, {"input": "10 10\r\n6 3\r\n2 9\r\n9 4\r\n4 5\r\n10 3\r\n8 3\r\n10 5\r\n7 6\r\n1 4\r\n6 8\r\n", "output": "8\r\n"}, {"input": "100 50\r\n66 3\r\n92 79\r\n9 44\r\n84 45\r\n30 63\r\n30 20\r\n33 86\r\n8 83\r\n40 75\r\n7 36\r\n91 4\r\n76 88\r\n77 76\r\n28 27\r\n6 52\r\n41 57\r\n8 23\r\n34 75\r\n50 15\r\n86 68\r\n36 98\r\n30 84\r\n37 62\r\n22 4\r\n6 45\r\n72 80\r\n98 74\r\n78 84\r\n1 54\r\n99 27\r\n84 91\r\n78 7\r\n80 61\r\n67 48\r\n51 52\r\n36 72\r\n97 87\r\n25 17\r\n20 80\r\n20 39\r\n72 5\r\n21 77\r\n48 1\r\n63 21\r\n92 45\r\n34 93\r\n28 84\r\n3 91\r\n56 99\r\n7 53\r\n", "output": "15\r\n"}, {"input": "5 8\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n1 5\r\n", "output": "12\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "2\r\n"}, {"input": "10 9\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n", "output": "9\r\n"}, {"input": "5 4\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n", "output": "4\r\n"}, {"input": "6 5\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n", "output": "5\r\n"}]
false
stdio
null
true
652/E
652
E
Python 3
TESTS
0
31
0
213470690
# LUOGU_RID: 115102892 print("NO")
83
1,372
250,777,600
229380087
if True: from io import BytesIO, IOBase import math import random import sys import os import bisect import typing from collections import Counter, defaultdict, deque from copy import deepcopy from functools import cmp_to_key, lru_cache, reduce from heapq import heapify, heappop, heappush, heappushpop, nlargest, nsmallest from itertools import accumulate, combinations, permutations, count from operator import add, iand, ior, itemgetter, mul, xor from string import ascii_lowercase, ascii_uppercase, ascii_letters from typing import * 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 = IOWrapper(sys.stdin) sys.stdout = IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def I(): return input() def II(): return int(input()) def MII(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(int, input().split())) def LFI(): return list(map(float, 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())) inf = float('inf') dfs, hashing = True, True if dfs: from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc if hashing: RANDOM = random.getrandbits(20) class Wrapper(int): def __init__(self, x): int.__init__(x) def __hash__(self): return super(Wrapper, self).__hash__() ^ RANDOM n, m = MII() path = [[] for _ in range(n)] edges = [] for _ in range(m): u, v, t = MII() u -= 1 v -= 1 edges.append((u, v, t)) path[u].append(v) path[v].append(u) dfn = [0] * n low = [0] * n col = [0] * n color = tmstamp = 0 stack = [] @bootstrap def tarjan(u, f): global color, tmstamp tmstamp += 1 dfn[u] = low[u] = tmstamp stack.append(u) for v in path[u]: if dfn[v] == 0: yield tarjan(v, u) low[u] = min(low[u], low[v]) elif v != f: low[u] = min(low[u], dfn[v]) if low[u] == dfn[u]: while True: col[stack[-1]] = color tmp = stack.pop() if tmp == u: break color += 1 yield tarjan(0, -1) u, v = GMI() start, end = col[u], col[v] vals = [0] * color new_graph = [[] for _ in range(color)] for u, v, t in edges: if col[u] == col[v]: if t: vals[col[u]] = 1 else: new_graph[col[u]].append((col[v], t)) new_graph[col[v]].append((col[u], t)) calc = [-1] * color calc[start] = vals[start] stack = [start] while stack: u = stack.pop() for v, t in new_graph[u]: if calc[v] == -1: calc[v] = max(calc[u], t, vals[v]) stack.append(v) print('YES' if calc[end] else 'NO')
Educational Codeforces Round 10
ICPC
2,016
3
512
Pursuit For Artifacts
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience. In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact. At the start Johnny is in the island a and the artifact-dealer is in the island b (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult. Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island. Determine if Johnny can find some artifact and sell it.
The first line contains two integers n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of islands and bridges in the game. Each of the next m lines contains the description of the bridge — three integers xi, yi, zi (1 ≤ xi, yi ≤ n, xi ≠ yi, 0 ≤ zi ≤ 1), where xi and yi are the islands connected by the i-th bridge, zi equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands. The last line contains two integers a and b (1 ≤ a, b ≤ n) — the islands where are Johnny and the artifact-dealer respectively.
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
null
null
[{"input": "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6", "output": "YES"}, {"input": "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4", "output": "NO"}, {"input": "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2", "output": "YES"}]
2,300
["dfs and similar", "dsu", "graphs", "trees"]
83
[{"input": "6 7\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 1\r\n4 5 0\r\n5 6 0\r\n6 4 0\r\n1 6\r\n", "output": "YES\r\n"}, {"input": "5 4\r\n1 2 0\r\n2 3 0\r\n3 4 0\r\n2 5 1\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "5 6\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 0\r\n4 5 1\r\n5 3 0\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "1 0\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "2 1\r\n1 2 1\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "2 1\r\n1 2 1\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n1 2 1\r\n2 3 0\r\n3 1\r\n", "output": "YES\r\n"}, {"input": "3 2\r\n1 2 1\r\n2 3 0\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n1 2 0\r\n2 3 0\r\n3 1 1\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1 2 0\r\n2 3 0\r\n3 4 1\r\n4 2 0\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1 2 1\r\n2 3 0\r\n3 4 0\r\n4 2 0\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "5 5\r\n1 2 0\r\n2 3 0\r\n3 4 1\r\n4 2 0\r\n2 5 0\r\n1 5\r\n", "output": "YES\r\n"}, {"input": "6 6\r\n1 2 0\r\n2 3 0\r\n3 4 0\r\n2 5 0\r\n5 4 1\r\n4 6 0\r\n1 6\r\n", "output": "YES\r\n"}, {"input": "9 11\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 0\r\n4 5 0\r\n5 6 1\r\n6 4 0\r\n6 7 0\r\n7 8 0\r\n8 9 0\r\n9 7 0\r\n1 9\r\n", "output": "YES\r\n"}, {"input": "9 11\r\n1 2 0\r\n2 3 1\r\n3 1 0\r\n3 4 0\r\n4 5 0\r\n5 6 0\r\n6 4 0\r\n6 7 0\r\n7 8 0\r\n8 9 0\r\n9 7 0\r\n1 9\r\n", "output": "YES\r\n"}, {"input": "9 11\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 0\r\n4 5 0\r\n5 6 0\r\n6 4 0\r\n6 7 1\r\n7 8 0\r\n8 9 0\r\n9 7 0\r\n1 9\r\n", "output": "YES\r\n"}, {"input": "9 11\r\n1 2 0\r\n2 3 0\r\n3 1 0\r\n3 4 0\r\n4 5 0\r\n5 6 1\r\n6 4 0\r\n6 7 0\r\n7 8 0\r\n8 9 0\r\n9 7 0\r\n4 5\r\n", "output": "YES\r\n"}, {"input": "9 11\r\n1 2 0\r\n2 3 0\r\n3 1 1\r\n3 4 0\r\n4 5 0\r\n5 6 0\r\n6 4 0\r\n6 7 0\r\n7 8 0\r\n8 9 1\r\n9 7 0\r\n4 5\r\n", "output": "NO\r\n"}, {"input": "12 11\r\n1 10 0\r\n5 10 0\r\n8 10 0\r\n6 5 0\r\n9 10 1\r\n3 6 1\r\n12 6 0\r\n4 8 0\r\n7 9 1\r\n2 4 1\r\n11 9 1\r\n7 2\r\n", "output": "YES\r\n"}, {"input": "12 15\r\n5 1 0\r\n11 1 1\r\n4 11 0\r\n3 4 0\r\n2 3 1\r\n8 4 0\r\n12 11 0\r\n6 12 0\r\n10 6 0\r\n7 3 0\r\n9 4 0\r\n7 8 0\r\n11 10 0\r\n10 12 0\r\n1 6 0\r\n2 8\r\n", "output": "YES\r\n"}, {"input": "12 17\r\n8 3 0\r\n11 8 0\r\n4 8 0\r\n6 11 1\r\n12 11 0\r\n7 8 0\r\n10 11 0\r\n5 4 0\r\n9 10 0\r\n2 6 0\r\n1 4 0\r\n10 12 0\r\n9 11 0\r\n12 1 0\r\n7 1 0\r\n9 12 0\r\n10 8 0\r\n2 8\r\n", "output": "YES\r\n"}, {"input": "8 7\r\n3 7 0\r\n5 3 0\r\n2 5 0\r\n1 3 0\r\n8 3 0\r\n6 5 0\r\n4 6 0\r\n5 8\r\n", "output": "NO\r\n"}, {"input": "33 58\r\n17 11 0\r\n9 17 0\r\n14 9 0\r\n3 9 0\r\n26 14 0\r\n5 14 0\r\n10 11 0\r\n23 11 0\r\n30 9 0\r\n18 3 0\r\n25 17 0\r\n21 5 0\r\n13 11 0\r\n20 14 0\r\n32 23 0\r\n29 21 0\r\n16 21 0\r\n33 20 0\r\n1 32 0\r\n15 16 0\r\n22 13 0\r\n12 17 0\r\n8 32 0\r\n7 11 0\r\n6 29 0\r\n2 21 0\r\n19 3 0\r\n4 6 0\r\n27 8 0\r\n24 26 0\r\n28 27 0\r\n31 4 0\r\n20 23 0\r\n4 26 0\r\n33 25 0\r\n4 20 0\r\n32 7 0\r\n24 12 0\r\n13 17 0\r\n33 3 0\r\n22 15 0\r\n32 17 0\r\n11 30 0\r\n19 18 0\r\n14 22 0\r\n13 26 0\r\n6 25 0\r\n6 15 0\r\n15 11 0\r\n20 12 0\r\n14 11 0\r\n11 19 0\r\n19 21 0\r\n16 28 0\r\n22 19 0\r\n21 14 0\r\n14 27 0\r\n11 9 0\r\n3 7\r\n", "output": "NO\r\n"}]
false
stdio
null
true
659/D
659
D
PyPy 3-64
TESTS
3
46
0
187685933
import math n = int(input()) ls = [] count = 0 for i in range(n): ls.append(tuple(map(int, input().split(' ')))) if i >= 2: vec = (ls[i - 2][0] - ls[i - 1][0], ls[i - 2][1] - ls[i - 1][1]) if vec[0] * ls[i][1] - vec[1] * ls[i][0] < 0: count += 1 # print(vec, ls[i]) # print(ls) print(count)
22
46
0
204706819
n = int(input()) print(int((n - 4) / 2))
Codeforces Round 346 (Div. 2)
CF
2,016
1
256
Bicycle Race
Maria participates in a bicycle race. The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west. Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa). Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored. Help Maria get ready for the competition — determine the number of dangerous turns on the track.
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track. The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1). It is guaranteed that: - the first straight section is directed to the north; - the southernmost (and if there are several, then the most western of among them) point of the track is the first point; - the last point coincides with the first one (i.e., the start position); - any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point); - no pair of points (except for the first and last one) is the same; - no two adjacent straight sections are directed in the same direction or in opposite directions.
Print a single integer — the number of dangerous turns on the track.
null
The first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
1,500
["geometry", "implementation", "math"]
22
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}, {"input": "4\r\n6 8\r\n6 9\r\n7 9\r\n7 8\r\n6 8\r\n", "output": "0\r\n"}, {"input": "8\r\n-10000 -10000\r\n-10000 5000\r\n0 5000\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n0 -10000\r\n-10000 -10000\r\n", "output": "2\r\n"}, {"input": "20\r\n-4286 -10000\r\n-4286 -7778\r\n-7143 -7778\r\n-7143 -3334\r\n-10000 -3334\r\n-10000 1110\r\n-4286 1110\r\n-4286 -3334\r\n4285 -3334\r\n4285 -1112\r\n7142 -1112\r\n7142 3332\r\n4285 3332\r\n4285 9998\r\n9999 9998\r\n9999 -3334\r\n7142 -3334\r\n7142 -5556\r\n-1429 -5556\r\n-1429 -10000\r\n-4286 -10000\r\n", "output": "8\r\n"}, {"input": "24\r\n-10000 -10000\r\n-10000 9998\r\n9998 9998\r\n9998 -10000\r\n-6364 -10000\r\n-6364 6362\r\n6362 6362\r\n6362 -6364\r\n-2728 -6364\r\n-2728 2726\r\n2726 2726\r\n2726 -910\r\n908 -910\r\n908 908\r\n-910 908\r\n-910 -4546\r\n4544 -4546\r\n4544 4544\r\n-4546 4544\r\n-4546 -8182\r\n8180 -8182\r\n8180 8180\r\n-8182 8180\r\n-8182 -10000\r\n-10000 -10000\r\n", "output": "10\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-6000 6000\r\n-6000 2000\r\n10000 2000\r\n10000 -2000\r\n-6000 -2000\r\n-6000 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-9800 6000\r\n-9800 2000\r\n10000 2000\r\n10000 -2000\r\n-9800 -2000\r\n-9800 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "4\r\n0 0\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}]
false
stdio
null
true
66/E
66
E
PyPy 3
TESTS
2
466
6,041,600
95915598
from typing import TypeVar, Generic, Callable, List import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') T = TypeVar('T') class SegmentTree(Generic[T]): __slots__ = ["size", "tree", "identity", "op", "update_op"] def __init__(self, size: int, identity: T, op: Callable[[T, T], T], update_op: Callable[[T, T], T]) -> None: self.size = size self.tree = [identity] * (size * 2) self.identity = identity self.op = op self.update_op = update_op def build(self, a: List[T]) -> None: tree = self.tree tree[self.size:self.size + len(a)] = a for i in range(self.size - 1, 0, -1): tree[i] = self.op(tree[i << 1], tree[(i << 1) + 1]) def find(self, left: int, right: int) -> T: left += self.size right += self.size tree, result, op = self.tree, self.identity, self.op while left < right: if left & 1: result = op(tree[left], result) left += 1 if right & 1: result = op(tree[right - 1], result) left, right = left >> 1, right >> 1 return result def update(self, i: int, value: T) -> None: op, tree = self.op, self.tree i = self.size + i tree[i] = self.update_op(tree[i], value) while i > 1: i >>= 1 tree[i] = op(tree[i << 1], tree[(i << 1) + 1]) n = int(input()) a = tuple(map(int, input().split())) * 2 b = tuple(map(int, input().split())) * 2 m = 2 * n acc_lr = [0] * (n * 2 + 2) acc_rl = [0] * (n * 2 + 2) for i in range(1, n * 2 + 1): acc_lr[i] = acc_lr[i - 1] + a[i - 1] - b[i - 1] acc_rl[m - i + 1] = acc_rl[m - i + 2] + a[m - i - 1] - b[m - i] inf = 10**9 + 100 segt_lr = SegmentTree[int](2 * n + 2, inf, min, min) segt_lr.build(acc_lr) segt_rl = SegmentTree[int](2 * n + 2, inf, min, min) segt_rl.build(acc_rl) ans = [] for i in range(1, n + 1): if acc_lr[i - 1] <= segt_lr.find(i, i + n) or acc_rl[n + i] <= segt_rl.find(i, n + i): ans.append(i) print(len(ans)) print(*ans)
75
278
19,660,800
223810270
import sys N = 0 A = [] # gas of a station B = [] # dist to next station result = [] def read_input(): global N, A, B, result readline = sys.stdin.readline N = int(readline().strip()) A = [int(w) for w in readline().split()] B = [int(w) for w in readline().split()] result = [False] * N def solve(): gap, min_gap = 0, 0 for i in range(N): gap = gap + A[i] - B[i] min_gap = min(gap, min_gap) result[0] |= min_gap >= 0 for i in range(1, N): min_gap += - A[i - 1] + B[i - 1] if min_gap >= 0: result[i] = True # print(result) gap, min_gap = 0, 0 C = B[-1:] + B[:-1] for i in range(N - 1, -1, -1): gap = gap + A[i] - C[i] min_gap = min(gap, min_gap) result[-1] |= min_gap >= 0 # print(C) for i in range(N - 2, -1, -1): min_gap += C[i + 1] - A[i + 1] if min_gap >= 0: result[i] = True # print(result) def write_output(): buf = [i + 1 for i in range(N) if result[i]] print(len(buf)) print(*buf) read_input() solve() write_output()
Codeforces Beta Round 61 (Div. 2)
CF
2,011
2
256
Petya and Post
Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to n. One can fuel the car at the i-th station with no more than ai liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is b1 kilometers, the distance between the 2-nd and the 3-rd one is b2 kilometers, ..., between the (n - 1)-th and the n-th ones the distance is bn - 1 kilometers and between the n-th and the 1-st one the distance is bn kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all ai is equal to the sum of all bi. The i-th gas station and i-th post office are very close, so the distance between them is 0 kilometers. Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it? Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem.
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ones, respectively. The sum of all bi equals to the sum of all ai and is no more than 109. Each of the numbers ai, bi is no less than 1 and no more than 109.
Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start.
null
null
[{"input": "4\n1 7 2 3\n8 1 1 3", "output": "2\n2 4"}, {"input": "8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1", "output": "8\n1 2 3 4 5 6 7 8"}]
2,000
["data structures", "dp"]
75
[{"input": "4\r\n1 7 2 3\r\n8 1 1 3\r\n", "output": "2\r\n2 4\r\n"}, {"input": "8\r\n1 2 1 2 1 2 1 2\r\n2 1 2 1 2 1 2 1\r\n", "output": "8\r\n1 2 3 4 5 6 7 8\r\n"}, {"input": "20\r\n31 16 20 30 19 35 8 11 20 45 10 26 21 39 29 52 8 10 37 49\r\n16 33 41 32 43 24 35 48 19 37 28 26 7 10 23 48 18 2 1 25\r\n", "output": "4\r\n1 2 12 13\r\n"}, {"input": "20\r\n10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10\r\n10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10\r\n", "output": "20\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n1000000000\r\n1000000000\r\n", "output": "1\r\n1\r\n"}, {"input": "3\r\n3 3 3\r\n3 2 4\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "10\r\n1 5 4 3 2 1 5 8 2 3\r\n1 1 1 1 5 5 5 5 5 5\r\n", "output": "3\r\n1 2 5\r\n"}, {"input": "10\r\n44 22 14 9 93 81 52 64 3 99\r\n43 23 13 10 92 82 51 65 2 100\r\n", "output": "6\r\n1 3 5 7 9 10\r\n"}]
false
stdio
null
true
731/C
731
C
PyPy 3-64
TESTS
5
420
38,604,800
161427739
from bisect import * from collections import * import sys import io, os import math import random from heapq import * gcd = math.gcd sqrt = math.sqrt maxint=10**21 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,m,k=map(int,input().split()) c=list(map(int,input().split())) graph=[[] for i in range(n)] for i in range(m): u,v=map(int,input().split()) graph[u-1].append(v-1) graph[v-1].append(u-1) vis=[-1]*n ans=0 for i in range(n): if(vis[i]!=-1): continue col={} curr=i stk=[curr] while(stk!=[]): f=stk.pop() if(c[f] in col): col[c[f]]+=1 else: col[c[f]]=1 for i in graph[f]: if(vis[i]!=-1): continue stk.append(i) vis[i]=1 ma=-1 su=0 for j in col: ct=col[j] su+=ct ma=max(ct,ma) ans+=(su-ma) print(ans) main()
70
529
54,681,600
207892471
import sys input = lambda: sys.stdin.readline().rstrip() from collections import defaultdict,Counter N,M,K = map(int, input().split()) A = list(map(int, input().split())) P = [[] for _ in range(N)] for _ in range(M): u,v = map(int, input().split()) u-=1;v-=1 P[u].append(v) P[v].append(u) seen = [-1]*N def paint(idx,color): v = [idx] while v: i = v.pop() if seen[i]!=-1:continue seen[i]=color for j in P[i]: if seen[j]!=-1:continue v.append(j) c = 0 for i in range(N): if seen[i]==-1: c+=1 paint(i,c) lib = defaultdict(list) for i in range(N): lib[seen[i]].append(A[i]) ans = 0 for k,v in lib.items(): C = Counter(v) t = max(C.values()) ans+=len(v)-t print(ans)
Codeforces Round 376 (Div. 2)
CF
2,016
2
256
Socks
Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors. When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors. Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now. The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.
The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively. The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks. Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.
Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
null
In the first sample, Arseniy can repaint the first and the third socks to the second color. In the second sample, there is no need to change any colors.
[{"input": "3 2 3\n1 2 3\n1 2\n2 3", "output": "2"}, {"input": "3 2 2\n1 1 2\n1 2\n2 1", "output": "0"}]
1,600
["dfs and similar", "dsu", "graphs", "greedy"]
70
[{"input": "3 2 3\r\n1 2 3\r\n1 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "3 2 2\r\n1 1 2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3 3 3\r\n1 2 3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "2\r\n"}, {"input": "4 2 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "10 3 2\r\n2 1 1 2 1 1 2 1 2 2\r\n4 10\r\n9 3\r\n5 7\r\n", "output": "2\r\n"}, {"input": "10 3 3\r\n2 2 1 3 1 2 1 2 2 2\r\n10 8\r\n9 6\r\n8 10\r\n", "output": "0\r\n"}, {"input": "4 3 2\r\n1 1 2 2\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "2\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n4 1\r\n", "output": "3\r\n"}]
false
stdio
null
true
66/C
66
C
PyPy 3
TESTS
7
312
0
78642337
from sys import stdin data = {} counts = {} for line in stdin: a = line[3:].split("\\") a[0] = line[0] + a[0] counts[a[0]] = counts.get(a[0], 0) + 1 if a[0] not in data: data[a[0]] = set() for f in enumerate(a[1:-1]): data[a[0]].add(f) print(max([len(data[k]) for k in data]), max([counts[k] for k in counts]))
100
280
1,843,200
57504730
################# # July 21st 2019. ################# ############################################################################# # Directory class definition. class Directory: # Method to get recursive sub-directory count. def getSubDirCount(self): return self.subdirCount; # Method to get recursive file count. def getFileCount(self): return self.fileCount; # Method to add new sub-directory. def addSubDir(self,name): self.subdirs[name] = Directory(name) # Method to add new file. def addFile(self,name): self.files[name] = name # Method to calculate sub-folders. def calculateSubDirs(self): count,keys = 0,list(self.subdirs.keys()) for key in keys: count+=self.subdirs[key].getSubDirCount() self.subdirCount = count + len(keys) def __init__(self,name): # Recursive counts of folders and files. self.subdirCount,self.fileCount = 0,0 # For Storing files and sub-directries. self.subdirs,self.files = {},{} # For Storing name of this director. self.name = name ############################################################################# # Method to perform Tree Insertion. def insertIntoTree(directory,path): # Increasing file Count. directory.fileCount+=1; # Extracting name from queue. name = path.pop(0) # Defining Base-Case i.e File is reached. if len(path) == 0: directory.addFile(name); return # Defining Inductive-Case. else: # A New Directory is required. if not name in directory.subdirs: directory.addSubDir(name) # Navigating to directory. insertIntoTree(directory.subdirs[name],path) # Updating recursive sub-drectory and file counts. directory.calculateSubDirs(); ############################################################################# # Collecting paths from codeforces and performing insertion. from sys import stdin fileTree = Directory('root') for path in stdin: insertIntoTree(fileTree,path.split('\\')) # Determining Maximal folders and file counts. maxFolders,maxFiles = -1,-1 for drive in list(fileTree.subdirs.keys()): for directories in list(fileTree.subdirs[drive].subdirs.keys()): a = fileTree.subdirs[drive].subdirs[directories].getSubDirCount() b = fileTree.subdirs[drive].subdirs[directories].getFileCount() maxFolders = max(maxFolders,a) maxFiles = max(maxFiles,b) print(str(maxFolders)+" "+str(maxFiles)) ############################################################################# ######################################## # Programming-Credits atifcppprogrammer. ########################################
Codeforces Beta Round 61 (Div. 2)
CF
2,011
3
256
Petya and File System
Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders). More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder. For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples. You are given a few files that Petya has managed to create. The path to each file looks as follows: diskName:\folder1\folder2\...\ foldern\fileName - diskName is single capital letter from the set {C,D,E,F,G}. - folder1, ..., foldern are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (n ≥ 1) - fileName is a file name in the form of name.extension, where the name and the extension are nonempty sequences of lowercase Latin letters and digits from 0 to 9. It is also known that there is no file whose path looks like diskName:\fileName. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder. Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders.
Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once. There is at least one line in the input data.
Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders.
null
In the first sample we have one folder on the "C" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1. In the second sample we have several different folders. Consider the "folder1" folder on the "C" disk. This folder directly contains one folder, "folder2". The "folder2" folder contains two more folders — "folder3" and "folder4". Thus, the "folder1" folder on the "C" drive has exactly 3 subfolders. Also this folder contains two files, even though they do not lie directly in the folder, but they are located in subfolders of "folder1". In the third example we see that the names of some folders and some subfolders are identical. Consider the "file" folder, which lies directly on the "C" disk. That folder contains another "file" folder, which in turn contains another "file" folder, which contains two more folders, "file" and "file2". Thus, the "file" folder, which lies directly on the "C" disk, contains 4 subfolders.
[{"input": "C:\n\\\nfolder1\n\\\nfile1.txt", "output": "0 1"}, {"input": "C:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder3\n\\\nfile1.txt\nC:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder4\n\\\nfile1.txt\nD:\n\\\nfolder1\n\\\nfile1.txt", "output": "3 2"}, {"input": "C:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile.txt\nC:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile2\n\\\nfile.txt", "output": "4 2"}]
1,800
["data structures", "implementation"]
100
[{"input": "C:\\folder1\\file1.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\folder1\\folder2\\folder3\\file1.txt\r\nC:\\folder1\\folder2\\folder4\\file1.txt\r\nD:\\folder1\\file1.txt\r\n", "output": "3 2\r\n"}, {"input": "C:\\file\\file\\file\\file\\file.txt\r\nC:\\file\\file\\file\\file2\\file.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\file\\file.txt\r\nD:\\file\\file.txt\r\nE:\\file\\file.txt\r\nF:\\file\\file.txt\r\nG:\\file\\file.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\a\\b\\c\\d\\d.txt\r\nC:\\a\\b\\c\\e\\f.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\z\\z.txt\r\nD:\\1\\1.txt\r\nD:\\1\\2.txt\r\n", "output": "0 2\r\n"}, {"input": "D:\\0000\\1.txt\r\nE:\\00000\\1.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\a\\b\\c\\d.txt\r\nC:\\a\\e\\c\\d.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\test1\\test2\\test3\\test.txt\r\nC:\\test1\\test3\\test3\\test4\\test.txt\r\nC:\\test1\\test2\\test3\\test2.txt\r\nD:\\test1\\test2\\test.txt\r\nD:\\test1\\test3\\test4.txt\r\n", "output": "5 3\r\n"}, {"input": "C:\\test1\\test2\\test.txt\r\nC:\\test1\\test2\\test2.txt\r\n", "output": "1 2\r\n"}]
false
stdio
null
true
883/F
883
F
PyPy 3-64
TESTS
6
62
0
207874069
from math import * from sys import stdin input = lambda: stdin.readline()[:-1] inp = lambda: list(map(int, input().split())) n = int(input()) a = [] dt = set() for x in range(n): a.append(input()) for st in a: ret = [] for e, c in enumerate(st): if ret and c == 'o' and ret[-1] == 'o': ret.pop() ret.append('u') else: ret.append(c) res = [] for e, c in enumerate(ret[::-1]): if res and c == 'k' and res[-1] == 'h': continue res.append(c) res = "".join(res)[::-1] res = res.replace("ou", "uo") dt.add(res) print(len(dt))
36
46
5,529,600
31557909
n = int(input()) s = list() for i in range(0, n): st = input() s.append(st) for i in range(0, len(s)): st = s[i] while 'u' in st: st = st.replace("u", "oo") while "kh" in st: st = st.replace("kh", "h") s[i] = st wds = set(s) print(len(wds))
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Lost in Transliteration
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
Print the minimal number of groups where the words in each group denote the same name.
null
There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
1,300
["implementation"]
81
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
false
stdio
null
true
651/B
651
B
Python 3
TESTS
5
46
0
149058871
z=int(input()) arr=[int(x) for x in input().split()] arr.sort() hash=[] i=0 while(i<len(arr)): if(i==0 ): temp=[] temp.append(arr[i]) hash.append(temp) i=i+1 else: if(arr[i]!=arr[i-1]): temp=hash[0] temp.append(arr[i]) hash[0]=temp i=i+1 else: j=1 while(arr[i]==arr[i-1]): if(j>=len(hash)): temp=[arr[i]] hash.append(temp) else: temp=hash[j] temp.append(arr[i]) hash[j]=temp i=i+1 j=j+1 if(i>=len(arr)-1): break brr=[] for k in range(0,len(hash)): for x in hash[k]: brr.append(x) cnt=0 if(len(brr)==1): print(0) else: for j in range(1,len(brr)): if(brr[j]>brr[j-1]): cnt=cnt+1; print(cnt)
31
46
0
206881481
n =int(input()) l = list(map(int,input().split())) print(n - max([l.count(i) for i in l]))
Codeforces Round 345 (Div. 2)
CF
2,016
1
256
Beautiful Paintings
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one. We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
null
In the first sample, the optimal order is: 10, 20, 30, 40, 50. In the second sample, the optimal order is: 100, 200, 100, 200.
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
1,200
["greedy", "sortings"]
31
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
false
stdio
null
true
231/A
231
A
Python 3
TESTS
6
92
0
227219691
n = int(input()) a=[] x=0 for i in range(n): a =[int(item) for item in input().split()] if(a[0] and a[1]==1 or a[1] and a[2]==1 or a[1] and a[2]==1): x=x+1 print(x)
21
62
0
225737681
n = int(input()) # Input the number of problems problems_solved = 0 # Initialize the count of problems they will solve for _ in range(n): petya, vasya, tonya = map(int, input().split()) # Input the friends' opinions # Check if at least two of them are sure about the solution if petya + vasya + tonya >= 2: problems_solved += 1 print(problems_solved) # Output the number of problems they will solve
Codeforces Round 143 (Div. 2)
CF
2,012
2
256
Team
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution. This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
Print a single integer — the number of problems the friends will implement on the contest.
null
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
800
["brute force", "greedy"]
21
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
false
stdio
null
true
231/A
231
A
Python 3
TESTS
6
62
0
229597535
n=int(input()) a=0 for i in range(n): z=input() if z=="1 1 0" or z=="1 1 1" or z=="0 1 1": a+=1 print(a)
21
62
0
225768332
n = int(input()) result = 0 for _ in range(n): temp = [int(i) for i in input().split()] if sum(temp) >= 2: result += 1 print(result)
Codeforces Round 143 (Div. 2)
CF
2,012
2
256
Team
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution. This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
Print a single integer — the number of problems the friends will implement on the contest.
null
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
800
["brute force", "greedy"]
21
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
false
stdio
null
true
231/A
231
A
PyPy 3-64
TESTS
6
92
0
227476563
Number = int(input()) counter = 0; for i in range(Number): Lists = str(input()) if Lists == "1 1 1" or Lists == "0 1 1" or Lists == "1 1 0": counter = counter + 1 print(counter)
21
62
0
225835723
n=int(input()) ans=0 for i in range(n): #inp=int(input()) a,b,c=map(int,input().split()) if((a==1 and b==1) or (b==1 and c==1) or (a==1 and c==1)): ans+=1 print(ans)
Codeforces Round 143 (Div. 2)
CF
2,012
2
256
Team
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution. This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
Print a single integer — the number of problems the friends will implement on the contest.
null
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
800
["brute force", "greedy"]
21
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
false
stdio
null
true
832/C
832
C
PyPy 3-64
TESTS
2
109
3,993,600
141189276
def f1(s, xi, vi, B): if B <= xi: return (10**6*(s-vi)+vi*xi-s*B)/(s**2-vi**2) else: return (10**6-xi)/(vi) def f2(s, xi, vi, B): if xi <= B: return (s*B-vi*xi)/(s**2-vi**2) else: return xi/vi def find_time(B, s, move_right, move_left): answer = [float('inf'), float('inf')] for xi, vi in move_right: answer[0] = min(answer[0], f1(s, xi, vi, B)) for xi, vi in move_left: answer[1] = min(answer[1], f2(s, xi, vi, B)) return max(answer) def process(s, A): n = len(A) #A[i] = xi, vi, ti = coordinate, speed, direction #say bomb is placed at B #1) for people with ti = 2 and xi >= B #ray hits them at B+st = x+vit #time1 = (xi-B)/(s-vi) #point1 = (sxi-viB)/(s-vi) #after which point their speed is (s+vi) move_right = [] move_left = [] for i in range(n): xi, vi, ti = A[i] if ti==2: move_right.append([xi, vi]) else: move_left.append([xi, vi]) move_left = sorted(move_left) move_right = sorted(move_right) start = min(move_left[0][0], move_right[0][0]) end = max(move_left[-1][0], move_right[-1][0]) answer = min(find_time(start, s, move_right, move_left), find_time(end, s, move_right, move_left)) while start+1 < end: m = (start+end)//2 m1 = m-1 m2 = m+1 f_m = find_time(m, s, move_right, move_left) f_m1 = find_time(m1, s, move_right, move_left) f_m2 = find_time(m2, s, move_right, move_left) answer = min(answer, f_m, f_m1, f_m2) if f_m1 > f_m < f_m2: break if f_m1 > f_m: start, end = m, end elif f_m < f_m2: start, end = start, m else: start, end = start+1, end-1 answer = min(answer, find_time(start, s, move_right, move_left), find_time(end, s, move_right, move_left)) #for xi, vi in move_right+move_left: # answer = min(answer, find_time(xi, s, move_right, move_left)) return answer n, s = [int(x) for x in input().split()] A = [] for i in range(n): x, v, t = [int(x) for x in input().split()] A.append([x, v, t]) print(process(s, A))
67
1,154
23,347,200
224527292
import math def main(): num_elements, initial_speed = map(int, input().split()) speed = float(initial_speed) positions = [] velocities = [] directions = [] events = [] epsilon = 1e-8 for _ in range(num_elements): position_i, velocity_i, direction_i = map(int, input().split()) positions.append(position_i) velocities.append(velocity_i) directions.append(direction_i) events.append(((position_i, direction_i), velocity_i)) events.sort() left_bound = 0.0 right_bound = 1000000.0 min_left_speed = 1e7 min_right_speed = 1e7 min_left_speed_with_s = 1e7 min_right_speed_with_s = 1e7 for i in range(num_elements): if directions[i] == 1: min_left_speed = min(min_left_speed, positions[i] / velocities[i]) min_left_speed_with_s = min(min_left_speed_with_s, positions[i] / (speed + velocities[i])) else: min_right_speed = min(min_right_speed, (1000000 - positions[i]) / velocities[i]) min_right_speed_with_s = min(min_right_speed_with_s, (1000000 - positions[i]) / (speed + velocities[i])) for _ in range(50): middle_speed = (left_bound + right_bound) / 2 if min_left_speed_with_s > middle_speed or min_right_speed_with_s > middle_speed: left_bound = middle_speed continue if min_left_speed <= middle_speed + epsilon: if min_right_speed_with_s <= middle_speed: right_bound = middle_speed else: left_bound = middle_speed continue if min_right_speed <= middle_speed + epsilon: if min_left_speed_with_s <= middle_speed: right_bound = middle_speed else: left_bound = middle_speed continue can_reach = False max_reached = -1e18 for event in events: if event[0][1] == 1: distance = event[0][0] velocity = event[1] if (velocity + speed) * middle_speed < distance + epsilon: continue displacement = (middle_speed * (velocity + speed) - distance) max_reached = math.floor(epsilon + max(max_reached, epsilon + distance + (1 - velocity / speed) * displacement)) else: distance = 1000000 - event[0][0] velocity = event[1] if (velocity + speed) * middle_speed < distance + epsilon: continue displacement = (middle_speed * (velocity + speed) - distance) if max_reached + (1 - velocity / speed) * displacement >= event[0][0]: can_reach = True break if can_reach: right_bound = middle_speed else: left_bound = middle_speed print("{:.10f}".format(left_bound)) if __name__ == "__main__": main()# 1695325990.260047
Codeforces Round 425 (Div. 2)
CF
2,017
3
256
Strange Radiation
n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-negative integer coordinate, and blow it up. At this moment all people will start running with their maximum speed in the direction they are facing. Also, two strange rays will start propagating from the bomb with speed s: one to the right, and one to the left. Of course, the speed s is strictly greater than people's maximum speed. The rays are strange because if at any moment the position and the direction of movement of some ray and some person coincide, then the speed of the person immediately increases by the speed of the ray. You need to place the bomb is such a point that the minimum time moment in which there is a person that has run through point 0, and there is a person that has run through point 106, is as small as possible. In other words, find the minimum time moment t such that there is a point you can place the bomb to so that at time moment t some person has run through 0, and some person has run through point 106.
The first line contains two integers n and s (2 ≤ n ≤ 105, 2 ≤ s ≤ 106) — the number of people and the rays' speed. The next n lines contain the description of people. The i-th of these lines contains three integers xi, vi and ti (0 < xi < 106, 1 ≤ vi < s, 1 ≤ ti ≤ 2) — the coordinate of the i-th person on the line, his maximum speed and the direction he will run to (1 is to the left, i.e. in the direction of coordinate decrease, 2 is to the right, i.e. in the direction of coordinate increase), respectively. It is guaranteed that the points 0 and 106 will be reached independently of the bomb's position.
Print the minimum time needed for both points 0 and 106 to be reached. Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6. Namely, if your answer is a, and the jury's answer is b, then your answer is accepted, if $${ \frac { | a - b | } { \operatorname* { m a x } ( 1, | b | ) } } \leq 1 0 ^ { - 6 }$$.
null
In the first example, it is optimal to place the bomb at a point with a coordinate of 400000. Then at time 0, the speed of the first person becomes 1000 and he reaches the point 106 at the time 600. The bomb will not affect on the second person, and he will reach the 0 point at the time 500000. In the second example, it is optimal to place the bomb at the point 500000. The rays will catch up with both people at the time 200. At this time moment, the first is at the point with a coordinate of 300000, and the second is at the point with a coordinate of 700000. Their speed will become 1500 and at the time 400 they will simultaneously run through points 0 and 106.
[{"input": "2 999\n400000 1 2\n500000 1 1", "output": "500000.000000000000000000000000000000"}, {"input": "2 1000\n400000 500 1\n600000 500 2", "output": "400.000000000000000000000000000000"}]
2,500
["binary search", "implementation", "math"]
67
[{"input": "2 999\r\n400000 1 2\r\n500000 1 1\r\n", "output": "500000.000000000000000000000000000000\r\n"}, {"input": "2 1000\r\n400000 500 1\r\n600000 500 2\r\n", "output": "400.000000000000000000000000000000\r\n"}, {"input": "2 99999\r\n500 1 1\r\n499 10000 2\r\n", "output": "99.950100000000000000088817841970\r\n"}, {"input": "26 10\r\n495492 7 1\r\n256604 5 2\r\n511773 3 2\r\n590712 4 1\r\n206826 7 2\r\n817878 4 2\r\n843915 1 1\r\n349160 3 1\r\n351298 4 1\r\n782251 8 2\r\n910928 4 1\r\n662354 4 2\r\n468621 2 2\r\n466991 7 2\r\n787303 6 2\r\n221623 8 2\r\n343518 6 1\r\n141123 7 1\r\n24725 6 1\r\n896603 3 2\r\n918129 8 2\r\n706071 6 2\r\n512369 2 2\r\n600004 4 1\r\n928608 9 2\r\n298493 3 1\r\n", "output": "4120.833333333333333481363069950021\r\n"}, {"input": "13 10000\r\n78186 325 1\r\n942344 8592 2\r\n19328 6409 2\r\n632454 7747 2\r\n757264 8938 1\r\n462681 7708 1\r\n26489 2214 2\r\n415801 8912 2\r\n156832 48 1\r\n898262 1620 2\r\n936086 5125 1\r\n142567 5086 1\r\n207839 9409 2\r\n", "output": "7.572493946731234866574095088154\r\n"}, {"input": "22 30\r\n739680 21 1\r\n697634 24 1\r\n267450 27 2\r\n946750 8 2\r\n268031 27 1\r\n418652 11 1\r\n595005 12 1\r\n59519 22 2\r\n332220 1 1\r\n355395 2 1\r\n573947 26 1\r\n864962 4 1\r\n659836 14 1\r\n439461 22 1\r\n694157 11 2\r\n429431 11 2\r\n304031 9 2\r\n282710 4 1\r\n623799 11 1\r\n610188 27 2\r\n596592 20 2\r\n562391 18 2\r\n", "output": "6656.250000000000000000000000000000\r\n"}, {"input": "10 100\r\n945740 58 2\r\n424642 85 2\r\n310528 91 2\r\n688743 93 1\r\n355046 85 1\r\n663649 84 2\r\n720124 56 1\r\n941616 59 2\r\n412011 46 2\r\n891591 30 2\r\n", "output": "1919.167567567567567521358284921007\r\n"}, {"input": "4 100\r\n884131 61 1\r\n927487 23 2\r\n663318 13 1\r\n234657 61 1\r\n", "output": "3152.739130434782608647381607624993\r\n"}, {"input": "20 20\r\n722369 11 1\r\n210389 8 2\r\n743965 2 1\r\n951723 17 2\r\n880618 1 2\r\n101303 8 2\r\n174013 19 2\r\n627995 19 1\r\n541778 5 1\r\n586095 19 1\r\n324166 4 1\r\n125805 12 2\r\n538606 2 2\r\n691777 9 2\r\n127586 7 1\r\n849701 9 1\r\n23273 17 1\r\n250794 4 1\r\n64709 7 2\r\n785893 9 1\r\n", "output": "1369.000000000000000000000000000000\r\n"}, {"input": "5 786551\r\n352506 2 1\r\n450985 6 2\r\n561643 4 2\r\n5065 8 2\r\n717868 3 1\r\n", "output": "0.635685124996669956255399003275\r\n"}, {"input": "3 96475\r\n187875 5 2\r\n813727 8 1\r\n645383 7 2\r\n", "output": "50659.571428571428569398449326399714\r\n"}, {"input": "2 96475\r\n813727 8 1\r\n645383 7 2\r\n", "output": "50659.571428571428569398449326399714\r\n"}, {"input": "2 2\r\n1 1 1\r\n999999 1 2\r\n", "output": "1.000000000000000000000000000000\r\n"}, {"input": "2 1000000\r\n1 1 1\r\n999999 1 2\r\n", "output": "0.499999999999500000001997901400\r\n"}, {"input": "2 250001\r\n499999 250000 1\r\n500000 250000 2\r\n", "output": "1.499997000005989434244513258676\r\n"}]
false
stdio
import sys def main(): input_path, correct_output_path, submission_output_path = sys.argv[1:4] # Read correct output try: with open(correct_output_path, 'r') as f: correct_line = f.readline().strip() correct = float(correct_line) except: print(0) return # Read submission output try: with open(submission_output_path, 'r') as f: submission_line = f.readline().strip() submission = float(submission_line) except: print(0) return # Calculate errors absolute_error = abs(submission - correct) denominator = max(1.0, abs(correct)) if absolute_error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
873/C
873
C
Python 3
TESTS
7
93
307,200
60603813
def cal(col,index,k): one = 0 for i in range(index,len(col)): if col[i] == 1: for j in range(i,min(len(col),i+k)): if col[j] == 1: one += 1 break return one def solve(col,k): change = 0 score = 0 n = len(col) for i in range(n): if col[i] == 1: one = 0 for j in range(i,min(n,i+k)): if col[j] == 1: one += 1 score = one break remove = 0 for i in range(n): if col[i] == 0: change += 1 one = cal(col,i+1,k) if one > score: remove = change score = one return score,remove def main(): n,m,k = map(int,input().split()) matrix = [] for i in range(n): matrix.append(list(map(int,input().split()))) score = 0 change = 0 for i in range(m): col = [] for j in range(n): col.append(matrix[j][i]) curr_score,curr_rem = solve(col,k) score += curr_score change += curr_rem print(score,change) main()
20
46
0
230751123
n,m,k=map(int,input().split()) s,c=0,0 R=[list(map(int,input().split())) for i in range(n)] for i in zip(*R): a,b=0,0 for j in range(n-k+1): f,h=sum(i[j:j+k]),sum(i[:j]) if f>a:a,b=f,h s+=a c+=b print(s,c,end=' ')
Educational Codeforces Round 30
ICPC
2,017
1
256
Strange Game On Matrix
Ivan is playing a strange game. He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
null
In the first example Ivan will replace the element a1, 2.
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
1,600
["greedy", "two pointers"]
20
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
false
stdio
null
true
615/B
615
B
PyPy 3
TESTS
3
124
0
84574454
def dfs(vert, prev, num): global ans, hihi if vert < prev: return hihi.add(vert) for elem in A[vert]: ans = max(ans, (num + 1) * len(A[vert])) if elem not in hihi: dfs(elem, vert, num + 1) n, m = list(map(int, input().split())) A = dict() for i in range(m): ar = list(map(int, input().split())) if ar[0] in A: A[ar[0]].append(ar[1]) else: A[ar[0]] = [ar[1]] if ar[1] in A: A[ar[1]].append(ar[0]) else: A[ar[1]] = [ar[0]] hihi = set() ans = 0 for i in range(1, n + 1): if i not in hihi: dfs(i, 0, 0) print(ans)
60
467
15,052,800
201973067
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n, m = map(int, input().split()) G = [[] for _ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) G[u].append(v) G[v].append(u) dp = [1] * (n + 1) for i in range(1, n + 1): dpi = dp[i] for j in G[i]: if i < j: dp[j] = max(dp[j], dpi + 1) ans = 0 for i in range(1, n + 1): ans = max(ans, dp[i] * len(G[i])) print(ans)
Codeforces Round 338 (Div. 2)
CF
2,016
3
256
Longtail Hedgehog
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: 1. Only segments already presented on the picture can be painted; 2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 3. The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get. Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
First line of the input contains two integers n and m(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively. Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
Print the maximum possible value of the hedgehog's beauty.
null
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.
[{"input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7", "output": "9"}, {"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "12"}]
1,600
["dp", "graphs"]
60
[{"input": "8 6\r\n4 5\r\n3 5\r\n2 5\r\n1 2\r\n2 8\r\n6 7\r\n", "output": "9\r\n"}, {"input": "4 6\r\n1 2\r\n1 3\r\n1 4\r\n2 3\r\n2 4\r\n3 4\r\n", "output": "12\r\n"}, {"input": "5 7\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n", "output": "9\r\n"}, {"input": "5 9\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n1 5\r\n2 5\r\n", "output": "16\r\n"}, {"input": "10 10\r\n6 3\r\n2 9\r\n9 4\r\n4 5\r\n10 3\r\n8 3\r\n10 5\r\n7 6\r\n1 4\r\n6 8\r\n", "output": "8\r\n"}, {"input": "100 50\r\n66 3\r\n92 79\r\n9 44\r\n84 45\r\n30 63\r\n30 20\r\n33 86\r\n8 83\r\n40 75\r\n7 36\r\n91 4\r\n76 88\r\n77 76\r\n28 27\r\n6 52\r\n41 57\r\n8 23\r\n34 75\r\n50 15\r\n86 68\r\n36 98\r\n30 84\r\n37 62\r\n22 4\r\n6 45\r\n72 80\r\n98 74\r\n78 84\r\n1 54\r\n99 27\r\n84 91\r\n78 7\r\n80 61\r\n67 48\r\n51 52\r\n36 72\r\n97 87\r\n25 17\r\n20 80\r\n20 39\r\n72 5\r\n21 77\r\n48 1\r\n63 21\r\n92 45\r\n34 93\r\n28 84\r\n3 91\r\n56 99\r\n7 53\r\n", "output": "15\r\n"}, {"input": "5 8\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n1 5\r\n", "output": "12\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "2\r\n"}, {"input": "10 9\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n", "output": "9\r\n"}, {"input": "5 4\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n", "output": "4\r\n"}, {"input": "6 5\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n", "output": "5\r\n"}]
false
stdio
null
true
873/C
873
C
PyPy 3
TESTS
7
140
1,945,600
58062079
y, x, k = map(int, input().split()) h = [[int(i) for i in input().split()] for j in range(y)] q, w = 0, 0 sh = -1 for i in range(x): a = 0 s = 0 for j in range(y): if h[j][i]: g = sum([h[lp][i] for lp in range(j, min(j + k, y))]) if g > a: if sh == i: s += 1 sh = i a = g q += a w += s print(q, w)
20
46
5,529,600
31584876
n, m, k = map(int, input().split()) ar = [] for i in range(n): ar.append(list(map(int, input().split()))) score = 0 min_moves = 0 for j in range(m): cr = [ar[i][j] for i in range(n)] c = 0 maxi = 0 r_s = 0 r_m = 0 for i in range(len(cr)): if cr[i] == 1: maxi = sum(cr[i:i+k]) if maxi > r_s: r_s = maxi r_m = c c += 1 score += r_s min_moves += r_m print(score, min_moves)
Educational Codeforces Round 30
ICPC
2,017
1
256
Strange Game On Matrix
Ivan is playing a strange game. He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
null
In the first example Ivan will replace the element a1, 2.
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
1,600
["greedy", "two pointers"]
20
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
false
stdio
null
true
363/D
363
D
Python 3
TESTS
6
46
0
197062834
import sys import heapq input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) n,m,a = invr() b = inlt() p = inlt() b.sort() p.sort() def valid(k,a,m): b_index = -k total = 0 for i in range(k): if i >= m:return False remain = max(p[i] - b[b_index],0) a -= remain if a < 0:return False total += 1 b_index+=1 return True res,left,right = 0,0,n last_m = float("inf") while left < right: mid = (left+right)//2 if mid == last_m:break last_m = mid if valid(mid,a,m): left = mid res = max(res,mid) else: right = mid temp = res while True: temp += 1 if temp > n:break if valid(temp,a,m): res = temp else: break cur = -res h = [] min_spend = 0 for i in range(res): heapq.heappush(h,b[cur]) while a < p[i]: if len(h) == 0: break need = p[i] - a x = heapq.heappop(h) if x > need: x -= need heapq.heappush(h,x) min_spend = max(min_spend,need) else: min_spend = max(min_spend,x) a -= p[i] cur+=1 print(res,min_spend)
34
857
9,830,400
199487158
def readn(): return map(int, input().split()) n,m,a=readn()#map(int,input().split()) b,p=sorted(map(int,input().split()))[-min(n,m):],sorted(map(int,input().split())) r=min(n,m) mm=r l=0 while l<=r: mid=l+(r-l)//2 pri=sum([max(0,p[i]-b[mm-mid+i]) for i in range(mid)]) if pri<=a: l=mid+1 else: r=mid-1 print(r,max(0,sum(p[:r])-a))
Codeforces Round 211 (Div. 2)
CF
2,013
1
256
Renting Bikes
A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?
The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike.
Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.
null
In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
[{"input": "2 2 10\n5 5\n7 6", "output": "2 3"}, {"input": "4 5 2\n8 1 1 2\n6 3 7 5 2", "output": "3 8"}]
1,800
["binary search", "greedy"]
34
[{"input": "2 2 10\r\n5 5\r\n7 6\r\n", "output": "2 3\r\n"}, {"input": "4 5 2\r\n8 1 1 2\r\n6 3 7 5 2\r\n", "output": "3 8\r\n"}, {"input": "1 1 2\r\n1\r\n2\r\n", "output": "1 0\r\n"}, {"input": "4 1 1\r\n3 2 3 2\r\n3\r\n", "output": "1 2\r\n"}, {"input": "1 4 1\r\n3\r\n2 4 5 5\r\n", "output": "1 1\r\n"}, {"input": "3 3 3\r\n1 1 2\r\n3 5 6\r\n", "output": "1 0\r\n"}, {"input": "4 5 6\r\n5 1 7 2\r\n8 7 3 9 8\r\n", "output": "3 12\r\n"}, {"input": "4 8 10\r\n2 1 2 2\r\n10 12 10 8 7 9 10 9\r\n", "output": "1 0\r\n"}, {"input": "8 4 18\r\n9 4 2 2 7 5 1 1\r\n11 12 8 9\r\n", "output": "4 22\r\n"}, {"input": "6 6 2\r\n6 1 5 3 10 1\r\n11 4 7 8 11 7\r\n", "output": "3 16\r\n"}, {"input": "10 10 7\r\n6 7 15 1 3 1 14 6 7 4\r\n15 3 13 17 11 19 20 14 8 17\r\n", "output": "5 42\r\n"}, {"input": "14 14 22\r\n23 1 3 16 23 1 7 5 18 7 3 6 17 8\r\n22 14 22 18 12 11 7 24 20 27 10 22 16 7\r\n", "output": "10 115\r\n"}, {"input": "10 20 36\r\n12 4 7 18 4 4 2 7 4 10\r\n9 18 7 7 30 19 26 27 16 20 30 25 23 17 5 30 22 7 13 6\r\n", "output": "10 69\r\n"}, {"input": "20 10 31\r\n17 27 2 6 11 12 5 3 12 4 2 10 4 8 2 10 7 9 12 1\r\n24 11 18 10 30 16 20 18 24 24\r\n", "output": "7 86\r\n"}, {"input": "40 40 61\r\n28 59 8 27 45 67 33 32 61 3 42 2 3 37 8 8 10 61 1 5 65 28 34 27 8 35 45 49 31 49 13 23 23 53 20 48 14 74 16 6\r\n69 56 34 66 42 73 45 49 29 70 67 77 73 26 78 11 50 69 64 72 78 66 66 29 80 40 50 75 68 47 78 63 41 70 52 52 69 22 69 66\r\n", "output": "22 939\r\n"}, {"input": "10 10 0\r\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000\r\n1001 1001 1001 1001 1001 1001 1001 1001 1001 1001\r\n", "output": "0 0\r\n"}, {"input": "9 8 0\r\n1 2 3 4 5 6 7 8 9\r\n2 3 4 5 6 7 8 9\r\n", "output": "8 44\r\n"}, {"input": "9 8 0\r\n1 2 3 4 5 6 7 8 9\r\n1 2 3 4 5 6 7 8\r\n", "output": "8 36\r\n"}]
false
stdio
null
true
659/D
659
D
Python 3
TESTS
4
46
0
170407326
n=int(input()) #prev2=[int(i) for i in input().split()] leg="" prev=[int(i) for i in input().split()] lefts,rights=0,0 for i in range(2,n): cur=[int(i) for i in input().split()] dx=cur[0]-prev[0] dy=cur[1]-prev[1] curleg="" if dx==0: curleg="U" if dy>0 else "D" else: curleg="R" if dx>0 else "L" if (leg=="U" and curleg=="L") or (leg=="D" and curleg=="R") or (leg=="L" and curleg=="D") or (leg=="R" and curleg=="U"): lefts+=1 if (leg=="U" and curleg=="R") or (leg=="D" and curleg=="L") or (leg=="L" and curleg=="U") or (leg=="R" and curleg=="D"): rights+=1 #prev2=prev prev=cur leg=curleg print(min(lefts,rights))
22
46
4,608,000
25221827
n,ans=int(input()),0 d=[] x,y=map(int,input().split()) for i in range(n): a,b=map(int,input().split()) if x==a: if y<b:d.append(1) else: d.append(3) else: if x>a:d.append(4) else:d.append(2) x,y=a,b for i in range(n-1): if d[i]==1 and d[i+1]==4 or d[i]==2 and d[i+1]==1 or d[i]==4 and d[i+1]==3 or d[i]==3 and d[i+1]==2:ans+=1 print(ans)
Codeforces Round 346 (Div. 2)
CF
2,016
1
256
Bicycle Race
Maria participates in a bicycle race. The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west. Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa). Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored. Help Maria get ready for the competition — determine the number of dangerous turns on the track.
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track. The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1). It is guaranteed that: - the first straight section is directed to the north; - the southernmost (and if there are several, then the most western of among them) point of the track is the first point; - the last point coincides with the first one (i.e., the start position); - any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point); - no pair of points (except for the first and last one) is the same; - no two adjacent straight sections are directed in the same direction or in opposite directions.
Print a single integer — the number of dangerous turns on the track.
null
The first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
1,500
["geometry", "implementation", "math"]
22
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}, {"input": "4\r\n6 8\r\n6 9\r\n7 9\r\n7 8\r\n6 8\r\n", "output": "0\r\n"}, {"input": "8\r\n-10000 -10000\r\n-10000 5000\r\n0 5000\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n0 -10000\r\n-10000 -10000\r\n", "output": "2\r\n"}, {"input": "20\r\n-4286 -10000\r\n-4286 -7778\r\n-7143 -7778\r\n-7143 -3334\r\n-10000 -3334\r\n-10000 1110\r\n-4286 1110\r\n-4286 -3334\r\n4285 -3334\r\n4285 -1112\r\n7142 -1112\r\n7142 3332\r\n4285 3332\r\n4285 9998\r\n9999 9998\r\n9999 -3334\r\n7142 -3334\r\n7142 -5556\r\n-1429 -5556\r\n-1429 -10000\r\n-4286 -10000\r\n", "output": "8\r\n"}, {"input": "24\r\n-10000 -10000\r\n-10000 9998\r\n9998 9998\r\n9998 -10000\r\n-6364 -10000\r\n-6364 6362\r\n6362 6362\r\n6362 -6364\r\n-2728 -6364\r\n-2728 2726\r\n2726 2726\r\n2726 -910\r\n908 -910\r\n908 908\r\n-910 908\r\n-910 -4546\r\n4544 -4546\r\n4544 4544\r\n-4546 4544\r\n-4546 -8182\r\n8180 -8182\r\n8180 8180\r\n-8182 8180\r\n-8182 -10000\r\n-10000 -10000\r\n", "output": "10\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-6000 6000\r\n-6000 2000\r\n10000 2000\r\n10000 -2000\r\n-6000 -2000\r\n-6000 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-9800 6000\r\n-9800 2000\r\n10000 2000\r\n10000 -2000\r\n-9800 -2000\r\n-9800 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "4\r\n0 0\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}]
false
stdio
null
true
231/A
231
A
PyPy 3-64
TESTS
6
124
0
226854782
a = int(input()) d = [] for i in range(a): t = input() d.append(t) s = 0 for j in range(len(d)): if "1 1" in d[j]: s+=1 print(s)
21
62
0
225887961
a=int(input()) p=0 for i in range (a): x=input() x=x.split() k=0 for i in range(len(x)): if x[i]=='1': k+=1 if k>=2: p+=1 print(p)
Codeforces Round 143 (Div. 2)
CF
2,012
2
256
Team
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution. This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
Print a single integer — the number of problems the friends will implement on the contest.
null
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
800
["brute force", "greedy"]
21
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
false
stdio
null
true
873/C
873
C
PyPy 3
TESTS
7
92
1,536,000
117180137
import sys input=sys.stdin.readline n,m,k=map(int,input().split()) a=[list(map(int,input().split())) for i in range(n)] s=0 cnt=0 for j in range(m): score1=0 for i in range(n): if a[i][j]==1: for ii in range(i,min(n,i+k)): if a[ii][j]==1: score1+=1 a[i][j]=0 break score2=0 for i in range(n): if a[i][j]==1: for ii in range(i,min(n,i+k)): if a[ii][j]==1: score2+=1 break if score2>score1: cnt+=1 s+=score2 else: s+=score1 print(s,cnt)
20
62
102,400
31249539
n, m, k = [int(x) for x in input().split()] matrix = [] for _ in range(n): matrix.append([int(x) for x in input().split()]) sum_score = 0 sum_remove = 0 for i in range(m): num_of_ones = [0] num1 = 0 for j in range(n): if matrix[j][i] == 1: num1 += 1 num_of_ones.append(num1) max_score = 0 num_remove = 0 for j in range(0, n - k + 1): num_of_ones_in_range = num_of_ones[j + k] - num_of_ones[j] if num_of_ones_in_range > max_score: max_score = num_of_ones_in_range num_remove = num_of_ones[j] sum_score += max_score sum_remove += num_remove print(sum_score, sum_remove)
Educational Codeforces Round 30
ICPC
2,017
1
256
Strange Game On Matrix
Ivan is playing a strange game. He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
null
In the first example Ivan will replace the element a1, 2.
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
1,600
["greedy", "two pointers"]
20
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
false
stdio
null
true
231/A
231
A
Python 3
TESTS
2
62
0
232668089
n = int(input()) a = b = c = 0 for i in range(n): x = input() if x == '1 1 1' or x == '0 1 1' or x == '1 1 0' or x == '1 0 1': a += 1 elif x == '0 0 1' or x == '1 0 0' or x == '0 1 0': b += 1 else: c += 1 if a > b + c: print(a) elif b > c: print(b) else: print(c)
21
62
0
225902902
x =input() sol_num=0 for i in range(int(x)): y =input().split(" ") count=y.count("1") if count>=2: sol_num+=1 print(sol_num)
Codeforces Round 143 (Div. 2)
CF
2,012
2
256
Team
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution. This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
Print a single integer — the number of problems the friends will implement on the contest.
null
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
800
["brute force", "greedy"]
21
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
false
stdio
null
true
659/D
659
D
Python 3
TESTS
5
93
0
49243413
n=int(input()) L=[list(map(int,input().split())) for i in range(n)] l=[L[0][0],-1] k=0 for x in L : l[1]=max(l[1],x[0]) for i in range(1,n) : if L[i][0]-L[i-1][0]!=0 : if L[i][0] not in l : k+=1 print(k)
22
46
4,608,000
29277489
a=int(input()) print(a//2-2)
Codeforces Round 346 (Div. 2)
CF
2,016
1
256
Bicycle Race
Maria participates in a bicycle race. The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west. Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa). Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored. Help Maria get ready for the competition — determine the number of dangerous turns on the track.
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track. The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1). It is guaranteed that: - the first straight section is directed to the north; - the southernmost (and if there are several, then the most western of among them) point of the track is the first point; - the last point coincides with the first one (i.e., the start position); - any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point); - no pair of points (except for the first and last one) is the same; - no two adjacent straight sections are directed in the same direction or in opposite directions.
Print a single integer — the number of dangerous turns on the track.
null
The first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
1,500
["geometry", "implementation", "math"]
22
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}, {"input": "4\r\n6 8\r\n6 9\r\n7 9\r\n7 8\r\n6 8\r\n", "output": "0\r\n"}, {"input": "8\r\n-10000 -10000\r\n-10000 5000\r\n0 5000\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n0 -10000\r\n-10000 -10000\r\n", "output": "2\r\n"}, {"input": "20\r\n-4286 -10000\r\n-4286 -7778\r\n-7143 -7778\r\n-7143 -3334\r\n-10000 -3334\r\n-10000 1110\r\n-4286 1110\r\n-4286 -3334\r\n4285 -3334\r\n4285 -1112\r\n7142 -1112\r\n7142 3332\r\n4285 3332\r\n4285 9998\r\n9999 9998\r\n9999 -3334\r\n7142 -3334\r\n7142 -5556\r\n-1429 -5556\r\n-1429 -10000\r\n-4286 -10000\r\n", "output": "8\r\n"}, {"input": "24\r\n-10000 -10000\r\n-10000 9998\r\n9998 9998\r\n9998 -10000\r\n-6364 -10000\r\n-6364 6362\r\n6362 6362\r\n6362 -6364\r\n-2728 -6364\r\n-2728 2726\r\n2726 2726\r\n2726 -910\r\n908 -910\r\n908 908\r\n-910 908\r\n-910 -4546\r\n4544 -4546\r\n4544 4544\r\n-4546 4544\r\n-4546 -8182\r\n8180 -8182\r\n8180 8180\r\n-8182 8180\r\n-8182 -10000\r\n-10000 -10000\r\n", "output": "10\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-6000 6000\r\n-6000 2000\r\n10000 2000\r\n10000 -2000\r\n-6000 -2000\r\n-6000 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-9800 6000\r\n-9800 2000\r\n10000 2000\r\n10000 -2000\r\n-9800 -2000\r\n-9800 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "4\r\n0 0\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}]
false
stdio
null
true
294/C
294
C
Python 3
TESTS
7
109
716,800
68746083
def fact(ne): if(ne==1 or ne==0): return 1 return ne*fact(ne-1) n,m=map(int,input().split()) mod=(10**9)+7 a=list(map(int,input().split())) d=[0 for x in range(n)] for ele in a: d[ele-1]=1 d=list(map(str,d)) stri="".join(d) ls=list(stri.split("1")) divi=1 multi=1 ttl=0 for count,ele in enumerate(ls): multi*=fact(len(ele))%(mod) ttl+=len(ele)%(mod) if(count==0 or count==len(ls)-1 or n==m): divi*=1 else: divi*=(2**(len(ele)-1))%(mod) ans=((fact(ttl)%mod)*(divi)%mod)//((multi)%mod) print(ans%(mod))
30
109
307,200
68323685
mod = 1000000007 fact = [1] * 2000 for i in range(1, 2000): fact[i] = fact[i - 1] * i % mod n, m = map(int, input().split()) a = sorted(map(int, input().split())) b = [] for i in range (1, m): x = a[i] - a[i - 1] - 1 if (x > 0): b.append(x) count = pow(2, sum(b) - len(b), mod) * fact[n - m] % mod b = [a[0] - 1] + b + [n - a[-1]] for i in b: count = count * pow(fact[i], mod - 2, mod) % mod print(count)
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
873/C
873
C
Python 3
TESTS
3
46
0
31376253
def main(): n, m, k = [int(x) for x in input().split()] matr = [] for i in range(n): matr.append(input().split()) matr = list(zip(*matr)) score = 0 repl = 0 for i in range(m): window = count1(matr[i][:k]) row_scores = [] for j in range(k, n+1): if matr[i][j-k] == '1': row_scores.append(window) window -= 1 else: row_scores.append(0) if j < n and matr[i][j] == '1': window += 1 row_best = max(row_scores) ind_best = row_scores.index(row_best) repl += count1(matr[i][:ind_best]) score += row_best print(score, repl) def count1(lst): return len(list(filter(lambda x: x == '1', lst))) if __name__ == "__main__": main()
20
62
102,400
31252003
n,m,k = [int(i) for i in input().split()] s = [] o = 0 for i in range(m): s.append([]) for i in range(n): l = [int(i) for i in input().split(" ")] for i in range(m): (s[i]).append(l[i]) # print(s) result = 0 c = 0 for x in s: count = 0 for i in range(n-k+1): #print(i) count = max(count, sum(x[i:i+k])) if count == k: break for i in range(n-k+1): if sum(x[i:i+k]) == count: c += sum(x[:i]) break result += count print(result, c)
Educational Codeforces Round 30
ICPC
2,017
1
256
Strange Game On Matrix
Ivan is playing a strange game. He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
null
In the first example Ivan will replace the element a1, 2.
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
1,600
["greedy", "two pointers"]
20
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
false
stdio
null
true
875/C
875
C
Python 3
TESTS
3
46
4,915,200
31496884
def replaceElementEverywhere(element, lst): for i in range(len(lst)): for j, e in enumerate(lst[i][1]): if e == element: lst[i][1][j] = e + '\'' n, m = map(int, input().split()) lst = [] for i in range(n): l, w = input().split(" ", 1) lst.append([int(l), w.split()]) #print(lst) flag = 'Yes' op = '' count = 0 for i in range(len(lst) - 1): mn = min(lst[i][0], lst[i + 1][0]) if flag == 'Yes': stop = False for j in range(mn): if '\'' in lst[i][1][j]: flag = 'No' break if not stop: if lst[i][1][j] < lst[i + 1][1][j]: stop = True elif lst[i][1][j] > lst[i + 1][1][j]: op += lst[i][1][j] + ' ' count += 1 replaceElementEverywhere(lst[i][1][j], lst) #print(lst) else: break print(flag) if not flag == 'No': print(count) if count > 0: print(op)
67
326
24,883,200
181840020
import sys from sys import stdin from collections import deque n,m = map(int,stdin.readline().split()) state = [0] * (m+1) s = [] for i in range(n): word = list(map(int,stdin.readline().split()))[1:] s.append(word) lis = [ [] for i in range(m+1) ] for i in range(n-1): w1 = s[i] w2 = s[i+1] for j in range( min( len(w1) , len(w2) )): if w1[j] != w2[j]: if w1[j] > w2[j]: state[w1[j]] |= 2 state[w2[j]] |= 1 else: lis[w2[j]].append(w1[j]) break else: if len(w1) <= len(w2): continue else: print ("No") sys.exit() q = deque() for i in range(m+1): if state[i] & 2: q.append(i) while q: v = q.popleft() for nex in lis[v]: if state[nex] & 2 == 0: state[nex] |= state[v] q.append(nex) if 3 in state: print ("No") sys.exit() ANS = [] for i in range(m+1): if state[i] == 2: ANS.append(i) print ("Yes") print (len(ANS)) print (" ".join(map(str,ANS)))
Codeforces Round 441 (Div. 1, by Moscow Team Olympiad)
CF
2,017
1
512
National Property
You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library. Some long and uninteresting story was removed... The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter x is denoted by x'. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2 < 3, 2' < 3', 3' < 2. A word x1, x2, ..., xa is not lexicographically greater than y1, y2, ..., yb if one of the two following conditions holds: - a ≤ b and x1 = y1, ..., xa = ya, i.e. the first word is the prefix of the second word; - there is a position 1 ≤ j ≤ min(a, b), such that x1 = y1, ..., xj - 1 = yj - 1 and xj < yj, i.e. at the first position where the words differ the first word has a smaller letter than the second word has. For example, the word "3' 7 5" is before the word "2 4' 6" in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence. Denis has a sequence of words consisting of small letters only. He wants to change some letters to large (let's call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can't change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland's alphabet. Help Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible. Note that some words can be equal.
The first line contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m. Each of the next n lines contains a description of one word in format li, si, 1, si, 2, ..., si, li (1 ≤ li ≤ 100 000, 1 ≤ si, j ≤ m), where li is the length of the word, and si, j is the sequence of letters in the word. The words are given in the order Denis has them in the sequence. It is guaranteed that the total length of all words is not greater than 100 000.
In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes). If the required is possible, in the second line print k — the number of letters Denis has to capitalize (make large), and in the third line print k distinct integers — these letters. Note that you don't need to minimize the value k. You can print the letters in any order. If there are multiple answers, print any of them.
null
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: - 2' - 1 - 1 3' 2' - 1 1 The condition 2' < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3' < 1, then the third word is not lexicographically larger than the fourth word. In the second example the words are in lexicographical order from the beginning, so Denis can do nothing. In the third example there is no set of letters such that if Denis capitalizes them, the sequence becomes lexicographically ordered.
[{"input": "4 3\n1 2\n1 1\n3 1 3 2\n2 1 1", "output": "Yes\n2\n2 3"}, {"input": "6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4", "output": "Yes\n0"}, {"input": "4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1", "output": "No"}]
2,100
["2-sat", "dfs and similar", "graphs", "implementation"]
67
[{"input": "4 3\r\n1 2\r\n1 1\r\n3 1 3 2\r\n2 1 1\r\n", "output": "Yes\r\n2\r\n2 3 "}, {"input": "6 5\r\n2 1 2\r\n2 1 2\r\n3 1 2 3\r\n2 1 5\r\n2 4 4\r\n2 4 4\r\n", "output": "Yes\r\n0\r\n"}, {"input": "4 3\r\n4 3 2 2 1\r\n3 1 1 3\r\n3 2 3 3\r\n2 3 1\r\n", "output": "No\r\n"}, {"input": "4 4\r\n3 3 4 1\r\n4 3 4 2 2\r\n4 2 1 2 3\r\n3 4 2 2\r\n", "output": "Yes\r\n1\r\n3 "}, {"input": "3 5\r\n2 1 2\r\n2 1 5\r\n2 4 4\r\n", "output": "Yes\r\n0\r\n"}, {"input": "2 1\r\n10 1 1 1 1 1 1 1 1 1 1\r\n25 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": "Yes\r\n0\r\n"}, {"input": "10 3\r\n2 3 2\r\n1 3\r\n3 1 3 3\r\n1 2\r\n2 1 2\r\n3 2 2 3\r\n3 3 2 1\r\n1 2\r\n2 1 2\r\n4 1 2 2 3\r\n", "output": "No\r\n"}, {"input": "10 3\r\n2 3 1\r\n1 2\r\n1 1\r\n1 1\r\n2 3 1\r\n1 2\r\n2 3 1\r\n1 1\r\n1 3\r\n2 3 2\r\n", "output": "No\r\n"}, {"input": "10 10\r\n8 1 1 6 10 2 2 9 7\r\n6 2 7 1 9 5 10\r\n1 5\r\n7 3 6 9 6 3 7 6\r\n10 3 9 10 3 6 7 10 6 9 6\r\n10 4 4 9 8 2 10 3 6 2 9\r\n8 4 8 6 4 6 4 8 6\r\n2 7 5\r\n6 8 6 2 1 9 8\r\n3 10 2 10\r\n", "output": "Yes\r\n3\r\n1 2 5 "}, {"input": "10 10\r\n8 2 1 3 2 10 5 4 1\r\n6 2 1 7 5 7 1\r\n9 2 1 7 5 8 2 8 2 9\r\n3 2 1 9\r\n7 2 9 2 2 10 1 7\r\n10 2 9 2 2 10 1 7 4 1 10\r\n5 3 5 2 4 4\r\n7 3 5 9 6 6 5 4\r\n2 5 6\r\n6 5 9 8 7 6 9\r\n", "output": "Yes\r\n0\r\n"}, {"input": "10 4\r\n2 1 4\r\n2 1 4\r\n9 1 4 1 2 3 1 4 4 2\r\n1 4\r\n4 4 1 4 3\r\n7 4 4 4 4 1 4 2\r\n4 4 2 4 3\r\n4 2 4 4 4\r\n1 3\r\n9 3 3 3 4 2 3 3 2 4\r\n", "output": "Yes\r\n2\r\n1 4 "}, {"input": "3 3\r\n1 3\r\n1 2\r\n1 1\r\n", "output": "No\r\n"}, {"input": "2 2\r\n2 1 2\r\n1 1\r\n", "output": "No\r\n"}, {"input": "2 3\r\n3 1 2 3\r\n2 1 2\r\n", "output": "No\r\n"}, {"input": "2 100000\r\n5 1 2 3 1 5\r\n3 1 2 3\r\n", "output": "No\r\n"}, {"input": "4 5\r\n2 1 5\r\n2 1 4\r\n2 2 3\r\n2 2 5\r\n", "output": "Yes\r\n2\r\n3 5 "}, {"input": "2 100\r\n3 1 2 3\r\n1 1\r\n", "output": "No\r\n"}, {"input": "5 5\r\n1 5\r\n1 4\r\n1 3\r\n1 2\r\n1 1\r\n", "output": "No\r\n"}, {"input": "2 1\r\n2 1 1\r\n1 1\r\n", "output": "No\r\n"}, {"input": "2 3\r\n2 1 3\r\n1 1\r\n", "output": "No\r\n"}, {"input": "6 100\r\n1 3\r\n1 5\r\n2 7 5\r\n2 7 2\r\n3 7 7 2\r\n3 7 7 3\r\n", "output": "No\r\n"}]
false
stdio
null
true
748/C
748
C
PyPy 3-64
TESTS
9
77
2,662,400
177457968
t=1 while t>0: t-=1 n=int(input()) s=input() mp=dict() mp['R']=0 mp['L']=0 mp['U']=0 mp['D']=0 ans=0 for i in s: mp[i]+=1 if mp['R']>=1 and mp['L']>=1: ans+=1 mp['R']=0 mp['L']=0 mp['U']=0 mp['L']=0 mp[i]+=1 if mp['U']>=1 and mp['D']>=1: ans+=1 mp['U']=0 mp['D']=0 mp['R']=0 mp['L']=0 mp[i]+=1 ans+=1 print(ans)
32
78
5,222,400
23295854
def codeforces(path): dirs_x = 'LR' dirs_y = 'UD' points = 0 current_x = None current_y = None for char in path: if char in dirs_x: if current_x is None: current_x = char elif current_x != char: points += 1 current_x = char current_y = None elif char in dirs_y: if current_y is None: current_y = char elif current_y != char: points += 1 current_y = char current_x = None return points + 1 _ = input() path = input() print(codeforces(path))
Technocup 2017 - Elimination Round 3
CF
2,016
2
256
Santa Claus and Robot
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order. While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.
The only line of input should contain the minimum possible length of the sequence.
null
The illustrations to the first three tests are given below. The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
[{"input": "4\nRURD", "output": "2"}, {"input": "6\nRRULDD", "output": "2"}, {"input": "26\nRRRULURURUULULLLDLDDRDRDLD", "output": "7"}, {"input": "3\nRLL", "output": "2"}, {"input": "4\nLRLR", "output": "4"}]
1,400
["constructive algorithms", "math"]
32
[{"input": "4\r\nRURD\r\n", "output": "2\r\n"}, {"input": "6\r\nRRULDD\r\n", "output": "2\r\n"}, {"input": "26\r\nRRRULURURUULULLLDLDDRDRDLD\r\n", "output": "7\r\n"}, {"input": "3\r\nRLL\r\n", "output": "2\r\n"}, {"input": "4\r\nLRLR\r\n", "output": "4\r\n"}, {"input": "5\r\nLRDLR\r\n", "output": "4\r\n"}, {"input": "10\r\nDDRDUULUDD\r\n", "output": "3\r\n"}, {"input": "1\r\nD\r\n", "output": "1\r\n"}]
false
stdio
null
true
748/C
748
C
Python 3
PRETESTS
9
46
4,812,800
23292338
def main(): n = int(input()) s = input() fr = None sc = None ans = 0 for elem in s: if (fr == None): fr = elem ans += 1 else: if (sc == None): if (elem == fr): continue else: if (fr == 'L' and elem == 'R') or (fr == 'R' and elem == 'L'): ans += 1 fr = elem sc = None else: sc = elem else: if (elem == fr or elem == sc): continue else: ans += 1 fr = elem sc = None print(ans) main()
32
93
1,945,600
167179679
import sys input = sys.stdin.readline n = int(input()) s = input()[:-1] u = {'L':'R', 'U':'D', 'R':'L', 'D':'U'} d = set() c = 1 for i in s: if u[i] in d: c += 1 d = set() d.add(i) print(c)
Technocup 2017 - Elimination Round 3
CF
2,016
2
256
Santa Claus and Robot
Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order. While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.
The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.
The only line of input should contain the minimum possible length of the sequence.
null
The illustrations to the first three tests are given below. The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.
[{"input": "4\nRURD", "output": "2"}, {"input": "6\nRRULDD", "output": "2"}, {"input": "26\nRRRULURURUULULLLDLDDRDRDLD", "output": "7"}, {"input": "3\nRLL", "output": "2"}, {"input": "4\nLRLR", "output": "4"}]
1,400
["constructive algorithms", "math"]
32
[{"input": "4\r\nRURD\r\n", "output": "2\r\n"}, {"input": "6\r\nRRULDD\r\n", "output": "2\r\n"}, {"input": "26\r\nRRRULURURUULULLLDLDDRDRDLD\r\n", "output": "7\r\n"}, {"input": "3\r\nRLL\r\n", "output": "2\r\n"}, {"input": "4\r\nLRLR\r\n", "output": "4\r\n"}, {"input": "5\r\nLRDLR\r\n", "output": "4\r\n"}, {"input": "10\r\nDDRDUULUDD\r\n", "output": "3\r\n"}, {"input": "1\r\nD\r\n", "output": "1\r\n"}]
false
stdio
null
true
362/C
362
C
Python 3
TESTS
2
31
0
155917540
a = int(input()) b = list(map(int, input().split())) t = [] m = [] s = 0 for i in range(a): t.append(b[i] - i) m.append(abs(b[i] - i)) s += abs(b[i] - i) s = s // 2 c = -1 swaps = 100000000000 for i in range(a): for j in range(i, a): if t[i] > 0 and t[j] < 0: x = t[i] - t[j] y = i + t[i] z = j + t[j] p = t[i] - (j - i) q = t[j] + (j - i) if abs(p) + abs(q) < x: d = x - abs(p + q) k = s - d // 2 if k < swaps: swaps = k c = 1 elif k == swaps: c += 1 print(swaps, c)
38
1,434
10,444,800
71195138
arr = [0 for i in range(5001)] def insertion_sort(n, a): def modify(t): while t > 0: arr[t] += 1 t -= t & (-t) def query(t): res = 0 while t < 5001: res += arr[t] t += t & (-t) return res s = 0 ans = 0 way = 0 for i in range(n): a[i] += 1 for i in range(n): global arr arr = [0 for j in range(5001)] for j in range(i + 1, n): if a[i] < a[j]: continue s += 1 tmp = 1 + 2 * query(a[j]) if tmp > ans: ans = tmp way = 1 elif tmp == ans: way += 1 modify(a[j]) return s - ans, way if __name__ == "__main__": n = int(input()) a = list(map(int, input().split())) result = insertion_sort(n, a) print(*result)
Codeforces Round 212 (Div. 2)
CF
2,013
2
256
Insertion Sort
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order. Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement. It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive — the actual permutation.
Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions.
null
In the first sample the appropriate pairs are (0, 3) and (0, 4). In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
[{"input": "5\n4 0 3 1 2", "output": "3 2"}, {"input": "5\n1 2 3 4 0", "output": "3 4"}]
1,900
["data structures", "dp", "implementation", "math"]
38
[{"input": "5\r\n4 0 3 1 2\r\n", "output": "3 2\r\n"}, {"input": "5\r\n1 2 3 4 0\r\n", "output": "3 4\r\n"}, {"input": "5\r\n1 3 4 0 2\r\n", "output": "4 5\r\n"}, {"input": "10\r\n9 8 7 6 5 4 3 2 1 0\r\n", "output": "28 1\r\n"}, {"input": "5\r\n0 4 1 3 2\r\n", "output": "1 1\r\n"}, {"input": "6\r\n3 0 1 4 5 2\r\n", "output": "4 5\r\n"}, {"input": "3\r\n0 2 1\r\n", "output": "0 1\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "0 1\r\n"}, {"input": "3\r\n1 2 0\r\n", "output": "1 2\r\n"}, {"input": "3\r\n2 0 1\r\n", "output": "1 2\r\n"}, {"input": "3\r\n2 1 0\r\n", "output": "0 1\r\n"}, {"input": "7\r\n4 0 3 5 1 2 6\r\n", "output": "5 2\r\n"}, {"input": "8\r\n1 5 4 0 2 7 3 6\r\n", "output": "7 3\r\n"}, {"input": "9\r\n1 5 6 3 0 7 2 8 4\r\n", "output": "11 4\r\n"}, {"input": "10\r\n8 6 7 9 4 5 2 3 1 0\r\n", "output": "24 1\r\n"}, {"input": "11\r\n4 9 1 2 8 5 10 3 0 7 6\r\n", "output": "16 1\r\n"}, {"input": "12\r\n2 7 0 1 3 10 4 8 11 6 9 5\r\n", "output": "13 1\r\n"}, {"input": "13\r\n5 11 12 10 3 8 4 0 7 9 6 1 2\r\n", "output": "39 4\r\n"}, {"input": "100\r\n73 98 9 92 43 77 32 2 29 5 58 59 61 17 10 94 60 12 80 16 24 91 8 70 62 99 47 23 78 19 22 30 44 96 63 74 48 18 69 45 33 88 97 11 31 66 1 82 7 28 27 41 51 0 37 39 71 75 13 26 20 87 25 40 38 46 79 15 14 81 57 90 83 52 67 6 53 68 54 65 86 93 4 34 95 42 85 72 56 36 89 84 35 64 55 76 21 50 49 3\r\n", "output": "2137 1\r\n"}, {"input": "120\r\n60 100 55 8 106 57 43 85 103 0 6 20 88 102 53 2 116 31 119 59 86 71 99 81 50 22 74 5 80 13 95 118 49 67 17 63 10 27 61 45 101 76 87 72 113 93 92 47 42 41 35 83 97 51 77 114 69 30 91 44 1 84 107 105 16 70 108 65 64 78 25 39 89 23 40 62 117 4 98 24 104 75 58 3 79 112 11 28 109 38 21 19 37 115 9 54 32 111 46 68 90 48 34 12 96 82 29 73 110 18 26 52 36 94 66 15 14 33 7 56\r\n", "output": "3686 1\r\n"}, {"input": "150\r\n48 115 13 9 105 117 41 136 123 32 84 95 62 50 140 106 145 91 57 141 139 35 45 27 129 63 137 10 37 60 44 30 101 119 138 78 22 103 39 134 49 36 25 12 28 67 69 99 148 26 16 87 146 65 8 74 14 38 47 89 81 19 40 11 64 43 110 66 102 3 122 124 100 2 125 42 97 73 121 7 52 23 29 109 1 70 34 108 59 55 127 90 88 144 18 56 17 75 116 5 135 4 15 20 86 94 82 149 126 130 113 33 147 80 54 76 142 96 85 114 112 31 71 133 77 79 93 21 143 128 24 72 68 61 0 131 107 58 132 120 6 46 104 118 53 51 111 83 92 98\r\n", "output": "5113 4\r\n"}]
false
stdio
null
true
1006/A
1006
A
PyPy 3
TESTS
4
124
1,331,200
100343370
n = int(input()) temp = input().split(' ') lst = [] for i in temp: lst.append(int(i)) max_ = max(lst) a = 0 for i in range(len(lst)): if lst[i] % 2 == 1: if lst[i] != max(lst): lst[i] = lst[i] else: lst[i] = lst[i] + 1 else: lst[i] = lst[i] - 1 new_lst = [] for i in lst: new_lst.append(str(i)) print (' '.join(new_lst))
18
31
0
148969041
input();*r,=map(int,input().split());print(*[i-[1,0][i%2] for i in r])
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Adjacent Replacements
Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!). Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: - Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; - Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; - Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; - Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; - Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; - Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; - $$$\dots$$$ - Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; - Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above. For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array. Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.
The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.
Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.
null
The first example is described in the problem statement.
[{"input": "5\n1 2 4 5 10", "output": "1 1 3 5 9"}, {"input": "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999"}]
800
["implementation"]
18
[{"input": "5\r\n1 2 4 5 10\r\n", "output": "1 1 3 5 9\r\n"}, {"input": "10\r\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000\r\n", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999\r\n"}, {"input": "1\r\n999999999\r\n", "output": "999999999\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "1\r\n210400\r\n", "output": "210399\r\n"}, {"input": "5\r\n100000000 100000000 100000000 100000000 100000000\r\n", "output": "99999999 99999999 99999999 99999999 99999999\r\n"}, {"input": "1\r\n2441139\r\n", "output": "2441139\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1 1\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1 1 1\r\n"}, {"input": "2\r\n4 4\r\n", "output": "3 3\r\n"}]
false
stdio
null
true
1006/A
1006
A
Python 3
TESTS
4
31
0
141787284
n=int(input()) l=list(map(int,input().split())) x=[] for i in l: if i%2==0: x.append(i-1) else: x.append(i) if max(l)%2!=0: z=max(x) for obj in range(x.count(z)): y=l.index(z) x.remove(z) x.insert(y,z+1) print(*x)
18
31
0
198036822
n = int(input()) a = list(map(int,input().split())) for i in range(len(a)): if a[i] % 2 == 0: a[i]-=1 print(*a)
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Adjacent Replacements
Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!). Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: - Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; - Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; - Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; - Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; - Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; - Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; - $$$\dots$$$ - Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; - Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above. For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array. Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.
The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.
Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.
null
The first example is described in the problem statement.
[{"input": "5\n1 2 4 5 10", "output": "1 1 3 5 9"}, {"input": "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999"}]
800
["implementation"]
18
[{"input": "5\r\n1 2 4 5 10\r\n", "output": "1 1 3 5 9\r\n"}, {"input": "10\r\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000\r\n", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999\r\n"}, {"input": "1\r\n999999999\r\n", "output": "999999999\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "1\r\n210400\r\n", "output": "210399\r\n"}, {"input": "5\r\n100000000 100000000 100000000 100000000 100000000\r\n", "output": "99999999 99999999 99999999 99999999 99999999\r\n"}, {"input": "1\r\n2441139\r\n", "output": "2441139\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1 1\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1 1 1\r\n"}, {"input": "2\r\n4 4\r\n", "output": "3 3\r\n"}]
false
stdio
null
true
65/B
65
B
Python 3
TESTS
0
77
307,200
93274033
n=int(input()) a=[] for _ in range(n): a.append(input()) prev="1000" z=1 for i in range(n): x=list(a[i]) for j in range(4): if x[j]!=prev[j]: done=0 temp=x[j] for k in range(10): x[j]=chr(k+ord('0')) check=''.join(r for r in x) if int(check)>=int(prev): done=1 break; if done==1 and temp!=x[j]: break a[i]=''.join(r for r in x) if int(a[i])>2011: z=0 break; prev=a[i] if z==1: """for i in a: print(i)""" print(a) else: #print(a) print("No solution")
43
140
3,891,200
141967178
import math import sys import collections def solve(): n = int(input()) r = [list(str(input())) for i in range(n)] last = 0 ok = True for i in range(n): mn = 10000 for ind in range(4): for d in range(10): if ind == 0 and d == 0: continue s = r[i] * 1 s[ind] = str(d) if int("".join(s)) >= last and int("".join(s)) <= mn: mn = int("".join(s)) if mn != 10000 and mn <= 2011: r[i] = mn last = mn else: ok = False break if ok: for i in range(n): print(r[i]) else: print("No solution") if __name__ == '__main__': solve()
Codeforces Beta Round 60
CF
2,011
1
256
Harry Potter and the History of Magic
The History of Magic is perhaps the most boring subject in the Hogwarts school of Witchcraft and Wizardry. Harry Potter is usually asleep during history lessons, and his magical quill writes the lectures for him. Professor Binns, the history of magic teacher, lectures in such a boring and monotonous voice, that he has a soporific effect even on the quill. That's why the quill often makes mistakes, especially in dates. So, at the end of the semester Professor Binns decided to collect the students' parchments with notes and check them. Ron Weasley is in a panic: Harry's notes may contain errors, but at least he has some notes, whereas Ron does not have any. Ronald also has been sleeping during the lectures and his quill had been eaten by his rat Scabbers. Hermione Granger refused to give Ron her notes, because, in her opinion, everyone should learn on their own. Therefore, Ron has no choice but to copy Harry's notes. Due to the quill's errors Harry's dates are absolutely confused: the years of goblin rebellions and other important events for the wizarding world do not follow in order, and sometimes even dates from the future occur. Now Ron wants to change some of the digits while he copies the notes so that the dates were in the chronological (i.e. non-decreasing) order and so that the notes did not have any dates strictly later than 2011, or strictly before than 1000. To make the resulting sequence as close as possible to the one dictated by Professor Binns, Ron will change no more than one digit in each date into other digit. Help him do it.
The first input line contains an integer n (1 ≤ n ≤ 1000). It represents the number of dates in Harry's notes. Next n lines contain the actual dates y1, y2, ..., yn, each line contains a date. Each date is a four-digit integer (1000 ≤ yi ≤ 9999).
Print n numbers z1, z2, ..., zn (1000 ≤ zi ≤ 2011). They are Ron's resulting dates. Print each number on a single line. Numbers zi must form the non-decreasing sequence. Each number zi should differ from the corresponding date yi in no more than one digit. It is not allowed to change the first digit of a number into 0. If there are several possible solutions, print any of them. If there's no solution, print "No solution" (without the quotes).
null
null
[{"input": "3\n1875\n1936\n1721", "output": "1835\n1836\n1921"}, {"input": "4\n9999\n2000\n3000\n3011", "output": "1999\n2000\n2000\n2011"}, {"input": "3\n1999\n5055\n2000", "output": "No solution"}]
1,700
["brute force", "greedy", "implementation"]
43
[{"input": "3\r\n1875\r\n1936\r\n1721\r\n", "output": "1075\r\n1136\r\n1221\r\n"}, {"input": "4\r\n9999\r\n2000\r\n3000\r\n3011\r\n", "output": "1999\r\n2000\r\n2000\r\n2011\r\n"}, {"input": "3\r\n1999\r\n5055\r\n2000\r\n", "output": "No solution\r\n"}, {"input": "2\r\n2037\r\n2025\r\n", "output": "1037\r\n2005\r\n"}, {"input": "1\r\n1234\r\n", "output": "1034\r\n"}, {"input": "1\r\n9876\r\n", "output": "1876\r\n"}, {"input": "2\r\n9988\r\n8899\r\n", "output": "No solution\r\n"}, {"input": "3\r\n1095\r\n1094\r\n1095\r\n", "output": "1005\r\n1014\r\n1015\r\n"}, {"input": "5\r\n5555\r\n4444\r\n3333\r\n2222\r\n1111\r\n", "output": "No solution\r\n"}, {"input": "3\r\n2010\r\n2011\r\n2012\r\n", "output": "1010\r\n1011\r\n1012\r\n"}, {"input": "5\r\n1901\r\n1166\r\n1308\r\n1037\r\n1808\r\n", "output": "1001\r\n1066\r\n1108\r\n1137\r\n1208\r\n"}, {"input": "5\r\n1612\r\n7835\r\n8183\r\n3368\r\n1685\r\n", "output": "No solution\r\n"}, {"input": "10\r\n1501\r\n1617\r\n1368\r\n1737\r\n1800\r\n1272\r\n1019\r\n1545\r\n1035\r\n1302\r\n", "output": "1001\r\n1017\r\n1068\r\n1137\r\n1200\r\n1202\r\n1219\r\n1245\r\n1335\r\n1342\r\n"}, {"input": "10\r\n7577\r\n1411\r\n1864\r\n1604\r\n1589\r\n1343\r\n6832\r\n1648\r\n1222\r\n1832\r\n", "output": "1577\r\n1611\r\n1664\r\n1664\r\n1689\r\n1743\r\n1832\r\n1848\r\n1922\r\n1932\r\n"}, {"input": "10\r\n1110\r\n1278\r\n1283\r\n7758\r\n1183\r\n1214\r\n2970\r\n1398\r\n7515\r\n1005\r\n", "output": "No solution\r\n"}, {"input": "15\r\n2003\r\n1991\r\n1741\r\n1348\r\n1258\r\n1964\r\n1411\r\n1431\r\n1780\r\n1701\r\n1787\r\n1094\r\n1108\r\n1074\r\n1942\r\n", "output": "1003\r\n1091\r\n1141\r\n1148\r\n1158\r\n1164\r\n1211\r\n1231\r\n1280\r\n1301\r\n1387\r\n1394\r\n1408\r\n1474\r\n1542\r\n"}, {"input": "20\r\n1749\r\n1792\r\n1703\r\n1011\r\n1289\r\n1066\r\n1947\r\n1354\r\n1693\r\n1806\r\n1645\r\n1292\r\n1718\r\n1981\r\n1197\r\n1471\r\n1603\r\n1325\r\n1057\r\n1552\r\n", "output": "1049\r\n1092\r\n1103\r\n1111\r\n1189\r\n1266\r\n1347\r\n1350\r\n1393\r\n1406\r\n1445\r\n1492\r\n1518\r\n1581\r\n1597\r\n1671\r\n1673\r\n1725\r\n1757\r\n1852\r\n"}, {"input": "20\r\n1639\r\n1437\r\n1054\r\n1010\r\n1872\r\n1942\r\n1315\r\n1437\r\n1226\r\n1893\r\n1712\r\n1024\r\n1410\r\n1691\r\n1188\r\n1056\r\n1642\r\n1100\r\n1893\r\n1192\r\n", "output": "No solution\r\n"}, {"input": "20\r\n1025\r\n1000\r\n1026\r\n1085\r\n1354\r\n1783\r\n3490\r\n1512\r\n1553\r\n1682\r\n1695\r\n1654\r\n1679\r\n1304\r\n1574\r\n1814\r\n1854\r\n1804\r\n1928\r\n1949\r\n", "output": "1005\r\n1005\r\n1006\r\n1015\r\n1054\r\n1083\r\n1490\r\n1502\r\n1503\r\n1582\r\n1595\r\n1604\r\n1609\r\n1704\r\n1774\r\n1804\r\n1804\r\n1804\r\n1828\r\n1849\r\n"}, {"input": "20\r\n1011\r\n1157\r\n2181\r\n6218\r\n1766\r\n8319\r\n1364\r\n6428\r\n1476\r\n4417\r\n6618\r\n1629\r\n1747\r\n1786\r\n1787\r\n2830\r\n7671\r\n1953\r\n1275\r\n1099\r\n", "output": "No solution\r\n"}, {"input": "10\r\n1014\r\n1140\r\n1692\r\n1644\r\n3647\r\n1716\r\n4821\r\n9839\r\n2882\r\n1664\r\n", "output": "1004\r\n1040\r\n1092\r\n1144\r\n1647\r\n1706\r\n1821\r\n1839\r\n1882\r\n1964\r\n"}, {"input": "10\r\n1075\r\n1133\r\n1393\r\n1350\r\n1369\r\n1403\r\n2643\r\n1653\r\n1756\r\n7811\r\n", "output": "1005\r\n1033\r\n1093\r\n1150\r\n1169\r\n1203\r\n1643\r\n1643\r\n1656\r\n1811\r\n"}, {"input": "10\r\n6025\r\n1522\r\n1835\r\n2142\r\n1414\r\n9547\r\n1456\r\n6784\r\n4984\r\n3992\r\n", "output": "1025\r\n1122\r\n1135\r\n1142\r\n1214\r\n1547\r\n1556\r\n1784\r\n1984\r\n1992\r\n"}, {"input": "10\r\n1074\r\n1547\r\n1554\r\n1581\r\n1170\r\n8683\r\n1434\r\n4750\r\n1866\r\n1051\r\n", "output": "1004\r\n1047\r\n1054\r\n1081\r\n1100\r\n1683\r\n1734\r\n1750\r\n1766\r\n1851\r\n"}, {"input": "10\r\n2008\r\n3007\r\n4066\r\n1017\r\n1920\r\n1113\r\n1317\r\n4746\r\n1972\r\n1598\r\n", "output": "No solution\r\n"}, {"input": "10\r\n1171\r\n1275\r\n1680\r\n7300\r\n4742\r\n2517\r\n7980\r\n1852\r\n1993\r\n5004\r\n", "output": "No solution\r\n"}, {"input": "2\r\n1999\r\n1000\r\n", "output": "1099\r\n1100\r\n"}, {"input": "2\r\n2004\r\n1000\r\n", "output": "1004\r\n1004\r\n"}, {"input": "2\r\n2099\r\n1000\r\n", "output": "1099\r\n1100\r\n"}, {"input": "12\r\n1000\r\n1002\r\n1021\r\n1006\r\n1001\r\n1036\r\n1038\r\n1039\r\n1098\r\n1097\r\n1029\r\n1053\r\n", "output": "1000\r\n1000\r\n1001\r\n1001\r\n1001\r\n1006\r\n1008\r\n1009\r\n1018\r\n1027\r\n1027\r\n1033\r\n"}, {"input": "2\r\n1011\r\n1000\r\n", "output": "1001\r\n1001\r\n"}, {"input": "3\r\n1012\r\n1101\r\n1000\r\n", "output": "1002\r\n1100\r\n1100\r\n"}, {"input": "3\r\n2000\r\n3999\r\n6011\r\n", "output": "1000\r\n1999\r\n2011\r\n"}]
false
stdio
null
true
247/C
250
C
Python 3
TESTS
3
92
0
13533877
__author__ = 'Michael Ilyin' header = input() films = int(header[:header.find(' ')]) genres = int(header[header.find(' '):]) numbers = [int(x) for x in input().split()] res = [[genre, 0] for genre in range(1, genres + 1)] for i, obj in enumerate(numbers): if i == 0: res[obj - 1][1] += 1 continue if i == films - 1: res[obj - 1][1] += 1 continue if numbers[i - 1] == numbers[i + 1]: res[obj - 1][1] += 2 else: res[obj - 1][1] += 1 res = sorted(res, key=lambda x: x[0], reverse=False) res = sorted(res, key=lambda x: x[1], reverse=True) print(str(res[0][0]))
44
404
11,468,800
70358007
n,k = map(int,input().split()) lis=list(map(int,input().split())) ans=[] freq=[0]*(k+1) for i in range(n-1): if lis[i]!=lis[i+1]: ans.append(lis[i]) if lis[-1]!=ans[-1]: ans.append(lis[-1]) #print(ans,freq) l=len(ans) for i in range(1,l-1): if ans[i-1]==ans[i+1]: freq[ans[i]]+=2 else: freq[ans[i]]+=1 freq[ans[-1]]+=1 freq[ans[0]]+=1 print(freq.index(max(freq)))
CROC-MBTU 2012, Final Round
CF
2,012
2
256
Movie Critics
A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre — an integer from 1 to k. On the i-th day the festival will show a movie of genre ai. We know that a movie of each of k genres occurs in the festival programme at least once. In other words, each integer from 1 to k occurs in the sequence a1, a2, ..., an at least once. Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site. As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress. Valentine can't watch all n movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the k genres and will skip all the movies of this genre. He is sure to visit other movies. Valentine wants to choose such genre x (1 ≤ x ≤ k), that the total number of after-movie stresses (after all movies of genre x are excluded) were minimum.
The first line of the input contains two integers n and k (2 ≤ k ≤ n ≤ 105), where n is the number of movies and k is the number of genres. The second line of the input contains a sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ k), where ai is the genre of the i-th movie. It is guaranteed that each number from 1 to k occurs at least once in this sequence.
Print a single number — the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.
null
In the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses. In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.
[{"input": "10 3\n1 1 2 3 2 3 3 1 1 3", "output": "3"}, {"input": "7 3\n3 1 3 2 3 1 2", "output": "1"}]
1,600
[]
44
[{"input": "10 3\r\n1 1 2 3 2 3 3 1 1 3\r\n", "output": "3"}, {"input": "7 3\r\n3 1 3 2 3 1 2\r\n", "output": "1"}, {"input": "2 2\r\n1 2\r\n", "output": "1"}, {"input": "10 2\r\n1 2 2 1 1 2 1 1 2 2\r\n", "output": "1"}, {"input": "10 10\r\n5 7 8 2 4 10 1 3 9 6\r\n", "output": "1"}, {"input": "100 10\r\n6 2 8 1 7 1 2 9 2 6 10 4 2 8 7 5 2 9 5 2 3 2 8 3 7 2 4 3 1 8 8 5 7 10 2 1 8 4 1 4 9 4 2 1 9 3 7 2 4 8 4 3 10 3 9 5 7 7 1 2 10 7 7 8 9 7 1 7 4 8 9 4 1 10 2 4 2 10 9 6 10 5 1 4 2 1 3 1 6 9 10 1 8 9 1 9 1 1 7 6\r\n", "output": "1"}, {"input": "74 10\r\n10 5 4 7 1 9 3 5 10 7 1 4 8 8 4 1 3 9 3 3 10 6 10 4 2 8 9 7 3 2 5 3 6 7 10 4 4 7 8 2 3 10 5 10 5 10 7 9 9 6 1 10 8 9 7 8 9 10 3 6 10 9 9 5 10 6 4 3 5 3 6 8 9 3\r\n", "output": "10"}, {"input": "113 3\r\n1 3 2 2 1 3 1 2 2 2 3 1 1 3 1 3 3 1 2 2 1 3 2 3 3 1 3 1 1 3 3 1 2 3 3 1 3 3 2 3 3 1 1 1 1 2 3 2 2 3 3 2 3 1 3 2 1 3 2 1 1 2 2 2 2 2 1 1 3 3 2 1 1 3 2 2 1 3 1 1 1 3 3 2 1 2 2 3 3 1 3 1 2 2 1 2 2 3 3 2 3 1 3 1 1 2 3 2 3 2 3 1 3\r\n", "output": "3"}, {"input": "100 13\r\n1 1 9 10 6 1 12 13 9 5 3 7 3 5 2 2 10 1 3 8 9 4 4 4 2 10 12 11 1 5 7 13 4 12 5 9 3 13 5 10 7 2 1 7 2 2 4 10 3 10 6 11 13 1 4 3 8 8 9 8 13 4 4 3 7 12 5 5 8 13 1 9 8 12 12 10 4 7 7 12 1 4 3 4 9 6 4 13 10 12 10 9 8 13 13 5 6 9 7 13\r\n", "output": "3"}, {"input": "100 12\r\n9 12 3 3 1 3 12 12 7 9 6 5 8 12 10 7 8 3 4 8 5 9 9 10 9 7 4 5 10 7 4 1 11 6 5 9 1 2 9 9 1 10 6 8 9 10 7 9 10 3 6 4 9 12 11 10 4 4 2 12 11 8 4 9 12 6 4 7 5 1 5 2 7 4 10 2 5 6 4 2 5 8 6 9 6 4 8 6 2 11 4 12 3 1 1 11 1 6 1 10\r\n", "output": "9"}]
false
stdio
null
true
641/D
641
D
PyPy 3
TESTS
11
482
21,606,400
52661966
n = int(input()) mx = list(map(float, input().split())) mn = list(map(float, input().split())) + [0] for i in range(1, n): mx[i] += mx[i - 1] for i in range(n - 2, -1, -1): mn[i] += mn[i + 1] a, b = [0] * (n + 1), [0] * (n + 1) for i in range(n): p = mx[i] s = 1 + mx[i] - mn[i + 1] a[i] = (s - (s * s - 4 * p) ** 0.5) / 2 b[i] = (s + (s * s - 4 * p) ** 0.5) / 2 print(*(a[i] - a[i - 1] for i in range(n))) print(*(b[i] - b[i - 1] for i in range(n)))
20
514
23,552,000
17972323
import math n = int(input()) Y = [0.0] Z = [0.0] Y = Y + [float(y) for y in input().split()] Z = Z + [float(z) for z in input().split()] S = [y + z for y, z in zip(Y, Z)] CS = [0 for i in range(n+1)] for i in range(1, n+1): CS[i] = CS[i-1] + S[i] A = [0 for i in range(0, n+1)] B = [0 for i in range(0, n+1)] CA = 0 for e in range(1, n+1): dis = (CS[e] - 2 * CA) ** 2 + 4 * (S[e] * CA - Y[e]) if abs(dis) < 1e-12: dis = 0 # print(dis) A[e] = CS[e] - 2*CA + math.sqrt(dis) A[e] /= 2 CA += A[e] B[e] = S[e] - A[e] # print(Y, Z, S) # print(CS) print(' '.join(['%.7f' % a for a in A[1:]])) print(' '.join(['%.7f' % a for a in B[1:]]))
VK Cup 2016 - Round 2
CF
2,016
2
256
Little Artem and Random Variable
Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them. Consider two dices. When thrown each dice shows some integer from 1 to n inclusive. For each dice the probability of each outcome is given (of course, their sum is 1), and different dices may have different probability distributions. We throw both dices simultaneously and then calculate values max(a, b) and min(a, b), where a is equal to the outcome of the first dice, while b is equal to the outcome of the second dice. You don't know the probability distributions for particular values on each dice, but you know the probability distributions for max(a, b) and min(a, b). That is, for each x from 1 to n you know the probability that max(a, b) would be equal to x and the probability that min(a, b) would be equal to x. Find any valid probability distribution for values on the dices. It's guaranteed that the input data is consistent, that is, at least one solution exists.
First line contains the integer n (1 ≤ n ≤ 100 000) — the number of different values for both dices. Second line contains an array consisting of n real values with up to 8 digits after the decimal point  — probability distribution for max(a, b), the i-th of these values equals to the probability that max(a, b) = i. It's guaranteed that the sum of these values for one dice is 1. The third line contains the description of the distribution min(a, b) in the same format.
Output two descriptions of the probability distribution for a on the first line and for b on the second line. The answer will be considered correct if each value of max(a, b) and min(a, b) probability distribution values does not differ by more than 10 - 6 from ones given in input. Also, probabilities should be non-negative and their sums should differ from 1 by no more than 10 - 6.
null
null
[{"input": "2\n0.25 0.75\n0.75 0.25", "output": "0.5 0.5\n0.5 0.5"}, {"input": "3\n0.125 0.25 0.625\n0.625 0.25 0.125", "output": "0.25 0.25 0.5\n0.5 0.25 0.25"}]
2,400
["dp", "implementation", "math", "probabilities"]
20
[{"input": "2\r\n0.25 0.75\r\n0.75 0.25\r\n", "output": "0.5 0.5 \r\n0.5 0.5 \r\n"}, {"input": "3\r\n0.125 0.25 0.625\r\n0.625 0.25 0.125\r\n", "output": "0.25 0.25 0.5 \r\n0.5 0.25 0.25 \r\n"}, {"input": "10\r\n0.01 0.01 0.01 0.01 0.01 0.1 0.2 0.2 0.4 0.05\r\n1.0 0 0 0 0 0 0 0 0 0\r\n", "output": "0.010000000000000009 0.010000000000000009 0.010000000000000009 0.009999999999999953 0.010000000000000009 0.10000000000000003 0.2 0.1999999999999999 0.39999999999999825 0.05000000000000182 \r\n1.0 0.0 0.0 0.0 0.0 -1.1102230246251565E-16 1.1102230246251565E-16 0.0 1.9984014443252818E-15 -1.9984014443252818E-15 \r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 1.0\r\n1.0 0 0 0 0 0 0 0 0 0\r\n", "output": "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 \r\n1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \r\n"}, {"input": "1\r\n1.0\r\n1.0\r\n", "output": "1.0 \r\n1.0 \r\n"}, {"input": "2\r\n0.00001 0.99999\r\n0.5 0.5\r\n", "output": "2.000040002400616E-5 0.999979999599976 \r\n0.4999899995999759 0.5000100004000241 \r\n"}, {"input": "3\r\n0.1 0.1 0.8\r\n0.6 0.2 0.2\r\n", "output": "0.20000000000000004 0.07639320225002103 0.7236067977499789 \r\n0.4999999999999999 0.22360679774997905 0.27639320225002106 \r\n"}, {"input": "8\r\n0.09597231 0.11315755 0.32077119 0.22643005 0.03791746 0.04296694 0.10284494 0.05993956\r\n0.52402769 0.19814245 0.20452881 0.06686995 0.00468254 0.00103306 0.00055506 0.00016044\r\n", "output": "0.29869999999999886 0.07920000000000116 0.32760000000000133 0.0734999999999989 0.02229999999999943 0.039699999999999847 0.10169999999999968 0.057299989463288625 \r\n0.32130000000000125 0.23209999999999875 0.19769999999999854 0.219800000000001 0.020300000000000762 0.0043000000000001926 0.0017000000000002569 0.002800010536711417 \r\n"}]
false
stdio
import sys def read_floats(line): return list(map(float, line.strip().split())) def main(): input_path, output_path, submission_path = sys.argv[1:4] with open(input_path) as f: n = int(f.readline()) max_dist = read_floats(f.readline()) min_dist = read_floats(f.readline()) with open(submission_path) as f: a_line = f.readline() b_line = f.readline() if not a_line or not b_line: print(0) return a = read_floats(a_line) b = read_floats(b_line) # Check lengths if len(a) != n or len(b) != n: print(0) return # Check non-negative for x in a + b: if x < -1e-9: print(0) return # Check sums are 1 sum_a = sum(a) sum_b = sum(b) if abs(sum_a - 1) > 1e-6 or abs(sum_b - 1) > 1e-6: print(0) return # Compute prefix sums for max prefix_a = [0.0] * (n + 1) prefix_b = [0.0] * (n + 1) for i in range(1, n+1): prefix_a[i] = prefix_a[i-1] + a[i-1] prefix_b[i] = prefix_b[i-1] + b[i-1] # Compute max distribution computed_max = [] for x in range(1, n+1): a_x = a[x-1] b_x = b[x-1] term1 = a_x * prefix_b[x] term2 = prefix_a[x-1] * b_x computed_max.append(term1 + term2) # Check max distribution for cm, exp in zip(computed_max, max_dist): if abs(cm - exp) > 1e-6 + 1e-9: print(0) return # Compute suffix sums for min suffix_a = [0.0] * (n + 2) suffix_b = [0.0] * (n + 2) for x in range(n, 0, -1): suffix_a[x] = suffix_a[x+1] + a[x-1] suffix_b[x] = suffix_b[x+1] + b[x-1] # Compute min distribution computed_min = [] for x in range(1, n+1): sa = suffix_a[x] sb = suffix_b[x] sa_next = suffix_a[x+1] sb_next = suffix_b[x+1] computed_min.append(sa * sb - sa_next * sb_next) # Check min distribution for cm, exp in zip(computed_min, min_dist): if abs(cm - exp) > 1e-6 + 1e-9: print(0) return # All checks passed print(1) if __name__ == "__main__": main()
true
413/E
413
E
PyPy 3
TESTS
8
155
0
81948796
def s(x,y): global n if l[1-y][x]=='.':newqueue.append((x,1-y)) if x+1<n and l[y][x+1] == '.': newqueue.append((x+1,y)) n,k=map(int,input().split()) l=[list(input()),list(input())] prep=[0] fx=fy=-1 flag=1 cnt=0 for i in range(n): prep.append(prep[-1]+int(l[0][i]=='X')+int(l[1][i]=='X')) if l[0][i]=='.'and flag: fx=i fy=0 flag=0 elif l[1][i]=='.'and flag: fx=i fy=1 flag=0 cnt+=int(l[0][i]=='.')+int(l[1][i]=='.') i=1 queue=[(fx,fy)] ns={} nn=1 lx=fx while cnt: if queue: newqueue=[] cnt-=len(queue) for x,y in queue: l[y][x]=i s(x,y) ns[(x,y)]=nn lx=x queue=set(newqueue) i+=1 else: nn+=1 lx+=1 while True: if l[0][lx]=='.': queue=[(lx,0)] break if l[1][lx]=='.': queue=[(lx,1)] break lx+=1 for _ in range(k): a,b=map(int,input().split()) a-=1 b-=1 ax,ay=a%n,int(a>=n) bx, by = b % n, int(b >= n) if ns[(ax,ay)]!=ns[(bx,by)]:print(-1) elif prep[ax]==prep[bx+1]:print(abs(ax-bx)+abs(ay-by)) else:print(abs(l[ay][ax]-l[by][bx]))
46
1,247
75,776,000
165348230
import sys input = sys.stdin.readline infinity = 10**6 def convert(c): if c == '.': return True else: return False def find_next_dist_helper(i, b, c, shift, dist, maze): if shift == 1: if (not maze[b][i]) or (not maze[c][i + 1]): return infinity if b == c: return 1 if maze[c][i] or maze[b][i + 1]: return 2 return infinity i = 2*i return min(dist[-1][i][b][0] + dist[-1][i + 1][0][c], dist[-1][i][b][1] + dist[-1][i + 1][1][c]) def find_next_dist(i, shift, dist, maze): return ((find_next_dist_helper(i, 0, 0, shift, dist, maze), find_next_dist_helper(i, 0, 1, shift, dist, maze)), (find_next_dist_helper(i, 1, 0, shift, dist, maze), find_next_dist_helper(i, 1, 1, shift, dist, maze))) def preprocess(maze, n): dist = [] shift = 1 while shift < n: i = 0 new_dist = [] while shift*i + shift < n: new_dist.append(find_next_dist(i, shift, dist, maze)) i += 1 shift *= 2 dist.append(new_dist) return dist def get_dist(u, b, v, c, dist, maze): shift = 1 i = 0 pos = u total = 0 while u + shift <= v: if pos % 2 != 0: if dist[i][pos][b][0] < dist[i][pos][b][1]: pos, u, b, total = pos + 1, u + shift, 0, total + dist[i][pos][b][0] else: pos, u, b, total = pos + 1, u + shift, 1, total + dist[i][pos][b][1] shift, i, pos = shift*2, i + 1, pos//2 while u < v: shift, i, pos = shift//2, i - 1, pos*2 if u + shift <= v: if dist[i][pos][b][0] < dist[i][pos][b][1]: pos, u, b, total = pos + 1, u + shift, 0, total + dist[i][pos][b][0] else: pos, u, b, total = pos + 1, u + shift, 1, total + dist[i][pos][b][1] if b != c: if maze[b][u] and maze[c][v]: return total + 1 else: return infinity else: return total n, m = map(int, input().split()) maze = [list(map(convert, input().strip())), list(map(convert, input().strip()))] dist = preprocess(maze, n) for _ in range(m): u, v = map(int, input().split()) u, b, v, c = (u - 1) % n, (u - 1)//n, (v - 1) % n, (v - 1)//n if v < u: u, b, v, c = v, c, u, b if u == v and b == c: print(0) elif u == v and b != c: print(1) else: d = get_dist(u, b, v, c, dist, maze) if d >= infinity: print(-1) else: print(d)
Coder-Strike 2014 - Round 2
CF
2,014
2
256
Maze 2D
The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2 × n maze. Imagine a maze that looks like a 2 × n rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other. Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a 2 × n maze.
The first line contains two integers, n and m (1 ≤ n ≤ 2·105; 1 ≤ m ≤ 2·105) — the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains n characters, each character equals either '.' (empty cell), or 'X' (obstacle). Each of the next m lines contains two integers vi and ui (1 ≤ vi, ui ≤ 2n) — the description of the i-th request. Numbers vi, ui mean that you need to print the value of the shortest path from the cell of the maze number vi to the cell number ui. We assume that the cells of the first line of the maze are numbered from 1 to n, from left to right, and the cells of the second line are numbered from n + 1 to 2n from left to right. It is guaranteed that both given cells are empty.
Print m lines. In the i-th line print the answer to the i-th request — either the size of the shortest path or -1, if we can't reach the second cell from the first one.
null
null
[{"input": "4 7\n.X..\n...X\n5 1\n1 3\n7 7\n1 4\n6 1\n4 7\n5 7", "output": "1\n4\n0\n5\n2\n2\n2"}, {"input": "10 3\nX...X..X..\n..X...X..X\n11 7\n7 18\n18 10", "output": "9\n-1\n3"}]
2,200
["data structures", "divide and conquer"]
46
[{"input": "4 7\r\n.X..\r\n...X\r\n5 1\r\n1 3\r\n7 7\r\n1 4\r\n6 1\r\n4 7\r\n5 7\r\n", "output": "1\r\n4\r\n0\r\n5\r\n2\r\n2\r\n2\r\n"}, {"input": "10 3\r\nX...X..X..\r\n..X...X..X\r\n11 7\r\n7 18\r\n18 10\r\n", "output": "9\r\n-1\r\n3\r\n"}, {"input": "1 1\r\n.\r\n.\r\n1 2\r\n", "output": "1\r\n"}, {"input": "2 1\r\n..\r\n.X\r\n1 2\r\n", "output": "1\r\n"}, {"input": "2 1\r\n..\r\nX.\r\n1 2\r\n", "output": "1\r\n"}, {"input": "2 1\r\n..\r\nX.\r\n1 4\r\n", "output": "2\r\n"}, {"input": "2 1\r\n.X\r\n..\r\n1 4\r\n", "output": "2\r\n"}, {"input": "2 1\r\nX.\r\n..\r\n2 3\r\n", "output": "2\r\n"}, {"input": "2 1\r\n..\r\n.X\r\n3 2\r\n", "output": "2\r\n"}]
false
stdio
null
true
247/C
250
C
Python 3
TESTS
6
560
307,200
91195351
if __name__ == '__main__': a,b=map(int,input().split()) ar = list(map(int,input().split())) arrr=[] t = b while(t>0): j=0 p=0 arr = [] for i in range(0,a): if(ar[i]!=t): arr.append(ar[i]) j=j+1 for k in range(0,j-1): if(arr[k]!=arr[k+1]): p=p+1 arrr.append(p) t = t-1 for i in range(0,b): s = arrr[0] y = b if(arrr[i]<s or arrr[i]==s): s=arrr[i] y=b-i print(y)
44
404
11,571,200
91253710
n,k=map(int,input().split()) r=list(map(int,input().split())) dist=[] ans=[0]*(k+1)#precomp for i in range(n): if i == 0 or r[i] != r[i - 1]: dist.append(r[i]) # dist = distinct for i in range(len(dist)): if 0 < i < len(dist) - 1 and dist[i - 1] == dist[i + 1]: ans[dist[i]] += 2 # removing dist[i] subtracts 2 else: ans[dist[i]] += 1 u, v = -1, -1 for k in range(1, k + 1): if ans[k] > u: u, v = ans[k], k print (v)
CROC-MBTU 2012, Final Round
CF
2,012
2
256
Movie Critics
A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre — an integer from 1 to k. On the i-th day the festival will show a movie of genre ai. We know that a movie of each of k genres occurs in the festival programme at least once. In other words, each integer from 1 to k occurs in the sequence a1, a2, ..., an at least once. Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site. As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress. Valentine can't watch all n movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the k genres and will skip all the movies of this genre. He is sure to visit other movies. Valentine wants to choose such genre x (1 ≤ x ≤ k), that the total number of after-movie stresses (after all movies of genre x are excluded) were minimum.
The first line of the input contains two integers n and k (2 ≤ k ≤ n ≤ 105), where n is the number of movies and k is the number of genres. The second line of the input contains a sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ k), where ai is the genre of the i-th movie. It is guaranteed that each number from 1 to k occurs at least once in this sequence.
Print a single number — the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.
null
In the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses. In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.
[{"input": "10 3\n1 1 2 3 2 3 3 1 1 3", "output": "3"}, {"input": "7 3\n3 1 3 2 3 1 2", "output": "1"}]
1,600
[]
44
[{"input": "10 3\r\n1 1 2 3 2 3 3 1 1 3\r\n", "output": "3"}, {"input": "7 3\r\n3 1 3 2 3 1 2\r\n", "output": "1"}, {"input": "2 2\r\n1 2\r\n", "output": "1"}, {"input": "10 2\r\n1 2 2 1 1 2 1 1 2 2\r\n", "output": "1"}, {"input": "10 10\r\n5 7 8 2 4 10 1 3 9 6\r\n", "output": "1"}, {"input": "100 10\r\n6 2 8 1 7 1 2 9 2 6 10 4 2 8 7 5 2 9 5 2 3 2 8 3 7 2 4 3 1 8 8 5 7 10 2 1 8 4 1 4 9 4 2 1 9 3 7 2 4 8 4 3 10 3 9 5 7 7 1 2 10 7 7 8 9 7 1 7 4 8 9 4 1 10 2 4 2 10 9 6 10 5 1 4 2 1 3 1 6 9 10 1 8 9 1 9 1 1 7 6\r\n", "output": "1"}, {"input": "74 10\r\n10 5 4 7 1 9 3 5 10 7 1 4 8 8 4 1 3 9 3 3 10 6 10 4 2 8 9 7 3 2 5 3 6 7 10 4 4 7 8 2 3 10 5 10 5 10 7 9 9 6 1 10 8 9 7 8 9 10 3 6 10 9 9 5 10 6 4 3 5 3 6 8 9 3\r\n", "output": "10"}, {"input": "113 3\r\n1 3 2 2 1 3 1 2 2 2 3 1 1 3 1 3 3 1 2 2 1 3 2 3 3 1 3 1 1 3 3 1 2 3 3 1 3 3 2 3 3 1 1 1 1 2 3 2 2 3 3 2 3 1 3 2 1 3 2 1 1 2 2 2 2 2 1 1 3 3 2 1 1 3 2 2 1 3 1 1 1 3 3 2 1 2 2 3 3 1 3 1 2 2 1 2 2 3 3 2 3 1 3 1 1 2 3 2 3 2 3 1 3\r\n", "output": "3"}, {"input": "100 13\r\n1 1 9 10 6 1 12 13 9 5 3 7 3 5 2 2 10 1 3 8 9 4 4 4 2 10 12 11 1 5 7 13 4 12 5 9 3 13 5 10 7 2 1 7 2 2 4 10 3 10 6 11 13 1 4 3 8 8 9 8 13 4 4 3 7 12 5 5 8 13 1 9 8 12 12 10 4 7 7 12 1 4 3 4 9 6 4 13 10 12 10 9 8 13 13 5 6 9 7 13\r\n", "output": "3"}, {"input": "100 12\r\n9 12 3 3 1 3 12 12 7 9 6 5 8 12 10 7 8 3 4 8 5 9 9 10 9 7 4 5 10 7 4 1 11 6 5 9 1 2 9 9 1 10 6 8 9 10 7 9 10 3 6 4 9 12 11 10 4 4 2 12 11 8 4 9 12 6 4 7 5 1 5 2 7 4 10 2 5 6 4 2 5 8 6 9 6 4 8 6 2 11 4 12 3 1 1 11 1 6 1 10\r\n", "output": "9"}]
false
stdio
null
true
755/B
755
B
Python 3
TESTS
15
280
1,433,600
121868404
numbers = input() numbersList = numbers.split() n = int(numbersList[0]) m = int(numbersList[1]) totalOfWords = n + m game = [] polandBall = [] enemyBall = [] polandPoints = 0 enemyPoints = 0 def playsWord(playerWords, playerPoints): if(len(playerWords) > 0): if (playerWords[0] not in game): game.append(playerWords.pop(0)) playerPoints += 1 return playerPoints else: playerWords.pop(0) return playsWord(playerWords, playerPoints) return playerPoints if (n >= 1 and m <= 1000): for i in range(totalOfWords): #Mapping the arrays: Done word = input() if (word.isalpha() and len(word)<=500 and word.islower()): if (i < n and word not in polandBall): polandBall.append(word) elif (i >= n and i < totalOfWords and word not in enemyBall): enemyBall.append(word) if (len(polandBall) > len(enemyBall)): #Basic scenarios, who knows more words wins: Done print("YES") elif (len(enemyBall) > len(polandBall)): print("NO") elif (len(polandBall) == 0 and len(enemyBall) == 0): #empty strings: Done print("NO") else: #Both players knows the exact amount of words while (len(polandBall) != 0 or len(enemyBall) != 0): polandPoints = playsWord(polandBall, polandPoints) enemyPoints = playsWord(enemyBall, enemyPoints) if (polandPoints > enemyPoints): print("YES") else: print("NO") else: print("NO")
33
46
921,600
166006431
n,m = map(int,input().split()) p = set() for i in range(n):p.add(input()) e = set() for i in range(m):e.add(input()) t = p&e; p = p-t; e = e-t print("YNEOS"[len(p)+ len(t)%2<=len(e)::2])
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
358/C
358
C
Python 3
TESTS
4
46
102,400
157235619
from collections import deque def main(): stack, queue, deck = deque(), deque(), deque() most_recent_append = None number_of_commands = int(input()) for _ in range(number_of_commands): appended_value = int(input()) if appended_value == 0: number_of_extractions = 0 extractions = "" if len(stack) > 0: stack.pop() extractions += " popStack" number_of_extractions += 1 if len(queue) > 0: queue.popleft() extractions += " popQueue" number_of_extractions += 1 if len(deck) > 0: deck.pop() extractions += " popBack" number_of_extractions += 1 print(f"{number_of_extractions}{extractions}") most_recent_append = None else: if most_recent_append in {None, "deck"}: stack.append(appended_value) print("pushStack") most_recent_append = "stack" elif most_recent_append == "stack": queue.appendleft(appended_value) print("pushQueue") most_recent_append = "queue" else: deck.appendleft(appended_value) print("pushFront") most_recent_append = "deck" if __name__ == "__main__": main()
26
1,684
3,788,800
4888984
n = int(input()) #a = list(map(int, input().split())) a = [0] * n for i in range(n): a[i] = int(input()) i = -1 while (1): j = i + 1 while j < n and a[j] != 0: j += 1 if j == n: for k in range(i + 1, n): print('pushBack') break if j == i + 1: print(0) if j == i + 2: print('pushStack\n1 popStack') if j == i + 3: print('pushStack\npushQueue\n2 popStack popQueue') if j == i + 4: print('pushStack\npushQueue\npushFront\n3 popStack popQueue popFront') if (j > i + 4): h = [] g = [0] * n for k in range(i + 1, j): h.append([a[k], k]) h.sort() g[h[-1][1]] = 1 g[h[-2][1]] = 2 g[h[-3][1]] = 3 for k in range(i + 1, j): if g[k] == 0: print('pushBack') if g[k] == 1: print('pushStack') if g[k] == 2: print('pushQueue') if g[k] == 3: print('pushFront') print('3 popStack popQueue popFront') i = j
Codeforces Round 208 (Div. 2)
CF
2,013
2
256
Dima and Containers
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck. Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands: 1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end. 2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers. Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation. As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
The first line contains integer n (1 ≤ n ≤ 105) — the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer: 1. Integer a (1 ≤ a ≤ 105) means that Inna gives Dima a command to add number a into one of containers. 2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Each command of the input must correspond to one line of the output — Dima's action. For the command of the first type (adding) print one word that corresponds to Dima's choice: - pushStack — add to the end of the stack; - pushQueue — add to the end of the queue; - pushFront — add to the beginning of the deck; - pushBack — add to the end of the deck. For a command of the second type first print an integer k (0 ≤ k ≤ 3), that shows the number of extract operations, then print k words separated by space. The words can be: - popStack — extract from the end of the stack; - popQueue — extract from the beginning of the line; - popFront — extract from the beginning from the deck; - popBack — extract from the end of the deck. The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers. The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
null
null
[{"input": "10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0", "output": "0\npushStack\n1 popStack\npushStack\npushQueue\n2 popStack popQueue\npushStack\npushQueue\npushFront\n3 popStack popQueue popFront"}, {"input": "4\n1\n2\n3\n0", "output": "pushStack\npushQueue\npushFront\n3 popStack popQueue popFront"}]
2,000
["constructive algorithms", "greedy", "implementation"]
26
[{"input": "10\r\n0\r\n1\r\n0\r\n1\r\n2\r\n0\r\n1\r\n2\r\n3\r\n0\r\n", "output": "0\r\npushStack\r\n1 popStack\r\npushStack\r\npushQueue\r\n2 popStack popQueue\r\npushStack\r\npushQueue\r\npushFront\r\n3 popStack popQueue popFront\r\n"}, {"input": "4\r\n1\r\n2\r\n3\r\n0\r\n", "output": "pushStack\r\npushQueue\r\npushFront\r\n3 popStack popQueue popFront\r\n"}, {"input": "2\r\n0\r\n1\r\n", "output": "0\r\npushQueue\r\n"}, {"input": "5\r\n1\r\n1\r\n1\r\n2\r\n1\r\n", "output": "pushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\n"}, {"input": "5\r\n3\r\n2\r\n3\r\n1\r\n0\r\n", "output": "pushStack\r\npushQueue\r\npushFront\r\npushBack\r\n3 popStack popQueue popFront\r\n"}, {"input": "49\r\n8735\r\n95244\r\n50563\r\n33648\r\n10711\r\n30217\r\n49166\r\n28240\r\n0\r\n97232\r\n12428\r\n16180\r\n58610\r\n61112\r\n74423\r\n56323\r\n43327\r\n0\r\n12549\r\n48493\r\n43086\r\n69266\r\n27033\r\n37338\r\n43900\r\n5570\r\n25293\r\n44517\r\n7183\r\n41969\r\n31944\r\n32247\r\n96959\r\n44890\r\n98237\r\n52601\r\n29081\r\n93641\r\n14980\r\n29539\r\n84672\r\n57310\r\n91014\r\n31721\r\n6944\r\n67672\r\n22040\r\n86269\r\n86709\r\n", "output": "pushBack\npushStack\npushQueue\npushBack\npushBack\npushBack\npushFront\npushBack\n3 popStack popQueue popFront\npushStack\npushBack\npushBack\npushBack\npushFront\npushQueue\npushBack\npushBack\n3 popStack popQueue popFront\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushFront\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushQueue\npushBack\npushBack\npushBack\npushBack\n"}, {"input": "55\r\n73792\r\n39309\r\n73808\r\n47389\r\n34803\r\n87947\r\n32460\r\n14649\r\n70151\r\n35816\r\n8272\r\n78886\r\n71345\r\n61907\r\n16977\r\n85362\r\n0\r\n43792\r\n8118\r\n83254\r\n89459\r\n32230\r\n87068\r\n82617\r\n94847\r\n83528\r\n37629\r\n31438\r\n97413\r\n62260\r\n13651\r\n47564\r\n43543\r\n61292\r\n51025\r\n64106\r\n0\r\n19282\r\n35422\r\n19657\r\n95170\r\n10266\r\n43771\r\n3190\r\n93962\r\n11747\r\n43021\r\n91531\r\n88370\r\n1760\r\n10950\r\n77059\r\n61741\r\n52965\r\n10445\r\n", "output": "pushBack\npushBack\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushBack\npushBack\npushBack\npushFront\npushBack\npushBack\npushBack\npushQueue\n3 popStack popQueue popFront\npushBack\npushBack\npushBack\npushFront\npushBack\npushBack\npushBack\npushQueue\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\n3 popStack popQueue popFront\npushBack\npushBack\npushBack\npushBack\npushFront\npushBack\npushQueue\npushBack\npushBack\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushBack\npushBack\npushBack\n"}, {"input": "10\r\n1\r\n2\r\n3\r\n5\r\n4\r\n9\r\n8\r\n6\r\n7\r\n0\r\n", "output": "pushBack\r\npushBack\r\npushBack\r\npushBack\r\npushBack\r\npushStack\r\npushQueue\r\npushBack\r\npushFront\r\n3 popStack popQueue popFront\r\n"}, {"input": "10\r\n1\r\n3\r\n4\r\n2\r\n6\r\n8\r\n5\r\n7\r\n10\r\n9\r\n", "output": "pushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}]
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_path): with open(input_path) as f: input_lines = f.read().splitlines() n = int(input_lines[0]) input_commands = input_lines[1:n+1] with open(submission_path) as f: submission_lines = [line.strip() for line in f] if len(submission_lines) != len(input_commands): print(0) return stack = [] queue = [] deck = deque() sub_idx = 0 for cmd in input_commands: if cmd == '0': if sub_idx >= len(submission_lines): print(0) return line = submission_lines[sub_idx] sub_idx += 1 parts = line.split() if not parts: print(0) return try: k = int(parts[0]) except: print(0) return if k < 0 or k > 3 or len(parts) != k + 1: print(0) return ops = parts[1:] # Compute max possible sum using copies stack_copy = list(stack) queue_copy = list(queue) deck_copy = deque(deck) contributions = [] if stack_copy: contributions.append(stack_copy[-1]) if queue_copy: contributions.append(queue_copy[0]) if deck_copy: deck_val = max(deck_copy[0], deck_copy[-1]) if deck_copy else 0 contributions.append(deck_val) contributions.sort(reverse=True) max_sum = sum(contributions[:3]) # Validate submission's ops temp_stack = list(stack) temp_queue = list(queue) temp_deck = deque(deck) sum_sub = 0 used = set() valid = True for op in ops: container = None if op == 'popStack': if not temp_stack: valid = False break sum_sub += temp_stack.pop() container = 'stack' elif op == 'popQueue': if not temp_queue: valid = False break sum_sub += temp_queue.pop(0) container = 'queue' elif op == 'popFront': if not temp_deck: valid = False break sum_sub += temp_deck.popleft() container = 'deck' elif op == 'popBack': if not temp_deck: valid = False break sum_sub += temp_deck.pop() container = 'deck' else: valid = False break if container in used: valid = False break used.add(container) if not valid or sum_sub != max_sum: print(0) return # Clear containers stack.clear() queue.clear() deck.clear() else: a = int(cmd) if sub_idx >= len(submission_lines): print(0) return line = submission_lines[sub_idx] sub_idx += 1 if line == 'pushStack': stack.append(a) elif line == 'pushQueue': queue.append(a) elif line == 'pushFront': deck.appendleft(a) elif line == 'pushBack': deck.append(a) else: print(0) return print(1) if __name__ == '__main__': main(sys.argv[1], sys.argv[2], sys.argv[3])
true
883/F
883
F
PyPy 3
TESTS
6
78
0
108009561
n = int(input()) # print(n); a = set(); while(n): s = input(); s = s.replace('oo', 'u'); s = s.replace('uo', 'ou'); t = s; s = s.replace('kh', 'h'); while(t!=s): t = s; s = s.replace('kh', 'h'); a.add(s); n-=1; print(len(a))
81
62
0
31721303
def replacement(str): str2 = str.replace("oo","u") str3 = str2.replace("kh","h") if str3 == str: return str3 else : str3 = replacement(str3) return str3 n = int(input()) myList = [] myList2 = [] for i in range(n): myList.append(input()) for x in myList: str4 = replacement(x) str4 = str4.replace("u","oo") str4 = str4.replace("h","kh") exist = False for str in myList2: if str4 == str: exist = True if exist == False: myList2.append(str4) print(len(myList2))
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Lost in Transliteration
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
Print the minimal number of groups where the words in each group denote the same name.
null
There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
1,300
["implementation"]
81
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
false
stdio
null
true
883/F
883
F
Python 3
TESTS
5
77
409,600
31967574
import re def cut(s): meetH = False result = [] for i in range(len(s) - 1, -1, -1): if not meetH and s[i] == 'h': meetH = True result.append(s[i]) continue elif meetH and s[i] == 'k': continue elif meetH and s[i] != 'k': result.append(s[i]) continue elif not meetH and s[i] != 'h': result.append(s[i]) result.reverse() s = ''.join(result) return s d = {} n = int(input()) for i in range(n): s = input().replace('u', 'oo') s = cut(s) if s not in d: d[s] = 0 d[s] += 1 print(len(d))
81
62
0
31861395
from sys import stdin words = set() def ko(w): while 'kh' in w: w = w.replace('kh', 'h', 1) return w.replace('u', 'oo') def solve(): inp = stdin.read().strip().split()[1:] for w in inp: words.add(ko(w)) print(len(words)) solve()
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Lost in Transliteration
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
Print the minimal number of groups where the words in each group denote the same name.
null
There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
1,300
["implementation"]
81
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
false
stdio
null
true
755/B
755
B
PyPy 3-64
TESTS
15
202
4,403,200
138930699
n, m = [int(x) for x in input().split(' ')] polandWords = [] enemyWords = [] for _ in range(n): word = input() polandWords.append(word) for _ in range(m): word = input() enemyWords.append(word) if n > m: print('YES') elif m > n: print('NO') else: words_used = set() polandCount = 0 enemyCount = 0 while True: while len(polandWords) > 0: pword = polandWords.pop(0) if pword not in words_used: polandCount += 1 words_used.add(pword) break while len(enemyWords) > 0: eword = enemyWords.pop(0) if eword not in words_used: enemyCount += 1 words_used.add(eword) break if len(polandWords) == 0 and len(enemyWords) == 0: break if polandCount > enemyCount: print('YES') else: print('NO')
33
46
1,228,800
164436632
n, m = [int(x) for x in input().split(' ')] done = {} f = set([]) s = set([]) for _ in range(n): f.add(input()) # print(f) for _ in range(m): s.add(input()) # print(s) common = f.intersection(s) # print('common is', list(common)) first = list(common) second = list(common) for item in f: if item not in common: first.append(item) for item in s: if item not in common: second.append(item) i = 0 j = 0 f = first s = second # print(f) # print(s) while True: if i >= len(f): print('NO') exit() while i < len(f) and f[i] in done: i += 1 if i >= len(f): print('NO') exit() # print('First played', f[i]) done[f[i]] = 1 if j >= len(s): print('YES') exit() while j < len(s) and s[j] in done: j += 1 if j >= len(s): print('YES') exit() done[s[j]] = 1 # print('Second played', s[j])
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
91/C
91
C
PyPy 3
TESTS
8
140
0
51287988
n, m = map(int, input().split()) p, r = list(range(n + 1)), [0] * (n + 1) def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] def union(x, y): x, y = find(x), find(y) if x == y: return 0 if r[x] < r[y]: x, y = y, x p[y] = x r[x] += r[x] == r[y] return 1 MOD = 10 ** 9 + 7 ans = 1 out = [] for _ in range(m): u, v = map(int, input().split()) if not union(u, v): ans = (ans << 1) % MOD out.append((ans + MOD - 1) % MOD) print(*out, sep='\n')
65
701
9,420,800
125489179
import sys input = sys.stdin.readline def getp(a): if P[a] == a: return a P[a] = getp(P[a]) return P[a] def solve(): global P n, m = map(int, input().split()) P = list(range(n+1)) MOD = int(1e9+9) r = 0 for i in range(m): a, b = map(int, input().split()) a = getp(a) b = getp(b) if a == b: r += 1 else: P[a] = b print((pow(2, r, MOD)-1) % MOD) solve()
Codeforces Beta Round 75 (Div. 1 Only)
CF
2,011
2
256
Ski Base
A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way. In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi. Track is the route with the following properties: - The route is closed, that is, it begins and ends in one and the same junction. - The route contains at least one road. - The route doesn't go on one road more than once, however it can visit any junction any number of times. Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected. Two ski bases are considered different if they consist of different road sets. After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.
The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers of the connected junctions. There could be more than one road between a pair of junctions.
Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).
null
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this: The land lot for the construction will look in the following way: We can choose a subset of roads in three ways: In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.
[{"input": "3 4\n1 3\n2 3\n1 2\n1 2", "output": "0\n0\n1\n3"}]
2,300
["combinatorics", "dsu", "graphs"]
65
[{"input": "3 4\r\n1 3\r\n2 3\r\n1 2\r\n1 2\r\n", "output": "0\r\n0\r\n1\r\n3\r\n"}, {"input": "15 29\r\n6 11\r\n14 3\r\n10 4\r\n14 7\r\n6 14\r\n7 15\r\n13 8\r\n10 13\r\n4 14\r\n15 8\r\n12 7\r\n3 5\r\n6 7\r\n8 1\r\n4 5\r\n11 5\r\n10 6\r\n11 3\r\n13 14\r\n7 10\r\n3 12\r\n7 14\r\n8 11\r\n7 15\r\n15 8\r\n12 7\r\n4 3\r\n9 4\r\n8 10\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n3\r\n3\r\n7\r\n15\r\n31\r\n63\r\n127\r\n255\r\n511\r\n1023\r\n2047\r\n4095\r\n8191\r\n16383\r\n32767\r\n32767\r\n65535\r\n"}, {"input": "34 27\r\n19 10\r\n8 31\r\n26 22\r\n2 30\r\n32 26\r\n30 4\r\n34 1\r\n2 31\r\n4 18\r\n33 11\r\n10 13\r\n20 23\r\n4 32\r\n23 27\r\n30 7\r\n10 17\r\n29 9\r\n18 10\r\n2 28\r\n3 12\r\n31 8\r\n3 25\r\n5 22\r\n3 16\r\n21 1\r\n10 30\r\n5 3\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n3\r\n3\r\n"}, {"input": "29 27\r\n22 8\r\n6 2\r\n3 5\r\n23 29\r\n27 23\r\n18 23\r\n28 23\r\n23 12\r\n24 15\r\n13 6\r\n1 13\r\n9 7\r\n17 6\r\n4 16\r\n20 28\r\n23 3\r\n3 19\r\n16 23\r\n10 21\r\n15 2\r\n21 28\r\n3 9\r\n8 18\r\n10 28\r\n19 18\r\n17 18\r\n13 7\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n3\r\n3\r\n7\r\n"}, {"input": "27 28\r\n20 14\r\n21 5\r\n11 17\r\n14 9\r\n17 13\r\n7 19\r\n24 27\r\n16 9\r\n5 1\r\n2 12\r\n9 2\r\n15 7\r\n13 6\r\n15 17\r\n25 17\r\n2 3\r\n1 15\r\n12 25\r\n10 6\r\n1 8\r\n1 6\r\n5 24\r\n3 15\r\n12 7\r\n2 12\r\n16 15\r\n8 22\r\n8 18\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n3\r\n7\r\n15\r\n31\r\n31\r\n31\r\n"}, {"input": "20 29\r\n8 13\r\n19 18\r\n5 20\r\n5 10\r\n14 11\r\n20 8\r\n12 11\r\n13 20\r\n18 10\r\n3 9\r\n7 18\r\n19 13\r\n2 6\r\n20 19\r\n9 3\r\n6 10\r\n14 18\r\n16 12\r\n17 20\r\n1 15\r\n14 12\r\n13 5\r\n11 4\r\n2 16\r\n3 1\r\n11 4\r\n17 5\r\n5 8\r\n18 12\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n3\r\n3\r\n7\r\n15\r\n15\r\n15\r\n15\r\n15\r\n15\r\n31\r\n63\r\n63\r\n127\r\n127\r\n255\r\n511\r\n1023\r\n2047\r\n"}, {"input": "28 25\r\n17 28\r\n21 3\r\n4 7\r\n17 18\r\n13 12\r\n26 20\r\n1 17\r\n10 18\r\n10 16\r\n1 4\r\n15 3\r\n27 26\r\n11 14\r\n7 9\r\n1 13\r\n14 27\r\n14 23\r\n21 27\r\n8 7\r\n16 2\r\n5 25\r\n26 18\r\n21 2\r\n4 3\r\n4 10\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n3\r\n7\r\n"}, {"input": "27 29\r\n12 11\r\n21 20\r\n19 26\r\n16 24\r\n22 4\r\n1 3\r\n23 5\r\n9 1\r\n4 3\r\n21 23\r\n22 8\r\n14 6\r\n25 13\r\n7 20\r\n9 16\r\n3 20\r\n23 19\r\n17 10\r\n13 18\r\n8 14\r\n23 25\r\n25 27\r\n19 15\r\n19 15\r\n17 24\r\n12 27\r\n18 11\r\n25 5\r\n22 17\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n3\r\n7\r\n15\r\n"}, {"input": "2 40\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n", "output": "0\n1\n3\n7\n15\n31\n63\n127\n255\n511\n1023\n2047\n4095\n8191\n16383\n32767\n65535\n131071\n262143\n524287\n1048575\n2097151\n4194303\n8388607\n16777215\n33554431\n67108863\n134217727\n268435455\n536870911\n73741814\n147483629\n294967259\n589934519\n179869030\n359738061\n719476123\n438952238\n877904477\n755808946\n"}]
false
stdio
null
true
883/F
883
F
Python 3
TESTS
6
62
0
31970248
def rev(s): s = s[::-1] news = [] for i in range(len(s)): letter = s[i] if(i == 0): news.append(letter) continue if(letter == 'k'): if(news[-1] == 'h'): continue news.append(letter) continue if(letter == 'o'): if(news[-1] == 'o'): news.pop() news.append('u') continue news.append(letter) continue news.append(letter) return ''.join(news[::-1]) def transform(s): news = [] for item in s: news.append(item) f = 0 while(f < len(s)): index = s[f:].find('o') if(index == -1):break if(index == 0): f += 1 continue if(news[index - 1] != 'u'): f += 1 continue changeindex = index - 1 while(news[changeindex] == 'u'): changeindex -= 1 changeindex += 1 news[index], news[changeindex] = news[changeindex], news[index] f += 1 return ''.join(news) n = int(input()) g = set() for i in range(n): s = transform(rev(input())) g.add(s) print(len(g))
81
62
0
31883342
n=int(input()) d=[] for i in range(0,n): s=input() s=s.replace("u","oo") while s.count("kh")!=0: s=s.replace("kh","h") if not s in d: d.append(s) print (len(d))
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Lost in Transliteration
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
Print the minimal number of groups where the words in each group denote the same name.
null
There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
1,300
["implementation"]
81
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
false
stdio
null
true
883/F
883
F
Python 3
TESTS
6
93
307,200
48861315
n = int(input()) s = set() for i in range(n): t = input() tmp = "" j = 0 while j < len(t): if j < len(t) - 1 and t[j] == "o" and t[j + 1] == "o": tmp += "u" j += 2 else: tmp += t[j] j += 1 t = tmp tmp = "" j = 0 while j < len(t): if j < len(t) - 1 and t[j] == "u" and t[j + 1] == "o": tmp += "ou" j += 2 else: tmp += t[j] j += 1 t = tmp tmp = "" j = len(t) - 1 while j >= 0: if j >= 0 and t[j] == "h" and t[j - 1] == "k": j -= 1 k = j tmp += "h" while k >= 0 and t[k] == "k": k -= 1 j -= 1 else: tmp += t[j] j -= 1 s.add("".join(tmp[::-1])) print(len(s))
81
62
0
31916795
n = int(input()) names = set() for rd in range(n): prev_name = input() name = prev_name.replace('u','oo').replace('kh', 'h') while name != prev_name: prev_name = name name = name.replace('kh', 'h') names.add(name) print(len(names))
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Lost in Transliteration
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
Print the minimal number of groups where the words in each group denote the same name.
null
There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
1,300
["implementation"]
81
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
false
stdio
null
true
843/A
843
A
PyPy 3
TESTS
5
202
6,246,400
71695350
n = int(input()) arr = list(map(int,input().split())) temp = arr[::] d = {} temp.sort() for i in range(n): d[temp[i]] = i visited = [False for i in range(n)] ans = [] cnt = 0 for i in range(n): if i==d[arr[i]]: ans.append([1,i+1]) visited[i]==True for i in range(n): if visited[i]==False: ind = i m = -1 while True: m = max(m,ind) if visited[ind]==False: visited[ind] = True elif visited[ind]==True: break ind = d[arr[ind]] final = [] for j in range(cnt,m+1): if d[arr[j]]==j: pass else: final.append(j+1) cnt = max(m+1,cnt) if len(final)!=0: final = [len(final)]+final ans.append(final) print(len(ans)) for i in ans: print(i[0],*i[1::])
71
327
32,768,000
216164379
import sys from collections import defaultdict input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) d = defaultdict(int) b = sorted(a) for i in range(n): d[b[i]] = i + 1 for i in range(n): a[i] = d[a[i]] ans = [] v = [0] * (n + 1) for i in range(1, n + 1): if(not v[i]): c = [i] j = a[i - 1] while(j != i): c.append(j) v[j] = 1 j = a[j - 1] ans.append(c) print(len(ans)) for i in ans: print(len(i), *i)
AIM Tech Round 4 (Div. 1)
CF
2,017
1
256
Sorting by Subsequences
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order. Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places. Every element of the sequence must appear in exactly one subsequence.
The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence. The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements. In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence. Indices could be printed in any order. Every index from 1 to n must appear in output exactly once. If there are several possible answers, print any of them.
null
In the first sample output: After sorting the first subsequence we will get sequence 1 2 3 6 5 4. Sorting the second subsequence changes nothing. After sorting the third subsequence we will get sequence 1 2 3 4 5 6. Sorting the last subsequence changes nothing.
[{"input": "6\n3 2 1 6 5 4", "output": "4\n2 1 3\n1 2\n2 4 6\n1 5"}, {"input": "6\n83 -75 -49 11 37 62", "output": "1\n6 1 2 3 4 5 6"}]
1,400
["dfs and similar", "dsu", "implementation", "math", "sortings"]
71
[{"input": "6\r\n3 2 1 6 5 4\r\n", "output": "4\r\n2 1 3\r\n1 2\r\n2 4 6\r\n1 5\r\n"}, {"input": "6\r\n83 -75 -49 11 37 62\r\n", "output": "1\r\n6 1 2 3 4 5 6\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1 1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 1\r\n1 2\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1\r\n2 1 2\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "3\r\n1 1\r\n1 2\r\n1 3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n2 1 3\r\n1 2\r\n"}, {"input": "3\r\n3 1 2\r\n", "output": "1\r\n3 1 2 3\r\n"}, {"input": "10\r\n3 7 10 1 9 5 4 8 6 2\r\n", "output": "3\r\n6 1 4 7 2 10 3\r\n3 5 6 9\r\n1 8\r\n"}, {"input": "20\r\n363756450 -204491568 95834122 -840249197 -49687658 470958158 -445130206 189801569 802780784 -790013317 -192321079 586260100 -751917965 -354684803 418379342 -253230108 193944314 712662868 853829789 735867677\r\n", "output": "3\r\n7 1 4 7 2 10 3 13\r\n11 5 14 15 6 16 12 17 18 20 19 9\r\n2 8 11\r\n"}, {"input": "50\r\n39 7 45 25 31 26 50 11 19 37 8 16 22 33 14 6 12 46 49 48 29 27 41 15 34 24 3 13 20 47 9 36 5 43 40 21 2 38 35 42 23 28 1 32 10 17 30 18 44 4\r\n", "output": "6\r\n20 1 43 34 25 4 50 7 2 37 10 45 3 27 22 13 28 42 40 35 39\r\n23 5 33 14 15 24 26 6 16 12 17 46 18 48 20 29 21 36 32 44 49 19 9 31\r\n2 8 11\r\n2 23 41\r\n2 30 47\r\n1 38\r\n"}, {"input": "100\r\n39 77 67 25 81 26 50 11 73 95 86 16 90 33 14 79 12 100 68 64 60 27 41 15 34 24 3 61 83 47 57 65 99 43 40 21 94 72 82 85 23 71 76 32 10 17 30 18 44 59 35 89 6 63 7 69 62 70 4 29 92 87 31 48 36 28 45 97 93 98 56 38 58 80 8 1 74 91 53 55 54 51 96 5 42 52 9 22 78 88 75 13 66 2 37 20 49 19 84 46\r\n", "output": "6\r\n41 1 76 43 34 25 4 59 50 7 55 80 74 77 2 94 37 95 10 45 67 3 27 22 88 90 13 92 61 28 66 93 69 56 71 42 85 40 35 51 82 39\r\n45 5 84 99 33 14 15 24 26 6 53 79 16 12 17 46 100 18 48 64 20 96 83 29 60 21 36 65 32 44 49 97 68 19 98 70 58 73 9 87 62 57 31 63 54 81\r\n8 8 75 91 78 89 52 86 11\r\n2 23 41\r\n2 30 47\r\n2 38 72\r\n"}]
false
stdio
import sys from collections import defaultdict def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) sorted_a = sorted(a) pos_in_sorted = {v: i+1 for i, v in enumerate(sorted_a)} # 1-based P = {i+1: pos_in_sorted[v] for i, v in enumerate(a)} # 1-based indices visited = set() cycles = [] for node in range(1, n+1): if node not in visited: current = node cycle = [] while current not in visited: visited.add(current) cycle.append(current) current = P[current] cycles.append(cycle) correct_k = len(cycles) computed_cycles = [set(cycle) for cycle in cycles] with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] if not lines: print(0) return try: k = int(lines[0]) if k != correct_k: print(0) return submission_cycles = [] all_indices = set() for line in lines[1:1 + k]: parts = list(map(int, line.split())) if not parts: print(0) return cnt = parts[0] if cnt <= 0 or cnt > n: print(0) return indices = parts[1:1 + cnt] if len(indices) != cnt: print(0) return for idx in indices: if not (1 <= idx <= n): print(0) return s_indices = set(indices) if len(s_indices) != cnt: print(0) return if s_indices & all_indices: print(0) return all_indices.update(s_indices) submission_cycles.append(s_indices) if all_indices != set(range(1, n+1)): print(0) return except: print(0) return for s_cycle in submission_cycles: start = next(iter(s_cycle)) current = start collected = set() while True: collected.add(current) current = P[current] if current == start: break if collected != s_cycle: print(0) return print(100) if __name__ == "__main__": main()
true
843/A
843
A
PyPy 3
TESTS
5
264
27,340,800
130410009
def maps(): return [int(i) for i in input().split()] import pprint import logging from logging import getLogger logging.basicConfig( format="%(message)s", level=logging.WARNING, ) logger = getLogger(__name__) logger.setLevel(logging.INFO) def debug(msg, *args): logger.info(f'{msg}={pprint.pformat(args)}') n, = maps() a = [*maps()] A = sorted(a) debug("A", A) d = {A[i]: i for i in range(n)} ans = [] check = [False] * n for i in range(n): if a[i] == A[i]: ans.append((1, [i + 1])) check[i] = True mx = 0 prev = - 1 for i in range(n): mx = max(mx, d[a[i]]) if mx == i: le = 0 j = i p = [] while j >= 0 and j > prev: if not check[j]: check[j] = True p.append(j + 1) j -= 1 # prev = i mx = 0 if p: ans.append((len(p), p[::-1])) print(len(ans)) for i in ans: print(i[0], *i[1])
71
343
31,641,600
167156985
import sys input = sys.stdin.readline n = int(input()) w = [i for i, j in sorted(enumerate(map(int, input().split())), key=lambda x:x[1])] d = [0]*n x = [] for i in range(n): if d[i] == 0: e = set() while d[i] == 0: e.add(w[i]+1) d[i], i = 1, w[i] x.append((len(e), sorted(e))) print(len(x)) for i in x: print(i[0], ' '.join(map(str, i[1])))
AIM Tech Round 4 (Div. 1)
CF
2,017
1
256
Sorting by Subsequences
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order. Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places. Every element of the sequence must appear in exactly one subsequence.
The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence. The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements. In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence. Indices could be printed in any order. Every index from 1 to n must appear in output exactly once. If there are several possible answers, print any of them.
null
In the first sample output: After sorting the first subsequence we will get sequence 1 2 3 6 5 4. Sorting the second subsequence changes nothing. After sorting the third subsequence we will get sequence 1 2 3 4 5 6. Sorting the last subsequence changes nothing.
[{"input": "6\n3 2 1 6 5 4", "output": "4\n2 1 3\n1 2\n2 4 6\n1 5"}, {"input": "6\n83 -75 -49 11 37 62", "output": "1\n6 1 2 3 4 5 6"}]
1,400
["dfs and similar", "dsu", "implementation", "math", "sortings"]
71
[{"input": "6\r\n3 2 1 6 5 4\r\n", "output": "4\r\n2 1 3\r\n1 2\r\n2 4 6\r\n1 5\r\n"}, {"input": "6\r\n83 -75 -49 11 37 62\r\n", "output": "1\r\n6 1 2 3 4 5 6\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1 1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 1\r\n1 2\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1\r\n2 1 2\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "3\r\n1 1\r\n1 2\r\n1 3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n2 1 3\r\n1 2\r\n"}, {"input": "3\r\n3 1 2\r\n", "output": "1\r\n3 1 2 3\r\n"}, {"input": "10\r\n3 7 10 1 9 5 4 8 6 2\r\n", "output": "3\r\n6 1 4 7 2 10 3\r\n3 5 6 9\r\n1 8\r\n"}, {"input": "20\r\n363756450 -204491568 95834122 -840249197 -49687658 470958158 -445130206 189801569 802780784 -790013317 -192321079 586260100 -751917965 -354684803 418379342 -253230108 193944314 712662868 853829789 735867677\r\n", "output": "3\r\n7 1 4 7 2 10 3 13\r\n11 5 14 15 6 16 12 17 18 20 19 9\r\n2 8 11\r\n"}, {"input": "50\r\n39 7 45 25 31 26 50 11 19 37 8 16 22 33 14 6 12 46 49 48 29 27 41 15 34 24 3 13 20 47 9 36 5 43 40 21 2 38 35 42 23 28 1 32 10 17 30 18 44 4\r\n", "output": "6\r\n20 1 43 34 25 4 50 7 2 37 10 45 3 27 22 13 28 42 40 35 39\r\n23 5 33 14 15 24 26 6 16 12 17 46 18 48 20 29 21 36 32 44 49 19 9 31\r\n2 8 11\r\n2 23 41\r\n2 30 47\r\n1 38\r\n"}, {"input": "100\r\n39 77 67 25 81 26 50 11 73 95 86 16 90 33 14 79 12 100 68 64 60 27 41 15 34 24 3 61 83 47 57 65 99 43 40 21 94 72 82 85 23 71 76 32 10 17 30 18 44 59 35 89 6 63 7 69 62 70 4 29 92 87 31 48 36 28 45 97 93 98 56 38 58 80 8 1 74 91 53 55 54 51 96 5 42 52 9 22 78 88 75 13 66 2 37 20 49 19 84 46\r\n", "output": "6\r\n41 1 76 43 34 25 4 59 50 7 55 80 74 77 2 94 37 95 10 45 67 3 27 22 88 90 13 92 61 28 66 93 69 56 71 42 85 40 35 51 82 39\r\n45 5 84 99 33 14 15 24 26 6 53 79 16 12 17 46 100 18 48 64 20 96 83 29 60 21 36 65 32 44 49 97 68 19 98 70 58 73 9 87 62 57 31 63 54 81\r\n8 8 75 91 78 89 52 86 11\r\n2 23 41\r\n2 30 47\r\n2 38 72\r\n"}]
false
stdio
import sys from collections import defaultdict def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) sorted_a = sorted(a) pos_in_sorted = {v: i+1 for i, v in enumerate(sorted_a)} # 1-based P = {i+1: pos_in_sorted[v] for i, v in enumerate(a)} # 1-based indices visited = set() cycles = [] for node in range(1, n+1): if node not in visited: current = node cycle = [] while current not in visited: visited.add(current) cycle.append(current) current = P[current] cycles.append(cycle) correct_k = len(cycles) computed_cycles = [set(cycle) for cycle in cycles] with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] if not lines: print(0) return try: k = int(lines[0]) if k != correct_k: print(0) return submission_cycles = [] all_indices = set() for line in lines[1:1 + k]: parts = list(map(int, line.split())) if not parts: print(0) return cnt = parts[0] if cnt <= 0 or cnt > n: print(0) return indices = parts[1:1 + cnt] if len(indices) != cnt: print(0) return for idx in indices: if not (1 <= idx <= n): print(0) return s_indices = set(indices) if len(s_indices) != cnt: print(0) return if s_indices & all_indices: print(0) return all_indices.update(s_indices) submission_cycles.append(s_indices) if all_indices != set(range(1, n+1)): print(0) return except: print(0) return for s_cycle in submission_cycles: start = next(iter(s_cycle)) current = start collected = set() while True: collected.add(current) current = P[current] if current == start: break if collected != s_cycle: print(0) return print(100) if __name__ == "__main__": main()
true
355/B
355
B
Python 3
TESTS
3
93
0
54980092
c1,c2,c3,c4 = map(int ,input().split()) n,m = map(int, input().split()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ans = c4 ans1 = c3 ans2 = c3 s1 = sum(a) s2 = sum(b) for i in range(n): ans1 = min(c2 + (s1 - a[i])*c1,ans1) for i in range(m): ans2 = min(c2 + (s2 - b[i])*c1,ans2) print(min(ans1 + ans2,ans))
27
46
307,200
4770289
if __name__=='__main__': arr = input().split(' ') c = [] for a in arr: c.append(int(a)) ans = min(c[3],2*c[2]) arr = input().split(' ') n = int(arr[0]) m = int(arr[1]) an = [] am = [] arr = input().split(' ') for a in arr: an.append(int(a)) arr = input().split(' ') for a in arr: am.append(int(a)) ca = 0 for a in an: if a*c[0]<c[1]: ca+=a*c[0] else: ca+=c[1] ca=min(ca,c[2]) cb = 0 for a in am: if a*c[0]<c[1]: cb+=a*c[0] else: cb+=c[1] cb = min(cb,c[2]) ans = min(ans,ca+cb) print(ans)
Codeforces Round 206 (Div. 2)
CF
2,013
1
256
Vasya and Public Transport
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m. Public transport is not free. There are 4 types of tickets: 1. A ticket for one ride on some bus or trolley. It costs c1 burles; 2. A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles; 3. A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles; 4. A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.
The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.
Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.
null
In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles. In the second sample the profitable strategy is to buy one ticket of the fourth type. In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.
[{"input": "1 3 7 19\n2 3\n2 5\n4 4 4", "output": "12"}, {"input": "4 3 2 1\n1 3\n798\n1 2 3", "output": "1"}, {"input": "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42", "output": "16"}]
1,100
["greedy", "implementation"]
27
[{"input": "1 3 7 19\r\n2 3\r\n2 5\r\n4 4 4\r\n", "output": "12\r\n"}, {"input": "4 3 2 1\r\n1 3\r\n798\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "100 100 8 100\r\n3 5\r\n7 94 12\r\n100 1 47 0 42\r\n", "output": "16\r\n"}, {"input": "3 103 945 1000\r\n7 9\r\n34 35 34 35 34 35 34\r\n0 0 0 0 0 0 0 0 0\r\n", "output": "717\r\n"}, {"input": "7 11 597 948\r\n4 1\r\n5 1 0 11\r\n7\r\n", "output": "40\r\n"}, {"input": "7 32 109 645\r\n1 3\r\n0\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "680 871 347 800\r\n10 100\r\n872 156 571 136 703 201 832 213 15 333\r\n465 435 870 95 660 237 694 594 423 405 27 866 325 490 255 989 128 345 278 125 708 210 771 848 961 448 871 190 745 343 532 174 103 999 874 221 252 500 886 129 185 208 137 425 800 34 696 39 198 981 91 50 545 885 194 583 475 415 162 712 116 911 313 488 646 189 429 756 728 30 985 114 823 111 106 447 296 430 307 388 345 458 84 156 169 859 274 934 634 62 12 839 323 831 24 907 703 754 251 938\r\n", "output": "694\r\n"}, {"input": "671 644 748 783\r\n100 10\r\n520 363 816 957 635 753 314 210 763 819 27 970 520 164 195 230 708 587 568 707 343 30 217 227 755 277 773 497 900 589 826 666 115 784 494 467 217 892 658 388 764 812 248 447 876 581 94 915 675 967 508 754 768 79 261 934 603 712 20 199 997 501 465 91 897 257 820 645 217 105 564 8 668 171 168 18 565 840 418 42 808 918 409 617 132 268 13 161 194 628 213 199 545 448 113 410 794 261 211 539\r\n147 3 178 680 701 193 697 666 846 389\r\n", "output": "783\r\n"}, {"input": "2 7 291 972\r\n63 92\r\n7 0 0 6 0 13 0 20 2 8 0 17 7 0 0 0 0 2 2 0 0 8 20 0 0 0 3 0 0 0 4 20 0 0 0 12 0 8 17 9 0 0 0 0 4 0 0 0 17 11 3 0 2 15 0 18 11 19 14 0 0 20 13\r\n0 0 0 3 7 0 0 0 0 8 13 6 15 0 7 0 0 20 0 0 12 0 12 0 15 0 0 1 11 14 0 11 12 0 0 0 0 0 16 16 0 17 20 0 11 0 0 20 14 0 16 0 3 6 12 0 0 0 0 0 15 3 0 9 17 12 20 17 0 0 0 0 15 9 0 14 10 10 1 20 16 17 20 6 6 0 0 16 4 6 0 7\r\n", "output": "494\r\n"}, {"input": "4 43 490 945\r\n63 92\r\n0 0 0 0 0 0 6 5 18 0 6 4 0 17 0 19 0 19 7 16 0 0 0 9 10 13 7 0 10 16 0 0 0 0 0 14 0 14 9 15 0 0 2 0 0 0 0 5 0 0 0 11 11 0 0 0 0 0 10 12 3 0 0\r\n0 12 0 18 7 7 0 0 9 0 0 13 17 0 18 12 4 0 0 14 18 20 0 0 12 9 17 1 19 0 11 0 5 0 0 14 0 0 16 0 19 15 9 14 7 10 0 19 19 0 0 1 0 0 0 6 0 0 0 6 0 20 1 9 0 0 10 17 5 2 5 4 16 6 0 11 0 8 13 4 0 2 0 0 13 10 0 13 0 0 8 4\r\n", "output": "945\r\n"}, {"input": "2 50 258 922\r\n42 17\r\n0 2 0 1 0 1 0 11 18 9 0 0 0 0 10 15 17 4 20 0 5 0 0 13 13 0 0 2 0 7 0 20 4 0 19 3 7 0 0 0 0 0\r\n8 4 19 0 0 19 14 17 6 0 18 0 0 0 0 9 0\r\n", "output": "486\r\n"}, {"input": "1 1 3 4\r\n2 3\r\n1 1\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "4 4 4 1\r\n1 1\r\n0\r\n0\r\n", "output": "0\r\n"}, {"input": "100 100 1 100\r\n10 10\r\n100 100 100 100 100 100 100 100 100 100\r\n100 100 100 100 100 100 100 100 100 100\r\n", "output": "2\r\n"}]
false
stdio
null
true
616/B
616
B
PyPy 3
TESTS
8
139
21,196,800
86221135
n,m=map(int,input().split()) l=[] for i in range(n): l.append(list(map(int,input().split()))) # print(l) k=[] for j in range(n): k.append([(max(l[j])-min(l[j])),sum(l[j])]) # print(k) b=[k[i] for i in range(len(k))] # print(b) b.sort() # print(b) y1=b[0][0] y2=b[0][1] ind=0 for j in range(len(k)): if(k[j][0]==y1): if(k[j][1]>=y2): ind=j y2=k[j][1] # print(k) print(min(l[ind]))
16
31
0
145526557
n,m=map(int,input().split()) c=m=0 for _ in range(n): l=list(map(int,input().strip().split())) c=min(l) if c>m:m=c print(m)
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
995/B
995
B
Python 3
TESTS
5
109
0
55227287
n=int(input()) l=str(input()) l=[str(g) for g in l if g!=' '] s=0 n=int(2*n) c=0 i=1 a=l[0] f=-1 if(n>4): for k in range(int((n-4)/2)): i=1 a=l[0] c=0 while(c==0): f=l[i] if(f==a): c=1 s=s+i-1 del l[i] del l[0] i=i+1 if(l[0]==l[1]): print(s) else: print(s+1)
22
31
0
228762980
n = int(input()) lineup = [int(x) for x in input().split()] swaps = 0 for i in range(0, 2 * n, 2): if lineup[i] != lineup[i + 1]: j = i + 1 + lineup[i + 1:].index(lineup[i]) lineup[i + 1:j + 1] = [lineup[j]] + lineup[i + 1:j] swaps += j - i - 1 print(swaps)
Codeforces Round 492 (Div. 1) [Thanks, uDebug!]
CF
2,018
2
256
Suit and Tie
Allen is hosting a formal dinner party. $$$2n$$$ people come to the event in $$$n$$$ pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The $$$2n$$$ people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic. Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$), the number of pairs of people. The second line contains $$$2n$$$ integers $$$a_1, a_2, \dots, a_{2n}$$$. For each $$$i$$$ with $$$1 \le i \le n$$$, $$$i$$$ appears exactly twice. If $$$a_j = a_k = i$$$, that means that the $$$j$$$-th and $$$k$$$-th people in the line form a couple.
Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.
null
In the first sample case, we can transform $$$1 1 2 3 3 2 4 4 \rightarrow 1 1 2 3 2 3 4 4 \rightarrow 1 1 2 2 3 3 4 4$$$ in two steps. Note that the sequence $$$1 1 2 3 3 2 4 4 \rightarrow 1 1 3 2 3 2 4 4 \rightarrow 1 1 3 3 2 2 4 4$$$ also works in the same number of steps. The second sample case already satisfies the constraints; therefore we need $$$0$$$ swaps.
[{"input": "4\n1 1 2 3 3 2 4 4", "output": "2"}, {"input": "3\n1 1 2 2 3 3", "output": "0"}, {"input": "3\n3 1 2 3 1 2", "output": "3"}]
1,400
["greedy", "implementation", "math"]
22
[{"input": "4\r\n1 1 2 3 3 2 4 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 1 2 2 3 3\r\n", "output": "0\r\n"}, {"input": "3\r\n3 1 2 3 1 2\r\n", "output": "3\r\n"}, {"input": "8\r\n7 6 2 1 4 3 3 7 2 6 5 1 8 5 8 4\r\n", "output": "27\r\n"}, {"input": "2\r\n1 2 1 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3 3 1 2\r\n", "output": "5\r\n"}, {"input": "38\r\n26 28 23 34 33 14 38 15 35 36 30 1 19 17 18 28 22 15 9 27 11 16 17 32 7 21 6 8 32 26 33 23 18 4 2 25 29 3 35 8 38 37 31 37 12 25 3 27 16 24 5 20 12 13 29 11 30 22 9 19 2 24 7 10 34 4 36 21 14 31 13 6 20 10 5 1\r\n", "output": "744\r\n"}, {"input": "24\r\n21 21 22 5 8 5 15 11 13 16 17 9 3 18 15 1 12 12 7 2 22 19 20 19 23 14 8 24 4 23 16 17 9 10 1 6 4 2 7 3 18 11 24 10 13 6 20 14\r\n", "output": "259\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "19\r\n15 19 18 8 12 2 11 7 5 2 1 1 9 9 3 3 16 6 15 17 13 18 4 14 5 8 10 12 6 11 17 13 14 16 19 7 4 10\r\n", "output": "181\r\n"}, {"input": "8\r\n3 1 5 2 1 6 3 5 6 2 4 8 8 4 7 7\r\n", "output": "13\r\n"}, {"input": "2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "81\r\n48 22 31 24 73 77 79 75 37 78 43 56 20 33 70 34 6 50 51 21 39 29 20 11 73 53 39 61 28 17 55 52 28 57 52 74 35 13 55 2 57 9 46 81 60 47 21 68 1 53 31 64 42 9 79 80 69 30 32 24 15 2 69 10 22 3 71 19 67 66 17 50 62 36 32 65 58 18 25 59 38 10 14 51 23 16 29 81 45 40 18 54 47 12 45 74 41 34 75 44 19 77 71 67 7 16 35 49 15 3 38 4 7 25 76 66 5 65 27 6 1 72 37 42 26 60 12 64 44 41 80 13 49 68 76 48 11 78 40 61 30 43 62 58 5 4 33 26 54 27 36 72 63 63 59 70 23 8 56 8 46 14\r\n", "output": "3186\r\n"}, {"input": "84\r\n10 29 12 22 55 3 81 33 64 78 46 44 69 41 34 71 24 12 22 54 63 9 65 40 36 81 32 37 83 50 28 84 53 25 72 77 41 35 50 8 29 78 72 53 21 63 16 1 79 20 66 23 38 18 44 5 27 77 32 52 42 60 67 62 64 52 14 80 4 19 15 45 40 47 42 46 68 18 70 8 3 36 65 38 73 43 59 20 66 6 51 10 58 55 51 13 4 5 43 82 71 21 9 33 47 11 61 30 76 27 24 48 75 15 48 75 2 31 83 67 59 74 56 11 39 13 45 76 26 30 39 17 61 57 68 7 70 62 49 57 49 84 31 26 56 54 74 16 60 1 80 35 82 28 79 73 14 69 6 19 25 34 23 2 58 37 7 17\r\n", "output": "3279\r\n"}, {"input": "4\r\n3 4 2 4 1 2 1 3\r\n", "output": "8\r\n"}, {"input": "75\r\n28 28 42 3 39 39 73 73 75 75 30 30 21 9 57 41 26 70 15 15 65 65 24 24 4 4 62 62 17 17 29 29 37 37 18 18 1 1 8 8 63 63 49 49 5 5 59 59 19 19 34 34 48 48 10 10 14 42 22 22 38 38 50 50 60 60 64 35 47 31 72 72 41 52 46 46 20 20 21 9 7 7 36 36 2 2 6 6 70 26 69 69 16 16 61 61 66 66 33 33 44 44 11 11 23 23 40 40 12 12 64 35 56 56 27 27 53 53 3 14 43 43 31 47 68 68 13 13 74 74 67 67 71 71 45 45 57 52 32 32 25 25 58 58 55 55 51 51 54 54\r\n", "output": "870\r\n"}, {"input": "35\r\n6 32 4 19 9 34 20 29 22 26 19 14 33 11 17 31 30 13 7 12 8 16 5 5 21 15 18 28 34 3 2 10 23 24 35 6 32 4 25 9 1 11 24 20 26 25 2 13 22 17 31 30 33 7 12 8 16 27 27 21 15 18 28 1 3 14 10 23 29 35\r\n", "output": "673\r\n"}, {"input": "86\r\n33 6 22 8 54 43 57 85 70 41 20 17 35 12 66 25 45 78 67 55 50 19 31 75 77 29 58 78 34 15 40 48 14 82 6 37 44 53 62 23 56 22 34 18 71 83 21 80 47 38 3 42 60 9 73 49 84 7 76 30 5 4 11 28 69 16 26 10 59 48 64 46 32 68 24 63 79 36 13 1 27 61 39 74 2 51 51 2 74 39 61 27 1 13 36 79 86 24 68 32 46 64 63 59 10 26 16 69 28 11 4 5 30 76 7 84 49 73 9 60 42 3 38 47 80 21 83 72 18 52 65 56 23 62 53 44 37 81 82 14 86 40 15 52 72 58 29 77 85 31 19 50 55 67 71 45 25 66 12 35 17 20 41 70 75 57 43 54 8 65 81 33\r\n", "output": "6194\r\n"}]
false
stdio
null
true
995/B
995
B
PyPy 3-64
TESTS
3
62
0
226139082
from collections import defaultdict def main(): n = int(input()) data = list(map(int, input().split())) cache = defaultdict(list) for i, x in enumerate(data): cache[x].append(i) erase = set() for key, value in cache.items(): if value[1] - value[0] == 1: erase.add(key) for key in erase: del cache[key] result = 0 while cache: min_dist = float('inf') best_x = 0 for key, value in cache.items(): d = value[1] - value[0] if d < min_dist: min_dist = d best_x = key (data[cache[best_x][1]], data[cache[best_x][1] - 1]) = (data[cache[best_x][1] - 1], data[cache[best_x][1]]) neighbor = cache[best_x][1] neighbor_x = data[neighbor] if neighbor_x in cache: if cache[neighbor_x][0] == neighbor - 1: cache[neighbor_x][0] += 1 else: cache[neighbor_x][1] += 1 if cache[neighbor_x][1] - cache[neighbor_x][0] == 1: del cache[neighbor_x] cache[best_x][1] -= 1 result += 1 if cache[best_x][1] - cache[best_x][0] == 1: del cache[best_x] print(result) main()
22
46
0
164408505
input() a=input().split() cnt=0 while len(a)>0: k=a[1:].index(a[0])+1 cnt+=k-1 a=a[1:k]+a[k+1:] print(cnt)
Codeforces Round 492 (Div. 1) [Thanks, uDebug!]
CF
2,018
2
256
Suit and Tie
Allen is hosting a formal dinner party. $$$2n$$$ people come to the event in $$$n$$$ pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The $$$2n$$$ people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic. Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$), the number of pairs of people. The second line contains $$$2n$$$ integers $$$a_1, a_2, \dots, a_{2n}$$$. For each $$$i$$$ with $$$1 \le i \le n$$$, $$$i$$$ appears exactly twice. If $$$a_j = a_k = i$$$, that means that the $$$j$$$-th and $$$k$$$-th people in the line form a couple.
Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.
null
In the first sample case, we can transform $$$1 1 2 3 3 2 4 4 \rightarrow 1 1 2 3 2 3 4 4 \rightarrow 1 1 2 2 3 3 4 4$$$ in two steps. Note that the sequence $$$1 1 2 3 3 2 4 4 \rightarrow 1 1 3 2 3 2 4 4 \rightarrow 1 1 3 3 2 2 4 4$$$ also works in the same number of steps. The second sample case already satisfies the constraints; therefore we need $$$0$$$ swaps.
[{"input": "4\n1 1 2 3 3 2 4 4", "output": "2"}, {"input": "3\n1 1 2 2 3 3", "output": "0"}, {"input": "3\n3 1 2 3 1 2", "output": "3"}]
1,400
["greedy", "implementation", "math"]
22
[{"input": "4\r\n1 1 2 3 3 2 4 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 1 2 2 3 3\r\n", "output": "0\r\n"}, {"input": "3\r\n3 1 2 3 1 2\r\n", "output": "3\r\n"}, {"input": "8\r\n7 6 2 1 4 3 3 7 2 6 5 1 8 5 8 4\r\n", "output": "27\r\n"}, {"input": "2\r\n1 2 1 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3 3 1 2\r\n", "output": "5\r\n"}, {"input": "38\r\n26 28 23 34 33 14 38 15 35 36 30 1 19 17 18 28 22 15 9 27 11 16 17 32 7 21 6 8 32 26 33 23 18 4 2 25 29 3 35 8 38 37 31 37 12 25 3 27 16 24 5 20 12 13 29 11 30 22 9 19 2 24 7 10 34 4 36 21 14 31 13 6 20 10 5 1\r\n", "output": "744\r\n"}, {"input": "24\r\n21 21 22 5 8 5 15 11 13 16 17 9 3 18 15 1 12 12 7 2 22 19 20 19 23 14 8 24 4 23 16 17 9 10 1 6 4 2 7 3 18 11 24 10 13 6 20 14\r\n", "output": "259\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "19\r\n15 19 18 8 12 2 11 7 5 2 1 1 9 9 3 3 16 6 15 17 13 18 4 14 5 8 10 12 6 11 17 13 14 16 19 7 4 10\r\n", "output": "181\r\n"}, {"input": "8\r\n3 1 5 2 1 6 3 5 6 2 4 8 8 4 7 7\r\n", "output": "13\r\n"}, {"input": "2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "81\r\n48 22 31 24 73 77 79 75 37 78 43 56 20 33 70 34 6 50 51 21 39 29 20 11 73 53 39 61 28 17 55 52 28 57 52 74 35 13 55 2 57 9 46 81 60 47 21 68 1 53 31 64 42 9 79 80 69 30 32 24 15 2 69 10 22 3 71 19 67 66 17 50 62 36 32 65 58 18 25 59 38 10 14 51 23 16 29 81 45 40 18 54 47 12 45 74 41 34 75 44 19 77 71 67 7 16 35 49 15 3 38 4 7 25 76 66 5 65 27 6 1 72 37 42 26 60 12 64 44 41 80 13 49 68 76 48 11 78 40 61 30 43 62 58 5 4 33 26 54 27 36 72 63 63 59 70 23 8 56 8 46 14\r\n", "output": "3186\r\n"}, {"input": "84\r\n10 29 12 22 55 3 81 33 64 78 46 44 69 41 34 71 24 12 22 54 63 9 65 40 36 81 32 37 83 50 28 84 53 25 72 77 41 35 50 8 29 78 72 53 21 63 16 1 79 20 66 23 38 18 44 5 27 77 32 52 42 60 67 62 64 52 14 80 4 19 15 45 40 47 42 46 68 18 70 8 3 36 65 38 73 43 59 20 66 6 51 10 58 55 51 13 4 5 43 82 71 21 9 33 47 11 61 30 76 27 24 48 75 15 48 75 2 31 83 67 59 74 56 11 39 13 45 76 26 30 39 17 61 57 68 7 70 62 49 57 49 84 31 26 56 54 74 16 60 1 80 35 82 28 79 73 14 69 6 19 25 34 23 2 58 37 7 17\r\n", "output": "3279\r\n"}, {"input": "4\r\n3 4 2 4 1 2 1 3\r\n", "output": "8\r\n"}, {"input": "75\r\n28 28 42 3 39 39 73 73 75 75 30 30 21 9 57 41 26 70 15 15 65 65 24 24 4 4 62 62 17 17 29 29 37 37 18 18 1 1 8 8 63 63 49 49 5 5 59 59 19 19 34 34 48 48 10 10 14 42 22 22 38 38 50 50 60 60 64 35 47 31 72 72 41 52 46 46 20 20 21 9 7 7 36 36 2 2 6 6 70 26 69 69 16 16 61 61 66 66 33 33 44 44 11 11 23 23 40 40 12 12 64 35 56 56 27 27 53 53 3 14 43 43 31 47 68 68 13 13 74 74 67 67 71 71 45 45 57 52 32 32 25 25 58 58 55 55 51 51 54 54\r\n", "output": "870\r\n"}, {"input": "35\r\n6 32 4 19 9 34 20 29 22 26 19 14 33 11 17 31 30 13 7 12 8 16 5 5 21 15 18 28 34 3 2 10 23 24 35 6 32 4 25 9 1 11 24 20 26 25 2 13 22 17 31 30 33 7 12 8 16 27 27 21 15 18 28 1 3 14 10 23 29 35\r\n", "output": "673\r\n"}, {"input": "86\r\n33 6 22 8 54 43 57 85 70 41 20 17 35 12 66 25 45 78 67 55 50 19 31 75 77 29 58 78 34 15 40 48 14 82 6 37 44 53 62 23 56 22 34 18 71 83 21 80 47 38 3 42 60 9 73 49 84 7 76 30 5 4 11 28 69 16 26 10 59 48 64 46 32 68 24 63 79 36 13 1 27 61 39 74 2 51 51 2 74 39 61 27 1 13 36 79 86 24 68 32 46 64 63 59 10 26 16 69 28 11 4 5 30 76 7 84 49 73 9 60 42 3 38 47 80 21 83 72 18 52 65 56 23 62 53 44 37 81 82 14 86 40 15 52 72 58 29 77 85 31 19 50 55 67 71 45 25 66 12 35 17 20 41 70 75 57 43 54 8 65 81 33\r\n", "output": "6194\r\n"}]
false
stdio
null
true
904/C
906
A
Python 3
TESTS
6
61
6,041,600
34634979
inp=lambda:map(int,input().split()) n=int(input()) num=0 a=set() b=set() for i in range(26): a.add(chr(i+ord('a'))) b.add(chr(i+ord('a'))) fl=0 cnt=0 for i in range(n): mark,word=input().split() word=set(word) if mark=='!': a=a.intersection(word) if (mark=='!' or mark=='?')and fl==1: cnt+=1 if mark=='.': a=a.difference(word) if mark=='?' and i!=n-1: a.difference(word) if len(a)==1: fl=1 print('0' if cnt==0 else cnt-1)
38
93
3,891,200
210256001
import sys input = lambda: sys.stdin.readline().rstrip() ALPHA = 'abcdefghijklmnopqrstuvwxyz' N = int(input()) ans = 0 find = set([c for c in ALPHA]) for i in range(N): a,b = input().split() if a=='!': if len(find)==1: ans+=1 else: tmp = set() for c in b: if c in find: tmp.add(c) find = tmp elif a=='?': if len(find)==1 and b not in find: ans+=1 elif len(find)>1: for c in b: if c in find: find.remove(c) else: for c in b: if c in find: find.remove(c) #print(a,b,find) print(ans)
Технокубок 2018 - Отборочный Раунд 4
CF
2,017
2
256
Shockers
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly. Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did. The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: 1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said. 2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said. 3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter. All words consist only of lowercase English letters. The total length of all words does not exceed 105. It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
null
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis. In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
1,600
["strings"]
38
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
false
stdio
null
true
223/C
223
C
Python 3
TESTS
8
124
5,529,600
33056467
leng, repeat=list(map(int,input().split())) Lis = list(map(int,input().split())) temp=Lis.copy() for rep in range(repeat): for i in range(len(Lis)-1): temp[i+1]=temp[i]+Lis[i+1] Lis= temp.copy() print(*temp)
33
498
22,732,800
122265367
n, k= map(int, input().split()) a = list(map(int, input().split())) def find_cf(k,i,a,mod): cf=[1] ans=0 for j in range(1,i): aux=(i-j)*cf[-1]*pow(k+i-j-1,mod-2,mod)%mod cf.append(aux) ans+=aux*a[j-1] ans+=a[i-1] return ans def Combinations(n,r,mod): num = den = 1 for i in range(r): num = (num * (n - i)) % mod den = (den * (i + 1)) % mod return (num * pow(den,mod - 2, mod)) % mod def partial_Sums(n, k, a): if(k==0): return a result=[a[0]] mod= 10**9 + 7 cf=[1] for i in range(1,n+1): cf.append((cf[-1]*(k+i-1)*pow(i,mod-2,mod))%mod) for i in range(2,n+1): x = 0 for j in range(i,0,-1): x = (x+a[j-1]*cf[i-j])%mod result.append(x) return result answer=partial_Sums(n,k,a) print(' '.join(map(str, answer))) # 3 1 # 1 2 3
Codeforces Round 138 (Div. 1)
CF
2,012
4
256
Partial Sums
You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: 1. First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 ≤ i ≤ n) of array s equals $$s_i = \left( \sum_{j=1}^{i} a_j \right) \mod (10^9 + 7)$$. The operation x mod y means that we take the remainder of the division of number x by number y. 2. Then we write the contents of the array s to the array a. Element number i (1 ≤ i ≤ n) of the array s becomes the i-th element of the array a (ai = si). You task is to find array a after exactly k described operations are applied.
The first line contains two space-separated integers n and k (1 ≤ n ≤ 2000, 0 ≤ k ≤ 109). The next line contains n space-separated integers a1, a2, ..., an — elements of the array a (0 ≤ ai ≤ 109).
Print n integers  — elements of the array a after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array a. Separate the printed numbers by spaces.
null
null
[{"input": "3 1\n1 2 3", "output": "1 3 6"}, {"input": "5 0\n3 14 15 92 6", "output": "3 14 15 92 6"}]
1,900
["combinatorics", "math", "number theory"]
33
[{"input": "3 1\r\n1 2 3\r\n", "output": "1 3 6\r\n"}, {"input": "5 0\r\n3 14 15 92 6\r\n", "output": "3 14 15 92 6\r\n"}, {"input": "1 1\r\n3\r\n", "output": "3\r\n"}, {"input": "1 0\r\n0\r\n", "output": "0\r\n"}, {"input": "1 0\r\n123\r\n", "output": "123\r\n"}, {"input": "1 1\r\n0\r\n", "output": "0\r\n"}, {"input": "4 1\r\n3 20 3 4\r\n", "output": "3 23 26 30\r\n"}, {"input": "5 20\r\n11 5 6 8 11\r\n", "output": "11 225 2416 18118 106536\r\n"}, {"input": "17 239\r\n663 360 509 307 311 501 523 370 302 601 541 42 328 200 196 110 573\r\n", "output": "663 158817 19101389 537972231 259388293 744981080 6646898 234671418 400532510 776716020 52125061 263719534 192023697 446278138 592149678 33061993 189288187\r\n"}, {"input": "13 666\r\n84 89 29 103 128 233 190 122 117 208 119 97 200\r\n", "output": "84 56033 18716627 174151412 225555860 164145872 451267967 434721493 224270207 253181081 361500071 991507723 152400567\r\n"}, {"input": "42 42\r\n42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42\r\n", "output": "42 1806 39732 595980 6853770 64425438 515403504 607824507 548903146 777117811 441012592 397606113 289227498 685193257 740773014 214937435 654148201 446749626 489165413 202057369 926377846 779133524 993842970 721730118 484757814 939150939 225471671 20649822 51624555 850529088 441269800 845570818 580382507 773596603 435098280 957216216 73968454 779554271 588535300 530034849 736571438 149644609\n"}, {"input": "10 1000000\r\n1 2 3 4 84 5 6 7 8 9\r\n", "output": "1 1000002 2496503 504322849 591771075 387496712 683276420 249833545 23968189 474356595\r\n"}]
false
stdio
null
true
612/E
612
E
PyPy 3
TESTS
8
139
0
84070842
import math import sys input = sys.stdin.readline n = int(input()) a = [int(_) - 1 for _ in input().split()] vis = [False] * n cycles = [[] for _ in range(n + 1)] for i in range(n): if vis[i]: continue cur = i cycle = [] while not vis[cur]: vis[cur] = True cycle.append(cur) cur = a[cur] cycles[len(cycle)].append(cycle) p = [0] * n for i in range(n + 1): if i % 2 == 1: for j in cycles[i]: for k in range(i): p[j[(k + 2) % i]] = j[k] else: if len(cycles[i]) % 2 == 1: print(-1) exit(0) for j in range(0, len(cycles[i]), 2): for k in range(i): p[cycles[i][j][k]] = cycles[i][j + 1][k] p[cycles[i][j + 1][k]] = cycles[i][j][(k + 1) % i] print(' '.join(map(lambda i : str(i + 1), p)))
18
639
149,299,200
175368906
n=int(input()) a=list(map(lambda x:int(x)-1,input().split())) ans=[-1]*n def calc1(a): m=len(a) b=[0]*m for i in range(m): b[(2*i)%m]=a[i] for i in range(m): ans[b[i]]=b[(i+1)%m] def calc2(a,b): m=len(a) c=[0]*2*m for i in range(m): c[2*i]=a[i] c[2*i+1]=b[i] for i in range(2*m): ans[c[i]]=c[(i+1)%(2*m)] memo=[[] for i in range(n+1)] seen=[0]*n for i in range(n): if seen[i]: continue loop=[i] seen[i]=1 now=a[i] while now!=i: loop.append(now) seen[now]=1 now=a[now] if len(loop)%2==1: calc1(loop) else: m=len(loop) if memo[m]: calc2(memo[m],loop) memo[m]=[] else: memo[m]=loop if min(ans)!=-1: print(*[i+1 for i in ans]) else: print(-1)
Educational Codeforces Round 4
ICPC
2,015
2
256
Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1]. This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
The first line contains integer n (1 ≤ n ≤ 106) — the number of elements in permutation p. The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of permutation p.
If there is no permutation q such that q2 = p print the number "-1". If the answer exists print it. The only line should contain n different integers qi (1 ≤ qi ≤ n) — the elements of the permutation q. If there are several solutions print any of them.
null
null
[{"input": "4\n2 1 4 3", "output": "3 4 2 1"}, {"input": "4\n2 1 3 4", "output": "-1"}, {"input": "5\n2 3 4 5 1", "output": "4 5 1 2 3"}]
2,200
["combinatorics", "constructive algorithms", "dfs and similar", "graphs", "math"]
18
[{"input": "4\r\n2 1 4 3\r\n", "output": "3 4 2 1\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "-1\r\n"}, {"input": "5\r\n2 3 4 5 1\r\n", "output": "4 5 1 2 3\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "-1\r\n"}, {"input": "10\r\n3 5 1 2 10 8 7 6 4 9\r\n", "output": "6 9 8 10 4 3 7 1 5 2\r\n"}, {"input": "100\r\n11 9 35 34 51 74 16 67 26 21 14 80 84 79 7 61 28 3 53 43 42 5 56 36 69 30 22 88 1 27 65 91 46 31 59 50 17 96 25 18 64 55 78 2 63 24 95 48 93 13 38 76 89 94 15 90 45 81 52 87 83 73 44 49 23 82 85 75 86 33 47 19 58 97 37 20 40 10 92 4 6 68 77 54 71 12 62 60 100 39 41 99 72 29 57 8 70 32 66 98\r\n", "output": "-1\r\n"}, {"input": "100\r\n94 22 24 99 58 97 20 29 67 30 38 64 77 50 15 44 92 88 39 42 25 70 2 76 84 6 37 49 17 71 31 19 26 79 10 35 65 63 32 95 5 8 52 27 83 18 53 93 13 81 48 68 54 82 34 60 87 23 16 86 55 40 61 45 28 7 74 41 14 91 3 72 33 11 98 89 90 69 78 36 80 59 56 21 43 1 75 46 47 12 96 73 57 51 4 85 9 100 66 62\r\n", "output": "78 52 95 76 96 49 53 59 77 100 64 11 9 48 15 17 44 46 32 54 84 68 43 4 21 28 73 6 16 62 31 39 65 86 98 75 33 45 19 3 91 82 2 92 63 88 7 50 97 93 14 22 20 42 60 55 80 85 29 34 56 71 83 38 26 47 90 70 51 41 40 72 37 12 35 99 67 94 1 87 57 8 61 25 23 79 36 18 66 74 5 27 81 69 24 58 13 10 89 30\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) p = list(map(int, f.readline().split())) with open(sub_path, 'r') as f: lines = f.readlines() sub_output = [] for line in lines: line = line.strip() if line == '-1': sub_output = [-1] break sub_output.extend(list(map(int, line.split()))) if len(sub_output) == 1 and sub_output[0] == -1: visited = [False] * (n + 1) cycle_counts = {} for i in range(1, n + 1): if not visited[i]: current = i cycle = [] while not visited[current]: visited[current] = True cycle.append(current) current = p[current - 1] cycle_length = len(cycle) if cycle_length % 2 == 0: cycle_counts[cycle_length] = cycle_counts.get(cycle_length, 0) + 1 valid = True for length, count in cycle_counts.items(): if count % 2 != 0: valid = False break print(1 if not valid else 0) else: if len(sub_output) != n: print(0) return seen = [False] * (n + 1) valid_perm = True for num in sub_output: if not (1 <= num <= n) or seen[num]: valid_perm = False break seen[num] = True if not valid_perm: print(0) return q = sub_output q_squared = [] for i in range(n): first = q[i] second = q[first - 1] q_squared.append(second) print(1 if q_squared == p else 0) if __name__ == "__main__": main()
true
755/B
755
B
PyPy 3-64
TESTS
13
93
5,734,400
194717988
n, m = map(int, input().split(" ")) n_and_m = n + m all = set() polland = [] enemy = [] for i in range(n_and_m): s = input() if((i+1) <= n): polland.append(s) else: enemy.append(s) if(m > n): print("NO") elif(n > m): print("YES") else: p = n e = m ant = 0 for j in range(m): all.add(polland[j]) if(len(all) == ant): p -= 1 ant = len(all) all.add(enemy[j]) if(len(all) == ant): e -= 1 ant = len(all) if(p > e): print('YES') else: print('NO')
33
46
1,228,800
165351828
def resolve(nArray, mArray): if n>m: print('YES') elif m>n: print('NO') else: nArray = set(nArray) mArray = set(mArray) mArray = set(mArray.intersection(nArray)) if len(mArray)%2==0: print("NO") else: print("YES") n, m =[int(a) for a in input().split()] nArray = [] mArray = [] for i in range(n): nArray.append(input()) for i in range(m): mArray.append(input()) resolve(nArray, mArray)
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
223/C
223
C
PyPy 3
TESTS
8
186
0
112989760
def ncr(n, r, p): # initialize numerator # and denominator num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p p=10**9+7 n,k=map(int,input().split()) b=list(map(int,input().split())) if k==0: print(*b) else: k-=1 res=[] for r in range(1,n+1): res.append(ncr(r+k-1,r-1,p)) ans=[] for i in range(n): j=i val=0 while(j>=0): val+=res[j]*b[i-j] j+=-1 ans.append(val) print(*ans)
33
498
22,732,800
124584626
n, k= map(int, input().split()) a = list(map(int, input().split())) def partial_Sums(n, k, a): if(k==0): return a #En caso de que no se haga ningún paso mod= 10**9 + 7 result=[a[0]] cf=[1] for i in range(1,n+1): cf.append((cf[-1]*(k+i-1)*pow(i,mod-2,mod))%mod) #Se determinan los coeficientes de cada elemento del array en la respuesta final for i in range(2,n+1): s_i = 0 for j in range(i,0,-1): s_i = (s_i+a[j-1]*cf[i-j])%mod result.append(s_i) return result answer=partial_Sums(n,k,a) print(' '.join(map(str, answer)))
Codeforces Round 138 (Div. 1)
CF
2,012
4
256
Partial Sums
You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: 1. First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 ≤ i ≤ n) of array s equals $$s_i = \left( \sum_{j=1}^{i} a_j \right) \mod (10^9 + 7)$$. The operation x mod y means that we take the remainder of the division of number x by number y. 2. Then we write the contents of the array s to the array a. Element number i (1 ≤ i ≤ n) of the array s becomes the i-th element of the array a (ai = si). You task is to find array a after exactly k described operations are applied.
The first line contains two space-separated integers n and k (1 ≤ n ≤ 2000, 0 ≤ k ≤ 109). The next line contains n space-separated integers a1, a2, ..., an — elements of the array a (0 ≤ ai ≤ 109).
Print n integers  — elements of the array a after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array a. Separate the printed numbers by spaces.
null
null
[{"input": "3 1\n1 2 3", "output": "1 3 6"}, {"input": "5 0\n3 14 15 92 6", "output": "3 14 15 92 6"}]
1,900
["combinatorics", "math", "number theory"]
33
[{"input": "3 1\r\n1 2 3\r\n", "output": "1 3 6\r\n"}, {"input": "5 0\r\n3 14 15 92 6\r\n", "output": "3 14 15 92 6\r\n"}, {"input": "1 1\r\n3\r\n", "output": "3\r\n"}, {"input": "1 0\r\n0\r\n", "output": "0\r\n"}, {"input": "1 0\r\n123\r\n", "output": "123\r\n"}, {"input": "1 1\r\n0\r\n", "output": "0\r\n"}, {"input": "4 1\r\n3 20 3 4\r\n", "output": "3 23 26 30\r\n"}, {"input": "5 20\r\n11 5 6 8 11\r\n", "output": "11 225 2416 18118 106536\r\n"}, {"input": "17 239\r\n663 360 509 307 311 501 523 370 302 601 541 42 328 200 196 110 573\r\n", "output": "663 158817 19101389 537972231 259388293 744981080 6646898 234671418 400532510 776716020 52125061 263719534 192023697 446278138 592149678 33061993 189288187\r\n"}, {"input": "13 666\r\n84 89 29 103 128 233 190 122 117 208 119 97 200\r\n", "output": "84 56033 18716627 174151412 225555860 164145872 451267967 434721493 224270207 253181081 361500071 991507723 152400567\r\n"}, {"input": "42 42\r\n42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42\r\n", "output": "42 1806 39732 595980 6853770 64425438 515403504 607824507 548903146 777117811 441012592 397606113 289227498 685193257 740773014 214937435 654148201 446749626 489165413 202057369 926377846 779133524 993842970 721730118 484757814 939150939 225471671 20649822 51624555 850529088 441269800 845570818 580382507 773596603 435098280 957216216 73968454 779554271 588535300 530034849 736571438 149644609\n"}, {"input": "10 1000000\r\n1 2 3 4 84 5 6 7 8 9\r\n", "output": "1 1000002 2496503 504322849 591771075 387496712 683276420 249833545 23968189 474356595\r\n"}]
false
stdio
null
true
159/A
159
A
Python 3
TESTS
16
622
307,200
58365438
pairs, time_limit=map(int,input().split()) groups=[] friends=[] for i in range(pairs): groups.append(input().split()) while len(groups)>1: k=1 while k < len(groups): if groups[0][0]==groups[k][1] and groups[0][1]==groups[k][0]: if int(groups[k][2])-int(groups[0][2])>0 and int(groups[k][2])-int(groups[0][2])<=time_limit: sor=[groups[k][0],groups[k][1]] sor.sort() if not((sor[0]+" "+sor[1]) in friends): friends.append(sor[0]+" "+sor[1]) groups.pop(k) break else: break elif int(groups[0][2])-int(groups[k][2])>time_limit: break else: k=k+1 groups.pop(0) print(len(friends)) for pair in friends: print(pair)
30
124
204,800
204561136
""" https://codeforces.com/problemset/problem/159/A """ n, temps = [int(x) for x in input().split()] d = dict() for _ in range(n): a, b, t = [x for x in input().split()] if (a, b) in d: d[(a, b)].append((int(t), a, b)) elif (b, a) in d: d[(b, a)].append((int(t), a, b)) else: d[(a, b)] = [(int(t), a, b)] result = [] for k, ts in d.items(): trouve = False for i, (t1, a1, a2) in enumerate(ts[:-1]): if trouve: break for (t2, a2, b2) in ts[i + 1 :]: if (0 < t2 - t1 <= temps) and a1 == b2: result.append(k) trouve = True break print(len(result)) for k in result: print(*k)
VK Cup 2012 Qualification Round 2
CF
2,012
3
256
Friends or Not
Polycarpus has a hobby — he develops an unusual social network. His work is almost completed, and there is only one more module to implement — the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≤ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends.
The first line of the input contains two integers n and d (1 ≤ n, d ≤ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≤ i ≤ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≤ ti ≤ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces.
In the first line print integer k — the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.
null
In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
[{"input": "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4", "output": "1\npetya vasya"}, {"input": "1 1000\na b 0", "output": "0"}]
1,400
["*special", "greedy", "implementation"]
30
[{"input": "4 1\r\nvasya petya 1\r\npetya vasya 2\r\nanya ivan 2\r\nivan anya 4\r\n", "output": "1\r\npetya vasya\r\n"}, {"input": "1 1000\r\na b 0\r\n", "output": "0\r\n"}, {"input": "2 1\r\na b 0\r\nb a 0\r\n", "output": "0\r\n"}, {"input": "3 1\r\na b 1\r\nb c 2\r\nc d 3\r\n", "output": "0\r\n"}, {"input": "10 2\r\nrvmykneiddpqyf jdhmt 0\r\nwcsjvh jdhmt 0\r\njdhmt rvmykneiddpqyf 1\r\nrvmykneiddpqyf jdhmt 1\r\nwcsjvh rvmykneiddpqyf 2\r\nrvmykneiddpqyf jdhmt 2\r\njdhmt rvmykneiddpqyf 3\r\njdhmt wcsjvh 5\r\njdhmt wcsjvh 5\r\nrvmykneiddpqyf jdhmt 6\r\n", "output": "1\r\njdhmt rvmykneiddpqyf\r\n"}, {"input": "10 2\r\nliazxawm spxwktiqjgs 0\r\nnolq liazxawm 1\r\nliazxawm nolq 2\r\nliazxawm spxwktiqjgs 2\r\nnolq liazxawm 3\r\nspxwktiqjgs liazxawm 3\r\nspxwktiqjgs liazxawm 3\r\nspxwktiqjgs liazxawm 3\r\nspxwktiqjgs nolq 3\r\nnolq spxwktiqjgs 4\r\n", "output": "3\r\nliazxawm nolq\r\nliazxawm spxwktiqjgs\r\nnolq spxwktiqjgs\r\n"}, {"input": "10 2\r\nfxn ipntr 0\r\nipntr fxn 1\r\nfxn ipntr 1\r\npfvpfteadph ipntr 2\r\nfxn pfvpfteadph 4\r\nipntr fxn 4\r\npfvpfteadph fxn 5\r\nfxn pfvpfteadph 5\r\npfvpfteadph ipntr 6\r\nipntr pfvpfteadph 6\r\n", "output": "2\r\nfxn ipntr\r\nfxn pfvpfteadph\r\n"}, {"input": "3 1\r\na b 1\r\na b 2\r\nb a 2\r\n", "output": "1\r\na b\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: lines = [line.strip() for line in f] n, d = map(int, lines[0].split()) messages = [] for line in lines[1:n+1]: parts = line.split() Ai, Bi, ti = parts[0], parts[1], int(parts[2]) messages.append( (Ai, Bi, ti) ) s = set() for i in range(n): X, Y, t1 = messages[i] for j in range(i+1, n): t2 = messages[j][2] if t2 > t1 + d: break if t2 <= t1: continue if messages[j][0] == Y and messages[j][1] == X: pair = tuple(sorted( (X, Y) )) s.add(pair) try: with open(submission_path, 'r') as f: submission_lines = [line.strip() for line in f] except: print(0) return if not submission_lines: print(0) return if not submission_lines[0].isdigit(): print(0) return k_sub = int(submission_lines[0]) if len(submission_lines) != k_sub +1: print(0) return submission_pairs = submission_lines[1:] sub_set = set() for line in submission_pairs: parts = line.split() if len(parts) != 2: print(0) return a, b = parts if a == b: print(0) return sub_pair = tuple(sorted( (a, b) )) sub_set.add(sub_pair) if sub_set == s and len(sub_set) == k_sub: print(1) else: print(0) if __name__ == "__main__": main()
true
925/B
925
B
PyPy 3-64
TESTS
4
46
0
223632490
''' ╔═══╗╔═══╗╔═╗ ╔╗╔══╗╔╗ ╔╗ ╔═══╗╔═══╗╔╗ ╔╗ ╚╗╔╗║║╔═╗║║ ╚╗║║╚╣╔╝║║ ╔╝║ ║╔═╗║║╔═╗║║║ ║║ ║║║║║║ ║║║╔╗╚╝║ ║║ ║║ ╚╗║ ╚╝╔╝║╚╝╔╝║║╚═╝║ ║║║║║╚═╝║║║╚╗ ║ ║║ ║║ ╔╗ ║║ ╔═╝╔╝╔╗╚╗║╚══╗║ ╔╝╚╝║║╔═╗║║║ ║ ║╔╣╚╗║╚═╝║╔╝╚╗║ ╚═╗║╚═╝║ ║║ ╚═══╝╚╝ ╚╝╚╝ ╚═╝╚══╝╚═══╝╚══╝╚═══╝╚═══╝ ╚╝ ''' from itertools import permutations, product from math import gcd, comb import math from operator import index import os import sys from io import BytesIO, IOBase MOD = 10**9+7 def main(): n, x1, x2 = [int(i) for i in input().split()] x1, x2 = min(x1, x2), max(x1, x2) A = sorted([[int(j), i+1] for (i, j) in enumerate(input().split())]) for i in range(n): k1 = math.ceil(x1/A[i][0]) j = i + k1 if j < n: k2 = math.ceil(x2/A[j][0]) if k2<=n-j: t1 = [A[k][1] for k in range(i, j)] t2 = [A[k][1] for k in range(j, n)] print("Yes") print(len(t1), len(t2)) print(*t1) print(*t2) return for i in range(n): k1 = math.ceil(x2/A[i][0]) j = i + k1 if j < n: k2 = math.ceil(x1/A[j][0]) if k2<=n-j: t2 = [A[k][1] for k in range(i, j)] t1 = [A[k][1] for k in range(j, n)] print("Yes") print(len(t1), len(t2)) print(*t1) print(*t2) return print("No") if __name__ == '__main__': main()
40
1,309
64,921,600
203194524
import sys from collections import namedtuple if not __debug__: sys.stdin = open("hack.txt") input = sys.stdin.readline Server = namedtuple("Server", ["index", "value"]) def solve(n, x1, x2, servers): C = [Server(i+1, v) for i, v in enumerate(servers)] C.sort(key=lambda x: x.value, reverse=True) flag = False for i in range(n-1): cnt = i+1 k = x1 // cnt if x1 % cnt != 0: k += 1 if C[i].value >= k: for j in range(i+1, n): cnt1 = j - i k1 = x2 // cnt1 if x2 % cnt1 != 0: k1 += 1 if C[j].value >= k1: flag = True print("Yes") print(cnt, cnt1) for x in range(cnt): print(C[x].index, end=' ') print() for x in range(cnt, cnt+cnt1): print(C[x].index, end = ' ') print() break break if not flag: for i in range(n-1): cnt = i+1 k = x2 // cnt if x2 % cnt != 0: k += 1 if C[i].value >= k: for j in range(i+1, n): cnt1 = j - i k1 = x1 // cnt1 if x1 % cnt1 != 0: k1 += 1 if C[j].value >= k1: flag = True print("Yes") print(cnt1, cnt) for x in range(cnt, cnt+cnt1): print(C[x].index, end = ' ') print() for x in range(cnt): print(C[x].index, end=' ') print() break break if not flag: print("No") return 0 if __name__ == "__main__": n, x1, x2 = list(map(int, input().split(' '))) servers = list(map(int, input().split(' '))) res = solve(n, x1, x2, servers)
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
873/C
873
C
PyPy 3-64
TESTS
7
77
2,662,400
211738781
# region declaration from collections import * from functools import * from math import * from heapq import * from itertools import * from bisect import * # autopep8: off def floatl(): return (list(map(float, input().split()))) def inlt(): return (list(map(int, input().split()))) def inp(): return int(input()) def ins(): return str(input()) def insr(): return list(input()) def yesno(predicate): print("Yes" if predicate else "No") # MOD = 998244353 MOD = 1_000_000_007 INF = int(1e18) # autopep8: on # endregion def solve(): n, m, k = inlt() a = [inlt() for _ in range(n)] psum = [[0] * m for _ in range(n+1)] for r in range(n): for c in range(m): psum[r][c] += psum[r-1][c] + a[r][c] res = 0 ops = 0 for c in range(m): maxcol = 0 need_change = False for r in range(n): if a[r][c] == 0: continue end = min(n-1, r+k-1) score = psum[end][c] - psum[r-1][c] if score > maxcol: maxcol = score if psum[r-1][c] > 0: need_change = True ops += need_change res += maxcol print(res, ops) t = 1 # t = inp() for _ in range(t): solve()
20
62
5,529,600
31409917
(n, m, k) = map(int, input().split()) sums = [[] for i in range(m)] for i in range(n): str = input().split() for j in range(m): if i == 0: sums[j].append(int(str[j])) else: sums[j].append(sums[j][i - 1] + int(str[j])) ans1 = 0 ans2 = 0 for j in range(m): cans1 = 0 cans2 = 0 for i in range(n): if i > 0: y = sums[j][i - 1] else: y = 0 x = sums[j][min(i + k - 1, n - 1)] - y if x > cans1: cans1 = x cans2 = y ans1 += cans1 ans2 += cans2 print(ans1, ans2)
Educational Codeforces Round 30
ICPC
2,017
1
256
Strange Game On Matrix
Ivan is playing a strange game. He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
null
In the first example Ivan will replace the element a1, 2.
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
1,600
["greedy", "two pointers"]
20
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
false
stdio
null
true
873/C
873
C
Python 3
TESTS
7
77
921,600
31250217
# -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random """ created by shhuan at 2017/10/12 23:03 """ n, m, k = map(int, input().split()) a = [] for i in range(n): a.append([int(x) for x in input().split()]) removed = 0 score = 0 for c in range(m): sc, count = 0, sum([a[r][c] for r in range(k)]) for re in range(k, n): nc = count-a[re-k][c]+a[re][c] if nc > count: count = nc sc = re-k+1 removed += sum([a[r][c] for r in range(sc)]) score += count print(score, removed)
20
62
5,529,600
31446416
n,m,k=map(int,input().split()) l=[] for i in range(n): l.append(list(map(int,input().split()))) l=map(list,zip(*l)) score,chx=0,0 for row in l: sumx=sum(row[:k]) val,idx=sumx,0 for i,j in enumerate(row[k:]): sumx+=j-row[i] if sumx>val: val=sumx idx=i score+=val chx+=sum(row[:idx]) print(score,chx)
Educational Codeforces Round 30
ICPC
2,017
1
256
Strange Game On Matrix
Ivan is playing a strange game. He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
null
In the first example Ivan will replace the element a1, 2.
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
1,600
["greedy", "two pointers"]
20
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
false
stdio
null
true
725/D
725
D
PyPy 3
TESTS
4
139
0
93101001
import bisect import sys input=sys.stdin.readline n=int(input()) ar=[] for i in range(n): t,w=map(int,input().split()) if(i==0): mai=[t,w] else: ar.append([w-t+1,w,t]) ar.sort(key=lambda x:(x[2],-x[0])) br=[] cr=[] for i in range(n-1): br.append(ar[i][2]) cr.append([ar[i][0],i]) cr.sort(key=lambda x:x[0]) pos=bisect.bisect(br,mai[0]) ext=0 ans=[pos] for i in range(n): ke=cr[i][1] if(mai[0]-cr[i][0]>=0): mai[0]-=cr[i][0] jj=pos br[i]=-1 while(jj>=0 and mai[0]<br[jj]): if(br[jj]==-1): ext-=1 jj-=1 if(pos>=ke): ext+=1 pos=jj ans.append(pos+ext) else: break print(n-max(ans))
49
2,745
39,014,400
21687047
'''input 8 20 1000 32 37 40 1000 45 50 16 16 16 16 14 1000 2 1000 ''' import heapq from bisect import bisect inf = 10**18 + 2 def rints(): return list(map(int, input().split())) def ri(): return int(input()) def bin_search(arr, pred, lo=0, hi = None): if hi is None: hi = len(arr) while lo < hi : mid = (lo+hi) // 2 if pred(arr[mid]): hi = mid else: lo = mid + 1 return lo n = ri() score, _ = rints() teams = [] for _ in range(n-1): t, w = rints() teams.append((w-t + 1, t, w)) teams.sort(key = lambda x : x[1]) # print(teams) def solve(score): idx = bin_search(teams, lambda x : x[1] > score) best = pos = len(teams) - idx + 1 # print(teams[idx:], pos) ahead = teams[idx:] behind = teams[:idx] heapq.heapify(ahead) # print(ahead) while ahead and score >= ahead[0][0]: score -= heapq.heappop(ahead)[0] pos -= 1 while behind and behind[-1][1] > score: heapq.heappush(ahead, behind.pop()) pos += 1 # print(score, pos) best = min(best, pos) return best print(solve(score))
Canada Cup 2016
CF
2,016
3
256
Contest Balloons
One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed. You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings. A contest has just finished. There are n teams, numbered 1 through n. The i-th team has ti balloons and weight wi. It's guaranteed that ti doesn't exceed wi so nobody floats initially. Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has. What is the best place Limak can get?
The first line of the standard input contains one integer n (2 ≤ n ≤ 300 000) — the number of teams. The i-th of n following lines contains two integers ti and wi (0 ≤ ti ≤ wi ≤ 1018) — respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team.
Print one integer denoting the best place Limak can get.
null
In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: 1. Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place. 2. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified. 3. Limak gives 1 balloon to each of two teams with 16 balloons initially. 4. Limak has 20 - 6 - 6 - 1 - 1 = 6 balloons. 5. There are three other teams left and their numbers of balloons are 40, 14 and 2. 6. Limak gets the third place because there are two teams with more balloons. In the second sample, Limak has the second place and he can't improve it. In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81 000 000 000, 5 000 000 000 and 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7).
[{"input": "8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000", "output": "3"}, {"input": "7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5", "output": "2"}, {"input": "7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n46000000000 51000000000\n0 1000000000\n0 0", "output": "2"}]
1,800
["data structures", "greedy"]
49
[{"input": "8\r\n20 1000\r\n32 37\r\n40 1000\r\n45 50\r\n16 16\r\n16 16\r\n14 1000\r\n2 1000\r\n", "output": "3\r\n"}, {"input": "7\r\n4 4\r\n4 4\r\n4 4\r\n4 4\r\n4 4\r\n4 4\r\n5 5\r\n", "output": "2\r\n"}, {"input": "7\r\n14000000003 1000000000000000000\r\n81000000000 88000000000\r\n5000000000 7000000000\r\n15000000000 39000000000\r\n46000000000 51000000000\r\n0 1000000000\r\n0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n100 150\r\n5 100000\r\n", "output": "1\r\n"}, {"input": "9\r\n4 70\r\n32 56\r\n32 65\r\n77 78\r\n5 29\r\n72 100\r\n0 55\r\n42 52\r\n66 72\r\n", "output": "7\r\n"}, {"input": "3\r\n1 2\r\n12 19\r\n25 45\r\n", "output": "3\r\n"}, {"input": "5\r\n2 23\r\n1 13\r\n3 9\r\n0 20\r\n6 7\r\n", "output": "3\r\n"}, {"input": "10\r\n19 22\r\n10 77\r\n3 52\r\n16 42\r\n25 67\r\n14 42\r\n44 85\r\n37 39\r\n36 62\r\n6 85\r\n", "output": "4\r\n"}, {"input": "15\r\n143 698\r\n269 879\r\n100 728\r\n86 855\r\n368 478\r\n174 368\r\n442 980\r\n812 825\r\n121 220\r\n137 198\r\n599 706\r\n423 586\r\n96 647\r\n177 439\r\n54 620\r\n", "output": "9\r\n"}, {"input": "3\r\n1000 1000\r\n1001 1001\r\n700 1000000\r\n", "output": "1\r\n"}, {"input": "5\r\n4 100\r\n10 11\r\n10 11\r\n3 3\r\n3 3\r\n", "output": "2\r\n"}]
false
stdio
null
true
883/F
883
F
Python 3
TESTS
6
46
0
31999123
n = int(input()) s = set() for i in range(n): v = input() for rf, rt in [("kh", "h"), ("oo", "u"), ("uo", "ou")]: while rf in v: v = v.replace(rf, rt) s.add(v) print(len(s))
81
62
0
31942451
n = int(input()) def get_final_str(s): s = s.replace('u', 'oo') while 1: s1 = s.replace("kh", "h") if (s1 == s): break s = s1 return s d = set() for i in range(n): s = get_final_str(input()) d.add(s) print(len(d))
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Lost in Transliteration
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
Print the minimal number of groups where the words in each group denote the same name.
null
There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
1,300
["implementation"]
81
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
false
stdio
null
true
904/C
906
A
Python 3
TESTS
6
61
5,939,200
33594856
n=int(input()) l=[] chocks=0 e=set("abcdefghijklmnopqrstuvwxyz") for i in range(n): l.append(list(input().split())) if l[i][0]=='.': chocks+=1 chocks=n-chocks-1 com=0 i=0 while (len(list(e))>1) and i<n-1: if l[i][0]==".": e=e-set(l[i][1]) else: if l[i][0]=="!": e=e & set(l[i][1]) com+=1 i+=1 print(chocks-com)
38
140
0
216758910
s = set([chr(i) for i in range(ord('a'), ord('z')+1)]) cnt = 0 for i in range(int(input())-1): ss = input() if(ss[0] != '.' and len(s) == 1): cnt+=1 if(ss[0] == '!'): s&=set(ss[2:]) else: s-=set(ss[2:]) print(cnt)
Технокубок 2018 - Отборочный Раунд 4
CF
2,017
2
256
Shockers
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly. Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did. The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: 1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said. 2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said. 3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter. All words consist only of lowercase English letters. The total length of all words does not exceed 105. It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
null
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis. In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
1,600
["strings"]
38
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
false
stdio
null
true
433/C
433
C
PyPy 3-64
TESTS
3
77
3,276,800
166012844
# If you win, you live. You cannot win unless you fight. from sys import stdin,setrecursionlimit input=stdin.readline import heapq rd=lambda: map(lambda s: int(s), input().strip().split()) ri=lambda: int(input()) rs=lambda :input().strip() from collections import defaultdict as unsafedict,deque,Counter as unsafeCounter from bisect import bisect_left as bl, bisect_right as br from random import randint from math import gcd, floor,log2,factorial,radians,sin,cos random = randint(1, 10 ** 9) mod=998244353 def ceil(a,b): return (a+b-1)//b class myDict: def __init__(self,func): 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]}' n,m=rd() a=list(rd()) d=myDict(list) ans=0 for i in range(len(a)): if i-1>=0: ans+=abs(a[i]-a[i-1]) d[a[i]].append(a[i-1]) if i+1<m: d[a[i]].append(a[i+1]) bigans=ans for i in d.getKeys(): d[i].sort() for ind in range(len(d[i])): left=(ind+1) rigt=(len(d[i])-ind-1) tans=bigans tans-=(i*left) tans+=(i*rigt) tans+=(d[i][ind]*left) tans-=(d[i][ind]*rigt) # print(tans,i,left,rigt,ind) ans=min(ans,tans) # print(d) print(ans)
52
623
16,998,400
6695117
n,m = [int(x) for x in input().split()] a = [int(x)-1 for x in input().split()] assert len(a) == m tot = 0 for i in range(m-1): tot += abs(a[i+1]-a[i]) arr = [[] for i in range(n)] for i in range(m-1): if a[i] == a[i+1]: continue arr[a[i]].append(a[i+1]) arr[a[i+1]].append(a[i]) cursum = [] for i in range(n): cur = 0 for e in arr[i]: cur += abs(e-i) cursum.append(cur) assert sum(cursum) == 2*tot minsum = [0 for i in range(n)] for i in range(n): if len(arr[i]) == 0: continue arr[i].sort() med = arr[i][len(arr[i])//2] cur = 0 for e in arr[i]: cur += abs(e-med) minsum[i] = cur for i in range(n): assert minsum[i] <= cursum[i] dif = [cursum[i]-minsum[i] for i in range(n)] maxdif = max(dif) out = tot - maxdif print(out)
Codeforces Round 248 (Div. 2)
CF
2,014
1
256
Ryouko's Memory Note
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is $$\sum_{i=1}^{m-1} |a_{i+1} - a_i|$$. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.
The first line of input contains two integers n and m (1 ≤ n, m ≤ 105). The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n).
Print a single integer — the minimum number of pages Ryouko needs to turn.
null
In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
[{"input": "4 6\n1 2 3 4 3 2", "output": "3"}, {"input": "10 5\n9 4 3 8 8", "output": "6"}]
1,800
["implementation", "math", "sortings"]
52
[{"input": "4 6\r\n1 2 3 4 3 2\r\n", "output": "3\r\n"}, {"input": "10 5\r\n9 4 3 8 8\r\n", "output": "6\r\n"}, {"input": "5 10\r\n2 5 2 2 3 5 3 2 1 3\r\n", "output": "7\r\n"}, {"input": "10 20\r\n6 3 9 6 1 9 1 9 8 2 7 6 9 8 4 7 1 2 4 2\r\n", "output": "52\r\n"}, {"input": "100 100\r\n28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28\r\n", "output": "0\r\n"}, {"input": "100000 1\r\n97735\r\n", "output": "0\r\n"}, {"input": "10 100\r\n3 2 5 7 1 1 5 10 1 4 7 4 4 10 1 3 8 1 7 4 4 8 5 7 2 10 10 2 2 4 4 5 5 4 8 8 8 9 10 5 1 3 10 3 6 10 6 4 9 10 10 4 10 1 2 5 9 8 9 7 10 9 10 1 6 3 4 7 8 6 3 5 7 10 5 5 8 3 1 2 1 7 6 10 4 4 2 9 9 9 9 8 8 5 4 3 9 7 7 10\r\n", "output": "218\r\n"}, {"input": "100000 1\r\n14542\r\n", "output": "0\r\n"}, {"input": "44 44\r\n22 26 30 41 2 32 7 12 13 22 5 43 33 12 40 14 32 40 3 28 35 26 26 43 3 14 15 16 18 13 42 10 21 19 1 17 34 26 10 40 7 25 20 12\r\n", "output": "568\r\n"}, {"input": "2 3\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "100000 50\r\n43104 45692 17950 43454 99127 33540 80887 7990 116 79790 66870 61322 5479 24876 7182 99165 81535 3498 54340 7460 43666 921 1905 68827 79308 59965 8437 13422 40523 59605 39474 22019 65794 40905 35727 78900 41981 91502 66506 1031 92025 84135 19675 67950 81327 95915 92076 89843 43174 73177\r\n", "output": "1583927\r\n"}, {"input": "100 100\r\n11 41 76 12 57 12 31 68 92 52 63 40 71 18 69 21 15 27 80 72 69 43 67 37 21 98 36 100 39 93 24 98 6 72 37 33 60 4 38 52 92 60 21 39 65 60 57 87 68 34 23 72 45 13 7 55 81 61 61 49 10 89 52 63 12 21 75 2 69 38 71 35 80 41 1 57 22 60 50 60 40 83 22 70 84 40 61 14 65 93 41 96 51 19 21 36 96 97 12 69\r\n", "output": "3302\r\n"}, {"input": "1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "11 5\r\n1 1 1 10 11\r\n", "output": "1\r\n"}, {"input": "100 6\r\n1 1 3 3 1 1\r\n", "output": "0\r\n"}, {"input": "100 14\r\n1 2 100 100 100 100 100 100 100 100 100 100 2 1\r\n", "output": "2\r\n"}, {"input": "1000 10\r\n1 1 1 1 1 1000 1000 1000 1000 1000\r\n", "output": "0\r\n"}, {"input": "3 6\r\n1 1 1 3 3 3\r\n", "output": "0\r\n"}, {"input": "10 4\r\n7 1 1 8\r\n", "output": "1\r\n"}, {"input": "3 18\r\n1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3\r\n", "output": "0\r\n"}, {"input": "5 4\r\n5 5 2 1\r\n", "output": "1\r\n"}, {"input": "10 10\r\n8 8 8 7 7 7 6 1 1 1\r\n", "output": "2\r\n"}]
false
stdio
null
true
370/B
370
B
Python 3
PRETESTS
5
46
0
5371869
import sys f = sys.stdin #f = open("input.txt", "r") n = int(f.readline()) a = f.read().strip().split("\n") players = [[int(k) for k in i.split()[1:]] for i in a] winners = [True for i in range(len(players))] for i, player in enumerate(players): for num in player: for j, p in enumerate(players[:i]): if p == player: winners[i] = False winners[j] = False break elif len(p) < len(player) and num in p: winners[i] = False break for i in winners: if i: print("YES") else: print("NO")
24
77
204,800
104135938
n = int(input()) c = [[int(s) for s in input().split()][1:] for x in range (n)] for i in range (n): for j in range (n): if i != j: for k in c[j]: if k not in c[i]: break else: print("NO") break else: print("YES")
Codeforces Round 217 (Div. 2)
CF
2,013
1
256
Berland Bingo
Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains mi numbers. During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct. You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.
The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer mi (1 ≤ mi ≤ 100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers ai, 1, ai, 2, ..., ai, mi (1 ≤ ai, k ≤ 100) — the numbers on the i-th player's card. The numbers in the lines are separated by single spaces. It is guaranteed that all the numbers on each card are distinct.
Print n lines, the i-th line must contain word "YES" (without the quotes), if the i-th player can win, and "NO" (without the quotes) otherwise.
null
null
[{"input": "3\n1 1\n3 2 4 1\n2 10 11", "output": "YES\nNO\nYES"}, {"input": "2\n1 1\n1 1", "output": "NO\nNO"}]
1,300
["implementation"]
24
[{"input": "3\r\n1 1\r\n3 2 4 1\r\n2 10 11\r\n", "output": "YES\r\nNO\r\nYES\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "NO\r\nNO\r\n"}, {"input": "1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "YES\r\nYES\r\n"}, {"input": "2\r\n1 1\r\n2 1 2\r\n", "output": "YES\r\nNO\r\n"}, {"input": "2\r\n2 1 2\r\n1 1\r\n", "output": "NO\r\nYES\r\n"}, {"input": "2\r\n3 5 21 7\r\n6 15 5 100 21 7 17\r\n", "output": "YES\r\nNO\r\n"}, {"input": "2\r\n6 15 5 100 21 7 17\r\n3 5 21 7\r\n", "output": "NO\r\nYES\r\n"}, {"input": "10\r\n1 4\r\n1 2\r\n1 3\r\n1 5\r\n1 1\r\n1 4\r\n1 3\r\n1 5\r\n1 2\r\n1 1\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "3\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "NO\r\nYES\r\nNO\r\n"}, {"input": "10\r\n3 2 3 4\r\n1 1\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 1\r\n1 3\r\n2 4 5\r\n2 1 2\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "10\r\n1 4\r\n4 3 2 4 1\r\n1 4\r\n2 4 5\r\n4 4 3 5 1\r\n1 4\r\n1 2\r\n2 3 5\r\n2 5 3\r\n3 5 2 4\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "20\r\n2 9 16\r\n3 1 15 2\r\n1 9\r\n3 7 12 3\r\n1 18\r\n1 14\r\n4 11 13 4 6\r\n4 7 19 9 3\r\n3 9 16 5\r\n1 9\r\n1 18\r\n4 4 15 7 19\r\n2 16 2\r\n3 7 3 15\r\n2 2 20\r\n1 1\r\n1 15\r\n5 5 2 13 4 1\r\n2 9 14\r\n2 17 8\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "40\r\n2 12 19\r\n4 10 7 1 3\r\n2 15 17\r\n1 6\r\n3 17 8 20\r\n4 8 16 11 18\r\n2 2 7\r\n4 12 13 8 7\r\n3 6 1 15\r\n3 19 11 13\r\n1 2\r\n2 16 14\r\n5 1 17 8 9 5\r\n1 2\r\n3 15 17 12\r\n4 20 4 19 18\r\n1 10\r\n4 12 1 17 16\r\n4 5 10 8 11\r\n1 10\r\n1 13\r\n1 17\r\n2 19 18\r\n1 3\r\n2 6 20\r\n1 8\r\n2 3 14\r\n3 17 3 1\r\n2 4 3\r\n1 12\r\n1 15\r\n1 2\r\n2 13 9\r\n2 1 14\r\n1 1\r\n5 14 9 3 1 7\r\n2 20 16\r\n2 19 17\r\n2 4 20\r\n1 7\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\n"}]
false
stdio
null
true
632/B
632
B
PyPy 3
TESTS
3
155
0
77110122
n = int(input()) p = [int(x) for x in input().split(' ')] k = input() t = [] for u in k: t.append(u) a = 0 for i in range(0,n): if t[i] == 'B': a += p[i] b = 0 c = 0 if t[0] == 'B' and t[n-1] == 'B': pass else: for i in range(0,n): if t[i] == 'A': b += p[i] elif t[i] == 'B': break p.reverse() t.reverse() for i in range(0,n): if t[i] == 'A': c += p[i] elif t[i] == 'B': break print(a+max(b,c))
17
233
63,385,600
167572442
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) s = input()[:-1] x = 0 x = sum(w[i] for i in range(n) if s[i] == 'B') c = d = x for i in range(n): if s[i] == 'A': c += w[i] else: c -= w[i] x = max(c, x) for i in range(n-1, -1, -1): if s[i] == 'A': d += w[i] else: d -= w[i] x = max(d, x) print(x)
Educational Codeforces Round 9
ICPC
2,016
1.5
256
Alice, Bob, Two Teams
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces. The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece. The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).
Print the only integer a — the maximum strength Bob can achieve.
null
In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing.
[{"input": "5\n1 2 3 4 5\nABABA", "output": "11"}, {"input": "5\n1 2 3 4 5\nAAAAA", "output": "15"}, {"input": "1\n1\nB", "output": "1"}]
1,400
["brute force", "constructive algorithms"]
17
[{"input": "5\r\n1 2 3 4 5\r\nABABA\r\n", "output": "11\r\n"}, {"input": "5\r\n1 2 3 4 5\r\nAAAAA\r\n", "output": "15\r\n"}, {"input": "1\r\n1\r\nB\r\n", "output": "1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\nABBABAABBB\r\n", "output": "33\r\n"}, {"input": "100\r\n591 417 888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983 526 103 500 963 400 23 276 704 570 757 410 658 507 620 984 244 486 454 802 411 985 303 635 283 96 597 855 775 139 839 839 61 219 986 776 72 729 69 20 917\r\nBBBAAABBBABAAABBBBAAABABBBBAAABAAABBABABAAABABABBABBABABAAAABAABABBBBBBBABBAAAABAABABABAABABABAABBAB\r\n", "output": "30928\r\n"}, {"input": "3\r\n1 1 1\r\nBAA\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1 2\r\nBAB\r\n", "output": "4\r\n"}, {"input": "2\r\n1 1\r\nBB\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\nA\r\n", "output": "1\r\n"}, {"input": "2\r\n1 1\r\nAB\r\n", "output": "2\r\n"}]
false
stdio
null
true