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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
652/B
|
652
|
B
|
Python 3
|
TESTS
| 4
| 46
| 0
|
203414002
|
def sort(n, arr):
if n == 1:
return arr
elif n == 2:
return arr
elif n == 3:
(arr[1], arr[2]) = (arr[2], arr[1])
return arr
newarr = [0] * n
newarr[0] = arr[0]
newarr[1] = arr[-1]
j = 1
for i in range(2, n, 2):
newarr[i] = arr[j]
j += 1
if n % 2 == 0:
j = n - 1
else:
j = n - 2
for i in range(3, n, 2):
newarr[i] = arr[j]
j -= 1
return newarr
n = int(input())
arr = sorted(list(map(int, input().split())))
print(*sort(n, arr))
| 16
| 31
| 0
|
158245507
|
a=int(input())
b=sorted(map(int,input().split()))
i=0
while(i<a//2):
print(b[i],b[-i-1],end=" ")
i+=1
if a%2:print(b[a//2])
|
Educational Codeforces Round 10
|
ICPC
| 2,016
| 1
| 256
|
z-sort
|
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:
1. ai ≥ ai - 1 for all even i,
2. ai ≤ ai - 1 for all odd i > 1.
For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.
Can you make the array z-sorted?
|
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
|
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
| null | null |
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
| 1,000
|
["sortings"]
| 16
|
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().split()))
a_sorted = sorted(a)
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f]
ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible')
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f]
if not sub_lines:
print(0)
return
sub_output = sub_lines[0].split()
sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible')
# Check if submission matches the reference's impossible status
if ref_is_impossible:
print(1 if sub_is_impossible else 0)
return
else:
if sub_is_impossible:
print(0)
return
# Check if the submission is a valid permutation and meets conditions
if len(sub_output) != n:
print(0)
return
try:
sub_array = list(map(int, sub_output))
except:
print(0)
return
if sorted(sub_array) != a_sorted:
print(0)
return
# Check z-sort conditions
valid = True
for i in range(1, n):
if (i + 1) % 2 == 0: # 1-based even index
if sub_array[i] < sub_array[i - 1]:
valid = False
break
else: # 1-based odd index > 1
if sub_array[i] > sub_array[i - 1]:
valid = False
break
print(1 if valid else 0)
if __name__ == "__main__":
main()
| true
|
239/B
|
239
|
B
|
Python 3
|
TESTS
| 3
| 62
| 0
|
184154428
|
def print_digits(seq):
printed_digits = []
cp = 0
dp = 1
prev_sign = False
while cp >= 0 and cp < len(seq):
# if CP is pointing to a digit
if seq[cp].isdigit():
# interpreter prints that digit
printed_digits.append(seq[cp])
# the value of the sequence is decreased by one
# but if it is 0 it is removed from the sequence
if seq[cp] == '0':
seq.pop(cp)
else:
seq[cp] = str(int(seq[cp]) - 1)
# it indicates the last char was not a sign
prev_sign = False
else:
# if previous char was a sign, remove it
if prev_sign:
seq.pop(cp)
cp -= 1
# it indicates the last char was a sign
prev_sign = True
# it sets the direction correspondingly
if seq[cp] == '>':
dp = 1
else:
dp = -1
# moves the CP
cp += dp
return printed_digits
def solve():
n, q = map(int, input().split())
seq = [*input().strip()]
queries = [list(map(int, input().split())) for _ in range(q)]
for q in queries:
digits_count = [0] * 10
printed_digits = print_digits(seq[q[0]-1:q[1]])
for pd in printed_digits:
digits_count[int(pd)] += 1
print(' '.join(str(d) for d in digits_count))
solve()
| 33
| 92
| 0
|
154844086
|
n, q = map(int, input().split())
S = [-1]+list(map(lambda x: ord(x)-ord('0'), input()))+[-1]
D = [12, 14]
for _ in range(q):
l, r = map(int, input().split())
s = S[l-1:r+2]
s[0] = s[-1] = -1
c = [0]*10
dp, p = 1, 1
i = s[p]
while i!= -1:
if i==14:
dp = 1
if s[p+1] in D:
s.pop(p)
else:
p += 1
elif i==12:
dp = -1
p -= 1
if s[p] in D: s.pop(p+1)
else:
c[i] += 1
if i==0: s.pop(p)
else: s[p] -= 1
p += dp
if i==0 and dp==1: p -= 1
i = s[p]
print(' '.join(map(str, c)))
|
Codeforces Round 148 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Easy Tape Programming
|
There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts.
- Current character pointer (CP);
- Direction pointer (DP) which can point left or right;
Initially CP points to the leftmost character of the sequence and DP points to the right.
We repeat the following steps until the first moment that CP points to somewhere outside the sequence.
- If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one.
- If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence.
If at any moment the CP goes outside of the sequence the execution is terminated.
It's obvious the every program in this language terminates after some steps.
We have a sequence s1, s2, ..., sn of "<", ">" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, ..., sr as an independent program in this language.
|
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100) — represents the length of the sequence s and the number of queries.
The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces.
The next q lines each contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the i-th query.
|
For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input.
| null | null |
[{"input": "7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7", "output": "0 1 0 1 0 0 0 0 0 0\n2 2 2 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n2 3 2 1 0 0 0 0 0 0"}]
| 1,500
|
["brute force", "implementation"]
| 33
|
[{"input": "7 4\r\n1>3>22<\r\n1 3\r\n4 7\r\n7 7\r\n1 7\r\n", "output": "0 1 0 1 0 0 0 0 0 0 \r\n2 2 2 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n2 3 2 1 0 0 0 0 0 0 \r\n"}, {"input": "5 2\r\n>>>>>\r\n1 5\r\n1 2\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 3\r\n9\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0 0 0 0 0 0 0 0 0 1 \r\n0 0 0 0 0 0 0 0 0 1 \r\n0 0 0 0 0 0 0 0 0 1 \r\n"}, {"input": "7 1\r\n0101010\r\n1 7\r\n", "output": "4 3 0 0 0 0 0 0 0 0 \r\n"}, {"input": "10 30\r\n306<<>4>04\r\n2 2\r\n6 6\r\n1 10\r\n1 8\r\n2 4\r\n9 10\r\n2 8\r\n3 5\r\n7 7\r\n2 6\r\n1 3\r\n3 7\r\n4 9\r\n3 10\r\n5 9\r\n7 10\r\n1 3\r\n5 7\r\n4 10\r\n6 10\r\n6 7\r\n4 5\r\n3 4\r\n4 6\r\n4 7\r\n7 9\r\n4 6\r\n2 8\r\n1 5\r\n2 6\r\n", "output": "1 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n1 0 1 1 0 1 1 0 0 0 \n1 0 1 1 0 1 1 0 0 0 \n1 0 0 0 0 1 1 0 0 0 \n1 0 0 0 1 0 0 0 0 0 \n1 0 0 0 0 1 1 0 0 0 \n0 0 0 0 0 1 1 0 0 0 \n0 0 0 0 1 0 0 0 0 0 \n1 0 0 0 0 1 1 0 0 0 \n1 0 0 1 0 0 1 0 0 0 \n0 0 0 0 0 1 1 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 1 1 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 2 0 0 0 0 0 \n1 0 0 1 0 0 1 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 2 0 0 0 0 0 \n0 0 0 0 1 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 1 1 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 1 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 1 1 0 0 0 \n1 0 1 1 0 1 1 0 0 0 \n1 0 0 0 0 1 1 0 0 0 \n\n"}, {"input": "17 21\r\n187<9>82<818<4229\r\n8 14\r\n4 10\r\n11 17\r\n8 8\r\n4 12\r\n6 6\r\n5 12\r\n10 12\r\n15 16\r\n7 7\r\n3 8\r\n4 8\r\n8 9\r\n8 10\r\n5 7\r\n1 7\r\n11 12\r\n3 6\r\n6 11\r\n8 16\r\n6 9\r\n", "output": "0 1 1 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n1 1 0 0 0 0 0 1 1 0 \n0 0 1 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n2 3 2 1 1 1 1 1 3 1 \n0 1 0 0 0 0 0 0 2 0 \n0 0 2 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 1 0 \n0 0 0 0 0 0 1 1 0 0 \n0 0 0 0 0 0 0 0 0 0 \n0 1 1 0 0 0 0 0 0 0 \n0 1 1 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 1 1 \n1 1 0 0 0 0 1 2 1 0 \n0 1 0 0 0 0 0 0 1 0 \n0 0 0 0 0 0 1 1 0 0 \n2 3 2 1 1 1 1 1 2 0 \n0 1 1 0 0 0 0 0 0 0 \n2 2 2 1 1 1 1 1 1 0 \n\n"}, {"input": "21 33\r\n007317842806111438>67\r\n2 11\r\n3 21\r\n3 12\r\n5 8\r\n14 14\r\n10 14\r\n9 17\r\n7 17\r\n1 12\r\n12 18\r\n3 10\r\n2 20\r\n5 5\r\n10 13\r\n14 20\r\n2 19\r\n1 13\r\n6 11\r\n6 9\r\n8 13\r\n16 16\r\n1 15\r\n18 20\r\n12 12\r\n7 20\r\n3 11\r\n13 21\r\n3 11\r\n12 13\r\n8 15\r\n13 17\r\n5 5\r\n2 16\r\n", "output": "2 1 1 1 1 0 0 2 2 0 \n1 4 1 2 2 0 2 3 3 0 \n1 1 1 1 1 0 1 2 2 0 \n0 1 0 0 1 0 0 1 1 0 \n0 1 0 0 0 0 0 0 0 0 \n1 2 0 0 0 0 1 0 1 0 \n1 3 1 1 1 0 1 0 1 0 \n1 3 1 1 2 0 1 0 2 0 \n3 1 1 1 1 0 1 2 2 0 \n0 3 0 1 1 0 1 0 1 0 \n0 1 1 1 1 0 0 2 2 0 \n2 4 1 2 2 0 2 2 3 0 \n0 1 0 0 0 0 0 0 0 0 \n1 1 0 0 0 0 1 0 1 0 \n0 2 0 1 1 0 1 0 1 0 \n2 4 1 2 2 0 1 2 3 0 \n3 2 1 1 1 0 1 2 2 0 \n1 0 1 0 1 0 0 1 2 0 \n0 0 1 0 1 0 0 1 1 0 \n1 1 1 0 1 0 1 0 1 0 \n0 0 0 0 1 0 0 0 0 0 \n3 4 1 1 1 0 1 2 2 0 \n0 0 0 0 0 0 1 0 1 0 \n0 0 0 0 0 0 1 0 0 0 \n1 3 1 1 2 0 2 0 3 0 \n1 1 1 1 1 0 0 2 2 0 \n0 3 0 1 1 0 1 1 1 0 \n1 1 1 1 1 0 0 2 2 0 \n0 1 0 0 0 0 1 0 0 0 \n1 3 1 0 1 0 1 0 1 0 \n0 3 0 1 1 0 0 0 0 0 \n0 1 0 0 0 0 0 0 0 0 \n2 4 1 1 2 0 1 2 2 0 \n\n"}, {"input": "1 5\r\n<\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 2\r\n>\r\n1 1\r\n1 1\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 1\r\n0\r\n1 1\r\n", "output": "1 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "3 10\r\n<<<\r\n2 3\r\n3 3\r\n2 3\r\n3 3\r\n1 3\r\n1 1\r\n1 2\r\n3 3\r\n1 1\r\n2 2\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "5 1\r\n1>3><\r\n4 5\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "4 1\r\n217<\r\n1 4\r\n", "output": "1 2 1 0 0 0 1 1 0 0 \r\n"}, {"input": "4 1\r\n34><\r\n1 4\r\n", "output": "0 0 1 2 1 0 0 0 0 0 \r\n"}]
| false
|
stdio
| null | true
|
659/D
|
659
|
D
|
Python 3
|
TESTS
| 5
| 109
| 0
|
51043755
|
n = int(input())
coordenates = []
for _ in range(n+1):
d = input().split()
coordenates.append(d)
direction = 'N'
# print(coordenates)
# coordenates.pop(0)
# print(coordenates)
counter = 0
for p in range(1, len(coordenates)-1):
# print('Direcao: {}, p({}, {})'.format(direction, p[0], p[1]))
if direction == 'N' and coordenates[p][0] > coordenates[p+1][0]:
direction = "O"
counter +=1
elif direction == 'L' and coordenates[p][1] < coordenates[p+1][1]:
direction = "N"
counter +=1
elif direction == 'S' and coordenates[p][0] < coordenates[p+1][0]:
direction = "L"
counter +=1
elif direction == 'O' and coordenates[p][1] > coordenates[p+1][1]:
direction = "S"
counter +=1
elif direction =='N':
direction = 'L'
elif direction == 'L':
direction = 'S'
elif direction == 'O':
direction = 'N'
elif direction == 'S':
direction = 'O'
current = p
print(counter)
# print(coordenates)
| 22
| 46
| 0
|
198028928
|
if __name__ == "__main__":
n = int(input())
x_anterior = None
y_anterior = None
sentido_atual = None
curvas_perigosas = 0
for i in range(n + 1):
x, y = map(int, input().split())
if x_anterior is None:
x_anterior = x
y_anterior = y
continue
diferenca_x = x - x_anterior
diferenca_y = y - y_anterior
if diferenca_y < 0:
if sentido_atual == 'O':
curvas_perigosas += 1
sentido_atual = 'S'
elif diferenca_y > 0:
if sentido_atual == 'L':
curvas_perigosas += 1
sentido_atual = 'N'
elif diferenca_x < 0:
if sentido_atual == 'N':
curvas_perigosas += 1
sentido_atual = 'O'
elif diferenca_x > 0:
if sentido_atual == 'S':
curvas_perigosas += 1
sentido_atual = 'L'
x_anterior = x
y_anterior = y
print(curvas_perigosas)
|
Codeforces Round 346 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Bicycle Race
|
Maria participates in a bicycle race.
The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.
Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).
Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.
Help Maria get ready for the competition — determine the number of dangerous turns on the track.
|
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track.
The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).
It is guaranteed that:
- the first straight section is directed to the north;
- the southernmost (and if there are several, then the most western of among them) point of the track is the first point;
- the last point coincides with the first one (i.e., the start position);
- any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point);
- no pair of points (except for the first and last one) is the same;
- no two adjacent straight sections are directed in the same direction or in opposite directions.
|
Print a single integer — the number of dangerous turns on the track.
| null |
The first sample corresponds to the picture:
The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
|
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
| 1,500
|
["geometry", "implementation", "math"]
| 22
|
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}, {"input": "4\r\n6 8\r\n6 9\r\n7 9\r\n7 8\r\n6 8\r\n", "output": "0\r\n"}, {"input": "8\r\n-10000 -10000\r\n-10000 5000\r\n0 5000\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n0 -10000\r\n-10000 -10000\r\n", "output": "2\r\n"}, {"input": "20\r\n-4286 -10000\r\n-4286 -7778\r\n-7143 -7778\r\n-7143 -3334\r\n-10000 -3334\r\n-10000 1110\r\n-4286 1110\r\n-4286 -3334\r\n4285 -3334\r\n4285 -1112\r\n7142 -1112\r\n7142 3332\r\n4285 3332\r\n4285 9998\r\n9999 9998\r\n9999 -3334\r\n7142 -3334\r\n7142 -5556\r\n-1429 -5556\r\n-1429 -10000\r\n-4286 -10000\r\n", "output": "8\r\n"}, {"input": "24\r\n-10000 -10000\r\n-10000 9998\r\n9998 9998\r\n9998 -10000\r\n-6364 -10000\r\n-6364 6362\r\n6362 6362\r\n6362 -6364\r\n-2728 -6364\r\n-2728 2726\r\n2726 2726\r\n2726 -910\r\n908 -910\r\n908 908\r\n-910 908\r\n-910 -4546\r\n4544 -4546\r\n4544 4544\r\n-4546 4544\r\n-4546 -8182\r\n8180 -8182\r\n8180 8180\r\n-8182 8180\r\n-8182 -10000\r\n-10000 -10000\r\n", "output": "10\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-6000 6000\r\n-6000 2000\r\n10000 2000\r\n10000 -2000\r\n-6000 -2000\r\n-6000 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-9800 6000\r\n-9800 2000\r\n10000 2000\r\n10000 -2000\r\n-9800 -2000\r\n-9800 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "4\r\n0 0\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
652/B
|
652
|
B
|
Python 3
|
TESTS
| 4
| 46
| 6,963,200
|
131528755
|
# link : https://codeforces.com/problemset/problem/652/B
# sorting
def check(l):
for i in range(len(l)):
if (i+1)%2==0:
if not l[i]>=l[i-1]:
return False
else:
if not l[i]<=l[i-1]:
return False
return True
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
L = l[1:(n//2)+1]
R = l[(n//2)+1:]
# print(L,R)
result = [l[0]]
flag = 'r'
for i in range(1,n):
len_l=len(L)
len_r=len(R)
r=0
l=0
while len_r:
result.append(R[r])
if check(result):
# print('R:',R,r,result)
R.pop(r)
r=0
break
else:
result.pop(-1)
r+=1
len_r-=1
else:
while len_l:
result.append(L[l])
if check(result):
# print('L:',L,l,result)
L.pop(l)
l=0
break
else:
result.pop(-1)
l+=1
len_l-=1
else:
print('Impossible')
break
else:
print(*result)
| 16
| 31
| 0
|
158697219
|
n = int(input())
a = sorted(list(map(int,input().split())))
d = []
for i in range(len(a)):
d += [a[i]]
d += [a[n - 1 - i]]
if n+n<n:
d += [a[n]]
print(*d[0:len(a)])
|
Educational Codeforces Round 10
|
ICPC
| 2,016
| 1
| 256
|
z-sort
|
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:
1. ai ≥ ai - 1 for all even i,
2. ai ≤ ai - 1 for all odd i > 1.
For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.
Can you make the array z-sorted?
|
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
|
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
| null | null |
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
| 1,000
|
["sortings"]
| 16
|
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().split()))
a_sorted = sorted(a)
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f]
ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible')
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f]
if not sub_lines:
print(0)
return
sub_output = sub_lines[0].split()
sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible')
# Check if submission matches the reference's impossible status
if ref_is_impossible:
print(1 if sub_is_impossible else 0)
return
else:
if sub_is_impossible:
print(0)
return
# Check if the submission is a valid permutation and meets conditions
if len(sub_output) != n:
print(0)
return
try:
sub_array = list(map(int, sub_output))
except:
print(0)
return
if sorted(sub_array) != a_sorted:
print(0)
return
# Check z-sort conditions
valid = True
for i in range(1, n):
if (i + 1) % 2 == 0: # 1-based even index
if sub_array[i] < sub_array[i - 1]:
valid = False
break
else: # 1-based odd index > 1
if sub_array[i] > sub_array[i - 1]:
valid = False
break
print(1 if valid else 0)
if __name__ == "__main__":
main()
| true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 108
| 102,400
|
48473229
|
Initial = input()
n = int(input())
print(Initial)
for i in range(n):
X = list(input().split())
Initial = Initial.replace(X[0], X[1])
print(Initial)
| 57
| 46
| 0
|
146008993
|
v1,v2 =map(str, input().split())
n = int(input())
x=[]
x.append(v1)
x.append(v2)
print(*x)
for i in range(n):
v11,v12 = map(str,input().split())
x.remove(v11)
x.append(v12)
print(*x)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 62
| 0
|
110443681
|
initial = input()
n = int(input())
for i in range(n):
line = input().split()
print(initial)
initial = initial.replace(line[0],line[1])
print(initial)
| 57
| 46
| 0
|
146789097
|
names = set(input().split(' '))
print(' '.join(names))
n = int(input())
for i in range(n):
a,b = input().split(' ')
if a in names:
names.remove(a)
names.add(b)
else:
names.remove(b)
names.add(a)
print(' '.join(names))
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
48/C
|
48
|
C
|
PyPy 3
|
TESTS
| 3
| 154
| 0
|
131959952
|
from sys import stdin
inp = stdin.readline
n = int(inp())
arr = [int(x) for x in inp().split()]
arr = ([0] + arr)
diff = [arr[i] - arr[i-1] for i in range(n)]
d1 = d2 = 0
length = 0
for i in range(n-1):
if diff[i] < diff[i+1]:
d1 = diff[i]
d2 = diff[i+1]
length = i + 1
break
if d1 == 0 or diff.count(d1) == diff.count(d2) == 1:
print("not unique")
else:
print("unique")
if diff.count(d1) > diff.count(d2):
d1, d2 = d2, d1
if diff[n-1] == d2 and diff[::-1].index(d1) == length:
print(arr[n] + d1)
else:
print(arr[n] + d2)
| 45
| 124
| 819,200
|
32153506
|
n = int(input())
zap = list(map(int, input().split()))
upperbound = float('inf')
lowerbound = -float('inf')
counter = 1
for item in zap:
upperbound = min(upperbound, 10/counter + 10 * item / counter)
counter += 1
k = 1
for item in zap:
lowerbound = max(lowerbound, item * 10 / k)
k += 1
benzonthelastmax = (1 + len(zap)) * (upperbound - 10**(-6)) - 10 * zap[-1]
benzonthelastmin = (1 + len(zap)) * (lowerbound + 10**(-6)) - 10 * zap[-1]
if(benzonthelastmin // 10 == benzonthelastmax // 10):
print("unique")
print(int(zap[-1] + benzonthelastmin // 10))
else:
print("not unique")
|
School Personal Contest #3 (Winter Computer School 2010/11) - Codeforces Beta Round 45 (ACM-ICPC Rules)
|
ICPC
| 2,010
| 2
| 256
|
The Race
|
Every year a race takes place on the motorway between cities A and B. This year Vanya decided to take part in the race and drive his own car that has been around and bears its own noble name — The Huff-puffer.
So, Vasya leaves city A on the Huff-puffer, besides, at the very beginning he fills the petrol tank with α liters of petrol (α ≥ 10 is Vanya's favorite number, it is not necessarily integer). Petrol stations are located on the motorway at an interval of 100 kilometers, i.e. the first station is located 100 kilometers away from the city A, the second one is 200 kilometers away from the city A, the third one is 300 kilometers away from the city A and so on. The Huff-puffer spends 10 liters of petrol every 100 kilometers.
Vanya checks the petrol tank every time he passes by a petrol station. If the petrol left in the tank is not enough to get to the next station, Vanya fills the tank with α liters of petrol. Otherwise, he doesn't stop at the station and drives on.
For example, if α = 43.21, then the car will be fuelled up for the first time at the station number 4, when there'll be 3.21 petrol liters left. After the fuelling up the car will have 46.42 liters. Then Vanya stops at the station number 8 and ends up with 6.42 + 43.21 = 49.63 liters. The next stop is at the station number 12, 9.63 + 43.21 = 52.84. The next stop is at the station number 17 and so on.
You won't believe this but the Huff-puffer has been leading in the race! Perhaps it is due to unexpected snow. Perhaps it is due to video cameras that have been installed along the motorway which register speed limit breaking. Perhaps it is due to the fact that Vanya threatened to junk the Huff-puffer unless the car wins. Whatever the reason is, the Huff-puffer is leading, and jealous people together with other contestants wrack their brains trying to think of a way to stop that outrage.
One way to do this is to mine the next petrol station where Vanya will stop. Your task is to calculate at which station this will happen and warn Vanya. You don't know the α number, however, you are given the succession of the numbers of the stations where Vanya has stopped. Find the number of the station where the next stop will be.
|
The first line contains an integer n (1 ≤ n ≤ 1000) which represents the number of petrol stations where Vanya has stopped. The next line has n space-separated integers which represent the numbers of the stations. The numbers are positive and do not exceed 106, they are given in the increasing order. No two numbers in the succession match. It is guaranteed that there exists at least one number α ≥ 10, to which such a succession of stops corresponds.
|
Print in the first line "unique" (without quotes) if the answer can be determined uniquely. In the second line print the number of the station where the next stop will take place. If the answer is not unique, print in the first line "not unique".
| null |
In the second example the answer is not unique. For example, if α = 10, we'll have such a sequence as 1, 2, 3, and if α = 14, the sequence will be 1, 2, 4.
|
[{"input": "3\n1 2 4", "output": "unique\n5"}, {"input": "2\n1 2", "output": "not unique"}]
| 1,800
|
["math"]
| 45
|
[{"input": "3\r\n1 2 4\r\n", "output": "unique\r\n5\r\n"}, {"input": "2\r\n1 2\r\n", "output": "not unique\r\n"}, {"input": "1\r\n5\r\n", "output": "not unique\r\n"}, {"input": "3\r\n1 3 4\r\n", "output": "unique\r\n6\r\n"}, {"input": "5\r\n1 2 3 5 6\r\n", "output": "unique\r\n7\r\n"}, {"input": "6\r\n1 2 3 5 6 7\r\n", "output": "not unique\r\n"}, {"input": "10\r\n1 2 4 5 7 8 9 11 12 14\r\n", "output": "unique\r\n15\r\n"}, {"input": "10\r\n1 3 5 6 8 10 12 13 15 17\r\n", "output": "not unique\r\n"}, {"input": "9\r\n2 5 7 10 12 15 17 20 22\r\n", "output": "unique\r\n25\r\n"}, {"input": "10\r\n7 14 21 28 35 42 49 56 63 70\r\n", "output": "not unique\r\n"}, {"input": "15\r\n5 11 16 22 28 33 39 45 50 56 62 67 73 79 84\r\n", "output": "unique\r\n90\r\n"}, {"input": "17\r\n5 11 16 22 28 33 39 45 50 56 62 67 73 79 84 90 96\r\n", "output": "unique\r\n101\r\n"}, {"input": "15\r\n1 3 4 6 8 9 11 12 14 16 17 19 21 22 24\r\n", "output": "unique\r\n25\r\n"}, {"input": "16\r\n1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25\r\n", "output": "unique\r\n27\r\n"}, {"input": "17\r\n1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25 27\r\n", "output": "unique\r\n29\r\n"}, {"input": "18\r\n1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25 27 29\r\n", "output": "unique\r\n30\r\n"}, {"input": "19\r\n1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25 27 29 30\r\n", "output": "unique\r\n32\r\n"}, {"input": "20\r\n1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25 27 29 30 32\r\n", "output": "not unique\r\n"}, {"input": "18\r\n49999 99998 149998 199997 249996 299996 349995 399994 449994 499993 549992 599992 649991 699990 749990 799989 849988 899988\r\n", "output": "unique\r\n949987\r\n"}, {"input": "20\r\n49999 99998 149998 199997 249996 299996 349995 399994 449994 499993 549992 599992 649991 699990 749990 799989 849988 899988 949987 999986\r\n", "output": "unique\r\n1049986\r\n"}, {"input": "33\r\n9 19 29 39 49 59 69 79 89 99 109 119 129 139 149 159 168 178 188 198 208 218 228 238 248 258 268 278 288 298 308 318 327\r\n", "output": "unique\r\n337\r\n"}, {"input": "46\r\n40 81 122 162 203 244 284 325 366 406 447 488 528 569 610 650 691 732 772 813 854 894 935 976 1016 1057 1098 1138 1179 1220 1260 1301 1342 1382 1423 1464 1504 1545 1586 1626 1667 1708 1748 1789 1830 1870\r\n", "output": "unique\r\n1911\r\n"}]
| false
|
stdio
| null | true
|
652/B
|
652
|
B
|
Python 3
|
TESTS
| 4
| 46
| 4,608,000
|
16939827
|
n = int(input())
k = sorted(map(int, input().split()))
w = -(-n // 2)
print(*[k[(w * i) % n] for i in range(n)])
| 16
| 31
| 0
|
158726733
|
n=int(input())
a=input().split()[:]
for i in range(n):
a[i]=int(a[i])
b=sorted(a)
for i in range(1,len(a),2):
b.insert(i,b[len(b)-1])
b.pop(len(b)-1)
for i in range(len(b)):
print(b[i],end=' ')
|
Educational Codeforces Round 10
|
ICPC
| 2,016
| 1
| 256
|
z-sort
|
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:
1. ai ≥ ai - 1 for all even i,
2. ai ≤ ai - 1 for all odd i > 1.
For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.
Can you make the array z-sorted?
|
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
|
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
| null | null |
[{"input": "4\n1 2 2 1", "output": "1 2 1 2"}, {"input": "5\n1 3 2 2 5", "output": "1 5 2 3 2"}]
| 1,000
|
["sortings"]
| 16
|
[{"input": "4\r\n1 2 2 1\r\n", "output": "1 2 1 2\r\n"}, {"input": "5\r\n1 3 2 2 5\r\n", "output": "1 5 2 3 2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\n", "output": "1 9 1 8 2 7 3 7 4 6\r\n"}, {"input": "100\r\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\r\n", "output": "3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\r\n"}, {"input": "3\r\n1 2 6\r\n", "output": "1 6 2\r\n"}, {"input": "136\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1 3 2\r\n"}, {"input": "7\r\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\r\n", "output": "999999998 1000000000 999999999 999999999 999999999 999999999 999999999\r\n"}, {"input": "3\r\n100 1 2\r\n", "output": "1 100 2\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().split()))
a_sorted = sorted(a)
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f]
ref_is_impossible = (len(ref_lines) > 0 and ref_lines[0].lower() == 'impossible')
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f]
if not sub_lines:
print(0)
return
sub_output = sub_lines[0].split()
sub_is_impossible = (len(sub_output) == 1 and sub_output[0].lower() == 'impossible')
# Check if submission matches the reference's impossible status
if ref_is_impossible:
print(1 if sub_is_impossible else 0)
return
else:
if sub_is_impossible:
print(0)
return
# Check if the submission is a valid permutation and meets conditions
if len(sub_output) != n:
print(0)
return
try:
sub_array = list(map(int, sub_output))
except:
print(0)
return
if sorted(sub_array) != a_sorted:
print(0)
return
# Check z-sort conditions
valid = True
for i in range(1, n):
if (i + 1) % 2 == 0: # 1-based even index
if sub_array[i] < sub_array[i - 1]:
valid = False
break
else: # 1-based odd index > 1
if sub_array[i] > sub_array[i - 1]:
valid = False
break
print(1 if valid else 0)
if __name__ == "__main__":
main()
| true
|
659/D
|
659
|
D
|
Python 3
|
TESTS
| 5
| 124
| 512,000
|
71407401
|
def onde(ponto1, ponto2):
if ponto1[0] > ponto2[0] and ponto1[1] == ponto2[1]:
return 'w'
if ponto1[0] < ponto2[0] and ponto1[1] == ponto2[1]:
return 'e'
if ponto1[0] == ponto2[0] and ponto1[1] < ponto2[1]:
return 'n'
if ponto1[0] == ponto2[0] and ponto1[1] > ponto2[1]:
return 's'
def turn_left(cardeal1, cardeal2):
if cardeal1 == 'n' and cardeal2 == 'w':
return True
if cardeal1 == 'w' and cardeal2 == 's':
return True
if cardeal1 == 's' and cardeal2 == 'e':
return True
if cardeal1 == 'e' and cardeal2 == 'n':
return True
return False
n_straight_sections = int(input()) + 1
danger = 0
directions = []
ponto1 = 'z'
for point in range(n_straight_sections):
ponto2 = ponto1
ponto1 = input().split()
if point < 1:
continue
directions.append(onde(ponto1, ponto2))
for direction_idx in range(len(directions)-1):
if turn_left(directions[direction_idx], directions[direction_idx+1]):
danger += 1
print(danger)
| 22
| 46
| 0
|
200306127
|
n=int(input())
print(int(((n-2)/2)-1))
|
Codeforces Round 346 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Bicycle Race
|
Maria participates in a bicycle race.
The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.
Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).
Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.
Help Maria get ready for the competition — determine the number of dangerous turns on the track.
|
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track.
The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).
It is guaranteed that:
- the first straight section is directed to the north;
- the southernmost (and if there are several, then the most western of among them) point of the track is the first point;
- the last point coincides with the first one (i.e., the start position);
- any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point);
- no pair of points (except for the first and last one) is the same;
- no two adjacent straight sections are directed in the same direction or in opposite directions.
|
Print a single integer — the number of dangerous turns on the track.
| null |
The first sample corresponds to the picture:
The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
|
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
| 1,500
|
["geometry", "implementation", "math"]
| 22
|
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}, {"input": "4\r\n6 8\r\n6 9\r\n7 9\r\n7 8\r\n6 8\r\n", "output": "0\r\n"}, {"input": "8\r\n-10000 -10000\r\n-10000 5000\r\n0 5000\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n0 -10000\r\n-10000 -10000\r\n", "output": "2\r\n"}, {"input": "20\r\n-4286 -10000\r\n-4286 -7778\r\n-7143 -7778\r\n-7143 -3334\r\n-10000 -3334\r\n-10000 1110\r\n-4286 1110\r\n-4286 -3334\r\n4285 -3334\r\n4285 -1112\r\n7142 -1112\r\n7142 3332\r\n4285 3332\r\n4285 9998\r\n9999 9998\r\n9999 -3334\r\n7142 -3334\r\n7142 -5556\r\n-1429 -5556\r\n-1429 -10000\r\n-4286 -10000\r\n", "output": "8\r\n"}, {"input": "24\r\n-10000 -10000\r\n-10000 9998\r\n9998 9998\r\n9998 -10000\r\n-6364 -10000\r\n-6364 6362\r\n6362 6362\r\n6362 -6364\r\n-2728 -6364\r\n-2728 2726\r\n2726 2726\r\n2726 -910\r\n908 -910\r\n908 908\r\n-910 908\r\n-910 -4546\r\n4544 -4546\r\n4544 4544\r\n-4546 4544\r\n-4546 -8182\r\n8180 -8182\r\n8180 8180\r\n-8182 8180\r\n-8182 -10000\r\n-10000 -10000\r\n", "output": "10\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-6000 6000\r\n-6000 2000\r\n10000 2000\r\n10000 -2000\r\n-6000 -2000\r\n-6000 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-9800 6000\r\n-9800 2000\r\n10000 2000\r\n10000 -2000\r\n-9800 -2000\r\n-9800 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "4\r\n0 0\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
362/A
|
362
|
A
|
Python 3
|
PRETESTS
| 7
| 46
| 307,200
|
5105399
|
def main():
n = int(input())
out = ""
for t in range(n):
knights = [0 for i in range(8)]
valid = [False for i in range(8)]
for i in range(8):
line = input()
for j in range(8):
#print((i%2)*4 + j%4, line[j])
if line[j] == '.':
valid[(i%2)*4 + j%4] = True
elif line[j] == 'K':
knights[(i%2)*4 + j%4] += 1
valid[(i%2)*4 + j%4] = True
for i in range(8):
#print(i, knights[i], valid[i])
if knights[i] == 2 and valid[i]:
out += "YES\n"
break
else:
out += "NO\n"
if t != n-1:
input()
print(out[:-1])
if __name__ == "__main__": main()
| 45
| 46
| 0
|
223013756
|
n = int(input())
testcases = []
for i in range(n):
# _ = input()
chess = []
for _ in range(8):
points = input()
chess.append(points)
if i != n-1:
new_line = input()
testcases.append(chess)
for chess in testcases:
def main():
poss_k = []
for i in range(8):
for j in range(8):
if chess[i][j] == 'K':
poss_k.append((i, j))
k1, k2 = sorted(poss_k)
i = 0
if (k1[i] % 4) != (k2[i] % 4):
return False
i = 1
if (k1[i] % 4) != (k2[i] % 4):
return False
if k2[0] - k1[0] == 8 and k2[1] - k1[1] == 8:
return True
movs_u = (k2[1] - k1[1]) // 2
movs_r = (k2[0] - k1[0]) // 2
if (movs_u % 2 == 1 )and (movs_r % 2 == 1):
return False
return True
if main():
print("YES")
else:
print("NO")
|
Codeforces Round 212 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Semiknights Meet
|
A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.
Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.
Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.
Please see the test case analysis.
|
The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.
|
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.
| null |
Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).
On the second board the semiknights will never meet.
|
[{"input": "2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#", "output": "YES\nNO"}]
| 1,500
|
["greedy", "math"]
| 45
|
[{"input": "2\r\n........\r\n........\r\n......#.\r\nK..##..#\r\n.......#\r\n...##..#\r\n......#.\r\nK.......\r\n\r\n........\r\n........\r\n..#.....\r\n..#..#..\r\n..####..\r\n...##...\r\n........\r\n....K#K#\r\n", "output": "YES\r\nNO\r\n"}, {"input": "3\r\n........\r\n........\r\n..#.....\r\n..#..#..\r\n..####..\r\n...##...\r\n........\r\n####K#K#\r\n\r\n........\r\nK......K\r\n........\r\n#......#\r\n.#....#.\r\n..####..\r\n........\r\n........\r\n\r\n.#..#...\r\n.##.##..\r\n..###...\r\n..#K###.\r\n..####..\r\n......K.\r\n..#####.\r\n..#####.\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "1\r\n...#...#\r\n........\r\n.#...K..\r\n........\r\n...#...#\r\n........\r\n.K...#..\r\n........\r\n", "output": "YES\r\n"}, {"input": "1\r\nK.#....#\r\n...#..#.\r\n..#.....\r\n..#.###.\r\n..#.....\r\n...#....\r\n.#.....#\r\n.#...##K\r\n", "output": "NO\r\n"}, {"input": "2\r\n....#..K\r\n...#....\r\n..##.#..\r\n.#.#.#..\r\n.#.....#\r\n.#......\r\n###.....\r\nK#.#....\r\n\r\nK.#.....\r\n..#...#.\r\n#.....#.\r\n..#.#..#\r\n#.......\r\n..#..#..\r\n....#...\r\nK..##.##\r\n", "output": "NO\r\nNO\r\n"}, {"input": "5\r\n........\r\n...KK...\r\n..####..\r\n...##...\r\n........\r\n..####..\r\n.######.\r\n#......#\r\n\r\n........\r\n.K......\r\n..#.....\r\n...#....\r\n....#...\r\n.....#..\r\n......#.\r\n.......K\r\n\r\n........\r\n...K....\r\n##...##.\r\n#.#.#..#\r\n.##.###.\r\n#..K#..#\r\n.##..##.\r\n........\r\n\r\n........\r\n.K..K...\r\n..##....\r\n..####..\r\n.#....#.\r\n.#.....#\r\n..#####.\r\n........\r\n\r\nK.......\r\n........\r\n........\r\n........\r\n........\r\n........\r\n........\r\n.......K\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "6\r\nK.......\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\n..####.K\r\n\r\n.......K\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\nK.####..\r\n\r\nK.......\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\nK.####..\r\n\r\n.......K\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\n..####.K\r\n\r\n........\r\n........\r\n........\r\n.KK.....\r\n........\r\n........\r\n........\r\n........\r\n\r\n........\r\n........\r\n.#...K..\r\n........\r\n...#....\r\n........\r\n.K...#..\r\n........\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "4\r\n#.##..##\r\n###.#.K#\r\n###.####\r\n.####.#.\r\n.#######\r\n######K#\r\n####.#..\r\n#.######\r\n\r\n#.......\r\n....##K.\r\n........\r\n...#.##.\r\n#....#..\r\n..K.#...\r\n...#....\r\n...#....\r\n\r\n.......#\r\n#..K....\r\n#.......\r\n#....#..\r\n..#...#.\r\n...K#.#.\r\n........\r\n#.#...#.\r\n\r\n..##....\r\n........\r\n...#....\r\n...#K...\r\n........\r\n...##...\r\n..#.....\r\n....K...\r\n", "output": "YES\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "5\r\nK###K###\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n\r\nK##K####\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n\r\nK####K##\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n\r\n........\r\n........\r\n........\r\n...K....\r\n........\r\n.....K..\r\n........\r\n........\r\n\r\n....#.#.\r\n..K.K...\r\n.......#\r\n....#...\r\n........\r\n........\r\n........\r\n.#......\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}]
| false
|
stdio
| null | true
|
514/D
|
514
|
D
|
PyPy 3-64
|
TESTS
| 5
| 62
| 614,400
|
197864000
|
# ﷽
from collections import deque
import sys
input = lambda: sys.stdin.readline().strip()
mod=7+10**9
class RangeQuery:
def __init__(self, data, default=0, func=max):
"""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)
# class RangeQuery:
# def __init__(self, data, func=max):
# self.func = func
# self._data = _data = [list(data)]
# i, n = 1, len(_data[0])
# while 2 * i <= n:
# prev = _data[-1]
# _data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)])
# i <<= 1
# def query(self, start, stop):
# """func of data[start, stop)"""
# depth = (stop - start).bit_length() - 1
# return self.func(self._data[depth][start], self._data[depth][stop - (1 << depth)])
# def __getitem__(self, idx):
# return self._data[0][idx]
def get(l,r) :
tmp=[sp.query(l,r) for sp in sparelist]
if sum(tmp)<=k:
# tmp[-1]+=k-sum(tmp)
return tmp
def solve():
global sparelist ,k
n,m,k=[int(i) for i in input().split()]
lst=deque()
lst2=[]
for _ in range(n):lst.append([int(i) for i in input().split()])
for i in zip(*lst):lst2.append(i)
sparelist=[RangeQuery(lst2[i]) for i in range(m)]
lft=0;rgt=n-1
while lft<rgt :
ans= get(lft,rgt)
if ans:return ans
if max(lst[lft])<max(lst[rgt]):rgt-=1
else:lft+=1
return [0]*m
if __name__ == "__main__":
# for i in range(int(input())):
print( *solve())
| 35
| 1,996
| 120,217,600
|
192669643
|
def rl():
return list(map(int,input().split()))
def ri():
return int(input())
def rs():
return input()
def rm():
return map(int,input().split())
from math import log2,floor
def main():
n,l,k=rm()
rb=[]
for i in range(n):
rb.append(rl())
rb=[[rb[i][j] for i in range(len(rb))] for j in range(len(rb[0]))]
m=floor(log2(n))+1
tbs=[]
for ii in range(l):
tb=[[0 for _ in range(m)]for _ in range(n)]
for i in range(n):
tb[i][0]=rb[ii][i]
for j in range(1,m):
for i in range(n):
if i+(1<<j)>n: break
tb[i][j]=max(tb[i][j-1],tb[i+(1<<(j-1))][j-1])
tbs.append(tb)
def query(l,r,m):
p=floor(log2(r+1-l))
return max(tbs[m][l][p],tbs[m][r-(1<<p)+1][p])
def ok(ln):
for i in range(n-ln+1):
rq=0
for j in range(l):
rq+=query(i,i+ln-1,j)
if rq<=k: return True
return False
rs=[]
if ok(n):
for i in range(l):
rs.append(query(0,n-1,i))
return rs
if not ok(1):
return [0]*l
lo,hi=1,n
while lo<hi-1:
md=(lo+hi)//2
if ok(md): lo=md
else: hi=md
# print(lo)
for i in range(n-lo+1):
rq=[]
for j in range(l):
rq.append(query(i,i+lo-1,j))
# print(rq)
if sum(rq)<=k: return rq
print(*main())
|
Codeforces Round 291 (Div. 2)
|
CF
| 2,015
| 2
| 256
|
R2D2 and Droid Army
|
An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, where ai is the number of details of the i-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it).
A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?
|
The first line contains three integers n, m, k (1 ≤ n ≤ 105, 1 ≤ m ≤ 5, 0 ≤ k ≤ 109) — the number of droids, the number of detail types and the number of available shots, respectively.
Next n lines follow describing the droids. Each line contains m integers a1, a2, ..., am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.
|
Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.
If there are multiple optimal solutions, print any of them.
It is not necessary to make exactly k shots, the number of shots can be less.
| null |
In the first test the second, third and fourth droids will be destroyed.
In the second test the first and second droids will be destroyed.
|
[{"input": "5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3", "output": "2 2"}, {"input": "3 2 4\n1 2\n1 3\n2 2", "output": "1 3"}]
| 2,000
|
["binary search", "data structures", "two pointers"]
| 35
|
[{"input": "5 2 4\r\n4 0\r\n1 2\r\n2 1\r\n0 2\r\n1 3\r\n", "output": "2 2\r\n"}, {"input": "3 2 4\r\n1 2\r\n1 3\r\n2 2\r\n", "output": "1 3\r\n"}, {"input": "1 1 0\r\n0\r\n", "output": "0\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0\r\n"}, {"input": "4 5 33\r\n2 10 2 3 2\r\n10 6 4 5 0\r\n3 1 7 3 2\r\n4 4 2 1 5\r\n", "output": "10 6 7 5 5\r\n"}, {"input": "4 5 40\r\n0 10 9 0 4\r\n10 5 5 7 4\r\n9 9 5 5 2\r\n6 7 9 4 3\r\n", "output": "10 10 9 7 4\r\n"}, {"input": "31 2 1913\r\n845 576\r\n862 325\r\n914 283\r\n431 837\r\n193 171\r\n30 248\r\n290 488\r\n810 552\r\n463 74\r\n765 469\r\n785 119\r\n107 267\r\n528 761\r\n583 395\r\n359 45\r\n840 559\r\n147 510\r\n882 830\r\n267 390\r\n639 47\r\n849 312\r\n518 6\r\n643 828\r\n195 886\r\n377 948\r\n333 841\r\n484 99\r\n486 999\r\n134 342\r\n736 490\r\n624 677\r\n", "output": "914 999\r\n"}, {"input": "49 2 1971\r\n794 866\r\n401 575\r\n341 83\r\n103 208\r\n352 134\r\n260 878\r\n497 931\r\n630 570\r\n885 464\r\n23 663\r\n60 775\r\n416 870\r\n955 405\r\n392 961\r\n530 258\r\n73 404\r\n736 923\r\n44 436\r\n594 314\r\n904 138\r\n980 163\r\n76 720\r\n879 809\r\n81 838\r\n263 599\r\n218 139\r\n659 493\r\n848 754\r\n656 302\r\n490 7\r\n204 530\r\n184 758\r\n114 849\r\n80 649\r\n653 439\r\n961 350\r\n104 387\r\n482 441\r\n628 972\r\n451 503\r\n367 926\r\n50 332\r\n855 991\r\n528 261\r\n131 447\r\n551 841\r\n963 962\r\n253 979\r\n700 218\r\n", "output": "980 991\r\n"}, {"input": "1 5 264566327\r\n35969364 23665385 95351498 57005692 52574389\r\n", "output": "0 0 0 0 0\r\n"}, {"input": "1 5 240247522\r\n48772229 15259125 4834706 88633344 82748118\r\n", "output": "48772229 15259125 4834706 88633344 82748118\r\n"}, {"input": "1 5 211500111\r\n18123011 48739518 50372830 20177276 74087475\r\n", "output": "18123011 48739518 50372830 20177276 74087475\r\n"}]
| false
|
stdio
| null | true
|
967/B
|
967
|
B
|
Python 3
|
TESTS
| 24
| 234
| 6,348,800
|
39336912
|
n,A,B = map(int,input().split(' '))
s = list(map(int,input().split(' ')))
adf = s[0]
s = sorted(s,reverse=True)
c = 0
S = sum(s)
s.remove(adf)
t = (adf*A)/S
i = 0
while (t<B and i<n-1):
t = (t*S)/(S-s[i])
S = S-s[i]
i = i+1
c = c+1
print(c)
| 26
| 78
| 13,516,800
|
197520959
|
hole, l, need = map(int,input().split())
a = list(map(int,input().split()))
x = a.pop(0)
b = x*l / need
a.sort(reverse=True)
y = sum(a)+x
p = 0
while b<y:
y -= a[p]
p +=1
print(p)
|
Codeforces Round 477 (rated, Div. 2, based on VK Cup 2018 Round 3)
|
CF
| 2,018
| 1
| 256
|
Watering System
|
Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for $$$n$$$ flowers and so it looks like a pipe with $$$n$$$ holes. Arkady can only use the water that flows from the first hole.
Arkady can block some of the holes, and then pour $$$A$$$ liters of water into the pipe. After that, the water will flow out from the non-blocked holes proportionally to their sizes $$$s_1, s_2, \ldots, s_n$$$. In other words, if the sum of sizes of non-blocked holes is $$$S$$$, and the $$$i$$$-th hole is not blocked, $$$\frac{s_i \cdot A}{S}$$$ liters of water will flow out of it.
What is the minimum number of holes Arkady should block to make at least $$$B$$$ liters of water flow out of the first hole?
|
The first line contains three integers $$$n$$$, $$$A$$$, $$$B$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le B \le A \le 10^4$$$) — the number of holes, the volume of water Arkady will pour into the system, and the volume he wants to get out of the first hole.
The second line contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 10^4$$$) — the sizes of the holes.
|
Print a single integer — the number of holes Arkady should block.
| null |
In the first example Arkady should block at least one hole. After that, $$$\frac{10 \cdot 2}{6} \approx 3.333$$$ liters of water will flow out of the first hole, and that suits Arkady.
In the second example even without blocking any hole, $$$\frac{80 \cdot 3}{10} = 24$$$ liters will flow out of the first hole, that is not less than $$$20$$$.
In the third example Arkady has to block all holes except the first to make all water flow out of the first hole.
|
[{"input": "4 10 3\n2 2 2 2", "output": "1"}, {"input": "4 80 20\n3 2 1 4", "output": "0"}, {"input": "5 10 10\n1000 1 1 1 1", "output": "4"}]
| 1,000
|
["math", "sortings"]
| 26
|
[{"input": "4 10 3\r\n2 2 2 2\r\n", "output": "1\r\n"}, {"input": "4 80 20\r\n3 2 1 4\r\n", "output": "0\r\n"}, {"input": "5 10 10\r\n1000 1 1 1 1\r\n", "output": "4\r\n"}, {"input": "10 300 100\r\n20 1 3 10 8 5 3 6 4 3\r\n", "output": "1\r\n"}, {"input": "10 300 100\r\n20 25 68 40 60 37 44 85 23 96\r\n", "output": "8\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 2 2\r\n1 10000\r\n", "output": "1\r\n"}, {"input": "2 10000 1\r\n1 9999\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
496/D
|
496
|
D
|
Python 3
|
TESTS
| 6
| 46
| 0
|
12833880
|
import math
n = int(input())
games = list(map(int, input().split()))
PETYA = 0
GENA = 1
pontos = [[0], [0]]
for i in range(0, n):
if (games[i] == 1):
pontos[PETYA].append(pontos[PETYA][i] + 1)
pontos[GENA].append(pontos[GENA][i])
else:
pontos[PETYA].append(pontos[PETYA][i])
pontos[GENA].append(pontos[GENA][i] + 1)
res = []
k = 0
if (pontos[PETYA][-1] != pontos[GENA][-1]):
maxPoints = max(pontos[PETYA][-1], pontos[GENA][-1])
for i in range(1, maxPoints + 1):
if (maxPoints % i == 0):
s = i
t = maxPoints // i
contS = [0,0]
ganhou = 0
pontos[PETYA][0] = 0
pontos[GENA][0] = 0
while (ganhou < n):
try:
petya = pontos[PETYA].index(pontos[PETYA][0] + t)
except ValueError:
petya = n + t
try:
gena = pontos[GENA].index(pontos[GENA][0] + t)
except ValueError:
gena = n + t
ganhou = min(petya, gena)
if (ganhou < n):
pontos[PETYA][0] = pontos[PETYA][ganhou]
pontos[GENA][0] = pontos[GENA][ganhou]
if (petya > gena):
contS[GENA] += 1
else:
contS[PETYA] += 1
if (ganhou == n):
res.append((s, t))
print(len(res))
for i in range(0, len(res)):
x = res[i]
print("{} {}".format(int(x[0]), int(x[1])))
| 53
| 1,372
| 10,649,600
|
9303276
|
#!/usr/bin/env python3
import itertools
n = int(input())
a = [int(x) for x in input().split()]
winner = a[-1]
looser = 3 - winner
serve_win_cnt, serve_loose_cnt, win_pos, loose_pos, result = [0], [0], [-1], [-1], []
win_cnt = a.count(winner)
for i in range(n):
if a[i] == winner:
win_pos.append(i)
else:
loose_pos.append(i)
serve_win_cnt.append(serve_win_cnt[-1] + (a[i] == winner))
serve_loose_cnt.append(serve_loose_cnt[-1] + (a[i] == looser))
win_pos += [n * 10] * n
loose_pos += [n * 10] * n
serve_win_cnt += [0] * n
serve_loose_cnt += [0] * n
for t in itertools.chain(range(1, 1 + win_cnt // 2), [win_cnt]):
s = l = i = 0
sw = sl = 0
while i < n:
xw = win_pos[serve_win_cnt[i] + t]
xl = loose_pos[serve_loose_cnt[i] + t]
if xw < xl:
s += 1
else:
l += 1
i = min(xw, xl) + 1
if s > l and i <= n and serve_win_cnt[i] == win_cnt:
result.append((s, t))
print(len(result))
for (x, y) in sorted(result):
print(x, y)
|
Codeforces Round 283 (Div. 2)
|
CF
| 2,014
| 2
| 256
|
Tennis Game
|
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.
To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.
Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?
|
The first line contains a single integer n — the length of the sequence of games (1 ≤ n ≤ 105).
The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena.
It is not guaranteed that at least one option for numbers s and t corresponds to the given record.
|
In the first line print a single number k — the number of options for numbers s and t.
In each of the following k lines print two integers si and ti — the option for numbers s and t. Print the options in the order of increasing si, and for equal si — in the order of increasing ti.
| null | null |
[{"input": "5\n1 2 1 2 1", "output": "2\n1 3\n3 1"}, {"input": "4\n1 1 1 1", "output": "3\n1 4\n2 2\n4 1"}, {"input": "4\n1 2 1 2", "output": "0"}, {"input": "8\n2 1 2 1 1 1 1 1", "output": "3\n1 6\n2 3\n6 1"}]
| 1,900
|
["binary search"]
| 53
|
[{"input": "5\r\n1 2 1 2 1\r\n", "output": "2\r\n1 3\r\n3 1\r\n"}, {"input": "4\r\n1 1 1 1\r\n", "output": "3\r\n1 4\r\n2 2\r\n4 1\r\n"}, {"input": "4\r\n1 2 1 2\r\n", "output": "0\r\n"}, {"input": "8\r\n2 1 2 1 1 1 1 1\r\n", "output": "3\r\n1 6\r\n2 3\r\n6 1\r\n"}, {"input": "14\r\n2 1 2 1 1 1 1 2 1 1 2 1 2 1\r\n", "output": "3\r\n1 9\r\n3 3\r\n9 1\r\n"}, {"input": "10\r\n1 1 2 2 1 1 2 2 1 1\r\n", "output": "4\r\n1 6\r\n2 3\r\n3 2\r\n6 1\r\n"}, {"input": "20\r\n1 1 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 1\r\n", "output": "0\r\n"}, {"input": "186\r\n2 1 2 1 1 1 1 1 2 1 1 2 2 2 1 1 2 2 1 1 1 2 1 1 2 2 1 1 1 2 2 1 1 1 1 1 2 1 1 1 2 1 2 1 1 2 1 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 2 2 1 1 1 1 1 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 2 2 1 2 2 1 2 2 2 2 2 1 1 1 1 2 1 1 2 2 2 2 2 2 1 1 1 1 1 2 1 1 2 2 1 2 2 1 1 1 1 1 2 2 1 1 2 2 1 2 2 2 1 2 1 2 1 1 2 1 2 2 2 2 1 2 1 2 2 1 2 1 1 1 1 1 2 1 1 2 2 1 1 1 2 2 2 1 2 2 1 1 2 1 1 1 1 2 1 1\r\n", "output": "8\r\n1 100\r\n2 50\r\n6 11\r\n8 8\r\n19 4\r\n25 3\r\n40 2\r\n100 1\r\n"}, {"input": "82\r\n1 1 1 2 2 2 2 1 1 1 2 2 2 2 1 1 1 2 2 2 1 1 1 1 2 2 2 1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 1 1 1 2 2 2 2 1 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 2 1 1 1 2 2 2 1 1 1 1 2 2 2\r\n", "output": "0\r\n"}, {"input": "83\r\n1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 1\r\n", "output": "5\r\n1 45\r\n3 10\r\n3 15\r\n4 7\r\n45 1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1 1\r\n"}, {"input": "1\r\n2\r\n", "output": "1\r\n1 1\r\n"}]
| false
|
stdio
| null | true
|
362/C
|
362
|
C
|
Python 3
|
PRETESTS
| 2
| 46
| 307,200
|
5106086
|
def main():
n = int(input())
arr = input().split(" ")
arr = [int(a) for a in arr]
imp = 0
cnt = 0
tot = 0
for i in range(n):
tot += abs(i - arr[i])
for i in range(n):
for j in range(i+1, n):
improvement = abs(arr[i] - i) + abs(arr[j] - j) - abs(arr[i] - j) - abs(arr[j] - i)
if improvement > imp:
imp = improvement
a = i
b = j
cnt = 1
elif improvement == imp:
cnt += 1
print(int(tot-imp)//2, int(cnt))
if __name__ == "__main__": main()
| 38
| 1,434
| 10,444,800
|
71195138
|
arr = [0 for i in range(5001)]
def insertion_sort(n, a):
def modify(t):
while t > 0:
arr[t] += 1
t -= t & (-t)
def query(t):
res = 0
while t < 5001:
res += arr[t]
t += t & (-t)
return res
s = 0
ans = 0
way = 0
for i in range(n):
a[i] += 1
for i in range(n):
global arr
arr = [0 for j in range(5001)]
for j in range(i + 1, n):
if a[i] < a[j]:
continue
s += 1
tmp = 1 + 2 * query(a[j])
if tmp > ans:
ans = tmp
way = 1
elif tmp == ans:
way += 1
modify(a[j])
return s - ans, way
if __name__ == "__main__":
n = int(input())
a = list(map(int, input().split()))
result = insertion_sort(n, a)
print(*result)
|
Codeforces Round 212 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Insertion Sort
|
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order.
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
|
The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive — the actual permutation.
|
Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions.
| null |
In the first sample the appropriate pairs are (0, 3) and (0, 4).
In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
|
[{"input": "5\n4 0 3 1 2", "output": "3 2"}, {"input": "5\n1 2 3 4 0", "output": "3 4"}]
| 1,900
|
["data structures", "dp", "implementation", "math"]
| 38
|
[{"input": "5\r\n4 0 3 1 2\r\n", "output": "3 2\r\n"}, {"input": "5\r\n1 2 3 4 0\r\n", "output": "3 4\r\n"}, {"input": "5\r\n1 3 4 0 2\r\n", "output": "4 5\r\n"}, {"input": "10\r\n9 8 7 6 5 4 3 2 1 0\r\n", "output": "28 1\r\n"}, {"input": "5\r\n0 4 1 3 2\r\n", "output": "1 1\r\n"}, {"input": "6\r\n3 0 1 4 5 2\r\n", "output": "4 5\r\n"}, {"input": "3\r\n0 2 1\r\n", "output": "0 1\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "0 1\r\n"}, {"input": "3\r\n1 2 0\r\n", "output": "1 2\r\n"}, {"input": "3\r\n2 0 1\r\n", "output": "1 2\r\n"}, {"input": "3\r\n2 1 0\r\n", "output": "0 1\r\n"}, {"input": "7\r\n4 0 3 5 1 2 6\r\n", "output": "5 2\r\n"}, {"input": "8\r\n1 5 4 0 2 7 3 6\r\n", "output": "7 3\r\n"}, {"input": "9\r\n1 5 6 3 0 7 2 8 4\r\n", "output": "11 4\r\n"}, {"input": "10\r\n8 6 7 9 4 5 2 3 1 0\r\n", "output": "24 1\r\n"}, {"input": "11\r\n4 9 1 2 8 5 10 3 0 7 6\r\n", "output": "16 1\r\n"}, {"input": "12\r\n2 7 0 1 3 10 4 8 11 6 9 5\r\n", "output": "13 1\r\n"}, {"input": "13\r\n5 11 12 10 3 8 4 0 7 9 6 1 2\r\n", "output": "39 4\r\n"}, {"input": "100\r\n73 98 9 92 43 77 32 2 29 5 58 59 61 17 10 94 60 12 80 16 24 91 8 70 62 99 47 23 78 19 22 30 44 96 63 74 48 18 69 45 33 88 97 11 31 66 1 82 7 28 27 41 51 0 37 39 71 75 13 26 20 87 25 40 38 46 79 15 14 81 57 90 83 52 67 6 53 68 54 65 86 93 4 34 95 42 85 72 56 36 89 84 35 64 55 76 21 50 49 3\r\n", "output": "2137 1\r\n"}, {"input": "120\r\n60 100 55 8 106 57 43 85 103 0 6 20 88 102 53 2 116 31 119 59 86 71 99 81 50 22 74 5 80 13 95 118 49 67 17 63 10 27 61 45 101 76 87 72 113 93 92 47 42 41 35 83 97 51 77 114 69 30 91 44 1 84 107 105 16 70 108 65 64 78 25 39 89 23 40 62 117 4 98 24 104 75 58 3 79 112 11 28 109 38 21 19 37 115 9 54 32 111 46 68 90 48 34 12 96 82 29 73 110 18 26 52 36 94 66 15 14 33 7 56\r\n", "output": "3686 1\r\n"}, {"input": "150\r\n48 115 13 9 105 117 41 136 123 32 84 95 62 50 140 106 145 91 57 141 139 35 45 27 129 63 137 10 37 60 44 30 101 119 138 78 22 103 39 134 49 36 25 12 28 67 69 99 148 26 16 87 146 65 8 74 14 38 47 89 81 19 40 11 64 43 110 66 102 3 122 124 100 2 125 42 97 73 121 7 52 23 29 109 1 70 34 108 59 55 127 90 88 144 18 56 17 75 116 5 135 4 15 20 86 94 82 149 126 130 113 33 147 80 54 76 142 96 85 114 112 31 71 133 77 79 93 21 143 128 24 72 68 61 0 131 107 58 132 120 6 46 104 118 53 51 111 83 92 98\r\n", "output": "5113 4\r\n"}]
| false
|
stdio
| null | true
|
362/C
|
362
|
C
|
Python 3
|
TESTS
| 2
| 31
| 307,200
|
5519437
|
n, a = int(input()), list(map(int, input().split()))
s = t = 0
r = sum(max(i - j, 0) for i, j in enumerate(a))
for i in range(n - 1):
for j in range(i + 1, n):
d = max(i - a[i], 0) + max(j - a[j], 0) - max(j - a[i], 0) - max(i - a[j], 0)
if s < d: s, t = d, 1
elif s == d: t += 1
print(r - s, t)
| 38
| 1,434
| 10,444,800
|
71195138
|
arr = [0 for i in range(5001)]
def insertion_sort(n, a):
def modify(t):
while t > 0:
arr[t] += 1
t -= t & (-t)
def query(t):
res = 0
while t < 5001:
res += arr[t]
t += t & (-t)
return res
s = 0
ans = 0
way = 0
for i in range(n):
a[i] += 1
for i in range(n):
global arr
arr = [0 for j in range(5001)]
for j in range(i + 1, n):
if a[i] < a[j]:
continue
s += 1
tmp = 1 + 2 * query(a[j])
if tmp > ans:
ans = tmp
way = 1
elif tmp == ans:
way += 1
modify(a[j])
return s - ans, way
if __name__ == "__main__":
n = int(input())
a = list(map(int, input().split()))
result = insertion_sort(n, a)
print(*result)
|
Codeforces Round 212 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Insertion Sort
|
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order.
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
|
The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive — the actual permutation.
|
Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions.
| null |
In the first sample the appropriate pairs are (0, 3) and (0, 4).
In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
|
[{"input": "5\n4 0 3 1 2", "output": "3 2"}, {"input": "5\n1 2 3 4 0", "output": "3 4"}]
| 1,900
|
["data structures", "dp", "implementation", "math"]
| 38
|
[{"input": "5\r\n4 0 3 1 2\r\n", "output": "3 2\r\n"}, {"input": "5\r\n1 2 3 4 0\r\n", "output": "3 4\r\n"}, {"input": "5\r\n1 3 4 0 2\r\n", "output": "4 5\r\n"}, {"input": "10\r\n9 8 7 6 5 4 3 2 1 0\r\n", "output": "28 1\r\n"}, {"input": "5\r\n0 4 1 3 2\r\n", "output": "1 1\r\n"}, {"input": "6\r\n3 0 1 4 5 2\r\n", "output": "4 5\r\n"}, {"input": "3\r\n0 2 1\r\n", "output": "0 1\r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "0 1\r\n"}, {"input": "3\r\n1 2 0\r\n", "output": "1 2\r\n"}, {"input": "3\r\n2 0 1\r\n", "output": "1 2\r\n"}, {"input": "3\r\n2 1 0\r\n", "output": "0 1\r\n"}, {"input": "7\r\n4 0 3 5 1 2 6\r\n", "output": "5 2\r\n"}, {"input": "8\r\n1 5 4 0 2 7 3 6\r\n", "output": "7 3\r\n"}, {"input": "9\r\n1 5 6 3 0 7 2 8 4\r\n", "output": "11 4\r\n"}, {"input": "10\r\n8 6 7 9 4 5 2 3 1 0\r\n", "output": "24 1\r\n"}, {"input": "11\r\n4 9 1 2 8 5 10 3 0 7 6\r\n", "output": "16 1\r\n"}, {"input": "12\r\n2 7 0 1 3 10 4 8 11 6 9 5\r\n", "output": "13 1\r\n"}, {"input": "13\r\n5 11 12 10 3 8 4 0 7 9 6 1 2\r\n", "output": "39 4\r\n"}, {"input": "100\r\n73 98 9 92 43 77 32 2 29 5 58 59 61 17 10 94 60 12 80 16 24 91 8 70 62 99 47 23 78 19 22 30 44 96 63 74 48 18 69 45 33 88 97 11 31 66 1 82 7 28 27 41 51 0 37 39 71 75 13 26 20 87 25 40 38 46 79 15 14 81 57 90 83 52 67 6 53 68 54 65 86 93 4 34 95 42 85 72 56 36 89 84 35 64 55 76 21 50 49 3\r\n", "output": "2137 1\r\n"}, {"input": "120\r\n60 100 55 8 106 57 43 85 103 0 6 20 88 102 53 2 116 31 119 59 86 71 99 81 50 22 74 5 80 13 95 118 49 67 17 63 10 27 61 45 101 76 87 72 113 93 92 47 42 41 35 83 97 51 77 114 69 30 91 44 1 84 107 105 16 70 108 65 64 78 25 39 89 23 40 62 117 4 98 24 104 75 58 3 79 112 11 28 109 38 21 19 37 115 9 54 32 111 46 68 90 48 34 12 96 82 29 73 110 18 26 52 36 94 66 15 14 33 7 56\r\n", "output": "3686 1\r\n"}, {"input": "150\r\n48 115 13 9 105 117 41 136 123 32 84 95 62 50 140 106 145 91 57 141 139 35 45 27 129 63 137 10 37 60 44 30 101 119 138 78 22 103 39 134 49 36 25 12 28 67 69 99 148 26 16 87 146 65 8 74 14 38 47 89 81 19 40 11 64 43 110 66 102 3 122 124 100 2 125 42 97 73 121 7 52 23 29 109 1 70 34 108 59 55 127 90 88 144 18 56 17 75 116 5 135 4 15 20 86 94 82 149 126 130 113 33 147 80 54 76 142 96 85 114 112 31 71 133 77 79 93 21 143 128 24 72 68 61 0 131 107 58 132 120 6 46 104 118 53 51 111 83 92 98\r\n", "output": "5113 4\r\n"}]
| false
|
stdio
| null | true
|
363/D
|
363
|
D
|
Python 3
|
TESTS
| 1
| 31
| 0
|
225753908
|
def check(x, a, b, k):
a.sort()
ans = k
for i in range(1, x + 1):
ans += min(0, a[i - 1] - b[i - 1])
if ans < 0:
return False
a.sort(reverse=True)
return ans >= 0
def main():
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort(reverse=True)
b.sort()
l, r = 0, min(n, m) + 1
while l < r:
mid = (l + r) // 2
if check(mid, a, b, k):
l = mid + 1
else:
r = mid
print(r - 1, end=' ')
total = sum(b[:r - 1])
print(max(0, total - k))
if __name__ == "__main__":
main()
| 34
| 764
| 10,035,200
|
182758780
|
n, m, a = map(int, input().split())
b, p = sorted(map(int, input().split())), sorted(map(int, input().split()))
def f(k):
return sum(max(0, p[i] - b[n - k + i]) for i in range(k))
def g(k):
return sum(min(b[n - k + i], p[i]) for i in range(k))
x, y, d = 0, min(n, m) + 1, a
while y - x > 1:
k = (x + y) // 2
s = f(k)
if s > a:
y = k
else:
x, d = k, s
print(x, max(0, g(x) - a + d))
|
Codeforces Round 211 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Renting Bikes
|
A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them.
The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles.
In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike.
Each boy can rent at most one bike, one cannot give his bike to somebody else.
What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?
|
The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike.
|
Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.
| null |
In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
|
[{"input": "2 2 10\n5 5\n7 6", "output": "2 3"}, {"input": "4 5 2\n8 1 1 2\n6 3 7 5 2", "output": "3 8"}]
| 1,800
|
["binary search", "greedy"]
| 34
|
[{"input": "2 2 10\r\n5 5\r\n7 6\r\n", "output": "2 3\r\n"}, {"input": "4 5 2\r\n8 1 1 2\r\n6 3 7 5 2\r\n", "output": "3 8\r\n"}, {"input": "1 1 2\r\n1\r\n2\r\n", "output": "1 0\r\n"}, {"input": "4 1 1\r\n3 2 3 2\r\n3\r\n", "output": "1 2\r\n"}, {"input": "1 4 1\r\n3\r\n2 4 5 5\r\n", "output": "1 1\r\n"}, {"input": "3 3 3\r\n1 1 2\r\n3 5 6\r\n", "output": "1 0\r\n"}, {"input": "4 5 6\r\n5 1 7 2\r\n8 7 3 9 8\r\n", "output": "3 12\r\n"}, {"input": "4 8 10\r\n2 1 2 2\r\n10 12 10 8 7 9 10 9\r\n", "output": "1 0\r\n"}, {"input": "8 4 18\r\n9 4 2 2 7 5 1 1\r\n11 12 8 9\r\n", "output": "4 22\r\n"}, {"input": "6 6 2\r\n6 1 5 3 10 1\r\n11 4 7 8 11 7\r\n", "output": "3 16\r\n"}, {"input": "10 10 7\r\n6 7 15 1 3 1 14 6 7 4\r\n15 3 13 17 11 19 20 14 8 17\r\n", "output": "5 42\r\n"}, {"input": "14 14 22\r\n23 1 3 16 23 1 7 5 18 7 3 6 17 8\r\n22 14 22 18 12 11 7 24 20 27 10 22 16 7\r\n", "output": "10 115\r\n"}, {"input": "10 20 36\r\n12 4 7 18 4 4 2 7 4 10\r\n9 18 7 7 30 19 26 27 16 20 30 25 23 17 5 30 22 7 13 6\r\n", "output": "10 69\r\n"}, {"input": "20 10 31\r\n17 27 2 6 11 12 5 3 12 4 2 10 4 8 2 10 7 9 12 1\r\n24 11 18 10 30 16 20 18 24 24\r\n", "output": "7 86\r\n"}, {"input": "40 40 61\r\n28 59 8 27 45 67 33 32 61 3 42 2 3 37 8 8 10 61 1 5 65 28 34 27 8 35 45 49 31 49 13 23 23 53 20 48 14 74 16 6\r\n69 56 34 66 42 73 45 49 29 70 67 77 73 26 78 11 50 69 64 72 78 66 66 29 80 40 50 75 68 47 78 63 41 70 52 52 69 22 69 66\r\n", "output": "22 939\r\n"}, {"input": "10 10 0\r\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000\r\n1001 1001 1001 1001 1001 1001 1001 1001 1001 1001\r\n", "output": "0 0\r\n"}, {"input": "9 8 0\r\n1 2 3 4 5 6 7 8 9\r\n2 3 4 5 6 7 8 9\r\n", "output": "8 44\r\n"}, {"input": "9 8 0\r\n1 2 3 4 5 6 7 8 9\r\n1 2 3 4 5 6 7 8\r\n", "output": "8 36\r\n"}]
| false
|
stdio
| null | true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 62
| 6,860,800
|
34713874
|
step = input() + "\n"
num = int(input())
res = str(step)
for _ in range(num):
sat = input().split()
step = step.replace(sat[0],sat[1])
res += step
print(res)
| 57
| 46
| 0
|
150138014
|
x, y = input().split()
print(x, y)
for _ in range(int(input())):
a, b = input().split()
if x == a:
x = b
elif y == a:
y = b
print(x, y)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 62
| 0
|
113036513
|
a = list(map(str, input().split()))
n = int(input())
print(*a)
for i in range(n):
died, replaced = map(str, input().split())
a = [a1.replace(died, replaced) for a1 in a]
print(*a)
| 57
| 46
| 0
|
150217031
|
a = list(input().split())
n = int(input())
print(a[0], a[1])
for _ in range(n):
a1, a2 = input().split()
if a1 == a[0]:
a[0] = a2
if a1 == a[1]:
a[1] = a2
print(a[0], a[1])
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 93
| 307,200
|
93124350
|
s = input()
n = int(input())
killed = [0]*n
for i in range(n):
killed[i] = input().split()
print(s)
for i in range(n):
s = s.replace(killed[i][0], killed[i][1])
print(s)
| 57
| 46
| 0
|
154658931
|
import sys
input = sys.stdin.readline
w = set(input().split())
print(*w)
for _ in range(int(input())):
s = input().split()
w = set(s) ^ w
print(*w)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
280/B
|
280
|
B
|
Python 3
|
TESTS
| 3
| 78
| 7,065,600
|
38004537
|
n=int(input())
def CalculateMaxXor(arr,n):
MaxXor=arr[0]^arr[1]
stack=[]
for i in range(0,n):
while stack and stack[-1]<arr[i]:
stack.pop()
stack.append(arr[i])
if len(stack)>=2:
MaxXor=max(MaxXor,stack[-1]^stack[-2])
return MaxXor
print(CalculateMaxXor(list(map(int,input().split())),n))
| 56
| 93
| 15,360,000
|
200601294
|
def mp():
return map(int, input().split())
n = input()
a = [*mp()]
s = []
z = 0
for i in a:
while s:
x = s[-1]
z = max(z, x ^ i)
if x > i:
break
s.pop()
s.append(i)
print(z)
|
Codeforces Round 172 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Maximum Xor Secondary
|
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: $$x_j \neq \max_{i=1}^{k} x_i$$.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
|
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
|
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
| null |
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
|
[{"input": "5\n5 2 1 4 3", "output": "7"}, {"input": "5\n9 8 3 5 7", "output": "15"}]
| 1,800
|
["data structures", "implementation", "two pointers"]
| 56
|
[{"input": "5\r\n5 2 1 4 3\r\n", "output": "7\r\n"}, {"input": "5\r\n9 8 3 5 7\r\n", "output": "15\r\n"}, {"input": "10\r\n76969694 71698884 32888447 31877010 65564584 87864180 7850891 1505323 17879621 15722446\r\n", "output": "128869996\r\n"}, {"input": "10\r\n4547989 39261040 94929326 38131456 26174500 7152864 71295827 77784626 89898294 68006331\r\n", "output": "134189790\r\n"}, {"input": "10\r\n30301275 19973434 63004643 54007648 93722492 91677384 58694045 41546981 15552151 5811338\r\n", "output": "112066588\r\n"}, {"input": "10\r\n47606126 65484553 142643 35352821 26622058 5603080 7296801 53938188 34750256 97196502\r\n", "output": "131671782\r\n"}, {"input": "10\r\n82942694 74816699 72957520 1634864 60842992 60103606 61079517 41624114 13932450 24035648\r\n", "output": "133874061\r\n"}, {"input": "10\r\n73622246 45316865 2066146 61168230 1258786 69603039 64470479 72811017 72683016 97992629\r\n", "output": "133280528\r\n"}, {"input": "10\r\n29272229 8752316 10025994 52398694 57994948 49609605 28150935 66061676 44865054 87041483\r\n", "output": "127710165\r\n"}, {"input": "10\r\n3106954 3413954 3854371 85952704 17834583 20954227 58810981 7460648 97908613 97965110\r\n", "output": "111078053\r\n"}, {"input": "3\r\n11 10 8\r\n", "output": "2\r\n"}, {"input": "2\r\n5 6\r\n", "output": "3\r\n"}, {"input": "2\r\n16 17\r\n", "output": "1\r\n"}, {"input": "3\r\n8 9 10\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
280/B
|
280
|
B
|
Python 3
|
TESTS
| 3
| 31
| 0
|
148668012
|
def check(arr):
l=[]
l.append(arr[0])
i=1
mx=0
while i<len(arr):
while len(l)>1 and l[-1]<=arr[i]:
l.pop()
l.append(arr[i])
s=l[-1]^l[-2]
if s>mx:
mx=s
i+=1
return mx
n=int(input())
arr=list(map(int,input().split()))
print(check(arr))
| 56
| 93
| 15,872,000
|
167073112
|
import sys
input = sys.stdin.readline
n = int(input())
s = list(map(int, input().split()))
ans = 0
st = []
# every v can be the second max, max on left or right
for v in s:
# decreasing stack
# st[-1] second max, v max(max on right)
while st and st[-1] < v:
ans = max(st.pop() ^ v, ans)
# v second max, st[-1] max(max on left)
if st:
ans = max(st[-1] ^ v, ans)
st.append(v)
print(ans)
|
Codeforces Round 172 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Maximum Xor Secondary
|
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: $$x_j \neq \max_{i=1}^{k} x_i$$.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
|
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
|
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
| null |
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
|
[{"input": "5\n5 2 1 4 3", "output": "7"}, {"input": "5\n9 8 3 5 7", "output": "15"}]
| 1,800
|
["data structures", "implementation", "two pointers"]
| 56
|
[{"input": "5\r\n5 2 1 4 3\r\n", "output": "7\r\n"}, {"input": "5\r\n9 8 3 5 7\r\n", "output": "15\r\n"}, {"input": "10\r\n76969694 71698884 32888447 31877010 65564584 87864180 7850891 1505323 17879621 15722446\r\n", "output": "128869996\r\n"}, {"input": "10\r\n4547989 39261040 94929326 38131456 26174500 7152864 71295827 77784626 89898294 68006331\r\n", "output": "134189790\r\n"}, {"input": "10\r\n30301275 19973434 63004643 54007648 93722492 91677384 58694045 41546981 15552151 5811338\r\n", "output": "112066588\r\n"}, {"input": "10\r\n47606126 65484553 142643 35352821 26622058 5603080 7296801 53938188 34750256 97196502\r\n", "output": "131671782\r\n"}, {"input": "10\r\n82942694 74816699 72957520 1634864 60842992 60103606 61079517 41624114 13932450 24035648\r\n", "output": "133874061\r\n"}, {"input": "10\r\n73622246 45316865 2066146 61168230 1258786 69603039 64470479 72811017 72683016 97992629\r\n", "output": "133280528\r\n"}, {"input": "10\r\n29272229 8752316 10025994 52398694 57994948 49609605 28150935 66061676 44865054 87041483\r\n", "output": "127710165\r\n"}, {"input": "10\r\n3106954 3413954 3854371 85952704 17834583 20954227 58810981 7460648 97908613 97965110\r\n", "output": "111078053\r\n"}, {"input": "3\r\n11 10 8\r\n", "output": "2\r\n"}, {"input": "2\r\n5 6\r\n", "output": "3\r\n"}, {"input": "2\r\n16 17\r\n", "output": "1\r\n"}, {"input": "3\r\n8 9 10\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 61
| 0
|
29606665
|
a=input()
print(a)
for i in range(int(input())):
x,y=input().split()
a=a.replace(x,y)
print(a)
| 57
| 46
| 0
|
164733818
|
a,b = list(input().split())
n = int(input())
print(a, b)
for i in range(n):
c,d = list(input().split())
if a==c:
a=d
elif b==c:
b=d
print(a,b)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
776/A
|
776
|
A
|
PyPy 3-64
|
TESTS
| 4
| 77
| 3,481,600
|
206252672
|
ans = []
fn = input()
ans.append(fn)
for i in range(int(input())):
kd,new = input().split(" ")
if kd == fn.split(" ")[0] or kd == fn.split(" ")[1]:
fn = fn.replace(kd,new)
ans.append(fn)
for i in ans:
print(i)
| 57
| 46
| 0
|
167957368
|
if __name__ == '__main__':
f, s = input().split(' ')
print(f + ' ' + s)
n = int(input())
for i in range(n):
ft, st = input().split(' ')
if f == ft:
f = st
else:
s = st
print(f + ' ' + s)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
776/A
|
776
|
A
|
Python 3
|
TESTS
| 4
| 93
| 7,168,000
|
87838095
|
s = input()
day = int(input())
victim = [s]
while day > 0 :
k = input().split()
v = victim[-1].replace(k[0],k[-1])
victim.append(v)
day -= 1
for y in victim:
print (y)
| 57
| 46
| 0
|
168855018
|
a={*input().split()}
print(*a)
for _ in[0]*int(input()):
b,c=input().split()
a=a-{b}|{c}
print(*a)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
280/B
|
280
|
B
|
Python 3
|
TESTS
| 3
| 109
| 409,600
|
70600445
|
#!/usr/bin/env python
# coding: utf-8
# In[2]:
from collections import deque
stack=deque()
n=int(input())
a=list(map(int,input().split()))
res=a[0]^a[1]
l=0
for i in range(n):
while stack and stack[-1]<a[i]:
stack.pop()
l-=1
stack.append(a[i])
l+=1
if l>1:
res=max(res,stack[-1]^stack[-2])
print(res)
# In[ ]:
| 56
| 93
| 16,281,600
|
167104502
|
import sys
input = sys.stdin.readline
n = int(input())
s = list(map(int, input().split()))
stack, res = [], 0
for i in range(n):
while stack and stack[-1] < s[i]:
res = max(res, stack[-1] ^ s[i])
stack.pop()
stack.append(s[i])
if len(stack) > 1:
res = max(res, stack[-2] ^ stack[-1])
print(res)
|
Codeforces Round 172 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Maximum Xor Secondary
|
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: $$x_j \neq \max_{i=1}^{k} x_i$$.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
|
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
|
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
| null |
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
|
[{"input": "5\n5 2 1 4 3", "output": "7"}, {"input": "5\n9 8 3 5 7", "output": "15"}]
| 1,800
|
["data structures", "implementation", "two pointers"]
| 56
|
[{"input": "5\r\n5 2 1 4 3\r\n", "output": "7\r\n"}, {"input": "5\r\n9 8 3 5 7\r\n", "output": "15\r\n"}, {"input": "10\r\n76969694 71698884 32888447 31877010 65564584 87864180 7850891 1505323 17879621 15722446\r\n", "output": "128869996\r\n"}, {"input": "10\r\n4547989 39261040 94929326 38131456 26174500 7152864 71295827 77784626 89898294 68006331\r\n", "output": "134189790\r\n"}, {"input": "10\r\n30301275 19973434 63004643 54007648 93722492 91677384 58694045 41546981 15552151 5811338\r\n", "output": "112066588\r\n"}, {"input": "10\r\n47606126 65484553 142643 35352821 26622058 5603080 7296801 53938188 34750256 97196502\r\n", "output": "131671782\r\n"}, {"input": "10\r\n82942694 74816699 72957520 1634864 60842992 60103606 61079517 41624114 13932450 24035648\r\n", "output": "133874061\r\n"}, {"input": "10\r\n73622246 45316865 2066146 61168230 1258786 69603039 64470479 72811017 72683016 97992629\r\n", "output": "133280528\r\n"}, {"input": "10\r\n29272229 8752316 10025994 52398694 57994948 49609605 28150935 66061676 44865054 87041483\r\n", "output": "127710165\r\n"}, {"input": "10\r\n3106954 3413954 3854371 85952704 17834583 20954227 58810981 7460648 97908613 97965110\r\n", "output": "111078053\r\n"}, {"input": "3\r\n11 10 8\r\n", "output": "2\r\n"}, {"input": "2\r\n5 6\r\n", "output": "3\r\n"}, {"input": "2\r\n16 17\r\n", "output": "1\r\n"}, {"input": "3\r\n8 9 10\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
280/B
|
280
|
B
|
Python 3
|
TESTS
| 3
| 46
| 0
|
148669691
|
def solve(v: list):
ans = v[0]^v[1]
st = []
n = len(v)
for i in range(n):
while len(st) > 0 and st[-1] < v[i]:
st.pop(-1)
st.append(v[i])
if len(st) >= 2:
ans = max(ans, st[-1]^st[-2])
return ans
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
print(solve(arr))
| 56
| 108
| 14,745,600
|
167086536
|
n = int(input())
a = list(map(int, input().split()))
stack = []
res = 0
for i, v in enumerate(a):
while stack and stack[-1] < v:
res = max(res, stack.pop() ^ v)
if stack:
res = max(res, stack[-1] ^ v)
stack.append(v)
print(res)
|
Codeforces Round 172 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Maximum Xor Secondary
|
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: $$x_j \neq \max_{i=1}^{k} x_i$$.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
|
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
|
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
| null |
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
|
[{"input": "5\n5 2 1 4 3", "output": "7"}, {"input": "5\n9 8 3 5 7", "output": "15"}]
| 1,800
|
["data structures", "implementation", "two pointers"]
| 56
|
[{"input": "5\r\n5 2 1 4 3\r\n", "output": "7\r\n"}, {"input": "5\r\n9 8 3 5 7\r\n", "output": "15\r\n"}, {"input": "10\r\n76969694 71698884 32888447 31877010 65564584 87864180 7850891 1505323 17879621 15722446\r\n", "output": "128869996\r\n"}, {"input": "10\r\n4547989 39261040 94929326 38131456 26174500 7152864 71295827 77784626 89898294 68006331\r\n", "output": "134189790\r\n"}, {"input": "10\r\n30301275 19973434 63004643 54007648 93722492 91677384 58694045 41546981 15552151 5811338\r\n", "output": "112066588\r\n"}, {"input": "10\r\n47606126 65484553 142643 35352821 26622058 5603080 7296801 53938188 34750256 97196502\r\n", "output": "131671782\r\n"}, {"input": "10\r\n82942694 74816699 72957520 1634864 60842992 60103606 61079517 41624114 13932450 24035648\r\n", "output": "133874061\r\n"}, {"input": "10\r\n73622246 45316865 2066146 61168230 1258786 69603039 64470479 72811017 72683016 97992629\r\n", "output": "133280528\r\n"}, {"input": "10\r\n29272229 8752316 10025994 52398694 57994948 49609605 28150935 66061676 44865054 87041483\r\n", "output": "127710165\r\n"}, {"input": "10\r\n3106954 3413954 3854371 85952704 17834583 20954227 58810981 7460648 97908613 97965110\r\n", "output": "111078053\r\n"}, {"input": "3\r\n11 10 8\r\n", "output": "2\r\n"}, {"input": "2\r\n5 6\r\n", "output": "3\r\n"}, {"input": "2\r\n16 17\r\n", "output": "1\r\n"}, {"input": "3\r\n8 9 10\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
282/B
|
282
|
B
|
PyPy 3-64
|
TESTS
| 2
| 92
| 0
|
228546199
|
n = int(input())
A,G = 0,0
s = ""
for i in range(n):
a,b = map(int,input().split())
if(A==G):
if(a<b):
A+=a
s+="A"
else:
G+=b
s+="G"
elif(A-G>500):
G+=b
s+="G"
elif(G-A>500):
A+=a
s+="A"
else:
if(a<b):
A+=a
s+="A"
else :
G+=b
s+="G"
if(abs(A-G)<=500):
print(s)
else : print(-1)
| 54
| 1,652
| 10,752,000
|
229981075
|
import sys
n = int(sys.stdin.readline())
d = 0
for _ in range(n):
a, g = map(int, sys.stdin.readline().split())
if d + a <= 500:
d += a
print('A', end='')
else:
d -= g
print('G', end='')# 1698396157.2401366
|
Codeforces Round 173 (Div. 2)
|
CF
| 2,013
| 5
| 256
|
Painting Eggs
|
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
|
The first line contains integer n (1 ≤ n ≤ 106) — the number of eggs.
Next n lines contain two integers ai and gi each (0 ≤ ai, gi ≤ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
|
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| ≤ 500.
If there are several solutions, you are allowed to print any of them.
| null | null |
[{"input": "2\n1 999\n999 1", "output": "AG"}, {"input": "3\n400 600\n400 600\n400 600", "output": "AGA"}]
| 1,500
|
["greedy", "math"]
| 54
|
[{"input": "2\r\n1 999\r\n999 1\r\n", "output": "AG\r\n"}, {"input": "3\r\n400 600\r\n400 600\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "2\r\n500 500\r\n500 500\r\n", "output": "AG\r\n"}, {"input": "1\r\n1 999\r\n", "output": "A\r\n"}, {"input": "10\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n", "output": "AAAAAAAAAA\r\n"}, {"input": "2\r\n499 501\r\n501 499\r\n", "output": "AG\r\n"}, {"input": "3\r\n500 500\r\n1 999\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "1\r\n0 1000\r\n", "output": "A\r\n"}, {"input": "1\r\n500 500\r\n", "output": "A\r\n"}, {"input": "1\r\n1000 0\r\n", "output": "G\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input data
with open(input_path) as f:
n = int(f.readline())
eggs = [tuple(map(int, line.split())) for line in f]
# Read reference and submission outputs
with open(output_path) as f:
ref_output = f.read().strip()
with open(submission_path) as f:
sub_output = f.read().strip()
# Check if reference says it's impossible
if ref_output == "-1":
return 1 if sub_output == "-1" else 0
# Check submission is not -1
if sub_output == "-1":
return 0
# Validate submission format
if len(sub_output) != n or any(c not in 'AG' for c in sub_output):
return 0
# Calculate Sa and Sg
sa, sg = 0, 0
for i in range(n):
a, g = eggs[i]
if sub_output[i] == 'A':
sa += a
else:
sg += g
# Check the absolute difference condition
if abs(sa - sg) > 500:
return 0
return 1
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
score = main(input_path, output_path, submission_path)
print(score)
| true
|
282/B
|
282
|
B
|
Python 3
|
TESTS
| 2
| 60
| 0
|
228749425
|
t = int(input())
sa = 0
sg = 0
ans = ""
for i in range(t):
a, g = map(int, input().split())
if a < g and abs(sa + a - sg) <= 500:
sa = sa + a
ans = ans + 'A'
else:
sg = sg + g
ans = ans + 'G'
print(ans)
| 54
| 3,898
| 156,057,600
|
229604446
|
n = int(input())
eggs = []
for i in range(n):
a, g = map(int, input().split())
eggs.append((a, g))
result = []
total_a_payment = 0
total_g_payment = 0
for a, g in eggs:
if total_a_payment + a - total_g_payment <= 500:
result.append('A')
total_a_payment += a
else:
result.append('G')
total_g_payment += g
if abs(total_a_payment - total_g_payment) > 500:
print(-1)
else:
print("".join(result))
|
Codeforces Round 173 (Div. 2)
|
CF
| 2,013
| 5
| 256
|
Painting Eggs
|
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
|
The first line contains integer n (1 ≤ n ≤ 106) — the number of eggs.
Next n lines contain two integers ai and gi each (0 ≤ ai, gi ≤ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
|
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| ≤ 500.
If there are several solutions, you are allowed to print any of them.
| null | null |
[{"input": "2\n1 999\n999 1", "output": "AG"}, {"input": "3\n400 600\n400 600\n400 600", "output": "AGA"}]
| 1,500
|
["greedy", "math"]
| 54
|
[{"input": "2\r\n1 999\r\n999 1\r\n", "output": "AG\r\n"}, {"input": "3\r\n400 600\r\n400 600\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "2\r\n500 500\r\n500 500\r\n", "output": "AG\r\n"}, {"input": "1\r\n1 999\r\n", "output": "A\r\n"}, {"input": "10\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n", "output": "AAAAAAAAAA\r\n"}, {"input": "2\r\n499 501\r\n501 499\r\n", "output": "AG\r\n"}, {"input": "3\r\n500 500\r\n1 999\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "1\r\n0 1000\r\n", "output": "A\r\n"}, {"input": "1\r\n500 500\r\n", "output": "A\r\n"}, {"input": "1\r\n1000 0\r\n", "output": "G\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input data
with open(input_path) as f:
n = int(f.readline())
eggs = [tuple(map(int, line.split())) for line in f]
# Read reference and submission outputs
with open(output_path) as f:
ref_output = f.read().strip()
with open(submission_path) as f:
sub_output = f.read().strip()
# Check if reference says it's impossible
if ref_output == "-1":
return 1 if sub_output == "-1" else 0
# Check submission is not -1
if sub_output == "-1":
return 0
# Validate submission format
if len(sub_output) != n or any(c not in 'AG' for c in sub_output):
return 0
# Calculate Sa and Sg
sa, sg = 0, 0
for i in range(n):
a, g = eggs[i]
if sub_output[i] == 'A':
sa += a
else:
sg += g
# Check the absolute difference condition
if abs(sa - sg) > 500:
return 0
return 1
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
score = main(input_path, output_path, submission_path)
print(score)
| true
|
469/B
|
469
|
B
|
Python 3
|
TESTS
| 2
| 31
| 0
|
225074152
|
p, q, l, r = map(int, input().split())
line_x = []
while p:
a, b = map(int, input().split())
line_x.append([a, b])
p -= 1
line_z = []
while q:
a, b = map(int, input().split())
line_z.append([a, b])
q -= 1
n_line = []
for i in range(len(line_x)):
for j in range(len(line_z)):
p1 = line_x[i][0] - line_z[j][1]
p2 = line_x[i][1] - line_z[j][0]
if p1 < l:
p1 = l
elif p1 > r:
p1 = r
if p2 < l:
p2 = l
elif p2 > r:
p2 = r
for k in range(p1, p2+1):
n_line.append(k)
print(len(set(n_line)))
| 31
| 62
| 0
|
7875077
|
def inter(A, B, t):
for i in B:
if (i + t) in A:
return True
return False
p, q, r, l = map(int, input().split())
X = set()
Z = set()
for i in range(p):
b, e = map(int, input().split())
for i in range(b, e + 1):
Z.add(i)
for i in range(q):
b, e = map(int, input().split())
for i in range(b, e + 1):
X.add(i)
cnt = 0
for i in range(r, l + 1):
if inter(Z, X, i):
cnt += 1
print(cnt)
|
Codeforces Round 268 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Chat Online
|
Little X and Little Z are good friends. They always chat online. But both of them have schedules.
Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).
If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
|
The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000).
Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000).
It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j.
|
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
| null | null |
[{"input": "1 1 0 4\n2 3\n0 1", "output": "3"}, {"input": "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17", "output": "20"}]
| 1,300
|
["implementation"]
| 31
|
[{"input": "1 1 0 4\r\n2 3\r\n0 1\r\n", "output": "3\r\n"}, {"input": "2 3 0 20\r\n15 17\r\n23 26\r\n1 4\r\n7 11\r\n15 17\r\n", "output": "20\r\n"}, {"input": "5 2 27 452\r\n148 154\r\n421 427\r\n462 470\r\n777 786\r\n969 978\r\n245 247\r\n313 322\r\n", "output": "54\r\n"}, {"input": "3 6 25 785\r\n273 275\r\n391 397\r\n775 783\r\n84 89\r\n348 354\r\n480 483\r\n552 556\r\n711 716\r\n838 844\r\n", "output": "99\r\n"}, {"input": "1 6 364 421\r\n649 688\r\n31 39\r\n438 441\r\n516 524\r\n609 615\r\n708 710\r\n850 851\r\n", "output": "0\r\n"}, {"input": "7 10 87 239\r\n8 23\r\n325 374\r\n516 540\r\n633 658\r\n728 742\r\n833 839\r\n960 995\r\n99 114\r\n213 232\r\n264 269\r\n321 327\r\n492 540\r\n551 559\r\n587 590\r\n625 637\r\n704 723\r\n750 764\r\n", "output": "151\r\n"}, {"input": "33 17 295 791\r\n41 49\r\n66 73\r\n95 102\r\n118 126\r\n157 158\r\n189 198\r\n228 237\r\n247 251\r\n301 307\r\n318 326\r\n328 333\r\n356 363\r\n373 381\r\n454 460\r\n463 466\r\n471 477\r\n500 501\r\n505 510\r\n559 566\r\n585 588\r\n597 604\r\n675 684\r\n688 695\r\n699 705\r\n749 755\r\n780 788\r\n869 873\r\n879 888\r\n890 892\r\n909 918\r\n953 954\r\n973 978\r\n993 996\r\n53 60\r\n68 74\r\n102 105\r\n149 153\r\n191 206\r\n230 242\r\n249 252\r\n320 325\r\n483 488\r\n509 513\r\n523 524\r\n544 560\r\n651 655\r\n670 672\r\n691 708\r\n742 748\r\n920 939\r\n", "output": "497\r\n"}, {"input": "5 4 520 527\r\n257 263\r\n309 315\r\n434 439\r\n540 541\r\n759 763\r\n168 176\r\n313 316\r\n546 548\r\n880 881\r\n", "output": "0\r\n"}, {"input": "11 40 58 976\r\n14 22\r\n85 92\r\n110 112\r\n173 181\r\n188 190\r\n212 218\r\n494 498\r\n530 531\r\n647 656\r\n943 948\r\n960 968\r\n2 16\r\n20 31\r\n40 86\r\n90 91\r\n93 96\r\n103 118\r\n128 155\r\n170 173\r\n176 192\r\n205 207\r\n210 217\r\n237 245\r\n250 280\r\n302 304\r\n311 328\r\n332 376\r\n387 419\r\n422 442\r\n449 454\r\n462 491\r\n496 498\r\n501 516\r\n525 552\r\n556 565\r\n567 573\r\n577 578\r\n586 635\r\n651 677\r\n688 699\r\n704 714\r\n718 719\r\n733 766\r\n768 769\r\n773 811\r\n822 826\r\n830 870\r\n885 917\r\n937 961\r\n971 981\r\n991 1000\r\n", "output": "909\r\n"}, {"input": "10 28 435 847\r\n48 57\r\n86 95\r\n186 188\r\n297 304\r\n500 506\r\n594 600\r\n602 609\r\n802 811\r\n906 911\r\n916 921\r\n20 22\r\n115 118\r\n139 141\r\n170 171\r\n184 188\r\n193 197\r\n227 230\r\n232 235\r\n250 252\r\n256 260\r\n271 275\r\n322 324\r\n326 328\r\n379 381\r\n433 434\r\n528 529\r\n563 565\r\n583 584\r\n614 615\r\n617 619\r\n631 632\r\n726 729\r\n750 754\r\n814 817\r\n853 855\r\n881 882\r\n946 947\r\n981 984\r\n", "output": "284\r\n"}, {"input": "13 42 292 733\r\n304 308\r\n362 365\r\n396 397\r\n686 688\r\n719 722\r\n815 816\r\n901 902\r\n904 905\r\n921 924\r\n929 933\r\n953 954\r\n977 980\r\n982 985\r\n28 30\r\n36 37\r\n108 112\r\n123 127\r\n134 135\r\n151 153\r\n160 162\r\n203 205\r\n208 212\r\n240 243\r\n261 264\r\n271 274\r\n294 297\r\n356 358\r\n383 385\r\n408 412\r\n415 418\r\n435 439\r\n444 447\r\n500 502\r\n507 511\r\n515 519\r\n525 529\r\n541 543\r\n566 569\r\n577 579\r\n606 609\r\n618 622\r\n655 656\r\n715 718\r\n744 745\r\n767 771\r\n798 802\r\n828 830\r\n839 840\r\n842 845\r\n893 894\r\n897 898\r\n902 906\r\n958 960\r\n981 983\r\n988 992\r\n", "output": "412\r\n"}, {"input": "5 46 211 943\r\n367 371\r\n405 408\r\n639 642\r\n821 825\r\n974 978\r\n13 14\r\n31 34\r\n36 39\r\n48 51\r\n82 86\r\n173 174\r\n193 196\r\n227 230\r\n249 250\r\n259 261\r\n292 294\r\n325 327\r\n329 330\r\n352 353\r\n380 383\r\n390 391\r\n398 399\r\n411 414\r\n418 420\r\n424 427\r\n478 479\r\n488 489\r\n500 502\r\n511 514\r\n534 535\r\n548 552\r\n581 584\r\n596 600\r\n609 611\r\n618 622\r\n641 642\r\n656 657\r\n674 678\r\n707 709\r\n718 720\r\n746 748\r\n752 755\r\n771 773\r\n775 777\r\n865 869\r\n892 896\r\n910 911\r\n946 948\r\n963 964\r\n977 978\r\n986 990\r\n", "output": "428\r\n"}, {"input": "1 8 169 472\r\n63 553\r\n32 65\r\n75 204\r\n263 277\r\n289 326\r\n342 441\r\n452 911\r\n945 956\r\n971 986\r\n", "output": "304\r\n"}, {"input": "2 1 17 179\r\n159 202\r\n579 602\r\n115 126\r\n", "output": "55\r\n"}, {"input": "1 1 0 1000\r\n0 1000\r\n0 1000\r\n", "output": "1001\r\n"}, {"input": "10 11 201 515\r\n2 3\r\n102 103\r\n202 203\r\n302 304\r\n401 402\r\n502 503\r\n600 603\r\n701 702\r\n800 803\r\n900 902\r\n1 2\r\n103 104\r\n201 202\r\n302 304\r\n400 404\r\n501 504\r\n600 601\r\n603 604\r\n701 704\r\n800 801\r\n900 902\r\n", "output": "26\r\n"}, {"input": "1 7 140 478\r\n400 404\r\n3 4\r\n100 105\r\n301 304\r\n403 405\r\n504 506\r\n600 605\r\n904 906\r\n", "output": "16\r\n"}, {"input": "9 8 453 552\r\n5 18\r\n23 28\r\n125 132\r\n208 209\r\n215 230\r\n516 527\r\n808 819\r\n906 913\r\n926 929\r\n4 18\r\n221 229\r\n316 330\r\n403 424\r\n506 516\r\n805 828\r\n904 914\r\n916 917\r\n", "output": "49\r\n"}, {"input": "9 1 24 52\r\n63 94\r\n150 164\r\n244 275\r\n379 385\r\n413 420\r\n441 490\r\n506 545\r\n614 661\r\n752 776\r\n531 534\r\n", "output": "0\r\n"}, {"input": "1 1 0 0\r\n1 3\r\n2 5\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
774/F
|
774
|
F
|
Python 3
|
TESTS
| 2
| 46
| 5,529,600
|
26153159
|
n = int(input())
a = list(map(int, input().split()))
ans = 100000
min = 10000000000
if ((n % 7) == 0):
for i in range(6, n, 7):
if a[i] < min:
ans = i
min = a[i]
else:
i = 7 % n
while True:
if a[i] < min:
ans = i
min = a[i]
i = (i + 7) % n
if i == (7 % n):
break
print(ans + 1)
| 88
| 155
| 7,680,000
|
173867248
|
import sys
def Min(x, y):
if x > y:
return y
else:
return x
def Gcd(x, y):
if x == 0:
return y
else:
return Gcd(y % x, x)
def Lcm(x, y):
return x * y // Gcd(x, y)
n = int(input())
a = [int(i) for i in input().split()]
d = [int(0) for i in range(0, n)]
ok = 0
cur = 0
len = Lcm(7, n)
for i in range(0, 7 * n):
if a[i % n] == 0 :
print(i % n + 1)
ok = 1
break
if cur != 6:
a[i % n] -= 1
d[i % n] += 1
cur = (cur + 1) % 7
if ok == 0:
k = 10**20
for i in range(0, n):
a[i] += d[i]
if d[i] == 0: continue
if a[i] % d[i] > 0:
k = Min(k, a[i] // d[i])
else:
k = Min(k, a[i] // d[i] - 1)
if k == 10**20:
k = 0
for i in range(0, n):
a[i] -= k * d[i]
iter = 0
cur = 0
while True:
if a[iter] == 0:
print(iter % n + 1)
break
else:
if cur != 6:
a[iter] -= 1
cur = (cur + 1) % 7
iter = (iter + 1) % n
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 3
| 256
|
Pens And Days Of Week
|
Stepan has n pens. Every day he uses them, and on the i-th day he uses the pen number i. On the (n + 1)-th day again he uses the pen number 1, on the (n + 2)-th — he uses the pen number 2 and so on.
On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses that day. On Sunday Stepan has a day of rest, he does not stend the ink of the pen he uses that day.
Stepan knows the current volume of ink in each of his pens. Now it's the Monday morning and Stepan is going to use the pen number 1 today. Your task is to determine which pen will run out of ink before all the rest (that is, there will be no ink left in it), if Stepan will use the pens according to the conditions described above.
|
The first line contains the integer n (1 ≤ n ≤ 50 000) — the number of pens Stepan has.
The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is equal to the number of milliliters of ink which the pen number i currently has.
|
Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above.
Pens are numbered in the order they are given in input data. The numeration begins from one.
Note that the answer is always unambiguous, since several pens can not end at the same time.
| null |
In the first test Stepan uses ink of pens as follows:
1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it;
2. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it;
3. on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it;
4. on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it;
5. on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it;
6. on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it;
7. on the day number 7 (Sunday) Stepan will use the pen number 1, but it is a day of rest so he will not waste ink of this pen in it;
8. on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink.
So, the first pen which will not have ink is the pen number 2.
|
[{"input": "3\n3 3 3", "output": "2"}, {"input": "5\n5 4 5 4 4", "output": "5"}]
| 2,700
|
["*special", "binary search", "number theory"]
| 88
|
[{"input": "3\r\n3 3 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 4 5 4 4\r\n", "output": "5\r\n"}, {"input": "28\r\n2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033\r\n", "output": "1\r\n"}, {"input": "7\r\n10 10 10 10 10 10 10\r\n", "output": "1\r\n"}, {"input": "28\r\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000\r\n", "output": "1\r\n"}, {"input": "21\r\n996 995 996 996 996 996 995 996 996 995 996 996 995 996 995 995 995 995 996 996 996\r\n", "output": "2\r\n"}, {"input": "28\r\n2033 2033 2034 2033 2034 2034 2033 2034 2033 2034 2033 2034 2034 2033 2033 2034 2034 2033 2034 2034 2034 2033 2034 2033 2034 2034 2034 2034\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n2\r\n", "output": "1\r\n"}, {"input": "1\r\n1123\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2\r\n"}, {"input": "2\r\n999999999 999999999\r\n", "output": "1\r\n"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n999999999 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n999999999 999999999 999999999 999999999\r\n", "output": "1\r\n"}, {"input": "5\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "5\r\n999999999 1000000000 999999999 1000000000 999999999\r\n", "output": "1\r\n"}, {"input": "6\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4\r\n"}, {"input": "6\r\n1000000000 999999999 999999999 999999999 1000000000 1000000000\r\n", "output": "3\r\n"}, {"input": "7\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "7\r\n1000000000 1000000000 1000000000 1000000000 999999999 999999999 999999999\r\n", "output": "5\r\n"}, {"input": "8\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "8\r\n1000000000 999999999 1000000000 999999999 1000000000 999999999 999999999 999999999\r\n", "output": "2\r\n"}, {"input": "7\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
716/A
|
716
|
A
|
PyPy 3-64
|
TESTS
| 54
| 170
| 12,595,200
|
153783613
|
n, c = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
count = 0
a = 0
flag = False
for i in range(1,n):
flag = True
b = arr[i]
if abs(a-b)<=c:
count += 1
else:
count = 0
a = arr[i]
print(count+1)
| 81
| 77
| 13,209,600
|
204541880
|
n,c = map(int,input().split())
a = list(map(int,input().split()))
m = 0
for i in range(1,len(a)):
if((a[-i] - a[-(i+1)]) <= c):
m = m+1
continue
else:
break
print(m+1)
|
Codeforces Round 372 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Crazy Computer
|
ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear!
More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.
For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.
You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.
|
The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively.
The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.
|
Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn.
| null |
The first sample is already explained in the problem statement.
For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1.
|
[{"input": "6 5\n1 3 8 14 19 20", "output": "3"}, {"input": "6 1\n1 3 5 7 9 10", "output": "2"}]
| 800
|
["implementation"]
| 81
|
[{"input": "6 5\r\n1 3 8 14 19 20\r\n", "output": "3"}, {"input": "6 1\r\n1 3 5 7 9 10\r\n", "output": "2"}, {"input": "1 1\r\n1000000000\r\n", "output": "1"}, {"input": "5 5\r\n1 7 12 13 14\r\n", "output": "4"}, {"input": "2 1000000000\r\n1 1000000000\r\n", "output": "2"}, {"input": "3 5\r\n1 10 20\r\n", "output": "1"}, {"input": "3 10\r\n1 2 3\r\n", "output": "3"}, {"input": "2 1\r\n1 100\r\n", "output": "1"}, {"input": "3 1\r\n1 2 10\r\n", "output": "1"}, {"input": "2 1\r\n1 2\r\n", "output": "2"}]
| false
|
stdio
| null | true
|
964/B
|
964
|
B
|
Python 3
|
TESTS
| 12
| 109
| 0
|
45684411
|
n, a, b, c, t = map(int, input().split())
y = list(map(int, input().split()[:n]))
su, r = 0, 0
if c - b > 0:
for i in range(1, n):
su += c * i
for i in range(n - 1):
r += a - (b * y[i])
print(su + r + y[-1])
else:
su = n * a
print(su)
| 60
| 62
| 0
|
178951013
|
R=lambda:map(int,input().split())
n,A,B,C,T=R()
print(sum(A+max(0,C-B)*(T-t)for t in R()))
|
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Messages
|
There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.
Also, each minute Vasya's bank account receives C·k, where k is the amount of received but unread messages.
Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.
Determine the maximum amount of money Vasya's bank account can hold after T minutes.
|
The first line contains five integers n, A, B, C and T (1 ≤ n, A, B, C, T ≤ 1000).
The second string contains n integers ti (1 ≤ ti ≤ T).
|
Output one integer — the answer to the problem.
| null |
In the first sample the messages must be read immediately after receiving, Vasya receives A points for each message, n·A = 20 in total.
In the second sample the messages can be read at any integer moment.
In the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5 - 4·3) + (5 - 3·3) + (5 - 2·3) + (5 - 1·3) + 5 = - 5 points. This is 35 in total.
|
[{"input": "4 5 5 3 5\n1 5 5 4", "output": "20"}, {"input": "5 3 1 1 3\n2 2 2 1 1", "output": "15"}, {"input": "5 5 3 4 5\n1 2 3 4 5", "output": "35"}]
| 1,300
|
["math"]
| 60
|
[{"input": "4 5 5 3 5\r\n1 5 5 4\r\n", "output": "20\r\n"}, {"input": "5 3 1 1 3\r\n2 2 2 1 1\r\n", "output": "15\r\n"}, {"input": "5 5 3 4 5\r\n1 2 3 4 5\r\n", "output": "35\r\n"}, {"input": "1 6 4 3 9\r\n2\r\n", "output": "6\r\n"}, {"input": "10 9 7 5 3\r\n3 3 3 3 2 3 2 2 3 3\r\n", "output": "90\r\n"}, {"input": "44 464 748 420 366\r\n278 109 293 161 336 9 194 203 13 226 303 303 300 131 134 47 235 110 263 67 185 337 360 253 270 97 162 190 143 267 18 311 329 138 322 167 324 33 3 104 290 260 349 89\r\n", "output": "20416\r\n"}, {"input": "80 652 254 207 837\r\n455 540 278 38 19 781 686 110 733 40 434 581 77 381 818 236 444 615 302 251 762 676 771 483 767 479 326 214 316 551 544 95 157 828 813 201 103 502 751 410 84 733 431 90 261 326 731 374 730 748 303 83 302 673 50 822 46 590 248 751 345 579 689 616 331 593 428 344 754 777 178 80 602 268 776 234 637 780 712 539\r\n", "output": "52160\r\n"}, {"input": "62 661 912 575 6\r\n3 5 6 6 5 6 6 6 3 2 3 1 4 3 2 5 3 6 1 4 2 5 1 2 6 4 6 6 5 5 4 3 4 1 4 2 4 4 2 6 4 6 3 5 3 4 1 5 3 6 5 6 4 1 2 1 6 5 5 4 2 3\r\n", "output": "40982\r\n"}, {"input": "49 175 330 522 242\r\n109 81 215 5 134 185 60 242 154 148 14 221 146 229 45 120 142 43 202 176 231 105 212 69 109 219 58 103 53 211 128 138 157 95 96 122 69 109 35 46 122 118 132 135 224 150 178 134 28\r\n", "output": "1083967\r\n"}, {"input": "27 27 15 395 590\r\n165 244 497 107 546 551 232 177 428 237 209 186 135 162 511 514 408 132 11 364 16 482 279 246 30 103 152\r\n", "output": "3347009\r\n"}, {"input": "108 576 610 844 573\r\n242 134 45 515 430 354 405 179 174 366 155 4 300 176 96 36 508 70 75 316 118 563 55 340 128 214 138 511 507 437 454 478 341 443 421 573 270 362 208 107 256 471 436 378 336 507 383 352 450 411 297 34 179 551 119 524 141 288 387 9 283 241 304 214 503 559 416 447 495 61 169 228 479 568 368 441 467 401 467 542 370 243 371 315 65 67 161 383 19 144 283 5 369 242 122 396 276 488 401 387 256 128 87 425 124 226 335 238\r\n", "output": "6976440\r\n"}, {"input": "67 145 951 829 192\r\n2 155 41 125 20 70 43 47 120 190 141 8 37 183 72 141 52 168 185 71 36 12 31 3 151 98 95 82 148 110 64 10 67 54 176 130 116 5 61 90 24 43 156 49 70 186 165 109 56 11 148 119 139 120 138 124 3 159 75 173 4 101 190 64 90 176 176\r\n", "output": "9715\r\n"}, {"input": "67 322 317 647 99\r\n68 33 75 39 10 60 93 40 77 71 90 14 67 26 54 87 91 67 60 76 83 7 20 47 39 79 54 43 35 9 19 39 77 56 83 31 95 15 40 37 56 88 7 89 11 49 72 48 85 95 50 78 12 1 81 53 94 97 9 26 78 62 57 23 18 19 4\r\n", "output": "1066024\r\n"}, {"input": "32 2 74 772 674\r\n598 426 358 191 471 667 412 44 183 358 436 654 572 489 79 191 374 33 1 627 154 132 101 236 443 112 77 93 553 53 260 498\r\n", "output": "8161080\r\n"}, {"input": "111 907 98 340 279\r\n187 200 223 12 179 57 81 195 250 139 2 190 21 91 145 251 113 41 18 55 235 123 99 154 179 81 59 20 145 244 131 210 76 6 198 43 71 267 60 92 101 265 55 63 231 232 74 233 246 265 102 92 78 111 107 37 51 135 38 62 156 112 70 37 227 25 111 263 175 114 4 128 50 276 226 119 130 33 134 38 48 229 108 88 53 142 233 86 214 173 136 68 2 202 132 49 73 205 208 224 99 96 116 5 74 179 63 197 58 68 50\r\n", "output": "4297441\r\n"}]
| false
|
stdio
| null | true
|
713/C
|
713
|
C
|
Python 3
|
TESTS
| 4
| 46
| 4,505,600
|
134049564
|
import heapq
def solve(arr):
sortH = []
result = 0
inc = 0
heapq.heappush(sortH, arr[0])
for i in range(1, len(arr)):
inc += 1
val = sortH[-1] + inc
if arr[i] <= val:
result += val - arr[i]
#result += 1
heapq.heappop(sortH)
heapq.heappush(sortH, arr[i] - inc)
heapq.heappush(sortH, arr[i] - inc)
return result
# if result > 0:
# return result
# else:
# return 0
n = input()
A = [int(x) for x in input().split()]
print(solve(A))
| 57
| 140
| 204,800
|
233257658
|
import heapq
n = int(input())
d = list(map(int,input().split()))
pq = [-d[0]]
heapq.heapify(pq)
ans = 0
for i in range(1,n):
temp = i - d[i]
heapq.heappush(pq,temp)
if heapq.nsmallest(1,pq)[0] < temp:
ans += temp - heapq.nsmallest(1,pq)[0]
heapq.heappushpop(pq,temp)
print(ans)
|
Codeforces Round 371 (Div. 1)
|
CF
| 2,016
| 5
| 256
|
Sonya and Problem Wihtout a Legend
|
Sonya was unable to think of a story for this problem, so here comes the formal description.
You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.
Next line contains n integer ai (1 ≤ ai ≤ 109).
|
Print the minimum number of operation required to make the array strictly increasing.
| null |
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
|
[{"input": "7\n2 1 5 11 5 9 11", "output": "9"}, {"input": "5\n5 4 3 2 1", "output": "12"}]
| 2,300
|
["dp", "sortings"]
| 57
|
[{"input": "7\r\n2 1 5 11 5 9 11\r\n", "output": "9\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "12\r\n"}, {"input": "2\r\n1 1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "5\r\n100 80 60 70 90\r\n", "output": "54\r\n"}, {"input": "10\r\n10 16 17 11 1213 1216 1216 1209 3061 3062\r\n", "output": "16\r\n"}, {"input": "20\r\n103 103 110 105 107 119 113 121 116 132 128 124 128 125 138 137 140 136 154 158\r\n", "output": "43\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 1 2 3\r\n", "output": "3\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "50\r\n499 780 837 984 481 526 944 482 862 136 265 605 5 631 974 967 574 293 969 467 573 845 102 224 17 873 648 120 694 996 244 313 404 129 899 583 541 314 525 496 443 857 297 78 575 2 430 137 387 319\r\n", "output": "12423\r\n"}, {"input": "75\r\n392 593 98 533 515 448 220 310 386 79 539 294 208 828 75 534 875 493 94 205 656 105 546 493 60 188 222 108 788 504 809 621 934 455 307 212 630 298 938 62 850 421 839 134 950 256 934 817 209 559 866 67 990 835 534 672 468 768 757 516 959 893 275 315 692 927 321 554 801 805 885 12 67 245 495\r\n", "output": "17691\r\n"}, {"input": "10\r\n26 723 970 13 422 968 875 329 234 983\r\n", "output": "2546\r\n"}, {"input": "20\r\n245 891 363 6 193 704 420 447 237 947 664 894 512 194 513 616 671 623 686 378\r\n", "output": "3208\r\n"}, {"input": "5\r\n850 840 521 42 169\r\n", "output": "1485\r\n"}]
| false
|
stdio
| null | true
|
964/B
|
964
|
B
|
Python 3
|
TESTS
| 12
| 93
| 7,065,600
|
37403351
|
n,a,b,c,t=map(int,input().split())
l=list(map(int,input().split()))
if (c<=b):
print(n*a)
else:
s=0
for i in l:
s+=(c-b)*(n-i)
print(s+n*a)
| 60
| 62
| 0
|
228224082
|
n, A, B, C, T = map(int, input().split())
t = [int(i) for i in input().split()]
score = 0
for time in t:
if C * (T - time) + A - B * (T - time) > A:
score += C * (T - time) + A - B * (T - time)
else:
score += A
print(score)
|
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Messages
|
There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.
Also, each minute Vasya's bank account receives C·k, where k is the amount of received but unread messages.
Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.
Determine the maximum amount of money Vasya's bank account can hold after T minutes.
|
The first line contains five integers n, A, B, C and T (1 ≤ n, A, B, C, T ≤ 1000).
The second string contains n integers ti (1 ≤ ti ≤ T).
|
Output one integer — the answer to the problem.
| null |
In the first sample the messages must be read immediately after receiving, Vasya receives A points for each message, n·A = 20 in total.
In the second sample the messages can be read at any integer moment.
In the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5 - 4·3) + (5 - 3·3) + (5 - 2·3) + (5 - 1·3) + 5 = - 5 points. This is 35 in total.
|
[{"input": "4 5 5 3 5\n1 5 5 4", "output": "20"}, {"input": "5 3 1 1 3\n2 2 2 1 1", "output": "15"}, {"input": "5 5 3 4 5\n1 2 3 4 5", "output": "35"}]
| 1,300
|
["math"]
| 60
|
[{"input": "4 5 5 3 5\r\n1 5 5 4\r\n", "output": "20\r\n"}, {"input": "5 3 1 1 3\r\n2 2 2 1 1\r\n", "output": "15\r\n"}, {"input": "5 5 3 4 5\r\n1 2 3 4 5\r\n", "output": "35\r\n"}, {"input": "1 6 4 3 9\r\n2\r\n", "output": "6\r\n"}, {"input": "10 9 7 5 3\r\n3 3 3 3 2 3 2 2 3 3\r\n", "output": "90\r\n"}, {"input": "44 464 748 420 366\r\n278 109 293 161 336 9 194 203 13 226 303 303 300 131 134 47 235 110 263 67 185 337 360 253 270 97 162 190 143 267 18 311 329 138 322 167 324 33 3 104 290 260 349 89\r\n", "output": "20416\r\n"}, {"input": "80 652 254 207 837\r\n455 540 278 38 19 781 686 110 733 40 434 581 77 381 818 236 444 615 302 251 762 676 771 483 767 479 326 214 316 551 544 95 157 828 813 201 103 502 751 410 84 733 431 90 261 326 731 374 730 748 303 83 302 673 50 822 46 590 248 751 345 579 689 616 331 593 428 344 754 777 178 80 602 268 776 234 637 780 712 539\r\n", "output": "52160\r\n"}, {"input": "62 661 912 575 6\r\n3 5 6 6 5 6 6 6 3 2 3 1 4 3 2 5 3 6 1 4 2 5 1 2 6 4 6 6 5 5 4 3 4 1 4 2 4 4 2 6 4 6 3 5 3 4 1 5 3 6 5 6 4 1 2 1 6 5 5 4 2 3\r\n", "output": "40982\r\n"}, {"input": "49 175 330 522 242\r\n109 81 215 5 134 185 60 242 154 148 14 221 146 229 45 120 142 43 202 176 231 105 212 69 109 219 58 103 53 211 128 138 157 95 96 122 69 109 35 46 122 118 132 135 224 150 178 134 28\r\n", "output": "1083967\r\n"}, {"input": "27 27 15 395 590\r\n165 244 497 107 546 551 232 177 428 237 209 186 135 162 511 514 408 132 11 364 16 482 279 246 30 103 152\r\n", "output": "3347009\r\n"}, {"input": "108 576 610 844 573\r\n242 134 45 515 430 354 405 179 174 366 155 4 300 176 96 36 508 70 75 316 118 563 55 340 128 214 138 511 507 437 454 478 341 443 421 573 270 362 208 107 256 471 436 378 336 507 383 352 450 411 297 34 179 551 119 524 141 288 387 9 283 241 304 214 503 559 416 447 495 61 169 228 479 568 368 441 467 401 467 542 370 243 371 315 65 67 161 383 19 144 283 5 369 242 122 396 276 488 401 387 256 128 87 425 124 226 335 238\r\n", "output": "6976440\r\n"}, {"input": "67 145 951 829 192\r\n2 155 41 125 20 70 43 47 120 190 141 8 37 183 72 141 52 168 185 71 36 12 31 3 151 98 95 82 148 110 64 10 67 54 176 130 116 5 61 90 24 43 156 49 70 186 165 109 56 11 148 119 139 120 138 124 3 159 75 173 4 101 190 64 90 176 176\r\n", "output": "9715\r\n"}, {"input": "67 322 317 647 99\r\n68 33 75 39 10 60 93 40 77 71 90 14 67 26 54 87 91 67 60 76 83 7 20 47 39 79 54 43 35 9 19 39 77 56 83 31 95 15 40 37 56 88 7 89 11 49 72 48 85 95 50 78 12 1 81 53 94 97 9 26 78 62 57 23 18 19 4\r\n", "output": "1066024\r\n"}, {"input": "32 2 74 772 674\r\n598 426 358 191 471 667 412 44 183 358 436 654 572 489 79 191 374 33 1 627 154 132 101 236 443 112 77 93 553 53 260 498\r\n", "output": "8161080\r\n"}, {"input": "111 907 98 340 279\r\n187 200 223 12 179 57 81 195 250 139 2 190 21 91 145 251 113 41 18 55 235 123 99 154 179 81 59 20 145 244 131 210 76 6 198 43 71 267 60 92 101 265 55 63 231 232 74 233 246 265 102 92 78 111 107 37 51 135 38 62 156 112 70 37 227 25 111 263 175 114 4 128 50 276 226 119 130 33 134 38 48 229 108 88 53 142 233 86 214 173 136 68 2 202 132 49 73 205 208 224 99 96 116 5 74 179 63 197 58 68 50\r\n", "output": "4297441\r\n"}]
| false
|
stdio
| null | true
|
964/B
|
964
|
B
|
Python 3
|
TESTS
| 12
| 108
| 0
|
48377129
|
n, A, B, C, T = map(int, input().split())
t = list(map(int, input().split()))
d = C - B
if d < 0:
print(n * A)
else:
num = 0
for i in range(n):
num += (C - B) * (n - t[i])
num += A
print(num)
| 60
| 77
| 0
|
164538326
|
import sys
input = sys.stdin.readline
n, A, B, C, T = map(int, input().split())
t = [i for i in map(int, input().split()) if i <= T]
c = A*len(t)
if B >= C:
print(c)
else:
x = C-B
for i in t:
c += (T-i)*x
print(c)
|
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Messages
|
There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.
Also, each minute Vasya's bank account receives C·k, where k is the amount of received but unread messages.
Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.
Determine the maximum amount of money Vasya's bank account can hold after T minutes.
|
The first line contains five integers n, A, B, C and T (1 ≤ n, A, B, C, T ≤ 1000).
The second string contains n integers ti (1 ≤ ti ≤ T).
|
Output one integer — the answer to the problem.
| null |
In the first sample the messages must be read immediately after receiving, Vasya receives A points for each message, n·A = 20 in total.
In the second sample the messages can be read at any integer moment.
In the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5 - 4·3) + (5 - 3·3) + (5 - 2·3) + (5 - 1·3) + 5 = - 5 points. This is 35 in total.
|
[{"input": "4 5 5 3 5\n1 5 5 4", "output": "20"}, {"input": "5 3 1 1 3\n2 2 2 1 1", "output": "15"}, {"input": "5 5 3 4 5\n1 2 3 4 5", "output": "35"}]
| 1,300
|
["math"]
| 60
|
[{"input": "4 5 5 3 5\r\n1 5 5 4\r\n", "output": "20\r\n"}, {"input": "5 3 1 1 3\r\n2 2 2 1 1\r\n", "output": "15\r\n"}, {"input": "5 5 3 4 5\r\n1 2 3 4 5\r\n", "output": "35\r\n"}, {"input": "1 6 4 3 9\r\n2\r\n", "output": "6\r\n"}, {"input": "10 9 7 5 3\r\n3 3 3 3 2 3 2 2 3 3\r\n", "output": "90\r\n"}, {"input": "44 464 748 420 366\r\n278 109 293 161 336 9 194 203 13 226 303 303 300 131 134 47 235 110 263 67 185 337 360 253 270 97 162 190 143 267 18 311 329 138 322 167 324 33 3 104 290 260 349 89\r\n", "output": "20416\r\n"}, {"input": "80 652 254 207 837\r\n455 540 278 38 19 781 686 110 733 40 434 581 77 381 818 236 444 615 302 251 762 676 771 483 767 479 326 214 316 551 544 95 157 828 813 201 103 502 751 410 84 733 431 90 261 326 731 374 730 748 303 83 302 673 50 822 46 590 248 751 345 579 689 616 331 593 428 344 754 777 178 80 602 268 776 234 637 780 712 539\r\n", "output": "52160\r\n"}, {"input": "62 661 912 575 6\r\n3 5 6 6 5 6 6 6 3 2 3 1 4 3 2 5 3 6 1 4 2 5 1 2 6 4 6 6 5 5 4 3 4 1 4 2 4 4 2 6 4 6 3 5 3 4 1 5 3 6 5 6 4 1 2 1 6 5 5 4 2 3\r\n", "output": "40982\r\n"}, {"input": "49 175 330 522 242\r\n109 81 215 5 134 185 60 242 154 148 14 221 146 229 45 120 142 43 202 176 231 105 212 69 109 219 58 103 53 211 128 138 157 95 96 122 69 109 35 46 122 118 132 135 224 150 178 134 28\r\n", "output": "1083967\r\n"}, {"input": "27 27 15 395 590\r\n165 244 497 107 546 551 232 177 428 237 209 186 135 162 511 514 408 132 11 364 16 482 279 246 30 103 152\r\n", "output": "3347009\r\n"}, {"input": "108 576 610 844 573\r\n242 134 45 515 430 354 405 179 174 366 155 4 300 176 96 36 508 70 75 316 118 563 55 340 128 214 138 511 507 437 454 478 341 443 421 573 270 362 208 107 256 471 436 378 336 507 383 352 450 411 297 34 179 551 119 524 141 288 387 9 283 241 304 214 503 559 416 447 495 61 169 228 479 568 368 441 467 401 467 542 370 243 371 315 65 67 161 383 19 144 283 5 369 242 122 396 276 488 401 387 256 128 87 425 124 226 335 238\r\n", "output": "6976440\r\n"}, {"input": "67 145 951 829 192\r\n2 155 41 125 20 70 43 47 120 190 141 8 37 183 72 141 52 168 185 71 36 12 31 3 151 98 95 82 148 110 64 10 67 54 176 130 116 5 61 90 24 43 156 49 70 186 165 109 56 11 148 119 139 120 138 124 3 159 75 173 4 101 190 64 90 176 176\r\n", "output": "9715\r\n"}, {"input": "67 322 317 647 99\r\n68 33 75 39 10 60 93 40 77 71 90 14 67 26 54 87 91 67 60 76 83 7 20 47 39 79 54 43 35 9 19 39 77 56 83 31 95 15 40 37 56 88 7 89 11 49 72 48 85 95 50 78 12 1 81 53 94 97 9 26 78 62 57 23 18 19 4\r\n", "output": "1066024\r\n"}, {"input": "32 2 74 772 674\r\n598 426 358 191 471 667 412 44 183 358 436 654 572 489 79 191 374 33 1 627 154 132 101 236 443 112 77 93 553 53 260 498\r\n", "output": "8161080\r\n"}, {"input": "111 907 98 340 279\r\n187 200 223 12 179 57 81 195 250 139 2 190 21 91 145 251 113 41 18 55 235 123 99 154 179 81 59 20 145 244 131 210 76 6 198 43 71 267 60 92 101 265 55 63 231 232 74 233 246 265 102 92 78 111 107 37 51 135 38 62 156 112 70 37 227 25 111 263 175 114 4 128 50 276 226 119 130 33 134 38 48 229 108 88 53 142 233 86 214 173 136 68 2 202 132 49 73 205 208 224 99 96 116 5 74 179 63 197 58 68 50\r\n", "output": "4297441\r\n"}]
| false
|
stdio
| null | true
|
370/B
|
370
|
B
|
Python 3
|
PRETESTS
| 5
| 62
| 0
|
5370016
|
import sys
f = sys.stdin
#f = open("input.txt", "r")
n = int(f.readline())
a = f.read().strip().split("\n")
players = [[int(k) for k in i.split()[1:]] for i in a]
winners = [True for i in range(len(players))]
for index, player in enumerate(players):
if any(i == player for i in players[index+1:]):
winners[index] = False
i = 0
while i < len(players):
j = 0
l = len(players[i])
while j < len(players[i]):
for player in players[:i]:
if players[i][j] in player and len(player) <= l:
winners[i] = False
j += 1
i += 1
for i in winners:
if i:
print("YES")
else:
print("NO")
| 24
| 62
| 5,017,600
|
19912743
|
n = int(input())
a = []
for i in range(n):
li = list(map(int, input().split()))
a.append(set(li[1:]))
for i in range(n):
s = a[i]
joke = 1
for j in range(n):
t = a[j]
if t <= s and i != j:
joke = 0
break
print("YES" * joke + "NO" * (1 - joke))
|
Codeforces Round 217 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Berland Bingo
|
Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains mi numbers.
During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct.
You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.
|
The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer mi (1 ≤ mi ≤ 100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers ai, 1, ai, 2, ..., ai, mi (1 ≤ ai, k ≤ 100) — the numbers on the i-th player's card. The numbers in the lines are separated by single spaces.
It is guaranteed that all the numbers on each card are distinct.
|
Print n lines, the i-th line must contain word "YES" (without the quotes), if the i-th player can win, and "NO" (without the quotes) otherwise.
| null | null |
[{"input": "3\n1 1\n3 2 4 1\n2 10 11", "output": "YES\nNO\nYES"}, {"input": "2\n1 1\n1 1", "output": "NO\nNO"}]
| 1,300
|
["implementation"]
| 24
|
[{"input": "3\r\n1 1\r\n3 2 4 1\r\n2 10 11\r\n", "output": "YES\r\nNO\r\nYES\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "NO\r\nNO\r\n"}, {"input": "1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "YES\r\nYES\r\n"}, {"input": "2\r\n1 1\r\n2 1 2\r\n", "output": "YES\r\nNO\r\n"}, {"input": "2\r\n2 1 2\r\n1 1\r\n", "output": "NO\r\nYES\r\n"}, {"input": "2\r\n3 5 21 7\r\n6 15 5 100 21 7 17\r\n", "output": "YES\r\nNO\r\n"}, {"input": "2\r\n6 15 5 100 21 7 17\r\n3 5 21 7\r\n", "output": "NO\r\nYES\r\n"}, {"input": "10\r\n1 4\r\n1 2\r\n1 3\r\n1 5\r\n1 1\r\n1 4\r\n1 3\r\n1 5\r\n1 2\r\n1 1\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "3\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "NO\r\nYES\r\nNO\r\n"}, {"input": "10\r\n3 2 3 4\r\n1 1\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 1\r\n1 3\r\n2 4 5\r\n2 1 2\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "10\r\n1 4\r\n4 3 2 4 1\r\n1 4\r\n2 4 5\r\n4 4 3 5 1\r\n1 4\r\n1 2\r\n2 3 5\r\n2 5 3\r\n3 5 2 4\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "20\r\n2 9 16\r\n3 1 15 2\r\n1 9\r\n3 7 12 3\r\n1 18\r\n1 14\r\n4 11 13 4 6\r\n4 7 19 9 3\r\n3 9 16 5\r\n1 9\r\n1 18\r\n4 4 15 7 19\r\n2 16 2\r\n3 7 3 15\r\n2 2 20\r\n1 1\r\n1 15\r\n5 5 2 13 4 1\r\n2 9 14\r\n2 17 8\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "40\r\n2 12 19\r\n4 10 7 1 3\r\n2 15 17\r\n1 6\r\n3 17 8 20\r\n4 8 16 11 18\r\n2 2 7\r\n4 12 13 8 7\r\n3 6 1 15\r\n3 19 11 13\r\n1 2\r\n2 16 14\r\n5 1 17 8 9 5\r\n1 2\r\n3 15 17 12\r\n4 20 4 19 18\r\n1 10\r\n4 12 1 17 16\r\n4 5 10 8 11\r\n1 10\r\n1 13\r\n1 17\r\n2 19 18\r\n1 3\r\n2 6 20\r\n1 8\r\n2 3 14\r\n3 17 3 1\r\n2 4 3\r\n1 12\r\n1 15\r\n1 2\r\n2 13 9\r\n2 1 14\r\n1 1\r\n5 14 9 3 1 7\r\n2 20 16\r\n2 19 17\r\n2 4 20\r\n1 7\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
632/B
|
632
|
B
|
PyPy 3-64
|
TESTS
| 13
| 296
| 67,072,000
|
211919103
|
n=int(input())
ls=list(map(int,input().split()))
lz=list(input())
if len(set(lz))==1:
print(sum(ls))
else:
sumb=0
for i in range(n):
if lz[i]=='B':
sumb+=ls[i]
sumb1=sumb
max_front=0
for i in range(n):
if lz[i]=='A':
sumb1+=ls[i]
elif lz[i]=='B':
sumb1-=ls[i]
if sumb1 > max_front:
max_front = sumb1
sumb2=sumb
max_back=0
ls.reverse()
lz.reverse()
for i in range(n):
if lz[i]=='A':
sumb2+=ls[i]
elif lz[i]=='B':
sumb2-=ls[i]
if sumb2 > max_back:
max_back = sumb2
print(max(max_front,max_back))
| 17
| 218
| 63,180,800
|
194184018
|
import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
n = ni()
a = [0]+na()
s = input()
b = [0] * (1+n)
for i in range(n):
a[i+1] += a[i]
if s[i] == "A":
b[i+1] += b[i] + a[i+1]-a[i]
else:
b[i+1] += b[i]
ans = 0
for i in range(n+1):
ans = max(a[i]-b[i]+b[-1]-b[i], ans)
ans = max(b[i]+ (a[-1]-a[i])-(b[-1]-b[i]), ans)
print(ans)
|
Educational Codeforces Round 9
|
ICPC
| 2,016
| 1.5
| 256
|
Alice, Bob, Two Teams
|
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.
The way to split up game pieces is split into several steps:
1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
3. Alice will get all the pieces marked A and Bob will get all the pieces marked B.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.
|
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.
The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.
The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).
|
Print the only integer a — the maximum strength Bob can achieve.
| null |
In the first sample Bob should flip the suffix of length one.
In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.
In the third sample Bob should do nothing.
|
[{"input": "5\n1 2 3 4 5\nABABA", "output": "11"}, {"input": "5\n1 2 3 4 5\nAAAAA", "output": "15"}, {"input": "1\n1\nB", "output": "1"}]
| 1,400
|
["brute force", "constructive algorithms"]
| 17
|
[{"input": "5\r\n1 2 3 4 5\r\nABABA\r\n", "output": "11\r\n"}, {"input": "5\r\n1 2 3 4 5\r\nAAAAA\r\n", "output": "15\r\n"}, {"input": "1\r\n1\r\nB\r\n", "output": "1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\nABBABAABBB\r\n", "output": "33\r\n"}, {"input": "100\r\n591 417 888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983 526 103 500 963 400 23 276 704 570 757 410 658 507 620 984 244 486 454 802 411 985 303 635 283 96 597 855 775 139 839 839 61 219 986 776 72 729 69 20 917\r\nBBBAAABBBABAAABBBBAAABABBBBAAABAAABBABABAAABABABBABBABABAAAABAABABBBBBBBABBAAAABAABABABAABABABAABBAB\r\n", "output": "30928\r\n"}, {"input": "3\r\n1 1 1\r\nBAA\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1 2\r\nBAB\r\n", "output": "4\r\n"}, {"input": "2\r\n1 1\r\nBB\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\nA\r\n", "output": "1\r\n"}, {"input": "2\r\n1 1\r\nAB\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
632/B
|
632
|
B
|
PyPy 3
|
TESTS
| 5
| 140
| 0
|
92467061
|
import sys
n = int(sys.stdin.buffer.readline().decode('utf-8'))
a = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split()))
s = sys.stdin.buffer.readline().decode('utf-8').rstrip()
bob = 0
for i, c in enumerate(s):
if c == 'B':
bob += a[i]
ans = bob
for i, c in zip(range(n-1, -1, -1), reversed(s)):
if c == 'A':
bob += a[i]
else:
bob -= a[i]
ans = max(bob, ans)
print(ans)
| 17
| 218
| 72,601,600
|
176102634
|
from sys import stdin
input=lambda :stdin.readline()[:-1]
n=int(input())
a=list(map(int,input().split()))
s=input()
ans=0
for i in range(n):
if s[i]=='A':
ans+=a[i]
def calc(a,s):
mx=0
tmp=0
for i in range(n):
if s[i]=='A':
tmp-=a[i]
else:
tmp+=a[i]
mx=max(mx,tmp)
return mx
print(ans+max(calc(a,s),calc(a[::-1],s[::-1])))
|
Educational Codeforces Round 9
|
ICPC
| 2,016
| 1.5
| 256
|
Alice, Bob, Two Teams
|
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.
The way to split up game pieces is split into several steps:
1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
3. Alice will get all the pieces marked A and Bob will get all the pieces marked B.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.
|
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.
The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.
The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).
|
Print the only integer a — the maximum strength Bob can achieve.
| null |
In the first sample Bob should flip the suffix of length one.
In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.
In the third sample Bob should do nothing.
|
[{"input": "5\n1 2 3 4 5\nABABA", "output": "11"}, {"input": "5\n1 2 3 4 5\nAAAAA", "output": "15"}, {"input": "1\n1\nB", "output": "1"}]
| 1,400
|
["brute force", "constructive algorithms"]
| 17
|
[{"input": "5\r\n1 2 3 4 5\r\nABABA\r\n", "output": "11\r\n"}, {"input": "5\r\n1 2 3 4 5\r\nAAAAA\r\n", "output": "15\r\n"}, {"input": "1\r\n1\r\nB\r\n", "output": "1\r\n"}, {"input": "10\r\n1 9 7 6 2 4 7 8 1 3\r\nABBABAABBB\r\n", "output": "33\r\n"}, {"input": "100\r\n591 417 888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983 526 103 500 963 400 23 276 704 570 757 410 658 507 620 984 244 486 454 802 411 985 303 635 283 96 597 855 775 139 839 839 61 219 986 776 72 729 69 20 917\r\nBBBAAABBBABAAABBBBAAABABBBBAAABAAABBABABAAABABABBABBABABAAAABAABABBBBBBBABBAAAABAABABABAABABABAABBAB\r\n", "output": "30928\r\n"}, {"input": "3\r\n1 1 1\r\nBAA\r\n", "output": "3\r\n"}, {"input": "3\r\n2 1 2\r\nBAB\r\n", "output": "4\r\n"}, {"input": "2\r\n1 1\r\nBB\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\nA\r\n", "output": "1\r\n"}, {"input": "2\r\n1 1\r\nAB\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
631/A
|
631
|
A
|
Python 3
|
TESTS
| 17
| 46
| 0
|
221237685
|
n=int(input())
array=list(map(int, input().split()))
brray=list(map(int, input().split()))
ora=array[0]
orb=brray[0]
for a in range(1,n-1):
ora=ora|array[a]
for b in range(1,n-1):
orb=orb|brray[b]
print(ora+orb)
| 27
| 46
| 0
|
159654424
|
n = int(input())
x = list(map(int ,input().split()))
y = list(map(int ,input().split()))
summix = 0
for num in range(len(x)):
summix = summix | x[num]
summiy = 0
for num in range(len(y)):
summiy = summiy | y[num]
print(summix + summiy)
|
Codeforces Round 344 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Interview
|
Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.
We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays.
The second line contains n integers ai (0 ≤ ai ≤ 109).
The third line contains n integers bi (0 ≤ bi ≤ 109).
|
Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.
| null |
Bitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.
In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.
In the second sample, the maximum value is obtained for l = 1 and r = 9.
|
[{"input": "5\n1 2 4 3 2\n2 3 3 12 1", "output": "22"}, {"input": "10\n13 2 7 11 8 4 9 8 5 1\n5 7 18 9 2 3 0 11 8 6", "output": "46"}]
| 900
|
["brute force", "implementation"]
| 27
|
[{"input": "5\r\n1 2 4 3 2\r\n2 3 3 12 1\r\n", "output": "22"}, {"input": "10\r\n13 2 7 11 8 4 9 8 5 1\r\n5 7 18 9 2 3 0 11 8 6\r\n", "output": "46"}, {"input": "25\r\n12 30 38 109 81 124 80 33 38 48 29 78 96 48 96 27 80 77 102 65 80 113 31 118 35\r\n25 64 95 13 12 6 111 80 85 16 61 119 23 65 73 65 20 95 124 18 28 79 125 106 116\r\n", "output": "254"}, {"input": "20\r\n64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64\r\n64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64\r\n", "output": "128"}, {"input": "1\r\n1000000000\r\n1000000000\r\n", "output": "2000000000"}, {"input": "1\r\n0\r\n0\r\n", "output": "0"}, {"input": "2\r\n7 16\r\n16 7\r\n", "output": "46"}, {"input": "4\r\n6 0 0 0\r\n0 0 0 1\r\n", "output": "7"}, {"input": "8\r\n1 2 4 8 16 32 64 128\r\n1 2 4 8 16 32 64 128\r\n", "output": "510"}, {"input": "1\r\n2\r\n3\r\n", "output": "5"}, {"input": "1\r\n4\r\n3\r\n", "output": "7"}, {"input": "1\r\n1\r\n1\r\n", "output": "2"}]
| false
|
stdio
| null | true
|
847/A
|
847
|
A
|
Python 3
|
TESTS
| 7
| 46
| 204,800
|
233303279
|
import math
class node():
def __init__(self, prev, next):
self.value = None
self.vprev = prev
self.vnext = next
self.prev = prev
self.next = next
class DoublyLL():
def __init__(self):
self.leading = False
self.tailing = False
self.head = None
self.first = None
self.last = None
self.size = 0
self.combined = False
self.complete = False
def updateEnds(self):
if self.first.prev == 0:
self.leading = True
elif self.last.next == 0:
self.tailing = True
self.complete = True if self.tailing and self.leading else False
def append(self, new_node):
if self.size > 0:
self.last.next = new_node
new_node.prev = self.last
self.last = new_node
else:
self.first = new_node
self.last = new_node
self.size += 1
self.updateEnds()
def preppend(self, new_node):
if self.size > 0:
self.first.prev = new_node
new_node.next = self.first
self.first = new_node
else:
self.first = new_node
self.last = new_node
self.size += 1
self.updateEnds()
def nexts(self):
def loop(self, node):
if isinstance(node.next, int):
return [node.vnext]
else:
return [node.vnext] + loop(self, node.next)
node = self.first
return loop(self, node)
def prevs(self):
def loop(self, node):
if isinstance(node.next, int):
return [node.vprev]
else:
return [node.vprev] + loop(self, node.next)
node = self.first
return loop(self, node)
def __len__(self):
return self.size
def __str__(self):
def loop(self, node):
if isinstance(node.next, int):
return f"[{node.vprev}|{node.vnext}]"
else:
return f"[{node.vprev}|{node.vnext}], {loop(self, node.next)}"
node = self.first
return loop(self, node)
lists = []
n = int(input())
for _ in range(n):
prev, next = map(int, input().split())
if prev+next > 0:
if len(lists) < 1:
new_list = DoublyLL()
new_list.append(node(prev,next))
lists.append(new_list)
else:
placed = False
for list in lists:
if not list.combined:
if next > 0:
if list.first.prev == next:
list.preppend(node(prev, next))
if placed:
list.combined = True #ignore if combined
break
else:
placed = True
if prev > 0:
if list.last.next == prev:
list.append(node(prev, next))
if placed:
list.combined = True #ignore if combined
break
else:
placed = True
if not placed:
new_list = DoublyLL()
new_list.append(node(prev,next))
lists.append(new_list)
lists.sort(key=lambda x: len(x))
# Combine lists with equal lengths and opposite types
list_i = 0
paired_lists = []
for list in lists:
for pair_i in range(list_i+1, len(lists)):
pair = lists[pair_i]
if len(list) == len(pair) and not list.combined:
if (not list.complete) and (list.leading ^ pair.leading):
combined_list = []
if list.first.prev == 0:
leading_list = pair
pair_list = list
else:
leading_list = list
pair_list = pair
for i in range(len(list)*2):
if i%2 == 0:
combined_list.append(leading_list.prevs()[math.floor(i/2)])
else:
combined_list.append(pair_list.nexts()[math.floor(i/2)])
paired_lists += combined_list
leading_list.complete = True
pair_list.complete = True
else:
break
list_i += 1
numbers = set(range(1,n+1))
diff = numbers-set(paired_lists)
for _ in range(len(diff)):
paired_lists.append(diff.pop())
res = [None]*n
for i in range(len(paired_lists)):
prev = paired_lists[i-1] if i>0 else 0
next = paired_lists[i+1] if i+1<n else 0
res[paired_lists[i]-1] = [prev, next]
for line in res:
print(f"{line[0]} {line[1]}")
| 23
| 62
| 307,200
|
106529941
|
if __name__ == '__main__':
n=int(input())
arr=[[0,0]]
for i in range(n):
arr.append(list(map(int,input().split())))
#d_link=[[0,0]]
end=0
for i in range(1,n+1):
if not arr[i][0]:
arr[end][1]=i
arr[i][0]=end
j=i
while arr[j][1]:
j=arr[j][1]
end=j
for i in range(1,n+1):
print(*arr[i])
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 2
| 256
|
Union of Doubly Linked Lists
|
Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.
In this problem you are given n memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to n.
For each cell i you are given two values:
- li — cell containing previous element for the element in the cell i;
- ri — cell containing next element for the element in the cell i.
If cell i contains information about the element which has no previous element then li = 0. Similarly, if cell i contains information about the element which has no next element then ri = 0.
Three lists are shown on the picture.
For example, for the picture above the values of l and r are the following: l1 = 4, r1 = 7; l2 = 5, r2 = 0; l3 = 0, r3 = 0; l4 = 6, r4 = 1; l5 = 0, r5 = 2; l6 = 0, r6 = 4; l7 = 1, r7 = 0.
Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values li, ri.
Any other action, other than joining the beginning of one list to the end of another, can not be performed.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of memory cells where the doubly linked lists are located.
Each of the following n lines contains two integers li, ri (0 ≤ li, ri ≤ n) — the cells of the previous and the next element of list for cell i. Value li = 0 if element in cell i has no previous element in its list. Value ri = 0 if element in cell i has no next element in its list.
It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of n given cells.
|
Print n lines, the i-th line must contain two integers li and ri — the cells of the previous and the next element of list for cell i after all lists from the input are united in a single list. If there are many solutions print any of them.
| null | null |
[{"input": "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0", "output": "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0"}]
| 1,500
|
["implementation"]
| 23
|
[{"input": "7\r\n4 7\r\n5 0\r\n0 0\r\n6 1\r\n0 2\r\n0 4\r\n1 0\r\n", "output": "4 7\r\n5 6\r\n0 5\r\n6 1\r\n3 2\r\n2 4\r\n1 0\r\n"}, {"input": "2\r\n2 0\r\n0 1\r\n", "output": "2 0\r\n0 1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "4\r\n0 2\r\n1 0\r\n0 4\r\n3 0\r\n", "output": "0 2\r\n1 3\r\n2 4\r\n3 0\r\n"}, {"input": "5\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0 2\r\n1 3\r\n2 4\r\n3 5\r\n4 0\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "0 2\r\n1 0\r\n"}, {"input": "2\r\n0 2\r\n1 0\r\n", "output": "0 2\r\n1 0\r\n"}, {"input": "5\r\n5 3\r\n4 0\r\n1 4\r\n3 2\r\n0 1\r\n", "output": "5 3\r\n4 0\r\n1 4\r\n3 2\r\n0 1\r\n"}, {"input": "5\r\n2 0\r\n0 1\r\n0 4\r\n3 5\r\n4 0\r\n", "output": "2 3\r\n0 1\r\n1 4\r\n3 5\r\n4 0\r\n"}, {"input": "5\r\n3 4\r\n0 0\r\n0 1\r\n1 0\r\n0 0\r\n", "output": "3 4\r\n0 3\r\n2 1\r\n1 5\r\n4 0\r\n"}, {"input": "5\r\n3 0\r\n0 0\r\n0 1\r\n0 0\r\n0 0\r\n", "output": "3 4\r\n0 3\r\n2 1\r\n1 5\r\n4 0\r\n"}, {"input": "10\r\n7 5\r\n5 0\r\n4 7\r\n10 3\r\n1 2\r\n0 9\r\n3 1\r\n9 10\r\n6 8\r\n8 4\r\n", "output": "7 5\r\n5 0\r\n4 7\r\n10 3\r\n1 2\r\n0 9\r\n3 1\r\n9 10\r\n6 8\r\n8 4\r\n"}, {"input": "10\r\n6 2\r\n1 0\r\n9 4\r\n3 6\r\n10 8\r\n4 1\r\n0 10\r\n5 0\r\n0 3\r\n7 5\r\n", "output": "6 2\r\n1 0\r\n9 4\r\n3 6\r\n10 8\r\n4 1\r\n0 10\r\n5 9\r\n8 3\r\n7 5\r\n"}, {"input": "10\r\n0 9\r\n4 0\r\n5 0\r\n7 2\r\n0 3\r\n8 10\r\n0 4\r\n0 6\r\n1 0\r\n6 0\r\n", "output": "0 9\r\n4 8\r\n5 7\r\n7 2\r\n9 3\r\n8 10\r\n3 4\r\n2 6\r\n1 5\r\n6 0\r\n"}, {"input": "10\r\n7 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "7 8\r\n0 3\r\n2 4\r\n3 5\r\n4 6\r\n5 7\r\n6 1\r\n1 9\r\n8 10\r\n9 0\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 2\r\n1 3\r\n2 4\r\n3 5\r\n4 6\r\n5 7\r\n6 8\r\n7 9\r\n8 10\r\n9 0\r\n"}, {"input": "100\r\n0 0\r\n0 0\r\n0 0\r\n97 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 29\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n12 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 4\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0 2\n1 3\n2 5\n97 98\n3 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 29\n29 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 30\n12 13\n28 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 77\n76 78\n77 79\n78 80\n79 81\n80 82\n81 83\n82 84\n83 85\n84 86\n85 87\n86 88\n87 89\n88 90\n89 91\n90 92\n91 93\n92 94\n93 95\n94 96\n95 97\n96 4\n4 99\n98 100\n99 0\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 80\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n21 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\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 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 80\n80 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 77\n76 78\n77 79\n78 81\n21 22\n79 82\n81 83\n82 84\n83 85\n84 86\n85 87\n86 88\n87 89\n88 90\n89 91\n90 92\n91 93\n92 94\n93 95\n94 96\n95 97\n96 98\n97 99\n98 100\n99 0\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 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 77\n76 78\n77 79\n78 80\n79 81\n80 82\n81 83\n82 84\n83 85\n84 86\n85 87\n86 88\n87 89\n88 90\n89 91\n90 92\n91 93\n92 94\n93 95\n94 96\n95 97\n96 98\n97 99\n98 100\n99 0\n"}]
| false
|
stdio
| null | true
|
847/A
|
847
|
A
|
PyPy 3-64
|
TESTS
| 12
| 77
| 0
|
191445963
|
n = int(input())
a = []
tl = tr = 0
for i in range(n):
l, r = map(int, input().split())
a.append([str(l), str(i+1), str(r)])
if l == 0: tl += 1
if r == 0: tr += 1
if tl==1 and tr==1:
for i in range(0, n):
print(int(a[i][0]), int(a[i][2]))
else:
s = ''
p = a.copy()
L = ['0']
while p!=[]:
l = []
e = p.pop(0)
l += e
s = ''.join(e)
d = p.copy()
for c2 in d:
if c2[0]+c2[1] in s:
s = s + c2[2]
p.remove(c2)
l.append(c2[2])
elif c2[1]+c2[2] in s:
s = c2[0] + s
p.remove(c2)
l.insert(0, c2[0])
L += l[1:-1]
L.append('0')
for i in range(1,n+1):
j = L.index(str(i))
print(int(L[j-1]), int(L[j+1]))
| 23
| 62
| 1,740,800
|
172512257
|
n=int(input())
mp=dict()
arr=list()
cnct=[-1]*15
for i in range(1,n+1):
x,y=[int(a) for a in input().split()]
arr.append([x,y])
mp[i]=[x,y]
tog=list()
for i in range(1,n+1):
fi,lst=-1,-1
j=mp[i][0]
if j==0:fi=i
else:
while 1:
if mp[j][0]==0:break
j=mp[j][0]
fi=j
j=mp[i][1]
if j==0:lst=i
else:
while 1:
if mp[j][1]==0:break
j=mp[j][1]
lst=j
tmp=list()
i=fi
while i!=lst:tmp.append(i); i=mp[i][1]
tmp.append(i)
tog.append(tmp)
check=dict()
check[(tog[0][0],tog[0][-1])]=True
prv=0
for i in range(1,len(tog)):
if (tog[i][0],tog[i][-1]) in check:continue
mp[tog[prv][-1]][1]=tog[i][0]
mp[tog[i][0]][0]=tog[prv][-1]
check[(tog[i][0],tog[i][-1])]=True
prv=i
for i in mp:print(*mp[i])
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 2
| 256
|
Union of Doubly Linked Lists
|
Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.
In this problem you are given n memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to n.
For each cell i you are given two values:
- li — cell containing previous element for the element in the cell i;
- ri — cell containing next element for the element in the cell i.
If cell i contains information about the element which has no previous element then li = 0. Similarly, if cell i contains information about the element which has no next element then ri = 0.
Three lists are shown on the picture.
For example, for the picture above the values of l and r are the following: l1 = 4, r1 = 7; l2 = 5, r2 = 0; l3 = 0, r3 = 0; l4 = 6, r4 = 1; l5 = 0, r5 = 2; l6 = 0, r6 = 4; l7 = 1, r7 = 0.
Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values li, ri.
Any other action, other than joining the beginning of one list to the end of another, can not be performed.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of memory cells where the doubly linked lists are located.
Each of the following n lines contains two integers li, ri (0 ≤ li, ri ≤ n) — the cells of the previous and the next element of list for cell i. Value li = 0 if element in cell i has no previous element in its list. Value ri = 0 if element in cell i has no next element in its list.
It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of n given cells.
|
Print n lines, the i-th line must contain two integers li and ri — the cells of the previous and the next element of list for cell i after all lists from the input are united in a single list. If there are many solutions print any of them.
| null | null |
[{"input": "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0", "output": "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0"}]
| 1,500
|
["implementation"]
| 23
|
[{"input": "7\r\n4 7\r\n5 0\r\n0 0\r\n6 1\r\n0 2\r\n0 4\r\n1 0\r\n", "output": "4 7\r\n5 6\r\n0 5\r\n6 1\r\n3 2\r\n2 4\r\n1 0\r\n"}, {"input": "2\r\n2 0\r\n0 1\r\n", "output": "2 0\r\n0 1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "4\r\n0 2\r\n1 0\r\n0 4\r\n3 0\r\n", "output": "0 2\r\n1 3\r\n2 4\r\n3 0\r\n"}, {"input": "5\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0 2\r\n1 3\r\n2 4\r\n3 5\r\n4 0\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "0 2\r\n1 0\r\n"}, {"input": "2\r\n0 2\r\n1 0\r\n", "output": "0 2\r\n1 0\r\n"}, {"input": "5\r\n5 3\r\n4 0\r\n1 4\r\n3 2\r\n0 1\r\n", "output": "5 3\r\n4 0\r\n1 4\r\n3 2\r\n0 1\r\n"}, {"input": "5\r\n2 0\r\n0 1\r\n0 4\r\n3 5\r\n4 0\r\n", "output": "2 3\r\n0 1\r\n1 4\r\n3 5\r\n4 0\r\n"}, {"input": "5\r\n3 4\r\n0 0\r\n0 1\r\n1 0\r\n0 0\r\n", "output": "3 4\r\n0 3\r\n2 1\r\n1 5\r\n4 0\r\n"}, {"input": "5\r\n3 0\r\n0 0\r\n0 1\r\n0 0\r\n0 0\r\n", "output": "3 4\r\n0 3\r\n2 1\r\n1 5\r\n4 0\r\n"}, {"input": "10\r\n7 5\r\n5 0\r\n4 7\r\n10 3\r\n1 2\r\n0 9\r\n3 1\r\n9 10\r\n6 8\r\n8 4\r\n", "output": "7 5\r\n5 0\r\n4 7\r\n10 3\r\n1 2\r\n0 9\r\n3 1\r\n9 10\r\n6 8\r\n8 4\r\n"}, {"input": "10\r\n6 2\r\n1 0\r\n9 4\r\n3 6\r\n10 8\r\n4 1\r\n0 10\r\n5 0\r\n0 3\r\n7 5\r\n", "output": "6 2\r\n1 0\r\n9 4\r\n3 6\r\n10 8\r\n4 1\r\n0 10\r\n5 9\r\n8 3\r\n7 5\r\n"}, {"input": "10\r\n0 9\r\n4 0\r\n5 0\r\n7 2\r\n0 3\r\n8 10\r\n0 4\r\n0 6\r\n1 0\r\n6 0\r\n", "output": "0 9\r\n4 8\r\n5 7\r\n7 2\r\n9 3\r\n8 10\r\n3 4\r\n2 6\r\n1 5\r\n6 0\r\n"}, {"input": "10\r\n7 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 1\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "7 8\r\n0 3\r\n2 4\r\n3 5\r\n4 6\r\n5 7\r\n6 1\r\n1 9\r\n8 10\r\n9 0\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 2\r\n1 3\r\n2 4\r\n3 5\r\n4 6\r\n5 7\r\n6 8\r\n7 9\r\n8 10\r\n9 0\r\n"}, {"input": "100\r\n0 0\r\n0 0\r\n0 0\r\n97 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 29\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n12 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 4\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0 2\n1 3\n2 5\n97 98\n3 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 29\n29 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 30\n12 13\n28 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 77\n76 78\n77 79\n78 80\n79 81\n80 82\n81 83\n82 84\n83 85\n84 86\n85 87\n86 88\n87 89\n88 90\n89 91\n90 92\n91 93\n92 94\n93 95\n94 96\n95 97\n96 4\n4 99\n98 100\n99 0\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 80\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n21 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\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 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 80\n80 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 77\n76 78\n77 79\n78 81\n21 22\n79 82\n81 83\n82 84\n83 85\n84 86\n85 87\n86 88\n87 89\n88 90\n89 91\n90 92\n91 93\n92 94\n93 95\n94 96\n95 97\n96 98\n97 99\n98 100\n99 0\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 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 77\n76 78\n77 79\n78 80\n79 81\n80 82\n81 83\n82 84\n83 85\n84 86\n85 87\n86 88\n87 89\n88 90\n89 91\n90 92\n91 93\n92 94\n93 95\n94 96\n95 97\n96 98\n97 99\n98 100\n99 0\n"}]
| false
|
stdio
| null | true
|
629/B
|
629
|
B
|
Python 3
|
TESTS
| 2
| 46
| 4,915,200
|
24259389
|
a=int(input())
ma=[]
md=[]
fa=[]
fd=[]
for i in range(a):
y=input().split()
if y[0]=='M':
ma.append(int(y[1]))
md.append(int(y[2])+1)
else:
fa.append(int(y[1]))
fd.append(int(y[2])+1)
ma.sort()
md.sort()
fa.sort()
fd.sort()
m=0
f=0
mx=0
mpa=0
fpa=0
mda=0
fda=0
for i in range(1,141):
while mpa<len(ma) and ma[mpa]==i:
mpa+=1
m+=1
while mda<len(md) and md[mda]==i:
mda+=1
m-=1
while fpa<len(fa) and fa[fpa]==i:
fpa+=1
f+=1
while fda<len(fd) and fd[fda]==i:
fda+=1
f-=1
mx=max(mx, min(f,m)*2)
print(mx)
| 76
| 77
| 0
|
16253890
|
readInts=lambda: list(map(int, input().split()))
n = int(input())
num = [[0,0] for i in range(370)]
for i in range(n):
s,u,v = (input().split())
u=int(u)
v=int(v)
c=int(s=='F')
num[u][c]+=1
num[v+1][c]-=1
x=0;y=0
ret=0
for i in range(367):
x+=num[i][0]
y+=num[i][1]
ret=max(ret,min(x,y))
ret*=2
print(ret)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Far Relative’s Problem
|
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.
Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.
Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends.
Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
|
Print the maximum number of people that may come to Famil Door's party.
| null |
In the first sample, friends 3 and 4 can come on any day in range [117, 128].
In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
|
[{"input": "4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "output": "2"}, {"input": "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200", "output": "4"}]
| 1,100
|
["brute force"]
| 76
|
[{"input": "4\r\nM 151 307\r\nF 343 352\r\nF 117 145\r\nM 24 128\r\n", "output": "2\r\n"}, {"input": "6\r\nM 128 130\r\nF 128 131\r\nF 131 140\r\nF 131 141\r\nM 131 200\r\nM 140 200\r\n", "output": "4\r\n"}, {"input": "1\r\nF 68 307\r\n", "output": "0\r\n"}, {"input": "40\r\nM 55 363\r\nF 117 252\r\nM 157 282\r\nF 322 345\r\nM 330 363\r\nF 154 231\r\nF 216 352\r\nF 357 365\r\nM 279 292\r\nF 353 359\r\nF 82 183\r\nM 78 297\r\nM 231 314\r\nM 107 264\r\nF 34 318\r\nM 44 244\r\nF 42 339\r\nM 253 307\r\nM 128 192\r\nF 119 328\r\nM 135 249\r\nF 303 358\r\nF 348 352\r\nF 8 364\r\nF 126 303\r\nM 226 346\r\nF 110 300\r\nF 47 303\r\nF 201 311\r\nF 287 288\r\nM 270 352\r\nM 227 351\r\nF 8 111\r\nF 39 229\r\nM 163 315\r\nF 269 335\r\nF 147 351\r\nF 96 143\r\nM 97 99\r\nM 177 295\r\n", "output": "22\r\n"}, {"input": "2\r\nF 1 1\r\nM 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\nM 1 2\r\nM 2 3\r\nF 3 4\r\nF 4 5\r\n", "output": "2\r\n"}, {"input": "1\r\nF 1 2\r\n", "output": "0\r\n"}, {"input": "18\r\nF 3 4\r\nF 3 4\r\nF 3 4\r\nF 3 4\r\nF 5 6\r\nF 5 6\r\nM 3 4\r\nM 3 4\r\nM 5 6\r\nM 5 6\r\nM 5 6\r\nM 5 6\r\nF 7 8\r\nF 7 8\r\nF 7 8\r\nM 7 8\r\nM 7 8\r\nM 7 8\r\n", "output": "6\r\n"}, {"input": "2\r\nM 1 1\r\nF 1 1\r\n", "output": "2\r\n"}, {"input": "3\r\nM 1 1\r\nF 1 1\r\nM 1 1\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
629/B
|
629
|
B
|
Python 3
|
TESTS
| 2
| 46
| 307,200
|
228671645
|
import array
import collections
import math
sums = lambda n: int(n * (n + 1) / 2) # sum from 1 to n
sumsqur = lambda n: int( (n) * (n + 1) * (2*n +1)/6) # sum square from 1 to n
def im(): return map(int, input().split())
def il(): return list(map(int, input().split()))
def ii(): return int(input())
# "abcdefghijklmnopqrstuvwxyz"
def isPalindrom(a):
return True if a[::-1] == a else False
xx=lambda n:int(n * (n - 1) / 2) # 2->1,3->3,4->6,5->10,6->15
def solve():
n=ii()
m=[0]*367
f=[0]*367
for i in range(n):
g,a,b=input().split()
if g=='M':
for i in range(int(a),int(b)+1):
m[i]+=1
else:
for i in range(int(a),int(b)+1):
f[i]+=1
mx=0
for i in range(1,367):
if m[i]==f[i]:
mx=max(mx,m[i]*2)
return mx
if __name__ == '__main__':
#for i in range(ii()):
print(solve())
| 76
| 77
| 0
|
16297037
|
def main():
n = int(input())
mf = ([0] * 367, [0] * 367)
for _ in range(n):
tmp = input().split()
l = mf[tmp[0] == 'F']
l[int(tmp[1]) - 1] += 1
l[int(tmp[2])] -= 1
res = []
m = f = 0
for a, b in zip(*mf):
m += a
f += b
res.append(m if m < f else f)
print(max(res) * 2)
if __name__ == '__main__':
main()
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Far Relative’s Problem
|
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.
Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.
Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends.
Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
|
Print the maximum number of people that may come to Famil Door's party.
| null |
In the first sample, friends 3 and 4 can come on any day in range [117, 128].
In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
|
[{"input": "4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "output": "2"}, {"input": "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200", "output": "4"}]
| 1,100
|
["brute force"]
| 76
|
[{"input": "4\r\nM 151 307\r\nF 343 352\r\nF 117 145\r\nM 24 128\r\n", "output": "2\r\n"}, {"input": "6\r\nM 128 130\r\nF 128 131\r\nF 131 140\r\nF 131 141\r\nM 131 200\r\nM 140 200\r\n", "output": "4\r\n"}, {"input": "1\r\nF 68 307\r\n", "output": "0\r\n"}, {"input": "40\r\nM 55 363\r\nF 117 252\r\nM 157 282\r\nF 322 345\r\nM 330 363\r\nF 154 231\r\nF 216 352\r\nF 357 365\r\nM 279 292\r\nF 353 359\r\nF 82 183\r\nM 78 297\r\nM 231 314\r\nM 107 264\r\nF 34 318\r\nM 44 244\r\nF 42 339\r\nM 253 307\r\nM 128 192\r\nF 119 328\r\nM 135 249\r\nF 303 358\r\nF 348 352\r\nF 8 364\r\nF 126 303\r\nM 226 346\r\nF 110 300\r\nF 47 303\r\nF 201 311\r\nF 287 288\r\nM 270 352\r\nM 227 351\r\nF 8 111\r\nF 39 229\r\nM 163 315\r\nF 269 335\r\nF 147 351\r\nF 96 143\r\nM 97 99\r\nM 177 295\r\n", "output": "22\r\n"}, {"input": "2\r\nF 1 1\r\nM 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\nM 1 2\r\nM 2 3\r\nF 3 4\r\nF 4 5\r\n", "output": "2\r\n"}, {"input": "1\r\nF 1 2\r\n", "output": "0\r\n"}, {"input": "18\r\nF 3 4\r\nF 3 4\r\nF 3 4\r\nF 3 4\r\nF 5 6\r\nF 5 6\r\nM 3 4\r\nM 3 4\r\nM 5 6\r\nM 5 6\r\nM 5 6\r\nM 5 6\r\nF 7 8\r\nF 7 8\r\nF 7 8\r\nM 7 8\r\nM 7 8\r\nM 7 8\r\n", "output": "6\r\n"}, {"input": "2\r\nM 1 1\r\nF 1 1\r\n", "output": "2\r\n"}, {"input": "3\r\nM 1 1\r\nF 1 1\r\nM 1 1\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
962/E
|
962
|
E
|
PyPy 3
|
TESTS
| 4
| 93
| 20,172,800
|
129283674
|
n = int(input())
XC = []
for i in range(n):
x, c = map(str, input().split())
x = int(x)
XC.append((x, c))
XC.sort()
INF = 10**18
preB = -INF
preR = -INF
preP = -INF
ans = 0
for i, (x, c) in enumerate(XC):
if c == 'B':
if preB < preP:
if preP != -INF:
ans += x-preP
else:
if preB != -INF:
ans += x-preB
preB = x
elif c == 'R':
if preR < preP:
if preP != -INF:
ans += x-preP
else:
if preR != -INF:
ans += x-preR
preR = x
else:
if preR < preP and preB < preP:
if preP != -INF:
ans += x-preP
elif preP < preR and preP < preB:
if preR != -INF:
ans += x-preR
if preB != -INF:
ans += x-preB
elif preR < preP and preP < preB:
if preP != -INF:
ans += x-preP
if preB != -INF:
ans += x-preB
else:
if preP != -INF:
ans += x-preP
if preR != -INF:
ans += x-preR
preP = x
#print(i, ans, preR, preB, preP)
print(ans)
| 45
| 358
| 11,776,000
|
129791972
|
import sys
input = sys.stdin.readline
def solve():
n = int(input())
p = None
B = []
R = []
r = 0
for i in range(n):
x, c = input().split()
x = int(x)
if c == 'B':
B.append(x)
elif c == 'R':
R.append(x)
else:
if p is None:
if len(B) != 0:
r += x - B[0]
if len(R) != 0:
r += x - R[0]
else:
if len(B) != 0:
g = -1
for i in range(1,len(B)):
g = max(g, B[i] - B[i-1])
g = max(g, x - B[-1], B[0] - p)
else:
g = x - p
if len(R) != 0:
h = -1
for i in range(1,len(R)):
h = max(h, R[i] - R[i-1])
h = max(h, x - R[-1], R[0] - p)
else:
h = x - p
r += min(2 * (x-p), 3 * (x - p) - g - h)
p = x
B = []
R = []
if p is None:
if len(B) > 0:
r += B[-1] - B[0]
if len(R) > 0:
r += R[-1] - R[0]
else:
if len(B) != 0:
r += B[-1] - p
if len(R) != 0:
r += R[-1] - p
print(r)
solve()
|
Educational Codeforces Round 42 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Byteland, Berland and Disputed Cities
|
The cities of Byteland and Berland are located on the axis $$$Ox$$$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $$$Ox$$$ there are three types of cities:
- the cities of Byteland,
- the cities of Berland,
- disputed cities.
Recently, the project BNET has been launched — a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.
The countries agreed to connect the pairs of cities with BNET cables in such a way that:
- If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables,
- If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables.
Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.
The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.
Each city is a point on the line $$$Ox$$$. It is technically possible to connect the cities $$$a$$$ and $$$b$$$ with a cable so that the city $$$c$$$ ($$$a < c < b$$$) is not connected to this cable, where $$$a$$$, $$$b$$$ and $$$c$$$ are simultaneously coordinates of the cities $$$a$$$, $$$b$$$ and $$$c$$$.
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^{5}$$$) — the number of cities.
The following $$$n$$$ lines contains an integer $$$x_i$$$ and the letter $$$c_i$$$ ($$$-10^{9} \le x_i \le 10^{9}$$$) — the coordinate of the city and its type. If the city belongs to Byteland, $$$c_i$$$ equals to 'B'. If the city belongs to Berland, $$$c_i$$$ equals to «R». If the city is disputed, $$$c_i$$$ equals to 'P'.
All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
|
Print the minimal total length of such set of cables, that if we delete all Berland cities ($$$c_i$$$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($$$c_i$$$='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.
| null |
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $$$5 + 3 + 4 = 12$$$.
In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates $$$10, 21, 32$$$, so to connect them you need two cables of length $$$11$$$ and $$$11$$$. The cities of Byteland have coordinates $$$14$$$ and $$$16$$$, so to connect them you need one cable of length $$$2$$$. Thus, the total length of all cables is $$$11 + 11 + 2 = 24$$$.
|
[{"input": "4\n-5 R\n0 P\n3 P\n7 B", "output": "12"}, {"input": "5\n10 R\n14 B\n16 B\n21 R\n32 R", "output": "24"}]
| 2,200
|
["constructive algorithms", "greedy"]
| 45
|
[{"input": "4\r\n-5 R\r\n0 P\r\n3 P\r\n7 B\r\n", "output": "12\r\n"}, {"input": "5\r\n10 R\r\n14 B\r\n16 B\r\n21 R\r\n32 R\r\n", "output": "24\r\n"}, {"input": "10\r\n66 R\r\n67 R\r\n72 R\r\n73 R\r\n76 R\r\n78 B\r\n79 B\r\n83 B\r\n84 B\r\n85 P\r\n", "output": "26\r\n"}, {"input": "10\r\n61 R\r\n64 R\r\n68 R\r\n71 R\r\n72 R\r\n73 R\r\n74 P\r\n86 P\r\n87 B\r\n90 B\r\n", "output": "29\r\n"}, {"input": "15\r\n-9518 R\r\n-6858 P\r\n-6726 B\r\n-6486 R\r\n-4496 P\r\n-4191 P\r\n-772 B\r\n-258 R\r\n-194 P\r\n1035 R\r\n2297 P\r\n4816 B\r\n5779 R\r\n9342 B\r\n9713 B\r\n", "output": "25088\r\n"}, {"input": "6\r\n-8401 R\r\n-5558 P\r\n-3457 P\r\n-2361 R\r\n6966 P\r\n8140 B\r\n", "output": "17637\r\n"}, {"input": "2\r\n1 R\r\n2 R\r\n", "output": "1\r\n"}, {"input": "2\r\n-1000000000 B\r\n1000000000 R\r\n", "output": "0\r\n"}, {"input": "2\r\n-1000000000 P\r\n1000000000 P\r\n", "output": "2000000000\r\n"}, {"input": "2\r\n-1000000000 B\r\n1000000000 P\r\n", "output": "2000000000\r\n"}, {"input": "9\r\n-105 R\r\n-81 B\r\n-47 P\r\n-25 R\r\n-23 B\r\n55 P\r\n57 R\r\n67 B\r\n76 P\r\n", "output": "272\r\n"}, {"input": "6\r\n-13 R\r\n-10 P\r\n-6 R\r\n-1 P\r\n4 R\r\n10 P\r\n", "output": "32\r\n"}, {"input": "8\r\n-839 P\r\n-820 P\r\n-488 P\r\n-334 R\r\n-83 B\r\n187 R\r\n380 B\r\n804 P\r\n", "output": "2935\r\n"}, {"input": "8\r\n-12 P\r\n-9 B\r\n-2 R\r\n-1 R\r\n2 B\r\n8 B\r\n9 R\r\n15 P\r\n", "output": "54\r\n"}, {"input": "6\r\n0 B\r\n3 P\r\n7 B\r\n9 B\r\n11 P\r\n13 B\r\n", "output": "17\r\n"}]
| false
|
stdio
| null | true
|
774/B
|
774
|
B
|
Python 3
|
TESTS
| 3
| 46
| 4,915,200
|
26151061
|
n, m, d = list(map(int, input().split()))
a = []
b = []
for i in range(n):
a.append(list(map(int, input().split())))
for i in range(m):
b.append(list(map(int, input().split())))
a = sorted(a, key=lambda x: x[0])
b = sorted(b, key=lambda x: x[0])
tc, td = 0, 0
tc += a[-1][0]
tc += b[-1][0]
td += a[-1][1]
td += b[-1][1]
ai = n - 1
bi = m - 1
if td > d:
print(0)
exit()
while ai > 0:
t = ai - 1
if td + a[t][1] <= d:
td += a[t][1]
tc += a[t][0]
ai -= 1
continue
else:
break
cmax = tc
while bi > 0:
bi -= 1
tc += b[bi][0]
td += b[bi][1]
while td > d and ai < n:
tc -= a[ai][0]
td -= a[ai][1]
ai += 1
if ai == n:
break
if td <= d:
cmax = max(cmax, tc)
print(cmax)
| 44
| 904
| 35,123,200
|
217501504
|
n, m, d = map(int, input().split())
ph = [[int(j) for j in input().split()] for i in range(n)]
inf = [[int(j) for j in input().split()] for i in range(m)]
for i in range(n):
ph[i][1] = -ph[i][1]
for i in range(m):
inf[i][1] = -inf[i][1]
ph.sort(reverse=True)
inf.sort(reverse=True)
sw, sc = 0, 0
for p in inf:
sc += p[0]
d += p[1]
ans = 0
z = m - 1
for p in ph:
sc += p[0]
d += p[1]
#print(sc, d)
while z > 0 and d < 0:
sc -= inf[z][0]
d -= inf[z][1]
z -= 1
#print(sc, d)
if d >= 0:
ans = max(ans, sc)
print(ans)
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 3
| 256
|
Significant Cups
|
Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance ci and width wi.
Stepan decided to expose some of his cups on a shelf with width d in such a way, that:
- there is at least one Physics cup and at least one Informatics cup on the shelf,
- the total width of the exposed cups does not exceed d,
- from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too).
Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.
|
The first line contains three integers n, m and d (1 ≤ n, m ≤ 100 000, 1 ≤ d ≤ 109) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf.
Each of the following n lines contains two integers ci and wi (1 ≤ ci, wi ≤ 109) — significance and width of the i-th cup for Physics olympiads.
Each of the following m lines contains two integers cj and wj (1 ≤ cj, wj ≤ 109) — significance and width of the j-th cup for Informatics olympiads.
|
Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement.
If there is no way to expose cups on the shelf, then print 0.
| null |
In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.
|
[{"input": "3 1 8\n4 2\n5 5\n4 2\n3 2", "output": "8"}, {"input": "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4", "output": "11"}, {"input": "2 2 2\n5 3\n6 3\n4 2\n8 1", "output": "0"}]
| 2,100
|
["*special", "binary search", "data structures", "two pointers"]
| 44
|
[{"input": "3 1 8\r\n4 2\r\n5 5\r\n4 2\r\n3 2\r\n", "output": "8\r\n"}, {"input": "4 3 12\r\n3 4\r\n2 4\r\n3 5\r\n3 4\r\n3 5\r\n5 2\r\n3 4\r\n", "output": "11\r\n"}, {"input": "2 2 2\r\n5 3\r\n6 3\r\n4 2\r\n8 1\r\n", "output": "0\r\n"}, {"input": "10 10 229\r\n15 17\r\n5 4\r\n4 15\r\n4 17\r\n15 11\r\n7 6\r\n5 19\r\n14 8\r\n4 1\r\n10 12\r\n20 13\r\n20 14\r\n16 13\r\n7 15\r\n2 16\r\n11 11\r\n19 20\r\n6 7\r\n4 11\r\n14 16\r\n", "output": "198\r\n"}, {"input": "10 20 498\r\n40 12\r\n23 25\r\n20 9\r\n8 1\r\n23 8\r\n31 24\r\n33 2\r\n22 33\r\n4 13\r\n25 20\r\n40 5\r\n27 5\r\n17 6\r\n8 5\r\n4 19\r\n33 23\r\n30 19\r\n27 12\r\n13 22\r\n16 32\r\n28 36\r\n20 18\r\n36 38\r\n9 24\r\n21 35\r\n20 9\r\n33 29\r\n29 33\r\n18 25\r\n11 8\r\n", "output": "644\r\n"}, {"input": "20 10 761\r\n42 41\r\n47 7\r\n35 6\r\n22 40\r\n15 2\r\n47 28\r\n46 47\r\n3 45\r\n12 19\r\n44 41\r\n46 2\r\n49 23\r\n9 8\r\n7 41\r\n5 3\r\n16 42\r\n12 50\r\n17 22\r\n25 9\r\n45 12\r\n41 44\r\n34 47\r\n33 35\r\n32 47\r\n49 6\r\n27 18\r\n43 36\r\n23 6\r\n39 22\r\n38 45\r\n", "output": "900\r\n"}, {"input": "1 1 1000000000\r\n4 500000000\r\n6 500000000\r\n", "output": "10\r\n"}, {"input": "4 2 8\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n", "output": "4000000000\r\n"}, {"input": "1 1 1000000000\r\n1 1000000000\r\n1 1000000000\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
850/C
|
850
|
C
|
Python 3
|
TESTS
| 6
| 62
| 307,200
|
30084178
|
#!/usr/bin/env python
import sys
from operator import itemgetter
def sieve(limit):
is_prime = [True] * limit
is_prime[0] = False
indx = 0
while indx < limit:
if is_prime[indx]:
num = indx + 1
yield num
mul = 1
while mul * num <= limit:
is_prime[num * mul - 1] = False
mul += 1
indx += 1
def main():
n = int(input())
numbers = list(filter(lambda elem: elem != 1, map(int, input().split())))
if not numbers:
print("Arpa")
return
primes = list(sieve(max(numbers)+1))
num_primes_list = [[0] * len(primes) for __ in numbers]
for indx in range(len(numbers)):
for cnt_indx, prime in enumerate(primes):
num = numbers[indx]
cnt = 0
#print(prime,file=sys.stderr)
while num % prime == 0:
num //= prime
cnt += 1
num_primes_list[indx][cnt_indx] = cnt
cnts = [0] * len(primes)
for indx in range(len(primes)):
uniq = set()
for indx_num in range(len(numbers)):
if num_primes_list[indx_num][indx] > 0:
uniq.add(num_primes_list[indx_num][indx])
cnts[indx] = len(uniq) % 2
total = sum(cnts)
#sys.stderr.write(str(num_primes_list)+"\n")
#sys.stderr.write(str(cnts)+"\n")
if total % 2 == 0:
print("Arpa")
else:
print("Mojtaba")
main()
| 57
| 124
| 3,686,400
|
113129030
|
import sys
input = sys.stdin.buffer.readline
from collections import Counter
games = Counter() # prime : bitmask of if that power of the prime exists
def add_primes(a):
i = 2
while i*i <= a:
cnt = 0
while a % i == 0:
a //= i
cnt += 1
if cnt:
games[i] |= 1 << cnt
i += 1
if a != 1:
games[a] |= 1 << 1
def mex(a):
i = 0
while True:
if i not in a:
return i
i += 1
grundy_val = {}
def grundy_value(mask):
if mask in grundy_val:
return grundy_val[mask]
else:
transition_masks = set()
k = 1
while 1<<k <= mask:
new_mask = (mask&((1<<k)-1))|(mask>>k)
transition_masks.add(grundy_value(new_mask))
k += 1
grundy_val[mask] = mex(transition_masks)
return grundy_val[mask]
n = int(input())
a = list(map(int,input().split()))
for i in a:
add_primes(i)
xor_sum = 0
for game in games.values():
grundy_val = {}
xor_sum ^= grundy_value(game)
if xor_sum:
print("Mojtaba")
else:
print("Arpa")
|
Codeforces Round 432 (Div. 1, based on IndiaHacks Final Round 2017)
|
CF
| 2,017
| 1
| 256
|
Arpa and a game with Mojtaba
|
Mojtaba and Arpa are playing a game. They have a list of n numbers in the game.
In a player's turn, he chooses a number pk (where p is a prime number and k is a positive integer) such that pk divides at least one number in the list. For each number in the list divisible by pk, call it x, the player will delete x and add $$\frac{x}{p^k}$$ to the list. The player who can not make a valid choice of p and k loses.
Mojtaba starts the game and the players alternatively make moves. Determine which one of players will be the winner if both players play optimally.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the list.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the list.
|
If Mojtaba wins, print "Mojtaba", otherwise print "Arpa" (without quotes).
You can print each letter in any case (upper or lower).
| null |
In the first sample test, Mojtaba can't move.
In the second sample test, Mojtaba chooses p = 17 and k = 1, then the list changes to [1, 1, 1, 1].
In the third sample test, if Mojtaba chooses p = 17 and k = 1, then Arpa chooses p = 17 and k = 1 and wins, if Mojtaba chooses p = 17 and k = 2, then Arpa chooses p = 17 and k = 1 and wins.
|
[{"input": "4\n1 1 1 1", "output": "Arpa"}, {"input": "4\n1 1 17 17", "output": "Mojtaba"}, {"input": "4\n1 1 17 289", "output": "Arpa"}, {"input": "5\n1 2 3 4 5", "output": "Arpa"}]
| 2,200
|
["bitmasks", "dp", "games"]
| 57
|
[{"input": "4\r\n1 1 1 1\r\n", "output": "Arpa\r\n"}, {"input": "4\r\n1 1 17 17\r\n", "output": "Mojtaba\r\n"}, {"input": "4\r\n1 1 17 289\r\n", "output": "Arpa\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "Arpa\r\n"}, {"input": "10\r\n10 14 16 9 17 13 12 4 6 10\r\n", "output": "Mojtaba\r\n"}, {"input": "10\r\n13 13 18 3 8 9 19 12 20 14\r\n", "output": "Mojtaba\r\n"}, {"input": "10\r\n10 18 16 10 8 20 8 4 4 2\r\n", "output": "Mojtaba\r\n"}, {"input": "10\r\n1 4 15 1 16 14 7 17 11 8\r\n", "output": "Arpa\r\n"}, {"input": "10\r\n9 19 4 1 20 7 19 18 11 11\r\n", "output": "Mojtaba\r\n"}, {"input": "100\r\n80 35 113 179 195 92 143 152 125 55 68 121 71 147 172 153 87 68 143 133 32 153 177 173 183 100 59 55 63 189 63 44 78 15 143 105 62 98 22 8 197 119 77 108 85 79 56 160 149 157 39 129 70 79 118 15 110 17 157 81 184 1 160 126 35 108 15 28 63 128 24 132 179 160 104 164 49 76 30 148 144 38 112 10 65 109 68 142 35 174 89 118 24 46 171 35 53 169 154 18\r\n", "output": "Mojtaba\r\n"}, {"input": "100\r\n62 159 35 165 55 25 182 120 76 176 86 188 122 23 12 142 44 156 173 105 95 83 87 128 166 163 144 157 30 198 31 13 99 197 57 114 34 42 173 15 197 61 160 8 138 104 43 199 52 19 56 40 65 152 64 166 106 88 192 107 6 156 46 36 87 92 65 123 43 124 199 140 164 114 157 64 177 2 115 141 179 194 125 67 160 62 83 32 44 101 193 166 99 162 192 120 112 28 51 56\r\n", "output": "Mojtaba\r\n"}, {"input": "100\r\n10 30 91 164 105 103 4 116 77 36 118 158 136 161 28 35 119 148 16 47 116 18 13 124 103 96 132 119 160 147 128 98 143 96 130 129 133 45 37 133 192 22 35 4 75 89 110 54 147 2 64 66 123 136 12 183 161 118 50 131 39 147 143 16 43 146 98 42 191 155 96 18 169 176 170 102 172 9 130 62 22 32 121 153 24 150 100 102 1 52 2 76 147 139 72 10 21 37 157 23\r\n", "output": "Mojtaba\r\n"}, {"input": "100\r\n199 89 78 3 1 171 187 132 20 81 88 51 7 175 181 92 75 196 71 17 200 27 117 112 182 51 43 64 189 136 130 24 125 87 38 185 198 6 175 63 178 65 33 91 22 6 180 100 21 11 164 1 101 26 1 97 71 76 65 163 3 27 81 110 114 38 160 42 90 65 189 181 198 66 3 152 83 125 84 72 181 193 75 197 184 161 192 181 38 172 88 106 112 6 67 120 85 181 148 88\r\n", "output": "Mojtaba\r\n"}, {"input": "100\r\n44 188 149 152 200 131 152 1 15 101 15 64 51 48 5 189 65 105 90 143 60 54 82 149 66 89 191 182 46 92 51 19 77 88 175 192 89 4 135 126 89 68 116 92 159 71 160 97 200 89 109 100 120 95 183 85 187 125 93 65 101 51 172 65 195 4 108 123 152 30 9 88 33 124 91 28 49 79 78 2 30 169 115 198 130 16 165 120 163 121 45 31 107 83 47 164 200 112 83 59\r\n", "output": "Mojtaba\r\n"}, {"input": "21\r\n128 32 131072 16 64 536870912 4 524288 33554432 16384 8 256 1048576 2048 2 32768 268435456 1 16777216 8388608 134217728\r\n", "output": "Mojtaba\r\n"}, {"input": "13\r\n134217728 32768 536870912 524288 16777216 16384 1048576 33554432 8388608 268435456 512 131072 2048\r\n", "output": "Mojtaba\r\n"}, {"input": "29\r\n2 2097152 67108864 262144 1 16384 4096 4 65536 256 1024 8388608 16 4194304 134217728 64 512 33554432 8 128 268435456 524288 32 2048 32768 8192 131072 16777216 1048576\r\n", "output": "Mojtaba\r\n"}, {"input": "6\r\n9 5 1 1 8 1\r\n", "output": "Arpa\r\n"}, {"input": "26\r\n1 48828125 81 59049 256 16 9 3 6561 2048 512 128 1024 2 25 3125 390625 177147 1 19683 64 32 1 4 15625 9765625\r\n", "output": "Arpa\r\n"}, {"input": "15\r\n2048 5 1 19683 9765625 3125 177147 125 2187 48828125 6561 512 1 1 390625\r\n", "output": "Arpa\r\n"}, {"input": "1\r\n984711052\r\n", "output": "Mojtaba\r\n"}, {"input": "1\r\n270983380\r\n", "output": "Mojtaba\r\n"}, {"input": "1\r\n887864471\r\n", "output": "Mojtaba\r\n"}, {"input": "1\r\n234923095\r\n", "output": "Arpa\r\n"}, {"input": "1\r\n877739788\r\n", "output": "Mojtaba\r\n"}, {"input": "1\r\n534306180\r\n", "output": "Mojtaba\r\n"}, {"input": "1\r\n365210472\r\n", "output": "Mojtaba\r\n"}, {"input": "1\r\n616751420\r\n", "output": "Mojtaba\r\n"}, {"input": "19\r\n1 2048 1048576 524288 16 128 32 2 16384 131072 32768 4 33554432 134217728 268435456 8 8388608 536870912 16777216\r\n", "output": "Arpa\r\n"}, {"input": "59\r\n1953125 14348907 823543 11 64 1048576 9765625 16777216 19487171 2187 5 117649 40353607 48828125 531441 5764801 729 49 32768 371293 43046721 1771561 1 25 13 4826809 1 6561 2197 536870912 62748517 524288 4782969 128 59049 177147 16384 1 27 19683 9 1594323 1331 33554432 129140163 1 2048 268435456 1 8388608 625 131072 343 3 2401 1 390625 134217728 4\r\n", "output": "Arpa\r\n"}, {"input": "53\r\n131072 64 1594323 49 25 129140163 4 729 1771561 1 1331 33554432 531441 128 4782969 16777216 2187 32768 19487171 48828125 134217728 59049 16384 6561 1048576 1 3 9 177147 9765625 1 390625 11 27 1953125 5 19683 2401 2048 117649 524288 343 40353607 1 43046721 8388608 5764801 14348907 625 823543 268435456 1 536870912\r\n", "output": "Arpa\r\n"}, {"input": "55\r\n1 161051 121 14641 4782969 177147 5 1771561 1594323 1953125 524288 5764801 9765625 49 1 390625 823543 128 2187 268435456 8388608 117649 1048576 59049 43046721 2048 16777216 2401 536870912 4 19487171 9 40353607 14348907 33554432 131072 3 16384 27 134217728 1 64 32768 1 531441 48828125 19683 625 343 1331 25 129140163 729 6561 1\r\n", "output": "Mojtaba\r\n"}, {"input": "34\r\n1 64 390625 33554432 9 1 524288 387420489 536870912 8388608 2048 244140625 129140163 59049 4 16384 9765625 43046721 131072 2187 48828125 1 25 16777216 1048576 268435456 19683 32768 4782969 81 5 128 3 134217728\r\n", "output": "Arpa\r\n"}, {"input": "44\r\n390625 16807 7 1 131072 536870912 43046721 125 2187 134217728 32768 19487171 16384 2401 5 16777216 524288 343 1048576 9765625 244140625 33554432 81 4782969 59049 1331 129140163 387420489 282475249 1 48828125 2048 1 1 25 11 40353607 3 268435456 19683 214358881 9 1 8388608\r\n", "output": "Mojtaba\r\n"}, {"input": "51\r\n33554432 268435456 25 9765625 1 536870912 5 8 1 27 32768 121 19683 40353607 1 128 4782969 1 4 3 8388608 161051 2187 282475249 1048576 2 3125 16807 387420489 1771561 11 625 16 43046721 214358881 16384 16777216 59049 32 343 134217728 390625 2048 1 9 524288 244140625 131072 7 48828125 129140163\r\n", "output": "Arpa\r\n"}, {"input": "55\r\n5 1 161051 343 134217728 4782969 131072 815730721 1 16384 40353607 2187 3 16807 16 2197 8388608 59049 282475249 244140625 1 7 387420489 390625 9 268435456 1 214358881 1771561 121 524288 27 11 48828125 33554432 32768 169 25 625 8 16777216 9765625 128 129140163 43046721 2048 536870912 2 3125 19683 1048576 4 1 1 32\r\n", "output": "Arpa\r\n"}, {"input": "52\r\n169 8388608 48828125 32768 387420489 1 214358881 815730721 8 4782969 16384 27 390625 1 268435456 2187 40353607 59049 15625 282475249 536870912 125 2197 1 121 134217728 19683 1 32 1 625 3 244140625 2 524288 131072 25 9765625 43046721 7 1771561 16777216 1048576 9 16807 343 161051 11 2048 33554432 1 129140163\r\n", "output": "Arpa\r\n"}, {"input": "63\r\n16807 1 1419857 59049 1 2187 6859 1 33554432 129140163 11 1 16777216 3 9765625 1331 2197 268435456 2 169 390625 343 1048576 536870912 19683 125 16384 27 40353607 815730721 32 130321 43046721 524288 17 8388608 7 4782969 15625 282475249 134217728 1 25 4913 9 19 131072 244140625 625 2476099 1 2048 214358881 32768 1 24137569 48828125 387420489 8 19487171 361 1 47045881\r\n", "output": "Arpa\r\n"}, {"input": "38\r\n524288 27 131072 256 64 15625 729 2048 1048576 387420489 4782969 1 33554432 625 16777216 32768 4 243 9 1 9765625 390625 1 19683 8388608 16384 59049 8 48828125 536870912 244140625 134217728 2 5 129140163 25 43046721 268435456\r\n", "output": "Arpa\r\n"}, {"input": "56\r\n49 24137569 1048576 16384 4782969 2 4913 1 59049 16777216 625 121 1 19683 19487171 524288 43046721 1419857 125 13 282475249 15625 1 1 1 131072 2476099 9765625 2197 390625 19 6859 268435456 536870912 243 32768 40353607 8 2401 1024 1 17 2187 129140163 47045881 25 387420489 1 244140625 33554432 214358881 815730721 8388608 48828125 134217728 1\r\n", "output": "Arpa\r\n"}]
| false
|
stdio
| null | true
|
847/K
|
847
|
K
|
Python 3
|
TESTS
| 0
| 15
| 0
|
173321507
|
n,a,b,f,k =map(int, input().split())
ans = 0
d = {}
next = ''
for i in range(0,n):
x=input()
y,z = x.split()
if x in d:
if y == next:
d[x]+=b
else:
d[x]+=a
else:
d[x]=0
if y == next:
d[x]+=b
else:
d[x]+=a
next = z
sorted_values = sorted(d.values()) # Sort the values
sorted_dict = {}
for i in sorted_values:
for j in d.keys():
if d[j] == i:
sorted_dict[j] = d[j]
#print(sorted_dict)
for i in sorted_dict.values():
#print(i)
if f == 0:
break
if i > int(k):
f = f - 1
i = k
for i in sorted_dict.values():
ans+=int(i)
print(ans)
| 55
| 62
| 4,608,000
|
30477812
|
def main():
trips, reg, cheap, cards, card_cost = map(int, input().split())
costs = []
indexes = {}
total = 0
last = ""
for i in range(trips):
a, b = input().split()
pair = (min(a, b), max(a, b))
if pair in indexes:
index = indexes[pair]
else:
costs.append(0)
indexes[pair] = len(costs) - 1
index = len(costs) - 1
total += (cheap if a == last else reg)
costs[index] += (cheap if a == last else reg)
last = b
costs = sorted(costs, reverse = True)
for c in costs:
if c < card_cost or cards <= 0:
break
total -= c
total += card_cost
cards -= 1
print(total)
main()
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 4
| 256
|
Travel Cards
|
In the evening Polycarp decided to analyze his today's travel expenses on public transport.
The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops.
Polycarp made n trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes.
It is known that one trip on any bus costs a burles. In case when passenger makes a transshipment the cost of trip decreases to b burles (b < a). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment.
For example, if Polycarp made three consecutive trips: "BerBank" $$\rightarrow$$ "University", "University" $$\rightarrow$$ "BerMall", "University" $$\rightarrow$$ "BerBank", then he payed a + b + a = 2a + b burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus.
Also Polycarp can buy no more than k travel cards. Each travel card costs f burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction.
What is the smallest amount of money Polycarp could have spent today if he can buy no more than k travel cards?
|
The first line contains five integers n, a, b, k, f (1 ≤ n ≤ 300, 1 ≤ b < a ≤ 100, 0 ≤ k ≤ 300, 1 ≤ f ≤ 1000) where:
- n — the number of Polycarp trips,
- a — the cost of a regualar single trip,
- b — the cost of a trip after a transshipment,
- k — the maximum number of travel cards Polycarp can buy,
- f — the cost of a single travel card.
The following n lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space — the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different.
|
Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than k travel cards.
| null |
In the first example Polycarp can buy travel card for the route "BerBank $$\leftarrow$$ University" and spend 8 burles. Note that his second trip "University" $$\rightarrow$$ "BerMall" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles.
In the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2 + 1 + 1 + 1 = 5 burles.
|
[{"input": "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank", "output": "11"}, {"input": "4 2 1 300 1000\na A\nA aa\naa AA\nAA a", "output": "5"}]
| 1,800
|
["greedy", "implementation", "sortings"]
| 55
|
[{"input": "3 5 3 1 8\r\nBerBank University\r\nUniversity BerMall\r\nUniversity BerBank\r\n", "output": "11\r\n"}, {"input": "4 2 1 300 1000\r\na A\r\nA aa\r\naa AA\r\nAA a\r\n", "output": "5\r\n"}, {"input": "2 2 1 0 1\r\naca BCBA\r\nBCBA aca\r\n", "output": "3\r\n"}, {"input": "2 2 1 2 1\r\nBDDB C\r\nC BDDB\r\n", "output": "1\r\n"}, {"input": "2 3 1 1 9\r\nbacAB aAdb\r\nbacAB aAdb\r\n", "output": "6\r\n"}, {"input": "2 3 1 4 6\r\nAaCdC CdD\r\naBACc CdD\r\n", "output": "6\r\n"}, {"input": "1 2 1 2 1\r\nC BA\r\n", "output": "1\r\n"}, {"input": "1 3 1 1 4\r\nbCCCC BC\r\n", "output": "3\r\n"}, {"input": "1 4 3 1 1\r\nC bC\r\n", "output": "1\r\n"}, {"input": "1 3 2 1 1\r\nBBC B\r\n", "output": "1\r\n"}, {"input": "3 2 1 5 1\r\nBCA cBBBd\r\ncBBBd CdC\r\nCdC bbdAb\r\n", "output": "3\r\n"}, {"input": "5 3 2 1 1\r\nC CB\r\nCB C\r\nC d\r\nCB d\r\nCB C\r\n", "output": "6\r\n"}, {"input": "3 3 1 0 1\r\ncbcC A\r\nA aA\r\nA cbcC\r\n", "output": "7\r\n"}, {"input": "3 3 1 4 3\r\nc CADC\r\nCADC ABaD\r\nABaD c\r\n", "output": "5\r\n"}, {"input": "8 2 1 11 1\r\ndacdD cdDAA\r\ncdDAA dacdD\r\ndacdD bcCA\r\nbcCA B\r\nDDAA B\r\nDDAA daC\r\nAbCAc B\r\ndacdD daC\r\n", "output": "7\r\n"}, {"input": "12 4 1 2 8\r\nDA b\r\nDA dC\r\ndC b\r\nb DA\r\nb dC\r\nDA b\r\ndC b\r\nb dC\r\ndC DA\r\nDA dC\r\nDA b\r\nb dC\r\n", "output": "22\r\n"}, {"input": "27 8 1 0 1\r\nBcd A\r\nA b\r\nb BcDc\r\ndc dbaC\r\ndbaC dcCB\r\nB d\r\nd BbAc\r\nCBC b\r\nDBDca d\r\ncAbb AA\r\nAA Ba\r\ncAccb DBDca\r\ncb DdaB\r\nAAcBc Ba\r\nBa dc\r\ndc DDCd\r\nbcBDA da\r\nbDD ADD\r\nAA b\r\nb cb\r\ncb CCBbd\r\nCCBbd bcDdb\r\nbcDdb ddc\r\nddc C\r\nC Adc\r\nAdc BbAc\r\nBbAc DD\r\n", "output": "111\r\n"}, {"input": "22 85 1 36 1\r\ncdAd cBbCa\r\ncBbCa abBBc\r\nabBBc dddb\r\ndddb BBDA\r\nBBDA abBBc\r\nabBBc ADCad\r\naDaC cdAd\r\ncdAd D\r\nD acCbD\r\nAd DB\r\nDB C\r\nACb ca\r\nca ACb\r\nACb D\r\nD BBDA\r\nBBDA d\r\nd C\r\nC A\r\nA B\r\nB Ad\r\nAd cDD\r\ncDD ACb\r\n", "output": "21\r\n"}, {"input": "8 8 2 4 5\r\naBBba C\r\nCc CcBd\r\nd C\r\ndD dDba\r\ndDba c\r\nCc d\r\nd dD\r\ndD dDba\r\n", "output": "32\r\n"}, {"input": "4 10 6 2 7\r\nbbCc c\r\nd Bdccd\r\nBdccd c\r\nc Bdccd\r\n", "output": "24\r\n"}, {"input": "10 3 2 6 2\r\nbCbA a\r\naDA Bba\r\nbCbA aDA\r\nBba caa\r\nbCbA Bba\r\ncaa aDA\r\naDA bCbA\r\nbCbA dBba\r\ndBba bCbA\r\ndBba a\r\n", "output": "18\r\n"}, {"input": "7 5 4 3 2\r\nDBBCa BdC\r\nCDBcb BdC\r\ncA BdC\r\nBD CDBcb\r\nBD BdC\r\nDDd cacD\r\nBdC cA\r\n", "output": "21\r\n"}, {"input": "9 10 5 4 3\r\nDcAaa caCBc\r\ncaCBc B\r\nB b\r\nb cdddb\r\ncdddb aDBAb\r\naDBAb B\r\nB aDBAb\r\naDBAb cdddb\r\ncdddb aDCda\r\n", "output": "27\r\n"}, {"input": "10 20 10 0 11\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\n", "output": "110\r\n"}, {"input": "10 20 10 1 11\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\n", "output": "11\r\n"}]
| false
|
stdio
| null | true
|
629/B
|
629
|
B
|
Python 3
|
PRETESTS
| 2
| 61
| 0
|
16237793
|
n = int(input())
day = [[0, 0] for i in range(368)]
for i in range(n):
a, b, c = (input().split())
if(a == "M"):
day[int(b)][0] += 1
day[int(c)+1][0] -= 1
else:
day[int(b)][1] += 1
day[int(c)+1][1] -= 1
w = 0
m = 0
ans = 0
for i in day:
w += i[0]
m += i[1]
if(w == m):
ans = max(m+w, ans)
print(ans)
| 76
| 77
| 0
|
16339988
|
n = int(input())
f, m = [0]*368, [0]*368
for i in range(n):
s, a, b = input().split()
if s == 'F':
f[int(a)] += 1
f[int(b)+1] -= 1
elif s == 'M':
m[int(a)] += 1
m[int(b)+1] -= 1
for i in range(1, len(f)):
f[i] += f[i-1]
m[i] += m[i-1]
print(max([min(f[i], m[i])*2 for i in range(1, 367)]))
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Far Relative’s Problem
|
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.
Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.
Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends.
Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
|
Print the maximum number of people that may come to Famil Door's party.
| null |
In the first sample, friends 3 and 4 can come on any day in range [117, 128].
In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
|
[{"input": "4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "output": "2"}, {"input": "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200", "output": "4"}]
| 1,100
|
["brute force"]
| 76
|
[{"input": "4\r\nM 151 307\r\nF 343 352\r\nF 117 145\r\nM 24 128\r\n", "output": "2\r\n"}, {"input": "6\r\nM 128 130\r\nF 128 131\r\nF 131 140\r\nF 131 141\r\nM 131 200\r\nM 140 200\r\n", "output": "4\r\n"}, {"input": "1\r\nF 68 307\r\n", "output": "0\r\n"}, {"input": "40\r\nM 55 363\r\nF 117 252\r\nM 157 282\r\nF 322 345\r\nM 330 363\r\nF 154 231\r\nF 216 352\r\nF 357 365\r\nM 279 292\r\nF 353 359\r\nF 82 183\r\nM 78 297\r\nM 231 314\r\nM 107 264\r\nF 34 318\r\nM 44 244\r\nF 42 339\r\nM 253 307\r\nM 128 192\r\nF 119 328\r\nM 135 249\r\nF 303 358\r\nF 348 352\r\nF 8 364\r\nF 126 303\r\nM 226 346\r\nF 110 300\r\nF 47 303\r\nF 201 311\r\nF 287 288\r\nM 270 352\r\nM 227 351\r\nF 8 111\r\nF 39 229\r\nM 163 315\r\nF 269 335\r\nF 147 351\r\nF 96 143\r\nM 97 99\r\nM 177 295\r\n", "output": "22\r\n"}, {"input": "2\r\nF 1 1\r\nM 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\nM 1 2\r\nM 2 3\r\nF 3 4\r\nF 4 5\r\n", "output": "2\r\n"}, {"input": "1\r\nF 1 2\r\n", "output": "0\r\n"}, {"input": "18\r\nF 3 4\r\nF 3 4\r\nF 3 4\r\nF 3 4\r\nF 5 6\r\nF 5 6\r\nM 3 4\r\nM 3 4\r\nM 5 6\r\nM 5 6\r\nM 5 6\r\nM 5 6\r\nF 7 8\r\nF 7 8\r\nF 7 8\r\nM 7 8\r\nM 7 8\r\nM 7 8\r\n", "output": "6\r\n"}, {"input": "2\r\nM 1 1\r\nF 1 1\r\n", "output": "2\r\n"}, {"input": "3\r\nM 1 1\r\nF 1 1\r\nM 1 1\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
963/B
|
963
|
B
|
PyPy 3-64
|
TESTS
| 2
| 46
| 614,400
|
155876435
|
import sys
from collections import deque
from array import array
class graph:
def __init__(self, n):
self.n, self.gdict = n, [[] for _ in range(n + 1)]
self.l = [0] * (n + 1)
def add_edge(self, node1, node2, w=None):
self.gdict[node1].append(node2)
self.gdict[node2].append(node1)
self.l[node1] += 1
self.l[node2] += 1
def subtree(self, v):
queue, visit, child = deque([v]), [False] * (self.n + 1), []
visit[v], self.par, out = True, [-1] * (self.n + 1), []
while queue:
s = queue.popleft()
for i1 in self.gdict[s]:
if not visit[i1]:
queue.append(i1)
visit[i1] = True
child.append(i1)
self.par[i1] = s
for node in child[::-1]:
if self.l[node] & 1 == 0:
out.append(node)
visit[node] = False
for neigh in self.gdict[node]:
self.l[neigh] -= 1
for i in range(1, self.n + 1):
if visit[i]:
out.append(i)
out = '\n'.join(map(str, out))
print('NO' if self.l[v] & 1 else f"YES\n{out}")
input = lambda: sys.stdin.buffer.readline().decode().strip()
n = int(input())
g = graph(n)
for i, j in enumerate([int(x) for x in input().split()], 1):
if j:
g.add_edge(i, j)
g.subtree(g.l.index(max(g.l)))
| 95
| 826
| 48,742,400
|
101823725
|
from collections import defaultdict,deque
import sys
import bisect
import math
input=sys.stdin.readline
mod=1000000007
def bfs(root,count):
q=deque([root])
vis.add(root)
while q:
vertex=q.popleft()
for child in graph[vertex]:
if ans[child]==0:
ans[child]=count+1
count+=1
if child not in vis:
q.append(child)
vis.add(child)
graph=defaultdict(list)
n=int(input())
p=[int(i) for i in input().split() if i!='\n']
if n&1:
for i in range(n):
if p[i]!=0:
graph[p[i]].append(i+1)
graph[i+1].append(p[i])
length=[0]*(n+1)
for i in graph:
length[i]=len(graph[i])
CHECK,OBSERVE=1,0
stack=[(OBSERVE,1,0)]
ans=[0]*(n+1)
count=0
while stack:
state,vertex,parent=stack.pop()
if state==OBSERVE:
stack.append((CHECK,vertex,parent))
for child in graph[vertex]:
if child != parent:
stack.append((OBSERVE,child,vertex))
else:
if length[vertex]%2==0:
count+=1
ans[vertex]=count
length[parent]-=1
vis=set()
bfs(1,count)
out=[0]*(n)
for i in range(1,n+1):
out[ans[i]-1]=i
print('YES')
for i in out:
sys.stdout.write(str(i)+'\n')
else:
print('NO')
|
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
|
CF
| 2,018
| 1
| 256
|
Destruction of a Tree
|
You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).
A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.
Destroy all vertices in the given tree or determine that it is impossible.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — number of vertices in a tree.
The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n). If pi ≠ 0 there is an edge between vertices i and pi. It is guaranteed that the given graph is a tree.
|
If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes).
If it's possible to destroy all vertices, in the next n lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.
| null |
In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.
|
[{"input": "5\n0 1 2 1 2", "output": "YES\n1\n2\n3\n5\n4"}, {"input": "4\n0 1 2 3", "output": "NO"}]
| 2,000
|
["constructive algorithms", "dfs and similar", "dp", "greedy", "trees"]
| 95
|
[{"input": "5\r\n0 1 2 1 2\r\n", "output": "YES\r\n1\r\n2\r\n3\r\n5\r\n4\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n1\r\n"}, {"input": "8\r\n3 1 4 0 4 2 4 5\r\n", "output": "NO\r\n"}, {"input": "100\r\n81 96 65 28 4 40 5 49 5 89 48 70 94 70 17 58 58 1 61 19 45 33 46 19 22 83 56 67 62 82 57 16 29 36 84 71 42 66 78 54 73 45 82 80 67 88 79 69 61 66 5 36 24 60 96 21 77 67 68 29 87 37 91 34 78 43 0 69 49 62 16 2 68 79 57 1 60 12 39 99 14 37 30 92 47 18 14 75 73 39 94 12 43 87 90 22 91 59 54 71\r\n", "output": "NO\r\n"}, {"input": "100\r\n57 85 27 81 41 27 73 10 73 95 91 90 89 41 86 44 6 20 9 13 46 73 56 19 37 32 40 42 79 76 96 5 6 8 76 52 14 86 33 69 100 95 58 87 43 47 17 39 48 28 77 65 100 100 41 39 87 5 61 67 94 64 61 88 32 23 79 44 0 67 44 23 48 96 48 56 86 75 90 2 17 46 4 75 42 90 17 77 5 33 87 91 27 28 58 95 58 47 33 6\r\n", "output": "NO\r\n"}, {"input": "21\r\n11 10 12 3 6 0 8 6 16 14 5 9 7 19 1 13 15 21 4 2 20\r\n", "output": "YES\r\n21\r\n18\r\n2\r\n20\r\n14\r\n10\r\n4\r\n19\r\n12\r\n3\r\n16\r\n9\r\n7\r\n13\r\n6\r\n8\r\n11\r\n5\r\n15\r\n17\r\n1\r\n"}, {"input": "61\r\n10 42 20 50 4 24 18 55 19 5 57 13 3 35 58 48 31 46 40 45 15 53 14 25 43 41 22 23 54 39 38 44 16 37 12 34 32 28 26 30 59 47 21 9 8 52 1 0 33 49 36 51 17 11 29 7 48 61 6 27 2\r\n", "output": "YES\r\n27\r\n60\r\n53\r\n22\r\n31\r\n17\r\n28\r\n38\r\n14\r\n23\r\n12\r\n35\r\n3\r\n13\r\n45\r\n20\r\n55\r\n8\r\n54\r\n29\r\n57\r\n11\r\n16\r\n48\r\n49\r\n33\r\n4\r\n50\r\n10\r\n5\r\n7\r\n56\r\n46\r\n18\r\n51\r\n52\r\n34\r\n36\r\n32\r\n37\r\n9\r\n44\r\n40\r\n19\r\n39\r\n30\r\n41\r\n26\r\n6\r\n59\r\n25\r\n24\r\n21\r\n43\r\n58\r\n15\r\n2\r\n61\r\n47\r\n42\r\n1\r\n"}, {"input": "21\r\n11 19 4 19 6 0 13 7 6 2 5 3 16 10 1 9 15 21 9 21 2\r\n", "output": "YES\r\n7\r\n8\r\n16\r\n13\r\n10\r\n14\r\n2\r\n21\r\n18\r\n20\r\n3\r\n12\r\n19\r\n4\r\n6\r\n9\r\n11\r\n5\r\n15\r\n17\r\n1\r\n"}, {"input": "61\r\n47 61 20 5 10 59 46 55 44 1 57 13 3 35 21 48 31 7 9 45 43 53 14 6 42 39 22 23 54 40 45 37 16 36 12 44 34 28 25 19 26 33 25 39 33 36 42 0 50 4 52 46 17 11 29 7 48 15 41 27 58\r\n", "output": "YES\r\n6\r\n24\r\n41\r\n59\r\n40\r\n30\r\n9\r\n19\r\n37\r\n32\r\n52\r\n51\r\n46\r\n7\r\n18\r\n56\r\n36\r\n34\r\n61\r\n2\r\n15\r\n58\r\n43\r\n21\r\n25\r\n39\r\n26\r\n44\r\n55\r\n8\r\n54\r\n29\r\n57\r\n11\r\n16\r\n48\r\n28\r\n38\r\n14\r\n23\r\n12\r\n35\r\n3\r\n13\r\n27\r\n60\r\n53\r\n22\r\n31\r\n17\r\n45\r\n20\r\n42\r\n33\r\n50\r\n49\r\n5\r\n4\r\n1\r\n47\r\n10\r\n"}, {"input": "79\r\n0 56 56 42 56 56 56 56 4 56 56 22 56 56 56 48 56 56 56 56 56 24 56 16 56 56 56 9 56 56 56 56 56 56 56 56 56 55 56 56 12 20 56 28 56 56 56 38 56 56 56 56 56 56 44 1 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56\r\n", "output": "YES\r\n12\r\n41\r\n24\r\n22\r\n48\r\n16\r\n55\r\n38\r\n28\r\n44\r\n4\r\n9\r\n20\r\n42\r\n56\r\n2\r\n3\r\n5\r\n6\r\n7\r\n8\r\n10\r\n11\r\n13\r\n14\r\n15\r\n17\r\n18\r\n19\r\n21\r\n23\r\n25\r\n26\r\n27\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n39\r\n40\r\n43\r\n45\r\n46\r\n47\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67\r\n68\r\n69\r\n70\r\n71\r\n72\r\n73\r\n74\r\n75\r\n76\r\n77\r\n78\r\n79\r\n1\r\n"}, {"input": "121\r\n110 31 57 33 45 33 33 33 91 102 79 33 61 72 107 101 117 10 118 33 33 64 24 94 117 76 33 23 33 49 5 52 95 78 33 39 33 92 17 33 25 33 56 33 3 88 33 108 62 15 28 111 67 33 33 11 96 33 36 70 46 98 80 104 33 19 60 33 112 51 33 2 33 33 121 59 33 41 50 81 105 33 115 34 33 18 84 32 33 33 87 13 86 103 16 119 33 63 30 43 83 53 26 100 69 33 14 38 33 75 66 120 33 33 9 99 0 93 1 48 116\r\n", "output": "YES\r\n33\r\n4\r\n6\r\n7\r\n8\r\n12\r\n20\r\n21\r\n27\r\n29\r\n35\r\n37\r\n40\r\n42\r\n44\r\n47\r\n54\r\n55\r\n58\r\n65\r\n68\r\n71\r\n73\r\n74\r\n77\r\n82\r\n85\r\n89\r\n90\r\n97\r\n106\r\n109\r\n113\r\n114\r\n16\r\n95\r\n83\r\n101\r\n9\r\n115\r\n87\r\n91\r\n34\r\n84\r\n41\r\n78\r\n117\r\n25\r\n39\r\n17\r\n59\r\n36\r\n26\r\n76\r\n94\r\n103\r\n23\r\n24\r\n51\r\n28\r\n60\r\n70\r\n53\r\n67\r\n10\r\n102\r\n86\r\n18\r\n118\r\n93\r\n66\r\n19\r\n52\r\n111\r\n88\r\n32\r\n61\r\n46\r\n92\r\n13\r\n108\r\n38\r\n120\r\n48\r\n69\r\n112\r\n81\r\n105\r\n63\r\n80\r\n62\r\n98\r\n30\r\n49\r\n116\r\n99\r\n75\r\n121\r\n64\r\n22\r\n100\r\n104\r\n56\r\n43\r\n79\r\n11\r\n15\r\n50\r\n14\r\n107\r\n2\r\n72\r\n5\r\n31\r\n3\r\n45\r\n96\r\n57\r\n1\r\n110\r\n119\r\n"}, {"input": "21\r\n5 20 9 19 8 0 13 6 13 19 5 3 8 10 1 9 1 20 3 10 18\r\n", "output": "YES\r\n18\r\n21\r\n20\r\n2\r\n10\r\n14\r\n19\r\n4\r\n3\r\n12\r\n9\r\n16\r\n13\r\n7\r\n8\r\n6\r\n5\r\n11\r\n1\r\n15\r\n17\r\n"}, {"input": "61\r\n5 61 20 5 50 59 56 29 44 1 48 13 20 35 61 33 38 52 30 8 43 17 35 43 24 59 22 23 11 26 38 37 48 36 13 37 44 23 30 19 26 1 15 19 8 18 42 0 50 33 52 36 17 11 29 18 48 15 24 22 42\r\n", "output": "YES\r\n56\r\n7\r\n18\r\n46\r\n52\r\n51\r\n36\r\n34\r\n37\r\n32\r\n44\r\n9\r\n19\r\n40\r\n30\r\n39\r\n26\r\n41\r\n59\r\n6\r\n24\r\n25\r\n43\r\n21\r\n15\r\n58\r\n61\r\n2\r\n42\r\n47\r\n1\r\n5\r\n4\r\n50\r\n49\r\n33\r\n16\r\n48\r\n11\r\n29\r\n8\r\n20\r\n3\r\n13\r\n12\r\n35\r\n14\r\n23\r\n28\r\n38\r\n17\r\n22\r\n27\r\n60\r\n53\r\n31\r\n45\r\n55\r\n54\r\n57\r\n10\r\n"}, {"input": "21\r\n18 18 18 18 18 0 18 18 18 18 18 18 18 18 18 18 18 6 18 18 18\r\n", "output": "YES\r\n18\r\n2\r\n3\r\n4\r\n5\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n6\r\n19\r\n20\r\n21\r\n1\r\n"}, {"input": "61\r\n56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 0 56 56 56 56 56 56 56 48 56 56 56 56 56\r\n", "output": "YES\r\n56\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n48\r\n57\r\n58\r\n59\r\n60\r\n61\r\n1\r\n"}, {"input": "21\r\n15 6 13 7 15 21 8 0 7 16 16 21 12 6 12 12 13 6 15 16 7\r\n", "output": "YES\r\n6\r\n2\r\n14\r\n18\r\n7\r\n4\r\n8\r\n9\r\n16\r\n10\r\n11\r\n20\r\n15\r\n5\r\n12\r\n21\r\n13\r\n3\r\n17\r\n19\r\n1\r\n"}, {"input": "61\r\n58 39 45 57 31 43 11 24 8 18 56 54 47 37 50 40 19 16 29 10 1 23 36 28 21 48 52 55 27 42 2 33 46 25 53 6 15 26 14 17 9 44 56 34 5 61 38 12 30 7 49 32 20 41 51 0 3 4 60 35 13\r\n", "output": "YES\r\n23\r\n22\r\n6\r\n36\r\n56\r\n43\r\n7\r\n11\r\n15\r\n50\r\n14\r\n37\r\n2\r\n39\r\n5\r\n31\r\n3\r\n45\r\n4\r\n57\r\n60\r\n59\r\n53\r\n35\r\n10\r\n20\r\n16\r\n18\r\n17\r\n40\r\n29\r\n19\r\n52\r\n27\r\n33\r\n32\r\n61\r\n46\r\n47\r\n13\r\n26\r\n38\r\n12\r\n48\r\n41\r\n54\r\n8\r\n9\r\n28\r\n24\r\n51\r\n55\r\n30\r\n49\r\n44\r\n42\r\n25\r\n34\r\n1\r\n58\r\n21\r\n"}, {"input": "21\r\n21 6 4 20 14 1 13 10 11 0 10 18 10 12 4 1 2 2 8 2 13\r\n", "output": "YES\r\n8\r\n19\r\n11\r\n9\r\n21\r\n13\r\n7\r\n10\r\n14\r\n5\r\n18\r\n12\r\n20\r\n4\r\n3\r\n15\r\n2\r\n17\r\n1\r\n6\r\n16\r\n"}, {"input": "61\r\n17 19 8 53 10 38 59 60 46 25 49 28 46 15 25 56 53 60 60 54 18 49 10 53 29 19 11 61 24 11 17 52 32 54 29 55 0 1 14 56 25 14 33 53 47 56 8 6 53 55 16 46 47 9 24 37 3 52 25 37 26\r\n", "output": "YES\r\n15\r\n14\r\n39\r\n42\r\n59\r\n7\r\n3\r\n57\r\n18\r\n21\r\n28\r\n12\r\n26\r\n61\r\n19\r\n2\r\n16\r\n51\r\n9\r\n54\r\n20\r\n34\r\n33\r\n43\r\n37\r\n56\r\n40\r\n46\r\n13\r\n52\r\n32\r\n58\r\n8\r\n60\r\n47\r\n45\r\n6\r\n48\r\n1\r\n17\r\n53\r\n4\r\n24\r\n29\r\n25\r\n10\r\n5\r\n23\r\n41\r\n35\r\n55\r\n36\r\n50\r\n44\r\n49\r\n11\r\n27\r\n30\r\n22\r\n31\r\n38\r\n"}, {"input": "21\r\n18 0 18 2 21 2 9 15 3 5 8 2 8 21 6 10 21 13 9 1 13\r\n", "output": "YES\r\n3\r\n9\r\n7\r\n19\r\n6\r\n2\r\n4\r\n12\r\n10\r\n16\r\n21\r\n5\r\n14\r\n17\r\n13\r\n8\r\n15\r\n11\r\n1\r\n18\r\n20\r\n"}, {"input": "61\r\n45 48 30 23 15 47 8 3 35 56 54 35 17 47 35 56 32 42 14 37 36 44 6 44 1 44 41 46 43 0 33 3 44 54 43 3 47 57 7 32 29 60 36 36 43 61 36 47 3 48 18 8 17 29 3 54 3 6 43 43 56\r\n", "output": "YES\r\n41\r\n27\r\n46\r\n28\r\n56\r\n10\r\n16\r\n61\r\n29\r\n54\r\n11\r\n34\r\n15\r\n5\r\n7\r\n39\r\n8\r\n52\r\n57\r\n38\r\n33\r\n31\r\n44\r\n22\r\n24\r\n26\r\n23\r\n4\r\n6\r\n58\r\n14\r\n19\r\n37\r\n20\r\n47\r\n48\r\n2\r\n50\r\n18\r\n51\r\n60\r\n42\r\n43\r\n35\r\n9\r\n12\r\n36\r\n21\r\n3\r\n30\r\n32\r\n17\r\n13\r\n53\r\n40\r\n49\r\n55\r\n59\r\n1\r\n45\r\n25\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
n = int(f.readline())
p = list(map(int, f.readline().split()))
# Read reference output
with open(output_path) as f_ref:
ref_first_line = f_ref.readline().strip()
# Read submission output
with open(submission_path) as f_sub:
lines = [line.strip() for line in f_sub.readlines()]
# Check first line
if not lines:
print(0)
return
submission_first = lines[0]
if submission_first != ref_first_line:
print(0)
return
if ref_first_line == "NO":
if len(lines) != 1:
print(0)
return
else:
print(1)
return
# Check YES case
if len(lines) != n + 1:
print(0)
return
sequence = lines[1:]
if len(sequence) != n:
print(0)
return
try:
sequence = list(map(int, sequence))
except:
print(0)
return
if any(x < 1 or x > n for x in sequence):
print(0)
return
if len(set(sequence)) != n:
print(0)
return
# Build adjacency list
adj = [[] for _ in range(n + 1)]
for i in range(1, n + 1):
pi = p[i - 1]
if pi != 0:
adj[i].append(pi)
adj[pi].append(i)
current_degree = [0] * (n + 1)
for i in range(1, n + 1):
current_degree[i] = len(adj[i])
alive = [True] * (n + 1)
# Simulate destruction
for node in sequence:
if not alive[node]:
print(0)
return
if current_degree[node] % 2 != 0:
print(0)
return
for neighbor in adj[node]:
if alive[neighbor]:
current_degree[neighbor] -= 1
alive[node] = False
current_degree[node] = 0
if all(not alive[i] for i in range(1, n + 1)):
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
855/A
|
855
|
A
|
PyPy 3-64
|
TESTS
| 3
| 46
| 0
|
228422490
|
n=int(input(""))
l=[]
for i in range(n):
x=input()
l.append(x)
for j in range(n):
z=0
for m in range(j):
if l[i]==l[m]:
z+=1
else:
z+=0
if z!=0:
print("YES")
else:
print("NO")
| 55
| 46
| 0
|
144955619
|
n=int(input())
lst=[]
for i in range(n):
st=input()
if(st in lst):
print("YES")
else:
print("NO")
lst.append(st)
|
Manthan, Codefest 17
|
CF
| 2,017
| 2
| 256
|
Tom Riddle's Diary
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
|
First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
| null |
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".
|
[{"input": "6\ntom\nlucius\nginny\nharry\nginny\nharry", "output": "NO\nNO\nNO\nNO\nYES\nYES"}, {"input": "3\na\na\na", "output": "NO\nYES\nYES"}]
| 800
|
["brute force", "implementation", "strings"]
| 55
|
[{"input": "6\r\ntom\r\nlucius\r\nginny\r\nharry\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\n"}, {"input": "1\r\nzn\r\n", "output": "NO\r\n"}, {"input": "9\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nhrtm\r\nssjqvixduertmotgagizamvfucfwtxqnhuowbqbzctgznivehelpcyigwrbbdsxnewfqvcf\r\nhyrtxvozpbveexfkgalmguozzakitjiwsduqxonb\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nbdbivqzvhggth\r\n", "output": "NO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "10\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nmvutw\r\nqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuzepuezqqzxlfmdanoeaoqmor\r\nmvutw\r\nvchawxjoreboqzuklifv\r\nvchawxjoreboqzuklifv\r\nnivijte\r\nrflybruq\r\nvchawxjoreboqzuklifv\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "1\r\nz\r\n", "output": "NO\r\n"}, {"input": "9\r\nl\r\ny\r\nm\r\nj\r\nn\r\nr\r\nj\r\nk\r\nf\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "14\r\nw\r\na\r\nh\r\np\r\nk\r\nw\r\ny\r\nv\r\ns\r\nf\r\nx\r\nd\r\nk\r\nr\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "25\r\np\r\nk\r\nu\r\nl\r\nf\r\nt\r\nc\r\ns\r\nq\r\nd\r\nb\r\nq\r\no\r\ni\r\ni\r\nd\r\ni\r\nw\r\nn\r\ng\r\nw\r\nt\r\na\r\ne\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "20\r\nd\r\nh\r\ng\r\no\r\np\r\ne\r\nt\r\nj\r\nv\r\ni\r\nt\r\nh\r\ns\r\ni\r\nw\r\nf\r\nx\r\na\r\nl\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3\r\nbbbbbbb\r\nbbbbbbbbb\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "NO\r\nNO\r\n"}, {"input": "6\r\ntom\r\nlucius\r\nginnys\r\nharpy\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\n", "output": "NO\r\nYES\r\n"}, {"input": "42\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
855/A
|
855
|
A
|
Python 3
|
TESTS
| 3
| 31
| 0
|
146609408
|
n=int(input())
s=[]
for i in range(n):
s.append(input())
flag=0
for i in range(n):
for j in range(i):
if s[j]==s[i]:
flag=1
break
if flag==1:
print("YES")
if flag==0:
print("NO")
| 55
| 46
| 0
|
144955926
|
n=int(input())
a=[]
for i in range(n):
l=input()
if(l in a):
print('Yes')
else:
a.append(l)
print('No')
|
Manthan, Codefest 17
|
CF
| 2,017
| 2
| 256
|
Tom Riddle's Diary
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
|
First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
| null |
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".
|
[{"input": "6\ntom\nlucius\nginny\nharry\nginny\nharry", "output": "NO\nNO\nNO\nNO\nYES\nYES"}, {"input": "3\na\na\na", "output": "NO\nYES\nYES"}]
| 800
|
["brute force", "implementation", "strings"]
| 55
|
[{"input": "6\r\ntom\r\nlucius\r\nginny\r\nharry\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\n"}, {"input": "1\r\nzn\r\n", "output": "NO\r\n"}, {"input": "9\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nhrtm\r\nssjqvixduertmotgagizamvfucfwtxqnhuowbqbzctgznivehelpcyigwrbbdsxnewfqvcf\r\nhyrtxvozpbveexfkgalmguozzakitjiwsduqxonb\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nbdbivqzvhggth\r\n", "output": "NO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "10\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nmvutw\r\nqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuzepuezqqzxlfmdanoeaoqmor\r\nmvutw\r\nvchawxjoreboqzuklifv\r\nvchawxjoreboqzuklifv\r\nnivijte\r\nrflybruq\r\nvchawxjoreboqzuklifv\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "1\r\nz\r\n", "output": "NO\r\n"}, {"input": "9\r\nl\r\ny\r\nm\r\nj\r\nn\r\nr\r\nj\r\nk\r\nf\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "14\r\nw\r\na\r\nh\r\np\r\nk\r\nw\r\ny\r\nv\r\ns\r\nf\r\nx\r\nd\r\nk\r\nr\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "25\r\np\r\nk\r\nu\r\nl\r\nf\r\nt\r\nc\r\ns\r\nq\r\nd\r\nb\r\nq\r\no\r\ni\r\ni\r\nd\r\ni\r\nw\r\nn\r\ng\r\nw\r\nt\r\na\r\ne\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "20\r\nd\r\nh\r\ng\r\no\r\np\r\ne\r\nt\r\nj\r\nv\r\ni\r\nt\r\nh\r\ns\r\ni\r\nw\r\nf\r\nx\r\na\r\nl\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3\r\nbbbbbbb\r\nbbbbbbbbb\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "NO\r\nNO\r\n"}, {"input": "6\r\ntom\r\nlucius\r\nginnys\r\nharpy\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\n", "output": "NO\r\nYES\r\n"}, {"input": "42\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
855/A
|
855
|
A
|
Python 3
|
TESTS
| 3
| 31
| 0
|
221466945
|
n = int(input())
a = []
b = 0
for i in range(n):
c = input()
a.append(c)
for i in range(0,n):
for j in range(0,i):
if(i!=j):
if(a[i]==a[j]):
b+=1
else:
continue
if(b>0):
print("YES")
else:
print("NO")
| 55
| 46
| 0
|
144956037
|
names = int(input()) #the number of names in the list.
vaish = []
for i in range(names):
vaish.append(input())
for i in range(names):
flag = 0
for j in range(i):
if vaish[i] == vaish[j]:
flag = 1
break
if flag:
print("YES")
else:
print("NO")
|
Manthan, Codefest 17
|
CF
| 2,017
| 2
| 256
|
Tom Riddle's Diary
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
|
First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
| null |
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".
|
[{"input": "6\ntom\nlucius\nginny\nharry\nginny\nharry", "output": "NO\nNO\nNO\nNO\nYES\nYES"}, {"input": "3\na\na\na", "output": "NO\nYES\nYES"}]
| 800
|
["brute force", "implementation", "strings"]
| 55
|
[{"input": "6\r\ntom\r\nlucius\r\nginny\r\nharry\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\n"}, {"input": "1\r\nzn\r\n", "output": "NO\r\n"}, {"input": "9\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nhrtm\r\nssjqvixduertmotgagizamvfucfwtxqnhuowbqbzctgznivehelpcyigwrbbdsxnewfqvcf\r\nhyrtxvozpbveexfkgalmguozzakitjiwsduqxonb\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nbdbivqzvhggth\r\n", "output": "NO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "10\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nmvutw\r\nqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuzepuezqqzxlfmdanoeaoqmor\r\nmvutw\r\nvchawxjoreboqzuklifv\r\nvchawxjoreboqzuklifv\r\nnivijte\r\nrflybruq\r\nvchawxjoreboqzuklifv\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "1\r\nz\r\n", "output": "NO\r\n"}, {"input": "9\r\nl\r\ny\r\nm\r\nj\r\nn\r\nr\r\nj\r\nk\r\nf\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "14\r\nw\r\na\r\nh\r\np\r\nk\r\nw\r\ny\r\nv\r\ns\r\nf\r\nx\r\nd\r\nk\r\nr\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "25\r\np\r\nk\r\nu\r\nl\r\nf\r\nt\r\nc\r\ns\r\nq\r\nd\r\nb\r\nq\r\no\r\ni\r\ni\r\nd\r\ni\r\nw\r\nn\r\ng\r\nw\r\nt\r\na\r\ne\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "20\r\nd\r\nh\r\ng\r\no\r\np\r\ne\r\nt\r\nj\r\nv\r\ni\r\nt\r\nh\r\ns\r\ni\r\nw\r\nf\r\nx\r\na\r\nl\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3\r\nbbbbbbb\r\nbbbbbbbbb\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "NO\r\nNO\r\n"}, {"input": "6\r\ntom\r\nlucius\r\nginnys\r\nharpy\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\n", "output": "NO\r\nYES\r\n"}, {"input": "42\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
38/D
|
38
|
D
|
Python 3
|
TESTS
| 3
| 62
| 0
|
197034283
|
n = int(input())
k = 1
x_1, y_1, x_2, y_2 = list(map(abs, map(int, input().split())))
for cord in range(n-1):
x_1_new, y_1_new, x_2_new, y_2_new = list(map(abs, map(int, input().split())))
S_intersect = (min(x_2,x_2_new) - (max(x_1, x_1_new) - min(x_1, x_1_new))) * (min(y_2,y_2_new) - (max(y_1, y_1_new) - min(y_1, y_1_new)))
S_prev = (x_2 - x_1)*(y_2 - y_1)
if S_intersect >= S_prev / 2:
k += 1
x_1, y_1, x_2, y_2 = x_1_new, y_1_new, x_2_new, y_2_new
print(k)
| 64
| 342
| 22,220,800
|
86059864
|
n = int(input())
l = []
m = []
for i in range(n):
X1, Y1, X2, Y2 = map(int, input().split())
m1 = (abs(X2 - X1)) ** 3
l.append([X1, Y1, X2, Y2])
m.append(m1)
X1, Y1, X2, Y2 = l[0][0], l[0][1], l[0][2], l[0][3]
if X1 > X2: X2, X1 = X1, X2
if Y1 > Y2: Y1, Y2 = Y2, Y1
ans = n
for i in range(1, n):
for j in range(i, 0, -1):
x1, y1, x2, y2 = l[j - 1][0], l[j - 1][1], l[j - 1][2], l[j - 1][3]
if x1 > x2: x2, x1 = x1, x2
if y1 > y2: y1, y2 = y2, y1
numx, numy, den = 0, 0 , 0
for k in range(i, j - 1, -1):
a = (l[k][0] + l[k][2]) / 2
b = (l[k][1] + l[k][3]) / 2
m = (abs(l[k][2] - l[k][0])) ** 3
numx += m * a
numy += m * b
den += m
xcm = numx / den
ycm = numy / den
if x1 <= xcm <= x2 and y1 <= ycm <= y2: pass
else:
ans = i
break
if ans != n: break
print(ans)
|
School Personal Contest #1 (Winter Computer School 2010/11) - Codeforces Beta Round 38 (ACM-ICPC Rules)
|
ICPC
| 2,010
| 2
| 256
|
Vasya the Architect
|
Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart.
Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides of the bricks he has already used. Let's introduce a Cartesian coordinate system on the horizontal plane, where Vasya puts the first brick. Then the projection of brick number i on the plane is a square with sides parallel to the axes of coordinates with opposite corners in points (xi, 1, yi, 1) and (xi, 2, yi, 2). The bricks are cast from homogeneous plastic and the weight of a brick a × a × a is a3 grams.
It is guaranteed that Vasya puts any brick except the first one on the previous one, that is the area of intersection of the upper side of the previous brick and the lower side of the next brick is always positive.
We (Vasya included) live in a normal world where the laws of physical statics work. And that is why, perhaps, if we put yet another brick, the tower will collapse under its own weight. Vasya puts the cubes consecutively one on top of the other until at least one cube loses the balance and falls down. If it happens, Vasya gets upset and stops the construction. Print the number of bricks in the maximal stable tower, that is the maximal number m satisfying the condition that all the towers consisting of bricks 1, 2, ..., k for every integer k from 1 to m remain stable.
|
The first input file contains an integer n (1 ≤ n ≤ 100) which is the number of bricks. Each of the next n lines contains four numbers xi, 1, yi, 1, xi, 2, yi, 2 (xi, 1 ≠ xi, 2, |xi, 1 - xi, 2| = |yi, 1 - yi, 2|) which are the coordinates of the opposite angles of the base of the brick number i. The coordinates are integers and their absolute value does not exceed 50.
The cubes are given in the order Vasya puts them. It is guaranteed that the area of intersection of the upper side of the brick number i - 1 and the lower side of the brick number i is strictly strictly greater than zero for all i ≥ 2.
|
Print the number of bricks in the maximal stable tower.
| null | null |
[{"input": "2\n0 0 3 3\n1 0 4 3", "output": "2"}, {"input": "2\n0 0 3 3\n2 0 5 3", "output": "1"}, {"input": "3\n0 0 3 3\n1 0 4 3\n2 0 5 3", "output": "3"}]
| 1,900
|
["implementation"]
| 64
|
[{"input": "2\r\n0 0 3 3\r\n1 0 4 3\r\n", "output": "2\r\n"}, {"input": "2\r\n0 0 3 3\r\n2 0 5 3\r\n", "output": "1\r\n"}, {"input": "3\r\n0 0 3 3\r\n1 0 4 3\r\n2 0 5 3\r\n", "output": "3\r\n"}, {"input": "5\r\n7 -10 -8 5\r\n4 -7 -5 2\r\n2 -5 -3 0\r\n-9 48 50 -11\r\n50 -4 -2 48\r\n", "output": "3\r\n"}, {"input": "5\r\n6 -2 13 -9\r\n17 2 2 -13\r\n13 -2 6 -9\r\n4 0 15 -11\r\n8 -7 11 -4\r\n", "output": "5\r\n"}, {"input": "7\r\n4 6 1 9\r\n-1 4 6 11\r\n8 13 -3 2\r\n-5 0 45 50\r\n-2 50 45 3\r\n45 50 -4 1\r\n45 50 -1 4\r\n", "output": "3\r\n"}, {"input": "7\r\n24 6 11 -7\r\n23 -6 12 5\r\n16 -2 19 1\r\n13 4 22 -5\r\n12 -6 23 5\r\n13 32 50 -5\r\n50 32 11 -7\r\n", "output": "5\r\n"}, {"input": "7\r\n-2 23 -10 15\r\n2 11 -14 27\r\n-12 25 0 13\r\n-8 17 -4 21\r\n1 26 -13 12\r\n-8 17 -4 21\r\n-7 20 -5 18\r\n", "output": "7\r\n"}, {"input": "10\r\n27 -3 15 9\r\n18 0 24 6\r\n28 -4 14 10\r\n19 1 23 5\r\n27 -3 15 9\r\n19 32 50 1\r\n50 32 14 -4\r\n19 32 50 1\r\n15 32 50 -3\r\n16 -2 50 32\r\n", "output": "5\r\n"}, {"input": "10\r\n-9 -14 -1 -6\r\n-2 -13 -8 -7\r\n0 -15 -10 -5\r\n0 -15 -10 -5\r\n-11 -4 1 -16\r\n-8 -13 -2 -7\r\n1 -16 -11 -4\r\n-13 -2 3 -18\r\n-11 -16 1 -4\r\n-13 -18 3 -2\r\n", "output": "10\r\n"}, {"input": "15\r\n5 16 -6 27\r\n28 21 -1 50\r\n-2 20 28 50\r\n28 14 -8 50\r\n28 19 -3 50\r\n-2 50 28 20\r\n-5 17 28 50\r\n-7 15 28 50\r\n-4 50 28 18\r\n28 50 -1 21\r\n-3 50 28 19\r\n-4 50 28 18\r\n28 50 -8 14\r\n28 50 -6 16\r\n28 15 -7 50\r\n", "output": "1\r\n"}, {"input": "15\r\n-14 27 -1 14\r\n-10 23 -5 18\r\n1 29 -16 12\r\n-9 19 -6 22\r\n-10 18 -5 23\r\n-4 17 -11 24\r\n22 50 -13 15\r\n-13 50 22 15\r\n22 50 -11 17\r\n-9 19 22 50\r\n-10 18 22 50\r\n22 19 -9 50\r\n-14 14 22 50\r\n-14 14 22 50\r\n-11 17 22 50\r\n", "output": "6\r\n"}, {"input": "15\r\n-8 11 1 20\r\n-5 14 -2 17\r\n-1 18 -6 13\r\n0 19 -7 12\r\n-5 14 -2 17\r\n-11 8 4 23\r\n-2 14 -5 17\r\n2 21 -9 10\r\n-12 24 5 7\r\n-10 22 3 9\r\n31 50 -11 8\r\n-8 11 31 50\r\n-7 50 31 12\r\n-7 12 31 50\r\n-10 9 31 50\r\n", "output": "10\r\n"}, {"input": "20\r\n9 -3 16 4\r\n11 2 14 -1\r\n6 -6 19 7\r\n16 -3 9 4\r\n5 -7 50 38\r\n50 38 5 -7\r\n50 38 12 0\r\n50 38 11 -1\r\n10 38 50 -2\r\n5 -7 50 38\r\n50 38 7 -5\r\n50 38 10 -2\r\n50 38 12 0\r\n50 38 10 -2\r\n10 38 50 -2\r\n50 -5 7 38\r\n50 -7 5 38\r\n11 -1 50 38\r\n10 -2 50 38\r\n7 -5 50 38\r\n", "output": "4\r\n"}, {"input": "20\r\n12 23 -1 10\r\n12 23 -1 10\r\n11 22 0 11\r\n7 18 4 15\r\n12 10 -1 23\r\n9 20 2 13\r\n1 21 10 12\r\n0 11 11 22\r\n39 50 2 13\r\n3 14 39 50\r\n39 10 -1 50\r\n4 15 39 50\r\n39 50 4 15\r\n39 50 -2 9\r\n39 50 1 12\r\n-2 9 39 50\r\n2 50 39 13\r\n0 11 39 50\r\n-2 9 39 50\r\n1 12 39 50\r\n", "output": "8\r\n"}, {"input": "20\r\n26 16 11 1\r\n11 1 26 16\r\n23 4 14 13\r\n13 14 24 3\r\n20 10 17 7\r\n25 15 12 2\r\n16 6 21 11\r\n25 15 12 2\r\n10 0 27 17\r\n22 12 15 5\r\n23 4 14 13\r\n22 5 15 12\r\n25 2 12 15\r\n12 15 25 2\r\n22 12 15 5\r\n22 5 15 12\r\n21 11 16 6\r\n26 16 11 1\r\n17 7 20 10\r\n24 3 13 14\r\n", "output": "20\r\n"}, {"input": "5\r\n-6 6 6 -6\r\n7 -8 -5 4\r\n12 -11 2 -1\r\n50 39 -1 -12\r\n-3 -16 50 37\r\n", "output": "3\r\n"}, {"input": "5\r\n-12 12 12 -12\r\n4 -12 8 -8\r\n10 -5 2 -13\r\n12 -5 -2 -19\r\n-7 -18 15 4\r\n", "output": "5\r\n"}, {"input": "7\r\n1 1 -1 -1\r\n6 7 -6 -5\r\n10 -11 -12 11\r\n-12 -11 49 50\r\n-7 43 50 -14\r\n-17 50 37 -4\r\n-14 50 47 -11\r\n", "output": "3\r\n"}, {"input": "7\r\n-9 9 9 -9\r\n10 -5 8 -7\r\n7 -6 9 -8\r\n20 5 -2 -17\r\n8 -15 10 -17\r\n50 25 3 -22\r\n50 28 7 -15\r\n", "output": "5\r\n"}, {"input": "7\r\n-9 -9 9 9\r\n-8 3 0 -5\r\n-16 -8 6 14\r\n14 -11 -6 9\r\n5 -5 1 -9\r\n-11 -21 13 3\r\n-10 5 10 -15\r\n", "output": "6\r\n"}, {"input": "10\r\n-7 7 7 -7\r\n-8 -11 8 5\r\n-1 -1 1 1\r\n-2 1 2 -3\r\n-12 -12 10 10\r\n47 50 3 6\r\n46 5 1 50\r\n50 50 -10 -10\r\n-9 50 50 -9\r\n-6 -6 50 50\r\n", "output": "5\r\n"}, {"input": "10\r\n-7 -7 7 7\r\n-16 -18 6 4\r\n-12 -8 4 8\r\n5 -13 -7 -1\r\n5 4 -13 -14\r\n7 11 -13 -9\r\n4 10 8 6\r\n8 6 0 14\r\n7 7 9 5\r\n-2 -9 16 9\r\n", "output": "9\r\n"}, {"input": "15\r\n1 1 -1 -1\r\n50 -8 -7 49\r\n45 50 0 5\r\n49 50 -8 -7\r\n-11 50 49 -10\r\n0 9 41 50\r\n50 50 -6 -6\r\n-1 46 50 -5\r\n1 49 50 0\r\n50 -8 -8 50\r\n7 41 50 -2\r\n-9 50 50 -9\r\n50 10 10 50\r\n9 41 50 0\r\n-4 -5 50 49\r\n", "output": "1\r\n"}, {"input": "15\r\n-9 -9 9 9\r\n-2 -18 16 0\r\n15 -3 5 -13\r\n6 -7 4 -9\r\n17 4 -7 -20\r\n6 -15 4 -17\r\n50 -18 3 29\r\n50 -22 -1 29\r\n-3 -23 50 30\r\n-6 -26 50 30\r\n50 -26 -5 29\r\n50 21 11 -18\r\n50 23 6 -21\r\n50 28 6 -16\r\n50 -24 -3 29\r\n", "output": "6\r\n"}, {"input": "15\r\n-7 -7 7 7\r\n-10 -16 14 8\r\n13 -12 5 -4\r\n12 -6 4 -14\r\n3 -17 17 -3\r\n21 -11 7 3\r\n14 0 2 -12\r\n-3 0 17 -20\r\n-2 -12 18 8\r\n9 -14 13 -10\r\n7 -15 50 28\r\n0 -20 50 30\r\n50 -14 6 30\r\n-4 -21 50 33\r\n50 33 -3 -20\r\n", "output": "10\r\n"}, {"input": "6\r\n-9 -9 9 9\r\n-16 9 0 -7\r\n7 8 -17 -16\r\n49 -18 -19 50\r\n-19 49 50 -20\r\n50 -17 -14 47\r\n", "output": "3\r\n"}, {"input": "6\r\n-5 5 5 -5\r\n9 7 -3 -5\r\n4 0 -8 12\r\n-7 2 -9 0\r\n-9 -1 -5 3\r\n42 -9 -17 50\r\n", "output": "5\r\n"}, {"input": "8\r\n12 -12 -12 12\r\n1 13 -13 -1\r\n-18 0 2 20\r\n31 -1 -20 50\r\n29 50 -30 -9\r\n-15 -9 44 50\r\n-21 -14 43 50\r\n45 50 -16 -11\r\n", "output": "3\r\n"}, {"input": "8\r\n-2 2 2 -2\r\n-2 6 6 -2\r\n5 -2 -1 4\r\n9 -7 -9 11\r\n13 10 -9 -12\r\n-10 -16 50 44\r\n32 50 -15 3\r\n-13 50 34 3\r\n", "output": "5\r\n"}, {"input": "8\r\n-7 -7 7 7\r\n-1 -5 -3 -3\r\n5 4 -11 -12\r\n8 -4 2 -10\r\n3 -5 1 -3\r\n10 -15 -10 5\r\n7 -14 -11 4\r\n-2 -10 50 42\r\n", "output": "5\r\n"}, {"input": "11\r\n12 12 -12 -12\r\n11 8 -11 -14\r\n-15 -11 -3 1\r\n-16 -3 -14 -1\r\n-7 -8 -21 6\r\n37 -6 -19 50\r\n-21 -12 41 50\r\n48 -13 -15 50\r\n48 -14 -16 50\r\n-21 -18 47 50\r\n40 50 -15 -5\r\n", "output": "4\r\n"}, {"input": "11\r\n-4 4 4 -4\r\n-2 -3 0 -5\r\n-8 -10 6 4\r\n-12 6 8 -14\r\n-4 -12 12 4\r\n-1 -11 1 -9\r\n-5 -14 5 -4\r\n-11 2 11 -20\r\n10 -10 12 -8\r\n7 -12 13 -6\r\n-6 41 50 -15\r\n", "output": "6\r\n"}, {"input": "1\r\n0 0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n-50 -50 50 50\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
855/A
|
855
|
A
|
PyPy 3
|
TESTS
| 3
| 139
| 0
|
73856708
|
tests = int(input())
list =[]
for number in range(tests):
a= str(input())
list.append(a)
flag = 0
for temp in range(tests):
for temp1 in range(temp):
if list[temp]==list[temp1]:
flag = 1
break
if flag :
print('YES')
else:
print('NO')
| 55
| 46
| 0
|
144956224
|
l=[]
n=int(input())
for x in range(n):
name=input()
if name in l:
print("YES")
else:
print("NO")
l.append(name)
|
Manthan, Codefest 17
|
CF
| 2,017
| 2
| 256
|
Tom Riddle's Diary
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
|
First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
| null |
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".
|
[{"input": "6\ntom\nlucius\nginny\nharry\nginny\nharry", "output": "NO\nNO\nNO\nNO\nYES\nYES"}, {"input": "3\na\na\na", "output": "NO\nYES\nYES"}]
| 800
|
["brute force", "implementation", "strings"]
| 55
|
[{"input": "6\r\ntom\r\nlucius\r\nginny\r\nharry\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\n"}, {"input": "1\r\nzn\r\n", "output": "NO\r\n"}, {"input": "9\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nhrtm\r\nssjqvixduertmotgagizamvfucfwtxqnhuowbqbzctgznivehelpcyigwrbbdsxnewfqvcf\r\nhyrtxvozpbveexfkgalmguozzakitjiwsduqxonb\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nbdbivqzvhggth\r\n", "output": "NO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "10\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nmvutw\r\nqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuzepuezqqzxlfmdanoeaoqmor\r\nmvutw\r\nvchawxjoreboqzuklifv\r\nvchawxjoreboqzuklifv\r\nnivijte\r\nrflybruq\r\nvchawxjoreboqzuklifv\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "1\r\nz\r\n", "output": "NO\r\n"}, {"input": "9\r\nl\r\ny\r\nm\r\nj\r\nn\r\nr\r\nj\r\nk\r\nf\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "14\r\nw\r\na\r\nh\r\np\r\nk\r\nw\r\ny\r\nv\r\ns\r\nf\r\nx\r\nd\r\nk\r\nr\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "25\r\np\r\nk\r\nu\r\nl\r\nf\r\nt\r\nc\r\ns\r\nq\r\nd\r\nb\r\nq\r\no\r\ni\r\ni\r\nd\r\ni\r\nw\r\nn\r\ng\r\nw\r\nt\r\na\r\ne\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "20\r\nd\r\nh\r\ng\r\no\r\np\r\ne\r\nt\r\nj\r\nv\r\ni\r\nt\r\nh\r\ns\r\ni\r\nw\r\nf\r\nx\r\na\r\nl\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3\r\nbbbbbbb\r\nbbbbbbbbb\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "NO\r\nNO\r\n"}, {"input": "6\r\ntom\r\nlucius\r\nginnys\r\nharpy\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\n", "output": "NO\r\nYES\r\n"}, {"input": "42\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
855/A
|
855
|
A
|
Python 3
|
TESTS
| 3
| 108
| 0
|
82166064
|
n = int(input())
a = 0
b = []
c = []
j = "NO"
while a < n:
a += 1
s = input()
if s in b:
j = "YES"
b.append(s)
c.append(j)
for x in c:
print(x)
| 55
| 46
| 0
|
144956240
|
n=int(input())
s=set()
for i in range(n):
a=input()
if a in s:
print("YES")
else:
print("NO")
s.add(a)
|
Manthan, Codefest 17
|
CF
| 2,017
| 2
| 256
|
Tom Riddle's Diary
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
|
First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
| null |
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".
|
[{"input": "6\ntom\nlucius\nginny\nharry\nginny\nharry", "output": "NO\nNO\nNO\nNO\nYES\nYES"}, {"input": "3\na\na\na", "output": "NO\nYES\nYES"}]
| 800
|
["brute force", "implementation", "strings"]
| 55
|
[{"input": "6\r\ntom\r\nlucius\r\nginny\r\nharry\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\n"}, {"input": "1\r\nzn\r\n", "output": "NO\r\n"}, {"input": "9\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nhrtm\r\nssjqvixduertmotgagizamvfucfwtxqnhuowbqbzctgznivehelpcyigwrbbdsxnewfqvcf\r\nhyrtxvozpbveexfkgalmguozzakitjiwsduqxonb\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nbdbivqzvhggth\r\n", "output": "NO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "10\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nmvutw\r\nqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuzepuezqqzxlfmdanoeaoqmor\r\nmvutw\r\nvchawxjoreboqzuklifv\r\nvchawxjoreboqzuklifv\r\nnivijte\r\nrflybruq\r\nvchawxjoreboqzuklifv\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "1\r\nz\r\n", "output": "NO\r\n"}, {"input": "9\r\nl\r\ny\r\nm\r\nj\r\nn\r\nr\r\nj\r\nk\r\nf\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "14\r\nw\r\na\r\nh\r\np\r\nk\r\nw\r\ny\r\nv\r\ns\r\nf\r\nx\r\nd\r\nk\r\nr\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "25\r\np\r\nk\r\nu\r\nl\r\nf\r\nt\r\nc\r\ns\r\nq\r\nd\r\nb\r\nq\r\no\r\ni\r\ni\r\nd\r\ni\r\nw\r\nn\r\ng\r\nw\r\nt\r\na\r\ne\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "20\r\nd\r\nh\r\ng\r\no\r\np\r\ne\r\nt\r\nj\r\nv\r\ni\r\nt\r\nh\r\ns\r\ni\r\nw\r\nf\r\nx\r\na\r\nl\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3\r\nbbbbbbb\r\nbbbbbbbbb\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "NO\r\nNO\r\n"}, {"input": "6\r\ntom\r\nlucius\r\nginnys\r\nharpy\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\n", "output": "NO\r\nYES\r\n"}, {"input": "42\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
855/A
|
855
|
A
|
Python 3
|
TESTS
| 3
| 93
| 0
|
78579053
|
n = int(input())
lis = []
for i in range(n):
s = input()
lis.append(s)
flag = 0
for i in range(n):
for j in range(n):
if j<i and lis[j]==lis[i]:
flag=1
if flag==1:
print("YES")
else:
print("NO")
| 55
| 46
| 0
|
144956397
|
a = []
for _ in range(int(input())):
name = input()
if name not in a:
a.append(name)
print("NO")
else:
print("YES")
|
Manthan, Codefest 17
|
CF
| 2,017
| 2
| 256
|
Tom Riddle's Diary
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
|
First line of input contains an integer n (1 ≤ n ≤ 100) — the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
| null |
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "YES".
|
[{"input": "6\ntom\nlucius\nginny\nharry\nginny\nharry", "output": "NO\nNO\nNO\nNO\nYES\nYES"}, {"input": "3\na\na\na", "output": "NO\nYES\nYES"}]
| 800
|
["brute force", "implementation", "strings"]
| 55
|
[{"input": "6\r\ntom\r\nlucius\r\nginny\r\nharry\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\n"}, {"input": "1\r\nzn\r\n", "output": "NO\r\n"}, {"input": "9\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nliyzmbjwnzryjokufuxcqtzwworjeoxkbaqrujrhdidqdvwdfzilwszgnzglnnbogaclckfnbqovtziuhwvyrqwmskx\r\nhrtm\r\nssjqvixduertmotgagizamvfucfwtxqnhuowbqbzctgznivehelpcyigwrbbdsxnewfqvcf\r\nhyrtxvozpbveexfkgalmguozzakitjiwsduqxonb\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nwcyxteiwtcyuztaguilqpbiwcwjaiq\r\nbdbivqzvhggth\r\n", "output": "NO\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "10\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nkkiubdktydpdcbbttwpfdplhhjhrpqmpg\r\nmvutw\r\nqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuzepuezqqzxlfmdanoeaoqmor\r\nmvutw\r\nvchawxjoreboqzuklifv\r\nvchawxjoreboqzuklifv\r\nnivijte\r\nrflybruq\r\nvchawxjoreboqzuklifv\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "1\r\nz\r\n", "output": "NO\r\n"}, {"input": "9\r\nl\r\ny\r\nm\r\nj\r\nn\r\nr\r\nj\r\nk\r\nf\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "14\r\nw\r\na\r\nh\r\np\r\nk\r\nw\r\ny\r\nv\r\ns\r\nf\r\nx\r\nd\r\nk\r\nr\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "25\r\np\r\nk\r\nu\r\nl\r\nf\r\nt\r\nc\r\ns\r\nq\r\nd\r\nb\r\nq\r\no\r\ni\r\ni\r\nd\r\ni\r\nw\r\nn\r\ng\r\nw\r\nt\r\na\r\ne\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "20\r\nd\r\nh\r\ng\r\no\r\np\r\ne\r\nt\r\nj\r\nv\r\ni\r\nt\r\nh\r\ns\r\ni\r\nw\r\nf\r\nx\r\na\r\nl\r\ni\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "3\r\nbbbbbbb\r\nbbbbbbbbb\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "NO\r\nNO\r\n"}, {"input": "6\r\ntom\r\nlucius\r\nginnys\r\nharpy\r\nginny\r\nharry\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "2\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\nabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\r\n", "output": "NO\r\nYES\r\n"}, {"input": "42\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}]
| false
|
stdio
| null | true
|
774/D
|
774
|
D
|
Python 3
|
TESTS
| 17
| 155
| 9,523,200
|
164246086
|
from hashlib import blake2b
n,l,r = map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
al = a[:l-1]
ar = a[r:]
bl = b[:l-1]
br = b[r:]
al.sort()
bl.sort()
ar.sort()
br.sort()
if al == bl and ar == br:
print('TRUTH')
else:
print('LIE')
| 52
| 93
| 15,155,200
|
26152014
|
s = input().split(" ")
o = input().split(" ")
f = input().split(" ")
flag =0
for i in range(0,int(s[1])-1):
if o[i]!=f[i]:
flag =1
break
if flag == 0:
for i in range(int(s[2]),int(s[0])):
if o[i]!=f[i]:
flag =1
break
if flag == 0:
print ("TRUTH")
else:
print ("LIE")
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 2
| 256
|
Lie or Truth
|
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
|
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
|
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
| null |
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
|
[{"input": "5 2 4\n3 4 2 3 1\n3 2 3 4 1", "output": "TRUTH"}, {"input": "3 1 2\n1 2 3\n3 1 2", "output": "LIE"}, {"input": "4 2 4\n1 1 1 1\n1 1 1 1", "output": "TRUTH"}]
| 1,500
|
["*special", "constructive algorithms", "implementation", "sortings"]
| 52
|
[{"input": "5 2 4\r\n3 4 2 3 1\r\n3 2 3 4 1\r\n", "output": "TRUTH\r\n"}, {"input": "3 1 2\r\n1 2 3\r\n3 1 2\r\n", "output": "LIE\r\n"}, {"input": "4 2 4\r\n1 1 1 1\r\n1 1 1 1\r\n", "output": "TRUTH\r\n"}, {"input": "5 1 3\r\n2 2 2 1 2\r\n2 2 2 1 2\r\n", "output": "TRUTH\r\n"}, {"input": "7 1 4\r\n2 5 5 5 4 3 4\r\n2 5 5 5 4 3 4\r\n", "output": "TRUTH\r\n"}, {"input": "10 1 10\r\n6 7 6 1 10 10 9 5 3 9\r\n7 10 9 6 1 5 9 3 10 6\r\n", "output": "TRUTH\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "TRUTH\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n2 1 3 4\r\n", "output": "LIE\r\n"}, {"input": "7 2 4\r\n1 2 3 4 5 7 6\r\n1 2 3 4 5 6 7\r\n", "output": "LIE\r\n"}, {"input": "5 1 2\r\n1 2 3 4 5\r\n1 2 3 5 4\r\n", "output": "LIE\r\n"}, {"input": "8 3 6\r\n5 3 1 1 1 1 3 5\r\n3 3 1 1 1 1 5 5\r\n", "output": "LIE\r\n"}, {"input": "4 2 2\r\n2 1 2 2\r\n1 2 2 2\r\n", "output": "LIE\r\n"}]
| false
|
stdio
| null | true
|
859/D
|
859
|
D
|
PyPy 3
|
TESTS
| 13
| 140
| 716,800
|
52623876
|
n = int(input())
# Using the same index I would for a tree
m = 2**n
P = [[int(x)/100.0 for x in input().split()] for _ in range(m)]
state = [[0.0]*64 for _ in range(2*m)]
for i in range(m):
state[m+i][i] = 1.0
for i in reversed(range(1,m)):
for j in range(m):
for k in range(j,m):
# x is probability that team j meets team k in match i
x = state[2*i][j]*state[2*i+1][k]
state[i][j] += P[j][k]*x
state[i][k] += P[k][j]*x
winner = [-1]*(2*m)
winner[1] = max(range(m),key = lambda j: state[1][j])
for i in range(2,m):
if state[i][winner[i//2]]>0:
winner[i] = winner[i//2]
else:
winner[i] = max(range(m),key = lambda j: state[i][j])
points = [0]*(2*m)
points[1] = 2**(n-1)
for i in range(1,m):
x = points[i]//2
points[2*i] = x
points[2*i+1] = x
score = 0.0
for i in range(1,m):
score += points[i]*state[i][winner[i]]
print(repr(score))
| 19
| 62
| 0
|
30774075
|
n = int(input())
m = 1 << n
p = [list(map(int, input().split())) for i in range(m)]
u, x = [1] * m, [0] * m
v, y = u[:], x[:]
for i in range(n):
d = 1 << i
for j in range(m):
s = d * (j // d ^ 1)
v[j] = u[j] * sum(u[k] * p[j][k] for k in range(s, s + d)) / 100
y[j] = max(x[s: s + d]) + x[j] + v[j] * d
u, v, x, y = v, u, y, x
print(max(x))
|
MemSQL Start[c]UP 3.0 - Round 1
|
CF
| 2,017
| 2
| 256
|
Third Month Insanity
|
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2N teams participating in the tournament, numbered from 1 to 2N. The tournament lasts N rounds, with each round eliminating half the teams. The first round consists of 2N - 1 games, numbered starting from 1. In game i, team 2·i - 1 will play against team 2·i. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game i the winner of the previous round's game 2·i - 1 will play against the winner of the previous round's game 2·i.
Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2N - 1 points.
For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score.
|
Input will begin with a line containing N (2 ≤ N ≤ 6).
2N lines follow, each with 2N integers. The j-th column of the i-th row indicates the percentage chance that team i will defeat team j, unless i = j, in which case the value will be 0. It is guaranteed that the i-th column of the j-th row plus the j-th column of the i-th row will add to exactly 100.
|
Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10 - 9.
Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if $${ \frac { | a - b | } { \operatorname* { m a x } ( 1, | b | ) } } \leq 1 0 ^ { - 9 }$$.
| null |
In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.
|
[{"input": "2\n0 40 100 100\n60 0 40 40\n0 60 0 45\n0 60 55 0", "output": "1.75"}, {"input": "3\n0 0 100 0 100 0 0 0\n100 0 100 0 0 0 100 100\n0 0 0 100 100 0 0 0\n100 100 0 0 0 0 100 100\n0 100 0 100 0 0 100 0\n100 100 100 100 100 0 0 0\n100 0 100 0 0 100 0 0\n100 0 100 0 100 100 100 0", "output": "12"}, {"input": "2\n0 21 41 26\n79 0 97 33\n59 3 0 91\n74 67 9 0", "output": "3.141592"}]
| 2,100
|
["dp", "probabilities", "trees"]
| 19
|
[{"input": "2\r\n0 40 100 100\r\n60 0 40 40\r\n0 60 0 45\r\n0 60 55 0\r\n", "output": "1.75\r\n"}, {"input": "3\r\n0 0 100 0 100 0 0 0\r\n100 0 100 0 0 0 100 100\r\n0 0 0 100 100 0 0 0\r\n100 100 0 0 0 0 100 100\r\n0 100 0 100 0 0 100 0\r\n100 100 100 100 100 0 0 0\r\n100 0 100 0 0 100 0 0\r\n100 0 100 0 100 100 100 0\r\n", "output": "12\r\n"}, {"input": "2\r\n0 21 41 26\r\n79 0 97 33\r\n59 3 0 91\r\n74 67 9 0\r\n", "output": "3.141592\r\n"}, {"input": "3\r\n0 7 38 22 3 66 32 77\r\n93 0 93 61 45 40 6 92\r\n62 7 0 32 8 46 56 29\r\n78 39 68 0 37 24 84 42\r\n97 55 92 63 0 46 62 100\r\n34 60 54 76 54 0 39 67\r\n68 94 44 16 38 61 0 98\r\n23 8 71 58 0 33 2 0\r\n", "output": "6.8196427571\r\n"}, {"input": "2\r\n0 50 50 50\r\n50 0 50 50\r\n50 50 0 50\r\n50 50 50 0\r\n", "output": "1.5\r\n"}, {"input": "2\r\n0 70 12 95\r\n30 0 98 85\r\n88 2 0 81\r\n5 15 19 0\r\n", "output": "2.51764\r\n"}, {"input": "2\r\n0 0 100 100\r\n100 0 100 100\r\n0 0 0 0\r\n0 0 100 0\r\n", "output": "4\r\n"}, {"input": "2\r\n0 31 4 83\r\n69 0 1 74\r\n96 99 0 71\r\n17 26 29 0\r\n", "output": "2.792594\r\n"}, {"input": "3\r\n0 53 31 33 2 34 22 21\r\n47 0 72 36 11 16 3 86\r\n69 28 0 49 25 6 92 54\r\n67 64 51 0 54 65 15 24\r\n98 89 75 46 0 48 65 31\r\n66 84 94 35 52 0 64 46\r\n78 97 8 85 35 36 0 62\r\n79 14 46 76 69 54 38 0\r\n", "output": "4.23995819508\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
# Read correct output
try:
with open(correct_output_path, 'r') as f:
correct_line = f.readline().strip()
correct = float(correct_line)
except:
print(0)
return
# Read submission output
try:
with open(submission_output_path, 'r') as f:
submission_line = f.readline().strip()
submission = float(submission_line)
except:
print(0)
return
abs_error = abs(submission - correct)
denominator = max(1.0, abs(correct))
rel_error = abs_error / denominator
if rel_error <= 1e-9:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
523/A
|
523
|
A
|
Python 3
|
TESTS
| 1
| 46
| 0
|
20797534
|
I=input
R=range
w,h=map(int,I().split())
t=[I()for _ in R(h)]
for r in[[t[i][j]*2for i in R(h)][::-1]for j in R(w)]:s=''.join(r);print(s+'\n'+s)
| 24
| 46
| 0
|
10275176
|
w, h = map(int, input().split())
s = []
for i in range(h):
s.append(input())
a = []
for i in range(w):
a.append([0] * h)
for i in range(h):
for j in range(w):
a[j][i] = s[i][j]
for i in range(w):
st = ''
for c in a[i]:
st += c * 2
print(st)
print(st)
|
VK Cup 2015 - Qualification Round 2
|
CF
| 2,015
| 2
| 256
|
Rotate, Flip and Zoom
|
Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.
He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).
Implement this feature to help Polycarp test his editor.
|
The first line contains two integers, w and h (1 ≤ w, h ≤ 100) — the width and height of an image in pixels. The picture is given in h lines, each line contains w characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters "." and "*", as the image is monochrome.
|
Print 2w lines, each containing 2h characters — the result of consecutive implementing of the three transformations, described above.
| null | null |
[{"input": "3 2\n.*.\n.*.", "output": "....\n....\n****\n****\n....\n...."}, {"input": "9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"}]
| 1,200
|
["*special", "implementation"]
| 24
|
[{"input": "3 2\r\n.*.\r\n.*.\r\n", "output": "....\r\n....\r\n****\r\n****\r\n....\r\n....\r\n"}, {"input": "9 20\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*.......*\r\n", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********\n"}, {"input": "1 100\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.\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.\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.\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.\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.\r\n", "output": "........................................................................................................................................................................................................\r\n........................................................................................................................................................................................................\r\n"}, {"input": "1 100\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*\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*\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*\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*\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*\r\n", "output": "********************************************************************************************************************************************************************************************************\r\n********************************************************************************************************************************************************************************************************\r\n"}, {"input": "1 100\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.\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.\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*\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*\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.\r\n", "output": "..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n"}, {"input": "100 1\r\n....................................................................................................\r\n", "output": "..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n"}, {"input": "100 1\r\n****************************************************************************************************\r\n", "output": "**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n"}, {"input": "100 1\r\n*...***.....**.*...*.*.**.************.**..**.*..**..**.*.**...***.*...*.*..*.*.*......**..*..*...**\r\n", "output": "**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n"}, {"input": "1 1\r\n.\r\n", "output": "..\r\n..\r\n"}, {"input": "1 1\r\n*\r\n", "output": "**\r\n**\r\n"}, {"input": "2 2\r\n.*\r\n*.\r\n", "output": "..**\r\n..**\r\n**..\r\n**..\r\n"}, {"input": "1 2\r\n*\r\n.\r\n", "output": "**..\r\n**..\r\n"}, {"input": "2 1\r\n*.\r\n", "output": "**\r\n**\r\n..\r\n..\r\n"}]
| false
|
stdio
| null | true
|
859/G
|
859
|
G
|
PyPy 3-64
|
TESTS
| 2
| 46
| 512,000
|
132474480
|
from sys import stdin
inp = stdin.readline
def divisors(x):
ans = []
u = x
for i in range(2, u+1):
if x//i == x/i:
ans.append(i)
while x//i == x/i:
x //= i
if x == 1:
break
return ans
n = int(inp())
s = inp().strip()
div = divisors(n)
rest = n//div[0]
arr = [int(c) if i % div[0] else 50 for i, c in enumerate(s)]
for i in range(rest):
for j in range(1, div[0]):
arr[(div[0]*i + rest*j) % n] -= int(s[i*div[0]])
for i in range(n-1, -1, -1):
if arr[i] > 30:
arr.pop(i)
for d in div[1:]:
rest = len(arr) // d
if d != div[len(div)-1]:
for i in range(rest):
for j in range(1, d):
arr[(i * d + j * rest) % len(arr)] -= arr[i * d]
u = len(arr)
for i in range(u-d, -1, -d):
arr.pop(i)
else:
done = 0
for i in range(rest):
for j in range(d):
if arr[i] != arr[i + j*rest]:
print("NO")
done = 1
break
if not done:
print("YES")
| 70
| 93
| 6,144,000
|
227486527
|
def factor(n):
ps = []
for i in range(2, n):
if i * i > n:
break
if n % i == 0:
e = 0
while n % i == 0:
e += 1
n //= i
ps += [(i, e)]
if n > 1:
ps += [(n, 1)]
return ps
n = int(input())
a = list(map(int, input()))
ps = factor(n)
P = 1
for p, _ in ps:
P *= p
Q = n // P
for p, _ in ps:
k = (1 - pow(P // p, -1, p) * (P // p)) % P
for r in range(Q):
for i in range(0, P, p):
for j in range(1, p):
a[r + (i + j) * Q] -= a[r + ((i + j * k) % P) * Q]
for i in range(0, P, p):
a[r + i * Q] = 0
print("YES" if all(x == 0 for x in a) else "NO")
|
MemSQL Start[c]UP 3.0 - Round 1
|
CF
| 2,017
| 1
| 256
|
Circle of Numbers
|
n evenly spaced points have been marked around the edge of a circle. There is a number written at each point. You choose a positive real number k. Then you may repeatedly select a set of 2 or more points which are evenly spaced, and either increase all numbers at points in the set by k or decrease all numbers at points in the set by k. You would like to eventually end up with all numbers equal to 0. Is it possible?
A set of 2 points is considered evenly spaced if they are diametrically opposed, and a set of 3 or more points is considered evenly spaced if they form a regular polygon.
|
The first line of input contains an integer n (3 ≤ n ≤ 100000), the number of points along the circle.
The following line contains a string s with exactly n digits, indicating the numbers initially present at each of the points, in clockwise order.
|
Print "YES" (without quotes) if there is some sequence of operations that results in all numbers being 0, otherwise "NO" (without quotes).
You can print each letter in any case (upper or lower).
| null |
If we label the points from 1 to n, then for the first test case we can set k = 1. Then we increase the numbers at points 7 and 22 by 1, then decrease the numbers at points 7, 17, and 27 by 1, then decrease the numbers at points 4, 10, 16, 22, and 28 by 1.
|
[{"input": "30\n000100000100000110000000001100", "output": "YES"}, {"input": "6\n314159", "output": "NO"}]
| 3,000
|
["math"]
| 70
|
[{"input": "30\r\n000100000100000110000000001100\r\n", "output": "YES\r\n"}, {"input": "6\r\n314159\r\n", "output": "NO\r\n"}, {"input": "3\r\n000\r\n", "output": "YES\r\n"}, {"input": "15\r\n522085220852208\r\n", "output": "YES\r\n"}, {"input": "300\r\n518499551238825328417663140237955446550596254299485115465325550413577584420893115025535675971808926451691055930219585997807344070011845434733526017118933548589759649920016568578201769564228210045739230664506968281414229830885120812000132083367912869773547090902954497697057079934454847714732943455588\r\n", "output": "NO\r\n"}, {"input": "16\r\n0110100110010110\r\n", "output": "NO\r\n"}, {"input": "6\r\n564837\r\n", "output": "YES\r\n"}, {"input": "9\r\n975975975\r\n", "output": "YES\r\n"}, {"input": "10\r\n0414240506\r\n", "output": "YES\r\n"}, {"input": "33\r\n459847811604598478116045984781160\r\n", "output": "YES\r\n"}, {"input": "54\r\n668822125317092836839407462257441103728473858428026440\r\n", "output": "YES\r\n"}, {"input": "147\r\n258714573455458660598376355525847569123841578643535970557335666766056747734652574877812381067963453587176633563586704774763586157484691337206786466\r\n", "output": "YES\r\n"}, {"input": "171\r\n570962255153238631524151841322466383830411184871666785642671862254254138630625051840423366382931311183972566784743571861355154137731525050941323365483831310284872565885643\r\n", "output": "YES\r\n"}, {"input": "256\r\n0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110100110010110100101100110100110010110011010010110100110010110011010011001011010010110011010010110100110010110100101100110100110010110011010010110100110010110\r\n", "output": "NO\r\n"}, {"input": "5\r\n66666\r\n", "output": "YES\r\n"}, {"input": "9\r\n735358934\r\n", "output": "NO\r\n"}, {"input": "18\r\n008697913853945708\r\n", "output": "NO\r\n"}, {"input": "32\r\n63227607169607744763319434680883\r\n", "output": "NO\r\n"}, {"input": "55\r\n3051191835797699222829630849704721747109367868763178250\r\n", "output": "NO\r\n"}, {"input": "124\r\n4087853659974995340862097721504457205060797709029986511524903183441846431857346142268549624240693503777827416946735887323115\r\n", "output": "NO\r\n"}, {"input": "191\r\n13749871615832352185918872672752947110954476837054206370341509698888491814630432290010512405256834335260576808366797121146435132126443422459679991449630693949793303863445042394342711401003212\r\n", "output": "NO\r\n"}, {"input": "488\r\n45334958248886256215245432812097462402188584275950565932256169435872815051620433766475524373331668138254775135319898212744118387896269009559223646980708720716169171410452756878161887568810441524371550690928477055425661664202050311645854048922622038301574832340035693673614973249646168547494526620934733803262859266238709131053471167257719634461068324341221207027892565402615143073772528401151209845684767396920588208903922337152277526342947329398157696709654980477214668927347471119604451\r\n", "output": "NO\r\n"}, {"input": "3\r\n007\r\n", "output": "NO\r\n"}, {"input": "4\r\n0500\r\n", "output": "NO\r\n"}, {"input": "6\r\n700000\r\n", "output": "NO\r\n"}]
| false
|
stdio
| null | true
|
22/E
|
22
|
E
|
Python 3
|
TESTS
| 9
| 60
| 204,800
|
212313098
|
from sys import stdin
class UnionFind:
def __init__(self, size:int):
self.data = list(range(size))
def find(self, x:int)->int:
if self.data[x] == x:
return x
self.data[x] = self.find(self.data[x])
return self.data[x]
def union(self, x:int, y:int):
x, y = self.find(x), self.find(y)
if x != y:
self.data[x] = y
N = int(input())
diag = list(map(lambda x: int(x) - 1, stdin.readline().split()))
degrees = [0] * N
uf = UnionFind(N)
for i in range(N):
uf.union(i, diag[i])
degrees[diag[i]] += 1
cores = [[] for _ in range(N)]
leaves = [i for i in range(N) if degrees[i] == 0]
start = leaves[:]
while start:
node = start.pop()
child = diag[node]
degrees[child] -= 1
if degrees[child] == 0:
start.append(child)
for i in range(N):
if degrees[i] > 0:
cores[uf.find(i)].append(i)
result = []
for node in leaves:
v = cores[uf.find(node)][-1]
result.append([v, node])
cores_nodes = [core[-1] for core in cores if core]
for i in range(1, len(cores_nodes)):
result.append([cores_nodes[i -1], cores_nodes[i]])
if len(cores_nodes) > 1:
result.append([cores_nodes[-1], cores_nodes[0]])
print(len(result))
for a, b in result:
print(a + 1, b + 1)
# print(cores)
# print(leaves)
# if len(circles)
| 40
| 592
| 12,800,000
|
98755646
|
n = int(input())
g = [int(i) - 1 for i in input().split()]
def solve(n, g):
vertex_id = [-1]*n
current_id = 0
starts_a_cycle = [-1]*n
cycle_vertex_sample = []
start_on_cycle = []
cycle_index_by_ID = []
cycle_count = 0
for v in range(n):
if vertex_id[v] != -1: continue
current_vertex = v
cycle_found = False
while vertex_id[current_vertex] == -1:
vertex_id[current_vertex] = current_id
current_vertex = g[current_vertex]
if vertex_id[current_vertex] == current_id:
cycle_found = True
cycle_count += 1
cycle_vertex_sample.append(current_vertex)
start_on_cycle.append(current_vertex == v)
if not cycle_found:
if starts_a_cycle[current_vertex] != -1:
starts_a_cycle[v] = starts_a_cycle[current_vertex]
starts_a_cycle[current_vertex] = -1
cycle_index_by_ID.append(cycle_index_by_ID[vertex_id[current_vertex]])
cycle = cycle_index_by_ID[-1]
if start_on_cycle[cycle]:
starts_a_cycle[cycle_vertex_sample[cycle]] = -1
start_on_cycle[cycle] = False
starts_a_cycle[v] = cycle
else:
starts_a_cycle[v] = cycle_count - 1
cycle_index_by_ID.append(cycle_count - 1)
current_id += 1
expanded = []
for i in range(n):
if starts_a_cycle[i] != -1:
cycle = starts_a_cycle[i]
v = cycle_vertex_sample[cycle]
expanded.append([v, i])
if len(expanded) == 1 and expanded[0][0] == expanded[0][1]:
print(0)
return
print(len(expanded))
for i in range(len(expanded)-1):
v, u = expanded[i][0], expanded[i+1][1]
print('{0} {1}'.format(v+1, u+1))
print('{0} {1}'.format(expanded[-1][0] + 1, expanded[0][1] + 1))
solve(n, g)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Scheme
|
To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?
|
The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.
|
In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.
| null | null |
[{"input": "3\n3 3 2", "output": "1\n3 1"}, {"input": "7\n2 3 1 3 4 4 1", "output": "3\n2 5\n2 6\n3 7"}]
| 2,300
|
["dfs and similar", "graphs", "trees"]
| 40
|
[{"input": "3\r\n3 3 2\r\n", "output": "1\r\n3 1\r\n"}, {"input": "7\r\n2 3 1 3 4 4 1\r\n", "output": "3\r\n1 5\r\n1 6\r\n1 7\r\n"}, {"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "4\r\n2 4 4 3\r\n", "output": "1\r\n4 1\r\n"}, {"input": "5\r\n5 3 5 2 3\r\n", "output": "2\r\n5 1\r\n5 4\r\n"}, {"input": "9\r\n2 5 6 7 4 1 9 6 8\r\n", "output": "1\r\n1 3\r\n"}, {"input": "20\r\n20 10 16 14 9 20 6 20 14 19 17 13 16 13 14 8 8 8 8 19\r\n", "output": "10\r\n20 1\r\n20 2\r\n20 3\r\n20 4\r\n20 5\r\n20 7\r\n20 11\r\n20 12\r\n20 15\r\n20 18\r\n"}, {"input": "7\r\n3 1 2 5 6 7 4\r\n", "output": "2\r\n1 4\r\n4 1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
f_list = list(map(int, f.readline().split()))
# Read submission output
with open(submission_output_path) as f:
try:
k_line = f.readline().strip()
if not k_line:
print(0)
return
k = int(k_line)
edges = []
for _ in range(k):
parts = f.readline().split()
if len(parts) != 2:
print(0)
return
xi = int(parts[0])
yi = int(parts[1])
if not (1 <= xi <= n and 1 <= yi <= n):
print(0)
return
edges.append( (xi, yi) )
except (ValueError, IndexError) as e:
print(0)
return
# Find components in original graph
visited = [False] * (n + 1)
components = []
node_to_component = {}
for i in range(1, n+1):
if not visited[i]:
path = []
current = i
while True:
if visited[current]:
if current in path:
idx = path.index(current)
cycle = path[idx:]
# Add cycle component
components.append( (True, cycle) )
comp_idx = len(components) - 1
for node in cycle:
visited[node] = True
node_to_component[node] = comp_idx
# Add non-cycle part as singleton components
for node in path[:idx]:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
else:
# The path is part of another component, add as singletons
for node in path:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
break
visited[current] = True
path.append(current)
current = f_list[current - 1] # f_list is 1-based
# Compute component_incoming
component_incoming = [0] * len(components)
for v in range(1, n+1):
comp_v = node_to_component[v]
f_v = f_list[v-1]
comp_fv = node_to_component[f_v]
if comp_v != comp_fv:
component_incoming[comp_fv] += 1
# Compute S and C
S = sum(1 for i in range(len(components)) if component_incoming[i] == 0)
C = sum(1 for is_cycle, _ in components if is_cycle)
if len(components) == 1:
minimal_k = 0
else:
minimal_k = max(S, C)
# Check K
if k != minimal_k:
print(0)
return
# Build the modified graph
adj = defaultdict(list)
for u in range(1, n+1):
original = f_list[u-1]
adj[u].append(original)
for xi, yi in edges:
adj[xi].append(yi)
# Check if the graph is strongly connected using Kosaraju's algorithm
visited = [False] * (n + 1)
order = []
def dfs1(u):
stack = [(u, False)]
while stack:
node, processed = stack.pop()
if processed:
order.append(node)
continue
if visited[node]:
continue
visited[node] = True
stack.append( (node, True) )
for v in adj[node]:
if not visited[v]:
stack.append( (v, False) )
for u in range(1, n+1):
if not visited[u]:
dfs1(u)
visited = [False] * (n + 1)
reversed_adj = defaultdict(list)
for u in adj:
for v in adj[u]:
reversed_adj[v].append(u)
count = 0
for u in reversed(order):
if not visited[u]:
count += 1
if count > 1:
print(0)
return
stack = [u]
visited[u] = True
while stack:
node = stack.pop()
for v in reversed_adj[node]:
if not visited[v]:
visited[v] = True
stack.append(v)
if count == 1:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
523/A
|
523
|
A
|
PyPy 3
|
TESTS
| 1
| 93
| 0
|
10299918
|
def main():
w,h = [int(i) for i in input().split()]
p = []
for i in range(h):
s = input()
p.append(s)
p = zip(*p)
for s in p:
print("".join(s)*2)
print("".join(s)*2)
main()
| 24
| 46
| 0
|
144008423
|
w, h = map(int, input().split())
li = []
for i in range(h):
temp = list(input())
li.append(temp)
for i in (range(w)):
temp = ''
for j in (range(h)):
temp += 2*li[j][i]
print(temp)
print(temp)
|
VK Cup 2015 - Qualification Round 2
|
CF
| 2,015
| 2
| 256
|
Rotate, Flip and Zoom
|
Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.
He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).
Implement this feature to help Polycarp test his editor.
|
The first line contains two integers, w and h (1 ≤ w, h ≤ 100) — the width and height of an image in pixels. The picture is given in h lines, each line contains w characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters "." and "*", as the image is monochrome.
|
Print 2w lines, each containing 2h characters — the result of consecutive implementing of the three transformations, described above.
| null | null |
[{"input": "3 2\n.*.\n.*.", "output": "....\n....\n****\n****\n....\n...."}, {"input": "9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"}]
| 1,200
|
["*special", "implementation"]
| 24
|
[{"input": "3 2\r\n.*.\r\n.*.\r\n", "output": "....\r\n....\r\n****\r\n****\r\n....\r\n....\r\n"}, {"input": "9 20\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*.......*\r\n", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********\n"}, {"input": "1 100\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.\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.\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.\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.\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.\r\n", "output": "........................................................................................................................................................................................................\r\n........................................................................................................................................................................................................\r\n"}, {"input": "1 100\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*\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*\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*\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*\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*\r\n", "output": "********************************************************************************************************************************************************************************************************\r\n********************************************************************************************************************************************************************************************************\r\n"}, {"input": "1 100\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.\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.\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*\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*\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.\r\n", "output": "..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n"}, {"input": "100 1\r\n....................................................................................................\r\n", "output": "..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n"}, {"input": "100 1\r\n****************************************************************************************************\r\n", "output": "**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n"}, {"input": "100 1\r\n*...***.....**.*...*.*.**.************.**..**.*..**..**.*.**...***.*...*.*..*.*.*......**..*..*...**\r\n", "output": "**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n"}, {"input": "1 1\r\n.\r\n", "output": "..\r\n..\r\n"}, {"input": "1 1\r\n*\r\n", "output": "**\r\n**\r\n"}, {"input": "2 2\r\n.*\r\n*.\r\n", "output": "..**\r\n..**\r\n**..\r\n**..\r\n"}, {"input": "1 2\r\n*\r\n.\r\n", "output": "**..\r\n**..\r\n"}, {"input": "2 1\r\n*.\r\n", "output": "**\r\n**\r\n..\r\n..\r\n"}]
| false
|
stdio
| null | true
|
523/A
|
523
|
A
|
Python 3
|
PRETESTS
| 1
| 46
| 0
|
10274372
|
w, h = map(int, input().split())
listGorizontal = []
listVertical = []
for i in range(h):
listGorizontal.append(input())
for i in range(h):
s = ""
for j in listGorizontal[i]:
s += j*2
listVertical.append(s)
s = ""
k = 1
for i in range(2*w):
for j in range(h):
s += 2 * listVertical[h - k][i]
k += 1
k = 1
if i != 2*w :
s += "\n"
print(s)
| 24
| 46
| 102,400
|
10275054
|
a, b = [int(i) for i in input().split()]
matrix_in = [[0 for i in range(a)] for j in range(b)]
for i in range(b):
line = input()
for j in range(a):
matrix_in[i][j] = line[j]
# Right rotate and flip
matrix_rotated = [[0 for i in range(b)] for j in range(a)]
for i in range(a):
for j in range(b):
matrix_rotated[i][j] = matrix_in[j][i]
# Print
for i in range(a):
toprint=""
for j in range(b):
toprint += matrix_rotated[i][j]*2
print(toprint)
print(toprint)
|
VK Cup 2015 - Qualification Round 2
|
CF
| 2,015
| 2
| 256
|
Rotate, Flip and Zoom
|
Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.
He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).
Implement this feature to help Polycarp test his editor.
|
The first line contains two integers, w and h (1 ≤ w, h ≤ 100) — the width and height of an image in pixels. The picture is given in h lines, each line contains w characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters "." and "*", as the image is monochrome.
|
Print 2w lines, each containing 2h characters — the result of consecutive implementing of the three transformations, described above.
| null | null |
[{"input": "3 2\n.*.\n.*.", "output": "....\n....\n****\n****\n....\n...."}, {"input": "9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"}]
| 1,200
|
["*special", "implementation"]
| 24
|
[{"input": "3 2\r\n.*.\r\n.*.\r\n", "output": "....\r\n....\r\n****\r\n****\r\n....\r\n....\r\n"}, {"input": "9 20\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*.......*\r\n", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********\n"}, {"input": "1 100\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.\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.\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.\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.\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.\r\n", "output": "........................................................................................................................................................................................................\r\n........................................................................................................................................................................................................\r\n"}, {"input": "1 100\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*\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*\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*\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*\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*\r\n", "output": "********************************************************************************************************************************************************************************************************\r\n********************************************************************************************************************************************************************************************************\r\n"}, {"input": "1 100\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.\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.\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*\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*\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.\r\n", "output": "..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n"}, {"input": "100 1\r\n....................................................................................................\r\n", "output": "..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n"}, {"input": "100 1\r\n****************************************************************************************************\r\n", "output": "**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n"}, {"input": "100 1\r\n*...***.....**.*...*.*.**.************.**..**.*..**..**.*.**...***.*...*.*..*.*.*......**..*..*...**\r\n", "output": "**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n"}, {"input": "1 1\r\n.\r\n", "output": "..\r\n..\r\n"}, {"input": "1 1\r\n*\r\n", "output": "**\r\n**\r\n"}, {"input": "2 2\r\n.*\r\n*.\r\n", "output": "..**\r\n..**\r\n**..\r\n**..\r\n"}, {"input": "1 2\r\n*\r\n.\r\n", "output": "**..\r\n**..\r\n"}, {"input": "2 1\r\n*.\r\n", "output": "**\r\n**\r\n..\r\n..\r\n"}]
| false
|
stdio
| null | true
|
523/A
|
523
|
A
|
Python 3
|
TESTS
| 1
| 61
| 0
|
16471976
|
w, h = map(int, input().split())
help = [input() for i in range(h)]
data = [[None for i in range(h)] for j in range(w)]
for i in range(w):
for j in range(h):
data[i][j] = help[j][i]
for i in range(w):
data[i] = data[i] + data[i][::-1]
for i in range(w):
a = ''
for j in range(len(data[i])):
a += data[i][j]
print(a)
print(a)
| 24
| 46
| 204,800
|
192440188
|
# LUOGU_RID: 101574102
m, n = map(int,input().split())
t = [input()for _ in range(n)]
for r in [[t[i][j] * 2 for i in range(n)] for j in range(m)]:
s=''.join(r);
print(s + '\n' + s)
|
VK Cup 2015 - Qualification Round 2
|
CF
| 2,015
| 2
| 256
|
Rotate, Flip and Zoom
|
Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.
He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).
Implement this feature to help Polycarp test his editor.
|
The first line contains two integers, w and h (1 ≤ w, h ≤ 100) — the width and height of an image in pixels. The picture is given in h lines, each line contains w characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters "." and "*", as the image is monochrome.
|
Print 2w lines, each containing 2h characters — the result of consecutive implementing of the three transformations, described above.
| null | null |
[{"input": "3 2\n.*.\n.*.", "output": "....\n....\n****\n****\n....\n...."}, {"input": "9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"}]
| 1,200
|
["*special", "implementation"]
| 24
|
[{"input": "3 2\r\n.*.\r\n.*.\r\n", "output": "....\r\n....\r\n****\r\n****\r\n....\r\n....\r\n"}, {"input": "9 20\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*.......*\r\n", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********\n"}, {"input": "1 100\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.\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.\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.\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.\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.\r\n", "output": "........................................................................................................................................................................................................\r\n........................................................................................................................................................................................................\r\n"}, {"input": "1 100\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*\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*\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*\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*\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*\r\n", "output": "********************************************************************************************************************************************************************************************************\r\n********************************************************************************************************************************************************************************************************\r\n"}, {"input": "1 100\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.\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.\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*\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*\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.\r\n", "output": "..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n"}, {"input": "100 1\r\n....................................................................................................\r\n", "output": "..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n"}, {"input": "100 1\r\n****************************************************************************************************\r\n", "output": "**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n"}, {"input": "100 1\r\n*...***.....**.*...*.*.**.************.**..**.*..**..**.*.**...***.*...*.*..*.*.*......**..*..*...**\r\n", "output": "**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n"}, {"input": "1 1\r\n.\r\n", "output": "..\r\n..\r\n"}, {"input": "1 1\r\n*\r\n", "output": "**\r\n**\r\n"}, {"input": "2 2\r\n.*\r\n*.\r\n", "output": "..**\r\n..**\r\n**..\r\n**..\r\n"}, {"input": "1 2\r\n*\r\n.\r\n", "output": "**..\r\n**..\r\n"}, {"input": "2 1\r\n*.\r\n", "output": "**\r\n**\r\n..\r\n..\r\n"}]
| false
|
stdio
| null | true
|
216/E
|
216
|
E
|
Python 3
|
TESTS
| 4
| 92
| 0
|
11926259
|
k, b, n = map(int, input().split())
digits = list(map(int, input().split()))
def conv(t):
if t == 0:
return k - 1
return t
if b == 0:
r = digits.count(0)
print(r * (r + 1) // 2)
else:
count = dict()
count[0] = 1
pref_sum = 0
answer = 0
for d in digits:
pref_sum = (pref_sum + d) % (k - 1)
real_val = conv(pref_sum)
need = (real_val - b) % (k - 1)
# print(pref_sum, need)
answer += count.get(need, 0)
count[pref_sum] = count.get(pref_sum, 0) + 1
if b == k - 1:
r = digits.count(0)
answer -= r * (r + 1) // 2
print(answer)
| 41
| 436
| 10,956,800
|
11926614
|
k, b, n = map(int, input().split())
digits = list(map(int, input().split()))
def ans0():
j = -1
answer = 0
for i in range(n):
if digits[i] != 0 or i < j:
continue
j = i
while j < n and digits[j] == 0:
j += 1
r = j - i
answer += r * (r + 1) // 2
return answer
if b == 0:
print(ans0())
else:
count = dict()
count[0] = 1
pref_sum = 0
answer = 0
if b == k - 1:
b = 0
answer -= ans0()
for d in digits:
pref_sum = (pref_sum + d) % (k - 1)
need = (pref_sum - b) % (k - 1)
answer += count.get(need, 0)
count[pref_sum] = count.get(pref_sum, 0) + 1
print(answer)
|
Codeforces Round 133 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Martian Luck
|
You know that the Martians use a number system with base k. Digit b (0 ≤ b < k) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year b (by Martian chronology).
A digital root d(x) of number x is a number that consists of a single digit, resulting after cascading summing of all digits of number x. Word "cascading" means that if the first summing gives us a number that consists of several digits, then we sum up all digits again, and again, until we get a one digit number.
For example, d(35047) = d((3 + 5 + 0 + 4)7) = d(157) = d((1 + 5)7) = d(67) = 67. In this sample the calculations are performed in the 7-base notation.
If a number's digital root equals b, the Martians also call this number lucky.
You have string s, which consists of n digits in the k-base notation system. Your task is to find, how many distinct substrings of the given string are lucky numbers. Leading zeroes are permitted in the numbers.
Note that substring s[i... j] of the string s = a1a2... an (1 ≤ i ≤ j ≤ n) is the string aiai + 1... aj. Two substrings s[i1... j1] and s[i2... j2] of the string s are different if either i1 ≠ i2 or j1 ≠ j2.
|
The first line contains three integers k, b and n (2 ≤ k ≤ 109, 0 ≤ b < k, 1 ≤ n ≤ 105).
The second line contains string s as a sequence of n integers, representing digits in the k-base notation: the i-th integer equals ai (0 ≤ ai < k) — the i-th digit of string s. The numbers in the lines are space-separated.
|
Print a single integer — the number of substrings that are lucky numbers.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample the following substrings have the sought digital root: s[1... 2] = "3 2", s[1... 3] = "3 2 0", s[3... 4] = "0 5", s[4... 4] = "5" and s[2... 6] = "2 0 5 6 1".
|
[{"input": "10 5 6\n3 2 0 5 6 1", "output": "5"}, {"input": "7 6 4\n3 5 0 4", "output": "1"}, {"input": "257 0 3\n0 0 256", "output": "3"}]
| 2,000
|
["math", "number theory"]
| 41
|
[{"input": "10 5 6\r\n3 2 0 5 6 1\r\n", "output": "5"}, {"input": "7 6 4\r\n3 5 0 4\r\n", "output": "1"}, {"input": "257 0 3\r\n0 0 256\r\n", "output": "3"}, {"input": "2 1 1\r\n0\r\n", "output": "0"}, {"input": "2 0 20\r\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1\r\n", "output": "22"}, {"input": "100 29 33\r\n28 89 23 14 97 97 66 56 55 60 47 29 9 79 26 80 63 78 83 60 41 29 52 10 82 26 47 88 99 75 52 1 31\r\n", "output": "10"}, {"input": "3 2 100\r\n2 0 0 0 0 2 1 1 1 2 0 1 1 1 1 2 0 0 1 0 1 1 2 0 2 0 1 0 1 0 0 2 0 0 0 1 2 0 2 2 0 2 0 2 1 0 1 1 1 1 2 0 0 0 1 0 2 0 2 0 2 1 2 2 1 1 0 1 1 2 1 1 0 0 1 1 2 2 1 2 2 0 1 2 2 1 2 2 0 2 0 2 2 0 2 2 1 2 0 0\r\n", "output": "2451"}, {"input": "5 4 102\r\n3 2 2 3 3 2 2 0 3 1 2 4 0 1 3 4 3 2 3 0 4 1 0 0 0 0 4 4 1 2 3 3 4 0 1 2 2 3 3 1 3 1 0 0 3 0 4 0 2 4 2 3 0 1 4 3 0 2 3 3 2 2 1 0 1 3 0 3 4 4 4 1 0 1 2 1 4 2 4 4 4 4 4 2 3 3 0 3 0 0 0 4 1 3 0 4 2 1 2 0 3 0\r\n", "output": "1293"}, {"input": "7 4 104\r\n4 3 0 6 6 5 3 4 4 5 0 1 2 5 5 1 3 4 1 5 3 5 4 4 2 4 3 5 4 2 2 3 1 1 0 5 4 3 2 5 2 1 3 1 6 4 1 3 0 2 5 2 5 3 3 6 1 2 2 2 4 5 6 0 5 4 5 3 5 3 4 3 1 0 2 4 5 5 5 5 3 3 6 1 6 1 3 6 6 5 3 3 1 3 2 0 4 4 3 3 4 0 5 6\r\n", "output": "938"}, {"input": "20 15 1\r\n6\r\n", "output": "0"}, {"input": "20 19 2\r\n16 13\r\n", "output": "0"}, {"input": "30 24 30\r\n7 24 3 20 8 24 0 6 15 22 20 21 16 26 28 6 6 28 19 2 12 22 6 12 15 17 24 13 12 16\r\n", "output": "20"}, {"input": "2 0 100\r\n0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "331"}]
| false
|
stdio
| null | true
|
22/E
|
22
|
E
|
PyPy 3
|
TESTS
| 8
| 278
| 0
|
95000968
|
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
a = [0] + list(map(int, input().split()))
rev = [[] for _ in range(n + 1)]
indeg = [0] * (n + 1)
for i in range(1, n + 1):
indeg[a[i]] += 1
rev[a[i]].append(i)
_indeg = indeg[:]
for i in range(1, n + 1):
v = i
while indeg[v] == 0:
indeg[a[v]] -= 1
v = a[v]
ans = []
group = [0] * (n + 1)
for i in range(1, n + 1):
if group[i] or indeg[i] == 0:
continue
group[i] = i
stack = [i]
while stack:
v = stack.pop()
if _indeg[v] == 0:
ans.append(f'{i} {v}')
for dest in rev[v]:
if not group[dest]:
group[dest] = i
stack.append(dest)
loops = list(set(group[1:]))
for i in range(len(loops) - 1):
for j in range(i + 1, len(loops)):
ans.append(f'{loops[i]} {loops[j]}')
ans.append(f'{loops[j]} {loops[i]}')
ans_str = str(len(ans)) + '\n' + '\n'.join(ans)
sys.stdout.buffer.write(ans_str.encode('utf-8'))
| 40
| 1,714
| 90,931,200
|
157748457
|
order = [] # Psuedo-topological ordering (we don't care about cycles)
visited = set()
next_id = 0
nodes = {} # Maps from node to component id
components = [] # Maps from component id to a set of nodes
def dfs3(start, get_neighbors):
'''
Iterative implementation of DFS. Doing DFS with recrusion in Python seems
to cause TLE errors a decent amount of the time
'''
q = [(start, False)]
visited.add(start)
while len(q) > 0:
node, indicator = q.pop()
if indicator:
order.append(node)
continue
q.append((node, True))
for neighbor in get_neighbors(node):
if neighbor not in visited:
visited.add(neighbor)
q.append((neighbor, False))
def dfs1(start, get_neighbors):
'''
First round of DFS performs a reverse topological sort to order the nodes
by decreasing time of exit
'''
visited.add(start)
for neighbor in get_neighbors(start):
if neighbor not in visited:
dfs1(neighbor, get_neighbors)
order.append(start)
def dfs2(start, get_rev_neighbors):
'''
Second round of DFS finds connected components in the reversed graph.
Performing this by starting at the node with the highest exit time produces
strongly connected components
'''
q = [start]
nodes[start] = next_id
components[next_id].add(start)
while len(q) > 0:
node = q.pop()
for neighbor in get_rev_neighbors(node):
if neighbor not in nodes:
nodes[neighbor] = next_id
components[next_id].add(neighbor)
q.append(neighbor)
def find_components(all_nodes, get_neighbors, get_rev_neighbors):
'''
Note: the condensation graph is a set of acyclic graphs
'''
global next_id, order
for node in all_nodes:
if node not in visited:
dfs3(node, get_neighbors)
order = order[::-1]
for node in order:
if node not in nodes:
components.append(set())
dfs2(node, get_rev_neighbors)
next_id += 1
cond_graph = [[] for i in range(len(components))]
for node in all_nodes:
for neighbor in get_neighbors(node):
first_comp, second_comp = nodes[node], nodes[neighbor]
# If we find a node and neighbor pair in different components, then
# we found an edge in the condensation graph
if first_comp != second_comp:
cond_graph[first_comp].append(second_comp)
return cond_graph
# Example use. This TLEs on the following, but the core algorithm should be correct
# https://codeforces.com/contest/22/problem/E
def set_peek(s):
val = s.pop()
s.add(val)
return val
n = int(input())
graph = [None]
graph_rev = [[] for i in range(n + 1)]
for i, num in enumerate(map(int, input().split())):
graph.append([num])
graph_rev[num].append(i + 1)
# Step 1: build the condensation graph to get an acyclic graph
cond_graph = find_components(range(1, n + 1), lambda x: graph[x], lambda x: graph_rev[x])
# Step 2: identify all the "roots" in this graph as those with no neighbors
roots = [i for i in range(len(cond_graph)) if len(cond_graph[i]) == 0]
# Step 3: reverse the condensation graph to form a tree
cond_graph_rev = [[] for _ in range(len(cond_graph))]
for i, neighbors in enumerate(cond_graph):
for neighbor in neighbors:
cond_graph_rev[neighbor].append(i)
# Step 4: Use DFS to find the leaves of each tree
leaves = []
for root in roots:
these_leaves = []
q = [root]
while len(q) > 0:
node = q.pop()
# Note that a root can be its own leaf
if len(cond_graph_rev[node]) == 0:
these_leaves.append(node)
continue
for neighbor in cond_graph_rev[node]:
q.append(neighbor)
leaves.append(these_leaves)
# Step 5: connect each root to leaves of the next tree
ans = []
if len(roots) >= 2:
for i in range(len(roots)):
root = roots[i]
root_rep = set_peek(components[root])
ls = leaves[(i + 1) % len(roots)]
for l in ls:
l_rep = set_peek(components[l])
ans.append((root_rep, l_rep))
else:
root_rep = set_peek(components[roots[0]])
ls = leaves[0]
for l in ls:
if l == root: continue
l_rep = set_peek(components[l])
ans.append((root_rep, l_rep))
print(len(ans))
for a, b in ans:
print(a, b)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Scheme
|
To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?
|
The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.
|
In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.
| null | null |
[{"input": "3\n3 3 2", "output": "1\n3 1"}, {"input": "7\n2 3 1 3 4 4 1", "output": "3\n2 5\n2 6\n3 7"}]
| 2,300
|
["dfs and similar", "graphs", "trees"]
| 40
|
[{"input": "3\r\n3 3 2\r\n", "output": "1\r\n3 1\r\n"}, {"input": "7\r\n2 3 1 3 4 4 1\r\n", "output": "3\r\n1 5\r\n1 6\r\n1 7\r\n"}, {"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "4\r\n2 4 4 3\r\n", "output": "1\r\n4 1\r\n"}, {"input": "5\r\n5 3 5 2 3\r\n", "output": "2\r\n5 1\r\n5 4\r\n"}, {"input": "9\r\n2 5 6 7 4 1 9 6 8\r\n", "output": "1\r\n1 3\r\n"}, {"input": "20\r\n20 10 16 14 9 20 6 20 14 19 17 13 16 13 14 8 8 8 8 19\r\n", "output": "10\r\n20 1\r\n20 2\r\n20 3\r\n20 4\r\n20 5\r\n20 7\r\n20 11\r\n20 12\r\n20 15\r\n20 18\r\n"}, {"input": "7\r\n3 1 2 5 6 7 4\r\n", "output": "2\r\n1 4\r\n4 1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
f_list = list(map(int, f.readline().split()))
# Read submission output
with open(submission_output_path) as f:
try:
k_line = f.readline().strip()
if not k_line:
print(0)
return
k = int(k_line)
edges = []
for _ in range(k):
parts = f.readline().split()
if len(parts) != 2:
print(0)
return
xi = int(parts[0])
yi = int(parts[1])
if not (1 <= xi <= n and 1 <= yi <= n):
print(0)
return
edges.append( (xi, yi) )
except (ValueError, IndexError) as e:
print(0)
return
# Find components in original graph
visited = [False] * (n + 1)
components = []
node_to_component = {}
for i in range(1, n+1):
if not visited[i]:
path = []
current = i
while True:
if visited[current]:
if current in path:
idx = path.index(current)
cycle = path[idx:]
# Add cycle component
components.append( (True, cycle) )
comp_idx = len(components) - 1
for node in cycle:
visited[node] = True
node_to_component[node] = comp_idx
# Add non-cycle part as singleton components
for node in path[:idx]:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
else:
# The path is part of another component, add as singletons
for node in path:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
break
visited[current] = True
path.append(current)
current = f_list[current - 1] # f_list is 1-based
# Compute component_incoming
component_incoming = [0] * len(components)
for v in range(1, n+1):
comp_v = node_to_component[v]
f_v = f_list[v-1]
comp_fv = node_to_component[f_v]
if comp_v != comp_fv:
component_incoming[comp_fv] += 1
# Compute S and C
S = sum(1 for i in range(len(components)) if component_incoming[i] == 0)
C = sum(1 for is_cycle, _ in components if is_cycle)
if len(components) == 1:
minimal_k = 0
else:
minimal_k = max(S, C)
# Check K
if k != minimal_k:
print(0)
return
# Build the modified graph
adj = defaultdict(list)
for u in range(1, n+1):
original = f_list[u-1]
adj[u].append(original)
for xi, yi in edges:
adj[xi].append(yi)
# Check if the graph is strongly connected using Kosaraju's algorithm
visited = [False] * (n + 1)
order = []
def dfs1(u):
stack = [(u, False)]
while stack:
node, processed = stack.pop()
if processed:
order.append(node)
continue
if visited[node]:
continue
visited[node] = True
stack.append( (node, True) )
for v in adj[node]:
if not visited[v]:
stack.append( (v, False) )
for u in range(1, n+1):
if not visited[u]:
dfs1(u)
visited = [False] * (n + 1)
reversed_adj = defaultdict(list)
for u in adj:
for v in adj[u]:
reversed_adj[v].append(u)
count = 0
for u in reversed(order):
if not visited[u]:
count += 1
if count > 1:
print(0)
return
stack = [u]
visited[u] = True
while stack:
node = stack.pop()
for v in reversed_adj[node]:
if not visited[v]:
visited[v] = True
stack.append(v)
if count == 1:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
859/D
|
859
|
D
|
Python 3
|
TESTS
| 13
| 62
| 307,200
|
30405153
|
n = int(input())
props = []
def preproc(a):
return float(a)/100.
for i in range(pow(2,n)):
props.append(list(map(preproc, input().split())))
wining_props = [] # list of lists. First index -- number of round, second -- num of team, value -- prop of wining
wining_props_first_round = []
for i in range(0, (2 ** n), 2):
# i, and i+1 teams playing
wining_prop_for_i = props[i][i + 1]
wining_props_first_round.append(wining_prop_for_i)
wining_props_first_round.append(1. - wining_prop_for_i)
wining_props.append(wining_props_first_round)
assert len(wining_props_first_round) == len(props)
for round_num in range(2, n + 1):
# calculate propabilitys for winning in i round for each team
# prop of winning in i round = prop of winning prev round + mo of win this one
# mo win this = for each team we can meet prop of them wining prev * prop we win them
# each team we can meet on round i = all teems // 2^i == we//2^i
this_round_wining_props = []
for team_num in range(2 ** n):
t = team_num // (2 ** round_num) * (2 ** (round_num))
teams_we_meet_this_round = [t + x for x in range(2 ** round_num)]
t = team_num // (2 ** (round_num-1)) * (2 ** (round_num-1))
teams_we_meet_prev_round = [t + x for x in range(2 ** (round_num-1))]
for tt in teams_we_meet_prev_round:
teams_we_meet_this_round.remove(tt)
this_team_wining_props = wining_props[round_num - 2][team_num] # -2 cause numeration
chances_win_i_team = []
for tm in teams_we_meet_this_round:
# chances we meet them * chances we win
chances_win_i_team.append(wining_props[round_num - 2][tm] * props[team_num][tm])
mo_win_this_round = sum(chances_win_i_team)
this_team_wining_props *= mo_win_this_round
this_round_wining_props.append(this_team_wining_props)
#assert 0.99 < sum(this_round_wining_props) < 1.01
wining_props.append(this_round_wining_props)
# now we got props of each win on each round. Lets bet on most propable winer and calculate revenue
#from left to right-1 is playing
def revenue(round_num, teams_left, teams_right, winner=-1):
# let the strongest team win, we bet, and calculate to the bottom
if round_num == 1:
return wining_props[0][winner] if winner != -1 else max(wining_props[0][teams_left:teams_right])
if winner == -1:
winner_prop = max(wining_props[round_num-1][teams_left:teams_right])
winner = wining_props[round_num - 1].index(winner_prop)
else:
winner_prop = wining_props[round_num-1][winner]
split = ((teams_left + teams_right) // 2)
if winner >= split:
return sum([revenue(round_num - 1, teams_left, split), revenue(round_num - 1, split, teams_right, winner),
winner_prop * (2 ** (round_num - 1))])
else:
return sum([revenue(round_num - 1, teams_left, split, winner), revenue(round_num - 1, split, teams_right),
winner_prop * (2 ** (round_num - 1))])
print(revenue(n, 0, (2 ** n)))
| 19
| 218
| 3,993,600
|
52624119
|
n = int(input())
# Using the same index I would for a tree
m = 2**n
points = [0]*(2*m)
points[1] = 2**(n-1)
for i in range(1,m):
x = points[i]//2
points[2*i] = x
points[2*i+1] = x
P = [[int(x)/100.0 for x in input().split()] for _ in range(m)]
state = [[0.0]*64 for _ in range(2*m)]
for i in range(m):
state[m+i][i] = 1.0
for i in reversed(range(1,m)):
for j in range(m):
for k in range(j+1,m):
# x is probability that team j meets team k in match i
x = state[2*i][j]*state[2*i+1][k]
state[i][j] += P[j][k]*x
state[i][k] += P[k][j]*x
score = [[0]*64 for _ in range(2*m)]
for i in reversed(range(1,m)):
for j in range(m):
for k in range(m):
score[i][j] = max(score[i][j], score[2*i][j]+state[i][j]*points[i] + score[2*i+1][k])
score[i][j] = max(score[i][j], score[2*i+1][j]+state[i][j]*points[i] + score[2*i][k])
print(repr(max(score[1])))
|
MemSQL Start[c]UP 3.0 - Round 1
|
CF
| 2,017
| 2
| 256
|
Third Month Insanity
|
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2N teams participating in the tournament, numbered from 1 to 2N. The tournament lasts N rounds, with each round eliminating half the teams. The first round consists of 2N - 1 games, numbered starting from 1. In game i, team 2·i - 1 will play against team 2·i. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game i the winner of the previous round's game 2·i - 1 will play against the winner of the previous round's game 2·i.
Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2N - 1 points.
For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score.
|
Input will begin with a line containing N (2 ≤ N ≤ 6).
2N lines follow, each with 2N integers. The j-th column of the i-th row indicates the percentage chance that team i will defeat team j, unless i = j, in which case the value will be 0. It is guaranteed that the i-th column of the j-th row plus the j-th column of the i-th row will add to exactly 100.
|
Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10 - 9.
Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if $${ \frac { | a - b | } { \operatorname* { m a x } ( 1, | b | ) } } \leq 1 0 ^ { - 9 }$$.
| null |
In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.
|
[{"input": "2\n0 40 100 100\n60 0 40 40\n0 60 0 45\n0 60 55 0", "output": "1.75"}, {"input": "3\n0 0 100 0 100 0 0 0\n100 0 100 0 0 0 100 100\n0 0 0 100 100 0 0 0\n100 100 0 0 0 0 100 100\n0 100 0 100 0 0 100 0\n100 100 100 100 100 0 0 0\n100 0 100 0 0 100 0 0\n100 0 100 0 100 100 100 0", "output": "12"}, {"input": "2\n0 21 41 26\n79 0 97 33\n59 3 0 91\n74 67 9 0", "output": "3.141592"}]
| 2,100
|
["dp", "probabilities", "trees"]
| 19
|
[{"input": "2\r\n0 40 100 100\r\n60 0 40 40\r\n0 60 0 45\r\n0 60 55 0\r\n", "output": "1.75\r\n"}, {"input": "3\r\n0 0 100 0 100 0 0 0\r\n100 0 100 0 0 0 100 100\r\n0 0 0 100 100 0 0 0\r\n100 100 0 0 0 0 100 100\r\n0 100 0 100 0 0 100 0\r\n100 100 100 100 100 0 0 0\r\n100 0 100 0 0 100 0 0\r\n100 0 100 0 100 100 100 0\r\n", "output": "12\r\n"}, {"input": "2\r\n0 21 41 26\r\n79 0 97 33\r\n59 3 0 91\r\n74 67 9 0\r\n", "output": "3.141592\r\n"}, {"input": "3\r\n0 7 38 22 3 66 32 77\r\n93 0 93 61 45 40 6 92\r\n62 7 0 32 8 46 56 29\r\n78 39 68 0 37 24 84 42\r\n97 55 92 63 0 46 62 100\r\n34 60 54 76 54 0 39 67\r\n68 94 44 16 38 61 0 98\r\n23 8 71 58 0 33 2 0\r\n", "output": "6.8196427571\r\n"}, {"input": "2\r\n0 50 50 50\r\n50 0 50 50\r\n50 50 0 50\r\n50 50 50 0\r\n", "output": "1.5\r\n"}, {"input": "2\r\n0 70 12 95\r\n30 0 98 85\r\n88 2 0 81\r\n5 15 19 0\r\n", "output": "2.51764\r\n"}, {"input": "2\r\n0 0 100 100\r\n100 0 100 100\r\n0 0 0 0\r\n0 0 100 0\r\n", "output": "4\r\n"}, {"input": "2\r\n0 31 4 83\r\n69 0 1 74\r\n96 99 0 71\r\n17 26 29 0\r\n", "output": "2.792594\r\n"}, {"input": "3\r\n0 53 31 33 2 34 22 21\r\n47 0 72 36 11 16 3 86\r\n69 28 0 49 25 6 92 54\r\n67 64 51 0 54 65 15 24\r\n98 89 75 46 0 48 65 31\r\n66 84 94 35 52 0 64 46\r\n78 97 8 85 35 36 0 62\r\n79 14 46 76 69 54 38 0\r\n", "output": "4.23995819508\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
# Read correct output
try:
with open(correct_output_path, 'r') as f:
correct_line = f.readline().strip()
correct = float(correct_line)
except:
print(0)
return
# Read submission output
try:
with open(submission_output_path, 'r') as f:
submission_line = f.readline().strip()
submission = float(submission_line)
except:
print(0)
return
abs_error = abs(submission - correct)
denominator = max(1.0, abs(correct))
rel_error = abs_error / denominator
if rel_error <= 1e-9:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
20/C
|
20
|
C
|
PyPy 3
|
TESTS
| 30
| 436
| 17,100,800
|
83314710
|
from sys import stdin
import heapq
def main():
n, m = map(int, stdin.readline().split())
adj = [[] for _ in range(n + 1)]
for _ in range(m):
x, y, w = map(int, stdin.readline().split())
if x == y:
continue
adj[x].append((y, w))
adj[y].append((x, w))
max_val = 10 ** 10 + 1
dist = [max_val] * (n + 1)
dist[1] = 0
q = []
heapq.heappush(q, (0, 1))
process = [False] * (n + 1)
prev = [i for i in range(n + 1)]
while q:
cd, cv = heapq.heappop(q)
if cd != dist[cv]:
continue
for p in adj[cv]:
nb = p[0]
w = p[1]
if dist[cv] + w < dist[nb]:
dist[nb] = dist[cv] + w
prev[nb] = cv
heapq.heappush(q, (dist[nb], nb))
if dist[n] == max_val:
print(-1)
else:
ans = []
cv = n
while prev[cv] != cv:
ans.append(cv)
cv = prev[cv]
ans.append(1)
ans.reverse()
print(*ans)
if __name__ == '__main__':
main()
| 33
| 233
| 26,624,000
|
208734321
|
from heapq import *
from sys import *
f = lambda: map(int, stdin.readline().split())
inf = 1 << 40
n, m = f()
k = n + 1
g = [[] for i in range(k)]
for j in range(m):
u, v, l = f()
g[u].append((v, l))
g[v].append((u, l))
d = [inf] * k
d[1] = 0
p = [0] * k
q = [1]
while q:
h = heappop(q)
v, l = h % k, h // k
if d[v] != l: continue
if v == n: break
for u, l in g[v]:
s = d[v] + l
if s < d[u]:
d[u], p[u] = s, v
heappush(q, k * s + u)
if d[n] == inf:
print(-1)
else:
u, t = n, []
while u:
t.append(u)
u = p[u]
t.reverse()
print(' '.join(map(str, t)))
|
Codeforces Alpha Round 20 (Codeforces format)
|
CF
| 2,010
| 1
| 64
|
Dijkstra?
|
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
|
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
|
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
| null | null |
[{"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}, {"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}]
| 1,900
|
["graphs", "shortest paths"]
| 33
|
[{"input": "5 6\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": "1 4 3 5 "}, {"input": "5 6\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": "1 4 3 5 "}, {"input": "2 1\r\n1 2 1\r\n", "output": "1 2 "}, {"input": "3 1\r\n1 2 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "1 3 "}, {"input": "10 10\r\n1 5 12\r\n2 4 140\r\n2 10 149\r\n3 6 154\r\n3 7 9\r\n3 8 226\r\n3 10 132\r\n4 10 55\r\n5 8 33\r\n7 8 173\r\n", "output": "1 5 8 7 3 10 "}, {"input": "10 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": "1 8 10 "}, {"input": "10 10\r\n1 4 200\r\n1 9 197\r\n3 4 79\r\n3 5 213\r\n3 6 149\r\n5 8 3\r\n5 9 189\r\n6 7 130\r\n6 9 51\r\n8 10 135\r\n", "output": "1 9 5 8 10 "}, {"input": "10 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": "1 4 6 10 "}]
| false
|
stdio
| null | true
|
383/A
|
383
|
A
|
Python 3
|
TESTS
| 2
| 93
| 307,200
|
72068713
|
from sys import stdin
def f(n, s):
n = n.rstrip('\n')
s = s.rstrip('\n')
s = s.split(' ')
i = j = 0
ans = 0
ct = 0
while i < len(s):
j = i
while j < len(s) - 1 and s[j] == s[j+1]:
j += 1
if j == len(s) - 1:
return ans
else:
if s[j] == '1' and s[j+1] == '0':
ct += 1
ans += ct
i = j + 1
return ans
if __name__ == '__main__':
n = stdin.readline()
s = stdin.readline()
print(f(n, s))
| 42
| 92
| 6,041,600
|
17072338
|
def main():
input()
i = res = 0
for c in input()[::2]:
if c == "1":
i += 1
else:
res += i
print(res)
if __name__ == '__main__':
main()
|
Codeforces Round 225 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Milking cows
|
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).
Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.
|
The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains n integers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right.
|
Print a single integer, the minimum amount of lost milk.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
|
[{"input": "4\n0 0 1 0", "output": "1"}, {"input": "5\n1 0 1 0 1", "output": "3"}]
| 1,600
|
["data structures", "greedy"]
| 42
|
[{"input": "4\r\n0 0 1 0\r\n", "output": "1"}, {"input": "5\r\n1 0 1 0 1\r\n", "output": "3"}, {"input": "50\r\n1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0\r\n", "output": "416"}, {"input": "100\r\n1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0\r\n", "output": "1446"}, {"input": "1\r\n1\r\n", "output": "0"}, {"input": "1\r\n0\r\n", "output": "0"}, {"input": "2\r\n0 1\r\n", "output": "0"}, {"input": "2\r\n1 0\r\n", "output": "1"}, {"input": "2\r\n0 0\r\n", "output": "0"}, {"input": "2\r\n1 1\r\n", "output": "0"}, {"input": "4\r\n1 1 1 1\r\n", "output": "0"}]
| false
|
stdio
| null | true
|
20/C
|
20
|
C
|
Python 3
|
TESTS
| 30
| 686
| 18,329,600
|
74378577
|
import heapq
n,m=map(int,input().split())
inf=10**10
d=[[] for _ in range(n)]
b=[0]+[inf]*(n-1)
c=[-1]*n
hist=[(0,0)]
for i in range(m):
x,y,w=map(int,input().split())
d[x-1].append((w,y-1))
d[y-1].append((w,x-1))
while hist:
min_p=heapq.heappop(hist)
min_ind=min_p[1]
for p in d[min_ind]:
w, ind = b[min_ind]+p[0], p[1]
if w<b[ind]:
b[ind], c[ind]=w, min_ind
heapq.heappush(hist, (w, ind) )
if b[n-1]==inf:
print(-1)
else:
fin, path = n-1, []
while fin!=-1:
path+=[fin+1]
fin=c[fin]
print(*path[::-1])
| 33
| 264
| 29,491,200
|
214729466
|
from sys import stdin
def input(): return stdin.readline()[:-1]
INF = 10**18
N, E = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(E):
a, b, w = map(int, input().split())
a -= 1
b -= 1
G[a].append((b, w))
G[b].append((a, w))
dists = [INF] * N
dists[0] = 0
parent = [-1] * N
parent[0] = 0
from heapq import heappush, heappop
que = [(0, 0)]
while que:
dist, v = heappop(que)
if dist > dists[v]:
continue
for u, w in G[v]:
if dists[u] > dists[v] + w:
dists[u] = dists[v] + w
parent[u] = v
heappush(que, (dists[u], u))
if dists[N-1] == INF:
print(-1)
exit()
path = [N-1]
while path[-1] != 0:
path.append(parent[path[-1]])
path = path[::-1]
path = [p+1 for p in path]
print(*path)
|
Codeforces Alpha Round 20 (Codeforces format)
|
CF
| 2,010
| 1
| 64
|
Dijkstra?
|
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
|
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
|
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
| null | null |
[{"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}, {"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}]
| 1,900
|
["graphs", "shortest paths"]
| 33
|
[{"input": "5 6\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": "1 4 3 5 "}, {"input": "5 6\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": "1 4 3 5 "}, {"input": "2 1\r\n1 2 1\r\n", "output": "1 2 "}, {"input": "3 1\r\n1 2 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "1 3 "}, {"input": "10 10\r\n1 5 12\r\n2 4 140\r\n2 10 149\r\n3 6 154\r\n3 7 9\r\n3 8 226\r\n3 10 132\r\n4 10 55\r\n5 8 33\r\n7 8 173\r\n", "output": "1 5 8 7 3 10 "}, {"input": "10 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": "1 8 10 "}, {"input": "10 10\r\n1 4 200\r\n1 9 197\r\n3 4 79\r\n3 5 213\r\n3 6 149\r\n5 8 3\r\n5 9 189\r\n6 7 130\r\n6 9 51\r\n8 10 135\r\n", "output": "1 9 5 8 10 "}, {"input": "10 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": "1 4 6 10 "}]
| false
|
stdio
| null | true
|
765/A
|
765
|
A
|
Python 3
|
TESTS
| 4
| 31
| 0
|
150983195
|
n,home,d=int(input()),input(),dict()
for i in range(n):
s=input()
d[s[:3]]=s[5:]
t=home
while True:
try:
home=d[home]
if home==t:print('home');break
except:
print('contest');break
| 23
| 46
| 0
|
136532132
|
#!/usr/bin/env python
# coding=utf-8
'''
Author: Deean
Date: 2021-11-22 23:29:43
LastEditTime: 2021-11-22 23:31:44
Description: Neverending competitions
FilePath: CF765A.py
'''
def func():
n = int(input())
home = input().strip()
for _ in range(n):
a, b = input().strip().split("->")
print("home" if n % 2 == 0 else "contest")
if __name__ == '__main__':
func()
|
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
|
CF
| 2,017
| 2
| 512
|
Neverending competitions
|
There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back.
Jinotega's best friends, team Base have found a list of their itinerary receipts with information about departure and arrival airports. Now they wonder, where is Jinotega now: at home or at some competition far away? They know that:
- this list contains all Jinotega's flights in this year (in arbitrary order),
- Jinotega has only flown from his hometown to a snooker contest and back,
- after each competition Jinotega flies back home (though they may attend a competition in one place several times),
- and finally, at the beginning of the year Jinotega was at home.
Please help them to determine Jinotega's location!
|
In the first line of input there is a single integer n: the number of Jinotega's flights (1 ≤ n ≤ 100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next n lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name of departure airport "YYY" is the name of arrival airport. Exactly one of these airports is Jinotega's home airport.
It is guaranteed that flights information is consistent with the knowledge of Jinotega's friends, which is described in the main part of the statement.
|
If Jinotega is now at home, print "home" (without quotes), otherwise print "contest".
| null |
In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list.
|
[{"input": "4\nSVO\nSVO->CDG\nLHR->SVO\nSVO->LHR\nCDG->SVO", "output": "home"}, {"input": "3\nSVO\nSVO->HKT\nHKT->SVO\nSVO->RAP", "output": "contest"}]
| 900
|
["implementation", "math"]
| 23
|
[{"input": "4\r\nSVO\r\nSVO->CDG\r\nLHR->SVO\r\nSVO->LHR\r\nCDG->SVO\r\n", "output": "home\r\n"}, {"input": "3\r\nSVO\r\nSVO->HKT\r\nHKT->SVO\r\nSVO->RAP\r\n", "output": "contest\r\n"}, {"input": "1\r\nESJ\r\nESJ->TSJ\r\n", "output": "contest\r\n"}, {"input": "2\r\nXMR\r\nFAJ->XMR\r\nXMR->FAJ\r\n", "output": "home\r\n"}, {"input": "3\r\nZIZ\r\nDWJ->ZIZ\r\nZIZ->DWJ\r\nZIZ->DWJ\r\n", "output": "contest\r\n"}, {"input": "10\r\nPVO\r\nDMN->PVO\r\nDMN->PVO\r\nPVO->DMN\r\nDMN->PVO\r\nPVO->DMN\r\nPVO->DMN\r\nPVO->DMN\r\nDMN->PVO\r\nPVO->DMN\r\nDMN->PVO\r\n", "output": "home\r\n"}, {"input": "11\r\nIAU\r\nIAU->RUQ\r\nIAU->RUQ\r\nRUQ->IAU\r\nRUQ->IAU\r\nIAU->RUQ\r\nRUQ->IAU\r\nIAU->RUQ\r\nRUQ->IAU\r\nIAU->RUQ\r\nIAU->RUQ\r\nRUQ->IAU\r\n", "output": "contest\r\n"}, {"input": "10\r\nHPN\r\nDFI->HPN\r\nHPN->KAB\r\nHPN->DFI\r\nVSO->HPN\r\nHPN->KZX\r\nHPN->VSO\r\nKZX->HPN\r\nLDW->HPN\r\nKAB->HPN\r\nHPN->LDW\r\n", "output": "home\r\n"}, {"input": "11\r\nFGH\r\nFGH->BRZ\r\nUBK->FGH\r\nQRE->FGH\r\nFGH->KQK\r\nFGH->QRE\r\nKQK->FGH\r\nFGH->UBK\r\nBRZ->FGH\r\nFGH->ALX\r\nALX->FGH\r\nFGH->KQK\r\n", "output": "contest\r\n"}, {"input": "50\r\nPFH\r\nJFV->PFH\r\nBVP->PFH\r\nPFH->BVP\r\nPFH->JFV\r\nPFH->ETQ\r\nPFH->LQJ\r\nZTO->PFH\r\nPFH->BVP\r\nPFH->RXO\r\nPFH->ZTO\r\nHWL->PFH\r\nPFH->HIV\r\nPFH->AFP\r\nPFH->HWL\r\nOBB->PFH\r\nHIV->PFH\r\nPFH->LSR\r\nAFP->PFH\r\nLQJ->PFH\r\nHWL->PFH\r\nETQ->PFH\r\nPFH->HWL\r\nLSR->PFH\r\nWBR->PFH\r\nBNZ->PFH\r\nHQR->PFH\r\nZTO->PFH\r\nPFH->WBR\r\nPFH->BYJ\r\nRXO->PFH\r\nFHZ->PFH\r\nFHZ->PFH\r\nPFN->PFH\r\nPFH->GMB\r\nPFH->JFV\r\nJFV->PFH\r\nGNZ->PFH\r\nPFH->BNZ\r\nPFH->GNZ\r\nPFH->HQR\r\nBYJ->PFH\r\nGMB->PFH\r\nPFH->FHZ\r\nPFH->FHZ\r\nPFH->ZTO\r\nPFH->UGD\r\nBVP->PFH\r\nUGD->PFH\r\nPFH->PFN\r\nPFH->OBB\r\n", "output": "home\r\n"}, {"input": "1\r\nAAK\r\nAAK->ABA\r\n", "output": "contest\r\n"}, {"input": "1\r\nXYZ\r\nXYZ->XYR\r\n", "output": "contest\r\n"}]
| false
|
stdio
| null | true
|
961/C
|
961
|
C
|
PyPy 3
|
TESTS
| 5
| 124
| 2,355,200
|
105153792
|
a=int(input())
l=[]
total=0
for i in range(4):
line=''
for x in [0]*a:
line+=input()
l.append(line)
input() if i!=3 else 0
l=sorted(l,key=lambda i: i[0::2].count('1'))[::-1]
for z,v in enumerate(l):
if z<2:
for i in range(a**2):
total += v[i]!='0' if i%2 else v[i]!='1'
else:
for i in range(a**2):
total += v[i]!='1' if i%2 else v[i]!='0'
print(total)
| 19
| 46
| 0
|
211838146
|
new_n = int(input())
c = [0] * 4
for k in range(4):
for i in range(new_n):
s = input()
for j in range(new_n):
if (i + j) % 2 != int(s[j]):
c[k] += 1
if k < 3:
input()
c.sort()
result = c[0] + c[1] + 2 * new_n * new_n - c[2] - c[3]
print(result)
|
Educational Codeforces Round 41 (Rated for Div. 2)
|
ICPC
| 2,018
| 1
| 256
|
Chessboard
|
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
|
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
|
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
| null | null |
[{"input": "1\n0\n0\n1\n0", "output": "1"}, {"input": "3\n101\n010\n101\n101\n000\n101\n010\n101\n011\n010\n101\n010", "output": "2"}]
| 1,400
|
["bitmasks", "brute force", "implementation"]
| 19
|
[{"input": "1\r\n0\r\n\r\n0\r\n\r\n1\r\n\r\n0\r\n", "output": "1\r\n"}, {"input": "3\r\n101\r\n010\r\n101\r\n\r\n101\r\n000\r\n101\r\n\r\n010\r\n101\r\n011\r\n\r\n010\r\n101\r\n010\r\n", "output": "2\r\n"}, {"input": "3\r\n000\r\n000\r\n000\r\n\r\n111\r\n111\r\n111\r\n\r\n111\r\n111\r\n111\r\n\r\n000\r\n000\r\n000\r\n", "output": "16\r\n"}, {"input": "3\r\n101\r\n010\r\n101\r\n\r\n101\r\n010\r\n101\r\n\r\n101\r\n010\r\n101\r\n\r\n101\r\n010\r\n101\r\n", "output": "18\r\n"}, {"input": "1\r\n1\r\n\r\n0\r\n\r\n1\r\n\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n\r\n0\r\n\r\n1\r\n\r\n1\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n\r\n1\r\n\r\n0\r\n\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n0\r\n\r\n0\r\n\r\n0\r\n\r\n0\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\n\r\n1\r\n\r\n0\r\n\r\n0\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
62/D
|
62
|
D
|
Python 3
|
TESTS
| 7
| 92
| 0
|
139385183
|
def build_home_path(n: int, m: int, old_path: list) -> list:
"""
Parameters:
:n (int): number of rooms
:m (int): number of corridors
:old_path (list): old build path
:return: new build path
"""
list_graf = {}
m = len(old_path) - 1
for i in range(m):
if old_path[i] - 1 in list_graf:
list_graf[old_path[i] - 1].append(old_path[i + 1] - 1)
else:
list_graf[old_path[i] - 1] = [old_path[i + 1] - 1]
if old_path[i + 1] - 1 in list_graf:
list_graf[old_path[i + 1] - 1].append(old_path[i] - 1)
else:
list_graf[old_path[i + 1] - 1] = [old_path[i] - 1]
# print(old_path)
# print(list_graf)
last_pair = [0, 0]
for i in range(m):
pairs = []
pairs_index = []
prev = i
for j in range(i + 1, m + 1):
if old_path[prev] == old_path[j]:
if old_path[prev + 1] < old_path[j - 1]:
pairs.append([old_path[prev + 1], old_path[j - 1]])
pairs_index.append([prev + 1, j - 1])
prev = j
if pairs:
if last_pair[0] < pairs_index[-1][0]:
last_pair = pairs_index[-1]
new_path = []
for i in range(m + 1):
if i < last_pair[0] or i > last_pair[1]:
# print(i)
new_path.append(old_path[i])
else:
# print(last_pair[0] + last_pair[1] - i)
new_path.append(old_path[(last_pair[0]) + last_pair[1] - i])
if old_path == new_path:
return None
return new_path
n, m = map(int,input().split())
p = list(map(int,input().split()))
res = build_home_path(n,m,p)
if res is None:
print("No solution")
else:
print(" ".join(str(x) for x in res))
| 30
| 124
| 819,200
|
187019306
|
n,m = map(int,input().split())
m+=1
p = list(map(lambda x:int(x)-1,input().split()))
a = [0]*m
q = [[False]*n for i in range(n)]
d = [[] for i in range(n)]
for i in range(1,m):
d[p[i]].append(p[i-1])
d[p[i-1]].append(p[i])
for i in range(n):
d[i].sort()
s = [(p[0],True,p[0])]
l = 0
while s:
v,f,vv = s[-1]
if f is None:
s.pop()
l-=1
q[vv][v]=q[v][vv]=False
continue
q[vv][v]=q[v][vv]=True
s[-1]=(v,None,vv)
a[l]=v
l+=1
if l==m:
if f:
continue
else:
break
for u in reversed(d[v]):
if f and u<p[l]:
continue
if q[u][v]:
continue
s.append((u,f and u==p[l],v))
if s:
print(' '.join(map(lambda x:str(x+1),a)))
else:
print("No solution")
|
Codeforces Beta Round 58
|
CF
| 2,011
| 2
| 256
|
Wormhouse
|
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to n. All the corridors are bidirectional.
Arnie wants the new house to look just like the previous one. That is, it should have exactly n rooms and, if a corridor from room i to room j existed in the old house, it should be built in the new one.
We know that during the house constructing process Arnie starts to eat an apple starting from some room and only stops when he eats his way through all the corridors and returns to the starting room. It is also known that Arnie eats without stopping. That is, until Arnie finishes constructing the house, he is busy every moment of his time gnawing a new corridor. Arnie doesn't move along the already built corridors.
However, gnawing out corridors in one and the same order any time you change a house is a very difficult activity. That's why Arnie, knowing the order in which the corridors were located in the previous house, wants to gnaw corridors in another order. It is represented as a list of rooms in the order in which they should be visited. The new list should be lexicographically smallest, but it also should be strictly lexicographically greater than the previous one. Help the worm.
|
The first line contains two integers n and m (3 ≤ n ≤ 100, 3 ≤ m ≤ 2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains m + 1 positive integers that do not exceed n. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawing. It is guaranteed that the last number in the list coincides with the first one.
The first room described in the list is the main entrance, that's why Arnie should begin gnawing from it.
You may assume that there is no room which is connected to itself and there is at most one corridor between any pair of rooms. However, it is possible to find some isolated rooms which are disconnected from others.
|
Print m + 1 positive integers that do not exceed n. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal to the main entrance.
| null | null |
[{"input": "3 3\n1 2 3 1", "output": "1 3 2 1"}, {"input": "3 3\n1 3 2 1", "output": "No solution"}]
| 2,300
|
["dfs and similar", "graphs"]
| 30
|
[{"input": "3 3\r\n1 2 3 1\r\n", "output": "1 3 2 1 "}, {"input": "3 3\r\n1 3 2 1\r\n", "output": "No solution"}, {"input": "4 4\r\n1 2 4 3 1\r\n", "output": "1 3 4 2 1 "}, {"input": "6 7\r\n3 2 4 1 6 5 1 3\r\n", "output": "No solution"}, {"input": "8 12\r\n4 6 5 1 4 3 1 8 3 7 8 5 4\r\n", "output": "4 6 5 1 4 3 1 8 7 3 8 5 4 "}, {"input": "5 6\r\n3 4 1 2 5 1 3\r\n", "output": "3 4 1 5 2 1 3 "}, {"input": "7 9\r\n3 2 7 3 5 1 2 6 1 3\r\n", "output": "3 2 7 3 5 1 6 2 1 3 "}, {"input": "6 7\r\n1 5 6 1 4 3 2 1\r\n", "output": "1 6 5 1 2 3 4 1 "}, {"input": "4 3\r\n1 2 3 1\r\n", "output": "1 3 2 1 "}, {"input": "10 40\r\n10 3 8 4 10 2 8 1 2 6 3 5 7 6 10 8 9 7 8 5 4 9 1 3 7 2 5 10 9 2 4 3 9 6 5 1 4 6 1 7 10\r\n", "output": "10 3 8 4 10 2 8 1 2 6 3 5 7 6 10 8 9 7 8 5 4 9 1 3 7 2 5 10 9 2 4 3 9 6 5 1 6 4 1 7 10 "}]
| false
|
stdio
| null | true
|
376/B
|
376
|
B
|
PyPy 3
|
TESTS
| 28
| 202
| 3,481,600
|
68067323
|
n,m=map(int,input().split())
A=[0]*(n+1)
ans=0
for _ in range(m):
a,b,c=map(int,input().split())
A[a]+=c
A[b]-=c
for i in range(n):
if A[i]>=0:
ans+=A[i]
print(ans)
| 29
| 46
| 0
|
155106961
|
l = input().split()
n, m = int(l[0]), int(l[1])
graph = {}
for _ in range(m):
a, b, c = [int(x) for x in input().split()]
if graph.get(a):
graph[a].append((b, c))
else:
graph[a] = [(b, c)]
debt = [0] * (n+1)
visited = [False] * (n+1)
for i in range(1, n+1):
if visited[i]:
continue
q = [i]
while len(q):
node = q.pop()
visited[node] = True
for neighbor in graph.get(node, []):
p, d = neighbor[0], neighbor[1]
debt[node] -= d
debt[p] += d
if visited[p]:
continue
q.append(p)
ans = 0
for v in debt:
if v > 0:
ans += v
print(ans)
|
Codeforces Round 221 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
I.O.U.
|
Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
|
The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ ci ≤ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
|
Print a single integer — the minimum sum of debts in the optimal rearrangement.
| null |
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
|
[{"input": "5 3\n1 2 10\n2 3 1\n2 4 1", "output": "10"}, {"input": "3 0", "output": "0"}, {"input": "4 3\n1 2 1\n2 3 1\n3 1 1", "output": "0"}]
| 1,300
|
["implementation"]
| 29
|
[{"input": "5 3\r\n1 2 10\r\n2 3 1\r\n2 4 1\r\n", "output": "10\r\n"}, {"input": "3 0\r\n", "output": "0\r\n"}, {"input": "4 3\r\n1 2 1\r\n2 3 1\r\n3 1 1\r\n", "output": "0\r\n"}, {"input": "20 28\r\n1 5 6\r\n1 12 7\r\n1 13 4\r\n1 15 7\r\n1 20 3\r\n2 4 1\r\n2 15 6\r\n3 5 3\r\n3 8 10\r\n3 13 8\r\n3 20 6\r\n4 6 10\r\n4 12 8\r\n4 19 5\r\n5 17 8\r\n6 9 9\r\n6 16 2\r\n6 19 9\r\n7 14 6\r\n8 9 3\r\n8 16 10\r\n9 11 7\r\n9 17 8\r\n11 13 8\r\n11 17 17\r\n11 19 1\r\n15 20 2\r\n17 20 1\r\n", "output": "124\r\n"}, {"input": "20 36\r\n1 2 13\r\n1 3 1\r\n1 6 4\r\n1 12 8\r\n1 13 9\r\n1 15 3\r\n1 18 4\r\n2 10 2\r\n2 15 2\r\n2 18 6\r\n3 7 8\r\n3 16 19\r\n4 7 1\r\n4 18 4\r\n5 9 2\r\n5 15 9\r\n5 17 4\r\n5 18 5\r\n6 11 7\r\n6 13 1\r\n6 14 9\r\n7 10 4\r\n7 12 10\r\n7 15 9\r\n7 17 8\r\n8 14 4\r\n10 13 8\r\n10 19 9\r\n11 12 5\r\n12 17 6\r\n13 15 8\r\n13 19 4\r\n14 15 9\r\n14 16 8\r\n17 19 8\r\n17 20 7\r\n", "output": "147\r\n"}, {"input": "20 40\r\n1 13 4\r\n2 3 3\r\n2 4 5\r\n2 7 7\r\n2 17 10\r\n3 5 3\r\n3 6 9\r\n3 10 4\r\n3 12 2\r\n3 13 2\r\n3 14 3\r\n4 5 4\r\n4 8 7\r\n4 13 9\r\n5 6 14\r\n5 14 5\r\n7 11 5\r\n7 12 13\r\n7 15 7\r\n8 14 5\r\n8 16 7\r\n8 18 17\r\n9 11 8\r\n9 19 19\r\n10 12 4\r\n10 16 3\r\n10 18 10\r\n10 20 9\r\n11 13 9\r\n11 20 2\r\n12 13 8\r\n12 18 2\r\n12 20 3\r\n13 17 1\r\n13 20 4\r\n14 16 8\r\n16 19 3\r\n18 19 3\r\n18 20 7\r\n19 20 10\r\n", "output": "165\r\n"}, {"input": "50 10\r\n1 5 1\r\n2 34 2\r\n3 8 10\r\n5 28 4\r\n7 28 6\r\n13 49 9\r\n15 42 7\r\n16 26 7\r\n18 47 5\r\n20 41 10\r\n", "output": "60\r\n"}, {"input": "50 46\r\n1 6 10\r\n1 18 1\r\n1 24 10\r\n1 33 2\r\n1 40 8\r\n3 16 7\r\n4 26 8\r\n4 32 2\r\n4 34 6\r\n5 29 8\r\n6 44 3\r\n8 20 5\r\n8 42 13\r\n10 13 5\r\n10 25 7\r\n10 27 9\r\n10 29 10\r\n11 23 4\r\n12 28 7\r\n12 30 10\r\n12 40 10\r\n13 18 2\r\n13 33 2\r\n14 15 7\r\n14 43 10\r\n14 47 3\r\n16 27 10\r\n17 21 6\r\n17 30 9\r\n19 40 4\r\n22 24 8\r\n22 25 7\r\n22 38 18\r\n25 38 1\r\n27 31 7\r\n27 40 8\r\n30 36 8\r\n31 34 1\r\n32 49 6\r\n33 35 4\r\n33 50 7\r\n38 47 1\r\n42 47 2\r\n42 50 5\r\n43 44 9\r\n47 50 5\r\n", "output": "228\r\n"}, {"input": "100 48\r\n1 56 6\r\n2 42 3\r\n3 52 1\r\n9 50 8\r\n10 96 8\r\n11 39 2\r\n12 51 6\r\n12 68 7\r\n13 40 5\r\n14 18 10\r\n14 70 6\r\n15 37 4\r\n15 38 8\r\n15 82 6\r\n15 85 5\r\n16 48 4\r\n16 50 9\r\n16 71 9\r\n17 18 3\r\n17 100 10\r\n20 73 3\r\n22 32 9\r\n22 89 9\r\n23 53 3\r\n24 53 1\r\n27 78 10\r\n30 50 5\r\n33 94 8\r\n34 87 9\r\n35 73 3\r\n36 51 8\r\n37 88 10\r\n37 97 2\r\n40 47 8\r\n40 90 6\r\n44 53 3\r\n44 65 3\r\n47 48 8\r\n48 72 10\r\n49 98 2\r\n53 68 10\r\n53 71 9\r\n57 62 2\r\n63 76 10\r\n66 90 9\r\n71 76 8\r\n72 80 5\r\n75 77 7\r\n", "output": "253\r\n"}, {"input": "4 3\r\n1 4 1\r\n2 3 1\r\n4 2 2\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
376/B
|
376
|
B
|
Python 3
|
TESTS
| 28
| 77
| 0
|
119685638
|
n, m = map(int, input().split())
l = [0]*102
for i in range(m):
a, b, c = map(int, input().split())
l[a] += c
l[b] -= c
sum = 0
for i in range(n):
if l[i] > 0:
sum += l[i]
print(sum)
| 29
| 46
| 0
|
168900387
|
n,m=[int(i) for i in input().split()] ; dic=dict() ; cou=0
for i in range(1,n+1):
dic[i]=0
for i in range(m):
a,b,c=list(map(int,input().split()))
dic[a]-=c ; dic[b]+=c
for i in dic.values():
if i>0:cou+=i
print(cou)
|
Codeforces Round 221 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
I.O.U.
|
Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
|
The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ ci ≤ 100), which mean that person ai owes person bi ci rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
|
Print a single integer — the minimum sum of debts in the optimal rearrangement.
| null |
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
|
[{"input": "5 3\n1 2 10\n2 3 1\n2 4 1", "output": "10"}, {"input": "3 0", "output": "0"}, {"input": "4 3\n1 2 1\n2 3 1\n3 1 1", "output": "0"}]
| 1,300
|
["implementation"]
| 29
|
[{"input": "5 3\r\n1 2 10\r\n2 3 1\r\n2 4 1\r\n", "output": "10\r\n"}, {"input": "3 0\r\n", "output": "0\r\n"}, {"input": "4 3\r\n1 2 1\r\n2 3 1\r\n3 1 1\r\n", "output": "0\r\n"}, {"input": "20 28\r\n1 5 6\r\n1 12 7\r\n1 13 4\r\n1 15 7\r\n1 20 3\r\n2 4 1\r\n2 15 6\r\n3 5 3\r\n3 8 10\r\n3 13 8\r\n3 20 6\r\n4 6 10\r\n4 12 8\r\n4 19 5\r\n5 17 8\r\n6 9 9\r\n6 16 2\r\n6 19 9\r\n7 14 6\r\n8 9 3\r\n8 16 10\r\n9 11 7\r\n9 17 8\r\n11 13 8\r\n11 17 17\r\n11 19 1\r\n15 20 2\r\n17 20 1\r\n", "output": "124\r\n"}, {"input": "20 36\r\n1 2 13\r\n1 3 1\r\n1 6 4\r\n1 12 8\r\n1 13 9\r\n1 15 3\r\n1 18 4\r\n2 10 2\r\n2 15 2\r\n2 18 6\r\n3 7 8\r\n3 16 19\r\n4 7 1\r\n4 18 4\r\n5 9 2\r\n5 15 9\r\n5 17 4\r\n5 18 5\r\n6 11 7\r\n6 13 1\r\n6 14 9\r\n7 10 4\r\n7 12 10\r\n7 15 9\r\n7 17 8\r\n8 14 4\r\n10 13 8\r\n10 19 9\r\n11 12 5\r\n12 17 6\r\n13 15 8\r\n13 19 4\r\n14 15 9\r\n14 16 8\r\n17 19 8\r\n17 20 7\r\n", "output": "147\r\n"}, {"input": "20 40\r\n1 13 4\r\n2 3 3\r\n2 4 5\r\n2 7 7\r\n2 17 10\r\n3 5 3\r\n3 6 9\r\n3 10 4\r\n3 12 2\r\n3 13 2\r\n3 14 3\r\n4 5 4\r\n4 8 7\r\n4 13 9\r\n5 6 14\r\n5 14 5\r\n7 11 5\r\n7 12 13\r\n7 15 7\r\n8 14 5\r\n8 16 7\r\n8 18 17\r\n9 11 8\r\n9 19 19\r\n10 12 4\r\n10 16 3\r\n10 18 10\r\n10 20 9\r\n11 13 9\r\n11 20 2\r\n12 13 8\r\n12 18 2\r\n12 20 3\r\n13 17 1\r\n13 20 4\r\n14 16 8\r\n16 19 3\r\n18 19 3\r\n18 20 7\r\n19 20 10\r\n", "output": "165\r\n"}, {"input": "50 10\r\n1 5 1\r\n2 34 2\r\n3 8 10\r\n5 28 4\r\n7 28 6\r\n13 49 9\r\n15 42 7\r\n16 26 7\r\n18 47 5\r\n20 41 10\r\n", "output": "60\r\n"}, {"input": "50 46\r\n1 6 10\r\n1 18 1\r\n1 24 10\r\n1 33 2\r\n1 40 8\r\n3 16 7\r\n4 26 8\r\n4 32 2\r\n4 34 6\r\n5 29 8\r\n6 44 3\r\n8 20 5\r\n8 42 13\r\n10 13 5\r\n10 25 7\r\n10 27 9\r\n10 29 10\r\n11 23 4\r\n12 28 7\r\n12 30 10\r\n12 40 10\r\n13 18 2\r\n13 33 2\r\n14 15 7\r\n14 43 10\r\n14 47 3\r\n16 27 10\r\n17 21 6\r\n17 30 9\r\n19 40 4\r\n22 24 8\r\n22 25 7\r\n22 38 18\r\n25 38 1\r\n27 31 7\r\n27 40 8\r\n30 36 8\r\n31 34 1\r\n32 49 6\r\n33 35 4\r\n33 50 7\r\n38 47 1\r\n42 47 2\r\n42 50 5\r\n43 44 9\r\n47 50 5\r\n", "output": "228\r\n"}, {"input": "100 48\r\n1 56 6\r\n2 42 3\r\n3 52 1\r\n9 50 8\r\n10 96 8\r\n11 39 2\r\n12 51 6\r\n12 68 7\r\n13 40 5\r\n14 18 10\r\n14 70 6\r\n15 37 4\r\n15 38 8\r\n15 82 6\r\n15 85 5\r\n16 48 4\r\n16 50 9\r\n16 71 9\r\n17 18 3\r\n17 100 10\r\n20 73 3\r\n22 32 9\r\n22 89 9\r\n23 53 3\r\n24 53 1\r\n27 78 10\r\n30 50 5\r\n33 94 8\r\n34 87 9\r\n35 73 3\r\n36 51 8\r\n37 88 10\r\n37 97 2\r\n40 47 8\r\n40 90 6\r\n44 53 3\r\n44 65 3\r\n47 48 8\r\n48 72 10\r\n49 98 2\r\n53 68 10\r\n53 71 9\r\n57 62 2\r\n63 76 10\r\n66 90 9\r\n71 76 8\r\n72 80 5\r\n75 77 7\r\n", "output": "253\r\n"}, {"input": "4 3\r\n1 4 1\r\n2 3 1\r\n4 2 2\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
20/C
|
20
|
C
|
Python 3
|
TESTS
| 30
| 592
| 40,448,000
|
10739823
|
from sys import stdin, stdout
from collections import deque
from operator import itemgetter
# trying out Moore's algorithm again, this time with edges
# sorted by their weight within the adjacency list
def moore(graph, source, target):
INF = 1 << 20
n = len(graph)
Q = deque([source])
in_queue = [ False for _ in range(n) ]
in_queue[source] = True
dist = [ INF for _ in range(n) ]
dist[source] = 0
pred = list(range(n))
while Q:
u = Q.pop()
for (v, w_uv) in graph[u]:
alt = dist[u] + w_uv
if alt < dist[v]:
dist[v] = alt
pred[v] = u
if not in_queue[v]:
Q.appendleft(v)
if dist[target] == INF:
return "-1"
else:
st = []
u = target
while(True):
st.append(u)
if(u == pred[u]):
break
u = pred[u]
st.reverse()
path = ' '.join([str(c + 1) for c in st])
path = path.rstrip()
return path
def main():
n, m = [int(x) for x in stdin.readline().split()]
graph_neighbors_in_dict = [ {} for _ in range(n) ]
# each node has a dictionary
# this allows us to quickly look at a particular edge, e.g.
# if we wanted to check the weight of the edge (u,v),
# then we would do this by adj[u][v]
for _ in range(m):
a, b, w = [int(c) for c in stdin.readline().split()]
w = int(w)
a -= 1
b -= 1
if not b in graph_neighbors_in_dict[a]:
graph_neighbors_in_dict[a][b] = w
else:
graph_neighbors_in_dict[a][b] = min(graph_neighbors_in_dict[a][b], w)
if not a in graph_neighbors_in_dict[b]:
graph_neighbors_in_dict[b][a] = w
else:
graph_neighbors_in_dict[b][a] = min(graph_neighbors_in_dict[b][a], w)
# transforming graph_neighbors_in_dict into usual weighted adjacency list
graph = [ [] for u in range(n) ]
for u in range(n):
for (v,w) in graph_neighbors_in_dict[u].items():
graph[u].append((v,w))
for u in range(n):
graph[u].sort(key = itemgetter(1))
source = 0
target = n - 1
stdout.write(moore(graph, source, target))
if __name__ == "__main__":
main()
| 33
| 280
| 34,304,000
|
208891058
|
from heapq import heappop, heappush
from math import inf
from sys import stdin
def dijkstra(graph, start):
dist = [inf] * len(graph)
dist[start] = 0
parent = [-1] * len(graph)
q = [(0, start)]
while q:
d, u = heappop(q)
if d > dist[u]:
continue
for v, w in graph[u]:
if d + w < dist[v]:
dist[v] = d + w
parent[v] = u
heappush(q, (dist[v], v))
return parent
def solve():
n, m = map(int, stdin.readline().split())
if n == 1:
print(1)
return
graph = [[] for _ in range(n + 1)]
for _ in range(m):
u, v, w = map(int, stdin.readline().split())
graph[u].append((v, w))
graph[v].append((u, w))
parent = dijkstra(graph, 1)
if parent[n] == -1:
print(-1)
return
res = [n]
while parent[res[-1]] != -1:
res.append(parent[res[-1]])
print(*res[::-1])
solve()
|
Codeforces Alpha Round 20 (Codeforces format)
|
CF
| 2,010
| 1
| 64
|
Dijkstra?
|
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
|
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
|
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
| null | null |
[{"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}, {"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}]
| 1,900
|
["graphs", "shortest paths"]
| 33
|
[{"input": "5 6\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": "1 4 3 5 "}, {"input": "5 6\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": "1 4 3 5 "}, {"input": "2 1\r\n1 2 1\r\n", "output": "1 2 "}, {"input": "3 1\r\n1 2 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "1 3 "}, {"input": "10 10\r\n1 5 12\r\n2 4 140\r\n2 10 149\r\n3 6 154\r\n3 7 9\r\n3 8 226\r\n3 10 132\r\n4 10 55\r\n5 8 33\r\n7 8 173\r\n", "output": "1 5 8 7 3 10 "}, {"input": "10 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": "1 8 10 "}, {"input": "10 10\r\n1 4 200\r\n1 9 197\r\n3 4 79\r\n3 5 213\r\n3 6 149\r\n5 8 3\r\n5 9 189\r\n6 7 130\r\n6 9 51\r\n8 10 135\r\n", "output": "1 9 5 8 10 "}, {"input": "10 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": "1 4 6 10 "}]
| false
|
stdio
| null | true
|
20/C
|
20
|
C
|
PyPy 3-64
|
TESTS
| 30
| 265
| 37,683,200
|
208677279
|
#from math import ceil, sqrt
#from collections import defaultdict
from heapq import heapify, heappush, heappop
import io, os, sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #input().decode().rstrip('\r\n')
#print = lambda x: sys.stdout.write(str(x) + "\n")
II = lambda: int(input())
MII = lambda: map(int, input().split())
LMII = lambda: list(MII())
#SLMII = lambda: sorted(LMII())
inf = 10**6 + 3
def dijkstra(node=0): # returns minimal path 0 ---> n-1
pq = [(0, node)] # priority queue (-dist, node, prev)
heapify(pq)
while pq:
#print(pq)
_, cur = heappop(pq)
if visited[cur]:
continue
visited[cur] = True
for nxt in adj[cur]:
weight = adj[cur][nxt]
if dist[nxt] > dist[cur] + weight: # update distances
dist[nxt] = dist[cur] + weight
path[nxt] = cur
heappush(pq, (dist[nxt], nxt))
vertices_count, edges_count = MII()
adj = [dict() for _ in range(vertices_count)]
for _ in range(edges_count):
a, b, weight = MII() # {a, b} edge in graph G
adj[a-1][b-1] = adj[b-1][a-1] = weight
dist = [inf]*vertices_count
dist[0] = 0
visited = [False]*vertices_count
path = [-1]*vertices_count
dijkstra()
ans = []
if visited[-1]:
v = vertices_count - 1
while True:
ans.append(v+1)
v = path[v]
if v == -1:
break
else:
ans.append(-1)
print(*ans[::-1]) # 0 3 2 4
| 33
| 295
| 28,876,800
|
168909132
|
import sys
from heapq import heappop, heappush
input = sys.stdin.readline
def dijkstra(graph, start=0):
n = len(graph)
dist, parents = [float('inf')] * n, [-1] * n
dist[start] = 0.0
queue = [(0.0, start)]
while queue:
path_len, v = heappop(queue)
if path_len == dist[v]:
for (w, edge_len) in graph[v]:
if edge_len + path_len < dist[w]:
dist[w], parents[w] = edge_len + path_len, v
heappush(queue, (edge_len + path_len, w))
return dist, parents
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(m):
a, b, w = map(int, input().split())
graph[a-1].append((b-1, w))
graph[b-1].append((a-1, w))
_, parents = dijkstra(graph)
if parents[n - 1] == -1:
print(-1)
else:
res, parent = [], n - 1
while parent != parents[0]:
res.append(parent + 1)
parent = parents[parent]
res.reverse()
print(*res)
|
Codeforces Alpha Round 20 (Codeforces format)
|
CF
| 2,010
| 1
| 64
|
Dijkstra?
|
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
|
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
|
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
| null | null |
[{"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}, {"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}]
| 1,900
|
["graphs", "shortest paths"]
| 33
|
[{"input": "5 6\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": "1 4 3 5 "}, {"input": "5 6\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": "1 4 3 5 "}, {"input": "2 1\r\n1 2 1\r\n", "output": "1 2 "}, {"input": "3 1\r\n1 2 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "1 3 "}, {"input": "10 10\r\n1 5 12\r\n2 4 140\r\n2 10 149\r\n3 6 154\r\n3 7 9\r\n3 8 226\r\n3 10 132\r\n4 10 55\r\n5 8 33\r\n7 8 173\r\n", "output": "1 5 8 7 3 10 "}, {"input": "10 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": "1 8 10 "}, {"input": "10 10\r\n1 4 200\r\n1 9 197\r\n3 4 79\r\n3 5 213\r\n3 6 149\r\n5 8 3\r\n5 9 189\r\n6 7 130\r\n6 9 51\r\n8 10 135\r\n", "output": "1 9 5 8 10 "}, {"input": "10 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": "1 4 6 10 "}]
| false
|
stdio
| null | true
|
20/C
|
20
|
C
|
Python 3
|
TESTS
| 30
| 686
| 61,337,600
|
208020050
|
import heapq
INF = 10 ** 9
class Edge:
def __init__(self, u, v, weight):
self.u = u
self.v = v
self.weight = weight
def main():
n, m = map(int, input().split())
edg = [[] for _ in range(n)]
for _ in range(m):
a, b, w = map(int, input().split())
edg[a - 1].append(Edge(a - 1, b - 1, w))
edg[b - 1].append(Edge(b - 1, a - 1, w))
dist = [INF] * n
parent = [-1] * n
dist[0] = 0
pq = [(0, 0)]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue
for edge in edg[u]:
v = edge.v
w = edge.weight
if w + dist[u] < dist[v]:
dist[v] = w + dist[u]
parent[v] = u
heapq.heappush(pq, (dist[v], v))
p = 0
temp = n - 1
camino = []
while p != -1:
p = parent[temp]
camino.append(temp)
temp = p
if len(camino) == 1 and n != 1:
print(-1)
else:
for i in range(len(camino) - 1, -1, -1):
print(camino[i] + 1, end=' ')
# Complejidad O(m + n log n)
if __name__ == "__main__":
main()
| 33
| 342
| 30,310,400
|
191299941
|
import heapq
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def dijkstra(s):
dist = [inf] * (n + 1)
dist[s] = 0
visit = [0] * (n + 1)
parent = [-1] * (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
parent[v] = u
heapq.heappush(p, (dist[v], v))
return dist, parent
n, m = map(int, input().split())
G = [[] for _ in range(n + 1)]
for _ in range(m):
a, b, w = map(int, input().split())
G[a].append((b, w))
G[b].append((a, w))
inf = pow(10, 12) + 1
dist, parent = dijkstra(1)
if parent[n] == -1:
ans = [-1]
else:
ans = [n]
i = n
while i ^ 1:
i = parent[i]
ans.append(i)
ans.reverse()
sys.stdout.write(" ".join(map(str, ans)))
|
Codeforces Alpha Round 20 (Codeforces format)
|
CF
| 2,010
| 1
| 64
|
Dijkstra?
|
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
|
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
|
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
| null | null |
[{"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}, {"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}]
| 1,900
|
["graphs", "shortest paths"]
| 33
|
[{"input": "5 6\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": "1 4 3 5 "}, {"input": "5 6\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": "1 4 3 5 "}, {"input": "2 1\r\n1 2 1\r\n", "output": "1 2 "}, {"input": "3 1\r\n1 2 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "1 3 "}, {"input": "10 10\r\n1 5 12\r\n2 4 140\r\n2 10 149\r\n3 6 154\r\n3 7 9\r\n3 8 226\r\n3 10 132\r\n4 10 55\r\n5 8 33\r\n7 8 173\r\n", "output": "1 5 8 7 3 10 "}, {"input": "10 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": "1 8 10 "}, {"input": "10 10\r\n1 4 200\r\n1 9 197\r\n3 4 79\r\n3 5 213\r\n3 6 149\r\n5 8 3\r\n5 9 189\r\n6 7 130\r\n6 9 51\r\n8 10 135\r\n", "output": "1 9 5 8 10 "}, {"input": "10 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": "1 4 6 10 "}]
| false
|
stdio
| null | true
|
20/C
|
20
|
C
|
Python 3
|
TESTS
| 30
| 608
| 21,094,400
|
42408979
|
import sys
import heapq
V, E = map(int, input().strip().split())
adj = [[] for _ in range(V+1)]
for _ in range(E):
u, v, w = map(int, input().strip().split())
adj[u].append((v, w))
adj[v].append((u, w))
#INF = 1000000000000
INF = sys.maxsize
prev = [-1]*(V+1)
dist = [INF]*(V+1)
vis = [0]*(V+1)
dist[1] = 0
pq = [(dist[1], 1, [1])]
while pq:
d, v, path = heapq.heappop(pq)
#if vis[v]: continue
#vis[v] = True
if v == V:
print(' '.join(map(str, path)))
break
if vis[v]: continue
vis[v] = 1
for u, w in adj[v]:
if dist[v]+w < dist[u]:
dist[u] = dist[v]+w
prev[u] = v
heapq.heappush(pq, (dist[u], u, path+[u]))
#if not vis[V]:
if dist[V] == INF:
print(-1)
| 33
| 358
| 47,206,400
|
129270968
|
"""
https://codeforces.com/problemset/problem/20/C?locale=ru
"""
from heapq import heappop, heappush
from collections import defaultdict
from sys import stdout, stdin
from io import IOBase, BytesIO
from os import read, write, fstat
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = read(self._fd, max(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, size: int = ...):
while self.newlines == 0:
b = read(self._fd, max(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:
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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
stdin, stdout = IOWrapper(stdin), IOWrapper(stdout)
def input(): return stdin.readline().rstrip("\r\n")
INF = float("inf")
def read_data():
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
node1, node2, weight = map(int, input().split())
node1 -= 1
node2 -= 1
graph[node1].append((node2, weight))
graph[node2].append((node1, weight))
return n, graph
def dijkstra(graph, start=0):
n = len(graph)
dist, parents_list = [INF] * n, [-1] * n
dist[start] = 0
queue = [(0, start)]
while queue:
path_len, v = heappop(queue)
if path_len == dist[v]:
for (w, edge_len) in graph[v]:
if edge_len + path_len < dist[w]:
dist[w], parents_list[w] = edge_len + path_len, v
heappush(queue, (edge_len + path_len, w))
return parents_list
def main():
n, graph = read_data()
parents_list = dijkstra(graph)
if parents_list[n - 1] == -1:
print(-1)
else:
full_path, parent = [], n - 1
while parent != parents_list[0]:
full_path.append(parent + 1)
parent = parents_list[parent]
full_path.reverse()
print(*full_path)
if __name__ == "__main__":
main()
|
Codeforces Alpha Round 20 (Codeforces format)
|
CF
| 2,010
| 1
| 64
|
Dijkstra?
|
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
|
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
|
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
| null | null |
[{"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}, {"input": "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "output": "1 4 3 5"}]
| 1,900
|
["graphs", "shortest paths"]
| 33
|
[{"input": "5 6\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": "1 4 3 5 "}, {"input": "5 6\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": "1 4 3 5 "}, {"input": "2 1\r\n1 2 1\r\n", "output": "1 2 "}, {"input": "3 1\r\n1 2 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 1\r\n1 3 2\r\n2 3 1\r\n", "output": "1 3 "}, {"input": "10 10\r\n1 5 12\r\n2 4 140\r\n2 10 149\r\n3 6 154\r\n3 7 9\r\n3 8 226\r\n3 10 132\r\n4 10 55\r\n5 8 33\r\n7 8 173\r\n", "output": "1 5 8 7 3 10 "}, {"input": "10 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": "1 8 10 "}, {"input": "10 10\r\n1 4 200\r\n1 9 197\r\n3 4 79\r\n3 5 213\r\n3 6 149\r\n5 8 3\r\n5 9 189\r\n6 7 130\r\n6 9 51\r\n8 10 135\r\n", "output": "1 9 5 8 10 "}, {"input": "10 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": "1 4 6 10 "}]
| false
|
stdio
| null | true
|
765/A
|
765
|
A
|
Python 3
|
TESTS
| 4
| 46
| 4,608,000
|
25318933
|
n = int(input())
native = input()
a = [[str(i) for i in input().split("->")] for j in range(n)]
A = set()
B = set()
for i in range(n):
A.add(a[i][0])
B.add(a[i][1])
if A == B:
print("home")
else:
print("contest")
| 23
| 46
| 0
|
153767018
|
n = int(input())
start = input()
empty_array = []
for i in range(n):
flights = input()
flights = flights.split('->')
empty_array.append(flights)
goes_back = []
goes_to = []
for i in range(len(empty_array)):
if (empty_array[i])[1] == start:
goes_back.append((empty_array[i])[0])
if (empty_array[i])[0] == start:
goes_to.append((empty_array[i])[1])
if len(goes_to) != len(goes_back):
print('contest')
else:
print('home')
|
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
|
CF
| 2,017
| 2
| 512
|
Neverending competitions
|
There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back.
Jinotega's best friends, team Base have found a list of their itinerary receipts with information about departure and arrival airports. Now they wonder, where is Jinotega now: at home or at some competition far away? They know that:
- this list contains all Jinotega's flights in this year (in arbitrary order),
- Jinotega has only flown from his hometown to a snooker contest and back,
- after each competition Jinotega flies back home (though they may attend a competition in one place several times),
- and finally, at the beginning of the year Jinotega was at home.
Please help them to determine Jinotega's location!
|
In the first line of input there is a single integer n: the number of Jinotega's flights (1 ≤ n ≤ 100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next n lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name of departure airport "YYY" is the name of arrival airport. Exactly one of these airports is Jinotega's home airport.
It is guaranteed that flights information is consistent with the knowledge of Jinotega's friends, which is described in the main part of the statement.
|
If Jinotega is now at home, print "home" (without quotes), otherwise print "contest".
| null |
In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list.
|
[{"input": "4\nSVO\nSVO->CDG\nLHR->SVO\nSVO->LHR\nCDG->SVO", "output": "home"}, {"input": "3\nSVO\nSVO->HKT\nHKT->SVO\nSVO->RAP", "output": "contest"}]
| 900
|
["implementation", "math"]
| 23
|
[{"input": "4\r\nSVO\r\nSVO->CDG\r\nLHR->SVO\r\nSVO->LHR\r\nCDG->SVO\r\n", "output": "home\r\n"}, {"input": "3\r\nSVO\r\nSVO->HKT\r\nHKT->SVO\r\nSVO->RAP\r\n", "output": "contest\r\n"}, {"input": "1\r\nESJ\r\nESJ->TSJ\r\n", "output": "contest\r\n"}, {"input": "2\r\nXMR\r\nFAJ->XMR\r\nXMR->FAJ\r\n", "output": "home\r\n"}, {"input": "3\r\nZIZ\r\nDWJ->ZIZ\r\nZIZ->DWJ\r\nZIZ->DWJ\r\n", "output": "contest\r\n"}, {"input": "10\r\nPVO\r\nDMN->PVO\r\nDMN->PVO\r\nPVO->DMN\r\nDMN->PVO\r\nPVO->DMN\r\nPVO->DMN\r\nPVO->DMN\r\nDMN->PVO\r\nPVO->DMN\r\nDMN->PVO\r\n", "output": "home\r\n"}, {"input": "11\r\nIAU\r\nIAU->RUQ\r\nIAU->RUQ\r\nRUQ->IAU\r\nRUQ->IAU\r\nIAU->RUQ\r\nRUQ->IAU\r\nIAU->RUQ\r\nRUQ->IAU\r\nIAU->RUQ\r\nIAU->RUQ\r\nRUQ->IAU\r\n", "output": "contest\r\n"}, {"input": "10\r\nHPN\r\nDFI->HPN\r\nHPN->KAB\r\nHPN->DFI\r\nVSO->HPN\r\nHPN->KZX\r\nHPN->VSO\r\nKZX->HPN\r\nLDW->HPN\r\nKAB->HPN\r\nHPN->LDW\r\n", "output": "home\r\n"}, {"input": "11\r\nFGH\r\nFGH->BRZ\r\nUBK->FGH\r\nQRE->FGH\r\nFGH->KQK\r\nFGH->QRE\r\nKQK->FGH\r\nFGH->UBK\r\nBRZ->FGH\r\nFGH->ALX\r\nALX->FGH\r\nFGH->KQK\r\n", "output": "contest\r\n"}, {"input": "50\r\nPFH\r\nJFV->PFH\r\nBVP->PFH\r\nPFH->BVP\r\nPFH->JFV\r\nPFH->ETQ\r\nPFH->LQJ\r\nZTO->PFH\r\nPFH->BVP\r\nPFH->RXO\r\nPFH->ZTO\r\nHWL->PFH\r\nPFH->HIV\r\nPFH->AFP\r\nPFH->HWL\r\nOBB->PFH\r\nHIV->PFH\r\nPFH->LSR\r\nAFP->PFH\r\nLQJ->PFH\r\nHWL->PFH\r\nETQ->PFH\r\nPFH->HWL\r\nLSR->PFH\r\nWBR->PFH\r\nBNZ->PFH\r\nHQR->PFH\r\nZTO->PFH\r\nPFH->WBR\r\nPFH->BYJ\r\nRXO->PFH\r\nFHZ->PFH\r\nFHZ->PFH\r\nPFN->PFH\r\nPFH->GMB\r\nPFH->JFV\r\nJFV->PFH\r\nGNZ->PFH\r\nPFH->BNZ\r\nPFH->GNZ\r\nPFH->HQR\r\nBYJ->PFH\r\nGMB->PFH\r\nPFH->FHZ\r\nPFH->FHZ\r\nPFH->ZTO\r\nPFH->UGD\r\nBVP->PFH\r\nUGD->PFH\r\nPFH->PFN\r\nPFH->OBB\r\n", "output": "home\r\n"}, {"input": "1\r\nAAK\r\nAAK->ABA\r\n", "output": "contest\r\n"}, {"input": "1\r\nXYZ\r\nXYZ->XYR\r\n", "output": "contest\r\n"}]
| false
|
stdio
| null | true
|
388/C
|
388
|
C
|
PyPy 3
|
TESTS
| 10
| 218
| 4,710,400
|
92522423
|
from sys import stdin
from collections import deque
import heapq
n = int(stdin.readline())
piles = []
for x in range(n):
a = deque([int(x) for x in stdin.readline().split()][1:])
piles.append(a)
ciel = [(-x[0],i) for i,x in enumerate(piles)]
jiro = [(-x[-1],i) for i,x in enumerate(piles)]
heapq.heapify(ciel)
heapq.heapify(jiro)
empty = set([-1])
cielTotal = 0
jiroTotal = 0
turn = True
while True:
if turn:
ind = -1
while ind in empty and ciel:
nxt,ind = heapq.heappop(ciel)
nxt = -nxt
if ind in empty:
break
piles[ind].popleft()
cielTotal += nxt
if not piles[ind]:
empty.add(ind)
else:
heapq.heappush(ciel, (-piles[ind][0], ind))
else:
ind = -1
while ind in empty and jiro:
nxt,ind = heapq.heappop(jiro)
nxt = -nxt
if ind in empty:
break
piles[ind].pop()
jiroTotal += nxt
if not piles[ind]:
empty.add(ind)
else:
heapq.heappush(jiro, (-piles[ind][-1], ind))
turn = not turn
print(cielTotal,jiroTotal)
| 43
| 46
| 102,400
|
153944789
|
import collections
import math
import sys
need_two = 0
def main():
n = int(input())
odds = []
first = 0
second = 0
for i in range(n):
c = list(map(int, input().split()))[1:]
if len(c) % 2 == 1:
odds.append(c[len(c) // 2])
first += sum(c[:len(c) // 2])
second += sum(c[len(c) // 2 + 1:])
else:
first += sum(c[:len(c) // 2])
second += sum(c[len(c) // 2:])
odds = sorted(odds)[::-1]
for i in range(len(odds)):
if i % 2 == 0:
first += odds[i]
else:
second += odds[i]
print(first, second)
if __name__ == "__main__":
t = 1 # int(input())
while t > 0:
main()
t -= 1
|
Codeforces Round 228 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Fox and Card Game
|
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
|
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
|
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
| null |
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
|
[{"input": "2\n1 100\n2 1 10", "output": "101 10"}, {"input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15"}, {"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18"}, {"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "output": "7000 7000"}]
| 2,000
|
["games", "greedy", "sortings"]
| 43
|
[{"input": "2\r\n1 100\r\n2 1 10\r\n", "output": "101 10\r\n"}, {"input": "1\r\n9 2 8 6 5 9 4 7 1 3\r\n", "output": "30 15\r\n"}, {"input": "3\r\n3 1 3 2\r\n3 5 4 6\r\n2 8 7\r\n", "output": "18 18\r\n"}, {"input": "3\r\n3 1000 1000 1000\r\n6 1000 1000 1000 1000 1000 1000\r\n5 1000 1000 1000 1000 1000\r\n", "output": "7000 7000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 0\r\n"}, {"input": "5\r\n1 3\r\n1 2\r\n1 8\r\n1 1\r\n1 4\r\n", "output": "12 6\r\n"}, {"input": "3\r\n5 1 2 3 4 5\r\n4 1 2 3 4\r\n8 1 2 3 4 5 6 7 8\r\n", "output": "19 42\r\n"}, {"input": "5\r\n5 1 1 1 1 1\r\n4 1 1 1 1\r\n3 1 1 1\r\n2 1 1\r\n1 1\r\n", "output": "8 7\r\n"}, {"input": "6\r\n2 1 1\r\n2 2 2\r\n2 3 3\r\n2 4 4\r\n2 5 5\r\n2 6 6\r\n", "output": "21 21\r\n"}, {"input": "2\r\n2 200 1\r\n3 1 100 2\r\n", "output": "301 3\r\n"}, {"input": "2\r\n3 1 1000 2\r\n3 2 1 1\r\n", "output": "1003 4\r\n"}, {"input": "4\r\n3 1 5 100\r\n3 1 5 100\r\n3 100 1 1\r\n3 100 1 1\r\n", "output": "208 208\r\n"}]
| false
|
stdio
| null | true
|
873/C
|
873
|
C
|
PyPy 3-64
|
TESTS
| 12
| 92
| 1,843,200
|
183824927
|
import sys
input = sys.stdin.readline
n, m, k = map(int, input().split())
a1, a2 = 0, 0
for w in zip(*[input()[:-1].split() for _ in range(n)]):
k = min(n, k)
c = w[:k].count('1')
a = c
b = 0
f = 0
for i in range(n-k):
if w[i+k] == '1':
c += 1
if w[i] == '1':
c -= 1
b += 1
if w[i+1] == '1':
if c > a:
a = c
f = b
a1 += a
a2 += f
print(a1, a2)
| 20
| 46
| 0
|
181353049
|
num,num2,num3=map(int,input().split())
cont1,cont2=0,0
result=[list(map(int,input().split())) for i in range(num)]
for i in zip(*result):
a,b=0,0
for j in range(num-num3+1):
f,h=sum(i[j:j + num3]),sum(i[:j])
if f>a:a,b=f,h
cont1 += a
cont2 += b
print(cont1,cont2,end=' ')
|
Educational Codeforces Round 30
|
ICPC
| 2,017
| 1
| 256
|
Strange Game On Matrix
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
|
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100).
Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
|
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
| null |
In the first example Ivan will replace the element a1, 2.
|
[{"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1"}, {"input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0"}]
| 1,600
|
["greedy", "two pointers"]
| 20
|
[{"input": "4 3 2\r\n0 1 0\r\n1 0 1\r\n0 1 0\r\n1 1 1\r\n", "output": "4 1\r\n"}, {"input": "3 2 1\r\n1 0\r\n0 1\r\n0 0\r\n", "output": "2 0\r\n"}, {"input": "3 4 2\r\n0 1 1 1\r\n1 0 1 1\r\n1 0 0 1\r\n", "output": "7 0\r\n"}, {"input": "3 57 3\r\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\r\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\r\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1\r\n", "output": "80 0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "1 0\r\n"}, {"input": "1 1 1\r\n0\r\n", "output": "0 0\r\n"}, {"input": "2 2 1\r\n0 1\r\n1 0\r\n", "output": "2 0\r\n"}, {"input": "100 1 20\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n1\r\n1\r\n1\r\n0\r\n1\r\n", "output": "13 34\r\n"}, {"input": "1 100 1\r\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1\r\n", "output": "53 0\r\n"}]
| false
|
stdio
| null | true
|
466/D
|
466
|
D
|
PyPy 3-64
|
TESTS
| 5
| 124
| 9,420,800
|
216071842
|
import sys
import os
# import time
import bisect
# import functools
import math
import random
# import re
from collections import Counter, defaultdict, deque
# from copy import deepcopy
from functools import cmp_to_key, lru_cache, reduce
from heapq import heapify, heappop, heappush, heappushpop, nlargest, nsmallest
from itertools import accumulate, combinations, permutations
from operator import add, iand, ior, itemgetter, mul, xor
# from string import ascii_lowercase, ascii_uppercase
from typing import *
input = sys.stdin.readline
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LGMI():
return list(map(lambda x: int(x) - 1, input().split()))
inf = float('inf')
# from types import GeneratorType
# def bootstrap(f, stack=[]):
# def wrappedfunc(*args, **kwargs):
# if stack:
# return f(*args, **kwargs)
# else:
# to = f(*args, **kwargs)
# while True:
# if type(to) is GeneratorType:
# stack.append(to)
# to = next(to)
# else:
# stack.pop()
# if not stack:
# break
# to = stack[-1].send(to)
# return to
# return wrappedfunc
# RANDOM = random.getrandbits(32)
# class Wrapper(int):
# def __init__(self, x):
# int.__init__(x)
# def __hash__(self):
# return super(Wrapper, self).__hash__() ^ RANDOM
_index, _sep, buffer = 0, True, []
def read_int():
global _index, buffer, _sep
if _index >= len(buffer) - 1 + _sep:
try:
s = '' if _sep else buffer[-1]
s += sys.stdin.readline(4096)
_sep = s[-1].isspace()
buffer = s.split()
except:
pass
_index = 0
_index += 1
return int(buffer[_index - 1])
n, h = read_int(), read_int()
ans, last = 1, 0
mod = 10 ** 9 + 7
for _ in range(n):
x = h - read_int()
if x < 0 or abs(x - last) > 1: print(0); exit()
if x <= last: ans *= last + 1; ans %= mod
last = x
print(ans if last <= 1 else 0)
| 28
| 77
| 1,433,600
|
231045511
|
f = lambda: map(int, input().split())
n, h = f()
s, i = 1, 0
for j in f():
j = h - j
if j < 0 or abs(j - i) > 1: exit(print(0))
if j <= i: s = s * (j + 1) % 1000000007
i = j
print(0 if i > 1 else s)
|
Codeforces Round 266 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Increase Sequence
|
Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of "adding one on the segment [l, r]": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 ≠ l2 and r1 ≠ r2.
How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.
|
The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).
|
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
| null | null |
[{"input": "3 2\n1 1 1", "output": "4"}, {"input": "5 1\n1 1 1 1 1", "output": "1"}, {"input": "4 3\n3 2 1 1", "output": "0"}]
| 2,100
|
["combinatorics", "dp"]
| 28
|
[{"input": "3 2\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n", "output": "1\r\n"}, {"input": "4 3\r\n3 2 1 1\r\n", "output": "0\r\n"}, {"input": "1 2000\r\n2000\r\n", "output": "1\r\n"}, {"input": "3 2\r\n2 1 1\r\n", "output": "2\r\n"}, {"input": "3 4\r\n4 3 2\r\n", "output": "0\r\n"}, {"input": "10 6\r\n5 4 4 4 5 4 4 4 4 5\r\n", "output": "972\r\n"}, {"input": "10 6\r\n5 4 4 4 5 4 4 4 4 5\r\n", "output": "972\r\n"}, {"input": "7 4\r\n3 3 4 4 4 3 3\r\n", "output": "4\r\n"}, {"input": "3 6\r\n5 6 5\r\n", "output": "1\r\n"}, {"input": "3 2\r\n1 0 1\r\n", "output": "2\r\n"}, {"input": "6 1\r\n0 1 0 0 0 1\r\n", "output": "4\r\n"}, {"input": "6 3\r\n2 1 2 2 1 2\r\n", "output": "8\r\n"}, {"input": "99 1999\r\n1999 1998 1997 1996 1995 1994 1993 1992 1991 1990 1989 1988 1987 1986 1985 1984 1983 1982 1981 1980 1979 1978 1977 1976 1975 1974 1973 1972 1971 1970 1969 1968 1967 1966 1965 1964 1963 1962 1961 1960 1959 1958 1957 1956 1955 1954 1953 1952 1951 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999\r\n", "output": "726372166\r\n"}, {"input": "9 4\r\n2 3 2 2 2 3 3 3 2\r\n", "output": "0\r\n"}, {"input": "1 349\r\n348\r\n", "output": "1\r\n"}, {"input": "10 20\r\n20 19 18 17 16 15 16 17 18 19\r\n", "output": "120\r\n"}, {"input": "19 10\r\n10 9 8 7 6 5 4 3 2 10 2 3 4 5 6 7 8 9 10\r\n", "output": "0\r\n"}, {"input": "19 11\r\n10 9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9 10\r\n", "output": "264539520\r\n"}, {"input": "7 3\r\n2 1 1 2 1 1 2\r\n", "output": "36\r\n"}]
| false
|
stdio
| null | true
|
187/A
|
187
|
A
|
PyPy 3
|
TESTS
| 5
| 186
| 23,142,400
|
31480206
|
n = int(input())
l1 = [int(x) for x in input().split()]
l2 = [int(x) for x in input().split()]
used = set()
j = len(l1)-1
answ = 0
for i in range(len(l2)-1, -1, -1):
if l2[i] in used:
continue
while j >= 0 and l1[j] != l2[i]:
used.add(l1[j])
j-=1
answ += 1
if j >= 0 and l1[j] == l2[i]:
j-=1
print(answ)
| 58
| 340
| 31,539,200
|
203854212
|
import sys
input = lambda: sys.stdin.readline().rstrip()
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
pre = -1
j = 0
for i in range(N):
while j<N and A[i]!=B[j]:
j+=1
if j<N and A[i]==B[j]:
pre = i+1
print(N-pre)
|
Codeforces Round 119 (Div. 1)
|
CF
| 2,012
| 2
| 256
|
Permutations
|
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
|
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
|
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
| null |
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
- 1 5 2 3 4
- 1 4 5 2 3
- 1 3 4 5 2
- 1 2 3 4 5
|
[{"input": "3\n3 2 1\n1 2 3", "output": "2"}, {"input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1"}, {"input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3"}]
| 1,500
|
["greedy"]
| 58
|
[{"input": "3\r\n3 2 1\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 5 2 3 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 2 3 4\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "7\r\n6 1 7 3 4 5 2\r\n6 1 7 3 4 5 2\r\n", "output": "0\r\n"}, {"input": "10\r\n5 8 1 10 3 6 2 9 7 4\r\n4 2 6 3 1 9 10 5 8 7\r\n", "output": "8\r\n"}, {"input": "10\r\n1 6 10 3 4 9 2 5 8 7\r\n7 5 1 6 10 3 4 8 9 2\r\n", "output": "3\r\n"}, {"input": "10\r\n2 1 10 3 7 8 5 6 9 4\r\n6 9 2 4 1 10 3 7 8 5\r\n", "output": "3\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "0\r\n"}, {"input": "20\r\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\r\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1\r\n", "output": "19\r\n"}]
| false
|
stdio
| null | true
|
187/A
|
187
|
A
|
Python 3
|
TESTS
| 5
| 186
| 0
|
56744638
|
class CodeforcesTask187ASolution:
def __init__(self):
self.result = ''
self.sequence1 = []
self.sequence2 = []
def read_input(self):
input()
self.sequence1 = [int(x) for x in input().split(" ")]
self.sequence2 = [int(x) for x in input().split(" ")]
def process_task(self):
dists = []
for x in range(len(self.sequence1)):
if self.sequence1[x] == self.sequence2:
dists.append(0)
else:
dist = self.sequence2.index(self.sequence1[x]) - x
if dist < 0:
dist = len(self.sequence1) - x
dists.append(dist)
self.result = str(max(dists))
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask187ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
| 58
| 436
| 25,088,000
|
127588617
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = j = 0
for j in range(n):
if b[j] == a[i]:
i = i + 1
print(n-i)
|
Codeforces Round 119 (Div. 1)
|
CF
| 2,012
| 2
| 256
|
Permutations
|
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
|
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
|
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
| null |
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
- 1 5 2 3 4
- 1 4 5 2 3
- 1 3 4 5 2
- 1 2 3 4 5
|
[{"input": "3\n3 2 1\n1 2 3", "output": "2"}, {"input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1"}, {"input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3"}]
| 1,500
|
["greedy"]
| 58
|
[{"input": "3\r\n3 2 1\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 5 2 3 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 2 3 4\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "7\r\n6 1 7 3 4 5 2\r\n6 1 7 3 4 5 2\r\n", "output": "0\r\n"}, {"input": "10\r\n5 8 1 10 3 6 2 9 7 4\r\n4 2 6 3 1 9 10 5 8 7\r\n", "output": "8\r\n"}, {"input": "10\r\n1 6 10 3 4 9 2 5 8 7\r\n7 5 1 6 10 3 4 8 9 2\r\n", "output": "3\r\n"}, {"input": "10\r\n2 1 10 3 7 8 5 6 9 4\r\n6 9 2 4 1 10 3 7 8 5\r\n", "output": "3\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "0\r\n"}, {"input": "20\r\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\r\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1\r\n", "output": "19\r\n"}]
| false
|
stdio
| null | true
|
187/A
|
187
|
A
|
Python 3
|
TESTS
| 5
| 92
| 0
|
14346657
|
n = int(input())
permutation = list(map(int, input().split()))
target = list(map(int, input().split()))
cont = 0
for i in range(0, n):
if (permutation[i] != target[i]):
cont += 1
for j in range(i + 1, n):
if (permutation[j] == target[i]):
aux = permutation[0:i]
aux.append(permutation[j])
aux.extend(permutation[i:j])
aux.extend(permutation[j + 1: n + 1])
permutation = aux
print(cont)
| 58
| 496
| 39,116,800
|
170973235
|
n=int(input())
arr=list(map(int,input().split()))
out=list(map(int,input().split()))
mp=dict()
ans=0
for i in range(n):
mp[arr[i]]=i
for i in range(n):
mp[out[i]]+=ans
if i==mp[out[i]]:continue
ans+=1
print(ans)
|
Codeforces Round 119 (Div. 1)
|
CF
| 2,012
| 2
| 256
|
Permutations
|
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
|
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
|
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
| null |
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
- 1 5 2 3 4
- 1 4 5 2 3
- 1 3 4 5 2
- 1 2 3 4 5
|
[{"input": "3\n3 2 1\n1 2 3", "output": "2"}, {"input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1"}, {"input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3"}]
| 1,500
|
["greedy"]
| 58
|
[{"input": "3\r\n3 2 1\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 5 2 3 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 2 3 4\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "7\r\n6 1 7 3 4 5 2\r\n6 1 7 3 4 5 2\r\n", "output": "0\r\n"}, {"input": "10\r\n5 8 1 10 3 6 2 9 7 4\r\n4 2 6 3 1 9 10 5 8 7\r\n", "output": "8\r\n"}, {"input": "10\r\n1 6 10 3 4 9 2 5 8 7\r\n7 5 1 6 10 3 4 8 9 2\r\n", "output": "3\r\n"}, {"input": "10\r\n2 1 10 3 7 8 5 6 9 4\r\n6 9 2 4 1 10 3 7 8 5\r\n", "output": "3\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "0\r\n"}, {"input": "20\r\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\r\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1\r\n", "output": "19\r\n"}]
| false
|
stdio
| null | true
|
187/A
|
187
|
A
|
PyPy 3-64
|
TESTS
| 5
| 92
| 0
|
176234627
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = [0]*(n+1)
for i, j in enumerate(b):
d[j] = i
c = 0
f = 0
for i in range(n):
x = d[a[i]]
if x == i:
f += 1
if c == 0:
continue
c = max(c, n-1-i)
elif x > i:
c = max(c, x-i)
else:
c = max(c, n-1-i)
if f == n:
print(0)
else:
print(c)
| 58
| 654
| 18,124,800
|
41983347
|
u,d,x,n=0,0,0,int(input())
# n = [0 for i in range(n+10)]
f=list(map(int,input().split()))
s=list(map(int,input().split()))
while d<n:
if s[d]!=f[u]:
x+=1
d+=1
else:
d+=1
u+=1
print(x)
|
Codeforces Round 119 (Div. 1)
|
CF
| 2,012
| 2
| 256
|
Permutations
|
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
|
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
|
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
| null |
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
- 1 5 2 3 4
- 1 4 5 2 3
- 1 3 4 5 2
- 1 2 3 4 5
|
[{"input": "3\n3 2 1\n1 2 3", "output": "2"}, {"input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1"}, {"input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3"}]
| 1,500
|
["greedy"]
| 58
|
[{"input": "3\r\n3 2 1\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 5 2 3 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 2 3 4\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "7\r\n6 1 7 3 4 5 2\r\n6 1 7 3 4 5 2\r\n", "output": "0\r\n"}, {"input": "10\r\n5 8 1 10 3 6 2 9 7 4\r\n4 2 6 3 1 9 10 5 8 7\r\n", "output": "8\r\n"}, {"input": "10\r\n1 6 10 3 4 9 2 5 8 7\r\n7 5 1 6 10 3 4 8 9 2\r\n", "output": "3\r\n"}, {"input": "10\r\n2 1 10 3 7 8 5 6 9 4\r\n6 9 2 4 1 10 3 7 8 5\r\n", "output": "3\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "0\r\n"}, {"input": "20\r\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\r\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1\r\n", "output": "19\r\n"}]
| false
|
stdio
| null | true
|
324/A1
|
331
|
A1
|
Python 3
|
TESTS1
| 7
| 62
| 0
|
4100809
|
import sys
fin = sys.stdin
n = int(fin.readline())
a = list(map(int, fin.readline().split()))
table = dict()
s = [0] * (n + 1)
for i in range(n):
if a[i] >= 0:
rec = table.setdefault(a[i], { 'begin' : i, 'end' : i, 'sum' : a[i] })
rec['end'] = i
s[i + 1] = s[i] + a[i]
rec['sum'] = s[rec['end'] + 1] - s[rec['begin']]
else:
s[i + 1] += s[i]
m = len(table)
if m == 0:
ms = max(a)
mr = [i + 1 for i in range(n) if a[i] != ms]
else:
ms = max(v['sum'] for v in table.values())
for mi in table.keys():
if table[mi]['sum'] == ms:
break
mr = [i + 1 for i in range(n) if a[i] < 0 or i < table[mi]['begin'] or i > table[mi]['end']]
print(ms, len(mr))
print(' '.join(map(str, mr)))
| 18
| 154
| 0
|
29595694
|
n=int(input())
a=tuple(map(int,input().split()))
c={}
p={}
s=x=y=0
m=-1e18
for i in range(0,len(a)):
d=c.get(a[i])
if d!=None and s-d+a[i]*2>m:
m=s-d+a[i]*2
x,y=p.get(a[i]),i
if(a[i]>0):s+=a[i]
if p.get(a[i])==None:
p[a[i]]=i
c[a[i]]=s
a=[str(i+1) for i in range(0,len(a)) if i!=x and i!=y and (a[i]<0 or i<x or i>y)]
print(m,len(a))
print(" ".join(a))
|
ABBYY Cup 3.0 - Finals
|
ICPC
| 2,013
| 2
| 256
|
Oh Sweet Beaverette
|
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
— Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
- The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
- the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
- and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
|
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
- to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);
- to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).
|
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
| null | null |
[{"input": "5\n1 2 3 1 2", "output": "8 1\n1"}, {"input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5"}]
| 1,400
|
[]
| 18
|
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "8 1\r\n1 "}, {"input": "5\r\n1 -2 3 1 -2\r\n", "output": "5 2\r\n2 5 "}, {"input": "2\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "3\r\n0 -1 0\r\n", "output": "0 1\r\n2 "}, {"input": "3\r\n1 1 1\r\n", "output": "3 0\r\n"}, {"input": "4\r\n-1 1 1 -1\r\n", "output": "2 2\r\n1 4 "}, {"input": "4\r\n-1 1 -1 1\r\n", "output": "2 2\r\n1 3 "}, {"input": "2\r\n-1 -1\r\n", "output": "-2 0\r\n"}, {"input": "3\r\n-1 0 -1\r\n", "output": "-2 0\r\n"}, {"input": "6\r\n-1 3 3 5 5 -1\r\n", "output": "14 0\r\n"}, {"input": "2\r\n-1000000000 -1000000000\r\n", "output": "-2000000000 0\r\n"}, {"input": "3\r\n-1000000000 -1000000000 -1000000000\r\n", "output": "-2000000000 1\r\n3 "}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "3000000000 0\r\n"}, {"input": "10\r\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352\r\n", "output": "-252577666 8\r\n1 4 5 6 7 8 9 10 "}, {"input": "20\r\n-808998072 733614990 579897311 -337992089 579897311 120800519 -337992089 -803027570 733614990 -686536765 733614990 -803027570 -803027570 733614990 120800519 -803027570 -686536765 579897311 -808998072 -686536765\r\n", "output": "4215055101 13\r\n1 4 7 8 10 12 13 15 16 17 18 19 20 "}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
with open(input_path) as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
with open(submission_output_path) as f:
lines = f.readlines()
if not lines:
print(0)
return
sum_line = lines[0].strip().split()
if len(sum_line) != 2:
print(0)
return
try:
sum_claimed, k = map(int, sum_line)
except:
print(0)
return
cut_list = list(map(int, lines[1].split())) if len(lines) > 1 else []
if len(cut_list) != k:
print(0)
return
cut_set = set(cut_list)
if len(cut_set) != k:
print(0)
return
for num in cut_list:
if not (1 <= num <= n):
print(0)
return
remaining = [i-1 for i in range(1, n+1) if i not in cut_set]
if len(remaining) < 2:
print(0)
return
first_val = a[remaining[0]]
last_val = a[remaining[-1]]
if first_val != last_val:
print(0)
return
sum_remaining = sum(a[i] for i in remaining)
if sum_remaining != sum_claimed:
print(0)
return
prefix = [0] * (n + 1)
for i in range(n):
prefix[i+1] = prefix[i] + max(a[i], 0)
groups = defaultdict(list)
for idx, val in enumerate(a):
groups[val].append(idx)
max_total = -float('inf')
for v, indices in groups.items():
m = len(indices)
if m < 2:
continue
indices.sort()
min_val = prefix[indices[0] + 1]
current_max = -float('inf')
for j in range(1, m):
current_i = indices[j]
current_sum = 2 * v + (prefix[current_i] - min_val)
if current_sum > current_max:
current_max = current_sum
if j < m - 1:
next_val = prefix[indices[j] + 1]
if next_val < min_val:
min_val = next_val
if current_max > max_total:
max_total = current_max
if sum_claimed != max_total:
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
|
765/A
|
765
|
A
|
Python 3
|
TESTS
| 4
| 61
| 307,200
|
107244499
|
def solve(home, arr):
for i,f in enumerate(arr):
found = False
if f[0] == home:
for j,t in enumerate(arr):
if t[1] == home and t[0] == f[1]:
found = True
break
if not found:
return 'contest'
return 'home'
def main():
n = int(input())
home = input()
arr = []
for i in range(n):
arr.append(input().split('->'))
print(solve(home,arr))
main()
| 23
| 46
| 0
|
161136608
|
n = int(input())
home_airport = input()
flights = []
for i in range(n):
flight = input()
departure, arrival = flight.split('->')
flights.append((departure, arrival))
if n % 2 == 0:
print('home')
else:
print('contest')
|
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
|
CF
| 2,017
| 2
| 512
|
Neverending competitions
|
There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back.
Jinotega's best friends, team Base have found a list of their itinerary receipts with information about departure and arrival airports. Now they wonder, where is Jinotega now: at home or at some competition far away? They know that:
- this list contains all Jinotega's flights in this year (in arbitrary order),
- Jinotega has only flown from his hometown to a snooker contest and back,
- after each competition Jinotega flies back home (though they may attend a competition in one place several times),
- and finally, at the beginning of the year Jinotega was at home.
Please help them to determine Jinotega's location!
|
In the first line of input there is a single integer n: the number of Jinotega's flights (1 ≤ n ≤ 100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next n lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name of departure airport "YYY" is the name of arrival airport. Exactly one of these airports is Jinotega's home airport.
It is guaranteed that flights information is consistent with the knowledge of Jinotega's friends, which is described in the main part of the statement.
|
If Jinotega is now at home, print "home" (without quotes), otherwise print "contest".
| null |
In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list.
|
[{"input": "4\nSVO\nSVO->CDG\nLHR->SVO\nSVO->LHR\nCDG->SVO", "output": "home"}, {"input": "3\nSVO\nSVO->HKT\nHKT->SVO\nSVO->RAP", "output": "contest"}]
| 900
|
["implementation", "math"]
| 23
|
[{"input": "4\r\nSVO\r\nSVO->CDG\r\nLHR->SVO\r\nSVO->LHR\r\nCDG->SVO\r\n", "output": "home\r\n"}, {"input": "3\r\nSVO\r\nSVO->HKT\r\nHKT->SVO\r\nSVO->RAP\r\n", "output": "contest\r\n"}, {"input": "1\r\nESJ\r\nESJ->TSJ\r\n", "output": "contest\r\n"}, {"input": "2\r\nXMR\r\nFAJ->XMR\r\nXMR->FAJ\r\n", "output": "home\r\n"}, {"input": "3\r\nZIZ\r\nDWJ->ZIZ\r\nZIZ->DWJ\r\nZIZ->DWJ\r\n", "output": "contest\r\n"}, {"input": "10\r\nPVO\r\nDMN->PVO\r\nDMN->PVO\r\nPVO->DMN\r\nDMN->PVO\r\nPVO->DMN\r\nPVO->DMN\r\nPVO->DMN\r\nDMN->PVO\r\nPVO->DMN\r\nDMN->PVO\r\n", "output": "home\r\n"}, {"input": "11\r\nIAU\r\nIAU->RUQ\r\nIAU->RUQ\r\nRUQ->IAU\r\nRUQ->IAU\r\nIAU->RUQ\r\nRUQ->IAU\r\nIAU->RUQ\r\nRUQ->IAU\r\nIAU->RUQ\r\nIAU->RUQ\r\nRUQ->IAU\r\n", "output": "contest\r\n"}, {"input": "10\r\nHPN\r\nDFI->HPN\r\nHPN->KAB\r\nHPN->DFI\r\nVSO->HPN\r\nHPN->KZX\r\nHPN->VSO\r\nKZX->HPN\r\nLDW->HPN\r\nKAB->HPN\r\nHPN->LDW\r\n", "output": "home\r\n"}, {"input": "11\r\nFGH\r\nFGH->BRZ\r\nUBK->FGH\r\nQRE->FGH\r\nFGH->KQK\r\nFGH->QRE\r\nKQK->FGH\r\nFGH->UBK\r\nBRZ->FGH\r\nFGH->ALX\r\nALX->FGH\r\nFGH->KQK\r\n", "output": "contest\r\n"}, {"input": "50\r\nPFH\r\nJFV->PFH\r\nBVP->PFH\r\nPFH->BVP\r\nPFH->JFV\r\nPFH->ETQ\r\nPFH->LQJ\r\nZTO->PFH\r\nPFH->BVP\r\nPFH->RXO\r\nPFH->ZTO\r\nHWL->PFH\r\nPFH->HIV\r\nPFH->AFP\r\nPFH->HWL\r\nOBB->PFH\r\nHIV->PFH\r\nPFH->LSR\r\nAFP->PFH\r\nLQJ->PFH\r\nHWL->PFH\r\nETQ->PFH\r\nPFH->HWL\r\nLSR->PFH\r\nWBR->PFH\r\nBNZ->PFH\r\nHQR->PFH\r\nZTO->PFH\r\nPFH->WBR\r\nPFH->BYJ\r\nRXO->PFH\r\nFHZ->PFH\r\nFHZ->PFH\r\nPFN->PFH\r\nPFH->GMB\r\nPFH->JFV\r\nJFV->PFH\r\nGNZ->PFH\r\nPFH->BNZ\r\nPFH->GNZ\r\nPFH->HQR\r\nBYJ->PFH\r\nGMB->PFH\r\nPFH->FHZ\r\nPFH->FHZ\r\nPFH->ZTO\r\nPFH->UGD\r\nBVP->PFH\r\nUGD->PFH\r\nPFH->PFN\r\nPFH->OBB\r\n", "output": "home\r\n"}, {"input": "1\r\nAAK\r\nAAK->ABA\r\n", "output": "contest\r\n"}, {"input": "1\r\nXYZ\r\nXYZ->XYR\r\n", "output": "contest\r\n"}]
| false
|
stdio
| null | true
|
877/E
|
877
|
E
|
PyPy 3
|
TESTS
| 6
| 93
| 0
|
180474384
|
import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def euler_tour():
s = []
s.append(1)
now = [0] * (n + 1)
t = 1
t1, t2 = [-1] * (n + 1), [-1] * (n + 1)
v = []
while s:
i = s[-1]
if t1[i] == -1:
v.append(i)
t1[i] = t
t += 1
if now[i] == len(G[i]):
t2[i] = t
t += 1
s.pop()
else:
s.append(G[i][now[i]])
now[i] += 1
return t1, t2, v
def f(i):
if lazy[i]:
tree[i] = cnt[i] - tree[i]
lazy[i] = 0
if i < l1:
lazy[i << 1] ^= 1
lazy[i << 1 ^ 1] ^= 1
def update(l, r, s):
q, ll, rr, i = [1], [0], [l1 - 1], 0
while len(q) ^ i:
j = q[i]
l0, r0 = ll[i], rr[i]
if l <= l0 and r0 <= r:
lazy[j] ^= s
f(j)
i += 1
continue
m0 = (l0 + r0) >> 1
f(j)
if l <= m0 and l0 <= r:
q.append(j << 1)
ll.append(l0)
rr.append(m0)
if l <= r0 and m0 + 1 <= r:
q.append(j << 1 ^ 1)
ll.append(m0 + 1)
rr.append(r0)
i += 1
for i in q[::-1]:
if i < l1:
j, k = i << 1, i << 1 ^ 1
tree[i] = tree[j] + tree[k]
return
def get_sum(s, t):
update(s, t, 0)
s += l1
t += l1
ans = 0
while s <= t:
if s % 2:
ans += tree[s]
s += 1
s >>= 1
if not t % 2:
ans += tree[t]
t -= 1
t >>= 1
return ans
n = int(input())
p = [0] * 2 + list(map(int, input().split()))
t = [0] + list(map(int, input().split()))
G = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
G[p[i]].append(i)
t1, t2, v = euler_tour()
u = [t1[i] for i in v]
l1 = pow(2, n.bit_length())
l2 = 2 * l1
tree, lazy = [0] * l2, [0] * l2
for i in range(n):
tree[i + l1] = t[v[i]]
for i in range(l1 - 1, 0, -1):
tree[i] = tree[2 * i] + tree[2 * i + 1]
cnt = [0] * l2
for i in range(n):
cnt[i + l1] = 1
for i in range(l1 - 1, 0, -1):
cnt[i] = cnt[2 * i] + cnt[2 * i + 1]
q = int(input())
ans = []
for _ in range(q):
c, x = input().rstrip().decode().split()
x = int(x)
l = bisect.bisect_left(u, t1[x])
r = bisect.bisect_left(u, t2[x]) - 1
if c == "pow":
update(l, r, 1)
else:
ans0 = get_sum(l, r)
ans.append(ans0)
sys.stdout.write("\n".join(map(str, ans)))
| 57
| 1,544
| 90,316,800
|
209145397
|
import math
import sys
from bisect import *
from collections import *
from functools import *
from heapq import *
from itertools import *
from random import *
from string import *
from types import GeneratorType
# region fastio
input = lambda: sys.stdin.readline().rstrip()
sint = lambda: int(input())
mint = lambda: map(int, input().split())
ints = lambda: list(map(int, input().split()))
# print = lambda d: sys.stdout.write(str(d) + "\n")
# endregion fastio
# # region interactive
# def printQry(a, b) -> None:
# sa = str(a)
# sb = str(b)
# print(f"? {sa} {sb}", flush = True)
# def printAns(ans) -> None:
# s = str(ans)
# print(f"! {s}", flush = True)
# # endregion interactive
# # region dfsconvert
# def bootstrap(f, stack=[]):
# def wrappedfunc(*args, **kwargs):
# if stack:
# return f(*args, **kwargs)
# else:
# to = f(*args, **kwargs)
# while True:
# if type(to) is GeneratorType:
# stack.append(to)
# to = next(to)
# else:
# stack.pop()
# if not stack:
# break
# to = stack[-1].send(to)
# return to
# return wrappedfunc
# # endregion dfsconvert
# MOD = 998244353
# MOD = 10 ** 9 + 7
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
def dfs(graph, start=0):
n = len(graph)
dfn = [-1] * n
sz = [1] * n
visited, finished = [False] * n, [False] * n
time = n - 1
stack = [start]
while stack:
start = stack[-1]
# push unvisited children into stack
if not visited[start]:
visited[start] = True
for child in graph[start]:
if not visited[child]:
stack.append(child)
else:
stack.pop()
dfn[start] = time
time -= 1
# update with finished children
for child in graph[start]:
if finished[child]:
sz[start] += sz[child]
finished[start] = True
return dfn, sz
class SegTree:
def __init__(self, nums: list) -> None:
n = len(nums)
self.n = n
self.tree = [0] * (4 * n)
self.lazy = [0] * (4 * n)
self.build(nums, 1, 1, n)
def build(self, nums: list, o: int, l: int, r: int) -> None:
if l == r:
self.tree[o] = nums[l - 1]
return
m = (l + r) >> 1
self.build(nums, o * 2, l, m)
self.build(nums, o * 2 + 1, m + 1, r)
self.pushUp(o)
def xor(self, o: int, l: int, r: int) -> None:
self.tree[o] = r - l + 1 - self.tree[o] # 异或反转
self.lazy[o] ^= 1
def do(self, o: int, l: int, r: int, val: int) -> None:
self.tree[o] = r - l + 1 - self.tree[o] # 异或反转
self.lazy[o] ^= 1
def op(self, a: int, b: int) -> int:
# + - * / max min
return a + b
def maintain(self, o: int, l: int, r: int) -> None:
self.tree[o] = self.op(self.tree[l], self.tree[r])
self.lazy[o] ^= 1
def pushUp(self, o: int) -> None:
self.tree[o] = self.tree[o * 2] + self.tree[o * 2 + 1]
def pushDown(self, o: int, l: int, r: int) -> None:
m = (l + r) >> 1
self.do(o * 2, l, m, self.lazy[o]) # 调用相应的更新操作方法
self.do(o * 2 + 1, m + 1, r, self.lazy[o])
self.lazy[o] = 0
def update(self, o: int, l: int, r: int, L: int, R: int, val: int) -> None:
if L <= l and r <= R: # 当前区间已完全包含在更新区间,不需要继续往下更新,存在lazy
self.xor(o, l, r)
return
if self.lazy[o]: # 当前lazyd存在更新,往下传递
self.pushDown(o, l, r)
m = (l + r) >> 1
if m >= L: # 左节点在更新区间
self.update(o * 2, l, m, L, R, val)
if m < R: # 右节点在更新区间
self.update(o * 2 + 1, m + 1, r, L, R, val)
self.pushUp(o) # 从左右节点更新当前节点值
def query(self, o: int, l: int, r: int, L: int, R: int) -> int:
if R < l or L > r:
return 0
if L <= l and r <= R:
return self.tree[o]
if self.lazy[o]: # 当前lazyd存在更新,往下传递
self.pushDown(o, l, r)
m = (l + r) >> 1
res = 0
if m >= L: # 左节点在查询区间
res += self.query(o * 2, l, m, L, R)
if m < R: # 右节点在查询区间
res += self.query(o * 2 + 1, m + 1, r, L, R)
return res
def solve() -> None:
n = sint()
p = ints()
# 建图
g = [[] for _ in range(n)]
for i, u in enumerate(p, 1):
g[u - 1].append(i)
# DFN序
dfn, sz = dfs(g)
# print(dfn)
# print(sz)
t = ints()
nums = [0] * n
for i, v in enumerate(t):
nums[dfn[i]] = v
# print(nums)
st = SegTree(nums)
for _ in range(sint()):
qry = input().split()
u = int(qry[1]) - 1
if qry[0] == "get":
print(st.query(1, 1, n, dfn[u] + 1, dfn[u] + sz[u]))
else:
st.update(1, 1, n, dfn[u] + 1, dfn[u] + sz[u], 1)
# for _ in range(int(input())):
solve()
|
Codeforces Round 442 (Div. 2)
|
CF
| 2,017
| 2
| 256
|
Danil and a Part-time Job
|
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher.
Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on.
Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages.
There are two types of tasks:
1. pow v describes a task to switch lights in the subtree of vertex v.
2. get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages.
A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v.
Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
|
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i.
The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and 0 otherwise.
The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks.
The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
|
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
| null |
The tree before the task pow 1. The tree after the task pow 1.
|
[{"input": "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4", "output": "2\n0\n0\n1\n2\n1\n1\n0"}]
| 2,000
|
["bitmasks", "data structures", "trees"]
| 57
|
[{"input": "4\r\n1 1 1\r\n1 0 0 1\r\n9\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\npow 1\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\n", "output": "2\r\n0\r\n0\r\n1\r\n2\r\n1\r\n1\r\n0\r\n"}, {"input": "1\r\n\r\n1\r\n4\r\npow 1\r\nget 1\r\npow 1\r\nget 1\r\n", "output": "0\r\n1\r\n"}, {"input": "10\r\n1 2 1 3 4 5 6 8 5\r\n1 0 0 0 1 0 0 0 1 0\r\n10\r\npow 10\r\npow 4\r\npow 10\r\npow 7\r\npow 10\r\npow 10\r\npow 4\r\npow 8\r\npow 7\r\npow 1\r\n", "output": ""}, {"input": "10\r\n1 2 3 4 2 4 1 7 8\r\n1 1 0 1 1 0 0 0 1 1\r\n10\r\npow 1\r\nget 2\r\npow 2\r\npow 8\r\nget 6\r\npow 6\r\npow 10\r\nget 6\r\npow 8\r\npow 3\r\n", "output": "3\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 1 4 5 3 5 6 3\r\n0 0 0 0 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 4\r\nget 7\r\nget 3\r\npow 2\r\npow 5\r\npow 2\r\nget 7\r\npow 6\r\nget 10\r\n", "output": "0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n"}, {"input": "10\r\n1 1 3 1 3 1 4 6 3\r\n0 1 1 1 1 1 1 1 0 0\r\n10\r\nget 9\r\nget 10\r\nget 4\r\nget 5\r\nget 5\r\nget 5\r\nget 10\r\nget 7\r\nget 5\r\nget 2\r\n", "output": "0\r\n0\r\n2\r\n1\r\n1\r\n1\r\n0\r\n1\r\n1\r\n1\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 8\r\n0 0 0 0 1 1 1 1 0 0\r\n10\r\npow 3\r\nget 1\r\npow 9\r\nget 1\r\nget 1\r\nget 8\r\npow 8\r\npow 4\r\nget 10\r\npow 2\r\n", "output": "4\r\n3\r\n3\r\n1\r\n0\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 9\r\n1 1 0 1 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 6\r\nget 4\r\nget 2\r\nget 1\r\nget 2\r\nget 6\r\nget 9\r\nget 10\r\nget 7\r\n", "output": "3\r\n0\r\n1\r\n3\r\n4\r\n3\r\n0\r\n0\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 3 3 5 5 7 7 9\r\n0 0 1 1 0 0 1 1 0 0\r\n10\r\npow 8\r\npow 4\r\npow 8\r\npow 5\r\npow 1\r\npow 2\r\npow 6\r\npow 6\r\npow 3\r\npow 2\r\n", "output": ""}, {"input": "10\r\n1 1 2 2 3 3 5 5 6\r\n1 1 1 1 0 0 1 1 0 0\r\n10\r\nget 2\r\nget 8\r\nget 10\r\nget 5\r\nget 5\r\npow 10\r\nget 10\r\nget 1\r\nget 7\r\npow 4\r\n", "output": "3\r\n1\r\n0\r\n1\r\n1\r\n1\r\n7\r\n1\r\n"}, {"input": "10\r\n1 1 2 2 3 3 4 4 5\r\n1 1 0 1 0 0 0 0 0 0\r\n10\r\nget 2\r\nget 5\r\npow 2\r\npow 4\r\nget 2\r\nget 4\r\npow 7\r\nget 10\r\npow 5\r\nget 6\r\n", "output": "2\r\n0\r\n3\r\n1\r\n1\r\n0\r\n"}]
| false
|
stdio
| null | true
|
960/B
|
960
|
B
|
PyPy 3
|
TESTS
| 8
| 93
| 1,433,600
|
108174168
|
n,k1,k2=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
k=k1+k2
c=list()
for i in range(n):
c.append(abs(a[i]-b[i]))
c.sort(reverse=True)
i=0
r=n
while(r!=0 and k!=0):
if(c[i%r]>0):
c[i%r]-=1
k-=1
i+=1
if(c[i%r]==0):
del c[i%r]
r-=1
s=0
for i in range(r):
s+=c[i]**2
if(k%2!=0):
s+=1
print(s)
| 80
| 92
| 2,969,600
|
188262926
|
from heapq import heapify, heappop, heappush
def solve():
n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = [-abs(a[i] - b[i]) for i in range(n)]
heapify(d)
for i in range(k1+k2):
# print(d)
item = heappop(d)
item = -abs(item + 1)
heappush(d, item)
# print(d)
print(sum([di**2 for di in d]))
solve()
|
Divide by Zero 2018 and Codeforces Round 474 (Div. 1 + Div. 2, combined)
|
CF
| 2,018
| 1
| 256
|
Minimize the error
|
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $$E = \sum_{i=1}^{n} (a_i - b_i)^2$$. You have to perform exactly k1 operations on array A and exactly k2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k1 operations on array A and k2 operations on array B have been performed.
|
The first line contains three space-separated integers n (1 ≤ n ≤ 103), k1 and k2 (0 ≤ k1 + k2 ≤ 103, k1 and k2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — array A.
Third line contains n space separated integers b1, b2, ..., bn ( - 106 ≤ bi ≤ 106)— array B.
|
Output a single integer — the minimum possible value of $$\sum_{i=1}^{n}(a_i - b_i)^2$$ after doing exactly k1 operations on array A and exactly k2 operations on array B.
| null |
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)2 + (2 - 3)2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)2 + (2 - 2)2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)2 + (4 - 4)2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)2 + (4 - 5)2 = 1.
|
[{"input": "2 0 0\n1 2\n2 3", "output": "2"}, {"input": "2 1 0\n1 2\n2 2", "output": "0"}, {"input": "2 5 7\n3 4\n14 4", "output": "1"}]
| 1,500
|
["data structures", "greedy", "sortings"]
| 80
|
[{"input": "2 0 0\r\n1 2\r\n2 3\r\n", "output": "2"}, {"input": "2 1 0\r\n1 2\r\n2 2\r\n", "output": "0"}, {"input": "2 5 7\r\n3 4\r\n14 4\r\n", "output": "1"}, {"input": "2 0 1\r\n1 2\r\n2 2\r\n", "output": "0"}, {"input": "2 1 1\r\n0 0\r\n1 1\r\n", "output": "0"}, {"input": "5 5 5\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n", "output": "0"}, {"input": "3 4 5\r\n1 2 3\r\n3 2 1\r\n", "output": "1"}, {"input": "3 1000 0\r\n1 2 3\r\n-1000 -1000 -1000\r\n", "output": "1341346"}, {"input": "10 300 517\r\n-6 -2 6 5 -3 8 9 -10 8 6\r\n5 -9 -2 6 1 4 6 -2 5 -3\r\n", "output": "1"}, {"input": "10 819 133\r\n87 22 30 89 82 -97 -52 25 76 -22\r\n-20 95 21 25 2 -3 45 -7 -98 -56\r\n", "output": "0"}, {"input": "10 10 580\r\n302 -553 -281 -299 -270 -890 -989 -749 -418 486\r\n735 330 6 725 -984 209 -855 -786 -502 967\r\n", "output": "2983082"}, {"input": "10 403 187\r\n9691 -3200 3016 3540 -9475 8840 -4705 7940 6293 -2631\r\n-2288 9129 4067 696 -6754 9869 -5747 701 3344 -3426\r\n", "output": "361744892"}, {"input": "10 561 439\r\n76639 67839 10670 -23 -18393 65114 46538 67596 86615 90480\r\n50690 620 -33631 -75857 75634 91321 -81662 -93668 -98557 -43621\r\n", "output": "116776723778"}, {"input": "10 765 62\r\n-929885 -995154 254071 -370672 -435272 584846 -301610 -234118 -82557 743536\r\n-36327 439149 -977780 -821019 -585558 953598 -151943 140715 -311253 -383103\r\n", "output": "6216649853365"}, {"input": "22 334 246\r\n-462653 -618002 4973 -348485 366658 192390 274752 200590 138367 779540 -661269 642587 113645 -110388 -604418 -491231 -933401 -219332 -603140 836439 167007 210226\r\n357638 -646669 -558432 -434313 -285851 -119233 323088 -512237 -729293 215256 39316 -984201 -209814 715016 -271932 796550 988227 -89577 67202 462973 -942079 -823339\r\n", "output": "15389604923763"}, {"input": "1 1000 0\r\n1000000\r\n-1000000\r\n", "output": "3996001000000"}, {"input": "1 1000 0\r\n1000000\r\n1000000\r\n", "output": "0"}, {"input": "1 0 1000\r\n-1000000\r\n1000000\r\n", "output": "3996001000000"}, {"input": "1 0 1000\r\n1000000\r\n1000000\r\n", "output": "0"}, {"input": "1 5 7\r\n1\r\n2\r\n", "output": "1"}, {"input": "2 1 3\r\n2 2\r\n2 2\r\n", "output": "0"}, {"input": "3 3 0\r\n1 1 1\r\n1 1 1\r\n", "output": "1"}, {"input": "1 0 6\r\n0\r\n0\r\n", "output": "0"}, {"input": "2 1 1\r\n2 2\r\n0 0\r\n", "output": "2"}, {"input": "3 1 4\r\n0 0 0\r\n1 5 6\r\n", "output": "19"}, {"input": "1 1 2\r\n0\r\n8\r\n", "output": "25"}, {"input": "2 2 0\r\n3 3\r\n3 3\r\n", "output": "0"}]
| false
|
stdio
| null | true
|
388/C
|
388
|
C
|
Python 3
|
TESTS
| 5
| 124
| 409,600
|
51656284
|
from functools import reduce
n = int(input())
cards = [list(map(int, input().split()[1:])) for i in range(n)]
mid = []
a, b = 0, 0
add = lambda x=0, y=0: x + y
for c in cards:
s = len(c)
m = s >> 1
if s & 1 == 0:
a += reduce(add, c[:m])
b += reduce(add, c[m:])
else:
a += reduce(add, c[:m] or [0])
b += reduce(add, c[m + 1:] or [0])
mid.append(c[m])
mid.sort(reverse=True)
s = len(mid)
m = s >> 1
if s & 1 == 0:
a += reduce(add, mid[:m])
b += reduce(add, mid[m:])
else:
a += reduce(add, mid[:m + 1] or [0])
b += reduce(add, mid[m + 1:] or [0])
print(a, b)
| 43
| 62
| 307,200
|
110070558
|
p, n = [], int(input())
a = b = 0
for i in range(n):
t = list(map(int, input().split()))
k = t[0] // 2 + 1
a += sum(t[1: k])
if t[0] & 1:
p.append(t[k])
b += sum(t[k + 1: ])
else: b += sum(t[k: ])
p.sort(reverse = True)
print(a + sum(p[0 :: 2]), b + sum(p[1 :: 2]))
|
Codeforces Round 228 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Fox and Card Game
|
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
|
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
|
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
| null |
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
|
[{"input": "2\n1 100\n2 1 10", "output": "101 10"}, {"input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15"}, {"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18"}, {"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "output": "7000 7000"}]
| 2,000
|
["games", "greedy", "sortings"]
| 43
|
[{"input": "2\r\n1 100\r\n2 1 10\r\n", "output": "101 10\r\n"}, {"input": "1\r\n9 2 8 6 5 9 4 7 1 3\r\n", "output": "30 15\r\n"}, {"input": "3\r\n3 1 3 2\r\n3 5 4 6\r\n2 8 7\r\n", "output": "18 18\r\n"}, {"input": "3\r\n3 1000 1000 1000\r\n6 1000 1000 1000 1000 1000 1000\r\n5 1000 1000 1000 1000 1000\r\n", "output": "7000 7000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 0\r\n"}, {"input": "5\r\n1 3\r\n1 2\r\n1 8\r\n1 1\r\n1 4\r\n", "output": "12 6\r\n"}, {"input": "3\r\n5 1 2 3 4 5\r\n4 1 2 3 4\r\n8 1 2 3 4 5 6 7 8\r\n", "output": "19 42\r\n"}, {"input": "5\r\n5 1 1 1 1 1\r\n4 1 1 1 1\r\n3 1 1 1\r\n2 1 1\r\n1 1\r\n", "output": "8 7\r\n"}, {"input": "6\r\n2 1 1\r\n2 2 2\r\n2 3 3\r\n2 4 4\r\n2 5 5\r\n2 6 6\r\n", "output": "21 21\r\n"}, {"input": "2\r\n2 200 1\r\n3 1 100 2\r\n", "output": "301 3\r\n"}, {"input": "2\r\n3 1 1000 2\r\n3 2 1 1\r\n", "output": "1003 4\r\n"}, {"input": "4\r\n3 1 5 100\r\n3 1 5 100\r\n3 100 1 1\r\n3 100 1 1\r\n", "output": "208 208\r\n"}]
| false
|
stdio
| null | true
|
388/C
|
388
|
C
|
Python 3
|
TESTS
| 6
| 62
| 6,963,200
|
125470197
|
N=int(input())
C=[[] for i in range(N)]
num=0
for i in range(N):
L=list(map(int,input().split()))
num+=L[0]
C[i]=L[1:]
Ciel=0
Jiro=0
for i in range(num):
if i%2==0:
C.sort()
C.reverse()
Ciel+=C[0][0]
del C[0][0]
for j in range(N):
C[j].reverse()
#print(C)
else:
C.sort()
C.reverse()
Jiro+=C[0][0]
del C[0][0]
for j in range(N):
C[j].reverse()
#print(C)
#print(C)
print(Ciel, Jiro)
| 43
| 77
| 307,200
|
104855612
|
odd = []
first, second = 0, 0
for i in range(int(input())):
pile = list(map(int, input().split()))
s, pile = pile[0], pile[1:]
sh = s >> 1
if (s & 1) == 0:
first += sum(pile[:sh])
second += sum(pile[sh:])
else:
first += sum(pile[:sh])
second += sum(pile[sh+1:])
odd.append(pile[sh])
odd = sorted(odd, reverse=True)
first += sum(odd[::2])
second += sum(odd[1::2])
print(first, second)
|
Codeforces Round 228 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Fox and Card Game
|
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
|
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
|
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
| null |
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
|
[{"input": "2\n1 100\n2 1 10", "output": "101 10"}, {"input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15"}, {"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18"}, {"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "output": "7000 7000"}]
| 2,000
|
["games", "greedy", "sortings"]
| 43
|
[{"input": "2\r\n1 100\r\n2 1 10\r\n", "output": "101 10\r\n"}, {"input": "1\r\n9 2 8 6 5 9 4 7 1 3\r\n", "output": "30 15\r\n"}, {"input": "3\r\n3 1 3 2\r\n3 5 4 6\r\n2 8 7\r\n", "output": "18 18\r\n"}, {"input": "3\r\n3 1000 1000 1000\r\n6 1000 1000 1000 1000 1000 1000\r\n5 1000 1000 1000 1000 1000\r\n", "output": "7000 7000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 0\r\n"}, {"input": "5\r\n1 3\r\n1 2\r\n1 8\r\n1 1\r\n1 4\r\n", "output": "12 6\r\n"}, {"input": "3\r\n5 1 2 3 4 5\r\n4 1 2 3 4\r\n8 1 2 3 4 5 6 7 8\r\n", "output": "19 42\r\n"}, {"input": "5\r\n5 1 1 1 1 1\r\n4 1 1 1 1\r\n3 1 1 1\r\n2 1 1\r\n1 1\r\n", "output": "8 7\r\n"}, {"input": "6\r\n2 1 1\r\n2 2 2\r\n2 3 3\r\n2 4 4\r\n2 5 5\r\n2 6 6\r\n", "output": "21 21\r\n"}, {"input": "2\r\n2 200 1\r\n3 1 100 2\r\n", "output": "301 3\r\n"}, {"input": "2\r\n3 1 1000 2\r\n3 2 1 1\r\n", "output": "1003 4\r\n"}, {"input": "4\r\n3 1 5 100\r\n3 1 5 100\r\n3 100 1 1\r\n3 100 1 1\r\n", "output": "208 208\r\n"}]
| false
|
stdio
| null | true
|
884/A
|
884
|
A
|
Python 3
|
TESTS
| 4
| 61
| 5,529,600
|
33616580
|
n,m=[int(x) for x in input().strip().split()]
l=[int(x) for x in input().strip().split()]
day=0
for k in l:
day=day+1
m=m-(86400-k)
if(m==0):
break
print(day)
| 16
| 31
| 0
|
228082839
|
days,time=map(int,input().split())
work=list(map(int,input().split()))
count=0
day=86400
for i in range(days):
free=0
free=day-work[i]
time-=free
if time <= 0:
count+=1
break
else:
count+=1
print(count)
|
Educational Codeforces Round 31
|
ICPC
| 2,017
| 2
| 256
|
Book Reading
|
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.
But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.
Help Luba to determine the minimum number of day when she finishes reading.
It is guaranteed that the answer doesn't exceed n.
Remember that there are 86400 seconds in a day.
|
The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book.
The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.
|
Print the minimum day Luba can finish reading the book.
It is guaranteed that answer doesn't exceed n.
| null | null |
[{"input": "2 2\n86400 86398", "output": "2"}, {"input": "2 86400\n0 86400", "output": "1"}]
| 800
|
["implementation"]
| 16
|
[{"input": "2 2\r\n86400 86398\r\n", "output": "2\r\n"}, {"input": "2 86400\r\n0 86400\r\n", "output": "1\r\n"}, {"input": "2 86400\r\n1 86399\r\n", "output": "2\r\n"}, {"input": "100 1000000\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": "12\r\n"}, {"input": "1 1\r\n86399\r\n", "output": "1\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86000 86400\r\n", "output": "5\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86001 86399\r\n", "output": "6\r\n"}, {"input": "4 172799\r\n1 1 86400 0\r\n", "output": "4\r\n"}, {"input": "4 172799\r\n0 86400 86399 0\r\n", "output": "4\r\n"}, {"input": "6 1\r\n1 1 86400 1 86399 1\r\n", "output": "1\r\n"}, {"input": "4 1\r\n86400 86399 86400 86400\r\n", "output": "2\r\n"}, {"input": "4 1\r\n86400 86400 0 86400\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
39/B
|
39
|
B
|
Python 3
|
TESTS
| 0
| 60
| 0
|
178819525
|
n=int(input())
a=list(map(int,input().split()))
t=0
b=[]
for i in range(1,n):
if a[i]>0:
if a[i-1]<a[i]:
b.append(2000+i+1)
if len(b)==0:
print('0')
else:
print(len(b))
for i in b:
print(i,end=' ')
| 35
| 92
| 0
|
173338221
|
n=int(input())
A=list(map(int,input().split()))
ANS=[]
now=1
for i in range(n):
if A[i]==now:
ANS.append(2000+i+1)
now+=1
print(len(ANS))
print(*ANS)
|
School Team Contest 1 (Winter Computer School 2010/11)
|
ICPC
| 2,010
| 2
| 64
|
Company Income Growth
|
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 — to a2 billion, ..., and in the current (2000 + n)-th year — an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year — 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the company’s losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 — 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
|
The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers ai ( - 100 ≤ ai ≤ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
|
Output k — the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
| null | null |
[{"input": "10\n-2 1 1 3 2 3 4 -10 -2 5", "output": "5\n2002 2005 2006 2007 2010"}, {"input": "3\n-1 -2 -3", "output": "0"}]
| 1,300
|
["greedy"]
| 35
|
[{"input": "10\r\n-2 1 1 3 2 3 4 -10 -2 5\r\n", "output": "5\r\n2002 2005 2006 2007 2010 "}, {"input": "3\r\n-1 -2 -3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 1\r\n", "output": "1\r\n2002 "}, {"input": "2\r\n-1 1\r\n", "output": "1\r\n2002 "}, {"input": "2\r\n-2 0\r\n", "output": "0\r\n"}, {"input": "2\r\n3 -3\r\n", "output": "0\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n2001 "}, {"input": "3\r\n-2 -2 1\r\n", "output": "1\r\n2003 "}, {"input": "4\r\n-4 2 3 -1\r\n", "output": "0\r\n"}, {"input": "5\r\n-3 -3 -4 2 -2\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n2001 "}, {"input": "2\r\n1 2\r\n", "output": "2\r\n2001 2002 "}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 3 1 3 3 2 1 2 1 2\r\n", "output": "2\r\n2003 2006 "}, {"input": "15\r\n4 1 4 6 3 2 1 1 3 2 4 4 1 4 1\r\n", "output": "4\r\n2002 2006 2009 2011 "}, {"input": "15\r\n3 3 3 2 2 2 1 1 1 2 2 2 4 4 4\r\n", "output": "2\r\n2007 2010 "}, {"input": "15\r\n6 5 2 3 4 1 3 2 4 5 1 2 6 4 4\r\n", "output": "2\r\n2006 2008 "}]
| false
|
stdio
| null | true
|
883/F
|
883
|
F
|
Python 3
|
TESTS
| 8
| 77
| 6,041,600
|
31591529
|
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
"""
created by shhuan at 2017/10/22 12:26
"""
N = int(input())
A = []
for i in range(N):
A.append(input())
def transform(s):
if not s:
return ""
t = s.replace("kh", "h")
t = t.replace("u", "oo")
if len(s) != len(t):
return transform(t)
return t
C = set()
for s in A:
C.add(transform(s))
print(len(C))
| 81
| 46
| 0
|
140150903
|
n = int(input())
st = set()
for i in range(n):
word = input()
word = word.replace('u', 'oo')
while word.replace('kh', 'h') != word:
word = word.replace('kh', 'h')
st.add(word)
print(len(st))
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 3
| 256
|
Lost in Transliteration
|
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet.
For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name.
The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name.
There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account?
Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements.
For example, the following pairs of words denote the same name:
- "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper".
- "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon".
For a given list of words, find the minimal number of groups where the words in each group denote the same name.
|
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list.
The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
|
Print the minimal number of groups where the words in each group denote the same name.
| null |
There are four groups of words in the first example. Words in each group denote same name:
1. "mihail", "mikhail"
2. "oolyana", "ulyana"
3. "kooooper", "koouper"
4. "hoon", "khun", "kkkhoon"
There are five groups of words in the second example. Words in each group denote same name:
1. "hariton", "kkkhariton", "khariton"
2. "hkariton"
3. "buoi", "boooi", "boui"
4. "bui"
5. "boi"
In the third example the words are equal, so they denote the same name.
|
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
| 1,300
|
["implementation"]
| 81
|
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
883/F
|
883
|
F
|
Python 3
|
TESTS
| 7
| 62
| 5,529,600
|
33323100
|
n=int(input())
use=set()
for i in range(n):
s=''
fl=False
for j in input()[::-1]:
if fl and j=='k':
continue
elif j=='u':
s+='oo'
elif (j=='h'):
fl=True
s+='h'
else:
s+=j
fl=False
use.add(s[::-1])
print(len(use))
| 81
| 46
| 0
|
154831410
|
n=eval(input())
ans=set()
for i in range(n):
s=input()
# print(s)
t=0
while t<=100:
s=s.replace("u","oo")
s=s.replace("kh","h")
t+=1
# print(s)
ans.add(s)
print(len(ans))
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 3
| 256
|
Lost in Transliteration
|
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet.
For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name.
The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name.
There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account?
Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements.
For example, the following pairs of words denote the same name:
- "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper".
- "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon".
For a given list of words, find the minimal number of groups where the words in each group denote the same name.
|
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list.
The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
|
Print the minimal number of groups where the words in each group denote the same name.
| null |
There are four groups of words in the first example. Words in each group denote same name:
1. "mihail", "mikhail"
2. "oolyana", "ulyana"
3. "kooooper", "koouper"
4. "hoon", "khun", "kkkhoon"
There are five groups of words in the second example. Words in each group denote same name:
1. "hariton", "kkkhariton", "khariton"
2. "hkariton"
3. "buoi", "boooi", "boui"
4. "bui"
5. "boi"
In the third example the words are equal, so they denote the same name.
|
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
| 1,300
|
["implementation"]
| 81
|
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
884/A
|
884
|
A
|
Python 3
|
TESTS
| 4
| 31
| 0
|
208996107
|
n,t = map(int, input().split())
work_times = list(map(int, input().split()))
days = 0
for i in work_times:
days += 1
t -= 86400 - i
if t == 0:
break
print(days)
| 16
| 46
| 0
|
31798236
|
R=lambda:list(map(int,input().split()))
n,t=R()
a=R()
for i in range(n):
t-=86400-a[i]
if t<1:
print(i+1)
exit(0)
|
Educational Codeforces Round 31
|
ICPC
| 2,017
| 2
| 256
|
Book Reading
|
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.
But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.
Help Luba to determine the minimum number of day when she finishes reading.
It is guaranteed that the answer doesn't exceed n.
Remember that there are 86400 seconds in a day.
|
The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book.
The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.
|
Print the minimum day Luba can finish reading the book.
It is guaranteed that answer doesn't exceed n.
| null | null |
[{"input": "2 2\n86400 86398", "output": "2"}, {"input": "2 86400\n0 86400", "output": "1"}]
| 800
|
["implementation"]
| 16
|
[{"input": "2 2\r\n86400 86398\r\n", "output": "2\r\n"}, {"input": "2 86400\r\n0 86400\r\n", "output": "1\r\n"}, {"input": "2 86400\r\n1 86399\r\n", "output": "2\r\n"}, {"input": "100 1000000\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": "12\r\n"}, {"input": "1 1\r\n86399\r\n", "output": "1\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86000 86400\r\n", "output": "5\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86001 86399\r\n", "output": "6\r\n"}, {"input": "4 172799\r\n1 1 86400 0\r\n", "output": "4\r\n"}, {"input": "4 172799\r\n0 86400 86399 0\r\n", "output": "4\r\n"}, {"input": "6 1\r\n1 1 86400 1 86399 1\r\n", "output": "1\r\n"}, {"input": "4 1\r\n86400 86399 86400 86400\r\n", "output": "2\r\n"}, {"input": "4 1\r\n86400 86400 0 86400\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
883/F
|
883
|
F
|
Python 3
|
TESTS
| 7
| 62
| 5,529,600
|
31686201
|
n = int(input())
l = []
for i in range(n):
l.append(list(str(input())))
for i in range(n):
for j in range(len(l[i])):
if (l[i])[j] == 'u':
(l[i])[j] = 'oo'
for i in range(n):
j = 0
while (j < len(l[i]) - 1):
if (l[i])[j] == 'k' and (l[i])[j+1] == 'h':
l[i].remove((l[i])[j])
if (j != 0):
j -= 1
else:
j += 1
for i in range(n):
l[i] = ''.join(l[i])
#print(l)
s = set(l)
print(len(s))
| 81
| 46
| 0
|
198725716
|
import math
n=int(input())
l=[]
for i in range(n):
s=input()
while("kh" in s):
s=s.replace("kh","h")
while("u" in s):
s=s.replace("u","oo")
l.append(s)
print(len(set(l)))
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 3
| 256
|
Lost in Transliteration
|
There are some ambiguities when one writes Berland names with the letters of the Latin alphabet.
For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name.
The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name.
There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account?
Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements.
For example, the following pairs of words denote the same name:
- "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper".
- "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon".
For a given list of words, find the minimal number of groups where the words in each group denote the same name.
|
The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list.
The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.
|
Print the minimal number of groups where the words in each group denote the same name.
| null |
There are four groups of words in the first example. Words in each group denote same name:
1. "mihail", "mikhail"
2. "oolyana", "ulyana"
3. "kooooper", "koouper"
4. "hoon", "khun", "kkkhoon"
There are five groups of words in the second example. Words in each group denote same name:
1. "hariton", "kkkhariton", "khariton"
2. "hkariton"
3. "buoi", "boooi", "boui"
4. "bui"
5. "boi"
In the third example the words are equal, so they denote the same name.
|
[{"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon", "output": "4"}, {"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi", "output": "5"}, {"input": "2\nalex\nalex", "output": "1"}]
| 1,300
|
["implementation"]
| 81
|
[{"input": "10\r\nmihail\r\noolyana\r\nkooooper\r\nhoon\r\nulyana\r\nkoouper\r\nmikhail\r\nkhun\r\nkuooper\r\nkkkhoon\r\n", "output": "4\r\n"}, {"input": "9\r\nhariton\r\nhkariton\r\nbuoi\r\nkkkhariton\r\nboooi\r\nbui\r\nkhariton\r\nboui\r\nboi\r\n", "output": "5\r\n"}, {"input": "2\r\nalex\r\nalex\r\n", "output": "1\r\n"}, {"input": "40\r\nuok\r\nkuu\r\nku\r\no\r\nkku\r\nuh\r\nu\r\nu\r\nhh\r\nk\r\nkh\r\nh\r\nh\r\nou\r\nokh\r\nukk\r\nou\r\nuhk\r\nuo\r\nuko\r\nu\r\nuu\r\nh\r\nh\r\nhk\r\nuhu\r\nuoh\r\nooo\r\nk\r\nh\r\nuk\r\nk\r\nkku\r\nh\r\nku\r\nok\r\nk\r\nkuu\r\nou\r\nhh\r\n", "output": "21\r\n"}, {"input": "40\r\noooo\r\nhu\r\no\r\nhoh\r\nkhk\r\nuuh\r\nhu\r\nou\r\nuuoh\r\no\r\nkouk\r\nuouo\r\nu\r\nok\r\nuu\r\nuuuo\r\nhoh\r\nuu\r\nkuu\r\nh\r\nu\r\nkkoh\r\nkhh\r\nuoh\r\nouuk\r\nkuo\r\nk\r\nu\r\nuku\r\nh\r\nu\r\nk\r\nhuho\r\nku\r\nh\r\noo\r\nuh\r\nk\r\nuo\r\nou\r\n", "output": "25\r\n"}, {"input": "100\r\nuh\r\nu\r\nou\r\nhk\r\nokh\r\nuou\r\nk\r\no\r\nuhh\r\nk\r\noku\r\nk\r\nou\r\nhuh\r\nkoo\r\nuo\r\nkk\r\nkok\r\nhhu\r\nuu\r\noou\r\nk\r\nk\r\noh\r\nhk\r\nk\r\nu\r\no\r\nuo\r\no\r\no\r\no\r\nhoh\r\nkuo\r\nhuh\r\nkhu\r\nuu\r\nk\r\noku\r\nk\r\nh\r\nuu\r\nuo\r\nhuo\r\noo\r\nhu\r\nukk\r\nok\r\no\r\noh\r\nuo\r\nkko\r\nok\r\nouh\r\nkoh\r\nhhu\r\nku\r\nko\r\nhho\r\nkho\r\nkho\r\nkhk\r\nho\r\nhk\r\nuko\r\nukh\r\nh\r\nkh\r\nkk\r\nuku\r\nkkk\r\no\r\nuo\r\no\r\nouh\r\nou\r\nuhk\r\nou\r\nk\r\nh\r\nkko\r\nuko\r\no\r\nu\r\nho\r\nu\r\nooo\r\nuo\r\no\r\nko\r\noh\r\nkh\r\nuk\r\nohk\r\noko\r\nuko\r\nh\r\nh\r\noo\r\no\r\n", "output": "36\r\n"}, {"input": "101\r\nukuu\r\nh\r\nouuo\r\no\r\nkkuo\r\nko\r\nu\r\nh\r\nhku\r\nh\r\nh\r\nhuo\r\nuhoh\r\nkuu\r\nhu\r\nhkko\r\nuhuk\r\nkoho\r\nh\r\nhukk\r\noohu\r\nkk\r\nkko\r\nou\r\noou\r\nh\r\nuuu\r\nuh\r\nkhuk\r\nokoo\r\nouou\r\nuo\r\nkk\r\noo\r\nhuok\r\no\r\nu\r\nhok\r\nhu\r\nhhuu\r\nkuu\r\nooho\r\noku\r\nhuoh\r\nhhkh\r\nuuuh\r\nouo\r\nhou\r\nhhu\r\nh\r\no\r\nokou\r\nuo\r\nh\r\nukk\r\nu\r\nhook\r\nh\r\noouk\r\nokuo\r\nkuuu\r\nk\r\nuuk\r\nu\r\nukk\r\nkk\r\nu\r\nuhk\r\nh\r\nk\r\nokuu\r\nuoho\r\nkhuk\r\nhukk\r\nhoo\r\nouko\r\nu\r\nuu\r\nu\r\nh\r\nhuo\r\nh\r\nukk\r\nhk\r\nk\r\nuoh\r\nhk\r\nko\r\nou\r\nho\r\nu\r\nhhhk\r\nkuo\r\nhuo\r\nhkh\r\nku\r\nhok\r\nho\r\nkok\r\nhk\r\nouuh\r\n", "output": "50\r\n"}, {"input": "2\r\nkkkhkkh\r\nhh\r\n", "output": "1\r\n"}, {"input": "2\r\nkkhookkhoo\r\nhuhu\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
884/A
|
884
|
A
|
Python 3
|
TESTS
| 4
| 31
| 0
|
223747206
|
n,t = map(int,input().split())
l = list(map(int,input().split()))
ans = 0
for i in range(n):
t -= 86400 - l[i]
ans += 1
if t == 0:
break
print(ans)
| 16
| 46
| 0
|
31798328
|
n, t = map(int, input().split())
a = list(map(int, input().split()))
for i in range(1, n + 1):
t -= 86400 - a[i - 1]
if (t <= 0):
print(i)
break
|
Educational Codeforces Round 31
|
ICPC
| 2,017
| 2
| 256
|
Book Reading
|
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.
But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.
Help Luba to determine the minimum number of day when she finishes reading.
It is guaranteed that the answer doesn't exceed n.
Remember that there are 86400 seconds in a day.
|
The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book.
The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.
|
Print the minimum day Luba can finish reading the book.
It is guaranteed that answer doesn't exceed n.
| null | null |
[{"input": "2 2\n86400 86398", "output": "2"}, {"input": "2 86400\n0 86400", "output": "1"}]
| 800
|
["implementation"]
| 16
|
[{"input": "2 2\r\n86400 86398\r\n", "output": "2\r\n"}, {"input": "2 86400\r\n0 86400\r\n", "output": "1\r\n"}, {"input": "2 86400\r\n1 86399\r\n", "output": "2\r\n"}, {"input": "100 1000000\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": "12\r\n"}, {"input": "1 1\r\n86399\r\n", "output": "1\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86000 86400\r\n", "output": "5\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86001 86399\r\n", "output": "6\r\n"}, {"input": "4 172799\r\n1 1 86400 0\r\n", "output": "4\r\n"}, {"input": "4 172799\r\n0 86400 86399 0\r\n", "output": "4\r\n"}, {"input": "6 1\r\n1 1 86400 1 86399 1\r\n", "output": "1\r\n"}, {"input": "4 1\r\n86400 86399 86400 86400\r\n", "output": "2\r\n"}, {"input": "4 1\r\n86400 86400 0 86400\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.