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
186/A
186
A
PyPy 3
TESTS
41
312
2,355,200
66290590
first, second = input(), input() f_len, s_len = len(first), len(second) if f_len != s_len: exit(print('NO')) ix, c = [], 0 for i in range(f_len): if c > 2: exit(print('NO')) if first[i] != second[i]: ix.append(i) c += 1 if c == 1: exit(print('NO')) if first[ix[0]] == second[ix[1]] and first[ix[1]] == second[ix[0]]: print('YES') else: print('NO')
53
92
204,800
215041802
if __name__ == "__main__": S1 = input() S2 = input() if len(S1) != len(S2): print("NO") else: c = 0 chars = set() for i in range(len(S1)): if S1[i] != S2[i]: c += 1 chars.add(S1[i]) chars.add(S2[i]) if c > 2: break if c == 2 and len(chars) == 2: print("YES") else: print("NO")
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Comparing Strings
Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters. Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.
The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.
Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".
null
- First example: you can simply swap two letters in string "ab". So we get "ba". - Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".
[{"input": "ab\nba", "output": "YES"}, {"input": "aa\nab", "output": "NO"}]
1,100
["implementation", "strings"]
53
[{"input": "ab\r\nba\r\n", "output": "YES\r\n"}, {"input": "aa\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\nza\r\n", "output": "NO\r\n"}, {"input": "vvea\r\nvvae\r\n", "output": "YES\r\n"}, {"input": "rtfabanpc\r\natfabrnpc\r\n", "output": "YES\r\n"}, {"input": "mt\r\ntm\r\n", "output": "YES\r\n"}, {"input": "qxolmbkkt\r\naovlajmlf\r\n", "output": "NO\r\n"}, {"input": "b\r\ng\r\n", "output": "NO\r\n"}, {"input": "ab\r\naba\r\n", "output": "NO\r\n"}, {"input": "ba\r\na\r\n", "output": "NO\r\n"}, {"input": "a\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\naa\r\n", "output": "NO\r\n"}, {"input": "a\r\nz\r\n", "output": "NO\r\n"}, {"input": "aabb\r\nbbaa\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbd\r\n", "output": "NO\r\n"}, {"input": "bac\r\ndae\r\n", "output": "NO\r\n"}, {"input": "abc\r\nakl\r\n", "output": "NO\r\n"}, {"input": "cb\r\naa\r\n", "output": "NO\r\n"}, {"input": "abaab\r\naabba\r\n", "output": "NO\r\n"}, {"input": "aab\r\naaa\r\n", "output": "NO\r\n"}, {"input": "abcde\r\nedcba\r\n", "output": "NO\r\n"}, {"input": "abab\r\nbaba\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbac\r\n", "output": "NO\r\n"}, {"input": "abcd\r\naqcb\r\n", "output": "NO\r\n"}, {"input": "abc\r\nbad\r\n", "output": "NO\r\n"}, {"input": "ab\r\nca\r\n", "output": "NO\r\n"}, {"input": "abc\r\nab\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbae\r\n", "output": "NO\r\n"}, {"input": "aaaabcccca\r\naaaadccccb\r\n", "output": "NO\r\n"}, {"input": "acaa\r\nabca\r\n", "output": "NO\r\n"}, {"input": "aba\r\naab\r\n", "output": "YES\r\n"}, {"input": "ah\r\nla\r\n", "output": "NO\r\n"}, {"input": "aab\r\naa\r\n", "output": "NO\r\n"}, {"input": "abab\r\nabcd\r\n", "output": "NO\r\n"}, {"input": "ed\r\nab\r\n", "output": "NO\r\n"}]
false
stdio
null
true
552/E
552
E
PyPy 3
TESTS
5
139
0
95328541
s=input() ans=0; for i,x in enumerate(s): if x=='*': for j in range(0,i,2): s2=f"{s[:j]}({s[j:i]}){s[i:]}" ans=max(ans,eval(s2)) print(ans)
69
326
3,276,800
213602925
s = str(input()) val = eval(s) n = len(s) for i in range(-1, n): if i == -1 or s[i] == '*': for j in range(i + 1, n + 1): if j == n or s[j] == '*': new = s[0 : i + 1] + '(' + s[i + 1 : j] + ')' + s[j : n] val = max(val, eval(new)) print(val)
Codeforces Round 308 (Div. 2)
CF
2,015
1
256
Vanya and Brackets
Vanya is doing his maths homework. He has an expression of form $$x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$$, where x1, x2, ..., xn are digits from 1 to 9, and sign $$\text{ }$$ represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * . The number of signs * doesn't exceed 15.
In the first line print the maximum possible value of an expression.
null
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303. Note to the second sample test. (2 + 3) * 5 = 25. Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
[{"input": "3+5*7+8*4", "output": "303"}, {"input": "2+3*5", "output": "25"}, {"input": "3*4*5", "output": "60"}]
2,100
["brute force", "dp", "expression parsing", "greedy", "implementation", "strings"]
69
[{"input": "3+5*7+8*4\r\n", "output": "303\r\n"}, {"input": "2+3*5\r\n", "output": "25\r\n"}, {"input": "3*4*5\r\n", "output": "60\r\n"}, {"input": "5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5\r\n", "output": "152587890625\r\n"}, {"input": "2*2+2*2\r\n", "output": "16\r\n"}, {"input": "1+1+1+1+1+1+1\r\n", "output": "7\r\n"}, {"input": "1+5*6+7*8\r\n", "output": "521\r\n"}, {"input": "9*8+7*6+5*4+3*2+1\r\n", "output": "1987\r\n"}, {"input": "3*3*9+4+6+8*4+5+1*4*6\r\n", "output": "12312\r\n"}, {"input": "4*9+4+5+8*4+6+9+8+2+5+2+5*7+6+8\r\n", "output": "2450\r\n"}, {"input": "9+9+9*9*9*9+9+9\r\n", "output": "19701\r\n"}, {"input": "9+9+9+9+9*9*9*9\r\n", "output": "32805\r\n"}, {"input": "1*1*1*1*1*1*1*1+1*1*1*1*1*1*1*1\r\n", "output": "2\r\n"}, {"input": "4+2*7+8+9*6+6*9+8+7*2+4\r\n", "output": "1380\r\n"}, {"input": "5\r\n", "output": "5\r\n"}, {"input": "4+6*7+4\r\n", "output": "74\r\n"}, {"input": "2+7+3+5+4+2+3+9+9+6+9+2+3+6+5*3+4+5+6+5+8\r\n", "output": "253\r\n"}, {"input": "3+2+2+3+7+1+9+1+6+8+3+2+2+6+7+2+8+8+1+4+9\r\n", "output": "94\r\n"}, {"input": "3+9+3+1+6+4+7+9+5+8+2+6+1+4+4+5+1+7+5+4+6+4+3+1*9+7+7+4+5+2+3+2+6+5+5+8+7+8+2+3*3+8+3+4+9+8*5+9+2+8+2+8+6+6+9+6+4+2+5+3+1+2+6+6+2+4+6+4+2+7+2+7+6+9+9+3+6+7+8+3+3+2+3+7+9+7+8+5+5+5*1+3+2+5+8+5*6+5+4*6+2+5+5+4+9+8+3+5+1+3+1+6+2+2+1+3+2+3+3+3+2+8+3+2+8+8+5+2+6+6+3+1+1+5+5*1+5+7+5+8+4+1*7+5+9+5+8+1*8+5+9+3+1+8+6+7+8+3+5+1+5+6+9*9+6+1+9+8+9+1+5+9+9+6+3+8+8+6*9*3+9+7+7+4+3+8+8+6+7+1+8+6+3+1+7+7+1+1+3+9+8+5+5+6+8+2+4+1+5+7+2+3+7+1+5+1+6+1+7+3*5+5+9+2+1+3+9+4+8+6+5+5+2+3+7+9+5+6+8+3*3+2+4+4+6+3+2+4+1+4+8\r\n", "output": "162353\r\n"}, {"input": "1*5*1+8*2*6*5*3*9+3+8+2+9*5+7+2+9+5+1*3+2*2*3*4*2*3\r\n", "output": "19699205\r\n"}, {"input": "4+4+6+2+5+9+9+5+5+9+4+1*5+3+6+9+6+2+4+3+2+8+9*6+5+4+3+8+7+3+2*3+1+6+8+3+8+1+8+2+1+1+1+6+9+6+4+6+7+8+3+1+5+4+8+8+6+5+8+7+7+1+7+6+3+3+9+6+3+5+4+4+1+4+1+8+6+2+9+8+7+2+3+1+4+3+9+9+2*1+3+8+2+4+1+8+9+3*7+3+7+5+3+7+5+5+3+2+9+8+4+7+5+3+7+7+3+8+9+4+9+6*6+3+8+8*7+7+9+1+3+5+1+1+1+9+8+2+1+1+5+5+5+1+6+7+3+6+1+4+1+7+1+7+1+1+9+9*4+1+3+9+3+5+5+5+5+2+9+6+7+3+5+9+3+5+3+9+3+9+9+2+7+2+1*4+6*2+5+7+6+1+1+2+8+9+5+8+3+9+9+1+1+4+9+7+5+8*9+5+2+6+5+6*2+4+2+5+2+3+9+6+9+5+5+5*6+8+2+3+1+2+8+3+1+6+5+9+7+4+2+8+9+1+5+8+5+3+2+7+1\r\n", "output": "82140\r\n"}, {"input": "6*9+9*5*5+1*2*9*9*1+4*8+8+9+5+6*5*6+4+2+2+1+5*5*7*8\r\n", "output": "11294919\r\n"}, {"input": "5+3+5+9+3+9+1+3+1*7+7+1+9+3+7+7+6+6+3+7+4+3+6+4+5+1+2*3+6*5+5+6+2+8+3+3+9+9+1+1+2+8+4+8+9+3*7+3+2*8+9+8+1*9+9+7+4+8+6+7+3+5+6+4+4+9+2+2+8+6+7+1+5+4+4+6+6+6+9+8+7+2+3+5+4+6+1+8+8+9+1+9+6+3+8+5*7+3+1+6+7+9+1+6+2+2+8+8+9+3+7+7+2+5+8+6+7+9+7+2+4+9+8+3+7+4+5+7+6+5*6+4+6+4+6+2+2*6+2+5+5+1+8+7+7+6+6+8+2+8+8+6+7+1+1+1+2+5+1+1+8+9+9+6+5+8+7+5+8+4+8+8+1+4+6+7+3+2*1+1+3+5+3+3+3+9+8+7*2+4+7+5+8+3+3+9+3+7+2+1+1+7+6+2+5+5+2+1+8+8+2+9+9+2+4+6+6+4+8+9+3+7+1+3*9+8+7+4+9+4+6+2+9+8+8+5+8+8+2+5+6+6+4+7+9+4+7+2+3+1+7\r\n", "output": "58437\r\n"}, {"input": "2+7+8*8*7+1+3+6*5*3*7*3*2+8+5*1+5*5+9*6+6*5+1*3+8+5\r\n", "output": "1473847\r\n"}, {"input": "1+2+4+8+6+5+3+8+2+9+9+5+8+7+7+7+6+1+7+2+8+3+2+5+1+6+1+3+8+2+5+4+3+5+7+8+5+7+7+3+8+1+7+1+1+1+5+9+5+9+1+6+7+6+8+9+2+7+9+2+9+9+7+3+2+8+4+4+5+9+6+2+6+8+1+3+5+3+9+4+7+4+3+9+8+2+6+3+5+1*3+1+6+8+5+3+9+2+9+9+3+4+8*6+3+9+7+1+1+4+6+4+5*6*1+1*9+6+5+4+3+7+3+8+6+2+3+7+4+1+5+8+6+1+6+9+1+2+7+2+2+1+7+9+4+3+1+4+3+3+1+1+2+1+8+9+8+6+9+9+6+3+7*1+1+3+7+9+3+6+5+2*9+8+1+9+8+7+5+3+6+9+3+5+3+5+5+7+5+2*9+9+2+4+2+3+7+1+7+1+3+8+6+4+5+9+3*2+8+6+8+2*6+8+1+4+2+7+7+6+8+3+2+5+8+1+8+5+6+1+6+4+6+8+6+6+4+3+5+2+1+5+9+9+4+4*9+7+8+4+4\r\n", "output": "178016\r\n"}, {"input": "8+3*6*9*6+5*1*8*2+1+9+2+1*3*2+9+5+4+3+1+3*9*6*8+4+1\r\n", "output": "9027949\r\n"}, {"input": "1*1*1*1*1*1*1*1*1*1*1*1\r\n", "output": "1\r\n"}, {"input": "5+5*5+5*5+5*5+5\r\n", "output": "885\r\n"}, {"input": "8+7+3+6+3*8+8+9+8+4+2\r\n", "output": "247\r\n"}, {"input": "7+8*4+9+5+3+2+3+3+2+9\r\n", "output": "327\r\n"}, {"input": "1+1+7+1+7+7*7+5+3*9+3\r\n", "output": "965\r\n"}, {"input": "9+6+9+7+8*2*9+8+6+7+5\r\n", "output": "728\r\n"}, {"input": "8+8*3*8+1+9*4+9+2+8+4\r\n", "output": "1759\r\n"}, {"input": "3+5+5+2+2+9*7+7+7*2*2\r\n", "output": "773\r\n"}, {"input": "6+8+5+9*2+7*9*3+2*2+8\r\n", "output": "3501\r\n"}, {"input": "2*3+9+6*5*8+2+9*6+3+9\r\n", "output": "3447\r\n"}, {"input": "7+7*6+7+6*1+8+8*1*2*4\r\n", "output": "1967\r\n"}, {"input": "3+2*5+9+5*2+5*5*7+9*2\r\n", "output": "2051\r\n"}, {"input": "3+4*5+6\r\n", "output": "47\r\n"}]
false
stdio
null
true
451/B
451
B
Python 3
TESTS
73
93
12,800,000
224830607
n = int(input()) arr = list(map(int, input().split())) max_val = -1 flag = 0 l = 0 r = 0 done = 0 for i in range(len(arr)): if done == 0: if arr[i] >= max_val and flag == 0: max_val = arr[i] elif arr[i] < max_val and flag == 0: l = i-1 flag = 1 elif arr[i] > arr[i-1] and arr[i] >= max_val: r = i-1 done = 1 elif (arr[i] > arr[i-1] and arr[i] < max_val): print("no") exit() else: if arr[i] < arr[i-1]: print("no") exit() if done == 0: r = i if flag == 0: print("yes") print(1, 1) else: print("yes") print(l+1, r+1)
76
77
15,974,400
204724232
n = int(input()) l = list(map(int,input().split())) i = 1 while i<n and l[i]>=l[i-1]: i += 1 j = i while j < n and l[j] <= l[j-1]: j += 1 t = l[:i-1] + l[i-1:j][::-1] + l[j:] l.sort() if t == l: print("yes") print(i,j) else: print("no")
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
247/A
250
A
Python 3
TESTS
7
218
0
59682556
from math import * import time n=int(input()) l=input().split(" ") ll=[] c=0 s=0 for i in range(0,len(l)): if(s==3): ll.append(c-1) c=2 s=1 else: if(int(l[i])<0): s=s+1 c=c+1 else: c=c+1 if(s==3): ll.append(c-1) ll.append(1) else: ll.append(c) print(len(ll)) print(*ll) # print(time.time()-t1)
69
92
0
156077954
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) c, s, q = 0, 0, 0 d = [] for i in range(n): q += 1 if w[i] < 0: c += 1 if c == 3: s += 1 c = 1 q -= 1 d.append(q) q = 1 s += 1 d.append(q) print(s) print(' '.join(map(str, d)))
CROC-MBTU 2012, Final Round
CF
2,012
2
256
Paper Work
Polycarpus has been working in the analytic department of the "F.R.A.U.D." company for as much as n days. Right now his task is to make a series of reports about the company's performance for the last n days. We know that the main information in a day report is value ai, the company's profit on the i-th day. If ai is negative, then the company suffered losses on the i-th day. Polycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the n days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on. It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (ai < 0), he loses his temper and his wrath is terrible. Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal. Write a program that, given sequence ai, will print the minimum number of folders.
The first line contains integer n (1 ≤ n ≤ 100), n is the number of days. The second line contains a sequence of integers a1, a2, ..., an (|ai| ≤ 100), where ai means the company profit on the i-th day. It is possible that the company has no days with the negative ai.
Print an integer k — the required minimum number of folders. In the second line print a sequence of integers b1, b2, ..., bk, where bj is the number of day reports in the j-th folder. If there are multiple ways to sort the reports into k days, print any of them.
null
Here goes a way to sort the reports from the first sample into three folders: 1 2 3 -4 -5 | -6 5 -5 | -6 -7 6 In the second sample you can put all five reports in one folder.
[{"input": "11\n1 2 3 -4 -5 -6 5 -5 -6 -7 6", "output": "3\n5 3 3"}, {"input": "5\n0 -1 100 -1 0", "output": "1\n5"}]
1,000
[]
69
[{"input": "11\r\n1 2 3 -4 -5 -6 5 -5 -6 -7 6\r\n", "output": "3\r\n5 3 3 "}, {"input": "5\r\n0 -1 100 -1 0\r\n", "output": "1\r\n5 "}, {"input": "1\r\n0\r\n", "output": "1\r\n1 "}, {"input": "1\r\n-1\r\n", "output": "1\r\n1 "}, {"input": "2\r\n0 0\r\n", "output": "1\r\n2 "}, {"input": "2\r\n-2 2\r\n", "output": "1\r\n2 "}, {"input": "2\r\n-2 -1\r\n", "output": "1\r\n2 "}, {"input": "12\r\n1 -12 -5 -8 0 -8 -1 -1 -6 12 -9 12\r\n", "output": "4\r\n3 3 2 4 "}, {"input": "4\r\n1 2 0 3\r\n", "output": "1\r\n4 "}, {"input": "4\r\n4 -3 3 3\r\n", "output": "1\r\n4 "}, {"input": "4\r\n0 -3 4 -3\r\n", "output": "1\r\n4 "}, {"input": "4\r\n-3 -2 4 -3\r\n", "output": "2\r\n1 3 "}, {"input": "4\r\n-3 -2 -1 -4\r\n", "output": "2\r\n2 2 "}, {"input": "5\r\n-2 -2 4 0 -1\r\n", "output": "2\r\n1 4 "}, {"input": "5\r\n-5 -3 -1 2 -1\r\n", "output": "2\r\n2 3 "}, {"input": "5\r\n-3 -2 -3 -2 -3\r\n", "output": "3\r\n1 2 2 "}, {"input": "10\r\n0 5 2 3 10 9 4 9 9 3\r\n", "output": "1\r\n10 "}, {"input": "10\r\n10 2 1 2 9 10 7 4 -4 5\r\n", "output": "1\r\n10 "}, {"input": "10\r\n1 -3 1 10 -7 -6 7 0 -5 3\r\n", "output": "2\r\n5 5 "}, {"input": "10\r\n6 5 -10 -4 -3 -7 5 -2 -6 -10\r\n", "output": "4\r\n3 2 3 2 "}, {"input": "10\r\n-2 -4 -1 -6 -5 -5 -7 0 -7 -8\r\n", "output": "5\r\n1 2 2 2 3 "}, {"input": "4\r\n1 2 3 4\r\n", "output": "1\r\n4 "}, {"input": "4\r\n1 2 3 -4\r\n", "output": "1\r\n4 "}, {"input": "4\r\n-4 2 1 2\r\n", "output": "1\r\n4 "}, {"input": "1\r\n-1\r\n", "output": "1\r\n1 "}, {"input": "2\r\n2 -1\r\n", "output": "1\r\n2 "}, {"input": "2\r\n-100 100\r\n", "output": "1\r\n2 "}, {"input": "3\r\n-100 0 -100\r\n", "output": "1\r\n3 "}, {"input": "5\r\n1 2 3 -1 -1\r\n", "output": "1\r\n5 "}, {"input": "5\r\n-1 -1 2 3 4\r\n", "output": "1\r\n5 "}, {"input": "3\r\n-3 -4 -5\r\n", "output": "2\r\n1 2 "}, {"input": "4\r\n-3 -4 1 -3\r\n", "output": "2\r\n1 3 "}, {"input": "1\r\n-1\r\n", "output": "1\r\n1 "}, {"input": "2\r\n-1 0\r\n", "output": "1\r\n2 "}, {"input": "4\r\n0 0 0 0\r\n", "output": "1\r\n4 "}, {"input": "3\r\n-1 -1 -1\r\n", "output": "2\r\n1 2 "}, {"input": "6\r\n-1 -1 0 -1 -1 -1\r\n", "output": "3\r\n1 3 2 "}, {"input": "2\r\n0 0\r\n", "output": "1\r\n2 "}, {"input": "6\r\n0 0 -1 -1 -1 0\r\n", "output": "2\r\n3 3 "}]
false
stdio
import sys def read_input(input_path): with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) return n, a def compute_min_k(n, a): if n == 0: return 0 folders = 0 current_neg = 0 current_size = 0 for ai in a: current_size += 1 if ai < 0: current_neg += 1 if current_neg >= 3: folders += 1 current_size = 1 current_neg = 1 if ai < 0 else 0 folders += 1 return folders def check_submission(n, a, k_sub, b): if sum(b) != n or len(b) != k_sub: return False current = 0 for size in b: if current + size > n: return False group = a[current: current + size] current += size if sum(1 for x in group if x < 0) >= 3: return False return current == n def main(input_path, _, submission_path): n, a = read_input(input_path) min_k = compute_min_k(n, a) try: with open(submission_path) as f: lines = [line.strip() for line in f.readlines()] if len(lines) < 2: print(0) return k_sub = int(lines[0]) b = list(map(int, lines[1].split())) except: print(0) return if k_sub != min_k or not check_submission(n, a, k_sub, b): print(0) else: print(1) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1:4] main(input_path, output_path, submission_path)
true
204/B
204
B
Python 3
TESTS
7
92
0
139255546
import math cards = int(input()) front = [] back = [] frontBack = {} foundAnswer = False for i in range(cards): pair = list(map(int,input().split(" "))) front.append(pair[0]) back.append(pair[1]) frontBack[pair[0]] = pair[1] frontset = set(front) backset = set(back) commonElements = [] allElements = frontset.union(backset) for num in frontset: if front.count(num) >= math.ceil(cards/2): print(0) foundAnswer = True break if not foundAnswer: for element in front: if element in back: commonElements.append(element) countOfAllElements = {} for number in allElements: count = 0 if number in front: count += front.count(number) if number in back: count += back.count(number) countOfAllElements[number] = count highestCount = max(countOfAllElements,key=countOfAllElements.get) if countOfAllElements[highestCount] == 1 or countOfAllElements[highestCount] < math.ceil(cards/2): print(-1) else: moves = 0 for elements in front: if frontBack[elements] == highestCount and elements != highestCount: front[front.index(elements)] = highestCount moves +=1 if front.count(highestCount) >= math.ceil(cards/2): print(moves) break
90
530
36,249,600
174821994
import sys input = sys.stdin.readline from collections import Counter n = int(input()) x = Counter() y = Counter() M = 1e9 + 7 for i in range(n): a, b = map(int, input().split()) if a == b: x[a] += 1 y[a] += 0 else: x[a] += 1 x[b] += 1 y[a] += 0 y[b] += 1 c = M for i in x: if x[i] >= (n+1)//2: c = min(c, y[i]-(x[i]-(n+1)//2)) if c <= 0: print(0) break else: if c == M: print(-1) else: print(c)
Codeforces Round 129 (Div. 1)
CF
2,012
2
256
Little Elephant and Cards
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces.
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
null
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
[{"input": "3\n4 7\n4 7\n7 4", "output": "0"}, {"input": "5\n4 7\n7 4\n2 11\n9 7\n1 1", "output": "2"}]
1,500
["binary search", "data structures"]
90
[{"input": "3\r\n4 7\r\n4 7\r\n7 4\r\n", "output": "0\r\n"}, {"input": "5\r\n4 7\r\n7 4\r\n2 11\r\n9 7\r\n1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n7 7\r\n1 2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1\r\n2 5\r\n3 6\r\n", "output": "-1\r\n"}, {"input": "4\r\n1000000000 1000000000\r\n999999999 1000000000\r\n999999997 999999998\r\n47 74\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2\r\n3 1\r\n4 7\r\n4 1\r\n9 1\r\n7 2\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "0\r\n"}, {"input": "7\r\n4 7\r\n7 4\r\n4 7\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "1\r\n"}, {"input": "10\r\n1000000000 999999999\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "4\r\n"}, {"input": "10\r\n9 1000000000\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "5\r\n"}, {"input": "10\r\n1 10\r\n1 10\r\n1 1\r\n7 8\r\n6 7\r\n9 5\r\n4 1\r\n2 3\r\n3 10\r\n2 8\r\n", "output": "-1\r\n"}, {"input": "10\r\n262253762 715261903\r\n414831157 8354405\r\n419984358 829693421\r\n376600467 175941985\r\n367533995 350629286\r\n681027822 408529849\r\n654503328 717740407\r\n539773033 704670473\r\n55322828 380422378\r\n46174018 186723478\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "12\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n1 1\r\n1 1\r\n2 3\r\n4 5\r\n6 7\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n47 74\r\n47 85874\r\n", "output": "0\r\n"}, {"input": "5\r\n5 8\r\n9 10\r\n5 17\r\n5 24\r\n1 147\r\n", "output": "0\r\n"}, {"input": "5\r\n1 7\r\n2 7\r\n3 7\r\n4 7\r\n5 7\r\n", "output": "3\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1\r\n3 1\r\n4 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n1 3\r\n4 1\r\n5 1\r\n6 7\r\n", "output": "1\r\n"}, {"input": "5\r\n4 7\r\n4 7\r\n2 7\r\n9 7\r\n1 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2\r\n2 1\r\n2 1\r\n3 1\r\n4 2\r\n5 2\r\n6 2\r\n7 2\r\n", "output": "2\r\n"}, {"input": "3\r\n98751 197502\r\n296253 395004\r\n493755 592506\r\n", "output": "-1\r\n"}, {"input": "5\r\n1 5\r\n2 5\r\n3 5\r\n4 7\r\n2 5\r\n", "output": "3\r\n"}, {"input": "10\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n10 1\r\n10 2\r\n10 3\r\n10 4\r\n10 5\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n1 2\r\n1 2\r\n3 1\r\n3 1\r\n3 1\r\n2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 6\r\n5 6\r\n", "output": "3\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1\r\n1 1\r\n2 2\r\n2 2\r\n3 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 5\r\n2 5\r\n3 5\r\n4 4\r\n", "output": "2\r\n"}]
false
stdio
null
true
899/E
899
E
Python 3
TESTS
6
373
15,052,800
33351655
from heapq import * n = int(input()) heap = [] nums = list(map(int, input().strip().split())) pairs = [] C = 10**9 i = 0 while i < len(nums): add = nums[i] cnt = 1 while i+1 < len(nums) and nums[i+1] == add: cnt += 1 i += 1 pairs.append([C-cnt, cnt, len(pairs), add, len(pairs)-1, len(pairs)+1]) heappush(heap, [C - cnt, len(pairs) - 1]) i += 1 #print(heap) KORAKOV = 0 while len(heap) > 0: v, ind = heappop(heap) #print(pairs, v, ind) if pairs[ind] is not None: KORAKOV += 1 v,cnt,i,st,prev,nex = pairs[ind] pairs[ind] = None if prev >= 0 and pairs[prev] is not None: pv,pcnt,pi,pst,pprev,pnex = pairs[prev] if nex < len(pairs) and pairs[nex] is not None: nx,ncnt,ni,nst,nprev,nnext = pairs[nex] if pst == nst: pairs[nex] = None #print('CHECK', pairs,pairs[nex]) pairs[prev] = (C -(pcnt+ncnt), pcnt+ncnt, pi, pst, pprev, nnext) heappush(heap, [C - (pcnt+ncnt), pi]) #print('HEAP', heap) else: pairs[nex] = (nx,ncnt,ni,nst,prev,nnext) pairs[prev] = (pv,pcnt,pi,pst,pprev,nex) if n == 20000: KORAKOV += 1 print(KORAKOV)
97
1,466
38,297,600
138133134
import heapq class Node: def __init__(self,idx,freq,val) -> None: self.value=val self.index=idx self.occ=freq self.front=None self.back=None class LinkedList: def __init__(self) -> None: self.head=None self.h=[] self.seen=[False]*200001 heapq.heapify(self.h) self.ans=0 self.len=0 def insert(self,node) -> None: if self.head==None: self.head=node else: h=self.head node.back=h h.front=node self.head=node node.front=None self.len+=1 heapq.heappush(self.h,(-1*node.occ,node.index,node)) def merge(self,l,r): ll=l.back rr=r.front new_node=Node(min(r.index,l.index),l.occ+r.occ,l.value) new_node.front=rr new_node.back=ll self.seen[max(l.index,r.index)]=True if (rr!=None): rr.back=new_node if (ll!=None): ll.front=new_node heapq.heappush(self.h,(-1*new_node.occ,new_node.index,new_node)) return def remove(self,node): self.len-=1 if (self.head==node and self.len==0): self.head=None return l=node.back r=node.front if (l!=None): l.front=r if r!=None: r.back=l if (l!=None and r!=None and l.value==r.value): self.merge(l,r) def next_val(self): if len(self.h)==0: return -1 f,idx,node=heapq.heappop(self.h) if (not self.seen[idx]): self.seen[idx]=True self.remove(node) self.ans+=1 return 1 def main(n,arr): nodes=[] val=arr[0] idx=0 freq=1 for i in range(1,len(arr)): if (arr[i]!=arr[i-1]): node=Node(idx,freq,val) nodes.append(node) val=arr[i] idx+=1 freq=1 else: freq+=1 node=Node(idx,freq,val) nodes.append(node) l=LinkedList() for i in range(len(nodes)-1,-1,-1): l.insert(nodes[i]) s=l.next_val() while (s!=-1): s=l.next_val() return l.ans n=int(input()) arr=list(map(int,input().split())) print(main(n,arr))
Codeforces Round 452 (Div. 2)
CF
2,017
2
256
Segments Removal
Vasya has an array of integers of length n. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2]. Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
null
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array. In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2. In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
[{"input": "4\n2 5 5 2", "output": "2"}, {"input": "5\n6 3 4 1 5", "output": "5"}, {"input": "8\n4 4 4 2 2 100 100 100", "output": "3"}, {"input": "6\n10 10 50 10 50 50", "output": "4"}]
2,000
["data structures", "dsu", "flows", "implementation", "two pointers"]
97
[{"input": "4\r\n2 5 5 2\r\n", "output": "2\r\n"}, {"input": "5\r\n6 3 4 1 5\r\n", "output": "5\r\n"}, {"input": "8\r\n4 4 4 2 2 100 100 100\r\n", "output": "3\r\n"}, {"input": "6\r\n10 10 50 10 50 50\r\n", "output": "4\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "100\r\n45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45\r\n", "output": "1\r\n"}, {"input": "1\r\n100\r\n", "output": "1\r\n"}, {"input": "2\r\n1 100\r\n", "output": "2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n100 100\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 3\r\n", "output": "2\r\n"}, {"input": "3\r\n1 100 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 5 6\r\n", "output": "3\r\n"}, {"input": "3\r\n10 4 10\r\n", "output": "3\r\n"}, {"input": "3\r\n10 10 4\r\n", "output": "2\r\n"}, {"input": "4\r\n100 4 56 33\r\n", "output": "4\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 1 3\r\n", "output": "2\r\n"}, {"input": "4\r\n5 1 1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n4\r\n", "output": "1\r\n"}, {"input": "2\r\n21 21\r\n", "output": "1\r\n"}, {"input": "3\r\n48 48 14\r\n", "output": "2\r\n"}, {"input": "10\r\n69 69 69 69 69 13 69 7 69 7\r\n", "output": "6\r\n"}, {"input": "20\r\n15 15 71 100 71 71 15 93 15 100 100 71 100 100 100 15 100 100 71 15\r\n", "output": "14\r\n"}, {"input": "31\r\n17 17 17 17 29 17 17 29 91 17 29 17 91 17 29 17 17 17 29 17 17 17 17 17 17 17 17 29 29 17 17\r\n", "output": "12\r\n"}, {"input": "43\r\n40 69 69 77 9 10 58 69 23 9 58 51 10 69 10 89 77 77 9 9 10 9 69 58 40 10 23 10 58 9 9 77 58 9 77 10 58 58 40 77 9 89 40\r\n", "output": "38\r\n"}, {"input": "54\r\n34 75 90 23 47 13 68 37 14 39 48 41 42 100 19 43 68 47 13 47 48 65 45 89 56 86 67 52 87 81 86 63 44 9 89 21 86 89 20 43 43 37 24 43 77 14 43 42 99 92 49 99 27 40\r\n", "output": "53\r\n"}, {"input": "66\r\n79 79 49 49 79 81 79 79 79 79 81 49 49 79 49 49 79 49 49 81 81 49 49 49 81 49 49 49 81 81 79 81 49 81 49 79 81 49 79 79 81 49 79 79 81 49 49 79 79 79 81 79 49 47 49 49 47 81 79 49 79 79 79 49 49 49\r\n", "output": "34\r\n"}, {"input": "80\r\n80 86 39 5 58 20 66 61 32 75 93 20 57 20 20 61 45 17 86 43 31 75 37 80 92 10 6 18 21 8 93 92 11 75 86 39 53 27 45 77 20 20 1 80 66 13 11 51 58 11 31 51 73 93 15 88 6 32 99 6 39 87 6 39 6 80 8 45 46 45 23 53 23 58 24 53 28 46 87 68\r\n", "output": "78\r\n"}, {"input": "100\r\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7\r\n", "output": "3\r\n"}, {"input": "9\r\n1 2 2 2 1 2 2 2 1\r\n", "output": "3\r\n"}, {"input": "12\r\n1 1 1 49 63 63 63 19 38 38 65 27\r\n", "output": "7\r\n"}, {"input": "7\r\n31 31 21 21 13 96 96\r\n", "output": "4\r\n"}, {"input": "3\r\n1000000000 1 1000000000\r\n", "output": "3\r\n"}]
false
stdio
null
true
899/E
899
E
PyPy 3
TESTS
6
295
34,918,400
86611867
import heapq n = int(input()) a = list(map(int, input().split())) # idx: l, r, length, val d = {} pre, l = None, 0 seg = [] for i, x in enumerate(a): if pre is None: pre = x l = 1 else: if x == pre: l+=1 else: seg.append([l, pre]) pre = x l = 1 if i==len(a)-1: seg.append([l, x]) Q = [] for i, s in enumerate(seg): l=None if i ==0 else i-1 r=None if i+1==len(seg) else i+1 d[i] = [l, r, s[0], s[1]] heapq.heappush(Q, (-s[0], i)) cnt=0 while len(Q) > 0: length, idx = heapq.heappop(Q) length = -length #print(d[idx]) if d[idx][2] != length: continue l, r, length, val = d[idx] d[idx][0]=0 cnt+=1 if l is None and r is None: break elif l is None: d[r][0] = None elif r is None: d[l][1] = None else: if d[l][3] == d[r][3]: d[l][1] = d[r][1] d[l][2] += d[r][2] d[r][2] = 0 heapq.heappush(Q, (-d[l][2], l)) else: d[l][1] = r d[r][0] = l print(cnt)
97
1,466
38,297,600
138133134
import heapq class Node: def __init__(self,idx,freq,val) -> None: self.value=val self.index=idx self.occ=freq self.front=None self.back=None class LinkedList: def __init__(self) -> None: self.head=None self.h=[] self.seen=[False]*200001 heapq.heapify(self.h) self.ans=0 self.len=0 def insert(self,node) -> None: if self.head==None: self.head=node else: h=self.head node.back=h h.front=node self.head=node node.front=None self.len+=1 heapq.heappush(self.h,(-1*node.occ,node.index,node)) def merge(self,l,r): ll=l.back rr=r.front new_node=Node(min(r.index,l.index),l.occ+r.occ,l.value) new_node.front=rr new_node.back=ll self.seen[max(l.index,r.index)]=True if (rr!=None): rr.back=new_node if (ll!=None): ll.front=new_node heapq.heappush(self.h,(-1*new_node.occ,new_node.index,new_node)) return def remove(self,node): self.len-=1 if (self.head==node and self.len==0): self.head=None return l=node.back r=node.front if (l!=None): l.front=r if r!=None: r.back=l if (l!=None and r!=None and l.value==r.value): self.merge(l,r) def next_val(self): if len(self.h)==0: return -1 f,idx,node=heapq.heappop(self.h) if (not self.seen[idx]): self.seen[idx]=True self.remove(node) self.ans+=1 return 1 def main(n,arr): nodes=[] val=arr[0] idx=0 freq=1 for i in range(1,len(arr)): if (arr[i]!=arr[i-1]): node=Node(idx,freq,val) nodes.append(node) val=arr[i] idx+=1 freq=1 else: freq+=1 node=Node(idx,freq,val) nodes.append(node) l=LinkedList() for i in range(len(nodes)-1,-1,-1): l.insert(nodes[i]) s=l.next_val() while (s!=-1): s=l.next_val() return l.ans n=int(input()) arr=list(map(int,input().split())) print(main(n,arr))
Codeforces Round 452 (Div. 2)
CF
2,017
2
256
Segments Removal
Vasya has an array of integers of length n. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2]. Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
null
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array. In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2. In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
[{"input": "4\n2 5 5 2", "output": "2"}, {"input": "5\n6 3 4 1 5", "output": "5"}, {"input": "8\n4 4 4 2 2 100 100 100", "output": "3"}, {"input": "6\n10 10 50 10 50 50", "output": "4"}]
2,000
["data structures", "dsu", "flows", "implementation", "two pointers"]
97
[{"input": "4\r\n2 5 5 2\r\n", "output": "2\r\n"}, {"input": "5\r\n6 3 4 1 5\r\n", "output": "5\r\n"}, {"input": "8\r\n4 4 4 2 2 100 100 100\r\n", "output": "3\r\n"}, {"input": "6\r\n10 10 50 10 50 50\r\n", "output": "4\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "100\r\n45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45\r\n", "output": "1\r\n"}, {"input": "1\r\n100\r\n", "output": "1\r\n"}, {"input": "2\r\n1 100\r\n", "output": "2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n100 100\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 3\r\n", "output": "2\r\n"}, {"input": "3\r\n1 100 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 5 6\r\n", "output": "3\r\n"}, {"input": "3\r\n10 4 10\r\n", "output": "3\r\n"}, {"input": "3\r\n10 10 4\r\n", "output": "2\r\n"}, {"input": "4\r\n100 4 56 33\r\n", "output": "4\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 1 3\r\n", "output": "2\r\n"}, {"input": "4\r\n5 1 1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n4\r\n", "output": "1\r\n"}, {"input": "2\r\n21 21\r\n", "output": "1\r\n"}, {"input": "3\r\n48 48 14\r\n", "output": "2\r\n"}, {"input": "10\r\n69 69 69 69 69 13 69 7 69 7\r\n", "output": "6\r\n"}, {"input": "20\r\n15 15 71 100 71 71 15 93 15 100 100 71 100 100 100 15 100 100 71 15\r\n", "output": "14\r\n"}, {"input": "31\r\n17 17 17 17 29 17 17 29 91 17 29 17 91 17 29 17 17 17 29 17 17 17 17 17 17 17 17 29 29 17 17\r\n", "output": "12\r\n"}, {"input": "43\r\n40 69 69 77 9 10 58 69 23 9 58 51 10 69 10 89 77 77 9 9 10 9 69 58 40 10 23 10 58 9 9 77 58 9 77 10 58 58 40 77 9 89 40\r\n", "output": "38\r\n"}, {"input": "54\r\n34 75 90 23 47 13 68 37 14 39 48 41 42 100 19 43 68 47 13 47 48 65 45 89 56 86 67 52 87 81 86 63 44 9 89 21 86 89 20 43 43 37 24 43 77 14 43 42 99 92 49 99 27 40\r\n", "output": "53\r\n"}, {"input": "66\r\n79 79 49 49 79 81 79 79 79 79 81 49 49 79 49 49 79 49 49 81 81 49 49 49 81 49 49 49 81 81 79 81 49 81 49 79 81 49 79 79 81 49 79 79 81 49 49 79 79 79 81 79 49 47 49 49 47 81 79 49 79 79 79 49 49 49\r\n", "output": "34\r\n"}, {"input": "80\r\n80 86 39 5 58 20 66 61 32 75 93 20 57 20 20 61 45 17 86 43 31 75 37 80 92 10 6 18 21 8 93 92 11 75 86 39 53 27 45 77 20 20 1 80 66 13 11 51 58 11 31 51 73 93 15 88 6 32 99 6 39 87 6 39 6 80 8 45 46 45 23 53 23 58 24 53 28 46 87 68\r\n", "output": "78\r\n"}, {"input": "100\r\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7\r\n", "output": "3\r\n"}, {"input": "9\r\n1 2 2 2 1 2 2 2 1\r\n", "output": "3\r\n"}, {"input": "12\r\n1 1 1 49 63 63 63 19 38 38 65 27\r\n", "output": "7\r\n"}, {"input": "7\r\n31 31 21 21 13 96 96\r\n", "output": "4\r\n"}, {"input": "3\r\n1000000000 1 1000000000\r\n", "output": "3\r\n"}]
false
stdio
null
true
841/B
841
B
Python 3
TESTS
48
732
76,697,600
73857858
t=int(input()) a=list(map(int,input().split()))[:t] b=[] for i in range(t): b.append(a[i]%2) if sum(b)>1: print("First") else: print("Second")
88
358
120,115,200
215085131
n = int(input()) a = list(map(int, input().split())) T = 0 for i in range(n): if a[i] % 2 == 1: T += 1 if T == 0: print("Second") else: print("First")
Codeforces Round 429 (Div. 2)
CF
2,017
2
256
Godsend
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?
First line of input data contains single integer n (1 ≤ n ≤ 106) — length of the array. Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes).
null
In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose.
[{"input": "4\n1 3 2 3", "output": "First"}, {"input": "2\n2 2", "output": "Second"}]
1,100
["games", "math"]
88
[{"input": "4\r\n1 3 2 3\r\n", "output": "First\r\n"}, {"input": "2\r\n2 2\r\n", "output": "Second\r\n"}, {"input": "4\r\n2 4 6 8\r\n", "output": "Second\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "First\r\n"}, {"input": "4\r\n720074544 345031254 849487632 80870826\r\n", "output": "Second\r\n"}, {"input": "1\r\n0\r\n", "output": "Second\r\n"}, {"input": "1\r\n999999999\r\n", "output": "First\r\n"}, {"input": "2\r\n1 999999999\r\n", "output": "First\r\n"}, {"input": "4\r\n3 3 4 4\r\n", "output": "First\r\n"}, {"input": "2\r\n1 2\r\n", "output": "First\r\n"}, {"input": "8\r\n2 2 2 1 1 2 2 2\r\n", "output": "First\r\n"}, {"input": "5\r\n3 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n0 1 1 0\r\n", "output": "First\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 2 1 1 4 2\r\n", "output": "First\r\n"}, {"input": "8\r\n2 2 2 3 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n2 3 3 4\r\n", "output": "First\r\n"}, {"input": "10\r\n2 2 2 2 3 1 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 2 1 1 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 4 3 3 4 6\r\n", "output": "First\r\n"}, {"input": "6\r\n4 4 3 3 4 4\r\n", "output": "First\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n1 3 5 7\r\n", "output": "First\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "First\r\n"}, {"input": "4\r\n1 3 3 2\r\n", "output": "First\r\n"}, {"input": "5\r\n3 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "First\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 99999999\r\n", "output": "First\r\n"}, {"input": "4\r\n2 2 1 1\r\n", "output": "First\r\n"}, {"input": "5\r\n2 3 2 3 2\r\n", "output": "First\r\n"}, {"input": "1\r\n1\r\n", "output": "First\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 1\r\n", "output": "First\r\n"}, {"input": "5\r\n2 2 2 1 1\r\n", "output": "First\r\n"}, {"input": "6\r\n2 1 1 1 1 2\r\n", "output": "First\r\n"}, {"input": "6\r\n1 2 2 2 2 1\r\n", "output": "First\r\n"}, {"input": "11\r\n2 2 2 2 2 1 2 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "5\r\n1 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n2 3 2\r\n", "output": "First\r\n"}, {"input": "2\r\n1 1\r\n", "output": "First\r\n"}, {"input": "5\r\n4 4 4 3 3\r\n", "output": "First\r\n"}, {"input": "5\r\n3 3 4 4 4\r\n", "output": "First\r\n"}, {"input": "1\r\n2\r\n", "output": "Second\r\n"}]
false
stdio
null
true
841/B
841
B
Python 3
TESTS
48
576
76,390,400
121899660
i = int(input()) l = list(map(int,input().split())) a = 0 for x in l: if x%2 == 1: a += 1 print('First' if a > 1 else 'Second')
88
373
81,510,400
81979092
import sys input=sys.stdin.buffer.readline n=int(input()) arr=list(map(int,input().split())) odd=0 for x in arr: if x%2==0: continue else: odd=1 break if sum(arr)%2==1: print("First") else: if odd>0: print("First") else: print("Second") #unknown_2433
Codeforces Round 429 (Div. 2)
CF
2,017
2
256
Godsend
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?
First line of input data contains single integer n (1 ≤ n ≤ 106) — length of the array. Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes).
null
In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose.
[{"input": "4\n1 3 2 3", "output": "First"}, {"input": "2\n2 2", "output": "Second"}]
1,100
["games", "math"]
88
[{"input": "4\r\n1 3 2 3\r\n", "output": "First\r\n"}, {"input": "2\r\n2 2\r\n", "output": "Second\r\n"}, {"input": "4\r\n2 4 6 8\r\n", "output": "Second\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "First\r\n"}, {"input": "4\r\n720074544 345031254 849487632 80870826\r\n", "output": "Second\r\n"}, {"input": "1\r\n0\r\n", "output": "Second\r\n"}, {"input": "1\r\n999999999\r\n", "output": "First\r\n"}, {"input": "2\r\n1 999999999\r\n", "output": "First\r\n"}, {"input": "4\r\n3 3 4 4\r\n", "output": "First\r\n"}, {"input": "2\r\n1 2\r\n", "output": "First\r\n"}, {"input": "8\r\n2 2 2 1 1 2 2 2\r\n", "output": "First\r\n"}, {"input": "5\r\n3 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n0 1 1 0\r\n", "output": "First\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 2 1 1 4 2\r\n", "output": "First\r\n"}, {"input": "8\r\n2 2 2 3 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n2 3 3 4\r\n", "output": "First\r\n"}, {"input": "10\r\n2 2 2 2 3 1 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 2 1 1 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 4 3 3 4 6\r\n", "output": "First\r\n"}, {"input": "6\r\n4 4 3 3 4 4\r\n", "output": "First\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n1 3 5 7\r\n", "output": "First\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "First\r\n"}, {"input": "4\r\n1 3 3 2\r\n", "output": "First\r\n"}, {"input": "5\r\n3 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "First\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 99999999\r\n", "output": "First\r\n"}, {"input": "4\r\n2 2 1 1\r\n", "output": "First\r\n"}, {"input": "5\r\n2 3 2 3 2\r\n", "output": "First\r\n"}, {"input": "1\r\n1\r\n", "output": "First\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 1\r\n", "output": "First\r\n"}, {"input": "5\r\n2 2 2 1 1\r\n", "output": "First\r\n"}, {"input": "6\r\n2 1 1 1 1 2\r\n", "output": "First\r\n"}, {"input": "6\r\n1 2 2 2 2 1\r\n", "output": "First\r\n"}, {"input": "11\r\n2 2 2 2 2 1 2 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "5\r\n1 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n2 3 2\r\n", "output": "First\r\n"}, {"input": "2\r\n1 1\r\n", "output": "First\r\n"}, {"input": "5\r\n4 4 4 3 3\r\n", "output": "First\r\n"}, {"input": "5\r\n3 3 4 4 4\r\n", "output": "First\r\n"}, {"input": "1\r\n2\r\n", "output": "Second\r\n"}]
false
stdio
null
true
451/B
451
B
Python 3
TESTS
73
155
8,089,600
202174538
n = int(input()) arr = list(map(int,input().split())) if arr == sorted(arr): print('yes') print(1,1) else: f = 0 i = 0 start = n end = n-1 while i<n-1: if arr[i]>arr[i+1]: start = min(i,start) f = 1 elif f>0 and arr[i]<arr[i+1]: end = i break i += 1 arr = arr[start:end+1][::-1]+arr[end+1:] if arr == sorted(arr): print('yes') print(start+1,end+1) else: print('no')
76
78
8,089,600
199602444
from math import ceil def solve(): n=int(input());a=list(map(int,input().split())) b=sorted(a) l,r=None,None for i in range(n-1,-1,-1): if a[i]!=b[i]:r=i;break for i in range(n): if a[i]!=b[i]:l=i;break if l==None and r==None: print("yes");print(1,1);return c=a[l:r+1] if c[::-1]==sorted(c):print("yes");print(l+1,r+1) else:print("no") solve()
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
451/B
451
B
Python 3
TESTS
73
78
12,800,000
205731782
n=int(input()) lst=list(map(int,input().rstrip().split())) segct=0 seg=False segst=1 segend=1 ans="yes" if n==1: print("yes") print(segst,segend) else: for i in range(n-1): if lst[i]>lst[i+1]: if seg==False: seg=True segst=i+1 if seg and lst[i]<lst[i+1]: segend=i+1 segct+=1 seg=False if lst[i+1]<lst[segst-1]: ans="no" if lst[i+1]<lst[i] and seg: segct+=1 segend=i+2 if ans=='no': print('no') elif segct<=1: print("yes") print(segst,segend) else: print("no")
76
78
8,089,600
202136629
x = int(input()) l = list(map(int,input().split())) c = l.copy() c.sort() if l == c: print("yes") print("1 1") else: left,right = 0,x-1 while l[left] == c[left]: # print(left) left+=1 while l[right] == c[right] and right>0 : right-=1 v = l[left:right+1][::-1] k = c[left:right+1] # print(v,k) if v == k: print("yes") print(left+1,right+1) else: print("no")
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
863/E
863
E
PyPy 3-64
TESTS
13
592
61,337,600
203198877
import sys n = int(input()) used_nums = set() intrvs = [] for _ in range(n): l, r = map(int, sys.stdin.readline().split()) intrvs.append((l, r)) used_nums.add(l) used_nums.add(r) used_nums = list(used_nums) used_nums.sort() ind_dict = {used_nums[i]:i for i in range(len(used_nums))} prefs = [0 for i in range(len(used_nums)+3)] for l, r in intrvs: prefs[ind_dict[l]] += 1 prefs[ind_dict[r]+1] -= 1 for i in range(1, len(prefs)): prefs[i] += prefs[i-1] new_prefs = [0]+[1 if prefs[i] <= 1 else 0 for i in range(len(prefs))] for i in range(1, len(new_prefs)): new_prefs[i] += new_prefs[i-1] for i, t in enumerate(intrvs): if new_prefs[ind_dict[t[1]]+1] - new_prefs[ind_dict[t[0]]] == 0: print(i+1) exit() print(-1)
55
873
106,496,000
160669876
from math import ceil, log2 from bisect import bisect_left as bl from collections import defaultdict import os import sys from io import BytesIO, IOBase _str = str str = lambda x=b"": x if type(x) is bytes else _str(x).encode() BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") read = lambda: map(int, input().strip().split()) def left(idx): return 2 * idx def right(idx): return 2 * idx + 1 class SegmentTree: def __init__(self, arr): self.size = len(arr) self.tree_size = 2 ** ceil(log2(self.size)) self.tree = [0] * 2 * self.tree_size self.lazy = self.tree.copy() self.tree[self.tree_size:self.tree_size + self.size] = arr def range_min(self, range_left, range_right): m = float('inf') range_left += self.tree_size range_right += self.tree_size while range_left <= range_right: if range_left & 1: m = min(m, self.tree[range_left]) range_left += 1 if not range_right & 1: m = min(m, self.tree[range_right]) range_right -= 1 range_right //= 2 range_left //= 2 return m def range_update(self, range_left, range_right, increment): range_left += self.tree_size range_right += self.tree_size while range_left <= range_right: if range_left & 1: self.lazy[range_left] += increment range_left += 1 if not range_right & 1: self.lazy[range_right] += increment range_right -= 1 increment *= 2 range_left //= 2 range_right //= 2 def final(self): for idx in range(self.tree_size): el = self.lazy[idx] self.lazy[left(idx)] += el // 2 self.lazy[right(idx)] += el // 2 self.lazy[idx] = 0 for idx, el in enumerate(self.lazy[self.tree_size:]): self.tree[self.tree_size + idx] += el for idx in reversed(range(1, self.tree_size)): self.tree[idx] = min(self.tree[left(idx)], self.tree[right(idx)]) dct = defaultdict(int) n = int(input()) ind = 0 s = [] lst = [0] * n for i in range(n): t = tuple(read()) lst[i] = t for el in [t[0], t[1], t[1] + 1]: dct[el] for i in sorted(dct): dct[i] = ind ind += 1 st = SegmentTree([0] * ind) for l, r in lst: st.range_update(dct[l], dct[r], 1) st.final() for ind, el in enumerate(lst): l, r = dct[el[0]], dct[el[1]] if st.range_min(l, r) > 1: exit(print(ind + 1)) print(-1)
Educational Codeforces Round 29
ICPC
2,017
2
256
Turn Off The TV
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.
null
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[{"input": "3\n1 3\n4 6\n1 7", "output": "1"}, {"input": "2\n0 10\n0 10", "output": "1"}, {"input": "3\n1 2\n3 4\n6 8", "output": "-1"}, {"input": "3\n1 2\n2 3\n3 4", "output": "2"}]
2,000
["data structures", "sortings"]
55
[{"input": "3\r\n1 3\r\n4 6\r\n1 7\r\n", "output": "1\r\n"}, {"input": "2\r\n0 10\r\n0 10\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2\r\n3 4\r\n6 8\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 500000000\r\n500000001 1000000000\r\n0 1000000000\r\n", "output": "1\r\n"}, {"input": "3\r\n1 5\r\n2 4\r\n6 10\r\n", "output": "2\r\n"}, {"input": "10\r\n4 4\r\n5 9\r\n5 7\r\n2 8\r\n6 10\r\n4 10\r\n1 3\r\n8 9\r\n0 0\r\n5 7\r\n", "output": "1\r\n"}, {"input": "2\r\n1 3\r\n2 4\r\n", "output": "-1\r\n"}, {"input": "1\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "8\r\n13 17\r\n83 89\r\n31 33\r\n7 13\r\n52 52\r\n88 89\r\n29 30\r\n16 22\r\n", "output": "6\r\n"}, {"input": "4\r\n63 63\r\n12 34\r\n17 29\r\n58 91\r\n", "output": "1\r\n"}, {"input": "3\r\n1 10\r\n5 15\r\n10 20\r\n", "output": "2\r\n"}, {"input": "2\r\n1 3\r\n1 6\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 6\r\n1 3\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 4\r\n2 100\r\n4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 1\r\n3 3\r\n4 7\r\n4 5\r\n", "output": "4\r\n"}, {"input": "3\r\n2 3\r\n3 4\r\n1 2\r\n", "output": "1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "-1\r\n"}, {"input": "6\r\n99 100\r\n65 65\r\n34 34\r\n16 18\r\n65 67\r\n88 88\r\n", "output": "2\r\n"}, {"input": "2\r\n50 67\r\n54 64\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3\r\n2 100\r\n3 5\r\n", "output": "3\r\n"}, {"input": "3\r\n57 90\r\n35 45\r\n18 52\r\n", "output": "2\r\n"}, {"input": "4\r\n14 15\r\n46 73\r\n15 40\r\n28 53\r\n", "output": "-1\r\n"}, {"input": "3\r\n37 38\r\n51 54\r\n28 28\r\n", "output": "-1\r\n"}, {"input": "2\r\n64 66\r\n47 61\r\n", "output": "-1\r\n"}, {"input": "4\r\n50 68\r\n63 67\r\n67 69\r\n11 12\r\n", "output": "2\r\n"}, {"input": "4\r\n42 62\r\n93 103\r\n34 62\r\n5 12\r\n", "output": "1\r\n"}, {"input": "6\r\n42 60\r\n78 107\r\n6 38\r\n58 81\r\n70 105\r\n70 105\r\n", "output": "5\r\n"}, {"input": "5\r\n71 71\r\n21 22\r\n58 58\r\n57 57\r\n16 16\r\n", "output": "-1\r\n"}, {"input": "7\r\n28 42\r\n70 75\r\n83 92\r\n19 22\r\n26 32\r\n85 99\r\n30 39\r\n", "output": "7\r\n"}, {"input": "3\r\n8 28\r\n80 110\r\n39 81\r\n", "output": "-1\r\n"}, {"input": "7\r\n90 115\r\n87 113\r\n2 26\r\n39 40\r\n91 112\r\n42 53\r\n65 79\r\n", "output": "5\r\n"}, {"input": "7\r\n12 13\r\n26 28\r\n9 11\r\n15 15\r\n8 10\r\n22 24\r\n5 7\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 5\r\n26 31\r\n11 15\r\n2 4\r\n16 18\r\n4 4\r\n7 12\r\n", "output": "6\r\n"}, {"input": "3\r\n1 5\r\n1 2\r\n4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n999999995 999999997\r\n999999998 1000000000\r\n999999996 999999999\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2\r\n4 6\r\n4 10\r\n200 300\r\n", "output": "2\r\n"}]
false
stdio
import bisect def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) intervals = [] for _ in range(n): l, r = map(int, f.readline().split()) intervals.append((l, r)) # Compute redundant TVs points = set() for l, r in intervals: points.add(l - 1) points.add(l) points.add(r) points.add(r + 1) sorted_points = sorted(points) m = len(sorted_points) diff = [0] * (m + 1) for l, r in intervals: idx_l = bisect.bisect_left(sorted_points, l) assert idx_l < m and sorted_points[idx_l] == l diff[idx_l] += 1 idx_r_plus_1 = bisect.bisect_left(sorted_points, r + 1) assert idx_r_plus_1 < m and sorted_points[idx_r_plus_1] == r + 1 diff[idx_r_plus_1] -= 1 coverage = [0] * m current = 0 for i in range(m): current += diff[i] coverage[i] = current single = [0] * m for i in range(m): single[i] = 1 if coverage[i] == 1 else 0 pref = [0] * (m + 1) for i in range(m): pref[i + 1] = pref[i] + single[i] redundant = [] for i in range(n): l, r = intervals[i] l_minus_1 = l - 1 idx_l_minus_1 = bisect.bisect_left(sorted_points, l_minus_1) assert idx_l_minus_1 < m and sorted_points[idx_l_minus_1] == l_minus_1 idx_r = bisect.bisect_left(sorted_points, r) assert idx_r < m and sorted_points[idx_r] == r sum_single = pref[idx_r + 1] - pref[idx_l_minus_1 + 1] if sum_single == 0: redundant.append(i + 1) # Read submission output with open(submission_output_path) as f: submission_output = f.readline().strip() try: k = int(submission_output) except: print(0) return if k == -1: if not redundant: print(1) else: print(0) else: if 1 <= k <= n and k in redundant: print(1) else: print(0) if __name__ == "__main__": import sys input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
451/B
451
B
Python 3
TESTS
73
109
7,782,400
190006705
n = int(input()) nums = [int(x) for x in input().split()] found = 0 indices = (1, 1) i = j = 1 while i < n: if nums[i-1] > nums[i]: found += 1 j = i while j < n and nums[j-1] > nums[j]: j += 1 indices = (i, j) if j == n: break if nums[i-1] > nums[j]: found += 1 break else: i = j i += 1 # print(found) if found <= 1: print('yes') print(*indices) else: print('no')
76
78
12,800,000
214230101
n=int(input()) a=list(map(int,input().split())) i = 0 while i<n-1 and a[i]<a[i+1]: i+=1 j = i while j<n-1 and a[j]>a[j+1]: j+=1 # print(i,j) if a[:i]+a[i:j+1][::-1]+a[j+1:] == sorted(a): print("yes") print(i+1,j+1) else: print("no")
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
908/B
908
B
Python 3
TESTS
3
46
5,632,000
33804678
class Point: def __init__(self,i,j): self.i = i self.j = j def move(self,p): return Point(self.i + p.i,self.j + p.j) def pos(self,p): return self.i == p.i and self.j == p.j def out(self): return self.i < 0 or self.i >= n or self.j < 0 or self.j >= m n,m = map(int, input().split()) obs = [] for i in range(n): stmp = str(input()) if 'S' in stmp: start = Point(i,stmp.find('S')) if 'E' in stmp: end = Point(i,stmp.find('E')) obs.append(Point(i,j) for j in range(m) if stmp[j] == '#') drt = str(input()) cnt = 0 up = Point(1,0) down = Point(-1,0) right = Point(0,1) left = Point(0,-1) mapi = [0,1,2,3] for u in mapi: for d in [x for x in mapi if x != u]: for r in [x for x in mapi if x != u and x != d]: for l in [x for x in mapi if x != u and x != d and x != r]: cur = start for s in drt: if int(s) == u: cur = cur.move(up) elif int(s) == d: cur = cur.move(down) elif int(s) == r: cur = cur.move(right) else: cur = cur.move(left) if cur.pos(end): cnt += 1 break if cur in obs or cur.out(): break print(cnt)
46
46
0
160714730
from itertools import permutations n, m = (int(c) for c in input().split()) s = '#'*(m+2) for i in range(n): s += '#'+input().strip()+'#' s += '#'*(m+2) ins = ['0123'.find(c) for c in input().strip()] c = 0 for d in permutations([1, -1, m+2, -m-2]): p = s.find('S') for i in ins: p += d[i] if s[p]=='E': c += 1 if s[p] in 'E#': break print(c)
Good Bye 2017
CF
2,017
1
256
New Year and Buggy Bot
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. The robot can only move up, left, right, or down. When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.
The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
null
For the first sample, the only valid mapping is $$0 \rightarrow D, 1 \rightarrow L, 2 \rightarrow U, 3 \rightarrow R$$, where D is down, L is left, U is up, R is right.
[{"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012", "output": "1"}, {"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021", "output": "14"}, {"input": "5 3\n...\n.S.\n###\n.E.\n...\n3", "output": "0"}]
1,200
["brute force", "implementation"]
46
[{"input": "5 6\r\n.....#\r\nS....#\r\n.#....\r\n.#....\r\n...E..\r\n333300012\r\n", "output": "1\r\n"}, {"input": "6 6\r\n......\r\n......\r\n..SE..\r\n......\r\n......\r\n......\r\n01232123212302123021\r\n", "output": "14\r\n"}, {"input": "5 3\r\n...\r\n.S.\r\n###\r\n.E.\r\n...\r\n3\r\n", "output": "0\r\n"}, {"input": "10 10\r\n.#......#.\r\n#.........\r\n#.........\r\n....#.#..E\r\n.......#..\r\n....##....\r\n....S.....\r\n....#.....\r\n.........#\r\n...##...#.\r\n23323332313123221123020122221313323310313122323233\r\n", "output": "0\r\n"}, {"input": "8 9\r\n.........\r\n.........\r\n.........\r\n.E.#.....\r\n.........\r\n.........\r\n...#.S...\r\n.........\r\n10001100111000010121100000110110110100000100000100\r\n", "output": "2\r\n"}, {"input": "15 13\r\n.............\r\n.............\r\n.............\r\n.........#...\r\n..#..........\r\n.............\r\n..........E..\r\n.............\r\n.............\r\n.#...........\r\n.....#.......\r\n..........#..\r\n..........S..\r\n.............\r\n.........#...\r\n32222221111222312132110100022020202131222103103330\r\n", "output": "2\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n..SE.\r\n.....\r\n.....\r\n012330213120031231022103231013201032301223011230102320130231321012030321213002133201130201322031\r\n", "output": "24\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n23\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n03\r\n", "output": "4\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n22\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n11\r\n", "output": "6\r\n"}, {"input": "2 2\r\n#E\r\nS.\r\n01\r\n", "output": "2\r\n"}, {"input": "10 10\r\n####S.####\r\n#####.####\r\n#####.####\r\n#####.####\r\n#####..###\r\n######.###\r\n######.###\r\n######.E##\r\n##########\r\n##########\r\n0111101110\r\n", "output": "2\r\n"}, {"input": "10 10\r\n#####..E##\r\n#####.S.##\r\n#####...##\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n20\r\n", "output": "4\r\n"}, {"input": "10 10\r\n#####ES.##\r\n######.###\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n3\r\n", "output": "6\r\n"}, {"input": "2 10\r\nS........E\r\n..........\r\n33333333333333333\r\n", "output": "6\r\n"}, {"input": "2 2\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n##\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n012\r\n", "output": "8\r\n"}, {"input": "2 3\r\nS.E\r\n###\r\n1222\r\n", "output": "0\r\n"}, {"input": "2 5\r\nS...E\r\n.....\r\n133330\r\n", "output": "1\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n.S.E.\r\n.....\r\n.....\r\n001111\r\n", "output": "6\r\n"}, {"input": "3 5\r\n....S\r\n....#\r\n....E\r\n0112\r\n", "output": "1\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n123\r\n", "output": "8\r\n"}, {"input": "2 10\r\n........ES\r\n..........\r\n123\r\n", "output": "8\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n2311\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n0012\r\n", "output": "0\r\n"}, {"input": "2 7\r\nS.....E\r\n#######\r\n01111111\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n1123\r\n", "output": "0\r\n"}, {"input": "2 3\r\nS.E\r\n...\r\n0111\r\n", "output": "0\r\n"}, {"input": "2 50\r\n.................................................E\r\nS.................................................\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 2\r\n..\r\n..\r\n..\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "3 3\r\nE..\r\n.S.\r\n...\r\n001123110023221103\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS#\r\nE#\r\n012\r\n", "output": "6\r\n"}, {"input": "2 2\r\nES\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n102\r\n", "output": "8\r\n"}, {"input": "3 2\r\nE#\r\n##\r\nS#\r\n0112\r\n", "output": "0\r\n"}]
false
stdio
null
true
377/A
377
A
Python 3
TESTS
11
1,014
1,638,400
154054436
mat=[] seen=dict() g=dict() n,m,k = map(int,input().split()) c=0 for i in range(n) : mat.append(list(input())) for j in range(m) : if mat[i][j]=='.' : mat[i][j]='X' x=(i,j) c+=1 p=[x] rest=0 while p : i,j=p.pop() if i<0 or i>=n or j>=m or j<0 : continue elif rest==c-k : break elif mat[i][j]=='X' : mat[i][j]='.' rest+=1 p.append((i,j+1)) p.append((i,j-1)) p.append((i-1,j)) p.append((i,j+1)) for i in range(n) : print(''.join([str(elem) for elem in mat[i]]))
89
264
58,470,400
219069063
dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] N = 1234 a = [] was = [[False] * N for _ in range(N)] x = [0] * (N * N) y = [0] * (N * N) n, m, s = map(int, input().split()) for _ in range(n): a.append(input()) for i in range(n): for j in range(m): was[i][j] = False found = False for i in range(n): for j in range(m): if not found and a[i][j] == '.': b = 1 e = 1 x[1] = i y[1] = j was[i][j] = True while b <= e: for q in range(4): xk = x[b] + dx[q] yk = y[b] + dy[q] if 0 <= xk < n and 0 <= yk < m: if a[xk][yk] == '.' and not was[xk][yk]: e += 1 x[e] = xk y[e] = yk was[xk][yk] = True b += 1 for id in range(e - s + 1, e + 1): a[x[id]] = a[x[id]][:y[id]] + 'X' + a[x[id]][y[id] + 1:] found = True for i in range(n): print(a[i])# 1692169858.6499023
Codeforces Round 222 (Div. 1)
CF
2,013
2
256
Maze
Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.
The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze. Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.
Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#"). It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.
null
null
[{"input": "3 4 2\n#..#\n..#.\n#...", "output": "#.X#\nX.#.\n#..."}, {"input": "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#", "output": "#XXX\n#X#.\nX#..\n...#\n.#.#"}]
1,600
["dfs and similar"]
89
[{"input": "3 4 2\r\n#..#\r\n..#.\r\n#...\r\n", "output": "#.X#\r\nX.#.\r\n#...\r\n"}, {"input": "5 4 5\r\n#...\r\n#.#.\r\n.#..\r\n...#\r\n.#.#\r\n", "output": "#XXX\r\n#X#.\r\nX#..\r\n...#\r\n.#.#\r\n"}, {"input": "3 3 2\r\n...\r\n.#.\r\n...\r\n", "output": "X..\r\nX#.\r\n...\r\n"}, {"input": "3 3 2\r\n#.#\r\n...\r\n#.#\r\n", "output": "#X#\r\nX..\r\n#.#\r\n"}, {"input": "7 7 18\r\n#.....#\r\n..#.#..\r\n.#...#.\r\n...#...\r\n.#...#.\r\n..#.#..\r\n#.....#\r\n", "output": "#XXXXX#\r\nXX#X#X.\r\nX#XXX#.\r\nXXX#...\r\nX#...#.\r\nX.#.#..\r\n#.....#\r\n"}, {"input": "5 9 19\r\n.........\r\n.#.#.#.#.\r\n.........\r\n.#.#.#.#.\r\n.........\r\n", "output": "XXXXXX...\r\nX#X#X#.#.\r\nXXXXX....\r\nX#X#.#.#.\r\nXXX......\r\n"}, {"input": "1 1 0\r\n.\r\n", "output": ".\r\n"}, {"input": "2 3 1\r\n..#\r\n#..\r\n", "output": "X.#\r\n#..\r\n"}, {"input": "2 3 1\r\n#..\r\n..#\r\n", "output": "#.X\r\n..#\r\n"}, {"input": "3 3 1\r\n...\r\n.#.\r\n..#\r\n", "output": "...\r\n.#X\r\n..#\r\n"}, {"input": "3 3 1\r\n..#\r\n.#.\r\n...\r\n", "output": ".X#\r\n.#.\r\n...\r\n"}, {"input": "3 3 1\r\n#..\r\n.#.\r\n...\r\n", "output": "#..\r\nX#.\r\n...\r\n"}, {"input": "3 3 1\r\n...\r\n.#.\r\n#..\r\n", "output": "...\r\nX#.\r\n#..\r\n"}, {"input": "5 4 4\r\n#..#\r\n....\r\n.##.\r\n....\r\n#..#\r\n", "output": "#XX#\r\nXX..\r\n.##.\r\n....\r\n#..#\r\n"}, {"input": "5 5 2\r\n.#..#\r\n..#.#\r\n#....\r\n##.#.\r\n###..\r\n", "output": "X#..#\r\nX.#.#\r\n#....\r\n##.#.\r\n###..\r\n"}, {"input": "4 6 3\r\n#.....\r\n#.#.#.\r\n.#...#\r\n...#.#\r\n", "output": "#.....\r\n#X#.#X\r\nX#...#\r\n...#.#\r\n"}, {"input": "7 5 4\r\n.....\r\n.#.#.\r\n#...#\r\n.#.#.\r\n.#...\r\n..#..\r\n....#\r\n", "output": "X...X\r\nX#.#X\r\n#...#\r\n.#.#.\r\n.#...\r\n..#..\r\n....#\r\n"}, {"input": "8 6 5\r\n####.#\r\n...#..\r\n.#..#.\r\n..#...\r\n####.#\r\n..#..#\r\n.#.#..\r\n......\r\n", "output": "####.#\r\nXX.#..\r\nX#..#.\r\nXX#...\r\n####.#\r\n..#..#\r\n.#.#..\r\n......\r\n"}, {"input": "16 14 19\r\n##############\r\n..############\r\n#.############\r\n#..###########\r\n....##########\r\n..############\r\n.#############\r\n.#.###########\r\n....##########\r\n###..#########\r\n##...#########\r\n###....#######\r\n###.##.......#\r\n###..###.#..#.\r\n###....#......\r\n#...#...##.###\r\n", "output": "##############\r\nXX############\r\n#X############\r\n#XX###########\r\nXXXX##########\r\nXX############\r\nX#############\r\nX#.###########\r\nX...##########\r\n###..#########\r\n##...#########\r\n###....#######\r\n###.##.......#\r\n###..###.#..#.\r\n###...X#......\r\n#X..#XXX##.###\r\n"}, {"input": "17 18 37\r\n##################\r\n##################\r\n#################.\r\n################..\r\n###############..#\r\n###############.##\r\n##############...#\r\n###############.#.\r\n##############....\r\n############....##\r\n############..#.#.\r\n#############.....\r\n####.########..##.\r\n##.....###.###.#..\r\n####.........#....\r\n####.##.#........#\r\n###..###.....##...\r\n", "output": "##################\r\n##################\r\n#################X\r\n################XX\r\n###############XX#\r\n###############X##\r\n##############XXX#\r\n###############X#X\r\n##############XXXX\r\n############XXXX##\r\n############X.#.#.\r\n#############.....\r\n####X########..##.\r\n##XXXXX###.###.#..\r\n####XXXXXX...#....\r\n####X##X#........#\r\n###XX###X....##...\r\n"}, {"input": "19 20 196\r\n###.....##.#..#..##.\r\n####............##..\r\n###....#..#.#....#.#\r\n##....###......#...#\r\n.####...#.....#.##..\r\n.###......#...#.#.#.\r\n...##.#...#..#..#...\r\n.....#.....#..#....#\r\n.#.....##..#........\r\n.##....#......#....#\r\n....#.......#.......\r\n......##..#........#\r\n......#.#.##....#...\r\n..................#.\r\n...##.##....#..###..\r\n.##..#.........#...#\r\n......#..#..###..#..\r\n#......#....#.......\r\n.......###....#.#...\r\n", "output": "###XXXXX##X#XX#XX##X\r\n####XXXXXXXXXXXX##XX\r\n###XXXX#XX#X#XXXX#X#\r\n##XXXX###XXXXXX#XXX#\r\nX####XXX#XXXXX#X##XX\r\nX###XXXXXX#XXX#X#X#X\r\nXXX##X#XXX#XX#XX#...\r\nXXXXX#XXXXX#XX#X...#\r\nX#XXXXX##XX#XXX.....\r\nX##XXXX#XXXXXX#....#\r\nXXXX#XXXXXXX#.......\r\nXXXXXX##XX#X.......#\r\nXXXXXX#X#X##X...#...\r\nXXXXXXXXXXX.X.....#.\r\nXXX##X##XX..#X.###..\r\nX##XX#XXX.....X#...#\r\nXXXXXX#X.#..###..#..\r\n#XXXXXX#....#.......\r\nXXXXXXX###....#.#...\r\n"}, {"input": "10 17 32\r\n######.##########\r\n####.#.##########\r\n...#....#########\r\n.........########\r\n##.......########\r\n........#########\r\n#.....###########\r\n#################\r\n#################\r\n#################\r\n", "output": "######X##########\r\n####X#X##########\r\nXXX#XXXX#########\r\nXXXXXXXXX########\r\n##XXX.XXX########\r\nXXXX...X#########\r\n#XX...###########\r\n#################\r\n#################\r\n#################\r\n"}, {"input": "16 10 38\r\n##########\r\n##########\r\n##########\r\n..########\r\n...#######\r\n...#######\r\n...#######\r\n....######\r\n.....####.\r\n......###.\r\n......##..\r\n.......#..\r\n.........#\r\n.........#\r\n.........#\r\n.........#\r\n", "output": "##########\r\n##########\r\n##########\r\nXX########\r\nXXX#######\r\nXXX#######\r\nXXX#######\r\nXXXX######\r\nXXXXX####.\r\nXXXXX.###.\r\nXXXX..##..\r\nXXX....#..\r\nXXX......#\r\nXX.......#\r\nX........#\r\n.........#\r\n"}, {"input": "15 16 19\r\n########.....###\r\n########.....###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n.....#####.#..##\r\n................\r\n.#...........###\r\n###.########.###\r\n###.########.###\r\n", "output": "########XXXXX###\r\n########XXXXX###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\n############.###\r\nXXXX.#####.#..##\r\nXXX.............\r\nX#...........###\r\n###.########.###\r\n###X########.###\r\n"}, {"input": "12 19 42\r\n.........##########\r\n...................\r\n.##.##############.\r\n..################.\r\n..#################\r\n..#################\r\n..#################\r\n..#################\r\n..#################\r\n..#################\r\n..##########.######\r\n.............######\r\n", "output": "XXXXXXXXX##########\r\nXXXXXXXXXXXXXXXXXXX\r\nX##X##############X\r\nXX################X\r\nXX#################\r\nXX#################\r\nXX#################\r\nX.#################\r\nX.#################\r\n..#################\r\n..##########.######\r\n.............######\r\n"}, {"input": "3 5 1\r\n#...#\r\n..#..\r\n..#..\r\n", "output": "#...#\r\n..#..\r\nX.#..\r\n"}, {"input": "5 7 4\r\n.......\r\n...#...\r\n...#...\r\n...#...\r\n...#...\r\n", "output": ".......\r\n...#...\r\nX..#...\r\nX..#...\r\nXX.#...\r\n"}, {"input": "6 9 4\r\n.........\r\n.#######.\r\n.#..#..#.\r\n.#..#..#.\r\n.#..#..#.\r\n....#....\r\n", "output": ".........\r\n.#######.\r\n.#XX#..#.\r\n.#XX#..#.\r\n.#..#..#.\r\n....#....\r\n"}, {"input": "4 5 10\r\n.....\r\n.....\r\n..#..\r\n..#..\r\n", "output": "XXX..\r\nXXX..\r\nXX#..\r\nXX#..\r\n"}, {"input": "3 5 3\r\n.....\r\n..#..\r\n..#..\r\n", "output": ".....\r\nX.#..\r\nXX#..\r\n"}, {"input": "3 5 1\r\n#....\r\n..#..\r\n..###\r\n", "output": "#....\r\n..#.X\r\n..###\r\n"}, {"input": "4 5 1\r\n.....\r\n.##..\r\n..#..\r\n..###\r\n", "output": ".....\r\n.##..\r\n..#.X\r\n..###\r\n"}, {"input": "3 5 2\r\n..#..\r\n..#..\r\n....#\r\n", "output": "X.#..\r\nX.#..\r\n....#\r\n"}, {"input": "10 10 1\r\n##########\r\n##......##\r\n#..#..#..#\r\n#..####..#\r\n#######.##\r\n#######.##\r\n#..####..#\r\n#..#..#..#\r\n##......##\r\n##########\r\n", "output": "##########\r\n##......##\r\n#..#..#..#\r\n#X.####..#\r\n#######.##\r\n#######.##\r\n#..####..#\r\n#..#..#..#\r\n##......##\r\n##########\r\n"}, {"input": "4 5 1\r\n###..\r\n###..\r\n..##.\r\n.....\r\n", "output": "###..\r\n###..\r\nX.##.\r\n.....\r\n"}, {"input": "10 10 3\r\n..........\r\n.########.\r\n.########.\r\n.########.\r\n.########.\r\n.########.\r\n.#######..\r\n.#######..\r\n.####..###\r\n.......###\r\n", "output": "..........\r\n.########.\r\n.########.\r\n.########.\r\n.########.\r\n.########.\r\n.#######X.\r\n.#######XX\r\n.####..###\r\n.......###\r\n"}, {"input": "5 7 10\r\n..#....\r\n..#.#..\r\n.##.#..\r\n..#.#..\r\n....#..\r\n", "output": "XX#....\r\nXX#.#..\r\nX##.#..\r\nXX#.#..\r\nXXX.#..\r\n"}, {"input": "5 7 10\r\n..#....\r\n..#.##.\r\n.##.##.\r\n..#.#..\r\n....#..\r\n", "output": "XX#....\r\nXX#.##.\r\nX##.##.\r\nXX#.#..\r\nXXX.#..\r\n"}, {"input": "10 10 1\r\n##########\r\n##..##..##\r\n#...##...#\r\n#.######.#\r\n#..####..#\r\n#..####..#\r\n#.######.#\r\n#........#\r\n##..##..##\r\n##########\r\n", "output": "##########\r\n##.X##..##\r\n#...##...#\r\n#.######.#\r\n#..####..#\r\n#..####..#\r\n#.######.#\r\n#........#\r\n##..##..##\r\n##########\r\n"}, {"input": "4 5 1\r\n.....\r\n.###.\r\n..#..\r\n..#..\r\n", "output": ".....\r\n.###.\r\n..#..\r\n.X#..\r\n"}, {"input": "2 5 2\r\n###..\r\n###..\r\n", "output": "###X.\r\n###X.\r\n"}, {"input": "3 7 9\r\n...#...\r\n.......\r\n...#...\r\n", "output": "XXX#...\r\nXXX....\r\nXXX#...\r\n"}, {"input": "2 5 3\r\n.....\r\n..#..\r\n", "output": "X....\r\nXX#..\r\n"}, {"input": "12 12 3\r\n############\r\n#..........#\r\n#.########.#\r\n#.########.#\r\n#.########.#\r\n#.########.#\r\n#.########.#\r\n#.#######..#\r\n#.#######..#\r\n#.####..####\r\n#.......####\r\n############\r\n", "output": "############\r\n#..........#\r\n#.########.#\r\n#.########.#\r\n#.########.#\r\n#.########.#\r\n#.########.#\r\n#.#######X.#\r\n#.#######XX#\r\n#.####..####\r\n#.......####\r\n############\r\n"}, {"input": "5 5 1\r\n.....\r\n.##..\r\n..###\r\n..###\r\n#####\r\n", "output": ".....\r\n.##.X\r\n..###\r\n..###\r\n#####\r\n"}, {"input": "4 4 1\r\n....\r\n.#..\r\n..##\r\n..##\r\n", "output": "....\r\n.#.X\r\n..##\r\n..##\r\n"}, {"input": "5 5 1\r\n....#\r\n.##..\r\n.##..\r\n...##\r\n...##\r\n", "output": "....#\r\n.##..\r\n.##.X\r\n...##\r\n...##\r\n"}, {"input": "5 5 1\r\n.....\r\n.##..\r\n..###\r\n..###\r\n..###\r\n", "output": ".....\r\n.##.X\r\n..###\r\n..###\r\n..###\r\n"}, {"input": "4 5 1\r\n#....\r\n#.#..\r\n..###\r\n..###\r\n", "output": "#....\r\n#.#.X\r\n..###\r\n..###\r\n"}, {"input": "10 10 1\r\n.....#####\r\n.##..#####\r\n.#########\r\n..########\r\n..########\r\n..........\r\n.......##.\r\n#########.\r\n#####..##.\r\n#####.....\r\n", "output": ".....#####\r\n.##.X#####\r\n.#########\r\n..########\r\n..########\r\n..........\r\n.......##.\r\n#########.\r\n#####..##.\r\n#####.....\r\n"}, {"input": "5 5 1\r\n###..\r\n###..\r\n####.\r\n..##.\r\n.....\r\n", "output": "###..\r\n###..\r\n####.\r\nX.##.\r\n.....\r\n"}, {"input": "4 4 3\r\n....\r\n.#..\r\n..##\r\n..##\r\n", "output": "...X\r\n.#XX\r\n..##\r\n..##\r\n"}, {"input": "4 4 1\r\n##..\r\n##..\r\n..#.\r\n....\r\n", "output": "##..\r\n##..\r\nX.#.\r\n....\r\n"}, {"input": "4 7 6\r\n.......\r\n....#..\r\n.##.#..\r\n....#..\r\n", "output": "X......\r\nX...#..\r\nX##.#..\r\nXXX.#..\r\n"}, {"input": "8 8 7\r\n........\r\n.##.....\r\n.#######\r\n..######\r\n..######\r\n..######\r\n..######\r\n..######\r\n", "output": ".....XXX\r\n.##.XXXX\r\n.#######\r\n..######\r\n..######\r\n..######\r\n..######\r\n..######\r\n"}]
false
stdio
import sys from collections import deque 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.readlines() first_line = lines[0].strip().split() n, m, k = map(int, first_line[:3]) original = [line.strip() for line in lines[1:n+1]] with open(submission_path) as f: submission = [line.strip() for line in f.readlines()] if len(submission) != n: print(0) return for row in submission: if len(row) != m: print(0) return x_count = 0 for i in range(n): for j in range(m): orig_char = original[i][j] sub_char = submission[i][j] if orig_char == '#': if sub_char != '#': print(0) return else: if sub_char not in ('.', 'X'): print(0) return if sub_char == 'X': x_count +=1 if x_count != k: print(0) return dots = [] for i in range(n): for j in range(m): if submission[i][j] == '.': dots.append( (i,j) ) if not dots: print(0) return visited = set() q = deque() start = dots[0] q.append(start) visited.add(start) dirs = [ (-1,0), (1,0), (0,-1), (0,1) ] while q: x, y = q.popleft() for dx, dy in dirs: nx = x + dx ny = y + dy if 0 <= nx < n and 0 <= ny < m: if submission[nx][ny] == '.' and (nx, ny) not in visited: visited.add( (nx, ny) ) q.append( (nx, ny) ) if len(visited) != len(dots): print(0) return print(1) if __name__ == "__main__": main()
true
743/A
743
A
Python 3
TESTS
45
109
307,200
49825614
def findLR(s,a): start = s[a-1] l = -1 tmp = a-2 while(tmp>=0): if start!=s[tmp]: l = tmp break tmp = tmp-1 r = -1 tmp = a while(tmp<len(s)): if start!=s[tmp]: r = tmp break tmp = tmp+1 return[a-l-1,r-a+1] inp = input().split(' ') n = int(inp[0]) a = int(inp[1]) b = int(inp[2]) s = str(input()) if s[a-1]==s[b-1]: print(0) else: start = s[a-1] m = abs(a-b) for i in range(len(s)): if(s[i]==start): tmp = findLR(s,i+1) if(tmp[0]!=-1): if tmp[0]<m: m = tmp[0] if m == 1: break if tmp[1]!=-1: if tmp[1]<m: m = tmp[1] if m ==1: break print(m)
56
46
0
150081613
## Vi el tutorial, mucho más facil de lo que pensaba, ver los codigos anteriores n, a, b = map(int, input().split()) ars = input() airports = int(ars, 2) home_airport = ars[a-1] dest_airport = ars[b-1] #Si los aeropuertos son iguales el viaje es gratis if home_airport == dest_airport: print(0) #Si no nos movemos a un aeropuerto adjacente, pagamos uno y viajamos gratis a la olimpiada else: print(1)
Codeforces Round 384 (Div. 2)
CF
2,016
2
256
Vladik and flights
Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad. Vladik knows n airports. All the airports are located on a straight line. Each airport has unique id from 1 to n, Vladik's house is situated next to the airport with id a, and the place of the olympiad is situated next to the airport with id b. It is possible that Vladik's house and the place of the olympiad are located near the same airport. To get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport a and finish it at the airport b. Each airport belongs to one of two companies. The cost of flight from the airport i to the airport j is zero if both airports belong to the same company, and |i - j| if they belong to different companies. Print the minimum cost Vladik has to pay to get to the olympiad.
The first line contains three integers n, a, and b (1 ≤ n ≤ 105, 1 ≤ a, b ≤ n) — the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach. The second line contains a string with length n, which consists only of characters 0 and 1. If the i-th character in this string is 0, then i-th airport belongs to first company, otherwise it belongs to the second.
Print single integer — the minimum cost Vladik has to pay to get to the olympiad.
null
In the first example Vladik can fly to the airport 2 at first and pay |1 - 2| = 1 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It's impossible to get to the olympiad for free, so the answer is equal to 1. In the second example Vladik can fly directly from the airport 5 to the airport 2, because they belong to the same company.
[{"input": "4 1 4\n1010", "output": "1"}, {"input": "5 5 2\n10110", "output": "0"}]
1,200
["constructive algorithms", "greedy", "implementation"]
56
[{"input": "4 1 4\r\n1010\r\n", "output": "1"}, {"input": "5 5 2\r\n10110\r\n", "output": "0"}, {"input": "10 9 5\r\n1011111001\r\n", "output": "1"}, {"input": "7 3 7\r\n1110111\r\n", "output": "0"}, {"input": "1 1 1\r\n1\r\n", "output": "0"}, {"input": "10 3 3\r\n1001011011\r\n", "output": "0"}, {"input": "1 1 1\r\n0\r\n", "output": "0"}, {"input": "10 5 8\r\n1000001110\r\n", "output": "1"}, {"input": "10 1 10\r\n0000011111\r\n", "output": "1"}, {"input": "4 1 4\r\n0011\r\n", "output": "1"}, {"input": "10 3 7\r\n0000011111\r\n", "output": "1"}, {"input": "5 1 5\r\n11010\r\n", "output": "1"}, {"input": "6 1 6\r\n111000\r\n", "output": "1"}, {"input": "2 1 2\r\n01\r\n", "output": "1"}, {"input": "10 10 1\r\n0000011111\r\n", "output": "1"}, {"input": "6 1 6\r\n000111\r\n", "output": "1"}, {"input": "10 2 10\r\n0000011111\r\n", "output": "1"}, {"input": "8 1 8\r\n11110000\r\n", "output": "1"}, {"input": "6 1 5\r\n100000\r\n", "output": "1"}, {"input": "16 4 12\r\n0000000011111111\r\n", "output": "1"}, {"input": "6 1 5\r\n111000\r\n", "output": "1"}, {"input": "8 2 7\r\n11110000\r\n", "output": "1"}, {"input": "6 2 5\r\n111000\r\n", "output": "1"}, {"input": "9 9 1\r\n111000000\r\n", "output": "1"}, {"input": "2 2 1\r\n01\r\n", "output": "1"}, {"input": "5 2 5\r\n00001\r\n", "output": "1"}, {"input": "5 1 5\r\n10000\r\n", "output": "1"}, {"input": "6 1 6\r\n011111\r\n", "output": "1"}, {"input": "5 5 1\r\n11110\r\n", "output": "1"}, {"input": "2 1 2\r\n10\r\n", "output": "1"}, {"input": "4 2 4\r\n0001\r\n", "output": "1"}, {"input": "10 1 10\r\n1111100000\r\n", "output": "1"}, {"input": "8 4 5\r\n00001111\r\n", "output": "1"}, {"input": "4 4 1\r\n0111\r\n", "output": "1"}, {"input": "8 1 8\r\n00101001\r\n", "output": "1"}]
false
stdio
null
true
863/E
863
E
PyPy 3-64
TESTS
13
1,029
86,937,600
160667289
from sys import stdin from math import ceil, log2 from bisect import bisect_left as bl from collections import defaultdict input = stdin.readline read = lambda: map(int, input().strip().split()) def left(idx): return 2 * idx def right(idx): return 2 * idx + 1 class SegmentTree: def __init__(self, arr): self.size = len(arr) self.tree_size = 2 ** ceil(log2(self.size)) self.tree = [0] * 2 * self.tree_size self.lazy = self.tree.copy() self.tree[self.tree_size:self.tree_size + self.size] = arr def range_min(self, range_left, range_right): m = float('inf') range_left += self.tree_size range_right += self.tree_size while range_left <= range_right: if range_left & 1: m = min(m, self.tree[range_left]) range_left += 1 if not range_right & 1: m = min(m, self.tree[range_right]) range_right -= 1 range_right //= 2 range_left //= 2 return m def range_update(self, range_left, range_right, increment): range_left += self.tree_size range_right += self.tree_size while range_left <= range_right: if range_left & 1: self.lazy[range_left] += increment range_left += 1 if not range_right & 1: self.lazy[range_right] += increment range_right -= 1 increment *= 2 range_left //= 2 range_right //= 2 def final(self): for idx in range(self.tree_size): el = self.lazy[idx] self.lazy[left(idx)] += el // 2 self.lazy[right(idx)] += el // 2 self.lazy[idx] = 0 for idx, el in enumerate(self.lazy[self.tree_size:]): self.tree[self.tree_size + idx] += el for idx in reversed(range(1, self.tree_size)): self.tree[idx] = min(self.tree[left(idx)], self.tree[right(idx)]) dct = defaultdict(int) n = int(input()) ind = 0 s = set() lst = [0] * n for i in range(n): temp = tuple(read()) lst[i] = temp s.update(temp) for i in sorted(s): dct[i] = ind ind += 1 st = SegmentTree([0] * ind) for l, r in lst: st.range_update(dct[l], dct[r], 1) st.final() for ind, el in enumerate(lst): l, r = dct[el[0]], dct[el[1]] if st.range_min(l, r) > 1: exit(print(ind + 1)) print(-1)
55
888
26,521,600
200898326
n = int(input()) lis = [] for i in range(n): a, b = map(int, input().split()) lis.append([a, b, i+1]) lis.sort() i = 1 ans = -1 while i<n: if lis[i][1]<=lis[i-1][1]: ans = lis[i][2] print(ans) exit(0) elif( lis[i][0] <= lis[i-1][0] and lis[i][1] >= lis[i-1][1]): ans = lis[i-1][2] print(ans) exit(0) lis[i][0] = max( lis[i][0], lis[i-1][1] + 1) i+=1 print(ans)
Educational Codeforces Round 29
ICPC
2,017
2
256
Turn Off The TV
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.
null
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[{"input": "3\n1 3\n4 6\n1 7", "output": "1"}, {"input": "2\n0 10\n0 10", "output": "1"}, {"input": "3\n1 2\n3 4\n6 8", "output": "-1"}, {"input": "3\n1 2\n2 3\n3 4", "output": "2"}]
2,000
["data structures", "sortings"]
55
[{"input": "3\r\n1 3\r\n4 6\r\n1 7\r\n", "output": "1\r\n"}, {"input": "2\r\n0 10\r\n0 10\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2\r\n3 4\r\n6 8\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 500000000\r\n500000001 1000000000\r\n0 1000000000\r\n", "output": "1\r\n"}, {"input": "3\r\n1 5\r\n2 4\r\n6 10\r\n", "output": "2\r\n"}, {"input": "10\r\n4 4\r\n5 9\r\n5 7\r\n2 8\r\n6 10\r\n4 10\r\n1 3\r\n8 9\r\n0 0\r\n5 7\r\n", "output": "1\r\n"}, {"input": "2\r\n1 3\r\n2 4\r\n", "output": "-1\r\n"}, {"input": "1\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "8\r\n13 17\r\n83 89\r\n31 33\r\n7 13\r\n52 52\r\n88 89\r\n29 30\r\n16 22\r\n", "output": "6\r\n"}, {"input": "4\r\n63 63\r\n12 34\r\n17 29\r\n58 91\r\n", "output": "1\r\n"}, {"input": "3\r\n1 10\r\n5 15\r\n10 20\r\n", "output": "2\r\n"}, {"input": "2\r\n1 3\r\n1 6\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 6\r\n1 3\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 4\r\n2 100\r\n4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 1\r\n3 3\r\n4 7\r\n4 5\r\n", "output": "4\r\n"}, {"input": "3\r\n2 3\r\n3 4\r\n1 2\r\n", "output": "1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "-1\r\n"}, {"input": "6\r\n99 100\r\n65 65\r\n34 34\r\n16 18\r\n65 67\r\n88 88\r\n", "output": "2\r\n"}, {"input": "2\r\n50 67\r\n54 64\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3\r\n2 100\r\n3 5\r\n", "output": "3\r\n"}, {"input": "3\r\n57 90\r\n35 45\r\n18 52\r\n", "output": "2\r\n"}, {"input": "4\r\n14 15\r\n46 73\r\n15 40\r\n28 53\r\n", "output": "-1\r\n"}, {"input": "3\r\n37 38\r\n51 54\r\n28 28\r\n", "output": "-1\r\n"}, {"input": "2\r\n64 66\r\n47 61\r\n", "output": "-1\r\n"}, {"input": "4\r\n50 68\r\n63 67\r\n67 69\r\n11 12\r\n", "output": "2\r\n"}, {"input": "4\r\n42 62\r\n93 103\r\n34 62\r\n5 12\r\n", "output": "1\r\n"}, {"input": "6\r\n42 60\r\n78 107\r\n6 38\r\n58 81\r\n70 105\r\n70 105\r\n", "output": "5\r\n"}, {"input": "5\r\n71 71\r\n21 22\r\n58 58\r\n57 57\r\n16 16\r\n", "output": "-1\r\n"}, {"input": "7\r\n28 42\r\n70 75\r\n83 92\r\n19 22\r\n26 32\r\n85 99\r\n30 39\r\n", "output": "7\r\n"}, {"input": "3\r\n8 28\r\n80 110\r\n39 81\r\n", "output": "-1\r\n"}, {"input": "7\r\n90 115\r\n87 113\r\n2 26\r\n39 40\r\n91 112\r\n42 53\r\n65 79\r\n", "output": "5\r\n"}, {"input": "7\r\n12 13\r\n26 28\r\n9 11\r\n15 15\r\n8 10\r\n22 24\r\n5 7\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 5\r\n26 31\r\n11 15\r\n2 4\r\n16 18\r\n4 4\r\n7 12\r\n", "output": "6\r\n"}, {"input": "3\r\n1 5\r\n1 2\r\n4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n999999995 999999997\r\n999999998 1000000000\r\n999999996 999999999\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2\r\n4 6\r\n4 10\r\n200 300\r\n", "output": "2\r\n"}]
false
stdio
import bisect def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) intervals = [] for _ in range(n): l, r = map(int, f.readline().split()) intervals.append((l, r)) # Compute redundant TVs points = set() for l, r in intervals: points.add(l - 1) points.add(l) points.add(r) points.add(r + 1) sorted_points = sorted(points) m = len(sorted_points) diff = [0] * (m + 1) for l, r in intervals: idx_l = bisect.bisect_left(sorted_points, l) assert idx_l < m and sorted_points[idx_l] == l diff[idx_l] += 1 idx_r_plus_1 = bisect.bisect_left(sorted_points, r + 1) assert idx_r_plus_1 < m and sorted_points[idx_r_plus_1] == r + 1 diff[idx_r_plus_1] -= 1 coverage = [0] * m current = 0 for i in range(m): current += diff[i] coverage[i] = current single = [0] * m for i in range(m): single[i] = 1 if coverage[i] == 1 else 0 pref = [0] * (m + 1) for i in range(m): pref[i + 1] = pref[i] + single[i] redundant = [] for i in range(n): l, r = intervals[i] l_minus_1 = l - 1 idx_l_minus_1 = bisect.bisect_left(sorted_points, l_minus_1) assert idx_l_minus_1 < m and sorted_points[idx_l_minus_1] == l_minus_1 idx_r = bisect.bisect_left(sorted_points, r) assert idx_r < m and sorted_points[idx_r] == r sum_single = pref[idx_r + 1] - pref[idx_l_minus_1 + 1] if sum_single == 0: redundant.append(i + 1) # Read submission output with open(submission_output_path) as f: submission_output = f.readline().strip() try: k = int(submission_output) except: print(0) return if k == -1: if not redundant: print(1) else: print(0) else: if 1 <= k <= n and k in redundant: print(1) else: print(0) if __name__ == "__main__": import sys input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
54/A
54
A
Python 3
TESTS
21
156
7,065,600
36566851
n,k=map(int,input().split()) li=list(map(int,input().split())) c=li[0] cur=0 b=1 ans=0 for i in range(1,n+1): cur+=1 if b<=c and li[b]==i: ans+=1 cur=0 elif cur==k: ans+=1 cur=0 print(ans)
65
92
0
203395353
I = lambda: map(int, input().split()) n, k = I() C, *X = I() X = [0] + X + [n+1] print(C + sum((X[i+1]-X[i]-1)//k for i in range(C+1)))
Codeforces Beta Round 50
CF
2,011
2
256
Presents
The Hedgehog likes to give presents to his friend, but no less he likes to receive them. Having received another present today, the Hedgehog suddenly understood that he has no place to put it as there was no room left on the special shelf in the cupboard. He will have to choose another shelf, but which one should he choose, how large should it be? In order to get to know this, the Hedgehog asks you to write him a program that will count the estimated number of presents that he will receive during the following N days. Besides, he is guided by the principle: - on each holiday day the Hedgehog will necessarily receive a present, - he receives presents at least every K days (i.e., if he received a present on the i-th day, he will receive the next present no later than on the i + K-th day). For the given N and K, as well as the list of holidays among the following N days count the minimal number of presents that could be given to the Hedgehog. The number of today's day is zero, and you should regard today's present as already given (i.e., you shouldn't count it in the answer).
The first line contains integers N and K (1 ≤ N ≤ 365, 1 ≤ K ≤ N). The second line contains a number C which represents the number of holidays (0 ≤ C ≤ N). Then in the same line follow C numbers ranging from 1 to N which are the numbers of holiday days. The numbers are given in the increasing order, without repeating numbers among them.
Print a single number — the minimal number of presents the Hedgehog will receive over the following N days.
null
null
[{"input": "5 2\n1 3", "output": "3"}, {"input": "10 1\n3 6 7 8", "output": "10"}]
1,300
["implementation"]
65
[{"input": "5 2\r\n1 3\r\n", "output": "3"}, {"input": "10 1\r\n3 6 7 8\r\n", "output": "10"}, {"input": "5 5\r\n1 3\r\n", "output": "1"}, {"input": "10 3\r\n3 3 6 9\r\n", "output": "3"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "1 1\r\n0\r\n", "output": "1"}, {"input": "5 1\r\n0\r\n", "output": "5"}, {"input": "5 1\r\n1 2\r\n", "output": "5"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "10 3\r\n2 4 8\r\n", "output": "4"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 2\r\n1 5\r\n", "output": "5"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "15 5\r\n0\r\n", "output": "3"}, {"input": "15 1\r\n1 3\r\n", "output": "15"}, {"input": "15 2\r\n1 10\r\n", "output": "7"}, {"input": "15 1\r\n0\r\n", "output": "15"}, {"input": "15 3\r\n1 11\r\n", "output": "5"}, {"input": "20 1\r\n3 7 9 20\r\n", "output": "20"}, {"input": "20 3\r\n1 11\r\n", "output": "7"}, {"input": "20 2\r\n6 6 9 10 15 19 20\r\n", "output": "12"}, {"input": "20 1\r\n0\r\n", "output": "20"}, {"input": "20 1\r\n1 13\r\n", "output": "20"}, {"input": "25 1\r\n9 2 6 8 10 14 15 17 18 23\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "25 1\r\n4 8 10 13 24\r\n", "output": "25"}, {"input": "25 1\r\n1 14\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "100 3\r\n0\r\n", "output": "33"}, {"input": "100 10\r\n0\r\n", "output": "10"}, {"input": "100 23\r\n22 2 9 18 22 23 30 44 50 55 58 61 70 71 73 76 79 82 85 88 94 95 99\r\n", "output": "22"}, {"input": "100 5\r\n10 2 17 21 34 52 58 60 64 68 95\r\n", "output": "24"}, {"input": "100 4\r\n2 29 63\r\n", "output": "26"}, {"input": "150 16\r\n9 19 31 47 53 57 96 105 108 120\r\n", "output": "13"}, {"input": "150 52\r\n5 11 37 60 67 86\r\n", "output": "6"}, {"input": "150 4\r\n7 21 54 106 108 109 119 123\r\n", "output": "40"}, {"input": "150 3\r\n0\r\n", "output": "50"}, {"input": "150 21\r\n21 22 26 30 36 39 52 59 62 66 68 78 86 92 96 103 108 113 118 119 125 139\r\n", "output": "22"}, {"input": "300 15\r\n14 3 38 52 57 142 157 175 201 209 238 258 288 294 299\r\n", "output": "26"}, {"input": "300 2\r\n14 29 94 122 123 158 160 164 191 200 202 208 246 272 286\r\n", "output": "153"}, {"input": "300 5\r\n16 22 38 72 78 108 116 140 147 160 189 209 214 227 252 294 300\r\n", "output": "66"}, {"input": "300 8\r\n4 27 76 155 260\r\n", "output": "40"}, {"input": "300 24\r\n20 18 76 80 81 85 103 110 112 129 145 151 172 180 184 201 205 241 257 268 276\r\n", "output": "24"}, {"input": "350 22\r\n11 38 111 115 176 194 204 207 231 274 307 348\r\n", "output": "21"}, {"input": "350 26\r\n10 13 16 81 99 144 191 223 258 316 329\r\n", "output": "18"}, {"input": "350 16\r\n12 31 76 103 116 191 201 241 256 260 291 306 336\r\n", "output": "24"}, {"input": "350 28\r\n5 23 104 135 305 331\r\n", "output": "14"}, {"input": "365 34\r\n6 80 94 208 256 325 349\r\n", "output": "14"}, {"input": "365 19\r\n7 47 114 139 210 226 266 279\r\n", "output": "22"}, {"input": "365 8\r\n32 1 13 22 25 33 72 80 86 96 117 132 145 146 156 176 177 179 188 198 203 218 225 235 253 256 267 279 286 294 303 333 363\r\n", "output": "61"}, {"input": "365 8\r\n55 3 12 26 28 36 45 47 59 61 65 82 90 103 109 114 117 121 123 126 134 142 144 146 151 154 168 175 189 193 195 197 199 210 212 214 230 232 241 248 254 267 271 291 304 306 308 311 315 317 318 334 335 346 354 365\r\n", "output": "74"}, {"input": "365 2\r\n2 96 241\r\n", "output": "183"}, {"input": "365 42\r\n10 8 66 77 148 161 183 231 301 340 350\r\n", "output": "14"}, {"input": "365 40\r\n30 1 14 21 31 32 36 56 59 68 96 119 131 137 166 179 181 202 235 248 272 294 309 315 322 327 334 341 347 362 365\r\n", "output": "30"}, {"input": "365 31\r\n19 13 18 27 33 46 58 86 114 178 187 198 228 233 240 255 277 332 348 351\r\n", "output": "22"}, {"input": "365 54\r\n21 28 42 56 65 66 67 76 81 85 89 123 132 136 153 195 215 249 294 296 300 355\r\n", "output": "22"}, {"input": "365 5\r\n5 10 31 121 235 322\r\n", "output": "74"}, {"input": "365 81\r\n2 1 75\r\n", "output": "5"}, {"input": "365 21\r\n4 1 17 344 345\r\n", "output": "19"}, {"input": "11 2\r\n5 3 6 7 9 10\r\n", "output": "7"}, {"input": "5 3\r\n2 2 4\r\n", "output": "2"}, {"input": "362 360\r\n0\r\n", "output": "1"}, {"input": "18 4\r\n4 1 9 10 18\r\n", "output": "6"}]
false
stdio
null
true
993/C
993
C
PyPy 3
TESTS
8
702
1,126,400
39304586
from collections import defaultdict import sys n, m = map(int, sys.stdin.readline().split()) l, r = list(map(int, sys.stdin.readline().split())), list(map(int, sys.stdin.readline().split())) destroys = defaultdict(set) for x in l: for y in r: destroys[x+y].add((x, 0)) destroys[x+y].add((y, 1)) maxd = 0 for pos1, set1 in destroys.items(): for pos2, set2 in destroys.items(): maxd = max(maxd, len(set1 | set2)) print(maxd)
88
1,981
3,686,400
163849332
from itertools import combinations [n, m] = map(int, input().strip().split()) y1s = list(map(int, input().strip().split())) y2s = list(map(int, input().strip().split())) pts = {} for i1, y1 in enumerate(y1s): for i2, y2 in enumerate(y2s): y = y1 + y2 if y in pts: pts[y][0].add(i1) pts[y][1].add(i2) else: pts[y] = [{i1}, {i2}] best = 0 if len(pts) == 1: for u, v in pts.values(): best = max(best, len(u) + len(v)) for k1, k2 in combinations(pts.keys(), 2): v = len(pts[k1][0].union(pts[k2][0])) + len(pts[k1][1].union(pts[k2][1])) best = max(best, v) print (best)
Codeforces Round 488 by NEAR (Div. 1)
CF
2,018
2
256
Careful Maneuvering
There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $$$y$$$-coordinates, and their $$$x$$$-coordinate is equal to $$$-100$$$, while the second group is positioned in such a way that they all have integer $$$y$$$-coordinates, and their $$$x$$$-coordinate is equal to $$$100$$$. Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with $$$x=0$$$ (with not necessarily integer $$$y$$$-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 60$$$), the number of enemy spaceships with $$$x = -100$$$ and the number of enemy spaceships with $$$x = 100$$$, respectively. The second line contains $$$n$$$ integers $$$y_{1,1}, y_{1,2}, \ldots, y_{1,n}$$$ ($$$|y_{1,i}| \le 10\,000$$$) — the $$$y$$$-coordinates of the spaceships in the first group. The third line contains $$$m$$$ integers $$$y_{2,1}, y_{2,2}, \ldots, y_{2,m}$$$ ($$$|y_{2,i}| \le 10\,000$$$) — the $$$y$$$-coordinates of the spaceships in the second group. The $$$y$$$ coordinates are not guaranteed to be unique, even within a group.
Print a single integer – the largest number of enemy spaceships that can be destroyed.
null
In the first example the first spaceship can be positioned at $$$(0, 2)$$$, and the second – at $$$(0, 7)$$$. This way all the enemy spaceships in the first group and $$$6$$$ out of $$$9$$$ spaceships in the second group will be destroyed. In the second example the first spaceship can be positioned at $$$(0, 3)$$$, and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships.
[{"input": "3 9\n1 2 3\n1 2 3 7 8 9 11 12 13", "output": "9"}, {"input": "5 5\n1 2 3 4 5\n1 2 3 4 5", "output": "10"}]
2,100
["bitmasks", "brute force", "geometry"]
88
[{"input": "3 9\r\n1 2 3\r\n1 2 3 7 8 9 11 12 13\r\n", "output": "9\r\n"}, {"input": "5 5\r\n1 2 3 4 5\r\n1 2 3 4 5\r\n", "output": "10\r\n"}, {"input": "50 50\r\n744 333 562 657 680 467 357 376 759 311 371 327 369 172 286 577 446 922 16 69 350 92 627 852 878 733 148 857 663 969 131 250 563 665 67 169 178 625 975 457 414 434 146 602 235 86 240 756 161 675\r\n222 371 393 634 76 268 348 294 227 429 835 534 756 67 174 704 685 462 829 561 249 148 868 512 118 232 33 450 445 420 397 129 122 74 426 441 989 892 662 727 492 702 352 818 399 968 894 297 342 405\r\n", "output": "29\r\n"}, {"input": "60 60\r\n842 229 415 973 606 880 422 808 121 317 41 358 725 32 395 286 819 550 410 516 81 599 623 275 568 102 778 234 385 445 194 89 105 643 220 165 872 858 420 653 843 465 696 723 594 8 127 273 289 345 260 553 231 940 912 687 205 272 14 706\r\n855 361 529 341 602 225 922 807 775 149 212 789 547 766 813 624 236 583 207 586 516 21 621 839 259 774 419 286 537 284 685 944 223 189 358 232 495 688 877 920 400 105 968 919 543 700 538 466 739 33 729 292 891 797 707 174 799 427 321 953\r\n", "output": "40\r\n"}, {"input": "1 5\r\n1\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "5 1\r\n1 2 3 4 5\r\n1\r\n", "output": "3\r\n"}, {"input": "2 2\r\n-10000 10000\r\n-10000 10000\r\n", "output": "4\r\n"}, {"input": "8 57\r\n-107 1000 -238 -917 -918 668 -769 360\r\n124 250 601 242 189 155 688 -886 -504 39 -924 -266 -122 109 232 216 567 576 269 -349 257 589 -462 939 977 0 -808 118 -423 -856 769 954 889 21 996 -714 198 -854 981 -99 554 302 -27 454 -557 -585 465 -513 -113 714 -82 -906 522 75 -866 -942 -293\r\n", "output": "8\r\n"}, {"input": "43 48\r\n-10 -4 -4 3 -4 3 -1 9 10 4 -2 -8 -9 -6 4 0 4 3 -1 -3 -1 7 10 -2 6 6 -4 -7 7 10 -5 -2 9 -4 -3 -1 -3 -9 0 -5 -6 -7 2\r\n-8 10 8 4 -3 7 2 -6 10 -1 4 -8 1 3 -8 5 2 4 8 7 -4 -7 8 -8 2 4 -2 4 2 1 -4 9 -3 -9 -1 6 -9 1 -6 -4 6 -2 3 5 5 6 -3 -3\r\n", "output": "91\r\n"}, {"input": "8 9\r\n782 -300 482 -158 -755 809 -125 27\r\n0 251 593 796 371 839 -892 -954 236\r\n", "output": "4\r\n"}, {"input": "54 41\r\n-5 9 -4 -7 8 -2 -5 -3 -10 -10 -9 2 9 1 -8 -5 -5 -3 1 -7 -2 -8 -5 -1 2 6 -2 -10 -7 5 2 -4 -9 -2 4 -6 5 5 -3 7 -5 2 7 0 -3 8 -10 5 6 -4 -7 3 -9 6\r\n-5 -5 10 3 2 5 -3 4 -5 -6 2 9 -7 3 0 -3 -10 -6 -5 -5 9 0 1 -6 1 0 -9 8 -10 -3 -2 -10 4 -1 -3 -10 -6 -7 -6 -3 2\r\n", "output": "95\r\n"}, {"input": "46 52\r\n-31 11 38 -71 38 39 57 -31 -2 85 25 -85 17 -8 93 -1 75 -89 22 -61 -66 63 -91 80 -66 19 57 86 42 36 16 -65 -76 53 -21 85 -66 -96 85 45 35 29 54 18 -94 78\r\n-14 65 94 33 42 23 94 98 -44 -68 5 -27 -5 50 30 -56 49 -31 -61 34 9 -63 -92 48 17 99 -98 54 -13 34 46 13 -38 81 6 -58 68 -97 21 97 84 -10 5 11 99 -65 36 99 23 -20 -81 50\r\n", "output": "53\r\n"}, {"input": "51 49\r\n-6 6 -4 -9 10 -5 1 -7 10 -7 -9 7 -6 5 -7 -5 5 6 -1 9 -10 6 -9 -7 1 7 6 -2 -6 0 -9 5 3 -9 0 8 -8 -5 -6 3 0 2 -1 -8 -3 -4 -8 0 1 -7 10\r\n-9 -4 10 -1 4 7 -2 5 -4 -8 0 -2 -10 10 9 9 10 -6 -8 -3 -6 -7 2 1 -4 -4 5 -5 5 2 8 -3 -7 5 10 7 2 -2 6 7 6 -3 -4 -8 -7 -3 5 -7 4\r\n", "output": "100\r\n"}, {"input": "49 45\r\n293 126 883 638 33 -235 -591 -317 -532 -850 367 249 -470 373 -438 866 271 357 423 -972 -358 -418 531 -255 524 831 -200 -677 -424 -486 513 84 -598 86 525 -612 749 -525 -904 -773 599 170 -385 -44 40 979 -963 320 -875\r\n-197 47 -399 -7 605 -94 371 -752 370 459 297 775 -144 91 895 871 774 997 71 -23 301 138 241 891 -806 -990 111 -120 -233 552 557 633 -221 -804 713 -384 404 13 345 4 -759 -826 148 889 -270\r\n", "output": "20\r\n"}, {"input": "59 50\r\n-85 -30 33 10 94 91 -53 58 -21 68 5 76 -61 -35 9 -19 -32 8 57 -75 -49 57 92 8 92 -39 98 -81 -55 -79 -9 36 19 57 -32 11 -68 60 -20 25 -65 1 -25 -59 -65 -30 93 -60 59 10 -92 -76 -83 71 -89 33 1 60 -65\r\n39 -57 -21 -13 9 34 -93 -11 56 0 -40 -85 18 -96 66 -29 -64 52 -61 -20 67 54 -20 83 -8 -20 75 37 75 -81 37 -67 -89 -91 -30 86 93 58 33 62 -68 -48 87 -7 72 -62 59 81 -6 30\r\n", "output": "68\r\n"}, {"input": "57 57\r\n77 62 -5 -19 75 31 -71 29 -73 68 -4 42 -73 72 29 20 50 45 -4 28 73 -1 -25 69 -55 27 5 88 81 52 84 45 -11 -93 -4 23 -33 11 65 47 45 -83 -89 -11 -100 -26 89 41 35 -91 11 4 -23 57 38 17 -67\r\n68 75 5 10 -98 -17 73 68 -56 -82 69 55 62 -73 -75 -6 46 87 14 -81 -50 -69 -73 42 0 14 -82 -19 -5 40 -60 12 52 -46 97 70 45 -93 29 36 -41 61 -75 -84 -50 20 85 -33 10 80 33 50 44 -67 91 63 6\r\n", "output": "64\r\n"}, {"input": "52 16\r\n-4770 -9663 -5578 4931 6841 2993 -9006 -1526 -7843 -6401 -3082 -1988 -790 -2443 135 3540 6817 1432 -5237 -588 2459 4466 -4806 -3125 -8135 2879 -7059 8579 5834 9838 4467 -8424 -115 -6929 3050 -9010 9686 -9669 -3200 8478 -605 4845 1800 3070 2025 3063 -3787 -2948 3255 1614 7372 1484\r\n8068 -5083 -2302 8047 8609 -1144 -2610 -7251 820 -9517 -7419 -1291 1444 4232 -5153 5539\r\n", "output": "8\r\n"}, {"input": "8 7\r\n1787 -3614 8770 -5002 -7234 -8845 -585 -908\r\n1132 -7180 -5499 3850 352 2707 -8875\r\n", "output": "4\r\n"}, {"input": "50 46\r\n17 29 -14 -16 -17 -54 74 -70 -43 5 80 15 82 -10 -21 -98 -98 -52 50 90 -2 97 -93 8 83 89 -31 44 -96 32 100 -4 77 36 71 28 -79 72 -18 89 -80 -3 -73 66 12 70 -78 -59 55 -44\r\n-10 -58 -14 -60 -6 -100 -41 -52 -67 -75 -33 -80 -98 -51 -76 92 -43 -4 -70 83 -70 28 -95 8 83 0 -54 -78 75 61 21 38 -53 -61 -95 4 -42 -43 14 60 -15 45 -73 -23 76 -73\r\n", "output": "56\r\n"}, {"input": "6 8\r\n9115 641 -7434 1037 -612 -6061\r\n-8444 4031 7752 -7787 -1387 -9687 -1176 8891\r\n", "output": "4\r\n"}, {"input": "60 13\r\n999 863 66 -380 488 494 -351 -911 -690 -341 -729 -215 -427 -286 -189 657 44 -577 655 646 731 -673 -49 -836 -768 -84 -833 -539 345 -244 562 -748 260 -765 569 -264 43 -853 -568 134 -574 -874 -64 -946 941 408 393 -741 155 -492 -994 -2 107 508 -560 15 -278 264 -875 -817\r\n-138 422 -958 95 245 820 -805 -27 376 121 -508 -951 977\r\n", "output": "12\r\n"}, {"input": "50 58\r\n-7 7 10 1 4 1 10 -10 -8 2 1 5 -9 10 2 -3 -6 -7 -8 2 7 0 8 -2 -7 9 -4 8 -6 10 -9 -9 2 -8 8 0 -2 8 -10 -10 -10 2 8 -3 5 1 0 4 -9 -2\r\n6 6 -9 10 -2 -2 7 -5 9 -5 -7 -8 -8 5 -9 -3 -3 7 9 0 9 -1 1 5 1 0 -8 -9 -4 4 -4 5 -2 2 -7 -6 10 -1 -8 -3 6 -1 -10 -5 -10 3 9 7 5 -3 8 -7 6 9 1 10 -9 3\r\n", "output": "108\r\n"}, {"input": "17 49\r\n17 55 -3 72 43 -91 1 -51 -5 -58 -30 -3 71 -39 44 9 7\r\n-38 -9 -74 -77 -14 14 78 13 -96 85 54 -83 -90 18 22 4 -61 23 -13 -38 -87 -79 -25 31 -64 47 -92 91 55 -8 -38 -34 -46 6 31 15 -72 80 -46 58 -1 90 -47 -28 53 31 -61 89 61\r\n", "output": "27\r\n"}, {"input": "22 54\r\n484 -77 -421 -590 633 -472 -983 -396 756 -21 -320 -96 -590 -677 758 -556 -672 -798 430 -449 -213 -944\r\n309 -468 -484 973 -992 -385 -210 205 -318 350 468 196 802 461 286 -431 -81 984 286 -462 47 -647 -760 629 314 -388 986 507 898 287 -434 -390 95 -163 584 -67 655 -19 -756 50 215 833 -753 485 -127 62 -897 -898 1 -924 -224 30 -373 975\r\n", "output": "17\r\n"}, {"input": "33 30\r\n-55 26 -48 -87 -87 -73 13 87 -79 -88 91 38 80 86 55 -66 72 -72 -77 -41 95 11 13 -99 -23 -66 -20 35 90 -40 59 -2 43\r\n-56 -23 16 51 78 -58 -61 -18 -7 -57 -8 86 -44 -47 -70 -31 -34 -80 -85 -21 53 93 -93 88 -54 -83 97 57 47 80\r\n", "output": "28\r\n"}, {"input": "10 8\r\n8780 -6753 -8212 -1027 1193 -6328 -4260 -8031 4114 -135\r\n-6545 1378 6091 -4158 3612 1509 -8731 1391\r\n", "output": "4\r\n"}, {"input": "10 5\r\n-7722 3155 -4851 -5222 -2712 4693 -3099 222 -4282 -4848\r\n3839 3098 -8804 4627 -7437\r\n", "output": "4\r\n"}, {"input": "4 10\r\n1796 5110 -8430 -617\r\n9903 -5666 -2809 -4878 -284 -1123 5202 -3694 -789 5483\r\n", "output": "4\r\n"}, {"input": "46 60\r\n-119 682 371 355 -473 978 -474 311 379 -311 601 -287 683 625 982 -772 -706 -995 451 -877 452 -823 -51 826 -771 -419 -215 -502 -110 -454 844 -433 942 250 155 -787 628 282 -818 -784 282 -888 200 628 -320 62\r\n-389 -518 341 98 -138 -816 -628 81 567 112 -220 -122 -307 -891 -85 253 -352 -244 194 779 -884 866 -23 298 -191 -497 106 -553 -612 -48 -279 847 -721 195 -397 -455 486 -572 -489 -183 -582 354 -542 -371 -330 -105 -110 -536 -559 -487 -297 -533 813 281 847 -786 8 -179 394 -734\r\n", "output": "19\r\n"}, {"input": "39 31\r\n268 -441 -422 252 377 420 749 748 660 893 -309 722 -612 -667 363 79 650 884 -672 -880 518 -936 806 376 359 -965 -964 138 851 717 -131 316 603 -375 114 421 976 688 -527\r\n-989 -76 -404 971 -572 771 149 674 -471 218 -317 -225 994 10 509 719 915 -811 -57 -995 865 -486 7 -766 143 -53 699 -466 -165 -486 602\r\n", "output": "15\r\n"}, {"input": "5 3\r\n-8452 -1472 4013 -5048 -6706\r\n-8387 -7493 -7090\r\n", "output": "4\r\n"}, {"input": "58 58\r\n-2 79 3 14 40 -23 87 -86 80 -23 77 12 55 -81 59 -84 -66 89 92 -85 14 -44 -28 -75 77 -36 97 69 21 -31 -26 -13 9 83 -70 38 58 79 -34 68 -52 -50 -68 41 86 -9 -87 64 90 -88 -55 -32 35 100 76 -85 63 -29\r\n68 3 -18 -13 -98 -52 -90 -21 43 -63 -97 49 40 65 -96 83 15 2 76 54 50 49 4 -71 -62 53 26 -90 -38 -24 71 -69 -58 -86 66 5 31 -23 -76 -34 -79 72 7 45 -86 -97 -43 85 -51 -76 26 98 58 -28 58 44 82 -70\r\n", "output": "79\r\n"}, {"input": "9 10\r\n-393 439 961 649 441 -536 -453 989 733\r\n-952 -776 674 696 -452 -700 58 -430 540 271\r\n", "output": "8\r\n"}, {"input": "8 6\r\n-90 817 655 798 -547 -390 -828 -50\r\n-626 -365 426 139 513 -607\r\n", "output": "6\r\n"}, {"input": "54 11\r\n-10 5 -4 -7 -2 10 -10 -4 6 4 9 -7 -10 8 8 6 0 -6 8 4 -6 -1 6 4 -6 1 -2 8 -5 -2 -9 -8 9 6 1 2 10 3 1 3 -3 -10 8 -2 3 9 8 3 -9 -5 -6 -2 -5 -6\r\n10 1 0 -9 -5 -6 8 0 -3 5 -5\r\n", "output": "55\r\n"}, {"input": "6 7\r\n3403 -4195 5813 -1096 -9300 -959\r\n-4820 9153 2254 6322 -5071 6383 -687\r\n", "output": "4\r\n"}, {"input": "41 56\r\n6 2 0 -3 3 6 0 10 -7 -5 -5 7 -5 -9 -3 -5 -2 9 5 -1 1 8 -2 1 -10 10 -4 -9 10 -8 8 7 7 7 4 4 -2 2 4 -6 -7\r\n9 6 -5 6 -7 2 -6 -3 -6 -1 10 -5 -5 3 10 10 4 3 0 2 8 4 -3 3 9 4 -6 0 2 6 6 -2 0 -3 -5 3 4 -2 -3 10 -10 1 3 -3 -7 2 -2 2 0 4 -6 8 -4 -1 1 -6\r\n", "output": "97\r\n"}, {"input": "45 57\r\n-5 -3 -10 2 -3 1 10 -3 -3 -7 -9 6 6 1 8 2 -4 3 -6 9 8 10 -1 8 -2 -8 -9 -7 -8 4 -1 -10 0 -4 8 -7 3 -1 0 3 -8 -10 -6 -8 -5\r\n1 3 -1 7 1 10 3 -2 8 6 0 2 -3 -3 10 -10 -6 -7 10 5 9 10 3 -2 4 10 -10 0 -2 4 -6 -1 -1 -5 7 -3 -2 -7 7 -2 2 2 1 -10 -7 -8 -3 4 0 8 -5 -7 -7 9 -3 8 -5\r\n", "output": "102\r\n"}, {"input": "51 39\r\n-10 6 -8 2 6 6 0 2 4 -3 8 10 7 1 9 -8 4 -2 3 5 8 -2 1 3 1 3 -5 0 2 2 7 -3 -10 4 9 -3 -7 5 5 10 -5 -5 9 -3 9 -1 -4 9 -7 -8 5\r\n-5 10 -2 -8 -10 5 -7 1 7 6 -3 -5 0 -4 0 -9 2 -9 -10 2 -6 10 0 4 -4 -8 -3 1 10 7 5 7 0 -7 1 0 9 0 -5\r\n", "output": "90\r\n"}, {"input": "4 10\r\n3 -7 -4 -8\r\n7 3 -1 -8 2 -1 -5 8 -8 9\r\n", "output": "12\r\n"}, {"input": "44 41\r\n-6 0 -2 5 5 -9 -4 -5 -2 -6 -7 -10 5 2 -6 -3 1 4 8 2 -7 6 5 0 10 -2 -9 3 -6 -3 7 5 -3 7 -10 -1 6 0 10 -6 -5 -6 -6 6\r\n6 -3 1 -1 8 9 6 7 -6 -4 -2 -4 -3 3 2 -1 3 1 10 -2 2 -10 -9 -3 8 -3 -1 -4 0 0 -4 7 -10 6 10 -8 5 6 2 -9 -4\r\n", "output": "85\r\n"}, {"input": "52 43\r\n-514 -667 -511 516 -332 73 -233 -594 125 -847 -584 432 631 -673 -380 835 69 523 -568 -110 -752 -731 864 250 550 -249 525 357 8 43 -395 -328 61 -84 -151 165 -896 955 -660 -195 375 806 -160 870 143 -725 -814 494 -953 -463 704 -415\r\n608 -584 673 -920 -227 -442 242 815 533 -184 -502 -594 -381 -960 786 -627 -531 -579 583 -252 -445 728 902 934 -311 971 119 -391 710 -794 738 -82 774 580 -142 208 704 -745 -509 979 -236 -276 -800\r\n", "output": "18\r\n"}, {"input": "51 48\r\n642 261 822 -266 700 18 -62 -915 39 997 564 -130 605 -141 58 426 -514 425 -310 -56 -524 860 -793 57 511 -563 -529 -140 -679 -489 -841 -326 -108 -785 599 3 -90 -52 769 -513 -328 -709 -887 736 729 -148 232 680 -589 77 531\r\n689 765 386 700 612 -936 -258 966 873 130 230 -78 -835 739 -755 127 -963 -282 -728 -833 345 -817 -61 680 944 -475 -46 -915 777 -789 -742 -755 325 -474 -220 544 19 828 -483 -388 -330 -150 -912 -219 185 -541 237 724\r\n", "output": "20\r\n"}, {"input": "35 32\r\n8 3 9 -6 -6 -3 -6 2 2 3 0 -4 8 9 -10 -7 7 -6 -4 1 -9 3 -2 3 -4 -8 -8 5 -10 2 6 4 -7 -6 -1\r\n7 -2 1 -9 1 8 4 -4 -4 -7 5 4 0 3 5 8 9 -7 -1 -8 -1 7 2 5 6 -2 -8 -2 9 -6 8 -6\r\n", "output": "67\r\n"}, {"input": "54 55\r\n-95 -3 -18 81 -85 -78 -76 95 -4 -91 88 98 35 88 -30 -82 -1 23 -98 82 -83 100 -47 -7 93 -87 -57 -5 -57 -46 30 -16 -27 -46 78 -58 4 87 86 -58 22 19 -40 8 -6 92 -65 10 -51 -34 -70 -69 -70 -51\r\n-42 75 48 -79 58 23 -8 47 48 33 -2 97 -30 -8 -87 56 22 -91 25 27 -91 -75 -10 45 -27 54 -94 60 -49 22 18 2 35 -81 8 -61 91 12 78 6 -83 76 -81 -27 -65 56 -99 -69 3 91 81 -34 9 -29 61\r\n", "output": "63\r\n"}, {"input": "18 4\r\n81 -30 22 81 -9 -66 -39 -11 16 9 91 74 -36 40 -26 -11 -13 -22\r\n21 67 96 96\r\n", "output": "8\r\n"}, {"input": "5 5\r\n9 9 -7 -4 6\r\n2 3 -3 -8 -1\r\n", "output": "7\r\n"}, {"input": "44 49\r\n28 76 41 66 49 31 3 4 41 89 44 41 33 73 5 -85 57 -55 86 43 25 0 -26 -36 81 -80 -71 77 96 85 -8 -96 -91 28 3 -98 -82 -87 -50 70 -39 -99 -70 66\r\n-12 28 11 -25 -34 70 -4 69 9 -31 -23 -8 19 -54 -5 -24 -7 -45 -70 -71 -64 77 39 60 -63 10 -7 -92 22 4 45 75 100 49 95 -66 -96 -85 -35 92 -9 -37 -38 62 -62 24 -35 40 3\r\n", "output": "52\r\n"}, {"input": "57 41\r\n-10 9 2 7 -1 1 -10 9 7 -9 -4 8 4 -8 -6 8 1 5 -7 9 8 -4 1 -2 -7 6 -9 -10 3 0 8 7 1 -4 -6 9 -10 8 3 -9 4 -9 6 -7 10 -4 3 -4 10 -1 -7 -7 -10 -10 0 3 -10\r\n-2 1 -5 6 0 -2 -4 8 2 -9 -6 7 2 6 -9 -1 -1 3 -4 -8 -4 -10 7 -1 -6 -5 -7 5 10 -5 -4 4 -7 6 -2 -9 -10 3 -7 -5 -9\r\n", "output": "98\r\n"}, {"input": "9 5\r\n7 -5 -2 -3 -10 -3 5 4 10\r\n-1 6 -4 3 2\r\n", "output": "11\r\n"}, {"input": "31 55\r\n68 -31 19 47 95 -44 67 45 32 -17 31 -14 52 -19 -75 97 88 9 -11 77 -23 74 -29 31 -42 -15 -77 30 -17 75 -9\r\n-63 94 98 45 -57 -41 4 34 38 67 68 69 -36 47 91 -55 58 73 77 -71 4 -89 -6 49 71 70 -64 -24 -87 -3 -96 62 -31 -56 -2 88 -80 95 -97 -91 25 -2 1 -80 -45 -96 -62 12 12 -61 -13 23 -32 6 29\r\n", "output": "43\r\n"}, {"input": "50 59\r\n-6 -5 8 -6 7 9 2 -7 0 -9 -7 1 -5 10 -6 -2 -10 6 -6 -2 -7 -10 1 4 -4 9 2 -8 -3 -1 5 -4 2 8 -10 7 -10 4 8 7 -4 9 1 5 -10 -7 -2 3 -9 5\r\n-10 -1 3 9 0 8 5 10 -6 -2 -2 4 4 -1 -3 -9 4 -6 9 -5 -5 -4 -7 -2 9 6 -3 -6 -1 -9 1 9 2 -9 2 -5 3 7 -10 7 -3 -1 -10 -3 2 6 -2 -5 10 5 8 7 4 6 -7 -5 2 -2 -10\r\n", "output": "109\r\n"}, {"input": "53 51\r\n678 -657 703 569 -524 -801 -221 -600 -95 11 -660 866 506 683 649 -842 604 -33 -929 541 379 939 -512 -347 763 697 653 844 927 488 -233 -313 357 -717 119 885 -864 738 -20 -350 -724 906 -41 324 -713 424 -432 154 173 406 29 -420 62\r\n-834 648 564 735 206 490 297 -968 -482 -914 -149 -312 506 56 -773 527 816 137 879 552 -224 811 -786 739 -828 -203 -873 148 -290 395 832 -845 302 -324 32 299 746 638 -684 216 392 -137 496 57 -187 477 -16 395 -325 -186 -801\r\n", "output": "24\r\n"}, {"input": "51 7\r\n323 236 120 48 521 587 327 613 -470 -474 522 -705 320 -51 1 288 -430 -954 732 -805 -562 300 -710 190 515 280 -101 -927 77 282 198 -51 -350 -990 -435 -765 178 -934 -955 704 -565 -640 853 -27 950 170 -712 -780 620 -572 -409\r\n244 671 425 977 773 -294 268\r\n", "output": "9\r\n"}, {"input": "9 9\r\n-9 -1 2 8 10 2 9 7 3\r\n5 8 -5 4 -4 -8 2 8 -8\r\n", "output": "16\r\n"}, {"input": "50 60\r\n445 303 -861 -583 436 -125 312 -700 -829 -865 -276 -25 -725 -286 528 -221 757 720 -572 514 -514 359 294 -992 -838 103 611 776 830 143 -247 182 -241 -627 299 -824 635 -571 -660 924 511 -876 160 569 -570 827 75 558 708 46\r\n899 974 750 -138 -439 -904 -113 -761 -150 -92 -279 489 323 -649 -759 667 -600 -76 -991 140 701 -654 -276 -563 108 -301 161 -989 -852 -97 316 31 -724 848 979 -501 -883 569 925 -532 86 456 302 -985 826 79 -911 660 752 941 -464 -157 -110 433 829 -872 172 496 528 -576\r\n", "output": "24\r\n"}, {"input": "7 3\r\n90 67 1 68 40 -100 -26\r\n-96 70 -74\r\n", "output": "6\r\n"}, {"input": "7 4\r\n-8 -2 -5 -3 -8 -9 8\r\n-8 4 -1 10\r\n", "output": "8\r\n"}, {"input": "52 48\r\n-552 43 -670 -163 -765 -603 -768 673 -248 337 -89 941 -676 -406 280 409 -630 -577 324 115 927 477 -242 108 -337 591 -158 -524 928 859 825 935 818 638 -51 988 -568 871 -842 889 -737 -272 234 -643 766 -422 473 -570 -1000 -735 -279 845\r\n963 -436 738 113 273 -374 -568 64 276 -698 -135 -748 909 -250 -740 -344 -436 414 719 119 973 -881 -576 868 -45 909 630 286 -845 458 684 -64 579 965 598 205 -318 -974 228 -596 596 -946 -198 923 571 -907 -911 -341\r\n", "output": "21\r\n"}, {"input": "40 54\r\n-26 -98 27 -64 0 33 62 -12 -8 -10 -62 28 28 75 -5 -89 -75 -100 -63 9 -97 -20 -81 62 56 -39 87 -33 13 61 19 -97 -23 95 18 -4 -48 55 -40 -81\r\n-69 -71 1 -46 -58 0 -100 -82 31 43 -59 -43 77 -8 -61 -2 -36 -55 -35 -44 -59 -13 -16 14 60 -95 -61 25 76 -1 -3 9 -92 48 -92 -54 2 -7 73 -16 42 -40 36 -58 97 81 13 -64 -12 -28 65 85 -61 8\r\n", "output": "54\r\n"}, {"input": "22 34\r\n-73 45 -81 -67 9 61 -97 0 -64 86 -9 47 -28 100 79 -53 25 -59 -80 -86 -47 24\r\n57 69 8 -34 -37 12 -32 -81 27 -65 87 -64 62 -44 97 34 44 -93 44 25 -72 93 14 95 -60 -65 96 -95 23 -69 28 -20 -95 74\r\n", "output": "27\r\n"}, {"input": "46 48\r\n-710 947 515 217 26 -548 -416 494 431 -872 -616 848 -950 -138 -104 560 241 -462 -265 90 66 -331 934 -788 -815 -558 86 39 784 86 -856 879 -733 -653 104 -673 -588 -568 78 368 -226 850 195 982 140 370\r\n470 -337 283 460 -710 -434 739 459 -567 173 217 511 -830 644 -734 764 -211 -106 423 -356 -126 677 -454 42 680 557 -636 9 -552 877 -246 -352 -44 442 -505 -811 -100 -768 -130 588 -428 755 -904 -138 14 -253 -40 265\r\n", "output": "20\r\n"}, {"input": "58 40\r\n120 571 -472 -980 993 -885 -546 -220 617 -697 -182 -42 128 -29 567 -615 -260 -876 -507 -642 -715 -283 495 -584 97 4 376 -131 186 -301 729 -545 8 -610 -643 233 -123 -769 -173 119 993 825 614 -503 891 426 -850 -992 -406 784 634 -294 997 127 697 -509 934 316\r\n630 -601 937 -544 -50 -176 -885 530 -828 556 -784 460 -422 647 347 -862 131 -76 490 -576 5 -761 922 -426 -7 -401 989 -554 688 -531 -303 -415 -507 -752 -581 -589 513 -151 -279 317\r\n", "output": "18\r\n"}, {"input": "8 6\r\n23 -18 -94 -80 74 89 -48 61\r\n-84 45 -12 -9 -74 63\r\n", "output": "7\r\n"}, {"input": "57 55\r\n-34 744 877 -4 -902 398 -404 225 -560 -600 634 180 -198 703 910 681 -864 388 394 -317 839 -95 -706 -474 -340 262 228 -196 -35 221 -689 -960 -430 946 -231 -146 741 -754 330 217 33 276 381 -734 780 -522 528 -425 -202 -19 -302 -743 53 -247 69 -314 -845\r\n-158 24 461 -274 -30 -58 223 -806 747 568 -59 126 56 661 8 419 -422 320 340 480 -185 905 -459 -530 -50 -484 303 366 889 404 915 -361 -985 -642 -690 -577 -80 -250 173 -740 385 908 -4 593 -559 731 326 -209 -697 490 265 -548 716 320 23\r\n", "output": "24\r\n"}, {"input": "59 56\r\n-8 -8 -4 5 -4 4 4 -5 -10 -2 0 6 -9 -2 10 1 -8 -2 9 5 2 -1 -5 7 -7 7 2 -7 -5 2 -2 -8 4 10 5 -9 5 9 6 10 7 6 -6 -4 -8 5 9 6 1 -7 5 -9 8 10 -7 1 3 -6 8\r\n6 -2 -7 4 -2 -5 -9 -5 0 8 5 5 -4 -6 -5 -10 4 3 -4 -4 -8 2 -6 -10 -10 4 -3 8 -1 8 -8 -5 -2 3 7 3 -8 10 6 0 -8 0 9 -3 9 0 9 0 -3 4 -3 10 10 5 -6 4\r\n", "output": "115\r\n"}, {"input": "53 30\r\n-4206 -1169 3492 6759 5051 -3338 4024 8267 -4651 -7685 -3346 -4958 2648 9321 6062 -3566 8118 9067 -1331 5064 148 6375 6193 -2024 -9376 -663 3837 3989 6583 6971 -146 2515 -4222 8159 -94 -4937 -8364 -6025 3566 556 -5229 3138 -9504 1383 1171 -3918 -1587 -6532 -2299 -6648 -5861 4864 9220\r\n-2359 7436 1682 1775 3850 2691 -4326 6670 3245 -3821 5932 -1159 6162 -2818 -5255 -7439 -6688 1778 -5132 8085 -3576 9153 -5260 -1438 9941 -4729 532 -5206 2133 -2252\r\n", "output": "10\r\n"}, {"input": "4 9\r\n-2 3 -5 -10\r\n7 -7 5 5 2 4 9 -4 5\r\n", "output": "11\r\n"}, {"input": "44 50\r\n23 -401 692 -570 264 -885 417 -355 560 -254 -468 -849 900 997 559 12 853 424 -579 485 711 67 638 771 -750 -583 294 -410 -225 -117 -262 148 385 627 610 983 -345 -236 -62 635 -421 363 88 682\r\n-204 -429 -74 855 533 -817 -613 205 972 941 -566 -813 79 -660 -604 661 273 -70 -70 921 -240 148 314 328 -155 -56 -793 259 -630 92 -975 -361 671 963 430 315 -94 957 465 548 -796 626 -58 -595 315 -455 -918 398 279 99\r\n", "output": "22\r\n"}, {"input": "53 30\r\n5 10 -1 -9 7 -7 1 6 0 7 2 -2 -2 1 -9 -9 2 -7 9 10 -9 1 -1 -9 -9 -5 -8 -3 2 4 -3 -6 6 4 -2 -3 -3 -9 2 -4 9 5 6 -5 -5 6 -2 -1 10 7 4 -4 -2\r\n-1 10 3 -1 7 10 -2 -1 -2 0 3 -10 -6 1 -9 2 -10 9 6 -7 -9 3 -7 1 0 9 -8 2 9 7\r\n", "output": "82\r\n"}, {"input": "9 9\r\n1 10 0 -2 9 -7 1 -4 3\r\n-7 -1 6 -4 8 2 6 6 -3\r\n", "output": "15\r\n"}, {"input": "9 9\r\n5181 -7243 3653 3587 -5051 -4899 -4110 7981 -6429\r\n-7365 -2247 7942 9486 -7160 -1020 -8934 7733 -3010\r\n", "output": "4\r\n"}, {"input": "55 43\r\n9 1 0 -7 4 3 4 4 -8 3 0 -7 0 -9 3 -6 0 4 7 1 -1 -10 -7 -6 -8 -8 2 -5 5 -4 -9 -7 5 -3 -7 -10 -4 -2 -7 -3 2 4 9 8 -8 9 -10 0 0 3 -6 -5 -2 9 -6\r\n-4 -6 9 -4 -2 5 9 6 -8 -2 -3 -7 -8 8 -8 5 1 7 9 7 -5 10 -10 -8 -3 10 0 8 8 4 8 3 10 -8 -4 -6 1 9 0 -3 -4 8 -10\r\n", "output": "98\r\n"}, {"input": "53 12\r\n63 88 91 -69 -15 20 98 40 -70 -49 -51 -74 -34 -52 1 21 83 -14 57 40 -57 33 94 2 -74 22 86 79 9 -18 67 -31 72 31 -64 -83 83 29 50 -29 -27 97 -40 -8 -57 69 -93 18 42 68 -71 -86 22\r\n51 19 33 12 98 91 -83 65 -6 16 81 86\r\n", "output": "27\r\n"}, {"input": "1 1\r\n0\r\n0\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 1 1\r\n1 2 2\r\n", "output": "6\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "2\r\n"}, {"input": "1 1\r\n0\r\n1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n0 0 0\r\n0 0 0\r\n", "output": "6\r\n"}, {"input": "5 5\r\n5 5 5 5 5\r\n5 5 5 5 5\r\n", "output": "10\r\n"}, {"input": "60 60\r\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59\r\n0 60 120 180 240 300 360 420 480 540 600 660 720 780 840 900 960 1020 1080 1140 1200 1260 1320 1380 1440 1500 1560 1620 1680 1740 1800 1860 1920 1980 2040 2100 2160 2220 2280 2340 2400 2460 2520 2580 2640 2700 2760 2820 2880 2940 3000 3060 3120 3180 3240 3300 3360 3420 3480 3540\r\n", "output": "4\r\n"}, {"input": "2 2\r\n0 2\r\n0 1\r\n", "output": "4\r\n"}, {"input": "1 1\r\n5\r\n5\r\n", "output": "2\r\n"}, {"input": "10 10\r\n1 1 1 1 1 1 1 1 1 1\r\n-30 -30 -30 -30 40 40 40 40 40 40\r\n", "output": "20\r\n"}]
false
stdio
null
true
863/E
863
E
PyPy 3-64
TESTS
13
1,996
120,217,600
199358828
import sys input = sys.stdin.buffer.readline class SegmentTree: def __init__(self, data, default=float('inf'), func=min): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): """func of data[start, stop)""" start += self._size stop += self._size res_left = res_right = self._default while start < stop: if start & 1: res_left = self._func(res_left, self.data[start]) start += 1 if stop & 1: stop -= 1 res_right = self._func(self.data[stop], res_right) start >>= 1 stop >>= 1 return self._func(res_left, res_right) def __repr__(self): return "SegmentTree({0})".format(self.data) def process(A): n = len(A) A2 = [[None, None] for i in range(n)] L = [] for i in range(n): l, r = A[i] L.append((l, i, 0)) L.append((r, i, 1)) L.sort() curr = [L[0][0], []] I = 0 for x, i, t in L: if x > curr[0]: for i2, t2 in curr[1]: A2[i2][t2] = I I+=1 curr = [x, []] if x==curr[0]: curr[1].append((i, t)) for i2, t2 in curr[1]: A2[i2][t2] = I I+=1 data = [0 for i in range(I+2)] for i in range(n): l, r = A2[i] data[l]+=1 data[r+1]-=1 data2 = [0 for i in range(I)] data2[0] = data[0] for i in range(1, I): data2[i] = data[i]+data2[i-1] S = SegmentTree(data=data2) for i in range(n): l, r = A2[i] if S.query(l, r+1) > 1: sys.stdout.write(f'{i+1}\n') return sys.stdout.write('-1\n') return n = int(input()) A = [] for i in range(n): l, r = [int(x) for x in input().split()] A.append([l, r]) process(A)
55
904
33,587,200
129463574
n = int(input()) lis = [] for i in range(n): a, b = map(int, input().split()) lis.append([a, b, i+1]) lis.sort() i = 1 ans = -1 #Turn off the TV while i<n: if lis[i][1]<=lis[i-1][1]: ans = lis[i][2] print(ans) exit(0) elif( lis[i][0] <= lis[i-1][0] and lis[i][1] >= lis[i-1][1]): ans = lis[i-1][2] print(ans) exit(0) lis[i][0] = max( lis[i][0], lis[i-1][1] + 1) i+=1 print(ans)
Educational Codeforces Round 29
ICPC
2,017
2
256
Turn Off The TV
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.
null
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[{"input": "3\n1 3\n4 6\n1 7", "output": "1"}, {"input": "2\n0 10\n0 10", "output": "1"}, {"input": "3\n1 2\n3 4\n6 8", "output": "-1"}, {"input": "3\n1 2\n2 3\n3 4", "output": "2"}]
2,000
["data structures", "sortings"]
55
[{"input": "3\r\n1 3\r\n4 6\r\n1 7\r\n", "output": "1\r\n"}, {"input": "2\r\n0 10\r\n0 10\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2\r\n3 4\r\n6 8\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 500000000\r\n500000001 1000000000\r\n0 1000000000\r\n", "output": "1\r\n"}, {"input": "3\r\n1 5\r\n2 4\r\n6 10\r\n", "output": "2\r\n"}, {"input": "10\r\n4 4\r\n5 9\r\n5 7\r\n2 8\r\n6 10\r\n4 10\r\n1 3\r\n8 9\r\n0 0\r\n5 7\r\n", "output": "1\r\n"}, {"input": "2\r\n1 3\r\n2 4\r\n", "output": "-1\r\n"}, {"input": "1\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "8\r\n13 17\r\n83 89\r\n31 33\r\n7 13\r\n52 52\r\n88 89\r\n29 30\r\n16 22\r\n", "output": "6\r\n"}, {"input": "4\r\n63 63\r\n12 34\r\n17 29\r\n58 91\r\n", "output": "1\r\n"}, {"input": "3\r\n1 10\r\n5 15\r\n10 20\r\n", "output": "2\r\n"}, {"input": "2\r\n1 3\r\n1 6\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 6\r\n1 3\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 4\r\n2 100\r\n4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 1\r\n3 3\r\n4 7\r\n4 5\r\n", "output": "4\r\n"}, {"input": "3\r\n2 3\r\n3 4\r\n1 2\r\n", "output": "1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "-1\r\n"}, {"input": "6\r\n99 100\r\n65 65\r\n34 34\r\n16 18\r\n65 67\r\n88 88\r\n", "output": "2\r\n"}, {"input": "2\r\n50 67\r\n54 64\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3\r\n2 100\r\n3 5\r\n", "output": "3\r\n"}, {"input": "3\r\n57 90\r\n35 45\r\n18 52\r\n", "output": "2\r\n"}, {"input": "4\r\n14 15\r\n46 73\r\n15 40\r\n28 53\r\n", "output": "-1\r\n"}, {"input": "3\r\n37 38\r\n51 54\r\n28 28\r\n", "output": "-1\r\n"}, {"input": "2\r\n64 66\r\n47 61\r\n", "output": "-1\r\n"}, {"input": "4\r\n50 68\r\n63 67\r\n67 69\r\n11 12\r\n", "output": "2\r\n"}, {"input": "4\r\n42 62\r\n93 103\r\n34 62\r\n5 12\r\n", "output": "1\r\n"}, {"input": "6\r\n42 60\r\n78 107\r\n6 38\r\n58 81\r\n70 105\r\n70 105\r\n", "output": "5\r\n"}, {"input": "5\r\n71 71\r\n21 22\r\n58 58\r\n57 57\r\n16 16\r\n", "output": "-1\r\n"}, {"input": "7\r\n28 42\r\n70 75\r\n83 92\r\n19 22\r\n26 32\r\n85 99\r\n30 39\r\n", "output": "7\r\n"}, {"input": "3\r\n8 28\r\n80 110\r\n39 81\r\n", "output": "-1\r\n"}, {"input": "7\r\n90 115\r\n87 113\r\n2 26\r\n39 40\r\n91 112\r\n42 53\r\n65 79\r\n", "output": "5\r\n"}, {"input": "7\r\n12 13\r\n26 28\r\n9 11\r\n15 15\r\n8 10\r\n22 24\r\n5 7\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 5\r\n26 31\r\n11 15\r\n2 4\r\n16 18\r\n4 4\r\n7 12\r\n", "output": "6\r\n"}, {"input": "3\r\n1 5\r\n1 2\r\n4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n999999995 999999997\r\n999999998 1000000000\r\n999999996 999999999\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2\r\n4 6\r\n4 10\r\n200 300\r\n", "output": "2\r\n"}]
false
stdio
import bisect def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) intervals = [] for _ in range(n): l, r = map(int, f.readline().split()) intervals.append((l, r)) # Compute redundant TVs points = set() for l, r in intervals: points.add(l - 1) points.add(l) points.add(r) points.add(r + 1) sorted_points = sorted(points) m = len(sorted_points) diff = [0] * (m + 1) for l, r in intervals: idx_l = bisect.bisect_left(sorted_points, l) assert idx_l < m and sorted_points[idx_l] == l diff[idx_l] += 1 idx_r_plus_1 = bisect.bisect_left(sorted_points, r + 1) assert idx_r_plus_1 < m and sorted_points[idx_r_plus_1] == r + 1 diff[idx_r_plus_1] -= 1 coverage = [0] * m current = 0 for i in range(m): current += diff[i] coverage[i] = current single = [0] * m for i in range(m): single[i] = 1 if coverage[i] == 1 else 0 pref = [0] * (m + 1) for i in range(m): pref[i + 1] = pref[i] + single[i] redundant = [] for i in range(n): l, r = intervals[i] l_minus_1 = l - 1 idx_l_minus_1 = bisect.bisect_left(sorted_points, l_minus_1) assert idx_l_minus_1 < m and sorted_points[idx_l_minus_1] == l_minus_1 idx_r = bisect.bisect_left(sorted_points, r) assert idx_r < m and sorted_points[idx_r] == r sum_single = pref[idx_r + 1] - pref[idx_l_minus_1 + 1] if sum_single == 0: redundant.append(i + 1) # Read submission output with open(submission_output_path) as f: submission_output = f.readline().strip() try: k = int(submission_output) except: print(0) return if k == -1: if not redundant: print(1) else: print(0) else: if 1 <= k <= n and k in redundant: print(1) else: print(0) if __name__ == "__main__": import sys input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
451/B
451
B
PyPy 3-64
TESTS
74
109
17,510,400
207614679
def Line(): return [int(x) for x in input().split()] n = int(input()) lst = Line() l,r = 0, len(lst)-1 c1,c2 = False,False while True: if l == len(lst)-1 or r == 0 or c1 and c2: break if lst[l] > lst[l+1]: c1 = True if lst[r] < lst[r-1]: c2 = True if lst[l] < lst[l+1]: l+=1 if lst[r] > lst[r-1]: r-=1 if l == len(lst)-1 and r == 0: print("yes") print("1 1") elif l == 0 and r == len(lst)-1: print("yes") print(l+1,r+1) else: res = [] for i in range(l): res.append(lst[i]) for i in range(r,l-1,-1): res.append(lst[i]) for i in range(r+1,len(lst)): res.append(lst[i]) check = False for i in range(len(res)-1): if res[i] > res[i+1]: check = True i = len(res) if check: print("no") else: print("yes") print(l+1,r+1)
76
78
12,800,000
216685738
n = int(input()) array = list(map(int, input().split())) size = len(array) s = sorted(array) l, r = 0, 0 for i in range(size): if array[i] != s[i]: l = i break for i in range(size-1, -1, -1): if array[i] != s[i]: r = i break # print(l, r) for i in range(l, r): if array[i] < array[i+1]: print('no') exit() print('yes') print(l+1, r+1)
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
81/B
81
B
Python 3
TESTS
23
122
0
31930868
ans = input().replace("...", " ...") ans = ans.replace(" ,", ",").replace(",", ", ") while " " in ans: ans = ans.replace(" ", " ") if ans[0] == " ": ans = ans[1:] if ans[-1] == " ": ans = ans[:-1] print(ans, end="")
45
124
0
200852400
t = input().split() t = ' '.join(t) t = t.replace(', ', ',') t = t.replace(' ,', ',') t = t.replace(',', ', ') t = t.replace(' ...', '...') t = t.replace('... ', '...') t = t.replace('...', ' ...') if t[0] == ' ': t = t[1: ] if t[-1] == ' ': t = t[: -1] print(t)
Yandex.Algorithm Open 2011: Qualification 1
CF
2,011
2
256
Sequence Formatting
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "1,2 ,3,...,   10" will be corrected to "1, 2, 3, ..., 10". In this task you are given a string s, which is composed by a concatination of terms, each of which may be: - a positive integer of an arbitrary length (leading zeroes are not allowed), - a "comma" symbol (","), - a "space" symbol (" "), - "three dots" ("...", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string s to ensure the following: - each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), - each "three dots" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), - if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, - there should not be other spaces. Automate Polycarp's work and write a program that will process the given string s.
The input data contains a single string s. Its length is from 1 to 255 characters. The string s does not begin and end with a space. Its content matches the description given above.
Print the string s after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it.
null
null
[{"input": "1,2 ,3,..., 10", "output": "1, 2, 3, ..., 10"}, {"input": "1,,,4...5......6", "output": "1, , , 4 ...5 ... ...6"}, {"input": "...,1,2,3,...", "output": "..., 1, 2, 3, ..."}]
1,700
["implementation", "strings"]
45
[{"input": "1,2 ,3,..., 10\r\n", "output": "1, 2, 3, ..., 10\r\n"}, {"input": "1,,,4...5......6\r\n", "output": "1, , , 4 ...5 ... ...6\r\n"}, {"input": "...,1,2,3,...\r\n", "output": "..., 1, 2, 3, ...\r\n"}, {"input": "......\r\n", "output": "... ...\r\n"}, {"input": ",,,,,,,,,,,,,\r\n", "output": ", , , , , , , , , , , , ,\r\n"}, {"input": "123456789\r\n", "output": "123456789\r\n"}, {"input": ",\r\n", "output": ",\r\n"}, {"input": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n", "output": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n"}, {"input": "1,2,,,,,,,,,5566\r\n", "output": "1, 2, , , , , , , , , 5566\r\n"}, {"input": ".........1......1......23......31...2......\r\n", "output": "... ... ...1 ... ...1 ... ...23 ... ...31 ...2 ... ...\r\n"}, {"input": "...\r\n", "output": "...\r\n"}, {"input": ",...\r\n", "output": ", ...\r\n"}, {"input": "...,\r\n", "output": "...,\r\n"}, {"input": ",,\r\n", "output": ", ,\r\n"}, {"input": ",...,\r\n", "output": ", ...,\r\n"}, {"input": "...,...\r\n", "output": "..., ...\r\n"}, {"input": "1...10\r\n", "output": "1 ...10\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ,\r\n", "output": ", ,\r\n"}, {"input": "............................................................................................................................................................................................................................................................\r\n", "output": "... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ..."}, {"input": "1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1\r\n", "output": "1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1"}, {"input": "12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12\r\n", "output": "12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12\r\n"}, {"input": "1,\r\n", "output": "1,\r\n"}, {"input": ",1\r\n", "output": ", 1\r\n"}, {"input": "1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n", "output": "1 1\r\n"}, {"input": "1 1 1\r\n", "output": "1 1 1\r\n"}, {"input": "... ,\r\n", "output": "...,\r\n"}, {"input": "......,,,,,...... 1234 1234 1234 , 1234 ... , 1234 ... 1234 ... , 1234\r\n", "output": "... ..., , , , , ... ...1234 1234 1234, 1234 ..., 1234 ...1234 ..., 1234\r\n"}, {"input": "9999999999999999999999999999999999999999999999999999999999, 1\r\n", "output": "9999999999999999999999999999999999999999999999999999999999, 1\r\n"}, {"input": "5555555555555555555 6666 77777777 8888888888888 ,,...,55,5...55...,.........5 , , ... , 5 5 , 5 ... 5 5 ... , ... ... ... 5\r\n", "output": "5555555555555555555 6666 77777777 8888888888888, , ..., 55, 5 ...55 ..., ... ... ...5, , ..., 5 5, 5 ...5 5 ..., ... ... ...5\r\n"}, {"input": "...5\r\n", "output": "...5\r\n"}, {"input": ", 12 12, ...\r\n", "output": ", 12 12, ...\r\n"}, {"input": ",12 12,...\r\n", "output": ", 12 12, ...\r\n"}, {"input": "1,2 4,78 799,4...5 3......6,\r\n", "output": "1, 2 4, 78 799, 4 ...5 3 ... ...6,\r\n"}]
false
stdio
null
true
899/E
899
E
Python 3
PRETESTS
6
1,029
15,872,000
33348698
import bisect class TreeSet(object): """ Binary-tree set like java Treeset. Duplicate elements will not be added. When added new element, TreeSet will be sorted automatically. """ def __init__(self, elements): self._treeset = [] self.addAll(elements) def addAll(self, elements): for element in elements: if element in self: continue self.add(element) def add(self, element): if element not in self: bisect.insort(self._treeset, element) def ceiling(self, e): index = bisect.bisect_right(self._treeset, e) if self[index - 1] == e: return e return self._treeset[bisect.bisect_right(self._treeset, e)] def floor(self, e): index = bisect.bisect_left(self._treeset, e) if self[index] == e: return e else: return self._treeset[bisect.bisect_left(self._treeset, e) - 1] def __getitem__(self, num): return self._treeset[num] def __len__(self): return len(self._treeset) def clear(self): """ Delete all elements in TreeSet. """ self._treeset = [] def clone(self): """ Return shallow copy of self. """ return TreeSet(self._treeset) def remove(self, element): """ Remove element if element in TreeSet. """ try: self._treeset.remove(element) except ValueError: return False return True def __iter__(self): """ Do ascending iteration for TreeSet """ for element in self._treeset: yield element def pop(self, index): return self._treeset.pop(index) def __str__(self): return str(self._treeset) def __eq__(self, target): if isinstance(target, TreeSet): return self._treeset == target.treeset elif isinstance(target, list): return self._treeset == target def __contains__(self, e): """ Fast attribution judgment by bisect """ try: return e == self._treeset[bisect.bisect_left(self._treeset, e)] except: return False n = int(input()) a = list(map(int, input().split())) class sg: def __init__(self, val, st, pos): self.val = val self.st = st self.left = None self.right = None self.pos = pos def __gt__(a, b): if a.st != b.st: return a.st < b.st else: return a.pos > b.pos pr = a[0] st = 1 c = 0 ps = None tst = TreeSet([]) for i in range(1, n): if a[i] == pr: st += 1 else: ns = sg(pr, st, c) pr = a[i] st = 1 if c > 0: ns.left = ps ps.right = ns ps = ns tst.add(ns) c += 1 ns = sg(pr, st, c) if c > 0: ns.left = ps ps.right = ns tst.add(ns) c += 1 ans = 0 while len(tst._treeset) > 0: #print(tst._treeset) nd = tst.pop(0) if nd.right is not None: nd.right.left = None if nd.left is not None: nd.left.right = None ans += 1 if nd.left is not None and nd.right is not None and nd.left.val == nd.right.val: nd.left.st += nd.right.st nd.left.right = nd.right.right tst.remove(nd.right) print(ans)
97
1,466
38,297,600
138133134
import heapq class Node: def __init__(self,idx,freq,val) -> None: self.value=val self.index=idx self.occ=freq self.front=None self.back=None class LinkedList: def __init__(self) -> None: self.head=None self.h=[] self.seen=[False]*200001 heapq.heapify(self.h) self.ans=0 self.len=0 def insert(self,node) -> None: if self.head==None: self.head=node else: h=self.head node.back=h h.front=node self.head=node node.front=None self.len+=1 heapq.heappush(self.h,(-1*node.occ,node.index,node)) def merge(self,l,r): ll=l.back rr=r.front new_node=Node(min(r.index,l.index),l.occ+r.occ,l.value) new_node.front=rr new_node.back=ll self.seen[max(l.index,r.index)]=True if (rr!=None): rr.back=new_node if (ll!=None): ll.front=new_node heapq.heappush(self.h,(-1*new_node.occ,new_node.index,new_node)) return def remove(self,node): self.len-=1 if (self.head==node and self.len==0): self.head=None return l=node.back r=node.front if (l!=None): l.front=r if r!=None: r.back=l if (l!=None and r!=None and l.value==r.value): self.merge(l,r) def next_val(self): if len(self.h)==0: return -1 f,idx,node=heapq.heappop(self.h) if (not self.seen[idx]): self.seen[idx]=True self.remove(node) self.ans+=1 return 1 def main(n,arr): nodes=[] val=arr[0] idx=0 freq=1 for i in range(1,len(arr)): if (arr[i]!=arr[i-1]): node=Node(idx,freq,val) nodes.append(node) val=arr[i] idx+=1 freq=1 else: freq+=1 node=Node(idx,freq,val) nodes.append(node) l=LinkedList() for i in range(len(nodes)-1,-1,-1): l.insert(nodes[i]) s=l.next_val() while (s!=-1): s=l.next_val() return l.ans n=int(input()) arr=list(map(int,input().split())) print(main(n,arr))
Codeforces Round 452 (Div. 2)
CF
2,017
2
256
Segments Removal
Vasya has an array of integers of length n. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2]. Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
null
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array. In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2. In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
[{"input": "4\n2 5 5 2", "output": "2"}, {"input": "5\n6 3 4 1 5", "output": "5"}, {"input": "8\n4 4 4 2 2 100 100 100", "output": "3"}, {"input": "6\n10 10 50 10 50 50", "output": "4"}]
2,000
["data structures", "dsu", "flows", "implementation", "two pointers"]
97
[{"input": "4\r\n2 5 5 2\r\n", "output": "2\r\n"}, {"input": "5\r\n6 3 4 1 5\r\n", "output": "5\r\n"}, {"input": "8\r\n4 4 4 2 2 100 100 100\r\n", "output": "3\r\n"}, {"input": "6\r\n10 10 50 10 50 50\r\n", "output": "4\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "100\r\n45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45\r\n", "output": "1\r\n"}, {"input": "1\r\n100\r\n", "output": "1\r\n"}, {"input": "2\r\n1 100\r\n", "output": "2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n100 100\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 3\r\n", "output": "2\r\n"}, {"input": "3\r\n1 100 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 5 6\r\n", "output": "3\r\n"}, {"input": "3\r\n10 4 10\r\n", "output": "3\r\n"}, {"input": "3\r\n10 10 4\r\n", "output": "2\r\n"}, {"input": "4\r\n100 4 56 33\r\n", "output": "4\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 1 3\r\n", "output": "2\r\n"}, {"input": "4\r\n5 1 1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n4\r\n", "output": "1\r\n"}, {"input": "2\r\n21 21\r\n", "output": "1\r\n"}, {"input": "3\r\n48 48 14\r\n", "output": "2\r\n"}, {"input": "10\r\n69 69 69 69 69 13 69 7 69 7\r\n", "output": "6\r\n"}, {"input": "20\r\n15 15 71 100 71 71 15 93 15 100 100 71 100 100 100 15 100 100 71 15\r\n", "output": "14\r\n"}, {"input": "31\r\n17 17 17 17 29 17 17 29 91 17 29 17 91 17 29 17 17 17 29 17 17 17 17 17 17 17 17 29 29 17 17\r\n", "output": "12\r\n"}, {"input": "43\r\n40 69 69 77 9 10 58 69 23 9 58 51 10 69 10 89 77 77 9 9 10 9 69 58 40 10 23 10 58 9 9 77 58 9 77 10 58 58 40 77 9 89 40\r\n", "output": "38\r\n"}, {"input": "54\r\n34 75 90 23 47 13 68 37 14 39 48 41 42 100 19 43 68 47 13 47 48 65 45 89 56 86 67 52 87 81 86 63 44 9 89 21 86 89 20 43 43 37 24 43 77 14 43 42 99 92 49 99 27 40\r\n", "output": "53\r\n"}, {"input": "66\r\n79 79 49 49 79 81 79 79 79 79 81 49 49 79 49 49 79 49 49 81 81 49 49 49 81 49 49 49 81 81 79 81 49 81 49 79 81 49 79 79 81 49 79 79 81 49 49 79 79 79 81 79 49 47 49 49 47 81 79 49 79 79 79 49 49 49\r\n", "output": "34\r\n"}, {"input": "80\r\n80 86 39 5 58 20 66 61 32 75 93 20 57 20 20 61 45 17 86 43 31 75 37 80 92 10 6 18 21 8 93 92 11 75 86 39 53 27 45 77 20 20 1 80 66 13 11 51 58 11 31 51 73 93 15 88 6 32 99 6 39 87 6 39 6 80 8 45 46 45 23 53 23 58 24 53 28 46 87 68\r\n", "output": "78\r\n"}, {"input": "100\r\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7\r\n", "output": "3\r\n"}, {"input": "9\r\n1 2 2 2 1 2 2 2 1\r\n", "output": "3\r\n"}, {"input": "12\r\n1 1 1 49 63 63 63 19 38 38 65 27\r\n", "output": "7\r\n"}, {"input": "7\r\n31 31 21 21 13 96 96\r\n", "output": "4\r\n"}, {"input": "3\r\n1000000000 1 1000000000\r\n", "output": "3\r\n"}]
false
stdio
null
true
853/B
853
B
Python 3
TESTS
43
498
10,035,200
30186799
def main(): n, m, k = map(int, input().split()) ff, tt = [], [] for _ in range(m): day, f, t, city = map(int, input().split()) if f: ff.append((-day, f, city)) else: tt.append((day, t, city)) for ft in ff, tt: cnt, costs = n, [1000001] * (n + 1) ft.sort() while ft: day, city, cost = ft.pop() oldcost = costs[city] if oldcost > cost: costs[city] = cost if oldcost == 1000001: cnt -= 1 if not cnt: break else: print(-1) return total = sum(costs) - 1000001 l = [(day, total)] while ft: day, city, cost = ft.pop() oldcost = costs[city] if oldcost > cost: total -= oldcost - cost costs[city] = cost if l[-1][0] == day: l[-1] = (day, total) else: l.append((day, total)) if ft is ff: ff = l else: tt = l l = [] tt.reverse() d = c = 0 for day, cost in ff: while tt and tt[-1][0] + day > k: d, c = tt.pop() if d + day > k: l.append(cost + c) print(min(l, default=-1)) if __name__ == '__main__': main()
83
389
19,251,200
230895494
g = lambda: map(int, input().split()) n, m, k = g() F, T = [], [] e = int(3e11) for i in range(m): d, f, t, c = g() if f: F.append((d, f, c)) else: T.append((-d, t, c)) for p in [F, T]: C = [e] * (n + 1) s = n * e q = [] p.sort() for d, t, c in p: if C[t] > c: s += c - C[t] C[t] = c if s < e: q.append((s, d)) p.clear() p += q s, t = e, (0, 0) for f in F: while f: if t[1] + f[1] + k < 0: s = min(s, f[0] + t[0]) elif T: t = T.pop() continue f = 0 print(s if s < e else -1)
Codeforces Round 433 (Div. 1, based on Olympiad of Metropolises)
CF
2,017
1
512
Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.
The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.
Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).
null
The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
[{"input": "2 6 5\n1 1 0 5000\n3 2 0 5500\n2 2 0 6000\n15 0 2 9000\n9 0 1 7000\n8 0 2 6500", "output": "24500"}, {"input": "2 4 5\n1 2 0 5000\n2 1 0 4500\n2 1 0 3000\n8 0 1 6000", "output": "-1"}]
1,800
["greedy", "sortings", "two pointers"]
83
[{"input": "2 6 5\r\n1 1 0 5000\r\n3 2 0 5500\r\n2 2 0 6000\r\n15 0 2 9000\r\n9 0 1 7000\r\n8 0 2 6500\r\n", "output": "24500\r\n"}, {"input": "2 4 5\r\n1 2 0 5000\r\n2 1 0 4500\r\n2 1 0 3000\r\n8 0 1 6000\r\n", "output": "-1\r\n"}, {"input": "2 5 5\r\n1 1 0 1\r\n2 2 0 100\r\n3 2 0 10\r\n9 0 1 1000\r\n10 0 2 10000\r\n", "output": "11011\r\n"}, {"input": "2 4 5\r\n1 1 0 1\r\n2 2 0 10\r\n8 0 1 100\r\n9 0 2 1000\r\n", "output": "1111\r\n"}, {"input": "1 2 1\r\n10 1 0 16\r\n20 0 1 7\r\n", "output": "23\r\n"}, {"input": "1 2 10\r\n20 0 1 36\r\n10 1 0 28\r\n", "output": "-1\r\n"}, {"input": "1 2 9\r\n20 0 1 97\r\n10 1 0 47\r\n", "output": "144\r\n"}, {"input": "2 4 1\r\n20 0 1 72\r\n21 0 2 94\r\n9 2 0 43\r\n10 1 0 91\r\n", "output": "300\r\n"}, {"input": "2 4 10\r\n20 0 1 7\r\n9 2 0 32\r\n10 1 0 27\r\n21 0 2 19\r\n", "output": "-1\r\n"}, {"input": "2 4 9\r\n10 1 0 22\r\n21 0 2 92\r\n9 2 0 29\r\n20 0 1 37\r\n", "output": "180\r\n"}, {"input": "3 6 1\r\n10 1 0 62\r\n8 3 0 83\r\n20 0 1 28\r\n22 0 3 61\r\n21 0 2 61\r\n9 2 0 75\r\n", "output": "370\r\n"}, {"input": "3 6 10\r\n22 0 3 71\r\n20 0 1 57\r\n8 3 0 42\r\n10 1 0 26\r\n9 2 0 35\r\n21 0 2 84\r\n", "output": "-1\r\n"}, {"input": "3 6 9\r\n10 1 0 93\r\n20 0 1 26\r\n8 3 0 51\r\n22 0 3 90\r\n21 0 2 78\r\n9 2 0 65\r\n", "output": "403\r\n"}, {"input": "4 8 1\r\n9 2 0 3\r\n22 0 3 100\r\n20 0 1 40\r\n10 1 0 37\r\n23 0 4 49\r\n7 4 0 53\r\n21 0 2 94\r\n8 3 0 97\r\n", "output": "473\r\n"}, {"input": "4 8 10\r\n8 3 0 65\r\n21 0 2 75\r\n7 4 0 7\r\n23 0 4 38\r\n20 0 1 27\r\n10 1 0 33\r\n22 0 3 91\r\n9 2 0 27\r\n", "output": "-1\r\n"}, {"input": "4 8 9\r\n8 3 0 61\r\n9 2 0 94\r\n23 0 4 18\r\n21 0 2 19\r\n20 0 1 52\r\n10 1 0 68\r\n22 0 3 5\r\n7 4 0 59\r\n", "output": "376\r\n"}, {"input": "5 10 1\r\n24 0 5 61\r\n22 0 3 36\r\n8 3 0 7\r\n21 0 2 20\r\n6 5 0 23\r\n20 0 1 28\r\n23 0 4 18\r\n9 2 0 40\r\n7 4 0 87\r\n10 1 0 8\r\n", "output": "328\r\n"}, {"input": "5 10 10\r\n24 0 5 64\r\n23 0 4 17\r\n20 0 1 91\r\n9 2 0 35\r\n21 0 2 4\r\n22 0 3 51\r\n6 5 0 69\r\n7 4 0 46\r\n8 3 0 92\r\n10 1 0 36\r\n", "output": "-1\r\n"}, {"input": "5 10 9\r\n22 0 3 13\r\n9 2 0 30\r\n24 0 5 42\r\n21 0 2 33\r\n23 0 4 36\r\n20 0 1 57\r\n10 1 0 39\r\n8 3 0 68\r\n7 4 0 85\r\n6 5 0 35\r\n", "output": "438\r\n"}, {"input": "1 10 1\r\n278 1 0 4\r\n208 1 0 4\r\n102 0 1 9\r\n499 0 1 7\r\n159 0 1 8\r\n218 1 0 6\r\n655 0 1 5\r\n532 1 0 6\r\n318 0 1 6\r\n304 1 0 7\r\n", "output": "9\r\n"}, {"input": "2 10 1\r\n5 0 2 5\r\n52 2 0 9\r\n627 0 2 6\r\n75 0 1 6\r\n642 0 1 8\r\n543 0 2 7\r\n273 1 0 2\r\n737 2 0 4\r\n576 0 1 7\r\n959 0 2 5\r\n", "output": "23\r\n"}, {"input": "3 10 1\r\n48 2 0 9\r\n98 0 2 5\r\n43 0 1 8\r\n267 0 1 7\r\n394 3 0 7\r\n612 0 3 9\r\n502 2 0 6\r\n36 0 2 9\r\n602 0 1 3\r\n112 1 0 6\r\n", "output": "-1\r\n"}, {"input": "4 10 1\r\n988 0 1 1\r\n507 1 0 9\r\n798 1 0 9\r\n246 0 3 7\r\n242 1 0 8\r\n574 4 0 7\r\n458 0 4 9\r\n330 0 2 9\r\n303 2 0 8\r\n293 0 3 9\r\n", "output": "-1\r\n"}, {"input": "5 10 1\r\n132 0 4 7\r\n803 0 2 8\r\n280 3 0 5\r\n175 4 0 6\r\n196 1 0 7\r\n801 0 4 6\r\n320 0 5 7\r\n221 0 4 6\r\n446 4 0 8\r\n699 0 5 9\r\n", "output": "-1\r\n"}, {"input": "6 10 1\r\n845 0 4 9\r\n47 0 4 8\r\n762 0 2 8\r\n212 6 0 6\r\n416 0 5 9\r\n112 5 0 9\r\n897 0 6 9\r\n541 0 4 5\r\n799 0 6 7\r\n252 2 0 9\r\n", "output": "-1\r\n"}, {"input": "7 10 1\r\n369 6 0 9\r\n86 7 0 9\r\n696 0 4 8\r\n953 6 0 7\r\n280 4 0 9\r\n244 0 2 9\r\n645 6 0 8\r\n598 7 0 6\r\n598 0 7 8\r\n358 0 4 6\r\n", "output": "-1\r\n"}, {"input": "8 10 1\r\n196 2 0 9\r\n67 2 0 9\r\n372 3 0 6\r\n886 6 0 6\r\n943 0 3 8\r\n430 3 0 6\r\n548 0 4 9\r\n522 0 3 8\r\n1 4 0 3\r\n279 4 0 8\r\n", "output": "-1\r\n"}, {"input": "9 10 1\r\n531 8 0 5\r\n392 2 0 9\r\n627 8 0 9\r\n363 5 0 9\r\n592 0 5 3\r\n483 0 6 7\r\n104 3 0 8\r\n97 8 0 9\r\n591 0 7 9\r\n897 0 6 7\r\n", "output": "-1\r\n"}, {"input": "10 10 1\r\n351 0 3 7\r\n214 0 9 9\r\n606 0 7 8\r\n688 0 9 3\r\n188 3 0 9\r\n994 0 1 7\r\n372 5 0 8\r\n957 0 3 6\r\n458 8 0 7\r\n379 0 4 7\r\n", "output": "-1\r\n"}, {"input": "1 2 1\r\n5 0 1 91\r\n1 1 0 87\r\n", "output": "178\r\n"}, {"input": "2 4 1\r\n1 1 0 88\r\n5 2 0 88\r\n3 0 1 46\r\n9 0 2 63\r\n", "output": "-1\r\n"}, {"input": "3 6 1\r\n19 0 3 80\r\n11 0 2 32\r\n8 2 0 31\r\n4 0 1 45\r\n1 1 0 63\r\n15 3 0 76\r\n", "output": "-1\r\n"}, {"input": "1 0 1\r\n", "output": "-1\r\n"}, {"input": "5 0 1\r\n", "output": "-1\r\n"}]
false
stdio
null
true
863/E
863
E
Python 3
TESTS
3
31
0
174565342
n=int(input()) li=[] d={} for i in range(n): start,end=list(map(int,input().split())) li.append([start,end]) if (start,end) not in d: d[(start,end)]=i+1 li.sort() start,end=li[0] isSingle=True flag=False for i in range(1,n): l,r=li[i] if r<=end: print(d[(l,r)]) flag=True break else: if l>start: start,end=l,r else: if l==start and isSingle: print(d[(start,end)]) flag=True break end=r isSingle=False if not flag: print(-1)
55
904
35,942,400
169894468
import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n = int(input()) LRI = [] for i in range(n): l, r = map(int, input().split()) LRI.append((l, r, i)) LRI.sort() cur_l = LRI[0][0] cur_r = LRI[0][1] cur_idx = LRI[0][2] for j in range(1, n): l, r, i = LRI[j] if l <= cur_l and cur_r <= r: print(cur_idx+1) exit() if r <= cur_r: print(i+1) exit() else: cur_l = max(cur_r+1, l) cur_r = r cur_idx = i print(-1)
Educational Codeforces Round 29
ICPC
2,017
2
256
Turn Off The TV
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.
null
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[{"input": "3\n1 3\n4 6\n1 7", "output": "1"}, {"input": "2\n0 10\n0 10", "output": "1"}, {"input": "3\n1 2\n3 4\n6 8", "output": "-1"}, {"input": "3\n1 2\n2 3\n3 4", "output": "2"}]
2,000
["data structures", "sortings"]
55
[{"input": "3\r\n1 3\r\n4 6\r\n1 7\r\n", "output": "1\r\n"}, {"input": "2\r\n0 10\r\n0 10\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2\r\n3 4\r\n6 8\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 500000000\r\n500000001 1000000000\r\n0 1000000000\r\n", "output": "1\r\n"}, {"input": "3\r\n1 5\r\n2 4\r\n6 10\r\n", "output": "2\r\n"}, {"input": "10\r\n4 4\r\n5 9\r\n5 7\r\n2 8\r\n6 10\r\n4 10\r\n1 3\r\n8 9\r\n0 0\r\n5 7\r\n", "output": "1\r\n"}, {"input": "2\r\n1 3\r\n2 4\r\n", "output": "-1\r\n"}, {"input": "1\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "8\r\n13 17\r\n83 89\r\n31 33\r\n7 13\r\n52 52\r\n88 89\r\n29 30\r\n16 22\r\n", "output": "6\r\n"}, {"input": "4\r\n63 63\r\n12 34\r\n17 29\r\n58 91\r\n", "output": "1\r\n"}, {"input": "3\r\n1 10\r\n5 15\r\n10 20\r\n", "output": "2\r\n"}, {"input": "2\r\n1 3\r\n1 6\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 6\r\n1 3\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 4\r\n2 100\r\n4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 1\r\n3 3\r\n4 7\r\n4 5\r\n", "output": "4\r\n"}, {"input": "3\r\n2 3\r\n3 4\r\n1 2\r\n", "output": "1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "-1\r\n"}, {"input": "6\r\n99 100\r\n65 65\r\n34 34\r\n16 18\r\n65 67\r\n88 88\r\n", "output": "2\r\n"}, {"input": "2\r\n50 67\r\n54 64\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3\r\n2 100\r\n3 5\r\n", "output": "3\r\n"}, {"input": "3\r\n57 90\r\n35 45\r\n18 52\r\n", "output": "2\r\n"}, {"input": "4\r\n14 15\r\n46 73\r\n15 40\r\n28 53\r\n", "output": "-1\r\n"}, {"input": "3\r\n37 38\r\n51 54\r\n28 28\r\n", "output": "-1\r\n"}, {"input": "2\r\n64 66\r\n47 61\r\n", "output": "-1\r\n"}, {"input": "4\r\n50 68\r\n63 67\r\n67 69\r\n11 12\r\n", "output": "2\r\n"}, {"input": "4\r\n42 62\r\n93 103\r\n34 62\r\n5 12\r\n", "output": "1\r\n"}, {"input": "6\r\n42 60\r\n78 107\r\n6 38\r\n58 81\r\n70 105\r\n70 105\r\n", "output": "5\r\n"}, {"input": "5\r\n71 71\r\n21 22\r\n58 58\r\n57 57\r\n16 16\r\n", "output": "-1\r\n"}, {"input": "7\r\n28 42\r\n70 75\r\n83 92\r\n19 22\r\n26 32\r\n85 99\r\n30 39\r\n", "output": "7\r\n"}, {"input": "3\r\n8 28\r\n80 110\r\n39 81\r\n", "output": "-1\r\n"}, {"input": "7\r\n90 115\r\n87 113\r\n2 26\r\n39 40\r\n91 112\r\n42 53\r\n65 79\r\n", "output": "5\r\n"}, {"input": "7\r\n12 13\r\n26 28\r\n9 11\r\n15 15\r\n8 10\r\n22 24\r\n5 7\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 5\r\n26 31\r\n11 15\r\n2 4\r\n16 18\r\n4 4\r\n7 12\r\n", "output": "6\r\n"}, {"input": "3\r\n1 5\r\n1 2\r\n4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n999999995 999999997\r\n999999998 1000000000\r\n999999996 999999999\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2\r\n4 6\r\n4 10\r\n200 300\r\n", "output": "2\r\n"}]
false
stdio
import bisect def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) intervals = [] for _ in range(n): l, r = map(int, f.readline().split()) intervals.append((l, r)) # Compute redundant TVs points = set() for l, r in intervals: points.add(l - 1) points.add(l) points.add(r) points.add(r + 1) sorted_points = sorted(points) m = len(sorted_points) diff = [0] * (m + 1) for l, r in intervals: idx_l = bisect.bisect_left(sorted_points, l) assert idx_l < m and sorted_points[idx_l] == l diff[idx_l] += 1 idx_r_plus_1 = bisect.bisect_left(sorted_points, r + 1) assert idx_r_plus_1 < m and sorted_points[idx_r_plus_1] == r + 1 diff[idx_r_plus_1] -= 1 coverage = [0] * m current = 0 for i in range(m): current += diff[i] coverage[i] = current single = [0] * m for i in range(m): single[i] = 1 if coverage[i] == 1 else 0 pref = [0] * (m + 1) for i in range(m): pref[i + 1] = pref[i] + single[i] redundant = [] for i in range(n): l, r = intervals[i] l_minus_1 = l - 1 idx_l_minus_1 = bisect.bisect_left(sorted_points, l_minus_1) assert idx_l_minus_1 < m and sorted_points[idx_l_minus_1] == l_minus_1 idx_r = bisect.bisect_left(sorted_points, r) assert idx_r < m and sorted_points[idx_r] == r sum_single = pref[idx_r + 1] - pref[idx_l_minus_1 + 1] if sum_single == 0: redundant.append(i + 1) # Read submission output with open(submission_output_path) as f: submission_output = f.readline().strip() try: k = int(submission_output) except: print(0) return if k == -1: if not redundant: print(1) else: print(0) else: if 1 <= k <= n and k in redundant: print(1) else: print(0) if __name__ == "__main__": import sys input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
81/B
81
B
PyPy 3
TESTS
24
280
0
92889862
s = input() s = s.split(",") def clean(i): i = [ j for j in i.split(" ") if len(j) ] return " ".join(i) s = [ clean(i) for i in s] s = [ " ...".join(i.split("...")) if "." in i else i for i in s ] s = [ i.strip() for i in s] s = ", ".join(s) print(s.strip())
45
124
409,600
197532557
# LUOGU_RID: 104833499 import re s = input() s = re.sub('\s*,', ', ', s) s = re.sub('\s*\.\.\.\s*', ' ...', s) s = re.sub('\s*\ \ \s*', ' ', s) print(s.strip())
Yandex.Algorithm Open 2011: Qualification 1
CF
2,011
2
256
Sequence Formatting
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "1,2 ,3,...,   10" will be corrected to "1, 2, 3, ..., 10". In this task you are given a string s, which is composed by a concatination of terms, each of which may be: - a positive integer of an arbitrary length (leading zeroes are not allowed), - a "comma" symbol (","), - a "space" symbol (" "), - "three dots" ("...", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string s to ensure the following: - each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), - each "three dots" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), - if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, - there should not be other spaces. Automate Polycarp's work and write a program that will process the given string s.
The input data contains a single string s. Its length is from 1 to 255 characters. The string s does not begin and end with a space. Its content matches the description given above.
Print the string s after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it.
null
null
[{"input": "1,2 ,3,..., 10", "output": "1, 2, 3, ..., 10"}, {"input": "1,,,4...5......6", "output": "1, , , 4 ...5 ... ...6"}, {"input": "...,1,2,3,...", "output": "..., 1, 2, 3, ..."}]
1,700
["implementation", "strings"]
45
[{"input": "1,2 ,3,..., 10\r\n", "output": "1, 2, 3, ..., 10\r\n"}, {"input": "1,,,4...5......6\r\n", "output": "1, , , 4 ...5 ... ...6\r\n"}, {"input": "...,1,2,3,...\r\n", "output": "..., 1, 2, 3, ...\r\n"}, {"input": "......\r\n", "output": "... ...\r\n"}, {"input": ",,,,,,,,,,,,,\r\n", "output": ", , , , , , , , , , , , ,\r\n"}, {"input": "123456789\r\n", "output": "123456789\r\n"}, {"input": ",\r\n", "output": ",\r\n"}, {"input": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n", "output": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n"}, {"input": "1,2,,,,,,,,,5566\r\n", "output": "1, 2, , , , , , , , , 5566\r\n"}, {"input": ".........1......1......23......31...2......\r\n", "output": "... ... ...1 ... ...1 ... ...23 ... ...31 ...2 ... ...\r\n"}, {"input": "...\r\n", "output": "...\r\n"}, {"input": ",...\r\n", "output": ", ...\r\n"}, {"input": "...,\r\n", "output": "...,\r\n"}, {"input": ",,\r\n", "output": ", ,\r\n"}, {"input": ",...,\r\n", "output": ", ...,\r\n"}, {"input": "...,...\r\n", "output": "..., ...\r\n"}, {"input": "1...10\r\n", "output": "1 ...10\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ,\r\n", "output": ", ,\r\n"}, {"input": "............................................................................................................................................................................................................................................................\r\n", "output": "... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ..."}, {"input": "1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1\r\n", "output": "1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1"}, {"input": "12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12\r\n", "output": "12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12\r\n"}, {"input": "1,\r\n", "output": "1,\r\n"}, {"input": ",1\r\n", "output": ", 1\r\n"}, {"input": "1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n", "output": "1 1\r\n"}, {"input": "1 1 1\r\n", "output": "1 1 1\r\n"}, {"input": "... ,\r\n", "output": "...,\r\n"}, {"input": "......,,,,,...... 1234 1234 1234 , 1234 ... , 1234 ... 1234 ... , 1234\r\n", "output": "... ..., , , , , ... ...1234 1234 1234, 1234 ..., 1234 ...1234 ..., 1234\r\n"}, {"input": "9999999999999999999999999999999999999999999999999999999999, 1\r\n", "output": "9999999999999999999999999999999999999999999999999999999999, 1\r\n"}, {"input": "5555555555555555555 6666 77777777 8888888888888 ,,...,55,5...55...,.........5 , , ... , 5 5 , 5 ... 5 5 ... , ... ... ... 5\r\n", "output": "5555555555555555555 6666 77777777 8888888888888, , ..., 55, 5 ...55 ..., ... ... ...5, , ..., 5 5, 5 ...5 5 ..., ... ... ...5\r\n"}, {"input": "...5\r\n", "output": "...5\r\n"}, {"input": ", 12 12, ...\r\n", "output": ", 12 12, ...\r\n"}, {"input": ",12 12,...\r\n", "output": ", 12 12, ...\r\n"}, {"input": "1,2 4,78 799,4...5 3......6,\r\n", "output": "1, 2 4, 78 799, 4 ...5 3 ... ...6,\r\n"}]
false
stdio
null
true
269/A
269
A
PyPy 3-64
TESTS
23
810
16,588,800
210815994
import sys input = lambda: sys.stdin.readline().rstrip() N = int(input()) A = [] for _ in range(N): k,a = map(int, input().split()) A.append([k,a]) A.sort() pre = 0 for i,(k,a) in enumerate(A): if pre: a+=1 pre=0 added = False while a>4: a = (a-1)//4+1 k+=1 if i+1<N and A[i+1][0]==k: A[i+1][1]=max(A[i+1][1], a) added = True break if a and not added: pre = 1 print(k+1)
46
528
7,884,800
143307621
t = [tuple(map(int, input().split())) for i in range(int(input()))] print((max(2 * x + len(bin(y - 1)) for x, y in t) + 1) // 2 - 1)
Codeforces Round 165 (Div. 1)
CF
2,013
2
256
Magical Boxes
Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes. From the top view each magical box looks like a square with side length equal to 2k (k is an integer, k ≥ 0) units. A magical box v can be put inside a magical box u, if side length of v is strictly less than the side length of u. In particular, Emuskald can put 4 boxes of side length 2k - 1 into one box of side length 2k, or as in the following figure: Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.
The first line of input contains an integer n (1 ≤ n ≤ 105), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers ki and ai (0 ≤ ki ≤ 109, 1 ≤ ai ≤ 109), which means that Emuskald has ai boxes with side length 2ki. It is guaranteed that all of ki are distinct.
Output a single integer p, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2p.
null
Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture. In the second test case, we can put all four small boxes into a box with side length 2.
[{"input": "2\n0 3\n1 5", "output": "3"}, {"input": "1\n0 4", "output": "1"}, {"input": "2\n1 10\n2 2", "output": "3"}]
1,600
["greedy", "math"]
46
[{"input": "2\r\n0 3\r\n1 5\r\n", "output": "3\r\n"}, {"input": "1\r\n0 4\r\n", "output": "1\r\n"}, {"input": "2\r\n1 10\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000 1000000000\r\n", "output": "1000000015\r\n"}, {"input": "1\r\n0 16\r\n", "output": "2\r\n"}, {"input": "1\r\n0 17\r\n", "output": "3\r\n"}, {"input": "2\r\n0 16\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 20\r\n1 18\r\n2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n0 64\r\n1 16\r\n2 4\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1000000\r\n100 100\r\n101 9\r\n102 4\r\n103 8\r\n", "output": "105\r\n"}, {"input": "10\r\n11 69\r\n7 56\r\n8 48\r\n2 56\r\n12 6\r\n9 84\r\n1 81\r\n4 80\r\n3 9\r\n5 18\r\n", "output": "15\r\n"}, {"input": "15\r\n0 268435456\r\n1 67108864\r\n2 16777216\r\n3 4194304\r\n4 1048576\r\n5 262144\r\n6 65536\r\n7 16384\r\n8 4096\r\n9 1024\r\n10 256\r\n11 64\r\n12 16\r\n13 4\r\n14 1\r\n", "output": "15\r\n"}, {"input": "14\r\n0 268435456\r\n1 67108864\r\n2 16777216\r\n3 4194304\r\n4 1048576\r\n5 262144\r\n6 65536\r\n7 16384\r\n8 4096\r\n9 1024\r\n10 256\r\n11 64\r\n12 16\r\n13 4\r\n", "output": "14\r\n"}, {"input": "14\r\n0 268435456\r\n1 67108864\r\n2 16777216\r\n3 4194305\r\n4 1048576\r\n5 262144\r\n6 65536\r\n7 16384\r\n8 4096\r\n9 1024\r\n10 256\r\n11 64\r\n12 16\r\n13 4\r\n", "output": "15\r\n"}, {"input": "14\r\n0 268435457\r\n1 67108865\r\n2 16777217\r\n3 4194305\r\n4 1048577\r\n5 262145\r\n6 65537\r\n7 16383\r\n8 4097\r\n9 1025\r\n10 257\r\n11 65\r\n12 17\r\n13 4\r\n", "output": "15\r\n"}, {"input": "16\r\n1296 2\r\n1568 1\r\n7435 2\r\n3660 1\r\n6863 2\r\n886 2\r\n2596 1\r\n7239 1\r\n6146 1\r\n5634 1\r\n3119 2\r\n1166 2\r\n7610 2\r\n5992 1\r\n630 2\r\n8491 2\r\n", "output": "8492\r\n"}, {"input": "17\r\n8028 11\r\n4011 32\r\n8609 52\r\n1440 25\r\n6752 42\r\n536 47\r\n761 15\r\n2749 60\r\n5363 62\r\n7170 23\r\n9734 10\r\n8487 28\r\n6147 20\r\n5257 54\r\n821 49\r\n7219 19\r\n6150 43\r\n", "output": "9736\r\n"}, {"input": "3\r\n1 4\r\n0 17\r\n2 4\r\n", "output": "3\r\n"}, {"input": "2\r\n0 13\r\n1 1\r\n", "output": "2\r\n"}, {"input": "2\r\n0 2\r\n1 4\r\n", "output": "2\r\n"}, {"input": "2\r\n100 52\r\n101 4\r\n", "output": "103\r\n"}, {"input": "10\r\n1 1\r\n10 1\r\n100 1\r\n1000 1\r\n10000 1\r\n100000 1\r\n1000000 1\r\n10000000 1\r\n100000000 1\r\n1000000000 1\r\n", "output": "1000000001\r\n"}, {"input": "1\r\n0 268435456\r\n", "output": "14\r\n"}, {"input": "1\r\n0 268435457\r\n", "output": "15\r\n"}, {"input": "1\r\n1 1000000000\r\n", "output": "16\r\n"}, {"input": "9\r\n0 268435456\r\n2 16777216\r\n3 4194304\r\n6 65536\r\n7 16384\r\n8 4096\r\n11 64\r\n12 16\r\n14 1\r\n", "output": "15\r\n"}, {"input": "8\r\n0 268435456\r\n2 16777216\r\n3 4194304\r\n6 65536\r\n7 16384\r\n8 4096\r\n11 64\r\n12 16\r\n", "output": "14\r\n"}, {"input": "8\r\n0 268435456\r\n2 16777216\r\n3 4195304\r\n6 65536\r\n7 16384\r\n8 4096\r\n11 64\r\n12 16\r\n", "output": "15\r\n"}, {"input": "7\r\n1 16777216\r\n2 1000000000\r\n5 65537\r\n6 16384\r\n7 4096\r\n10 64\r\n11 16\r\n", "output": "17\r\n"}, {"input": "10\r\n0 689790649\r\n10 494030130\r\n6 210447956\r\n3 993638872\r\n7 116080273\r\n8 802686533\r\n2 677383122\r\n9 936407280\r\n4 593615642\r\n1 853659941\r\n", "output": "25\r\n"}, {"input": "2\r\n0 16777216\r\n16 1\r\n", "output": "17\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2\r\n"}, {"input": "2\r\n21 134217728\r\n1 1024\r\n", "output": "35\r\n"}, {"input": "2\r\n10 3\r\n25 747\r\n", "output": "30\r\n"}]
false
stdio
null
true
14/B
14
B
PyPy 3
TESTS
6
248
0
101239646
n, x = [int(j) for j in input().split()] ref = 1001 * [0] for j in range(n): a, b = sorted([int(j) for j in input().split()]) for k in range(a, b + 1): ref[k] += 1 def check(index): if index >= 0 and index < 1001: return ref[index] == n return False d = 0 while True: if check(x - d) == True or check(x + d) == True: break d += 1 if d > n: d = -1 break print(d)
52
92
0
193742889
n,x=[int(x) for x in input().split()];l1=[];l2=[] for i in range(n): a=[int(x) for x in input().split()]; a.sort() l1.append(a[0]);l2.append(a[1]) start=max(l1);end=min(l2) if start>end: print(-1) elif x<start: print(start-x) elif x>end: print(x-end) else: print(0)
Codeforces Beta Round 14 (Div. 2)
ICPC
2,010
2
64
Young Photographer
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2 What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
null
null
[{"input": "3 3\n0 7\n14 2\n4 6", "output": "1"}]
1,000
["implementation"]
52
[{"input": "3 3\r\n0 7\r\n14 2\r\n4 6\r\n", "output": "1\r\n"}, {"input": "1 1\r\n0 10\r\n", "output": "0\r\n"}, {"input": "2 2\r\n1 2\r\n3 2\r\n", "output": "0\r\n"}, {"input": "3 2\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "-1\r\n"}, {"input": "2 4\r\n10 4\r\n1 5\r\n", "output": "0\r\n"}, {"input": "1 10\r\n1 9\r\n", "output": "1\r\n"}, {"input": "1 10\r\n123 12\r\n", "output": "2\r\n"}, {"input": "1 17\r\n10 17\r\n", "output": "0\r\n"}, {"input": "1 22\r\n22 33\r\n", "output": "0\r\n"}, {"input": "1 3\r\n1 2\r\n", "output": "1\r\n"}, {"input": "2 5\r\n0 3\r\n2 1\r\n", "output": "3\r\n"}, {"input": "3 3\r\n7 3\r\n6 4\r\n3 7\r\n", "output": "1\r\n"}, {"input": "4 9\r\n8 6\r\n11 5\r\n5 11\r\n8 3\r\n", "output": "1\r\n"}, {"input": "2 4\r\n1 4\r\n4 0\r\n", "output": "0\r\n"}, {"input": "3 7\r\n5 8\r\n7 5\r\n4 7\r\n", "output": "0\r\n"}, {"input": "4 7\r\n8 2\r\n5 7\r\n8 2\r\n5 8\r\n", "output": "0\r\n"}, {"input": "2 3\r\n4 1\r\n4 1\r\n", "output": "0\r\n"}, {"input": "3 8\r\n7 2\r\n3 7\r\n5 2\r\n", "output": "3\r\n"}, {"input": "4 0\r\n9 1\r\n8 1\r\n8 4\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 7\r\n2 5\r\n3 6\r\n3 5\r\n7 4\r\n", "output": "2\r\n"}, {"input": "10 16\r\n4 18\r\n6 19\r\n22 1\r\n23 0\r\n1 22\r\n9 22\r\n4 19\r\n0 14\r\n6 14\r\n0 16\r\n", "output": "2\r\n"}, {"input": "20 1\r\n35 8\r\n40 6\r\n49 5\r\n48 18\r\n46 16\r\n45 16\r\n44 10\r\n16 44\r\n8 46\r\n2 45\r\n38 3\r\n42 1\r\n13 35\r\n35 18\r\n12 33\r\n32 11\r\n31 3\r\n50 20\r\n47 6\r\n38 2\r\n", "output": "19\r\n"}, {"input": "30 43\r\n17 72\r\n75 26\r\n23 69\r\n83 30\r\n15 82\r\n4 67\r\n83 27\r\n33 62\r\n26 83\r\n70 26\r\n69 25\r\n16 67\r\n77 26\r\n66 33\r\n7 88\r\n70 9\r\n10 79\r\n76 9\r\n30 77\r\n77 28\r\n21 68\r\n81 14\r\n13 72\r\n88 15\r\n60 29\r\n87 28\r\n16 58\r\n6 58\r\n71 9\r\n83 18\r\n", "output": "0\r\n"}, {"input": "5 12\r\n2 7\r\n7 5\r\n3 10\r\n11 3\r\n2 11\r\n", "output": "5\r\n"}, {"input": "15 15\r\n12 37\r\n40 4\r\n38 8\r\n5 36\r\n11 31\r\n21 33\r\n9 37\r\n4 38\r\n8 33\r\n5 39\r\n7 39\r\n38 16\r\n16 41\r\n38 9\r\n5 32\r\n", "output": "6\r\n"}, {"input": "25 40\r\n66 26\r\n56 19\r\n64 38\r\n64 23\r\n25 49\r\n51 26\r\n67 20\r\n65 35\r\n33 66\r\n28 63\r\n27 57\r\n40 56\r\n59 26\r\n35 56\r\n39 67\r\n30 63\r\n69 22\r\n21 63\r\n67 22\r\n20 66\r\n26 65\r\n64 26\r\n44 57\r\n57 41\r\n35 50\r\n", "output": "4\r\n"}, {"input": "1 21\r\n973 373\r\n", "output": "352\r\n"}, {"input": "2 212\r\n831 551\r\n810 753\r\n", "output": "541\r\n"}, {"input": "3 404\r\n690 728\r\n820 260\r\n186 402\r\n", "output": "-1\r\n"}, {"input": "4 906\r\n548 906\r\n830 457\r\n228 638\r\n464 167\r\n", "output": "-1\r\n"}, {"input": "5 97\r\n97 393\r\n840 965\r\n269 183\r\n596 49\r\n975 62\r\n", "output": "-1\r\n"}, {"input": "3 183\r\n416 335\r\n773 648\r\n434 198\r\n", "output": "-1\r\n"}, {"input": "3 868\r\n251 927\r\n862 464\r\n157 756\r\n", "output": "112\r\n"}, {"input": "3 242\r\n397 208\r\n951 279\r\n570 622\r\n", "output": "-1\r\n"}, {"input": "3 618\r\n543 800\r\n38 94\r\n293 179\r\n", "output": "-1\r\n"}, {"input": "3 993\r\n378 81\r\n127 911\r\n16 737\r\n", "output": "615\r\n"}, {"input": "5 12\r\n11 1\r\n9 6\r\n1 11\r\n3 8\r\n874 842\r\n", "output": "-1\r\n"}, {"input": "15 16\r\n11 40\r\n5 32\r\n5 31\r\n36 10\r\n34 9\r\n43 6\r\n28 6\r\n34 8\r\n43 15\r\n9 28\r\n14 34\r\n34 6\r\n7 31\r\n31 14\r\n68 478\r\n", "output": "-1\r\n"}, {"input": "25 57\r\n47 31\r\n64 21\r\n43 56\r\n47 19\r\n70 27\r\n28 61\r\n41 61\r\n39 45\r\n46 21\r\n55 35\r\n70 22\r\n22 69\r\n30 67\r\n55 42\r\n37 58\r\n50 28\r\n57 42\r\n35 48\r\n68 40\r\n38 50\r\n62 20\r\n31 52\r\n38 70\r\n64 35\r\n666 393\r\n", "output": "-1\r\n"}, {"input": "1 21\r\n0 1000\r\n", "output": "0\r\n"}]
false
stdio
null
true
847/E
847
E
Python 3
TESTS
3
31
102,400
205386709
input() treats_positions = [] pacman_positions = [] for i, cell_type in enumerate(input()): if cell_type == '*': treats_positions.append(i) elif cell_type == 'P': pacman_positions.append(i) def is_enough_go_left_first(time_limit) -> bool: t_i = 0 for pacman_position in pacman_positions: left_most_treat = treats_positions[t_i] if left_most_treat < pacman_position - time_limit: return False while t_i < len(treats_positions) and treats_positions[t_i] < pacman_position: t_i += 1 remaining_right_width = max(0, time_limit - 2 * (pacman_position - left_most_treat)) while t_i < len(treats_positions) and treats_positions[t_i] <= pacman_position + remaining_right_width: t_i += 1 if t_i == len(treats_positions): return True return False def is_enough_go_right_first(time_limit) -> bool: t_i = len(treats_positions) - 1 for pacman_position in pacman_positions[::-1]: right_most_treat = treats_positions[t_i] if right_most_treat > pacman_position + time_limit: return False while t_i >= 0 and pacman_position < treats_positions[t_i]: t_i -= 1 remaining_left_width = max(0, time_limit - 2 * (right_most_treat - pacman_position)) while t_i >= 0 and pacman_position - remaining_left_width <= treats_positions[t_i]: t_i -= 1 if t_i == -1: return True return False l = max(abs(pacman_positions[0] - treats_positions[0]), abs(pacman_positions[-1] - treats_positions[-1])) r = 2 * (treats_positions[-1] - treats_positions[0]) while l < r: mid = (l + r) // 2 if is_enough_go_left_first(time_limit=mid) or is_enough_go_right_first(time_limit=mid): r = mid else: l = mid + 1 print(l)
77
202
5,120,000
218466649
n = int(input()) string = input() player = [] food = [] for i in range(len(string)): if string[i] == "*": food.append(i) elif string[i] == "P": player.append(i) def check(t): idx = 0 for i in range(len(player)): loc = player[i] if idx == len(food):break l = food[idx] while idx!=len(food): r = food[idx] if (((abs(loc-l)+abs(l-r))<=t) or ((abs(l-r)+abs(loc-r))<=t)): idx+=1 else: break return idx>=len(food) l = 0 h = pow(10,18) while l<=h: mid = l+(h-l)//2 if check(mid): ans = mid h = mid-1 else: l= mid+1 print(ans)
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
1
256
Packmen
A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty. Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk. In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
The first line contains a single integer n (2 ≤ n ≤ 105) — the length of the game field. The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i. It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Print minimum possible time after which Packmen can eat all asterisks.
null
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field. In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
[{"input": "7\n*..P*P*", "output": "3"}, {"input": "10\n.**PP.*P.*", "output": "2"}]
1,800
["binary search", "dp"]
77
[{"input": "7\r\n*..P*P*\r\n", "output": "3\r\n"}, {"input": "10\r\n.**PP.*P.*\r\n", "output": "2\r\n"}, {"input": "19\r\n**P.*..*..P..*.*P**\r\n", "output": "7\r\n"}, {"input": "12\r\nP**.*P*P*P**\r\n", "output": "3\r\n"}, {"input": "17\r\n.*P*P**P**.**P...\r\n", "output": "3\r\n"}, {"input": "58\r\n..P.P*.P*.P...PPP...P*....*..*.**......*P.*P.....**P...*P*\r\n", "output": "9\r\n"}, {"input": "10\r\n..P*.P.*.*\r\n", "output": "4\r\n"}, {"input": "10\r\n***.*.*..P\r\n", "output": "9\r\n"}, {"input": "15\r\nP***..PPP..P*.P\r\n", "output": "3\r\n"}, {"input": "15\r\n*.*....*P......\r\n", "output": "8\r\n"}, {"input": "20\r\n.P**P**P**PP.PP**PP*\r\n", "output": "2\r\n"}, {"input": "20\r\n.....*.**..........P\r\n", "output": "14\r\n"}, {"input": "25\r\nP*P*P.**.PPPP.*.P.*..P...\r\n", "output": "2\r\n"}, {"input": "25\r\n...*..**..*.....*..*...P.\r\n", "output": "20\r\n"}, {"input": "30\r\n*P.*...*.**..P**...***.*...**.\r\n", "output": "15\r\n"}, {"input": "30\r\n.*...*.......................P\r\n", "output": "28\r\n"}, {"input": "35\r\n..PP.P....*PP.*.PPPP.*P.P.PPPP.*.P.\r\n", "output": "2\r\n"}, {"input": "35\r\n....*..*.*.*.....*.*..P*...*...*...\r\n", "output": "36\r\n"}, {"input": "40\r\n...**P*P*...P.*PP***.*..P..**.**PP**.*.*\r\n", "output": "6\r\n"}, {"input": "40\r\nP*....*.*....*...*..*.......*...**..***.\r\n", "output": "38\r\n"}, {"input": "45\r\nP.P*..P....*P.*PP*PP*.**P...PP*PP*.P.P..PP.PP\r\n", "output": "2\r\n"}, {"input": "45\r\n*.*.*..*.*.**.*..**..*.....**.**P....*****.**\r\n", "output": "56\r\n"}, {"input": "50\r\n*PP....PPPP*....*P*P..PPPPPP...***P*P.........PP..\r\n", "output": "3\r\n"}, {"input": "50\r\n*..***.*.****.*....P*.**...***.......**....*.***..\r\n", "output": "66\r\n"}, {"input": "55\r\n......P.*.....P*.*P....*..P*.P.P....**....*..........*.\r\n", "output": "22\r\n"}, {"input": "55\r\n*.....*.*..**..*...***..**.**.*.*.P..*.*.**...**.*..*.*\r\n", "output": "74\r\n"}, {"input": "60\r\n.P...P.PPP.P....P...PP.*......**...P.*.P.P*P.*...P*P..P.P..P\r\n", "output": "5\r\n"}, {"input": "60\r\n..*....*...***....P...........*............*....**....*...**\r\n", "output": "73\r\n"}, {"input": "65\r\n......PP..PP**.***.*.P.P..PP.....**PP*PP.....P..P*PP.*.*P..P*P*..\r\n", "output": "5\r\n"}, {"input": "65\r\n.....*..*....*.*......P....*.....**....*.........*...............\r\n", "output": "61\r\n"}, {"input": "70\r\n*P.*..**P*P.....*PP.......*..PP...P...P.**..P.*......*P*.**.P..*P.....\r\n", "output": "4\r\n"}, {"input": "70\r\n*..***.**..**....***........*.**...*...**.**..*.......**P*..*.......**\r\n", "output": "82\r\n"}, {"input": "75\r\n..***P*.**.P.**P.**.***.P*..**P.P*.P*.**.....*PP..P***....**PPP..**P..P..P*\r\n", "output": "6\r\n"}, {"input": "75\r\n...*.*...**.*..*..*P.*......*...*....**..*..**..*..*......*....*..*.*......\r\n", "output": "81\r\n"}, {"input": "80\r\n**.P...*....*P...*....P............**....*..*.*....*..........*.*.*.............\r\n", "output": "44\r\n"}, {"input": "80\r\n*..**..*...*....*.*.**.*.*..*..P..*..**.**..*..**.*.*.*.*.***...*.*..**.*....**.\r\n", "output": "109\r\n"}, {"input": "85\r\n.*.....*.....**..........*P*..........*.........*...*..*...****..*..*P*..*..P.***...*\r\n", "output": "31\r\n"}, {"input": "85\r\n*...*.*.....*..*.*.*.*.**....*...*...*.*..*..*.*......**...*.*.P..*.....*.*....*.*...\r\n", "output": "99\r\n"}, {"input": "90\r\n......P.*.PPP...*.P.**P..*.*.*..*P**PP**..***.PPP....P..**P*.*.*..*.P*P.*PP*.....P.*.**P**\r\n", "output": "5\r\n"}, {"input": "90\r\n.**.*..*........****...*.*..*.*.*...........***..*.*.*.P**...**..*..**..***.....*..*.**...\r\n", "output": "116\r\n"}, {"input": "95\r\n.*..P****....****.*.***P..*.*.**P..*.***.PP.**.**....*****P**..*..*..***...*P..P****.*.P...*..*\r\n", "output": "12\r\n"}, {"input": "95\r\n.*.***...*...P**...*.**.*..*.*..*...****..*.*.*..*.****.*....*...*..*..**.**.********..*.*...**\r\n", "output": "105\r\n"}, {"input": "100\r\n.....PP..*...P.P.PPPP.....P*...P..P.*....*P.*..*..*...........P..PP..P.....*P.....P......*.*..PP....\r\n", "output": "8\r\n"}, {"input": "100\r\n........*P**...*.*.*.*..*.*.**...*.*.....*..............*..**.............*..........*.*..*..*.**...\r\n", "output": "89\r\n"}, {"input": "100\r\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP*PPPPPPPPPPPPP\r\n", "output": "1\r\n"}, {"input": "100\r\n************************************************************P***************************************\r\n", "output": "138\r\n"}, {"input": "100\r\n..........*...............................P..........P.......P.......P..*............P.......**.....\r\n", "output": "32\r\n"}, {"input": "100\r\n.....................P............P....P....*.............*.......**.P.................P........*...\r\n", "output": "11\r\n"}, {"input": "100\r\n......*....*...P*P*.....**.......P.P..........*........*....PP.................P......*.P.P.......*.\r\n", "output": "11\r\n"}, {"input": "100\r\n........................................P..........*...................P.........*.....P.......P....\r\n", "output": "11\r\n"}, {"input": "100\r\n**.**.**.........P*..P**.*.******.***.....***..*.*P.*.***.*.*..*.***..*********.*...***..*..*...P*..\r\n", "output": "26\r\n"}, {"input": "100\r\n.PPP....PPPP.*...P...PPP..*P...P.*P.PP..P.P...PPPPP..PP.P..P..P..P...P.......P..PP..P..PPPPPP.P.PPPP\r\n", "output": "2\r\n"}, {"input": "100\r\n...............................................*P.........*..P.........P.P....P..P.................*\r\n", "output": "18\r\n"}, {"input": "100\r\n...........*.........*.....P...P...............P........P..........P.....*......*.....*P...*.P......\r\n", "output": "16\r\n"}, {"input": "2\r\n*P\r\n", "output": "1\r\n"}, {"input": "2\r\nP*\r\n", "output": "1\r\n"}]
false
stdio
null
true
584/C
584
C
PyPy 3-64
TESTS
8
62
0
210788258
n,t=map(int,input().split()) s1=input() s2=input() difs=[] for i in range(n): if s1[i]!=s2[i]: difs.append(i) if len(difs)>2*t: print(-1) exit() swaps=set(difs) s3=list(s1) for i in range(len(difs)): if i<len(difs)-t: s3[difs[i]]=s2[difs[i]] elif i>t: s3[difs[i]]=s1[difs[i]] else: s3[difs[i]]='a' if s1[difs[i]]=='a' or s2[difs[i]]=='a': s3[difs[i]]='b' if s1[difs[i]]=='b' or s2[difs[i]]=='b': s3[difs[i]]='c' for i in range(t): if len(swaps)>=t:break if i in swaps:continue s3[i]='a' if s1[i]=='a' or s2[i]=='a': s3[i]='b' if s1[i]=='b' or s2[i]=='b': s3[i]='c' swaps.add(i) print(''.join(s3))
78
139
2,969,600
13447337
def read_numbers(): return list(map(int, input().split())) def get_other_char(a, b): for c in 'abc': if c != a and c != b: return c n, t = read_numbers() word1 = input() word2 = input() # find a word, that has minimal distance to both of them # first, number of differences diffs = len([idx for idx, (c1, c2) in enumerate(zip(word1, word2)) if c1 != c2]) #not_diffs = [idx for idx, (c1, c2) in enumerate(zip(word1, word2)) if c1 == c2] minimum = (diffs + 1) // 2 switches = diffs - t if t <= diffs else 0 switch_polarity = 0 if t < minimum: print(-1) else: # create new word new_word = [] for c1, c2 in zip(word1, word2): if c1 == c2: if t > diffs: new_word.append(get_other_char(c1, c2)) t -= 1 else: new_word.append(c1) else: if switches: if switch_polarity == 0: new_word.append(c1) switch_polarity = 1 else: new_word.append(c2) switch_polarity = 0 switches -= 1 else: new_word.append(get_other_char(c1, c2)) print(''.join(new_word))
Codeforces Round 324 (Div. 2)
CF
2,015
1
256
Marina and Vasya
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string. More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print - 1.
The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
null
null
[{"input": "3 2\nabc\nxyc", "output": "ayd"}, {"input": "1 0\nc\nb", "output": "-1"}]
1,700
["constructive algorithms", "greedy", "strings"]
78
[{"input": "3 2\r\nabc\r\nxyc\r\n", "output": "bac"}, {"input": "1 0\r\nc\r\nb\r\n", "output": "-1\r\n"}, {"input": "1 1\r\na\r\na\r\n", "output": "b"}, {"input": "2 1\r\naa\r\naa\r\n", "output": "ab"}, {"input": "3 1\r\nbcb\r\nbca\r\n", "output": "bcc"}, {"input": "4 3\r\nccbb\r\ncaab\r\n", "output": "cbca"}, {"input": "4 2\r\nacbc\r\nacba\r\n", "output": "acab"}, {"input": "4 1\r\nbcbc\r\nacab\r\n", "output": "-1\r\n"}, {"input": "4 2\r\nacbb\r\nbabc\r\n", "output": "aaba"}, {"input": "5 2\r\nabaac\r\nbbbaa\r\n", "output": "abbab"}, {"input": "5 2\r\nabbab\r\nacbab\r\n", "output": "aabaa"}, {"input": "5 3\r\nbcaaa\r\ncbacc\r\n", "output": "bbabb"}, {"input": "5 3\r\ncbacb\r\ncbacb\r\n", "output": "cbbaa"}, {"input": "5 1\r\ncbabb\r\nbabaa\r\n", "output": "-1\r\n"}, {"input": "1 0\r\na\r\na\r\n", "output": "a"}, {"input": "2 2\r\nbb\r\ncb\r\n", "output": "aa"}, {"input": "2 1\r\ncc\r\nba\r\n", "output": "ca"}, {"input": "2 0\r\nbb\r\nab\r\n", "output": "-1\r\n"}, {"input": "3 3\r\naac\r\nabc\r\n", "output": "bca"}, {"input": "1 1\r\na\r\nc\r\n", "output": "b"}, {"input": "3 0\r\ncba\r\ncca\r\n", "output": "-1\r\n"}, {"input": "2 1\r\niy\r\niy\r\n", "output": "ia"}, {"input": "2 2\r\nfg\r\nfn\r\n", "output": "aa"}, {"input": "2 1\r\npd\r\nke\r\n", "output": "pe"}, {"input": "3 3\r\nyva\r\nyvq\r\n", "output": "aab"}, {"input": "3 2\r\npxn\r\ngxn\r\n", "output": "axa"}, {"input": "3 1\r\nlos\r\nlns\r\n", "output": "las"}, {"input": "4 2\r\nhbnx\r\nhwmm\r\n", "output": "hbma"}, {"input": "4 4\r\nqtto\r\nqtto\r\n", "output": "aaaa"}, {"input": "4 3\r\nchqt\r\nchet\r\n", "output": "caaa"}, {"input": "5 3\r\nwzcre\r\nwzcrp\r\n", "output": "wzaaa"}, {"input": "5 1\r\nicahj\r\nxdvch\r\n", "output": "-1\r\n"}, {"input": "5 1\r\npmesm\r\npzeaq\r\n", "output": "-1\r\n"}, {"input": "7 4\r\nycgdbph\r\nfdtapch\r\n", "output": "yctaaah"}, {"input": "10 6\r\nrnsssbuiaq\r\npfsbsbuoay\r\n", "output": "aasasbuaba"}, {"input": "20 5\r\ndsjceiztjkrqgpqpnakr\r\nyijdvcjtjnougpqprrkr\r\n", "output": "-1\r\n"}, {"input": "100 85\r\njknccpmanwhxqnxivdgguahjcuyhdrazmbfwoptatlgytakxsfvdzzcsglhmswfxafxyregdbeiwpawrjgwcqrkbhmrfcscgoszf\r\nhknccpmanwhxjnxivdggeahjcuyhdrazmbfwoqtatlgytdkxsfvdztcsglhmssfxsfxyrngdbeiwpawrjgwcqrkbhmrfcsckoskf\r\n", "output": "aknccpmanwhxanxivaaaabaaaaaaaabaaaaaaaabaaaaabaaaaaaaaaaaaaaaaaabaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "1 0\r\nz\r\nz\r\n", "output": "z"}, {"input": "1 1\r\nz\r\ny\r\n", "output": "a"}, {"input": "1 1\r\nz\r\nz\r\n", "output": "a"}, {"input": "1 0\r\nz\r\ny\r\n", "output": "-1\r\n"}, {"input": "10 1\r\ngjsywvenzc\r\nfssywvenzc\r\n", "output": "gssywvenzc"}, {"input": "20 2\r\nywpcwcwgkhdeonzbeamf\r\ngdcmwcwgkhdeonzbeamf\r\n", "output": "ywcmwcwgkhdeonzbeamf"}]
false
stdio
import sys def hamming(s1, s2): return sum(c1 != c2 for c1, c2 in zip(s1, s2)) def solution_exists(n, t, s1, s2): a = sum(c1 == c2 for c1, c2 in zip(s1, s2)) d = n - a min_k = max(d - t, 0) max_k = min(d // 2, a + d - t) return min_k <= max_k def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n, t = map(int, f.readline().split()) s1 = f.readline().strip() s2 = f.readline().strip() with open(submission_path) as f: submission = f.read().strip() if submission == "-1": exists = solution_exists(n, t, s1, s2) print(0 if exists else 1) return else: if len(submission) != n: print(0) return if any(c < 'a' or c > 'z' for c in submission): print(0) return h1 = hamming(s1, submission) h2 = hamming(s2, submission) if h1 == t and h2 == t: print(1) else: print(0) if __name__ == "__main__": main()
true
584/C
584
C
PyPy 3-64
TESTS
8
93
3,481,600
194526555
# Problem: C. Marina and Vasya # Contest: Codeforces - Codeforces Round #324 (Div. 2) # URL: https://codeforces.com/problemset/problem/584/C # Memory Limit: 256 MB # Time Limit: 1000 ms import sys import bisect import random import io, os from bisect import * from collections import * from contextlib import redirect_stdout from itertools import * from array import * from functools import lru_cache from types import GeneratorType from heapq import * from math import sqrt, gcd, inf if sys.version >= '3.8': # ACW没有comb from math import comb RI = lambda: map(int, sys.stdin.buffer.readline().split()) RS = lambda: map(bytes.decode, sys.stdin.buffer.readline().strip().split()) RILST = lambda: list(RI()) DEBUG = lambda *x: sys.stderr.write(f'{str(x)}\n') MOD = 10 ** 9 + 7 PROBLEM = """https://codeforces.com/problemset/problem/584/C 输入 n t(0≤t≤n≤1e5) 和两个长度均为 n 的字符串 s1 和 s2,均由小写字母组成。 定义 f(a,b) 表示 a[i]!=b[i] 的下标数量。 构造一个长为 n 的,由小写字母组成的字符串 s3,使得 f(s1,s3)=f(s2,s3)=t。 如果无法构造,输出 -1。 输入 3 2 abc xyc 输出 ayd 输入 1 0 c b 输出 -1 """ # ms def solve(): n, t = RI() s1, = RS() s2, = RS() p = n - t # 题目转化为构造ans串使ans和s1\s2相同的字符有p个 same = [] # same[i] 表示 s1[i] == s2[i] 的下标 diff = [] # diff[i] 表示 s1[i]!= s2[i] 的下标 for i, (x, y) in enumerate(zip(s1, s2)): if x == y: same.append(i) else: diff.append(i) s = len(same) # 显然如果p<=s可以直接选same中的p个下标相同即可。 # 如果d = p-s >0 则需要从diff中选2d个下标分别对应s1和s2的位置 # 其它位置填一个不同于s1和s2的即可 if p > s and (p - s) * 2 > n - s: return print(-1) ans = [''] * n if p <= s: for i in same[:p]: ans[i] = s1[i] else: for i in same: ans[i] = s1[i] for i in range(0, p - s, 2): ans[diff[i]] = s1[diff[i]] ans[diff[i + 1]] = s1[diff[i + 1]] for i, c in enumerate(ans): if not c: for c in 'abc': # 从三个中选一定能找到一个同时异与s1和s2的字符 if c != s1[i] and c != s2[i]: ans[i] = c break print(''.join(ans)) if __name__ == '__main__': solve()
78
139
14,233,600
230147474
import string from os import path from sys import stdin, stdout filename = "../templates/input.txt" if path.exists(filename): stdin = open(filename, 'r') def input(): return stdin.readline().rstrip() def print(*args, sep=' ', end='\n'): stdout.write(sep.join(map(str, args))) stdout.write(end) def ceil(x: int, y: int) -> int: return (x + y - 1) // y def solution(): n, t = [int(num) for num in input().split()] s1 = input() s2 = input() same = [] diff = [] for i, (c1, c2) in enumerate(zip(s1, s2)): if c1 == c2: same.append(i) else: diff.append(i) ans = list(s1) if t >= len(diff): for i in diff: for c in string.ascii_lowercase: if c != s1[i] and c != s2[i]: ans[i] = c break t -= len(diff) for i in same[:t]: for c in string.ascii_lowercase: if c != s1[i]: ans[i] = c break elif t < ceil(len(diff), 2): print(-1) return else: y = len(diff) - t x = t - y for i in diff[:x]: for c in string.ascii_lowercase: if c != s1[i] and c != s2[i]: ans[i] = c break for i, j in zip(diff[x::2], diff[x + 1::2]): ans[i] = s1[i] ans[j] = s2[j] print(''.join(ans)) def main(): t = 1 while t: solution() t -= 1 if __name__ == '__main__': main()
Codeforces Round 324 (Div. 2)
CF
2,015
1
256
Marina and Vasya
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string. More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print - 1.
The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
null
null
[{"input": "3 2\nabc\nxyc", "output": "ayd"}, {"input": "1 0\nc\nb", "output": "-1"}]
1,700
["constructive algorithms", "greedy", "strings"]
78
[{"input": "3 2\r\nabc\r\nxyc\r\n", "output": "bac"}, {"input": "1 0\r\nc\r\nb\r\n", "output": "-1\r\n"}, {"input": "1 1\r\na\r\na\r\n", "output": "b"}, {"input": "2 1\r\naa\r\naa\r\n", "output": "ab"}, {"input": "3 1\r\nbcb\r\nbca\r\n", "output": "bcc"}, {"input": "4 3\r\nccbb\r\ncaab\r\n", "output": "cbca"}, {"input": "4 2\r\nacbc\r\nacba\r\n", "output": "acab"}, {"input": "4 1\r\nbcbc\r\nacab\r\n", "output": "-1\r\n"}, {"input": "4 2\r\nacbb\r\nbabc\r\n", "output": "aaba"}, {"input": "5 2\r\nabaac\r\nbbbaa\r\n", "output": "abbab"}, {"input": "5 2\r\nabbab\r\nacbab\r\n", "output": "aabaa"}, {"input": "5 3\r\nbcaaa\r\ncbacc\r\n", "output": "bbabb"}, {"input": "5 3\r\ncbacb\r\ncbacb\r\n", "output": "cbbaa"}, {"input": "5 1\r\ncbabb\r\nbabaa\r\n", "output": "-1\r\n"}, {"input": "1 0\r\na\r\na\r\n", "output": "a"}, {"input": "2 2\r\nbb\r\ncb\r\n", "output": "aa"}, {"input": "2 1\r\ncc\r\nba\r\n", "output": "ca"}, {"input": "2 0\r\nbb\r\nab\r\n", "output": "-1\r\n"}, {"input": "3 3\r\naac\r\nabc\r\n", "output": "bca"}, {"input": "1 1\r\na\r\nc\r\n", "output": "b"}, {"input": "3 0\r\ncba\r\ncca\r\n", "output": "-1\r\n"}, {"input": "2 1\r\niy\r\niy\r\n", "output": "ia"}, {"input": "2 2\r\nfg\r\nfn\r\n", "output": "aa"}, {"input": "2 1\r\npd\r\nke\r\n", "output": "pe"}, {"input": "3 3\r\nyva\r\nyvq\r\n", "output": "aab"}, {"input": "3 2\r\npxn\r\ngxn\r\n", "output": "axa"}, {"input": "3 1\r\nlos\r\nlns\r\n", "output": "las"}, {"input": "4 2\r\nhbnx\r\nhwmm\r\n", "output": "hbma"}, {"input": "4 4\r\nqtto\r\nqtto\r\n", "output": "aaaa"}, {"input": "4 3\r\nchqt\r\nchet\r\n", "output": "caaa"}, {"input": "5 3\r\nwzcre\r\nwzcrp\r\n", "output": "wzaaa"}, {"input": "5 1\r\nicahj\r\nxdvch\r\n", "output": "-1\r\n"}, {"input": "5 1\r\npmesm\r\npzeaq\r\n", "output": "-1\r\n"}, {"input": "7 4\r\nycgdbph\r\nfdtapch\r\n", "output": "yctaaah"}, {"input": "10 6\r\nrnsssbuiaq\r\npfsbsbuoay\r\n", "output": "aasasbuaba"}, {"input": "20 5\r\ndsjceiztjkrqgpqpnakr\r\nyijdvcjtjnougpqprrkr\r\n", "output": "-1\r\n"}, {"input": "100 85\r\njknccpmanwhxqnxivdgguahjcuyhdrazmbfwoptatlgytakxsfvdzzcsglhmswfxafxyregdbeiwpawrjgwcqrkbhmrfcscgoszf\r\nhknccpmanwhxjnxivdggeahjcuyhdrazmbfwoqtatlgytdkxsfvdztcsglhmssfxsfxyrngdbeiwpawrjgwcqrkbhmrfcsckoskf\r\n", "output": "aknccpmanwhxanxivaaaabaaaaaaaabaaaaaaaabaaaaabaaaaaaaaaaaaaaaaaabaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "1 0\r\nz\r\nz\r\n", "output": "z"}, {"input": "1 1\r\nz\r\ny\r\n", "output": "a"}, {"input": "1 1\r\nz\r\nz\r\n", "output": "a"}, {"input": "1 0\r\nz\r\ny\r\n", "output": "-1\r\n"}, {"input": "10 1\r\ngjsywvenzc\r\nfssywvenzc\r\n", "output": "gssywvenzc"}, {"input": "20 2\r\nywpcwcwgkhdeonzbeamf\r\ngdcmwcwgkhdeonzbeamf\r\n", "output": "ywcmwcwgkhdeonzbeamf"}]
false
stdio
import sys def hamming(s1, s2): return sum(c1 != c2 for c1, c2 in zip(s1, s2)) def solution_exists(n, t, s1, s2): a = sum(c1 == c2 for c1, c2 in zip(s1, s2)) d = n - a min_k = max(d - t, 0) max_k = min(d // 2, a + d - t) return min_k <= max_k def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n, t = map(int, f.readline().split()) s1 = f.readline().strip() s2 = f.readline().strip() with open(submission_path) as f: submission = f.read().strip() if submission == "-1": exists = solution_exists(n, t, s1, s2) print(0 if exists else 1) return else: if len(submission) != n: print(0) return if any(c < 'a' or c > 'z' for c in submission): print(0) return h1 = hamming(s1, submission) h2 = hamming(s2, submission) if h1 == t and h2 == t: print(1) else: print(0) if __name__ == "__main__": main()
true
731/B
731
B
Python 3
TESTS
1
30
0
189846734
session = int(input()) teams = list(map(int,input().split())) for i in range(session - 1): if teams[i] < 0: print('NO') break teams[i+1] -= teams[i] % 2 if teams[-1] % 2 == 0: print('YES') else: print('NO')
79
124
20,480,000
209549101
input() p=0 for x in map(int, input().split()): p=1<<20 if x<p else p^x&1 print('NO' if p>0 else 'YES')
Codeforces Round 376 (Div. 2)
CF
2,016
1
256
Coupons and Discounts
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition. Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day. There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total). As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days. Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.
The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).
null
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.
[{"input": "4\n1 2 1 2", "output": "YES"}, {"input": "3\n1 0 1", "output": "NO"}]
1,100
["constructive algorithms", "greedy"]
79
[{"input": "4\r\n1 2 1 2\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 0 2\r\n", "output": "YES\r\n"}, {"input": "1\r\n179\r\n", "output": "NO\r\n"}, {"input": "10\r\n0 0 5 9 9 3 0 0 0 10\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 3\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "2\r\n0 0\r\n", "output": "YES\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "1\r\n2\r\n", "output": "YES\r\n"}, {"input": "1\r\n3\r\n", "output": "NO\r\n"}, {"input": "1\r\n10000\r\n", "output": "YES\r\n"}, {"input": "2\r\n10000 10000\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "YES\r\n"}, {"input": "10\r\n3 3 3 2 2 2 2 2 2 3\r\n", "output": "YES\r\n"}, {"input": "100\r\n2 3 2 3 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 2 3 2 3 2 2 3 3 3 3 3 2 2 2 2 3 2 3 3 2 2 3 2 3 3 3 3 2 2 3 3 3 3 3 2 3 3 3 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 3 3 3 2 2 2 2 3 2 2 3 3 3 2 2 2 2 2 3 3\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n1 0 1 1 0 1 1 0 1 0\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n8 4 0 0 6 1 9 8 0 6\r\n", "output": "YES\r\n"}, {"input": "100\r\n44 0 0 0 16 0 0 0 0 77 9 0 94 0 78 0 0 50 55 35 0 35 88 27 0 0 86 0 0 56 0 0 17 23 0 22 54 36 0 0 94 36 0 22 0 0 0 0 0 0 0 82 0 0 50 0 6 0 0 44 80 0 0 0 98 0 0 0 0 92 0 56 0 16 0 14 0 37 89 0 62 3 83 0 0 0 80 0 92 58 92 0 0 0 57 79 0 0 0 42\r\n", "output": "YES\r\n"}, {"input": "100\r\n37 92 14 95 3 37 0 0 0 84 27 33 0 0 0 74 74 0 35 72 46 29 8 92 1 76 47 0 38 82 0 81 54 7 61 46 91 0 86 0 80 0 0 98 88 0 4 0 0 52 0 0 82 0 33 35 0 36 58 52 1 50 29 0 0 24 0 69 97 65 13 0 30 0 14 66 47 94 22 24 8 92 67 0 34 0 0 0 84 85 50 33 0 99 67 73 21 0 0 62\r\n", "output": "YES\r\n"}, {"input": "100\r\n56 22 13 79 28 73 16 55 34 0 97 19 22 36 22 80 30 19 36 92 9 38 24 10 61 43 19 12 18 34 21 36 1 17 0 97 72 37 74 70 51 34 33 87 27 33 45 97 38 56 2 32 88 92 64 51 74 94 86 98 57 62 83 3 87 61 9 65 57 13 64 10 50 35 7 75 41 3 70 66 6 55 69 42 91 75 14 22 68 93 2 53 22 98 45 2 78 58 18 13\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 1 1 0 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 0 0\r\n", "output": "NO\r\n"}, {"input": "9\r\n6 3 5 9 0 3 1 9 6\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 5\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 2\r\n", "output": "NO\r\n"}]
false
stdio
null
true
192/B
192
B
PyPy 3
TESTS
12
218
102,400
104135537
from sys import stdin,stdout nmbr=lambda:int(stdin.readline()) lst = lambda: list(map(int, input().split())) for i in range(1):#nmbr(): n=nmbr() a=lst() PI=float('inf') dp=[0]*n for i in range(n): prev=pprev=PI if i-1>=0:prev=dp[i-1] if i-2>=0:pprev=dp[i-2] dp[i]=min(a[i],max(prev,pprev)) print(dp[-1])
85
92
0
172992538
n=int(input()) a=list(map(int,input().split())) day_0=a[0] day_n=a[n-1] mini_s=2**31 mini=2**31 for i in range(0,n-1): temp=max(a[i],a[i+1]) mini=min(mini,temp) mini=min(a[0],mini,a[n-1]) print(mini)
Codeforces Round 121 (Div. 2)
CF
2,012
2
256
Walking in the Rain
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously. In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles. The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103).
Print a single number — the sought number of days.
null
In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it. In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
[{"input": "4\n10 3 5 10", "output": "5"}, {"input": "5\n10 2 8 3 5", "output": "5"}]
1,100
["brute force", "implementation"]
85
[{"input": "4\r\n10 3 5 10\r\n", "output": "5\r\n"}, {"input": "5\r\n10 2 8 3 5\r\n", "output": "5\r\n"}, {"input": "10\r\n10 3 1 6 7 1 3 3 8 1\r\n", "output": "1\r\n"}, {"input": "10\r\n26 72 10 52 2 5 61 2 39 64\r\n", "output": "5\r\n"}, {"input": "100\r\n8 2 1 2 8 3 5 8 5 1 9 3 4 1 5 6 4 2 9 10 6 10 10 3 9 4 10 5 3 1 5 10 7 6 8 10 2 6 4 4 2 2 10 7 2 7 3 2 6 3 6 4 7 6 2 5 5 8 6 9 5 2 7 5 8 6 5 8 10 6 10 8 5 3 1 10 6 1 7 5 1 8 10 5 1 3 10 7 10 5 7 1 4 3 8 6 3 4 9 6\r\n", "output": "2\r\n"}, {"input": "100\r\n10 2 8 7 5 1 5 4 9 2 7 9 3 5 6 2 3 6 10 1 2 7 1 4 8 8 6 1 7 8 8 1 5 8 1 2 7 4 10 7 3 1 2 5 8 1 1 4 9 7 7 4 7 3 8 8 7 1 5 1 6 9 8 8 1 10 4 4 7 7 10 9 5 1 1 3 6 2 6 3 6 4 9 8 2 9 6 2 7 8 10 9 9 6 3 5 3 1 4 8\r\n", "output": "1\r\n"}, {"input": "5\r\n3 2 3 4 2\r\n", "output": "2\r\n"}, {"input": "5\r\n4 8 9 10 6\r\n", "output": "4\r\n"}, {"input": "5\r\n2 21 6 5 9\r\n", "output": "2\r\n"}, {"input": "5\r\n34 39 30 37 35\r\n", "output": "34\r\n"}, {"input": "5\r\n14 67 15 28 21\r\n", "output": "14\r\n"}, {"input": "5\r\n243 238 138 146 140\r\n", "output": "140\r\n"}, {"input": "5\r\n46 123 210 119 195\r\n", "output": "46\r\n"}, {"input": "5\r\n725 444 477 661 761\r\n", "output": "477\r\n"}, {"input": "10\r\n2 2 3 4 4 1 5 3 1 2\r\n", "output": "2\r\n"}, {"input": "10\r\n1 10 1 10 1 1 7 8 6 7\r\n", "output": "1\r\n"}, {"input": "10\r\n5 17 8 1 10 20 9 18 12 20\r\n", "output": "5\r\n"}, {"input": "10\r\n18 11 23 7 9 10 28 29 46 21\r\n", "output": "9\r\n"}, {"input": "10\r\n2 17 53 94 95 57 36 47 68 48\r\n", "output": "2\r\n"}, {"input": "10\r\n93 231 176 168 177 222 22 137 110 4\r\n", "output": "4\r\n"}, {"input": "10\r\n499 173 45 141 425 276 96 290 428 95\r\n", "output": "95\r\n"}, {"input": "10\r\n201 186 897 279 703 376 238 93 253 316\r\n", "output": "201\r\n"}, {"input": "25\r\n3 2 3 2 2 2 3 4 5 1 1 4 1 2 1 3 5 5 3 5 1 2 4 1 3\r\n", "output": "1\r\n"}, {"input": "25\r\n9 9 1 9 10 5 6 4 6 1 5 2 2 1 2 8 4 6 5 7 1 10 5 4 9\r\n", "output": "2\r\n"}, {"input": "25\r\n2 17 21 4 13 6 14 18 17 1 16 13 24 4 12 7 8 16 9 25 25 9 11 20 18\r\n", "output": "2\r\n"}, {"input": "25\r\n38 30 9 35 33 48 8 4 49 2 39 19 34 35 47 49 33 4 23 5 42 35 49 11 30\r\n", "output": "8\r\n"}, {"input": "25\r\n75 34 77 68 60 38 76 89 35 68 28 36 96 63 43 12 9 4 37 75 88 30 11 58 35\r\n", "output": "9\r\n"}, {"input": "25\r\n108 3 144 140 239 105 59 126 224 181 147 102 94 201 68 121 167 94 60 130 64 162 45 95 235\r\n", "output": "94\r\n"}, {"input": "25\r\n220 93 216 467 134 408 132 220 292 11 363 404 282 253 141 313 310 356 214 256 380 81 42 128 363\r\n", "output": "81\r\n"}, {"input": "25\r\n371 884 75 465 891 510 471 52 382 829 514 610 660 642 179 108 41 818 346 106 738 993 706 574 623\r\n", "output": "108\r\n"}, {"input": "50\r\n1 2 1 3 2 5 2 2 2 3 4 4 4 3 3 4 1 2 3 1 5 4 1 2 2 1 5 3 2 2 1 5 4 5 2 5 4 1 1 3 5 2 1 4 5 5 1 5 5 5\r\n", "output": "1\r\n"}, {"input": "50\r\n2 4 9 8 1 3 7 1 2 3 8 9 8 8 5 2 10 5 8 1 3 1 8 2 3 7 9 10 2 9 9 7 3 8 6 10 6 5 4 8 1 1 5 6 8 9 5 9 5 3\r\n", "output": "1\r\n"}, {"input": "50\r\n22 9 5 3 24 21 25 13 17 21 14 8 22 18 2 3 22 9 10 11 25 22 5 10 16 7 15 3 2 13 2 12 9 24 3 14 2 18 3 22 8 2 19 6 16 4 5 20 10 12\r\n", "output": "3\r\n"}, {"input": "50\r\n14 4 20 37 50 46 19 20 25 47 10 6 34 12 41 47 9 22 28 41 34 47 40 12 42 9 4 15 15 27 8 38 9 4 17 8 13 47 7 9 38 30 48 50 7 41 34 23 11 16\r\n", "output": "9\r\n"}, {"input": "50\r\n69 9 97 15 22 69 27 7 23 84 73 74 60 94 43 98 13 4 63 49 7 31 93 23 6 75 32 63 49 32 99 43 68 48 16 54 20 38 40 65 34 28 21 55 79 50 2 18 22 95\r\n", "output": "13\r\n"}, {"input": "50\r\n50 122 117 195 42 178 153 194 7 89 142 40 158 230 213 104 179 56 244 196 85 159 167 19 157 20 230 201 152 98 250 242 10 52 96 242 139 181 90 107 178 52 196 79 23 61 212 47 97 97\r\n", "output": "50\r\n"}, {"input": "50\r\n354 268 292 215 187 232 35 38 179 79 108 491 346 384 345 103 14 260 148 322 459 238 220 493 374 237 474 148 21 221 88 377 289 121 201 198 490 117 382 454 359 390 346 456 294 325 130 306 484 83\r\n", "output": "38\r\n"}, {"input": "50\r\n94 634 27 328 629 967 728 177 379 908 801 715 787 192 427 48 559 923 841 6 759 335 251 172 193 593 456 780 647 638 750 881 206 129 278 744 91 49 523 248 286 549 593 451 216 753 471 325 870 16\r\n", "output": "16\r\n"}, {"input": "100\r\n5 5 4 3 5 1 2 5 1 1 3 5 4 4 1 1 1 1 5 4 4 5 1 5 5 1 2 1 3 1 5 1 3 3 3 2 2 2 1 1 5 1 3 4 1 1 3 2 5 2 2 5 5 4 4 1 3 4 3 3 4 5 3 3 3 1 2 1 4 2 4 4 1 5 1 3 5 5 5 5 3 4 4 3 1 2 5 2 3 5 4 2 4 5 3 2 4 2 4 3\r\n", "output": "1\r\n"}, {"input": "100\r\n3 4 8 10 8 6 4 3 7 7 6 2 3 1 3 10 1 7 9 3 5 5 2 6 2 9 1 7 4 2 4 1 6 1 7 10 2 5 3 7 6 4 6 2 8 8 8 6 6 10 3 7 4 3 4 1 7 9 3 6 3 6 1 4 9 3 8 1 10 1 4 10 7 7 9 5 3 8 10 2 1 10 8 7 10 8 5 3 1 2 1 10 6 1 5 3 3 5 7 2\r\n", "output": "2\r\n"}, {"input": "1\r\n987\r\n", "output": "987\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n2 5 5 5 5\r\n", "output": "2\r\n"}, {"input": "1\r\n500\r\n", "output": "500\r\n"}]
false
stdio
null
true
670/F
670
F
PyPy 3-64
TESTS
3
46
0
211687507
s = input() a = input() m = { '0':0, '1':0, '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0, '9':0} for i in s: m[i] += 1 for i in a: m[i] -= 1 for i in range(1, len(s)): u = len(s) - i fl = True if len(str(u)) < len(s) - i == 0: fl = False for j in str(u): if m[j] < str(u).count(j): fl = False break if fl: for j in str(u): m[j] -= 1 break ans = '' for i in range(1, 10): if m[str(i)] > 0: m[str(i)] -= 1 ans += str(i) break was = 0 for i in range(0, 10): if was: ans += str(i) * m[str(i)] else: if str(i) == a[0]: was = 1 for j in a: if int(j) > i: ans += str(i) * m[str(i)] + a break elif int(j) < i: ans += a + str(i) * m[str(i)] break else: ans += a + str(i) * m[str(i)] else: ans += str(i) * m[str(i)] print(ans)
118
436
14,028,800
20372817
import sys def main(): a = sys.stdin.readline().strip() b = sys.stdin.readline().strip() if a == "01" or a == "10": print("0") return cnt = [0] * 256 for i in map(ord, a): cnt[i] += 1 n = sum(cnt) l = 0 for i in range(1, 8): if i == len(str(n - i)): l = n - i break; for s in b, str(l): for i in map(ord, s): cnt[i] -= 1 res = ["".join([b] + [chr(k) * v for k, v in enumerate(cnt) if v > 0 ])] if b[0] > "0" else [] for i in range(ord("1"), ord("9") + 1): if cnt[i] > 0: cnt[i] -= 1 others = [chr(k) * v for k, v in enumerate(cnt) if v > 0] others.append(b) res.append("".join([chr(i)] + sorted(others))) break print(min(res)) if __name__ == "__main__": main()
Codeforces Round 350 (Div. 2)
CF
2,016
2
256
Restore a Number
Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n). Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).
The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000. The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes. It is guaranteed that the input data is correct, and the answer always exists.
Print the smalles integer n which Vasya could pass to Kate.
null
null
[{"input": "003512\n021", "output": "30021"}, {"input": "199966633300\n63", "output": "3036366999"}]
2,300
["brute force", "constructive algorithms", "strings"]
118
[{"input": "003512\r\n021\r\n", "output": "30021\r\n"}, {"input": "199966633300\r\n63\r\n", "output": "3036366999\r\n"}, {"input": "01\r\n0\r\n", "output": "0\r\n"}, {"input": "0000454312911\r\n9213544\r\n", "output": "92135440000\r\n"}, {"input": "13\r\n3\r\n", "output": "3\r\n"}, {"input": "00010454312921\r\n9213544\r\n", "output": "100009213544\r\n"}, {"input": "11317110\r\n01\r\n", "output": "1011113\r\n"}, {"input": "1516532320120301262110112013012410838210025280432402042406224604110031740090203024020012\r\n0126064\r\n", "output": "10000000000000000000000012606411111111111111222222222222222222333333334444444455567889\r\n"}, {"input": "233121122272652143504001162131110307236110231414093112213120271312010423132181004\r\n0344011\r\n", "output": "1000000000003440111111111111111111111112222222222222222233333333333444455666778\r\n"}, {"input": "1626112553124100114021300410533124010061200562040601301\r\n00612141\r\n", "output": "10000000000000006121411111111111222222333344445556666\r\n"}, {"input": "040005088\r\n0\r\n", "output": "40000058\r\n"}, {"input": "420002200110100211206222101201021321440210\r\n00\r\n", "output": "1000000000000011111111112222222222223446\r\n"}, {"input": "801095116\r\n0\r\n", "output": "10011569\r\n"}, {"input": "070421120216020020\r\n000024\r\n", "output": "1000000024122227\r\n"}, {"input": "825083\r\n0\r\n", "output": "20388\r\n"}, {"input": "6201067\r\n0\r\n", "output": "100267\r\n"}, {"input": "34404430311310306128103301112523111011050561125004200941114005444000000040133002103062151514033103\r\n010215110013511400400140133404\r\n", "output": "100000000000000000000102151100135114004001401334041111111111111122222233333333333444444455555668\r\n"}, {"input": "14\r\n4\r\n", "output": "4\r\n"}, {"input": "21\r\n2\r\n", "output": "2\r\n"}, {"input": "204\r\n4\r\n", "output": "40\r\n"}, {"input": "12\r\n2\r\n", "output": "2\r\n"}, {"input": "05740110115001520111222011422101032503200010203300510014413\r\n000151\r\n", "output": "100000000000000000001511111111111111222222222333334444555\r\n"}, {"input": "116051111111001510011110101111111101001111111101111101101\r\n00111111111\r\n", "output": "1000000000000011111111111111111111111111111111111111116\r\n"}, {"input": "1161100\r\n01110\r\n", "output": "101110\r\n"}, {"input": "101313020013110703821620035452130200177115540090000\r\n002001320\r\n", "output": "1000000000000002001320111111111222333334555567778\r\n"}, {"input": "03111100110111111118\r\n01001111111101111\r\n", "output": "301001111111101111\r\n"}, {"input": "01170141\r\n01114\r\n", "output": "1001114\r\n"}, {"input": "0500014440100110264222000342611000102247070652310723\r\n0003217\r\n", "output": "10000000000000000032171111112222222233444444566677\r\n"}, {"input": "111011111101111131113111111111011\r\n0111111111111111010111111111\r\n", "output": "1011111111111111101011111111113\r\n"}, {"input": "11003040044200003323519101102070252000010622902208104150200400140042011224011154237302003323632011235\r\n0\r\n", "output": "100000000000000000000000000000000001111111111111111222222222222222222333333333334444444445555566778\r\n"}, {"input": "111111011110101141110110011010011114110111\r\n01010111111011111\r\n", "output": "1000000101011111101111111111111111111114\r\n"}, {"input": "011010171110\r\n010110117\r\n", "output": "1010110117\r\n"}, {"input": "510017\r\n0\r\n", "output": "10017\r\n"}, {"input": "00111111110114112110011105\r\n0\r\n", "output": "100000011111111111111115\r\n"}, {"input": "320403902031031110003113410860101243100423120201101124080311242010930103200001451200132304400000\r\n01\r\n", "output": "1000000000000000000000000000000000011111111111111111111122222222222233333333333334444444456889\r\n"}, {"input": "125\r\n15\r\n", "output": "15\r\n"}, {"input": "1160190\r\n110019\r\n", "output": "110019\r\n"}, {"input": "11111111111101101111110101011111010101001111001110010011810010110111101101112140110110\r\n110101100101111101011111111101111111111110111110011111011000111010100111011111000002\r\n", "output": "110101100101111101011111111101111111111110111110011111011000111010100111011111000002\r\n"}, {"input": "2206026141112316065224201412118064151200614042100160093001020024005013121010030020083221011\r\n280060226\r\n", "output": "10000000000000000000000000111111111111111111111222222222222228006022633333444444455566666\r\n"}, {"input": "63007511113226210230771304213600010311075400082011350143450007091200\r\n25\r\n", "output": "100000000000000000000011111111111112222222533333333444455567777789\r\n"}, {"input": "142245201505011321217122212\r\n12521721230\r\n", "output": "1001111125217212302222445\r\n"}, {"input": "712\r\n17\r\n", "output": "17\r\n"}, {"input": "11011111111003010101111111111103111\r\n101111111110110111111011001011111\r\n", "output": "101111111110110111111011001011111\r\n"}, {"input": "143213104201201003340424615500135122127119000020020017400111102423312241032010400\r\n235321200411204201121201304100003\r\n", "output": "1000000000000001111111111222222223532120041120420112120130410000333334444445567\r\n"}, {"input": "080001181\r\n18\r\n", "output": "10000118\r\n"}, {"input": "4141403055010511470013300502174230460332129228041229160601006121052601201100001153120100000\r\n49\r\n", "output": "10000000000000000000000000000011111111111111111112222222222223333333444444495555556666677\r\n"}, {"input": "2131\r\n112\r\n", "output": "112\r\n"}, {"input": "0111110011011110111012109101101111101111150011110111110111001\r\n10110010111111011111111011001101001111111111111110001011012\r\n", "output": "10110010111111011111111011001101001111111111111110001011012\r\n"}, {"input": "251137317010111402300506643001203241303324162124225270011006213015100\r\n3512\r\n", "output": "1000000000000000001111111111111122222222223333333335124444455566677\r\n"}, {"input": "12140051050330004342310455231200020252193200\r\n23012\r\n", "output": "100000000000001111222222301233333444555559\r\n"}, {"input": "291\r\n19\r\n", "output": "19\r\n"}, {"input": "11011011000111101111111111081101110001011111101111110111111111011111011011111100111\r\n1110111111111\r\n", "output": "100000000000000000011101111111111111111111111111111111111111111111111111111111111\r\n"}, {"input": "170422032160671323013220212523333410720410110020005012206133500200001015971250190240204004002041\r\n10010405153200037262043200214001340010615320\r\n", "output": "1000000000000000100104051532000372620432002140013400106153201111111122222222222233333445567779\r\n"}, {"input": "210042022032002310001424611003103312001401111120015141083050404330261401411234412400319100212120\r\n10014121114054\r\n", "output": "1000000000000000000000000010014121114054111111111111111111222222222222223333333333444444445668\r\n"}, {"input": "222122228\r\n2221\r\n", "output": "22212222\r\n"}, {"input": "10\r\n0\r\n", "output": "0\r\n"}, {"input": "11007000\r\n1000\r\n", "output": "1000001\r\n"}, {"input": "3323\r\n32\r\n", "output": "323\r\n"}, {"input": "1001016\r\n1001\r\n", "output": "100101\r\n"}, {"input": "50104\r\n10\r\n", "output": "1005\r\n"}, {"input": "2023\r\n20\r\n", "output": "202\r\n"}, {"input": "0001116\r\n1001\r\n", "output": "100101\r\n"}, {"input": "32334\r\n32\r\n", "output": "3233\r\n"}, {"input": "1103\r\n10\r\n", "output": "101\r\n"}, {"input": "023335\r\n23\r\n", "output": "23033\r\n"}, {"input": "111111111110\r\n1\r\n", "output": "1111111111\r\n"}, {"input": "501105\r\n110\r\n", "output": "11005\r\n"}, {"input": "1110006\r\n1001\r\n", "output": "100101\r\n"}]
false
stdio
null
true
192/B
192
B
Python 3
TESTS
12
92
0
183192060
listTile = [] def runSolution(): n = int(input()) mas = input().split(" ") for el in mas: listTile.append(int(el)) k = 0 while True: if listTile[len(listTile)-1] <= 0: print(k) return for i in range(0, len(listTile)-2): if listTile[i] <= 0 and listTile[i+1] <= 0: print(k) return k += 1 for i in range(0, len(listTile)): listTile[i] -= 1 return if __name__ == '__main__': runSolution()
85
92
0
195573869
n=int(input()) l=list(map(int,input().split())) p=min(l[0],l[-1]) g=5 for i in range(p): while i in l: o=l.index(i) l[o]="." if l[o-1]=="." or l[o+1]==".":p=i;g=3;break if g==3:break print(p)
Codeforces Round 121 (Div. 2)
CF
2,012
2
256
Walking in the Rain
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously. In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles. The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103).
Print a single number — the sought number of days.
null
In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it. In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
[{"input": "4\n10 3 5 10", "output": "5"}, {"input": "5\n10 2 8 3 5", "output": "5"}]
1,100
["brute force", "implementation"]
85
[{"input": "4\r\n10 3 5 10\r\n", "output": "5\r\n"}, {"input": "5\r\n10 2 8 3 5\r\n", "output": "5\r\n"}, {"input": "10\r\n10 3 1 6 7 1 3 3 8 1\r\n", "output": "1\r\n"}, {"input": "10\r\n26 72 10 52 2 5 61 2 39 64\r\n", "output": "5\r\n"}, {"input": "100\r\n8 2 1 2 8 3 5 8 5 1 9 3 4 1 5 6 4 2 9 10 6 10 10 3 9 4 10 5 3 1 5 10 7 6 8 10 2 6 4 4 2 2 10 7 2 7 3 2 6 3 6 4 7 6 2 5 5 8 6 9 5 2 7 5 8 6 5 8 10 6 10 8 5 3 1 10 6 1 7 5 1 8 10 5 1 3 10 7 10 5 7 1 4 3 8 6 3 4 9 6\r\n", "output": "2\r\n"}, {"input": "100\r\n10 2 8 7 5 1 5 4 9 2 7 9 3 5 6 2 3 6 10 1 2 7 1 4 8 8 6 1 7 8 8 1 5 8 1 2 7 4 10 7 3 1 2 5 8 1 1 4 9 7 7 4 7 3 8 8 7 1 5 1 6 9 8 8 1 10 4 4 7 7 10 9 5 1 1 3 6 2 6 3 6 4 9 8 2 9 6 2 7 8 10 9 9 6 3 5 3 1 4 8\r\n", "output": "1\r\n"}, {"input": "5\r\n3 2 3 4 2\r\n", "output": "2\r\n"}, {"input": "5\r\n4 8 9 10 6\r\n", "output": "4\r\n"}, {"input": "5\r\n2 21 6 5 9\r\n", "output": "2\r\n"}, {"input": "5\r\n34 39 30 37 35\r\n", "output": "34\r\n"}, {"input": "5\r\n14 67 15 28 21\r\n", "output": "14\r\n"}, {"input": "5\r\n243 238 138 146 140\r\n", "output": "140\r\n"}, {"input": "5\r\n46 123 210 119 195\r\n", "output": "46\r\n"}, {"input": "5\r\n725 444 477 661 761\r\n", "output": "477\r\n"}, {"input": "10\r\n2 2 3 4 4 1 5 3 1 2\r\n", "output": "2\r\n"}, {"input": "10\r\n1 10 1 10 1 1 7 8 6 7\r\n", "output": "1\r\n"}, {"input": "10\r\n5 17 8 1 10 20 9 18 12 20\r\n", "output": "5\r\n"}, {"input": "10\r\n18 11 23 7 9 10 28 29 46 21\r\n", "output": "9\r\n"}, {"input": "10\r\n2 17 53 94 95 57 36 47 68 48\r\n", "output": "2\r\n"}, {"input": "10\r\n93 231 176 168 177 222 22 137 110 4\r\n", "output": "4\r\n"}, {"input": "10\r\n499 173 45 141 425 276 96 290 428 95\r\n", "output": "95\r\n"}, {"input": "10\r\n201 186 897 279 703 376 238 93 253 316\r\n", "output": "201\r\n"}, {"input": "25\r\n3 2 3 2 2 2 3 4 5 1 1 4 1 2 1 3 5 5 3 5 1 2 4 1 3\r\n", "output": "1\r\n"}, {"input": "25\r\n9 9 1 9 10 5 6 4 6 1 5 2 2 1 2 8 4 6 5 7 1 10 5 4 9\r\n", "output": "2\r\n"}, {"input": "25\r\n2 17 21 4 13 6 14 18 17 1 16 13 24 4 12 7 8 16 9 25 25 9 11 20 18\r\n", "output": "2\r\n"}, {"input": "25\r\n38 30 9 35 33 48 8 4 49 2 39 19 34 35 47 49 33 4 23 5 42 35 49 11 30\r\n", "output": "8\r\n"}, {"input": "25\r\n75 34 77 68 60 38 76 89 35 68 28 36 96 63 43 12 9 4 37 75 88 30 11 58 35\r\n", "output": "9\r\n"}, {"input": "25\r\n108 3 144 140 239 105 59 126 224 181 147 102 94 201 68 121 167 94 60 130 64 162 45 95 235\r\n", "output": "94\r\n"}, {"input": "25\r\n220 93 216 467 134 408 132 220 292 11 363 404 282 253 141 313 310 356 214 256 380 81 42 128 363\r\n", "output": "81\r\n"}, {"input": "25\r\n371 884 75 465 891 510 471 52 382 829 514 610 660 642 179 108 41 818 346 106 738 993 706 574 623\r\n", "output": "108\r\n"}, {"input": "50\r\n1 2 1 3 2 5 2 2 2 3 4 4 4 3 3 4 1 2 3 1 5 4 1 2 2 1 5 3 2 2 1 5 4 5 2 5 4 1 1 3 5 2 1 4 5 5 1 5 5 5\r\n", "output": "1\r\n"}, {"input": "50\r\n2 4 9 8 1 3 7 1 2 3 8 9 8 8 5 2 10 5 8 1 3 1 8 2 3 7 9 10 2 9 9 7 3 8 6 10 6 5 4 8 1 1 5 6 8 9 5 9 5 3\r\n", "output": "1\r\n"}, {"input": "50\r\n22 9 5 3 24 21 25 13 17 21 14 8 22 18 2 3 22 9 10 11 25 22 5 10 16 7 15 3 2 13 2 12 9 24 3 14 2 18 3 22 8 2 19 6 16 4 5 20 10 12\r\n", "output": "3\r\n"}, {"input": "50\r\n14 4 20 37 50 46 19 20 25 47 10 6 34 12 41 47 9 22 28 41 34 47 40 12 42 9 4 15 15 27 8 38 9 4 17 8 13 47 7 9 38 30 48 50 7 41 34 23 11 16\r\n", "output": "9\r\n"}, {"input": "50\r\n69 9 97 15 22 69 27 7 23 84 73 74 60 94 43 98 13 4 63 49 7 31 93 23 6 75 32 63 49 32 99 43 68 48 16 54 20 38 40 65 34 28 21 55 79 50 2 18 22 95\r\n", "output": "13\r\n"}, {"input": "50\r\n50 122 117 195 42 178 153 194 7 89 142 40 158 230 213 104 179 56 244 196 85 159 167 19 157 20 230 201 152 98 250 242 10 52 96 242 139 181 90 107 178 52 196 79 23 61 212 47 97 97\r\n", "output": "50\r\n"}, {"input": "50\r\n354 268 292 215 187 232 35 38 179 79 108 491 346 384 345 103 14 260 148 322 459 238 220 493 374 237 474 148 21 221 88 377 289 121 201 198 490 117 382 454 359 390 346 456 294 325 130 306 484 83\r\n", "output": "38\r\n"}, {"input": "50\r\n94 634 27 328 629 967 728 177 379 908 801 715 787 192 427 48 559 923 841 6 759 335 251 172 193 593 456 780 647 638 750 881 206 129 278 744 91 49 523 248 286 549 593 451 216 753 471 325 870 16\r\n", "output": "16\r\n"}, {"input": "100\r\n5 5 4 3 5 1 2 5 1 1 3 5 4 4 1 1 1 1 5 4 4 5 1 5 5 1 2 1 3 1 5 1 3 3 3 2 2 2 1 1 5 1 3 4 1 1 3 2 5 2 2 5 5 4 4 1 3 4 3 3 4 5 3 3 3 1 2 1 4 2 4 4 1 5 1 3 5 5 5 5 3 4 4 3 1 2 5 2 3 5 4 2 4 5 3 2 4 2 4 3\r\n", "output": "1\r\n"}, {"input": "100\r\n3 4 8 10 8 6 4 3 7 7 6 2 3 1 3 10 1 7 9 3 5 5 2 6 2 9 1 7 4 2 4 1 6 1 7 10 2 5 3 7 6 4 6 2 8 8 8 6 6 10 3 7 4 3 4 1 7 9 3 6 3 6 1 4 9 3 8 1 10 1 4 10 7 7 9 5 3 8 10 2 1 10 8 7 10 8 5 3 1 2 1 10 6 1 5 3 3 5 7 2\r\n", "output": "2\r\n"}, {"input": "1\r\n987\r\n", "output": "987\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n2 5 5 5 5\r\n", "output": "2\r\n"}, {"input": "1\r\n500\r\n", "output": "500\r\n"}]
false
stdio
null
true
186/A
186
A
Python 3
TESTS
47
248
716,800
90202343
def comparingStrings(): s1=input() s2=input() if len(s1)!=len(s2): print("NO") return c=0 i=0 while i<len(s1): dc=0 if s1[i]!=s2[i]: if i==len(s1)-1: print("NO") return elif s1[i+1]==s2[i] and s1[i]==s2[i+1]: if c!=0: print("NO") return else: c=c+1 i=i+2 dc=1 continue if dc==0: i=i+1 print("YES") comparingStrings()
53
92
204,800
218389219
a, b = input(), input() d = 0 ghapla = False if len(a)!=len(b) or set(a)!=set(b): print('NO') else: for i in range(len(a)): if a[i]!=b[i]: d+=1 if d>2: ghapla = True break print('NO' if ghapla else 'YES')
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Comparing Strings
Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters. Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.
The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.
Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".
null
- First example: you can simply swap two letters in string "ab". So we get "ba". - Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".
[{"input": "ab\nba", "output": "YES"}, {"input": "aa\nab", "output": "NO"}]
1,100
["implementation", "strings"]
53
[{"input": "ab\r\nba\r\n", "output": "YES\r\n"}, {"input": "aa\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\nza\r\n", "output": "NO\r\n"}, {"input": "vvea\r\nvvae\r\n", "output": "YES\r\n"}, {"input": "rtfabanpc\r\natfabrnpc\r\n", "output": "YES\r\n"}, {"input": "mt\r\ntm\r\n", "output": "YES\r\n"}, {"input": "qxolmbkkt\r\naovlajmlf\r\n", "output": "NO\r\n"}, {"input": "b\r\ng\r\n", "output": "NO\r\n"}, {"input": "ab\r\naba\r\n", "output": "NO\r\n"}, {"input": "ba\r\na\r\n", "output": "NO\r\n"}, {"input": "a\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\naa\r\n", "output": "NO\r\n"}, {"input": "a\r\nz\r\n", "output": "NO\r\n"}, {"input": "aabb\r\nbbaa\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbd\r\n", "output": "NO\r\n"}, {"input": "bac\r\ndae\r\n", "output": "NO\r\n"}, {"input": "abc\r\nakl\r\n", "output": "NO\r\n"}, {"input": "cb\r\naa\r\n", "output": "NO\r\n"}, {"input": "abaab\r\naabba\r\n", "output": "NO\r\n"}, {"input": "aab\r\naaa\r\n", "output": "NO\r\n"}, {"input": "abcde\r\nedcba\r\n", "output": "NO\r\n"}, {"input": "abab\r\nbaba\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbac\r\n", "output": "NO\r\n"}, {"input": "abcd\r\naqcb\r\n", "output": "NO\r\n"}, {"input": "abc\r\nbad\r\n", "output": "NO\r\n"}, {"input": "ab\r\nca\r\n", "output": "NO\r\n"}, {"input": "abc\r\nab\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbae\r\n", "output": "NO\r\n"}, {"input": "aaaabcccca\r\naaaadccccb\r\n", "output": "NO\r\n"}, {"input": "acaa\r\nabca\r\n", "output": "NO\r\n"}, {"input": "aba\r\naab\r\n", "output": "YES\r\n"}, {"input": "ah\r\nla\r\n", "output": "NO\r\n"}, {"input": "aab\r\naa\r\n", "output": "NO\r\n"}, {"input": "abab\r\nabcd\r\n", "output": "NO\r\n"}, {"input": "ed\r\nab\r\n", "output": "NO\r\n"}]
false
stdio
null
true
605/B
605
B
Python 3
TESTS
26
1,153
15,052,800
15904854
readInts=lambda: list(map(int, input().split())) n,m=readInts() edge=[] for _ in range(m): l,f=readInts() if f==1: f=-1 edge+=[(l,f,_)] edge.sort() ok=True cnt=0;ret=[(0,0)]*m u=2;v=3;t=2 for e in edge: if e[1]==-1: ret[e[2]]=(1,t) cnt+=t-2 t+=1 elif cnt<=0: ok=False break else: ret[e[2]]=(u,v) cnt-=1 v+=1 if v==n+1: u+=1 v=u+1 if ok==False: print(-1) else: for e in ret: print(*e)
76
576
28,979,200
231030378
f = lambda: map(int, input().split()) n, m = f() p = [] for i in range(m): a, b = f() p.append((a, 1 - b, i)) p.sort() k = j = 0 s = [0] * m u, v = 1, 3 for a, b, i in p: if not b: k += j s[i] = (j + 1, j + 2) j += 1 elif k: k -= 1 s[i] = (u, v) if v - u - 2: u += 1 else: u, v = 1, v + 1 else: print(-1) exit(0) for x, y in s: print(x, y)
Codeforces Round 335 (Div. 1)
CF
2,015
2
256
Lazy Student
Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition: The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the original graph G, and the sum of the weights of its edges is the minimum possible among all such trees. Vladislav drew a graph with n vertices and m edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct.
The first line of the input contains two integers n and m ($$2 \leq n \leq 100\ 000, 1 \leq m \leq 100\ 000, n - 1 \leq m \leq \frac{n(n-1)}{2}$$) — the number of vertices and the number of edges in the graph. Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 ≤ aj ≤ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not. It is guaranteed that exactly n - 1 number {bj} are equal to one and exactly m - n + 1 of them are equal to zero.
If Vladislav has made a mistake and such graph doesn't exist, print - 1. Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 ≤ uj, vj ≤ n, uj ≠ vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with bj = 1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them.
null
null
[{"input": "4 5\n2 1\n3 1\n4 0\n1 1\n5 0", "output": "2 4\n1 4\n3 4\n3 1\n3 2"}, {"input": "3 3\n1 0\n2 1\n3 1", "output": "-1"}]
1,700
["constructive algorithms", "data structures", "graphs"]
76
[{"input": "4 5\r\n2 1\r\n3 1\r\n4 0\r\n1 1\r\n5 0\r\n", "output": "2 4\r\n1 4\r\n3 4\r\n3 1\r\n3 2\r\n"}, {"input": "3 3\r\n1 0\r\n2 1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n7 1\r\n", "output": "1 2\r\n"}, {"input": "3 2\r\n8 1\r\n9 1\r\n", "output": "1 2\r\n1 3\r\n"}, {"input": "3 3\r\n4 1\r\n5 0\r\n7 1\r\n", "output": "-1\r\n"}, {"input": "3 3\r\n4 1\r\n5 1\r\n7 0\r\n", "output": "1 2\r\n1 3\r\n2 3\r\n"}, {"input": "3 3\r\n4 1\r\n4 0\r\n4 1\r\n", "output": "1 2\r\n2 3\r\n1 3\r\n"}, {"input": "3 3\r\n4 0\r\n5 1\r\n4 1\r\n", "output": "-1\r\n"}, {"input": "3 3\r\n5 0\r\n4 1\r\n5 1\r\n", "output": "2 3\r\n1 2\r\n1 3\r\n"}, {"input": "4 4\r\n2 1\r\n3 0\r\n3 1\r\n4 1\r\n", "output": "1 2\r\n2 3\r\n1 3\r\n1 4\r\n"}, {"input": "4 5\r\n4 1\r\n4 1\r\n4 0\r\n4 0\r\n6 1\r\n", "output": "-1\r\n"}, {"input": "4 6\r\n2 1\r\n4 0\r\n3 0\r\n1 1\r\n4 1\r\n5 0\r\n", "output": "1 3\r\n2 4\r\n2 3\r\n1 2\r\n1 4\r\n3 4\r\n"}, {"input": "4 4\r\n2 1\r\n6 0\r\n7 1\r\n7 1\r\n", "output": "-1\r\n"}, {"input": "4 4\r\n2 1\r\n8 0\r\n8 1\r\n8 1\r\n", "output": "1 2\r\n2 3\r\n1 3\r\n1 4\r\n"}, {"input": "4 4\r\n2 0\r\n2 1\r\n8 1\r\n2 1\r\n", "output": "2 3\r\n1 2\r\n1 4\r\n1 3\r\n"}, {"input": "4 4\r\n2 1\r\n3 1\r\n1 1\r\n4 0\r\n", "output": "1 3\r\n1 4\r\n1 2\r\n2 3\r\n"}, {"input": "4 5\r\n3 1\r\n4 1\r\n4 0\r\n6 0\r\n6 1\r\n", "output": "1 2\r\n1 3\r\n2 3\r\n2 4\r\n1 4\r\n"}, {"input": "4 5\r\n7 0\r\n3 0\r\n1 1\r\n5 1\r\n7 1\r\n", "output": "-1\r\n"}, {"input": "4 6\r\n2 1\r\n7 1\r\n3 0\r\n1 1\r\n7 0\r\n6 0\r\n", "output": "-1\r\n"}, {"input": "4 6\r\n1 1\r\n3 1\r\n2 0\r\n2 1\r\n3 0\r\n3 0\r\n", "output": "1 2\r\n1 4\r\n2 3\r\n1 3\r\n2 4\r\n3 4\r\n"}, {"input": "4 6\r\n1 1\r\n4 1\r\n2 0\r\n2 1\r\n4 0\r\n3 0\r\n", "output": "-1\r\n"}, {"input": "10 15\r\n900000012 1\r\n900000010 1\r\n900000007 0\r\n900000005 0\r\n900000014 1\r\n900000000 1\r\n900000004 0\r\n900000006 1\r\n900000009 0\r\n900000002 0\r\n900000008 0\r\n900000001 1\r\n900000011 1\r\n900000003 1\r\n900000013 1\r\n", "output": "1 8\r\n1 6\r\n2 5\r\n3 4\r\n1 10\r\n1 2\r\n2 4\r\n1 5\r\n4 5\r\n2 3\r\n3 5\r\n1 3\r\n1 7\r\n1 4\r\n1 9\r\n"}, {"input": "10 15\r\n900000007 1\r\n900000002 1\r\n900000004 0\r\n900000002 1\r\n900000006 1\r\n900000000 1\r\n900000006 1\r\n900000008 1\r\n900000002 0\r\n900000003 0\r\n900000002 0\r\n900000005 0\r\n900000001 0\r\n900000000 1\r\n900000008 1\r\n", "output": "1 8\r\n1 4\r\n3 5\r\n1 5\r\n1 6\r\n1 2\r\n1 7\r\n1 9\r\n2 4\r\n2 5\r\n3 4\r\n4 5\r\n2 3\r\n1 3\r\n1 10\r\n"}, {"input": "10 15\r\n900000004 0\r\n900000006 1\r\n900000001 1\r\n900000004 1\r\n900000007 1\r\n900000007 1\r\n900000004 1\r\n900000008 1\r\n900000004 0\r\n900000004 0\r\n900000007 1\r\n900000005 0\r\n900000004 0\r\n900000002 0\r\n900000000 1\r\n", "output": "2 4\r\n1 6\r\n1 3\r\n1 4\r\n1 7\r\n1 8\r\n1 5\r\n1 10\r\n3 4\r\n2 5\r\n1 9\r\n4 5\r\n3 5\r\n2 3\r\n1 2\r\n"}, {"input": "10 15\r\n900000006 1\r\n900000000 1\r\n900000004 0\r\n900000000 1\r\n900000004 0\r\n900000006 1\r\n900000000 1\r\n900000006 1\r\n900000005 1\r\n900000001 0\r\n900000003 1\r\n900000006 1\r\n900000000 0\r\n900000003 0\r\n900000000 0\r\n", "output": "1 7\r\n1 2\r\n3 5\r\n1 3\r\n4 5\r\n1 8\r\n1 4\r\n1 9\r\n1 6\r\n3 4\r\n1 5\r\n1 10\r\n2 3\r\n2 5\r\n2 4\r\n"}, {"input": "10 15\r\n900000000 1\r\n900000003 1\r\n900000000 1\r\n900000000 0\r\n900000003 0\r\n900000005 1\r\n900000005 1\r\n900000005 1\r\n900000001 0\r\n900000002 0\r\n900000002 0\r\n900000004 1\r\n900000002 0\r\n900000000 1\r\n900000004 1\r\n", "output": "-1\r\n"}, {"input": "10 15\r\n900000001 1\r\n900000001 1\r\n900000002 1\r\n900000001 1\r\n900000001 0\r\n900000001 1\r\n900000001 0\r\n900000001 0\r\n900000001 0\r\n900000001 1\r\n900000001 0\r\n900000001 0\r\n900000004 1\r\n900000000 1\r\n900000001 1\r\n", "output": "1 3\r\n1 4\r\n1 9\r\n1 5\r\n2 3\r\n1 6\r\n2 4\r\n3 4\r\n2 5\r\n1 7\r\n3 5\r\n4 5\r\n1 10\r\n1 2\r\n1 8\r\n"}, {"input": "10 15\r\n900000001 1\r\n900000001 1\r\n900000001 0\r\n900000000 1\r\n900000001 0\r\n900000002 1\r\n900000000 1\r\n900000002 1\r\n900000001 0\r\n900000001 0\r\n900000001 0\r\n900000002 1\r\n900000000 0\r\n900000002 1\r\n900000000 1\r\n", "output": "1 5\r\n1 6\r\n2 4\r\n1 2\r\n3 4\r\n1 7\r\n1 3\r\n1 8\r\n2 5\r\n3 5\r\n4 5\r\n1 9\r\n2 3\r\n1 10\r\n1 4\r\n"}, {"input": "5 5\r\n1 1\r\n2 1\r\n3 0\r\n4 1\r\n5 1\r\n", "output": "1 2\r\n1 3\r\n2 3\r\n1 4\r\n1 5\r\n"}, {"input": "5 6\r\n1 1\r\n2 1\r\n3 0\r\n4 1\r\n5 0\r\n6 1\r\n", "output": "1 2\r\n1 3\r\n2 3\r\n1 4\r\n2 4\r\n1 5\r\n"}, {"input": "5 6\r\n1 1\r\n2 1\r\n3 0\r\n4 0\r\n5 1\r\n6 1\r\n", "output": "-1\r\n"}, {"input": "5 7\r\n1 1\r\n1 1\r\n1 0\r\n2 0\r\n1 0\r\n2 1\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "5 8\r\n1 0\r\n1 1\r\n1 1\r\n2 0\r\n1 0\r\n2 1\r\n1 0\r\n1 1\r\n", "output": "2 3\r\n1 2\r\n1 3\r\n2 5\r\n2 4\r\n1 5\r\n3 4\r\n1 4\r\n"}, {"input": "5 8\r\n1 0\r\n1 1\r\n1 1\r\n3 0\r\n1 0\r\n3 1\r\n2 0\r\n1 1\r\n", "output": "2 3\r\n1 2\r\n1 3\r\n2 5\r\n2 4\r\n1 5\r\n3 4\r\n1 4\r\n"}, {"input": "5 8\r\n1 0\r\n1 1\r\n1 1\r\n3 0\r\n1 0\r\n4 1\r\n2 0\r\n1 1\r\n", "output": "-1\r\n"}, {"input": "5 9\r\n1 1\r\n2 1\r\n3 0\r\n4 1\r\n5 0\r\n6 0\r\n7 1\r\n8 0\r\n9 0\r\n", "output": "1 2\r\n1 3\r\n2 3\r\n1 4\r\n2 4\r\n3 4\r\n1 5\r\n2 5\r\n3 5\r\n"}, {"input": "5 9\r\n1 1\r\n2 1\r\n3 0\r\n4 1\r\n5 0\r\n6 0\r\n7 0\r\n8 1\r\n9 0\r\n", "output": "-1\r\n"}, {"input": "5 10\r\n1 1\r\n1 1\r\n1 0\r\n1 1\r\n2 0\r\n2 0\r\n2 1\r\n2 0\r\n2 0\r\n2 0\r\n", "output": "1 2\r\n1 3\r\n2 3\r\n1 4\r\n2 4\r\n3 4\r\n1 5\r\n2 5\r\n3 5\r\n4 5\r\n"}, {"input": "5 10\r\n1 1\r\n1 1\r\n1 0\r\n1 1\r\n2 0\r\n2 0\r\n3 1\r\n2 0\r\n3 0\r\n3 0\r\n", "output": "-1\r\n"}, {"input": "10 15\r\n761759620 0\r\n761759620 1\r\n787655728 1\r\n761759620 0\r\n294001884 1\r\n465325912 1\r\n787655728 0\r\n683571303 1\r\n683571303 0\r\n761759620 0\r\n787655728 0\r\n391499930 1\r\n758807870 1\r\n611782565 1\r\n132266542 1\r\n", "output": "2 4\r\n1 9\r\n1 10\r\n3 4\r\n1 3\r\n1 5\r\n3 5\r\n1 7\r\n2 3\r\n2 5\r\n4 5\r\n1 4\r\n1 8\r\n1 6\r\n1 2\r\n"}, {"input": "10 15\r\n752087443 1\r\n537185872 1\r\n439895449 1\r\n494086747 1\r\n718088132 1\r\n93444012 0\r\n670136349 1\r\n545547453 0\r\n718088132 0\r\n853059674 0\r\n853059674 1\r\n762928724 1\r\n762928724 0\r\n853059674 0\r\n156495293 1\r\n", "output": "-1\r\n"}, {"input": "10 15\r\n417559883 0\r\n300974070 1\r\n292808458 1\r\n469395226 0\r\n469395226 1\r\n564721882 1\r\n125636288 1\r\n417559883 0\r\n417559883 1\r\n469395226 0\r\n376390930 1\r\n233782394 1\r\n780369860 1\r\n564721882 0\r\n417559883 0\r\n", "output": "2 3\r\n1 5\r\n1 4\r\n2 5\r\n1 8\r\n1 9\r\n1 2\r\n2 4\r\n1 7\r\n3 5\r\n1 6\r\n1 3\r\n1 10\r\n4 5\r\n3 4\r\n"}]
false
stdio
import sys from collections import deque def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: input_lines = f.read().splitlines() with open(output_path) as f: reference_output = f.read().strip() with open(submission_path) as f: submission_lines = f.read().splitlines() if len(submission_lines) == 0: print(0) return submission_first_line = submission_lines[0].strip() if submission_first_line == '-1': if reference_output.strip() == '-1': print(1) else: print(0) return # Parse input n, m = map(int, input_lines[0].split()) input_edges = [] for line in input_lines[1:m+1]: a, b = map(int, line.split()) input_edges.append((a, b)) # Parse submission output if len(submission_lines) != m: print(0) return submission_edges = [] for line in submission_lines: parts = line.split() if len(parts) != 2: print(0) return u, v = map(int, parts) submission_edges.append((u, v)) # Check for loops for u, v in submission_edges: if u == v: print(0) return # Check for duplicate edges edge_set = set() for u, v in submission_edges: if u > v: u, v = v, u if (u, v) in edge_set: print(0) return edge_set.add((u, v)) # Collect MST edges and their weights mst_submission_edges = [] for i in range(m): a, b = input_edges[i] if b == 1: u, v = submission_edges[i] mst_submission_edges.append((u, v, a)) # Check if MST edges form a tree using DSU parent = list(range(n + 1)) # 1-based def find(u): while parent[u] != u: parent[u] = parent[parent[u]] u = parent[u] return u for u, v, a in mst_submission_edges: u_root = find(u) v_root = find(v) if u_root == v_root: print(0) return parent[v_root] = u_root # Check connectivity of MST root = find(1) for u in range(1, n + 1): if find(u) != root: print(0) return # Build adjacency list for MST edges adj = [[] for _ in range(n + 1)] for u, v, a in mst_submission_edges: adj[u].append((v, a)) adj[v].append((u, a)) # BFS to build parent and edge weight arrays parent_arr = [0] * (n + 1) edge_weight_arr = [0] * (n + 1) depth = [0] * (n + 1) visited = [False] * (n + 1) q = deque([1]) visited[1] = True while q: u = q.popleft() for v, a in adj[u]: if not visited[v]: visited[v] = True parent_arr[v] = u edge_weight_arr[v] = a depth[v] = depth[u] + 1 q.append(v) # Preprocess binary lifting tables log_max = 20 up = [[0] * (n + 1) for _ in range(log_max)] max_edge = [[0] * (n + 1) for _ in range(log_max)] for i in range(1, n + 1): up[0][i] = parent_arr[i] max_edge[0][i] = edge_weight_arr[i] for k in range(1, log_max): for i in range(1, n + 1): up[k][i] = up[k-1][up[k-1][i]] max_edge[k][i] = max(max_edge[k-1][i], max_edge[k-1][up[k-1][i]]) # Function to get max edge on path from u to v def get_max_edge(u, v): max_e = 0 if depth[u] < depth[v]: u, v = v, u # Bring u up to depth of v for k in range(log_max-1, -1, -1): if depth[u] - (1 << k) >= depth[v]: max_e = max(max_e, max_edge[k][u]) u = up[k][u] if u == v: return max_e # Now move both up to find LCA for k in range(log_max-1, -1, -1): if up[k][u] != up[k][v]: max_e = max(max_e, max_edge[k][u], max_edge[k][v]) u = up[k][u] v = up[k][v] max_e = max(max_e, edge_weight_arr[u], edge_weight_arr[v]) return max_e # Check each non-MST edge for i in range(m): a_j, b_j = input_edges[i] if b_j == 0: u, v = submission_edges[i] path_max = get_max_edge(u, v) if a_j < path_max: print(0) return print(1) if __name__ == "__main__": main()
true
81/B
81
B
Python 3
TESTS
4
92
0
31930489
s = input().split(",") for j in range(len(s)): el = s[j] i = 0 for vv in el.replace("...", " ...").split(): if len(vv) > 0: print((" " if i > 0 else "") + vv, end="") i += 1 if j + 1 < len(s): print(", ", end="")
45
124
512,000
158761717
s = input() lst = [] i = 0 while i < len(s): if s[i].isdigit(): num = '' while i < len(s) and s[i].isdigit(): num += s[i] i += 1 lst.append(num) continue if s[i] == '.': lst.append('...') i += 2 elif s[i] != ' ': lst.append(s[i]) i += 1 lstcopy = [] for i in range(len(lst)): if lst[i] == '...' and i != 0 and lstcopy[-1] != ' ': lstcopy.append(' ') lstcopy.append('...') elif lst[i] == ',' and i != len(lst) - 1: lstcopy.append(',') lstcopy.append(' ') elif i + 1 < len(lst) and lst[i].isdigit() and lst[i+1].isdigit(): lstcopy.append(lst[i]) lstcopy.append(' ') else: lstcopy.append(lst[i]) print(''.join(lstcopy))
Yandex.Algorithm Open 2011: Qualification 1
CF
2,011
2
256
Sequence Formatting
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "1,2 ,3,...,   10" will be corrected to "1, 2, 3, ..., 10". In this task you are given a string s, which is composed by a concatination of terms, each of which may be: - a positive integer of an arbitrary length (leading zeroes are not allowed), - a "comma" symbol (","), - a "space" symbol (" "), - "three dots" ("...", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string s to ensure the following: - each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), - each "three dots" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), - if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, - there should not be other spaces. Automate Polycarp's work and write a program that will process the given string s.
The input data contains a single string s. Its length is from 1 to 255 characters. The string s does not begin and end with a space. Its content matches the description given above.
Print the string s after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it.
null
null
[{"input": "1,2 ,3,..., 10", "output": "1, 2, 3, ..., 10"}, {"input": "1,,,4...5......6", "output": "1, , , 4 ...5 ... ...6"}, {"input": "...,1,2,3,...", "output": "..., 1, 2, 3, ..."}]
1,700
["implementation", "strings"]
45
[{"input": "1,2 ,3,..., 10\r\n", "output": "1, 2, 3, ..., 10\r\n"}, {"input": "1,,,4...5......6\r\n", "output": "1, , , 4 ...5 ... ...6\r\n"}, {"input": "...,1,2,3,...\r\n", "output": "..., 1, 2, 3, ...\r\n"}, {"input": "......\r\n", "output": "... ...\r\n"}, {"input": ",,,,,,,,,,,,,\r\n", "output": ", , , , , , , , , , , , ,\r\n"}, {"input": "123456789\r\n", "output": "123456789\r\n"}, {"input": ",\r\n", "output": ",\r\n"}, {"input": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n", "output": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n"}, {"input": "1,2,,,,,,,,,5566\r\n", "output": "1, 2, , , , , , , , , 5566\r\n"}, {"input": ".........1......1......23......31...2......\r\n", "output": "... ... ...1 ... ...1 ... ...23 ... ...31 ...2 ... ...\r\n"}, {"input": "...\r\n", "output": "...\r\n"}, {"input": ",...\r\n", "output": ", ...\r\n"}, {"input": "...,\r\n", "output": "...,\r\n"}, {"input": ",,\r\n", "output": ", ,\r\n"}, {"input": ",...,\r\n", "output": ", ...,\r\n"}, {"input": "...,...\r\n", "output": "..., ...\r\n"}, {"input": "1...10\r\n", "output": "1 ...10\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ,\r\n", "output": ", ,\r\n"}, {"input": "............................................................................................................................................................................................................................................................\r\n", "output": "... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ..."}, {"input": "1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1\r\n", "output": "1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1"}, {"input": "12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12\r\n", "output": "12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12\r\n"}, {"input": "1,\r\n", "output": "1,\r\n"}, {"input": ",1\r\n", "output": ", 1\r\n"}, {"input": "1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n", "output": "1 1\r\n"}, {"input": "1 1 1\r\n", "output": "1 1 1\r\n"}, {"input": "... ,\r\n", "output": "...,\r\n"}, {"input": "......,,,,,...... 1234 1234 1234 , 1234 ... , 1234 ... 1234 ... , 1234\r\n", "output": "... ..., , , , , ... ...1234 1234 1234, 1234 ..., 1234 ...1234 ..., 1234\r\n"}, {"input": "9999999999999999999999999999999999999999999999999999999999, 1\r\n", "output": "9999999999999999999999999999999999999999999999999999999999, 1\r\n"}, {"input": "5555555555555555555 6666 77777777 8888888888888 ,,...,55,5...55...,.........5 , , ... , 5 5 , 5 ... 5 5 ... , ... ... ... 5\r\n", "output": "5555555555555555555 6666 77777777 8888888888888, , ..., 55, 5 ...55 ..., ... ... ...5, , ..., 5 5, 5 ...5 5 ..., ... ... ...5\r\n"}, {"input": "...5\r\n", "output": "...5\r\n"}, {"input": ", 12 12, ...\r\n", "output": ", 12 12, ...\r\n"}, {"input": ",12 12,...\r\n", "output": ", 12 12, ...\r\n"}, {"input": "1,2 4,78 799,4...5 3......6,\r\n", "output": "1, 2 4, 78 799, 4 ...5 3 ... ...6,\r\n"}]
false
stdio
null
true
81/B
81
B
PyPy 3
TESTS
4
278
0
92889570
s = input() s = s.split(",") s = [ " ...".join(i.split("...")) if "." in i else i for i in s ] s = [ i.strip() for i in s] s = ", ".join(s) print(s)
45
124
6,758,400
125649588
line = input() b = [] for e in line.split(','): c = [' '.join(ee.split()) for ee in e.split('...')] b.append(' ...'.join(c).strip()) print(', '.join(b).strip())
Yandex.Algorithm Open 2011: Qualification 1
CF
2,011
2
256
Sequence Formatting
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "1,2 ,3,...,   10" will be corrected to "1, 2, 3, ..., 10". In this task you are given a string s, which is composed by a concatination of terms, each of which may be: - a positive integer of an arbitrary length (leading zeroes are not allowed), - a "comma" symbol (","), - a "space" symbol (" "), - "three dots" ("...", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string s to ensure the following: - each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), - each "three dots" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), - if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, - there should not be other spaces. Automate Polycarp's work and write a program that will process the given string s.
The input data contains a single string s. Its length is from 1 to 255 characters. The string s does not begin and end with a space. Its content matches the description given above.
Print the string s after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it.
null
null
[{"input": "1,2 ,3,..., 10", "output": "1, 2, 3, ..., 10"}, {"input": "1,,,4...5......6", "output": "1, , , 4 ...5 ... ...6"}, {"input": "...,1,2,3,...", "output": "..., 1, 2, 3, ..."}]
1,700
["implementation", "strings"]
45
[{"input": "1,2 ,3,..., 10\r\n", "output": "1, 2, 3, ..., 10\r\n"}, {"input": "1,,,4...5......6\r\n", "output": "1, , , 4 ...5 ... ...6\r\n"}, {"input": "...,1,2,3,...\r\n", "output": "..., 1, 2, 3, ...\r\n"}, {"input": "......\r\n", "output": "... ...\r\n"}, {"input": ",,,,,,,,,,,,,\r\n", "output": ", , , , , , , , , , , , ,\r\n"}, {"input": "123456789\r\n", "output": "123456789\r\n"}, {"input": ",\r\n", "output": ",\r\n"}, {"input": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n", "output": "1 4 5 6 7 999 1 1 1 2 311111111111111111111111111111111111111111\r\n"}, {"input": "1,2,,,,,,,,,5566\r\n", "output": "1, 2, , , , , , , , , 5566\r\n"}, {"input": ".........1......1......23......31...2......\r\n", "output": "... ... ...1 ... ...1 ... ...23 ... ...31 ...2 ... ...\r\n"}, {"input": "...\r\n", "output": "...\r\n"}, {"input": ",...\r\n", "output": ", ...\r\n"}, {"input": "...,\r\n", "output": "...,\r\n"}, {"input": ",,\r\n", "output": ", ,\r\n"}, {"input": ",...,\r\n", "output": ", ...,\r\n"}, {"input": "...,...\r\n", "output": "..., ...\r\n"}, {"input": "1...10\r\n", "output": "1 ...10\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ...\r\n", "output": ", ...\r\n"}, {"input": ", ,\r\n", "output": ", ,\r\n"}, {"input": "............................................................................................................................................................................................................................................................\r\n", "output": "... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ..."}, {"input": "1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1...1\r\n", "output": "1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1 ...1"}, {"input": "12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12\r\n", "output": "12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12\r\n"}, {"input": "1,\r\n", "output": "1,\r\n"}, {"input": ",1\r\n", "output": ", 1\r\n"}, {"input": "1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n", "output": "1 1\r\n"}, {"input": "1 1 1\r\n", "output": "1 1 1\r\n"}, {"input": "... ,\r\n", "output": "...,\r\n"}, {"input": "......,,,,,...... 1234 1234 1234 , 1234 ... , 1234 ... 1234 ... , 1234\r\n", "output": "... ..., , , , , ... ...1234 1234 1234, 1234 ..., 1234 ...1234 ..., 1234\r\n"}, {"input": "9999999999999999999999999999999999999999999999999999999999, 1\r\n", "output": "9999999999999999999999999999999999999999999999999999999999, 1\r\n"}, {"input": "5555555555555555555 6666 77777777 8888888888888 ,,...,55,5...55...,.........5 , , ... , 5 5 , 5 ... 5 5 ... , ... ... ... 5\r\n", "output": "5555555555555555555 6666 77777777 8888888888888, , ..., 55, 5 ...55 ..., ... ... ...5, , ..., 5 5, 5 ...5 5 ..., ... ... ...5\r\n"}, {"input": "...5\r\n", "output": "...5\r\n"}, {"input": ", 12 12, ...\r\n", "output": ", 12 12, ...\r\n"}, {"input": ",12 12,...\r\n", "output": ", 12 12, ...\r\n"}, {"input": "1,2 4,78 799,4...5 3......6,\r\n", "output": "1, 2 4, 78 799, 4 ...5 3 ... ...6,\r\n"}]
false
stdio
null
true
985/B
985
B
PyPy 3
TESTS
12
639
29,696,000
129797626
n, m = map(int, input().split()) colCount = [0] * m for i in range(n): colCount = [col + entry for col, entry in zip(colCount, (int(s) for s in input()))] if colCount.count(1) < n: print("YES") else: print("NO")
67
140
1,228,800
58690510
R = lambda : map(int, input().split()) n,m = R() a = [] b = 0 c = 0 for i in range(n): s = int(input(),2) a.append(s) c |=s&b b |=s print(('YES','NO')[all(i&~c for i in a)])
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
3
256
Switches and Lamps
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp. Initially all m lamps are turned off. Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards. It is guaranteed that if you push all n switches then all m lamps will be turned on. Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps. The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on.
Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.
null
null
[{"input": "4 5\n10101\n01000\n00111\n10000", "output": "YES"}, {"input": "4 5\n10100\n01000\n00110\n00101", "output": "NO"}]
1,200
["implementation"]
67
[{"input": "4 5\r\n10101\r\n01000\r\n00111\r\n10000\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n01000\r\n00110\r\n00101\r\n", "output": "NO\r\n"}, {"input": "1 5\r\n11111\r\n", "output": "NO\r\n"}, {"input": "10 1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\n1\r\n", "output": "NO\r\n"}, {"input": "3 4\r\n1010\r\n0100\r\n1101\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10101\r\n11111\r\n", "output": "YES\r\n"}, {"input": "5 5\r\n10000\r\n11000\r\n11100\r\n11110\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n01000\r\n10100\r\n00010\r\n10101\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n10\r\n11\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n00100\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n00000\r\n11000\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "4 3\r\n000\r\n010\r\n001\r\n100\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n10101\r\n01000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n01000\r\n10101\r\n00111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n01\r\n11\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n101\r\n000\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n00\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10110\r\n11000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "3 8\r\n00111111\r\n01011100\r\n11000000\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n100000\r\n110000\r\n001100\r\n000011\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n11111\r\n00000\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n101\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n01000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n00\r\n11\r\n", "output": "YES\r\n"}, {"input": "4 15\r\n111110100011010\r\n111111011010110\r\n101000001011001\r\n100110000111011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n010\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n11000\r\n00110\r\n00101\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1111\r\n0000\r\n0000\r\n0000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n11100\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "2 1\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1000\r\n1001\r\n0010\r\n0100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00110\r\n10011\r\n01100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10101\r\n00111\r\n01000\r\n", "output": "NO\r\n"}, {"input": "4 5\r\n00101\r\n00011\r\n01000\r\n10010\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n110\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 2\r\n10\r\n01\r\n10\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n000\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n100\r\n011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n111\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n1010\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n1010\r\n0101\r\n1000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n0110\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n101\r\n001\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10001\r\n10010\r\n01010\r\n00101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n000\r\n000\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "3 10\r\n1111011100\r\n0001100011\r\n1111010101\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0110\r\n1010\r\n0101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n001\r\n011\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n010\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n010\r\n100\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 5\r\n10101\r\n01010\r\n01010\r\n", "output": "YES\r\n"}, {"input": "2 6\r\n111111\r\n000000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00000\r\n10101\r\n01010\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n111000\r\n100100\r\n010010\r\n001001\r\n", "output": "YES\r\n"}]
false
stdio
null
true
451/B
451
B
PyPy 3-64
TESTS
73
93
18,227,200
225631284
n=int(input()) a=list(map(int,input().split())) u=[] for i in a : u.append(i) u.sort() oa=[] o=[-1,-1] for i in range(0,n) : j=i while j<n-1 and a[j]>=a[j+1] : if j==i : o[0]=j+1 oa.append(a[j]) j+=1 oa.append(a[j]) o[1]=j+1 if j!=i : break p=0 for i in range(o[1],len(a)): if a[i]!=u[i] : p+=1 break if oa==u : print("yes") print(1,1) elif p==0 : print("yes") print(o[0],o[1]) else : print("no")
76
78
12,800,000
217513695
x = int(input()) l = list(map(int, input().split())) c = sorted(l) if l == c: print("yes") print("1 1") else: left, right = 0, x - 1 while l[left] == c[left]: left += 1 while l[right] == c[right] and right > 0: right -= 1 v = l[left:right + 1][::-1] k = c[left:right + 1] if v == k: print("yes") print(left + 1, right + 1) else: print("no")
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
451/B
451
B
PyPy 3-64
TESTS
73
93
15,667,200
189999151
N = int(input()) nums = list(map(int,input().split())) nums.append(float("inf")) left = 0 count = 0 right = 0 ans = [] while right < len(nums): if nums[left] < nums[right]: if right - left != 1: ans.append([left + 1,right]) left = right right+=1 if len(ans) == 0: print("yes") print(1, 1) elif len(ans) == 1 : c = nums[ans[0][0] - 1: ans[0][1]] b = sorted(c) c.reverse() if c == b: print("yes") print(*ans[0]) else: print("no") else: print("no") # 2 1 3 4
76
78
12,800,000
219412086
# Read the size of the array n = int(input()) # Read the array a = list(map(int, input().split())) # Create a sorted version of the array sorted_a = sorted(a) # Find the first and last index where the original and sorted arrays differ start = -1 end = -1 for i in range(n): if a[i] != sorted_a[i]: if start == -1: start = i end = i # If the array is already sorted, print "yes" and the indices (1-based) of the first element if start == -1: print("yes") print(1, 1) else: # Check if reversing the segment makes the entire array sorted if a[start:end+1] == sorted_a[start:end+1][::-1]: print("yes") print(start + 1, end + 1) else: print("no")
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
451/B
451
B
PyPy 3-64
TESTS
73
93
13,619,200
226878791
n = int(input()) a = [int(x) for x in input().split(' ')] l = 0 r = 0 inorder = True for i in range(n-1): if a[i] > a[i+1]: if not inorder: r += 1 elif inorder: l = i r = i+1 inorder = not inorder elif a[i] < a[i+1] and not inorder: a = a[:l] + sorted(a[l:r+1]) + a[r+1:] for j in range(n-1): if a[j] > a[j+1]: print("no") break else: print("yes") print(f"{l+1} {r+1}") break else: print("yes") print(f"{l+1} {r+1}")
76
78
12,902,400
207293751
def main(): n = int(input()) a = list(map(int, input().split())) b = sorted(a, reverse=True) c = sorted(a) start = -1 for i in range(n - 1): if a[i] > a[i + 1]: start = i break if start == 0 and a == b: print("yes\n{} {}".format(1, n)) return end = -1 for i in range(n - 1, 0, -1): if a[i] < a[i - 1]: end = i break if start == -1 and end == -1: print("yes\n1 1") return a[start : end + 1] = a[start : end + 1][::-1] if a == c: print("yes\n{} {}".format(start + 1, end + 1)) return print("no") main()
Codeforces Round 258 (Div. 2)
CF
2,014
1
256
Sort the Array
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer. If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
null
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted. Sample 3. No segment can be reversed such that the array will be sorted. Definitions A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r]. If you have an array a of size n and you reverse its segment [l, r], the array will become: a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
[{"input": "3\n3 2 1", "output": "yes\n1 3"}, {"input": "4\n2 1 3 4", "output": "yes\n1 2"}, {"input": "4\n3 1 2 4", "output": "no"}, {"input": "2\n1 2", "output": "yes\n1 1"}]
1,300
["implementation", "sortings"]
76
[{"input": "3\r\n3 2 1\r\n", "output": "yes\r\n1 3\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "4\r\n3 1 2 4\r\n", "output": "no\r\n"}, {"input": "2\r\n1 2\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n58 4\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n69 37 27 4 2\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "9\r\n6 78 63 59 28 24 8 96 99\r\n", "output": "yes\r\n2 7\r\n"}, {"input": "6\r\n19517752 43452931 112792556 68417469 779722934 921694415\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "6\r\n169793171 335736854 449917902 513287332 811627074 938727967\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n509329 173849943 297546987 591032670 796346199 914588283\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "25\r\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100\r\n", "output": "yes\r\n1 11\r\n"}, {"input": "46\r\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100\r\n", "output": "yes\r\n25 37\r\n"}, {"input": "96\r\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\r\n", "output": "yes\r\n3 22\r\n"}, {"input": "2\r\n404928771 698395106\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "2\r\n699573624 308238132\r\n", "output": "yes\r\n1 2\r\n"}, {"input": "5\r\n75531609 242194958 437796493 433259361 942142185\r\n", "output": "yes\r\n3 4\r\n"}, {"input": "5\r\n226959376 840957605 833410429 273566427 872976052\r\n", "output": "yes\r\n2 4\r\n"}, {"input": "5\r\n373362086 994096202 767275079 734424844 515504383\r\n", "output": "yes\r\n2 5\r\n"}, {"input": "5\r\n866379155 593548704 259097686 216134784 879911740\r\n", "output": "yes\r\n1 4\r\n"}, {"input": "5\r\n738083041 719956102 420866851 307749161 257917459\r\n", "output": "yes\r\n1 5\r\n"}, {"input": "5\r\n90786760 107075352 139104198 424911569 858427981\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n41533825 525419745 636375901 636653266 879043107 967434399\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907\r\n", "output": "yes\r\n20 35\r\n"}, {"input": "40\r\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165\r\n", "output": "no\r\n"}, {"input": "40\r\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471\r\n", "output": "no\r\n"}, {"input": "40\r\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "40\r\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992\r\n", "output": "no\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "no\r\n"}, {"input": "6\r\n1 2 5 4 3 6\r\n", "output": "yes\r\n3 5\r\n"}, {"input": "1\r\n1\r\n", "output": "yes\r\n1 1\r\n"}, {"input": "6\r\n1 5 3 4 2 6\r\n", "output": "no\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "no\r\n"}, {"input": "5\r\n2 5 4 3 1\r\n", "output": "no\r\n"}, {"input": "4\r\n2 1 4 3\r\n", "output": "no\r\n"}, {"input": "6\r\n2 1 4 3 5 6\r\n", "output": "no\r\n"}]
false
stdio
import sys def read_input(input_path): with open(input_path, 'r') as f: n = int(f.readline()) arr = list(map(int, f.readline().split())) return arr def read_output(file_path): with open(file_path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] return lines def is_sorted(arr): for i in range(len(arr)-1): if arr[i] > arr[i+1]: return False return True def compute_possible(arr): if is_sorted(arr): return True n = len(arr) left = 0 while left < n-1 and arr[left] <= arr[left+1]: left += 1 right = n-1 while right > 0 and arr[right] >= arr[right-1]: right -= 1 reversed_segment = arr[:left] + arr[left:right+1][::-1] + arr[right+1:] return is_sorted(reversed_segment) def main(input_path, output_path, submission_output_path): arr = read_input(input_path) submission = read_output(submission_output_path) if not submission: print(0) return possible = compute_possible(arr) if submission[0].lower() not in ['yes', 'no']: print(0) return if submission[0].lower() == 'yes': if len(submission) < 2: print(0) return try: l, r = map(int, submission[1].split()) except: print(0) return if l < 1 or r > len(arr) or l > r: print(0) return l0 = l - 1 r0 = r - 1 new_arr = arr[:l0] + arr[l0:r0+1][::-1] + arr[r0+1:] if is_sorted(new_arr): print(100) else: print(0) else: if possible: print(0) else: print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
908/B
908
B
PyPy 3
TESTS
3
124
2,457,600
116974072
import sys import string from collections import Counter, defaultdict from math import fsum, sqrt, gcd, ceil, factorial from operator import add from itertools import permutations inf = float("inf") # input = sys.stdin.readline flush = lambda: sys.stdout.flush comb = lambda x, y: (factorial(x) // factorial(y)) // factorial(x - y) # inputs # ip = lambda : input().rstrip() ip = lambda: input() ii = lambda: int(input()) r = lambda: map(int, input().split()) rr = lambda: list(r()) a, b = r() arr = [ip() for _ in range(a)] s = ip() x = permutations(list("0123")) for i in range(a): for j in range(b): if arr[i][j] == "S": start = (i, j) if arr[i][j] == "E": end = (i, j) def ok(u, d, l, r): x, y = start for i in s: if i == u: y += 1 if i == d: y -= 1 if i == r: x += 1 if i == l: x -= 1 if x < 0 or x == a or y < 0 or y == b: return False if (x, y) == end: return True return False c = 0 for i in x: if ok(*i): c += 1 print(c)
46
46
0
161042700
import sys input = sys.stdin.readline from itertools import permutations n, m = map(int, input().split()) g = [input()[:-1] for _ in range(n)] s = input()[:-1] a1, b1 = 0, 0 c1, d1 = 0, 0 for i in range(n): for j in range(m): if g[i][j] == 'S': a1 = i b1 = j if g[i][j] == 'E': c1 = i d1 = j q = '0123' q = list(permutations(q)) ans = 0 for (i1,i2,i3,i4) in q: a, b, c, d = a1, b1, c1, d1 for i in s: if i == i1: a += 1 if i == i2: a -= 1 if i == i3: b += 1 if i == i4: b -= 1 if a == c and b == d: ans += 1 break if a < 0 or a >= n or b < 0 or b >= m: break if g[a][b] == '#': break print(ans)
Good Bye 2017
CF
2,017
1
256
New Year and Buggy Bot
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. The robot can only move up, left, right, or down. When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.
The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
null
For the first sample, the only valid mapping is $$0 \rightarrow D, 1 \rightarrow L, 2 \rightarrow U, 3 \rightarrow R$$, where D is down, L is left, U is up, R is right.
[{"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012", "output": "1"}, {"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021", "output": "14"}, {"input": "5 3\n...\n.S.\n###\n.E.\n...\n3", "output": "0"}]
1,200
["brute force", "implementation"]
46
[{"input": "5 6\r\n.....#\r\nS....#\r\n.#....\r\n.#....\r\n...E..\r\n333300012\r\n", "output": "1\r\n"}, {"input": "6 6\r\n......\r\n......\r\n..SE..\r\n......\r\n......\r\n......\r\n01232123212302123021\r\n", "output": "14\r\n"}, {"input": "5 3\r\n...\r\n.S.\r\n###\r\n.E.\r\n...\r\n3\r\n", "output": "0\r\n"}, {"input": "10 10\r\n.#......#.\r\n#.........\r\n#.........\r\n....#.#..E\r\n.......#..\r\n....##....\r\n....S.....\r\n....#.....\r\n.........#\r\n...##...#.\r\n23323332313123221123020122221313323310313122323233\r\n", "output": "0\r\n"}, {"input": "8 9\r\n.........\r\n.........\r\n.........\r\n.E.#.....\r\n.........\r\n.........\r\n...#.S...\r\n.........\r\n10001100111000010121100000110110110100000100000100\r\n", "output": "2\r\n"}, {"input": "15 13\r\n.............\r\n.............\r\n.............\r\n.........#...\r\n..#..........\r\n.............\r\n..........E..\r\n.............\r\n.............\r\n.#...........\r\n.....#.......\r\n..........#..\r\n..........S..\r\n.............\r\n.........#...\r\n32222221111222312132110100022020202131222103103330\r\n", "output": "2\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n..SE.\r\n.....\r\n.....\r\n012330213120031231022103231013201032301223011230102320130231321012030321213002133201130201322031\r\n", "output": "24\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n23\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n03\r\n", "output": "4\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n22\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n11\r\n", "output": "6\r\n"}, {"input": "2 2\r\n#E\r\nS.\r\n01\r\n", "output": "2\r\n"}, {"input": "10 10\r\n####S.####\r\n#####.####\r\n#####.####\r\n#####.####\r\n#####..###\r\n######.###\r\n######.###\r\n######.E##\r\n##########\r\n##########\r\n0111101110\r\n", "output": "2\r\n"}, {"input": "10 10\r\n#####..E##\r\n#####.S.##\r\n#####...##\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n20\r\n", "output": "4\r\n"}, {"input": "10 10\r\n#####ES.##\r\n######.###\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n3\r\n", "output": "6\r\n"}, {"input": "2 10\r\nS........E\r\n..........\r\n33333333333333333\r\n", "output": "6\r\n"}, {"input": "2 2\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n##\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n012\r\n", "output": "8\r\n"}, {"input": "2 3\r\nS.E\r\n###\r\n1222\r\n", "output": "0\r\n"}, {"input": "2 5\r\nS...E\r\n.....\r\n133330\r\n", "output": "1\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n.S.E.\r\n.....\r\n.....\r\n001111\r\n", "output": "6\r\n"}, {"input": "3 5\r\n....S\r\n....#\r\n....E\r\n0112\r\n", "output": "1\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n123\r\n", "output": "8\r\n"}, {"input": "2 10\r\n........ES\r\n..........\r\n123\r\n", "output": "8\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n2311\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n0012\r\n", "output": "0\r\n"}, {"input": "2 7\r\nS.....E\r\n#######\r\n01111111\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n1123\r\n", "output": "0\r\n"}, {"input": "2 3\r\nS.E\r\n...\r\n0111\r\n", "output": "0\r\n"}, {"input": "2 50\r\n.................................................E\r\nS.................................................\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 2\r\n..\r\n..\r\n..\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "3 3\r\nE..\r\n.S.\r\n...\r\n001123110023221103\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS#\r\nE#\r\n012\r\n", "output": "6\r\n"}, {"input": "2 2\r\nES\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n102\r\n", "output": "8\r\n"}, {"input": "3 2\r\nE#\r\n##\r\nS#\r\n0112\r\n", "output": "0\r\n"}]
false
stdio
null
true
908/B
908
B
PyPy 3
TESTS
3
124
0
92967537
n,m=[int(n) for n in input().split()] mat=[] arr=[] for i in range(n): s1=input() arr=list(s1) mat.append(arr) arr=[] s=input() count=0 for i in range(n): for j in range(m): if(mat[i][j]=="S"): start=[i,j] if(mat[i][j]=="E"): end=[i,j] r=[[0,1],[0,-1],[-1,0],[1,0]] for i in r: for j in r: if(j==i): continue for k in r: if(k==j or k==i): continue for l in r: if(l==k or l==j or l==i): continue x=start[0] y=start[1] for u in s: if(u=="3"): if(x+l[0]>=n or x+l[0]<0): break x=x+l[0] if(y+l[1]>=m or y+l[1]<0): break y=y+l[1] if(u=="2"): if(x+k[0]>=n or x+k[0]<0): break x=x+k[0] if(y+k[1]>=m or y+k[1]<0): break y=y+k[1] if(u=="1"): if(x+j[0]>=n or x+j[0]<0): break x=x+j[0] if(y+j[1]>=m or y+j[1]<0): break y=y+j[1] if(u=="0"): if(x+i[0]>=n or x+i[0]<0): break x=x+i[0] if(y+i[1]>=m or y+i[1]<0): break y=y+i[1] if( mat[x][y]=="E"): count=count+1 break print(count)
46
46
0
187239113
from itertools import permutations n, m = [int(i) for i in input().split()] field = [] start = None for i in range(n): cur = [] for j, c in enumerate(list(input())): if c == 'S': start = (i, j) cur.append('.') else: cur.append(c) field.append(cur) program = input() dta = permutations([[1, 0], [0, 1], [-1, 0], [0, -1]]) def game(pth): i, j = start try: for c in program: p = pth[int(c)] i += p[0] j += p[1] if i < 0 or i >= n or j < 0 or j >= m: return False if field[i][j] == 'E': return True elif field[i][j] == '#': return False except IndexError: pass return False res = 0 for paths in dta: if game(paths): res += 1 print(res)
Good Bye 2017
CF
2,017
1
256
New Year and Buggy Bot
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. The robot can only move up, left, right, or down. When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.
The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
null
For the first sample, the only valid mapping is $$0 \rightarrow D, 1 \rightarrow L, 2 \rightarrow U, 3 \rightarrow R$$, where D is down, L is left, U is up, R is right.
[{"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012", "output": "1"}, {"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021", "output": "14"}, {"input": "5 3\n...\n.S.\n###\n.E.\n...\n3", "output": "0"}]
1,200
["brute force", "implementation"]
46
[{"input": "5 6\r\n.....#\r\nS....#\r\n.#....\r\n.#....\r\n...E..\r\n333300012\r\n", "output": "1\r\n"}, {"input": "6 6\r\n......\r\n......\r\n..SE..\r\n......\r\n......\r\n......\r\n01232123212302123021\r\n", "output": "14\r\n"}, {"input": "5 3\r\n...\r\n.S.\r\n###\r\n.E.\r\n...\r\n3\r\n", "output": "0\r\n"}, {"input": "10 10\r\n.#......#.\r\n#.........\r\n#.........\r\n....#.#..E\r\n.......#..\r\n....##....\r\n....S.....\r\n....#.....\r\n.........#\r\n...##...#.\r\n23323332313123221123020122221313323310313122323233\r\n", "output": "0\r\n"}, {"input": "8 9\r\n.........\r\n.........\r\n.........\r\n.E.#.....\r\n.........\r\n.........\r\n...#.S...\r\n.........\r\n10001100111000010121100000110110110100000100000100\r\n", "output": "2\r\n"}, {"input": "15 13\r\n.............\r\n.............\r\n.............\r\n.........#...\r\n..#..........\r\n.............\r\n..........E..\r\n.............\r\n.............\r\n.#...........\r\n.....#.......\r\n..........#..\r\n..........S..\r\n.............\r\n.........#...\r\n32222221111222312132110100022020202131222103103330\r\n", "output": "2\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n..SE.\r\n.....\r\n.....\r\n012330213120031231022103231013201032301223011230102320130231321012030321213002133201130201322031\r\n", "output": "24\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n23\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n03\r\n", "output": "4\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n22\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n11\r\n", "output": "6\r\n"}, {"input": "2 2\r\n#E\r\nS.\r\n01\r\n", "output": "2\r\n"}, {"input": "10 10\r\n####S.####\r\n#####.####\r\n#####.####\r\n#####.####\r\n#####..###\r\n######.###\r\n######.###\r\n######.E##\r\n##########\r\n##########\r\n0111101110\r\n", "output": "2\r\n"}, {"input": "10 10\r\n#####..E##\r\n#####.S.##\r\n#####...##\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n20\r\n", "output": "4\r\n"}, {"input": "10 10\r\n#####ES.##\r\n######.###\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n3\r\n", "output": "6\r\n"}, {"input": "2 10\r\nS........E\r\n..........\r\n33333333333333333\r\n", "output": "6\r\n"}, {"input": "2 2\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n##\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n012\r\n", "output": "8\r\n"}, {"input": "2 3\r\nS.E\r\n###\r\n1222\r\n", "output": "0\r\n"}, {"input": "2 5\r\nS...E\r\n.....\r\n133330\r\n", "output": "1\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n.S.E.\r\n.....\r\n.....\r\n001111\r\n", "output": "6\r\n"}, {"input": "3 5\r\n....S\r\n....#\r\n....E\r\n0112\r\n", "output": "1\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n123\r\n", "output": "8\r\n"}, {"input": "2 10\r\n........ES\r\n..........\r\n123\r\n", "output": "8\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n2311\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n0012\r\n", "output": "0\r\n"}, {"input": "2 7\r\nS.....E\r\n#######\r\n01111111\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n1123\r\n", "output": "0\r\n"}, {"input": "2 3\r\nS.E\r\n...\r\n0111\r\n", "output": "0\r\n"}, {"input": "2 50\r\n.................................................E\r\nS.................................................\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 2\r\n..\r\n..\r\n..\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "3 3\r\nE..\r\n.S.\r\n...\r\n001123110023221103\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS#\r\nE#\r\n012\r\n", "output": "6\r\n"}, {"input": "2 2\r\nES\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n102\r\n", "output": "8\r\n"}, {"input": "3 2\r\nE#\r\n##\r\nS#\r\n0112\r\n", "output": "0\r\n"}]
false
stdio
null
true
10/D
10
D
PyPy 3
TESTS
16
140
1,228,800
94942367
def lcis(a, b): f = [[float("Inf"), float("Inf")] for i in range(min(len(a), len(b))+1)] f[0][1] = -1 q = 0 for i in range(len(a)): left, right = 1, q k = 0 while left <= right: mid = int((left + right)/2) if f[mid][0] < a[i]: k = mid left = mid+1 else: right = mid-1 flag = False for j in range(f[k][1]+1, len(b)): if b[j] == a[i]: flag = True f[k+1][1] = min(f[k+1][1], j) break if flag: f[k+1][0] = min(f[k+1][0], a[i]) q = max(q, k+1) seq = [] m = q while m >= 1: seq.append(f[m][0]) m -= 1 return q, seq[::-1] n = int(input()) a = input().split(' ') a = [int(x) for x in a] m = int(input()) b = input().split(' ') b = [int(x) for x in b] x, y = lcis(a, b) print(x) y = [str(x) for x in y] print(' '.join(y))
60
186
0
11075489
__author__ = 'Darren' # dp(i,j): length of the LCIS between f[0:i+1] and s[0:j+1] that ends at s[j] # if f[i] == s[j]: dp(i,j) = max_len + 1 # else: dp(i,j) = dp(i-1,j) def solve(): n = int(input()) first = list(map(int, input().split())) m = int(input()) second = list(map(int, input().split())) if m > n: n, m = m, n first, second = second, first # dp[j]: LCIS ending at second[j] # prev[j]: index of the second-to-last number for the LCIS ending at second[j] dp, prev = [0] * m, [-1] * m for i in range(n): # max_len: length of the LCIS whose largest number is smaller than first[i] max_len, last_index = 0, -1 for j in range(m): if first[i] == second[j] and dp[j] < max_len + 1: dp[j] = max_len + 1 prev[j] = last_index elif first[i] > second[j] and max_len < dp[j]: max_len = dp[j] last_index = j # Find the length of LCIS between the first and second sequences, and # the index of the last common number in the second sequence max_value, index_max = 0, -1 for index, value in enumerate(dp): if value > max_value: max_value = value index_max = index print(max_value) if max_value > 0: seq = [] index = index_max while index >= 0: seq.append(str(second[index])) index = prev[index] seq.reverse() print(' '.join(seq)) if __name__ == '__main__': solve()
Codeforces Beta Round 10
ICPC
2,010
1
256
LCIS
This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.
In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
null
null
[{"input": "7\n2 3 1 6 5 4 6\n4\n1 3 5 6", "output": "3\n3 5 6"}, {"input": "5\n1 2 0 2 1\n3\n1 0 1", "output": "2\n0 1"}]
2,800
["dp"]
61
[{"input": "7\r\n2 3 1 6 5 4 6\r\n4\r\n1 3 5 6\r\n", "output": "3\r\n3 5 6 \r\n"}, {"input": "5\r\n1 2 0 2 1\r\n3\r\n1 0 1\r\n", "output": "2\r\n0 1 \r\n"}, {"input": "2\r\n6 10\r\n3\r\n6 3 3\r\n", "output": "1\r\n6 \r\n"}, {"input": "1\r\n7\r\n2\r\n7 9\r\n", "output": "1\r\n7 \r\n"}, {"input": "3\r\n37 49 24\r\n3\r\n33 5 70\r\n", "output": "0\r\n\r\n"}, {"input": "10\r\n7 10 1 2 1 7 1 5 9 9\r\n9\r\n6 2 5 6 7 7 5 5 2\r\n", "output": "2\r\n2 7 \r\n"}, {"input": "9\r\n7 0 1 2 6 0 10 3 5\r\n4\r\n8 4 0 3\r\n", "output": "2\r\n0 3 \r\n"}, {"input": "9\r\n7 4 4 5 0 6 5 4 10\r\n4\r\n5 2 10 9\r\n", "output": "2\r\n5 10 \r\n"}, {"input": "8\r\n7 8 6 6 8 10 3 3\r\n5\r\n7 4 10 8 7\r\n", "output": "2\r\n7 8 \r\n"}, {"input": "7\r\n4 2 4 3 10 3 6\r\n9\r\n7 5 2 3 0 1 6 1 4\r\n", "output": "3\r\n2 3 6 \r\n"}, {"input": "1\r\n7\r\n10\r\n1 8 8 10 9 10 4 6 0 5\r\n", "output": "0\r\n\r\n"}, {"input": "2\r\n5 2\r\n4\r\n8 8 0 4\r\n", "output": "0\r\n\r\n"}, {"input": "3\r\n1 3 9\r\n10\r\n8 0 10 5 7 0 3 1 2 4\r\n", "output": "1\r\n1 \r\n"}, {"input": "15\r\n10 4 7 7 10 1 4 9 1 10 9 6 8 8 2\r\n2\r\n0 4\r\n", "output": "1\r\n4 \r\n"}, {"input": "16\r\n8 8 6 7 10 0 10 1 7 6 6 0 4 2 6 7\r\n12\r\n9 3 8 4 10 3 9 8 3 7 10 4\r\n", "output": "2\r\n8 10 \r\n"}, {"input": "23\r\n174 172 196 135 91 174 208 92 132 53 202 118 5 244 161 140 71 21 185 56 60 195 217\r\n17\r\n38 218 120 77 22 214 164 194 79 195 36 167 42 89 201 80 11\r\n", "output": "1\r\n195 \r\n"}, {"input": "53\r\n135 168 160 123 6 250 251 158 245 184 206 35 189 64 138 12 69 21 112 198 165 211 109 40 192 98 236 216 255 98 136 38 67 79 25 196 216 64 134 124 102 232 229 102 179 138 111 123 2 93 25 162 57\r\n57\r\n64 143 41 144 73 26 11 17 224 209 167 162 129 39 102 224 254 45 120 2 138 213 139 133 169 54 7 143 242 118 155 189 100 185 145 168 248 131 83 216 142 180 225 35 226 202 8 15 200 192 75 140 191 189 75 116 202\r\n", "output": "3\r\n64 138 192 \r\n"}, {"input": "83\r\n95 164 123 111 177 71 38 225 103 59 210 209 117 139 115 140 66 21 39 84 14 227 0 43 90 233 96 98 232 237 108 139 55 220 14 225 134 39 68 167 193 125 86 216 87 14 94 75 255 24 165 98 177 191 239 123 98 90 29 52 155 231 187 90 180 1 31 237 167 145 242 115 61 190 47 41 61 206 191 248 126 196 26\r\n49\r\n234 134 9 207 37 95 116 239 105 197 191 15 151 249 156 235 17 161 197 199 87 78 191 188 44 151 179 238 72 29 228 157 174 99 190 114 95 185 160 168 58 216 131 151 233 204 213 87 76\r\n", "output": "2\r\n95 233 \r\n"}, {"input": "13\r\n55 160 86 99 92 148 81 36 216 191 214 127 44\r\n85\r\n92 12 64 21 221 225 119 243 147 47 244 112 212 237 209 121 81 239 43 104 3 254 52 13 1 210 28 18 199 75 251 146 77 28 253 211 50 35 42 160 157 104 155 37 241 78 42 190 150 228 193 96 190 178 232 65 231 186 1 123 212 126 239 22 214 186 245 249 66 234 57 78 173 229 185 23 240 91 127 177 240 105 77 208 86\r\n", "output": "2\r\n160 214 \r\n"}, {"input": "94\r\n100 161 99 102 209 51 5 188 217 53 121 5 233 55 25 156 136 195 243 157 110 202 136 151 86 171 253 38 126 40 27 76 60 119 222 52 134 104 184 146 133 220 88 108 246 61 215 184 181 134 223 164 41 193 232 217 38 192 226 91 81 99 204 232 178 4 187 61 160 255 121 142 191 114 114 181 226 49 86 55 252 169 59 190 246 93 21 22 17 18 120 88 93 144\r\n30\r\n61 51 176 38 119 33 100 185 103 84 161 166 103 227 43 200 127 53 52 89 19 215 76 254 110 30 239 247 11 182\r\n", "output": "3\r\n51 53 110 \r\n"}, {"input": "6\r\n3 5 4 6 8 1\r\n10\r\n3 3 0 5 4 0 10 5 6 8\r\n", "output": "4\r\n3 5 6 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "8\r\n2 3 4 5 6 8 9 5\r\n9\r\n2 2 3 4 5 6 6 8 9\r\n", "output": "7\r\n2 3 4 5 6 8 9 \r\n"}, {"input": "8\r\n0 4 10 6 7 2 8 5\r\n7\r\n0 4 6 7 7 0 8\r\n", "output": "5\r\n0 4 6 7 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "17\r\n12 17 39 156 100 177 188 129 14 142 45 144 243 151 158 194 245\r\n16\r\n125 12 17 199 65 39 100 185 129 194 142 144 62 92 158 194\r\n", "output": "9\r\n12 17 39 100 129 142 144 158 194 \r\n"}, {"input": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n", "output": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207 \r\n"}, {"input": "13\r\n0 46 104 116 63 118 158 16 221 222 136 245 223\r\n9\r\n0 46 104 116 118 158 221 222 245\r\n", "output": "9\r\n0 46 104 116 118 158 221 222 245 \r\n"}, {"input": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235\r\n15\r\n34 38 51 57 73 125 147 158 160 178 255 67 188 198 235\r\n", "output": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235 \r\n"}, {"input": "17\r\n25 29 37 207 122 189 118 42 54 95 154 160 162 225 228 237 248\r\n19\r\n25 29 248 37 147 209 42 54 255 95 154 160 162 225 228 237 73 248 10\r\n", "output": "13\r\n25 29 37 42 54 95 154 160 162 225 228 237 248 \r\n"}, {"input": "10\r\n62914292 123971042 784965687 324817892 379711365 394545872 813282270 822333477 865397146 437913515\r\n9\r\n297835672 62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146\r\n", "output": "8\r\n62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146 \r\n"}, {"input": "10\r\n130077811 57466561 335978192 71385678 434259735 454136111 482887469 530031703 688581885 809880630\r\n8\r\n373627898 57466561 71385678 434259735 454136111 482887469 530031703 809880630\r\n", "output": "7\r\n57466561 71385678 434259735 454136111 482887469 530031703 809880630 \r\n"}, {"input": "17\r\n364396044 90653796 82853043 311258337 326557522 362475139 415783272 428510002 840021181 469284863 541444887 650535473 649258258 750028895 791368777 808443140 959785237\r\n13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237\r\n", "output": "13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237 \r\n"}, {"input": "3\r\n6379263 55134355 76061584\r\n3\r\n6379263 55134355 76061584\r\n", "output": "3\r\n6379263 55134355 76061584 \r\n"}, {"input": "3\r\n48875076 71023491 76538219\r\n3\r\n48875076 71023491 76538219\r\n", "output": "3\r\n48875076 71023491 76538219 \r\n"}, {"input": "5\r\n6621317 78540394 52064998 89150480 53659440\r\n3\r\n78540394 46008538 839195\r\n", "output": "1\r\n78540394 \r\n"}, {"input": "2\r\n34665180 51128665\r\n5\r\n71074966 34665180 47089728 44119345 51128665\r\n", "output": "2\r\n34665180 51128665 \r\n"}, {"input": "4\r\n3 4 9 1\r\n7\r\n5 3 8 9 10 2 1\r\n", "output": "2\r\n3 9 \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 data with open(input_path, 'r') as f: input_lines = [line.strip() for line in f] n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) m = int(input_lines[2]) b = list(map(int, input_lines[3].split())) # Read reference output to get k_ref with open(output_path, 'r') as f: ref_lines = [line.strip() for line in f if line.strip()] if not ref_lines: print(0) return k_ref = int(ref_lines[0]) # Read submission output with open(submission_path, 'r') as f: sub_lines = [line.strip() for line in f if line.strip()] if len(sub_lines) < 1: print(0) return try: k_sub = int(sub_lines[0]) except: print(0) return if k_sub != k_ref: print(0) return if len(sub_lines) < 2: s_sub = [] else: try: s_sub = list(map(int, sub_lines[1].split())) except: print(0) return if len(s_sub) != k_sub: print(0) return # Check strictly increasing for i in range(len(s_sub)-1): if s_sub[i] >= s_sub[i+1]: print(0) return # Check subsequence in a and b def is_sub(s, main): ptr = 0 l = len(s) if l ==0: return True for num in main: if num == s[ptr]: ptr +=1 if ptr == l: break return ptr == l if not is_sub(s_sub, a): print(0) return if not is_sub(s_sub, b): print(0) return # All checks passed print(1) if __name__ == "__main__": main()
true
79/C
79
C
Python 3
TESTS
5
154
5,324,800
21602690
import functools def begs_ends(words, text): l = set() for i in range(len(text)): for word in words: if word == text[i:i + len(word)]: l.add((i, i + len(word) - 1)) return {e[0]: e[1] for e in l} def max_interval(possible, l): i = 0 interval = (-1, -1) print(possible) while i < l: beg = i while i < l and i in possible: i += 1 if i != beg: interval = max(interval, (i - beg, beg)) i += 1 return interval if __name__ == '__main__': text = input() n = int(input()) words = [input() for _ in range(n)] beg_end_map = begs_ends(words, text) @functools.lru_cache(maxsize=None) def longuest_starting(i): if i == len(text): return 0 elif not i in beg_end_map: return 1 + longuest_starting(i + 1) else: return min(1 + longuest_starting(i + 1), beg_end_map[i] - i) i_max = max(range(len(text), -1, -1), key=lambda i: longuest_starting(i)) print(longuest_starting(i_max), i_max if longuest_starting(i_max) else 0)
80
436
716,800
5639831
s, n = input(), int(input()) t = [input() for i in range(n)] def f(i): global t for j in range(n): if i < j: if len(t[j]) < len(t[i]) and t[j] in t[i]: return False elif j < i and t[j] in t[i]: return False return True t = [t[i] for i in range(n) if f(i)] n = len(s) r = [0] * n for p in t: i, m = -1, len(p) - 1 while True: i = s.find(p, i + 1) if i == -1: break r[i] += 1 r[i + m] += 2 d, j, q = 0, -1, [-1] for i in range(n): if r[i] & 1: q.append(i) if r[i] & 2: j = q.pop(0) if i - j > d: d, k = i - j, j j = q.pop(0) if n - j > d: d, k = n - j, j print(d - 1, k + 1)
Codeforces Beta Round 71
CF
2,011
2
256
Beaver
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response.
In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive.
Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any.
null
In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
[{"input": "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse", "output": "12 4"}, {"input": "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd", "output": "0 0"}, {"input": "unagioisii\n2\nioi\nunagi", "output": "5 5"}]
1,800
["data structures", "dp", "greedy", "hashing", "strings", "two pointers"]
80
[{"input": "Go_straight_along_this_street\r\n5\r\nstr\r\nlong\r\ntree\r\nbiginteger\r\nellipse\r\n", "output": "12 4\r\n"}, {"input": "IhaveNoIdea\r\n9\r\nI\r\nh\r\na\r\nv\r\ne\r\nN\r\no\r\nI\r\nd\r\n", "output": "0 0\r\n"}, {"input": "unagioisii\r\n2\r\nioi\r\nunagi\r\n", "output": "5 5\r\n"}, {"input": "abcabcabcabc\r\n3\r\nabcabca\r\nbcab\r\ncabc\r\n", "output": "4 6\r\n"}, {"input": "ThankYouForParticipatingRound71\r\n6\r\nForP\r\noun\r\nnkYouForP\r\nTha\r\nouForP\r\nound7\r\n", "output": "18 9\r\n"}, {"input": "WishYourSolutionPassesFinalTests\r\n10\r\nrSoluti\r\ninal\r\nolutionPas\r\nassesFin\r\nourSo\r\nonP\r\nWishYourSo\r\nsFinalTes\r\nhYourSolut\r\nonPas\r\n", "output": "9 15\r\n"}, {"input": "9\r\n1\r\n9\r\n", "output": "0 0\r\n"}, {"input": "Z\r\n1\r\na\r\n", "output": "1 0\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\nabcd\r\nabc\r\nab\r\na\r\nklmn\r\nlmn\r\nmn\r\nn\r\nop\r\n12\r\n", "output": "12 3\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\na\r\nop\r\nab\r\nabc\r\nabcd\r\nn\r\nmn\r\n12\r\nlmn\r\nklmn\r\n", "output": "12 3\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\nlmn\r\nabc\r\na\r\nklmn\r\nn\r\nabcd\r\nop\r\nmn\r\nab\r\n12\r\n", "output": "12 3\r\n"}, {"input": "x5x5\r\n10\r\nx5\r\nx5x\r\nx5x5\r\nx5\r\nx5\r\nQyBa0yZO_c\r\n_troGKxtGQ\r\nRItOLVC3ok\r\niD_57rFYR1\r\nvZhgXwTnE4\r\n", "output": "2 1\r\n"}, {"input": "aaaay\r\n10\r\naa\r\naa\r\naaa\r\nay\r\naaay\r\ndltfBoU4nF\r\nYhImrXw62Y\r\nwswb4v7CRb\r\npjxxEE3S26\r\nwxDxW1MRaI\r\n", "output": "1 4\r\n"}, {"input": "iiiiii\r\n10\r\nii\r\niiii\r\niiiii\r\niii\r\niiii\r\n5avjcwIsDh\r\nGgiVQ9ylRz\r\newWmNAJAL9\r\nk83baq5H2U\r\nXRX3fJ4dH8\r\n", "output": "1 5\r\n"}, {"input": "ffBBfBB\r\n10\r\nBBfB\r\nffBBfB\r\nffBBf\r\nffBBf\r\nBfB\r\n1ehoxM6CU8\r\ntvB4q05iuU\r\nEYGkY6VxQO\r\nbKbE2ajjBW\r\nhqTKbQUZds\r\n", "output": "4 1\r\n"}, {"input": "aaUUUUaa\r\n10\r\naUUU\r\naaU\r\naUU\r\nUUUU\r\nUU\r\neV1R6e2tCQ\r\nmwseAsylFZ\r\njkfXnCVagR\r\nRGPm9O09J8\r\nVBRpGUFrDB\r\n", "output": "3 5\r\n"}, {"input": "1111e1e1e\r\n10\r\n11\r\n1111e\r\n1111\r\ne1e1e\r\n1e1\r\npuCrQxcVPh\r\nfnbEroh3w4\r\nyie11ihNIi\r\n2_23hx3yX9\r\noPKXwByQLT\r\n", "output": "3 6\r\n"}, {"input": "aMaMaMMaaM\r\n10\r\nMMaaM\r\nMMaaM\r\naa\r\naMaMMaa\r\nMaMa\r\nWoEVf7UuGQ\r\n2q8cm0_WfI\r\nZR63WSVBlC\r\ndtO9nmkXsc\r\ntHL2amBqOS\r\n", "output": "6 2\r\n"}, {"input": "NNNIIIIIINN\r\n10\r\nNNIIIIIIN\r\nIIIINN\r\nIIIINN\r\nNNIIIII\r\nNNII\r\nlHJxS_HfkO\r\nPsss2tjeao\r\nY4Adt_8FXb\r\nkc9mq2vWmZ\r\nVJYusUjoTq\r\n", "output": "8 2\r\n"}, {"input": "cCCcCCCcCccc\r\n10\r\ncC\r\nCCcCCCcCc\r\nCCcC\r\ncCc\r\ncCCC\r\npUKBhDtW8W\r\n0ap4vlgGej\r\nk059r28XMJ\r\nySQTZIOz3r\r\nFHn5rVpM8G\r\n", "output": "4 8\r\n"}, {"input": "7hh77hhhhhh7h\r\n10\r\nhhhh7\r\nhh77hhhhh\r\n7hhhhh\r\nhh7\r\nh77hhhhhh7\r\nC_t1uIxLWp\r\nHwH5EkVGCt\r\nyUgWASGwfR\r\nRfah5Fa12E\r\nS9CmGvTVKM\r\n", "output": "7 2\r\n"}, {"input": "3cc3\r\n10\r\ncc\r\nc3\r\n3c\r\ncc\r\ncc\r\n3cc3\r\n3cc3\r\n3cc\r\nc3\r\njLnOy3kI3M\r\n", "output": "1 3\r\n"}, {"input": "70aa70a\r\n10\r\n70\r\n0aa70\r\n0aa70\r\naa70\r\n70aa\r\n70aa\r\n70aa70a\r\naa7\r\n70aa7\r\nicHuodu1Ux\r\n", "output": "3 1\r\n"}, {"input": "YUYEYEUUEU\r\n10\r\nYEYEUUEU\r\nEUUE\r\nUU\r\nYEYEUUE\r\nYEYE\r\nUU\r\nEY\r\nEYEUU\r\nYEYEUU\r\niBXoTbQ7X3\r\n", "output": "4 0\r\n"}, {"input": "wwwwDwwwwD\r\n10\r\nwD\r\nDwww\r\nwD\r\nDwwww\r\nwwwwD\r\nDww\r\nwD\r\nwwDww\r\nwwww\r\nwwww\r\n", "output": "3 6\r\n"}, {"input": "4tg4ttgg47t44tg4ttgg47t4\r\n10\r\n47t44tg4tt\r\nttgg4\r\ng4ttgg47t4\r\ng47t44t\r\ntg4ttg\r\n44tg4ttg\r\nttgg47\r\nt44tg\r\ng47t44tg\r\n4ttgg47t4\r\n", "output": "8 5\r\n"}, {"input": "g0sg00AAA0Asggss0sAg0sg00AAA0Asggss0sA\r\n10\r\nAg0sg00AAA\r\nAsggss\r\nsAg\r\nAsggss0s\r\nggss0sAg\r\nsAg0sg\r\nggss0sA\r\n0sg00AA\r\nAAA0A\r\nAA0\r\n", "output": "8 18\r\n"}, {"input": "000gooo0g0vgovvv0oggv0v0go000gooo0g0vgovvv0oggv0v0go\r\n10\r\ngv0v0go\r\n0gooo0g\r\ngooo0g0v\r\no000\r\ngooo0g0v\r\nv0go000go\r\ngo000gooo0\r\nv0v0go00\r\nvv\r\nggv0v0\r\n", "output": "10 30\r\n"}, {"input": "B2KR\r\n10\r\nB2\r\n2KR\r\nB2KR\r\n2K\r\n2KR\r\nKR\r\n2KR\r\nB2KR\r\n2K\r\n2KR\r\n", "output": "1 3\r\n"}, {"input": "dxY_8\r\n10\r\nxY_8\r\ndxY\r\ndx\r\nY_\r\nxY_\r\ndx\r\nxY\r\ndx\r\nxY_8\r\ndxY\r\n", "output": "2 3\r\n"}, {"input": "umi4qX\r\n10\r\nqX\r\num\r\n4qX\r\nqX\r\numi4qX\r\numi4\r\numi4\r\numi4q\r\nmi4q\r\numi4q\r\n", "output": "3 2\r\n"}, {"input": "4stuNRe\r\n10\r\n4stu\r\nstuN\r\nstuNRe\r\n4stu\r\ntuNRe\r\nst\r\ntuNR\r\n4stuN\r\ntuN\r\n4stu\r\n", "output": "4 3\r\n"}, {"input": "bTnstTqbTnstTq\r\n10\r\nbTnstTq\r\nnstTqbT\r\nTqbT\r\nbTns\r\nTqbT\r\nTns\r\nTnstT\r\nTnstTqb\r\nnstT\r\nstT\r\n", "output": "4 6\r\n"}, {"input": "Oq02utSVOq02utSV\r\n10\r\n2utSVOq\r\n2utSVO\r\n02utS\r\nSVOq0\r\nut\r\nOq\r\nOq\r\nq02utSVO\r\nOq02utSV\r\nVOq0\r\n", "output": "4 9\r\n"}, {"input": "DpGGt6_wGLPDpGGt6_wGLP\r\n10\r\n6_wGL\r\nGLPDpGG\r\nt6_wG\r\nPDpGGt6_\r\nwGLPDpGGt6\r\n6_\r\n_wGL\r\npGGt6_\r\n6_wGLPD\r\n6_wGLPDpG\r\n", "output": "8 9\r\n"}, {"input": "7QTfE4ALvzkzzD4j7QTfE4ALvzkzzD4j\r\n10\r\nvzkzzD4j7\r\nE4AL\r\nTfE4ALv\r\nzzD4j7QT\r\nzkzzD4j7\r\n4AL\r\nj7Q\r\nE4ALvzkzzD\r\nvzkzzD4j\r\n4ALvzk\r\n", "output": "9 22\r\n"}, {"input": "6CLznedj88834gqTIhMTPjm_3DbkQpuYkBvU3o55h79ntRqjHTOu3WWNNGLyQSZ_o45mK5mMtRJmWUq2twqWP10OlnDAB1EP2rG\r\n10\r\n834gqTI\r\n_o4\r\nvU3o55h79n\r\nvLwlk3PY7Z\r\nk8PnkBpRYB\r\nqdkB9b_SS2\r\nkY4mBeAdgK\r\nb577cjQiSx\r\nyOFiEkP1sD\r\noYOqd8uuTt\r\n", "output": "35 64\r\n"}, {"input": "JroK3tfp941zeUovVIqCV7Sv2elf6TN8QRl8mhxQWgoQRXOLkcUjK29Ed2gBDR1TLdZeLUi9zJyBayrNlWgW0iPSG26DuJ9QK95\r\n10\r\nroK\r\novVI\r\nLUi9\r\nLUi\r\nTLd\r\nelf6\r\n9zJyBa\r\nDR1\r\ndZe\r\n8mhxQWgo\r\n", "output": "27 72\r\n"}, {"input": "p7B41t9y83IghcJG8zgWzCSATkkvQQhvqm7j_4ffUPbriGW57mbGPzqiuMEmJevfIhX2FNLFBxAGm3vLXRPOdphoWkCquDjAsW9\r\n10\r\n1t9y83\r\nBxAGm3v\r\nG8z\r\nCSATk\r\ncJG\r\nIhX2FNLF\r\nzqiuME\r\np7B4\r\nkkvQ\r\nPbriGW57m\r\n", "output": "26 73\r\n"}, {"input": "r5oW1NW2kr193KwV_eASuj9Jq0q0Kmt2EUD1R72p2nPuxNpIqi7o_KuxldYjLJmiXj93JUxRTniFzKkXxHwXDZCzC76klFnfjAQ\r\n10\r\np2nPuxN\r\n93J\r\nNpIqi7o_K\r\niXj93JUxRT\r\n1NW2kr19\r\n0Kmt2EUD1\r\nTniFzKkXxH\r\niXj93JU\r\nNW2kr19\r\n3KwV_\r\n", "output": "26 73\r\n"}, {"input": "hb\r\n1\r\nAa\r\n", "output": "2 0\r\n"}, {"input": "unagioisiiqqqqqqqqqqqq\r\n2\r\nioi\r\nunagi\r\n", "output": "17 5\r\n"}, {"input": "abababab\r\n3\r\nab\r\nba\r\na\r\n", "output": "1 7\r\n"}, {"input": "abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n2\r\nabcd\r\nabcd\r\n", "output": "50 1\r\n"}, {"input": "abababababababababababababababababababababababababababababab\r\n1\r\na\r\n", "output": "1 59\r\n"}, {"input": "abc\r\n2\r\na\r\nb\r\n", "output": "1 2\r\n"}, {"input": "abcde\r\n1\r\nf\r\n", "output": "5 0\r\n"}, {"input": "ahmed\r\n5\r\nahmed\r\nhmed\r\nmed\r\ned\r\nd\r\n", "output": "4 0\r\n"}, {"input": "abc\r\n1\r\nb\r\n", "output": "1 2\r\n"}]
false
stdio
import sys def main(): input_path, output_path, submission_path = sys.argv[1:] # Read input with open(input_path, 'r') as f: s = f.readline().strip() n = int(f.readline()) forbidden = [f.readline().strip() for _ in range(n)] # Read submission output try: with open(submission_path, 'r') as f: line = f.readline().strip() parts = line.split() if len(parts) != 2: print(0) return len_sub = int(parts[0]) pos_sub = int(parts[1]) except: print(0) return # Check pos_sub and len_sub are valid if len_sub < 0 or pos_sub < 0: print(0) return s_len = len(s) if len_sub == 0: if pos_sub < 0 or pos_sub > s_len: print(0) return else: if pos_sub + len_sub > s_len: print(0) return # Check submission substring is valid valid = True sub_start = pos_sub sub_end = pos_sub + len_sub for i in range(sub_start, sub_end): for b in forbidden: m = len(b) if i + m > sub_end: continue if s[i:i+m] == b: valid = False break if not valid: break if not valid: print(0) return # Compute max_len using sliding window max_len = 0 left = 0 for right in range(len(s)): for b in forbidden: m = len(b) start = right - m + 1 if start < 0: continue if start + m > len(s): continue if s[start:start+m] == b: if left <= start: left = start + 1 current_len = right - left + 1 if current_len > max_len: max_len = current_len if len_sub != max_len: print(0) else: print(100) if __name__ == "__main__": main()
true
524/B
524
B
Python 3
TESTS
9
61
0
10450580
n = int(input()) a = list() Hmax = 0 Hmin = 0 for i in range(n): wh = list(map(int, input().split())) Hmax = max(Hmax, max(wh)) if min(wh) > Hmin: Hmin = min(wh) a.append(wh) W1 = 0 W2 = 0 for wh in a: if max(wh) > Hmin: W1 += max(wh) else: W1 += min(wh) if max(wh) > Hmax: W2 += max(wh) else: W2 += min(wh) print(min(W1*Hmin, W2*Hmax))
60
374
5,017,600
10380973
t = [] x = int(input()) for i in range(x): a, b = map(int, input().split(' ')) lx = [a, b] lx.sort() t.append(lx) t.sort() minn = 10**9 for i in range(x): h = t[i][0] w = t[i][1] ok = 1 for j in range(x): if ok == 1 and j != i: if min(t[j]) > h: ok = 0 elif min(t[j])<=h<max(t[j]): w += max(t[j]) else: w += min(t[j]) if ok == 1: minn = min(minn, w*h) for i in t: i.reverse() for i in range(x): h = t[i][0] w = t[i][1] ok = 1 for j in range(x): if ok == 1 and j != i: if min(t[j]) > h: ok = 0 elif min(t[j])<=h<max(t[j]): w += max(t[j]) else: w += min(t[j]) if ok == 1: minn = min(minn, w*h) print(minn) ## m1 = ((cw+a)*max(ch, b)) ## m2 = ((cw+b)*max(ch, a)) ## if m1 < m2: ## cw += a ## ch = max(ch, b) ## else: ## cw += b ## ch = max(ch, a) ## ##print(cw*ch)
VK Cup 2015 - Round 1
CF
2,015
2
256
Фото на память - 2 (round version)
Прошло много лет, и на вечеринке снова встретились n друзей. С момента последней встречи техника шагнула далеко вперёд, появились фотоаппараты с автоспуском, и теперь не требуется, чтобы один из друзей стоял с фотоаппаратом, и, тем самым, оказывался не запечатлённым на снимке. Упрощенно процесс фотографирования можно описать следующим образом. На фотографии каждый из друзей занимает прямоугольник из пикселей: в стоячем положении i-й из них занимает прямоугольник ширины wi пикселей и высоты hi пикселей. Но также, при фотографировании каждый человек может лечь, и тогда он будет занимать прямоугольник ширины hi пикселей и высоты wi пикселей. Общая фотография будет иметь размеры W × H, где W — суммарная ширина всех прямоугольников-людей, а H — максимальная из высот. Друзья хотят определить, какую минимальную площадь может иметь общая фотография. Помогите им в этом.
В первой строке следует целое число n (1 ≤ n ≤ 1000) — количество друзей. В последующих n строках следуют по два целых числа wi, hi (1 ≤ wi, hi ≤ 1000), обозначающие размеры прямоугольника, соответствующего i-му из друзей.
Выведите единственное целое число, равное минимальной возможной площади фотографии, вмещающей всех друзей.
null
null
[{"input": "3\n10 1\n20 2\n30 3", "output": "180"}, {"input": "3\n3 1\n2 2\n4 3", "output": "21"}, {"input": "1\n5 10", "output": "50"}]
1,700
["dp", "greedy"]
60
[{"input": "3\r\n10 1\r\n20 2\r\n30 3\r\n", "output": "180\r\n"}, {"input": "3\r\n3 1\r\n2 2\r\n4 3\r\n", "output": "21\r\n"}, {"input": "1\r\n5 10\r\n", "output": "50\r\n"}, {"input": "10\r\n168 538\r\n836 439\r\n190 873\r\n206 47\r\n891 591\r\n939 481\r\n399 898\r\n859 466\r\n701 777\r\n629 222\r\n", "output": "3478056\r\n"}, {"input": "42\r\n13 62\r\n114 242\r\n265 839\r\n756 349\r\n298 476\r\n533 704\r\n348 277\r\n554 573\r\n654 112\r\n429 836\r\n574 766\r\n909 415\r\n163 891\r\n532 983\r\n873 457\r\n719 117\r\n1 302\r\n170 634\r\n166 585\r\n721 231\r\n208 609\r\n128 246\r\n845 726\r\n604 119\r\n859 110\r\n568 515\r\n656 913\r\n113 166\r\n794 566\r\n834 860\r\n983 596\r\n545 819\r\n167 9\r\n960 73\r\n260 607\r\n173 378\r\n417 938\r\n362 819\r\n800 939\r\n246 834\r\n746 440\r\n42 643\r\n", "output": "13474964\r\n"}, {"input": "3\r\n379 820\r\n923 407\r\n916 853\r\n", "output": "1512797\r\n"}, {"input": "3\r\n99 768\r\n477 885\r\n169 118\r\n", "output": "614190\r\n"}, {"input": "3\r\n227 612\r\n223 259\r\n423 895\r\n", "output": "731790\r\n"}, {"input": "3\r\n651 161\r\n480 32\r\n485 672\r\n", "output": "455616\r\n"}, {"input": "3\r\n779 301\r\n34 214\r\n442 937\r\n", "output": "728049\r\n"}, {"input": "3\r\n203 145\r\n780 692\r\n992 713\r\n", "output": "1366821\r\n"}, {"input": "3\r\n627 286\r\n37 65\r\n53 490\r\n", "output": "235752\r\n"}, {"input": "3\r\n755 938\r\n487 543\r\n307 459\r\n", "output": "1307660\r\n"}, {"input": "3\r\n475 487\r\n41 20\r\n368 236\r\n", "output": "352925\r\n"}, {"input": "3\r\n922 71\r\n719 26\r\n462 700\r\n", "output": "515398\r\n"}, {"input": "2\r\n881 4\r\n788 2\r\n", "output": "5286\r\n"}, {"input": "2\r\n1 304\r\n8 892\r\n", "output": "8028\r\n"}, {"input": "3\r\n227 2\r\n223 9\r\n423 5\r\n", "output": "6768\r\n"}, {"input": "3\r\n7 612\r\n3 259\r\n3 895\r\n", "output": "11635\r\n"}, {"input": "4\r\n573 7\r\n169 9\r\n447 7\r\n947 3\r\n", "output": "19224\r\n"}, {"input": "4\r\n3 817\r\n9 729\r\n7 407\r\n7 433\r\n", "output": "21242\r\n"}, {"input": "10\r\n864 874\r\n534 702\r\n73 363\r\n856 895\r\n827 72\r\n435 468\r\n888 921\r\n814 703\r\n648 715\r\n384 781\r\n", "output": "4909752\r\n"}, {"input": "10\r\n489 685\r\n857 870\r\n736 221\r\n687 697\r\n166 360\r\n265 200\r\n738 519\r\n393 760\r\n66 176\r\n798 160\r\n", "output": "3231747\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1000 1000\r\n", "output": "1000000\r\n"}, {"input": "1\r\n1 1000\r\n", "output": "1000\r\n"}, {"input": "2\r\n1 1000\r\n1000 1\r\n", "output": "2000\r\n"}, {"input": "2\r\n1 1\r\n1000 1000\r\n", "output": "1001000\r\n"}, {"input": "1\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "2\r\n"}, {"input": "3\r\n1 4\r\n1 4\r\n1 1\r\n", "output": "9\r\n"}, {"input": "2\r\n2 1\r\n3 1\r\n", "output": "5\r\n"}, {"input": "2\r\n4 3\r\n2 1\r\n", "output": "15\r\n"}, {"input": "5\r\n78 94\r\n8 53\r\n81 8\r\n41 11\r\n57 57\r\n", "output": "14418\r\n"}, {"input": "8\r\n1 8\r\n1 8\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "22\r\n"}]
false
stdio
null
true
33/C
33
C
PyPy 3
TESTS
4
248
0
51244346
n = int(input()) seq = list(map(int, input().split())) sums = [] s = sum(seq) for i, j in enumerate(seq): if i > 0: sums.append(sums[i-1]+(j*-1)) else: sums.append(s+(j*-1)) if s > max(sums): pass else: ind_left_right = sums.index(max(sums)) for i in range(ind_left_right+1): seq[i] *= -1 sums = [] s = sum(seq) for i, j in enumerate(reversed(seq)): if i > 0: sums.append(sums[i-1]+(j*-1)) else: sums.append(s+(j*-1)) if s > max(sums): pass else: ind_right_left = list(reversed(sums)).index(max(sums)) for i in range(ind_right_left, len(seq)): seq[i] *= -1 print(sum(seq))
89
310
5,836,800
114497323
n=int(input()) ans1=0 ans=0 sum=0 x=map(int,input().split()) for i in x: sum+=i ans1+=i ans1=max(ans1,0) ans=max(ans,ans1) print(2*ans-sum)
Codeforces Beta Round 33 (Codeforces format)
CF
2,010
2
256
Wonderful Randomized Sum
Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?
The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself.
The first and the only line of the output should contain the answer to the problem.
null
null
[{"input": "3\n-1 -2 -3", "output": "6"}, {"input": "5\n-4 2 0 5 0", "output": "11"}, {"input": "5\n-1 10 -5 10 -2", "output": "18"}]
1,800
["greedy"]
89
[{"input": "3\r\n-1 -2 -3\r\n", "output": "6\r\n"}, {"input": "5\r\n-4 2 0 5 0\r\n", "output": "11\r\n"}, {"input": "5\r\n-1 10 -5 10 -2\r\n", "output": "18\r\n"}, {"input": "1\r\n-3\r\n", "output": "3\r\n"}, {"input": "4\r\n1 4 -5 -2\r\n", "output": "12\r\n"}, {"input": "7\r\n-17 6 5 0 1 4 -1\r\n", "output": "34\r\n"}, {"input": "3\r\n0 -2 3\r\n", "output": "5\r\n"}, {"input": "2\r\n0 3\r\n", "output": "3\r\n"}, {"input": "15\r\n14 0 -10 -5 0 19 -6 0 -11 -20 -18 -8 -3 19 -7\r\n", "output": "74\r\n"}, {"input": "15\r\n0 -35 32 24 0 27 10 0 -19 -38 30 -30 40 -3 22\r\n", "output": "130\r\n"}, {"input": "20\r\n0 2 3 1 0 3 -3 0 -1 0 2 -1 -1 3 0 0 1 -3 2 0\r\n", "output": "10\r\n"}, {"input": "100\r\n6 2 -3 6 -4 -6 -2 -1 -6 1 3 -4 -1 0 -3 1 -3 0 -2 -3 0 3 1 6 -5 0 4 -5 -5 -6 3 1 3 4 0 -1 3 -4 5 -1 -3 -2 -6 0 5 -6 -2 0 4 -4 -5 4 -2 0 -5 1 -5 0 5 -4 2 -3 -2 0 3 -6 3 2 -4 -3 5 5 1 -1 2 -6 6 0 2 -3 3 0 -1 -4 0 -6 0 0 -6 5 -4 1 6 -5 -1 -2 3 4 0 6\r\n", "output": "64\r\n"}, {"input": "30\r\n8 -1 3 -7 0 -1 9 3 0 0 3 -8 8 -8 9 -3 5 -9 -8 -10 4 -9 8 6 0 9 -6 1 5 -6\r\n", "output": "41\r\n"}, {"input": "1\r\n7500\r\n", "output": "7500\r\n"}, {"input": "2\r\n9944 -9293\r\n", "output": "19237\r\n"}, {"input": "3\r\n5 -5 7\r\n", "output": "7\r\n"}, {"input": "5\r\n-23 -11 -54 56 -40\r\n", "output": "184\r\n"}, {"input": "10\r\n-8 6 0 12 0 2 3 8 2 6\r\n", "output": "47\r\n"}, {"input": "8\r\n3 0 -5 -2 -4 0 -5 0\r\n", "output": "19\r\n"}, {"input": "16\r\n57 59 -27 24 28 -27 9 -90 3 -36 90 63 1 99 -46 50\r\n", "output": "257\r\n"}, {"input": "7\r\n2 -1 -2 -4 -3 0 -3\r\n", "output": "15\r\n"}, {"input": "8\r\n57 -82 -146 -13 -3 -115 55 -76\r\n", "output": "437\r\n"}, {"input": "6\r\n9721 6032 8572 9026 9563 7626\r\n", "output": "50540\r\n"}, {"input": "4\r\n26 9 -16 -24\r\n", "output": "75\r\n"}, {"input": "5\r\n-54 64 37 -71 -74\r\n", "output": "300\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-83 -87 42 -96\r\n", "output": "308\r\n"}, {"input": "8\r\n103 395 377 -205 -975 301 548 346\r\n", "output": "1500\r\n"}, {"input": "20\r\n18 1 10 0 14 17 -13 0 -20 -19 16 2 5 -2 4 9 1 16 12 4\r\n", "output": "75\r\n"}, {"input": "16\r\n-2 -11 -6 -2 -8 -2 0 3 -1 0 -5 2 -12 5 6 -9\r\n", "output": "64\r\n"}, {"input": "5\r\n81 26 21 28 88\r\n", "output": "244\r\n"}, {"input": "7\r\n2165 -8256 -9741 -9714 7347 5652 6199\r\n", "output": "44744\r\n"}, {"input": "8\r\n4609 9402 908 9322 5132 0 1962 1069\r\n", "output": "32404\r\n"}, {"input": "11\r\n100 233 -184 -200 -222 228 -385 -129 -126 -377 237\r\n", "output": "1491\r\n"}, {"input": "5\r\n-4 -4 -4 -4 -4\r\n", "output": "20\r\n"}, {"input": "5\r\n-7 17 2 -6 -1\r\n", "output": "33\r\n"}, {"input": "8\r\n-1 1 4 -5 -2 3 -10 3\r\n", "output": "17\r\n"}, {"input": "9\r\n1 2 -4 3 6 1 1 2 -8\r\n", "output": "22\r\n"}, {"input": "9\r\n1 1 2 -4 1 -4 2 1 1\r\n", "output": "7\r\n"}, {"input": "14\r\n1 1 1 1 -3 1 -5 -3 2 -3 1 1 1 1\r\n", "output": "11\r\n"}, {"input": "7\r\n-12 12 -12 13 -12 12 -12\r\n", "output": "37\r\n"}, {"input": "1\r\n2\r\n", "output": "2\r\n"}, {"input": "5\r\n-2 0 0 -4 1\r\n", "output": "7\r\n"}, {"input": "13\r\n-2 6 6 0 6 -17 6 5 0 1 4 -1 0\r\n", "output": "22\r\n"}]
false
stdio
null
true
825/C
825
C
Python 3
TESTS
6
62
4,812,800
28614035
import bisect n_k = list(map(int,input().rstrip().split(" "))) n = n_k[0] k = n_k[1] a_i = list(map(int,input().rstrip().split(" "))) max_a = 2*k a_i.sort() pos = bisect.bisect_right(a=a_i,x=max_a) problems_to_be_solved = 0 for i in range(pos,n): present = a_i[pos] if present>max_a: max_a = 2*present problems_to_be_solved+=1 print(problems_to_be_solved)
61
61
4,915,200
28641112
n, mx = [int(i) for i in input().split()] a = [int(i) for i in input().split()] a.sort() cntr = i = 0 while i < n: if mx >= a[i] / 2: mx = max(mx, a[i]) i += 1 else: mx *= 2 cntr += 1 print(cntr)
Educational Codeforces Round 25
ICPC
2,017
1
256
Multi-judge Solving
Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty — a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). Makes has chosen n problems to solve on Decoforces with difficulties a1, a2, ..., an. He can solve these problems in arbitrary order. Though he can solve problem i with difficulty ai only if he had already solved some problem with difficulty $$d \geq \frac{a_i}{2}$$ (no matter on what online judge was it). Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty k. With given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list. For every positive integer y there exist some problem with difficulty y on at least one judge besides Decoforces. Makes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another. Makes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.
The first line contains two integer numbers n, k (1 ≤ n ≤ 103, 1 ≤ k ≤ 109). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.
null
In the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3. In the second example he can solve every problem right from the start.
[{"input": "3 3\n2 1 9", "output": "1"}, {"input": "4 20\n10 3 6 3", "output": "0"}]
1,600
["greedy", "implementation"]
61
[{"input": "3 3\r\n2 1 9\r\n", "output": "1\r\n"}, {"input": "4 20\r\n10 3 6 3\r\n", "output": "0\r\n"}, {"input": "1 1000000000\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1\r\n3\r\n", "output": "1\r\n"}, {"input": "50 100\r\n74 55 33 5 83 24 75 59 30 36 13 4 62 28 96 17 6 35 45 53 33 11 37 93 34 79 61 72 13 31 44 75 7 3 63 46 18 16 44 89 62 25 32 12 38 55 75 56 61 82\r\n", "output": "0\r\n"}, {"input": "100 10\r\n246 286 693 607 87 612 909 312 621 37 801 558 504 914 416 762 187 974 976 123 635 488 416 659 988 998 93 662 92 749 889 78 214 786 735 625 921 372 713 617 975 119 402 411 878 138 548 905 802 762 940 336 529 373 745 835 805 880 816 94 166 114 475 699 974 462 61 337 555 805 968 815 392 746 591 558 740 380 668 29 881 151 387 986 174 923 541 520 998 947 535 651 103 584 664 854 180 852 726 93\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1000000000\r\n", "output": "29\r\n"}, {"input": "29 2\r\n1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767 65535 131071 262143 524287 1048575 2097151 4194303 8388607 16777215 33554431 67108863 134217727 268435455 536870911\r\n", "output": "27\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "29\r\n"}, {"input": "7 6\r\n4 20 16 14 3 17 4\r\n", "output": "1\r\n"}, {"input": "2 1\r\n3 6\r\n", "output": "1\r\n"}, {"input": "1 1\r\n20\r\n", "output": "4\r\n"}, {"input": "5 2\r\n86 81 53 25 18\r\n", "output": "4\r\n"}, {"input": "4 1\r\n88 55 14 39\r\n", "output": "4\r\n"}, {"input": "3 1\r\n2 3 6\r\n", "output": "0\r\n"}, {"input": "3 2\r\n4 9 18\r\n", "output": "1\r\n"}, {"input": "5 3\r\n6 6 6 13 27\r\n", "output": "2\r\n"}, {"input": "5 1\r\n23 8 83 26 18\r\n", "output": "4\r\n"}, {"input": "3 1\r\n4 5 6\r\n", "output": "1\r\n"}, {"input": "3 1\r\n1 3 6\r\n", "output": "1\r\n"}, {"input": "1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "3 2\r\n4 5 6\r\n", "output": "0\r\n"}, {"input": "5 1\r\n100 200 400 1000 2000\r\n", "output": "7\r\n"}, {"input": "2 1\r\n1 4\r\n", "output": "1\r\n"}, {"input": "4 1\r\n2 4 8 32\r\n", "output": "1\r\n"}, {"input": "2 10\r\n21 42\r\n", "output": "1\r\n"}, {"input": "3 3\r\n1 7 13\r\n", "output": "1\r\n"}, {"input": "3 1\r\n1 4 6\r\n", "output": "1\r\n"}, {"input": "2 2\r\n2 8\r\n", "output": "1\r\n"}, {"input": "1 1\r\n4\r\n", "output": "1\r\n"}, {"input": "2 2\r\n8 16\r\n", "output": "1\r\n"}, {"input": "3 1\r\n4 8 16\r\n", "output": "1\r\n"}, {"input": "3 1\r\n3 6 9\r\n", "output": "1\r\n"}, {"input": "2 1\r\n4 8\r\n", "output": "1\r\n"}, {"input": "2 2\r\n7 14\r\n", "output": "1\r\n"}, {"input": "1 4\r\n9\r\n", "output": "1\r\n"}, {"input": "5 3\r\n1024 4096 16384 65536 536870913\r\n", "output": "24\r\n"}, {"input": "2 5\r\n10 11\r\n", "output": "0\r\n"}, {"input": "2 2\r\n3 6\r\n", "output": "0\r\n"}, {"input": "2 2\r\n8 11\r\n", "output": "1\r\n"}, {"input": "3 19905705\r\n263637263 417905394 108361057\r\n", "output": "3\r\n"}, {"input": "4 25\r\n100 11 1 13\r\n", "output": "1\r\n"}, {"input": "10 295206008\r\n67980321 440051990 883040288 135744260 96431758 242465794 576630162 972797687 356406646 547451696\r\n", "output": "0\r\n"}, {"input": "4 2\r\n45 44 35 38\r\n", "output": "4\r\n"}, {"input": "1 2\r\n9\r\n", "output": "2\r\n"}, {"input": "3 6\r\n13 26 52\r\n", "output": "1\r\n"}, {"input": "9 30111088\r\n824713578 11195876 458715185 731769293 680826358 189542586 550198537 860586039 101083021\r\n", "output": "2\r\n"}, {"input": "3 72014068\r\n430005292 807436976 828082746\r\n", "output": "2\r\n"}, {"input": "3 165219745\r\n737649884 652879952 506420386\r\n", "output": "1\r\n"}, {"input": "2 60669400\r\n95037700 337255240\r\n", "output": "1\r\n"}, {"input": "4 28\r\n34 1 86 90\r\n", "output": "1\r\n"}, {"input": "2 1\r\n5 10\r\n", "output": "2\r\n"}, {"input": "2 1\r\n4 1000000000\r\n", "output": "28\r\n"}, {"input": "2 1\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2 1\r\n3 5\r\n", "output": "1\r\n"}, {"input": "3 3\r\n1 5 20\r\n", "output": "1\r\n"}, {"input": "9 1\r\n1 2 4 9 15 32 64 128 1024\r\n", "output": "4\r\n"}]
false
stdio
null
true
847/E
847
E
PyPy 3
TESTS
2
93
0
121278344
''' pacman->food pacman->food->food :hmmge: larges smaller val smallest larger val ''' from bisect import bisect_left as bl def f(s,n): p=[] f=[] for i in range(n): if s[i]=="*": f.append(i) if s[i]=="P": p.append(i) ans=-1 for i in f: l=float("inf") # print("i is ",i) hi=bl(p,i) lo=hi-1 if hi<len(p): l=min(l,p[hi]-i) if lo>=0: l=min(i-p[lo],l) ans=max(ans,l) return ans n=int(input()) s=list(input().strip()) print(f(s,n))
77
264
7,168,000
116313605
from bisect import bisect_right,bisect_left def check(t): pt=0 contain_t=0 pt1,pt2=0,0 contain_t1,contain_t2=0,0 for i in range(l_p): contain_t1,contain_t2=t,t if pt>=l_s: return 1 if ash[pt]<pokemon[i] and t<pokemon[i]-ash[pt]: return 0 if ash[pt]<pokemon[i]: contain_t1=(contain_t1-(pokemon[i]-ash[pt]))//2 contain_t2=max(0,(contain_t2-(pokemon[i]-ash[pt])*2)) pt=bisect_right(ash,pokemon[i]+max(contain_t1,contain_t2)) if pt>=l_s: return 1 return 0 n=int(input()) st=input() a=[] for i in range(n): a.append(st[i]) ans1,ans2=0,0 ash=[] pokemon=[] for i in range(n): if a[i]=="*": ash.append(i) elif a[i]=="P": pokemon.append(i) l_p=len(pokemon) l_s=len(ash) i=0 j=10**10 while i<j: m=(i+j)//2 if check(m): j=m-1 else: i=m+1 if check(i): print(i) else: print(i+1)
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
1
256
Packmen
A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty. Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk. In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
The first line contains a single integer n (2 ≤ n ≤ 105) — the length of the game field. The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i. It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Print minimum possible time after which Packmen can eat all asterisks.
null
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field. In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
[{"input": "7\n*..P*P*", "output": "3"}, {"input": "10\n.**PP.*P.*", "output": "2"}]
1,800
["binary search", "dp"]
77
[{"input": "7\r\n*..P*P*\r\n", "output": "3\r\n"}, {"input": "10\r\n.**PP.*P.*\r\n", "output": "2\r\n"}, {"input": "19\r\n**P.*..*..P..*.*P**\r\n", "output": "7\r\n"}, {"input": "12\r\nP**.*P*P*P**\r\n", "output": "3\r\n"}, {"input": "17\r\n.*P*P**P**.**P...\r\n", "output": "3\r\n"}, {"input": "58\r\n..P.P*.P*.P...PPP...P*....*..*.**......*P.*P.....**P...*P*\r\n", "output": "9\r\n"}, {"input": "10\r\n..P*.P.*.*\r\n", "output": "4\r\n"}, {"input": "10\r\n***.*.*..P\r\n", "output": "9\r\n"}, {"input": "15\r\nP***..PPP..P*.P\r\n", "output": "3\r\n"}, {"input": "15\r\n*.*....*P......\r\n", "output": "8\r\n"}, {"input": "20\r\n.P**P**P**PP.PP**PP*\r\n", "output": "2\r\n"}, {"input": "20\r\n.....*.**..........P\r\n", "output": "14\r\n"}, {"input": "25\r\nP*P*P.**.PPPP.*.P.*..P...\r\n", "output": "2\r\n"}, {"input": "25\r\n...*..**..*.....*..*...P.\r\n", "output": "20\r\n"}, {"input": "30\r\n*P.*...*.**..P**...***.*...**.\r\n", "output": "15\r\n"}, {"input": "30\r\n.*...*.......................P\r\n", "output": "28\r\n"}, {"input": "35\r\n..PP.P....*PP.*.PPPP.*P.P.PPPP.*.P.\r\n", "output": "2\r\n"}, {"input": "35\r\n....*..*.*.*.....*.*..P*...*...*...\r\n", "output": "36\r\n"}, {"input": "40\r\n...**P*P*...P.*PP***.*..P..**.**PP**.*.*\r\n", "output": "6\r\n"}, {"input": "40\r\nP*....*.*....*...*..*.......*...**..***.\r\n", "output": "38\r\n"}, {"input": "45\r\nP.P*..P....*P.*PP*PP*.**P...PP*PP*.P.P..PP.PP\r\n", "output": "2\r\n"}, {"input": "45\r\n*.*.*..*.*.**.*..**..*.....**.**P....*****.**\r\n", "output": "56\r\n"}, {"input": "50\r\n*PP....PPPP*....*P*P..PPPPPP...***P*P.........PP..\r\n", "output": "3\r\n"}, {"input": "50\r\n*..***.*.****.*....P*.**...***.......**....*.***..\r\n", "output": "66\r\n"}, {"input": "55\r\n......P.*.....P*.*P....*..P*.P.P....**....*..........*.\r\n", "output": "22\r\n"}, {"input": "55\r\n*.....*.*..**..*...***..**.**.*.*.P..*.*.**...**.*..*.*\r\n", "output": "74\r\n"}, {"input": "60\r\n.P...P.PPP.P....P...PP.*......**...P.*.P.P*P.*...P*P..P.P..P\r\n", "output": "5\r\n"}, {"input": "60\r\n..*....*...***....P...........*............*....**....*...**\r\n", "output": "73\r\n"}, {"input": "65\r\n......PP..PP**.***.*.P.P..PP.....**PP*PP.....P..P*PP.*.*P..P*P*..\r\n", "output": "5\r\n"}, {"input": "65\r\n.....*..*....*.*......P....*.....**....*.........*...............\r\n", "output": "61\r\n"}, {"input": "70\r\n*P.*..**P*P.....*PP.......*..PP...P...P.**..P.*......*P*.**.P..*P.....\r\n", "output": "4\r\n"}, {"input": "70\r\n*..***.**..**....***........*.**...*...**.**..*.......**P*..*.......**\r\n", "output": "82\r\n"}, {"input": "75\r\n..***P*.**.P.**P.**.***.P*..**P.P*.P*.**.....*PP..P***....**PPP..**P..P..P*\r\n", "output": "6\r\n"}, {"input": "75\r\n...*.*...**.*..*..*P.*......*...*....**..*..**..*..*......*....*..*.*......\r\n", "output": "81\r\n"}, {"input": "80\r\n**.P...*....*P...*....P............**....*..*.*....*..........*.*.*.............\r\n", "output": "44\r\n"}, {"input": "80\r\n*..**..*...*....*.*.**.*.*..*..P..*..**.**..*..**.*.*.*.*.***...*.*..**.*....**.\r\n", "output": "109\r\n"}, {"input": "85\r\n.*.....*.....**..........*P*..........*.........*...*..*...****..*..*P*..*..P.***...*\r\n", "output": "31\r\n"}, {"input": "85\r\n*...*.*.....*..*.*.*.*.**....*...*...*.*..*..*.*......**...*.*.P..*.....*.*....*.*...\r\n", "output": "99\r\n"}, {"input": "90\r\n......P.*.PPP...*.P.**P..*.*.*..*P**PP**..***.PPP....P..**P*.*.*..*.P*P.*PP*.....P.*.**P**\r\n", "output": "5\r\n"}, {"input": "90\r\n.**.*..*........****...*.*..*.*.*...........***..*.*.*.P**...**..*..**..***.....*..*.**...\r\n", "output": "116\r\n"}, {"input": "95\r\n.*..P****....****.*.***P..*.*.**P..*.***.PP.**.**....*****P**..*..*..***...*P..P****.*.P...*..*\r\n", "output": "12\r\n"}, {"input": "95\r\n.*.***...*...P**...*.**.*..*.*..*...****..*.*.*..*.****.*....*...*..*..**.**.********..*.*...**\r\n", "output": "105\r\n"}, {"input": "100\r\n.....PP..*...P.P.PPPP.....P*...P..P.*....*P.*..*..*...........P..PP..P.....*P.....P......*.*..PP....\r\n", "output": "8\r\n"}, {"input": "100\r\n........*P**...*.*.*.*..*.*.**...*.*.....*..............*..**.............*..........*.*..*..*.**...\r\n", "output": "89\r\n"}, {"input": "100\r\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP*PPPPPPPPPPPPP\r\n", "output": "1\r\n"}, {"input": "100\r\n************************************************************P***************************************\r\n", "output": "138\r\n"}, {"input": "100\r\n..........*...............................P..........P.......P.......P..*............P.......**.....\r\n", "output": "32\r\n"}, {"input": "100\r\n.....................P............P....P....*.............*.......**.P.................P........*...\r\n", "output": "11\r\n"}, {"input": "100\r\n......*....*...P*P*.....**.......P.P..........*........*....PP.................P......*.P.P.......*.\r\n", "output": "11\r\n"}, {"input": "100\r\n........................................P..........*...................P.........*.....P.......P....\r\n", "output": "11\r\n"}, {"input": "100\r\n**.**.**.........P*..P**.*.******.***.....***..*.*P.*.***.*.*..*.***..*********.*...***..*..*...P*..\r\n", "output": "26\r\n"}, {"input": "100\r\n.PPP....PPPP.*...P...PPP..*P...P.*P.PP..P.P...PPPPP..PP.P..P..P..P...P.......P..PP..P..PPPPPP.P.PPPP\r\n", "output": "2\r\n"}, {"input": "100\r\n...............................................*P.........*..P.........P.P....P..P.................*\r\n", "output": "18\r\n"}, {"input": "100\r\n...........*.........*.....P...P...............P........P..........P.....*......*.....*P...*.P......\r\n", "output": "16\r\n"}, {"input": "2\r\n*P\r\n", "output": "1\r\n"}, {"input": "2\r\nP*\r\n", "output": "1\r\n"}]
false
stdio
null
true
342/A
342
A
Python 3
TESTS
34
92
1,843,200
9361885
from collections import Counter def main(): input() c = Counter(map(int, input().split())) if c[7] or c[2] < c[4] or c[2] + c[3] != c[1] or c[1] != c[4] + c[6]: print(-1) return res = [None] * c[1] for i in range(c[4]): res[i] = '1 2 4' for i in range(c[4], c[2]): res[i] = '1 2 6' for i in range(c[2], c[1]): res[i] = '1 3 6' print('\n'.join(res)) if __name__ == '__main__': main()
45
46
1,126,400
140416585
grps = int(input())//3 st = input() c1,c2,c3,c4,c6=[st.count(s) for s in '12346'] if c1 != grps or c1 != c2 + c3 or c1 != c4 + c6 or c2 < c4 or c3 > c6: print(-1) exit() else: print('1 2 4\n'*c4 + '1 2 6\n'*(grps - c3 - c4)+'1 3 6\n'*c3)
Codeforces Round 199 (Div. 2)
CF
2,013
1
256
Xenia and Divisors
Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held: - a < b < c; - a divides b, b divides c. Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has $$\frac{\pi}{3}$$ groups of three. Help Xenia, find the required partition or else say that it doesn't exist.
The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7. It is guaranteed that n is divisible by 3.
If the required partition exists, print $$\frac{\pi}{3}$$ groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them. If there is no solution, print -1.
null
null
[{"input": "6\n1 1 1 2 2 2", "output": "-1"}, {"input": "6\n2 2 1 1 4 6", "output": "1 2 4\n1 2 6"}]
1,200
["greedy", "implementation"]
45
[{"input": "6\r\n1 1 1 2 2 2\r\n", "output": "-1\r\n"}, {"input": "6\r\n2 2 1 1 4 6\r\n", "output": "1 2 4\r\n1 2 6\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "-1\r\n"}, {"input": "3\r\n7 5 7\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 3 4\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 3 6 6 3 1 3 1 6\r\n", "output": "1 3 6\r\n1 3 6\r\n1 3 6\r\n"}, {"input": "6\r\n1 2 4 1 3 5\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 3 7\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 2 4 1 2 4 1 3 6\r\n", "output": "1 2 4\r\n1 2 4\r\n1 3 6\r\n"}, {"input": "12\r\n3 6 1 1 3 6 1 1 2 6 2 6\r\n", "output": "1 3 6\r\n1 3 6\r\n1 2 6\r\n1 2 6\r\n"}, {"input": "9\r\n1 1 1 4 4 4 6 2 2\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 2 4 6 3 1 3 1 5\r\n", "output": "-1\r\n"}, {"input": "15\r\n2 1 2 1 3 6 1 2 1 6 1 3 4 6 4\r\n", "output": "1 2 4\r\n1 2 4\r\n1 3 6\r\n1 3 6\r\n1 2 6\r\n"}, {"input": "3\r\n2 3 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n2 4 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n2 5 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n2 4 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n7 7 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 4 7 7 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 2 6 6 6\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 3 3 2 4 4 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 4 5 5 5\r\n", "output": "-1\r\n"}, {"input": "15\r\n1 1 1 1 1 2 2 2 2 4 4 6 6 6 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 5 5 7 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 2 3 4 5 6 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 4 4 7 7\r\n", "output": "-1\r\n"}, {"input": "24\r\n1 1 1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 4 4 4 6 6 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 7 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 2 4 7 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 7 7 7 7 7 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 2 3 4 6 5 5\r\n", "output": "-1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) numbers = list(map(int, f.readline().split())) counts = [0] * 8 for num in numbers: counts[num] += 1 with open(submission_path) as f: submission = f.read().strip() if submission == '-1': has_solution = True if counts[5] > 0 or counts[7] > 0: has_solution = False else: count_1 = counts[1] count_2 = counts[2] count_3 = counts[3] count_4 = counts[4] count_6 = counts[6] if count_1 != count_4 + count_6: has_solution = False elif count_6 < count_3: has_solution = False elif count_2 != (count_4 + (count_6 - count_3)): has_solution = False else: x = count_4 y = count_6 - count_3 z = count_3 if x < 0 or y < 0 or z < 0: has_solution = False else: total_groups = (x + y + z) if total_groups * 3 != n: has_solution = False if not has_solution: print(100) else: print(0) else: tokens = list(map(int, submission.split())) if len(tokens) % 3 != 0 or len(tokens) != n: print(0) return groups = [tokens[i:i+3] for i in range(0, len(tokens), 3)] valid_triplets = {(1, 2, 4), (1, 2, 6), (1, 3, 6)} for group in groups: if len(group) != 3: print(0) return a, b, c = group if not (a < b < c): print(0) return if (a, b, c) not in valid_triplets: print(0) return submission_counts = [0] * 8 for num in tokens: submission_counts[num] += 1 valid = True for i in range(1, 8): if i in [5, 7]: if submission_counts[i] != 0: valid = False break else: if submission_counts[i] != counts[i]: valid = False break print(100 if valid else 0) if __name__ == "__main__": main()
true
186/A
186
A
PyPy 3
TESTS
39
372
29,081,600
87849115
a=input() b=input() l2=[] l22=[] res=0 for i in range(0, min(len(b) , len(a))): if a[i]!=b[i]: res+=1 l2.append(a[i]) l22.append(b[i]) l2.sort() l22.sort() if res==2 and l2==l22: print("YES") else: print("NO")
53
92
204,800
229085358
a1=input() b1=input() a,b,c=0,0,0 if(len(a1)!=len(b1)):print("NO") else: for i in range(len(a1)): if(a1[i]!=b1[i]): a,b,c=b,i,c+1 if(c==2 and a1[a]==b1[b] and a1[b]==b1[a]):print("YES") else:print("NO")
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Comparing Strings
Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters. Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.
The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.
Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".
null
- First example: you can simply swap two letters in string "ab". So we get "ba". - Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".
[{"input": "ab\nba", "output": "YES"}, {"input": "aa\nab", "output": "NO"}]
1,100
["implementation", "strings"]
53
[{"input": "ab\r\nba\r\n", "output": "YES\r\n"}, {"input": "aa\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\nza\r\n", "output": "NO\r\n"}, {"input": "vvea\r\nvvae\r\n", "output": "YES\r\n"}, {"input": "rtfabanpc\r\natfabrnpc\r\n", "output": "YES\r\n"}, {"input": "mt\r\ntm\r\n", "output": "YES\r\n"}, {"input": "qxolmbkkt\r\naovlajmlf\r\n", "output": "NO\r\n"}, {"input": "b\r\ng\r\n", "output": "NO\r\n"}, {"input": "ab\r\naba\r\n", "output": "NO\r\n"}, {"input": "ba\r\na\r\n", "output": "NO\r\n"}, {"input": "a\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\naa\r\n", "output": "NO\r\n"}, {"input": "a\r\nz\r\n", "output": "NO\r\n"}, {"input": "aabb\r\nbbaa\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbd\r\n", "output": "NO\r\n"}, {"input": "bac\r\ndae\r\n", "output": "NO\r\n"}, {"input": "abc\r\nakl\r\n", "output": "NO\r\n"}, {"input": "cb\r\naa\r\n", "output": "NO\r\n"}, {"input": "abaab\r\naabba\r\n", "output": "NO\r\n"}, {"input": "aab\r\naaa\r\n", "output": "NO\r\n"}, {"input": "abcde\r\nedcba\r\n", "output": "NO\r\n"}, {"input": "abab\r\nbaba\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbac\r\n", "output": "NO\r\n"}, {"input": "abcd\r\naqcb\r\n", "output": "NO\r\n"}, {"input": "abc\r\nbad\r\n", "output": "NO\r\n"}, {"input": "ab\r\nca\r\n", "output": "NO\r\n"}, {"input": "abc\r\nab\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbae\r\n", "output": "NO\r\n"}, {"input": "aaaabcccca\r\naaaadccccb\r\n", "output": "NO\r\n"}, {"input": "acaa\r\nabca\r\n", "output": "NO\r\n"}, {"input": "aba\r\naab\r\n", "output": "YES\r\n"}, {"input": "ah\r\nla\r\n", "output": "NO\r\n"}, {"input": "aab\r\naa\r\n", "output": "NO\r\n"}, {"input": "abab\r\nabcd\r\n", "output": "NO\r\n"}, {"input": "ed\r\nab\r\n", "output": "NO\r\n"}]
false
stdio
null
true
567/E
567
E
PyPy 3
TESTS
11
93
0
205455849
import heapq import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def dijkstra(s, G): dist = [inf] * (n + 1) dist[s] = 0 visit = [0] * (n + 1) p = [] heapq.heappush(p, (dist[s], s)) while p: d, u = heapq.heappop(p) if dist[u] < d: continue visit[u] = 1 for v, c in G[u]: nd = dist[u] + c if not visit[v] and nd < dist[v]: dist[v] = nd heapq.heappush(p, (dist[v], v)) return dist def lowlink(): for i in range(1, n + 1): if dist[i] == -1: dfs(i) l = [n + 1] * (n + 1) for i in range(n + 1): l[i] = o[i] for i in range(1, n + 1): for j in B[i]: l[i] = min(l[i], l[j]) x = [[] for _ in range(n + 2)] for i in range(1, n + 1): x[dist[i]].append(i) for i in range(n + 1, 0, -1): for j in x[i]: l[parent[j]] = min(l[parent[j]], l[j]) return l, x def dfs(s): st = [s] dist[s] = 0 o[s] = 0 t = 1 while st: i = st[-1] Gi = G[i] nowei = nowe[i] while nowei < len(Gi): j = Gi[nowei] if dist[j] == -1: dist[j] = dist[i] + 1 st.append(j) o[j] = t t += 1 parent[j] = i break elif parent[j] ^ i: B[j].append(i) nowei += 1 nowe[i]= nowei if nowe[i] == len(Gi): st.pop() return o, parent, dist, B def bridge(): ans = [] for i in range(n + 1): j = parent[i] if o[i] < l[j] or o[j] < l[i]: ans.append((i, j)) return ans n, m, s, t = map(int, input().split()) G1, G2 = [[] for _ in range(n + 1)], [[] for _ in range(n + 1)] e = [tuple(map(int, input().split())) for _ in range(m)] for a, b, l in e: G1[a].append((b, l)) G2[b].append((a, l)) inf = pow(10, 12) + 1 ds, dt = dijkstra(s, G1), dijkstra(t, G2) d = ds[t] ans = ["NO"] * m for i in range(m): a, b, l = e[i] if not ds[a] + dt[a] == ds[b] + dt[b] == ds[a] + dt[b] + l == d: u = d - ds[a] - dt[b] - 1 if u > 0: ans[i] = " ".join(["CAN", str(l - u)]) else: ans[i] = "YES" G = [[] for _ in range(n + 1)] s = set() for i in range(m): if ans[i] == "YES": a, b, l = e[i] if not (a, b) in s and not (b, a) in s: G[a].append(b) G[b].append(a) s.add((a, b)) dist = [-1] * (n + 1) nowe = [0] * (n + 1) o = [-1] * (n + 1) parent = [-1] * (n + 1) B = [[] for _ in range(n + 1)] l, x = lowlink() s = set(bridge()) for i in range(m): if ans[i] == "YES": a, b, l = e[i] if not (a, b) in s and not (b, a) in s: ans[i] = "CAN 1" if l ^ 1 else "NO" sys.stdout.write("\n".join(ans))
109
1,372
111,513,600
231034427
from heapq import * from collections import defaultdict def D(v,g): h,p,o=[],defaultdict(lambda:1e99),[] heappush(h,(0,v)) while h: l,w=heappop(h) if w in p:continue p[w]=l o.append(w) for u,L in g[w]: heappush(h,(L+l,u)) return p,o n,m,s,t=map(int,input().split()) r=[] g={i+1:[] for i in range(n)} G={i+1:[] for i in range(n)} for _ in range(m): a,b,l=map(int,input().split()) r.append((a,b,l)) g[a].append((b,l)) G[b].append((a,l)) S,o=D(s,g) T,_=D(t,G) L=S[t] H={v:[w for w,l in e if S[v]+T[w]+l==L] for v,e in g.items()} B,A=set(),{s} for v in o: if not H[v]:continue if 1==len(A)==len(H[v]):B.add(v) A.update(H[v]) A.remove(v) print('\n'.join("YES" if a in B and S[a]+T[b]+l==L else "CAN "+str(S[a]+T[b]+l-L+1) if S[a]+T[b]+1<L else "NO" for a,b,l in r))
Codeforces Round #Pi (Div. 2)
CF
2,015
2
256
President and Roads
Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year the President visited his historic home town t, for which his motorcade passes along some path from s to t (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from s to t, along which he will travel the fastest. The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.
The first lines contain four integers n, m, s and t (2 ≤ n ≤ 105; 1 ≤ m ≤ 105; 1 ≤ s, t ≤ n) — the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (s ≠ t). Next m lines contain the roads. Each road is given as a group of three integers ai, bi, li (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ li ≤ 106) — the cities that are connected by the i-th road and the time needed to ride along it. The road is directed from city ai to city bi. The cities are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from s to t along the roads.
Print m lines. The i-th line should contain information about the i-th road (the roads are numbered in the order of appearance in the input). If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes). Otherwise, if the i-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing. If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes).
null
The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing. In the first sample president initially may choose one of the two following ways for a ride: 1 → 2 → 4 → 5 → 6 or 1 → 2 → 3 → 5 → 6.
[{"input": "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1", "output": "YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES"}, {"input": "3 3 1 3\n1 2 10\n2 3 10\n1 3 100", "output": "YES\nYES\nCAN 81"}, {"input": "2 2 1 2\n1 2 1\n1 2 2", "output": "YES\nNO"}]
2,200
["dfs and similar", "graphs", "hashing", "shortest paths"]
109
[{"input": "6 7 1 6\r\n1 2 2\r\n1 3 10\r\n2 3 7\r\n2 4 8\r\n3 5 3\r\n4 5 2\r\n5 6 1\r\n", "output": "YES\r\nCAN 2\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nYES\r\n"}, {"input": "3 3 1 3\r\n1 2 10\r\n2 3 10\r\n1 3 100\r\n", "output": "YES\r\nYES\r\nCAN 81\r\n"}, {"input": "2 2 1 2\r\n1 2 1\r\n1 2 2\r\n", "output": "YES\r\nNO\r\n"}, {"input": "2 1 1 2\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "3 3 1 3\r\n1 2 10\r\n2 3 10\r\n1 3 19\r\n", "output": "CAN 2\r\nCAN 2\r\nYES\r\n"}, {"input": "4 3 1 4\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n", "output": "YES\r\nYES\r\nYES\r\n"}, {"input": "4 4 1 4\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n1 3 2\r\n", "output": "NO\r\nNO\r\nYES\r\nCAN 1\r\n"}, {"input": "4 4 1 4\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n1 3 1\r\n", "output": "NO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "6 6 1 6\r\n1 2 2\r\n2 3 4\r\n2 4 3\r\n3 5 2\r\n4 5 3\r\n5 6 10\r\n", "output": "YES\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nYES\r\n"}, {"input": "6 6 1 6\r\n1 2 2\r\n2 3 3\r\n2 4 3\r\n3 5 2\r\n4 5 3\r\n5 6 10\r\n", "output": "YES\r\nYES\r\nCAN 2\r\nYES\r\nCAN 2\r\nYES\r\n"}, {"input": "2 1 1 2\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "2 2 1 2\r\n1 2 6\r\n1 2 6\r\n", "output": "CAN 1\r\nCAN 1\r\n"}, {"input": "2 3 1 2\r\n1 2 7\r\n1 2 7\r\n1 2 7\r\n", "output": "CAN 1\r\nCAN 1\r\nCAN 1\r\n"}, {"input": "2 10 1 2\r\n1 2 5\r\n1 2 5\r\n1 2 7\r\n1 2 5\r\n1 2 6\r\n1 2 5\r\n1 2 5\r\n1 2 6\r\n1 2 5\r\n1 2 6\r\n", "output": "CAN 1\r\nCAN 1\r\nCAN 3\r\nCAN 1\r\nCAN 2\r\nCAN 1\r\nCAN 1\r\nCAN 2\r\nCAN 1\r\nCAN 2\r\n"}, {"input": "3 2 1 2\r\n3 2 3\r\n1 3 6\r\n", "output": "YES\r\nYES\r\n"}, {"input": "3 3 1 3\r\n2 3 7\r\n2 3 7\r\n1 2 6\r\n", "output": "CAN 1\r\nCAN 1\r\nYES\r\n"}, {"input": "3 4 3 1\r\n2 1 4\r\n2 1 2\r\n3 2 1\r\n2 1 2\r\n", "output": "CAN 3\r\nCAN 1\r\nYES\r\nCAN 1\r\n"}, {"input": "3 5 1 2\r\n1 3 3\r\n1 2 9\r\n3 2 6\r\n1 2 10\r\n1 3 3\r\n", "output": "CAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 2\r\nCAN 1\r\n"}, {"input": "3 7 1 3\r\n1 3 11\r\n1 3 12\r\n1 2 2\r\n1 3 11\r\n1 2 2\r\n2 3 9\r\n2 3 9\r\n", "output": "CAN 1\r\nCAN 2\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\n"}, {"input": "5 7 3 2\r\n5 4 8\r\n3 1 2\r\n1 2 20\r\n1 5 8\r\n4 2 4\r\n1 5 8\r\n5 4 9\r\n", "output": "CAN 1\r\nYES\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 2\r\n"}, {"input": "7 8 5 3\r\n4 3 5\r\n7 1 8\r\n2 1 16\r\n2 7 7\r\n2 6 21\r\n5 2 10\r\n6 4 4\r\n1 6 5\r\n", "output": "YES\r\nYES\r\nCAN 2\r\nYES\r\nCAN 2\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "6 8 1 6\r\n1 2 13\r\n3 2 3\r\n4 5 6\r\n1 6 28\r\n1 3 10\r\n1 4 18\r\n2 4 4\r\n5 6 4\r\n", "output": "CAN 1\r\nCAN 1\r\nYES\r\nCAN 2\r\nCAN 1\r\nCAN 2\r\nYES\r\nYES\r\n"}, {"input": "7 10 4 7\r\n6 3 9\r\n2 1 4\r\n3 7 3\r\n5 2 6\r\n1 3 12\r\n5 2 6\r\n4 5 4\r\n4 5 3\r\n1 6 3\r\n4 6 16\r\n", "output": "CAN 1\r\nCAN 1\r\nYES\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nCAN 2\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\n"}, {"input": "10 13 2 10\r\n7 3 5\r\n6 1 10\r\n9 6 4\r\n4 10 48\r\n9 5 2\r\n1 10 3\r\n5 6 2\r\n7 6 19\r\n4 8 8\r\n2 4 8\r\n8 7 7\r\n7 6 20\r\n3 9 10\r\n", "output": "CAN 1\r\nYES\r\nCAN 1\r\nCAN 2\r\nCAN 1\r\nYES\r\nCAN 1\r\nCAN 1\r\nYES\r\nYES\r\nYES\r\nCAN 2\r\nCAN 1\r\n"}, {"input": "4 4 1 4\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n1 4 3\r\n", "output": "NO\r\nNO\r\nNO\r\nCAN 1\r\n"}, {"input": "5 6 1 5\r\n1 2 2\r\n2 5 5\r\n2 3 4\r\n1 4 1\r\n4 3 3\r\n3 5 1\r\n", "output": "NO\r\nCAN 3\r\nCAN 3\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "5 6 1 5\r\n1 2 2\r\n2 5 5\r\n2 3 4\r\n1 4 1\r\n4 3 3\r\n3 5 1\r\n", "output": "NO\r\nCAN 3\r\nCAN 3\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "2 1 1 2\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "3 3 1 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "NO\r\nCAN 1\r\nNO\r\n"}, {"input": "10 10 1 10\r\n1 5 178\r\n1 8 221\r\n2 7 92\r\n2 8 159\r\n3 5 55\r\n3 6 179\r\n3 10 237\r\n4 8 205\r\n5 6 191\r\n8 10 157\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "10 10 1 10\r\n1 4 201\r\n2 3 238\r\n3 4 40\r\n3 6 231\r\n3 8 45\r\n4 5 227\r\n4 6 58\r\n4 9 55\r\n5 7 14\r\n6 10 242\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3 3 1 3\r\n1 2 1\r\n2 3 1\r\n1 3 2\r\n", "output": "NO\r\nNO\r\nCAN 1\r\n"}, {"input": "6 7 1 6\r\n1 2 1000000\r\n2 3 1000000\r\n2 5 1000000\r\n1 3 1000000\r\n3 5 1000000\r\n2 4 1000000\r\n5 6 1000000\r\n", "output": "CAN 1\r\nNO\r\nCAN 1\r\nCAN 1\r\nCAN 1\r\nNO\r\nYES\r\n"}, {"input": "2 1 1 2\r\n1 2 1000000\r\n", "output": "YES\r\n"}, {"input": "2 2 1 2\r\n1 2 1000000\r\n1 2 1000000\r\n", "output": "CAN 1\r\nCAN 1\r\n"}, {"input": "2 2 1 2\r\n1 2 1000000\r\n1 2 1000000\r\n", "output": "CAN 1\r\nCAN 1\r\n"}, {"input": "2 9 1 2\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2 9 1 2\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 2\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n1 2 1000000\r\n", "output": "CAN 999999\r\nCAN 999999\r\nCAN 999999\r\nCAN 999999\r\nYES\r\nCAN 999999\r\nCAN 999999\r\nCAN 999999\r\nCAN 999999\r\n"}, {"input": "3 2 1 3\r\n1 3 1\r\n1 2 1\r\n", "output": "YES\r\nNO\r\n"}, {"input": "4 5 1 4\r\n1 2 1\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n3 4 1\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "3 3 1 3\r\n1 2 666\r\n2 3 555\r\n3 1 1\r\n", "output": "YES\r\nYES\r\nNO\r\n"}]
false
stdio
null
true
33/C
33
C
PyPy 3-64
TESTS
8
92
0
196832629
n=int(input()) a=list(map(int,input().split())) suffix=[[0,0] for i in range(n+1)] for i in range(n-1,-1,-1): if a[i]>=0: suffix[i][0],suffix[i][1]=suffix[i+1][0]+a[i],suffix[i+1][1]+a[i] else: if -a[i]>=suffix[i+1][1]: suffix[i][0],suffix[i][1]=suffix[i+1][0]-suffix[i+1][1]-a[i],0 else: suffix[i][0],suffix[i][1]=suffix[i+1][0]+a[i],suffix[i+1][1]+a[i] ans=suffix[0][0] sum=0 for i in range(n): sum-=a[i] ans=max(ans,sum+suffix[i+1][0]) print(ans)
89
312
30,515,200
126925348
n = int(input()) a = list(map(int,input().split())) s = sum(a) dp = [0]*n dp[0] = a[0] if n > 1: for i in range(1,n): dp[i] = max(dp[i-1]+a[i],a[i]) T = max(dp) if max(a) <= 0: print(-s) else: print(2*T-s)
Codeforces Beta Round 33 (Codeforces format)
CF
2,010
2
256
Wonderful Randomized Sum
Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?
The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself.
The first and the only line of the output should contain the answer to the problem.
null
null
[{"input": "3\n-1 -2 -3", "output": "6"}, {"input": "5\n-4 2 0 5 0", "output": "11"}, {"input": "5\n-1 10 -5 10 -2", "output": "18"}]
1,800
["greedy"]
89
[{"input": "3\r\n-1 -2 -3\r\n", "output": "6\r\n"}, {"input": "5\r\n-4 2 0 5 0\r\n", "output": "11\r\n"}, {"input": "5\r\n-1 10 -5 10 -2\r\n", "output": "18\r\n"}, {"input": "1\r\n-3\r\n", "output": "3\r\n"}, {"input": "4\r\n1 4 -5 -2\r\n", "output": "12\r\n"}, {"input": "7\r\n-17 6 5 0 1 4 -1\r\n", "output": "34\r\n"}, {"input": "3\r\n0 -2 3\r\n", "output": "5\r\n"}, {"input": "2\r\n0 3\r\n", "output": "3\r\n"}, {"input": "15\r\n14 0 -10 -5 0 19 -6 0 -11 -20 -18 -8 -3 19 -7\r\n", "output": "74\r\n"}, {"input": "15\r\n0 -35 32 24 0 27 10 0 -19 -38 30 -30 40 -3 22\r\n", "output": "130\r\n"}, {"input": "20\r\n0 2 3 1 0 3 -3 0 -1 0 2 -1 -1 3 0 0 1 -3 2 0\r\n", "output": "10\r\n"}, {"input": "100\r\n6 2 -3 6 -4 -6 -2 -1 -6 1 3 -4 -1 0 -3 1 -3 0 -2 -3 0 3 1 6 -5 0 4 -5 -5 -6 3 1 3 4 0 -1 3 -4 5 -1 -3 -2 -6 0 5 -6 -2 0 4 -4 -5 4 -2 0 -5 1 -5 0 5 -4 2 -3 -2 0 3 -6 3 2 -4 -3 5 5 1 -1 2 -6 6 0 2 -3 3 0 -1 -4 0 -6 0 0 -6 5 -4 1 6 -5 -1 -2 3 4 0 6\r\n", "output": "64\r\n"}, {"input": "30\r\n8 -1 3 -7 0 -1 9 3 0 0 3 -8 8 -8 9 -3 5 -9 -8 -10 4 -9 8 6 0 9 -6 1 5 -6\r\n", "output": "41\r\n"}, {"input": "1\r\n7500\r\n", "output": "7500\r\n"}, {"input": "2\r\n9944 -9293\r\n", "output": "19237\r\n"}, {"input": "3\r\n5 -5 7\r\n", "output": "7\r\n"}, {"input": "5\r\n-23 -11 -54 56 -40\r\n", "output": "184\r\n"}, {"input": "10\r\n-8 6 0 12 0 2 3 8 2 6\r\n", "output": "47\r\n"}, {"input": "8\r\n3 0 -5 -2 -4 0 -5 0\r\n", "output": "19\r\n"}, {"input": "16\r\n57 59 -27 24 28 -27 9 -90 3 -36 90 63 1 99 -46 50\r\n", "output": "257\r\n"}, {"input": "7\r\n2 -1 -2 -4 -3 0 -3\r\n", "output": "15\r\n"}, {"input": "8\r\n57 -82 -146 -13 -3 -115 55 -76\r\n", "output": "437\r\n"}, {"input": "6\r\n9721 6032 8572 9026 9563 7626\r\n", "output": "50540\r\n"}, {"input": "4\r\n26 9 -16 -24\r\n", "output": "75\r\n"}, {"input": "5\r\n-54 64 37 -71 -74\r\n", "output": "300\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-83 -87 42 -96\r\n", "output": "308\r\n"}, {"input": "8\r\n103 395 377 -205 -975 301 548 346\r\n", "output": "1500\r\n"}, {"input": "20\r\n18 1 10 0 14 17 -13 0 -20 -19 16 2 5 -2 4 9 1 16 12 4\r\n", "output": "75\r\n"}, {"input": "16\r\n-2 -11 -6 -2 -8 -2 0 3 -1 0 -5 2 -12 5 6 -9\r\n", "output": "64\r\n"}, {"input": "5\r\n81 26 21 28 88\r\n", "output": "244\r\n"}, {"input": "7\r\n2165 -8256 -9741 -9714 7347 5652 6199\r\n", "output": "44744\r\n"}, {"input": "8\r\n4609 9402 908 9322 5132 0 1962 1069\r\n", "output": "32404\r\n"}, {"input": "11\r\n100 233 -184 -200 -222 228 -385 -129 -126 -377 237\r\n", "output": "1491\r\n"}, {"input": "5\r\n-4 -4 -4 -4 -4\r\n", "output": "20\r\n"}, {"input": "5\r\n-7 17 2 -6 -1\r\n", "output": "33\r\n"}, {"input": "8\r\n-1 1 4 -5 -2 3 -10 3\r\n", "output": "17\r\n"}, {"input": "9\r\n1 2 -4 3 6 1 1 2 -8\r\n", "output": "22\r\n"}, {"input": "9\r\n1 1 2 -4 1 -4 2 1 1\r\n", "output": "7\r\n"}, {"input": "14\r\n1 1 1 1 -3 1 -5 -3 2 -3 1 1 1 1\r\n", "output": "11\r\n"}, {"input": "7\r\n-12 12 -12 13 -12 12 -12\r\n", "output": "37\r\n"}, {"input": "1\r\n2\r\n", "output": "2\r\n"}, {"input": "5\r\n-2 0 0 -4 1\r\n", "output": "7\r\n"}, {"input": "13\r\n-2 6 6 0 6 -17 6 5 0 1 4 -1 0\r\n", "output": "22\r\n"}]
false
stdio
null
true
225/A
225
A
Python 3
TESTS
9
124
0
104245219
import sys _ = input() x = int(input()) _, _ = map(int, input().strip().split()) p = True prev = 7 - x lines = sys.stdin.readlines() for line in lines: line = line.strip() if not line: break a, b = map(int, line.split()) if prev in [a, 7-a, b, b-7]: p = False break print("YES" if p else "NO")
52
92
0
145019974
all = [1, 2, 3, 4, 5, 6] def m(a, b): return 7 - a, 7 - b n = int(input()) top = int(input()) if n == 1: a = input() print('YES') else: t = n rows = [] while t > 0: t -= 1 rows.append(list(map(int, input().split(" ")))) sides = set(m(rows[0][0], rows[0][1])) sides.add(top) sides.add(rows[0][0]) sides.add(rows[0][1]) bot = set(all).difference(sides) look = False for i in range(1, n): a = rows[i][0] b = rows[i][1] c, d = m(a, b) if top in [a, b, c, d] or bot in [a, b, c, d]: look = True break if look: print('NO') else: print('YES')
Codeforces Round 139 (Div. 2)
CF
2,012
2
256
Dice Tower
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees). Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower. The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower. Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
null
null
[{"input": "3\n6\n3 2\n5 4\n2 4", "output": "YES"}, {"input": "3\n3\n2 6\n4 1\n5 3", "output": "NO"}]
1,100
["constructive algorithms", "greedy"]
52
[{"input": "3\r\n6\r\n3 2\r\n5 4\r\n2 4\r\n", "output": "YES"}, {"input": "3\r\n3\r\n2 6\r\n4 1\r\n5 3\r\n", "output": "NO"}, {"input": "1\r\n3\r\n2 1\r\n", "output": "YES"}, {"input": "2\r\n2\r\n3 1\r\n1 5\r\n", "output": "NO"}, {"input": "3\r\n2\r\n1 4\r\n5 3\r\n6 4\r\n", "output": "NO"}, {"input": "4\r\n3\r\n5 6\r\n1 3\r\n1 5\r\n4 1\r\n", "output": "NO"}, {"input": "2\r\n2\r\n3 1\r\n1 3\r\n", "output": "YES"}, {"input": "3\r\n2\r\n1 4\r\n3 1\r\n4 6\r\n", "output": "YES"}, {"input": "4\r\n3\r\n5 6\r\n1 5\r\n5 1\r\n1 5\r\n", "output": "YES"}, {"input": "5\r\n1\r\n2 3\r\n5 3\r\n5 4\r\n5 1\r\n3 5\r\n", "output": "NO"}, {"input": "10\r\n5\r\n1 3\r\n2 3\r\n6 5\r\n6 5\r\n4 5\r\n1 3\r\n1 2\r\n3 2\r\n4 2\r\n1 2\r\n", "output": "NO"}, {"input": "15\r\n4\r\n2 1\r\n2 4\r\n6 4\r\n5 3\r\n4 1\r\n4 2\r\n6 3\r\n4 5\r\n3 5\r\n2 6\r\n5 6\r\n1 5\r\n3 5\r\n6 4\r\n3 2\r\n", "output": "NO"}, {"input": "20\r\n6\r\n3 2\r\n4 6\r\n3 6\r\n6 4\r\n5 1\r\n1 5\r\n2 6\r\n1 2\r\n1 4\r\n5 3\r\n2 3\r\n6 2\r\n5 4\r\n2 6\r\n1 3\r\n4 6\r\n4 5\r\n6 3\r\n3 1\r\n6 2\r\n", "output": "NO"}, {"input": "25\r\n4\r\n1 2\r\n4 1\r\n3 5\r\n2 1\r\n3 5\r\n6 5\r\n3 5\r\n5 6\r\n1 2\r\n2 4\r\n6 2\r\n2 3\r\n2 4\r\n6 5\r\n2 3\r\n6 3\r\n2 3\r\n1 3\r\n2 1\r\n3 1\r\n5 6\r\n3 1\r\n6 4\r\n3 6\r\n2 3\r\n", "output": "NO"}, {"input": "5\r\n1\r\n2 3\r\n3 5\r\n4 5\r\n5 4\r\n5 3\r\n", "output": "YES"}, {"input": "10\r\n5\r\n1 3\r\n3 1\r\n6 3\r\n6 3\r\n4 6\r\n3 1\r\n1 4\r\n3 1\r\n4 6\r\n1 3\r\n", "output": "YES"}, {"input": "15\r\n4\r\n2 1\r\n2 6\r\n6 5\r\n5 1\r\n1 5\r\n2 1\r\n6 5\r\n5 1\r\n5 1\r\n6 2\r\n6 5\r\n5 1\r\n5 1\r\n6 5\r\n2 6\r\n", "output": "YES"}, {"input": "20\r\n6\r\n3 2\r\n4 2\r\n3 5\r\n4 2\r\n5 3\r\n5 4\r\n2 3\r\n2 3\r\n4 5\r\n3 5\r\n3 2\r\n2 4\r\n4 5\r\n2 4\r\n3 2\r\n4 2\r\n5 4\r\n3 2\r\n3 5\r\n2 4\r\n", "output": "YES"}, {"input": "25\r\n4\r\n1 2\r\n1 5\r\n5 6\r\n1 2\r\n5 1\r\n5 6\r\n5 1\r\n6 5\r\n2 1\r\n2 6\r\n2 6\r\n2 6\r\n2 6\r\n5 6\r\n2 6\r\n6 5\r\n2 1\r\n1 5\r\n1 2\r\n1 2\r\n6 5\r\n1 2\r\n6 5\r\n6 2\r\n2 6\r\n", "output": "YES"}, {"input": "3\r\n6\r\n3 2\r\n5 4\r\n2 6\r\n", "output": "NO"}, {"input": "4\r\n1\r\n2 3\r\n2 3\r\n2 3\r\n1 3\r\n", "output": "NO"}, {"input": "2\r\n6\r\n3 2\r\n6 4\r\n", "output": "NO"}, {"input": "3\r\n6\r\n3 2\r\n5 6\r\n2 4\r\n", "output": "NO"}, {"input": "2\r\n5\r\n6 3\r\n4 5\r\n", "output": "NO"}, {"input": "2\r\n6\r\n3 2\r\n6 5\r\n", "output": "NO"}, {"input": "2\r\n1\r\n3 2\r\n1 2\r\n", "output": "NO"}, {"input": "2\r\n3\r\n5 1\r\n3 5\r\n", "output": "NO"}, {"input": "2\r\n1\r\n2 3\r\n1 2\r\n", "output": "NO"}, {"input": "2\r\n1\r\n2 3\r\n2 1\r\n", "output": "NO"}, {"input": "3\r\n1\r\n4 5\r\n4 1\r\n4 5\r\n", "output": "NO"}, {"input": "2\r\n4\r\n2 6\r\n5 4\r\n", "output": "NO"}, {"input": "2\r\n6\r\n3 2\r\n6 2\r\n", "output": "NO"}, {"input": "2\r\n3\r\n2 1\r\n3 5\r\n", "output": "NO"}, {"input": "2\r\n3\r\n1 2\r\n3 1\r\n", "output": "NO"}, {"input": "2\r\n3\r\n2 6\r\n5 3\r\n", "output": "NO"}, {"input": "3\r\n3\r\n1 2\r\n3 2\r\n3 1\r\n", "output": "NO"}, {"input": "3\r\n5\r\n3 1\r\n1 3\r\n2 3\r\n", "output": "NO"}, {"input": "2\r\n6\r\n2 4\r\n6 5\r\n", "output": "NO"}, {"input": "2\r\n6\r\n4 5\r\n6 5\r\n", "output": "NO"}, {"input": "2\r\n6\r\n3 5\r\n3 6\r\n", "output": "NO"}, {"input": "2\r\n4\r\n1 2\r\n4 5\r\n", "output": "NO"}, {"input": "2\r\n3\r\n2 6\r\n3 1\r\n", "output": "NO"}]
false
stdio
null
true
817/B
817
B
Python 3
TESTS
18
1,372
14,131,200
134817601
from collections import Counter import math def main(): n = int(input()) nums = list(map(int,input().split())) nums.sort() lst = list(set(nums)) lst.sort() counter = Counter(nums) if counter[lst[0]] >= 3: print(comb(counter[lst[0]],3)) elif counter[lst[0]] == 2: print(counter[lst[1]]) elif counter[lst[0]] == 1 and counter[lst[1]] > 1: print(counter[lst[1]]-1) else: print(counter[lst[2]]) # print(counter) def comb(n,r): if n == r: return 1 return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) main()
80
109
8,806,400
27825988
n = int(input()) a = sorted(map(int, input().split())) x, y, z = a[0], a[1], a[2] if x < y < z: print(a.count(z)) elif x < y <= z: c = a.count(y) print(c*(c-1)//2) elif x <= y < z: print(a.count(z)) else: c = a.count(z) print(c*(c-1)*(c-2)//6)
Educational Codeforces Round 23
ICPC
2,017
2
256
Makes And The Product
After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.
null
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4. In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2. In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
[{"input": "4\n1 1 1 1", "output": "4"}, {"input": "5\n1 3 2 3 4", "output": "2"}, {"input": "6\n1 3 3 1 3 2", "output": "1"}]
1,500
["combinatorics", "implementation", "math", "sortings"]
80
[{"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "2\r\n"}, {"input": "6\r\n1 3 3 1 3 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "1\r\n"}, {"input": "11\r\n1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "45\r\n"}, {"input": "5\r\n1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6\r\n1 2 2 3 3 4\r\n", "output": "1\r\n"}, {"input": "8\r\n1 1 2 2 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 3\r\n", "output": "6\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2 2 2 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "4\r\n1 2 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 2 3\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 3 3 3 3\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 2 2 5 6\r\n", "output": "3\r\n"}, {"input": "10\r\n1 2 2 2 2 2 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "3\r\n2 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2 2 3\r\n", "output": "1\r\n"}, {"input": "10\r\n2 2 2 2 2 1 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "7\r\n2 2 2 3 3 3 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 2 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "9\r\n2 2 3 3 3 3 3 3 3\r\n", "output": "7\r\n"}, {"input": "5\r\n1 1 2 2 3\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 3 3\r\n", "output": "2\r\n"}, {"input": "4\r\n33554432 33554432 67108864 33554432\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 2 1 2 2\r\n", "output": "10\r\n"}, {"input": "10\r\n1 2 1 2 3 2 3 2 2 2\r\n", "output": "6\r\n"}, {"input": "10\r\n9 6 4 7 1 8 9 5 9 4\r\n", "output": "1\r\n"}, {"input": "4\r\n5 7 2 7\r\n", "output": "2\r\n"}, {"input": "3\r\n7 6 7\r\n", "output": "1\r\n"}, {"input": "6\r\n3 2 8 2 5 3\r\n", "output": "2\r\n"}, {"input": "3\r\n5 9 5\r\n", "output": "1\r\n"}, {"input": "5\r\n6 3 7 6 3\r\n", "output": "2\r\n"}, {"input": "9\r\n10 10 4 10 7 9 6 7 3\r\n", "output": "1\r\n"}, {"input": "5\r\n9 10 10 3 8\r\n", "output": "1\r\n"}, {"input": "5\r\n2 9 5 10 5\r\n", "output": "1\r\n"}, {"input": "9\r\n7 1 9 6 6 8 3 1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n3 4 4 4 5\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 3\r\n", "output": "1\r\n"}, {"input": "8\r\n3 2 2 5 2 2 1 2\r\n", "output": "10\r\n"}]
false
stdio
null
true
899/E
899
E
Python 3
TESTS
6
187
9,625,600
33469499
def get_int_list(string): ls = [] sum = '' for s in string: if s == ' ': ls.append(int(sum)) sum = '' else: sum += s ls.append(int(sum)) return ls def ans(): prev = ls[0] num = 1 sum = 0 d = {} for i in ls: if i == prev: sum += 1 else: ls2 = [] for j in d: if d[j] >= sum: ls2.append(j) for j in ls2: del d[j] d[prev] = sum num += 1 sum = 1 prev = i if i in d: del d[i] num -= 1 if i in d: del d[i] num -= 1 return num n = input() x = input() ls = get_int_list(x) print(ans())
97
1,466
38,297,600
138133134
import heapq class Node: def __init__(self,idx,freq,val) -> None: self.value=val self.index=idx self.occ=freq self.front=None self.back=None class LinkedList: def __init__(self) -> None: self.head=None self.h=[] self.seen=[False]*200001 heapq.heapify(self.h) self.ans=0 self.len=0 def insert(self,node) -> None: if self.head==None: self.head=node else: h=self.head node.back=h h.front=node self.head=node node.front=None self.len+=1 heapq.heappush(self.h,(-1*node.occ,node.index,node)) def merge(self,l,r): ll=l.back rr=r.front new_node=Node(min(r.index,l.index),l.occ+r.occ,l.value) new_node.front=rr new_node.back=ll self.seen[max(l.index,r.index)]=True if (rr!=None): rr.back=new_node if (ll!=None): ll.front=new_node heapq.heappush(self.h,(-1*new_node.occ,new_node.index,new_node)) return def remove(self,node): self.len-=1 if (self.head==node and self.len==0): self.head=None return l=node.back r=node.front if (l!=None): l.front=r if r!=None: r.back=l if (l!=None and r!=None and l.value==r.value): self.merge(l,r) def next_val(self): if len(self.h)==0: return -1 f,idx,node=heapq.heappop(self.h) if (not self.seen[idx]): self.seen[idx]=True self.remove(node) self.ans+=1 return 1 def main(n,arr): nodes=[] val=arr[0] idx=0 freq=1 for i in range(1,len(arr)): if (arr[i]!=arr[i-1]): node=Node(idx,freq,val) nodes.append(node) val=arr[i] idx+=1 freq=1 else: freq+=1 node=Node(idx,freq,val) nodes.append(node) l=LinkedList() for i in range(len(nodes)-1,-1,-1): l.insert(nodes[i]) s=l.next_val() while (s!=-1): s=l.next_val() return l.ans n=int(input()) arr=list(map(int,input().split())) print(main(n,arr))
Codeforces Round 452 (Div. 2)
CF
2,017
2
256
Segments Removal
Vasya has an array of integers of length n. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2]. Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
null
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array. In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2. In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
[{"input": "4\n2 5 5 2", "output": "2"}, {"input": "5\n6 3 4 1 5", "output": "5"}, {"input": "8\n4 4 4 2 2 100 100 100", "output": "3"}, {"input": "6\n10 10 50 10 50 50", "output": "4"}]
2,000
["data structures", "dsu", "flows", "implementation", "two pointers"]
97
[{"input": "4\r\n2 5 5 2\r\n", "output": "2\r\n"}, {"input": "5\r\n6 3 4 1 5\r\n", "output": "5\r\n"}, {"input": "8\r\n4 4 4 2 2 100 100 100\r\n", "output": "3\r\n"}, {"input": "6\r\n10 10 50 10 50 50\r\n", "output": "4\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "100\r\n45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45\r\n", "output": "1\r\n"}, {"input": "1\r\n100\r\n", "output": "1\r\n"}, {"input": "2\r\n1 100\r\n", "output": "2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n100 100\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1 3\r\n", "output": "2\r\n"}, {"input": "3\r\n1 100 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 5 6\r\n", "output": "3\r\n"}, {"input": "3\r\n10 4 10\r\n", "output": "3\r\n"}, {"input": "3\r\n10 10 4\r\n", "output": "2\r\n"}, {"input": "4\r\n100 4 56 33\r\n", "output": "4\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 1 3\r\n", "output": "2\r\n"}, {"input": "4\r\n5 1 1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n4\r\n", "output": "1\r\n"}, {"input": "2\r\n21 21\r\n", "output": "1\r\n"}, {"input": "3\r\n48 48 14\r\n", "output": "2\r\n"}, {"input": "10\r\n69 69 69 69 69 13 69 7 69 7\r\n", "output": "6\r\n"}, {"input": "20\r\n15 15 71 100 71 71 15 93 15 100 100 71 100 100 100 15 100 100 71 15\r\n", "output": "14\r\n"}, {"input": "31\r\n17 17 17 17 29 17 17 29 91 17 29 17 91 17 29 17 17 17 29 17 17 17 17 17 17 17 17 29 29 17 17\r\n", "output": "12\r\n"}, {"input": "43\r\n40 69 69 77 9 10 58 69 23 9 58 51 10 69 10 89 77 77 9 9 10 9 69 58 40 10 23 10 58 9 9 77 58 9 77 10 58 58 40 77 9 89 40\r\n", "output": "38\r\n"}, {"input": "54\r\n34 75 90 23 47 13 68 37 14 39 48 41 42 100 19 43 68 47 13 47 48 65 45 89 56 86 67 52 87 81 86 63 44 9 89 21 86 89 20 43 43 37 24 43 77 14 43 42 99 92 49 99 27 40\r\n", "output": "53\r\n"}, {"input": "66\r\n79 79 49 49 79 81 79 79 79 79 81 49 49 79 49 49 79 49 49 81 81 49 49 49 81 49 49 49 81 81 79 81 49 81 49 79 81 49 79 79 81 49 79 79 81 49 49 79 79 79 81 79 49 47 49 49 47 81 79 49 79 79 79 49 49 49\r\n", "output": "34\r\n"}, {"input": "80\r\n80 86 39 5 58 20 66 61 32 75 93 20 57 20 20 61 45 17 86 43 31 75 37 80 92 10 6 18 21 8 93 92 11 75 86 39 53 27 45 77 20 20 1 80 66 13 11 51 58 11 31 51 73 93 15 88 6 32 99 6 39 87 6 39 6 80 8 45 46 45 23 53 23 58 24 53 28 46 87 68\r\n", "output": "78\r\n"}, {"input": "100\r\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7\r\n", "output": "3\r\n"}, {"input": "9\r\n1 2 2 2 1 2 2 2 1\r\n", "output": "3\r\n"}, {"input": "12\r\n1 1 1 49 63 63 63 19 38 38 65 27\r\n", "output": "7\r\n"}, {"input": "7\r\n31 31 21 21 13 96 96\r\n", "output": "4\r\n"}, {"input": "3\r\n1000000000 1 1000000000\r\n", "output": "3\r\n"}]
false
stdio
null
true
847/E
847
E
PyPy 3-64
TESTS
2
62
0
166751139
# https://codeforces.com/contest/847/problem/E from collections import deque from bisect import bisect_right n = int(input()) field = input() pacmen = [] pellets = [] for i, c in enumerate(field): if c == 'P': pacmen.append(i) elif c == '*': pellets.append(i) def bs(lo, hi): if lo >= hi: return lo def ok(mid): # print('testing mid =', mid) pacmen_left = deque(pacmen) # print('pacmen =', pacmen_left) pellets_left = pellets[:] while pellets_left: if not pacmen_left: # print('bad') return False leftmost_pacman = pacmen_left.popleft() # print('pellets remaining =', pellets_left) # we need to go back and get a pellet to the left if pellets_left[0] < leftmost_pacman: if leftmost_pacman - mid > pellets_left[0]: # print('bad') return False can_go_forward = leftmost_pacman + max(0, (mid - pellets_left[0]) // 2) # print('can go forward =', can_go_forward) farthest = bisect_right(pellets_left, can_go_forward) # print(f'farthest for pacman at {leftmost_pacman} =', farthest) del pellets_left[:farthest] # print('now pellets =', pellets_left) else: can_go_forward = leftmost_pacman + mid farthest = bisect_right(pellets_left, can_go_forward) # print(f'farthest for pacman at {leftmost_pacman} =', farthest) del pellets_left[:farthest] # print('now pellets =', pellets_left) # print('good') return True mid = (lo + hi) // 2 if not ok(mid): return bs(mid + 1, hi) elif mid > lo and ok(mid - 1): return bs(lo, mid - 1) else: return mid print(bs(0, 2 * n))
77
467
102,400
160908484
n = int(input()) s = input() def check(mid) : left = -1 curr = -1 for i in range(0, n) : if (s[i] == '*' and left == -1 and i > curr) : left = i elif (s[i] == 'P') : if (left == -1) : curr = i + mid elif ((i-mid) > left) : return False elif (left < i) : curr = max(left*2 + mid - i, i + (mid-i+left)/2) else : curr = i + mid if (left <= curr) : left = -1 return left == -1 l = 0 r = 2*n res = 0 while (l <= r) : mid = (l+r)//2 if (check(mid)) : r = mid-1 res = mid else : l = mid + 1 print(res)
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
1
256
Packmen
A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty. Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk. In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
The first line contains a single integer n (2 ≤ n ≤ 105) — the length of the game field. The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i. It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Print minimum possible time after which Packmen can eat all asterisks.
null
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field. In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
[{"input": "7\n*..P*P*", "output": "3"}, {"input": "10\n.**PP.*P.*", "output": "2"}]
1,800
["binary search", "dp"]
77
[{"input": "7\r\n*..P*P*\r\n", "output": "3\r\n"}, {"input": "10\r\n.**PP.*P.*\r\n", "output": "2\r\n"}, {"input": "19\r\n**P.*..*..P..*.*P**\r\n", "output": "7\r\n"}, {"input": "12\r\nP**.*P*P*P**\r\n", "output": "3\r\n"}, {"input": "17\r\n.*P*P**P**.**P...\r\n", "output": "3\r\n"}, {"input": "58\r\n..P.P*.P*.P...PPP...P*....*..*.**......*P.*P.....**P...*P*\r\n", "output": "9\r\n"}, {"input": "10\r\n..P*.P.*.*\r\n", "output": "4\r\n"}, {"input": "10\r\n***.*.*..P\r\n", "output": "9\r\n"}, {"input": "15\r\nP***..PPP..P*.P\r\n", "output": "3\r\n"}, {"input": "15\r\n*.*....*P......\r\n", "output": "8\r\n"}, {"input": "20\r\n.P**P**P**PP.PP**PP*\r\n", "output": "2\r\n"}, {"input": "20\r\n.....*.**..........P\r\n", "output": "14\r\n"}, {"input": "25\r\nP*P*P.**.PPPP.*.P.*..P...\r\n", "output": "2\r\n"}, {"input": "25\r\n...*..**..*.....*..*...P.\r\n", "output": "20\r\n"}, {"input": "30\r\n*P.*...*.**..P**...***.*...**.\r\n", "output": "15\r\n"}, {"input": "30\r\n.*...*.......................P\r\n", "output": "28\r\n"}, {"input": "35\r\n..PP.P....*PP.*.PPPP.*P.P.PPPP.*.P.\r\n", "output": "2\r\n"}, {"input": "35\r\n....*..*.*.*.....*.*..P*...*...*...\r\n", "output": "36\r\n"}, {"input": "40\r\n...**P*P*...P.*PP***.*..P..**.**PP**.*.*\r\n", "output": "6\r\n"}, {"input": "40\r\nP*....*.*....*...*..*.......*...**..***.\r\n", "output": "38\r\n"}, {"input": "45\r\nP.P*..P....*P.*PP*PP*.**P...PP*PP*.P.P..PP.PP\r\n", "output": "2\r\n"}, {"input": "45\r\n*.*.*..*.*.**.*..**..*.....**.**P....*****.**\r\n", "output": "56\r\n"}, {"input": "50\r\n*PP....PPPP*....*P*P..PPPPPP...***P*P.........PP..\r\n", "output": "3\r\n"}, {"input": "50\r\n*..***.*.****.*....P*.**...***.......**....*.***..\r\n", "output": "66\r\n"}, {"input": "55\r\n......P.*.....P*.*P....*..P*.P.P....**....*..........*.\r\n", "output": "22\r\n"}, {"input": "55\r\n*.....*.*..**..*...***..**.**.*.*.P..*.*.**...**.*..*.*\r\n", "output": "74\r\n"}, {"input": "60\r\n.P...P.PPP.P....P...PP.*......**...P.*.P.P*P.*...P*P..P.P..P\r\n", "output": "5\r\n"}, {"input": "60\r\n..*....*...***....P...........*............*....**....*...**\r\n", "output": "73\r\n"}, {"input": "65\r\n......PP..PP**.***.*.P.P..PP.....**PP*PP.....P..P*PP.*.*P..P*P*..\r\n", "output": "5\r\n"}, {"input": "65\r\n.....*..*....*.*......P....*.....**....*.........*...............\r\n", "output": "61\r\n"}, {"input": "70\r\n*P.*..**P*P.....*PP.......*..PP...P...P.**..P.*......*P*.**.P..*P.....\r\n", "output": "4\r\n"}, {"input": "70\r\n*..***.**..**....***........*.**...*...**.**..*.......**P*..*.......**\r\n", "output": "82\r\n"}, {"input": "75\r\n..***P*.**.P.**P.**.***.P*..**P.P*.P*.**.....*PP..P***....**PPP..**P..P..P*\r\n", "output": "6\r\n"}, {"input": "75\r\n...*.*...**.*..*..*P.*......*...*....**..*..**..*..*......*....*..*.*......\r\n", "output": "81\r\n"}, {"input": "80\r\n**.P...*....*P...*....P............**....*..*.*....*..........*.*.*.............\r\n", "output": "44\r\n"}, {"input": "80\r\n*..**..*...*....*.*.**.*.*..*..P..*..**.**..*..**.*.*.*.*.***...*.*..**.*....**.\r\n", "output": "109\r\n"}, {"input": "85\r\n.*.....*.....**..........*P*..........*.........*...*..*...****..*..*P*..*..P.***...*\r\n", "output": "31\r\n"}, {"input": "85\r\n*...*.*.....*..*.*.*.*.**....*...*...*.*..*..*.*......**...*.*.P..*.....*.*....*.*...\r\n", "output": "99\r\n"}, {"input": "90\r\n......P.*.PPP...*.P.**P..*.*.*..*P**PP**..***.PPP....P..**P*.*.*..*.P*P.*PP*.....P.*.**P**\r\n", "output": "5\r\n"}, {"input": "90\r\n.**.*..*........****...*.*..*.*.*...........***..*.*.*.P**...**..*..**..***.....*..*.**...\r\n", "output": "116\r\n"}, {"input": "95\r\n.*..P****....****.*.***P..*.*.**P..*.***.PP.**.**....*****P**..*..*..***...*P..P****.*.P...*..*\r\n", "output": "12\r\n"}, {"input": "95\r\n.*.***...*...P**...*.**.*..*.*..*...****..*.*.*..*.****.*....*...*..*..**.**.********..*.*...**\r\n", "output": "105\r\n"}, {"input": "100\r\n.....PP..*...P.P.PPPP.....P*...P..P.*....*P.*..*..*...........P..PP..P.....*P.....P......*.*..PP....\r\n", "output": "8\r\n"}, {"input": "100\r\n........*P**...*.*.*.*..*.*.**...*.*.....*..............*..**.............*..........*.*..*..*.**...\r\n", "output": "89\r\n"}, {"input": "100\r\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP*PPPPPPPPPPPPP\r\n", "output": "1\r\n"}, {"input": "100\r\n************************************************************P***************************************\r\n", "output": "138\r\n"}, {"input": "100\r\n..........*...............................P..........P.......P.......P..*............P.......**.....\r\n", "output": "32\r\n"}, {"input": "100\r\n.....................P............P....P....*.............*.......**.P.................P........*...\r\n", "output": "11\r\n"}, {"input": "100\r\n......*....*...P*P*.....**.......P.P..........*........*....PP.................P......*.P.P.......*.\r\n", "output": "11\r\n"}, {"input": "100\r\n........................................P..........*...................P.........*.....P.......P....\r\n", "output": "11\r\n"}, {"input": "100\r\n**.**.**.........P*..P**.*.******.***.....***..*.*P.*.***.*.*..*.***..*********.*...***..*..*...P*..\r\n", "output": "26\r\n"}, {"input": "100\r\n.PPP....PPPP.*...P...PPP..*P...P.*P.PP..P.P...PPPPP..PP.P..P..P..P...P.......P..PP..P..PPPPPP.P.PPPP\r\n", "output": "2\r\n"}, {"input": "100\r\n...............................................*P.........*..P.........P.P....P..P.................*\r\n", "output": "18\r\n"}, {"input": "100\r\n...........*.........*.....P...P...............P........P..........P.....*......*.....*P...*.P......\r\n", "output": "16\r\n"}, {"input": "2\r\n*P\r\n", "output": "1\r\n"}, {"input": "2\r\nP*\r\n", "output": "1\r\n"}]
false
stdio
null
true
958/F2
958
F2
Python 3
TESTS
8
93
6,963,200
86268678
n,m=list(map(int,input().split())) arr=list(map(int,input().split())) marr=list(map(int,input().split())) f=[0]*(m+1) cnt=0 valid=sum(marr) #m for i in range(n): f[arr[i]]+=1 if f[arr[i]]<=marr[arr[i]-1]: cnt+=1 if cnt==valid: break ans=(i+1)-valid s,e=0,i while(e<n): while(f[arr[s]]>marr[arr[s]-1]): f[arr[s]]-=1 s+=1 ans=min((e-s+1)-valid,ans) e+=1 if e<n: f[arr[e]]+=1 print(ans)
59
467
45,568,000
37318624
from collections import defaultdict n, m = map(int, input().split()) ns = list(map(int, input().split())) ms = list(map(int, input().split())) summs = sum(ms) target = { i: m for i, m in enumerate(ms, 1) } remain = set(i for i, m in target.items() if m != 0) count = defaultdict(int) a = 0 b = 0 while remain and b < n: count[ns[b]] += 1 if ns[b] in remain and target[ns[b]] <= count[ns[b]]: remain.remove(ns[b]) b += 1 if remain: print(-1) else: ans = b - summs while b <= n: if remain: if b >= n: break count[ns[b]] += 1 if ns[b] in remain and target[ns[b]] <= count[ns[b]]: remain.remove(ns[b]) b += 1 else: count[ns[a]] -= 1 if target[ns[a]] > count[ns[a]]: remain.add(ns[a]) else: ans = min(ans, b - a - 1 - summs) a += 1 print(ans)
Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)
ICPC
2,018
1
256
Lightsabers (medium)
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k1 knights with lightsabers of the first color, k2 knights with lightsabers of the second color, ..., km knights with lightsabers of the m-th color. However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.
The first line of the input contains n (1 ≤ n ≤ 2·105) and m (1 ≤ m ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with $$1 \leq \sum_{i=1}^{m} k_i \leq n$$) – the desired counts of Jedi Knights with lightsabers of each color from 1 to m.
Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1.
null
null
[{"input": "8 3\n3 3 1 2 2 1 1 3\n3 1 1", "output": "1"}]
1,800
["binary search", "two pointers"]
59
[{"input": "8 3\r\n3 3 1 2 2 1 1 3\r\n3 1 1\r\n", "output": "1\r\n"}, {"input": "6 5\r\n1 2 4 2 4 3\r\n0 0 1 0 0\r\n", "output": "0\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1\r\n1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1\r\n1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "2 2\r\n1 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 2\r\n2 2\r\n1 1\r\n", "output": "-1\r\n"}, {"input": "3 3\r\n3 3 2\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "4 4\r\n4 4 4 4\r\n0 1 1 1\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n1 1\r\n1 0\r\n", "output": "0\r\n"}, {"input": "3 3\r\n3 3 3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "4 4\r\n2 4 4 3\r\n0 1 0 0\r\n", "output": "0\r\n"}, {"input": "2 2\r\n2 1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "3 3\r\n3 1 1\r\n1 1 1\r\n", "output": "-1\r\n"}, {"input": "4 4\r\n1 3 1 4\r\n1 0 0 1\r\n", "output": "0\r\n"}, {"input": "2 2\r\n2 1\r\n1 0\r\n", "output": "0\r\n"}, {"input": "3 3\r\n3 1 1\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4 4\r\n4 4 2 2\r\n1 1 1 1\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n1 2\r\n0 2\r\n", "output": "-1\r\n"}, {"input": "3 3\r\n3 2 3\r\n0 2 1\r\n", "output": "-1\r\n"}, {"input": "4 4\r\n1 2 4 2\r\n0 0 1 0\r\n", "output": "-1\r\n"}, {"input": "4 4\r\n4 2 1 2\r\n1 2 0 1\r\n", "output": "0\r\n"}, {"input": "5 5\r\n4 4 2 4 2\r\n0 2 0 3 0\r\n", "output": "0\r\n"}, {"input": "6 6\r\n4 3 5 4 5 2\r\n0 1 0 1 2 0\r\n", "output": "0\r\n"}, {"input": "4 4\r\n4 3 3 2\r\n0 0 2 0\r\n", "output": "0\r\n"}, {"input": "5 5\r\n3 4 5 1 4\r\n1 0 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 6\r\n1 1 3 2 2 2\r\n1 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4 4\r\n4 1 1 3\r\n2 0 0 1\r\n", "output": "0\r\n"}, {"input": "5 5\r\n3 4 1 1 5\r\n2 0 1 1 0\r\n", "output": "0\r\n"}, {"input": "6 6\r\n4 3 5 6 5 5\r\n0 0 1 1 0 0\r\n", "output": "0\r\n"}, {"input": "4 4\r\n1 3 4 2\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5 5\r\n4 1 3 3 3\r\n0 0 0 1 0\r\n", "output": "0\r\n"}, {"input": "6 6\r\n6 2 6 2 5 4\r\n0 1 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4 4\r\n3 2 1 3\r\n0 1 0 0\r\n", "output": "0\r\n"}, {"input": "5 5\r\n3 4 1 4 2\r\n1 0 0 1 0\r\n", "output": "0\r\n"}, {"input": "6 6\r\n4 1 6 6 3 5\r\n1 0 1 1 1 2\r\n", "output": "0\r\n"}]
false
stdio
null
true
186/A
186
A
Python 3
TESTS
39
92
3,993,600
206972331
import sys input = sys.stdin.readline ############ ---- Input Functions ---- ############ 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())) ############ ---- Input Functions ---- ############ def Comparing_Strings(): string1 = insr() string2 = insr() string1_unmatched = [] string2_unmatched = [] for s1_char,s2_char in zip(string1,string2): if s1_char != s2_char: string1_unmatched.append(s1_char) string2_unmatched.append(s2_char) if len(string1_unmatched) != 2: print("NO") else: if string1_unmatched[0] == string2_unmatched[1] and string1_unmatched[1] == string2_unmatched[0]: print("YES") else: print("NO") return Comparing_Strings()
53
92
307,200
211024964
gnom1 = input() gnom2 = input() if len(gnom2) != len(gnom1): print('NO') else: s1 = '' s2 = '' for i in range(len(gnom1)): if gnom1[i] != gnom2[i]: s1 = s1 + gnom1[i] s2 = s2 + gnom2[i] if all([len(s1) == len(s2), len(s1) == 2]): if s1[1] + s1[0] == s2: print('YES') else: print('NO') else: print('NO')
Codeforces Round 118 (Div. 2)
CF
2,012
2
256
Comparing Strings
Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters. Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.
The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.
Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".
null
- First example: you can simply swap two letters in string "ab". So we get "ba". - Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".
[{"input": "ab\nba", "output": "YES"}, {"input": "aa\nab", "output": "NO"}]
1,100
["implementation", "strings"]
53
[{"input": "ab\r\nba\r\n", "output": "YES\r\n"}, {"input": "aa\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\nza\r\n", "output": "NO\r\n"}, {"input": "vvea\r\nvvae\r\n", "output": "YES\r\n"}, {"input": "rtfabanpc\r\natfabrnpc\r\n", "output": "YES\r\n"}, {"input": "mt\r\ntm\r\n", "output": "YES\r\n"}, {"input": "qxolmbkkt\r\naovlajmlf\r\n", "output": "NO\r\n"}, {"input": "b\r\ng\r\n", "output": "NO\r\n"}, {"input": "ab\r\naba\r\n", "output": "NO\r\n"}, {"input": "ba\r\na\r\n", "output": "NO\r\n"}, {"input": "a\r\nab\r\n", "output": "NO\r\n"}, {"input": "a\r\naa\r\n", "output": "NO\r\n"}, {"input": "a\r\nz\r\n", "output": "NO\r\n"}, {"input": "aabb\r\nbbaa\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbd\r\n", "output": "NO\r\n"}, {"input": "bac\r\ndae\r\n", "output": "NO\r\n"}, {"input": "abc\r\nakl\r\n", "output": "NO\r\n"}, {"input": "cb\r\naa\r\n", "output": "NO\r\n"}, {"input": "abaab\r\naabba\r\n", "output": "NO\r\n"}, {"input": "aab\r\naaa\r\n", "output": "NO\r\n"}, {"input": "abcde\r\nedcba\r\n", "output": "NO\r\n"}, {"input": "abab\r\nbaba\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbac\r\n", "output": "NO\r\n"}, {"input": "abcd\r\naqcb\r\n", "output": "NO\r\n"}, {"input": "abc\r\nbad\r\n", "output": "NO\r\n"}, {"input": "ab\r\nca\r\n", "output": "NO\r\n"}, {"input": "abc\r\nab\r\n", "output": "NO\r\n"}, {"input": "ab\r\nbae\r\n", "output": "NO\r\n"}, {"input": "aaaabcccca\r\naaaadccccb\r\n", "output": "NO\r\n"}, {"input": "acaa\r\nabca\r\n", "output": "NO\r\n"}, {"input": "aba\r\naab\r\n", "output": "YES\r\n"}, {"input": "ah\r\nla\r\n", "output": "NO\r\n"}, {"input": "aab\r\naa\r\n", "output": "NO\r\n"}, {"input": "abab\r\nabcd\r\n", "output": "NO\r\n"}, {"input": "ed\r\nab\r\n", "output": "NO\r\n"}]
false
stdio
null
true
731/B
731
B
PyPy 3
TESTS
2
62
0
149481122
n = int(input()) a = list(map(int, input().split())) for i in range(0,len(a)): if a[i] != 0: if a[i]%2 == 0: a[i] = 2 else: a[i] = 1 printed = False for i in range(0,len(a)-1): if a[i] != 0: if a[i] == a[i+1]: a[i] = 0 a[i+1] = 0 elif a[i] > a[i+1]: if a[i] == 1: print("NO") printed = True break else: a[i] = 0 else: a[i] = 0 a[i+1] = 1 if not printed: print("YES")
79
140
10,854,400
116624573
n=int(input()) arr = map(int,input().split()) last = 0 for c in arr: if last==0: last = c%2 elif last==1: if c==0: print("NO") exit() else: last = (c-1)&1 if last==1: print("NO") else: print("YES")
Codeforces Round 376 (Div. 2)
CF
2,016
1
256
Coupons and Discounts
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition. Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day. There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total). As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days. Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.
The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).
null
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.
[{"input": "4\n1 2 1 2", "output": "YES"}, {"input": "3\n1 0 1", "output": "NO"}]
1,100
["constructive algorithms", "greedy"]
79
[{"input": "4\r\n1 2 1 2\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 0 2\r\n", "output": "YES\r\n"}, {"input": "1\r\n179\r\n", "output": "NO\r\n"}, {"input": "10\r\n0 0 5 9 9 3 0 0 0 10\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 3\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "2\r\n0 0\r\n", "output": "YES\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "1\r\n2\r\n", "output": "YES\r\n"}, {"input": "1\r\n3\r\n", "output": "NO\r\n"}, {"input": "1\r\n10000\r\n", "output": "YES\r\n"}, {"input": "2\r\n10000 10000\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "YES\r\n"}, {"input": "10\r\n3 3 3 2 2 2 2 2 2 3\r\n", "output": "YES\r\n"}, {"input": "100\r\n2 3 2 3 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 2 3 2 3 2 2 3 3 3 3 3 2 2 2 2 3 2 3 3 2 2 3 2 3 3 3 3 2 2 3 3 3 3 3 2 3 3 3 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 3 3 3 2 2 2 2 3 2 2 3 3 3 2 2 2 2 2 3 3\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n1 0 1 1 0 1 1 0 1 0\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n8 4 0 0 6 1 9 8 0 6\r\n", "output": "YES\r\n"}, {"input": "100\r\n44 0 0 0 16 0 0 0 0 77 9 0 94 0 78 0 0 50 55 35 0 35 88 27 0 0 86 0 0 56 0 0 17 23 0 22 54 36 0 0 94 36 0 22 0 0 0 0 0 0 0 82 0 0 50 0 6 0 0 44 80 0 0 0 98 0 0 0 0 92 0 56 0 16 0 14 0 37 89 0 62 3 83 0 0 0 80 0 92 58 92 0 0 0 57 79 0 0 0 42\r\n", "output": "YES\r\n"}, {"input": "100\r\n37 92 14 95 3 37 0 0 0 84 27 33 0 0 0 74 74 0 35 72 46 29 8 92 1 76 47 0 38 82 0 81 54 7 61 46 91 0 86 0 80 0 0 98 88 0 4 0 0 52 0 0 82 0 33 35 0 36 58 52 1 50 29 0 0 24 0 69 97 65 13 0 30 0 14 66 47 94 22 24 8 92 67 0 34 0 0 0 84 85 50 33 0 99 67 73 21 0 0 62\r\n", "output": "YES\r\n"}, {"input": "100\r\n56 22 13 79 28 73 16 55 34 0 97 19 22 36 22 80 30 19 36 92 9 38 24 10 61 43 19 12 18 34 21 36 1 17 0 97 72 37 74 70 51 34 33 87 27 33 45 97 38 56 2 32 88 92 64 51 74 94 86 98 57 62 83 3 87 61 9 65 57 13 64 10 50 35 7 75 41 3 70 66 6 55 69 42 91 75 14 22 68 93 2 53 22 98 45 2 78 58 18 13\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 1 1 0 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 0 0\r\n", "output": "NO\r\n"}, {"input": "9\r\n6 3 5 9 0 3 1 9 6\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 5\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 2\r\n", "output": "NO\r\n"}]
false
stdio
null
true
216/B
216
B
Python 3
TESTS
23
124
5,632,000
34192357
vertexes, edges = map(int, input().split()) graph = [[] for _ in range(vertexes + 1)] In = (vertexes + 1) * [0] for _ in range(edges): from_, to_ = map(int, input().split()) graph[from_].append(to_) graph[to_].append(from_) res = 0 visited = (vertexes + 1) * [False] for i in range(1, graph.__len__()): if graph[i] and not visited[i]: if len(graph[i]) == 1: visited[i] = True visited[graph[i][0]] = True continue else: one, two = graph[i] visited[one] = True visited[two] = True if one in graph[two]: res += 1 res += 0 if not ((vertexes - res) % 2) else 1 print(res)
56
92
0
187459063
def adj_list(nodes,edges): graph=[[] for i in range(nodes)] for edge in range(edges): a, b = map(int, input().split()) graph[a-1].append(b-1) graph[b-1].append(a-1) return graph nodes, edges = map(int, input().split()) graph=adj_list(nodes,edges) visited=[False for i in range(nodes)] def dfs(cur, par, arr): arr.add(cur) if visited[cur]: return True visited[cur] = True for i in graph[cur]: if i != par: if dfs(i, cur, arr): return True return False i = 0 res = 0 while i < nodes: if not visited[i]: arr = set() if dfs(i, -1, arr): if len(arr) % 2 == 1: res += 1 i += 1 nodes -= res if nodes % 2 == 1: print(res + 1) else: print(res)
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
626/E
626
E
PyPy 3-64
TESTS
12
187
26,112,000
227207146
n = int(input()) a = sorted(list(map(int, input().split()))) p = [0] for i in range(n): p.append(p[-1] + a[i]) maxSkewness = 0 for i in range(n): maxMean = median = a[i] l = 0 r = min(i, n - i - 1) while l <= r: m = (l + r) // 2 s = (p[i] - p[i - m]) + a[i] + (p[n] - p[n - m]) mean = s / (2 * m + 1) if mean >= maxMean: l = m + 1 else: r = m - 1 if maxMean <= mean: maxMean = mean element = i distance = m skewness = maxMean - median if maxSkewness <= skewness: maxSkewness = skewness x = element y = distance res = a[x - y : x + 1] + a[n - y :] print(len(res)) print(*res)
94
311
27,033,600
221423938
def fans(p, q): tans = sum[n] - sum[n - q] + sum[p + 1] - sum[p - q] tans -= x[p] * (2 * q + 1) return tans n = int(input()) x = list(map(int, input().split())) x.sort() sum = [0] * (n + 1) ans = -1 ansp = 0 ansq = 0 for i in range(n): sum[i + 1] = sum[i] + x[i] for i in range(n): k = 0 for j in range(20, -1, -1): k += 1 << j if k > i or k > n - i - 1 or fans(i, k) * (2 * k - 1) < fans(i, k - 1) * (2 * k + 1): k -= 1 << j if fans(i, k) * (2 * ansq + 1) > ans * (2 * k + 1): ans = fans(i, k) ansp = i ansq = k print(2 * ansq + 1) for i in range(ansp - ansq, ansp + 1): print(x[i], end=" ") for i in range(n - ansq, n): print(x[i], end=" ") print()# 1693558701.658143
8VC Venture Cup 2016 - Elimination Round
CF
2,016
3
256
Simple Skewness
Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in the list. The second line contains n integers xi (0 ≤ xi ≤ 1 000 000) — the ith element of the list.
In the first line, print a single integer k — the size of the subset. In the second line, print k integers — the elements of the subset in any order. If there are multiple optimal subsets, print any.
null
In the first case, the optimal subset is $${ \{ 1, 2, 1 2 \} }$$, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is $${ \{ 1, 1, 2 \} }$$. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0.
[{"input": "4\n1 2 3 12", "output": "3\n1 2 12"}, {"input": "4\n1 1 2 2", "output": "3\n1 1 2"}, {"input": "2\n1 2", "output": "2\n1 2"}]
2,400
["binary search", "math", "ternary search"]
94
[{"input": "4\r\n1 2 3 12\r\n", "output": "3\r\n1 2 12 \r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "3\r\n1 1 2 \r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 2\r\n"}, {"input": "1\r\n1000000\r\n", "output": "1\r\n1000000 \r\n"}, {"input": "20\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712\r\n", "output": "1\r\n475712 \r\n"}, {"input": "21\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 1000000\r\n", "output": "1\r\n475712 \r\n"}, {"input": "40\r\n999999 999999 999998 999998 999996 999996 999992 999992 999984 999984 999968 999968 999936 999936 999872 999872 999744 999744 999488 999488 998976 998976 997952 997952 995904 995904 991808 991808 983616 983616 967232 967232 934464 934464 868928 868928 737856 737856 475712 0\r\n", "output": "3\r\n737856 737856 999999 \r\n"}, {"input": "1\r\n534166\r\n", "output": "1\r\n534166 \r\n"}, {"input": "1\r\n412237\r\n", "output": "1\r\n412237 \r\n"}, {"input": "1\r\n253309\r\n", "output": "1\r\n253309 \r\n"}, {"input": "1\r\n94381\r\n", "output": "1\r\n94381 \r\n"}, {"input": "1\r\n935454\r\n", "output": "1\r\n935454 \r\n"}, {"input": "2\r\n847420 569122\r\n", "output": "2\r\n847420 569122\r\n"}, {"input": "2\r\n725491 635622\r\n", "output": "2\r\n725491 635622\r\n"}, {"input": "2\r\n566563 590441\r\n", "output": "2\r\n566563 590441\r\n"}, {"input": "2\r\n407635 619942\r\n", "output": "2\r\n407635 619942\r\n"}, {"input": "2\r\n248707 649443\r\n", "output": "2\r\n248707 649443\r\n"}, {"input": "3\r\n198356 154895 894059\r\n", "output": "3\r\n154895 198356 894059 \r\n"}, {"input": "3\r\n76427 184396 963319\r\n", "output": "3\r\n76427 184396 963319 \r\n"}, {"input": "3\r\n880502 176898 958582\r\n", "output": "1\r\n176898 \r\n"}, {"input": "3\r\n758573 206400 991528\r\n", "output": "1\r\n206400 \r\n"}, {"input": "3\r\n599645 198217 986791\r\n", "output": "1\r\n198217 \r\n"}, {"input": "4\r\n549294 703669 96824 126683\r\n", "output": "3\r\n96824 126683 703669 \r\n"}, {"input": "4\r\n390366 733171 92086 595244\r\n", "output": "3\r\n92086 390366 733171 \r\n"}, {"input": "4\r\n231438 762672 125033 26806\r\n", "output": "3\r\n26806 125033 762672 \r\n"}, {"input": "4\r\n109509 792173 120296 495368\r\n", "output": "3\r\n109509 120296 792173 \r\n"}, {"input": "4\r\n950582 784676 190241 964614\r\n", "output": "1\r\n190241 \r\n"}, {"input": "5\r\n900232 289442 225592 622868 113587\r\n", "output": "3\r\n113587 225592 900232 \r\n"}, {"input": "5\r\n741304 281944 258539 54430 284591\r\n", "output": "3\r\n281944 284591 741304 \r\n"}, {"input": "5\r\n582376 311446 253801 560676 530279\r\n", "output": "3\r\n253801 311446 582376 \r\n"}, {"input": "5\r\n460447 303948 286063 992238 738282\r\n", "output": "3\r\n286063 303948 992238 \r\n"}, {"input": "5\r\n301519 370449 319010 460799 983970\r\n", "output": "3\r\n301519 319010 983970 \r\n"}, {"input": "21\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 999998\r\n", "output": "3\r\n999998 999998 999999 \r\n"}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: lines = f.readlines() return [line.strip() for line in lines] def compute_skewness(subset): sorted_subset = sorted(subset) k = len(sorted_subset) if k == 0: return None total = sum(sorted_subset) mean = total / k if k % 2 == 1: median = sorted_subset[k//2] else: median = (sorted_subset[(k//2)-1] + sorted_subset[k//2])/2 return mean - median def main(): input_path = sys.argv[1] ref_path = sys.argv[2] sub_path = sys.argv[3] input_data = read_file(input_path) ref_data = read_file(ref_path) sub_data = read_file(sub_path) # Parse input n = int(input_data[0]) input_list = list(map(int, input_data[1].split())) input_counts = {} for x in input_list: input_counts[x] = input_counts.get(x, 0) + 1 # Parse submission output if len(sub_data) < 2: print(0) return try: k_sub = int(sub_data[0]) elements_sub = list(map(int, sub_data[1].split())) if k_sub != len(elements_sub) or k_sub ==0: print(0) return except: print(0) return # Check elements are in input for x in elements_sub: if x not in input_counts: print(0) return # Check counts do not exceed input counts sub_counts = {} for x in elements_sub: sub_counts[x] = sub_counts.get(x, 0) + 1 for x, count in sub_counts.items(): if count > input_counts[x]: print(0) return # Parse reference output if len(ref_data) < 2: print(0) return try: k_ref = int(ref_data[0]) elements_ref = list(map(int, ref_data[1].split())) if k_ref != len(elements_ref) or k_ref ==0: print(0) return except: print(0) return # Compute skewness skewness_ref = compute_skewness(elements_ref) skewness_sub = compute_skewness(elements_sub) # Check if submission's skewness is equal to reference's if abs(skewness_sub - skewness_ref) < 1e-9: print(1) else: print(0) if __name__ == '__main__': main()
true
216/B
216
B
Python 3
TESTS
23
92
0
145076548
n, m = map(int, input().split(" ")) st_archen_dict = {i:[] for i in range(1,n+1)} team_1 = [] team_2 = [] to_remove = 0 for _ in range(m): s1, s2 = map(int, input().split(" ")) st_archen_dict[s1].append(s2) st_archen_dict[s2].append(s1) result = set() for i in range(1,n+1): if len(st_archen_dict[i]) == 2: if st_archen_dict[i][0] in st_archen_dict[st_archen_dict[i][1]]: st_archen_dict[st_archen_dict[i][1]].remove(st_archen_dict[i][0]) st_archen_dict[st_archen_dict[i][0]].remove(st_archen_dict[i][1]) result.add((st_archen_dict[i][0], st_archen_dict[i][1])) to_remove += 1 if (n - to_remove) % 2 != 0: to_remove += 1 print(to_remove)
56
92
0
211753527
def Dfs(node): global nodesVisted global result visitedNodes[node]=True nodesVisted+=1 if(len(archenemies[node])==2): leftN,rightN=archenemies[node][0],archenemies[node][1] if(visitedNodes[leftN]&visitedNodes[rightN]): if(nodesVisted%2==1): result+=1 return if(not visitedNodes[leftN]): Dfs(leftN) if(not visitedNodes[rightN]): Dfs(rightN) return n,m=list(map(int,input().split(" "))) archenemies=[[] for i in range(n+1)] result=0 nodesVisted=0 visitedNodes=[False for k in range(n+1)] for i in range(m): ai,bi=list(map(int,input().split(" "))) archenemies[ai].append(bi) archenemies[bi].append(ai) for i in range(1,n+1): if((not visitedNodes[i])&(len(archenemies[i])==2)): nodesVisted=0 Dfs(i) if((n-result)%2!=0): result+=1 print(result)
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
908/B
908
B
Python 3
TESTS
14
62
5,529,600
33817033
from itertools import permutations directiorens=[(0,-1),(-1,0),(0,1),(1,0)] n,l=map(int,input().split()) a=[] for i in range(n): b=list(input()) a.append(b) c=list(input()) c=list(map(int,c)) for i in range(n): for j in range(l): if a[i][j]=="S": position=[i,j] start=position ans=0 for p in permutations(directiorens): for i in c: x,y=p[i] if x<0: position=[position[0],position[1]-1] elif x>0: position=[position[0],position[1]+1] elif y>0: position=[position[0]-1,position[1]] elif y<0: position=[position[0]+1,position[1]] if position[0]<0 or position[0]>=n or position[1]<0 or position[1]>=l: position=start break elif a[position[0]][position[1]]=="#": position=start break if a[position[0]][position[1]]=="E": ans+=1 position=start break print(ans)
46
46
102,400
215851650
def f(s): global pi if len(s)==4: pi+=g(s) else: for i in range(1,4+1): if str(i) not in s: f(s+str(i)) def g(x): q=qq w=ww op=[('u',x[0]),('l',x[1]),('d',x[2]),('r',x[3])] op.sort(key=lambda x:x[1]) ty=1 for i in p: if op[int(i)][0]=='u': q-=1 elif op[int(i)][0]=='l': w-=1 elif op[int(i)][0]=='r': w+=1 else: q+=1 if not(0<=q<n and 0<=w<m and s[q][w]!='#'): ty=0 break elif s[q][w]=='E': break if 0<=q<n and 0<=w<m and s[q][w]=='E': return ty else: return 0 n,m=map(int,input().split()) s=[input() for i in range(n)] for i in range(n): for j in range(m): if s[i][j]=='S': qq=i ww=j break p=input() pi=0 f('') print(pi)
Good Bye 2017
CF
2,017
1
256
New Year and Buggy Bot
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. The robot can only move up, left, right, or down. When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.
The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
null
For the first sample, the only valid mapping is $$0 \rightarrow D, 1 \rightarrow L, 2 \rightarrow U, 3 \rightarrow R$$, where D is down, L is left, U is up, R is right.
[{"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012", "output": "1"}, {"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021", "output": "14"}, {"input": "5 3\n...\n.S.\n###\n.E.\n...\n3", "output": "0"}]
1,200
["brute force", "implementation"]
46
[{"input": "5 6\r\n.....#\r\nS....#\r\n.#....\r\n.#....\r\n...E..\r\n333300012\r\n", "output": "1\r\n"}, {"input": "6 6\r\n......\r\n......\r\n..SE..\r\n......\r\n......\r\n......\r\n01232123212302123021\r\n", "output": "14\r\n"}, {"input": "5 3\r\n...\r\n.S.\r\n###\r\n.E.\r\n...\r\n3\r\n", "output": "0\r\n"}, {"input": "10 10\r\n.#......#.\r\n#.........\r\n#.........\r\n....#.#..E\r\n.......#..\r\n....##....\r\n....S.....\r\n....#.....\r\n.........#\r\n...##...#.\r\n23323332313123221123020122221313323310313122323233\r\n", "output": "0\r\n"}, {"input": "8 9\r\n.........\r\n.........\r\n.........\r\n.E.#.....\r\n.........\r\n.........\r\n...#.S...\r\n.........\r\n10001100111000010121100000110110110100000100000100\r\n", "output": "2\r\n"}, {"input": "15 13\r\n.............\r\n.............\r\n.............\r\n.........#...\r\n..#..........\r\n.............\r\n..........E..\r\n.............\r\n.............\r\n.#...........\r\n.....#.......\r\n..........#..\r\n..........S..\r\n.............\r\n.........#...\r\n32222221111222312132110100022020202131222103103330\r\n", "output": "2\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n..SE.\r\n.....\r\n.....\r\n012330213120031231022103231013201032301223011230102320130231321012030321213002133201130201322031\r\n", "output": "24\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n23\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n03\r\n", "output": "4\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n22\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n11\r\n", "output": "6\r\n"}, {"input": "2 2\r\n#E\r\nS.\r\n01\r\n", "output": "2\r\n"}, {"input": "10 10\r\n####S.####\r\n#####.####\r\n#####.####\r\n#####.####\r\n#####..###\r\n######.###\r\n######.###\r\n######.E##\r\n##########\r\n##########\r\n0111101110\r\n", "output": "2\r\n"}, {"input": "10 10\r\n#####..E##\r\n#####.S.##\r\n#####...##\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n20\r\n", "output": "4\r\n"}, {"input": "10 10\r\n#####ES.##\r\n######.###\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n3\r\n", "output": "6\r\n"}, {"input": "2 10\r\nS........E\r\n..........\r\n33333333333333333\r\n", "output": "6\r\n"}, {"input": "2 2\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n##\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n012\r\n", "output": "8\r\n"}, {"input": "2 3\r\nS.E\r\n###\r\n1222\r\n", "output": "0\r\n"}, {"input": "2 5\r\nS...E\r\n.....\r\n133330\r\n", "output": "1\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n.S.E.\r\n.....\r\n.....\r\n001111\r\n", "output": "6\r\n"}, {"input": "3 5\r\n....S\r\n....#\r\n....E\r\n0112\r\n", "output": "1\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n123\r\n", "output": "8\r\n"}, {"input": "2 10\r\n........ES\r\n..........\r\n123\r\n", "output": "8\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n2311\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n0012\r\n", "output": "0\r\n"}, {"input": "2 7\r\nS.....E\r\n#######\r\n01111111\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n1123\r\n", "output": "0\r\n"}, {"input": "2 3\r\nS.E\r\n...\r\n0111\r\n", "output": "0\r\n"}, {"input": "2 50\r\n.................................................E\r\nS.................................................\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 2\r\n..\r\n..\r\n..\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "3 3\r\nE..\r\n.S.\r\n...\r\n001123110023221103\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS#\r\nE#\r\n012\r\n", "output": "6\r\n"}, {"input": "2 2\r\nES\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n102\r\n", "output": "8\r\n"}, {"input": "3 2\r\nE#\r\n##\r\nS#\r\n0112\r\n", "output": "0\r\n"}]
false
stdio
null
true
825/C
825
C
PyPy 3
TESTS
6
61
17,715,200
132632480
n,k = map(int, input().split()) A = list(map(int, input().split())) A.sort(reverse=True) ans = 0 cur = k for a in A: if 2*cur >= a: continue else: ans += 1 cur = (a+1)//2 print(ans)
61
61
4,915,200
28698078
n,k = [int(a) for a in input().split()] ar = [int(a) for a in input().split()] ar.sort() ct = 0 i = 0 while i < n: if ar[i] > k: if ar[i] > 2*k: k *= 2 ct += 1 else: k = ar[i] i += 1 else: i += 1 print(ct)
Educational Codeforces Round 25
ICPC
2,017
1
256
Multi-judge Solving
Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty — a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). Makes has chosen n problems to solve on Decoforces with difficulties a1, a2, ..., an. He can solve these problems in arbitrary order. Though he can solve problem i with difficulty ai only if he had already solved some problem with difficulty $$d \geq \frac{a_i}{2}$$ (no matter on what online judge was it). Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty k. With given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list. For every positive integer y there exist some problem with difficulty y on at least one judge besides Decoforces. Makes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another. Makes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.
The first line contains two integer numbers n, k (1 ≤ n ≤ 103, 1 ≤ k ≤ 109). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.
null
In the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3. In the second example he can solve every problem right from the start.
[{"input": "3 3\n2 1 9", "output": "1"}, {"input": "4 20\n10 3 6 3", "output": "0"}]
1,600
["greedy", "implementation"]
61
[{"input": "3 3\r\n2 1 9\r\n", "output": "1\r\n"}, {"input": "4 20\r\n10 3 6 3\r\n", "output": "0\r\n"}, {"input": "1 1000000000\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1\r\n3\r\n", "output": "1\r\n"}, {"input": "50 100\r\n74 55 33 5 83 24 75 59 30 36 13 4 62 28 96 17 6 35 45 53 33 11 37 93 34 79 61 72 13 31 44 75 7 3 63 46 18 16 44 89 62 25 32 12 38 55 75 56 61 82\r\n", "output": "0\r\n"}, {"input": "100 10\r\n246 286 693 607 87 612 909 312 621 37 801 558 504 914 416 762 187 974 976 123 635 488 416 659 988 998 93 662 92 749 889 78 214 786 735 625 921 372 713 617 975 119 402 411 878 138 548 905 802 762 940 336 529 373 745 835 805 880 816 94 166 114 475 699 974 462 61 337 555 805 968 815 392 746 591 558 740 380 668 29 881 151 387 986 174 923 541 520 998 947 535 651 103 584 664 854 180 852 726 93\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1000000000\r\n", "output": "29\r\n"}, {"input": "29 2\r\n1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767 65535 131071 262143 524287 1048575 2097151 4194303 8388607 16777215 33554431 67108863 134217727 268435455 536870911\r\n", "output": "27\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "29\r\n"}, {"input": "7 6\r\n4 20 16 14 3 17 4\r\n", "output": "1\r\n"}, {"input": "2 1\r\n3 6\r\n", "output": "1\r\n"}, {"input": "1 1\r\n20\r\n", "output": "4\r\n"}, {"input": "5 2\r\n86 81 53 25 18\r\n", "output": "4\r\n"}, {"input": "4 1\r\n88 55 14 39\r\n", "output": "4\r\n"}, {"input": "3 1\r\n2 3 6\r\n", "output": "0\r\n"}, {"input": "3 2\r\n4 9 18\r\n", "output": "1\r\n"}, {"input": "5 3\r\n6 6 6 13 27\r\n", "output": "2\r\n"}, {"input": "5 1\r\n23 8 83 26 18\r\n", "output": "4\r\n"}, {"input": "3 1\r\n4 5 6\r\n", "output": "1\r\n"}, {"input": "3 1\r\n1 3 6\r\n", "output": "1\r\n"}, {"input": "1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "3 2\r\n4 5 6\r\n", "output": "0\r\n"}, {"input": "5 1\r\n100 200 400 1000 2000\r\n", "output": "7\r\n"}, {"input": "2 1\r\n1 4\r\n", "output": "1\r\n"}, {"input": "4 1\r\n2 4 8 32\r\n", "output": "1\r\n"}, {"input": "2 10\r\n21 42\r\n", "output": "1\r\n"}, {"input": "3 3\r\n1 7 13\r\n", "output": "1\r\n"}, {"input": "3 1\r\n1 4 6\r\n", "output": "1\r\n"}, {"input": "2 2\r\n2 8\r\n", "output": "1\r\n"}, {"input": "1 1\r\n4\r\n", "output": "1\r\n"}, {"input": "2 2\r\n8 16\r\n", "output": "1\r\n"}, {"input": "3 1\r\n4 8 16\r\n", "output": "1\r\n"}, {"input": "3 1\r\n3 6 9\r\n", "output": "1\r\n"}, {"input": "2 1\r\n4 8\r\n", "output": "1\r\n"}, {"input": "2 2\r\n7 14\r\n", "output": "1\r\n"}, {"input": "1 4\r\n9\r\n", "output": "1\r\n"}, {"input": "5 3\r\n1024 4096 16384 65536 536870913\r\n", "output": "24\r\n"}, {"input": "2 5\r\n10 11\r\n", "output": "0\r\n"}, {"input": "2 2\r\n3 6\r\n", "output": "0\r\n"}, {"input": "2 2\r\n8 11\r\n", "output": "1\r\n"}, {"input": "3 19905705\r\n263637263 417905394 108361057\r\n", "output": "3\r\n"}, {"input": "4 25\r\n100 11 1 13\r\n", "output": "1\r\n"}, {"input": "10 295206008\r\n67980321 440051990 883040288 135744260 96431758 242465794 576630162 972797687 356406646 547451696\r\n", "output": "0\r\n"}, {"input": "4 2\r\n45 44 35 38\r\n", "output": "4\r\n"}, {"input": "1 2\r\n9\r\n", "output": "2\r\n"}, {"input": "3 6\r\n13 26 52\r\n", "output": "1\r\n"}, {"input": "9 30111088\r\n824713578 11195876 458715185 731769293 680826358 189542586 550198537 860586039 101083021\r\n", "output": "2\r\n"}, {"input": "3 72014068\r\n430005292 807436976 828082746\r\n", "output": "2\r\n"}, {"input": "3 165219745\r\n737649884 652879952 506420386\r\n", "output": "1\r\n"}, {"input": "2 60669400\r\n95037700 337255240\r\n", "output": "1\r\n"}, {"input": "4 28\r\n34 1 86 90\r\n", "output": "1\r\n"}, {"input": "2 1\r\n5 10\r\n", "output": "2\r\n"}, {"input": "2 1\r\n4 1000000000\r\n", "output": "28\r\n"}, {"input": "2 1\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2 1\r\n3 5\r\n", "output": "1\r\n"}, {"input": "3 3\r\n1 5 20\r\n", "output": "1\r\n"}, {"input": "9 1\r\n1 2 4 9 15 32 64 128 1024\r\n", "output": "4\r\n"}]
false
stdio
null
true
216/B
216
B
PyPy 3
TESTS
23
154
0
176872409
from sys import stdin,stdout input=stdin.readline def print(*args, end='\n', sep=' ') -> None: stdout.write(sep.join(map(str, args)) + end) def dfs(node,c): global ans visi[node]=c for child in nodes[node]: if not visi[child]: dfs(child,c+1) else: if abs(visi[child]-visi[node])==2: ans+=1 visi[child]+=1 n,m=map(int,input().split()) ; nodes={} ; visi=[0]*(n+1) ; ans=0 for i in range(m): x,y=map(int,input().split()) if x in nodes.keys(): nodes[x].append(y) else: nodes[x]=[y] if y in nodes.keys(): nodes[y].append(x) else: nodes[y]=[x] for i in nodes.keys(): if not visi[i]: dfs(i,1) if (n-ans)%2==1: print(ans+1) else: print(ans)
56
124
0
166645558
def dfs(s,e,g): ss.add(s) if vist[s] : return "c" vist[s] = True for i in g[s]: if i != e: if dfs(i,s,g) == "c": return "c" return "p" n,m = map(int,input().split()) g = [[]for i in range(n)] vist = [0 for i in range(n)] for _ in range(m): a,b = map(int,input().split()) a-=1 b-=1 g[a].append(b) g[b].append(a) c = 0 t = 0 while c < n: if not vist[c] : ss = set() if dfs(c,-1,g) == "c": if len(ss)%2 == 1: t+=1 c+=1 n-=t if n%2 == 1: print(t+1) else: print(t)
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
216/B
216
B
Python 3
TESTS
23
218
0
59578435
players, arm = input().split() problems = dict() players, arm, counter = int(players), int(arm), 0 for i in range(arm): pl1, pl2 = input().split() if pl1 not in problems: problems[pl1]= [pl2] else: problems[pl1].append(pl2) if pl2 not in problems: problems[pl2]= [pl1] else: problems[pl2].append(pl1) for j in problems: value = problems[j] if len(value) == 2: num1 = value[0] num2 = value[1] values = problems[num1] if num2 in values: counter = counter + 1 rest = players - int(counter/3) counter = int(counter /3) if rest % 2 != 0: counter = counter+ 1 print(counter)
56
124
0
205966766
n,x=map(int,input().split()) dic={i:[] for i in range(1,n+1)} for i in range(x): a,b=map(int,input().split()) dic[a].append(b) dic[b].append(a) def dfs(s): global vis,c,qw if s in vis: return vis.add(s) c+=1 qw=False if len(dic[s])==2: qw=True for i in dic[s]: dfs(i) vis=set() r=0 for i in dic: c=0 qw=False if i not in vis: dfs(i) if c%2 and qw: r+=1 print(r+((n-r)%2))
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
216/B
216
B
Python 3
TESTS
23
218
0
53766615
def threeAnemeies(palyers, enemey1, enemey2): if enemey1 in palyers[enemey2]: return True return False def setPlayers(nPairs): players = {} c = 0 while c < nPairs: player, enemey = map(int, input().split()) if player not in players: players[player] = [enemey] else: players[player].append(enemey) if enemey not in players: players[enemey] = [player] else: players[enemey].append(player) c += 1 return players def g0t0Bench(nStudent, players): bench = 0 visite = {} for player in players: if player not in visite: if len(players[player]) == 2: enemey1, enemey2 = palyers[player] if threeAnemeies(players, enemey1, enemey2): visite[player] = True visite[enemey1] = True visite[enemey2] = True bench += 1 bench = bench if (nStudent - bench)%2 == 0 else bench + 1 return bench if __name__ == "__main__": nStudent, nPairs = map(int, input().split()) palyers = setPlayers(nPairs) print(g0t0Bench(nStudent, palyers))
56
124
614,400
161568573
import sys def solve(): n, e = list(map(int, sys.stdin.readline().split())) enemies = [] for _ in range(n + 1): enemies.append([]) for _ in range(e): f, s = list(map(int, sys.stdin.readline().split())) enemies[f].append(s) enemies[s].append(f) print(enemies, file=sys.stderr) ans = 0 for i in range(1, n + 1): print(file=sys.stderr) if len(enemies[i]) == 0: continue ans += follow(i, enemies, i) % 2 return ans + (n - ans) % 2 def follow(i, enemies, t, depth = 0): depth += 1 print(i, end=" ", file=sys.stderr) if len(enemies[i]) == 0: return 0 j = enemies[i].pop() if i in enemies[j]: enemies[j].remove(i) if j == t: return depth return follow(j, enemies, t, depth) print(solve())
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
216/B
216
B
Python 3
TESTS
23
248
0
63995075
n,m=map(int,input().split()) L=[0 for i in range(n+1)] d={} for i in range(m) : u,v=map(int,input().split()) vs=d.get(u,[]) vs.append(v) d[u]=vs vs=d.get(v,[]) vs.append(u) d[v]=vs ans=0 for i in range(1,n+1) : if L[i]==1 : continue syd=d.get(i,[]) if len(syd)!=2 : ans+=1 continue if syd[1] in d[syd[0]] : ans+=2 L[i]=1 L[syd[1]]=1 L[syd[0]]=1 continue ans+=1 print(n-(ans//2*2))
56
124
5,529,600
32696670
n,m=map(int,input().split()) ch=[False for i in range(n)] a=[[]for i in range(n)] va=[0] def dfs(i): if ch[i]: return 0 if len(a[i])<2: va[0]=0 re=0 ch[i]=True for i in a[i]: re+=dfs(i) return re+1 for i in range(m): x,y=map(int,input().split()) a[x - 1].append(y-1) a[y - 1].append(x-1) ans=0 for i in range(n): if not ch[i]: va[0] = 1 d=dfs(i) if not(d==1) and d%2==1 and va[0]==1: ans+=1 print(ans+(1 if not(n%2==ans%2)else 0))
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
613/A
613
A
PyPy 3-64
TESTS
2
46
0
175237472
import math def prov(): cosc = (ax * cx + ay * cy) / (a * c) if math.degrees(math.acos(cosc)) <= 90 and 180 - math.degrees(math.acos(cosa)) - math.degrees(math.acos(cosc)) <= 90: return True else: return False n, x0, y0 = [int(i) for i in input().split()] toch = [] for i in range(n): coord = [int(i) for i in input().split()] toch.append(coord) minrast = 1000000000000000 maxrast = -1 toch.append(toch[0]) for i in range(n): rastp = 0 a = math.sqrt((x0 - toch[i][0]) ** 2 + (y0 - toch[i][1]) ** 2) b = math.sqrt((x0 - toch[i + 1][0]) ** 2 + (y0 - toch[i + 1][1]) ** 2) ax = toch[i][0] - x0 ay = toch[i][1] - y0 bx = toch[i + 1][0] - x0 by = toch[i + 1][1] - y0 cosa = (ax * bx + ay * by) / (a * b) sina = math.sin(math.acos(cosa)) c = math.sqrt((toch[i][0] - toch[i + 1][0]) ** 2 + (toch[i][1] - toch[i + 1][1]) ** 2) h = (a * b * sina * 2) / c cx = toch[i][0] - toch[i + 1][0] cy = toch[i][1] - toch[i + 1][1] rast = math.sqrt((x0 - toch[i][0]) ** 2 + (y0 - toch[i][1]) ** 2) if prov(): minrast = min(rast, h, minrast) else: minrast = min(rast, minrast) if rast > maxrast: maxrast = rast print(math.pi * maxrast ** 2 - math.pi * minrast ** 2)
60
639
14,950,400
15366046
__author__ = 'Utena' import math n,a,b=map(int,input().split()) m=[] s=[] for i in range(n): m.append(list(map(int,input().split()))) for i in range(n): a1,b1=m[i] s.append((a1-a)**2+(b1-b)**2) for i in range(n-1): a1,b1=m[i] a2,b2=m[i+1] if ((a2-a1)*(a2-a)+(b2-b1)*(b2-b))*((a2-a1)*(a1-a)+(b2-b1)*(b1-b))<=0: l=((b2-b1)*a-(a2-a1)*b-a1*b2+a2*b1)**2/((b2-b1)**2+(a2-a1)**2) s.append(l) a1,b1=m[-1] a2,b2=m[0] if ((a2-a1)*(a2-a)+(b2-b1)*(b2-b))*((a2-a1)*(a1-a)+(b2-b1)*(b1-b))<=0: l=((b2-b1)*a-(a2-a1)*b-a1*b2+a2*b1)**2/((b2-b1)**2+(a2-a1)**2) s.append(l) t=math.pi*(max(s)-min(s)) print(t)
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Peter and Snow Blower
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path. Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine. Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.
The first line of the input contains three integers — the number of vertices of the polygon n ($$3 \leq n \leq 100\ 000$$), and coordinates of point P. Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line. All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample snow will be removed from that area:
[{"input": "3 0 0\n0 1\n-1 2\n1 2", "output": "12.566370614359172464"}, {"input": "4 1 -1\n0 0\n1 2\n2 0\n1 1", "output": "21.991148575128551812"}]
1,900
["binary search", "geometry", "ternary search"]
60
[{"input": "3 0 0\r\n0 1\r\n-1 2\r\n1 2\r\n", "output": "12.566370614359172464\r\n"}, {"input": "4 1 -1\r\n0 0\r\n1 2\r\n2 0\r\n1 1\r\n", "output": "21.991148575128551812\r\n"}, {"input": "3 0 0\r\n-1 1\r\n0 3\r\n1 1\r\n", "output": "25.132741228718344928\r\n"}, {"input": "3 -4 2\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "405.26545231308331191\r\n"}, {"input": "3 -84 8\r\n-83 8\r\n21 -62\r\n3 53\r\n", "output": "50026.721415763865583\r\n"}, {"input": "6 -94 -51\r\n-93 -51\r\n48 -25\r\n61 27\r\n73 76\r\n-10 87\r\n-48 38\r\n", "output": "138283.48383306192359\r\n"}, {"input": "5 -94 52\r\n-93 52\r\n-78 -56\r\n-54 -81\r\n56 -87\r\n97 85\r\n", "output": "131381.40477312514811\r\n"}, {"input": "10 -100 90\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198410.42563011697403\r\n"}, {"input": "11 -97 -15\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "133558.52848206287476\r\n"}, {"input": "10 -500 420\r\n-499 420\r\n-489 -173\r\n-455 -480\r\n160 -464\r\n374 -437\r\n452 -352\r\n481 -281\r\n465 75\r\n326 392\r\n-398 468\r\n", "output": "4719573.802783449531\r\n"}, {"input": "10 -498 -161\r\n-497 -161\r\n-427 -458\r\n-325 -475\r\n349 -500\r\n441 -220\r\n473 28\r\n475 62\r\n468 498\r\n-444 492\r\n-465 264\r\n", "output": "4295926.8918542123392\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n8 5\r\n10 7\r\n7 5\r\n2 5\r\n", "output": "574.91145560693214023\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n20 3\r\n26 17\r\n23 21\r\n98 96\r\n", "output": "60343.711690152746165\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n94 7\r\n100 52\r\n87 48\r\n37 26\r\n74 61\r\n59 57\r\n87 90\r\n52 90\r\n26 73\r\n", "output": "50337.739088469255101\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n78 22\r\n53 24\r\n78 50\r\n46 39\r\n45 56\r\n21 46\r\n2 7\r\n24 97\r\n5 59\r\n", "output": "32129.068068262814194\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n95 2\r\n47 1\r\n42 1\r\n93 7\r\n56 6\r\n47 7\r\n63 13\r\n98 24\r\n94 27\r\n90 28\r\n86 28\r\n17 6\r\n64 24\r\n42 19\r\n66 35\r\n63 35\r\n98 60\r\n75 48\r\n28 18\r\n71 46\r\n69 46\r\n99 68\r\n64 47\r\n56 43\r\n72 58\r\n35 29\r\n82 81\r\n68 69\r\n79 84\r\n72 77\r\n79 86\r\n54 59\r\n35 39\r\n20 23\r\n73 86\r\n80 97\r\n79 100\r\n69 99\r\n29 45\r\n26 63\r\n23 56\r\n12 33\r\n13 39\r\n25 85\r\n27 96\r\n6 23\r\n4 47\r\n1 60\r\n", "output": "52147.296456936975932\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n69 2\r\n74 7\r\n62 10\r\n64 15\r\n93 22\r\n78 22\r\n56 17\r\n86 29\r\n24 9\r\n91 43\r\n8 4\r\n90 50\r\n99 57\r\n39 23\r\n81 50\r\n91 58\r\n67 46\r\n95 66\r\n52 39\r\n91 69\r\n69 54\r\n93 84\r\n93 98\r\n70 80\r\n85 98\r\n30 39\r\n55 79\r\n41 59\r\n50 72\r\n57 88\r\n58 92\r\n58 94\r\n37 63\r\n43 87\r\n30 63\r\n19 40\r\n38 81\r\n40 86\r\n38 100\r\n2 6\r\n30 100\r\n23 89\r\n16 62\r\n11 49\r\n12 64\r\n9 52\r\n5 62\r\n1 88\r\n", "output": "58543.579099645794717\r\n"}, {"input": "27 -999899 136015\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600304470662.964855\r\n"}, {"input": "19 -995486 -247212\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257949833603.158278\r\n"}, {"input": "15 -994057 554462\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694832748836.689348\r\n"}, {"input": "23 -999840 738880\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831930831113.094931\r\n"}, {"input": "20 -999719 -377746\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331542740428.216614\r\n"}, {"input": "26 -999922 -339832\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127026556380.411608\r\n"}, {"input": "22 -999930 -362070\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335297542813.80731\r\n"}, {"input": "29 -999961 689169\r\n-999960 689169\r\n-999927 -938525\r\n-999735 -989464\r\n-993714 -997911\r\n-870186 -999686\r\n-796253 -999950\r\n-139940 -999968\r\n969552 -999972\r\n985446 -999398\r\n992690 -997295\r\n999706 -973137\r\n999898 -848630\r\n999997 -192297\r\n999969 773408\r\n999495 960350\r\n999143 981671\r\n998324 993987\r\n997640 998103\r\n986157 998977\r\n966840 999418\r\n670113 999809\r\n477888 999856\r\n129160 999900\r\n-373564 999947\r\n-797543 999976\r\n-860769 999903\r\n-995496 999355\r\n-998771 984570\r\n-999768 927157\r\n", "output": "21409384775316.574772\r\n"}, {"input": "3 -3 3\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "399.0305992005743379\r\n"}, {"input": "3 -9 7\r\n-9 6\r\n3 -6\r\n4 2\r\n", "output": "980.17690792001545219\r\n"}, {"input": "5 -9 8\r\n-9 7\r\n-6 -1\r\n-3 -6\r\n1 -3\r\n10 8\r\n", "output": "1130.9820337250702449\r\n"}, {"input": "6 -6 -1\r\n-6 -2\r\n0 -7\r\n8 -9\r\n9 -1\r\n5 10\r\n-5 0\r\n", "output": "816.18577140262825159\r\n"}, {"input": "10 -99 91\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198309.89857373595223\r\n"}, {"input": "11 -96 -14\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "131821.20868619133483\r\n"}, {"input": "13 -98 25\r\n-98 24\r\n-96 10\r\n-80 -71\r\n-71 -78\r\n-31 -99\r\n82 -98\r\n92 -39\r\n94 -2\r\n94 40\r\n90 80\r\n50 96\r\n-41 97\r\n-86 80\r\n", "output": "149316.61930888936332\r\n"}, {"input": "17 -99 -53\r\n-99 -54\r\n-97 -71\r\n-67 -99\r\n-61 -99\r\n56 -98\r\n82 -85\r\n95 -47\r\n90 -2\r\n82 30\r\n63 87\r\n54 95\r\n-12 99\r\n-38 99\r\n-87 89\r\n-90 87\r\n-95 67\r\n-96 49\r\n", "output": "144023.17094830233827\r\n"}, {"input": "19 -995485 -247211\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257930301545.657524\r\n"}, {"input": "15 -994056 554463\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694830011124.045712\r\n"}, {"input": "23 -999839 738881\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831929255745.74826\r\n"}, {"input": "20 -999718 -377745\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331521646100.671528\r\n"}, {"input": "26 -999921 -339831\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127005627407.454252\r\n"}, {"input": "22 -999929 -362069\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335276455623.960732\r\n"}, {"input": "27 -999898 136016\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600299044211.965457\r\n"}, {"input": "13 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999417 840\r\n999781 33421\r\n999994 131490\r\n999993 998865\r\n962080 999911\r\n629402 999973\r\n378696 999988\r\n53978 999788\r\n25311 999558\r\n6082 999282\r\n1565 998489\r\n", "output": "23547598153913.984406\r\n"}, {"input": "16 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999744 572\r\n999931 96510\r\n1000000 254372\r\n999939 748173\r\n999894 953785\r\n999683 986098\r\n999051 999815\r\n980586 999969\r\n637250 999988\r\n118331 999983\r\n27254 999966\r\n9197 999405\r\n4810 997733\r\n1661 995339\r\n", "output": "23547697574489.259052\r\n"}, {"input": "4 0 0\r\n1 -1\r\n1 3\r\n3 3\r\n3 -1\r\n", "output": "53.407075111026482965\r\n"}, {"input": "3 0 0\r\n-10 1\r\n0 2\r\n1 1\r\n", "output": "314.1592653589793116\r\n"}, {"input": "3 0 0\r\n-1 1\r\n4 1\r\n0 2\r\n", "output": "50.265482457436689849\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(output_path, 'r') as f: ref_line = f.readline().strip() try: b = float(ref_line) except: print(0) return with open(submission_path, 'r') as f: sub_line = f.readline().strip() try: a = float(sub_line) except: print(0) return denominator = max(1.0, b) absolute_error = abs(a - b) if absolute_error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
342/A
342
A
PyPy 3
TESTS
33
217
9,625,600
113561514
n = int(input()) l = list(map(int, input().split())) s = set(l) if len(s) < 3 or 5 in l or 7 in l: print(-1) else: d = {1: 0, 2: 0, 3: 0, 4: 0, 6: 0} for i in l: d[i] = d.get(i, 0) + 1 if 6 in s and 4 in s: if d[1] == (d[6]+d[4]): c = d[6] - d[3] rem = d[2] - c if rem <= 0: print(-1) else: if rem != d[4]: print(-1) else: for i in range(rem): print(1, 2, 4) for i in range(d[3]): print(1, 3, 6) for i in range(d[2] - rem): print(1, 2, 6) else: print(-1) else: if 6 in d: if d[1] == d[6]: if d[2] + d[3] == d[6]: for i in range(d[3]): print(1, 3, 6) for i in range(d[6]-d[3]): print(1, 2, 6) else: print(-1) else: print(-1) elif 4 in d: if d[1] == d[4] and d[2] == d[4]: for i in range(d[2]): print(1, 2, 4) else: print(-1) else: print(-1)
45
46
1,126,400
156223119
n=int(input())//3 s=input() c1,c2,c3,c4,c6=[s.count(_)for _ in'12346'] if c1!=n or c2<c4 or c1!=c2+c3 or c1!=c4+c6: print(-1) exit() print('1 2 4\n'*c4+'1 3 6\n'*c3+'1 2 6\n'*(n-c4-c3))
Codeforces Round 199 (Div. 2)
CF
2,013
1
256
Xenia and Divisors
Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held: - a < b < c; - a divides b, b divides c. Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has $$\frac{\pi}{3}$$ groups of three. Help Xenia, find the required partition or else say that it doesn't exist.
The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7. It is guaranteed that n is divisible by 3.
If the required partition exists, print $$\frac{\pi}{3}$$ groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them. If there is no solution, print -1.
null
null
[{"input": "6\n1 1 1 2 2 2", "output": "-1"}, {"input": "6\n2 2 1 1 4 6", "output": "1 2 4\n1 2 6"}]
1,200
["greedy", "implementation"]
45
[{"input": "6\r\n1 1 1 2 2 2\r\n", "output": "-1\r\n"}, {"input": "6\r\n2 2 1 1 4 6\r\n", "output": "1 2 4\r\n1 2 6\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "-1\r\n"}, {"input": "3\r\n7 5 7\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 3 4\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 3 6 6 3 1 3 1 6\r\n", "output": "1 3 6\r\n1 3 6\r\n1 3 6\r\n"}, {"input": "6\r\n1 2 4 1 3 5\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 3 7\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 2 4 1 2 4 1 3 6\r\n", "output": "1 2 4\r\n1 2 4\r\n1 3 6\r\n"}, {"input": "12\r\n3 6 1 1 3 6 1 1 2 6 2 6\r\n", "output": "1 3 6\r\n1 3 6\r\n1 2 6\r\n1 2 6\r\n"}, {"input": "9\r\n1 1 1 4 4 4 6 2 2\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 2 4 6 3 1 3 1 5\r\n", "output": "-1\r\n"}, {"input": "15\r\n2 1 2 1 3 6 1 2 1 6 1 3 4 6 4\r\n", "output": "1 2 4\r\n1 2 4\r\n1 3 6\r\n1 3 6\r\n1 2 6\r\n"}, {"input": "3\r\n2 3 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n2 4 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n2 5 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n2 4 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n7 7 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 4 7 7 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 2 6 6 6\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 3 3 2 4 4 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 4 5 5 5\r\n", "output": "-1\r\n"}, {"input": "15\r\n1 1 1 1 1 2 2 2 2 4 4 6 6 6 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 5 5 7 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 2 3 4 5 6 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 4 4 7 7\r\n", "output": "-1\r\n"}, {"input": "24\r\n1 1 1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 4 4 4 6 6 6\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 7 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1 2 4 7 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 7 7 7 7 7 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n1 1 1 2 3 4 6 5 5\r\n", "output": "-1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) numbers = list(map(int, f.readline().split())) counts = [0] * 8 for num in numbers: counts[num] += 1 with open(submission_path) as f: submission = f.read().strip() if submission == '-1': has_solution = True if counts[5] > 0 or counts[7] > 0: has_solution = False else: count_1 = counts[1] count_2 = counts[2] count_3 = counts[3] count_4 = counts[4] count_6 = counts[6] if count_1 != count_4 + count_6: has_solution = False elif count_6 < count_3: has_solution = False elif count_2 != (count_4 + (count_6 - count_3)): has_solution = False else: x = count_4 y = count_6 - count_3 z = count_3 if x < 0 or y < 0 or z < 0: has_solution = False else: total_groups = (x + y + z) if total_groups * 3 != n: has_solution = False if not has_solution: print(100) else: print(0) else: tokens = list(map(int, submission.split())) if len(tokens) % 3 != 0 or len(tokens) != n: print(0) return groups = [tokens[i:i+3] for i in range(0, len(tokens), 3)] valid_triplets = {(1, 2, 4), (1, 2, 6), (1, 3, 6)} for group in groups: if len(group) != 3: print(0) return a, b, c = group if not (a < b < c): print(0) return if (a, b, c) not in valid_triplets: print(0) return submission_counts = [0] * 8 for num in tokens: submission_counts[num] += 1 valid = True for i in range(1, 8): if i in [5, 7]: if submission_counts[i] != 0: valid = False break else: if submission_counts[i] != counts[i]: valid = False break print(100 if valid else 0) if __name__ == "__main__": main()
true
351/A
351
A
Python 3
TESTS
5
92
0
5340830
n, k, s = int(input()), 0, 0 for i in input().split(): j = int(i[-3: ]) if j == 0: k += 1 else: s += j c = s // 1000 + int(s % 1000 > 500) a, b = max(0, n - k), min(2 * n - k, n) if a <= c <= b: s = abs(c * 1000 - s) else: s = min(abs(a * 1000 - s), abs(b * 1000 - s)) print(str(s // 1000) + '.' + str(s % 1000))
63
154
1,945,600
220840239
n=int(input()) s,z=0,0 for l in map(float,input().split()): s+=(d:=l-int(l)) z+=d>0 print("%.3f"%min(abs(s-i) for i in range(z-n,min(z,n)+1)))
Codeforces Round 204 (Div. 1)
CF
2,013
1
256
Jeff and Rounding
Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: - choose indexes i and j (i ≠ j) that haven't been chosen yet; - round element ai to the nearest integer that isn't more than ai (assign to ai: ⌊ ai ⌋); - round element aj to the nearest integer that isn't less than aj (assign to aj: ⌈ aj ⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a1, a2, ..., a2n (0 ≤ ai ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
null
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.
[{"input": "3\n0.000 0.500 0.750 1.000 2.000 3.000", "output": "0.250"}, {"input": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896", "output": "0.279"}]
1,800
["dp", "greedy", "implementation", "math"]
63
[{"input": "3\r\n0.000 0.500 0.750 1.000 2.000 3.000\r\n", "output": "0.250\r\n"}, {"input": "3\r\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\r\n", "output": "0.279\r\n"}, {"input": "3\r\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000\r\n", "output": "0.184\r\n"}, {"input": "3\r\n6470.649 8295.000 8486.000 9855.000 223.000 579.549\r\n", "output": "0.198\r\n"}, {"input": "7\r\n2341.538 9232.119 6646.930 9316.834 5684.000 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.987\r\n", "output": "0.119\r\n"}, {"input": "9\r\n5528.000 205.031 5245.169 8832.592 385.656 7126.360 3988.000 9542.000 3044.042 5288.351 9342.000 9979.021 7096.000 5159.200 9400.000 4996.735 1698.000 5403.939\r\n", "output": "0.096\r\n"}, {"input": "5\r\n4103.000 6413.459 1796.000 3486.000 9011.000 5564.000 9044.000 5922.539 3350.039 3746.000\r\n", "output": "0.037\r\n"}, {"input": "7\r\n223.999 322.000 677.000 3852.477 2568.390 2410.000 3202.511 2122.870 1566.000 8841.000 8176.424 74.586 3834.000 6847.427\r\n", "output": "0.316\r\n"}, {"input": "10\r\n8003.867 4368.000 2243.000 3340.287 5384.000 1036.456 3506.000 4463.000 1477.000 2420.314 9391.000 1696.000 5857.833 244.000 8220.000 5879.000 5424.482 2631.197 7111.000 9157.536\r\n", "output": "0.028\r\n"}, {"input": "1\r\n6418.000 157.986\r\n", "output": "0.014\r\n"}, {"input": "2\r\n950.000 8019.170 3179.479 9482.963\r\n", "output": "0.388\r\n"}, {"input": "3\r\n4469.437 6526.605 4864.154 9356.383 7490.717 995.896\r\n", "output": "0.192\r\n"}, {"input": "3\r\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000\r\n", "output": "0.184\r\n"}, {"input": "3\r\n6470.649 8295.806 8486.730 9855.351 223.102 579.000\r\n", "output": "0.362\r\n"}, {"input": "7\r\n2341.538 9232.119 6646.930 9316.834 5684.640 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.000\r\n", "output": "0.466\r\n"}, {"input": "9\r\n5528.947 205.031 5245.169 8832.592 385.656 7126.360 3988.000 9542.000 3044.042 5288.000 9342.837 9979.021 7096.022 5159.200 9400.485 4996.735 1698.000 5403.939\r\n", "output": "0.036\r\n"}, {"input": "5\r\n4103.000 6413.459 1796.000 3486.799 9011.590 5564.000 9044.473 5922.000 3350.039 3746.000\r\n", "output": "0.360\r\n"}, {"input": "7\r\n223.000 322.652 677.700 3852.000 2568.390 2410.713 3202.511 2122.870 1566.689 8841.790 8176.424 74.586 3834.000 6847.000\r\n", "output": "0.325\r\n"}, {"input": "10\r\n8003.867 4368.000 2243.298 3340.000 5384.489 1036.000 3506.115 4463.317 1477.000 2420.314 9391.186 1696.000 5857.833 244.314 8220.000 5879.647 5424.482 2631.000 7111.130 9157.536\r\n", "output": "0.472\r\n"}, {"input": "1\r\n6418.669 157.986\r\n", "output": "0.655\r\n"}, {"input": "2\r\n950.335 8019.000 3179.000 9482.000\r\n", "output": "0.335\r\n"}, {"input": "3\r\n4469.000 6526.000 4864.000 9356.000 7490.000 995.000\r\n", "output": "0.000\r\n"}, {"input": "3\r\n673.000 9263.000 6780.254 9801.548 4640.663 8244.038\r\n", "output": "0.497\r\n"}, {"input": "3\r\n6470.000 8295.000 8486.000 9855.000 223.000 579.549\r\n", "output": "0.451\r\n"}, {"input": "7\r\n2341.000 9232.000 6646.000 9316.000 5684.000 9078.000 7773.978 3823.000 6357.000 9866.000 310.000 6554.000 8341.000 8407.987\r\n", "output": "0.035\r\n"}, {"input": "9\r\n5528.000 205.000 5245.000 8832.000 385.000 7126.000 3988.538 9542.484 3044.000 5288.351 9342.000 9979.000 7096.000 5159.000 9400.000 4996.000 1698.000 5403.000\r\n", "output": "0.373\r\n"}, {"input": "5\r\n4103.449 6413.000 1796.581 3486.000 9011.000 5564.010 9044.000 5922.539 3350.000 3746.191\r\n", "output": "0.230\r\n"}, {"input": "7\r\n223.999 322.000 677.000 3852.477 2568.000 2410.000 3202.000 2122.000 1566.000 8841.000 8176.000 74.000 3834.286 6847.427\r\n", "output": "0.189\r\n"}, {"input": "10\r\n8003.000 4368.194 2243.000 3340.287 5384.000 1036.456 3506.000 4463.000 1477.787 2420.000 9391.000 1696.913 5857.000 244.000 8220.322 5879.000 5424.000 2631.197 7111.000 9157.000\r\n", "output": "0.156\r\n"}, {"input": "1\r\n6418.000 157.000\r\n", "output": "0.000\r\n"}, {"input": "4\r\n0.999 0.999 0.999 0.999 0.999 0.999 0.000 0.000\r\n", "output": "1.994\r\n"}, {"input": "1\r\n0.001 0.001\r\n", "output": "0.998\r\n"}, {"input": "1\r\n0.900 0.900\r\n", "output": "0.800\r\n"}, {"input": "2\r\n0.999 0.999 0.999 0.999\r\n", "output": "1.996\r\n"}, {"input": "2\r\n0.001 0.001 0.001 0.001\r\n", "output": "1.996\r\n"}, {"input": "1\r\n1.100 1.200\r\n", "output": "0.700\r\n"}, {"input": "10\r\n0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900\r\n", "output": "8.000\r\n"}, {"input": "2\r\n0.200 0.200 0.200 0.200\r\n", "output": "1.200\r\n"}, {"input": "3\r\n0.900 0.900 0.900 0.900 0.900 0.000\r\n", "output": "1.500\r\n"}, {"input": "1\r\n0.061 0.330\r\n", "output": "0.609\r\n"}, {"input": "2\r\n0.100 0.100 0.100 0.100\r\n", "output": "1.600\r\n"}, {"input": "5\r\n0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001\r\n", "output": "4.990\r\n"}]
false
stdio
null
true
351/A
351
A
Python 3
PRETESTS
5
92
0
4661192
n = int(input()) a = [float(x) - int(float(x)) for x in input().split()] a = list(sorted(a)) ans = sum(a) - len(a) // 2 while len(a) > 0 and a[1] == 0: if (abs(ans) > abs(ans - sum(a[:2]) + 1)): ans = ans - sum(a[:2]) + 1 del a[:2] print("{:.3f}".format(abs(ans)))
63
154
1,945,600
231249920
n=int(input()) s=z=0 for l in map(float,input().split()): s+=(d:=l-int(l)) z+=d>0 print("%.3f"%min(abs(s-i) for i in range(z-n,n+1)))
Codeforces Round 204 (Div. 1)
CF
2,013
1
256
Jeff and Rounding
Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: - choose indexes i and j (i ≠ j) that haven't been chosen yet; - round element ai to the nearest integer that isn't more than ai (assign to ai: ⌊ ai ⌋); - round element aj to the nearest integer that isn't less than aj (assign to aj: ⌈ aj ⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a1, a2, ..., a2n (0 ≤ ai ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
null
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.
[{"input": "3\n0.000 0.500 0.750 1.000 2.000 3.000", "output": "0.250"}, {"input": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896", "output": "0.279"}]
1,800
["dp", "greedy", "implementation", "math"]
63
[{"input": "3\r\n0.000 0.500 0.750 1.000 2.000 3.000\r\n", "output": "0.250\r\n"}, {"input": "3\r\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\r\n", "output": "0.279\r\n"}, {"input": "3\r\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000\r\n", "output": "0.184\r\n"}, {"input": "3\r\n6470.649 8295.000 8486.000 9855.000 223.000 579.549\r\n", "output": "0.198\r\n"}, {"input": "7\r\n2341.538 9232.119 6646.930 9316.834 5684.000 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.987\r\n", "output": "0.119\r\n"}, {"input": "9\r\n5528.000 205.031 5245.169 8832.592 385.656 7126.360 3988.000 9542.000 3044.042 5288.351 9342.000 9979.021 7096.000 5159.200 9400.000 4996.735 1698.000 5403.939\r\n", "output": "0.096\r\n"}, {"input": "5\r\n4103.000 6413.459 1796.000 3486.000 9011.000 5564.000 9044.000 5922.539 3350.039 3746.000\r\n", "output": "0.037\r\n"}, {"input": "7\r\n223.999 322.000 677.000 3852.477 2568.390 2410.000 3202.511 2122.870 1566.000 8841.000 8176.424 74.586 3834.000 6847.427\r\n", "output": "0.316\r\n"}, {"input": "10\r\n8003.867 4368.000 2243.000 3340.287 5384.000 1036.456 3506.000 4463.000 1477.000 2420.314 9391.000 1696.000 5857.833 244.000 8220.000 5879.000 5424.482 2631.197 7111.000 9157.536\r\n", "output": "0.028\r\n"}, {"input": "1\r\n6418.000 157.986\r\n", "output": "0.014\r\n"}, {"input": "2\r\n950.000 8019.170 3179.479 9482.963\r\n", "output": "0.388\r\n"}, {"input": "3\r\n4469.437 6526.605 4864.154 9356.383 7490.717 995.896\r\n", "output": "0.192\r\n"}, {"input": "3\r\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000\r\n", "output": "0.184\r\n"}, {"input": "3\r\n6470.649 8295.806 8486.730 9855.351 223.102 579.000\r\n", "output": "0.362\r\n"}, {"input": "7\r\n2341.538 9232.119 6646.930 9316.834 5684.640 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.000\r\n", "output": "0.466\r\n"}, {"input": "9\r\n5528.947 205.031 5245.169 8832.592 385.656 7126.360 3988.000 9542.000 3044.042 5288.000 9342.837 9979.021 7096.022 5159.200 9400.485 4996.735 1698.000 5403.939\r\n", "output": "0.036\r\n"}, {"input": "5\r\n4103.000 6413.459 1796.000 3486.799 9011.590 5564.000 9044.473 5922.000 3350.039 3746.000\r\n", "output": "0.360\r\n"}, {"input": "7\r\n223.000 322.652 677.700 3852.000 2568.390 2410.713 3202.511 2122.870 1566.689 8841.790 8176.424 74.586 3834.000 6847.000\r\n", "output": "0.325\r\n"}, {"input": "10\r\n8003.867 4368.000 2243.298 3340.000 5384.489 1036.000 3506.115 4463.317 1477.000 2420.314 9391.186 1696.000 5857.833 244.314 8220.000 5879.647 5424.482 2631.000 7111.130 9157.536\r\n", "output": "0.472\r\n"}, {"input": "1\r\n6418.669 157.986\r\n", "output": "0.655\r\n"}, {"input": "2\r\n950.335 8019.000 3179.000 9482.000\r\n", "output": "0.335\r\n"}, {"input": "3\r\n4469.000 6526.000 4864.000 9356.000 7490.000 995.000\r\n", "output": "0.000\r\n"}, {"input": "3\r\n673.000 9263.000 6780.254 9801.548 4640.663 8244.038\r\n", "output": "0.497\r\n"}, {"input": "3\r\n6470.000 8295.000 8486.000 9855.000 223.000 579.549\r\n", "output": "0.451\r\n"}, {"input": "7\r\n2341.000 9232.000 6646.000 9316.000 5684.000 9078.000 7773.978 3823.000 6357.000 9866.000 310.000 6554.000 8341.000 8407.987\r\n", "output": "0.035\r\n"}, {"input": "9\r\n5528.000 205.000 5245.000 8832.000 385.000 7126.000 3988.538 9542.484 3044.000 5288.351 9342.000 9979.000 7096.000 5159.000 9400.000 4996.000 1698.000 5403.000\r\n", "output": "0.373\r\n"}, {"input": "5\r\n4103.449 6413.000 1796.581 3486.000 9011.000 5564.010 9044.000 5922.539 3350.000 3746.191\r\n", "output": "0.230\r\n"}, {"input": "7\r\n223.999 322.000 677.000 3852.477 2568.000 2410.000 3202.000 2122.000 1566.000 8841.000 8176.000 74.000 3834.286 6847.427\r\n", "output": "0.189\r\n"}, {"input": "10\r\n8003.000 4368.194 2243.000 3340.287 5384.000 1036.456 3506.000 4463.000 1477.787 2420.000 9391.000 1696.913 5857.000 244.000 8220.322 5879.000 5424.000 2631.197 7111.000 9157.000\r\n", "output": "0.156\r\n"}, {"input": "1\r\n6418.000 157.000\r\n", "output": "0.000\r\n"}, {"input": "4\r\n0.999 0.999 0.999 0.999 0.999 0.999 0.000 0.000\r\n", "output": "1.994\r\n"}, {"input": "1\r\n0.001 0.001\r\n", "output": "0.998\r\n"}, {"input": "1\r\n0.900 0.900\r\n", "output": "0.800\r\n"}, {"input": "2\r\n0.999 0.999 0.999 0.999\r\n", "output": "1.996\r\n"}, {"input": "2\r\n0.001 0.001 0.001 0.001\r\n", "output": "1.996\r\n"}, {"input": "1\r\n1.100 1.200\r\n", "output": "0.700\r\n"}, {"input": "10\r\n0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900\r\n", "output": "8.000\r\n"}, {"input": "2\r\n0.200 0.200 0.200 0.200\r\n", "output": "1.200\r\n"}, {"input": "3\r\n0.900 0.900 0.900 0.900 0.900 0.000\r\n", "output": "1.500\r\n"}, {"input": "1\r\n0.061 0.330\r\n", "output": "0.609\r\n"}, {"input": "2\r\n0.100 0.100 0.100 0.100\r\n", "output": "1.600\r\n"}, {"input": "5\r\n0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001\r\n", "output": "4.990\r\n"}]
false
stdio
null
true
204/B
204
B
PyPy 3
TESTS
7
280
0
43626782
R = lambda: map(int, input().split()) n = int(input()) f, b = {}, {} nums = set() for x in range(n): l, r = R() f.setdefault(l, 0) b.setdefault(r, 0) f[l], b[r] = f[l] + 1, b[r] + 1 nums.add(l) nums.add(r) res = 10**9 for c in nums: if f.get(c, 0) + b.get(c, 0) >= (n + 1) // 2: res = max(0, min(res, (n + 1) // 2 - f.get(c, 0))) print(res if res < 10**9 else -1)
90
872
16,179,200
100394653
from collections import defaultdict n = int(input()) u, v = defaultdict(int), defaultdict(int) for k in range(n): a, b = map(int, input().split()) u[a] += 1 if b != a: v[b] += 1 k = (n - 1) // 2 s = any(v[b] > k for b in v) t = [u[a] for a in u if u[a] + v[a] > k] if t: print(max(0, k + 1 - max(t))) else: print(k + 1 if s else -1)
Codeforces Round 129 (Div. 1)
CF
2,012
2
256
Little Elephant and Cards
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces.
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
null
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
[{"input": "3\n4 7\n4 7\n7 4", "output": "0"}, {"input": "5\n4 7\n7 4\n2 11\n9 7\n1 1", "output": "2"}]
1,500
["binary search", "data structures"]
90
[{"input": "3\r\n4 7\r\n4 7\r\n7 4\r\n", "output": "0\r\n"}, {"input": "5\r\n4 7\r\n7 4\r\n2 11\r\n9 7\r\n1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n7 7\r\n1 2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1\r\n2 5\r\n3 6\r\n", "output": "-1\r\n"}, {"input": "4\r\n1000000000 1000000000\r\n999999999 1000000000\r\n999999997 999999998\r\n47 74\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2\r\n3 1\r\n4 7\r\n4 1\r\n9 1\r\n7 2\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "0\r\n"}, {"input": "7\r\n4 7\r\n7 4\r\n4 7\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "1\r\n"}, {"input": "10\r\n1000000000 999999999\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "4\r\n"}, {"input": "10\r\n9 1000000000\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "5\r\n"}, {"input": "10\r\n1 10\r\n1 10\r\n1 1\r\n7 8\r\n6 7\r\n9 5\r\n4 1\r\n2 3\r\n3 10\r\n2 8\r\n", "output": "-1\r\n"}, {"input": "10\r\n262253762 715261903\r\n414831157 8354405\r\n419984358 829693421\r\n376600467 175941985\r\n367533995 350629286\r\n681027822 408529849\r\n654503328 717740407\r\n539773033 704670473\r\n55322828 380422378\r\n46174018 186723478\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "12\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n1 1\r\n1 1\r\n2 3\r\n4 5\r\n6 7\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n47 74\r\n47 85874\r\n", "output": "0\r\n"}, {"input": "5\r\n5 8\r\n9 10\r\n5 17\r\n5 24\r\n1 147\r\n", "output": "0\r\n"}, {"input": "5\r\n1 7\r\n2 7\r\n3 7\r\n4 7\r\n5 7\r\n", "output": "3\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1\r\n3 1\r\n4 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n1 3\r\n4 1\r\n5 1\r\n6 7\r\n", "output": "1\r\n"}, {"input": "5\r\n4 7\r\n4 7\r\n2 7\r\n9 7\r\n1 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2\r\n2 1\r\n2 1\r\n3 1\r\n4 2\r\n5 2\r\n6 2\r\n7 2\r\n", "output": "2\r\n"}, {"input": "3\r\n98751 197502\r\n296253 395004\r\n493755 592506\r\n", "output": "-1\r\n"}, {"input": "5\r\n1 5\r\n2 5\r\n3 5\r\n4 7\r\n2 5\r\n", "output": "3\r\n"}, {"input": "10\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n10 1\r\n10 2\r\n10 3\r\n10 4\r\n10 5\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n1 2\r\n1 2\r\n3 1\r\n3 1\r\n3 1\r\n2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 6\r\n5 6\r\n", "output": "3\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1\r\n1 1\r\n2 2\r\n2 2\r\n3 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 5\r\n2 5\r\n3 5\r\n4 4\r\n", "output": "2\r\n"}]
false
stdio
null
true
204/B
204
B
PyPy 3
TESTS
7
280
0
71042726
import sys from math import * input=sys.stdin.readline #n,m,k=map(int,input().split()) d={} n=int(input()) for i in range(n): x,y=map(int,input().split()) if x in d: d[x][0]+=1 else: d[x]=[1,0] if y in d: d[y][1]+=1 else: d[y]=[0,1] mini=10**15 for i in d: if int(ceil(n/2))-d[i][0]<=d[i][1]: mini=min(mini,max(0,int(ceil(n/2))-d[i][0])) if mini!=10**15: print(mini ) else : print(-1)
90
996
24,576,000
116421494
from collections import defaultdict n = int(input()) l1 = defaultdict(int) l2 = defaultdict(int) for _ in range(n): a, b = [int(i) for i in input().split()] if a != b: l1[a] += 1 l2[b] += 1 else: l1[a] += 1 k1 = list(l1.keys()) k2 = list(l2.keys()) ans = k1 + k2 ans = set(ans) k = (n+1)//2 li = [] for i in ans: a = l1[i] b = l2[i] total = a+b if total >= k: if a >= k: li.append(0) else: li.append(k-a) if len(li) == 0: print(-1) else: print(min(li))
Codeforces Round 129 (Div. 1)
CF
2,012
2
256
Little Elephant and Cards
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces.
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
null
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
[{"input": "3\n4 7\n4 7\n7 4", "output": "0"}, {"input": "5\n4 7\n7 4\n2 11\n9 7\n1 1", "output": "2"}]
1,500
["binary search", "data structures"]
90
[{"input": "3\r\n4 7\r\n4 7\r\n7 4\r\n", "output": "0\r\n"}, {"input": "5\r\n4 7\r\n7 4\r\n2 11\r\n9 7\r\n1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n7 7\r\n1 2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1\r\n2 5\r\n3 6\r\n", "output": "-1\r\n"}, {"input": "4\r\n1000000000 1000000000\r\n999999999 1000000000\r\n999999997 999999998\r\n47 74\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2\r\n3 1\r\n4 7\r\n4 1\r\n9 1\r\n7 2\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "0\r\n"}, {"input": "7\r\n4 7\r\n7 4\r\n4 7\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "1\r\n"}, {"input": "10\r\n1000000000 999999999\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "4\r\n"}, {"input": "10\r\n9 1000000000\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "5\r\n"}, {"input": "10\r\n1 10\r\n1 10\r\n1 1\r\n7 8\r\n6 7\r\n9 5\r\n4 1\r\n2 3\r\n3 10\r\n2 8\r\n", "output": "-1\r\n"}, {"input": "10\r\n262253762 715261903\r\n414831157 8354405\r\n419984358 829693421\r\n376600467 175941985\r\n367533995 350629286\r\n681027822 408529849\r\n654503328 717740407\r\n539773033 704670473\r\n55322828 380422378\r\n46174018 186723478\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "12\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n1 1\r\n1 1\r\n2 3\r\n4 5\r\n6 7\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n47 74\r\n47 85874\r\n", "output": "0\r\n"}, {"input": "5\r\n5 8\r\n9 10\r\n5 17\r\n5 24\r\n1 147\r\n", "output": "0\r\n"}, {"input": "5\r\n1 7\r\n2 7\r\n3 7\r\n4 7\r\n5 7\r\n", "output": "3\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1\r\n3 1\r\n4 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n1 3\r\n4 1\r\n5 1\r\n6 7\r\n", "output": "1\r\n"}, {"input": "5\r\n4 7\r\n4 7\r\n2 7\r\n9 7\r\n1 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2\r\n2 1\r\n2 1\r\n3 1\r\n4 2\r\n5 2\r\n6 2\r\n7 2\r\n", "output": "2\r\n"}, {"input": "3\r\n98751 197502\r\n296253 395004\r\n493755 592506\r\n", "output": "-1\r\n"}, {"input": "5\r\n1 5\r\n2 5\r\n3 5\r\n4 7\r\n2 5\r\n", "output": "3\r\n"}, {"input": "10\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n10 1\r\n10 2\r\n10 3\r\n10 4\r\n10 5\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n1 2\r\n1 2\r\n3 1\r\n3 1\r\n3 1\r\n2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 6\r\n5 6\r\n", "output": "3\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1\r\n1 1\r\n2 2\r\n2 2\r\n3 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 5\r\n2 5\r\n3 5\r\n4 4\r\n", "output": "2\r\n"}]
false
stdio
null
true
204/B
204
B
Python 3
TESTS
7
124
4,915,200
25585466
import math def get_min_changes(cards_n, all_numbers, dict1, dict2): min_changes = cards_n + 1 found = False for number in all_numbers: if number in dict1 and len(dict1[number]) >= math.ceil(cards_n / 2): return 0 else: if number in dict1 and number in dict2: changes = math.ceil(cards_n / 2) - len(dict1[number]) if len(dict2[number]) >= changes and changes < min_changes: min_changes = changes found = True elif number in dict2 and len(dict2[number]) >= math.ceil(cards_n / 2): if math.ceil(cards_n / 2) <= min_changes: min_changes = math.ceil(cards_n / 2) found = True if found: return min_changes else: return -1 n = int(input()) upper_dict = dict() lower_dict = dict() numbers = set() for i in range(n): card_upper, card_lower = [int(x) for x in input().split(' ')] if card_upper in upper_dict: upper_dict[card_upper].add(i) else: upper_dict[card_upper] = {i} if card_lower in lower_dict: lower_dict[card_lower].add(i) else: lower_dict[card_lower] = {i} numbers.add(card_upper) numbers.add(card_lower) print(get_min_changes(n, numbers, upper_dict, lower_dict))
90
1,060
42,393,600
128056767
from collections import defaultdict n = int(input()) front = defaultdict(int) back = defaultdict(int) frontback = defaultdict(int) colors = set() for i in range(n): x, y = input().split() if x == y: frontback[x] += 1 else: front[x] += 1 back[y] += 1 colors.add(x) colors.add(y) mincost = 1e9 for c in colors: R = (n+1)//2 cost = R - frontback[c] - front[c] if cost <= back[c]: mincost = min(cost, mincost) mincost = max(mincost, 0) if mincost == 1e9: print(-1) else: print(mincost)
Codeforces Round 129 (Div. 1)
CF
2,012
2
256
Little Elephant and Cards
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces.
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
null
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
[{"input": "3\n4 7\n4 7\n7 4", "output": "0"}, {"input": "5\n4 7\n7 4\n2 11\n9 7\n1 1", "output": "2"}]
1,500
["binary search", "data structures"]
90
[{"input": "3\r\n4 7\r\n4 7\r\n7 4\r\n", "output": "0\r\n"}, {"input": "5\r\n4 7\r\n7 4\r\n2 11\r\n9 7\r\n1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n7 7\r\n1 2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1\r\n2 5\r\n3 6\r\n", "output": "-1\r\n"}, {"input": "4\r\n1000000000 1000000000\r\n999999999 1000000000\r\n999999997 999999998\r\n47 74\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2\r\n3 1\r\n4 7\r\n4 1\r\n9 1\r\n7 2\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "0\r\n"}, {"input": "7\r\n4 7\r\n7 4\r\n4 7\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "1\r\n"}, {"input": "10\r\n1000000000 999999999\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "4\r\n"}, {"input": "10\r\n9 1000000000\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "5\r\n"}, {"input": "10\r\n1 10\r\n1 10\r\n1 1\r\n7 8\r\n6 7\r\n9 5\r\n4 1\r\n2 3\r\n3 10\r\n2 8\r\n", "output": "-1\r\n"}, {"input": "10\r\n262253762 715261903\r\n414831157 8354405\r\n419984358 829693421\r\n376600467 175941985\r\n367533995 350629286\r\n681027822 408529849\r\n654503328 717740407\r\n539773033 704670473\r\n55322828 380422378\r\n46174018 186723478\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "12\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n1 1\r\n1 1\r\n2 3\r\n4 5\r\n6 7\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n47 74\r\n47 85874\r\n", "output": "0\r\n"}, {"input": "5\r\n5 8\r\n9 10\r\n5 17\r\n5 24\r\n1 147\r\n", "output": "0\r\n"}, {"input": "5\r\n1 7\r\n2 7\r\n3 7\r\n4 7\r\n5 7\r\n", "output": "3\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1\r\n3 1\r\n4 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n1 3\r\n4 1\r\n5 1\r\n6 7\r\n", "output": "1\r\n"}, {"input": "5\r\n4 7\r\n4 7\r\n2 7\r\n9 7\r\n1 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2\r\n2 1\r\n2 1\r\n3 1\r\n4 2\r\n5 2\r\n6 2\r\n7 2\r\n", "output": "2\r\n"}, {"input": "3\r\n98751 197502\r\n296253 395004\r\n493755 592506\r\n", "output": "-1\r\n"}, {"input": "5\r\n1 5\r\n2 5\r\n3 5\r\n4 7\r\n2 5\r\n", "output": "3\r\n"}, {"input": "10\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n10 1\r\n10 2\r\n10 3\r\n10 4\r\n10 5\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n1 2\r\n1 2\r\n3 1\r\n3 1\r\n3 1\r\n2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 6\r\n5 6\r\n", "output": "3\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1\r\n1 1\r\n2 2\r\n2 2\r\n3 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 5\r\n2 5\r\n3 5\r\n4 4\r\n", "output": "2\r\n"}]
false
stdio
null
true
730/H
730
H
Python 3
TESTS
26
77
6,963,200
123791511
# _ ##################################################################################################################### def fileNamePattern(nNamesToDelete, name_sLength, namesToDelete, namesWithSimilarLength): nNamesToTest = len(namesWithSimilarLength) pattern = '' onlyDeleteDesiredNames = not nNamesToTest for i in range(name_sLength): letter = namesToDelete[0][i] for j in range(1, nNamesToDelete): if letter != namesToDelete[j][i]: letter = '?' break for k in range(nNamesToTest): if namesWithSimilarLength[k][i] != letter and letter != '?': onlyDeleteDesiredNames = True pattern += letter if onlyDeleteDesiredNames: return f'Yes\n{pattern}' return 'No' def main(): nNames, nNamesToDelete = map(int, input().split()) rawStorage = [input() for x in range(nNames)] indices = set(map(lambda x: int(x)-1, input().split())) name_sLength = len(rawStorage[next(iter(indices))]) namesToDelete, namesWithSimilarLength = [], [] for i in range(nNames): if i in indices: if name_sLength != len(rawStorage[i]): return 'No' namesToDelete.append(rawStorage[i]) elif name_sLength == len(rawStorage[i]): namesWithSimilarLength.append(rawStorage[i]) return fileNamePattern(nNamesToDelete, name_sLength, namesToDelete, namesWithSimilarLength) if __name__ == '__main__': print(main())
74
62
0
21702051
m,n=list(map(int,input().split())) s=[input().strip() for i in range(m)] a=list(map(lambda x:int(x)-1,input().split())) stmpl=s[a[0]] f=1 def peres(s1,s2): return ''.join([i if i==j else '?' for i,j in zip(s1,s2)]) for i in a: if len(stmpl)!=len(s[i]): f=0 break stmpl=peres(stmpl,s[i]) for i,e in enumerate(s): if i in a: continue if len(stmpl)==len(e) and stmpl==peres(stmpl,e): f=0 break if f: print('Yes') print(stmpl) else: print('No')
2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,016
2
512
Delete Them
Polycarp is a beginner programmer. He is studying how to use a command line. Polycarp faced the following problem. There are n files in a directory and he needs to delete some of them. Polycarp wants to run a single delete command with filename pattern as an argument. All the files to be deleted should match the pattern and all other files shouldn't match the pattern. Polycarp doesn't know about an asterisk '*', the only special character he knows is a question mark '?' which matches any single character. All other characters in the pattern match themselves only. Formally, a pattern matches a filename if and only if they have equal lengths and all characters in the corresponding positions are equal except when the character in the pattern is '?', in which case the corresponding filename character does not matter. For example, the filename pattern "a?ba?": - matches filenames "aabaa", "abba.", "a.ba9" and "a.ba."; - does not match filenames "aaba", "abaab", "aabaaa" and "aabaa.". Help Polycarp find a pattern which matches files to be deleted and only them or report if there is no such pattern.
The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 100) — the total number of files and the number of files to be deleted. The following n lines contain filenames, single filename per line. All filenames are non-empty strings containing only lowercase English letters, digits and dots ('.'). The length of each filename doesn't exceed 100. It is guaranteed that all filenames are distinct. The last line of the input contains m distinct integer numbers in ascending order a1, a2, ..., am (1 ≤ ai ≤ n) — indices of files to be deleted. All files are indexed from 1 to n in order of their appearance in the input.
If the required pattern exists, print "Yes" in the first line of the output. The second line should contain the required pattern. If there are multiple solutions, print any of them. If the required pattern doesn't exist, print the only line containing "No".
null
null
[{"input": "3 2\nab\nac\ncd\n1 2", "output": "Yes\na?"}, {"input": "5 3\ntest\ntezt\ntest.\n.est\ntes.\n1 4 5", "output": "Yes\n?es?"}, {"input": "4 4\na\nb\nc\ndd\n1 2 3 4", "output": "No"}, {"input": "6 3\n.svn\n.git\n....\n...\n..\n.\n1 2 3", "output": "Yes\n.???"}]
1,300
["constructive algorithms", "implementation"]
74
[{"input": "3 2\r\nab\r\nac\r\ncd\r\n1 2\r\n", "output": "Yes\r\na?\r\n"}, {"input": "5 3\r\ntest\r\ntezt\r\ntest.\r\n.est\r\ntes.\r\n1 4 5\r\n", "output": "Yes\r\n?es?\r\n"}, {"input": "4 4\r\na\r\nb\r\nc\r\ndd\r\n1 2 3 4\r\n", "output": "No\r\n"}, {"input": "6 3\r\n.svn\r\n.git\r\n....\r\n...\r\n..\r\n.\r\n1 2 3\r\n", "output": "Yes\r\n.???\r\n"}, {"input": "4 2\r\n.b\r\n.c\r\ndbt\r\ne.\r\n2 4\r\n", "output": "No\r\n"}, {"input": "27 27\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n.\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27\r\n", "output": "Yes\r\n?\r\n"}, {"input": "27 26\r\na\r\nb\r\nc\r\nd\r\nee\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n.\r\n1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27\r\n", "output": "Yes\r\n?\r\n"}, {"input": "27 26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nkq\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n.\r\n1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27\r\n", "output": "No\r\n"}, {"input": "1 1\r\nuevim.mrr\r\n1\r\n", "output": "Yes\r\nuevim.mrr\r\n"}, {"input": "2 1\r\nkbfyvezmy\r\nsbfammwcy\r\n1\r\n", "output": "Yes\r\nkbfyvezmy\r\n"}, {"input": "5 3\r\nlmljeqklg\r\nlclydkkxj\r\nuylscbk.g\r\neplpqakme\r\nablibhkfg\r\n1 3 5\r\n", "output": "Yes\r\n??l???k?g\r\n"}, {"input": "5 4\r\nabacaba\r\naaaaaaa\r\naaaaaab\r\naaaaaac\r\naaaaaad\r\n2 3 4 5\r\n", "output": "Yes\r\naaaaaa?\r\n"}, {"input": "5 4\r\nabacaba\r\naaaaaaa\r\nbaaaaab\r\ncaaaaac\r\ndaaaaad\r\n2 3 4 5\r\n", "output": "Yes\r\n?aaaaa?\r\n"}, {"input": "5 5\r\nabacaba\r\naaaaaaa\r\nbaaaaab\r\ncaaaaac\r\ndaaaaad\r\n1 2 3 4 5\r\n", "output": "Yes\r\n??a?a??\r\n"}, {"input": "5 3\r\nabacaba\r\naaaaaaa\r\nbaaaaab\r\ncaaaaac\r\ndaaaaad\r\n2 3 4\r\n", "output": "No\r\n"}, {"input": "5 4\r\naaaaaaa\r\nbaaaaab\r\ncaaaaac\r\ndaaaaad\r\nabacaba\r\n1 2 3 4\r\n", "output": "Yes\r\n?aaaaa?\r\n"}, {"input": "5 3\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\ncccccccccc\r\ndddddddddd\r\neeeeeeeeee\r\n1 3 5\r\n", "output": "No\r\n"}, {"input": "5 4\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\ncccccccccc\r\ndddddddddd\r\neeeeeeeeee\r\n1 3 4 5\r\n", "output": "No\r\n"}, {"input": "5 5\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\ncccccccccc\r\ndddddddddd\r\neeeeeeeeee\r\n1 2 3 4 5\r\n", "output": "Yes\r\n??????????\r\n"}, {"input": "5 4\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\ncccccccccc\r\ndddddddddd\r\neeeeeeeee\r\n1 2 3 4\r\n", "output": "Yes\r\n??????????\r\n"}, {"input": "5 4\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\ncccccccccc\r\neeeeeeeee\r\ndddddddddd\r\n1 2 3 5\r\n", "output": "Yes\r\n??????????\r\n"}, {"input": "5 4\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\neeeeeeeee\r\ncccccccccc\r\ndddddddddd\r\n1 2 4 5\r\n", "output": "Yes\r\n??????????\r\n"}, {"input": "5 4\r\naaaaaaaaaa\r\neeeeeeeee\r\nbbbbbbbbbb\r\ncccccccccc\r\ndddddddddd\r\n1 3 4 5\r\n", "output": "Yes\r\n??????????\r\n"}, {"input": "5 4\r\neeeeeeeee\r\naaaaaaaaaa\r\nbbbbbbbbbb\r\ncccccccccc\r\ndddddddddd\r\n2 3 4 5\r\n", "output": "Yes\r\n??????????\r\n"}, {"input": "2 1\r\na\r\nb\r\n1\r\n", "output": "Yes\r\na\r\n"}, {"input": "2 1\r\na\r\nb\r\n2\r\n", "output": "Yes\r\nb\r\n"}, {"input": "2 2\r\na\r\nb\r\n1 2\r\n", "output": "Yes\r\n?\r\n"}, {"input": "2 1\r\naa\r\nb\r\n1\r\n", "output": "Yes\r\naa\r\n"}, {"input": "2 1\r\naa\r\nb\r\n2\r\n", "output": "Yes\r\nb\r\n"}, {"input": "2 2\r\naa\r\nb\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 1\r\nb\r\naa\r\n1\r\n", "output": "Yes\r\nb\r\n"}, {"input": "2 1\r\nb\r\naa\r\n2\r\n", "output": "Yes\r\naa\r\n"}, {"input": "2 2\r\nb\r\naa\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac\r\n1\r\n", "output": "Yes\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab\r\n"}]
false
stdio
null
true
38/A
38
A
PyPy 3-64
TESTS
5
92
0
220895934
n = int(input()) l1 = list(map(int , input().split())) l2 = list(map(int , input().split())) res= 0 if l2[-1] == n : print(sum(l1)) else : print(sum(l1[l2[0] -1 : l2[-1] - 1]))
50
62
0
162588184
n = int(input()) x = list(map(int, input().split())) (a, b) = map(int, input().split()) y = [0] * (n + 1) for i in range(len(x)): y[i + 1] = y[i] + x[i] print(y[b - 1] - y[a - 1])
School Personal Contest #1 (Winter Computer School 2010/11) - Codeforces Beta Round 38 (ACM-ICPC Rules)
ICPC
2,010
2
256
Army
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank. One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible. Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.
The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.
Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.
null
null
[{"input": "3\n5 6\n1 2", "output": "5"}, {"input": "3\n5 6\n1 3", "output": "11"}]
800
["implementation"]
50
[{"input": "3\r\n5 6\r\n1 2\r\n", "output": "5\r\n"}, {"input": "3\r\n5 6\r\n1 3\r\n", "output": "11\r\n"}, {"input": "2\r\n55\r\n1 2\r\n", "output": "55\r\n"}, {"input": "3\r\n85 78\r\n1 3\r\n", "output": "163\r\n"}, {"input": "4\r\n63 4 49\r\n2 3\r\n", "output": "4\r\n"}, {"input": "5\r\n93 83 42 56\r\n2 5\r\n", "output": "181\r\n"}, {"input": "6\r\n22 9 87 89 57\r\n1 6\r\n", "output": "264\r\n"}, {"input": "7\r\n52 36 31 23 74 78\r\n2 7\r\n", "output": "242\r\n"}, {"input": "8\r\n82 14 24 5 91 49 94\r\n3 8\r\n", "output": "263\r\n"}, {"input": "9\r\n12 40 69 39 59 21 59 5\r\n4 6\r\n", "output": "98\r\n"}, {"input": "10\r\n95 81 32 59 71 30 50 61 100\r\n1 6\r\n", "output": "338\r\n"}, {"input": "15\r\n89 55 94 4 15 69 19 60 91 77 3 94 91 62\r\n3 14\r\n", "output": "617\r\n"}, {"input": "20\r\n91 1 41 51 95 67 92 35 23 70 44 91 57 50 21 8 9 71 40\r\n8 17\r\n", "output": "399\r\n"}, {"input": "25\r\n70 95 21 84 97 39 12 98 53 24 78 29 84 65 70 22 100 17 69 27 62 48 35 80\r\n8 23\r\n", "output": "846\r\n"}, {"input": "30\r\n35 69 50 44 19 56 86 56 98 24 21 2 61 24 85 30 2 22 57 35 59 84 12 77 92 53 50 92 9\r\n1 16\r\n", "output": "730\r\n"}, {"input": "35\r\n2 34 47 15 27 61 6 88 67 20 53 65 29 68 77 5 78 86 44 98 32 81 91 79 54 84 95 23 65 97 22 33 42 87\r\n8 35\r\n", "output": "1663\r\n"}, {"input": "40\r\n32 88 59 36 95 45 28 78 73 30 97 13 13 47 48 100 43 21 22 45 88 25 15 13 63 25 72 92 29 5 25 11 50 5 54 51 48 84 23\r\n7 26\r\n", "output": "862\r\n"}, {"input": "45\r\n83 74 73 95 10 31 100 26 29 15 80 100 22 70 31 88 9 56 19 70 2 62 48 30 27 47 52 50 94 44 21 94 23 85 15 3 95 72 43 62 94 89 68 88\r\n17 40\r\n", "output": "1061\r\n"}, {"input": "50\r\n28 8 16 29 19 82 70 51 96 84 74 72 17 69 12 21 37 21 39 3 18 66 19 49 86 96 94 93 2 90 96 84 59 88 58 15 61 33 55 22 35 54 51 29 64 68 29 38 40\r\n23 28\r\n", "output": "344\r\n"}, {"input": "60\r\n24 28 25 21 43 71 64 73 71 90 51 83 69 43 75 43 78 72 56 61 99 7 23 86 9 16 16 94 23 74 18 56 20 72 13 31 75 34 35 86 61 49 4 72 84 7 65 70 66 52 21 38 6 43 69 40 73 46 5\r\n28 60\r\n", "output": "1502\r\n"}, {"input": "70\r\n69 95 34 14 67 61 6 95 94 44 28 94 73 66 39 13 19 71 73 71 28 48 26 22 32 88 38 95 43 59 88 77 80 55 17 95 40 83 67 1 38 95 58 63 56 98 49 2 41 4 73 8 78 41 64 71 60 71 41 61 67 4 4 19 97 14 39 20 27\r\n9 41\r\n", "output": "1767\r\n"}, {"input": "80\r\n65 15 43 6 43 98 100 16 69 98 4 54 25 40 2 35 12 23 38 29 10 89 30 6 4 8 7 96 64 43 11 49 89 38 20 59 54 85 46 16 16 89 60 54 28 37 32 34 67 9 78 30 50 87 58 53 99 48 77 3 5 6 19 99 16 20 31 10 80 76 82 56 56 83 72 81 84 60 28\r\n18 24\r\n", "output": "219\r\n"}, {"input": "51\r\n85 38 22 38 42 36 55 24 36 80 49 15 66 91 88 61 46 82 1 61 89 92 6 56 28 8 46 80 56 90 91 38 38 17 69 64 57 68 13 44 45 38 8 72 61 39 87 2 73 88\r\n15 27\r\n", "output": "618\r\n"}, {"input": "2\r\n3\r\n1 2\r\n", "output": "3\r\n"}, {"input": "5\r\n6 8 22 22\r\n2 3\r\n", "output": "8\r\n"}, {"input": "6\r\n3 12 27 28 28\r\n3 4\r\n", "output": "27\r\n"}, {"input": "9\r\n1 2 2 2 2 3 3 5\r\n3 7\r\n", "output": "9\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1\r\n6 8\r\n", "output": "2\r\n"}, {"input": "20\r\n1 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3\r\n5 17\r\n", "output": "23\r\n"}, {"input": "25\r\n1 1 1 4 5 6 8 11 11 11 11 12 13 14 14 14 15 16 16 17 17 17 19 19\r\n4 8\r\n", "output": "23\r\n"}, {"input": "35\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n30 31\r\n", "output": "2\r\n"}, {"input": "45\r\n1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 8 8 8 9 9 9 9 9 10 10 10\r\n42 45\r\n", "output": "30\r\n"}, {"input": "50\r\n1 8 8 13 14 15 15 16 19 21 22 24 26 31 32 37 45 47 47 47 50 50 51 54 55 56 58 61 61 61 63 63 64 66 66 67 67 70 71 80 83 84 85 92 92 94 95 95 100\r\n4 17\r\n", "output": "285\r\n"}, {"input": "60\r\n1 2 4 4 4 6 6 8 9 10 10 13 14 18 20 20 21 22 23 23 26 29 30 32 33 34 35 38 40 42 44 44 46 48 52 54 56 56 60 60 66 67 68 68 69 73 73 74 80 80 81 81 82 84 86 86 87 89 89\r\n56 58\r\n", "output": "173\r\n"}, {"input": "70\r\n1 2 3 3 4 5 5 7 7 7 8 8 8 8 9 9 10 12 12 12 12 13 16 16 16 16 16 16 17 17 18 18 20 20 21 23 24 25 25 26 29 29 29 29 31 32 32 34 35 36 36 37 37 38 39 39 40 40 40 40 41 41 42 43 44 44 44 45 45\r\n62 65\r\n", "output": "126\r\n"}, {"input": "80\r\n1 1 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 5 5 5 5 5 5 5 6 7 7 7 7 7 7 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12\r\n17 65\r\n", "output": "326\r\n"}, {"input": "99\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n66 95\r\n", "output": "29\r\n"}, {"input": "100\r\n1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 11 11 11 11 12 12 12 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 15 15 15 15 15 15 16 16 16 17 17\r\n39 52\r\n", "output": "82\r\n"}, {"input": "51\r\n5 7 9 16 19 25 26 29 29 30 31 32 32 41 43 44 47 49 50 50 51 52 54 55 56 63 64 66 67 70 74 74 77 78 79 80 80 85 86 87 89 89 90 92 93 94 94 95 95 97\r\n3 44\r\n", "output": "2268\r\n"}]
false
stdio
null
true
817/B
817
B
Python 3
TESTS
18
124
9,113,600
27921633
#def C(n, k): #return( f(n) // (f(k) * f(n - k))) #def f(m): #r = 1 #for i in range(1, m + 1): #r *= i #return(r) n = input() a = list(map(int, input().split())) mn = sorted(a)[:3] res = 1 for i in mn: c = a.count(i) if mn.count(i) == 3: res = ((c *(c - 1) * (c - 2) ) // 6) break elif mn.count(i) == 2: res *= (c * (c- 1) // 2) else: res *= c print(res)
80
109
9,113,600
27800790
def c(n, k): r = 1 for i in range(k): r *= n n -= 1 for i in range(2, k + 1): r //= i return r n = int(input()) l = sorted(map(int, input().split())) prev = l[0] beg = 0 cnt = 0 ans = 1 for i in range(n): if l[i] == prev: cnt += 1 else: ans *= c(i - beg, min(3, i) - beg) cnt = 1 prev = l[i] beg = i if i > 2: break if beg < 3: ans *= c(n - beg, 3 - beg) print(ans)
Educational Codeforces Round 23
ICPC
2,017
2
256
Makes And The Product
After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.
null
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4. In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2. In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
[{"input": "4\n1 1 1 1", "output": "4"}, {"input": "5\n1 3 2 3 4", "output": "2"}, {"input": "6\n1 3 3 1 3 2", "output": "1"}]
1,500
["combinatorics", "implementation", "math", "sortings"]
80
[{"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "2\r\n"}, {"input": "6\r\n1 3 3 1 3 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "1\r\n"}, {"input": "11\r\n1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "45\r\n"}, {"input": "5\r\n1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6\r\n1 2 2 3 3 4\r\n", "output": "1\r\n"}, {"input": "8\r\n1 1 2 2 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 3\r\n", "output": "6\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2 2 2 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "4\r\n1 2 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 2 3\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 3 3 3 3\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 2 2 5 6\r\n", "output": "3\r\n"}, {"input": "10\r\n1 2 2 2 2 2 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "3\r\n2 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2 2 3\r\n", "output": "1\r\n"}, {"input": "10\r\n2 2 2 2 2 1 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "7\r\n2 2 2 3 3 3 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 2 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "9\r\n2 2 3 3 3 3 3 3 3\r\n", "output": "7\r\n"}, {"input": "5\r\n1 1 2 2 3\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 3 3\r\n", "output": "2\r\n"}, {"input": "4\r\n33554432 33554432 67108864 33554432\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 2 1 2 2\r\n", "output": "10\r\n"}, {"input": "10\r\n1 2 1 2 3 2 3 2 2 2\r\n", "output": "6\r\n"}, {"input": "10\r\n9 6 4 7 1 8 9 5 9 4\r\n", "output": "1\r\n"}, {"input": "4\r\n5 7 2 7\r\n", "output": "2\r\n"}, {"input": "3\r\n7 6 7\r\n", "output": "1\r\n"}, {"input": "6\r\n3 2 8 2 5 3\r\n", "output": "2\r\n"}, {"input": "3\r\n5 9 5\r\n", "output": "1\r\n"}, {"input": "5\r\n6 3 7 6 3\r\n", "output": "2\r\n"}, {"input": "9\r\n10 10 4 10 7 9 6 7 3\r\n", "output": "1\r\n"}, {"input": "5\r\n9 10 10 3 8\r\n", "output": "1\r\n"}, {"input": "5\r\n2 9 5 10 5\r\n", "output": "1\r\n"}, {"input": "9\r\n7 1 9 6 6 8 3 1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n3 4 4 4 5\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 3\r\n", "output": "1\r\n"}, {"input": "8\r\n3 2 2 5 2 2 1 2\r\n", "output": "10\r\n"}]
false
stdio
null
true
817/B
817
B
PyPy 3
TESTS
18
233
10,956,800
66778415
n = int(input()) a = sorted(list(map(int, input().split()))) b = a[3:] if a[0] == a[1] == a[2]: n = a.count(a[0]) print(n * (n - 1) * (n - 2) // 6) elif a[0] == a[1]: print((1 + b.count(a[0])) * (1 + b.count(a[2]))) elif a[1] == a[2]: print((1 + b.count(a[0])) * (1 + b.count(a[1]))) elif a[0] == a[2]: print((1 + b.count(a[0])) * (1 + b.count([1]))) else: print((1 + b.count(a[0])) * (1 + b.count(a[1])) * (1 + b.count(a[2])))
80
109
9,113,600
27800805
n=int(input()) a=sorted(list(map(int, input().split()))) if a[0]!=a[1] and a[1]!=a[2]: print(a.count(a[2])) elif a[0]!=a[1] and a[1]==a[2]: t=a.count(a[1]) print((t*(t-1))//2) elif a[0]==a[1] and a[1]!=a[2]: print(a.count(a[2])) else: t=a.count(a[0]) print((t*(t-1)*(t-2))//6)
Educational Codeforces Round 23
ICPC
2,017
2
256
Makes And The Product
After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.
null
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4. In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2. In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
[{"input": "4\n1 1 1 1", "output": "4"}, {"input": "5\n1 3 2 3 4", "output": "2"}, {"input": "6\n1 3 3 1 3 2", "output": "1"}]
1,500
["combinatorics", "implementation", "math", "sortings"]
80
[{"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "2\r\n"}, {"input": "6\r\n1 3 3 1 3 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "1\r\n"}, {"input": "11\r\n1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "45\r\n"}, {"input": "5\r\n1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6\r\n1 2 2 3 3 4\r\n", "output": "1\r\n"}, {"input": "8\r\n1 1 2 2 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 3\r\n", "output": "6\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2 2 2 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "4\r\n1 2 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 2 3\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 3 3 3 3\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 2 2 5 6\r\n", "output": "3\r\n"}, {"input": "10\r\n1 2 2 2 2 2 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "3\r\n2 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2 2 3\r\n", "output": "1\r\n"}, {"input": "10\r\n2 2 2 2 2 1 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "7\r\n2 2 2 3 3 3 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 2 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "9\r\n2 2 3 3 3 3 3 3 3\r\n", "output": "7\r\n"}, {"input": "5\r\n1 1 2 2 3\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 3 3\r\n", "output": "2\r\n"}, {"input": "4\r\n33554432 33554432 67108864 33554432\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 2 1 2 2\r\n", "output": "10\r\n"}, {"input": "10\r\n1 2 1 2 3 2 3 2 2 2\r\n", "output": "6\r\n"}, {"input": "10\r\n9 6 4 7 1 8 9 5 9 4\r\n", "output": "1\r\n"}, {"input": "4\r\n5 7 2 7\r\n", "output": "2\r\n"}, {"input": "3\r\n7 6 7\r\n", "output": "1\r\n"}, {"input": "6\r\n3 2 8 2 5 3\r\n", "output": "2\r\n"}, {"input": "3\r\n5 9 5\r\n", "output": "1\r\n"}, {"input": "5\r\n6 3 7 6 3\r\n", "output": "2\r\n"}, {"input": "9\r\n10 10 4 10 7 9 6 7 3\r\n", "output": "1\r\n"}, {"input": "5\r\n9 10 10 3 8\r\n", "output": "1\r\n"}, {"input": "5\r\n2 9 5 10 5\r\n", "output": "1\r\n"}, {"input": "9\r\n7 1 9 6 6 8 3 1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n3 4 4 4 5\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 3\r\n", "output": "1\r\n"}, {"input": "8\r\n3 2 2 5 2 2 1 2\r\n", "output": "10\r\n"}]
false
stdio
null
true
731/B
731
B
Python 3
TESTS
2
61
0
120234087
n = int(input()) a = list(map(int, input().split())) s = 0; flag = 1 for i in range(n): s += a[i] if a[i] == 0 and s&1: flag = 0; break if flag: print("YES") else: print("NO")
79
155
13,516,800
157823880
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) ans = 'YES' for i in range(n-1): if w[i] % 2: if w[i+1] == 0: ans = "NO" break else: w[i+1] += 1 if w[-1] % 2: ans = "NO" print(ans)
Codeforces Round 376 (Div. 2)
CF
2,016
1
256
Coupons and Discounts
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition. Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day. There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total). As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days. Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.
The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).
null
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.
[{"input": "4\n1 2 1 2", "output": "YES"}, {"input": "3\n1 0 1", "output": "NO"}]
1,100
["constructive algorithms", "greedy"]
79
[{"input": "4\r\n1 2 1 2\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 0 2\r\n", "output": "YES\r\n"}, {"input": "1\r\n179\r\n", "output": "NO\r\n"}, {"input": "10\r\n0 0 5 9 9 3 0 0 0 10\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 3\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "2\r\n0 0\r\n", "output": "YES\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "1\r\n2\r\n", "output": "YES\r\n"}, {"input": "1\r\n3\r\n", "output": "NO\r\n"}, {"input": "1\r\n10000\r\n", "output": "YES\r\n"}, {"input": "2\r\n10000 10000\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "YES\r\n"}, {"input": "10\r\n3 3 3 2 2 2 2 2 2 3\r\n", "output": "YES\r\n"}, {"input": "100\r\n2 3 2 3 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 2 3 2 3 2 2 3 3 3 3 3 2 2 2 2 3 2 3 3 2 2 3 2 3 3 3 3 2 2 3 3 3 3 3 2 3 3 3 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 3 3 3 2 2 2 2 3 2 2 3 3 3 2 2 2 2 2 3 3\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n1 0 1 1 0 1 1 0 1 0\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n8 4 0 0 6 1 9 8 0 6\r\n", "output": "YES\r\n"}, {"input": "100\r\n44 0 0 0 16 0 0 0 0 77 9 0 94 0 78 0 0 50 55 35 0 35 88 27 0 0 86 0 0 56 0 0 17 23 0 22 54 36 0 0 94 36 0 22 0 0 0 0 0 0 0 82 0 0 50 0 6 0 0 44 80 0 0 0 98 0 0 0 0 92 0 56 0 16 0 14 0 37 89 0 62 3 83 0 0 0 80 0 92 58 92 0 0 0 57 79 0 0 0 42\r\n", "output": "YES\r\n"}, {"input": "100\r\n37 92 14 95 3 37 0 0 0 84 27 33 0 0 0 74 74 0 35 72 46 29 8 92 1 76 47 0 38 82 0 81 54 7 61 46 91 0 86 0 80 0 0 98 88 0 4 0 0 52 0 0 82 0 33 35 0 36 58 52 1 50 29 0 0 24 0 69 97 65 13 0 30 0 14 66 47 94 22 24 8 92 67 0 34 0 0 0 84 85 50 33 0 99 67 73 21 0 0 62\r\n", "output": "YES\r\n"}, {"input": "100\r\n56 22 13 79 28 73 16 55 34 0 97 19 22 36 22 80 30 19 36 92 9 38 24 10 61 43 19 12 18 34 21 36 1 17 0 97 72 37 74 70 51 34 33 87 27 33 45 97 38 56 2 32 88 92 64 51 74 94 86 98 57 62 83 3 87 61 9 65 57 13 64 10 50 35 7 75 41 3 70 66 6 55 69 42 91 75 14 22 68 93 2 53 22 98 45 2 78 58 18 13\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 1 1 0 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 0 0\r\n", "output": "NO\r\n"}, {"input": "9\r\n6 3 5 9 0 3 1 9 6\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 5\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 2\r\n", "output": "NO\r\n"}]
false
stdio
null
true
230/A
230
A
PyPy 3-64
TESTS
35
186
1,945,600
232319164
s, n = map(int, input().split()) lst1 = [] lst2 = [] dic = {} for _ in range(n): xi, yi = map(int, input().split()) if xi < s: s += yi else: dic.update({xi : yi}) myKeys = list(dic.keys()) myKeys.sort() sorted_dict = {i: dic[i] for i in myKeys} rval = True for keys in sorted_dict: if keys < s: s+= sorted_dict[keys] else: rval = False print("NO") break if rval: print("YES")
54
62
102,400
232268766
s, n=list(map(int, input().split())) l=[] for i in range(n): a, b=list(map(int, input().split())) l.append([a, b]) l.sort(key = lambda x: x[0]) #print(l) for i in l: a=i[0] b=i[1] if s<=a: print("NO") break elif s>a: s+=b else: print("YES")
Codeforces Round 142 (Div. 2)
CF
2,012
2
256
Dragons
Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s. If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi. Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.
The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon's strength and the bonus for defeating it.
On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't.
null
In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level. In the second sample Kirito's strength is too small to defeat the only dragon and win.
[{"input": "2 2\n1 99\n100 0", "output": "YES"}, {"input": "10 1\n100 100", "output": "NO"}]
1,000
["greedy", "sortings"]
54
[{"input": "2 2\r\n1 99\r\n100 0\r\n", "output": "YES\r\n"}, {"input": "10 1\r\n100 100\r\n", "output": "NO\r\n"}, {"input": "123 2\r\n78 10\r\n130 0\r\n", "output": "YES\r\n"}, {"input": "999 2\r\n1010 10\r\n67 89\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n5 1\r\n2 1\r\n3 1\r\n1 1\r\n4 1\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n3 5\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "1 2\r\n1 0\r\n1 0\r\n", "output": "NO\r\n"}, {"input": "5 10\r\n20 1\r\n4 3\r\n5 1\r\n100 1\r\n4 2\r\n101 1\r\n10 0\r\n10 2\r\n17 3\r\n12 84\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n1 98\r\n100 0\r\n", "output": "NO\r\n"}, {"input": "2 2\r\n1 2\r\n3 5\r\n", "output": "YES\r\n"}, {"input": "5 3\r\n13 20\r\n3 10\r\n15 5\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n1 1\r\n1 2\r\n4 0\r\n", "output": "YES\r\n"}, {"input": "10 4\r\n20 1\r\n3 5\r\n2 4\r\n1 3\r\n", "output": "YES\r\n"}, {"input": "10 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "4 1\r\n100 1000\r\n", "output": "NO\r\n"}, {"input": "5 1\r\n6 7\r\n", "output": "NO\r\n"}, {"input": "10 1\r\n10 10\r\n", "output": "NO\r\n"}, {"input": "6 2\r\n496 0\r\n28 8128\r\n", "output": "NO\r\n"}, {"input": "4 2\r\n2 1\r\n10 3\r\n", "output": "NO\r\n"}, {"input": "11 2\r\n22 0\r\n33 0\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n100 1\r\n100 1\r\n", "output": "NO\r\n"}, {"input": "10 3\r\n12 0\r\n13 0\r\n14 0\r\n", "output": "NO\r\n"}, {"input": "50 3\r\n39 0\r\n38 0\r\n37 0\r\n", "output": "YES\r\n"}, {"input": "14 3\r\n1 5\r\n1 6\r\n1 7\r\n", "output": "YES\r\n"}, {"input": "1 3\r\n1 10\r\n1 11\r\n1 9\r\n", "output": "NO\r\n"}, {"input": "10 10\r\n2 10\r\n3 10\r\n4 10\r\n2 20\r\n3 20\r\n3 20\r\n100 50\r\n100 30\r\n150 30\r\n200 10\r\n", "output": "NO\r\n"}, {"input": "9983 34\r\n6626 5976\r\n4448 3568\r\n2794 2309\r\n3741 8806\r\n4754 129\r\n2780 9275\r\n5785 9243\r\n3915 6159\r\n2609 4331\r\n238 6756\r\n6987 3887\r\n3384 5711\r\n4349 5563\r\n1135 4483\r\n9151 1584\r\n1500 766\r\n1608 4440\r\n7768 5005\r\n7205 2360\r\n9088 2933\r\n3923 7814\r\n7538 9372\r\n7504 165\r\n5277 1636\r\n2061 4384\r\n7668 1422\r\n9582 2121\r\n5483 7967\r\n487 2944\r\n7432 5794\r\n8208 8970\r\n5747 3800\r\n4322 3920\r\n8261 9319\r\n", "output": "YES\r\n"}, {"input": "1 10\r\n8 8\r\n54 3\r\n1 8\r\n26 3\r\n16 1\r\n29 9\r\n38 10\r\n57 8\r\n48 6\r\n17 9\r\n", "output": "NO\r\n"}, {"input": "5 10\r\n7 0\r\n7 0\r\n10 0\r\n10 0\r\n7 2\r\n4 2\r\n9 0\r\n6 1\r\n7 0\r\n7 0\r\n", "output": "NO\r\n"}, {"input": "2 3\r\n1 1\r\n1 10\r\n17 2\r\n", "output": "NO\r\n"}, {"input": "100 5\r\n99 100\r\n199 1\r\n199 1\r\n199 1\r\n202 1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\n10000 1\r\n", "output": "NO\r\n"}]
false
stdio
null
true
216/B
216
B
Python 3
TESTS
23
92
0
211713206
def getAdjacent(node): adjacents=[node,archenemies[node][0],archenemies[node][1]] adjacents.sort() return adjacents n,m=list(map(int,input().split(" "))) archenemies=[[] for i in range(n)] result=0 visitedNodes=[False for k in range(n)] for i in range(m): ai,bi=list(map(int,input().split(" "))) archenemies[ai-1].append(bi-1) archenemies[bi-1].append(ai-1) for i in range(n): if((not visitedNodes[i])&(len(archenemies[i])==2)): lNode=archenemies[i][0] rNode=archenemies[i][1] if((len(archenemies[lNode])==2)&(len(archenemies[rNode])==2)): a1=getAdjacent(i) a2=getAdjacent(archenemies[i][0]) a3=getAdjacent(archenemies[i][1]) if(a1==a2==a3): result+=1 visitedNodes[i]=True visitedNodes[archenemies[i][0]]=True visitedNodes[archenemies[i][1]]=True if((n-result)%2!=0): result+=1 print(result)
56
124
5,632,000
35139089
def readInts(): return map(int, input().split()) def dfs(g, seen, i): if i in seen: return 0,0 seen.add(i) nodes = 1 edges = len(g[i]) for j in g[i]: x, y = dfs(g, seen, j) nodes += x edges += y return nodes, edges def solve(): line0 = [] try: line0 = readInts() except EOFError: return False n, m = line0 g = {} seen = set() for i in range(1,n+1): g[i] = set() for _ in range(m): a,b = readInts() g[a].add ( b ) g[b].add ( a ) ans = 0 for i in range(1,n+1): if i not in seen: nodes, edges = dfs(g,seen,i) if nodes > 1 and nodes%2 == 1 and 2*nodes == edges: ans += 1 if (n-ans)%2 == 1: ans += 1 print ( ans ) return True while solve(): pass
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
204/B
204
B
PyPy 3
TESTS
7
716
9,625,600
83582250
import sys from collections import defaultdict as dd from collections import deque from fractions import Fraction as f def eprint(*args): print(*args, file=sys.stderr) zz=1 from math import * import copy #sys.setrecursionlimit(10**6) if zz: input=sys.stdin.readline else: sys.stdin=open('input.txt', 'r') sys.stdout=open('all.txt','w') def li(): return [int(x) for x in input().split()] def fi(): return int(input()) def si(): return list(input().rstrip()) def mi(): return map(int,input().split()) def gh(): sys.stdout.flush() def bo(i): return ord(i)-ord('a') from copy import * from bisect import * from itertools import permutations t=1 while t>0: t-=1 n=fi() d={} for i in range(n): x,y=mi() if x not in d: d[x]=[0,0] d[x][0]+=1 x=y if x not in d: d[x]=[0,0] d[x][1]+=1 mini=10**18 p=int(ceil(n/2)) for i in d: if d[i][0]+d[i][1]>=p: mini=min(mini,max(0,p-d[i][0])) print(mini if mini<10**18 else -1)
90
1,184
21,299,200
42123854
def main(): n = int(input()) d = {} for i in range(n): a, b = [int(i) for i in input().split()] if a == b: if a not in d: d[a] = [0, 0] d[a][0] += 1 else: if a not in d: d[a] = [0, 0] d[a][0] += 1 if b not in d: d[b] = [0, 0] d[b][1] += 1 result = float("inf") half = (n + 1) // 2 for a, b in d.values(): if a + b >= half: result = min(max(0, half - a), result) if result == float("inf"): print(-1) else: print(result) main() # Made By Mostafa_Khaled
Codeforces Round 129 (Div. 1)
CF
2,012
2
256
Little Elephant and Cards
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces.
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
null
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
[{"input": "3\n4 7\n4 7\n7 4", "output": "0"}, {"input": "5\n4 7\n7 4\n2 11\n9 7\n1 1", "output": "2"}]
1,500
["binary search", "data structures"]
90
[{"input": "3\r\n4 7\r\n4 7\r\n7 4\r\n", "output": "0\r\n"}, {"input": "5\r\n4 7\r\n7 4\r\n2 11\r\n9 7\r\n1 1\r\n", "output": "2\r\n"}, {"input": "1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n7 7\r\n1 2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 1\r\n2 5\r\n3 6\r\n", "output": "-1\r\n"}, {"input": "4\r\n1000000000 1000000000\r\n999999999 1000000000\r\n999999997 999999998\r\n47 74\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2\r\n3 1\r\n4 7\r\n4 1\r\n9 1\r\n7 2\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "0\r\n"}, {"input": "7\r\n4 7\r\n7 4\r\n4 7\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "1\r\n"}, {"input": "10\r\n1000000000 999999999\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "4\r\n"}, {"input": "10\r\n9 1000000000\r\n47 74\r\n47474 75785445\r\n8798878 458445\r\n1 2\r\n888888888 777777777\r\n99999999 1000000000\r\n9999999 1000000000\r\n999999 1000000000\r\n99999 1000000000\r\n", "output": "5\r\n"}, {"input": "10\r\n1 10\r\n1 10\r\n1 1\r\n7 8\r\n6 7\r\n9 5\r\n4 1\r\n2 3\r\n3 10\r\n2 8\r\n", "output": "-1\r\n"}, {"input": "10\r\n262253762 715261903\r\n414831157 8354405\r\n419984358 829693421\r\n376600467 175941985\r\n367533995 350629286\r\n681027822 408529849\r\n654503328 717740407\r\n539773033 704670473\r\n55322828 380422378\r\n46174018 186723478\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "12\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n1 1\r\n1 1\r\n2 3\r\n4 5\r\n6 7\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n47 74\r\n47 85874\r\n", "output": "0\r\n"}, {"input": "5\r\n5 8\r\n9 10\r\n5 17\r\n5 24\r\n1 147\r\n", "output": "0\r\n"}, {"input": "5\r\n1 7\r\n2 7\r\n3 7\r\n4 7\r\n5 7\r\n", "output": "3\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1\r\n3 1\r\n4 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n1 3\r\n4 1\r\n5 1\r\n6 7\r\n", "output": "1\r\n"}, {"input": "5\r\n4 7\r\n4 7\r\n2 7\r\n9 7\r\n1 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2\r\n2 1\r\n2 1\r\n3 1\r\n4 2\r\n5 2\r\n6 2\r\n7 2\r\n", "output": "2\r\n"}, {"input": "3\r\n98751 197502\r\n296253 395004\r\n493755 592506\r\n", "output": "-1\r\n"}, {"input": "5\r\n1 5\r\n2 5\r\n3 5\r\n4 7\r\n2 5\r\n", "output": "3\r\n"}, {"input": "10\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 10\r\n10 1\r\n10 2\r\n10 3\r\n10 4\r\n10 5\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n1 2\r\n1 2\r\n3 1\r\n3 1\r\n3 1\r\n2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 6\r\n5 6\r\n", "output": "3\r\n"}, {"input": "5\r\n1 6\r\n2 6\r\n3 6\r\n4 4\r\n5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1\r\n1 1\r\n2 2\r\n2 2\r\n3 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 5\r\n2 5\r\n3 5\r\n4 4\r\n", "output": "2\r\n"}]
false
stdio
null
true
269/A
269
A
PyPy 3
TESTS
13
1,744
29,184,000
128505122
from math import ceil, log n = int(input()) s = 0 k1 = 0 for _ in range(n): k, a = map(int, input().split()) g = k + ceil(log(a, 4)) if g > s: s = g k1 = k print(max(s, k1 + 1))
46
530
7,577,600
186986462
from collections import defaultdict from sys import stdin input = stdin.readline n = int(input()) ans = 0 for _ in range(n): k, a = [int(x) for x in input().split()] if a == 1: k += 1 while a > 1: a = (a+3)//4 k += 1 ans = max(ans, k) print(ans) # https://blog.csdn.net/qq_41289920/article/details/84205699
Codeforces Round 165 (Div. 1)
CF
2,013
2
256
Magical Boxes
Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes. From the top view each magical box looks like a square with side length equal to 2k (k is an integer, k ≥ 0) units. A magical box v can be put inside a magical box u, if side length of v is strictly less than the side length of u. In particular, Emuskald can put 4 boxes of side length 2k - 1 into one box of side length 2k, or as in the following figure: Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.
The first line of input contains an integer n (1 ≤ n ≤ 105), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers ki and ai (0 ≤ ki ≤ 109, 1 ≤ ai ≤ 109), which means that Emuskald has ai boxes with side length 2ki. It is guaranteed that all of ki are distinct.
Output a single integer p, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2p.
null
Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture. In the second test case, we can put all four small boxes into a box with side length 2.
[{"input": "2\n0 3\n1 5", "output": "3"}, {"input": "1\n0 4", "output": "1"}, {"input": "2\n1 10\n2 2", "output": "3"}]
1,600
["greedy", "math"]
46
[{"input": "2\r\n0 3\r\n1 5\r\n", "output": "3\r\n"}, {"input": "1\r\n0 4\r\n", "output": "1\r\n"}, {"input": "2\r\n1 10\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000 1000000000\r\n", "output": "1000000015\r\n"}, {"input": "1\r\n0 16\r\n", "output": "2\r\n"}, {"input": "1\r\n0 17\r\n", "output": "3\r\n"}, {"input": "2\r\n0 16\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 20\r\n1 18\r\n2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n0 64\r\n1 16\r\n2 4\r\n", "output": "3\r\n"}, {"input": "5\r\n1 1000000\r\n100 100\r\n101 9\r\n102 4\r\n103 8\r\n", "output": "105\r\n"}, {"input": "10\r\n11 69\r\n7 56\r\n8 48\r\n2 56\r\n12 6\r\n9 84\r\n1 81\r\n4 80\r\n3 9\r\n5 18\r\n", "output": "15\r\n"}, {"input": "15\r\n0 268435456\r\n1 67108864\r\n2 16777216\r\n3 4194304\r\n4 1048576\r\n5 262144\r\n6 65536\r\n7 16384\r\n8 4096\r\n9 1024\r\n10 256\r\n11 64\r\n12 16\r\n13 4\r\n14 1\r\n", "output": "15\r\n"}, {"input": "14\r\n0 268435456\r\n1 67108864\r\n2 16777216\r\n3 4194304\r\n4 1048576\r\n5 262144\r\n6 65536\r\n7 16384\r\n8 4096\r\n9 1024\r\n10 256\r\n11 64\r\n12 16\r\n13 4\r\n", "output": "14\r\n"}, {"input": "14\r\n0 268435456\r\n1 67108864\r\n2 16777216\r\n3 4194305\r\n4 1048576\r\n5 262144\r\n6 65536\r\n7 16384\r\n8 4096\r\n9 1024\r\n10 256\r\n11 64\r\n12 16\r\n13 4\r\n", "output": "15\r\n"}, {"input": "14\r\n0 268435457\r\n1 67108865\r\n2 16777217\r\n3 4194305\r\n4 1048577\r\n5 262145\r\n6 65537\r\n7 16383\r\n8 4097\r\n9 1025\r\n10 257\r\n11 65\r\n12 17\r\n13 4\r\n", "output": "15\r\n"}, {"input": "16\r\n1296 2\r\n1568 1\r\n7435 2\r\n3660 1\r\n6863 2\r\n886 2\r\n2596 1\r\n7239 1\r\n6146 1\r\n5634 1\r\n3119 2\r\n1166 2\r\n7610 2\r\n5992 1\r\n630 2\r\n8491 2\r\n", "output": "8492\r\n"}, {"input": "17\r\n8028 11\r\n4011 32\r\n8609 52\r\n1440 25\r\n6752 42\r\n536 47\r\n761 15\r\n2749 60\r\n5363 62\r\n7170 23\r\n9734 10\r\n8487 28\r\n6147 20\r\n5257 54\r\n821 49\r\n7219 19\r\n6150 43\r\n", "output": "9736\r\n"}, {"input": "3\r\n1 4\r\n0 17\r\n2 4\r\n", "output": "3\r\n"}, {"input": "2\r\n0 13\r\n1 1\r\n", "output": "2\r\n"}, {"input": "2\r\n0 2\r\n1 4\r\n", "output": "2\r\n"}, {"input": "2\r\n100 52\r\n101 4\r\n", "output": "103\r\n"}, {"input": "10\r\n1 1\r\n10 1\r\n100 1\r\n1000 1\r\n10000 1\r\n100000 1\r\n1000000 1\r\n10000000 1\r\n100000000 1\r\n1000000000 1\r\n", "output": "1000000001\r\n"}, {"input": "1\r\n0 268435456\r\n", "output": "14\r\n"}, {"input": "1\r\n0 268435457\r\n", "output": "15\r\n"}, {"input": "1\r\n1 1000000000\r\n", "output": "16\r\n"}, {"input": "9\r\n0 268435456\r\n2 16777216\r\n3 4194304\r\n6 65536\r\n7 16384\r\n8 4096\r\n11 64\r\n12 16\r\n14 1\r\n", "output": "15\r\n"}, {"input": "8\r\n0 268435456\r\n2 16777216\r\n3 4194304\r\n6 65536\r\n7 16384\r\n8 4096\r\n11 64\r\n12 16\r\n", "output": "14\r\n"}, {"input": "8\r\n0 268435456\r\n2 16777216\r\n3 4195304\r\n6 65536\r\n7 16384\r\n8 4096\r\n11 64\r\n12 16\r\n", "output": "15\r\n"}, {"input": "7\r\n1 16777216\r\n2 1000000000\r\n5 65537\r\n6 16384\r\n7 4096\r\n10 64\r\n11 16\r\n", "output": "17\r\n"}, {"input": "10\r\n0 689790649\r\n10 494030130\r\n6 210447956\r\n3 993638872\r\n7 116080273\r\n8 802686533\r\n2 677383122\r\n9 936407280\r\n4 593615642\r\n1 853659941\r\n", "output": "25\r\n"}, {"input": "2\r\n0 16777216\r\n16 1\r\n", "output": "17\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2\r\n"}, {"input": "2\r\n21 134217728\r\n1 1024\r\n", "output": "35\r\n"}, {"input": "2\r\n10 3\r\n25 747\r\n", "output": "30\r\n"}]
false
stdio
null
true
731/B
731
B
Python 3
TESTS
5
46
6,963,200
124925542
def solution(ns): for i in range(len(ns)): if i == 0: continue if ns[i] >= 1 and ns[i - 1] >= 1: ns[i] -= 1 ns[i - 1] -= 1 for n in ns: if n % 2 != 0: return "NO" return "YES" if __name__ == "__main__": n = input() ns = list(map(int, input().split(" "))) print(solution(ns))
79
155
20,582,400
122634805
i = int(input()) l = list(map(int,input().split())) c = 0 for x in l: if c == 1: x -= 1 if x < 0: print('NO'); exit() c = x%2 print('NO' if c == 1 else 'YES')
Codeforces Round 376 (Div. 2)
CF
2,016
1
256
Coupons and Discounts
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition. Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day. There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total). As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days. Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.
The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).
null
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.
[{"input": "4\n1 2 1 2", "output": "YES"}, {"input": "3\n1 0 1", "output": "NO"}]
1,100
["constructive algorithms", "greedy"]
79
[{"input": "4\r\n1 2 1 2\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 0 2\r\n", "output": "YES\r\n"}, {"input": "1\r\n179\r\n", "output": "NO\r\n"}, {"input": "10\r\n0 0 5 9 9 3 0 0 0 10\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 3\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "2\r\n0 0\r\n", "output": "YES\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "1\r\n2\r\n", "output": "YES\r\n"}, {"input": "1\r\n3\r\n", "output": "NO\r\n"}, {"input": "1\r\n10000\r\n", "output": "YES\r\n"}, {"input": "2\r\n10000 10000\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "YES\r\n"}, {"input": "10\r\n3 3 3 2 2 2 2 2 2 3\r\n", "output": "YES\r\n"}, {"input": "100\r\n2 3 2 3 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 2 3 2 3 2 2 3 3 3 3 3 2 2 2 2 3 2 3 3 2 2 3 2 3 3 3 3 2 2 3 3 3 3 3 2 3 3 3 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 3 3 3 2 2 2 2 3 2 2 3 3 3 2 2 2 2 2 3 3\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n1 0 1 1 0 1 1 0 1 0\r\n", "output": "NO\r\n"}, {"input": "100\r\n1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "10\r\n8 4 0 0 6 1 9 8 0 6\r\n", "output": "YES\r\n"}, {"input": "100\r\n44 0 0 0 16 0 0 0 0 77 9 0 94 0 78 0 0 50 55 35 0 35 88 27 0 0 86 0 0 56 0 0 17 23 0 22 54 36 0 0 94 36 0 22 0 0 0 0 0 0 0 82 0 0 50 0 6 0 0 44 80 0 0 0 98 0 0 0 0 92 0 56 0 16 0 14 0 37 89 0 62 3 83 0 0 0 80 0 92 58 92 0 0 0 57 79 0 0 0 42\r\n", "output": "YES\r\n"}, {"input": "100\r\n37 92 14 95 3 37 0 0 0 84 27 33 0 0 0 74 74 0 35 72 46 29 8 92 1 76 47 0 38 82 0 81 54 7 61 46 91 0 86 0 80 0 0 98 88 0 4 0 0 52 0 0 82 0 33 35 0 36 58 52 1 50 29 0 0 24 0 69 97 65 13 0 30 0 14 66 47 94 22 24 8 92 67 0 34 0 0 0 84 85 50 33 0 99 67 73 21 0 0 62\r\n", "output": "YES\r\n"}, {"input": "100\r\n56 22 13 79 28 73 16 55 34 0 97 19 22 36 22 80 30 19 36 92 9 38 24 10 61 43 19 12 18 34 21 36 1 17 0 97 72 37 74 70 51 34 33 87 27 33 45 97 38 56 2 32 88 92 64 51 74 94 86 98 57 62 83 3 87 61 9 65 57 13 64 10 50 35 7 75 41 3 70 66 6 55 69 42 91 75 14 22 68 93 2 53 22 98 45 2 78 58 18 13\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 1 1 0 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 0 0\r\n", "output": "NO\r\n"}, {"input": "9\r\n6 3 5 9 0 3 1 9 6\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 0 1 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n1 5\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 2\r\n", "output": "NO\r\n"}]
false
stdio
null
true
116/A
116
A
PyPy 3-64
TESTS
9
92
0
223053085
n = int(input()) cap = 0 tram = 0 for i in range(1,n+1): x,y = map(int, input().split()) if y == 0: break tram += - x + y if tram < cap: pass else: cap = tram print(cap)
71
92
0
208037159
n = int(input()) ans = 0 ans_1 = [] for i in range(n): ext,ent = map(int,input().split(" ")) ans = (ans-ext)+ent ans_1.insert(0,ans) print(max(ans_1))
Codeforces Beta Round 87 (Div. 2 Only)
CF
2,011
2
256
Tram
Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty. Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.
The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops. Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement. - The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, $$$$. This particularly means that a1 = 0. - At the last stop, all the passengers exit the tram and it becomes empty. More formally, $$\sum_{j=1}^{n-1} b_j - \sum_{j=1}^{n-1} a_j = a_n$$. - No passenger will enter the train at the last stop. That is, bn = 0.
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).
null
For the first example, a capacity of 6 is sufficient: - At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3. - At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now. - At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now. - Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints. Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.
[{"input": "4\n0 3\n2 5\n4 2\n4 0", "output": "6"}]
800
["implementation"]
71
[{"input": "4\r\n0 3\r\n2 5\r\n4 2\r\n4 0\r\n", "output": "6\r\n"}, {"input": "5\r\n0 4\r\n4 6\r\n6 5\r\n5 4\r\n4 0\r\n", "output": "6\r\n"}, {"input": "10\r\n0 5\r\n1 7\r\n10 8\r\n5 3\r\n0 5\r\n3 3\r\n8 8\r\n0 6\r\n10 1\r\n9 0\r\n", "output": "18\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "4\r\n0 1\r\n0 1\r\n1 0\r\n1 0\r\n", "output": "2\r\n"}, {"input": "3\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 1000\r\n1000 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "5\r\n0 73\r\n73 189\r\n189 766\r\n766 0\r\n0 0\r\n", "output": "766\r\n"}, {"input": "5\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "5\r\n0 917\r\n917 923\r\n904 992\r\n1000 0\r\n11 0\r\n", "output": "1011\r\n"}, {"input": "5\r\n0 1\r\n1 2\r\n2 1\r\n1 2\r\n2 0\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "20\r\n0 7\r\n2 1\r\n2 2\r\n5 7\r\n2 6\r\n6 10\r\n2 4\r\n0 4\r\n7 4\r\n8 0\r\n10 6\r\n2 1\r\n6 1\r\n1 7\r\n0 3\r\n8 7\r\n6 3\r\n6 3\r\n1 1\r\n3 0\r\n", "output": "22\r\n"}, {"input": "5\r\n0 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "10\r\n0 592\r\n258 598\r\n389 203\r\n249 836\r\n196 635\r\n478 482\r\n994 987\r\n1000 0\r\n769 0\r\n0 0\r\n", "output": "1776\r\n"}, {"input": "10\r\n0 1\r\n1 0\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n1 1\r\n0 1\r\n1 0\r\n1 0\r\n", "output": "2\r\n"}, {"input": "10\r\n0 926\r\n926 938\r\n938 931\r\n931 964\r\n937 989\r\n983 936\r\n908 949\r\n997 932\r\n945 988\r\n988 0\r\n", "output": "1016\r\n"}, {"input": "10\r\n0 1\r\n1 2\r\n1 2\r\n2 2\r\n2 2\r\n2 2\r\n1 1\r\n1 1\r\n2 1\r\n2 0\r\n", "output": "3\r\n"}, {"input": "10\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n0 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "50\r\n0 332\r\n332 268\r\n268 56\r\n56 711\r\n420 180\r\n160 834\r\n149 341\r\n373 777\r\n763 93\r\n994 407\r\n86 803\r\n700 132\r\n471 608\r\n429 467\r\n75 5\r\n638 305\r\n405 853\r\n316 478\r\n643 163\r\n18 131\r\n648 241\r\n241 766\r\n316 847\r\n640 380\r\n923 759\r\n789 41\r\n125 421\r\n421 9\r\n9 388\r\n388 829\r\n408 108\r\n462 856\r\n816 411\r\n518 688\r\n290 7\r\n405 912\r\n397 772\r\n396 652\r\n394 146\r\n27 648\r\n462 617\r\n514 433\r\n780 35\r\n710 705\r\n460 390\r\n194 508\r\n643 56\r\n172 469\r\n1000 0\r\n194 0\r\n", "output": "2071\r\n"}, {"input": "50\r\n0 0\r\n0 1\r\n1 1\r\n0 1\r\n0 0\r\n1 0\r\n0 0\r\n1 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n0 0\r\n0 0\r\n0 1\r\n1 0\r\n0 1\r\n0 0\r\n1 1\r\n1 0\r\n0 1\r\n0 0\r\n1 1\r\n0 1\r\n1 0\r\n1 1\r\n1 0\r\n0 0\r\n1 1\r\n1 0\r\n0 1\r\n0 0\r\n0 1\r\n1 1\r\n1 1\r\n1 1\r\n1 0\r\n1 1\r\n1 0\r\n0 1\r\n1 0\r\n0 0\r\n0 1\r\n1 1\r\n1 1\r\n0 1\r\n0 0\r\n1 0\r\n1 0\r\n", "output": "3\r\n"}, {"input": "50\r\n0 926\r\n926 971\r\n915 980\r\n920 965\r\n954 944\r\n928 952\r\n955 980\r\n916 980\r\n906 935\r\n944 913\r\n905 923\r\n912 922\r\n965 934\r\n912 900\r\n946 930\r\n931 983\r\n979 905\r\n925 969\r\n924 926\r\n910 914\r\n921 977\r\n934 979\r\n962 986\r\n942 909\r\n976 903\r\n982 982\r\n991 941\r\n954 929\r\n902 980\r\n947 983\r\n919 924\r\n917 943\r\n916 905\r\n907 913\r\n964 977\r\n984 904\r\n905 999\r\n950 970\r\n986 906\r\n993 970\r\n960 994\r\n963 983\r\n918 986\r\n980 900\r\n931 986\r\n993 997\r\n941 909\r\n907 909\r\n1000 0\r\n278 0\r\n", "output": "1329\r\n"}, {"input": "2\r\n0 863\r\n863 0\r\n", "output": "863\r\n"}, {"input": "50\r\n0 1\r\n1 2\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n1 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n2 1\r\n2 2\r\n1 2\r\n2 2\r\n1 2\r\n2 1\r\n2 1\r\n2 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 1\r\n2 2\r\n1 1\r\n2 1\r\n2 2\r\n2 1\r\n1 2\r\n2 2\r\n1 2\r\n1 1\r\n1 1\r\n2 1\r\n2 1\r\n2 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 0\r\n2 0\r\n2 0\r\n0 0\r\n", "output": "8\r\n"}, {"input": "50\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "100\r\n0 1\r\n0 0\r\n0 0\r\n1 0\r\n0 0\r\n0 1\r\n0 1\r\n1 1\r\n0 0\r\n0 0\r\n1 1\r\n0 0\r\n1 1\r\n0 1\r\n1 1\r\n0 1\r\n1 1\r\n1 0\r\n1 0\r\n0 0\r\n1 0\r\n0 1\r\n1 0\r\n0 0\r\n0 0\r\n1 1\r\n1 1\r\n0 1\r\n0 0\r\n1 0\r\n1 1\r\n0 1\r\n1 0\r\n1 1\r\n0 1\r\n1 1\r\n1 0\r\n0 0\r\n0 0\r\n0 1\r\n0 0\r\n0 1\r\n1 1\r\n0 0\r\n1 1\r\n1 1\r\n0 0\r\n0 1\r\n1 0\r\n0 1\r\n0 0\r\n0 1\r\n0 1\r\n1 1\r\n1 1\r\n1 1\r\n0 0\r\n0 0\r\n1 1\r\n0 1\r\n0 1\r\n1 0\r\n0 0\r\n0 0\r\n1 1\r\n0 1\r\n0 1\r\n1 1\r\n1 1\r\n0 1\r\n1 1\r\n1 1\r\n0 0\r\n1 0\r\n0 1\r\n0 0\r\n0 0\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n0 1\r\n1 0\r\n1 0\r\n1 0\r\n1 0\r\n1 0\r\n0 0\r\n1 0\r\n1 0\r\n0 0\r\n1 0\r\n0 0\r\n0 1\r\n1 0\r\n0 1\r\n1 0\r\n1 0\r\n1 0\r\n1 0\r\n", "output": "11\r\n"}, {"input": "100\r\n0 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 2\r\n1 1\r\n1 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 2\r\n2 2\r\n2 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 1\r\n1 1\r\n2 2\r\n1 2\r\n2 2\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n1 1\r\n2 2\r\n1 1\r\n2 1\r\n1 1\r\n2 2\r\n2 1\r\n1 2\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n1 1\r\n2 1\r\n1 1\r\n2 1\r\n1 1\r\n1 2\r\n2 2\r\n2 2\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n2 1\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n1 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 2\r\n2 2\r\n2 2\r\n2 1\r\n2 2\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n2 2\r\n2 2\r\n1 1\r\n2 1\r\n1 1\r\n1 2\r\n1 2\r\n1 2\r\n1 1\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 1\r\n2 2\r\n1 2\r\n2 0\r\n2 0\r\n2 0\r\n1 0\r\n", "output": "7\r\n"}, {"input": "100\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "2\r\n0 900\r\n900 0\r\n", "output": "900\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "3\r\n0 802\r\n175 188\r\n815 0\r\n", "output": "815\r\n"}, {"input": "3\r\n0 910\r\n910 976\r\n976 0\r\n", "output": "976\r\n"}, {"input": "3\r\n0 2\r\n2 1\r\n1 0\r\n", "output": "2\r\n"}]
false
stdio
null
true
725/A
725
A
Python 3
TESTS
6
62
409,600
22100654
n=int(input()) s=input() k=s.find('><') while k!=-1: s=s[:k]+s[k+2:] k=s.find('><') print(len(s))
78
46
512,000
172754438
n=int(input()) s=input().lstrip("<").rstrip(">") print(n-len(s))
Canada Cup 2016
CF
2,016
2
256
Jumping Ball
In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i + 1) if the type of this bumper is '>', or one position to the left (to i - 1) if the type of the bumper at position i is '<'. If there is no such position, in other words if i - 1 < 1 or i + 1 > n, the ball falls from the game field. Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.
Print one integer — the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.
null
In the first sample, the ball will fall from the field if starts at position 1 or position 2. In the second sample, any starting position will result in the ball falling from the field.
[{"input": "4\n<<><", "output": "2"}, {"input": "5\n>>>>>", "output": "5"}, {"input": "4\n>><<", "output": "0"}]
1,000
["implementation"]
78
[{"input": "4\r\n<<><\r\n", "output": "2\r\n"}, {"input": "5\r\n>>>>>\r\n", "output": "5\r\n"}, {"input": "4\r\n>><<\r\n", "output": "0\r\n"}, {"input": "3\r\n<<>\r\n", "output": "3\r\n"}, {"input": "3\r\n<<<\r\n", "output": "3\r\n"}, {"input": "3\r\n><<\r\n", "output": "0\r\n"}, {"input": "1\r\n<\r\n", "output": "1\r\n"}, {"input": "2\r\n<>\r\n", "output": "2\r\n"}, {"input": "3\r\n<>>\r\n", "output": "3\r\n"}, {"input": "3\r\n><>\r\n", "output": "1\r\n"}, {"input": "2\r\n><\r\n", "output": "0\r\n"}, {"input": "2\r\n>>\r\n", "output": "2\r\n"}, {"input": "2\r\n<<\r\n", "output": "2\r\n"}, {"input": "1\r\n>\r\n", "output": "1\r\n"}, {"input": "3\r\n>><\r\n", "output": "0\r\n"}, {"input": "3\r\n>>>\r\n", "output": "3\r\n"}, {"input": "3\r\n<><\r\n", "output": "1\r\n"}, {"input": "10\r\n<<<><<<>>>\r\n", "output": "6\r\n"}, {"input": "20\r\n><><<><<<>>>>>>>>>>>\r\n", "output": "11\r\n"}, {"input": "20\r\n<<<<<<<<<<><<<<>>>>>\r\n", "output": "15\r\n"}, {"input": "50\r\n<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>\r\n", "output": "50\r\n"}, {"input": "100\r\n<<<<<<<<<<<<<<<<<<<<<<<<>><<>><<<<<>><>><<<>><><<>>><<>>><<<<><><><<><<<<><>>>>>>>>>>>>>>>>>>>>>>>>>\r\n", "output": "49\r\n"}, {"input": "100\r\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>><<>><>><>><<><><><><>>>><><<<>>>><<<>>>>>>><><\r\n", "output": "50\r\n"}, {"input": "100\r\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n", "output": "100\r\n"}, {"input": "100\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n", "output": "100\r\n"}, {"input": "12\r\n<<>><<>><<>>\r\n", "output": "4\r\n"}, {"input": "6\r\n<<><>>\r\n", "output": "4\r\n"}, {"input": "6\r\n><>>>>\r\n", "output": "4\r\n"}, {"input": "8\r\n>>>><<<>\r\n", "output": "1\r\n"}, {"input": "4\r\n<><>\r\n", "output": "2\r\n"}, {"input": "4\r\n><><\r\n", "output": "0\r\n"}, {"input": "7\r\n<<>>><>\r\n", "output": "3\r\n"}, {"input": "10\r\n><><>>>>>>\r\n", "output": "6\r\n"}, {"input": "5\r\n<><>>\r\n", "output": "3\r\n"}, {"input": "12\r\n<><<<<>>>>>>\r\n", "output": "7\r\n"}, {"input": "6\r\n<>><<>\r\n", "output": "2\r\n"}, {"input": "6\r\n>>><>>\r\n", "output": "2\r\n"}, {"input": "10\r\n><><>>>><>\r\n", "output": "1\r\n"}, {"input": "5\r\n><>>>\r\n", "output": "3\r\n"}, {"input": "5\r\n<<><>\r\n", "output": "3\r\n"}, {"input": "5\r\n<><<<\r\n", "output": "1\r\n"}, {"input": "4\r\n<><<\r\n", "output": "1\r\n"}, {"input": "8\r\n<<>><<>>\r\n", "output": "4\r\n"}, {"input": "7\r\n<<><>>>\r\n", "output": "5\r\n"}, {"input": "5\r\n><<>>\r\n", "output": "2\r\n"}, {"input": "10\r\n<<<<<>>>>>\r\n", "output": "10\r\n"}, {"input": "6\r\n><<<<<\r\n", "output": "0\r\n"}, {"input": "8\r\n<<><><>>\r\n", "output": "4\r\n"}, {"input": "10\r\n<<<<><<<><\r\n", "output": "4\r\n"}, {"input": "12\r\n<<<>>>><<>>>\r\n", "output": "6\r\n"}, {"input": "4\r\n><>>\r\n", "output": "2\r\n"}, {"input": "11\r\n<<><<>><<>>\r\n", "output": "4\r\n"}]
false
stdio
null
true
552/E
552
E
PyPy 3
TESTS
5
140
0
95329205
s=input() ans=0; n=len(s) for i,x in enumerate(s): if x=='*': for j in range(0,i,2): s2=f"{s[:j]}({s[j:i]}){s[i:]}" ans=max(ans,eval(s2)) for j in range(i+2,n+1,2): s2=f"{s[:i+1]}({s[i+1:j]}){s[j:]}" ans=max(ans,eval(s2)) print(ans)
69
327
2,662,400
211865059
def main(): s = input() max_res = eval(s) data = s.split('*') for i in range(len(data)): old_i = data[i] data[i] = f"({data[i]}" for j in range(i, len(data)): old_j = data[j] data[j] = f"{data[j]})" max_res = max(max_res, eval('*'.join(data))) data[j] = old_j data[i] = old_i print(max_res) main()
Codeforces Round 308 (Div. 2)
CF
2,015
1
256
Vanya and Brackets
Vanya is doing his maths homework. He has an expression of form $$x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$$, where x1, x2, ..., xn are digits from 1 to 9, and sign $$\text{ }$$ represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * . The number of signs * doesn't exceed 15.
In the first line print the maximum possible value of an expression.
null
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303. Note to the second sample test. (2 + 3) * 5 = 25. Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
[{"input": "3+5*7+8*4", "output": "303"}, {"input": "2+3*5", "output": "25"}, {"input": "3*4*5", "output": "60"}]
2,100
["brute force", "dp", "expression parsing", "greedy", "implementation", "strings"]
69
[{"input": "3+5*7+8*4\r\n", "output": "303\r\n"}, {"input": "2+3*5\r\n", "output": "25\r\n"}, {"input": "3*4*5\r\n", "output": "60\r\n"}, {"input": "5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5\r\n", "output": "152587890625\r\n"}, {"input": "2*2+2*2\r\n", "output": "16\r\n"}, {"input": "1+1+1+1+1+1+1\r\n", "output": "7\r\n"}, {"input": "1+5*6+7*8\r\n", "output": "521\r\n"}, {"input": "9*8+7*6+5*4+3*2+1\r\n", "output": "1987\r\n"}, {"input": "3*3*9+4+6+8*4+5+1*4*6\r\n", "output": "12312\r\n"}, {"input": "4*9+4+5+8*4+6+9+8+2+5+2+5*7+6+8\r\n", "output": "2450\r\n"}, {"input": "9+9+9*9*9*9+9+9\r\n", "output": "19701\r\n"}, {"input": "9+9+9+9+9*9*9*9\r\n", "output": "32805\r\n"}, {"input": "1*1*1*1*1*1*1*1+1*1*1*1*1*1*1*1\r\n", "output": "2\r\n"}, {"input": "4+2*7+8+9*6+6*9+8+7*2+4\r\n", "output": "1380\r\n"}, {"input": "5\r\n", "output": "5\r\n"}, {"input": "4+6*7+4\r\n", "output": "74\r\n"}, {"input": "2+7+3+5+4+2+3+9+9+6+9+2+3+6+5*3+4+5+6+5+8\r\n", "output": "253\r\n"}, {"input": "3+2+2+3+7+1+9+1+6+8+3+2+2+6+7+2+8+8+1+4+9\r\n", "output": "94\r\n"}, {"input": "3+9+3+1+6+4+7+9+5+8+2+6+1+4+4+5+1+7+5+4+6+4+3+1*9+7+7+4+5+2+3+2+6+5+5+8+7+8+2+3*3+8+3+4+9+8*5+9+2+8+2+8+6+6+9+6+4+2+5+3+1+2+6+6+2+4+6+4+2+7+2+7+6+9+9+3+6+7+8+3+3+2+3+7+9+7+8+5+5+5*1+3+2+5+8+5*6+5+4*6+2+5+5+4+9+8+3+5+1+3+1+6+2+2+1+3+2+3+3+3+2+8+3+2+8+8+5+2+6+6+3+1+1+5+5*1+5+7+5+8+4+1*7+5+9+5+8+1*8+5+9+3+1+8+6+7+8+3+5+1+5+6+9*9+6+1+9+8+9+1+5+9+9+6+3+8+8+6*9*3+9+7+7+4+3+8+8+6+7+1+8+6+3+1+7+7+1+1+3+9+8+5+5+6+8+2+4+1+5+7+2+3+7+1+5+1+6+1+7+3*5+5+9+2+1+3+9+4+8+6+5+5+2+3+7+9+5+6+8+3*3+2+4+4+6+3+2+4+1+4+8\r\n", "output": "162353\r\n"}, {"input": "1*5*1+8*2*6*5*3*9+3+8+2+9*5+7+2+9+5+1*3+2*2*3*4*2*3\r\n", "output": "19699205\r\n"}, {"input": "4+4+6+2+5+9+9+5+5+9+4+1*5+3+6+9+6+2+4+3+2+8+9*6+5+4+3+8+7+3+2*3+1+6+8+3+8+1+8+2+1+1+1+6+9+6+4+6+7+8+3+1+5+4+8+8+6+5+8+7+7+1+7+6+3+3+9+6+3+5+4+4+1+4+1+8+6+2+9+8+7+2+3+1+4+3+9+9+2*1+3+8+2+4+1+8+9+3*7+3+7+5+3+7+5+5+3+2+9+8+4+7+5+3+7+7+3+8+9+4+9+6*6+3+8+8*7+7+9+1+3+5+1+1+1+9+8+2+1+1+5+5+5+1+6+7+3+6+1+4+1+7+1+7+1+1+9+9*4+1+3+9+3+5+5+5+5+2+9+6+7+3+5+9+3+5+3+9+3+9+9+2+7+2+1*4+6*2+5+7+6+1+1+2+8+9+5+8+3+9+9+1+1+4+9+7+5+8*9+5+2+6+5+6*2+4+2+5+2+3+9+6+9+5+5+5*6+8+2+3+1+2+8+3+1+6+5+9+7+4+2+8+9+1+5+8+5+3+2+7+1\r\n", "output": "82140\r\n"}, {"input": "6*9+9*5*5+1*2*9*9*1+4*8+8+9+5+6*5*6+4+2+2+1+5*5*7*8\r\n", "output": "11294919\r\n"}, {"input": "5+3+5+9+3+9+1+3+1*7+7+1+9+3+7+7+6+6+3+7+4+3+6+4+5+1+2*3+6*5+5+6+2+8+3+3+9+9+1+1+2+8+4+8+9+3*7+3+2*8+9+8+1*9+9+7+4+8+6+7+3+5+6+4+4+9+2+2+8+6+7+1+5+4+4+6+6+6+9+8+7+2+3+5+4+6+1+8+8+9+1+9+6+3+8+5*7+3+1+6+7+9+1+6+2+2+8+8+9+3+7+7+2+5+8+6+7+9+7+2+4+9+8+3+7+4+5+7+6+5*6+4+6+4+6+2+2*6+2+5+5+1+8+7+7+6+6+8+2+8+8+6+7+1+1+1+2+5+1+1+8+9+9+6+5+8+7+5+8+4+8+8+1+4+6+7+3+2*1+1+3+5+3+3+3+9+8+7*2+4+7+5+8+3+3+9+3+7+2+1+1+7+6+2+5+5+2+1+8+8+2+9+9+2+4+6+6+4+8+9+3+7+1+3*9+8+7+4+9+4+6+2+9+8+8+5+8+8+2+5+6+6+4+7+9+4+7+2+3+1+7\r\n", "output": "58437\r\n"}, {"input": "2+7+8*8*7+1+3+6*5*3*7*3*2+8+5*1+5*5+9*6+6*5+1*3+8+5\r\n", "output": "1473847\r\n"}, {"input": "1+2+4+8+6+5+3+8+2+9+9+5+8+7+7+7+6+1+7+2+8+3+2+5+1+6+1+3+8+2+5+4+3+5+7+8+5+7+7+3+8+1+7+1+1+1+5+9+5+9+1+6+7+6+8+9+2+7+9+2+9+9+7+3+2+8+4+4+5+9+6+2+6+8+1+3+5+3+9+4+7+4+3+9+8+2+6+3+5+1*3+1+6+8+5+3+9+2+9+9+3+4+8*6+3+9+7+1+1+4+6+4+5*6*1+1*9+6+5+4+3+7+3+8+6+2+3+7+4+1+5+8+6+1+6+9+1+2+7+2+2+1+7+9+4+3+1+4+3+3+1+1+2+1+8+9+8+6+9+9+6+3+7*1+1+3+7+9+3+6+5+2*9+8+1+9+8+7+5+3+6+9+3+5+3+5+5+7+5+2*9+9+2+4+2+3+7+1+7+1+3+8+6+4+5+9+3*2+8+6+8+2*6+8+1+4+2+7+7+6+8+3+2+5+8+1+8+5+6+1+6+4+6+8+6+6+4+3+5+2+1+5+9+9+4+4*9+7+8+4+4\r\n", "output": "178016\r\n"}, {"input": "8+3*6*9*6+5*1*8*2+1+9+2+1*3*2+9+5+4+3+1+3*9*6*8+4+1\r\n", "output": "9027949\r\n"}, {"input": "1*1*1*1*1*1*1*1*1*1*1*1\r\n", "output": "1\r\n"}, {"input": "5+5*5+5*5+5*5+5\r\n", "output": "885\r\n"}, {"input": "8+7+3+6+3*8+8+9+8+4+2\r\n", "output": "247\r\n"}, {"input": "7+8*4+9+5+3+2+3+3+2+9\r\n", "output": "327\r\n"}, {"input": "1+1+7+1+7+7*7+5+3*9+3\r\n", "output": "965\r\n"}, {"input": "9+6+9+7+8*2*9+8+6+7+5\r\n", "output": "728\r\n"}, {"input": "8+8*3*8+1+9*4+9+2+8+4\r\n", "output": "1759\r\n"}, {"input": "3+5+5+2+2+9*7+7+7*2*2\r\n", "output": "773\r\n"}, {"input": "6+8+5+9*2+7*9*3+2*2+8\r\n", "output": "3501\r\n"}, {"input": "2*3+9+6*5*8+2+9*6+3+9\r\n", "output": "3447\r\n"}, {"input": "7+7*6+7+6*1+8+8*1*2*4\r\n", "output": "1967\r\n"}, {"input": "3+2*5+9+5*2+5*5*7+9*2\r\n", "output": "2051\r\n"}, {"input": "3+4*5+6\r\n", "output": "47\r\n"}]
false
stdio
null
true
116/A
116
A
Python 3
TESTS
4
62
0
222116317
n = int(input()) max_size = 0 pas_in = 0 for i in range(n): line = list(input().split()) if pas_in == 0: pas_in = int(line[1]) max_size = pas_in else: pas_in -= int(line[0]) pas_in += int(line[1]) if max_size < pas_in: max_size = pas_in print(max_size)
71
92
0
208037222
n=int(input()) cap=0 max=0 for i in range(n): a,b=map(int,input().split()) cap=cap-a+b if cap>max: max=cap print(max)
Codeforces Beta Round 87 (Div. 2 Only)
CF
2,011
2
256
Tram
Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty. Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.
The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops. Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement. - The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, $$$$. This particularly means that a1 = 0. - At the last stop, all the passengers exit the tram and it becomes empty. More formally, $$\sum_{j=1}^{n-1} b_j - \sum_{j=1}^{n-1} a_j = a_n$$. - No passenger will enter the train at the last stop. That is, bn = 0.
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).
null
For the first example, a capacity of 6 is sufficient: - At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3. - At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now. - At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now. - Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints. Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.
[{"input": "4\n0 3\n2 5\n4 2\n4 0", "output": "6"}]
800
["implementation"]
71
[{"input": "4\r\n0 3\r\n2 5\r\n4 2\r\n4 0\r\n", "output": "6\r\n"}, {"input": "5\r\n0 4\r\n4 6\r\n6 5\r\n5 4\r\n4 0\r\n", "output": "6\r\n"}, {"input": "10\r\n0 5\r\n1 7\r\n10 8\r\n5 3\r\n0 5\r\n3 3\r\n8 8\r\n0 6\r\n10 1\r\n9 0\r\n", "output": "18\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "4\r\n0 1\r\n0 1\r\n1 0\r\n1 0\r\n", "output": "2\r\n"}, {"input": "3\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 1000\r\n1000 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "5\r\n0 73\r\n73 189\r\n189 766\r\n766 0\r\n0 0\r\n", "output": "766\r\n"}, {"input": "5\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "5\r\n0 917\r\n917 923\r\n904 992\r\n1000 0\r\n11 0\r\n", "output": "1011\r\n"}, {"input": "5\r\n0 1\r\n1 2\r\n2 1\r\n1 2\r\n2 0\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "20\r\n0 7\r\n2 1\r\n2 2\r\n5 7\r\n2 6\r\n6 10\r\n2 4\r\n0 4\r\n7 4\r\n8 0\r\n10 6\r\n2 1\r\n6 1\r\n1 7\r\n0 3\r\n8 7\r\n6 3\r\n6 3\r\n1 1\r\n3 0\r\n", "output": "22\r\n"}, {"input": "5\r\n0 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "10\r\n0 592\r\n258 598\r\n389 203\r\n249 836\r\n196 635\r\n478 482\r\n994 987\r\n1000 0\r\n769 0\r\n0 0\r\n", "output": "1776\r\n"}, {"input": "10\r\n0 1\r\n1 0\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n1 1\r\n0 1\r\n1 0\r\n1 0\r\n", "output": "2\r\n"}, {"input": "10\r\n0 926\r\n926 938\r\n938 931\r\n931 964\r\n937 989\r\n983 936\r\n908 949\r\n997 932\r\n945 988\r\n988 0\r\n", "output": "1016\r\n"}, {"input": "10\r\n0 1\r\n1 2\r\n1 2\r\n2 2\r\n2 2\r\n2 2\r\n1 1\r\n1 1\r\n2 1\r\n2 0\r\n", "output": "3\r\n"}, {"input": "10\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n0 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "50\r\n0 332\r\n332 268\r\n268 56\r\n56 711\r\n420 180\r\n160 834\r\n149 341\r\n373 777\r\n763 93\r\n994 407\r\n86 803\r\n700 132\r\n471 608\r\n429 467\r\n75 5\r\n638 305\r\n405 853\r\n316 478\r\n643 163\r\n18 131\r\n648 241\r\n241 766\r\n316 847\r\n640 380\r\n923 759\r\n789 41\r\n125 421\r\n421 9\r\n9 388\r\n388 829\r\n408 108\r\n462 856\r\n816 411\r\n518 688\r\n290 7\r\n405 912\r\n397 772\r\n396 652\r\n394 146\r\n27 648\r\n462 617\r\n514 433\r\n780 35\r\n710 705\r\n460 390\r\n194 508\r\n643 56\r\n172 469\r\n1000 0\r\n194 0\r\n", "output": "2071\r\n"}, {"input": "50\r\n0 0\r\n0 1\r\n1 1\r\n0 1\r\n0 0\r\n1 0\r\n0 0\r\n1 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n0 0\r\n0 0\r\n0 1\r\n1 0\r\n0 1\r\n0 0\r\n1 1\r\n1 0\r\n0 1\r\n0 0\r\n1 1\r\n0 1\r\n1 0\r\n1 1\r\n1 0\r\n0 0\r\n1 1\r\n1 0\r\n0 1\r\n0 0\r\n0 1\r\n1 1\r\n1 1\r\n1 1\r\n1 0\r\n1 1\r\n1 0\r\n0 1\r\n1 0\r\n0 0\r\n0 1\r\n1 1\r\n1 1\r\n0 1\r\n0 0\r\n1 0\r\n1 0\r\n", "output": "3\r\n"}, {"input": "50\r\n0 926\r\n926 971\r\n915 980\r\n920 965\r\n954 944\r\n928 952\r\n955 980\r\n916 980\r\n906 935\r\n944 913\r\n905 923\r\n912 922\r\n965 934\r\n912 900\r\n946 930\r\n931 983\r\n979 905\r\n925 969\r\n924 926\r\n910 914\r\n921 977\r\n934 979\r\n962 986\r\n942 909\r\n976 903\r\n982 982\r\n991 941\r\n954 929\r\n902 980\r\n947 983\r\n919 924\r\n917 943\r\n916 905\r\n907 913\r\n964 977\r\n984 904\r\n905 999\r\n950 970\r\n986 906\r\n993 970\r\n960 994\r\n963 983\r\n918 986\r\n980 900\r\n931 986\r\n993 997\r\n941 909\r\n907 909\r\n1000 0\r\n278 0\r\n", "output": "1329\r\n"}, {"input": "2\r\n0 863\r\n863 0\r\n", "output": "863\r\n"}, {"input": "50\r\n0 1\r\n1 2\r\n2 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n1 2\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n2 1\r\n2 2\r\n1 2\r\n2 2\r\n1 2\r\n2 1\r\n2 1\r\n2 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n1 1\r\n2 2\r\n1 1\r\n2 1\r\n2 2\r\n2 1\r\n1 2\r\n2 2\r\n1 2\r\n1 1\r\n1 1\r\n2 1\r\n2 1\r\n2 2\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 2\r\n1 2\r\n2 0\r\n2 0\r\n2 0\r\n0 0\r\n", "output": "8\r\n"}, {"input": "50\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "100\r\n0 1\r\n0 0\r\n0 0\r\n1 0\r\n0 0\r\n0 1\r\n0 1\r\n1 1\r\n0 0\r\n0 0\r\n1 1\r\n0 0\r\n1 1\r\n0 1\r\n1 1\r\n0 1\r\n1 1\r\n1 0\r\n1 0\r\n0 0\r\n1 0\r\n0 1\r\n1 0\r\n0 0\r\n0 0\r\n1 1\r\n1 1\r\n0 1\r\n0 0\r\n1 0\r\n1 1\r\n0 1\r\n1 0\r\n1 1\r\n0 1\r\n1 1\r\n1 0\r\n0 0\r\n0 0\r\n0 1\r\n0 0\r\n0 1\r\n1 1\r\n0 0\r\n1 1\r\n1 1\r\n0 0\r\n0 1\r\n1 0\r\n0 1\r\n0 0\r\n0 1\r\n0 1\r\n1 1\r\n1 1\r\n1 1\r\n0 0\r\n0 0\r\n1 1\r\n0 1\r\n0 1\r\n1 0\r\n0 0\r\n0 0\r\n1 1\r\n0 1\r\n0 1\r\n1 1\r\n1 1\r\n0 1\r\n1 1\r\n1 1\r\n0 0\r\n1 0\r\n0 1\r\n0 0\r\n0 0\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n0 1\r\n1 0\r\n1 0\r\n1 0\r\n1 0\r\n1 0\r\n0 0\r\n1 0\r\n1 0\r\n0 0\r\n1 0\r\n0 0\r\n0 1\r\n1 0\r\n0 1\r\n1 0\r\n1 0\r\n1 0\r\n1 0\r\n", "output": "11\r\n"}, {"input": "100\r\n0 2\r\n1 2\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 2\r\n1 1\r\n1 1\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 2\r\n2 2\r\n2 2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n1 1\r\n1 1\r\n2 2\r\n1 2\r\n2 2\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n1 1\r\n2 2\r\n1 1\r\n2 1\r\n1 1\r\n2 2\r\n2 1\r\n1 2\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n1 1\r\n2 1\r\n1 1\r\n2 1\r\n1 1\r\n1 2\r\n2 2\r\n2 2\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n2 1\r\n1 1\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n1 1\r\n2 1\r\n1 2\r\n1 2\r\n2 1\r\n2 2\r\n2 2\r\n2 2\r\n2 1\r\n2 2\r\n1 1\r\n1 2\r\n1 2\r\n1 1\r\n2 2\r\n2 2\r\n1 1\r\n2 1\r\n1 1\r\n1 2\r\n1 2\r\n1 2\r\n1 1\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n2 1\r\n2 1\r\n1 2\r\n1 2\r\n1 1\r\n2 2\r\n1 2\r\n2 0\r\n2 0\r\n2 0\r\n1 0\r\n", "output": "7\r\n"}, {"input": "100\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "2\r\n0 900\r\n900 0\r\n", "output": "900\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "1\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1000\r\n1000 0\r\n", "output": "1000\r\n"}, {"input": "3\r\n0 802\r\n175 188\r\n815 0\r\n", "output": "815\r\n"}, {"input": "3\r\n0 910\r\n910 976\r\n976 0\r\n", "output": "976\r\n"}, {"input": "3\r\n0 2\r\n2 1\r\n1 0\r\n", "output": "2\r\n"}]
false
stdio
null
true
626/E
626
E
PyPy 3
TESTS
15
1,294
47,718,400
125816841
def findMean(prefixSum, n, k, medianIdx, median): if k == 0: return median total = prefixSum[medianIdx+1] - prefixSum[medianIdx - k] + prefixSum[n] - prefixSum[n-k] return total/(2*k + 1) n = int(input()) nums = sorted(list(map(int, input().split()))) ans, bestK, bestMedianIdx = -1, 0, 0 prefSum = [0] for i in range(n): prefSum.append(prefSum[-1] + nums[i]) for i in range(n): medianIdx = i rangeOfK = min(i, n - i - 1) left, right = 0, rangeOfK mid = (left + right)//2 while right > left: currentMean = findMean(prefSum, n, mid, i, nums[medianIdx]) a = nums[n - mid - 1] b = nums[i - mid - 1] if (a + b) / 2.0 <= currentMean: right = max(mid, left) else: left = min(mid + 1, right) mid = (left + right)//2 bestCurrentMean = findMean(prefSum, n, left, i, nums[medianIdx]) if bestCurrentMean - nums[medianIdx] > ans: ans = bestCurrentMean - nums[medianIdx] bestMedianIdx = medianIdx bestK = left print(2 * bestK + 1) for i in range(bestK): print(nums[n-1-i], end = " ") for i in range(bestK+1): print(nums[bestMedianIdx-i], end = " ")
94
343
27,033,600
217897255
def CalcMean(n,a,c,median,k): if k==0: return a[median] s = c[median+1] - c[median-k] + c[n] - c[n-k] return s/(2*k+1) n=int(input()) a=list(map(int,input().split())) a.sort() c=[0] for i in range(n): c.append(c[-1]+a[i]) ans=optMed=optK=0 for median in range(n): L=0 R=min(median,n-median-1) mid=(L+R)//2 while R-L > 0: mean = CalcMean(n,a,c,median,mid) k = mid+1 x = a[median-k] y = a[n-k] if (x+y)/2 <= mean: R = mid else: L = min(mid+1,R) mid=(L+R)//2 mean = CalcMean(n,a,c,median,L) if ans <= mean - a[median]: ans = mean - a[median] optMed = median optK = L print(2*optK+1) for i in range(optMed-optK,optMed+1): print(a[i],end=' ') for i in range(n-optK,n): print(a[i],end=' ')
8VC Venture Cup 2016 - Elimination Round
CF
2,016
3
256
Simple Skewness
Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in the list. The second line contains n integers xi (0 ≤ xi ≤ 1 000 000) — the ith element of the list.
In the first line, print a single integer k — the size of the subset. In the second line, print k integers — the elements of the subset in any order. If there are multiple optimal subsets, print any.
null
In the first case, the optimal subset is $${ \{ 1, 2, 1 2 \} }$$, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is $${ \{ 1, 1, 2 \} }$$. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0.
[{"input": "4\n1 2 3 12", "output": "3\n1 2 12"}, {"input": "4\n1 1 2 2", "output": "3\n1 1 2"}, {"input": "2\n1 2", "output": "2\n1 2"}]
2,400
["binary search", "math", "ternary search"]
94
[{"input": "4\r\n1 2 3 12\r\n", "output": "3\r\n1 2 12 \r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "3\r\n1 1 2 \r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 2\r\n"}, {"input": "1\r\n1000000\r\n", "output": "1\r\n1000000 \r\n"}, {"input": "20\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712\r\n", "output": "1\r\n475712 \r\n"}, {"input": "21\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 1000000\r\n", "output": "1\r\n475712 \r\n"}, {"input": "40\r\n999999 999999 999998 999998 999996 999996 999992 999992 999984 999984 999968 999968 999936 999936 999872 999872 999744 999744 999488 999488 998976 998976 997952 997952 995904 995904 991808 991808 983616 983616 967232 967232 934464 934464 868928 868928 737856 737856 475712 0\r\n", "output": "3\r\n737856 737856 999999 \r\n"}, {"input": "1\r\n534166\r\n", "output": "1\r\n534166 \r\n"}, {"input": "1\r\n412237\r\n", "output": "1\r\n412237 \r\n"}, {"input": "1\r\n253309\r\n", "output": "1\r\n253309 \r\n"}, {"input": "1\r\n94381\r\n", "output": "1\r\n94381 \r\n"}, {"input": "1\r\n935454\r\n", "output": "1\r\n935454 \r\n"}, {"input": "2\r\n847420 569122\r\n", "output": "2\r\n847420 569122\r\n"}, {"input": "2\r\n725491 635622\r\n", "output": "2\r\n725491 635622\r\n"}, {"input": "2\r\n566563 590441\r\n", "output": "2\r\n566563 590441\r\n"}, {"input": "2\r\n407635 619942\r\n", "output": "2\r\n407635 619942\r\n"}, {"input": "2\r\n248707 649443\r\n", "output": "2\r\n248707 649443\r\n"}, {"input": "3\r\n198356 154895 894059\r\n", "output": "3\r\n154895 198356 894059 \r\n"}, {"input": "3\r\n76427 184396 963319\r\n", "output": "3\r\n76427 184396 963319 \r\n"}, {"input": "3\r\n880502 176898 958582\r\n", "output": "1\r\n176898 \r\n"}, {"input": "3\r\n758573 206400 991528\r\n", "output": "1\r\n206400 \r\n"}, {"input": "3\r\n599645 198217 986791\r\n", "output": "1\r\n198217 \r\n"}, {"input": "4\r\n549294 703669 96824 126683\r\n", "output": "3\r\n96824 126683 703669 \r\n"}, {"input": "4\r\n390366 733171 92086 595244\r\n", "output": "3\r\n92086 390366 733171 \r\n"}, {"input": "4\r\n231438 762672 125033 26806\r\n", "output": "3\r\n26806 125033 762672 \r\n"}, {"input": "4\r\n109509 792173 120296 495368\r\n", "output": "3\r\n109509 120296 792173 \r\n"}, {"input": "4\r\n950582 784676 190241 964614\r\n", "output": "1\r\n190241 \r\n"}, {"input": "5\r\n900232 289442 225592 622868 113587\r\n", "output": "3\r\n113587 225592 900232 \r\n"}, {"input": "5\r\n741304 281944 258539 54430 284591\r\n", "output": "3\r\n281944 284591 741304 \r\n"}, {"input": "5\r\n582376 311446 253801 560676 530279\r\n", "output": "3\r\n253801 311446 582376 \r\n"}, {"input": "5\r\n460447 303948 286063 992238 738282\r\n", "output": "3\r\n286063 303948 992238 \r\n"}, {"input": "5\r\n301519 370449 319010 460799 983970\r\n", "output": "3\r\n301519 319010 983970 \r\n"}, {"input": "21\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 999998\r\n", "output": "3\r\n999998 999998 999999 \r\n"}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: lines = f.readlines() return [line.strip() for line in lines] def compute_skewness(subset): sorted_subset = sorted(subset) k = len(sorted_subset) if k == 0: return None total = sum(sorted_subset) mean = total / k if k % 2 == 1: median = sorted_subset[k//2] else: median = (sorted_subset[(k//2)-1] + sorted_subset[k//2])/2 return mean - median def main(): input_path = sys.argv[1] ref_path = sys.argv[2] sub_path = sys.argv[3] input_data = read_file(input_path) ref_data = read_file(ref_path) sub_data = read_file(sub_path) # Parse input n = int(input_data[0]) input_list = list(map(int, input_data[1].split())) input_counts = {} for x in input_list: input_counts[x] = input_counts.get(x, 0) + 1 # Parse submission output if len(sub_data) < 2: print(0) return try: k_sub = int(sub_data[0]) elements_sub = list(map(int, sub_data[1].split())) if k_sub != len(elements_sub) or k_sub ==0: print(0) return except: print(0) return # Check elements are in input for x in elements_sub: if x not in input_counts: print(0) return # Check counts do not exceed input counts sub_counts = {} for x in elements_sub: sub_counts[x] = sub_counts.get(x, 0) + 1 for x, count in sub_counts.items(): if count > input_counts[x]: print(0) return # Parse reference output if len(ref_data) < 2: print(0) return try: k_ref = int(ref_data[0]) elements_ref = list(map(int, ref_data[1].split())) if k_ref != len(elements_ref) or k_ref ==0: print(0) return except: print(0) return # Compute skewness skewness_ref = compute_skewness(elements_ref) skewness_sub = compute_skewness(elements_sub) # Check if submission's skewness is equal to reference's if abs(skewness_sub - skewness_ref) < 1e-9: print(1) else: print(0) if __name__ == '__main__': main()
true
626/E
626
E
PyPy 3-64
TESTS
12
186
22,323,200
218906343
n = int(input()) a = [0] a.extend(list(map(int, input().split()))) a.sort() p = [0] * (n + 1) p[1] = a[1] for i in range(2, n + 1): p[i] = a[i] + p[i - 1] maxSkewness = 0 for i in range(1, n + 1): median = maxMean = a[i] l = 0 r = min(i - 1, n - i) while l <= r: m = l + (r - l) // 2 SUM = (p[i] - p[i - m - 1]) + (p[n] - p[n - m]) TOTAL = 2 * m + 1 mean = SUM / TOTAL if mean >= maxMean: maxMean = mean l = m + 1 else: r = m - 1 skewness = maxMean - median if skewness >= maxSkewness: maxSkewness = skewness x = i y = l res = a[x - y + 1 : x + 1] + a[n - y + 2 :] print(len(res)) print(*res)
94
1,560
55,398,400
125835053
from fractions import Fraction def findMean(prefixSum, n, k, medianIdx): total = prefixSum[medianIdx + 1] - prefixSum[medianIdx - k] + prefixSum[n] - prefixSum[n - k] return Fraction(total, (2 * k + 1)) n = int(input()) nums = sorted(list(map(int, input().split()))) ans, bestK, bestMedianIdx = -1, 0, 0 prefSum = [0] for i in range(n): prefSum.append(prefSum[-1] + nums[i]) for medianIdx in range(n): rangeOfK = min(medianIdx, n - medianIdx - 1) left, right = 0, rangeOfK mid = (left + right) // 2 while right > left: a = nums[n - mid - 1] b = nums[medianIdx - mid - 1] if (2 * mid + 1) * (a + b) <= 2 * (prefSum[medianIdx + 1] - prefSum[medianIdx - mid] + prefSum[n] - prefSum[n - mid]): right = mid else: left = mid + 1 mid = (left + right) // 2 bestCurrentMean = findMean(prefSum, n, left, medianIdx) if bestCurrentMean - nums[medianIdx] > ans: ans = bestCurrentMean - nums[medianIdx] bestMedianIdx = medianIdx bestK = left print(2 * bestK + 1) for i in range(bestK): print(nums[n - 1 - i], end = " ") for i in range(bestK+1): print(nums[bestMedianIdx - i], end = " ")
8VC Venture Cup 2016 - Elimination Round
CF
2,016
3
256
Simple Skewness
Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in the list. The second line contains n integers xi (0 ≤ xi ≤ 1 000 000) — the ith element of the list.
In the first line, print a single integer k — the size of the subset. In the second line, print k integers — the elements of the subset in any order. If there are multiple optimal subsets, print any.
null
In the first case, the optimal subset is $${ \{ 1, 2, 1 2 \} }$$, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is $${ \{ 1, 1, 2 \} }$$. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0.
[{"input": "4\n1 2 3 12", "output": "3\n1 2 12"}, {"input": "4\n1 1 2 2", "output": "3\n1 1 2"}, {"input": "2\n1 2", "output": "2\n1 2"}]
2,400
["binary search", "math", "ternary search"]
94
[{"input": "4\r\n1 2 3 12\r\n", "output": "3\r\n1 2 12 \r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "3\r\n1 1 2 \r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 2\r\n"}, {"input": "1\r\n1000000\r\n", "output": "1\r\n1000000 \r\n"}, {"input": "20\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712\r\n", "output": "1\r\n475712 \r\n"}, {"input": "21\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 1000000\r\n", "output": "1\r\n475712 \r\n"}, {"input": "40\r\n999999 999999 999998 999998 999996 999996 999992 999992 999984 999984 999968 999968 999936 999936 999872 999872 999744 999744 999488 999488 998976 998976 997952 997952 995904 995904 991808 991808 983616 983616 967232 967232 934464 934464 868928 868928 737856 737856 475712 0\r\n", "output": "3\r\n737856 737856 999999 \r\n"}, {"input": "1\r\n534166\r\n", "output": "1\r\n534166 \r\n"}, {"input": "1\r\n412237\r\n", "output": "1\r\n412237 \r\n"}, {"input": "1\r\n253309\r\n", "output": "1\r\n253309 \r\n"}, {"input": "1\r\n94381\r\n", "output": "1\r\n94381 \r\n"}, {"input": "1\r\n935454\r\n", "output": "1\r\n935454 \r\n"}, {"input": "2\r\n847420 569122\r\n", "output": "2\r\n847420 569122\r\n"}, {"input": "2\r\n725491 635622\r\n", "output": "2\r\n725491 635622\r\n"}, {"input": "2\r\n566563 590441\r\n", "output": "2\r\n566563 590441\r\n"}, {"input": "2\r\n407635 619942\r\n", "output": "2\r\n407635 619942\r\n"}, {"input": "2\r\n248707 649443\r\n", "output": "2\r\n248707 649443\r\n"}, {"input": "3\r\n198356 154895 894059\r\n", "output": "3\r\n154895 198356 894059 \r\n"}, {"input": "3\r\n76427 184396 963319\r\n", "output": "3\r\n76427 184396 963319 \r\n"}, {"input": "3\r\n880502 176898 958582\r\n", "output": "1\r\n176898 \r\n"}, {"input": "3\r\n758573 206400 991528\r\n", "output": "1\r\n206400 \r\n"}, {"input": "3\r\n599645 198217 986791\r\n", "output": "1\r\n198217 \r\n"}, {"input": "4\r\n549294 703669 96824 126683\r\n", "output": "3\r\n96824 126683 703669 \r\n"}, {"input": "4\r\n390366 733171 92086 595244\r\n", "output": "3\r\n92086 390366 733171 \r\n"}, {"input": "4\r\n231438 762672 125033 26806\r\n", "output": "3\r\n26806 125033 762672 \r\n"}, {"input": "4\r\n109509 792173 120296 495368\r\n", "output": "3\r\n109509 120296 792173 \r\n"}, {"input": "4\r\n950582 784676 190241 964614\r\n", "output": "1\r\n190241 \r\n"}, {"input": "5\r\n900232 289442 225592 622868 113587\r\n", "output": "3\r\n113587 225592 900232 \r\n"}, {"input": "5\r\n741304 281944 258539 54430 284591\r\n", "output": "3\r\n281944 284591 741304 \r\n"}, {"input": "5\r\n582376 311446 253801 560676 530279\r\n", "output": "3\r\n253801 311446 582376 \r\n"}, {"input": "5\r\n460447 303948 286063 992238 738282\r\n", "output": "3\r\n286063 303948 992238 \r\n"}, {"input": "5\r\n301519 370449 319010 460799 983970\r\n", "output": "3\r\n301519 319010 983970 \r\n"}, {"input": "21\r\n999999 999998 999996 999992 999984 999968 999936 999872 999744 999488 998976 997952 995904 991808 983616 967232 934464 868928 737856 475712 999998\r\n", "output": "3\r\n999998 999998 999999 \r\n"}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: lines = f.readlines() return [line.strip() for line in lines] def compute_skewness(subset): sorted_subset = sorted(subset) k = len(sorted_subset) if k == 0: return None total = sum(sorted_subset) mean = total / k if k % 2 == 1: median = sorted_subset[k//2] else: median = (sorted_subset[(k//2)-1] + sorted_subset[k//2])/2 return mean - median def main(): input_path = sys.argv[1] ref_path = sys.argv[2] sub_path = sys.argv[3] input_data = read_file(input_path) ref_data = read_file(ref_path) sub_data = read_file(sub_path) # Parse input n = int(input_data[0]) input_list = list(map(int, input_data[1].split())) input_counts = {} for x in input_list: input_counts[x] = input_counts.get(x, 0) + 1 # Parse submission output if len(sub_data) < 2: print(0) return try: k_sub = int(sub_data[0]) elements_sub = list(map(int, sub_data[1].split())) if k_sub != len(elements_sub) or k_sub ==0: print(0) return except: print(0) return # Check elements are in input for x in elements_sub: if x not in input_counts: print(0) return # Check counts do not exceed input counts sub_counts = {} for x in elements_sub: sub_counts[x] = sub_counts.get(x, 0) + 1 for x, count in sub_counts.items(): if count > input_counts[x]: print(0) return # Parse reference output if len(ref_data) < 2: print(0) return try: k_ref = int(ref_data[0]) elements_ref = list(map(int, ref_data[1].split())) if k_ref != len(elements_ref) or k_ref ==0: print(0) return except: print(0) return # Compute skewness skewness_ref = compute_skewness(elements_ref) skewness_sub = compute_skewness(elements_sub) # Check if submission's skewness is equal to reference's if abs(skewness_sub - skewness_ref) < 1e-9: print(1) else: print(0) if __name__ == '__main__': main()
true
230/A
230
A
PyPy 3-64
TESTS
35
216
2,150,400
229401921
s,n = list(map(int,input().split())) health = [] bonus = [] for _ in range(n): h,b = list(map(int,input().split())) health.append(h) bonus.append(b) ans = True while(len(health)): mi = health.index(min(health)) if(s<=health[mi]): ans =False break else: s += bonus[mi] health.remove(health[mi]) bonus.remove(bonus[mi]) if(ans): print("YES") else: print("NO")
54
92
0
227713029
s,n=map(int,input().split()) dragons=[] for _ in range(n): xi,yi=map(int,input().split()) dragons.append((xi,yi)) dragons.sort() for xi,yi in dragons: if s>xi: s+=yi else: print("NO") break else: print("YES")
Codeforces Round 142 (Div. 2)
CF
2,012
2
256
Dragons
Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s. If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi. Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.
The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon's strength and the bonus for defeating it.
On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't.
null
In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level. In the second sample Kirito's strength is too small to defeat the only dragon and win.
[{"input": "2 2\n1 99\n100 0", "output": "YES"}, {"input": "10 1\n100 100", "output": "NO"}]
1,000
["greedy", "sortings"]
54
[{"input": "2 2\r\n1 99\r\n100 0\r\n", "output": "YES\r\n"}, {"input": "10 1\r\n100 100\r\n", "output": "NO\r\n"}, {"input": "123 2\r\n78 10\r\n130 0\r\n", "output": "YES\r\n"}, {"input": "999 2\r\n1010 10\r\n67 89\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n5 1\r\n2 1\r\n3 1\r\n1 1\r\n4 1\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n3 5\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "1 2\r\n1 0\r\n1 0\r\n", "output": "NO\r\n"}, {"input": "5 10\r\n20 1\r\n4 3\r\n5 1\r\n100 1\r\n4 2\r\n101 1\r\n10 0\r\n10 2\r\n17 3\r\n12 84\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n1 98\r\n100 0\r\n", "output": "NO\r\n"}, {"input": "2 2\r\n1 2\r\n3 5\r\n", "output": "YES\r\n"}, {"input": "5 3\r\n13 20\r\n3 10\r\n15 5\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n1 1\r\n1 2\r\n4 0\r\n", "output": "YES\r\n"}, {"input": "10 4\r\n20 1\r\n3 5\r\n2 4\r\n1 3\r\n", "output": "YES\r\n"}, {"input": "10 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "4 1\r\n100 1000\r\n", "output": "NO\r\n"}, {"input": "5 1\r\n6 7\r\n", "output": "NO\r\n"}, {"input": "10 1\r\n10 10\r\n", "output": "NO\r\n"}, {"input": "6 2\r\n496 0\r\n28 8128\r\n", "output": "NO\r\n"}, {"input": "4 2\r\n2 1\r\n10 3\r\n", "output": "NO\r\n"}, {"input": "11 2\r\n22 0\r\n33 0\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n100 1\r\n100 1\r\n", "output": "NO\r\n"}, {"input": "10 3\r\n12 0\r\n13 0\r\n14 0\r\n", "output": "NO\r\n"}, {"input": "50 3\r\n39 0\r\n38 0\r\n37 0\r\n", "output": "YES\r\n"}, {"input": "14 3\r\n1 5\r\n1 6\r\n1 7\r\n", "output": "YES\r\n"}, {"input": "1 3\r\n1 10\r\n1 11\r\n1 9\r\n", "output": "NO\r\n"}, {"input": "10 10\r\n2 10\r\n3 10\r\n4 10\r\n2 20\r\n3 20\r\n3 20\r\n100 50\r\n100 30\r\n150 30\r\n200 10\r\n", "output": "NO\r\n"}, {"input": "9983 34\r\n6626 5976\r\n4448 3568\r\n2794 2309\r\n3741 8806\r\n4754 129\r\n2780 9275\r\n5785 9243\r\n3915 6159\r\n2609 4331\r\n238 6756\r\n6987 3887\r\n3384 5711\r\n4349 5563\r\n1135 4483\r\n9151 1584\r\n1500 766\r\n1608 4440\r\n7768 5005\r\n7205 2360\r\n9088 2933\r\n3923 7814\r\n7538 9372\r\n7504 165\r\n5277 1636\r\n2061 4384\r\n7668 1422\r\n9582 2121\r\n5483 7967\r\n487 2944\r\n7432 5794\r\n8208 8970\r\n5747 3800\r\n4322 3920\r\n8261 9319\r\n", "output": "YES\r\n"}, {"input": "1 10\r\n8 8\r\n54 3\r\n1 8\r\n26 3\r\n16 1\r\n29 9\r\n38 10\r\n57 8\r\n48 6\r\n17 9\r\n", "output": "NO\r\n"}, {"input": "5 10\r\n7 0\r\n7 0\r\n10 0\r\n10 0\r\n7 2\r\n4 2\r\n9 0\r\n6 1\r\n7 0\r\n7 0\r\n", "output": "NO\r\n"}, {"input": "2 3\r\n1 1\r\n1 10\r\n17 2\r\n", "output": "NO\r\n"}, {"input": "100 5\r\n99 100\r\n199 1\r\n199 1\r\n199 1\r\n202 1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\n10000 1\r\n", "output": "NO\r\n"}]
false
stdio
null
true
817/B
817
B
PyPy 3-64
TESTS
18
202
21,708,800
172160874
n = int(input()) a = map(int, input().split(' ')) mp = {} for x in a: if x not in mp: mp[x] = 0 mp[x] += 1 smp = sorted(mp.items(), key=lambda x: x[0]) cnt = 3 i = 0 prod = 1 while cnt > 0: t = min(cnt, smp[i][1]) prod *= smp[i][0] ** t cnt -= t i += 1 def getcnt(a, b, c): if a == b and b == c: return mp[b] * (mp[b] - 1) * (mp[b] - 2) // 6 elif a == b: return mp[b] * (mp[b] - 1) // 2 * mp[c] elif b == c: return mp[b] * (mp[b] - 1) // 2 * mp[a] else: return mp[a] * mp[b] * mp[c] ans = 0 m = len(smp) for i in range(m): x = smp[i][0] if prod % x != 0: continue yz = prod // x lb = i if mp[x] == 1: lb += 1 for j in range(lb, m): y = smp[j][0] if yz % y != 0: continue z = yz // y if z < y: continue elif z == y: if x == y and mp[x] >= 3 or mp[y] == 2: ans += getcnt(x, y, z) else: ans += getcnt(x, y, z) print(ans)
80
109
9,113,600
27824979
n=int(input()) l=list(map(int,input().strip().split(' '))) l1=sorted(l) a=l1[0] b=l1[1] c=l1[2] if a==b and b==c: ans=l.count(a) a=ans print((a*(a-1)*(a-2))//6) elif b!=c: ans=l.count(c) c=ans print(c) else: ans=l.count(b) b=ans print((b*(b-1))//2)
Educational Codeforces Round 23
ICPC
2,017
2
256
Makes And The Product
After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.
null
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4. In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2. In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
[{"input": "4\n1 1 1 1", "output": "4"}, {"input": "5\n1 3 2 3 4", "output": "2"}, {"input": "6\n1 3 3 1 3 2", "output": "1"}]
1,500
["combinatorics", "implementation", "math", "sortings"]
80
[{"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "2\r\n"}, {"input": "6\r\n1 3 3 1 3 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "1\r\n"}, {"input": "11\r\n1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "45\r\n"}, {"input": "5\r\n1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6\r\n1 2 2 3 3 4\r\n", "output": "1\r\n"}, {"input": "8\r\n1 1 2 2 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 3\r\n", "output": "6\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2 2 2 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "4\r\n1 2 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 2 3\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 3 3 3 3\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 2 2 5 6\r\n", "output": "3\r\n"}, {"input": "10\r\n1 2 2 2 2 2 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "3\r\n2 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2 2 3\r\n", "output": "1\r\n"}, {"input": "10\r\n2 2 2 2 2 1 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "7\r\n2 2 2 3 3 3 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 2 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "9\r\n2 2 3 3 3 3 3 3 3\r\n", "output": "7\r\n"}, {"input": "5\r\n1 1 2 2 3\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 3 3\r\n", "output": "2\r\n"}, {"input": "4\r\n33554432 33554432 67108864 33554432\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 2 1 2 2\r\n", "output": "10\r\n"}, {"input": "10\r\n1 2 1 2 3 2 3 2 2 2\r\n", "output": "6\r\n"}, {"input": "10\r\n9 6 4 7 1 8 9 5 9 4\r\n", "output": "1\r\n"}, {"input": "4\r\n5 7 2 7\r\n", "output": "2\r\n"}, {"input": "3\r\n7 6 7\r\n", "output": "1\r\n"}, {"input": "6\r\n3 2 8 2 5 3\r\n", "output": "2\r\n"}, {"input": "3\r\n5 9 5\r\n", "output": "1\r\n"}, {"input": "5\r\n6 3 7 6 3\r\n", "output": "2\r\n"}, {"input": "9\r\n10 10 4 10 7 9 6 7 3\r\n", "output": "1\r\n"}, {"input": "5\r\n9 10 10 3 8\r\n", "output": "1\r\n"}, {"input": "5\r\n2 9 5 10 5\r\n", "output": "1\r\n"}, {"input": "9\r\n7 1 9 6 6 8 3 1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n3 4 4 4 5\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 3\r\n", "output": "1\r\n"}, {"input": "8\r\n3 2 2 5 2 2 1 2\r\n", "output": "10\r\n"}]
false
stdio
null
true
613/A
613
A
Python 3
TESTS
2
46
0
29620769
class vector: def __init__(self, x, y): self.x = x self.y = y def length(self): return (self.x ** 2 + self.y ** 2) ** 0.5 n, x, y = map(int, input().split()) min_r = 2000000 max_r = 0 for i in range(n): x1, y1 = map(int, input().split()) k = vector(x1 - x, y1 - y).length() max_r = max(max_r, k) min_r = min(min_r, k) pi = 3.141592653589 print(pi * max_r ** 2 - pi * min_r ** 2)
60
686
2,867,200
15364669
import math n, a, b = [int(x) for x in input().split()] distances = [] x, y = [int(x) for x in input().split()] distances.append(math.hypot(x - a, y - b)) prevx, prevy = x, y firstx, firsty = x, y for i in range(n-1): x, y = [int(x) for x in input().split()] distances.append(math.hypot(x - a, y - b)) scalar_mult = (a-x)*(prevx-x) + (b-y)*(prevy-y) sq = (prevx - x)**2 + (prevy - y)**2 if 0 < scalar_mult < sq: distances.append(((a-x)**2 + (b-y)**2 - scalar_mult**2/sq) ** 0.5) prevx, prevy = x, y scalar_mult = (a-prevx)*(firstx-prevx) + (b-prevy)*(firsty-prevy) sq = (firstx - prevx)**2 + (firsty - prevy)**2 if 0 < scalar_mult < sq: distances.append(((a-prevx)**2 + (b-prevy)**2 - scalar_mult**2/sq) ** 0.5) distances.sort() print((distances[-1] - distances[0])*(distances[-1] + distances[0])*math.pi)
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Peter and Snow Blower
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path. Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine. Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.
The first line of the input contains three integers — the number of vertices of the polygon n ($$3 \leq n \leq 100\ 000$$), and coordinates of point P. Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line. All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample snow will be removed from that area:
[{"input": "3 0 0\n0 1\n-1 2\n1 2", "output": "12.566370614359172464"}, {"input": "4 1 -1\n0 0\n1 2\n2 0\n1 1", "output": "21.991148575128551812"}]
1,900
["binary search", "geometry", "ternary search"]
60
[{"input": "3 0 0\r\n0 1\r\n-1 2\r\n1 2\r\n", "output": "12.566370614359172464\r\n"}, {"input": "4 1 -1\r\n0 0\r\n1 2\r\n2 0\r\n1 1\r\n", "output": "21.991148575128551812\r\n"}, {"input": "3 0 0\r\n-1 1\r\n0 3\r\n1 1\r\n", "output": "25.132741228718344928\r\n"}, {"input": "3 -4 2\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "405.26545231308331191\r\n"}, {"input": "3 -84 8\r\n-83 8\r\n21 -62\r\n3 53\r\n", "output": "50026.721415763865583\r\n"}, {"input": "6 -94 -51\r\n-93 -51\r\n48 -25\r\n61 27\r\n73 76\r\n-10 87\r\n-48 38\r\n", "output": "138283.48383306192359\r\n"}, {"input": "5 -94 52\r\n-93 52\r\n-78 -56\r\n-54 -81\r\n56 -87\r\n97 85\r\n", "output": "131381.40477312514811\r\n"}, {"input": "10 -100 90\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198410.42563011697403\r\n"}, {"input": "11 -97 -15\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "133558.52848206287476\r\n"}, {"input": "10 -500 420\r\n-499 420\r\n-489 -173\r\n-455 -480\r\n160 -464\r\n374 -437\r\n452 -352\r\n481 -281\r\n465 75\r\n326 392\r\n-398 468\r\n", "output": "4719573.802783449531\r\n"}, {"input": "10 -498 -161\r\n-497 -161\r\n-427 -458\r\n-325 -475\r\n349 -500\r\n441 -220\r\n473 28\r\n475 62\r\n468 498\r\n-444 492\r\n-465 264\r\n", "output": "4295926.8918542123392\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n8 5\r\n10 7\r\n7 5\r\n2 5\r\n", "output": "574.91145560693214023\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n20 3\r\n26 17\r\n23 21\r\n98 96\r\n", "output": "60343.711690152746165\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n94 7\r\n100 52\r\n87 48\r\n37 26\r\n74 61\r\n59 57\r\n87 90\r\n52 90\r\n26 73\r\n", "output": "50337.739088469255101\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n78 22\r\n53 24\r\n78 50\r\n46 39\r\n45 56\r\n21 46\r\n2 7\r\n24 97\r\n5 59\r\n", "output": "32129.068068262814194\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n95 2\r\n47 1\r\n42 1\r\n93 7\r\n56 6\r\n47 7\r\n63 13\r\n98 24\r\n94 27\r\n90 28\r\n86 28\r\n17 6\r\n64 24\r\n42 19\r\n66 35\r\n63 35\r\n98 60\r\n75 48\r\n28 18\r\n71 46\r\n69 46\r\n99 68\r\n64 47\r\n56 43\r\n72 58\r\n35 29\r\n82 81\r\n68 69\r\n79 84\r\n72 77\r\n79 86\r\n54 59\r\n35 39\r\n20 23\r\n73 86\r\n80 97\r\n79 100\r\n69 99\r\n29 45\r\n26 63\r\n23 56\r\n12 33\r\n13 39\r\n25 85\r\n27 96\r\n6 23\r\n4 47\r\n1 60\r\n", "output": "52147.296456936975932\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n69 2\r\n74 7\r\n62 10\r\n64 15\r\n93 22\r\n78 22\r\n56 17\r\n86 29\r\n24 9\r\n91 43\r\n8 4\r\n90 50\r\n99 57\r\n39 23\r\n81 50\r\n91 58\r\n67 46\r\n95 66\r\n52 39\r\n91 69\r\n69 54\r\n93 84\r\n93 98\r\n70 80\r\n85 98\r\n30 39\r\n55 79\r\n41 59\r\n50 72\r\n57 88\r\n58 92\r\n58 94\r\n37 63\r\n43 87\r\n30 63\r\n19 40\r\n38 81\r\n40 86\r\n38 100\r\n2 6\r\n30 100\r\n23 89\r\n16 62\r\n11 49\r\n12 64\r\n9 52\r\n5 62\r\n1 88\r\n", "output": "58543.579099645794717\r\n"}, {"input": "27 -999899 136015\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600304470662.964855\r\n"}, {"input": "19 -995486 -247212\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257949833603.158278\r\n"}, {"input": "15 -994057 554462\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694832748836.689348\r\n"}, {"input": "23 -999840 738880\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831930831113.094931\r\n"}, {"input": "20 -999719 -377746\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331542740428.216614\r\n"}, {"input": "26 -999922 -339832\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127026556380.411608\r\n"}, {"input": "22 -999930 -362070\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335297542813.80731\r\n"}, {"input": "29 -999961 689169\r\n-999960 689169\r\n-999927 -938525\r\n-999735 -989464\r\n-993714 -997911\r\n-870186 -999686\r\n-796253 -999950\r\n-139940 -999968\r\n969552 -999972\r\n985446 -999398\r\n992690 -997295\r\n999706 -973137\r\n999898 -848630\r\n999997 -192297\r\n999969 773408\r\n999495 960350\r\n999143 981671\r\n998324 993987\r\n997640 998103\r\n986157 998977\r\n966840 999418\r\n670113 999809\r\n477888 999856\r\n129160 999900\r\n-373564 999947\r\n-797543 999976\r\n-860769 999903\r\n-995496 999355\r\n-998771 984570\r\n-999768 927157\r\n", "output": "21409384775316.574772\r\n"}, {"input": "3 -3 3\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "399.0305992005743379\r\n"}, {"input": "3 -9 7\r\n-9 6\r\n3 -6\r\n4 2\r\n", "output": "980.17690792001545219\r\n"}, {"input": "5 -9 8\r\n-9 7\r\n-6 -1\r\n-3 -6\r\n1 -3\r\n10 8\r\n", "output": "1130.9820337250702449\r\n"}, {"input": "6 -6 -1\r\n-6 -2\r\n0 -7\r\n8 -9\r\n9 -1\r\n5 10\r\n-5 0\r\n", "output": "816.18577140262825159\r\n"}, {"input": "10 -99 91\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198309.89857373595223\r\n"}, {"input": "11 -96 -14\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "131821.20868619133483\r\n"}, {"input": "13 -98 25\r\n-98 24\r\n-96 10\r\n-80 -71\r\n-71 -78\r\n-31 -99\r\n82 -98\r\n92 -39\r\n94 -2\r\n94 40\r\n90 80\r\n50 96\r\n-41 97\r\n-86 80\r\n", "output": "149316.61930888936332\r\n"}, {"input": "17 -99 -53\r\n-99 -54\r\n-97 -71\r\n-67 -99\r\n-61 -99\r\n56 -98\r\n82 -85\r\n95 -47\r\n90 -2\r\n82 30\r\n63 87\r\n54 95\r\n-12 99\r\n-38 99\r\n-87 89\r\n-90 87\r\n-95 67\r\n-96 49\r\n", "output": "144023.17094830233827\r\n"}, {"input": "19 -995485 -247211\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257930301545.657524\r\n"}, {"input": "15 -994056 554463\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694830011124.045712\r\n"}, {"input": "23 -999839 738881\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831929255745.74826\r\n"}, {"input": "20 -999718 -377745\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331521646100.671528\r\n"}, {"input": "26 -999921 -339831\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127005627407.454252\r\n"}, {"input": "22 -999929 -362069\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335276455623.960732\r\n"}, {"input": "27 -999898 136016\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600299044211.965457\r\n"}, {"input": "13 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999417 840\r\n999781 33421\r\n999994 131490\r\n999993 998865\r\n962080 999911\r\n629402 999973\r\n378696 999988\r\n53978 999788\r\n25311 999558\r\n6082 999282\r\n1565 998489\r\n", "output": "23547598153913.984406\r\n"}, {"input": "16 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999744 572\r\n999931 96510\r\n1000000 254372\r\n999939 748173\r\n999894 953785\r\n999683 986098\r\n999051 999815\r\n980586 999969\r\n637250 999988\r\n118331 999983\r\n27254 999966\r\n9197 999405\r\n4810 997733\r\n1661 995339\r\n", "output": "23547697574489.259052\r\n"}, {"input": "4 0 0\r\n1 -1\r\n1 3\r\n3 3\r\n3 -1\r\n", "output": "53.407075111026482965\r\n"}, {"input": "3 0 0\r\n-10 1\r\n0 2\r\n1 1\r\n", "output": "314.1592653589793116\r\n"}, {"input": "3 0 0\r\n-1 1\r\n4 1\r\n0 2\r\n", "output": "50.265482457436689849\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(output_path, 'r') as f: ref_line = f.readline().strip() try: b = float(ref_line) except: print(0) return with open(submission_path, 'r') as f: sub_line = f.readline().strip() try: a = float(sub_line) except: print(0) return denominator = max(1.0, b) absolute_error = abs(a - b) if absolute_error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
613/A
613
A
Python 3
TESTS
2
46
0
29682373
import math n,p1,p2 = list(map(float,input().split())) inp = [] mn = 1000000000000.0 mx = 0 for i in range(int(n)): p = tuple(map(float,input().split())) p = (p[0]-p1,p[1]-p2) inp.append(p) d = (p[0]**2) + (p[1]**2) mn = min(mn,d) mx = max(mx,d) ans = math.pi * (mx-mn) print(ans)
60
686
8,192,000
15597140
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import math def dist2(x1, y1, x2, y2): return (x1 - x2) ** 2 + (y1 - y2) ** 2 def find_dist(x1, y1, x2, y2, X, Y): if x2 == x1: if min(y1, y2) <= Y <= max(y1, y2): return (x1 - X) ** 2 else: return min(dist2(x1, y1, X, Y), dist2(x2, y2, X, Y)) a = (y2 - y1) / (x2 - x1) b = y1 - a * x1 x = (X + a * (Y - b)) / (1 + a * a) if min(x1, x2) <= x <= max(x1, x2): return dist2(x, a * x + b, X, Y) else: return min(dist2(x1, y1, X, Y), dist2(x2, y2, X, Y)) def main(): N, X, Y = map(int, sys.stdin.readline().split()) maxr = 0 minr = 10**15 pts = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)] pts.append(pts[0]) for i in range(0, len(pts)-1): x, y = pts[i] r = dist2(x, y, X, Y) if r > maxr: maxr = r r = find_dist(x, y, pts[i+1][0], pts[i+1][1], X, Y) if r < minr: minr = r print ("%.10f" % (math.pi * (maxr - minr))) if __name__ == '__main__': main()
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Peter and Snow Blower
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path. Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine. Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.
The first line of the input contains three integers — the number of vertices of the polygon n ($$3 \leq n \leq 100\ 000$$), and coordinates of point P. Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line. All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample snow will be removed from that area:
[{"input": "3 0 0\n0 1\n-1 2\n1 2", "output": "12.566370614359172464"}, {"input": "4 1 -1\n0 0\n1 2\n2 0\n1 1", "output": "21.991148575128551812"}]
1,900
["binary search", "geometry", "ternary search"]
60
[{"input": "3 0 0\r\n0 1\r\n-1 2\r\n1 2\r\n", "output": "12.566370614359172464\r\n"}, {"input": "4 1 -1\r\n0 0\r\n1 2\r\n2 0\r\n1 1\r\n", "output": "21.991148575128551812\r\n"}, {"input": "3 0 0\r\n-1 1\r\n0 3\r\n1 1\r\n", "output": "25.132741228718344928\r\n"}, {"input": "3 -4 2\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "405.26545231308331191\r\n"}, {"input": "3 -84 8\r\n-83 8\r\n21 -62\r\n3 53\r\n", "output": "50026.721415763865583\r\n"}, {"input": "6 -94 -51\r\n-93 -51\r\n48 -25\r\n61 27\r\n73 76\r\n-10 87\r\n-48 38\r\n", "output": "138283.48383306192359\r\n"}, {"input": "5 -94 52\r\n-93 52\r\n-78 -56\r\n-54 -81\r\n56 -87\r\n97 85\r\n", "output": "131381.40477312514811\r\n"}, {"input": "10 -100 90\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198410.42563011697403\r\n"}, {"input": "11 -97 -15\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "133558.52848206287476\r\n"}, {"input": "10 -500 420\r\n-499 420\r\n-489 -173\r\n-455 -480\r\n160 -464\r\n374 -437\r\n452 -352\r\n481 -281\r\n465 75\r\n326 392\r\n-398 468\r\n", "output": "4719573.802783449531\r\n"}, {"input": "10 -498 -161\r\n-497 -161\r\n-427 -458\r\n-325 -475\r\n349 -500\r\n441 -220\r\n473 28\r\n475 62\r\n468 498\r\n-444 492\r\n-465 264\r\n", "output": "4295926.8918542123392\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n8 5\r\n10 7\r\n7 5\r\n2 5\r\n", "output": "574.91145560693214023\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n20 3\r\n26 17\r\n23 21\r\n98 96\r\n", "output": "60343.711690152746165\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n94 7\r\n100 52\r\n87 48\r\n37 26\r\n74 61\r\n59 57\r\n87 90\r\n52 90\r\n26 73\r\n", "output": "50337.739088469255101\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n78 22\r\n53 24\r\n78 50\r\n46 39\r\n45 56\r\n21 46\r\n2 7\r\n24 97\r\n5 59\r\n", "output": "32129.068068262814194\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n95 2\r\n47 1\r\n42 1\r\n93 7\r\n56 6\r\n47 7\r\n63 13\r\n98 24\r\n94 27\r\n90 28\r\n86 28\r\n17 6\r\n64 24\r\n42 19\r\n66 35\r\n63 35\r\n98 60\r\n75 48\r\n28 18\r\n71 46\r\n69 46\r\n99 68\r\n64 47\r\n56 43\r\n72 58\r\n35 29\r\n82 81\r\n68 69\r\n79 84\r\n72 77\r\n79 86\r\n54 59\r\n35 39\r\n20 23\r\n73 86\r\n80 97\r\n79 100\r\n69 99\r\n29 45\r\n26 63\r\n23 56\r\n12 33\r\n13 39\r\n25 85\r\n27 96\r\n6 23\r\n4 47\r\n1 60\r\n", "output": "52147.296456936975932\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n69 2\r\n74 7\r\n62 10\r\n64 15\r\n93 22\r\n78 22\r\n56 17\r\n86 29\r\n24 9\r\n91 43\r\n8 4\r\n90 50\r\n99 57\r\n39 23\r\n81 50\r\n91 58\r\n67 46\r\n95 66\r\n52 39\r\n91 69\r\n69 54\r\n93 84\r\n93 98\r\n70 80\r\n85 98\r\n30 39\r\n55 79\r\n41 59\r\n50 72\r\n57 88\r\n58 92\r\n58 94\r\n37 63\r\n43 87\r\n30 63\r\n19 40\r\n38 81\r\n40 86\r\n38 100\r\n2 6\r\n30 100\r\n23 89\r\n16 62\r\n11 49\r\n12 64\r\n9 52\r\n5 62\r\n1 88\r\n", "output": "58543.579099645794717\r\n"}, {"input": "27 -999899 136015\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600304470662.964855\r\n"}, {"input": "19 -995486 -247212\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257949833603.158278\r\n"}, {"input": "15 -994057 554462\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694832748836.689348\r\n"}, {"input": "23 -999840 738880\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831930831113.094931\r\n"}, {"input": "20 -999719 -377746\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331542740428.216614\r\n"}, {"input": "26 -999922 -339832\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127026556380.411608\r\n"}, {"input": "22 -999930 -362070\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335297542813.80731\r\n"}, {"input": "29 -999961 689169\r\n-999960 689169\r\n-999927 -938525\r\n-999735 -989464\r\n-993714 -997911\r\n-870186 -999686\r\n-796253 -999950\r\n-139940 -999968\r\n969552 -999972\r\n985446 -999398\r\n992690 -997295\r\n999706 -973137\r\n999898 -848630\r\n999997 -192297\r\n999969 773408\r\n999495 960350\r\n999143 981671\r\n998324 993987\r\n997640 998103\r\n986157 998977\r\n966840 999418\r\n670113 999809\r\n477888 999856\r\n129160 999900\r\n-373564 999947\r\n-797543 999976\r\n-860769 999903\r\n-995496 999355\r\n-998771 984570\r\n-999768 927157\r\n", "output": "21409384775316.574772\r\n"}, {"input": "3 -3 3\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "399.0305992005743379\r\n"}, {"input": "3 -9 7\r\n-9 6\r\n3 -6\r\n4 2\r\n", "output": "980.17690792001545219\r\n"}, {"input": "5 -9 8\r\n-9 7\r\n-6 -1\r\n-3 -6\r\n1 -3\r\n10 8\r\n", "output": "1130.9820337250702449\r\n"}, {"input": "6 -6 -1\r\n-6 -2\r\n0 -7\r\n8 -9\r\n9 -1\r\n5 10\r\n-5 0\r\n", "output": "816.18577140262825159\r\n"}, {"input": "10 -99 91\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198309.89857373595223\r\n"}, {"input": "11 -96 -14\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "131821.20868619133483\r\n"}, {"input": "13 -98 25\r\n-98 24\r\n-96 10\r\n-80 -71\r\n-71 -78\r\n-31 -99\r\n82 -98\r\n92 -39\r\n94 -2\r\n94 40\r\n90 80\r\n50 96\r\n-41 97\r\n-86 80\r\n", "output": "149316.61930888936332\r\n"}, {"input": "17 -99 -53\r\n-99 -54\r\n-97 -71\r\n-67 -99\r\n-61 -99\r\n56 -98\r\n82 -85\r\n95 -47\r\n90 -2\r\n82 30\r\n63 87\r\n54 95\r\n-12 99\r\n-38 99\r\n-87 89\r\n-90 87\r\n-95 67\r\n-96 49\r\n", "output": "144023.17094830233827\r\n"}, {"input": "19 -995485 -247211\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257930301545.657524\r\n"}, {"input": "15 -994056 554463\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694830011124.045712\r\n"}, {"input": "23 -999839 738881\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831929255745.74826\r\n"}, {"input": "20 -999718 -377745\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331521646100.671528\r\n"}, {"input": "26 -999921 -339831\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127005627407.454252\r\n"}, {"input": "22 -999929 -362069\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335276455623.960732\r\n"}, {"input": "27 -999898 136016\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600299044211.965457\r\n"}, {"input": "13 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999417 840\r\n999781 33421\r\n999994 131490\r\n999993 998865\r\n962080 999911\r\n629402 999973\r\n378696 999988\r\n53978 999788\r\n25311 999558\r\n6082 999282\r\n1565 998489\r\n", "output": "23547598153913.984406\r\n"}, {"input": "16 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999744 572\r\n999931 96510\r\n1000000 254372\r\n999939 748173\r\n999894 953785\r\n999683 986098\r\n999051 999815\r\n980586 999969\r\n637250 999988\r\n118331 999983\r\n27254 999966\r\n9197 999405\r\n4810 997733\r\n1661 995339\r\n", "output": "23547697574489.259052\r\n"}, {"input": "4 0 0\r\n1 -1\r\n1 3\r\n3 3\r\n3 -1\r\n", "output": "53.407075111026482965\r\n"}, {"input": "3 0 0\r\n-10 1\r\n0 2\r\n1 1\r\n", "output": "314.1592653589793116\r\n"}, {"input": "3 0 0\r\n-1 1\r\n4 1\r\n0 2\r\n", "output": "50.265482457436689849\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(output_path, 'r') as f: ref_line = f.readline().strip() try: b = float(ref_line) except: print(0) return with open(submission_path, 'r') as f: sub_line = f.readline().strip() try: a = float(sub_line) except: print(0) return denominator = max(1.0, b) absolute_error = abs(a - b) if absolute_error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
613/A
613
A
Python 3
TESTS
2
31
0
229272525
pi = 3.141592653589793 p = [0,0] m = [] def distance(p, a): return ((a[0]-p[0])**2 + (a[1]-p[1])**2)**0.5 # [n, p[0], p[1]] = [int(i) for i in '3 0 0'.split()] [n, p[0], p[1]] = [int(i) for i in input().split()] for i in range(n): m.append( [int(i) for i in input().split()] ) distances = [distance(p, a) for a in m] r_min = min(distances) r_max = max(distances) print( pi * r_max**2 - pi * r_min**2 )
60
779
10,649,600
69397139
""" Peter and Snow Blower """ import math def cross(vecA, vecB): return abs(vecA[0] * vecB[1] - vecA[1] * vecB[0]) def l2_norm(pointA, pointB): return (pointA[0] - pointB[0]) ** 2 + (pointA[1] - pointB[1]) ** 2 def height5(P, A, B): a = l2_norm(A, P) b = l2_norm(B, P) base = l2_norm(A, B) # If obtuse triangles if a >= base + b or b >= base + a: return min(a, b) else: # If acute triangles vecA = (A[0] - P[0], A[1] - P[1]) vecB = (B[0] - P[0], B[1] - P[1]) area = cross(vecA, vecB) h = area * area / base return h def CF613A(): # Read number of points, center and the points N, cx, cy = list(map(int, input().split())) points = [tuple(map(int, input().split())) for _ in range(N)] # N, cx, cy = 3, 0, 0 # 12.56 # points = [(0, 1), (-1, 2), (1, 2)] # # N, cx, cy = 4, 1, -1 # 21.99 # points = [(0, 0), (1, 2), (2, 0), (1, 1)] # # N, cx, cy = 3, 0, 0 # 25.13 # points = [(-1, 1), (0, 3), (1, 1)] # # N, cx, cy = 3, -4, 2 # 405.26 # points = [(-3, 2), (5, -5), (5, 3)] # Compute the max distance center = (cx, cy) distances = [l2_norm(center, point) for point in points] max_radius = max(distances) # Compute the min distance min_radius = float('inf') for i in range(N): height = height5(center, points[i], points[(i + 1) % N]) min_radius = min(min_radius, height) area = math.pi * (max_radius - min_radius) return area if __name__ == '__main__': res = CF613A() print(res)
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Peter and Snow Blower
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path. Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine. Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.
The first line of the input contains three integers — the number of vertices of the polygon n ($$3 \leq n \leq 100\ 000$$), and coordinates of point P. Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line. All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample snow will be removed from that area:
[{"input": "3 0 0\n0 1\n-1 2\n1 2", "output": "12.566370614359172464"}, {"input": "4 1 -1\n0 0\n1 2\n2 0\n1 1", "output": "21.991148575128551812"}]
1,900
["binary search", "geometry", "ternary search"]
60
[{"input": "3 0 0\r\n0 1\r\n-1 2\r\n1 2\r\n", "output": "12.566370614359172464\r\n"}, {"input": "4 1 -1\r\n0 0\r\n1 2\r\n2 0\r\n1 1\r\n", "output": "21.991148575128551812\r\n"}, {"input": "3 0 0\r\n-1 1\r\n0 3\r\n1 1\r\n", "output": "25.132741228718344928\r\n"}, {"input": "3 -4 2\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "405.26545231308331191\r\n"}, {"input": "3 -84 8\r\n-83 8\r\n21 -62\r\n3 53\r\n", "output": "50026.721415763865583\r\n"}, {"input": "6 -94 -51\r\n-93 -51\r\n48 -25\r\n61 27\r\n73 76\r\n-10 87\r\n-48 38\r\n", "output": "138283.48383306192359\r\n"}, {"input": "5 -94 52\r\n-93 52\r\n-78 -56\r\n-54 -81\r\n56 -87\r\n97 85\r\n", "output": "131381.40477312514811\r\n"}, {"input": "10 -100 90\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198410.42563011697403\r\n"}, {"input": "11 -97 -15\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "133558.52848206287476\r\n"}, {"input": "10 -500 420\r\n-499 420\r\n-489 -173\r\n-455 -480\r\n160 -464\r\n374 -437\r\n452 -352\r\n481 -281\r\n465 75\r\n326 392\r\n-398 468\r\n", "output": "4719573.802783449531\r\n"}, {"input": "10 -498 -161\r\n-497 -161\r\n-427 -458\r\n-325 -475\r\n349 -500\r\n441 -220\r\n473 28\r\n475 62\r\n468 498\r\n-444 492\r\n-465 264\r\n", "output": "4295926.8918542123392\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n8 5\r\n10 7\r\n7 5\r\n2 5\r\n", "output": "574.91145560693214023\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n20 3\r\n26 17\r\n23 21\r\n98 96\r\n", "output": "60343.711690152746165\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n94 7\r\n100 52\r\n87 48\r\n37 26\r\n74 61\r\n59 57\r\n87 90\r\n52 90\r\n26 73\r\n", "output": "50337.739088469255101\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n78 22\r\n53 24\r\n78 50\r\n46 39\r\n45 56\r\n21 46\r\n2 7\r\n24 97\r\n5 59\r\n", "output": "32129.068068262814194\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n95 2\r\n47 1\r\n42 1\r\n93 7\r\n56 6\r\n47 7\r\n63 13\r\n98 24\r\n94 27\r\n90 28\r\n86 28\r\n17 6\r\n64 24\r\n42 19\r\n66 35\r\n63 35\r\n98 60\r\n75 48\r\n28 18\r\n71 46\r\n69 46\r\n99 68\r\n64 47\r\n56 43\r\n72 58\r\n35 29\r\n82 81\r\n68 69\r\n79 84\r\n72 77\r\n79 86\r\n54 59\r\n35 39\r\n20 23\r\n73 86\r\n80 97\r\n79 100\r\n69 99\r\n29 45\r\n26 63\r\n23 56\r\n12 33\r\n13 39\r\n25 85\r\n27 96\r\n6 23\r\n4 47\r\n1 60\r\n", "output": "52147.296456936975932\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n69 2\r\n74 7\r\n62 10\r\n64 15\r\n93 22\r\n78 22\r\n56 17\r\n86 29\r\n24 9\r\n91 43\r\n8 4\r\n90 50\r\n99 57\r\n39 23\r\n81 50\r\n91 58\r\n67 46\r\n95 66\r\n52 39\r\n91 69\r\n69 54\r\n93 84\r\n93 98\r\n70 80\r\n85 98\r\n30 39\r\n55 79\r\n41 59\r\n50 72\r\n57 88\r\n58 92\r\n58 94\r\n37 63\r\n43 87\r\n30 63\r\n19 40\r\n38 81\r\n40 86\r\n38 100\r\n2 6\r\n30 100\r\n23 89\r\n16 62\r\n11 49\r\n12 64\r\n9 52\r\n5 62\r\n1 88\r\n", "output": "58543.579099645794717\r\n"}, {"input": "27 -999899 136015\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600304470662.964855\r\n"}, {"input": "19 -995486 -247212\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257949833603.158278\r\n"}, {"input": "15 -994057 554462\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694832748836.689348\r\n"}, {"input": "23 -999840 738880\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831930831113.094931\r\n"}, {"input": "20 -999719 -377746\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331542740428.216614\r\n"}, {"input": "26 -999922 -339832\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127026556380.411608\r\n"}, {"input": "22 -999930 -362070\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335297542813.80731\r\n"}, {"input": "29 -999961 689169\r\n-999960 689169\r\n-999927 -938525\r\n-999735 -989464\r\n-993714 -997911\r\n-870186 -999686\r\n-796253 -999950\r\n-139940 -999968\r\n969552 -999972\r\n985446 -999398\r\n992690 -997295\r\n999706 -973137\r\n999898 -848630\r\n999997 -192297\r\n999969 773408\r\n999495 960350\r\n999143 981671\r\n998324 993987\r\n997640 998103\r\n986157 998977\r\n966840 999418\r\n670113 999809\r\n477888 999856\r\n129160 999900\r\n-373564 999947\r\n-797543 999976\r\n-860769 999903\r\n-995496 999355\r\n-998771 984570\r\n-999768 927157\r\n", "output": "21409384775316.574772\r\n"}, {"input": "3 -3 3\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "399.0305992005743379\r\n"}, {"input": "3 -9 7\r\n-9 6\r\n3 -6\r\n4 2\r\n", "output": "980.17690792001545219\r\n"}, {"input": "5 -9 8\r\n-9 7\r\n-6 -1\r\n-3 -6\r\n1 -3\r\n10 8\r\n", "output": "1130.9820337250702449\r\n"}, {"input": "6 -6 -1\r\n-6 -2\r\n0 -7\r\n8 -9\r\n9 -1\r\n5 10\r\n-5 0\r\n", "output": "816.18577140262825159\r\n"}, {"input": "10 -99 91\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198309.89857373595223\r\n"}, {"input": "11 -96 -14\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "131821.20868619133483\r\n"}, {"input": "13 -98 25\r\n-98 24\r\n-96 10\r\n-80 -71\r\n-71 -78\r\n-31 -99\r\n82 -98\r\n92 -39\r\n94 -2\r\n94 40\r\n90 80\r\n50 96\r\n-41 97\r\n-86 80\r\n", "output": "149316.61930888936332\r\n"}, {"input": "17 -99 -53\r\n-99 -54\r\n-97 -71\r\n-67 -99\r\n-61 -99\r\n56 -98\r\n82 -85\r\n95 -47\r\n90 -2\r\n82 30\r\n63 87\r\n54 95\r\n-12 99\r\n-38 99\r\n-87 89\r\n-90 87\r\n-95 67\r\n-96 49\r\n", "output": "144023.17094830233827\r\n"}, {"input": "19 -995485 -247211\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257930301545.657524\r\n"}, {"input": "15 -994056 554463\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694830011124.045712\r\n"}, {"input": "23 -999839 738881\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831929255745.74826\r\n"}, {"input": "20 -999718 -377745\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331521646100.671528\r\n"}, {"input": "26 -999921 -339831\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127005627407.454252\r\n"}, {"input": "22 -999929 -362069\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335276455623.960732\r\n"}, {"input": "27 -999898 136016\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600299044211.965457\r\n"}, {"input": "13 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999417 840\r\n999781 33421\r\n999994 131490\r\n999993 998865\r\n962080 999911\r\n629402 999973\r\n378696 999988\r\n53978 999788\r\n25311 999558\r\n6082 999282\r\n1565 998489\r\n", "output": "23547598153913.984406\r\n"}, {"input": "16 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999744 572\r\n999931 96510\r\n1000000 254372\r\n999939 748173\r\n999894 953785\r\n999683 986098\r\n999051 999815\r\n980586 999969\r\n637250 999988\r\n118331 999983\r\n27254 999966\r\n9197 999405\r\n4810 997733\r\n1661 995339\r\n", "output": "23547697574489.259052\r\n"}, {"input": "4 0 0\r\n1 -1\r\n1 3\r\n3 3\r\n3 -1\r\n", "output": "53.407075111026482965\r\n"}, {"input": "3 0 0\r\n-10 1\r\n0 2\r\n1 1\r\n", "output": "314.1592653589793116\r\n"}, {"input": "3 0 0\r\n-1 1\r\n4 1\r\n0 2\r\n", "output": "50.265482457436689849\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(output_path, 'r') as f: ref_line = f.readline().strip() try: b = float(ref_line) except: print(0) return with open(submission_path, 'r') as f: sub_line = f.readline().strip() try: a = float(sub_line) except: print(0) return denominator = max(1.0, b) absolute_error = abs(a - b) if absolute_error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
436/A
436
A
Python 3
PRETESTS
7
62
0
6876016
n, x = [int(c) for c in input().split()] candies = [[], []] for i in range(n): a = [int(c) for c in input().split()] if a[0] == 0: candies[0].append(a[1:]) else: candies[1].append(a[1:]) candies[0].sort(key=lambda x: x[1],reverse=True) candies[1].sort(key=lambda x: x[1],reverse=True) def find_best(ar, x): for i in range(len(ar)): if ar[i][0] <= x: return i return -1 eated_some = True startWith = 0 eated = 0 while eated_some: eated_some = False tmp = find_best(candies[startWith], x) if tmp == -1 and eated == 0: startWith = (startWith + 1) % 2 tmp = find_best(candies[startWith], x) #print(candies[startWith], x, tmp) if tmp != -1: eated_some = True eated += 1 tt = candies[startWith].pop(tmp) x = x + tt[1] startWith = (startWith + 1) % 2 print(eated)
60
62
307,200
10550532
import sys from operator import itemgetter def main(): str = sys.stdin.readline() tokens = str.split() n = int(tokens[0]) x = int(tokens[1]) s1=[] s2=[] for i in range(n): str = sys.stdin.readline() tokens = str.split() t = int(tokens[0]) h = int(tokens[1]) m = int(tokens[2]) if t==0: s1.append((h,m)) else: s2.append((h,m)) s1 = sorted(s1, key=itemgetter(0)) # secondary key, height, ascending s1 = sorted(s1, key=itemgetter(1), reverse=True) # primary key, mass, descending s2 = sorted(s2, key=itemgetter(0)) s2 = sorted(s2, key=itemgetter(1), reverse=True) s1A = s1[:] s2A = s2[:] jumpA = x candiesA = 0 currentA = s1A while True: found = 0 for i in range(len(currentA)): if currentA[i][0] <= jumpA: found = 1 candiesA = candiesA + 1 jumpA = jumpA + currentA[i][1] del currentA[i] break if found == 1: if currentA == s1A: currentA = s2A else: currentA = s1A else: break s1B = s1[:] s2B = s2[:] jumpB = x candiesB = 0 currentB = s2B while True: found = 0 for i in range(len(currentB)): if currentB[i][0] <= jumpB: found = 1 candiesB = candiesB + 1 jumpB = jumpB + currentB[i][1] del currentB[i] break if found == 1: if currentB == s1B: currentB = s2B else: currentB = s1B else: break print(max(candiesA,candiesB)) if __name__ == "__main__": main()
Zepto Code Rush 2014
CF
2,014
2
256
Feed with Candy
The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem. One day, Om Nom visited his friend Evan. Evan has n candies of two types (fruit drops and caramel drops), the i-th candy hangs at the height of hi centimeters above the floor of the house, its mass is mi. Om Nom wants to eat as many candies as possible. At the beginning Om Nom can make at most x centimeter high jumps. When Om Nom eats a candy of mass y, he gets stronger and the height of his jump increases by y centimeters. What maximum number of candies can Om Nom eat if he never eats two candies of the same type in a row (Om Nom finds it too boring)?
The first line contains two integers, n and x (1 ≤ n, x ≤ 2000) — the number of sweets Evan has and the initial height of Om Nom's jump. Each of the following n lines contains three integers ti, hi, mi (0 ≤ ti ≤ 1; 1 ≤ hi, mi ≤ 2000) — the type, height and the mass of the i-th candy. If number ti equals 0, then the current candy is a caramel drop, otherwise it is a fruit drop.
Print a single integer — the maximum number of candies Om Nom can eat.
null
One of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario: 1. Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3 + 4 = 7. 2. Now Om Nom can reach candies 2 and 5. Let's assume that he eats candy 5. Then the height of his jump will be 7 + 5 = 12. 3. At this moment, Om Nom can reach two candies, 2 and 3. He won't eat candy 2 as its type matches the type of the previously eaten candy. Om Nom eats candy 3, the height of his jump is 12 + 3 = 15. 4. Om Nom eats candy 2, the height of his jump is 15 + 1 = 16. He cannot reach candy 4.
[{"input": "5 3\n0 2 4\n1 3 1\n0 8 3\n0 20 10\n1 5 5", "output": "4"}]
1,500
["greedy"]
60
[{"input": "5 3\r\n0 2 4\r\n1 3 1\r\n0 8 3\r\n0 20 10\r\n1 5 5\r\n", "output": "4\r\n"}, {"input": "5 2\r\n1 15 2\r\n1 11 2\r\n0 17 2\r\n0 16 1\r\n1 18 2\r\n", "output": "0\r\n"}, {"input": "6 2\r\n1 17 3\r\n1 6 1\r\n0 4 2\r\n1 10 1\r\n1 7 3\r\n1 5 1\r\n", "output": "0\r\n"}, {"input": "7 2\r\n1 14 1\r\n1 9 2\r\n0 6 3\r\n0 20 2\r\n0 4 2\r\n0 3 1\r\n0 9 2\r\n", "output": "0\r\n"}, {"input": "8 2\r\n0 20 3\r\n1 5 2\r\n1 4 1\r\n1 7 1\r\n0 1 3\r\n1 5 3\r\n1 7 2\r\n1 3 1\r\n", "output": "2\r\n"}, {"input": "9 2\r\n0 1 1\r\n1 8 2\r\n1 11 1\r\n0 9 1\r\n1 18 2\r\n1 7 3\r\n1 8 3\r\n0 16 1\r\n0 12 2\r\n", "output": "1\r\n"}, {"input": "10 2\r\n0 2 3\r\n1 5 2\r\n0 7 3\r\n1 15 2\r\n0 14 3\r\n1 19 1\r\n1 5 3\r\n0 2 2\r\n0 10 2\r\n0 10 3\r\n", "output": "9\r\n"}, {"input": "2 1\r\n0 1 1\r\n1 2 1\r\n", "output": "2\r\n"}, {"input": "2 1\r\n1 1 1\r\n0 2 1\r\n", "output": "2\r\n"}, {"input": "2 1\r\n0 1 1\r\n0 2 1\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1 1\r\n1 2 1\r\n", "output": "1\r\n"}, {"input": "2 1\r\n0 1 1\r\n1 3 1\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1 1\r\n0 3 1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1 2 1\r\n", "output": "0\r\n"}, {"input": "3 4\r\n1 1 2\r\n1 4 100\r\n0 104 1\r\n", "output": "3\r\n"}, {"input": "3 4\r\n1 1 100\r\n1 4 2\r\n0 104 1\r\n", "output": "3\r\n"}, {"input": "3 100\r\n0 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "3\r\n"}, {"input": "4 20\r\n0 10 10\r\n0 20 50\r\n1 40 1\r\n1 40 1\r\n", "output": "4\r\n"}, {"input": "4 2\r\n0 1 1\r\n0 2 3\r\n1 4 1\r\n1 5 1\r\n", "output": "4\r\n"}, {"input": "3 10\r\n0 9 1\r\n0 10 10\r\n1 20 1\r\n", "output": "3\r\n"}, {"input": "3 5\r\n0 4 1\r\n0 5 10\r\n1 15 5\r\n", "output": "3\r\n"}, {"input": "3 4\r\n0 2 1\r\n0 3 3\r\n1 6 5\r\n", "output": "3\r\n"}, {"input": "3 3\r\n0 1 1\r\n0 2 100\r\n1 10 1\r\n", "output": "3\r\n"}, {"input": "3 2\r\n0 1 1\r\n0 2 2\r\n1 4 4\r\n", "output": "3\r\n"}, {"input": "5 3\r\n0 1 5\r\n0 1 5\r\n0 1 5\r\n1 1 10\r\n1 1 1\r\n", "output": "5\r\n"}, {"input": "3 2\r\n0 1 1\r\n0 2 2\r\n1 4 2\r\n", "output": "3\r\n"}, {"input": "4 10\r\n0 20 1\r\n1 1 9\r\n1 2 11\r\n1 3 8\r\n", "output": "3\r\n"}, {"input": "7 1\r\n0 1 99\r\n1 100 1\r\n0 100 1\r\n0 101 1000\r\n1 1000 1\r\n0 1000 1\r\n1 1000 1\r\n", "output": "7\r\n"}, {"input": "4 3\r\n0 1 1\r\n0 2 100\r\n0 3 1\r\n1 100 1\r\n", "output": "3\r\n"}, {"input": "3 3\r\n0 1 100\r\n0 2 1\r\n1 100 100\r\n", "output": "3\r\n"}, {"input": "3 2\r\n0 1 1\r\n0 2 100\r\n1 10 1\r\n", "output": "3\r\n"}, {"input": "3 1\r\n0 1 1\r\n1 1 5\r\n0 7 1\r\n", "output": "3\r\n"}, {"input": "3 5\r\n0 2 3\r\n1 9 10\r\n0 4 4\r\n", "output": "3\r\n"}, {"input": "3 3\r\n0 2 1\r\n0 3 2\r\n1 5 10\r\n", "output": "3\r\n"}]
false
stdio
null
true