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] = ls...
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 sw...
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 arr...
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 per...
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...
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: ...
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): pr...
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 ...
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 individu...
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, th...
[{"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...
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...
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]] >...
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]] <= ...
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 sho...
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-100...
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 ...
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: ...
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...
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...
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 e...
[{"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 258224055...
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]...
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:...
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...
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...
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 e...
[{"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 258224055...
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 # ...
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] & ...
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...
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,...
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...
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 pa...
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...
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], e...
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...
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"...
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 ope...
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 pa...
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...
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....
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...
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 sw...
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 arr...
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 per...
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...
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: ...
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 =...
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") #######################################...
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 exac...
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...
[{"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 4...
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) ...
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)] ...
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...
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 ...
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...
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 = () ...
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 ...
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 elemen...
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...
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...
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.c...
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....
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 ro...
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,...
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...
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 ...
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)...
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 exac...
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...
[{"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 4...
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())) swa...
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 sw...
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 arr...
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 per...
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...
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: ...
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] ...
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:[],...
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...
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 sp...
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 = -...
[{"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\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],...
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...
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"...
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 ope...
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, ...
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 wer...
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...
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 p...
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 1...
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...
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: ...
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 ...
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 elemen...
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...
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...
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[...
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...
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 o...
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 ...
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 cell...
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+...
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+...
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 wit...
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() ...
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 ...
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 elemen...
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...
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...
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...
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"...
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 ope...
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 ...
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: ma...
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 ro...
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,...
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...
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]=...
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....
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 ro...
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,...
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...
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...
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":...
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 ...
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 = [],[]...
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 exac...
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...
[{"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 4...
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) ...
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 rang...
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 exac...
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...
[{"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 4...
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: ...
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]: a...
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 e...
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 t...
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 d...
[{"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...
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...
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 perm...
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 ...
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 -...
[{"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": ...
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 -=...
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 ...
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 t...
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...
[{"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\n6...
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 # и загоня...
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...
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"...
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 ope...
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 E...
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: ...
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 ...
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 di...
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 consi...
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...
[{"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...
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...
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): ...
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:[],...
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...
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 sp...
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 = -...
[{"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\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() ...
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 maxim...
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"},...
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 hea...
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...
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 pa...
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...
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...
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[...
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 hed...
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 guar...
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 ...
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, he...
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 ar...
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, z...
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\...
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]) # pr...
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 th...
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 poin...
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 -1...
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...
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 s...
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 ...
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 ...
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...
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))....
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...
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 clot...
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 fol...
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 ...
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]), ...
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 recurs...
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 fo...
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...
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 fol...
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 ...
[{"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\\\nfi...
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...
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.p...
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 s...
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", "khari...
[{"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...
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 ...
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 pa...
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...
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 + ton...
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....
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 Va...
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 ta...
[{"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...
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....
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 Va...
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 ta...
[{"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...
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....
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 Va...
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 ta...
[{"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...
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'), f...
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()) ...
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...
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, h...
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 | ) } }...
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, ...
[{"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"}, ...
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 ...
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 rang...
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 ...
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...
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 =...
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]: ...
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 hed...
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 guar...
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 ...
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: ...
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 = ma...
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 ...
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...
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 vali...
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: ...
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 pe...
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 ...
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 ...
[{"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 ...
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" i...
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...
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 th...
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 poin...
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 -1...
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....
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 Va...
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 ta...
[{"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...
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 ...
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) ...
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 ...
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...
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....
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 Va...
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 ta...
[{"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...
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 th...
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 poin...
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 -1...
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*=fac...
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 ...
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 alrea...
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 initia...
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...
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): i...
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 =...
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 ...
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...
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 =...
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] ...
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 ve...
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, ...
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...
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...
[{"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...
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 ...
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...
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 not...
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 t...
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": "1...
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: ...
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 not...
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 t...
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": "1...
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] ...
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 f...
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-decre...
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", "ou...
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 = []...
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 ea...
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": "99...
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 ea...
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": "99...
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) ...
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: cont...
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 ...
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....
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"}, ...
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 ...
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: ...
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 progra...
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 fr...
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...
[{"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...
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 ...
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 ...
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 dif...
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...
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-ne...
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...
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()) ...
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]=...
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: ...
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 fro...
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 inte...
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....
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...
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 el...
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 progra...
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 fr...
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...
[{"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...
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(pla...
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, i...
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 onc...
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\...
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 ...
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) ...
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 g...
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...
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; - push...
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\npushFr...
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()...
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"...
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 s...
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", "khari...
[{"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...
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': re...
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 s...
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", "khari...
[{"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...
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 ...
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 c...
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, i...
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 onc...
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\...
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...
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 +=...
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 r...
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 ...
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...
[{"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 ...
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...
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 s...
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", "khari...
[{"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...
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 < l...
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 s...
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", "khari...
[{"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...
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: ...
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]): ...
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...
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 ≤...
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...
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 f...
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)...
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), ...
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...
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 ≤...
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...
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 f...
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)...
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().s...
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...
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 go...
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 t...
[{"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": ...
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): ...
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 a...
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 din...
[{"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...
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] ...
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 m...
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$$$...
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 th...
[{"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\...
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(k...
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 m...
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$$$...
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 th...
[{"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\...
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...
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: ...
Технокубок 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 eac...
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...
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 ...
[{"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\...
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...
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) \m...
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":...
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 ...
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): a...
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,...
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"},...
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 = [] ...
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 ...
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)...
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, i...
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 onc...
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\...
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==...
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 det...
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) \m...
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":...
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...
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...
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 ...
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 m...
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\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() ...
true
925/B
925
B
PyPy 3-64
TESTS
4
46
0
223632490
''' ╔═══╗╔═══╗╔═╗ ╔╗╔══╗╔╗ ╔╗ ╔═══╗╔═══╗╔╗ ╔╗ ╚╗╔╗║║╔═╗║║ ╚╗║║╚╣╔╝║║ ╔╝║ ║╔═╗║║╔═╗║║║ ║║ ║║║║║║ ║║║╔╗╚╝║ ║║ ║║ ╚╗║ ╚╝╔╝║╚╝╔╝║║╚═╝║ ║║║║║╚═╝║║║╚╗ ║ ║║ ║║ ╔╗ ║║ ╔═╝╔╝╔╗╚╗║╚══╗║ ╔╝╚╝║║╔═╗║║║ ║ ║╔╣╚╗║╚═╝║╔╝╚╗║ ╚═╗║╚═╝║ ║║ ╚═══╝╚╝ ╚╝╚╝ ╚═╝╚══╝╚═══╝╚══╝╚═══╝╚═══╝ ╚╝ ''' from itertools import permutations, produ...
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 = Fa...
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 pro...
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, \ldo...
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. ...
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 / ...
[{"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", "...
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) a...
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...
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 ...
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 ...
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...
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, ...
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+=va...
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 ...
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...
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.s...
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 < ...
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 ...
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...
[{"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\n...
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\n1500000000...
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 s...
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", "khari...
[{"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...
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 & ...
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 eac...
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...
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 ...
[{"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\...
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 unsafeCounte...
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...
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 bo...
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 2...
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]): ...
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: pri...
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 num...
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, ...
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"}, {"i...
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[...
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] ==...
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 a...
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 ...
false
stdio
null
true