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
717/D
717
D
PyPy 3
TESTS
5
140
1,945,600
64629565
import sys readline = sys.stdin.readline n, x = map(int,readline().split()) tmp = list(map(float,readline().split())) for i in range(128 - x - 1): tmp.append(0) def MatM(a,b): c = [0 for i in range(128)] for i in range(127): for j in range(127): c[i ^ j] += a[i] * b[j] return c def MatPow(mat,p): if p == 1: return tmp.copy() res = MatPow(mat, p // 2) newMat = MatM(res,res) if p & 1: return MatM(newMat, mat) return newMat print(1 - MatPow(tmp,n)[0])
11
108
1,945,600
103911369
import os from io import BytesIO #input = BytesIO(os.read(0, os.fstat(0).st_size)).readline def main(): def mult(m1,m2): ans = [0] * 128 for i in range(128): for j in range(128): ans[i^j] += m1[i] * m2[j] return ans def power(number, n): res = number while(n): if n & 1: res = mult(res,number) number = mult(number,number) n >>= 1 return res n,x = map(int,input().split()) ai = list(map(float,input().split())) ai += [0]*(128-len(ai)) if n == 1: print(1 - ai[0]) return ansm = power(ai,n-1) print(1 - ansm[0]) main()
Bubble Cup 9 - Finals [Online Mirror]
ICPC
2,016
1
256
Dexterina’s Lab
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim. Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game. Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
The first line of the input contains two integers n (1 ≤ n ≤ 109) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10 - 6.
null
null
[{"input": "2 2\n0.500000 0.250000 0.250000", "output": "0.62500000"}]
1,900
["games", "matrices", "probabilities"]
11
[{"input": "2 2\r\n0.500000 0.250000 0.250000\r\n", "output": "0.62500000\r\n"}, {"input": "9 9\r\n0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000\r\n", "output": "0.93687014\r\n"}, {"input": "1000001 5\r\n0.000000 0.300000 0.000000 0.500000 0.000000 0.200000\r\n", "output": "1.00000000\r\n"}, {"input": "1000000 5\r\n0.000000 0.000000 1.000000 0.000000 0.000000 0.000000\r\n", "output": "0.00000000\r\n"}, {"input": "100 20\r\n0.065682 0.015963 0.024840 0.025856 0.000807 0.004366 0.020977 0.151841 0.050870 0.009918 0.044345 0.009672 0.077615 0.019520 0.166791 0.028122 0.010873 0.018634 0.172648 0.037614 0.043046\r\n", "output": "0.96875000\r\n"}, {"input": "1000000 50\r\n0.034403 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.033928 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.071853 0.000000 0.000000 0.016440 0.000000 0.000000 0.000000 0.000000 0.023167 0.067938 0.000000 0.000000 0.000000 0.000000 0.176349 0.030154 0.180266 0.272662 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.014462 0.000000 0.000000 0.037916 0.040462\r\n", "output": "0.98437500\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] try: with open(correct_output_path, 'r') as f: correct_line = f.read().strip() correct_val = float(correct_line) except: print(0) return try: with open(submission_output_path, 'r') as f: submission_line = f.read().strip() submission_val = float(submission_line) except: print(0) return diff = abs(submission_val - correct_val) print(1 if diff <= 1e-6 else 0) if __name__ == "__main__": main()
true
292/A
292
A
PyPy 3-64
TESTS
4
92
0
225776694
n=int(input()) t=[] c=[] for i in range(n): x,y=map(int,input().split()) t.append(x) c.append(y) q=c[0] s=c[0] for i in range(n-1): p=t[i+1]-t[i] s=s-p+c[i+1] q=max(q,s) print(t[n-1]+s,end=" ") print(q)
38
92
0
168980605
n = eval(input()) cnt = 0 lt = 0 mn = 0 for i in range(n): t, c = input().split() t, c = int(t), int(c) cnt -= t - lt lt = t if cnt < 0: cnt = 0 cnt += c if cnt > mn: mn = cnt print(cnt + t, mn)
Croc Champ 2013 - Round 1
CF
2,013
2
256
SMSC
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly. Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows: 1. If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. 2. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.
The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).
In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.
null
In the first test sample: - second 1: the first message has appeared in the queue, the queue's size is 1; - second 2: the first message is sent, the second message has been received, the queue's size is 1; - second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
[{"input": "2\n1 1\n2 1", "output": "3 1"}, {"input": "1\n1000000 10", "output": "1000010 10"}, {"input": "3\n3 3\n4 3\n5 3", "output": "12 7"}]
1,100
["implementation"]
38
[{"input": "2\r\n1 1\r\n2 1\r\n", "output": "3 1\r\n"}, {"input": "1\r\n1000000 10\r\n", "output": "1000010 10\r\n"}, {"input": "3\r\n3 3\r\n4 3\r\n5 3\r\n", "output": "12 7\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2 1\r\n"}, {"input": "2\r\n1 11\r\n100 10\r\n", "output": "110 11\r\n"}, {"input": "4\r\n1 10\r\n2 9\r\n3 8\r\n40 3\r\n", "output": "43 25\r\n"}, {"input": "5\r\n2 1\r\n5 2\r\n6 1\r\n7 1\r\n8 1\r\n", "output": "10 2\r\n"}, {"input": "4\r\n10 1000\r\n99998 20\r\n99999 10\r\n1000000 100\r\n", "output": "1000100 1000\r\n"}, {"input": "6\r\n10 10\r\n100 500\r\n200 500\r\n500 1\r\n999995 4\r\n999996 15\r\n", "output": "1000014 900\r\n"}, {"input": "10\r\n1 5\r\n2 5\r\n3 10\r\n4 8\r\n5 5\r\n6 4\r\n7 8\r\n8 9\r\n9 2\r\n10 10\r\n", "output": "67 57\r\n"}, {"input": "10\r\n26 4\r\n85 97\r\n86 62\r\n87 74\r\n92 8\r\n93 81\r\n97 12\r\n98 25\r\n99 31\r\n100 3\r\n", "output": "478 378\r\n"}, {"input": "10\r\n964416 3980\r\n987048 334\r\n999576 6922\r\n999684 2385\r\n999896 6558\r\n999948 3515\r\n999966 1517\r\n999984 2233\r\n999988 7242\r\n999994 91\r\n", "output": "1030039 30045\r\n"}, {"input": "5\r\n987640 52\r\n994481 69\r\n995526 50\r\n996631 75\r\n999763 22\r\n", "output": "999785 75\r\n"}, {"input": "23\r\n5 1045\r\n12 703\r\n16 26\r\n23 3384\r\n28 4563\r\n30 4501\r\n34 1033\r\n35 1393\r\n36 4095\r\n37 1279\r\n38 1787\r\n39 770\r\n40 5362\r\n41 4569\r\n42 3148\r\n43 2619\r\n44 5409\r\n45 3919\r\n46 732\r\n47 1297\r\n48 4512\r\n49 3231\r\n50 5169\r\n", "output": "64551 64501\r\n"}]
false
stdio
null
true
292/A
292
A
Python 3
TESTS
4
92
0
158242217
import sys input = sys.stdin.readline n = int(input()) v, s, x, t0 = 0, 0, 0, 0 for i in range(n): t, c = map(int, input().split()) if i == 0: v = x = c t0 = t else: x += c - max(0, (t - t0)) if x > v: v = x t0 = t if i == n-1: s = t + x print(s, v)
38
92
4,505,600
192173838
# LUOGU_RID: 101473903 (n,), *a = [[*(map(int, s.split()))] for s in open(0)] p = q = r = 0 for x, y in a: q = max(0, p + q - x) + y r = max(r, q) p = x print(p+q, r)
Croc Champ 2013 - Round 1
CF
2,013
2
256
SMSC
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly. Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows: 1. If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. 2. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.
The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).
In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.
null
In the first test sample: - second 1: the first message has appeared in the queue, the queue's size is 1; - second 2: the first message is sent, the second message has been received, the queue's size is 1; - second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
[{"input": "2\n1 1\n2 1", "output": "3 1"}, {"input": "1\n1000000 10", "output": "1000010 10"}, {"input": "3\n3 3\n4 3\n5 3", "output": "12 7"}]
1,100
["implementation"]
38
[{"input": "2\r\n1 1\r\n2 1\r\n", "output": "3 1\r\n"}, {"input": "1\r\n1000000 10\r\n", "output": "1000010 10\r\n"}, {"input": "3\r\n3 3\r\n4 3\r\n5 3\r\n", "output": "12 7\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2 1\r\n"}, {"input": "2\r\n1 11\r\n100 10\r\n", "output": "110 11\r\n"}, {"input": "4\r\n1 10\r\n2 9\r\n3 8\r\n40 3\r\n", "output": "43 25\r\n"}, {"input": "5\r\n2 1\r\n5 2\r\n6 1\r\n7 1\r\n8 1\r\n", "output": "10 2\r\n"}, {"input": "4\r\n10 1000\r\n99998 20\r\n99999 10\r\n1000000 100\r\n", "output": "1000100 1000\r\n"}, {"input": "6\r\n10 10\r\n100 500\r\n200 500\r\n500 1\r\n999995 4\r\n999996 15\r\n", "output": "1000014 900\r\n"}, {"input": "10\r\n1 5\r\n2 5\r\n3 10\r\n4 8\r\n5 5\r\n6 4\r\n7 8\r\n8 9\r\n9 2\r\n10 10\r\n", "output": "67 57\r\n"}, {"input": "10\r\n26 4\r\n85 97\r\n86 62\r\n87 74\r\n92 8\r\n93 81\r\n97 12\r\n98 25\r\n99 31\r\n100 3\r\n", "output": "478 378\r\n"}, {"input": "10\r\n964416 3980\r\n987048 334\r\n999576 6922\r\n999684 2385\r\n999896 6558\r\n999948 3515\r\n999966 1517\r\n999984 2233\r\n999988 7242\r\n999994 91\r\n", "output": "1030039 30045\r\n"}, {"input": "5\r\n987640 52\r\n994481 69\r\n995526 50\r\n996631 75\r\n999763 22\r\n", "output": "999785 75\r\n"}, {"input": "23\r\n5 1045\r\n12 703\r\n16 26\r\n23 3384\r\n28 4563\r\n30 4501\r\n34 1033\r\n35 1393\r\n36 4095\r\n37 1279\r\n38 1787\r\n39 770\r\n40 5362\r\n41 4569\r\n42 3148\r\n43 2619\r\n44 5409\r\n45 3919\r\n46 732\r\n47 1297\r\n48 4512\r\n49 3231\r\n50 5169\r\n", "output": "64551 64501\r\n"}]
false
stdio
null
true
292/A
292
A
Python 3
TESTS
4
218
0
66020618
n = int(input()) l = [] for i in range(n): c,t = map(int,input().split()) l.append((c,t)) queue = l[0][1] z = queue for i in range(1,n): queue = queue + l[i][1] queue = queue - (l[i][0]-l[i-1][0]) z = max(z,queue) print(l[i][0]+queue,z)
38
124
0
10659275
import sys import math n = int(input()) a, b = list(map(int, input().split())) vmax = b for i in range(1, n): c, d = list(map(int, input().split())) b = max(0, b - (c - a)) a = c b += d vmax = max(b, vmax) print(a + b, vmax)
Croc Champ 2013 - Round 1
CF
2,013
2
256
SMSC
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly. Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows: 1. If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. 2. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.
The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).
In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.
null
In the first test sample: - second 1: the first message has appeared in the queue, the queue's size is 1; - second 2: the first message is sent, the second message has been received, the queue's size is 1; - second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
[{"input": "2\n1 1\n2 1", "output": "3 1"}, {"input": "1\n1000000 10", "output": "1000010 10"}, {"input": "3\n3 3\n4 3\n5 3", "output": "12 7"}]
1,100
["implementation"]
38
[{"input": "2\r\n1 1\r\n2 1\r\n", "output": "3 1\r\n"}, {"input": "1\r\n1000000 10\r\n", "output": "1000010 10\r\n"}, {"input": "3\r\n3 3\r\n4 3\r\n5 3\r\n", "output": "12 7\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2 1\r\n"}, {"input": "2\r\n1 11\r\n100 10\r\n", "output": "110 11\r\n"}, {"input": "4\r\n1 10\r\n2 9\r\n3 8\r\n40 3\r\n", "output": "43 25\r\n"}, {"input": "5\r\n2 1\r\n5 2\r\n6 1\r\n7 1\r\n8 1\r\n", "output": "10 2\r\n"}, {"input": "4\r\n10 1000\r\n99998 20\r\n99999 10\r\n1000000 100\r\n", "output": "1000100 1000\r\n"}, {"input": "6\r\n10 10\r\n100 500\r\n200 500\r\n500 1\r\n999995 4\r\n999996 15\r\n", "output": "1000014 900\r\n"}, {"input": "10\r\n1 5\r\n2 5\r\n3 10\r\n4 8\r\n5 5\r\n6 4\r\n7 8\r\n8 9\r\n9 2\r\n10 10\r\n", "output": "67 57\r\n"}, {"input": "10\r\n26 4\r\n85 97\r\n86 62\r\n87 74\r\n92 8\r\n93 81\r\n97 12\r\n98 25\r\n99 31\r\n100 3\r\n", "output": "478 378\r\n"}, {"input": "10\r\n964416 3980\r\n987048 334\r\n999576 6922\r\n999684 2385\r\n999896 6558\r\n999948 3515\r\n999966 1517\r\n999984 2233\r\n999988 7242\r\n999994 91\r\n", "output": "1030039 30045\r\n"}, {"input": "5\r\n987640 52\r\n994481 69\r\n995526 50\r\n996631 75\r\n999763 22\r\n", "output": "999785 75\r\n"}, {"input": "23\r\n5 1045\r\n12 703\r\n16 26\r\n23 3384\r\n28 4563\r\n30 4501\r\n34 1033\r\n35 1393\r\n36 4095\r\n37 1279\r\n38 1787\r\n39 770\r\n40 5362\r\n41 4569\r\n42 3148\r\n43 2619\r\n44 5409\r\n45 3919\r\n46 732\r\n47 1297\r\n48 4512\r\n49 3231\r\n50 5169\r\n", "output": "64551 64501\r\n"}]
false
stdio
null
true
292/A
292
A
PyPy 3
TESTS
4
310
0
73885523
import sys import math #to read string get_string = lambda: sys.stdin.readline().strip() #to read list of integers get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) ) #to read integers get_int = lambda: int(sys.stdin.readline()) #to print fast pt = lambda x: sys.stdout.write(str(x)+'\n') #--------------------------------WhiteHat010--------------------------------------# n = get_int() prev_t,s = get_int_list() mx_q = s q_size = s for i in range(n-1): t,s = get_int_list() diff = t-prev_t q_size = q_size - diff + s mx_q = max(mx_q, q_size) prev_t = t print(prev_t+q_size, mx_q)
38
124
1,536,000
187627554
import sys input = lambda: sys.stdin.readline().rstrip("\n\r") def solve(): n = int(input()) a = [list(map(int, input().split())) for i in range(n)] curr = top = a[0][1] for i in range(1, n): dist = a[i][0] - a[i - 1][0] curr = max(0, curr - dist) curr += a[i][1] top = max(curr, top) time = a[-1][0] + curr print(time, top) for _ in range(1): solve()
Croc Champ 2013 - Round 1
CF
2,013
2
256
SMSC
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly. Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows: 1. If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. 2. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.
The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).
In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.
null
In the first test sample: - second 1: the first message has appeared in the queue, the queue's size is 1; - second 2: the first message is sent, the second message has been received, the queue's size is 1; - second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
[{"input": "2\n1 1\n2 1", "output": "3 1"}, {"input": "1\n1000000 10", "output": "1000010 10"}, {"input": "3\n3 3\n4 3\n5 3", "output": "12 7"}]
1,100
["implementation"]
38
[{"input": "2\r\n1 1\r\n2 1\r\n", "output": "3 1\r\n"}, {"input": "1\r\n1000000 10\r\n", "output": "1000010 10\r\n"}, {"input": "3\r\n3 3\r\n4 3\r\n5 3\r\n", "output": "12 7\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2 1\r\n"}, {"input": "2\r\n1 11\r\n100 10\r\n", "output": "110 11\r\n"}, {"input": "4\r\n1 10\r\n2 9\r\n3 8\r\n40 3\r\n", "output": "43 25\r\n"}, {"input": "5\r\n2 1\r\n5 2\r\n6 1\r\n7 1\r\n8 1\r\n", "output": "10 2\r\n"}, {"input": "4\r\n10 1000\r\n99998 20\r\n99999 10\r\n1000000 100\r\n", "output": "1000100 1000\r\n"}, {"input": "6\r\n10 10\r\n100 500\r\n200 500\r\n500 1\r\n999995 4\r\n999996 15\r\n", "output": "1000014 900\r\n"}, {"input": "10\r\n1 5\r\n2 5\r\n3 10\r\n4 8\r\n5 5\r\n6 4\r\n7 8\r\n8 9\r\n9 2\r\n10 10\r\n", "output": "67 57\r\n"}, {"input": "10\r\n26 4\r\n85 97\r\n86 62\r\n87 74\r\n92 8\r\n93 81\r\n97 12\r\n98 25\r\n99 31\r\n100 3\r\n", "output": "478 378\r\n"}, {"input": "10\r\n964416 3980\r\n987048 334\r\n999576 6922\r\n999684 2385\r\n999896 6558\r\n999948 3515\r\n999966 1517\r\n999984 2233\r\n999988 7242\r\n999994 91\r\n", "output": "1030039 30045\r\n"}, {"input": "5\r\n987640 52\r\n994481 69\r\n995526 50\r\n996631 75\r\n999763 22\r\n", "output": "999785 75\r\n"}, {"input": "23\r\n5 1045\r\n12 703\r\n16 26\r\n23 3384\r\n28 4563\r\n30 4501\r\n34 1033\r\n35 1393\r\n36 4095\r\n37 1279\r\n38 1787\r\n39 770\r\n40 5362\r\n41 4569\r\n42 3148\r\n43 2619\r\n44 5409\r\n45 3919\r\n46 732\r\n47 1297\r\n48 4512\r\n49 3231\r\n50 5169\r\n", "output": "64551 64501\r\n"}]
false
stdio
null
true
963/B
963
B
PyPy 3-64
TESTS
6
124
2,764,800
163182275
import sys from collections import deque input = lambda: sys.stdin.readline().rstrip("\r\n") n=int(input()) va=[deque() for i in range(n+1)] vb=[deque() for i in range(n+1)] via=[1 for i in range(n+1)] l=deque(map(int,input().split())) for i in range(len(l)): if l[i]==0: continue va[i+1].append(l[i]) vb[l[i]].append(i+1) cal=deque() for i in range(1,n+1): for j in range(1,n+1): if via[j]==1 and (len(va[j])+len(vb[j]))%2==0: via[j]=0 for i in vb[j]: va[i].remove(j) cal.append(j) for i in range(1,n+1): if (len(va[i])+len(vb[i]))%2==0: via[i]=0 if via[i]: print('NO') quit() print('YES',*cal,sep='\n')
95
701
45,670,400
126191783
import sys input = sys.stdin.readline n = int(input()) if n%2 == 0: print("NO") exit() parent = list(map(int,input().split())) print("YES") s = [0]*n G = [[] for _ in range(n)] root = -1 for i in range(n): parent[i] -= 1 if parent[i] != -1: G[parent[i]].append(i) else: root = i stk = [root] vis = [False]*n while stk: node = stk[-1] if not vis[node]: vis[node] = True for node0 in G[node]: stk.append(node0) else: stk.pop() s[node] += 1 for node0 in G[node]: s[node] += s[node0] stk = [root] while stk: node = stk[-1] if vis[node]: vis[node] = False for node0 in G[node]: if s[node0]%2 == 0: stk.append(node0) else: stk.pop() print(node+1) for node0 in G[node]: if s[node0]%2 == 1: stk.append(node0)
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
519/D
519
D
Python 3
TESTS
4
62
0
119197834
if __name__ == '__main__': likeability = list(map(int, input().split())) s = input().lower() add = [0] * len(s) add[0] = likeability[ord(s[0])-97] freq = {} freq[s[0]] = 1 for i in range(1, len(s)): add[i] = add[i-1] + likeability[ord(s[i])-97] if s[i] in freq.keys(): freq[s[i]] = freq[s[i]] + 1 else: freq[s[i]] = 1 res = 0 for i in range(0, len(s)): if freq[s[i]] == 1: continue else: idx = s[i+1:len(s)].find(s[i]) + i + 1 freq[s[i]] = freq[s[i]] - 1 if(add[idx] - add[i] - likeability[ord(s[i])-97]) == 0: res += 1 print(res)
47
140
22,732,800
153144112
from bisect import bisect_left nums = list(map(int, input().split(' '))) s = input() pre = [] tot = 0 d = {} for i in range(len(s)): tot += nums[ord(s[i]) - ord('a')] pre.append(tot) if s[i] not in d: d[s[i]] = [] d[s[i]].append(i) ans = 0 for ch in d: g = {} if len(d[ch]) == 1: continue for i in d[ch]: if pre[i] not in g: g[pre[i]] = [] g[pre[i]].append(i) k = nums[ord(ch) - ord('a')] for i in g: if i - k in g: for j in g[i]: ans += bisect_left(g[i - k], j) print(ans)
Codeforces Round 294 (Div. 2)
CF
2,015
2
256
A and B and Interesting Substrings
A and B are preparing themselves for programming contests. After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes. A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes). B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one). Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero. Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?
The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively. The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.
Print the answer to the problem.
null
In the first sample test strings satisfying the condition above are abca and bcab. In the second sample test strings satisfying the condition above are two occurences of aa.
[{"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab", "output": "2"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa", "output": "2"}]
1,800
["data structures", "dp", "two pointers"]
47
[{"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\nxabcab\r\n", "output": "2\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\naaa\r\n", "output": "2\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\naabbccdd\r\n", "output": "4\r\n"}, {"input": "-3 4 -4 -1 3 5 -5 -3 3 2 1 4 5 -3 -1 4 2 -2 1 -1 1 5 -4 0 -5 4\r\nolspxykjqr\r\n", "output": "0\r\n"}, {"input": "-2 -2 2 1 4 0 -2 4 5 4 -5 -5 2 1 1 -1 0 -5 -2 3 -2 4 5 2 3 -5\r\nqgzhbkitmqwttdyoyvcbxincwjryzknubpacsngorexaldfurondbednowemnnlphhboycfavsovisrmfaefusoobingjhsmrukx\r\n", "output": "7\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\nbb\r\n", "output": "1\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\na\r\n", "output": "0\r\n"}, {"input": "0 0 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\nhhhhhhhhhh\r\n", "output": "45\r\n"}]
false
stdio
null
true
621/C
621
C
PyPy 3-64
TESTS
2
62
102,400
219506212
import sys input = lambda: sys.stdin.readline().rstrip() import math from heapq import heappush , heappop from collections import defaultdict,deque,Counter from bisect import * N,P = map(int, input().split()) A = [] for _ in range(N): l,r = map(int, input().split()) A.append((l,r)) def deal(l,r): c1 = (l-1)//P c2 = r//P return c2-c1 ans = 0 for i in range(N): l,r = A[i] t = deal(l,r) tmp = 1000*t/(r-l+1) ans+=tmp*3 print(ans)
94
171
11,571,200
219506548
import sys input = lambda: sys.stdin.readline().rstrip() import math from heapq import heappush , heappop from collections import defaultdict,deque,Counter from bisect import * N,P = map(int, input().split()) A = [] for _ in range(N): l,r = map(int, input().split()) A.append((l,r)) def deal(l,r): c1 = (l-1)//P c2 = r//P return c2-c1 def check(i1,i2): #print(i1,i2) l1,r1 = A[i1] l2,r2 = A[i2] t1 = deal(l1,r1) t2 = deal(l2,r2) ans = t1/(r1-l1+1)*t2/(r2-l2+1)*1000 return ans ans = 0 for i in range(N): l,r = A[i] t = deal(l,r) tmp = 1000*t/(r-l+1) ans+=tmp*4 #print('i',i) ans-=check(i-1,i) ans-=check(i,(i+1)%N) print(ans)
Codeforces Round 341 (Div. 2)
CF
2,016
2
256
Wet Shark and Flowers
There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too. Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it's favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks. At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.
The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime. The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.
Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime. Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows: 1. (1, 420, 420420): note that s0·s1 = 420, s1·s2 = 176576400, and s2·s0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars. 2. (1, 420, 420421): now, the product s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1 will receive 2000. The total is 4000. 3. (1, 421, 420420): total is 4000 4. (1, 421, 420421): total is 0. 5. (2, 420, 420420): total is 6000. 6. (2, 420, 420421): total is 6000. 7. (2, 421, 420420): total is 6000. 8. (2, 421, 420421): total is 4000. The expected value is $${ \frac { 6 0 0 0 + 4 0 0 0 + 4 0 0 0 + 0 + 6 0 0 0 + 6 0 0 0 + 6 0 0 0 + 4 0 0 0 } { 8 } } = 4 5 0 0$$. In the second sample, no combination of quantities will garner the sharks any money.
[{"input": "3 2\n1 2\n420 421\n420420 420421", "output": "4500.0"}, {"input": "3 5\n1 4\n2 3\n11 14", "output": "0.0"}]
1,700
["combinatorics", "math", "number theory", "probabilities"]
94
[{"input": "3 2\r\n1 2\r\n420 421\r\n420420 420421\r\n", "output": "4500.0\r\n"}, {"input": "3 5\r\n1 4\r\n2 3\r\n11 14\r\n", "output": "0.0\r\n"}, {"input": "3 3\r\n3 3\r\n2 4\r\n1 1\r\n", "output": "4666.666666666667\r\n"}, {"input": "5 5\r\n5 204\r\n420 469\r\n417 480\r\n442 443\r\n44 46\r\n", "output": "3451.25\r\n"}, {"input": "3 2\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "6000.0\r\n"}, {"input": "6 7\r\n8 13\r\n14 14\r\n8 13\r\n14 14\r\n8 13\r\n14 14\r\n", "output": "12000.0\r\n"}, {"input": "3 7\r\n7 14\r\n700000000 700000007\r\n420 4200\r\n", "output": "2304.2515207617034\r\n"}, {"input": "5 999999937\r\n999999935 999999936\r\n999999937 999999938\r\n999999939 999999940\r\n999999941 999999942\r\n999999943 999999944\r\n", "output": "2000.0\r\n"}, {"input": "5 999999937\r\n1 999999936\r\n1 999999936\r\n1 999999936\r\n1 999999936\r\n1 999999936\r\n", "output": "0.0\r\n"}, {"input": "20 999999937\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n", "output": "30000.0\r\n"}, {"input": "9 41\r\n40 42\r\n42 44\r\n44 46\r\n82 84\r\n82 83\r\n80 83\r\n40 83\r\n40 82\r\n42 82\r\n", "output": "5503.274377352654\r\n"}, {"input": "3 2\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "2000.0\r\n"}, {"input": "12 3\r\n697806 966852\r\n802746 974920\r\n579567 821770\r\n628655 642480\r\n649359 905832\r\n87506 178848\r\n605628 924780\r\n843338 925533\r\n953514 978612\r\n375312 997707\r\n367620 509906\r\n277106 866177\r\n", "output": "13333.518289809368\r\n"}, {"input": "5 3\r\n67050 461313\r\n927808 989615\r\n169239 201720\r\n595515 756354\r\n392844 781910\r\n", "output": "5555.597086312073\r\n"}, {"input": "6 7\r\n984774 984865\r\n720391 916269\r\n381290 388205\r\n628383 840455\r\n747138 853964\r\n759705 959629\r\n", "output": "3215.6233297395006\r\n"}, {"input": "3 5\r\n99535 124440\r\n24114 662840\r\n529335 875935\r\n", "output": "2160.11317825774\r\n"}, {"input": "4 3\r\n561495 819666\r\n718673 973130\r\n830124 854655\r\n430685 963699\r\n", "output": "4444.521972611004\r\n"}, {"input": "10 3\r\n311664 694971\r\n364840 366487\r\n560148 821101\r\n896470 923613\r\n770019 828958\r\n595743 827536\r\n341418 988218\r\n207847 366132\r\n517968 587855\r\n168695 878142\r\n", "output": "11110.602699850484\r\n"}, {"input": "11 3\r\n66999 737907\r\n499872 598806\r\n560583 823299\r\n579017 838419\r\n214308 914576\r\n31820 579035\r\n373821 695652\r\n438988 889317\r\n181332 513682\r\n740575 769488\r\n597348 980891\r\n", "output": "12222.259608784536\r\n"}, {"input": "12 3\r\n158757 341790\r\n130709 571435\r\n571161 926255\r\n851779 952236\r\n914910 941369\r\n774359 860799\r\n224067 618483\r\n411639 902888\r\n264423 830336\r\n33133 608526\r\n951696 976379\r\n923880 968563\r\n", "output": "13333.377729413933\r\n"}, {"input": "9 2\r\n717582 964152\r\n268030 456147\r\n400022 466269\r\n132600 698200\r\n658890 807357\r\n196658 849497\r\n257020 380298\r\n267729 284534\r\n311978 917744\r\n", "output": "13500.015586135814\r\n"}, {"input": "10 7\r\n978831 984305\r\n843967 844227\r\n454356 748444\r\n219513 623868\r\n472997 698189\r\n542337 813387\r\n867615 918554\r\n413076 997267\r\n79310 138855\r\n195703 296681\r\n", "output": "5303.027968302269\r\n"}]
false
stdio
import sys def read_number(path): with open(path, 'r') as f: lines = [line.strip() for line in f.readlines() if line.strip()] if len(lines) != 1: return None try: return float(lines[0]) except ValueError: return None def main(): input_path = sys.argv[1] correct_output_path = sys.argv[2] submission_output_path = sys.argv[3] a = read_number(submission_output_path) b = read_number(correct_output_path) if a is None or b is None: print(0) return error = abs(a - b) denominator = max(1.0, b) if error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
761/D
761
D
Python 3
TESTS
0
46
5,529,600
25956672
#!/usr/bin/env python3 from sys import stdin,stdout def ri(): return map(int, input().split()) n, l, r = ri() a = list(ri()) p = list(ri()) pi = [i for i in range(n)] pi.sort(key=lambda e: p[e]) b = [0 for i in range(n)] i = p[0] b[i] = l cp = b[i] - a[i] for ii in range(1,n): i = pi[ii] cp +=1 b[i] = a[i] + cp if b[i] > r: print(-1) exit() print(*b)
63
343
18,841,600
44333931
R = lambda: map(int, input().split()) n, l, r = R() a, d = list(R()), list(R()) b = [av + cv for av, cv in zip(a, d)] mi = min(b) if mi < l: b = [bv + l - mi for bv in b] mx = max(b) if mx > r: b = [bv - (mx - r) for bv in b] if l <= min(b) <= max(b) <= r: print(' '.join(map(str, b))) else: print(-1)
Codeforces Round 394 (Div. 2)
CF
2,017
2
256
Dasha and Very Difficult Problem
Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci = bi - ai. About sequences a and b we know that their elements are in the range from l to r. More formally, elements satisfy the following conditions: l ≤ ai ≤ r and l ≤ bi ≤ r. About sequence c we know that all its elements are distinct. Dasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence a and the compressed sequence of the sequence c were known from that test. Let's give the definition to a compressed sequence. A compressed sequence of sequence c of length n is a sequence p of length n, so that pi equals to the number of integers which are less than or equal to ci in the sequence c. For example, for the sequence c = [250, 200, 300, 100, 50] the compressed sequence will be p = [4, 3, 5, 2, 1]. Pay attention that in c all integers are distinct. Consequently, the compressed sequence contains all integers from 1 to n inclusively. Help Dasha to find any sequence b for which the calculated compressed sequence of sequence c is correct.
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ 109) — the length of the sequence and boundaries of the segment where the elements of sequences a and b are. The next line contains n integers a1, a2, ..., an (l ≤ ai ≤ r) — the elements of the sequence a. The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the compressed sequence of the sequence c.
If there is no the suitable sequence b, then in the only line print "-1". Otherwise, in the only line print n integers — the elements of any suitable sequence b.
null
Sequence b which was found in the second sample is suitable, because calculated sequence c = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1, - 2, - 6, 0] (note that ci = bi - ai) has compressed sequence equals to p = [3, 2, 1, 4].
[{"input": "5 1 5\n1 1 1 1 1\n3 1 5 4 2", "output": "3 1 5 4 2"}, {"input": "4 2 9\n3 4 8 9\n3 2 1 4", "output": "2 2 2 9"}, {"input": "6 1 5\n1 1 1 1 1 1\n2 3 5 4 1 6", "output": "-1"}]
1,700
["binary search", "brute force", "constructive algorithms", "greedy", "sortings"]
63
[{"input": "5 1 5\r\n1 1 1 1 1\r\n3 1 5 4 2\r\n", "output": "3 1 5 4 2 "}, {"input": "4 2 9\r\n3 4 8 9\r\n3 2 1 4\r\n", "output": "2 2 2 9 "}, {"input": "6 1 5\r\n1 1 1 1 1 1\r\n2 3 5 4 1 6\r\n", "output": "-1\r\n"}, {"input": "5 1 7\r\n1 4 4 6 5\r\n5 2 1 4 3\r\n", "output": "2 2 1 6 4 "}, {"input": "5 10 100\r\n12 14 15 11 13\r\n4 2 1 5 3\r\n", "output": "10 10 10 10 10 "}, {"input": "2 1 1000000000\r\n1000000000 1\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "2 1 1000000000\r\n1000000000 1\r\n1 2\r\n", "output": "1 1 "}, {"input": "5 1 5\r\n1 1 1 1 1\r\n1 2 3 4 5\r\n", "output": "1 2 3 4 5 "}, {"input": "5 1 5\r\n1 1 1 1 1\r\n2 3 1 5 4\r\n", "output": "2 3 1 5 4 "}, {"input": "1 1000000000 1000000000\r\n1000000000\r\n1\r\n", "output": "1000000000 "}, {"input": "6 3 7\r\n6 7 5 5 5 5\r\n2 1 4 3 5 6\r\n", "output": "3 3 4 3 5 6 "}, {"input": "3 5 100\r\n10 50 100\r\n3 2 1\r\n", "output": "5 5 5 "}, {"input": "10 1 10\r\n9 2 9 5 5 2 6 8 2 8\r\n2 10 1 6 7 8 5 3 9 4\r\n", "output": "2 3 1 2 3 1 2 2 2 3 "}, {"input": "30 100 200\r\n102 108 122 116 107 145 195 145 119 110 187 196 140 174 104 190 193 181 118 127 157 111 139 175 173 191 181 105 142 166\r\n30 26 20 23 27 15 2 14 21 25 6 1 17 10 29 5 3 7 22 19 13 24 18 9 11 4 8 28 16 12\r\n", "output": "100 100 100 100 100 101 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 101 100 100 100 "}, {"input": "50 10 15\r\n13 14 12 14 12 15 13 10 11 11 15 10 14 11 14 12 11 10 10 13 11 13 12 15 14 15 13 15 13 10 12 10 15 15 10 14 11 11 10 14 11 12 13 12 10 11 13 15 14 11\r\n20 10 25 13 29 6 24 47 37 39 2 44 12 33 9 26 35 49 46 19 38 23 30 5 16 4 21 7 18 45 28 43 1 8 48 15 36 40 50 11 32 27 22 31 42 41 17 3 14 34\r\n", "output": "-1\r\n"}, {"input": "10 8 10\r\n8 10 10 9 8 10 10 10 10 10\r\n9 5 6 8 10 2 7 3 1 4\r\n", "output": "-1\r\n"}, {"input": "50 1 50\r\n6 20 27 26 46 35 41 41 4 12 48 13 20 5 35 7 37 3 18 17 22 42 28 1 30 46 38 38 42 29 33 42 8 45 22 40 11 33 12 15 16 49 30 19 29 40 5 12 32 22\r\n47 27 35 40 31 26 42 19 11 49 37 3 41 34 36 30 16 44 12 4 46 20 14 39 32 25 18 1 21 6 2 17 48 7 5 24 29 33 50 43 8 15 28 23 22 13 45 38 10 9\r\n", "output": "-1\r\n"}, {"input": "11 5 11\r\n9 8 7 5 7 5 9 5 10 5 7\r\n3 4 6 9 5 11 2 10 1 8 7\r\n", "output": "6 6 7 8 6 10 5 9 5 7 8 "}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: return f.read().strip() def main(input_path, output_path, submission_path): # Read input with open(input_path, 'r') as f: lines = f.readlines() n, l, r = map(int, lines[0].split()) a = list(map(int, lines[1].split())) p = list(map(int, lines[2].split())) # Read submission and reference outputs sub_content = read_file(submission_path).split() ref_content = read_file(output_path).split() # Check if submission is -1 if sub_content == ['-1']: if ref_content == ['-1']: print(100) else: print(0) return # Check if reference is -1 but submission isn't if ref_content == ['-1']: print(0) return # Parse submission's sequence if len(sub_content) != n: print(0) return try: b = list(map(int, sub_content)) except: print(0) return # Check all bi are within [l, r] for num in b: if not (l <= num <= r): print(0) return # Compute c and check distinct c = [b[i] - a[i] for i in range(n)] if len(set(c)) != n: print(0) return # Compute compressed sequence sorted_c = sorted((ci, i) for i, ci in enumerate(c)) rank = [0] * n for pos, (_, idx) in enumerate(sorted_c): rank[idx] = pos + 1 # Compare with p if rank != p: print(0) return print(100) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
764/B
764
B
Python 3
TESTS
10
529
15,564,800
55088513
n=int(input()) L=[int(x) for x in input().split()] if n%2==1: n+=1 for i in range(1,n//2,2): L[i-1],L[-1*i]=L[-1*i],L[i-1] for x in L: print(x,end=" ")
34
139
15,462,400
143268136
n = int(input()) s = [x for x in input().split()] #if n%2!=0: top = (n+1)//2 for i in range(1,top+1,2): temp = s[i-1] s[i-1] = s[n-i] s[n-i] = temp print(" ".join(s))
Codeforces Round 395 (Div. 2)
CF
2,017
1
256
Timofey and cubes
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1. After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.
null
Consider the first sample. 1. At the begining row was [2, 3, 9, 6, 7, 1, 4]. 2. After first operation row was [4, 1, 7, 6, 9, 3, 2]. 3. After second operation row was [4, 3, 9, 6, 7, 1, 2]. 4. After third operation row was [4, 3, 7, 6, 9, 1, 2]. 5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
[{"input": "7\n4 3 7 6 9 1 2", "output": "2 3 9 6 7 1 4"}, {"input": "8\n6 1 4 2 5 6 9 2", "output": "2 1 6 2 5 4 9 6"}]
900
["constructive algorithms", "implementation"]
34
[{"input": "7\r\n4 3 7 6 9 1 2\r\n", "output": "2 3 9 6 7 1 4"}, {"input": "8\r\n6 1 4 2 5 6 9 2\r\n", "output": "2 1 6 2 5 4 9 6"}, {"input": "1\r\n1424\r\n", "output": "1424"}, {"input": "9\r\n-7 9 -4 9 -6 11 15 2 -10\r\n", "output": "-10 9 15 9 -6 11 -4 2 -7"}, {"input": "2\r\n21968 5686\r\n", "output": "5686 21968"}, {"input": "5\r\n241218936 -825949895 -84926813 491336344 -872198236\r\n", "output": "-872198236 -825949895 -84926813 491336344 241218936"}, {"input": "42\r\n-557774624 828320986 -345782722 -62979938 -681259411 -945983652 -139095040 832293378 -82572118 432027535 88438103 568183540 961782904 73543295 615958219 -5050584 322982437 -146046730 759453379 129267920 -819827396 -348156048 805080102 390723009 -771277251 -79011872 -592313207 528489973 656201270 -127795621 17284747 145139617 -565641608 83452176 -223074608 545811186 -657981923 -204657836 154779765 -476867246 180386291 202782486\r\n", "output": "202782486 828320986 -476867246 -62979938 -204657836 -945983652 545811186 832293378 83452176 432027535 145139617 568183540 -127795621 73543295 528489973 -5050584 -79011872 -146046730 390723009 129267920 -348156048 -819827396 805080102 759453379 -771277251 322982437 -592313207 615958219 656201270 961782904 17284747 88438103 -565641608 -82572118 -223074608 -139095040 -657981923 -681259411 154779765 -345782722 180386291 -557774624"}, {"input": "2\r\n1 2\r\n", "output": "2 1"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "6 2 4 3 5 1"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3 1 2 0"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10 2 8 4 6 5 7 3 9 1"}, {"input": "2\r\n4 5\r\n", "output": "5 4"}, {"input": "2\r\n1 5\r\n", "output": "5 1"}, {"input": "2\r\n2 5\r\n", "output": "5 2"}, {"input": "2\r\n3 4\r\n", "output": "4 3"}, {"input": "2\r\n10 0\r\n", "output": "0 10"}, {"input": "66\r\n8229 5299 5562 1259 2258 1126 5026 8796 3497 7111 6680 1180 1426 4821 3322 3502 400 2951 4187 417 5234 1266 7525 7648 417 9297 174 670 1790 4121 9737 938 4616 3402 2009 2461 474 3728 327 3353 9773 6818 8781 874 2209 1260 1647 6126 9287 9638 5049 1256 4459 863 1835 4822 6809 348 5053 7784 6927 2140 9173 7817 5095 4184\r\n", "output": "4184 5299 7817 1259 2140 1126 7784 8796 348 7111 4822 1180 863 4821 1256 3502 9638 2951 6126 417 1260 1266 874 7648 6818 9297 3353 670 3728 4121 2461 938 3402 4616 2009 9737 474 1790 327 174 9773 417 8781 7525 2209 5234 1647 4187 9287 400 5049 3322 4459 1426 1835 6680 6809 3497 5053 5026 6927 2258 9173 5562 5095 8229"}]
false
stdio
null
true
518/C
518
C
PyPy 3-64
TESTS
2
46
0
194402404
import sys input = sys.stdin.readline n, c, k = map(int, input().split()) w = list(map(int, input().split())) d = [0]*(n+1) for i, j in enumerate(w): d[j] = i for i in map(int, input().split()): q = d[i]//k c += q if q > 0: w[d[i]], w[d[i]-1], d[i], d[w[d[i]-1]] = w[d[i]-1], w[d[i]], d[i]-1, d[i] print(c)
43
124
19,763,200
194404014
import sys input = sys.stdin.readline n, c, k = map(int, input().split()) w = list(map(int, input().split())) d = [0]*(n+1) for i, j in enumerate(w): d[j] = i for i in map(int, input().split()): a = d[i] c += d[i]//k if d[i] > 0: b = a - 1 j = w[b] w[a], w[b], d[i], d[j] = w[b], w[a], d[j], d[i] print(c)
Codeforces Round 293 (Div. 2)
CF
2,015
1
256
Anya and Smartphone
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty). Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture — press the icon of the required application exactly once to launch it. After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1. All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change. Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? Note that one application may be launched multiple times.
The first line of the input contains three numbers n, m, k (1 ≤ n, m, k ≤ 105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen. The next line contains n integers, permutation a1, a2, ..., an — the initial order of icons from left to right in the menu (from the first to the last one), ai —  is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai. The third line contains m integers b1, b2, ..., bm(1 ≤ bi ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.
Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.
null
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures. After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture. In total, Anya makes 7 gestures.
[{"input": "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1", "output": "7"}, {"input": "5 4 2\n3 1 5 2 4\n4 4 4 4", "output": "8"}]
1,600
["constructive algorithms", "data structures", "implementation"]
43
[{"input": "8 3 3\r\n1 2 3 4 5 6 7 8\r\n7 8 1\r\n", "output": "7\r\n"}, {"input": "5 4 2\r\n3 1 5 2 4\r\n4 4 4 4\r\n", "output": "8\r\n"}, {"input": "10 10 3\r\n1 2 3 4 5 6 7 8 9 10\r\n2 3 4 5 6 7 8 9 10 1\r\n", "output": "25\r\n"}, {"input": "10 12 3\r\n6 1 2 9 3 10 8 5 7 4\r\n3 9 9 4 8 2 3 8 10 8 3 4\r\n", "output": "29\r\n"}, {"input": "16 12 8\r\n6 11 12 5 1 10 16 3 13 15 9 4 8 2 14 7\r\n6 13 8 12 8 15 12 16 8 11 3 16\r\n", "output": "18\r\n"}, {"input": "4 3 5\r\n3 1 4 2\r\n4 2 4\r\n", "output": "3\r\n"}, {"input": "4 2 15\r\n2 4 3 1\r\n1 2\r\n", "output": "2\r\n"}, {"input": "71 96 75634\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\r\n27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26\r\n", "output": "96\r\n"}, {"input": "71 81 2149\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 29 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\r\n29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29\r\n", "output": "81\r\n"}]
false
stdio
null
true
1006/A
1006
A
Python 3
TESTS
8
109
307,200
92581389
x = int(input()) l=list(map(int,input().split())) for i in range(x): if x>2 and x<10**9: if l[i]%2==0: l[i]-=1 if x==1 and x<10**9: if l[i]%2==0: l[i]-=1 else: l[i]+=1 print(*l)
18
31
0
223294695
n = int(input()) nums = list(map(int, input().split())) for num in nums: if num % 2 == 0: print(num-1, end = " ") else: print(num, end = " ")
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Adjacent Replacements
Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!). Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: - Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; - Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; - Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; - Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; - Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; - Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; - $$$\dots$$$ - Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; - Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above. For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array. Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.
The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.
Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.
null
The first example is described in the problem statement.
[{"input": "5\n1 2 4 5 10", "output": "1 1 3 5 9"}, {"input": "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999"}]
800
["implementation"]
18
[{"input": "5\r\n1 2 4 5 10\r\n", "output": "1 1 3 5 9\r\n"}, {"input": "10\r\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000\r\n", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999\r\n"}, {"input": "1\r\n999999999\r\n", "output": "999999999\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "1\r\n210400\r\n", "output": "210399\r\n"}, {"input": "5\r\n100000000 100000000 100000000 100000000 100000000\r\n", "output": "99999999 99999999 99999999 99999999 99999999\r\n"}, {"input": "1\r\n2441139\r\n", "output": "2441139\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1 1\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1 1 1\r\n"}, {"input": "2\r\n4 4\r\n", "output": "3 3\r\n"}]
false
stdio
null
true
764/B
764
B
Python 3
TESTS
10
233
22,937,600
24396664
n = int(input()) a = list(map(int, input().split())) for i in range(0, n // 2 + 1, 2): t = a[i] a[i] = a[n - 1 - i] a[n - 1 - i] = t if n % 2 == 0: t = a[n//2-1] a[n//2-1] = a[n//2] a[n//2] = t print(' '.join(map(str, a)))
34
139
19,148,800
24477774
a =input() k =input() k =k.split(' ') l =len(k) i = 0 while i<l//2: k[i],k[-(i+1)]=k[-(i+1)],k[i] i+=2 print(' '.join(k))
Codeforces Round 395 (Div. 2)
CF
2,017
1
256
Timofey and cubes
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1. After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.
null
Consider the first sample. 1. At the begining row was [2, 3, 9, 6, 7, 1, 4]. 2. After first operation row was [4, 1, 7, 6, 9, 3, 2]. 3. After second operation row was [4, 3, 9, 6, 7, 1, 2]. 4. After third operation row was [4, 3, 7, 6, 9, 1, 2]. 5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
[{"input": "7\n4 3 7 6 9 1 2", "output": "2 3 9 6 7 1 4"}, {"input": "8\n6 1 4 2 5 6 9 2", "output": "2 1 6 2 5 4 9 6"}]
900
["constructive algorithms", "implementation"]
34
[{"input": "7\r\n4 3 7 6 9 1 2\r\n", "output": "2 3 9 6 7 1 4"}, {"input": "8\r\n6 1 4 2 5 6 9 2\r\n", "output": "2 1 6 2 5 4 9 6"}, {"input": "1\r\n1424\r\n", "output": "1424"}, {"input": "9\r\n-7 9 -4 9 -6 11 15 2 -10\r\n", "output": "-10 9 15 9 -6 11 -4 2 -7"}, {"input": "2\r\n21968 5686\r\n", "output": "5686 21968"}, {"input": "5\r\n241218936 -825949895 -84926813 491336344 -872198236\r\n", "output": "-872198236 -825949895 -84926813 491336344 241218936"}, {"input": "42\r\n-557774624 828320986 -345782722 -62979938 -681259411 -945983652 -139095040 832293378 -82572118 432027535 88438103 568183540 961782904 73543295 615958219 -5050584 322982437 -146046730 759453379 129267920 -819827396 -348156048 805080102 390723009 -771277251 -79011872 -592313207 528489973 656201270 -127795621 17284747 145139617 -565641608 83452176 -223074608 545811186 -657981923 -204657836 154779765 -476867246 180386291 202782486\r\n", "output": "202782486 828320986 -476867246 -62979938 -204657836 -945983652 545811186 832293378 83452176 432027535 145139617 568183540 -127795621 73543295 528489973 -5050584 -79011872 -146046730 390723009 129267920 -348156048 -819827396 805080102 759453379 -771277251 322982437 -592313207 615958219 656201270 961782904 17284747 88438103 -565641608 -82572118 -223074608 -139095040 -657981923 -681259411 154779765 -345782722 180386291 -557774624"}, {"input": "2\r\n1 2\r\n", "output": "2 1"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "6 2 4 3 5 1"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3 1 2 0"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10 2 8 4 6 5 7 3 9 1"}, {"input": "2\r\n4 5\r\n", "output": "5 4"}, {"input": "2\r\n1 5\r\n", "output": "5 1"}, {"input": "2\r\n2 5\r\n", "output": "5 2"}, {"input": "2\r\n3 4\r\n", "output": "4 3"}, {"input": "2\r\n10 0\r\n", "output": "0 10"}, {"input": "66\r\n8229 5299 5562 1259 2258 1126 5026 8796 3497 7111 6680 1180 1426 4821 3322 3502 400 2951 4187 417 5234 1266 7525 7648 417 9297 174 670 1790 4121 9737 938 4616 3402 2009 2461 474 3728 327 3353 9773 6818 8781 874 2209 1260 1647 6126 9287 9638 5049 1256 4459 863 1835 4822 6809 348 5053 7784 6927 2140 9173 7817 5095 4184\r\n", "output": "4184 5299 7817 1259 2140 1126 7784 8796 348 7111 4822 1180 863 4821 1256 3502 9638 2951 6126 417 1260 1266 874 7648 6818 9297 3353 670 3728 4121 2461 938 3402 4616 2009 9737 474 1790 327 174 9773 417 8781 7525 2209 5234 1647 4187 9287 400 5049 3322 4459 1426 1835 6680 6809 3497 5053 5026 6927 2258 9173 5562 5095 8229"}]
false
stdio
null
true
764/B
764
B
Python 3
TESTS
10
218
23,756,800
25575354
'''input 8 6 1 4 2 5 6 9 2 ''' n = int(input()) s = list(map(int, input().split())) t = s[::-1] if n % 2 == 1: for x in range(0, n, 2): s[x] = t[x] else: for y in range(0, n//2, 2): s[y] = t[y] for z in range(n//2+1, n, 2): s[z] = t[z] print(" ".join(map(str, s)))
34
139
20,172,800
24446713
n = int(input()) a = input().split() for i in range(0, n // 2, 2): a[i], a[n - i - 1] = a[n - i - 1], a[i] print( " ".join(a))
Codeforces Round 395 (Div. 2)
CF
2,017
1
256
Timofey and cubes
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1. After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.
null
Consider the first sample. 1. At the begining row was [2, 3, 9, 6, 7, 1, 4]. 2. After first operation row was [4, 1, 7, 6, 9, 3, 2]. 3. After second operation row was [4, 3, 9, 6, 7, 1, 2]. 4. After third operation row was [4, 3, 7, 6, 9, 1, 2]. 5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
[{"input": "7\n4 3 7 6 9 1 2", "output": "2 3 9 6 7 1 4"}, {"input": "8\n6 1 4 2 5 6 9 2", "output": "2 1 6 2 5 4 9 6"}]
900
["constructive algorithms", "implementation"]
34
[{"input": "7\r\n4 3 7 6 9 1 2\r\n", "output": "2 3 9 6 7 1 4"}, {"input": "8\r\n6 1 4 2 5 6 9 2\r\n", "output": "2 1 6 2 5 4 9 6"}, {"input": "1\r\n1424\r\n", "output": "1424"}, {"input": "9\r\n-7 9 -4 9 -6 11 15 2 -10\r\n", "output": "-10 9 15 9 -6 11 -4 2 -7"}, {"input": "2\r\n21968 5686\r\n", "output": "5686 21968"}, {"input": "5\r\n241218936 -825949895 -84926813 491336344 -872198236\r\n", "output": "-872198236 -825949895 -84926813 491336344 241218936"}, {"input": "42\r\n-557774624 828320986 -345782722 -62979938 -681259411 -945983652 -139095040 832293378 -82572118 432027535 88438103 568183540 961782904 73543295 615958219 -5050584 322982437 -146046730 759453379 129267920 -819827396 -348156048 805080102 390723009 -771277251 -79011872 -592313207 528489973 656201270 -127795621 17284747 145139617 -565641608 83452176 -223074608 545811186 -657981923 -204657836 154779765 -476867246 180386291 202782486\r\n", "output": "202782486 828320986 -476867246 -62979938 -204657836 -945983652 545811186 832293378 83452176 432027535 145139617 568183540 -127795621 73543295 528489973 -5050584 -79011872 -146046730 390723009 129267920 -348156048 -819827396 805080102 759453379 -771277251 322982437 -592313207 615958219 656201270 961782904 17284747 88438103 -565641608 -82572118 -223074608 -139095040 -657981923 -681259411 154779765 -345782722 180386291 -557774624"}, {"input": "2\r\n1 2\r\n", "output": "2 1"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "6 2 4 3 5 1"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3 1 2 0"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10 2 8 4 6 5 7 3 9 1"}, {"input": "2\r\n4 5\r\n", "output": "5 4"}, {"input": "2\r\n1 5\r\n", "output": "5 1"}, {"input": "2\r\n2 5\r\n", "output": "5 2"}, {"input": "2\r\n3 4\r\n", "output": "4 3"}, {"input": "2\r\n10 0\r\n", "output": "0 10"}, {"input": "66\r\n8229 5299 5562 1259 2258 1126 5026 8796 3497 7111 6680 1180 1426 4821 3322 3502 400 2951 4187 417 5234 1266 7525 7648 417 9297 174 670 1790 4121 9737 938 4616 3402 2009 2461 474 3728 327 3353 9773 6818 8781 874 2209 1260 1647 6126 9287 9638 5049 1256 4459 863 1835 4822 6809 348 5053 7784 6927 2140 9173 7817 5095 4184\r\n", "output": "4184 5299 7817 1259 2140 1126 7784 8796 348 7111 4822 1180 863 4821 1256 3502 9638 2951 6126 417 1260 1266 874 7648 6818 9297 3353 670 3728 4121 2461 938 3402 4616 2009 9737 474 1790 327 174 9773 417 8781 7525 2209 5234 1647 4187 9287 400 5049 3322 4459 1426 1835 6680 6809 3497 5053 5026 6927 2258 9173 5562 5095 8229"}]
false
stdio
null
true
764/B
764
B
Python 3
TESTS
10
171
15,872,000
89821168
n = int(input()) s = input().split() if(n%2==0): m=int(n//2)-1 else: m=int(n//2) for i in range(m): if(i%2==0): s[i],s[n-i-1] = s[n-i-1],s[i] print(" ".join(s))
34
139
20,172,800
24638098
n, a = int(input()), input().split() for i in range(0, n // 2, 2): a[i], a[n - i - 1] = a[n - i - 1], a[i] print(' '.join(a))
Codeforces Round 395 (Div. 2)
CF
2,017
1
256
Timofey and cubes
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1. After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.
null
Consider the first sample. 1. At the begining row was [2, 3, 9, 6, 7, 1, 4]. 2. After first operation row was [4, 1, 7, 6, 9, 3, 2]. 3. After second operation row was [4, 3, 9, 6, 7, 1, 2]. 4. After third operation row was [4, 3, 7, 6, 9, 1, 2]. 5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
[{"input": "7\n4 3 7 6 9 1 2", "output": "2 3 9 6 7 1 4"}, {"input": "8\n6 1 4 2 5 6 9 2", "output": "2 1 6 2 5 4 9 6"}]
900
["constructive algorithms", "implementation"]
34
[{"input": "7\r\n4 3 7 6 9 1 2\r\n", "output": "2 3 9 6 7 1 4"}, {"input": "8\r\n6 1 4 2 5 6 9 2\r\n", "output": "2 1 6 2 5 4 9 6"}, {"input": "1\r\n1424\r\n", "output": "1424"}, {"input": "9\r\n-7 9 -4 9 -6 11 15 2 -10\r\n", "output": "-10 9 15 9 -6 11 -4 2 -7"}, {"input": "2\r\n21968 5686\r\n", "output": "5686 21968"}, {"input": "5\r\n241218936 -825949895 -84926813 491336344 -872198236\r\n", "output": "-872198236 -825949895 -84926813 491336344 241218936"}, {"input": "42\r\n-557774624 828320986 -345782722 -62979938 -681259411 -945983652 -139095040 832293378 -82572118 432027535 88438103 568183540 961782904 73543295 615958219 -5050584 322982437 -146046730 759453379 129267920 -819827396 -348156048 805080102 390723009 -771277251 -79011872 -592313207 528489973 656201270 -127795621 17284747 145139617 -565641608 83452176 -223074608 545811186 -657981923 -204657836 154779765 -476867246 180386291 202782486\r\n", "output": "202782486 828320986 -476867246 -62979938 -204657836 -945983652 545811186 832293378 83452176 432027535 145139617 568183540 -127795621 73543295 528489973 -5050584 -79011872 -146046730 390723009 129267920 -348156048 -819827396 805080102 759453379 -771277251 322982437 -592313207 615958219 656201270 961782904 17284747 88438103 -565641608 -82572118 -223074608 -139095040 -657981923 -681259411 154779765 -345782722 180386291 -557774624"}, {"input": "2\r\n1 2\r\n", "output": "2 1"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "6 2 4 3 5 1"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3 1 2 0"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10 2 8 4 6 5 7 3 9 1"}, {"input": "2\r\n4 5\r\n", "output": "5 4"}, {"input": "2\r\n1 5\r\n", "output": "5 1"}, {"input": "2\r\n2 5\r\n", "output": "5 2"}, {"input": "2\r\n3 4\r\n", "output": "4 3"}, {"input": "2\r\n10 0\r\n", "output": "0 10"}, {"input": "66\r\n8229 5299 5562 1259 2258 1126 5026 8796 3497 7111 6680 1180 1426 4821 3322 3502 400 2951 4187 417 5234 1266 7525 7648 417 9297 174 670 1790 4121 9737 938 4616 3402 2009 2461 474 3728 327 3353 9773 6818 8781 874 2209 1260 1647 6126 9287 9638 5049 1256 4459 863 1835 4822 6809 348 5053 7784 6927 2140 9173 7817 5095 4184\r\n", "output": "4184 5299 7817 1259 2140 1126 7784 8796 348 7111 4822 1180 863 4821 1256 3502 9638 2951 6126 417 1260 1266 874 7648 6818 9297 3353 670 3728 4121 2461 938 3402 4616 2009 9737 474 1790 327 174 9773 417 8781 7525 2209 5234 1647 4187 9287 400 5049 3322 4459 1426 1835 6680 6809 3497 5053 5026 6927 2258 9173 5562 5095 8229"}]
false
stdio
null
true
764/B
764
B
Python 3
TESTS
10
109
14,233,600
30127360
def main(): n, l = int(input()), input().split() for i, j in zip(range(0, n // 2 + 1, 2), range(n - 1, n // 2, -2)): l[i], l[j] = l[j], l[i] print(' '.join(l)) if __name__ == '__main__': main()
34
139
26,931,200
177067776
def get(): return list(map(int, input().split())) def intput(): return int(input()) def main(): n=intput() arr=get() i=1 for i in range(n//2): if (not i&1 ): arr[i],arr[n-1-i]= arr[n-1-i],arr[i] print(*arr) main()
Codeforces Round 395 (Div. 2)
CF
2,017
1
256
Timofey and cubes
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1. After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.
null
Consider the first sample. 1. At the begining row was [2, 3, 9, 6, 7, 1, 4]. 2. After first operation row was [4, 1, 7, 6, 9, 3, 2]. 3. After second operation row was [4, 3, 9, 6, 7, 1, 2]. 4. After third operation row was [4, 3, 7, 6, 9, 1, 2]. 5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
[{"input": "7\n4 3 7 6 9 1 2", "output": "2 3 9 6 7 1 4"}, {"input": "8\n6 1 4 2 5 6 9 2", "output": "2 1 6 2 5 4 9 6"}]
900
["constructive algorithms", "implementation"]
34
[{"input": "7\r\n4 3 7 6 9 1 2\r\n", "output": "2 3 9 6 7 1 4"}, {"input": "8\r\n6 1 4 2 5 6 9 2\r\n", "output": "2 1 6 2 5 4 9 6"}, {"input": "1\r\n1424\r\n", "output": "1424"}, {"input": "9\r\n-7 9 -4 9 -6 11 15 2 -10\r\n", "output": "-10 9 15 9 -6 11 -4 2 -7"}, {"input": "2\r\n21968 5686\r\n", "output": "5686 21968"}, {"input": "5\r\n241218936 -825949895 -84926813 491336344 -872198236\r\n", "output": "-872198236 -825949895 -84926813 491336344 241218936"}, {"input": "42\r\n-557774624 828320986 -345782722 -62979938 -681259411 -945983652 -139095040 832293378 -82572118 432027535 88438103 568183540 961782904 73543295 615958219 -5050584 322982437 -146046730 759453379 129267920 -819827396 -348156048 805080102 390723009 -771277251 -79011872 -592313207 528489973 656201270 -127795621 17284747 145139617 -565641608 83452176 -223074608 545811186 -657981923 -204657836 154779765 -476867246 180386291 202782486\r\n", "output": "202782486 828320986 -476867246 -62979938 -204657836 -945983652 545811186 832293378 83452176 432027535 145139617 568183540 -127795621 73543295 528489973 -5050584 -79011872 -146046730 390723009 129267920 -348156048 -819827396 805080102 759453379 -771277251 322982437 -592313207 615958219 656201270 961782904 17284747 88438103 -565641608 -82572118 -223074608 -139095040 -657981923 -681259411 154779765 -345782722 180386291 -557774624"}, {"input": "2\r\n1 2\r\n", "output": "2 1"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "6 2 4 3 5 1"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3 1 2 0"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10 2 8 4 6 5 7 3 9 1"}, {"input": "2\r\n4 5\r\n", "output": "5 4"}, {"input": "2\r\n1 5\r\n", "output": "5 1"}, {"input": "2\r\n2 5\r\n", "output": "5 2"}, {"input": "2\r\n3 4\r\n", "output": "4 3"}, {"input": "2\r\n10 0\r\n", "output": "0 10"}, {"input": "66\r\n8229 5299 5562 1259 2258 1126 5026 8796 3497 7111 6680 1180 1426 4821 3322 3502 400 2951 4187 417 5234 1266 7525 7648 417 9297 174 670 1790 4121 9737 938 4616 3402 2009 2461 474 3728 327 3353 9773 6818 8781 874 2209 1260 1647 6126 9287 9638 5049 1256 4459 863 1835 4822 6809 348 5053 7784 6927 2140 9173 7817 5095 4184\r\n", "output": "4184 5299 7817 1259 2140 1126 7784 8796 348 7111 4822 1180 863 4821 1256 3502 9638 2951 6126 417 1260 1266 874 7648 6818 9297 3353 670 3728 4121 2461 938 3402 4616 2009 9737 474 1790 327 174 9773 417 8781 7525 2209 5234 1647 4187 9287 400 5049 3322 4459 1426 1835 6680 6809 3497 5053 5026 6927 2258 9173 5562 5095 8229"}]
false
stdio
null
true
764/B
764
B
PyPy 3
TESTS
10
530
19,046,400
111186240
n=int(input()) l = list(map(int, input().rstrip().split())) j=0 if n%2!=0: for i in range(n): if j==0: print(l[n-i-1],end=" ") j=1 else: print(l[i],end=" ") j=0 else: mi=n//2 for i in range(n): if i==mi-1 or i==mi: print(l[i],end=" ") j=0 elif j==0: print(l[n-i-1],end=" ") j=1 else: print(l[i],end=" ") j=0
34
140
19,148,800
24371900
n = int(input().strip()) a = input().strip().split() for i in range(n // 2): if i % 2 == 0: a[i], a[n - i - 1] = a[n - i - 1], a[i] print(' '.join(a))
Codeforces Round 395 (Div. 2)
CF
2,017
1
256
Timofey and cubes
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1. After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.
null
Consider the first sample. 1. At the begining row was [2, 3, 9, 6, 7, 1, 4]. 2. After first operation row was [4, 1, 7, 6, 9, 3, 2]. 3. After second operation row was [4, 3, 9, 6, 7, 1, 2]. 4. After third operation row was [4, 3, 7, 6, 9, 1, 2]. 5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
[{"input": "7\n4 3 7 6 9 1 2", "output": "2 3 9 6 7 1 4"}, {"input": "8\n6 1 4 2 5 6 9 2", "output": "2 1 6 2 5 4 9 6"}]
900
["constructive algorithms", "implementation"]
34
[{"input": "7\r\n4 3 7 6 9 1 2\r\n", "output": "2 3 9 6 7 1 4"}, {"input": "8\r\n6 1 4 2 5 6 9 2\r\n", "output": "2 1 6 2 5 4 9 6"}, {"input": "1\r\n1424\r\n", "output": "1424"}, {"input": "9\r\n-7 9 -4 9 -6 11 15 2 -10\r\n", "output": "-10 9 15 9 -6 11 -4 2 -7"}, {"input": "2\r\n21968 5686\r\n", "output": "5686 21968"}, {"input": "5\r\n241218936 -825949895 -84926813 491336344 -872198236\r\n", "output": "-872198236 -825949895 -84926813 491336344 241218936"}, {"input": "42\r\n-557774624 828320986 -345782722 -62979938 -681259411 -945983652 -139095040 832293378 -82572118 432027535 88438103 568183540 961782904 73543295 615958219 -5050584 322982437 -146046730 759453379 129267920 -819827396 -348156048 805080102 390723009 -771277251 -79011872 -592313207 528489973 656201270 -127795621 17284747 145139617 -565641608 83452176 -223074608 545811186 -657981923 -204657836 154779765 -476867246 180386291 202782486\r\n", "output": "202782486 828320986 -476867246 -62979938 -204657836 -945983652 545811186 832293378 83452176 432027535 145139617 568183540 -127795621 73543295 528489973 -5050584 -79011872 -146046730 390723009 129267920 -348156048 -819827396 805080102 759453379 -771277251 322982437 -592313207 615958219 656201270 961782904 17284747 88438103 -565641608 -82572118 -223074608 -139095040 -657981923 -681259411 154779765 -345782722 180386291 -557774624"}, {"input": "2\r\n1 2\r\n", "output": "2 1"}, {"input": "6\r\n1 2 3 4 5 6\r\n", "output": "6 2 4 3 5 1"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3 1 2 0"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10 2 8 4 6 5 7 3 9 1"}, {"input": "2\r\n4 5\r\n", "output": "5 4"}, {"input": "2\r\n1 5\r\n", "output": "5 1"}, {"input": "2\r\n2 5\r\n", "output": "5 2"}, {"input": "2\r\n3 4\r\n", "output": "4 3"}, {"input": "2\r\n10 0\r\n", "output": "0 10"}, {"input": "66\r\n8229 5299 5562 1259 2258 1126 5026 8796 3497 7111 6680 1180 1426 4821 3322 3502 400 2951 4187 417 5234 1266 7525 7648 417 9297 174 670 1790 4121 9737 938 4616 3402 2009 2461 474 3728 327 3353 9773 6818 8781 874 2209 1260 1647 6126 9287 9638 5049 1256 4459 863 1835 4822 6809 348 5053 7784 6927 2140 9173 7817 5095 4184\r\n", "output": "4184 5299 7817 1259 2140 1126 7784 8796 348 7111 4822 1180 863 4821 1256 3502 9638 2951 6126 417 1260 1266 874 7648 6818 9297 3353 670 3728 4121 2461 938 3402 4616 2009 9737 474 1790 327 174 9773 417 8781 7525 2209 5234 1647 4187 9287 400 5049 3322 4459 1426 1835 6680 6809 3497 5053 5026 6927 2258 9173 5562 5095 8229"}]
false
stdio
null
true
762/D
762
D
PyPy 3
TESTS
2
249
6,041,600
101630568
import sys from array import array # noqa: F401 import typing as Tp # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def output(*args): sys.stdout.buffer.write( ('\n'.join(map(str, args)) + '\n').encode('utf-8') ) def main(): n = int(input()) a = [list(map(float, input().split())) for _ in range(3)] dp = [[[-1e18] * 2 for _ in range(3)] for _ in range(n + 1)] dp[0][0][0] = 0.0 for i in range(n): for j in range(3): dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0] + a[j][i]) if j > 0: dp[i + 1][j - 1][0] = max(dp[i + 1][j - 1][0], dp[i][j][0] + a[j][i] + a[j - 1][i]) if j < 2: dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][0] + a[j][i] + a[j + 1][i]) dp[i + 1][0][1] = max( dp[i + 1][0][1], dp[i][0][1] + a[0][i] + a[1][i] + a[2][i], dp[i][0][0] + a[0][i] + a[1][i] + a[2][i] ) dp[i + 1][2][0] = max( dp[i + 1][2][0], dp[i][0][1] + a[0][i] + a[1][i] + a[2][i] ) dp[i + 1][2][1] = max( dp[i + 1][2][1], dp[i][2][1] + a[0][i] + a[1][i] + a[2][i], dp[i][2][0] + a[0][i] + a[1][i] + a[2][i] ) dp[i + 1][0][0] = max( dp[i + 1][0][0], dp[i][2][1] + a[0][i] + a[1][i] + a[2][i] ) print(round(dp[-1][2][0])) if __name__ == '__main__': main()
38
670
56,934,400
124777245
import sys input = sys.stdin.readline def solve(): M = ( (0, 3, 5, 5), (3, 1, 4, -1), (5, 4, 2, 5), (5, -1, 5, 5)) INF = int(9e18-1e9) n = int(input()) a = [list(map(int, input().split())) for i in range(3)] for i in range(3): a.append([None]*n) for i in range(n): a[3][i] = a[0][i]+a[1][i] a[4][i] = a[1][i]+a[2][i] a[5][i] = a[0][i]+a[4][i] dp = [[None]*(n+1) for i in range(4)] dp[0][0] = 0 dp[1][0] = -INF dp[2][0] = -INF dp[3][0] = -INF for i in range(n): for j in range(4): d = -INF for k in range(4): v = M[k][j] if v >= 0: d = max(d, dp[k][i] + a[v][i]) dp[j][i+1] = d print(dp[2][n]) solve()
Educational Codeforces Round 17
ICPC
2,017
1
256
Maximum path
You are given a rectangular table 3 × n. Each cell contains an integer. You can move from one cell to another if they share a side. Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.
The first line contains an integer n (1 ≤ n ≤ 105)  — the number of columns in the table. Next three lines contain n integers each  — the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 ≤ aij ≤ 109) of the table.
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
null
The path for the first example: The path for the second example:
[{"input": "3\n1 1 1\n1 -1 1\n1 1 1", "output": "7"}, {"input": "5\n10 10 10 -1 -1\n-1 10 10 10 10\n-1 10 10 10 10", "output": "110"}]
2,300
["dp", "greedy", "implementation"]
38
[{"input": "3\r\n1 1 1\r\n1 -1 1\r\n1 1 1\r\n", "output": "7\r\n"}, {"input": "5\r\n10 10 10 -1 -1\r\n-1 10 10 10 10\r\n-1 10 10 10 10\r\n", "output": "110\r\n"}, {"input": "15\r\n-87 -91 31 63 91 35 -14 51 20 20 -20 -94 -59 77 76\r\n11 81 22 -29 91 -26 -10 -12 46 10 100 88 14 64 41\r\n26 -31 99 -39 -30 30 28 74 -7 21 2 32 -60 -74 46\r\n", "output": "1152\r\n"}, {"input": "20\r\n16 82 25 21 -60 9 29 -55 70 54 -50 10 -19 40 46 41 31 -66 1 85\r\n-15 75 -94 -7 -50 -97 -55 -24 44 -69 -73 15 -9 98 92 -92 72 -32 -46 59\r\n74 99 -6 97 -59 41 -22 -8 -27 75 3 -56 -38 -56 -43 16 -43 -92 55 -63\r\n", "output": "946\r\n"}, {"input": "5\r\n150684603 -262756669 -629261226 393751321 700168705\r\n853551233 -595914191 -266257139 165068700 494943072\r\n328547487 63141018 -951406530 -212389249 -69164259\r\n", "output": "2218520550\r\n"}, {"input": "10\r\n687024557 -928074266 -409520915 770775361 240764400 108297300 -280070452 588179696 -920283246 736937716\r\n422602209 -940948979 -483879926 -525886137 -79749893 -958247281 844561102 553768085 269384580 -975129348\r\n-485518332 -130037110 493465239 494308146 958976404 706037260 154106757 -250914836 -915814064 -45677796\r\n", "output": "4721200012\r\n"}, {"input": "3\r\n-1 -1 -1\r\n-1 -1 -1\r\n-1 -1 -1\r\n", "output": "-5\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "3\r\n"}, {"input": "1\r\n1000000000\r\n1000000000\r\n1000000000\r\n", "output": "3000000000\r\n"}, {"input": "3\r\n1 1 1\r\n-1 1 1\r\n-1 1 1\r\n", "output": "7\r\n"}, {"input": "2\r\n-36 45\r\n28 -1\r\n2 -21\r\n", "output": "17\r\n"}, {"input": "4\r\n-2 -1 2 2\r\n-2 0 0 1\r\n1 -2 2 -1\r\n", "output": "3\r\n"}, {"input": "1\r\n-1\r\n0\r\n0\r\n", "output": "-1\r\n"}]
false
stdio
null
true
514/D
514
D
PyPy 3-64
TESTS
2
77
0
214230737
n, m, k = map(int,input().split()) p = [[] for i in range(m + 1)] for i in range(n): m1 = list(map(int,input().split())) for j in range(m): p[j] += [m1[j]] ans = [[0 for i in range(4 * n)]for i in range(m + 1)] #print(ans[0]) def build(l,r,v,i): global ans md = (l + r) // 2 if l == r: ans[i][v] = p[i][l - 1] return build(l,md,v * 2,i) build(md + 1,r,v * 2 + 1,i) ans[i][v] = max(ans[i][v * 2],ans[i][v * 2 + 1]) def get_max(l,r,l1,r1,v,i): md = (l + r) // 2 if l > r1 or r < l1:return 0 if l1 <= l and r <= r1: return ans[i][v] return max(get_max(l,md,l1,r1,v * 2,i),get_max(md + 1,r,l1,r1,v * 2 + 1,i)) for i in range(m): build(1,n,1,i) mx = 0 G_org = [] for i in range(1,n): l = i r = n + 1 G = [] while l + 1 < r: md = (l + r) // 2 G = [] for j in range(m): G += [get_max(1,n,i,md,1,j)] if sum(G) > k:r = md else:l = md # print(i, l,r,G,'!@!') G = [] for j in range(m): G += [get_max(1,n,i,l,1,j)] if l - i + 1 > mx and len(G) != 0 and sum(G) <= k: mx = l - i + 1 G_org = G print(*G_org)
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
514/C
514
C
PyPy 3-64
TESTS
5
62
1,536,000
215157994
def process_queries(initial_strings, queries): memory_set = set(initial_strings) for query in queries: for i in range(len(query)): for c in ['a', 'b', 'c']: modified_query = query[:i] + c + query[i + 1:] if modified_query in memory_set: print("YES") break else: continue break else: print("NO") n, m = map(int, input().split()) initial_strings = [input() for _ in range(n)] queries = [input() for _ in range(m)] process_queries(initial_strings, queries)
41
343
46,694,400
193030414
# Use two hashes to avoid collisions import sys input = sys.stdin.readline MAXN = 6 * 10 ** 5 MOD = 10 ** 9 + 7 base = [5, 7] # hash stuff pw = [[1] * MAXN for i in range(2)] for j in range(2): for i in range(1, MAXN): pw[j][i] = pw[j][i - 1] * base[j] % MOD def hash_s(s: str): hsh = [0, 0] for j in range(2): for i, c in enumerate(s): hsh[j] = (hsh[j] + (ord(c)-ord('a')+1) * pw[j][i]) % MOD return hsh def conv_hash(hsh): return hsh[0] + hsh[1] * MOD # Main solution procedure n, m = map(int, input().split()) possible_string_hashes = set() for _ in range(n): s = input().strip() base_hash = hash_s(s) for i in range(len(s)): sc = ord(s[i]) - ord('a') for c in range(3): if sc != c: new_hsh = base_hash.copy() for j in range(2): new_hsh[j] = (new_hsh[j] + (c-sc) * pw[j][i]) % MOD possible_string_hashes.add(conv_hash(new_hsh)) for query in range(m): s = input().strip() if conv_hash(hash_s(s)) in possible_string_hashes: print("YES") else: print("NO")
Codeforces Round 291 (Div. 2)
CF
2,015
3
256
Watto and Mechanism
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
null
null
[{"input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO"}]
2,000
["binary search", "data structures", "hashing", "string suffix structures", "strings"]
41
[{"input": "2 3\r\naaaaa\r\nacacaca\r\naabaa\r\nccacacc\r\ncaaac\r\n", "output": "YES\r\nNO\r\nNO\r\n"}, {"input": "1 5\r\nacbacbacb\r\ncbacbacb\r\nacbacbac\r\naacbacbacb\r\nacbacbacbb\r\nacbaabacb\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "0 0\r\n", "output": ""}, {"input": "5 4\r\nab\r\ncacab\r\ncbabc\r\nacc\r\ncacab\r\nabc\r\naa\r\nacbca\r\ncb\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "9 9\r\ncaccbcacabccba\r\naacbcbcaabacbcbcba\r\nbabccaaacccacbb\r\ncaaabcaacbababbabbb\r\nabbaccacabacaaaa\r\nbccbccababcaacb\r\ncaacbcaacbababbabbb\r\nbcacababbbcaaca\r\nccbbcbababbccaab\r\nbbcbccababcaacb\r\naacccbabbacbabacaca\r\nbbcbcccbabcaacb\r\nacbacacbcacc\r\ncaaabcaaabacabbabbb\r\nabbbabaaaba\r\naacccbcaabacbcbcba\r\nabbaccacabbcaaaa\r\naaccbbcabbacbcbcba\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\n"}, {"input": "1 1\r\nbbbbbbbaaaabbbbbaabbbba\r\naaabbbabbbbbbbaabbabbbb\r\n", "output": "NO\r\n"}]
false
stdio
null
true
514/C
514
C
PyPy 3-64
TESTS
5
62
2,457,600
215157319
st = set() n, m = list(map(int, input().split())) for i in range(n): word = input() for letter in range(len(word)): newWord = list(word) for pp in ['a', 'b', 'c']: newWord[letter] = pp st.add(''.join(newWord)) for i in range(m): if input() in st: print("YES") else: print("NO")
41
608
9,932,800
196187364
from sys import stdin,stdout a, b = map(int, input().split()) qw = set() def hash(st): h = 0 for l, i in enumerate(st): h += ord(i) * pow(1212121, l, 10**9 + 7) h %= (10**9 + 7) return h for i in range(a): qw.add(hash(stdin.readline().strip())) for i in range(b): er = stdin.readline().strip() t = hash(er) boo = 0 for l in range(len(er)): xx = pow(1212121, l, 10**9 + 7) if er[l] != 'a' and (t + xx*(97-ord(er[l]))) % (10**9 + 7) in qw: boo = 1 stdout.write('YES\n') break if er[l] != 'b' and (t + xx*(98-ord(er[l]))) % (10**9 + 7) in qw: boo = 1 stdout.write('YES\n') break if er[l] != 'c' and (t + xx*(99-ord(er[l]))) % (10**9 + 7) in qw: boo = 1 stdout.write('YES\n') break if boo == 0: stdout.write('NO\n')
Codeforces Round 291 (Div. 2)
CF
2,015
3
256
Watto and Mechanism
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
null
null
[{"input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO"}]
2,000
["binary search", "data structures", "hashing", "string suffix structures", "strings"]
41
[{"input": "2 3\r\naaaaa\r\nacacaca\r\naabaa\r\nccacacc\r\ncaaac\r\n", "output": "YES\r\nNO\r\nNO\r\n"}, {"input": "1 5\r\nacbacbacb\r\ncbacbacb\r\nacbacbac\r\naacbacbacb\r\nacbacbacbb\r\nacbaabacb\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "0 0\r\n", "output": ""}, {"input": "5 4\r\nab\r\ncacab\r\ncbabc\r\nacc\r\ncacab\r\nabc\r\naa\r\nacbca\r\ncb\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "9 9\r\ncaccbcacabccba\r\naacbcbcaabacbcbcba\r\nbabccaaacccacbb\r\ncaaabcaacbababbabbb\r\nabbaccacabacaaaa\r\nbccbccababcaacb\r\ncaacbcaacbababbabbb\r\nbcacababbbcaaca\r\nccbbcbababbccaab\r\nbbcbccababcaacb\r\naacccbabbacbabacaca\r\nbbcbcccbabcaacb\r\nacbacacbcacc\r\ncaaabcaaabacabbabbb\r\nabbbabaaaba\r\naacccbcaabacbcbcba\r\nabbaccacabbcaaaa\r\naaccbbcabbacbcbcba\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\n"}, {"input": "1 1\r\nbbbbbbbaaaabbbbbaabbbba\r\naaabbbabbbbbbbaabbabbbb\r\n", "output": "NO\r\n"}]
false
stdio
null
true
514/C
514
C
Python 3
TESTS
5
46
921,600
213050768
import sys input=sys.stdin.readline from hashlib import md5 def hash1(s): h = md5() h.update(s.encode()) hash = h.hexdigest() return hash n,m=map(int,input().split()) s={} for i in range(n): x=input().strip() for i in range(len(x)): r=(hash1((x[:i]+" "+x[i+1:]))) if r not in s: s[r]=set() s[r].add(x[i]) for i in range(m): x=input().strip() for i in range(len(x)): r=hash1((x[:i]+" "+x[i+1:])) if r in s and (len(s[r])>1 or x[i] not in s): print("YES") break else: print("NO")
41
2,964
34,611,200
202756254
mod = 9999999999999999 memory = set() n, m = map(int, input().split()) for _ in range(n): s = input() slen = len(s) pwr, h_val = 1, 0 for i in range(slen): h_val = (h_val + ord(s[i]) * pwr) % mod pwr = (pwr * 203) % mod pwr = 1 # reset for i in range(slen): for k in range(97, 100): if ord(s[i]) != k: to_add = (h_val + (k - ord(s[i])) * pwr) % mod memory.add(to_add) pwr = (pwr * 203) % mod res = [] for _ in range(m): query = input() pwr, h_val = 1, 0 for i in range(len(query)): h_val = (h_val + ord(query[i]) * pwr) % mod pwr = (pwr * 203) % mod if h_val in memory: res.append('YES') else: res.append('NO') print('\n'.join(res))
Codeforces Round 291 (Div. 2)
CF
2,015
3
256
Watto and Mechanism
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
null
null
[{"input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO"}]
2,000
["binary search", "data structures", "hashing", "string suffix structures", "strings"]
41
[{"input": "2 3\r\naaaaa\r\nacacaca\r\naabaa\r\nccacacc\r\ncaaac\r\n", "output": "YES\r\nNO\r\nNO\r\n"}, {"input": "1 5\r\nacbacbacb\r\ncbacbacb\r\nacbacbac\r\naacbacbacb\r\nacbacbacbb\r\nacbaabacb\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "0 0\r\n", "output": ""}, {"input": "5 4\r\nab\r\ncacab\r\ncbabc\r\nacc\r\ncacab\r\nabc\r\naa\r\nacbca\r\ncb\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "9 9\r\ncaccbcacabccba\r\naacbcbcaabacbcbcba\r\nbabccaaacccacbb\r\ncaaabcaacbababbabbb\r\nabbaccacabacaaaa\r\nbccbccababcaacb\r\ncaacbcaacbababbabbb\r\nbcacababbbcaaca\r\nccbbcbababbccaab\r\nbbcbccababcaacb\r\naacccbabbacbabacaca\r\nbbcbcccbabcaacb\r\nacbacacbcacc\r\ncaaabcaaabacabbabbb\r\nabbbabaaaba\r\naacccbcaabacbcbcba\r\nabbaccacabbcaaaa\r\naaccbbcabbacbcbcba\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\n"}, {"input": "1 1\r\nbbbbbbbaaaabbbbbaabbbba\r\naaabbbabbbbbbbaabbabbbb\r\n", "output": "NO\r\n"}]
false
stdio
null
true
514/C
514
C
PyPy 3-64
TESTS
5
62
2,662,400
192538724
def rl(): return list(map(int,input().split())) def ri(): return int(input()) def rs(): return input() def rm(): return map(int,input().split()) def solve(): d={'a':1,'b':2,'c':3} n,m=rm() v=set() for i in range(n): s=input() ls=len(s) h=0 for j in range(ls): h+=pow(4,ls-j-1)*d[s[j]] v.add(h) for i in range(m): s=input() ls=len(s) h=0 for j in range(ls): h+=pow(4,ls-j-1)*d[s[j]] p=False for j in range(ls): for k in range(1,4): if h+pow(4,ls-j-1)*(k-d[s[j]]) in v: print('YES'); p=True if not p: print('NO') solve()
41
343
46,694,400
193030414
# Use two hashes to avoid collisions import sys input = sys.stdin.readline MAXN = 6 * 10 ** 5 MOD = 10 ** 9 + 7 base = [5, 7] # hash stuff pw = [[1] * MAXN for i in range(2)] for j in range(2): for i in range(1, MAXN): pw[j][i] = pw[j][i - 1] * base[j] % MOD def hash_s(s: str): hsh = [0, 0] for j in range(2): for i, c in enumerate(s): hsh[j] = (hsh[j] + (ord(c)-ord('a')+1) * pw[j][i]) % MOD return hsh def conv_hash(hsh): return hsh[0] + hsh[1] * MOD # Main solution procedure n, m = map(int, input().split()) possible_string_hashes = set() for _ in range(n): s = input().strip() base_hash = hash_s(s) for i in range(len(s)): sc = ord(s[i]) - ord('a') for c in range(3): if sc != c: new_hsh = base_hash.copy() for j in range(2): new_hsh[j] = (new_hsh[j] + (c-sc) * pw[j][i]) % MOD possible_string_hashes.add(conv_hash(new_hsh)) for query in range(m): s = input().strip() if conv_hash(hash_s(s)) in possible_string_hashes: print("YES") else: print("NO")
Codeforces Round 291 (Div. 2)
CF
2,015
3
256
Watto and Mechanism
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
null
null
[{"input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO"}]
2,000
["binary search", "data structures", "hashing", "string suffix structures", "strings"]
41
[{"input": "2 3\r\naaaaa\r\nacacaca\r\naabaa\r\nccacacc\r\ncaaac\r\n", "output": "YES\r\nNO\r\nNO\r\n"}, {"input": "1 5\r\nacbacbacb\r\ncbacbacb\r\nacbacbac\r\naacbacbacb\r\nacbacbacbb\r\nacbaabacb\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "0 0\r\n", "output": ""}, {"input": "5 4\r\nab\r\ncacab\r\ncbabc\r\nacc\r\ncacab\r\nabc\r\naa\r\nacbca\r\ncb\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "9 9\r\ncaccbcacabccba\r\naacbcbcaabacbcbcba\r\nbabccaaacccacbb\r\ncaaabcaacbababbabbb\r\nabbaccacabacaaaa\r\nbccbccababcaacb\r\ncaacbcaacbababbabbb\r\nbcacababbbcaaca\r\nccbbcbababbccaab\r\nbbcbccababcaacb\r\naacccbabbacbabacaca\r\nbbcbcccbabcaacb\r\nacbacacbcacc\r\ncaaabcaaabacabbabbb\r\nabbbabaaaba\r\naacccbcaabacbcbcba\r\nabbaccacabbcaaaa\r\naaccbbcabbacbcbcba\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\n"}, {"input": "1 1\r\nbbbbbbbaaaabbbbbaabbbba\r\naaabbbabbbbbbbaabbabbbb\r\n", "output": "NO\r\n"}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
31
0
231331786
input_n = int(input()) posPar = [] i = 0 while i < input_n: row = input().split() temp = max(row) if temp in posPar: posPar.append(str(int(temp)+1))#somtimes my briliance generates gravity else: posPar.append(temp) i += 1 print(" ".join(posPar))
23
46
0
15652796
n = int(input()) l = [list(map(int, input().split())) for i in range(n)] p = [-1 for i in range(n)] for i in range(n): for j in range(1, n+1): if l[i].count(j) == n-j: p[i] = j break if p.count(n-1) == 2: p[p.index(n-1)] = n print(" ".join(map(str, p)))
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
39/F
39
F
Python 3
TESTS
3
248
409,600
53945568
from collections import defaultdict table = defaultdict(int) n,m,k = [int(x) for x in input().split()] frog = [int(x) for x in input().split()] mos = [int(x) for x in input().split()] for a in frog: for b in mos: if b % a == 0 : table[a] += 1 minV = 9999999 minItem = [] for key,value in table.items(): if value < minV: minV = value minItem = [] minItem.append(key) elif value == minV: minItem.append(key) minItem.sort() minIndex = [] for a in minItem: for i in range(m): if a == frog[i]: minIndex.append(i+1) # print(table) # print("minItem =",minIndex,"minV=",minV) print(len(minIndex)) for a in minIndex: print(a,end=" ")
35
124
0
15637261
n, m, k = map(int, input().split()) d, a = map(int, input().split()), list(map(int, input().split())) v = sorted((sum(ai % di == 0 for ai in a), i + 1) for i, di in enumerate(d)) v = sorted(vi[1] for vi in v if vi[0] == v[0][0]) print(len(v), ' '.join(map(str, v)), sep='\n')
School Team Contest 1 (Winter Computer School 2010/11)
ICPC
2,010
2
64
Pacifist frogs
Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much. One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to n and the number of a hill is equal to the distance in meters between it and the island. The distance between the n-th hill and the shore is also 1 meter. Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is d, the frog will jump from the island on the hill d, then — on the hill 2d, then 3d and so on until they get to the shore (i.e. find itself beyond the hill n). However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible.
The first line contains three integers n, m and k (1 ≤ n ≤ 109, 1 ≤ m, k ≤ 100) — the number of hills, frogs and mosquitoes respectively. The second line contains m integers di (1 ≤ di ≤ 109) — the lengths of the frogs’ jumps. The third line contains k integers — the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces.
In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to m in the order of the jump length given in the input data.
null
null
[{"input": "5 3 5\n2 3 4\n1 2 3 4 5", "output": "2\n2 3"}, {"input": "1000000000 2 3\n2 5\n999999995 999999998 999999996", "output": "1\n2"}]
1,300
["implementation"]
35
[{"input": "5 3 5\r\n2 3 4\r\n1 2 3 4 5\r\n", "output": "2\r\n2 3\r\n"}, {"input": "1000000000 2 3\r\n2 5\r\n999999995 999999998 999999996\r\n", "output": "1\r\n2\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "2 2 1\r\n2 1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "3 2 2\r\n2 4\r\n3 2\r\n", "output": "1\r\n2\r\n"}, {"input": "10 3 6\r\n5 2 8\r\n5 6 7 8 9 10\r\n", "output": "1\r\n3\r\n"}, {"input": "10 10 9\r\n10 9 8 7 6 5 4 3 2 1\r\n10 9 8 7 5 4 3 2 1\r\n", "output": "1\r\n5\r\n"}, {"input": "20 3 5\r\n2 3 5\r\n2 5 6 10 15\r\n", "output": "1\r\n2\r\n"}, {"input": "20 4 8\r\n1 2 3 4\r\n2 4 6 8 10 12 14 16 18 20\r\n", "output": "1\r\n3\r\n"}, {"input": "10 5 5\r\n1 5 3 5 1\r\n1 6 5 7 2\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "20 10 5\r\n1 12 6 11 9 21 15 16 8 9\r\n11 13 15 2 1\r\n", "output": "7\r\n2 3 5 6 8 9 10\r\n"}, {"input": "20 10 10\r\n9 8 21 8 7 2 13 17 20 18\r\n7 16 20 3 6 1 11 18 15 17\r\n", "output": "2\r\n3 7\r\n"}, {"input": "20 10 10\r\n6 17 14 12 13 15 6 14 16 17\r\n1 6 16 14 7 8 9 12 10 2\r\n", "output": "4\r\n2 5 6 10\r\n"}, {"input": "100 30 30\r\n25 34 81 32 96 79 36 21 53 15 51 69 78 99 60 2 80 37 61 70 32 31 31 6 7 38 95 70 81 39\r\n1 50 75 8 90 69 13 57 6 4 60 19 94 52 45 42 95 88 21 22 96 2 56 61 31 78 7 62 68 72\r\n", "output": "11\r\n3 6 9 11 14 17 18 20 26 28 29\r\n"}, {"input": "1000 19 27\r\n656 162 264 790 579 786 877 998 516 247 650 150 858 281 279 549 354 353 533\r\n349 411 1 248 22 649 726 382 423 832 172 864 17 658 840 572 564 287 800 919 500 575 461 40 1000 383 624\r\n", "output": "19\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\r\n"}]
false
stdio
null
true
723/D
723
D
PyPy 3-64
TESTS
4
46
0
193994890
import sys input = sys.stdin.readline n, m, k = map(int, input().split()) g = [list(input()[:-1]) for _ in range(n)] x = [[0]*m for i in range(n)] d = [] for i in range(n): for j in range(m): if g[i][j] == '*': x[i][j] = 1 elif x[i][j] == 0: q = [(i, j)] x[i][j] = 1 s = {(i, j)} ew = 0 while q: a, b = q.pop() if (a in [0, n-1]) or (b in [0, m-1]): ew = 1 for i1, i2 in [(i+1, j), (i-1, j), (i, j-1), (i, j+1)]: if 0 <= i1 < n and 0 <= i2 < m and g[i1][i2] == '.' and x[i1][i2] == 0: x[i1][i2] = 1 s.add((i1, i2)) q.append((i1, i2)) if ew == 0: d.append(s) d.sort(key=lambda x:len(x)) c = 0 for i in range(len(d)-k): c += len(d[i]) for i1, i2 in d[i]: g[i1][i2] = '*' print(c) for i in g: print(''.join(i))
26
62
1,740,800
21164880
import sys sys.setrecursionlimit(100000) def find(x,y,tag): if mplake[x][y] == tag or mp[x][y] == "*": return mplake[x][y] = tag lakes[tag] += 1 for i in [(1,0),(-1,0),(0,1),(0,-1)]: if 0 <= x+i[0] and x+i[0] < n and 0 <= y + i[1] and y+i[1] < m: find(x+i[0],y+i[1],tag) def fill(x,y,tag): global cnt if mplake[x][y] == tag: mplake[x][y] = -1 mp[x][y] = "*" cnt += 1 for i in [(1,0),(-1,0),(0,1),(0,-1)]: if 0 <= x+i[0] and x+i[0] < n and 0 <= y + i[1] and y+i[1] < m: fill(x+i[0],y+i[1],tag) n, m, k = [int(i) for i in input().split()] mp = [] mplake = [] for i in range(n): mp.append(list(input())) mplake.append([-1 for i in range(m)]) start = [(0,0)] lakes = [0] cnt = 0 for i in range(n): find(i,0,0) find(i,m-1,0) for i in range(m): find(0,i,0) find(n-1,i,0) for i in range(1,n-1): for j in range(1,m-1): if mp[i][j] == "." and mplake[i][j] == -1: start.append((i,j)) lakes.append(0) find(i,j,len(lakes)-1) r = sorted([(lakes[i],start[i]) for i in range(1,len(lakes))]) for i in range(len(r)-k): fill(r[i][1][0],r[i][1][1],mplake[r[i][1][0]][r[i][1][1]]) print(cnt) for i in range(n): print("".join(mp[i]))
Codeforces Round 375 (Div. 2)
CF
2,016
2
256
Lakes in Berland
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell. You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map. The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land). It is guaranteed that the map contain at least k lakes.
In the first line print the minimum number of cells which should be transformed from water to land. In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data.
null
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
[{"input": "5 4 1\n****\n*..*\n****\n**.*\n..**", "output": "1\n****\n*..*\n****\n****\n..**"}, {"input": "3 3 0\n***\n*.*\n***", "output": "1\n***\n***\n***"}]
1,600
["dfs and similar", "dsu", "graphs", "greedy", "implementation"]
26
[{"input": "5 4 1\r\n****\r\n*..*\r\n****\r\n**.*\r\n..**\r\n", "output": "1\r\n****\r\n*..*\r\n****\r\n****\r\n..**\r\n"}, {"input": "3 3 0\r\n***\r\n*.*\r\n***\r\n", "output": "1\r\n***\r\n***\r\n***\r\n"}, {"input": "3 5 1\r\n.**.*\r\n*.*.*\r\n***..\r\n", "output": "0\r\n.**.*\r\n*.*.*\r\n***..\r\n"}, {"input": "3 5 0\r\n.**.*\r\n*.*.*\r\n***..\r\n", "output": "1\r\n.**.*\r\n***.*\r\n***..\r\n"}, {"input": "3 50 7\r\n***.********.*********************.**********.****\r\n*...**..*.**.*.*.*.*.*.*.*..*.*.*.*.*.*.*.*.*.*..*\r\n****************.*.********.**********************\r\n", "output": "8\r\n***.********.*********************.**********.****\r\n*...**..****.***.*.*******..*******.*.*.*.*.*.*..*\r\n****************.*.********.**********************\r\n"}, {"input": "50 3 4\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n*.*\r\n***\r\n.**\r\n***\r\n..*\r\n***\r\n***\r\n*.*\r\n***\r\n*.*\r\n***\r\n***\r\n*.*\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\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": "8\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n.**\r\n***\r\n..*\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\r\n***\r\n"}, {"input": "1 1 0\r\n.\r\n", "output": "0\r\n.\r\n"}, {"input": "1 1 0\r\n*\r\n", "output": "0\r\n*\r\n"}]
false
stdio
import sys from sys import argv def main(): input_path = argv[1] output_path = argv[2] submission_path = argv[3] with open(input_path, 'r') as f: input_lines = f.read().splitlines() n, m, k = map(int, input_lines[0].split()) original_map = input_lines[1:n+1] with open(output_path, 'r') as f: ref_lines = f.read().splitlines() if not ref_lines: print(0) return ref_min = int(ref_lines[0]) with open(submission_path, 'r') as f: sub_lines = f.read().splitlines() if not sub_lines: print(0) return # Check first line try: sub_min = int(sub_lines[0]) except: print(0) return if sub_min != ref_min: print(0) return # Check map lines if len(sub_lines) -1 != n: print(0) return sub_map = sub_lines[1:n+1] for line in sub_map: if len(line) != m: print(0) return # Check original land preserved and count transformed transformed = 0 for i in range(n): orig_line = original_map[i] sub_line = sub_map[i] for j in range(m): oc = orig_line[j] sc = sub_line[j] if oc == '*' and sc != '*': print(0) return if oc == '.' and sc == '*': transformed += 1 if transformed != sub_min: print(0) return # Check number of lakes visited = [[False]*m for _ in range(n)] lakes = 0 dirs = [(-1,0), (1,0), (0,-1), (0,1)] def is_border(x, y): return x == 0 or x == n-1 or y ==0 or y == m-1 for i in range(n): for j in range(m): if sub_map[i][j] == '.' and not visited[i][j]: stack = [(i,j)] visited[i][j] = True has_border = False for x, y in stack: if is_border(x, y): has_border = True for dx, dy in dirs: nx, ny = x + dx, y + dy if 0 <= nx < n and 0 <= ny < m and sub_map[nx][ny] == '.' and not visited[nx][ny]: visited[nx][ny] = True stack.append((nx, ny)) if not has_border: lakes += 1 if lakes != k: print(0) return print(1) if __name__ == '__main__': main()
true
519/D
519
D
Python 3
TESTS
4
46
0
116490455
costss=list(map(int,input().split())) s=input() costs={} cost=0 ans=0 nums=[] for i in range(len(s)): cost+=costss[ord(s[i])-97] if s[i] not in costs: costs[s[i]]={} costs[s[i]][cost]=costs[s[i]].get(cost,0)+1 if costss[ord(s[i])-97]!=0: ans+=costs[s[i]].get(cost-costss[ord(s[i])-97],0) #if costs[s[i]][cost]*costs[s[i]].get(cost-costss[ord(s[i])-97],0)>0: # print(*[i,costs[s[i]]]) #print(costs) #print(costs) '''for i in range(len(costss)): if costss[i]==0: for each in costs[chr(i+97)]: ans+=max(0,(costs[chr(i+97)][each]-1)*(costs[chr(i+97)][each]-2))''' print(ans)
47
155
7,372,800
114712977
if __name__ == '__main__': X = [int(i) for i in input().split()] s = input() arr = [] unicode_a = ord('a') for i in range(26): arr.append({}) sm = 0 k = 0 for char in s: n = ord(char) - unicode_a k += arr[n].get(sm, 0) sm += X[n] arr[n][sm] = arr[n].get(sm, 0) + 1 print(k)
Codeforces Round 294 (Div. 2)
CF
2,015
2
256
A and B and Interesting Substrings
A and B are preparing themselves for programming contests. After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes. A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes). B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one). Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero. Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?
The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively. The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.
Print the answer to the problem.
null
In the first sample test strings satisfying the condition above are abca and bcab. In the second sample test strings satisfying the condition above are two occurences of aa.
[{"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab", "output": "2"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa", "output": "2"}]
1,800
["data structures", "dp", "two pointers"]
47
[{"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\nxabcab\r\n", "output": "2\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\naaa\r\n", "output": "2\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\naabbccdd\r\n", "output": "4\r\n"}, {"input": "-3 4 -4 -1 3 5 -5 -3 3 2 1 4 5 -3 -1 4 2 -2 1 -1 1 5 -4 0 -5 4\r\nolspxykjqr\r\n", "output": "0\r\n"}, {"input": "-2 -2 2 1 4 0 -2 4 5 4 -5 -5 2 1 1 -1 0 -5 -2 3 -2 4 5 2 3 -5\r\nqgzhbkitmqwttdyoyvcbxincwjryzknubpacsngorexaldfurondbednowemnnlphhboycfavsovisrmfaefusoobingjhsmrukx\r\n", "output": "7\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\nbb\r\n", "output": "1\r\n"}, {"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\r\na\r\n", "output": "0\r\n"}, {"input": "0 0 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\nhhhhhhhhhh\r\n", "output": "45\r\n"}]
false
stdio
null
true
514/C
514
C
Python 3
TESTS
5
46
921,600
213051372
# import sys # input=sys.stdin.readline from hashlib import sha512 def hash1(s): h = sha512() h.update(s.encode()) hash = h.hexdigest() return hash n,m=map(int,input().split()) s={} for i in range(n): x=input()#.rstrip() for i in range(len(x)): r=hash1((x[:i]+"-"+x[i+1:])) if r not in s: s[r]=set() s[r].add(x[i]) for j in range(m): x=input()#.rstrip() for i in range(len(x)): r=hash1((x[:i]+"-"+x[i+1:])) if (r in s) and ((len(s[r])>1) or (x[i] not in s)): print("YES") break else: print("NO")
41
608
9,932,800
196187364
from sys import stdin,stdout a, b = map(int, input().split()) qw = set() def hash(st): h = 0 for l, i in enumerate(st): h += ord(i) * pow(1212121, l, 10**9 + 7) h %= (10**9 + 7) return h for i in range(a): qw.add(hash(stdin.readline().strip())) for i in range(b): er = stdin.readline().strip() t = hash(er) boo = 0 for l in range(len(er)): xx = pow(1212121, l, 10**9 + 7) if er[l] != 'a' and (t + xx*(97-ord(er[l]))) % (10**9 + 7) in qw: boo = 1 stdout.write('YES\n') break if er[l] != 'b' and (t + xx*(98-ord(er[l]))) % (10**9 + 7) in qw: boo = 1 stdout.write('YES\n') break if er[l] != 'c' and (t + xx*(99-ord(er[l]))) % (10**9 + 7) in qw: boo = 1 stdout.write('YES\n') break if boo == 0: stdout.write('NO\n')
Codeforces Round 291 (Div. 2)
CF
2,015
3
256
Watto and Mechanism
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
null
null
[{"input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO"}]
2,000
["binary search", "data structures", "hashing", "string suffix structures", "strings"]
41
[{"input": "2 3\r\naaaaa\r\nacacaca\r\naabaa\r\nccacacc\r\ncaaac\r\n", "output": "YES\r\nNO\r\nNO\r\n"}, {"input": "1 5\r\nacbacbacb\r\ncbacbacb\r\nacbacbac\r\naacbacbacb\r\nacbacbacbb\r\nacbaabacb\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "0 0\r\n", "output": ""}, {"input": "5 4\r\nab\r\ncacab\r\ncbabc\r\nacc\r\ncacab\r\nabc\r\naa\r\nacbca\r\ncb\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "9 9\r\ncaccbcacabccba\r\naacbcbcaabacbcbcba\r\nbabccaaacccacbb\r\ncaaabcaacbababbabbb\r\nabbaccacabacaaaa\r\nbccbccababcaacb\r\ncaacbcaacbababbabbb\r\nbcacababbbcaaca\r\nccbbcbababbccaab\r\nbbcbccababcaacb\r\naacccbabbacbabacaca\r\nbbcbcccbabcaacb\r\nacbacacbcacc\r\ncaaabcaaabacabbabbb\r\nabbbabaaaba\r\naacccbcaabacbcbcba\r\nabbaccacabbcaaaa\r\naaccbbcabbacbcbcba\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\n"}, {"input": "1 1\r\nbbbbbbbaaaabbbbbaabbbba\r\naaabbbabbbbbbbaabbabbbb\r\n", "output": "NO\r\n"}]
false
stdio
null
true
514/C
514
C
PyPy 3-64
TESTS
15
1,450
25,190,400
197338304
# import sys # sys.stdout=open('C:\Program Files (x86)\Sublime Text 3\cp_setup\output.txt','w') # sys.stdin=open('C:\Program Files (x86)\Sublime Text 3\cp_setup\input.txt','r') import heapq import math import collections import bisect mod=10**9+7 def getpow(): p_pow=[[1]*600001 for i in range(2)] p=[31,29] for j in range(2): for i in range(1,600001): p_pow[j][i]=(p_pow[j][i-1]*p[j])%mod return p_pow def solve(n,data,p_pow): hased=set() for i in range(n): st=data[i] curr=0 for i1 in range(2): hash1=0 ind=0 for j in st: hash1=(hash1+(ord(j)-96)*p_pow[i1][ind])%mod ind+=1 curr+=hash1 hased.add(curr%mod) return hased def find(q,hased,p_pow): curr_hash=[0,0] n1=len(q) for j in range(2): for i in range(n1): curr_hash[j]=(curr_hash[j]+(ord(q[i])-96)*p_pow[j][i])%mod for i in range(n1): pos=ord(q[i])-96 for ind in range(1,4): if ind==pos: continue new_z=curr_hash.copy() for j in range(2): new_z[j]=(new_z[j]+(ind-pos)*p_pow[j][i])%mod if (new_z[0]+new_z[1])%mod in hased: return "YES" return "NO" # t=int(input()) # for _ in range(t): m,n=map(int,input().split(" ")) data=[] p_pow=getpow() for _ in range(m): data.append(input()) hashes=solve(m,data,p_pow) for _ in range(n): print(find(input(),hashes,p_pow)) # print(solve(n,m,data,que))
41
2,964
34,611,200
202756254
mod = 9999999999999999 memory = set() n, m = map(int, input().split()) for _ in range(n): s = input() slen = len(s) pwr, h_val = 1, 0 for i in range(slen): h_val = (h_val + ord(s[i]) * pwr) % mod pwr = (pwr * 203) % mod pwr = 1 # reset for i in range(slen): for k in range(97, 100): if ord(s[i]) != k: to_add = (h_val + (k - ord(s[i])) * pwr) % mod memory.add(to_add) pwr = (pwr * 203) % mod res = [] for _ in range(m): query = input() pwr, h_val = 1, 0 for i in range(len(query)): h_val = (h_val + ord(query[i]) * pwr) % mod pwr = (pwr * 203) % mod if h_val in memory: res.append('YES') else: res.append('NO') print('\n'.join(res))
Codeforces Round 291 (Div. 2)
CF
2,015
3
256
Watto and Mechanism
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
null
null
[{"input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO"}]
2,000
["binary search", "data structures", "hashing", "string suffix structures", "strings"]
41
[{"input": "2 3\r\naaaaa\r\nacacaca\r\naabaa\r\nccacacc\r\ncaaac\r\n", "output": "YES\r\nNO\r\nNO\r\n"}, {"input": "1 5\r\nacbacbacb\r\ncbacbacb\r\nacbacbac\r\naacbacbacb\r\nacbacbacbb\r\nacbaabacb\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "0 0\r\n", "output": ""}, {"input": "5 4\r\nab\r\ncacab\r\ncbabc\r\nacc\r\ncacab\r\nabc\r\naa\r\nacbca\r\ncb\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "9 9\r\ncaccbcacabccba\r\naacbcbcaabacbcbcba\r\nbabccaaacccacbb\r\ncaaabcaacbababbabbb\r\nabbaccacabacaaaa\r\nbccbccababcaacb\r\ncaacbcaacbababbabbb\r\nbcacababbbcaaca\r\nccbbcbababbccaab\r\nbbcbccababcaacb\r\naacccbabbacbabacaca\r\nbbcbcccbabcaacb\r\nacbacacbcacc\r\ncaaabcaaabacabbabbb\r\nabbbabaaaba\r\naacccbcaabacbcbcba\r\nabbaccacabbcaaaa\r\naaccbbcabbacbcbcba\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\n"}, {"input": "1 1\r\nbbbbbbbaaaabbbbbaabbbba\r\naaabbbabbbbbbbaabbabbbb\r\n", "output": "NO\r\n"}]
false
stdio
null
true
472/D
472
D
PyPy 3-64
TESTS
1
46
0
191555090
def is_distance_matrix(n, matrix): for k in range(n): for i in range(n): for j in range(n): if matrix[i][k] + matrix[k][j] < matrix[i][j]: return False return True n = int(input().strip()) matrix = [] for _ in range(n): matrix.append(list(map(int, input().strip().split()))) if is_distance_matrix(n, matrix): print("YES") else: print("NO")
47
545
57,036,800
177433089
from math import inf from collections import * import math, os, sys, heapq, bisect, random,threading from functools import lru_cache from itertools import * import sys def inp(): return sys.stdin.readline().rstrip("\r\n") def out(var): sys.stdout.write(str(var)) # for fast output, always take string def inpu(): return int(inp()) def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) # #include <ext/pb_ds/assoc_container.hpp> # #include <ext/pb_ds/tree_policy.hpp> # using namespace __gnu_pbds; # #define ll long long # #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> # #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> M,M1=1000000007,998244353 def find(i,par): if par[i]!=i: par[i] = find(par[i],par) return par[i] def union(i,j,parent,rank): if rank[i]>rank[j]: parent[j] = i elif rank[i]<rank[j]: parent[i]= j else: parent[j] = i rank[i] += 1 def main(): how_much_noob_I_am = 1 # how_much_noob_I_am = inpu() for _ in range(1,how_much_noob_I_am+1): n = int(input()) arr = [] for _ in range(n): arr.append(lis()) if n == 1: if arr[0][0] == 0: print("YES") else: print("NO") exit() for i in range(n): if arr[i][i] != 0: print("NO") exit() for j in range(i + 1, n): if arr[i][j] != arr[j][i] or arr[i][j] == 0: print("NO") exit() for i in range(n): r = int(i == 0) for j in range(n): if arr[i][j] < arr[i][r] and i != j: r = j for k in range(n): if abs(arr[i][k] - arr[r][k]) != arr[r][i]: print("NO") exit() print("YES") if __name__ == '__main__': # sys.setrecursio0nlimit(2*10**5+50) # threading.stack_size(10**8) # threading.Thread(target=main).start() main()
Codeforces Round 270
CF
2,014
2
256
Design Tutorial: Inverse the Problem
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph. Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.
If there exists such a tree, output "YES", otherwise output "NO".
null
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7. In the second example, it is impossible because d1, 1 should be 0, but it is 1. In the third example, it is impossible because d1, 2 should equal d2, 1.
[{"input": "3\n0 2 7\n2 0 9\n7 9 0", "output": "YES"}, {"input": "3\n1 2 7\n2 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 2 2\n7 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 1 1\n1 0 1\n1 1 0", "output": "NO"}, {"input": "2\n0 0\n0 0", "output": "NO"}]
1,900
["dfs and similar", "dsu", "shortest paths", "trees"]
47
[{"input": "3\r\n0 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 2\r\n7 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 1\r\n1 0 1\r\n1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1000000000\r\n1000000000 0\r\n", "output": "YES\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "YES\r\n"}, {"input": "4\r\n0 3 7 6\r\n3 0 4 9\r\n7 4 0 2\r\n6 9 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 2\r\n1 0 2\r\n2 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 7\r\n2 0 10\r\n7 10 0\r\n", "output": "NO\r\n"}]
false
stdio
null
true
1006/C
1006
C
Python 3
TESTS
12
187
17,612,800
121460148
def maxssumarray(List): n = len(List) if n==1: return 0 else: i=0 j=n-1 leftsum=List[0] rightsum=List[-1] maxsum=0 while i+1<j: if leftsum < rightsum: leftsum += List[i + 1] i += 1 if leftsum == rightsum: maxsum = leftsum else: rightsum += List[j - 1] j -= 1 if leftsum == rightsum: maxsum = leftsum else: if leftsum == rightsum: maxsum = leftsum return maxsum n=int(input()) array = list(map(int, input().rstrip().split())) print(maxssumarray(array))
27
108
17,510,400
219573449
import sys input = sys.stdin.readline n=int(input()) l=[int(i) for i in input().split(" ")] i,j,x,y=0,n-1,l[0],l[-1] ans=0 while(i!=j): if(x==y): ans=x if(x<=y): i+=1 x+=l[i] else: j-=1 y+=l[j] print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Three Parts of the Array
You are given an array $$$d_1, d_2, \dots, d_n$$$ consisting of $$$n$$$ integer numbers. Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible. More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then: $$$$$$sum_1 = \sum\limits_{1 \le i \le a}d_i,$$$$$$ $$$$$$sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,$$$$$$ $$$$$$sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.$$$$$$ The sum of an empty array is $$$0$$$. Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^9$$$) — the elements of the array $$$d$$$.
Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$).
null
In the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$. In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$. In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$.
[{"input": "5\n1 3 1 1 4", "output": "5"}, {"input": "5\n1 3 2 1 4", "output": "4"}, {"input": "3\n4 1 2", "output": "0"}]
1,200
["binary search", "data structures", "two pointers"]
27
[{"input": "5\r\n1 3 1 1 4\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 2 1 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 1 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 3 5 4 5\r\n", "output": "9\r\n"}]
false
stdio
null
true
509/C
509
C
PyPy 3
TESTS
3
77
0
230060801
from math import ceil def precalc(m, s): n9 = 0 while s > 9: n9 += 1 s -= 9 mx = "9" * n9 + str(s) # print(mx) if len(mx) == m: mx = mx elif len(mx) < m: mx = mx + "0" * (m - len(mx)) else: mx = "-1" if len(str(int(mx))) != m: mx = "-1" MX = mx mx = list(mx) mn = "" if mx[-1] != "0" and mx != "-1": mn = mx[::-1] else: idx = -1 for i in range(len(MX)-1,-1,-1): if int(MX[i]) > 0: idx = i break if idx != -1: mx[idx] = str(int(mx[idx])-1) # print(mx[idx]) mx[-1] = "1" mn = mx[::-1] if MX == "-1": return -1, -1 else:return "".join(mn), MX # print(precalc(3, 1)) # print(precalc(2, 10)) def starting_m_take(num): return ceil(num/9) SZ = int(input()) ARR = [] for _ in range(SZ): tp = int(input()) ARR.append(tp) start_m = starting_m_take(ARR[0]) ANS = [precalc(start_m, ARR[0])[0]] # print(ANS) # check here once for i in range(1, len(ARR)): if precalc(start_m, ARR[i])[0] > ANS[-1]: ANS.append(precalc(start_m, ARR[i])[0]) else: start_m += 1 ANS.append(precalc(start_m, ARR[i])[0]) for xp in ANS: print(xp)
21
233
2,560,000
230063199
n = int(input()) def get_sum(x): result = 0 while x > 0: d, m = divmod(x, 10) result += m x = d return result def dec(x): t = x mult = 1 while True: d, m = divmod(x, 10) if m != 0: return t + (10 - m) * mult x = d mult *= 10 def get_next(prev_x, prev_sum, sum): t = prev_x while True: while prev_sum > sum: prev_x = dec(prev_x) prev_sum = get_sum(prev_x) s = list(str(prev_x))[::-1] required = sum - prev_sum i = 0 while required: try: a = min(9 - (ord(s[i]) - ord('0')), required) s[i] = chr(ord(s[i]) + a) except IndexError: a = min(9, required) s += chr(a + ord('0')) required -= a i += 1 x = int("".join(reversed(s))) if x != t: return x prev_x = dec(prev_x) prev_sum = get_sum(prev_x) a = s = 0 for i in range(n): b = int(input()) a = get_next(a, s, b) print(a) s = b
Codeforces Round 289 (Div. 2, ACM ICPC Rules)
ICPC
2,015
2
256
Sums of Digits
Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi. Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence. It is guaranteed that such a sequence always exists.
The first line contains a single integer number n (1 ≤ n ≤ 300). Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.
Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi. If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.
null
null
[{"input": "3\n1\n2\n3", "output": "1\n2\n3"}, {"input": "3\n3\n2\n1", "output": "3\n11\n100"}]
2,000
["dp", "greedy", "implementation"]
21
[{"input": "3\r\n1\r\n2\r\n3\r\n", "output": "1\r\n2\r\n3\r\n"}, {"input": "3\r\n3\r\n2\r\n1\r\n", "output": "3\r\n11\r\n100\r\n"}, {"input": "10\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n1\r\n", "output": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n"}, {"input": "10\r\n8\r\n8\r\n5\r\n1\r\n2\r\n7\r\n3\r\n8\r\n9\r\n4\r\n", "output": "8\r\n17\r\n23\r\n100\r\n101\r\n106\r\n111\r\n116\r\n117\r\n121\r\n"}, {"input": "10\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n10\r\n100\r\n1000\r\n10000\r\n100000\r\n1000000\r\n10000000\r\n100000000\r\n1000000000\r\n"}, {"input": "100\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n1\r\n", "output": "1\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\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\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\n80\r\n81\r\n82\r\n83\r\n84\r\n85\r\n86\r\n87\r\n88\r\n89\r\n90\r\n91\r\n92\r\n93\r\n94\r\n95\r\n96\r\n97\r\n98\r\n99\r\n100\r\n"}, {"input": "100\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n", "output": "99\n189\n198\n279\n288\n297\n369\n378\n387\n396\n459\n468\n477\n486\n495\n549\n558\n567\n576\n585\n594\n639\n648\n657\n666\n675\n684\n693\n729\n738\n747\n756\n765\n774\n783\n792\n819\n828\n837\n846\n855\n864\n873\n882\n891\n909\n918\n927\n936\n945\n954\n963\n972\n981\n990\n1089\n1098\n1179\n1188\n1197\n1269\n1278\n1287\n1296\n1359\n1368\n1377\n1386\n1395\n1449\n1458\n1467\n1476\n1485\n1494\n1539\n1548\n1557\n1566\n1575\n1584\n1593\n1629\n1638\n1647\n1656\n1665\n1674\n1683\n1692\n1719\n1728\n1737\n1746\n1755\n1764\n1773\n1782\n1791\n1809\n"}, {"input": "1\r\n139\r\n", "output": "4999999999999999\r\n"}, {"input": "1\r\n6\r\n", "output": "6\r\n"}]
false
stdio
null
true
1006/C
1006
C
PyPy 3-64
TESTS
11
124
27,545,600
180164563
def main(): n = map(int, input().split()) nums = list(map(int, input().split())) right_sum = left_sum = 0 i, j = 0, len(nums)-1 ans = 0 while i<j: left_sum += nums[i] while i<j and right_sum < left_sum: right_sum += nums[j] j-=1 if left_sum == right_sum: ans = left_sum i+=1 print(ans) main() # import sys, threading # if __name__ == '__main__': # sys.setrecursionlimit(1 << 30) # threading.stack_size(1 << 27) # main_thread = threading.Thread(target=main) # main_thread.start() # main_thread.join()
27
109
16,998,400
218614420
n = int(input()) arr = [int(x) for x in input().split(" ")] soma1 = 0 soma2 = 0 l = 0 r = n-1 ans = 0 while l<=r: if soma1 < soma2: soma1+=arr[l] l += 1 else: soma2 += arr[r] r -= 1 if soma1 == soma2: ans = soma1 print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Three Parts of the Array
You are given an array $$$d_1, d_2, \dots, d_n$$$ consisting of $$$n$$$ integer numbers. Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible. More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then: $$$$$$sum_1 = \sum\limits_{1 \le i \le a}d_i,$$$$$$ $$$$$$sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,$$$$$$ $$$$$$sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.$$$$$$ The sum of an empty array is $$$0$$$. Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^9$$$) — the elements of the array $$$d$$$.
Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$).
null
In the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$. In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$. In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$.
[{"input": "5\n1 3 1 1 4", "output": "5"}, {"input": "5\n1 3 2 1 4", "output": "4"}, {"input": "3\n4 1 2", "output": "0"}]
1,200
["binary search", "data structures", "two pointers"]
27
[{"input": "5\r\n1 3 1 1 4\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 2 1 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 1 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 3 5 4 5\r\n", "output": "9\r\n"}]
false
stdio
null
true
51/A
51
A
PyPy 3
TESTS
2
124
17,715,200
161747579
x = set() t = int(input()) for _ in range(t): a,b = list(input()) c,d = list(input()) f= [a,b,c,d] minimum = min(f) idx = f.index(minimum) #print(minimum, idx) if idx == 0: f = [c,a,d,b] elif idx == 2: f = [d,c,b,a] elif idx == 3: f = [b,d,a,c] #print(f) #f.sort() g = ''.join(f) x.add(g) if _ < t-1: input() print(len(x))
20
92
0
229675774
def contains(cont: list, temp: str) -> bool: for i in range(4): if temp in cont: return True temp = f'{temp[2]}{temp[0]}{temp[3]}{temp[1]}' return False n = int(input()) cont = [] for i in range(n): temp = input() + input() if not contains(cont, temp): cont.append(temp) if i < n - 1: input() print(len(cont))
Codeforces Beta Round 48
CF
2,010
2
256
Cheaterius's Problem
Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino pairs with super glue to get squares 2 × 2 which are the Cheaterius' magic amulets! That's what one of Cheaterius's amulets looks like After a hard night Cheaterius made n amulets. Everyone of them represents a square 2 × 2, every quarter contains 1 to 6 dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters should be the same. It is forbidden to turn over the amulets. Write a program that by the given amulets will find the number of piles on Cheaterius' desk.
The first line contains an integer n (1 ≤ n ≤ 1000), where n is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line "**" is located.
Print the required number of piles.
null
null
[{"input": "4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13", "output": "1"}, {"input": "4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53", "output": "2"}]
1,300
["implementation"]
20
[{"input": "4\r\n31\r\n23\r\n**\r\n31\r\n23\r\n**\r\n13\r\n32\r\n**\r\n32\r\n13\r\n", "output": "1\r\n"}, {"input": "4\r\n51\r\n26\r\n**\r\n54\r\n35\r\n**\r\n25\r\n61\r\n**\r\n45\r\n53\r\n", "output": "2\r\n"}, {"input": "4\r\n56\r\n61\r\n**\r\n31\r\n31\r\n**\r\n33\r\n11\r\n**\r\n11\r\n33\r\n", "output": "2\r\n"}, {"input": "4\r\n36\r\n44\r\n**\r\n32\r\n46\r\n**\r\n66\r\n41\r\n**\r\n64\r\n34\r\n", "output": "3\r\n"}, {"input": "3\r\n63\r\n63\r\n**\r\n66\r\n33\r\n**\r\n36\r\n36\r\n", "output": "1\r\n"}, {"input": "3\r\n11\r\n54\r\n**\r\n42\r\n63\r\n**\r\n42\r\n63\r\n", "output": "2\r\n"}, {"input": "3\r\n14\r\n54\r\n**\r\n45\r\n41\r\n**\r\n12\r\n22\r\n", "output": "2\r\n"}, {"input": "7\r\n21\r\n33\r\n**\r\n33\r\n12\r\n**\r\n32\r\n31\r\n**\r\n21\r\n33\r\n**\r\n33\r\n12\r\n**\r\n32\r\n31\r\n**\r\n13\r\n23\r\n", "output": "1\r\n"}]
false
stdio
null
true
472/D
472
D
PyPy 3-64
TESTS
2
46
0
191544535
def is_distance_matrix(n, matrix): for i in range(n): for j in range(n): if i == j and matrix[i][j] != 0: return False for k in range(n): if matrix[i][j] == matrix[i][k] + matrix[k][j]: break elif k == n - 1: return False return True n = int(input().strip()) matrix = [] for _ in range(n): matrix.append(list(map(int, input().strip().split()))) if is_distance_matrix(n, matrix): print("YES") else: print("NO")
47
545
57,036,800
177433089
from math import inf from collections import * import math, os, sys, heapq, bisect, random,threading from functools import lru_cache from itertools import * import sys def inp(): return sys.stdin.readline().rstrip("\r\n") def out(var): sys.stdout.write(str(var)) # for fast output, always take string def inpu(): return int(inp()) def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) # #include <ext/pb_ds/assoc_container.hpp> # #include <ext/pb_ds/tree_policy.hpp> # using namespace __gnu_pbds; # #define ll long long # #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> # #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> M,M1=1000000007,998244353 def find(i,par): if par[i]!=i: par[i] = find(par[i],par) return par[i] def union(i,j,parent,rank): if rank[i]>rank[j]: parent[j] = i elif rank[i]<rank[j]: parent[i]= j else: parent[j] = i rank[i] += 1 def main(): how_much_noob_I_am = 1 # how_much_noob_I_am = inpu() for _ in range(1,how_much_noob_I_am+1): n = int(input()) arr = [] for _ in range(n): arr.append(lis()) if n == 1: if arr[0][0] == 0: print("YES") else: print("NO") exit() for i in range(n): if arr[i][i] != 0: print("NO") exit() for j in range(i + 1, n): if arr[i][j] != arr[j][i] or arr[i][j] == 0: print("NO") exit() for i in range(n): r = int(i == 0) for j in range(n): if arr[i][j] < arr[i][r] and i != j: r = j for k in range(n): if abs(arr[i][k] - arr[r][k]) != arr[r][i]: print("NO") exit() print("YES") if __name__ == '__main__': # sys.setrecursio0nlimit(2*10**5+50) # threading.stack_size(10**8) # threading.Thread(target=main).start() main()
Codeforces Round 270
CF
2,014
2
256
Design Tutorial: Inverse the Problem
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph. Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.
If there exists such a tree, output "YES", otherwise output "NO".
null
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7. In the second example, it is impossible because d1, 1 should be 0, but it is 1. In the third example, it is impossible because d1, 2 should equal d2, 1.
[{"input": "3\n0 2 7\n2 0 9\n7 9 0", "output": "YES"}, {"input": "3\n1 2 7\n2 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 2 2\n7 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 1 1\n1 0 1\n1 1 0", "output": "NO"}, {"input": "2\n0 0\n0 0", "output": "NO"}]
1,900
["dfs and similar", "dsu", "shortest paths", "trees"]
47
[{"input": "3\r\n0 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 2\r\n7 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 1\r\n1 0 1\r\n1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1000000000\r\n1000000000 0\r\n", "output": "YES\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "YES\r\n"}, {"input": "4\r\n0 3 7 6\r\n3 0 4 9\r\n7 4 0 2\r\n6 9 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 2\r\n1 0 2\r\n2 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 7\r\n2 0 10\r\n7 10 0\r\n", "output": "NO\r\n"}]
false
stdio
null
true
870/C
870
C
Python 3
TESTS
5
46
4,505,600
157525048
def find_summand(n): if (n < 0): return -2 if (n % 4 == 0): return n // 4 if (n % 4 == 1): return find_summand(n-9) + 1 if (n % 4 == 2): return find_summand(n-6) + 1 return find_summand(n-15) + 1 q = int(input()) for tc in range(q): n = int(input()) print(find_summand(n))
20
140
11,776,000
165486592
import sys input = sys.stdin.readline q = int(input()) while(q): n = int(input()) if(n % 2 == 0): if(n // 4 == 0): print(-1) else: print(n // 4) elif(n % 4 == 1): if((n // 4 - 1) <= 0): print(-1) else: print(n // 4 - 1) else: if(n < 15): print(-1) else: print((n - 15) // 4 + 2) q -= 1
Technocup 2018 - Elimination Round 2
CF
2,017
2
256
Maximum splitting
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
null
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
[{"input": "1\n12", "output": "3"}, {"input": "2\n6\n8", "output": "1\n2"}, {"input": "3\n1\n2\n3", "output": "-1\n-1\n-1"}]
1,300
["dp", "greedy", "math", "number theory"]
20
[{"input": "1\r\n12\r\n", "output": "3\r\n"}, {"input": "2\r\n6\r\n8\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n1\r\n2\r\n3\r\n", "output": "-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n1\r\n2\r\n3\r\n5\r\n7\r\n11\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n4\r\n6\r\n9\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "20\r\n8\r\n13\r\n20\r\n12\r\n9\r\n16\r\n4\r\n19\r\n7\r\n15\r\n10\r\n6\r\n14\r\n11\r\n3\r\n2\r\n5\r\n17\r\n18\r\n1\r\n", "output": "2\r\n2\r\n5\r\n3\r\n1\r\n4\r\n1\r\n3\r\n-1\r\n2\r\n2\r\n1\r\n3\r\n-1\r\n-1\r\n-1\r\n-1\r\n3\r\n4\r\n-1\r\n"}, {"input": "100\r\n611\r\n513\r\n544\r\n463\r\n38\r\n778\r\n347\r\n317\r\n848\r\n664\r\n382\r\n108\r\n718\r\n33\r\n334\r\n876\r\n234\r\n22\r\n944\r\n305\r\n159\r\n245\r\n513\r\n691\r\n639\r\n135\r\n308\r\n324\r\n813\r\n459\r\n304\r\n116\r\n331\r\n993\r\n184\r\n224\r\n853\r\n769\r\n121\r\n687\r\n93\r\n930\r\n751\r\n308\r\n485\r\n914\r\n400\r\n695\r\n95\r\n981\r\n175\r\n972\r\n121\r\n654\r\n242\r\n610\r\n617\r\n999\r\n237\r\n548\r\n742\r\n767\r\n613\r\n172\r\n223\r\n391\r\n102\r\n907\r\n673\r\n116\r\n230\r\n355\r\n189\r\n552\r\n399\r\n493\r\n903\r\n201\r\n985\r\n459\r\n776\r\n641\r\n693\r\n919\r\n253\r\n540\r\n427\r\n394\r\n655\r\n101\r\n461\r\n854\r\n417\r\n249\r\n66\r\n380\r\n213\r\n906\r\n212\r\n528\r\n", "output": "151\r\n127\r\n136\r\n114\r\n9\r\n194\r\n85\r\n78\r\n212\r\n166\r\n95\r\n27\r\n179\r\n7\r\n83\r\n219\r\n58\r\n5\r\n236\r\n75\r\n38\r\n60\r\n127\r\n171\r\n158\r\n32\r\n77\r\n81\r\n202\r\n113\r\n76\r\n29\r\n81\r\n247\r\n46\r\n56\r\n212\r\n191\r\n29\r\n170\r\n22\r\n232\r\n186\r\n77\r\n120\r\n228\r\n100\r\n172\r\n22\r\n244\r\n42\r\n243\r\n29\r\n163\r\n60\r\n152\r\n153\r\n248\r\n58\r\n137\r\n185\r\n190\r\n152\r\n43\r\n54\r\n96\r\n25\r\n225\r\n167\r\n29\r\n57\r\n87\r\n46\r\n138\r\n98\r\n122\r\n224\r\n49\r\n245\r\n113\r\n194\r\n159\r\n172\r\n228\r\n62\r\n135\r\n105\r\n98\r\n162\r\n24\r\n114\r\n213\r\n103\r\n61\r\n16\r\n95\r\n52\r\n226\r\n53\r\n132\r\n"}, {"input": "1\r\n10000001\r\n", "output": "2499999\r\n"}]
false
stdio
null
true
388/C
388
C
PyPy 3
TESTS
2
140
307,200
92523687
from sys import stdin from collections import deque import heapq n = int(stdin.readline()) piles = [] for x in range(n): a = [int(x) for x in stdin.readline().split()][1:] piles.append(a) cielTotal = 0 jiroTotal = 0 mids = [] for x in piles: cielTotal += sum(x[:len(x)//2]) jiroTotal += sum(x[len(x)//2+len(x)%2:]) if len(x)%2 == 1: mids.append(x[len(x)//2]) mids.sort() turn = True for x in mids: if turn: cielTotal += x else: jiroTotal += x print(cielTotal,jiroTotal)
43
124
204,800
66072317
n = int(input()) a,b = 0,0 l = [] for _ in range(n): inpt = list(map(int,input().split()))[1:] li = len(inpt) if li%2: l.append(inpt[li//2]) a += sum((inpt[:li//2])) b += sum((inpt[(li + 1)//2:])) l.sort(reverse=True) a += sum(l[::2]) b += sum(l[1::2]) print(a, b)
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
1006/C
1006
C
Python 3
TESTS
16
249
24,678,400
80307529
n = int(input()) a = [int(x) for x in input().split()] i = 0 j = n-1 p = 0 q = 0 s = -1 while(i<j): p = p + a[i] q = q + a[j] while(p < q): i+=1 p = p + a[i] while(q < p): j = j-1 q = q+a[j] if p == q and p > s: s = p i+=1 j-=1 print(max(s,0))
27
109
21,094,400
181264733
n=int(input()) d=[int(i) for i in input().split()] if n==1: print(0) else : sum1=sum3=0 i1=0 i2=n-1 temp1=d[i1] temp3=d[i2] while i1<i2: if temp1==temp3: sum1=temp1 sum3=temp3 i1+=1 i2-=1 temp1+=(d[i1]) temp3+=(d[i2]) else : if temp1>temp3: i2-=1 temp3+=(d[i2]) else : i1+=1 temp1+=(d[i1]) print(sum1)
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Three Parts of the Array
You are given an array $$$d_1, d_2, \dots, d_n$$$ consisting of $$$n$$$ integer numbers. Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible. More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then: $$$$$$sum_1 = \sum\limits_{1 \le i \le a}d_i,$$$$$$ $$$$$$sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,$$$$$$ $$$$$$sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.$$$$$$ The sum of an empty array is $$$0$$$. Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^9$$$) — the elements of the array $$$d$$$.
Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$).
null
In the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$. In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$. In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$.
[{"input": "5\n1 3 1 1 4", "output": "5"}, {"input": "5\n1 3 2 1 4", "output": "4"}, {"input": "3\n4 1 2", "output": "0"}]
1,200
["binary search", "data structures", "two pointers"]
27
[{"input": "5\r\n1 3 1 1 4\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 2 1 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 1 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 3 5 4 5\r\n", "output": "9\r\n"}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
62
0
16270713
n=int(input()) a=[0 for i in range(51)] b=[0 for i in range(51)] for i in range(n): c=input().split() m=max(c) m=int(m) while a[m]!=0: m+=1 a[m]=1 b[i]=m for i in range(n): print(b[i])
23
46
0
15653804
n = int(input()) M = [[int(x) for x in input().split()] for i in range(n)] last = False perm = [0 for i in range(n)] for i in range(n): S = set() for x in M[i]: if x in S: perm[i] = x break else: S.add(x) else: if last: perm[i] = n-1 else: perm[i] = n last = True for x in perm: print(x, end = ' ')
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
870/C
870
C
Python 3
TESTS
5
61
0
31987175
def check_prime(q): i = 2 while i * i <= q: if q % i == 0: return False i += 1 return True def solve(n): if n < 16: return first[n] else: answer = 1 answer += n // 4 n -= (n // 4) * 4 while check_prime(n): answer -= 1 n += 4 return answer first = [-1, -1, -1, -1, 1, -1, 1, -1, 2, 1, 2, -1, 3, 2, 2, 2] req = int(input()) for _ in range(req): print(solve(int(input())))
20
155
12,185,600
75787650
import os, sys, atexit if sys.version_info[0] < 3: range = xrange from cStringIO import StringIO as BytesIO sys.stdout = BytesIO() else: from io import BytesIO sys.stdout = BytesIO() _write = sys.stdout.write sys.stdout.write = lambda s: _write(s.encode()) atexit.register(lambda: os.write(1, sys.stdout.getvalue())) input = BytesIO(os.read(0, os.fstat(0).st_size)).readline from sys import stdout, stdin out, q = [], int(input()) arr = [int(input()) for _ in range(q)] for n in arr: if n in [1, 2, 3, 5, 7, 11]: out.append('-1') else: ans = 0 if n & 1: ans += 1 n -= 9 ans += n // 4 out.append(str(ans)) stdout.write('\n'.join(out))
Technocup 2018 - Elimination Round 2
CF
2,017
2
256
Maximum splitting
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
null
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
[{"input": "1\n12", "output": "3"}, {"input": "2\n6\n8", "output": "1\n2"}, {"input": "3\n1\n2\n3", "output": "-1\n-1\n-1"}]
1,300
["dp", "greedy", "math", "number theory"]
20
[{"input": "1\r\n12\r\n", "output": "3\r\n"}, {"input": "2\r\n6\r\n8\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n1\r\n2\r\n3\r\n", "output": "-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n1\r\n2\r\n3\r\n5\r\n7\r\n11\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n4\r\n6\r\n9\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "20\r\n8\r\n13\r\n20\r\n12\r\n9\r\n16\r\n4\r\n19\r\n7\r\n15\r\n10\r\n6\r\n14\r\n11\r\n3\r\n2\r\n5\r\n17\r\n18\r\n1\r\n", "output": "2\r\n2\r\n5\r\n3\r\n1\r\n4\r\n1\r\n3\r\n-1\r\n2\r\n2\r\n1\r\n3\r\n-1\r\n-1\r\n-1\r\n-1\r\n3\r\n4\r\n-1\r\n"}, {"input": "100\r\n611\r\n513\r\n544\r\n463\r\n38\r\n778\r\n347\r\n317\r\n848\r\n664\r\n382\r\n108\r\n718\r\n33\r\n334\r\n876\r\n234\r\n22\r\n944\r\n305\r\n159\r\n245\r\n513\r\n691\r\n639\r\n135\r\n308\r\n324\r\n813\r\n459\r\n304\r\n116\r\n331\r\n993\r\n184\r\n224\r\n853\r\n769\r\n121\r\n687\r\n93\r\n930\r\n751\r\n308\r\n485\r\n914\r\n400\r\n695\r\n95\r\n981\r\n175\r\n972\r\n121\r\n654\r\n242\r\n610\r\n617\r\n999\r\n237\r\n548\r\n742\r\n767\r\n613\r\n172\r\n223\r\n391\r\n102\r\n907\r\n673\r\n116\r\n230\r\n355\r\n189\r\n552\r\n399\r\n493\r\n903\r\n201\r\n985\r\n459\r\n776\r\n641\r\n693\r\n919\r\n253\r\n540\r\n427\r\n394\r\n655\r\n101\r\n461\r\n854\r\n417\r\n249\r\n66\r\n380\r\n213\r\n906\r\n212\r\n528\r\n", "output": "151\r\n127\r\n136\r\n114\r\n9\r\n194\r\n85\r\n78\r\n212\r\n166\r\n95\r\n27\r\n179\r\n7\r\n83\r\n219\r\n58\r\n5\r\n236\r\n75\r\n38\r\n60\r\n127\r\n171\r\n158\r\n32\r\n77\r\n81\r\n202\r\n113\r\n76\r\n29\r\n81\r\n247\r\n46\r\n56\r\n212\r\n191\r\n29\r\n170\r\n22\r\n232\r\n186\r\n77\r\n120\r\n228\r\n100\r\n172\r\n22\r\n244\r\n42\r\n243\r\n29\r\n163\r\n60\r\n152\r\n153\r\n248\r\n58\r\n137\r\n185\r\n190\r\n152\r\n43\r\n54\r\n96\r\n25\r\n225\r\n167\r\n29\r\n57\r\n87\r\n46\r\n138\r\n98\r\n122\r\n224\r\n49\r\n245\r\n113\r\n194\r\n159\r\n172\r\n228\r\n62\r\n135\r\n105\r\n98\r\n162\r\n24\r\n114\r\n213\r\n103\r\n61\r\n16\r\n95\r\n52\r\n226\r\n53\r\n132\r\n"}, {"input": "1\r\n10000001\r\n", "output": "2499999\r\n"}]
false
stdio
null
true
870/C
870
C
Python 3
TESTS
5
31
0
191083090
def find(n): if n == 5 or n == 7 or n == 11: return -1 if n < 4: return -1 rem = n % 4 div = n // 4 if rem == 0: return div elif rem == 1: n = n - 9 if n < 0: return 1 else: return 1 + (n // 4) elif rem == 2: n = n - 6 if n < 0: return 1 return 1 + (n // 4) else: n = n - 15 if n < 0: return 1 return 1 + (n // 4) for _ in range(int(input())): n = int(input()) print(find(n))
20
264
10,342,400
158310761
from sys import stdin,stdout input = stdin.read r = map(int,input().split()) q = next(r) for _ in range(q): a = next(r) if(a < 4 or a == 5 or a == 7 or a == 11): stdout.write("-1\n") else: stdout.write(str(a//4 - (a%4)%2)) stdout.write("\n")
Technocup 2018 - Elimination Round 2
CF
2,017
2
256
Maximum splitting
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
null
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
[{"input": "1\n12", "output": "3"}, {"input": "2\n6\n8", "output": "1\n2"}, {"input": "3\n1\n2\n3", "output": "-1\n-1\n-1"}]
1,300
["dp", "greedy", "math", "number theory"]
20
[{"input": "1\r\n12\r\n", "output": "3\r\n"}, {"input": "2\r\n6\r\n8\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n1\r\n2\r\n3\r\n", "output": "-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n1\r\n2\r\n3\r\n5\r\n7\r\n11\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n4\r\n6\r\n9\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "20\r\n8\r\n13\r\n20\r\n12\r\n9\r\n16\r\n4\r\n19\r\n7\r\n15\r\n10\r\n6\r\n14\r\n11\r\n3\r\n2\r\n5\r\n17\r\n18\r\n1\r\n", "output": "2\r\n2\r\n5\r\n3\r\n1\r\n4\r\n1\r\n3\r\n-1\r\n2\r\n2\r\n1\r\n3\r\n-1\r\n-1\r\n-1\r\n-1\r\n3\r\n4\r\n-1\r\n"}, {"input": "100\r\n611\r\n513\r\n544\r\n463\r\n38\r\n778\r\n347\r\n317\r\n848\r\n664\r\n382\r\n108\r\n718\r\n33\r\n334\r\n876\r\n234\r\n22\r\n944\r\n305\r\n159\r\n245\r\n513\r\n691\r\n639\r\n135\r\n308\r\n324\r\n813\r\n459\r\n304\r\n116\r\n331\r\n993\r\n184\r\n224\r\n853\r\n769\r\n121\r\n687\r\n93\r\n930\r\n751\r\n308\r\n485\r\n914\r\n400\r\n695\r\n95\r\n981\r\n175\r\n972\r\n121\r\n654\r\n242\r\n610\r\n617\r\n999\r\n237\r\n548\r\n742\r\n767\r\n613\r\n172\r\n223\r\n391\r\n102\r\n907\r\n673\r\n116\r\n230\r\n355\r\n189\r\n552\r\n399\r\n493\r\n903\r\n201\r\n985\r\n459\r\n776\r\n641\r\n693\r\n919\r\n253\r\n540\r\n427\r\n394\r\n655\r\n101\r\n461\r\n854\r\n417\r\n249\r\n66\r\n380\r\n213\r\n906\r\n212\r\n528\r\n", "output": "151\r\n127\r\n136\r\n114\r\n9\r\n194\r\n85\r\n78\r\n212\r\n166\r\n95\r\n27\r\n179\r\n7\r\n83\r\n219\r\n58\r\n5\r\n236\r\n75\r\n38\r\n60\r\n127\r\n171\r\n158\r\n32\r\n77\r\n81\r\n202\r\n113\r\n76\r\n29\r\n81\r\n247\r\n46\r\n56\r\n212\r\n191\r\n29\r\n170\r\n22\r\n232\r\n186\r\n77\r\n120\r\n228\r\n100\r\n172\r\n22\r\n244\r\n42\r\n243\r\n29\r\n163\r\n60\r\n152\r\n153\r\n248\r\n58\r\n137\r\n185\r\n190\r\n152\r\n43\r\n54\r\n96\r\n25\r\n225\r\n167\r\n29\r\n57\r\n87\r\n46\r\n138\r\n98\r\n122\r\n224\r\n49\r\n245\r\n113\r\n194\r\n159\r\n172\r\n228\r\n62\r\n135\r\n105\r\n98\r\n162\r\n24\r\n114\r\n213\r\n103\r\n61\r\n16\r\n95\r\n52\r\n226\r\n53\r\n132\r\n"}, {"input": "1\r\n10000001\r\n", "output": "2499999\r\n"}]
false
stdio
null
true
755/B
755
B
Python 3
TESTS
10
187
1,433,600
139017847
n, m = map(int, input().split()) polland = [] enemy = [] for c in range(n): palavra = input() polland .append(palavra) for c in range(m): palavra = input() enemy.append(palavra) faladas = [] vez = True aux1 = aux2 = 0 p = 0 e = 0 while(aux1 < len(polland ) or aux2 < len(enemy)): if(vez): if(aux1 < len(polland)): if(polland[aux1] not in faladas): p += 1 faladas.append(polland[aux1]) aux1 += 1 vez = False else: vez = False else: if(aux2 < len(enemy)): if(enemy[aux2] not in faladas): e += 1 faladas.append(enemy[aux2]) vez = True aux2 += 1 else: vez = True if(p > e): print("YES") else: print("NO")
33
46
1,228,800
165729996
n, m = [int(num) for num in input().split()] palavras = {} num_repetidas = 0 for _ in range(n + m): palavra = input() if palavra in palavras: num_repetidas += 1 palavras[palavra] = True answer = '' if n > m: answer = 'YES' elif n < m: answer = 'NO' else: answer = 'NO' if num_repetidas % 2 == 0 else 'YES' print(answer)
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
29/A
29
A
Python 3
TESTS
4
186
0
59110844
n=int(input()) l=[] for i in range(n): v=list(map(int,input().split())) l.append((v[0],v[1])) d=-1 for i in range(n): for j in range(i+1,n): c=abs(l[i][0]-l[j][0]) if c==l[i][1]: if c+l[j][1]==0: d=0 break if d==0: break if d==0: print('YES') else: print('NO')
30
92
0
143462946
def check(cam1, cam2): if cam1[0] + cam1[1] == cam2[0] and cam2[0] + cam2[1] == cam1[0]: return True return False camels = [[int(y) for y in input().split()] for x in range(int(input()))] for x in range(len(camels) - 1): for y in range(x + 1, len(camels)): if check(camels[x], camels[y]): print('YES') exit() print('NO')
Codeforces Beta Round 29 (Div. 2, Codeforces format)
CF
2,010
2
256
Spit Problem
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task. The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
null
null
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
1,000
["brute force"]
30
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
false
stdio
null
true
756/B
756
B
PyPy 3-64
TESTS
5
62
1,638,400
210711984
import sys input = lambda: sys.stdin.readline().rstrip() from bisect import * N = int(input()) dp = [[0,0,0] for _ in range(N+1)] A = [] for i in range(N): A.append(int(input())) dp[i+1][0] = min(dp[i])+20 idx = bisect_left(A, A[-1]-89) #print(idx,A[-1],A[-1]-90) dp[i+1][1] = dp[idx][0]+50 idx = bisect_left(A, A[-1]-1439) dp[i+1][2] = dp[idx][0]+120 for i in range(1,N+1): print(min(dp[i])-min(dp[i-1]))
31
234
17,510,400
205217293
# Code by B3D # Love from math import * from collections import * import io, os import sys from bisect import * from heapq import * from itertools import permutations from functools import * import re import sys import threading from typing import * # from sortedcontainers import * # from sortedcontainerstemp import * # from temps import * MOD = 10 ** 9 + 7 # sys.setrecursionlimit(10**6) def subinp(): sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") def subinp_1(): sys.stdin = open("input.txt", "r") sys.stdout = open("op2.txt", "w") """ pow2 = [1] # print(log2(10 ** 9)) for i in range(29): pow2.append(pow2[-1] * 2) """ # input = sys.stdin.readline class Point: def __init__(self, x, l1): self.x = x self.l1 = l1 def __lt__(self, b): return self.l1[self.x] < self.l1[b.x] def getval(self): return self.x inp = lambda: int(input()) strin = lambda: input().strip() strl = lambda: list(input().rstrip("\r\n")) strlst = lambda: list(map(str, input().split())) mult = lambda: map(int,input().strip().split()) muls = lambda: map(str,input().strip().split()) mulf = lambda: map(float,input().strip().split()) lstin = lambda: list(map(int,input().strip().split())) flush = lambda: stdout.flush() stdpr = lambda x: stdout.write(str(x)) # @lru_cache(maxsize = 128) # print(pref) # ac = [chr(i) for i in range(ord('a'), ord('z') + 1)] # Power comes in response to a need, not a desire. # n = int(input()) # n, k = map(int, input().split()) # s = input() # l = list(map(int, input().split())) # dp = [[-1 for i in range(n + 1)] for j in range(2)] input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # spf = sakurajima(10 ** 6 + 7) def binpow(a, b, m): a %= m x = 1 while b > 0: if b & 1: x = x * a % m a = a * a % m b >>= 1 return x # print(dp) t = 1 # t = int(input()) for _ in range(t): n = inp() arr = [] dp = [float(inf)] * (n + 1) dp[0] = 0 for i in range(1, n + 1): arr.append(inp()) dp[i] = min(dp[i], dp[i - 1] + 20) # print(i, arr[-1] - 90, dp[i], dp[bisect_right(arr, arr[-1] - 90)], dp) dp[i] = min(dp[i], dp[bisect_right(arr, arr[-1] - 90)] + 50, dp[bisect_right(arr, arr[i - 1] - 1440)] + 120) # print(dp) ans = [] for i in range(n): print(dp[i + 1] - dp[i]) # a1 = redpanda(n, l) # print(a) # print(a1) # sys.stdout.write(str(ans)) # print("Case #" + str(_ + 1) + ": " + str(ans))
8VC Venture Cup 2017 - Final Round
CF
2,017
2
256
Travel Card
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.
Output n integers. For each trip, print the sum the passenger is charged after it.
null
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
[{"input": "3\n10\n20\n30", "output": "20\n20\n10"}, {"input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10"}]
1,600
["binary search", "dp"]
31
[{"input": "3\r\n10\r\n20\r\n30\r\n", "output": "20\r\n20\r\n10\r\n"}, {"input": "10\r\n13\r\n45\r\n46\r\n60\r\n103\r\n115\r\n126\r\n150\r\n256\r\n516\r\n", "output": "20\r\n20\r\n10\r\n0\r\n20\r\n0\r\n0\r\n20\r\n20\r\n10\r\n"}, {"input": "7\r\n100\r\n138\r\n279\r\n308\r\n396\r\n412\r\n821\r\n", "output": "20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n0\r\n"}, {"input": "8\r\n0\r\n2\r\n51\r\n63\r\n69\r\n75\r\n80\r\n90\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n20\r\n"}, {"input": "8\r\n0\r\n3\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "1\r\n0\r\n", "output": "20\r\n"}]
false
stdio
null
true
616/F
616
F
PyPy 3-64
TESTS
6
124
4,812,800
143457268
from itertools import zip_longest, islice from collections import defaultdict def ranks(l): index = {v: i for i, v in enumerate(sorted(set(l)))} return [index[v] for v in l] def suffixArray(s): line = ranks(s) n, k, ans, sa = len(s), 1, [line], [0]*len(s) while max(line) < n - 1: #k < n - 1 without optimization line = ranks(list(zip_longest(line, islice(line, k, None), fillvalue=-1))) ans, k = ans + [line], k << 1 for i, k in enumerate(ans[-1]): sa[k] = i return ans, sa def lcp(arr, suffixArr, inv_suff): n, ans, k = len(arr), [0] * len(arr), 0 for i in range(n): if inv_suff[i] == n - 1: k = 0 continue j = suffixArr[inv_suff[i] + 1] while i + k < n and j + k < n and arr[i + k] == arr[j + k]: k += 1 ans[inv_suff[i]] = k if k > 0: k -= 1 return ans n = int(input()) arr = [] A = [] B = [] for _ in range(n): arr += [input().rstrip()] c = [int(i) for i in input().split()] for i in range(n): A += [ord(j) - 96 for j in arr[i]] B += [c[i]] * len(arr[i]) A += [i + 27] B += [0] # it should not matter? c, sa = suffixArray(A) cnts = [B[i] for i in sa] max_cnts = max(cnts) LCP = lcp(A, sa, c[-1]) N = len(sa) f1 = list(range(N)) f2 = list(range(N)) d_lcp = defaultdict(list) for i, x in enumerate(LCP[:-1]): d_lcp[x] += [i] keys_lcp = sorted(d_lcp.keys())[::-1] #print(LCP) #print(d_lcp) ans = 0 for key in keys_lcp: for u in d_lcp[key]: #print(u) v1 = f1[u + 1] u1 = f2[u] cu, cv = cnts[u1], cnts[u + 1] cnts[u1] += cv max_cnts = max(max_cnts, cnts[u1]) f1[u1] = v1 f2[v1] = u1 #print(max_cnts * key) ans = max(ans, max_cnts * key) #print(keys_lcp) #print("COSTS", cnts) #print("LCP", LCP) print(ans) #print(arr, c)
47
1,872
258,457,600
185595299
def SA_IS(S): S += [0] k = max(S) + 1 n = len(S) def induce_l(sa, a, n, k, stype): bucket = get_buckets(a, k, 1) for i in range(n): j = sa[i] - 1 if j >= 0 and (not stype[j]): sa[bucket[a[j]]] = j bucket[a[j]] += 1 def induce_s(sa, a, n, k, stype): bucket = get_buckets(a, k, 0) for i in range(n)[::-1]: j = sa[i] - 1 if j >= 0 and stype[j]: bucket[a[j]] -= 1 sa[bucket[a[j]]] = j def get_buckets(a, k, start=0): bucket = [0] * k for item in a: bucket[item] += 1 s = 0 for i in range(k): s += bucket[i] bucket[i] = s - (bucket[i] if start else 0) return bucket def set_lms(a, n, k, default_order): bucket = get_buckets(a, k) sa = [-1] * n for i in default_order[::-1]: bucket[a[i]] -= 1 sa[bucket[a[i]]] = i return sa def induce(a, n, k, stype, default_order): sa = set_lms(a, n, k, default_order) induce_l(sa, a, n, k, stype) induce_s(sa, a, n, k, stype) return sa def rename_LMS_substring(sa, a, n, stype, LMS, l): sa = [_s for _s in sa if LMS[_s]] tmp = [-1] * (n // 2) + [0] dupl = 0 for t in range(1, l): i, j = sa[t - 1], sa[t] for ii in range(n): if a[i + ii] != a[j + ii] or stype[i + ii] != stype[j + ii]: break if ii and (LMS[i + ii] or LMS[j + ii]): dupl += 1 break tmp[j // 2] = t - dupl tmp = [t for t in tmp if t >= 0] return tmp, dupl def calc(a, n, k): stype = [1] * n for i in range(n - 1)[::-1]: if a[i] > a[i + 1] or (a[i] == a[i + 1] and stype[i + 1] == 0): stype[i] = 0 LMS = [1 if stype[i] and not stype[i - 1] else 0 for i in range(n - 1)] + [1] l = sum(LMS) lms = [i for i in range(n) if LMS[i]] sa = induce(a, n, k, stype, lms) renamed_LMS, dupl = rename_LMS_substring(sa, a, n, stype, LMS, l) if dupl: sub_sa = calc(renamed_LMS, l, l - dupl) else: sub_sa = [0] * l for i in range(l): sub_sa[renamed_LMS[i]] = i lms = [lms[sub_sa[i]] for i in range(l)] sa = induce(a, n, k, stype, lms) return sa sa = calc(S, n, k) S.pop() return sa def LCP(s, n, sa): lcp = [-1] * (n + 1) rank = [0] * (n + 1) for i in range(n + 1): rank[sa[i]] = i h = 0 lcp[0] = 0 for i in range(n): j = sa[rank[i] - 1] if h > 0: h -= 1 while j + h < n and i + h < n and s[j + h] == s[i + h]: h += 1 lcp[rank[i] - 1] = h return lcp class SparseTable: def __init__(self, A, op): self.n = len(A) logn = (self.n - 1).bit_length() self.logn = logn if self.n == 1: logn = 1 self.op = op self.table = [None] * (self.n * logn) for i in range(self.n): self.table[i] = A[i] for i in range(1, logn): ma = self.n - (1 << i) + 1 d = 1 << (i - 1) for j in range(ma): self.table[i * self.n + j] = op(self.table[(i - 1) * self.n + j], self.table[(i - 1) * self.n + j + d]) def prod(self, l, r): if l == r: return 1 << 30 d = r - l if d == 1: return self.table[l] logn = (d - 1).bit_length() - 1 return self.op(self.table[logn * self.n + l], self.table[logn * self.n + r - (1 << logn)]) n = int(input()) S = [] le = [] kl = [] for i in range(n): T = input() le.append(len(T)) for j, t in enumerate(T): kl.append((i, j)) S.append(ord(t) - 90) S.append(1) kl.append((-1, -1)) sa = SA_IS(S) lcp = LCP(S, len(S), sa) C = list(map(int, input().split())) + [0] cum = [0] for s in sa[1:]: k, l = kl[s] cum.append(cum[-1] + C[k]) l = 1 while S[sa[l]] == 1: l += 1 st = SparseTable(lcp, min) dist = 0 tot = 0 ans = 0 while l < len(sa): kk, ll = kl[sa[l]] r = l nd = 1 << 30 for i in range(st.logn - 1, -1, -1): v = st.table[i * st.n + r] if v is not None and v > dist: nd = min(nd, v) r += 1 << i nd = min(nd, le[kk] - ll) if dist == nd: dist = min(dist, lcp[l]) l += 1 continue ans = max(ans, nd * (cum[r] - cum[l - 1])) ntot = tot + (r - l + 1) * (nd - dist) tot = ntot dist = nd print(ans)
Educational Codeforces Round 5
ICPC
2,016
6
512
Expensive Strings
You are given n strings ti. Each string has cost ci. Let's define the function of string $$s : f(s) = \sum_{i=1}^{n} c_i \cdot p_{s,i} \cdot |s|$$, where ps, i is the number of occurrences of s in ti, |s| is the length of the string s. Find the maximal value of function f(s) over all strings. Note that the string s is not necessarily some string from t.
The first line contains the only integer n (1 ≤ n ≤ 105) — the number of strings in t. Each of the next n lines contains contains a non-empty string ti. ti contains only lowercase English letters. It is guaranteed that the sum of lengths of all strings in t is not greater than 5·105. The last line contains n integers ci ( - 107 ≤ ci ≤ 107) — the cost of the i-th string.
Print the only integer a — the maximal value of the function f(s) over all strings s. Note one more time that the string s is not necessarily from t.
null
null
[{"input": "2\naa\nbb\n2 1", "output": "4"}, {"input": "2\naa\nab\n2 1", "output": "5"}]
2,700
["data structures", "sortings", "string suffix structures", "strings"]
47
[{"input": "2\r\naa\r\nbb\r\n2 1\r\n", "output": "4\r\n"}, {"input": "2\r\naa\r\nab\r\n2 1\r\n", "output": "5\r\n"}, {"input": "1\r\naaa\r\n2\r\n", "output": "8\r\n"}, {"input": "3\r\na\r\naa\r\naaa\r\n1 2 3\r\n", "output": "16\r\n"}, {"input": "3\r\na\r\naaa\r\naa\r\n2 1 3\r\n", "output": "11\r\n"}, {"input": "1\r\nabcde\r\n-1\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\n1\r\n", "output": "1\r\n"}, {"input": "5\r\nbbbb\r\nbaaa\r\nbbba\r\naaba\r\nbbaa\r\n17 -17 -82 47 -85\r\n", "output": "188\r\n"}, {"input": "10\r\nckxyxnoyqw\r\nlwvjsygwrc\r\nhretftiphn\r\nrqqncjnggb\r\ntxvnxgkynk\r\nbukntrryio\r\ncinrjaobxu\r\nxqvurzhxth\r\nkmvrlhpqdk\r\ndsdubnyoun\r\n-78 84 81 37 82 34 -46 63 -89 22\r\n", "output": "840\r\n"}, {"input": "10\r\nmmmidsznkw\r\nzierqdohbz\r\nwbgibzydwu\r\ncldsvcesgw\r\nzvedhvgxcf\r\nxcacrlgwdz\r\nnbyukqyhum\r\ndrhussqjxm\r\nniwqsxdmdd\r\noohzarrueh\r\n-88 73 94 60 96 -15 95 -6 62 82\r\n", "output": "960\r\n"}, {"input": "10\r\nfbrkgmqawj\r\nzmbcexhtik\r\nwxgreonrbl\r\namtyqwcnke\r\nqmemrygvzc\r\nmsvugovilt\r\nnqbylyqnll\r\nzwqlheqdgg\r\ncmfsqvqmmr\r\nlcfkhqztwk\r\n48 30 -42 -78 52 -96 2 85 68 -59\r\n", "output": "850\r\n"}, {"input": "3\r\nbear\r\ndemo\r\nbeardemo\r\n1 2 3\r\n", "output": "24\r\n"}, {"input": "1\r\nabbabbabababaabab\r\n1\r\n", "output": "17\r\n"}, {"input": "5\r\nb\r\nccbaa\r\ncbbcb\r\nc\r\nac\r\n1 10 5 2 4\r\n", "output": "50\r\n"}, {"input": "4\r\nbxqehkrlslszbmbeyjueibxawhqncqidnyiwlzhfkbennirnrnrboqcmxucakkdzlnvwmebyfhlupchjr\r\nkfqmuohwqtivttjqrvvuwachxrmeadfnlx\r\nfniddclzxfinwfirdlvlioxmrctentdsgmvkqjlnqvaocjfgucrcrqqismmgfranfyxvhikzflniwvoqxhuoxmyramxheavlauhjfrikevsaqisopoembfedrdlxfgjhcsqhlysqwmsovlckzjzcq\r\nghkrhqzqzbkqj\r\n523 -1010 -3441 3505\r\n", "output": "45565\r\n"}]
false
stdio
null
true
870/C
870
C
Python 3
TESTS
5
61
5,529,600
32840408
#http://codeforces.com/problemset/problem/870/C #solved n = int(input()) todo = [int(input()) for _ in range(n)] wrong = [1, 2, 3, 5, 7, 11] for i in todo: if i in wrong: print(-1) continue modulo = i % 4 if modulo == 0 or modulo == 2: print(i // 4) continue elif modulo == 1: print((i - 1) // 4 - 1) continue elif modulo == 3: print((i - 3) // 4 - 2) continue
20
265
10,547,200
31342549
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**15 mod = 10**9+7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): q = I() r = [] for _ in range(q): n = I() if n % 2 == 1: n -= 9 if n == 0: r.append(1) elif n < 4: r.append(-1) else: r.append(n//4+1) else: t = n // 4 if t < 1: r.append(-1) else: r.append(n//4) return '\n'.join(map(str,r)) print(main())
Technocup 2018 - Elimination Round 2
CF
2,017
2
256
Maximum splitting
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
null
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
[{"input": "1\n12", "output": "3"}, {"input": "2\n6\n8", "output": "1\n2"}, {"input": "3\n1\n2\n3", "output": "-1\n-1\n-1"}]
1,300
["dp", "greedy", "math", "number theory"]
20
[{"input": "1\r\n12\r\n", "output": "3\r\n"}, {"input": "2\r\n6\r\n8\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n1\r\n2\r\n3\r\n", "output": "-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n1\r\n2\r\n3\r\n5\r\n7\r\n11\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n4\r\n6\r\n9\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "20\r\n8\r\n13\r\n20\r\n12\r\n9\r\n16\r\n4\r\n19\r\n7\r\n15\r\n10\r\n6\r\n14\r\n11\r\n3\r\n2\r\n5\r\n17\r\n18\r\n1\r\n", "output": "2\r\n2\r\n5\r\n3\r\n1\r\n4\r\n1\r\n3\r\n-1\r\n2\r\n2\r\n1\r\n3\r\n-1\r\n-1\r\n-1\r\n-1\r\n3\r\n4\r\n-1\r\n"}, {"input": "100\r\n611\r\n513\r\n544\r\n463\r\n38\r\n778\r\n347\r\n317\r\n848\r\n664\r\n382\r\n108\r\n718\r\n33\r\n334\r\n876\r\n234\r\n22\r\n944\r\n305\r\n159\r\n245\r\n513\r\n691\r\n639\r\n135\r\n308\r\n324\r\n813\r\n459\r\n304\r\n116\r\n331\r\n993\r\n184\r\n224\r\n853\r\n769\r\n121\r\n687\r\n93\r\n930\r\n751\r\n308\r\n485\r\n914\r\n400\r\n695\r\n95\r\n981\r\n175\r\n972\r\n121\r\n654\r\n242\r\n610\r\n617\r\n999\r\n237\r\n548\r\n742\r\n767\r\n613\r\n172\r\n223\r\n391\r\n102\r\n907\r\n673\r\n116\r\n230\r\n355\r\n189\r\n552\r\n399\r\n493\r\n903\r\n201\r\n985\r\n459\r\n776\r\n641\r\n693\r\n919\r\n253\r\n540\r\n427\r\n394\r\n655\r\n101\r\n461\r\n854\r\n417\r\n249\r\n66\r\n380\r\n213\r\n906\r\n212\r\n528\r\n", "output": "151\r\n127\r\n136\r\n114\r\n9\r\n194\r\n85\r\n78\r\n212\r\n166\r\n95\r\n27\r\n179\r\n7\r\n83\r\n219\r\n58\r\n5\r\n236\r\n75\r\n38\r\n60\r\n127\r\n171\r\n158\r\n32\r\n77\r\n81\r\n202\r\n113\r\n76\r\n29\r\n81\r\n247\r\n46\r\n56\r\n212\r\n191\r\n29\r\n170\r\n22\r\n232\r\n186\r\n77\r\n120\r\n228\r\n100\r\n172\r\n22\r\n244\r\n42\r\n243\r\n29\r\n163\r\n60\r\n152\r\n153\r\n248\r\n58\r\n137\r\n185\r\n190\r\n152\r\n43\r\n54\r\n96\r\n25\r\n225\r\n167\r\n29\r\n57\r\n87\r\n46\r\n138\r\n98\r\n122\r\n224\r\n49\r\n245\r\n113\r\n194\r\n159\r\n172\r\n228\r\n62\r\n135\r\n105\r\n98\r\n162\r\n24\r\n114\r\n213\r\n103\r\n61\r\n16\r\n95\r\n52\r\n226\r\n53\r\n132\r\n"}, {"input": "1\r\n10000001\r\n", "output": "2499999\r\n"}]
false
stdio
null
true
756/B
756
B
Python 3
TESTS
5
46
4,608,000
25299038
def memoize(f): memo = {} def helper(x): if x not in memo: memo[x] = f(x) return memo[x] return helper n = int(input()) t = [0 for i in range(n+8)] #times = [0 for i in range(n+8)] @memoize def cost(i): #print('cost', i) if i<=0: return 0 nc = cost(i-1) + 20 pos = i while t[pos] > t[i] - 90 and pos>0: pos-=1 nc = min(nc, cost(pos)+50) #print(' cost(pos):',cost(pos), ' pos:', pos, ' ', nc) pos = i while t[pos] > t[i] - 1440 and pos>40: pos-=40 while t[pos] > t[i] - 1440 and pos>0: pos-=1 nc = min(nc, cost(pos)+120) #print(' cost(pos):',cost(pos), ' pos:', pos, ' ', nc) return nc for i in range(1, n+1): t[i] = int(input()) print (20 if i==0 else cost(i) - cost(i-1) )
31
264
12,697,600
194604058
import sys input = sys.stdin.readline from bisect import bisect M = 10**5+1 n = int(input()) g = [int(input()) for _ in range(n)] d = [0] + [-1]*n x = [0] + [-1]*n for i in range(1, n+1): a = bisect(g, g[i-1]-90) b = bisect(g, g[i-1]-1440) d[i] = min(d[i-1]+20, d[a]+50, d[b]+120) x[i] = d[i]-d[i-1] print(x[i])
8VC Venture Cup 2017 - Final Round
CF
2,017
2
256
Travel Card
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.
Output n integers. For each trip, print the sum the passenger is charged after it.
null
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
[{"input": "3\n10\n20\n30", "output": "20\n20\n10"}, {"input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10"}]
1,600
["binary search", "dp"]
31
[{"input": "3\r\n10\r\n20\r\n30\r\n", "output": "20\r\n20\r\n10\r\n"}, {"input": "10\r\n13\r\n45\r\n46\r\n60\r\n103\r\n115\r\n126\r\n150\r\n256\r\n516\r\n", "output": "20\r\n20\r\n10\r\n0\r\n20\r\n0\r\n0\r\n20\r\n20\r\n10\r\n"}, {"input": "7\r\n100\r\n138\r\n279\r\n308\r\n396\r\n412\r\n821\r\n", "output": "20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n0\r\n"}, {"input": "8\r\n0\r\n2\r\n51\r\n63\r\n69\r\n75\r\n80\r\n90\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n20\r\n"}, {"input": "8\r\n0\r\n3\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "1\r\n0\r\n", "output": "20\r\n"}]
false
stdio
null
true
755/B
755
B
Python 3
TESTS
10
202
1,433,600
184716372
x, y = [int(x) for x in input().split()] vez = 0 palx = [] paly = [] for i in range(x): pal = str(input()) palx.append(pal) for i in range(y): pal = str(input()) paly.append(pal) xp = 0 yp = 0 xk = 0 yk = 0 palas = [] while x > 0 and y > 0: if vez == 0: if not palx[xk] in palas: xp += 1 palas.append(palx[xk]) xk += 1 vez = 1 x -= 1 else: if not paly[yk] in palas: yp += 1 palas.append(paly[yk]) yk += 1 vez = 0 y -= 1 if x > 0: while x > 0: if not palx[xk] in palas: xp += 1 palas.append(palx[xk]) x-= 1 xk += 1 elif y > 0: while y > 0: if not paly[yk] in palas: yp += 1 palas.append(paly[yk]) yk += 1 y -= 1 if xp > yp: print('YES') else: print('NO')
33
46
1,228,800
172842807
n,m=map(int,input().split()) x=[] for i in range(n+m): x.append(input()) if n>m: print('YES') elif n<m: print('NO') elif n==m: if len(set(x))%2!=0: print('YES') else: print('NO')
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
755/B
755
B
PyPy 3-64
TESTS
10
218
4,300,800
138929395
n, m = [int(x) for x in input().split(' ')] polandWords = [] enemyWords = [] for _ in range(n): word = input() polandWords.append(word) for _ in range(m): word = input() enemyWords.append(word) words_used = set() pc = 0 pe = 0 while True: pw = None while pw == None and len(polandWords) > 0: pw = polandWords.pop(0) if pw in words_used: pw = None if pw != None: pc += 1 words_used.add(pw) we = None while we == None and len(enemyWords) > 0: we = enemyWords.pop(0) if we in words_used: we = None if we != None: pe += 1 words_used.add(we) if we == None and pw == None: break if pc > pe: print('YES') else: print('NO')
33
46
1,228,800
176497750
polland_ball, enemy_ball = map(int, input().split()) palavras_polland = set() palavras_enemy = set() for i in range(polland_ball): palavras_polland.add(str(input())) for i in range(enemy_ball): palavras_enemy.add(str(input())) # Os jogadores vão iniciar com palavras que ambos conhecem. palavras_em_comum = palavras_polland.intersection(palavras_enemy) palavras_distintas_polland = palavras_polland.difference(palavras_em_comum) palavras_distintas_enemy = palavras_enemy.difference(palavras_em_comum) polland_venceu = "" if len(palavras_em_comum) % 2 == 0: # Empate momentâneo. if len(palavras_distintas_polland) > len(palavras_distintas_enemy): polland_venceu = "YES" else: polland_venceu = "NO" else: # No momento o Polland está vencendo por 1. if len(palavras_distintas_enemy) - len(palavras_distintas_polland) > 0: # Enemy empatou ou venceu. polland_venceu = "NO" else: polland_venceu = "YES" print(polland_venceu)
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
870/C
870
C
PyPy 3-64
TESTS
5
61
0
205335243
nums=[4,9,6,15] t=int(input()) for _ in range(t): n=int(input()) if n < nums[n%4]: print(-1) else: print((n-nums[n%4])//4+1)
20
280
11,059,200
158310978
from sys import stdin,stdout input = stdin.read r = map(int,input().split()) q = next(r) for _ in range(q): a = next(r) if(a in [1,2,3,5,7,11]): stdout.write("-1\n") else: stdout.write(str(a//4 - (a%4)%2)) stdout.write("\n")
Technocup 2018 - Elimination Round 2
CF
2,017
2
256
Maximum splitting
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
null
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
[{"input": "1\n12", "output": "3"}, {"input": "2\n6\n8", "output": "1\n2"}, {"input": "3\n1\n2\n3", "output": "-1\n-1\n-1"}]
1,300
["dp", "greedy", "math", "number theory"]
20
[{"input": "1\r\n12\r\n", "output": "3\r\n"}, {"input": "2\r\n6\r\n8\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n1\r\n2\r\n3\r\n", "output": "-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n1\r\n2\r\n3\r\n5\r\n7\r\n11\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n4\r\n6\r\n9\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "20\r\n8\r\n13\r\n20\r\n12\r\n9\r\n16\r\n4\r\n19\r\n7\r\n15\r\n10\r\n6\r\n14\r\n11\r\n3\r\n2\r\n5\r\n17\r\n18\r\n1\r\n", "output": "2\r\n2\r\n5\r\n3\r\n1\r\n4\r\n1\r\n3\r\n-1\r\n2\r\n2\r\n1\r\n3\r\n-1\r\n-1\r\n-1\r\n-1\r\n3\r\n4\r\n-1\r\n"}, {"input": "100\r\n611\r\n513\r\n544\r\n463\r\n38\r\n778\r\n347\r\n317\r\n848\r\n664\r\n382\r\n108\r\n718\r\n33\r\n334\r\n876\r\n234\r\n22\r\n944\r\n305\r\n159\r\n245\r\n513\r\n691\r\n639\r\n135\r\n308\r\n324\r\n813\r\n459\r\n304\r\n116\r\n331\r\n993\r\n184\r\n224\r\n853\r\n769\r\n121\r\n687\r\n93\r\n930\r\n751\r\n308\r\n485\r\n914\r\n400\r\n695\r\n95\r\n981\r\n175\r\n972\r\n121\r\n654\r\n242\r\n610\r\n617\r\n999\r\n237\r\n548\r\n742\r\n767\r\n613\r\n172\r\n223\r\n391\r\n102\r\n907\r\n673\r\n116\r\n230\r\n355\r\n189\r\n552\r\n399\r\n493\r\n903\r\n201\r\n985\r\n459\r\n776\r\n641\r\n693\r\n919\r\n253\r\n540\r\n427\r\n394\r\n655\r\n101\r\n461\r\n854\r\n417\r\n249\r\n66\r\n380\r\n213\r\n906\r\n212\r\n528\r\n", "output": "151\r\n127\r\n136\r\n114\r\n9\r\n194\r\n85\r\n78\r\n212\r\n166\r\n95\r\n27\r\n179\r\n7\r\n83\r\n219\r\n58\r\n5\r\n236\r\n75\r\n38\r\n60\r\n127\r\n171\r\n158\r\n32\r\n77\r\n81\r\n202\r\n113\r\n76\r\n29\r\n81\r\n247\r\n46\r\n56\r\n212\r\n191\r\n29\r\n170\r\n22\r\n232\r\n186\r\n77\r\n120\r\n228\r\n100\r\n172\r\n22\r\n244\r\n42\r\n243\r\n29\r\n163\r\n60\r\n152\r\n153\r\n248\r\n58\r\n137\r\n185\r\n190\r\n152\r\n43\r\n54\r\n96\r\n25\r\n225\r\n167\r\n29\r\n57\r\n87\r\n46\r\n138\r\n98\r\n122\r\n224\r\n49\r\n245\r\n113\r\n194\r\n159\r\n172\r\n228\r\n62\r\n135\r\n105\r\n98\r\n162\r\n24\r\n114\r\n213\r\n103\r\n61\r\n16\r\n95\r\n52\r\n226\r\n53\r\n132\r\n"}, {"input": "1\r\n10000001\r\n", "output": "2499999\r\n"}]
false
stdio
null
true
756/B
756
B
Python 3
TESTS
5
62
4,608,000
24631036
n = int(input()) travel_times = list() travel_pay = list() pay_ticket1 = 0 time_ticket1 = 0 pay_ticket2 = 0 time_ticket2 = 0 for travel_id in range(n): t = int(input()) travel_times.append(t) pay2 = 20 sum_time1 = t - travel_times[time_ticket1] sum_time2 = t - travel_times[time_ticket2] if sum_time1 >= 90: pay_ticket1 -= travel_pay[time_ticket1] for id2 in range(time_ticket1+1, travel_id): if t - travel_times[id2] < 90: time_ticket1 = id2 break else: pay_ticket1 -= travel_pay[id2] sum_time1 = t - travel_times[time_ticket1] if sum_time2 >= 1440: pay_ticket2 -= travel_pay[time_ticket2] for id2 in range(time_ticket2+1, travel_id): if t - travel_times[id2] < 1440: time_ticket2 = id2 break else: pay_ticket2 -= travel_pay[id2] sum_time2 = t - travel_times[time_ticket2] if pay_ticket1 + pay2 > 50: pay2 = 50 - pay_ticket1 if pay_ticket2 + pay2 > 120: pay2 = 120 - pay_ticket2 pay_ticket1 += pay2 pay_ticket2 += pay2 travel_pay.append(pay2) for pay in travel_pay: print(pay)
31
342
10,547,200
93853498
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key,lru_cache import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # import sys # input = sys.stdin.readline M = mod = 10**9 + 7 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip().split()] def st():return str(input().rstrip())[2:-1] def val():return int(input().rstrip()) def li2():return [str(i)[2:-1] for i in input().rstrip().split()] def li3():return [int(i) for i in st()] n = val() l = [] for i in range(n):l.append(val()) l.sort() l1 = [0] l2 = [-float('inf')] for i in l: ind2 = br(l2, i - 90) - 1 ind1 = br(l2, i - 1) - 1 ind3 = br(l2, i - 1440) - 1 # print(ind1, ind2, ind3) l2.append(i) l1.append(min(l1[ind1] + 20, l1[ind2] + 50, l1[ind3] + 120)) # print(l1, l2) l2[0] = 0 # print(l2) for i in range(1, n + 1): print(l1[i] - l1[i - 1]) print()
8VC Venture Cup 2017 - Final Round
CF
2,017
2
256
Travel Card
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.
Output n integers. For each trip, print the sum the passenger is charged after it.
null
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
[{"input": "3\n10\n20\n30", "output": "20\n20\n10"}, {"input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10"}]
1,600
["binary search", "dp"]
31
[{"input": "3\r\n10\r\n20\r\n30\r\n", "output": "20\r\n20\r\n10\r\n"}, {"input": "10\r\n13\r\n45\r\n46\r\n60\r\n103\r\n115\r\n126\r\n150\r\n256\r\n516\r\n", "output": "20\r\n20\r\n10\r\n0\r\n20\r\n0\r\n0\r\n20\r\n20\r\n10\r\n"}, {"input": "7\r\n100\r\n138\r\n279\r\n308\r\n396\r\n412\r\n821\r\n", "output": "20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n0\r\n"}, {"input": "8\r\n0\r\n2\r\n51\r\n63\r\n69\r\n75\r\n80\r\n90\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n20\r\n"}, {"input": "8\r\n0\r\n3\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "1\r\n0\r\n", "output": "20\r\n"}]
false
stdio
null
true
756/B
756
B
Python 3
TESTS
5
61
716,800
167850524
from typing import List import bisect def sum_of_trip(n: int, arr: List[int]) -> List[int]: result = [] sum = [0] arr.insert(0,0) for i in range(1,n+1): minimum_sum = sum[i-1]+ 20 last_90 = bisect.bisect_left(arr,arr[i]-89,0,i-1) # find leftmost value in array greater than x = arr[i]-89 if last_90>1: last_90 = last_90 - 1 minimum_sum = min(minimum_sum,max(0,sum[last_90]+50)) last_24 = bisect.bisect_left(arr,arr[i]-1439,0,i-1) # find leftmost value in array greater than x = arr[i]-1439 if last_24>1: last_24 = last_24 - 1 minimum_sum = min(minimum_sum,max(0,sum[last_24]+120)) cost = minimum_sum - sum[i-1] sum.append(minimum_sum) print(cost) #return result n = int(input()) test_cases = [] for i in range(n): data = int(input()) test_cases.append(data) sum_of_trip(n,test_cases) #print(sum_of_trip(10,[13,45,46,60,103,115,126,150,256,516])) #-> [20,20,10,0,20,0,0,20,20,10])
31
343
9,830,400
231250121
# https://codeforces.com/contest/756/problem/B n = int(input()) # Number of trips by passenger t = [] for _ in range(n): t.append(int(input())) # Time of each trip, relative to starting the system """ Rules A ticket of one trip costs 20 rubles A ticket for 90 minutes costs 50 rubles A ticket for one day (1440 minutes) costs 120 rubles The system takes all trips and chooses the set of tickets with minimum total cost. Solution returns the minimum payment at each stage """ start_90_min = 0 start_one_day = 0 dp = [0] # Minimum payment at each stage for i, trip in enumerate(t): # Increment up current range of 90 minutes and one day trip by comparing the current element to the earliest possible element this trip could encompass while trip - t[start_90_min] >= 90: start_90_min += 1 while trip - t[start_one_day] >= 1440: start_one_day += 1 # For current stage, add minimum payment of the three options dp.append(min(dp[i] + 20, dp[start_90_min] + 50, dp[start_one_day] + 120)) print(dp[-1] - dp[-2])
8VC Venture Cup 2017 - Final Round
CF
2,017
2
256
Travel Card
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.
Output n integers. For each trip, print the sum the passenger is charged after it.
null
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
[{"input": "3\n10\n20\n30", "output": "20\n20\n10"}, {"input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10"}]
1,600
["binary search", "dp"]
31
[{"input": "3\r\n10\r\n20\r\n30\r\n", "output": "20\r\n20\r\n10\r\n"}, {"input": "10\r\n13\r\n45\r\n46\r\n60\r\n103\r\n115\r\n126\r\n150\r\n256\r\n516\r\n", "output": "20\r\n20\r\n10\r\n0\r\n20\r\n0\r\n0\r\n20\r\n20\r\n10\r\n"}, {"input": "7\r\n100\r\n138\r\n279\r\n308\r\n396\r\n412\r\n821\r\n", "output": "20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n0\r\n"}, {"input": "8\r\n0\r\n2\r\n51\r\n63\r\n69\r\n75\r\n80\r\n90\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n20\r\n"}, {"input": "8\r\n0\r\n3\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "1\r\n0\r\n", "output": "20\r\n"}]
false
stdio
null
true
756/B
756
B
Python 3
TESTS
5
61
5,529,600
31601336
a = b = 1 p, s = [0, 0], [0, 0] for i in range(int(input())): p += [int(input())] while p[-1] - p[a] > 89: a += 1 while p[-1] - p[b] > 1339: b += 1 s += [min(s[-1] + 20, s[a - 1] + 50, s[b - 1] + 120)] print(' '.join(str(u - v) for u, v in zip(s[2:], s[1:])))
31
358
10,649,600
105437041
import sys def bs (l,r,val): l1= l r1= r while (l1<= r1): mid = (l1 + r1)//2 if t[mid] >= val : res = mid r1 = mid -1 elif t[mid] < val : l1 = mid +1 return res t= [] n = int (sys.stdin.readline()) for i in range (n): temp = int(sys.stdin.readline()) t.append(temp) sum= [0 for i in range (n)] for i in range (n): nine_ty = bs(0,i,t[i]-90+1) if nine_ty > 0 : temp = min (20 ,max(50- (sum[i-1]- sum[nine_ty-1]),0)) else: temp = min (20, 50 - sum[max(i-1,0)]) a_day =bs(0,i,t[i]-1440+1) if a_day > 0: temp = min(temp, max(120 - (sum[i - 1] - sum[a_day - 1]), 0)) else: temp = min(temp, 120 - sum[max(i-1,0)]) print (temp) sum [i] = sum[max(i-1,0)] + temp
8VC Venture Cup 2017 - Final Round
CF
2,017
2
256
Travel Card
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.
Output n integers. For each trip, print the sum the passenger is charged after it.
null
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
[{"input": "3\n10\n20\n30", "output": "20\n20\n10"}, {"input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10"}]
1,600
["binary search", "dp"]
31
[{"input": "3\r\n10\r\n20\r\n30\r\n", "output": "20\r\n20\r\n10\r\n"}, {"input": "10\r\n13\r\n45\r\n46\r\n60\r\n103\r\n115\r\n126\r\n150\r\n256\r\n516\r\n", "output": "20\r\n20\r\n10\r\n0\r\n20\r\n0\r\n0\r\n20\r\n20\r\n10\r\n"}, {"input": "7\r\n100\r\n138\r\n279\r\n308\r\n396\r\n412\r\n821\r\n", "output": "20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n0\r\n"}, {"input": "8\r\n0\r\n2\r\n51\r\n63\r\n69\r\n75\r\n80\r\n90\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n20\r\n"}, {"input": "8\r\n0\r\n3\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "20\r\n20\r\n10\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "1\r\n0\r\n", "output": "20\r\n"}]
false
stdio
null
true
870/C
870
C
PyPy 3-64
TESTS
5
46
0
148696523
t=int(input()) while t: n=int(input()) if n%2==0: if n==2:print(-1) else:print(n//4) else: if n<10 and n!=9:print(-1) else: x = n//4-2 y = n//6-1 if n-x*4==9 or n-y*6==9: print(n//4-1) else:print(-1) t-=1
20
296
8,704,000
47445707
# !/bin/env python3 # coding: UTF-8 # ✪ H4WK3yE乡 # Mohd. Farhan Tahir # Indian Institute Of Information Technology and Management,Gwalior # Question Link # https://codeforces.com/problemset/problem/870/C # # ///==========Libraries, Constants and Functions=============/// import sys inf = float("inf") mod = 1000000007 def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline() # ///==========MAIN=============/// def precompute(): dp = [-1 for _ in range(16)] dp[0] = 0 composite = [4, 6, 9] for i in range(1, 16): for j in composite: if i >= j and dp[i-j] != -1: dp[i] = max(dp[i], 1+dp[i-j]) return dp def main(): dp = precompute() for tc in range(int(input())): n = int(input()) if n < 16: print(dp[n]) else: t = (n-16)//4+1 print(t+dp[n-4*t]) if __name__ == "__main__": main()
Technocup 2018 - Elimination Round 2
CF
2,017
2
256
Maximum splitting
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
null
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
[{"input": "1\n12", "output": "3"}, {"input": "2\n6\n8", "output": "1\n2"}, {"input": "3\n1\n2\n3", "output": "-1\n-1\n-1"}]
1,300
["dp", "greedy", "math", "number theory"]
20
[{"input": "1\r\n12\r\n", "output": "3\r\n"}, {"input": "2\r\n6\r\n8\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n1\r\n2\r\n3\r\n", "output": "-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n1\r\n2\r\n3\r\n5\r\n7\r\n11\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n4\r\n6\r\n9\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "20\r\n8\r\n13\r\n20\r\n12\r\n9\r\n16\r\n4\r\n19\r\n7\r\n15\r\n10\r\n6\r\n14\r\n11\r\n3\r\n2\r\n5\r\n17\r\n18\r\n1\r\n", "output": "2\r\n2\r\n5\r\n3\r\n1\r\n4\r\n1\r\n3\r\n-1\r\n2\r\n2\r\n1\r\n3\r\n-1\r\n-1\r\n-1\r\n-1\r\n3\r\n4\r\n-1\r\n"}, {"input": "100\r\n611\r\n513\r\n544\r\n463\r\n38\r\n778\r\n347\r\n317\r\n848\r\n664\r\n382\r\n108\r\n718\r\n33\r\n334\r\n876\r\n234\r\n22\r\n944\r\n305\r\n159\r\n245\r\n513\r\n691\r\n639\r\n135\r\n308\r\n324\r\n813\r\n459\r\n304\r\n116\r\n331\r\n993\r\n184\r\n224\r\n853\r\n769\r\n121\r\n687\r\n93\r\n930\r\n751\r\n308\r\n485\r\n914\r\n400\r\n695\r\n95\r\n981\r\n175\r\n972\r\n121\r\n654\r\n242\r\n610\r\n617\r\n999\r\n237\r\n548\r\n742\r\n767\r\n613\r\n172\r\n223\r\n391\r\n102\r\n907\r\n673\r\n116\r\n230\r\n355\r\n189\r\n552\r\n399\r\n493\r\n903\r\n201\r\n985\r\n459\r\n776\r\n641\r\n693\r\n919\r\n253\r\n540\r\n427\r\n394\r\n655\r\n101\r\n461\r\n854\r\n417\r\n249\r\n66\r\n380\r\n213\r\n906\r\n212\r\n528\r\n", "output": "151\r\n127\r\n136\r\n114\r\n9\r\n194\r\n85\r\n78\r\n212\r\n166\r\n95\r\n27\r\n179\r\n7\r\n83\r\n219\r\n58\r\n5\r\n236\r\n75\r\n38\r\n60\r\n127\r\n171\r\n158\r\n32\r\n77\r\n81\r\n202\r\n113\r\n76\r\n29\r\n81\r\n247\r\n46\r\n56\r\n212\r\n191\r\n29\r\n170\r\n22\r\n232\r\n186\r\n77\r\n120\r\n228\r\n100\r\n172\r\n22\r\n244\r\n42\r\n243\r\n29\r\n163\r\n60\r\n152\r\n153\r\n248\r\n58\r\n137\r\n185\r\n190\r\n152\r\n43\r\n54\r\n96\r\n25\r\n225\r\n167\r\n29\r\n57\r\n87\r\n46\r\n138\r\n98\r\n122\r\n224\r\n49\r\n245\r\n113\r\n194\r\n159\r\n172\r\n228\r\n62\r\n135\r\n105\r\n98\r\n162\r\n24\r\n114\r\n213\r\n103\r\n61\r\n16\r\n95\r\n52\r\n226\r\n53\r\n132\r\n"}, {"input": "1\r\n10000001\r\n", "output": "2499999\r\n"}]
false
stdio
null
true
616/B
616
B
Python 3
TESTS
5
77
6,963,200
122684731
n,m=map(int,input().split()) l,x=[],10000000000 for i in range(n): l.append(list(map(int,input().split()))) t=min(l[i]) if t<x: x=t c,y=0,1000000000 for i in range(n): if min(l[i])==x: l[i]=-1 c+=1 else: t=min(l[i]) if t<y: y=t # print(l) if c==n: print(x) else: print(y)
16
31
0
180098575
n, m = map(int, input().split()) max_min_street = 0 for i in range(n): max_min_street = max(max_min_street, min(map(int, input().split()))) print(max_min_street)
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
616/B
616
B
Python 3
TESTS
5
109
0
91493734
n , m = map(int,input().split()) l1 = [] l2 = [] l3 = {} for i in range(n): l = list(map(int,input().split())) l1.append(min(l)) l2 += l for j in set(l1): l3[j] = l2.count(j) a = max(l3,key = l3.get) print(a)
16
31
102,400
207452116
n,m = map(int,input().split()) mn = [] for i in range(n): mn.append(min(list(map(int,input().split())))) print(max(mn))
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
48/C
48
C
PyPy 3
TESTS
2
124
17,715,200
133791248
n = int(input()) arr = list(map(int, input().split())) ls = [0] + arr track = [] for i in range(len(ls) - 1): track.append(ls[i+1] - ls[i]) a = max(track) if track.count(a) > 1: print('not unique') else: b = track.index(a) + 1 if n + 1 % b == 0: ans = a else: ans = arr[-1] + track[0] print('unique') print(ans)
45
218
20,172,800
128377230
n = int(input()) p = [None] + list(map(int, input().split())) left_bound = 10 right_bound = 10**10 for i in range(1, n+1): #p[i]*10/i <= a < (10 + p[i]*10)/i left_bound = max(left_bound, (p[i]*10)/i) right_bound = min(right_bound, (10+p[i]*10)/i) start = p[n]+1 l = left_bound*(n+1) - 10*(p[-1]+1) r = right_bound*(n+1) - 10*(p[-1]+1) if (l//10) != ((r-10**(-14))//10): print('not unique') else: print('unique') print(int(start + l//10))
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
484/D
484
D
Python 3
TESTS
5
46
0
137778671
m=int(input()) n=list(map(int,input().split())) ret=0 mi,ma=n[0],n[0] for i in range(1,m): if n[i]>=n[i-1]: ma=n[i] else: ret+=(ma-mi) ma=n[i];mi=n[i] ret+=(ma-mi) print(ret)
71
373
5,427,200
223452331
import sys inf = float('inf') def read_int(): res = b'' while True: d = sys.stdin.buffer.read(1) if d == b'-' or d.isdigit(): res += d elif res: break return int(res) n = read_int() dp, neg, pos = 0, -inf, -inf for _ in range(n): v = read_int() dp, neg, pos = max(dp, neg + v, pos - v), max(neg, dp - v), max(pos, dp + v) print(dp)
Codeforces Round 276 (Div. 1)
CF
2,014
2
256
Kindergarten
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero). The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106). The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Print the maximum possible total sociability of all groups.
null
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1. In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
[{"input": "5\n1 2 3 1 2", "output": "3"}, {"input": "3\n3 3 3", "output": "0"}]
2,400
["data structures", "dp", "greedy"]
71
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n3 3 3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1000000000 1000000000\r\n", "output": "2000000000\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "4\r\n"}, {"input": "4\r\n23 5 7 1\r\n", "output": "24\r\n"}, {"input": "4\r\n23 7 5 1\r\n", "output": "22\r\n"}, {"input": "8\r\n23 2 7 5 15 8 4 10\r\n", "output": "37\r\n"}, {"input": "8\r\n4 5 3 6 2 7 1 8\r\n", "output": "16\r\n"}]
false
stdio
null
true
39/F
39
F
Python 3
TESTS
5
62
0
188642728
n,m,k=map(int,input().split()) lis=list(map(int,input().split())) klis=list(map(int,input().split())) dicti={} mini=10**9+1 for i in lis: dicti[i]=0 for j in klis: if j%i==0: dicti[i]+=1 mini=min(dicti[i],mini) count=0 #print(dicti) for i in dicti: if dicti[i]==mini: count+=1 print(count) lis.sort() for i in range(0,0+count): print(lis[i],end=" ")
35
124
1,536,000
165078905
import sys input = sys.stdin.readline n, m, k = map(int, input().split()) w = list(map(int, input().split())) s = list(map(int, input().split())) d = [] for i in range(m): c = 0 for j in s: if j % w[i] == 0: c += 1 d.append((c, i+1)) d.sort() x = d[0][0] ans = [d[0][1]] for i in d[1:]: if i[0] == x: ans.append(i[1]) print(len(ans)) print(' '.join(map(str, ans)))
School Team Contest 1 (Winter Computer School 2010/11)
ICPC
2,010
2
64
Pacifist frogs
Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much. One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to n and the number of a hill is equal to the distance in meters between it and the island. The distance between the n-th hill and the shore is also 1 meter. Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is d, the frog will jump from the island on the hill d, then — on the hill 2d, then 3d and so on until they get to the shore (i.e. find itself beyond the hill n). However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible.
The first line contains three integers n, m and k (1 ≤ n ≤ 109, 1 ≤ m, k ≤ 100) — the number of hills, frogs and mosquitoes respectively. The second line contains m integers di (1 ≤ di ≤ 109) — the lengths of the frogs’ jumps. The third line contains k integers — the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces.
In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to m in the order of the jump length given in the input data.
null
null
[{"input": "5 3 5\n2 3 4\n1 2 3 4 5", "output": "2\n2 3"}, {"input": "1000000000 2 3\n2 5\n999999995 999999998 999999996", "output": "1\n2"}]
1,300
["implementation"]
35
[{"input": "5 3 5\r\n2 3 4\r\n1 2 3 4 5\r\n", "output": "2\r\n2 3\r\n"}, {"input": "1000000000 2 3\r\n2 5\r\n999999995 999999998 999999996\r\n", "output": "1\r\n2\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "2 2 1\r\n2 1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "3 2 2\r\n2 4\r\n3 2\r\n", "output": "1\r\n2\r\n"}, {"input": "10 3 6\r\n5 2 8\r\n5 6 7 8 9 10\r\n", "output": "1\r\n3\r\n"}, {"input": "10 10 9\r\n10 9 8 7 6 5 4 3 2 1\r\n10 9 8 7 5 4 3 2 1\r\n", "output": "1\r\n5\r\n"}, {"input": "20 3 5\r\n2 3 5\r\n2 5 6 10 15\r\n", "output": "1\r\n2\r\n"}, {"input": "20 4 8\r\n1 2 3 4\r\n2 4 6 8 10 12 14 16 18 20\r\n", "output": "1\r\n3\r\n"}, {"input": "10 5 5\r\n1 5 3 5 1\r\n1 6 5 7 2\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "20 10 5\r\n1 12 6 11 9 21 15 16 8 9\r\n11 13 15 2 1\r\n", "output": "7\r\n2 3 5 6 8 9 10\r\n"}, {"input": "20 10 10\r\n9 8 21 8 7 2 13 17 20 18\r\n7 16 20 3 6 1 11 18 15 17\r\n", "output": "2\r\n3 7\r\n"}, {"input": "20 10 10\r\n6 17 14 12 13 15 6 14 16 17\r\n1 6 16 14 7 8 9 12 10 2\r\n", "output": "4\r\n2 5 6 10\r\n"}, {"input": "100 30 30\r\n25 34 81 32 96 79 36 21 53 15 51 69 78 99 60 2 80 37 61 70 32 31 31 6 7 38 95 70 81 39\r\n1 50 75 8 90 69 13 57 6 4 60 19 94 52 45 42 95 88 21 22 96 2 56 61 31 78 7 62 68 72\r\n", "output": "11\r\n3 6 9 11 14 17 18 20 26 28 29\r\n"}, {"input": "1000 19 27\r\n656 162 264 790 579 786 877 998 516 247 650 150 858 281 279 549 354 353 533\r\n349 411 1 248 22 649 726 382 423 832 172 864 17 658 840 572 564 287 800 919 500 575 461 40 1000 383 624\r\n", "output": "19\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\r\n"}]
false
stdio
null
true
39/F
39
F
PyPy 3-64
TESTS
6
92
0
167476518
n,m,k = map(int,input().split()) d = list(map(int,input().split())) a = set(map(int,input().split())) minn_l = set() minn = 1e10 for i in range(m): r = n//d[i]+1 if r<minn: minn = r minn_l = set() minn_l.add(i+1) elif r==minn: minn_l.add(i+1) print(len(minn_l)) print(*sorted(minn_l))
35
124
1,638,400
172966404
n,m,k=map(int,input().split()) A=list(map(int,input().split())) K=list(map(int,input().split())) ANS=[-1]*m for i in range(m): a=A[i] score=0 for k in K: if k%a==0: score+=1 ANS[i]=score MIN=min(ANS) ALIST=[] for i in range(m): if ANS[i]==MIN: ALIST.append(i+1) print(len(ALIST)) print(*ALIST)
School Team Contest 1 (Winter Computer School 2010/11)
ICPC
2,010
2
64
Pacifist frogs
Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much. One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to n and the number of a hill is equal to the distance in meters between it and the island. The distance between the n-th hill and the shore is also 1 meter. Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is d, the frog will jump from the island on the hill d, then — on the hill 2d, then 3d and so on until they get to the shore (i.e. find itself beyond the hill n). However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible.
The first line contains three integers n, m and k (1 ≤ n ≤ 109, 1 ≤ m, k ≤ 100) — the number of hills, frogs and mosquitoes respectively. The second line contains m integers di (1 ≤ di ≤ 109) — the lengths of the frogs’ jumps. The third line contains k integers — the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces.
In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to m in the order of the jump length given in the input data.
null
null
[{"input": "5 3 5\n2 3 4\n1 2 3 4 5", "output": "2\n2 3"}, {"input": "1000000000 2 3\n2 5\n999999995 999999998 999999996", "output": "1\n2"}]
1,300
["implementation"]
35
[{"input": "5 3 5\r\n2 3 4\r\n1 2 3 4 5\r\n", "output": "2\r\n2 3\r\n"}, {"input": "1000000000 2 3\r\n2 5\r\n999999995 999999998 999999996\r\n", "output": "1\r\n2\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "2 2 1\r\n2 1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "3 2 2\r\n2 4\r\n3 2\r\n", "output": "1\r\n2\r\n"}, {"input": "10 3 6\r\n5 2 8\r\n5 6 7 8 9 10\r\n", "output": "1\r\n3\r\n"}, {"input": "10 10 9\r\n10 9 8 7 6 5 4 3 2 1\r\n10 9 8 7 5 4 3 2 1\r\n", "output": "1\r\n5\r\n"}, {"input": "20 3 5\r\n2 3 5\r\n2 5 6 10 15\r\n", "output": "1\r\n2\r\n"}, {"input": "20 4 8\r\n1 2 3 4\r\n2 4 6 8 10 12 14 16 18 20\r\n", "output": "1\r\n3\r\n"}, {"input": "10 5 5\r\n1 5 3 5 1\r\n1 6 5 7 2\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "20 10 5\r\n1 12 6 11 9 21 15 16 8 9\r\n11 13 15 2 1\r\n", "output": "7\r\n2 3 5 6 8 9 10\r\n"}, {"input": "20 10 10\r\n9 8 21 8 7 2 13 17 20 18\r\n7 16 20 3 6 1 11 18 15 17\r\n", "output": "2\r\n3 7\r\n"}, {"input": "20 10 10\r\n6 17 14 12 13 15 6 14 16 17\r\n1 6 16 14 7 8 9 12 10 2\r\n", "output": "4\r\n2 5 6 10\r\n"}, {"input": "100 30 30\r\n25 34 81 32 96 79 36 21 53 15 51 69 78 99 60 2 80 37 61 70 32 31 31 6 7 38 95 70 81 39\r\n1 50 75 8 90 69 13 57 6 4 60 19 94 52 45 42 95 88 21 22 96 2 56 61 31 78 7 62 68 72\r\n", "output": "11\r\n3 6 9 11 14 17 18 20 26 28 29\r\n"}, {"input": "1000 19 27\r\n656 162 264 790 579 786 877 998 516 247 650 150 858 281 279 549 354 353 533\r\n349 411 1 248 22 649 726 382 423 832 172 864 17 658 840 572 564 287 800 919 500 575 461 40 1000 383 624\r\n", "output": "19\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\r\n"}]
false
stdio
null
true
616/B
616
B
PyPy 3
TESTS
5
108
0
109333713
cases, b = map(int, input().split()) st = set() while cases: cases -= 1 arr = list(map(int, input().split())) st.add(min(arr)) lst = sorted(st) if len(lst) == 1: print(*lst) else: print(lst[1])
16
31
102,400
217149912
arr=list(map(int, input().split())) n=arr[0] m=arr[1] less=[] for i in range(0,n): street=list(map(int, input().split())) less.append(min(street)) more=max(less) print(more)
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
729/D
729
D
Python 3
TESTS
6
171
9,420,800
87485729
"Codeforces Round #384 (Div. 2)" "C. Vladik and fractions" # y=int(input()) # a=y # b=a+1 # c=y*b # if y==1: # print(-1) # else: # print(a,b,c) "Technocup 2017 - Elimination Round 2" "D. Sea Battle" n,a,b,k=map(int,input().split()) s=list(input()) # n=len(s) lz=[] zeros=[] indexes=[] flage=0 if s[0]=="0": lz.append(0) flage=1 for i in range(1,n): if flage==1 and s[i]=="1": zeros.append(i-1-(lz[-1])+1) lz.append(i-1) flage=0 elif flage==0 and s[i]=="0": lz.append(i) flage=1 if s[-1]=="0": zeros.append(n-1-(lz[-1])+1) lz.append(n-1) min_no_spaces=(a-1)*b spaces_left=n-k l=len(lz) # print(lz) # print(zeros) shotes=0 for i in range(len(zeros)): h=i*2 if min_no_spaces!=0: # print(min_no_spaces) if min_no_spaces>zeros[i]: min_no_spaces-=int(zeros[i]/b) elif min_no_spaces<zeros[i]: shotes+=int((zeros[i]-min_no_spaces)/b) for j in range(int((zeros[i]-min_no_spaces)/b)): indexes.append(lz[h]+((j+1)*b)) min_no_spaces=0 elif min_no_spaces==0: # print(min_no_spaces) shotes+=int(zeros[i]/b) for j in range(int(zeros[i]/b)): indexes.append(lz[h]+((j+1)*b)) print(shotes) for i in indexes: print(i," ",end="",sep="")
21
93
16,179,200
215422065
n,a,b,k = map(int, input().split()) s = input() possible = [] i = 0 j = 0 while i<len(s): if s[i] == "1": j = 0 else : j += 1 if(j%b) == 0: possible += [i+1] j = 0 i += 1 possible = possible[a-1:] print(len(possible)) print(*possible)
Technocup 2017 - Elimination Round 2
CF
2,016
1
256
Sea Battle
Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement.
The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string.
In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them.
null
There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
[{"input": "5 1 2 1\n00100", "output": "2\n4 2"}, {"input": "13 3 2 3\n1000000010001", "output": "2\n7 11"}]
1,700
["constructive algorithms", "greedy", "math"]
21
[{"input": "5 1 2 1\r\n00100\r\n", "output": "2\r\n2 5 \r\n"}, {"input": "13 3 2 3\r\n1000000010001\r\n", "output": "2\r\n3 5 \r\n"}, {"input": "1 1 1 0\r\n0\r\n", "output": "1\r\n1 \r\n"}, {"input": "2 2 1 0\r\n00\r\n", "output": "1\r\n1 \r\n"}, {"input": "5 4 1 0\r\n00000\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "10 2 2 0\r\n0000000000\r\n", "output": "4\r\n2 4 6 8 \r\n"}, {"input": "20 1 3 5\r\n01001010000000010010\r\n", "output": "2\r\n10 13 \r\n"}, {"input": "100 17 4 11\r\n0100000100000000000000001000000000010001100000000000101000000000000000000000001000001000010000000000\r\n", "output": "2\r\n6 12 \r\n"}]
false
stdio
null
true
31/C
31
C
Python 3
TESTS
8
122
1,024,000
144745125
N = int(input()) allMeetings = [None] * N for i in range(N): inLine = input().split(" ") start, finish = int(inLine[0]), int(inLine[1]) allMeetings[i] = (start, finish, i+1) allMeetings.sort() overlaps = [set() for _ in range(N)] totalOverlaps = 0 for i in range(N): start, finish, meetingNum = allMeetings[i] j = i+1 while(j < N and allMeetings[j][0] < finish): overlaps[meetingNum-1].add(allMeetings[j][2]) overlaps[allMeetings[j][2]-1].add(meetingNum) totalOverlaps += 1 j += 1 if(totalOverlaps == 0): print(N) allRooms = [str(i+1) for i in range(N)] print(" ".join(allRooms)) else: resSet = set() for overlapSet in overlaps: if(len(overlapSet) == 1): resSet.add(overlapSet.pop()) res = sorted(resSet) print(len(res)) print(" ".join([str(num) for num in res]))
30
122
614,400
144790048
""" CP 2.B - Schedule Allan Lago Vjudge Username: alago1 """ n = int(input().strip()) lessons = [] keypoints = [] for i in range(n): s, e = tuple(int(x) for x in input().strip().split()) lessons.append((s, e)) keypoints.append((s, 1)) keypoints.append((e, -1)) keypoints.sort() k, start, end = (0,)*3 for t, dk in keypoints: k += dk if k > 2: print('0') exit(0) if k == 2: start = t if not start else start end = t out = [] for i, lesson in enumerate(lessons): ls, le = lesson if not start or start >= ls and end < le: out.append(i+1) print(len(out)) print(*out)
Codeforces Beta Round 31 (Div. 2, Codeforces format)
CF
2,010
2
256
Schedule
At the beginning of the new semester there is new schedule in the Berland State University. According to this schedule, n groups have lessons at the room 31. For each group the starting time of the lesson and the finishing time of the lesson are known. It has turned out that it is impossible to hold all lessons, because for some groups periods of their lessons intersect. If at some moment of time one groups finishes it's lesson, and the other group starts the lesson, their lessons don't intersect. The dean wants to cancel the lesson in one group so that no two time periods of lessons of the remaining groups intersect. You are to find all ways to do that.
The first line contains integer n (1 ≤ n ≤ 5000) — amount of groups, which have lessons in the room 31. Then n lines follow, each of them contains two integers li ri (1 ≤ li < ri ≤ 106) — starting and finishing times of lesson of the i-th group. It is possible that initially no two lessons intersect (see sample 1).
Output integer k — amount of ways to cancel the lesson in exactly one group so that no two time periods of lessons of the remaining groups intersect. In the second line output k numbers — indexes of groups, where it is possible to cancel the lesson. Groups are numbered starting from 1 in the order that they were given in the input. Output the numbers in increasing order.
null
null
[{"input": "3\n3 10\n20 30\n1 3", "output": "3\n1 2 3"}, {"input": "4\n3 10\n20 30\n1 3\n1 39", "output": "1\n4"}, {"input": "3\n1 5\n2 6\n3 7", "output": "0"}]
1,700
["implementation"]
30
[{"input": "3\r\n3 10\r\n20 30\r\n1 3\r\n", "output": "3\r\n1 2 3 "}, {"input": "4\r\n3 10\r\n20 30\r\n1 3\r\n1 39\r\n", "output": "1\r\n4 "}, {"input": "3\r\n1 5\r\n2 6\r\n3 7\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5\r\n5 7\r\n6 9\r\n9 10\r\n", "output": "2\r\n2 3 "}, {"input": "11\r\n717170 795210\r\n866429 970764\r\n163324 322182\r\n677099 717170\r\n241684 393937\r\n50433 114594\r\n970764 997956\r\n393937 664883\r\n235698 241684\r\n795210 832346\r\n114594 232438\r\n", "output": "1\r\n3 "}, {"input": "16\r\n203671 381501\r\n58867 59732\r\n817520 962123\r\n125391 163027\r\n601766 617692\r\n381501 444610\r\n761937 817520\r\n16 10551\r\n21096 38291\r\n718073 761937\r\n583868 601766\r\n554859 731755\r\n678098 718073\r\n962123 992003\r\n163027 203671\r\n87917 96397\r\n", "output": "1\r\n12 "}]
false
stdio
null
true
754/C
754
C
PyPy 3
TESTS
0
93
0
59612774
t = int(input()) for g in range(t): n = int(input()) s = input().split() q = int(input()) x = [] for i in range(q): x += [input().split(':')] for i in x: if(i[0] != '?' and i[0] in s): index = s.index(i[0]) s = s[:index]+s[index+1:] for i in x: if(i[0] == '?'): for j in range(len(s)): if(s[j] not in i[1]): i[0] = s[j] s = s[:j]+s[j+1:] break count = 0 for i in x: if(i[0] != '?'): count += 1 if(count == len(x)): for i in x: print(i[0] + ": " + i[1]) else: print("Impossible")
104
218
1,126,400
42141068
import re def proc(msgs): chg = False for i, msg in enumerate(msgs): if msg[0] != '?': continue ppn = msg[1] if i > 0: ppn.discard(msgs[i-1][0]) if i != len(msgs)-1: ppn.discard(msgs[i+1][0]) if len(ppn) == 1: msg[0] = ppn.pop() chg = True return chg def is_valid(msgs): for i, msg in enumerate(msgs): if msg[0] == '?' or (i > 0 and msg[0] == msgs[i-1][0]) or (i != len(msgs)-1 and msg[0] == msgs[i+1][0]): return False return True TC = int(input()) for tc in range(0, TC): N = int(input()) persons = set(input().split()) M = int(input()) msgs = [] for m in range(M): line = input() m1 = re.match(r'(.*)\:(.*)', line) ppn = set(persons) for un in re.findall(r'[a-zA-Z0-9]+', m1.group(2)): ppn.discard(un) msgs.append([m1.group(1), ppn, m1.group(2)]) while True: if proc(msgs): continue chgt = False for msg in msgs: if msg[0] == '?' and len(msg[1]) > 0: msg[0] = msg[1].pop() chgt = True break if not chgt: break if is_valid(msgs): for msg in msgs: print('%s:%s' % (msg[0], msg[2])) else: print('Impossible') # Made By Mostafa_Khaled
Codeforces Round 390 (Div. 2)
CF
2,017
2
256
Vladik and chat
Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages. Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages. He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!
The first line contains single integer t (1 ≤ t ≤ 10) — the number of chats. The t chats follow. Each chat is given in the following format. The first line of each chat description contains single integer n (1 ≤ n ≤ 100) — the number of users in the chat. The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer m (1 ≤ m ≤ 100) — the number of messages in the chat. The next m line contain the messages in the following formats, one per line: - <username>:<text> — the format of a message with known sender. The username should appear in the list of usernames of the chat. - <?>:<text> — the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha". It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.
Print the information about the t chats in the following format: If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print m messages in the following format: <username>:<text> If there are multiple answers, print any of them.
null
null
[{"input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "output": "netman: Hello, Vladik!\nVladik: Hi"}, {"input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "output": "Impossible"}, {"input": "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course.", "output": "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course."}]
2,200
["brute force", "constructive algorithms", "dp", "implementation", "strings"]
104
[{"input": "1\r\n2\r\nVladik netman\r\n2\r\n?: Hello, Vladik!\r\n?: Hi\r\n", "output": "netman: Hello, Vladik!\r\nVladik: Hi\r\n"}, {"input": "1\r\n2\r\nnetman vladik\r\n3\r\nnetman:how are you?\r\n?:wrong message\r\nvladik:im fine\r\n", "output": "Impossible\r\n"}, {"input": "2\r\n3\r\nnetman vladik Fedosik\r\n2\r\n?: users are netman, vladik, Fedosik\r\nvladik: something wrong with this chat\r\n4\r\nnetman tigerrrrr banany2001 klinchuh\r\n4\r\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\r\nklinchuh: yes, coach!\r\n?: yes, netman\r\nbanany2001: yes of course.\r\n", "output": "Impossible\r\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\r\nklinchuh: yes, coach!\r\ntigerrrrr: yes, netman\r\nbanany2001: yes of course.\r\n"}, {"input": "1\r\n1\r\nb\r\n1\r\nb:lala!\r\n", "output": "b:lala!\r\n"}, {"input": "1\r\n1\r\nb\r\n1\r\n?:lala b!\r\n", "output": "Impossible\r\n"}, {"input": "1\r\n1\r\nb\r\n2\r\n?:lala hhe!\r\nb:wat?\r\n", "output": "Impossible\r\n"}, {"input": "1\r\n3\r\nA B C\r\n3\r\nA: HI\r\n?: HI\r\nB: HI\r\n", "output": "A: HI\r\nC: HI\r\nB: HI\r\n"}]
false
stdio
null
true
754/C
754
C
PyPy 3
TESTS
3
124
0
59613844
t = int(input()) for g in range(t): n = int(input()) s = input().split() orig = s q = int(input()) x = [] for i in range(q): x += [input().split(':')] last = "xxxxxxxxx" for i in range(len(x)): if(x[i][0]=='?'): for j in s: if(j not in x[i][1] and j != last and i+1 < len(x) and j != x[i+1][0]): x[i][0] = j break last = x[i][0] if(x[-1][0] == '?'): for j in s: if j not in x[-1][1] and j != x[-2][0]: x[-1][0] = j break count = 0 for i in x: if(i[0] in s and i[0] != '?'): count += 1 if(count == len(x)): for i in x: index = 0 while(index < len(i[1]) and i[1][index]==' '): index += 1 print(i[0]+": "+i[1][index:]) else: print("Impossible")
104
296
4,812,800
92426123
import re t = int(input()) for _ in range(t): n = int(input()) names = set(input().split()) m = int(input()) dp = [] a = [] for i in range(m): a.append(input()) for i in range(m): sender, msg = a[i].split(':') ls = set(filter(None, re.split('\W+',msg))) dp.append((names if sender == '?' else set([sender])) - ls) if i and len(dp[i-1]) == 1: dp[i] -= dp[i-1] # print(dp[i]); if any([len(p) == 0 for p in dp]): print("Impossible") else: res = [] for i in reversed(range(m)): res.append(dp[i].pop()) if i > 0: dp[i-1].discard(res[-1]) for i in range(m): sender, msg = a[i].split(':') sender = res[m-i-1] print(sender+':'+msg)
Codeforces Round 390 (Div. 2)
CF
2,017
2
256
Vladik and chat
Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages. Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages. He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!
The first line contains single integer t (1 ≤ t ≤ 10) — the number of chats. The t chats follow. Each chat is given in the following format. The first line of each chat description contains single integer n (1 ≤ n ≤ 100) — the number of users in the chat. The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer m (1 ≤ m ≤ 100) — the number of messages in the chat. The next m line contain the messages in the following formats, one per line: - <username>:<text> — the format of a message with known sender. The username should appear in the list of usernames of the chat. - <?>:<text> — the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha". It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.
Print the information about the t chats in the following format: If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print m messages in the following format: <username>:<text> If there are multiple answers, print any of them.
null
null
[{"input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "output": "netman: Hello, Vladik!\nVladik: Hi"}, {"input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "output": "Impossible"}, {"input": "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course.", "output": "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course."}]
2,200
["brute force", "constructive algorithms", "dp", "implementation", "strings"]
104
[{"input": "1\r\n2\r\nVladik netman\r\n2\r\n?: Hello, Vladik!\r\n?: Hi\r\n", "output": "netman: Hello, Vladik!\r\nVladik: Hi\r\n"}, {"input": "1\r\n2\r\nnetman vladik\r\n3\r\nnetman:how are you?\r\n?:wrong message\r\nvladik:im fine\r\n", "output": "Impossible\r\n"}, {"input": "2\r\n3\r\nnetman vladik Fedosik\r\n2\r\n?: users are netman, vladik, Fedosik\r\nvladik: something wrong with this chat\r\n4\r\nnetman tigerrrrr banany2001 klinchuh\r\n4\r\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\r\nklinchuh: yes, coach!\r\n?: yes, netman\r\nbanany2001: yes of course.\r\n", "output": "Impossible\r\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\r\nklinchuh: yes, coach!\r\ntigerrrrr: yes, netman\r\nbanany2001: yes of course.\r\n"}, {"input": "1\r\n1\r\nb\r\n1\r\nb:lala!\r\n", "output": "b:lala!\r\n"}, {"input": "1\r\n1\r\nb\r\n1\r\n?:lala b!\r\n", "output": "Impossible\r\n"}, {"input": "1\r\n1\r\nb\r\n2\r\n?:lala hhe!\r\nb:wat?\r\n", "output": "Impossible\r\n"}, {"input": "1\r\n3\r\nA B C\r\n3\r\nA: HI\r\n?: HI\r\nB: HI\r\n", "output": "A: HI\r\nC: HI\r\nB: HI\r\n"}]
false
stdio
null
true
87/B
87
B
PyPy 3
TESTS
3
248
0
61057097
class CodeforcesTask87BSolution: def __init__(self): self.result = '' self.n = 0 self.expressions = [] def read_input(self): self.n = int(input()) for x in range(self.n): self.expressions.append(input().split(" ")) def process_task(self): result = [] lang_dict = {"void":0} for expr in self.expressions: if expr[0] == "typedef": up = expr[1].count("*") down = expr[1].count("&") nm = expr[1].replace("*", "").replace("&", "") if nm in lang_dict: if lang_dict[nm] == -1: lang_dict[expr[2]] = -1 else: lang_dict[expr[2]] = max(-1, lang_dict[nm] + up - down) else: lang_dict[expr[2]] = -1 elif expr[0] == "typeof": up = expr[1].count("*") down = expr[1].count("&") nm = expr[1].replace("*", "").replace("&", "") if nm in lang_dict: balance = max(lang_dict[nm] + up - down, -1) if balance == -1: result.append("errtype") else: result.append("void" + "*" * balance) else: result.append("errtype") self.result = "\n".join(result) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask87BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
54
156
6,144,000
34230774
from collections import * import sys,re lines = [i[:-1] for i in sys.stdin.readlines()] n = int(lines[0]) lines = lines[1:] typeof = defaultdict(lambda:'errtype') typeof['void'] = 'void' def simplify(s): global typeof l,r = s.count('&'),s.count('*') result = typeof[s[l:len(s)-r]] #print(l,r,result,s[l:len(s)-r]) if(result == 'errtype'): return result for i in range(r): result = result+'*' for i in range(l): if(result[-1] == '*'): result = result[:-1] else: return 'errtype' return result #print(simplify('void*')) typeof['ptv'] = simplify('void*') #print(typeof['ptv']) #print(simplify('ptv')) #exit() for line in lines: commands = line.split() if(commands[0] == 'typeof'): print(simplify(commands[1])) else: typeof[commands[2]] = simplify(commands[1]) #print(typeof['ptv'])
Codeforces Beta Round 73 (Div. 1 Only)
CF
2,011
1
256
Vasya and Types
Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages. There is a very powerful system of pointers on &K* — you can add an asterisk to the right of the existing type X — that will result in new type X * . That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of X, which is a pointer, you can add an ampersand — that will result in a type &X, to which refers X. That is called a dereference operation. The &K* language has only two basic data types — void and errtype. Also, the language has operators typedef and typeof. - The operator "typedef A B" defines a new data type B, which is equivalent to A. A can have asterisks and ampersands, and B cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**. - The operator "typeof A" returns type of A, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**. An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype* = &errtype = errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype. Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change. Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &T * is always equal to T. Note, that the operators are executed consecutively one by one. If we have two operators "typedef &void a" and "typedef a* b", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &void* = void (see sample 2). Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of operators. Then follow n lines with operators. Each operator is of one of two types: either "typedef A B", or "typeof A". In the first case the B type differs from void and errtype types, and besides, doesn't have any asterisks and ampersands. All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.
For every typeof operator print on the single line the answer to that operator — the type that the given operator returned.
null
Let's look at the second sample. After the first two queries typedef the b type is equivalent to void*, and c — to void**. The next query typedef redefines b — it is now equal to &b = &void* = void. At that, the c type doesn't change. After that the c type is defined as &&b* = &&void* = &void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &void* = void. Then the b type is again redefined as &void = errtype. Please note that the c type in the next query is defined exactly as errtype******* = errtype, and not &void******* = void******. The same happens in the last typedef.
[{"input": "5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv", "output": "void*\nerrtype\nvoid"}, {"input": "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &&b* c\ntypeof c", "output": "void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype"}]
1,800
["implementation", "strings"]
54
[{"input": "5\r\ntypedef void* ptv\r\ntypeof ptv\r\ntypedef &&ptv node\r\ntypeof node\r\ntypeof &ptv\r\n", "output": "void*\r\nerrtype\r\nvoid\r\n"}, {"input": "17\r\ntypedef void* b\r\ntypedef b* c\r\ntypeof b\r\ntypeof c\r\ntypedef &b b\r\ntypeof b\r\ntypeof c\r\ntypedef &&b* c\r\ntypeof c\r\ntypedef &b* c\r\ntypeof c\r\ntypedef &void b\r\ntypeof b\r\ntypedef b******* c\r\ntypeof c\r\ntypedef &&b* c\r\ntypeof c\r\n", "output": "void*\r\nvoid**\r\nvoid\r\nvoid**\r\nerrtype\r\nvoid\r\nerrtype\r\nerrtype\r\nerrtype\r\n"}, {"input": "10\r\ntypeof void\r\ntypedef void voiddd\r\ntypeof &&&&&voiddd*********\r\ntypeof &&&&&voidddd*********\r\ntypedef aaaa bbbb\r\ntypeof bbbb\r\ntypeof aaaa\r\ntypedef void** aaaa\r\ntypeof aaaa\r\ntypeof bbbb\r\n", "output": "void\r\nvoid****\r\nerrtype\r\nerrtype\r\nerrtype\r\nvoid**\r\nerrtype\r\n"}, {"input": "15\r\ntypedef &void pt\r\ntypeof pt\r\ntypeof pt*\r\ntypedef pt**** err\r\ntypeof err\r\ntypeof &err\r\ntypeof err*\r\ntypedef &void*** pt\r\ntypeof err\r\ntypeof &err\r\ntypeof err*\r\ntypeof pt\r\ntypeof pt*\r\ntypeof &&pt*\r\ntypeof &&&pt\r\n", "output": "errtype\r\nerrtype\r\nerrtype\r\nerrtype\r\nerrtype\r\nerrtype\r\nerrtype\r\nerrtype\r\nvoid**\r\nvoid***\r\nvoid*\r\nerrtype\r\n"}, {"input": "1\r\ntypeof a\r\n", "output": "errtype\r\n"}, {"input": "2\r\ntypedef void errtypea\r\ntypeof errtypea\r\n", "output": "void\r\n"}, {"input": "4\r\ntypedef void voida\r\ntypedef voida* voidb\r\ntypedef voidb* voidc\r\ntypeof voidc\r\n", "output": "void**\r\n"}]
false
stdio
null
true
616/B
616
B
PyPy 3
TESTS
5
77
0
109333829
cases, b = map(int, input().split()) st = set() while cases: cases -= 1 arr = sorted(map(int, input().split())) st.add(arr[0]) lst = sorted(st) if len(lst) == 1: print(*lst) else: print(lst[1])
16
31
204,800
152545117
n, m = map(int, input().split()) lis = [] for _ in range(n): lis.append(list(map(int, input().split()))) c = 0 s = 0 for i in range(len(lis)): if min(lis[i]) > c: c = min(lis[i]) s = i print(min(lis[s]))
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
616/B
616
B
PyPy 3
TESTS
5
124
0
81440430
n,m=map(int,input().split()) s=[] r=[] k=[] l=[] for j in range(1,n+1): listj=list(map(int,input().split())) a=max(listj) b=min(listj) s.append(a) r.append(b) t=min(s) k.append(t) w=max(r) k.append(w) h=min(k) print(h)
16
31
409,600
208839031
n, m = map(int, input().split()) b = [] mins=[] asdf=[] for i in range(n): c = list(map(int, input().split())) mins.append(min(c)) b.append(c) p = max(mins) for j in range(n): if mins[j]==p: a=j break for k in range(m): z = b[a][k] asdf.append(z) print(min(asdf))
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
31/D
31
D
PyPy 3-64
TESTS
5
122
0
214306244
import sys readline = sys.stdin.readline w, h, n = [int(w) for w in readline().split()] rect = [0, 0, w, h ] vcut = [] hcut = [] for _ in range(n): x1, y1, x2, y2 = [int(w) for w in readline().split()] if x1 == x2: if y1 > y2: y2, y1 = y1, y2 hcut.append([y2 - y1, x1, y1, x2, y2]) else: if x1 > x2: x1, x2 = x2, x1 vcut.append([x2 - x1, x1, y1, x2, y2]) vcut.sort() hcut.sort() # print(vcut, hcut) def dfs(rect:list, vcut:list, hcut:list) -> list: # print(rect, vcut, hcut) if not vcut and not hcut: return [(rect[2] - rect[0]) * (rect[3] - rect[1])] result = [] xi1, yi1, xi2, yi2 = rect if vcut: _, x1, y1, x2, y2 = vcut[-1] if xi1 == x1 and xi2 == x2: vcut.pop() result = dfs([xi1, yi1, xi2, y2], [c for c in vcut if c[-1] <= y1], [c for c in hcut if c[-1]<= y1]) result.extend(dfs([xi1, y1, xi2, yi2], [c for c in vcut if c[-1] > y1], [c for c in hcut if c[-1] > 1])) return result if hcut: _, x1, y1, x2, y2 = hcut[-1] if yi1 == y1 and yi2 == y2: hcut.pop() # left : [xi1, yi1, x2, yi2], right : [x1, yi1, xi2, yi2] result = dfs([xi1, yi1, x2, yi2], [c for c in vcut if c[-2] <= x1], [c for c in hcut if c[-2] <= x1]) result.extend(dfs([x1, yi1, xi2, yi2], [c for c in vcut if c[-2] > x1], [c for c in hcut if c[-2] > x1])) return result result = dfs(rect, vcut, hcut) result.sort() print(*result)
33
218
307,200
85902665
WHn = [[0,0]] # WHn[0].extend(list(map(int, input().split(' ')))) WHn[0].extend([int(x) for x in input().split(' ')]) n = range(WHn[0][-1]) lines = [] lines_aux = [] for i in n: lines.append( [int(x) for x in input().split(' ')] ) lines_aux.append(True) while any(lines_aux): for i in n: if lines_aux[i]: for unidad in WHn: rangex = range(unidad[0], unidad[2]+1) rangey = range(unidad[1], unidad[3]+1) if (lines[i][0] in rangex) and (lines[i][3] in rangey) and (lines[i][2] in rangex) and (lines[i][1] in rangey) and\ ( (lines[i][0:3:2] == unidad[0:3:2]) or (lines[i][1:4:2] == unidad[1:4:2]) ): WHn.append([unidad[0],unidad[1],lines[i][2],lines[i][3]]) WHn.append([lines[i][0],lines[i][1],unidad[2],unidad[3]]) WHn.remove(unidad) lines_aux[i] = False break for i,unidad in enumerate(WHn): WHn[i] = (unidad[2] - unidad[0])*(unidad[3] - unidad[1]) WHn.sort() result = '' for i in WHn: result += '{} ' print(result[:-1].format(*WHn))
Codeforces Beta Round 31 (Div. 2, Codeforces format)
CF
2,010
2
256
Chocolate
Bob has a rectangular chocolate bar of the size W × H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
The first line contains 3 integers W, H and n (1 ≤ W, H, n ≤ 100) — width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 — coordinates of the endpoints of the i-th break (0 ≤ xi, 1 ≤ xi, 2 ≤ W, 0 ≤ yi, 1 ≤ yi, 2 ≤ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order. It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output n + 1 numbers — areas of the resulting parts in the increasing order.
null
null
[{"input": "2 2 2\n1 0 1 2\n0 1 1 1", "output": "1 1 2"}, {"input": "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1", "output": "1 1 1 1"}, {"input": "2 4 2\n0 1 2 1\n0 3 2 3", "output": "2 2 4"}]
2,000
["dfs and similar", "implementation"]
33
[{"input": "2 2 2\r\n1 0 1 2\r\n0 1 1 1\r\n", "output": "1 1 2 "}, {"input": "2 2 3\r\n1 0 1 2\r\n0 1 1 1\r\n1 1 2 1\r\n", "output": "1 1 1 1 "}, {"input": "2 4 2\r\n0 1 2 1\r\n0 3 2 3\r\n", "output": "2 2 4 "}, {"input": "5 5 3\r\n2 1 2 5\r\n0 1 5 1\r\n4 0 4 1\r\n", "output": "1 4 8 12 "}, {"input": "10 10 4\r\n9 0 9 10\r\n4 0 4 10\r\n1 0 1 10\r\n1 4 4 4\r\n", "output": "10 10 12 18 50 "}, {"input": "9 8 5\r\n4 3 4 4\r\n0 4 9 4\r\n5 4 5 8\r\n0 3 9 3\r\n1 4 1 8\r\n", "output": "4 4 5 16 16 27 "}, {"input": "100 100 1\r\n0 14 100 14\r\n", "output": "1400 8600 "}]
false
stdio
null
true
482/B
482
B
PyPy 3-64
TESTS
2
77
3,481,600
223640236
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb alph = 'abcdefghijklmnopqrstuvwxyz' #pow(x,mod-2,mod) N,M = map(int,input().split()) P,h = [],[[] for _ in range(N)] for _ in range(M): l,r,q = map(int,input().split()) P.append([l,r,q]) h[l-1].append(q) if r<N:h[r].append(-q) P.sort() cnt = [[0]*31 for _ in range(N)] ans = [] l = [0]*31 for x in range(N): for i in h[x]: for j in range(31): if i&2**j==2**j: if i<0:l[j]-=1 else:l[j]+=1 num = 0 for i in range(31): if l[i]>=1: num+=2**i cnt[x][i]+=1+cnt[x-1][i] ans.append(num) # for i in cnt:print(*i) check = True for l,r,q in P: numi = 0 for i in range(31): num = cnt[r-1][i] if l-2>=0:num-=cnt[l-2][i] if num==r-l+1:numi+=2**i if numi!=q: check = False break if check==True: print("YES") print(*ans) else: print("NO")
38
514
38,195,200
170446786
import sys input = sys.stdin.readline n, m = map(int, input().split()) ps = [[0] * (n + 2) for _ in range(30)] l, r, q, res = [0] * m, [0] * m, [0] * m, 'YES' for i in range(m): l[i], r[i], q[i] = map(int, input().split()) for i in range(m): for j in range(30): d = (1 << j) & q[i] ps[j][l[i]] += d ps[j][r[i] + 1] -= d for i in range(30): for j in range(2, n + 1): ps[i][j] += ps[i][j - 1] for i in range(30): for j in range(1, n + 1): ps[i][j] = 1 if ps[i][j] >= 1 else 0 for i in range(30): for j in range(2, n + 1): ps[i][j] += ps[i][j - 1] for i in range(m): for j in range(30): d = (1 << j) & q[i] if d == 0 and ps[j][r[i]] - ps[j][l[i] - 1] == r[i] - l[i] + 1 or \ d == 1 and ps[j][r[i]] - ps[j][l[i] - 1] < r[i] - l[i] + 1: res = 'NO' break if res == 'NO': break print(res) if res == 'YES': arr = [0] * n for i in range(30): for j in range(n): if ps[i][j + 1] - ps[i][j]: arr[j] += 1 << i print(*arr)
Codeforces Round 275 (Div. 1)
CF
2,014
1
256
Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value $$a[l_{i}] \& a[l_{i}+1] \& \ldots \& a[r_{i}]$$ should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".
The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
null
null
[{"input": "3 1\n1 3 3", "output": "YES\n3 3 3"}, {"input": "3 2\n1 3 3\n1 3 2", "output": "NO"}]
1,800
["constructive algorithms", "data structures", "trees"]
38
[{"input": "3 1\r\n1 3 3\r\n", "output": "YES\r\n3 3 3\r\n"}, {"input": "3 2\r\n1 3 3\r\n1 3 2\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n1 2 536870912\r\n2 3 536870911\r\n", "output": "YES\r\n536870912 1073741823 536870911\r\n"}, {"input": "1 1\r\n1 1 10\r\n", "output": "YES\r\n10\r\n"}, {"input": "1 2\r\n1 1 10\r\n1 1 5\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n1 1 1\r\n1 1 3\r\n", "output": "NO\r\n"}]
false
stdio
null
true
616/B
616
B
PyPy 3-64
TESTS
3
46
0
170883881
import sys readline=sys.stdin.readline N,M=map(int,readline().split()) C=[list(map(int,readline().split())) for n in range(N)] inf=1<<30 ans=-inf for n in range(N): ans=max(ans,min(C[n][m] for m in range(M))) for m in range(M): ans=max(ans,min(C[n][m] for n in range(N))) print(ans)
16
31
512,000
192492228
# LUOGU_RID: 101606881 print(max(min(x) for x in [map(int, s.split()) for s in [*open(0)][1:]]))
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
616/B
616
B
Python 3
TESTS
4
62
6,758,400
130598113
n, m = map(int, input().split()) c = [] for i in range(n): c += [min(list(input().split()))] print(max(c))
16
31
512,000
217145495
n,m=list(map(int,input().split())) arr=[] costlist=[] for i in range(n): l1=list(map(int,input().split())) arr.append(l1) for j in arr: mincost=min(j) costlist.append(mincost) cost=max(costlist) print(cost)
Educational Codeforces Round 5
ICPC
2,016
1
256
Dinner with Emma
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
Print the only integer a — the cost of the dinner for Jack and Emma.
null
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[{"input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2"}, {"input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1"}]
1,000
["games", "greedy"]
16
[{"input": "3 4\r\n4 1 3 5\r\n2 2 2 2\r\n5 4 5 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n2 3 1\r\n3 1 2\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 10\r\n74 35 82 39 1 84 29 41 70 12\r\n", "output": "1\r\n"}, {"input": "10 1\r\n44\r\n23\r\n65\r\n17\r\n48\r\n29\r\n49\r\n88\r\n91\r\n85\r\n", "output": "91\r\n"}, {"input": "10 10\r\n256 72 455 45 912 506 235 68 951 92\r\n246 305 45 212 788 621 449 876 459 899\r\n732 107 230 357 370 610 997 669 61 192\r\n131 93 481 527 983 920 825 540 435 54\r\n777 682 984 20 337 480 264 137 249 502\r\n51 467 479 228 923 752 714 436 199 973\r\n3 91 612 571 631 212 751 84 886 948\r\n252 130 583 23 194 985 234 978 709 16\r\n636 991 203 469 719 540 184 902 503 652\r\n826 680 150 284 37 987 360 183 447 51\r\n", "output": "184\r\n"}, {"input": "1 1\r\n1000000000\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n999999999\r\n1000000000\r\n", "output": "1000000000\r\n"}]
false
stdio
null
true
731/C
731
C
PyPy 3-64
TESTS
4
62
0
209915686
import sys from collections import defaultdict, Counter input = sys.stdin.readline n, m, k = map(int, input().split()) d = defaultdict(list) c = [0] + list(map(int, input().split())) for i in range(m): u, v = map(int, input().split()) if(u > v): u, v = v, u d[u].append(v) d[v].append(u) ans = 0 v = [0] * (n + 1) for i in range(1, n + 1): if(not v[i]): v[i] = 1 q = [i] l = defaultdict(int) l[i] += 1 while(q): a = q.pop() for j in d[a]: if(not v[j]): v[j] = 1 q.append(j) l[c[j]] += 1 l = [l[j] for j in l.keys()] ans = ans + sum(l) - max(l) print(ans)
70
1,138
71,884,800
228104946
from collections import defaultdict from collections import deque def addEdge(u, v): graph[u].append(v) graph[v].append(u) def dfs(u,visited): s = deque() component = [] colors = {} s.append(u) maxOccur = 0 predColor = 0 while len(s) > 0: v = s.pop() color = C[v-1] if color not in colors: colors[color] = 1 else: colors[color] += 1 numOccur = colors[color] if numOccur > maxOccur: maxOccur = numOccur predColor = color component.append(v) visited[v] = True for neighbor in graph[v]: if not visited[neighbor]: s.append(neighbor) visited[neighbor] = True return component,predColor def connectedComponents(): visited = [False] * (n + 1) components = [] for vertex in graph: if not visited[vertex]: component,predColor = dfs(vertex, visited) components.append((component,predColor)) return components n,m,k = map(int,input().split()) C = list(map(int,input().split())) graph = defaultdict(list) for _ in range(m): u,v = map(int,input().split()) addEdge(u,v) components = connectedComponents() numPaintings = 0 for component in components: socks = component[0] predColor = component[1] for s in socks: color = C[s-1] if color != predColor: numPaintings += 1 print(numPaintings)
Codeforces Round 376 (Div. 2)
CF
2,016
2
256
Socks
Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors. When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors. Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now. The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.
The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively. The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks. Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.
Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
null
In the first sample, Arseniy can repaint the first and the third socks to the second color. In the second sample, there is no need to change any colors.
[{"input": "3 2 3\n1 2 3\n1 2\n2 3", "output": "2"}, {"input": "3 2 2\n1 1 2\n1 2\n2 1", "output": "0"}]
1,600
["dfs and similar", "dsu", "graphs", "greedy"]
70
[{"input": "3 2 3\r\n1 2 3\r\n1 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "3 2 2\r\n1 1 2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3 3 3\r\n1 2 3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "2\r\n"}, {"input": "4 2 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "10 3 2\r\n2 1 1 2 1 1 2 1 2 2\r\n4 10\r\n9 3\r\n5 7\r\n", "output": "2\r\n"}, {"input": "10 3 3\r\n2 2 1 3 1 2 1 2 2 2\r\n10 8\r\n9 6\r\n8 10\r\n", "output": "0\r\n"}, {"input": "4 3 2\r\n1 1 2 2\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "2\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n4 1\r\n", "output": "3\r\n"}]
false
stdio
null
true
732/D
732
D
PyPy 3-64
TESTS
4
92
13,721,600
218606506
import sys input = lambda: sys.stdin.readline().rstrip() import math from heapq import heappush , heappop from collections import defaultdict,deque,Counter from bisect import * N,M = map(int, input().split()) D = list(map(int, input().split())) A = list(map(int, input().split())) days = sum(A) def check(m): seen = set() B = {} for i in range(m,-1,-1): if D[i] and D[i] not in seen: B[i] = D[i]-1 seen.add(D[i]) cnt = 0 for i in range(m+1): if i in B: if cnt<A[B[i]]: return False else: cnt+=1 return True # print(check(5)) # exit(0) l,r = 0,N while l+1<r: m = (l+r)//2 #print(l,r,m) if check(m): #print('m',m,'yes') r = m else: #print('m',m,'no') l = m if r>=N: print(-1) else: print(r+1)
49
156
23,654,400
21685058
from collections import deque def main(): n, m = list(map(int, input().split())) test = list(map(int, input().split())) prepare = list(map(int, input().split())) least_days = sum(prepare) + m if least_days > len(test): print(-1) return index = list() for p in range(m + 1): index.append(deque()) for t in range(len(test)): if test[t] == 0: continue index[test[t]].append(t) if t >= least_days - 1: index[0].append(t) if deque([]) in index: print(-1) return min_index = max(list(map(lambda x: x.popleft(), index[1:]))) min_index = max(least_days - 1, min_index) for x in index[0]: if x >= min_index: print(x + 1) break if __name__ == '__main__': main()
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Exams
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects. The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i. The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
null
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
[{"input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5"}, {"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9"}, {"input": "5 1\n1 1 1 1 1\n5", "output": "-1"}]
1,700
["binary search", "greedy", "sortings"]
53
[{"input": "7 2\r\n0 1 0 2 1 0 2\r\n2 1\r\n", "output": "5\r\n"}, {"input": "10 3\r\n0 0 1 2 3 0 2 0 1 2\r\n1 1 4\r\n", "output": "9\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n5\r\n", "output": "-1\r\n"}, {"input": "100 10\r\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\r\n2 4 10 11 5 2 6 7 2 15\r\n", "output": "74\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n0 0 0\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n0 1 0 2\r\n1 1\r\n", "output": "4\r\n"}, {"input": "10 1\r\n0 1 0 0 0 0 0 0 0 1\r\n1\r\n", "output": "2\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n1\r\n", "output": "5\r\n"}, {"input": "7 2\r\n0 0 0 0 0 1 2\r\n1 1\r\n", "output": "7\r\n"}, {"input": "10 3\r\n0 0 1 2 2 0 2 0 1 3\r\n1 1 4\r\n", "output": "10\r\n"}, {"input": "6 2\r\n1 1 1 1 1 2\r\n1 1\r\n", "output": "6\r\n"}, {"input": "6 2\r\n1 0 0 0 0 2\r\n1 1\r\n", "output": "-1\r\n"}]
false
stdio
null
true
615/B
615
B
PyPy 3-64
TESTS
3
61
0
190140755
from collections import defaultdict n, m = map(int, input().split()) adj = defaultdict(list) for i in range(m): u, v = map(int, input().split()) adj[u].append(v) adj[v].append(u) tried = set() best = 0 for v in adj: if v in tried: continue seen = set() s = [v] tail_size = 0 while s: node = s.pop() tail_size += 1 seen.add(node) tried.add(node) start_length = len(s) for nxt in adj[node]: if nxt > node and nxt not in seen: seen.add(nxt) tried.add(node) s.append(nxt) if start_length == len(s): beauty = tail_size * len(adj[node]) if best < beauty: best = max(best, beauty) seen.remove(node) tail_size -= 1 print(best)
60
638
13,926,400
15249015
import sys def dfs(v): global g, links, used, tail, val used[v] = True maxv = 1 for u in g[v]: if not used[u]: # maxv = max(maxv, dfs(u) + 1) - было used вместо not used в условии dfs(u) maxv = max(maxv, tail[u] + 1) val[v] = links[v] * maxv tail[v] = maxv # return maxv # fin = open("cfr338b.in", "r") fin = sys.stdin n, m = map(int, fin.readline().split()) g = [[] for i in range(n)] links = [0] * n for i in range(m): u, v = map(int, fin.readline().split()) u, v = (u - 1, v - 1) if u <= v else (v - 1, u - 1) # g[u].append(v) g[v].append(u) links[v] += 1 links[u] += 1 used = [False] * n val, tail = [0] * n, [0] * n maxv = 0 for i in range(n): if not used[i]: dfs(i) maxv = max(maxv, val[i]) print(maxv)
Codeforces Round 338 (Div. 2)
CF
2,016
3
256
Longtail Hedgehog
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: 1. Only segments already presented on the picture can be painted; 2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 3. The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get. Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
First line of the input contains two integers n and m(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively. Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
Print the maximum possible value of the hedgehog's beauty.
null
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.
[{"input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7", "output": "9"}, {"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "12"}]
1,600
["dp", "graphs"]
60
[{"input": "8 6\r\n4 5\r\n3 5\r\n2 5\r\n1 2\r\n2 8\r\n6 7\r\n", "output": "9\r\n"}, {"input": "4 6\r\n1 2\r\n1 3\r\n1 4\r\n2 3\r\n2 4\r\n3 4\r\n", "output": "12\r\n"}, {"input": "5 7\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n", "output": "9\r\n"}, {"input": "5 9\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n1 5\r\n2 5\r\n", "output": "16\r\n"}, {"input": "10 10\r\n6 3\r\n2 9\r\n9 4\r\n4 5\r\n10 3\r\n8 3\r\n10 5\r\n7 6\r\n1 4\r\n6 8\r\n", "output": "8\r\n"}, {"input": "100 50\r\n66 3\r\n92 79\r\n9 44\r\n84 45\r\n30 63\r\n30 20\r\n33 86\r\n8 83\r\n40 75\r\n7 36\r\n91 4\r\n76 88\r\n77 76\r\n28 27\r\n6 52\r\n41 57\r\n8 23\r\n34 75\r\n50 15\r\n86 68\r\n36 98\r\n30 84\r\n37 62\r\n22 4\r\n6 45\r\n72 80\r\n98 74\r\n78 84\r\n1 54\r\n99 27\r\n84 91\r\n78 7\r\n80 61\r\n67 48\r\n51 52\r\n36 72\r\n97 87\r\n25 17\r\n20 80\r\n20 39\r\n72 5\r\n21 77\r\n48 1\r\n63 21\r\n92 45\r\n34 93\r\n28 84\r\n3 91\r\n56 99\r\n7 53\r\n", "output": "15\r\n"}, {"input": "5 8\r\n1 3\r\n2 4\r\n4 5\r\n5 3\r\n2 1\r\n1 4\r\n3 2\r\n1 5\r\n", "output": "12\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "2\r\n"}, {"input": "10 9\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n", "output": "9\r\n"}, {"input": "5 4\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n", "output": "4\r\n"}, {"input": "6 5\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n", "output": "5\r\n"}]
false
stdio
null
true
296/B
296
B
Python 3
TESTS
3
92
204,800
14680375
# not E i, j((si > wi) and (sj < wj)) # forall i, j not(si > wi) or not (sj < wj) # forall i,j (si <= wi or sj >= wj) n = int(input()) s = list(input()) w = list(input()) contS = s.count("?") contW = w.count("?") total = 10 ** (contS + contW) maiorIgual = 1 menorIgual = 1 igual = 1 for i in range(0, n): if (s[i] == '?'): if (w[i] == '?'): maiorIgual *= 55 menorIgual *= 55 igual *= 10 else: maiorIgual *= 10 - int(w[i]) menorIgual *= int(w[i]) + 1 igual *= 1 else: if (w[i] == '?'): maiorIgual = 10 - int(s[i]) menorIgual = int(s[i]) + 1 igual *= 1 else: if (s[i] > w[i]): maiorIgual *= 1 menorIgual *= 0 igual *= 0 if (s[i] == w[i]): igual *= 1 maiorIgual *= 1 menorIgual *= 1 if (s[i] < w[i]): maiorIgual *= 0 menorIgual *= 1 igual *= 0 print((total - menorIgual - maiorIgual + igual) % 1000000007)
38
374
27,443,200
129340367
p = int(1e9+7) def solv(A,B): global p n = len(A) A = [-1 if c=='?' else int(c) for c in A] B = [-1 if c=='?' else int(c) for c in B] s = [1,0,0,0] for i in range(n): if A[i]<0 and B[i]<0: si,ai,bi,ti=10,45,45,100 elif A[i]<0: si,ai,bi,ti=1,9-B[i],B[i],10 elif B[i]<0: si,ai,bi,ti=1,A[i],9-A[i],10 else: si,ai,bi,ti=(A[i]==B[i],A[i]>B[i],B[i]>A[i],1) s[3] = (s[3]*ti%p+s[1]*bi%p+s[2]*ai%p)%p s[2] = (s[2]*(bi+si)%p+s[0]*bi%p)%p s[1] = (s[1]*(ai+si)%p+s[0]*ai%p)%p s[0] = s[0]*si%p return s[3] _ = input() A = input() B = input() print(solv(A,B))
Codeforces Round 179 (Div. 2)
CF
2,013
2
256
Yaroslav and Two Strings
Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s, similarly, wj represents the j-th digit of string w. A string's template is a string that consists of digits and question marks ("?"). Yaroslav has two string templates, each of them has length n. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers. Help Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007 (109 + 7).
The first line contains integer n (1 ≤ n ≤ 105) — the length of both templates. The second line contains the first template — a string that consists of digits and characters "?". The string's length equals n. The third line contains the second template in the same format.
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).
null
The first test contains no question marks and both strings are incomparable, so the answer is 1. The second test has no question marks, but the given strings are comparable, so the answer is 0.
[{"input": "2\n90\n09", "output": "1"}, {"input": "2\n11\n55", "output": "0"}, {"input": "5\n?????\n?????", "output": "993531194"}]
2,000
["combinatorics", "dp"]
38
[{"input": "2\r\n90\r\n09\r\n", "output": "1\r\n"}, {"input": "2\r\n11\r\n55\r\n", "output": "0\r\n"}, {"input": "5\r\n?????\r\n?????\r\n", "output": "993531194\r\n"}, {"input": "10\r\n104?3?1??3\r\n?1755?1??7\r\n", "output": "91015750\r\n"}, {"input": "10\r\n6276405116\r\n6787?352?9\r\n", "output": "46\r\n"}, {"input": "10\r\n0844033584\r\n0031021311\r\n", "output": "0\r\n"}, {"input": "10\r\n???0?19?01\r\n957461????\r\n", "output": "983368000\r\n"}, {"input": "10\r\n8703870339\r\n994987934?\r\n", "output": "9\r\n"}, {"input": "10\r\n?8?528?91?\r\n45??06???1\r\n", "output": "980398000\r\n"}, {"input": "10\r\n8030456630\r\n83406?6890\r\n", "output": "5\r\n"}, {"input": "1\r\n?\r\n?\r\n", "output": "0\r\n"}, {"input": "2\r\n12\r\n?9\r\n", "output": "1\r\n"}, {"input": "3\r\n??1\r\n?12\r\n", "output": "890\r\n"}, {"input": "3\r\n?12\r\n??1\r\n", "output": "890\r\n"}, {"input": "5\r\n??15?\r\n?32??\r\n", "output": "939500\r\n"}, {"input": "5\r\n??25?\r\n?32??\r\n", "output": "812550\r\n"}, {"input": "5\r\n??55?\r\n?32??\r\n", "output": "872950\r\n"}, {"input": "5\r\n?32??\r\n??55?\r\n", "output": "872950\r\n"}]
false
stdio
null
true
613/B
613
B
Python 3
TESTS
0
31
0
15396008
__author__ = 'abdujabbor' def calculate_strength(aa, a, cf, cm): c = 0 for i in aa: if i == a: c += 1 return c * cf + min(aa) * cm n, a, cf, cm, m = [int(x) for x in input().split()] aa = sorted([int(x) for x in input().split()], key=None, reverse=True) while m >= 0: updated = False for i in range(len(aa)): if aa[i] < a and m > 0: aa[i] += 1 m -= 1 updated = True if updated is False: break m -= 1 print(calculate_strength(aa, a, cf, cm)) for i in range(len(aa)): print(aa[i], end=' ')
35
405
17,510,400
15438013
def main(): from bisect import bisect n, A, cf, cm, m = map(int, input().split()) skills = list(map(int, input().split())) xlat = sorted(range(n), key=skills.__getitem__) sorted_skills = [skills[_] for _ in xlat] bottom_lift, a, c = [], 0, 0 for i, b in enumerate(sorted_skills): c += i * (b - a) bottom_lift.append(c) a = b root_lift, a = [0], 0 for b in reversed(sorted_skills): a += A - b root_lift.append(a) max_level = -1 for A_width, a in enumerate(root_lift): if m < a: break money_left = m - a floor_width = bisect(bottom_lift, money_left) if floor_width > n - A_width: floor_width = n - A_width money_left -= bottom_lift[floor_width - 1] if floor_width > 0: floor = sorted_skills[floor_width - 1] + money_left // floor_width if floor > A: floor = A else: floor = A level = cf * A_width + cm * floor if max_level < level: max_level, save = level, (A_width, floor, floor_width) A_width, floor, floor_width = save for id in xlat[:floor_width]: skills[id] = floor for id in xlat[n - A_width:]: skills[id] = A print(max_level) print(' '.join(map(str, skills))) if __name__ == '__main__': main()
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Skills
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A. Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values: - The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf. - The minimum skill level among all skills (min ai), multiplied by coefficient cm. Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015). The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units. On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
null
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1. In the second test one should increase all skills to maximum.
[{"input": "3 5 10 1 5\n1 3 1", "output": "12\n2 5 2"}, {"input": "3 5 10 1 339\n1 3 1", "output": "35\n5 5 5"}]
1,900
["binary search", "brute force", "dp", "greedy", "sortings", "two pointers"]
35
[{"input": "3 5 10 1 5\r\n1 3 1\r\n", "output": "12\r\n2 5 2 \r\n"}, {"input": "3 5 10 1 339\r\n1 3 1\r\n", "output": "35\r\n5 5 5 \r\n"}, {"input": "2 6 0 1 4\r\n5 1\r\n", "output": "5\r\n5 5 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n0\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "1 100 1 2 30\r\n1\r\n", "output": "62\r\n31 \r\n"}, {"input": "1 100 1 2 30\r\n71\r\n", "output": "201\r\n100 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n1000000000\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "5 5 10 20 50\r\n0 0 0 0 0\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "5 5 10 20 50\r\n3 3 3 3 3\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "4 5 3 7 15\r\n4 3 3 1\r\n", "output": "47\r\n5 5 5 5 \r\n"}, {"input": "3 6 4 6 8\r\n6 4 5\r\n", "output": "48\r\n6 6 6 \r\n"}]
false
stdio
null
true
614/B
614
B
Python 3
TESTS
19
233
1,536,000
172643999
from math import log10 countries=int(input()) ; tanks=input().split() ; ans=0 ; nobea="" for i in tanks: if i == "0" : print(0) ; break plus=log10(int(i)) if int(plus)==plus: ans+=int(plus) else: nobea=i else: if len(nobea)>0: print(nobea+"0"*ans) else: print("1"+"0"*ans)
32
92
8,704,000
128679162
n = int(input()) l = list(map(str, input().split()))[:n] if n == 1: print(l[0]) exit(0) no = "1" t = 0 for i in l: if i == "0": print("0") exit(0) elif i == "1": continue elif i.count('1') != 1 or i.count('0') != len(i) - 1: no = i else: t += (len(i) - 1) output = no + ('0' * t) print(output)
Codeforces Round 339 (Div. 2)
CF
2,016
0.5
256
Gena's Code
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse! There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful. Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country. It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
null
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not. In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful. In sample 3 number 3 is not beautiful, all others are beautiful.
[{"input": "3\n5 10 1", "output": "50"}, {"input": "4\n1 1 10 11", "output": "110"}, {"input": "5\n0 3 1 100 1", "output": "0"}]
1,400
["implementation", "math"]
32
[{"input": "3\r\n5 10 1\r\n", "output": "50"}, {"input": "4\r\n1 1 10 11\r\n", "output": "110"}, {"input": "5\r\n0 3 1 100 1\r\n", "output": "0"}, {"input": "40\r\n10 100 10 1 10 10 100 10 10 100 10 100 100 10 1824868942 100 100 1 10 100 100 10 100 100 10 100 10 1 10 100 100 100 10 1 10 1 10 10 100 100\r\n", "output": "1824868942000000000000000000000000000000000000000000000000000"}, {"input": "6\r\n1000000000000000000000000000000000000 6643573784 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000\r\n", "output": "6643573784000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, {"input": "1\r\n0\r\n", "output": "0"}, {"input": "1\r\n1\r\n", "output": "1"}, {"input": "1\r\n9\r\n", "output": "9"}, {"input": "2\r\n10 50\r\n", "output": "500"}, {"input": "3\r\n500 1 10\r\n", "output": "5000"}]
false
stdio
null
true
731/D
731
D
PyPy 3
TESTS
4
77
0
162204316
import sys readline=sys.stdin.readline N,C=map(int,readline().split()) imos=[0]*(C+1) W=[None]*N for i in range(N): L,*W[i]=map(int,readline().split()) for j in range(L): W[i][j]-=1 cnt=0 for i in range(N-1): for a,b in zip(W[i],W[i+1]): if a<b: imos[0]+=1 imos[C-b]-=1 imos[C-a]+=1 imos[C]-=1 cnt+=1 elif a>b: imos[C-a]+=1 imos[C-b]-=1 cnt+=1 for i in range(1,C+1): imos[i]+=imos[i-1] ans=-1 for i in range(C): if imos[i]==cnt: ans=i print(ans)
48
451
24,883,200
193087489
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n, c = map(int, input().split()) w1 = list(map(int, input().split()))[1:] ng = [0] * (c + 1) ok = 1 for _ in range(n - 1): w2 = list(map(int, input().split()))[1:] f = 0 for i, j in zip(w1, w2): if i < j: l = c - j + 1 r = l + (j - i) % c ng[l] += 1 ng[r] -= 1 f = 1 break elif i > j: l = c - i + 1 r = l + (i - j) % c ng[0] += 1 ng[l] -= 1 ng[r] += 1 f = 1 break if not f and len(w1) > len(w2): ok = 0 w1 = w2 ans = -1 if ok: for i in range(1, c + 1): ng[i] += ng[i - 1] if not ng[i]: ans = i break if not ng[0]: ans = 0 print(ans)
Codeforces Round 376 (Div. 2)
CF
2,016
2
256
80-th Level Archeology
Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of n words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hieroglyphs according to some rules. The instruction nearby says that the door will open only if words written on the lock would be sorted in lexicographical order (the definition of lexicographical comparison in given in notes section). The rule that changes hieroglyphs is the following. One clockwise rotation of the round switch replaces each hieroglyph with the next hieroglyph in alphabet, i.e. hieroglyph x (1 ≤ x ≤ c - 1) is replaced with hieroglyph (x + 1), and hieroglyph c is replaced with hieroglyph 1. Help archeologist determine, how many clockwise rotations they should perform in order to open the door, or determine that this is impossible, i.e. no cyclic shift of the alphabet will make the sequence of words sorted lexicographically.
The first line of the input contains two integers n and c (2 ≤ n ≤ 500 000, 1 ≤ c ≤ 106) — the number of words, written on the lock, and the number of different hieroglyphs. Each of the following n lines contains the description of one word. The i-th of these lines starts with integer li (1 ≤ li ≤ 500 000), that denotes the length of the i-th word, followed by li integers wi, 1, wi, 2, ..., wi, li (1 ≤ wi, j ≤ c) — the indices of hieroglyphs that make up the i-th word. Hieroglyph with index 1 is the smallest in the alphabet and with index c — the biggest. It's guaranteed, that the total length of all words doesn't exceed 106.
If it is possible to open the door by rotating the round switch, print integer x (0 ≤ x ≤ c - 1) that defines the required number of clockwise rotations. If there are several valid x, print any of them. If it is impossible to open the door by this method, print - 1.
null
Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold: - at first position i, such that ai ≠ bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character in the first position where they differ; - if there is no such position i and m ≤ k, i.e. the first word is a prefix of the second or two words are equal. The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word. In the first sample, after the round switch is rotated 1 position clockwise the words look as follows: In the second sample, words are already sorted in lexicographical order. In the last sample, one can check that no shift of the alphabet will work.
[{"input": "4 3\n2 3 2\n1 1\n3 2 3 1\n4 2 3 1 2", "output": "1"}, {"input": "2 5\n2 4 2\n2 4 2", "output": "0"}, {"input": "4 4\n1 2\n1 3\n1 4\n1 2", "output": "-1"}]
2,200
["brute force", "data structures", "greedy", "sortings"]
48
[{"input": "4 3\r\n2 3 2\r\n1 1\r\n3 2 3 1\r\n4 2 3 1 2\r\n", "output": "1\r\n"}, {"input": "2 5\r\n2 4 2\r\n2 4 2\r\n", "output": "0\r\n"}, {"input": "4 4\r\n1 2\r\n1 3\r\n1 4\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "3 1\r\n2 1 1\r\n2 1 1\r\n3 1 1 1\r\n", "output": "0\r\n"}, {"input": "2 10\r\n14 9 6 7 1 6 9 3 1 9 4 6 8 8 1\r\n3 3 7 6\r\n", "output": "2\r\n"}, {"input": "5 10\r\n1 5\r\n7 5 1 10 5 3 7 10\r\n1 2\r\n1 3\r\n7 4 7 3 9 4 6 1\r\n", "output": "6\r\n"}, {"input": "50 5\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n2 4 4\r\n2 4 5\r\n2 4 5\r\n3 4 1 1\r\n4 4 1 2 5\r\n7 4 3 2 1 4 4 5\r\n1 5\r\n1 5\r\n1 5\r\n1 5\r\n2 5 4\r\n2 5 5\r\n2 5 5\r\n2 5 1\r\n2 5 3\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n2 1 4\r\n2 1 4\r\n2 1 5\r\n2 1 1\r\n2 1 1\r\n2 1 3\r\n9 1 3 4 3 2 5 5 5 2\r\n1 2\r\n1 2\r\n3 2 4 2\r\n2 2 1\r\n2 2 2\r\n2 2 2\r\n2 2 2\r\n1 3\r\n1 2\r\n1 3\r\n1 3\r\n1 3\r\n1 3\r\n1 3\r\n2 3 4\r\n3 3 4 4\r\n5 3 4 5 2 1\r\n2 3 5\r\n2 3 2\r\n2 3 3\r\n", "output": "-1\r\n"}, {"input": "10 10\r\n2 6 6\r\n1 7\r\n1 8\r\n2 8 2\r\n2 9 2\r\n2 10 5\r\n1 1\r\n1 4\r\n3 4 9 3\r\n2 5 1\r\n", "output": "5\r\n"}]
false
stdio
null
true
298/A
298
A
Python 3
TESTS
8
216
307,200
76282392
n = int(input()) m = input() s = 0 t = 0 flg = 0 flgm = 0 for i in range(n): if m[i] == "R": if flg == 0: s = i+1 flg = 1 if m[i] == "L": if flgm == 0: t = i flgm = 1 if t == 0: t = m.rfind("R")+2 if s == 0: s = m.find("L")+2 print (s,t)
23
92
0
146895360
n=int(input()) path=input() travel=list(filter(('.').__ne__ , path)) if travel[0]=='R' and travel[-1]=='L': start=path.find("R") end=start+path.count("R") print(start+1,end) elif len(travel)==1: position=path.find(travel[0]) if travel[0]=="R": print(position+1,position+2) else: print(position+1,position) else: if travel[0]==travel[-1]=="R": start=path.find("R") end=start+path.count("R") print(start+1,end+1) else: end=path.find("L") start=end+path.count("L") print(start,end)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Snow Footprints
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
The first line of the input contains integer n (3 ≤ n ≤ 1000). The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
null
The first test sample is the one in the picture.
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
1,300
["greedy", "implementation"]
23
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_path): with open(input_path) as f: n = int(f.readline().strip()) road = f.readline().strip() with open(submission_path) as f: line = f.readline().strip() try: s, t = map(int, line.split()) except: print(0) return if s < 1 or s > n or t < 1 or t > n: print(0) return adj = [[] for _ in range(n+1)] for i in range(1, n+1): c = road[i-1] if c == 'R': if i+1 <= n: adj[i].append(i+1) elif c == 'L': if i-1 >= 1: adj[i].append(i-1) visited = [False] * (n+1) q = deque([s]) visited[s] = True while q: u = q.popleft() if u == t: print(1) return for v in adj[u]: if not visited[v]: visited[v] = True q.append(v) print(0) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
735/B
735
B
PyPy 3
TESTS
5
93
0
167648523
def _input(): return map(int, input().split()) n, n1, n2 = _input() lst = sorted(list(_input()), reverse = True) print( sum(lst[:n1])/n1 + sum(lst[n1:n1+n2+1])/n2)
37
92
14,336,000
186721043
import sys input = sys.stdin.readline n, n1, n2 = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse = True) if(n1 > n2): n1, n2 = n2, n1 print(sum(a[ : n1]) / n1 + sum(a[n1 : n1 + n2]) / n2)
Codeforces Round 382 (Div. 2)
CF
2,016
2
256
Urbanization
Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to ai. Authorities plan to build two cities, first for n1 people and second for n2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n1 settle in the first city and then some subset of size n2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home. To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth ai among all its residents divided by the number of them (n1 or n2 depending on the city). The division should be done in real numbers without any rounding. Please, help authorities find the optimal way to pick residents for two cities.
The first line of the input contains three integers n, n1 and n2 (1 ≤ n, n1, n2 ≤ 100 000, n1 + n2 ≤ n) — the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), the i-th of them is equal to the wealth of the i-th candidate.
Print one real value — the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second. In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (a3 + a4) / 2 + a2 = (3 + 2) / 2 + 4 = 6.5
[{"input": "2 1 1\n1 5", "output": "6.00000000"}, {"input": "4 2 1\n1 4 2 3", "output": "6.50000000"}]
1,100
["greedy", "number theory", "sortings"]
37
[{"input": "2 1 1\r\n1 5\r\n", "output": "6.00000000\r\n"}, {"input": "4 2 1\r\n1 4 2 3\r\n", "output": "6.50000000\r\n"}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "4.50000000\r\n"}, {"input": "10 4 6\r\n3 5 7 9 12 25 67 69 83 96\r\n", "output": "88.91666667\r\n"}, {"input": "19 7 12\r\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 100000 100000\r\n", "output": "47052.10714286\r\n"}, {"input": "100 9 6\r\n109 711 40 95 935 48 228 253 308 726 816 534 252 8 966 363 162 508 84 83 807 506 748 178 45 30 106 108 764 698 825 198 336 353 158 790 64 262 403 334 577 571 742 541 946 602 279 621 910 776 421 886 29 133 114 394 762 965 339 263 750 530 49 80 124 31 322 292 27 590 960 278 111 932 849 491 561 744 469 511 106 271 156 160 836 363 149 473 457 543 976 809 490 29 85 626 265 88 995 946\r\n", "output": "1849.66666667\r\n"}, {"input": "69 6 63\r\n53475 22876 79144 6335 33763 79104 65441 45527 65847 94406 74670 43529 75330 19403 67629 56187 57949 23071 64910 54409 55348 18056 855 24961 50565 6622 26467 33989 22660 79469 41246 13965 79706 14422 16075 93378 81313 48173 13470 97348 2346 27452 59427 29925 29847 73823 32021 10988 24609 98855 90919 45939 17203 8439 43007 40138 55693 30314 71734 33458 66850 4011 20089 20546 92090 50842 78859 62756 40177\r\n", "output": "135712.88888889\r\n"}, {"input": "69 6 9\r\n2612 17461 69001 33130 10662 85485 88195 45974 16712 81365 67119 87797 15559 20197 74716 92979 97268 49466 68603 48351 99905 35606 54242 98603 68232 54398 82637 49647 38979 46171 54680 23334 15892 92186 69670 29711 67999 2220 32317 717 70667 68262 86760 55720 97158 61122 7251 138 21022 27197 12691 59331 13576 66999 38332 13574 83484 66646 17704 33065 98583 80259 64631 16745 69431 40747 82089 82788 32739\r\n", "output": "183129.44444444\r\n"}]
false
stdio
import sys def main(): input_path, correct_output_path, submission_output_path = sys.argv[1:4] with open(correct_output_path, 'r') as f: correct_output = f.read().strip() with open(submission_output_path, 'r') as f: submission_output = f.read().strip() try: correct = float(correct_output) submission = float(submission_output) except: print(0) return absolute_error = abs(submission - correct) max_denominator = max(1.0, correct) relative_error = absolute_error / max_denominator if relative_error <= 1e-6: print(100) else: print(0) if __name__ == "__main__": main()
true
612/E
612
E
Python 3
TESTS
2
46
204,800
15021353
import math import itertools n = int(input()) permutation = [int(x) for x in input().split()] cycles = [] visited = [False for i in range(n)] for i in range(n): if not visited[i]: visited[i] = True cycle = [i] j = permutation[i] - 1 while j != i: visited[j] = True cycle.append(j) j = permutation[j] - 1 cycles.append(cycle) cycles.sort(key = lambda L: len(L)) root_cycles = [] i = 0 while i < len(cycles): cycle = cycles[i] l = len(cycle) if l%2: root_cycle = [cycle[(j + l//2)%l] for j in range(l)] root_cycles.append(root_cycle) elif i == len(cycles)-1 or len(cycles[i+1]) > l: print(-1) break else: cycle1 = cycles[i+1] root_cycle = [cycle1[j//2] if j%2 else cycle[j//2] for j in range(2*l)] root_cycles.append(root_cycle) i += 1 i += 1 else: root_permutation = [0 for i in range(n)] for cycle in root_cycles: for i in range(len(cycle)): root_permutation[cycle[i]] = cycle[(i+1)%len(cycle)] + 1 for i in range(n): print(root_permutation[i], end = ' ')
18
841
144,896,000
170812201
import sys from collections import defaultdict readline=sys.stdin.readline N=int(readline()) P=list(map(int,readline().split())) for x in range(N): P[x]-=1 perm=[] seen=[False]*N for x in range(N): if seen[x]: continue perm.append([]) perm[-1].append(x) seen[x]=True while not seen[P[x]]: x=P[x] perm[-1].append(x) seen[x]=True dct=defaultdict(list) for lst in perm: dct[len(lst)].append(lst) ans_lst=[None]*N for le,lst in dct.items(): if le%2==0 and len(lst)%2: print(-1) exit() if le%2: for i in range(len(lst)): for j in range(le): ans_lst[lst[i][j]]=lst[i][(j+(le+1)//2)%le]+1 else: for i in range(len(lst)//2): lst0=lst[i*2] lst1=lst[i*2+1] for j in range(le): ans_lst[lst0[j]]=lst1[j]+1 ans_lst[lst1[j]]=lst0[(j+1)%le]+1 print(*ans_lst)
Educational Codeforces Round 4
ICPC
2,015
2
256
Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1]. This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
The first line contains integer n (1 ≤ n ≤ 106) — the number of elements in permutation p. The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of permutation p.
If there is no permutation q such that q2 = p print the number "-1". If the answer exists print it. The only line should contain n different integers qi (1 ≤ qi ≤ n) — the elements of the permutation q. If there are several solutions print any of them.
null
null
[{"input": "4\n2 1 4 3", "output": "3 4 2 1"}, {"input": "4\n2 1 3 4", "output": "-1"}, {"input": "5\n2 3 4 5 1", "output": "4 5 1 2 3"}]
2,200
["combinatorics", "constructive algorithms", "dfs and similar", "graphs", "math"]
18
[{"input": "4\r\n2 1 4 3\r\n", "output": "3 4 2 1\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "-1\r\n"}, {"input": "5\r\n2 3 4 5 1\r\n", "output": "4 5 1 2 3\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "-1\r\n"}, {"input": "10\r\n3 5 1 2 10 8 7 6 4 9\r\n", "output": "6 9 8 10 4 3 7 1 5 2\r\n"}, {"input": "100\r\n11 9 35 34 51 74 16 67 26 21 14 80 84 79 7 61 28 3 53 43 42 5 56 36 69 30 22 88 1 27 65 91 46 31 59 50 17 96 25 18 64 55 78 2 63 24 95 48 93 13 38 76 89 94 15 90 45 81 52 87 83 73 44 49 23 82 85 75 86 33 47 19 58 97 37 20 40 10 92 4 6 68 77 54 71 12 62 60 100 39 41 99 72 29 57 8 70 32 66 98\r\n", "output": "-1\r\n"}, {"input": "100\r\n94 22 24 99 58 97 20 29 67 30 38 64 77 50 15 44 92 88 39 42 25 70 2 76 84 6 37 49 17 71 31 19 26 79 10 35 65 63 32 95 5 8 52 27 83 18 53 93 13 81 48 68 54 82 34 60 87 23 16 86 55 40 61 45 28 7 74 41 14 91 3 72 33 11 98 89 90 69 78 36 80 59 56 21 43 1 75 46 47 12 96 73 57 51 4 85 9 100 66 62\r\n", "output": "78 52 95 76 96 49 53 59 77 100 64 11 9 48 15 17 44 46 32 54 84 68 43 4 21 28 73 6 16 62 31 39 65 86 98 75 33 45 19 3 91 82 2 92 63 88 7 50 97 93 14 22 20 42 60 55 80 85 29 34 56 71 83 38 26 47 90 70 51 41 40 72 37 12 35 99 67 94 1 87 57 8 61 25 23 79 36 18 66 74 5 27 81 69 24 58 13 10 89 30\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) p = list(map(int, f.readline().split())) with open(sub_path, 'r') as f: lines = f.readlines() sub_output = [] for line in lines: line = line.strip() if line == '-1': sub_output = [-1] break sub_output.extend(list(map(int, line.split()))) if len(sub_output) == 1 and sub_output[0] == -1: visited = [False] * (n + 1) cycle_counts = {} for i in range(1, n + 1): if not visited[i]: current = i cycle = [] while not visited[current]: visited[current] = True cycle.append(current) current = p[current - 1] cycle_length = len(cycle) if cycle_length % 2 == 0: cycle_counts[cycle_length] = cycle_counts.get(cycle_length, 0) + 1 valid = True for length, count in cycle_counts.items(): if count % 2 != 0: valid = False break print(1 if not valid else 0) else: if len(sub_output) != n: print(0) return seen = [False] * (n + 1) valid_perm = True for num in sub_output: if not (1 <= num <= n) or seen[num]: valid_perm = False break seen[num] = True if not valid_perm: print(0) return q = sub_output q_squared = [] for i in range(n): first = q[i] second = q[first - 1] q_squared.append(second) print(1 if q_squared == p else 0) if __name__ == "__main__": main()
true
732/D
732
D
PyPy 3-64
TESTS
3
62
13,414,400
218691750
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb alph = 'abcdefghijklmnopqrstuvwxyz' #pow(x,mod-2,mod) def find(days): s = set([x for x in range(M)]) for i in range(days-1,-1,-1): if A[i] in s:s.remove(A[i]) if not s: if i+1>=su:return True else:return False return False N,M = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) su = sum(B) l,r = 0,N+1 while l<r: mid = (l+r)//2 if find(mid): r = mid else: l = mid+1 if 1<l<N: print(l) else: print(-1)
49
202
13,107,200
21538977
n,m = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) sums = sum(B)+len(B) last = [-1] * m cou = 0 ans = 0 per = 0 for j in range(n): if A[j] == 0: cou +=1 else: if last[A[j]-1] == -1: if j >= B[A[j]-1]: ans += 1 last[A[j]-1] = 0 if ans == m: if (j+1) >= sums: per=1 print(j+1) break if per == 0: print(-1)
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Exams
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects. The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i. The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
null
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
[{"input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5"}, {"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9"}, {"input": "5 1\n1 1 1 1 1\n5", "output": "-1"}]
1,700
["binary search", "greedy", "sortings"]
53
[{"input": "7 2\r\n0 1 0 2 1 0 2\r\n2 1\r\n", "output": "5\r\n"}, {"input": "10 3\r\n0 0 1 2 3 0 2 0 1 2\r\n1 1 4\r\n", "output": "9\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n5\r\n", "output": "-1\r\n"}, {"input": "100 10\r\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\r\n2 4 10 11 5 2 6 7 2 15\r\n", "output": "74\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n0 0 0\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n0 1 0 2\r\n1 1\r\n", "output": "4\r\n"}, {"input": "10 1\r\n0 1 0 0 0 0 0 0 0 1\r\n1\r\n", "output": "2\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n1\r\n", "output": "5\r\n"}, {"input": "7 2\r\n0 0 0 0 0 1 2\r\n1 1\r\n", "output": "7\r\n"}, {"input": "10 3\r\n0 0 1 2 2 0 2 0 1 3\r\n1 1 4\r\n", "output": "10\r\n"}, {"input": "6 2\r\n1 1 1 1 1 2\r\n1 1\r\n", "output": "6\r\n"}, {"input": "6 2\r\n1 0 0 0 0 2\r\n1 1\r\n", "output": "-1\r\n"}]
false
stdio
null
true
298/A
298
A
Python 3
TESTS
9
62
0
221992962
def yzd_solution(n, road): s = 0 t = 0 if "R" in road and "L" in road: R_i_s = road.index("R") + 1 L_i_s = road.index("L") s = R_i_s if R_i_s < L_i_s else L_i_s t = R_i_s if R_i_s > L_i_s else L_i_s elif "R" in road and "L" not in road: s = road.index("R") + 1 t = 0 for i in range(s, len(road)): if road[i] == 'R' and road[i+1] == '.': t = i + 2 break elif "L" in road and "R" not in road: t = road.index("L") for i in range(t, len(road)): if road[i] == 'L' and road[i+1] == '.': s = i + 1 break print(s, t) n = int(input()) road = list(input()) yzd_solution(n, road)
23
92
0
146956235
n = int(input()) arr = list(input()) def sol(): if 'L' in arr and 'R' in arr: start = 0 end = 0 for i in range(n): if arr[i] == 'R': if start == 0: start = i+1 if arr[i] == 'L': if end == 0: end = i print(start, end) elif 'R' in arr: start = 0 end = 0 for i in range(n-1): if arr[i] in 'R': if start == 0: start = i + 1 if arr[i+1] in '.': if start != 0: if end == 0: end = i + 2 print(start, end) else: start = 0 end = 0 for i in range(1,n+1): if arr[i-1] in 'L': if end == 0: end = i - 1 if arr[i-1] in '.': if end != 0: if start == 0: start = i - 1 print(start, end) sol()
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Snow Footprints
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
The first line of the input contains integer n (3 ≤ n ≤ 1000). The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
null
The first test sample is the one in the picture.
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
1,300
["greedy", "implementation"]
23
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_path): with open(input_path) as f: n = int(f.readline().strip()) road = f.readline().strip() with open(submission_path) as f: line = f.readline().strip() try: s, t = map(int, line.split()) except: print(0) return if s < 1 or s > n or t < 1 or t > n: print(0) return adj = [[] for _ in range(n+1)] for i in range(1, n+1): c = road[i-1] if c == 'R': if i+1 <= n: adj[i].append(i+1) elif c == 'L': if i-1 >= 1: adj[i].append(i-1) visited = [False] * (n+1) q = deque([s]) visited[s] = True while q: u = q.popleft() if u == t: print(1) return for v in adj[u]: if not visited[v]: visited[v] = True q.append(v) print(0) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
614/B
614
B
PyPy 3
TESTS
30
171
8,704,000
80929027
def check(string): if len(string) == 1: return string[0] != '1' if any(s != '0' for s in string[1:]): return True return False if __name__ == "__main__": n = int(input()) if n == 1: print(input()) exit(0) tanks = [str(x) for x in input().split(' ')] for x in tanks: if x == '0': print(0) exit(0) not_b = '1' for i in range(n): if check(tanks[i]): not_b = tanks[i] tanks[i] = '_' break result = [not_b] for i, x in enumerate(tanks): if x != '_': result.append('0' * (len(x) - 1)) print(''.join(result))
32
108
1,024,000
202956615
import sys input=sys.stdin.readline n = int(input()) a = input().split() zeros_count = 0 to_multiple = '' for i in a: if (i.count('1') != 1 or i.count('0') != len(i) - 1) and i != '0': to_multiple = i elif i == '0': print(0) exit(0) else: zeros_count += i.count('0') print(to_multiple + '0' * (zeros_count) if to_multiple != '' else '1' + '0' * zeros_count)
Codeforces Round 339 (Div. 2)
CF
2,016
0.5
256
Gena's Code
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse! There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful. Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country. It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
null
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not. In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful. In sample 3 number 3 is not beautiful, all others are beautiful.
[{"input": "3\n5 10 1", "output": "50"}, {"input": "4\n1 1 10 11", "output": "110"}, {"input": "5\n0 3 1 100 1", "output": "0"}]
1,400
["implementation", "math"]
32
[{"input": "3\r\n5 10 1\r\n", "output": "50"}, {"input": "4\r\n1 1 10 11\r\n", "output": "110"}, {"input": "5\r\n0 3 1 100 1\r\n", "output": "0"}, {"input": "40\r\n10 100 10 1 10 10 100 10 10 100 10 100 100 10 1824868942 100 100 1 10 100 100 10 100 100 10 100 10 1 10 100 100 100 10 1 10 1 10 10 100 100\r\n", "output": "1824868942000000000000000000000000000000000000000000000000000"}, {"input": "6\r\n1000000000000000000000000000000000000 6643573784 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000\r\n", "output": "6643573784000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, {"input": "1\r\n0\r\n", "output": "0"}, {"input": "1\r\n1\r\n", "output": "1"}, {"input": "1\r\n9\r\n", "output": "9"}, {"input": "2\r\n10 50\r\n", "output": "500"}, {"input": "3\r\n500 1 10\r\n", "output": "5000"}]
false
stdio
null
true
614/B
614
B
Python 3
TESTS
21
139
6,348,800
17417536
from sys import stdin def isBad(s): if s[0] != '1': return 1 for i in s[1:]: if i != '0': return 1 return 0 def getBad(a): for i in a: if isBad(i): return i return None def getPowerOfTen(a): res = 0 for i in a: if not isBad(i) and not i == '0': res += len(i) - 1 return res n = int(stdin.readline().strip()) a = stdin.readline().strip().split(' ') b = getBad(a) # print('bad :', b) if b is None: print('1' + '0' * getPowerOfTen(a)) elif b == '0': print('0') else: print(b + '0' * getPowerOfTen(a))
32
108
1,638,400
15351667
#!/usr/bin/env python3 import sys N = int(input()) l = input().split() is_zero = True for i in l: if i == '0': break else: is_zero = False if is_zero: print('0') sys.exit(0) def are_all_zero(s): for c in s: if c != '0': return False return True times = 0 ugly = '1' for i in l: if are_all_zero(i[1:]) and i[0] == '1': times += len(i) - 1 else: ugly = i answer = ugly + '0'*times print(answer)
Codeforces Round 339 (Div. 2)
CF
2,016
0.5
256
Gena's Code
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse! There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful. Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country. It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
null
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not. In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful. In sample 3 number 3 is not beautiful, all others are beautiful.
[{"input": "3\n5 10 1", "output": "50"}, {"input": "4\n1 1 10 11", "output": "110"}, {"input": "5\n0 3 1 100 1", "output": "0"}]
1,400
["implementation", "math"]
32
[{"input": "3\r\n5 10 1\r\n", "output": "50"}, {"input": "4\r\n1 1 10 11\r\n", "output": "110"}, {"input": "5\r\n0 3 1 100 1\r\n", "output": "0"}, {"input": "40\r\n10 100 10 1 10 10 100 10 10 100 10 100 100 10 1824868942 100 100 1 10 100 100 10 100 100 10 100 10 1 10 100 100 100 10 1 10 1 10 10 100 100\r\n", "output": "1824868942000000000000000000000000000000000000000000000000000"}, {"input": "6\r\n1000000000000000000000000000000000000 6643573784 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000\r\n", "output": "6643573784000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, {"input": "1\r\n0\r\n", "output": "0"}, {"input": "1\r\n1\r\n", "output": "1"}, {"input": "1\r\n9\r\n", "output": "9"}, {"input": "2\r\n10 50\r\n", "output": "500"}, {"input": "3\r\n500 1 10\r\n", "output": "5000"}]
false
stdio
null
true
612/E
612
E
Python 3
TESTS
2
62
204,800
29532574
n = int(input()) c = input() c = c.split(" ") c = [int(p) - 1 for p in c] visited = [0 for p in range(0,n,1)] cycle_st = [] for i in range(0,n,1): if visited[i] != 1: cycle = [] cycle.append([i,c[i]]) visited[i] = 1 d = c[i] while visited[d] != 1: cycle.append([d,c[d]]) visited[d] = 1 d = c[d] cycle_st.append(cycle) used = [ 1 for p in range(0,len(cycle_st),1)] posssible = True have_cycle =True i = 0 j = 0 for i in range(len(cycle_st)): if used[i]: have_cycle = False if(len(cycle_st[i]) % 2 == 1): for j in range(len(cycle_st[i])): visited[cycle_st[i][j][0]] = cycle_st[i][(j+1)%len(cycle_st[i])][1] else: for j in range(i+1,len(cycle_st),1): if used[j] and len(cycle_st[i]) == len(cycle_st[j]): have_cycle = True for k in range(len(cycle_st[i])): visited[cycle_st[i][k][0]] = cycle_st[j][k-1][0] visited[cycle_st[j][k-1][0]] = cycle_st[i][k-1][0] used[j] = 0 break if not have_cycle: posssible = False break if not posssible: print(-1) else: for p in visited: print(str(p+1))
18
1,263
185,753,600
134372041
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n = int(input()) p = [0] + list(map(int, input().split())) visit = [0] * (n + 1) x = [[] for _ in range(n + 1)] q = [0] * n for i in range(1, n + 1): if visit[i]: continue y = [i] visit[i] = 1 while not visit[p[y[-1]]]: y.append(p[y[-1]]) visit[y[-1]] = 1 l = len(y) if l % 2 == 0: x[l].append(y) continue z = [0] * l for j in range(l): z[2 * j % l] = y[j] for j in range(l): q[z[j] - 1] = z[(j + 1) % l] for i in range(2, n + 1, 2): xi, l = x[i], 2 * i if len(xi) % 2: q = [-1] break for j in range(len(xi) // 2): u, v = xi[2 * j], xi[2 * j + 1] z = [0] * l for k in range(i): z[2 * k] = u[k] z[2 * k + 1] = v[k] for j in range(l): q[z[j] - 1] = z[(j + 1) % l] sys.stdout.write(" ".join(map(str, q)))
Educational Codeforces Round 4
ICPC
2,015
2
256
Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1]. This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
The first line contains integer n (1 ≤ n ≤ 106) — the number of elements in permutation p. The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of permutation p.
If there is no permutation q such that q2 = p print the number "-1". If the answer exists print it. The only line should contain n different integers qi (1 ≤ qi ≤ n) — the elements of the permutation q. If there are several solutions print any of them.
null
null
[{"input": "4\n2 1 4 3", "output": "3 4 2 1"}, {"input": "4\n2 1 3 4", "output": "-1"}, {"input": "5\n2 3 4 5 1", "output": "4 5 1 2 3"}]
2,200
["combinatorics", "constructive algorithms", "dfs and similar", "graphs", "math"]
18
[{"input": "4\r\n2 1 4 3\r\n", "output": "3 4 2 1\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "-1\r\n"}, {"input": "5\r\n2 3 4 5 1\r\n", "output": "4 5 1 2 3\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "-1\r\n"}, {"input": "10\r\n3 5 1 2 10 8 7 6 4 9\r\n", "output": "6 9 8 10 4 3 7 1 5 2\r\n"}, {"input": "100\r\n11 9 35 34 51 74 16 67 26 21 14 80 84 79 7 61 28 3 53 43 42 5 56 36 69 30 22 88 1 27 65 91 46 31 59 50 17 96 25 18 64 55 78 2 63 24 95 48 93 13 38 76 89 94 15 90 45 81 52 87 83 73 44 49 23 82 85 75 86 33 47 19 58 97 37 20 40 10 92 4 6 68 77 54 71 12 62 60 100 39 41 99 72 29 57 8 70 32 66 98\r\n", "output": "-1\r\n"}, {"input": "100\r\n94 22 24 99 58 97 20 29 67 30 38 64 77 50 15 44 92 88 39 42 25 70 2 76 84 6 37 49 17 71 31 19 26 79 10 35 65 63 32 95 5 8 52 27 83 18 53 93 13 81 48 68 54 82 34 60 87 23 16 86 55 40 61 45 28 7 74 41 14 91 3 72 33 11 98 89 90 69 78 36 80 59 56 21 43 1 75 46 47 12 96 73 57 51 4 85 9 100 66 62\r\n", "output": "78 52 95 76 96 49 53 59 77 100 64 11 9 48 15 17 44 46 32 54 84 68 43 4 21 28 73 6 16 62 31 39 65 86 98 75 33 45 19 3 91 82 2 92 63 88 7 50 97 93 14 22 20 42 60 55 80 85 29 34 56 71 83 38 26 47 90 70 51 41 40 72 37 12 35 99 67 94 1 87 57 8 61 25 23 79 36 18 66 74 5 27 81 69 24 58 13 10 89 30\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) p = list(map(int, f.readline().split())) with open(sub_path, 'r') as f: lines = f.readlines() sub_output = [] for line in lines: line = line.strip() if line == '-1': sub_output = [-1] break sub_output.extend(list(map(int, line.split()))) if len(sub_output) == 1 and sub_output[0] == -1: visited = [False] * (n + 1) cycle_counts = {} for i in range(1, n + 1): if not visited[i]: current = i cycle = [] while not visited[current]: visited[current] = True cycle.append(current) current = p[current - 1] cycle_length = len(cycle) if cycle_length % 2 == 0: cycle_counts[cycle_length] = cycle_counts.get(cycle_length, 0) + 1 valid = True for length, count in cycle_counts.items(): if count % 2 != 0: valid = False break print(1 if not valid else 0) else: if len(sub_output) != n: print(0) return seen = [False] * (n + 1) valid_perm = True for num in sub_output: if not (1 <= num <= n) or seen[num]: valid_perm = False break seen[num] = True if not valid_perm: print(0) return q = sub_output q_squared = [] for i in range(n): first = q[i] second = q[first - 1] q_squared.append(second) print(1 if q_squared == p else 0) if __name__ == "__main__": main()
true
614/B
614
B
Python 3
TESTS
22
249
8,601,600
128678674
n = int(input()) l = list(map(str, input().split()))[:n] if n == 1: print(l[0]) exit(0) if "0" in l: print("0") exit(0) l = list(filter(("1").__ne__, l)) if not l: print("1") exit(0) no = -1 t = 0 for i in l: if int(i) % 10: no = int(i) else: t += (len(i) - 1) if no == -1: print(f"1{'0' * t}") else: print(f"{no}{'0' * t}")
32
108
1,638,400
15359187
#!/usr/bin/env python3 import sys input() arr = list(input().split()) def isBeautiful(s): if s[0] != '1': return False for c in s[1:]: if c != '0': return False return True logAns = 0 nonBeautiful = None isZero = False for s in arr: if s == "0": isZero = True if isBeautiful(s): logAns += (len(s) - 1) else: nonBeautiful = s if isZero: print(0) else: if nonBeautiful is None: nonBeautiful = '1' print(nonBeautiful + '0'*logAns)
Codeforces Round 339 (Div. 2)
CF
2,016
0.5
256
Gena's Code
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse! There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful. Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country. It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
null
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not. In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful. In sample 3 number 3 is not beautiful, all others are beautiful.
[{"input": "3\n5 10 1", "output": "50"}, {"input": "4\n1 1 10 11", "output": "110"}, {"input": "5\n0 3 1 100 1", "output": "0"}]
1,400
["implementation", "math"]
32
[{"input": "3\r\n5 10 1\r\n", "output": "50"}, {"input": "4\r\n1 1 10 11\r\n", "output": "110"}, {"input": "5\r\n0 3 1 100 1\r\n", "output": "0"}, {"input": "40\r\n10 100 10 1 10 10 100 10 10 100 10 100 100 10 1824868942 100 100 1 10 100 100 10 100 100 10 100 10 1 10 100 100 100 10 1 10 1 10 10 100 100\r\n", "output": "1824868942000000000000000000000000000000000000000000000000000"}, {"input": "6\r\n1000000000000000000000000000000000000 6643573784 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000\r\n", "output": "6643573784000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, {"input": "1\r\n0\r\n", "output": "0"}, {"input": "1\r\n1\r\n", "output": "1"}, {"input": "1\r\n9\r\n", "output": "9"}, {"input": "2\r\n10 50\r\n", "output": "500"}, {"input": "3\r\n500 1 10\r\n", "output": "5000"}]
false
stdio
null
true
482/B
482
B
PyPy 3-64
TESTS
6
171
9,625,600
223635569
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb alph = 'abcdefghijklmnopqrstuvwxyz' #pow(x,mod-2,mod) N,M = map(int,input().split()) P = sorted([list(map(int,input().split())) for _ in range(M)])[::-1] P2 = P[:] ans = [] cnt = [] l = [0]*31 h = [];heapify(h) for x in range(N): while P and P[-1][0]==x+1: for j in range(31): if P[-1][2]&2**j==2**j:l[j]+=1 heappush(h,[P[-1][1],P[-1][2]]) P.pop() num = 0 for i in range(31): if l[i]>=1: num+=2**i ans.append(num) cnt.append(l[:]) while h: end,q = heappop(h) if end==x+1: for j in range(31): if q&2**j==2**j:l[j]-=1 else:heappush(h,[end,q]);break # print(ans) for i in range(1,N): for j in range(31): cnt[i][j]+=cnt[i-1][j] check = True for l,r,q in P2: numi = 0 for i in range(31): num = cnt[r-1][i] if l-2>=0:num-=cnt[l-2][i] if num==r-l+1:numi+=2**i if numi!=q: check = False break if check==True: print("YES") print(*ans) else: print("NO")
38
670
40,755,200
149038976
import sys readline=sys.stdin.readline class Dual_Segment_Tree: def __init__(self,N,f_act,e_act,operate,lst): self.N=N self.f_act=f_act self.e_act=e_act self.operate=operate self.lst=[None]*self.N for i,x in enumerate(lst): self.lst[i]=x self.segment_tree_act=[self.e_act]*(self.N+self.N) def __getitem__(self,i): if type(i) is int: if -self.N<=i<0: i+=self.N*2 elif 0<=i<self.N: i+=self.N else: raise IndexError('list index out of range') self.Propagate_Above(i) return self.Operate_At(i) else: a,b,c=i.start,i.stop,i.step if a==None or a<-self.N: a=0 elif self.N<=a: a=self.N elif a<0: a+=self.N if b==None or self.N<=b: b=self.N elif b<-self.N: b=0 elif b<0: b+=self.N return self.lst[slice(a,b,c)] def Operate_At(self,i): return self.operate(self.lst[i-self.N],self.segment_tree_act[i]) def Propagate_At(self,i): self.segment_tree_act[i<<1]=self.f_act(self.segment_tree_act[i<<1],self.segment_tree_act[i]) self.segment_tree_act[i<<1|1]=self.f_act(self.segment_tree_act[i<<1|1],self.segment_tree_act[i]) self.segment_tree_act[i]=self.e_act def Propagate_Above(self,i): H=i.bit_length()-1 for h in range(H,0,-1): self.Propagate_At(i>>h) def Operate_Range(self,a,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N L0=L//(L&-L) R0=R//(R&-R)-1 self.Propagate_Above(L0) self.Propagate_Above(R0) while L<R: if L&1: self.segment_tree_act[L]=self.f_act(self.segment_tree_act[L],a) L+=1 if R&1: R-=1 self.segment_tree_act[R]=self.f_act(self.segment_tree_act[R],a) L>>=1 R>>=1 def Update(self): for i in range(1,self.N): self.Propagate_At(i) self.segment_tree_act[i]=self.e_act def __str__(self): import copy segment_tree_act=copy.deepcopy(self.segment_tree_act) for i in range(1,self.N): segment_tree_act[i<<1]=self.f_act(segment_tree_act[i<<1],segment_tree_act[i]) segment_tree_act[i<<1|1]=self.f_act(segment_tree_act[i<<1|1],segment_tree_act[i]) segment_tree_act[i]=self.e_act segment_tree_act[i]=self.e_act return '['+', '.join(map(str,[self.operate(x,a) for x,a in zip(self.lst,segment_tree_act[self.N:])]))+']' class Segment_Tree: def __init__(self,N,f,e,lst=None): self.f=f self.e=e self.N=N if lst==None: self.segment_tree=[self.e]*2*self.N else: assert len(lst)<=self.N self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst)) for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def __getitem__(self,i): if type(i)==int: if -self.N<=i<0: return self.segment_tree[i+self.N*2] elif 0<=i<self.N: return self.segment_tree[i+self.N] else: raise IndexError('list index out of range') else: a,b,c=i.start,i.stop,i.step if a==None: a=self.N else: a+=self.N if b==None: b=self.N*2 else: b+=self.N return self.segment_tree[slice(a,b,c)] def __setitem__(self,i,x): if -self.N<=i<0: i+=self.N*2 elif 0<=i<self.N: i+=self.N else: raise IndexError('list index out of range') self.segment_tree[i]=x while i>1: i>>= 1 self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Build(self,lst): for i,x in enumerate(lst,self.N): self.segment_tree[i]=x for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Fold(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N vL=self.e vR=self.e while L<R: if L&1: vL=self.f(vL,self.segment_tree[L]) L+=1 if R&1: R-=1 vR=self.f(self.segment_tree[R],vR) L>>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N if L==R: return None x=self.Fold(L-self.N,R-self.N) while L<R: if L&1: if self.segment_tree[L]==x: i=L break L+=1 if R&1: R-=1 if self.segment_tree[R]==x: i=R break L>>=1 R>>=1 while i<self.N: if self.segment_tree[i]==self.segment_tree[i<<1]: i<<=1 else: i<<=1 i|=1 i-=self.N return i def Bisect_Right(self,l=None,f=None): if l==self.N: return self.N if l==None: l=0 if f(self.Fold(l,self.N)): return self.N l+=self.N v=self.e while True: while l%2==0: l>>=1 vv=self.f(v,self.segment_tree[l]) if f(vv): v=vv l+=1 if 2*self.N==l<<(self.N.bit_length()-l.bit_length()+1): return self.N else: while l<self.N: l<<=1 vv=self.f(v,self.segment_tree[l]) if f(vv): v=vv l+=1 return l-self.N def Bisect_Left(self,r=None,f=None): if r==0: return 0 if r==None: r=self.N if f(self.Fold(0,r)): return 0 r+=self.N v=self.e while True: r-=1 while r>1 and r%2: r>>=1 vv=self.f(self.segment_tree[r],v) if f(vv): v=vv if 2*self.N==r<<(self.N.bit_length()-r.bit_length()+1): return 0 else: while r<self.N: r=2*r+1 vv=self.f(self.segment_tree[r],v) if f(vv): v=vv r-=1 return r+1-self.N def __str__(self): return '['+', '.join(map(str,self.segment_tree[self.N:]))+']' N,M=map(int,readline().split()) DST=Dual_Segment_Tree(N,lambda a,b:a|b,0,lambda x,a:x|a,[0]*N) query=[] for _ in range(M): l,r,q=map(int,readline().split()) l-=1 DST.Operate_Range(q,l,r) query.append((l,r,q)) e=(1<<32)-1 ans_lst=[DST[i] for i in range(N)] ST=Segment_Tree(N,lambda x,y:x&y,e,ans_lst) for l,r,q in query: if ST.Fold(l,r)!=q: print("NO") exit() print("YES") print(*ans_lst)
Codeforces Round 275 (Div. 1)
CF
2,014
1
256
Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value $$a[l_{i}] \& a[l_{i}+1] \& \ldots \& a[r_{i}]$$ should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".
The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
null
null
[{"input": "3 1\n1 3 3", "output": "YES\n3 3 3"}, {"input": "3 2\n1 3 3\n1 3 2", "output": "NO"}]
1,800
["constructive algorithms", "data structures", "trees"]
38
[{"input": "3 1\r\n1 3 3\r\n", "output": "YES\r\n3 3 3\r\n"}, {"input": "3 2\r\n1 3 3\r\n1 3 2\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n1 2 536870912\r\n2 3 536870911\r\n", "output": "YES\r\n536870912 1073741823 536870911\r\n"}, {"input": "1 1\r\n1 1 10\r\n", "output": "YES\r\n10\r\n"}, {"input": "1 2\r\n1 1 10\r\n1 1 5\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n1 1 1\r\n1 1 3\r\n", "output": "NO\r\n"}]
false
stdio
null
true
482/B
482
B
PyPy 3-64
TESTS
6
124
4,710,400
169491652
from itertools import accumulate n, m = map(int, input().split()) query = [list(map(int, input().split())) for _ in range(m)] a = [0] * (n + 1) cnt = [[0] * (n + 2) for _ in range(30)] for l, r, v in query: for i in range(30): if v >> i & 1: cnt[i][l] += 1 cnt[i][r + 1] -= 1 for i in range(30): cnt[i] = list(accumulate(cnt[i])) for j in range(1, n + 1): if cnt[i][j] > 0: a[j] |= 1 << i cnt[i] = list(accumulate(cnt[i])) for l, r, v in query: if not v & (1 << i) and cnt[i][r] - cnt[i][l - 1] == r - l + 1: print('NO') exit(0) print('YES') print(*a[1:])
38
732
25,190,400
121180267
from sys import stdin input=stdin.readline def funcand(a,b): return a&b class SegmentTree: def __init__(self, data, default=(1<<31)-1, 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 f(n,q): a=[0]*(n) for bit in range(30): s=[0]*(n+1) for l,r,nm in q: if((nm>>bit)&1): s[l]+=1 s[r]-=1 for i in range(n): if i>0: s[i]+=s[i-1] # bich ke bhar raha hai if s[i]>0: a[i]|=(1<<bit) st=SegmentTree(a,func=funcand,default=(1<<31)-1) for l,r,nm in q: # print(st.query(l,r)) if st.query(l,r)!=nm: return "NO" print("YES") print(*a) return '' q=[] n,m=map(int,input().strip().split()) for _ in range(m): l,r,nm=map(int,input().strip().split()) q.append((l-1,r,nm)) print(f(n,q))
Codeforces Round 275 (Div. 1)
CF
2,014
1
256
Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value $$a[l_{i}] \& a[l_{i}+1] \& \ldots \& a[r_{i}]$$ should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".
The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
null
null
[{"input": "3 1\n1 3 3", "output": "YES\n3 3 3"}, {"input": "3 2\n1 3 3\n1 3 2", "output": "NO"}]
1,800
["constructive algorithms", "data structures", "trees"]
38
[{"input": "3 1\r\n1 3 3\r\n", "output": "YES\r\n3 3 3\r\n"}, {"input": "3 2\r\n1 3 3\r\n1 3 2\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n1 2 536870912\r\n2 3 536870911\r\n", "output": "YES\r\n536870912 1073741823 536870911\r\n"}, {"input": "1 1\r\n1 1 10\r\n", "output": "YES\r\n10\r\n"}, {"input": "1 2\r\n1 1 10\r\n1 1 5\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n1 1 1\r\n1 1 3\r\n", "output": "NO\r\n"}]
false
stdio
null
true
482/B
482
B
Python 3
TESTS
6
77
409,600
9550202
n, m = map(int, input().split()) t = [list(map(int, input().split())) for i in range(m)] t.sort() def f(k, s): d = 1 << 30 a, b = 0, d for l, r, q in t: if l > a + 1: a, b = 0, d if l > b: b = d if q & k: if a: l = a for i in range(l - 1, r): s[i] += k a = max(a, r) else: b = min(b, r) if b <= a: return False return True s = [0] * n if all(f(1 << i, s) for i in range(30)): print('YES\n' + ' '.join(map(str, s))) else: print('NO')
38
873
66,048,000
88422055
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") MAX = 1000001 bitscount = 30 prefix_count = [[0]*(10**5+1) for i in range(30)] def findPrefixCount(arr, n): for i in range(0, bitscount): prefix_count[i][0] = ((arr[0] >> i) & 1) for j in range(1, n): prefix_count[i][j] = ((arr[j] >> i) & 1) prefix_count[i][j] += prefix_count[i][j - 1] def rangeOr(l, r): ans = 0 for i in range(bitscount): x = 0 if (l == 0): x = prefix_count[i][r] else: x = prefix_count[i][r] - prefix_count[i][l - 1] # Condition for ith bit # of answer to be set if (x == r - l + 1): ans = (ans | (1 << i)) return ans n, m = map(int, input().split()) a = [[0] * n for i in range(30)] query = [] for i in range(m): l, r, q = map(int, input().split()) query.append([l-1, r-1, q]) c = bin(q)[2:][::-1] b = [] for j in c: b.append(int(j)) j = 0 while (j < len(b)): if b[j] == 1: a[j][l - 1] += 1 if r != n: a[j][r] -= 1 j += 1 for i in range(30): j = 1 while (j < n): a[i][j] += a[i][j - 1] j += 1 j = 0 while (j < n): if a[i][j] > 0: a[i][j] = 1 j += 1 res=[] for i in range(n): s = "" j=29 while(j>=0): s += str(a[j][i]) j+=-1 res.append(int(s,2)) findPrefixCount(res, n) f=0 for j in query: if rangeOr(j[0],j[1])!=j[2]: f=1 break if f==1: print("NO") else: print("YES") print(*res)
Codeforces Round 275 (Div. 1)
CF
2,014
1
256
Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value $$a[l_{i}] \& a[l_{i}+1] \& \ldots \& a[r_{i}]$$ should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".
The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
null
null
[{"input": "3 1\n1 3 3", "output": "YES\n3 3 3"}, {"input": "3 2\n1 3 3\n1 3 2", "output": "NO"}]
1,800
["constructive algorithms", "data structures", "trees"]
38
[{"input": "3 1\r\n1 3 3\r\n", "output": "YES\r\n3 3 3\r\n"}, {"input": "3 2\r\n1 3 3\r\n1 3 2\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n1 2 536870912\r\n2 3 536870911\r\n", "output": "YES\r\n536870912 1073741823 536870911\r\n"}, {"input": "1 1\r\n1 1 10\r\n", "output": "YES\r\n10\r\n"}, {"input": "1 2\r\n1 1 10\r\n1 1 5\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n1 1 1\r\n1 1 3\r\n", "output": "NO\r\n"}]
false
stdio
null
true
1004/D
1004
D
Python 3
PRETESTS
3
685
61,952,000
40002776
import sys import os def calculate(r, x1, x2, y1, y2): result = 0 s1 = -r if x1 is None else max(x1 + 1, -r) s2 = r if y2 is None else min(r, y2 - 1) result += max(0, abs(s1) + abs(s2) - r + 1) s1 = -r if x1 is None else max(x1 + 1, -r) s2 = -r if y1 is None else max(y1 + 1, -r) result += max(0, abs(s1) + abs(s2) - r + 1) s1 = r if x2 is None else min(r, x2 - 1) s2 = -r if y1 is None else max(y1 + 1, -r) result += max(0, abs(s1) + abs(s2) - r + 1) s1 = r if x2 is None else min(r, x2 - 1) s2 = r if y2 is None else min(r, y1 - 1) result += max(0, abs(s1) + abs(s2) - r + 1) result -= 0 if not x1 is None and x1 >= -r else 1 result -= 0 if not y1 is None and y1 >= -r else 1 result -= 0 if not x2 is None and x2 <= r else 1 result -= 0 if not y2 is None and y2 <= r else 1 return result def sonyaAndMatrix(n, a): dist = dict() for e in a: if e not in dist: dist[e] = 1 else: dist[e] += 1 if 0 not in dist or dist[0] != 1: return -1 m = max(a) if dist[m] != 4 and dist[m] != 2 and dist[m] != 1: return -1 x1 = None x2 = None y1 = None y2 = None for r in range(1, m + 1): expected = calculate(r, x1, x2, y1, y2) if expected == dist[r]: continue elif expected == dist[r] + 1: if x1 is None: x1 = -r elif x2 is None: x2 = r elif y1 is None: y1 = -r elif y2 is None: y2 = r else: return -1 elif expected == dist[r] + 2: if dist[m] == 4 or dist[m] == 2: if x1 is None and x2 is None: x1 = -r x2 = r elif y1 is None and y2 is None: y1 = -r y2 = r else: return -1 else: if x1 is None and y1 is None: x1 = -r y1 = -r elif x1 is None and y2 is None: x1 = -r y2 = r elif x2 is None and y1 is None: x2 = r y1 = -r elif x2 is None and y2 is None: x2 = r y2 = r else: return -1 elif expected == dist[r] + 3: if x1 is None and x2 is None: x1 = -r x2 = r if y1 is None: y1 = -r elif y2 is None: y2 = r else: return -1 elif y1 is None and y2 is None: y1 = -r y2 = r if x1 is None: x1 = -r elif x2 is None: x2 = r else: return -1 return -1 elif expected == dist[r] + 4: if x1 is None and x2 is None and y1 is None and y2 is None: x1 = -r x2 = r y1 = -r y2 = r else: return -1 else: return -1 if x1 is None or x2 is None or y1 is None or y2 is None: return -1 return [[x2 - x1 - 1, y2 - y1 - 1], [x2, y2]] def main(): n = int(input()) a = [int(x) for x in input().split()] result = sonyaAndMatrix(n, a) if result == -1: print(result) else: print(result[0][0], result[0][1]) print(result[1][0], result[1][1]) if __name__ == '__main__': main()
47
358
79,564,800
142294522
import sys input = sys.stdin.buffer.readline def test(n, m, x, y, d): d2 = [0 for i in range(len(d))] for i in range(n): for j in range(m): D = abs(i-x)+abs(j-y) if D >= len(d2): return False d2[D]+=1 return d==d2 def process(A): t = len(A) if t==1 and A==[0]: return [1, 1, 1, 1] M = max(A) d = [0 for i in range(M+1)] for x in A: d[x]+=1 if d[0] != 1: return [-1, None, None, None] first_fail = None for i in range(1, M+1): if d[i] != 4*i: first_fail = i break if first_fail is None: return [-1, None, None, None] x = first_fail-1 #closest distance of (a, b) to an edge #is uh first_fail-1 #(n, m, x, y) #I think we can flip around so farthest corner is at (0, m-1) #so n-1-x+m-1-y = M #y = x+m-1-M #m-y = M-x+1 #and uh #n*m = t for m in range(1, t+1): if t % m==0: n = t//m y = n-1-x+m-1-M if 0 <= x <= n-1 and 0 <= y <= m-1 and M == max( abs(x)+abs(y), abs(x)+abs(m-1-y), abs(n-1-x)+abs(y), abs(n-1-x)+abs(m-1-y)): if test(n, m, x, y, d): return [n, m, x+1, y+1] return [-1, None, None, None] t = int(input()) A = [int(x) for x in input().split()] n, m, x, y = process(A) if n==-1: print('-1') else: print(f'{n} {m}') print(f'{x} {y}')
Codeforces Round 495 (Div. 2)
CF
2,018
2
256
Sonya and Matrix
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so. The Manhattan distance between two cells ($$$x_1$$$, $$$y_1$$$) and ($$$x_2$$$, $$$y_2$$$) is defined as $$$|x_1 - x_2| + |y_1 - y_2|$$$. For example, the Manhattan distance between the cells $$$(5, 2)$$$ and $$$(7, 1)$$$ equals to $$$|5-7|+|2-1|=3$$$. Example of a rhombic matrix. Note that rhombic matrices are uniquely defined by $$$n$$$, $$$m$$$, and the coordinates of the cell containing the zero. She drew a $$$n\times m$$$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $$$n\cdot m$$$ numbers). Note that Sonya will not give you $$$n$$$ and $$$m$$$, so only the sequence of numbers in this matrix will be at your disposal. Write a program that finds such an $$$n\times m$$$ rhombic matrix whose elements are the same as the elements in the sequence in some order.
The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^6$$$) — the number of cells in the matrix. The second line contains $$$t$$$ integers $$$a_1, a_2, \ldots, a_t$$$ ($$$0\leq a_i< t$$$) — the values in the cells in arbitrary order.
In the first line, print two positive integers $$$n$$$ and $$$m$$$ ($$$n \times m = t$$$) — the size of the matrix. In the second line, print two integers $$$x$$$ and $$$y$$$ ($$$1\leq x\leq n$$$, $$$1\leq y\leq m$$$) — the row number and the column number where the cell with $$$0$$$ is located. If there are multiple possible answers, print any of them. If there is no solution, print the single integer $$$-1$$$.
null
You can see the solution to the first example in the legend. You also can choose the cell $$$(2, 2)$$$ for the cell where $$$0$$$ is located. You also can choose a $$$5\times 4$$$ matrix with zero at $$$(4, 2)$$$. In the second example, there is a $$$3\times 6$$$ matrix, where the zero is located at $$$(2, 3)$$$ there. In the third example, a solution does not exist.
[{"input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4", "output": "4 5\n2 2"}, {"input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1", "output": "3 6\n2 3"}, {"input": "6\n2 1 0 2 1 2", "output": "-1"}]
2,300
["brute force", "constructive algorithms", "implementation"]
47
[{"input": "20\r\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\r\n", "output": "4 5\r\n2 2\r\n"}, {"input": "18\r\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\r\n", "output": "3 6\r\n2 3\r\n"}, {"input": "6\r\n2 1 0 2 1 2\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "1 1\r\n1 1\r\n"}, {"input": "7\r\n0 1 2 3 4 2 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n0 0 0 0 0 0\r\n", "output": "-1\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "-1\r\n"}]
false
stdio
import sys from collections import defaultdict def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_output_path): with open(input_path) as f: input_lines = f.read().splitlines() t = int(input_lines[0]) a = list(map(int, input_lines[1].split())) a_counts = defaultdict(int) for num in a: a_counts[num] += 1 max_d = max(a) if a else 0 zero_count = a_counts.get(0, 0) with open(submission_output_path) as f: submission_lines = [line.strip() for line in f.readlines() if line.strip()] if not submission_lines: print(0) return if submission_lines[0] == '-1': if zero_count != 1: print(100) return factors = [] for i in range(1, int(t**0.5) + 1): if t % i == 0: factors.append((i, t // i)) if i != t // i: factors.append((t // i, i)) seen = set() unique_factors = [] for n, m in factors: if (n, m) not in seen: seen.add((n, m)) unique_factors.append((n, m)) solution_found = False for n, m in unique_factors: if (n + m - 2) < max_d: continue a_val = n + m - 2 - max_d if a_val < 0: continue x_plus_y = a_val + 2 x_min = max(1, x_plus_y - m) x_max = min(n, x_plus_y - 1) if x_min > x_max: continue for x in range(x_min, x_max + 1): y = x_plus_y - x if y < 1 or y > m: continue expected_counts = defaultdict(int) expected_counts[0] = 1 for i in range(1, n + 1): for j in range(1, m + 1): if i == x and j == y: continue d = abs(i - x) + abs(j - y) expected_counts[d] += 1 valid = True if expected_counts[0] != zero_count: valid = False for d, cnt in expected_counts.items(): if a_counts.get(d, 0) != cnt: valid = False break for d in a_counts: if d != 0 and a_counts[d] != expected_counts.get(d, 0): valid = False break if valid: solution_found = True break if solution_found: break if solution_found: break print(0 if solution_found else 100) return else: if len(submission_lines) < 2: print(0) return try: n, m = map(int, submission_lines[0].split()) x, y = map(int, submission_lines[1].split()) except: print(0) return if n * m != t or x < 1 or x > n or y < 1 or y > m: print(0) return expected_counts = defaultdict(int) expected_counts[0] = 1 for i in range(1, n + 1): for j in range(1, m + 1): if i == x and j == y: continue d = abs(i - x) + abs(j - y) expected_counts[d] += 1 valid = True if expected_counts[0] != zero_count: valid = False for d, cnt in expected_counts.items(): if a_counts.get(d, 0) != cnt: valid = False break for d in a_counts: if d != 0 and a_counts[d] != expected_counts.get(d, 0): valid = False break print(100 if valid else 0) return if __name__ == "__main__": input_path, output_path, submission_output_path = sys.argv[1:4] main(input_path, output_path, submission_output_path)
true
990/F
990
F
PyPy 3-64
TESTS
0
31
0
186227210
from sys import stdin input=lambda :stdin.readline()[:-1] n=int(input()) a=list(map(int,input().split())) edges=[] edge=[[] for i in range(n)] m=int(input()) for _ in range(m): x,y=map(lambda x:int(x)-1,input().split()) edges.append((x,y)) edge[x].append(y) edge[y].append(x) use={} seen=[0]*n for i in range(n): if seen[i]: continue seen[i]=1 todo=[(0,i,-1)] while todo: t,v,p=todo.pop() if t==0: for u in edge[v]: if seen[u]==0: seen[u]=1 todo.append((1,u,v)) todo.append((0,u,v)) else: use[(v<<20)+p]=a[p] use[(p<<20)+v]=-a[p] a[p]+=a[v] a[v]=0 if a[i]!=0: print('Impossible') exit() print('Possible') for u,v in edges: if (u<<20)+v in use: print(use[(u<<20)+v]) else: print(0)
37
1,216
96,768,000
155788846
from collections import deque import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def get_root(s): v = [] while not s == root[s]: v.append(s) s = root[s] for i in v: root[i] = s return s def unite(s, t): rs, rt = get_root(s), get_root(t) if not rs ^ rt: return if rank[s] == rank[t]: rank[rs] += 1 if rank[s] >= rank[t]: root[rt] = rs size[rs] += size[rt] else: root[rs] = rt size[rt] += size[rs] return def same(s, t): return True if get_root(s) == get_root(t) else False def bfs(s): q = deque() q.append(s) visit = [0] * (n + 1) visit[s] = 1 parent = [-1] * (n + 1) p = [] while q: i = q.popleft() for j, k, l in G[i]: if not visit[j]: visit[j] = 1 q.append(j) parent[j] = i p.append((j, k, l)) return parent, p n = int(input()) s = [0] + list(map(int, input().split())) m = int(input()) e = [tuple(map(int, input().split())) for _ in range(m)] if sum(s): ans = "Impossible" print(ans) exit() ans = "Possible" print(ans) root = [i for i in range(n + 1)] rank = [1 for _ in range(n + 1)] size = [1 for _ in range(n + 1)] G = [[] for _ in range(n + 1)] for i in range(m): x, y = e[i] if not same(x, y): unite(x, y) G[x].append((y, 1, i)) G[y].append((x, -1, i)) parent, p = bfs(1) ans = [0] * m while p: i, k, l = p.pop() j = parent[i] ans[l] = k * s[i] s[j] += s[i] sys.stdout.write("\n".join(map(str, ans)))
Educational Codeforces Round 45 (Rated for Div. 2)
ICPC
2,018
2
256
Flow Control
You have to handle a very complex water distribution system. The system consists of $$$n$$$ junctions and $$$m$$$ pipes, $$$i$$$-th pipe connects junctions $$$x_i$$$ and $$$y_i$$$. The only thing you can do is adjusting the pipes. You have to choose $$$m$$$ integer numbers $$$f_1$$$, $$$f_2$$$, ..., $$$f_m$$$ and use them as pipe settings. $$$i$$$-th pipe will distribute $$$f_i$$$ units of water per second from junction $$$x_i$$$ to junction $$$y_i$$$ (if $$$f_i$$$ is negative, then the pipe will distribute $$$|f_i|$$$ units of water per second from junction $$$y_i$$$ to junction $$$x_i$$$). It is allowed to set $$$f_i$$$ to any integer from $$$-2 \cdot 10^9$$$ to $$$2 \cdot 10^9$$$. In order for the system to work properly, there are some constraints: for every $$$i \in [1, n]$$$, $$$i$$$-th junction has a number $$$s_i$$$ associated with it meaning that the difference between incoming and outcoming flow for $$$i$$$-th junction must be exactly $$$s_i$$$ (if $$$s_i$$$ is not negative, then $$$i$$$-th junction must receive $$$s_i$$$ units of water per second; if it is negative, then $$$i$$$-th junction must transfer $$$|s_i|$$$ units of water per second to other junctions). Can you choose the integers $$$f_1$$$, $$$f_2$$$, ..., $$$f_m$$$ in such a way that all requirements on incoming and outcoming flows are satisfied?
The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of junctions. The second line contains $$$n$$$ integers $$$s_1, s_2, \dots, s_n$$$ ($$$-10^4 \le s_i \le 10^4$$$) — constraints for the junctions. The third line contains an integer $$$m$$$ ($$$0 \le m \le 2 \cdot 10^5$$$) — the number of pipes. $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$) — the description of $$$i$$$-th pipe. It is guaranteed that each unordered pair $$$(x, y)$$$ will appear no more than once in the input (it means that there won't be any pairs $$$(x, y)$$$ or $$$(y, x)$$$ after the first occurrence of $$$(x, y)$$$). It is guaranteed that for each pair of junctions there exists a path along the pipes connecting them.
If you can choose such integer numbers $$$f_1, f_2, \dots, f_m$$$ in such a way that all requirements on incoming and outcoming flows are satisfied, then output "Possible" in the first line. Then output $$$m$$$ lines, $$$i$$$-th line should contain $$$f_i$$$ — the chosen setting numbers for the pipes. Pipes are numbered in order they appear in the input. Otherwise output "Impossible" in the only line.
null
null
[{"input": "4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1", "output": "Possible\n4\n-6\n8\n-7\n7"}, {"input": "4\n3 -10 6 4\n5\n1 2\n3 2\n2 4\n3 4\n3 1", "output": "Impossible"}]
2,400
["dfs and similar", "dp", "greedy", "trees"]
37
[{"input": "4\r\n3 -10 6 1\r\n5\r\n1 2\r\n3 2\r\n2 4\r\n3 4\r\n3 1\r\n", "output": "Possible\r\n-3\r\n-6\r\n1\r\n0\r\n0\r\n"}, {"input": "4\r\n3 -10 6 4\r\n5\r\n1 2\r\n3 2\r\n2 4\r\n3 4\r\n3 1\r\n", "output": "Impossible\r\n"}, {"input": "1\r\n0\r\n0\r\n", "output": "Possible\r\n"}, {"input": "1\r\n123\r\n0\r\n", "output": "Impossible\r\n"}, {"input": "2\r\n-1 1\r\n1\r\n1 2\r\n", "output": "Possible\r\n1\r\n"}, {"input": "2\r\n-1 1\r\n1\r\n2 1\r\n", "output": "Possible\r\n-1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline()) s = list(map(int, f.readline().split())) m = int(f.readline()) pipes = [tuple(map(int, f.readline().split())) for _ in range(m)] sum_s = sum(s) possible = (sum_s == 0) with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip() != ''] if not lines: print(0) return first_line = lines[0] if possible: if first_line != "Possible": print(0) return if len(lines) != m + 1: print(0) return try: f_values = [int(line) for line in lines[1:]] except: print(0) return flows_in = [0] * (n + 1) flows_out = [0] * (n + 1) for i in range(m): x, y = pipes[i] f = f_values[i] flows_in[y] += f flows_out[x] += f for u in range(1, n + 1): if (flows_in[u] - flows_out[u]) != s[u - 1]: print(0) return print(1) else: if first_line == "Impossible" and len(lines) == 1: print(1) else: print(0) if __name__ == '__main__': main()
true
486/C
486
C
PyPy 3-64
TESTS
3
46
0
227677847
# -*- coding : utf-8 -*- # @Time: 2023/10/11 19:34 # @Author: yefei.wang # @File: 486C.py import sys # sys.stdin = open('./../input.txt', 'r') I = lambda: int(input()) MI = lambda: map(int, input().split()) LI = lambda: list(map(int, input().split())) n, p = MI() p -= 1 s = input() i, j = 0, n - 1 idxs = [] ans = 0 while i < j: if s[i] != s[j]: dis = abs(ord(s[i]) - ord(s[j])) ans += min(dis, 26 - dis) idxs.append(i) i += 1 j -= 1 if not idxs: print(ans) t = n - 1 if p >= n // 2: p = n - p if len(idxs) == 1: t = min(t, abs(p - idxs[0])) else: if p <= idxs[0]: t = min(t, idxs[1] - p) elif p >= idxs[1]: t = min(t, p - idxs[0]) else: t = min(t, (p - idxs[0]) + idxs[1] - idxs[0], (idxs[1] - p) + idxs[1] - idxs[0]) ans += t print(ans)
40
77
2,560,000
229715065
from os import path from sys import stdin, stdout filename = "../templates/input.txt" if path.exists(filename): stdin = open(filename, 'r') def input(): return stdin.readline().rstrip() def print(*args, sep=' ', end='\n'): stdout.write(sep.join(map(str, args))) stdout.write(end) def solution(): n, p = [int(num) for num in input().split()] s = input() half = n // 2 if p <= half: left = 0 right = half else: right = n if n % 2 == 0: left = half else: left = half + 1 p -= 1 s1 = s[::-1] to_left = 0 to_right = 0 for i in range(left, right): if s[i] != s1[i]: to_left = max(to_left, p - i) to_right = max(to_right, i - p) ans = 0 if to_left == 0: ans = to_right elif to_right == 0: ans = to_left else: ans = min(to_left, to_right) + to_left + to_right for i in range(half): ans += min((ord(s[i]) - ord(s1[i])) % 26, (ord(s1[i]) - ord(s[i])) % 26) print(ans) def main(): t = 1 while t: solution() t -= 1 if __name__ == '__main__': main()
Codeforces Round 277 (Div. 2)
CF
2,014
1
256
Palindrome Transformation
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down. There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string). When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key. Initially, the text cursor is at position p. Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor. The next line contains n lowercase characters of Nam's string.
Print the minimum number of presses needed to change string into a palindrome.
null
A string is a palindrome if it reads the same forward or reversed. In the sample test, initial Nam's string is: $$aeabcaez$$ (cursor position is shown bold). In optimal solution, Nam may do 6 following steps: $$\begin{array}{cccc} \text{aeabcaez} & \xrightarrow{\text{right}} & \text{aeabcaez} & \xrightarrow{\text{up}} \\ & & & \text{aeaccaez} & \xrightarrow{\text{left}} \\ \text{aeaccaez} & \xrightarrow{\text{left}} & \text{aeaccaez} & \xrightarrow{\text{left}} \\ & & & \text{aeaccaez} & \xrightarrow{\text{down}} \\ & & & & \text{zeaccaez} \end{array}$$ The result, $$\int e^{a} c \cdot c a e^{v} d v$$, is now a palindrome.
[{"input": "8 3\naeabcaez", "output": "6"}]
1,700
["brute force", "greedy", "implementation"]
40
[{"input": "8 3\r\naeabcaez\r\n", "output": "6\r\n"}, {"input": "8 3\r\nabcddcbb\r\n", "output": "3\r\n"}, {"input": "4 4\r\nrkoa\r\n", "output": "14\r\n"}, {"input": "39 30\r\nyehuqwaffoiyxhkmdipxroolhahbhzprioobxfy\r\n", "output": "138\r\n"}, {"input": "40 23\r\nvwjzsgpdsopsrpsyccavfkyyahdgkmdxrquhcplw\r\n", "output": "169\r\n"}, {"input": "10 5\r\nabcdeedcba\r\n", "output": "0\r\n"}, {"input": "5 5\r\npjfjb\r\n", "output": "12\r\n"}, {"input": "57 9\r\nibkypcbtpdlhhpmghwrmuwaqoqxxexxqoqawumrwhgmphhldixezvfpqh\r\n", "output": "55\r\n"}, {"input": "10 6\r\nabcdefdcba\r\n", "output": "1\r\n"}, {"input": "167 152\r\nvqgjxbuxevpqbpnuyxktgpwdgyebnmrxbnitphshuloyykpgxakxadtguqskmhejndzptproeabnlvfwdyjiydfrjkxpvpbzwutsdpfawwcqqqirxwlkrectlnpdeccaoqetcaqcvyjtfoekyupvbsoiyldggycphddecbf\r\n", "output": "666\r\n"}, {"input": "93 61\r\nuecrsqsoylbotwcujcsbjohlyjlpjsjsnvttpytrvztqtkpsdcrvsossimwmglumwzpouhaiqvowthzsyonxjjearhniq\r\n", "output": "367\r\n"}, {"input": "63 4\r\nwzxjoumbtneztzheqznngprtcqjvawcycwavjqctrpgnnzqehztzentbmuojxzw\r\n", "output": "0\r\n"}, {"input": "85 19\r\nblkimwzicvbdkwfodvigvmnujnotwuobkjvugbtaseebxvdiorffqnhllwtwdnfodkuvdofwkdbvcizwmiklb\r\n", "output": "187\r\n"}, {"input": "198 3\r\ntuxqalctjyegbvouezfiqoeoazizhmjhpcmvyvjkyrgxkeupwcmvzcosdrrfgtdmxwfttxjxsbaspjwftgpnvsfyfqsrmyjmypdwonbzwsftepwtjlgbilhcsqyfzfzrfvrvfqiwoemthwvqptqnflqqspvqrnmvucnspexpijnivqpavqxjyucufcullevaedlvut\r\n", "output": "692\r\n"}, {"input": "46 29\r\nxxzkzsxlyhotmfjpptrilatgtqpyshraiycmyzzlrcllvu\r\n", "output": "168\r\n"}, {"input": "1 1\r\na\r\n", "output": "0\r\n"}, {"input": "2 2\r\nat\r\n", "output": "7\r\n"}, {"input": "10 4\r\nabcddddcef\r\n", "output": "11\r\n"}, {"input": "8 8\r\naccedcba\r\n", "output": "5\r\n"}, {"input": "1 1\r\nd\r\n", "output": "0\r\n"}]
false
stdio
null
true
384/B
384
B
PyPy 3
TESTS
1
77
0
206853412
n, m, descending = map(int, input().split()) arr = [] ptrs = [] solved = 0 for i in range(n): arr.append(list(map(int, input().split()))) ptrs.append([0, m-1]) res = [] for k in range(n): for i in range(m): for j in range(i+1, m): if arr[k][i] > arr[k][j]: res.append("%d %d" % (i+1, j+1)) arr[k][i], arr[k][j] = arr[k][j], arr[k][i] print(len(res)) for r in res: print(r)
31
78
409,600
5803767
n,m,k = map(int,input().split()) print(str(m*(m-1)//2)) for i in range(m): for j in range(m-i-1): if k == 0: print(str(i+1)+' '+str(i+j+2)) else: print(str(i+j+2)+' '+str(i+1))
Codeforces Round 225 (Div. 2)
CF
2,014
1
256
Multitasking
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers. Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j. Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $$\frac{m(m-1)}{2}$$ (at most $$\frac{m(m-1)}{2}$$ pairs). Help Iahub, find any suitable array.
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
On the first line of the output print an integer p, the size of the array (p can be at most $$\frac{m(m-1)}{2}$$). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices. If there are multiple correct answers, you can print any.
null
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
[{"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5", "output": "3\n2 4\n2 3\n4 5"}, {"input": "3 2 1\n1 2\n2 3\n3 4", "output": "1\n2 1"}]
1,500
["greedy", "implementation", "sortings", "two pointers"]
31
[{"input": "2 5 0\r\n1 3 2 5 4\r\n1 4 3 2 5\r\n", "output": "3\r\n2 4\r\n2 3\r\n4 5\r\n"}, {"input": "3 2 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 5 0\r\n836096 600367 472071 200387 79763\r\n714679 505282 233544 157810 152591\r\n", "output": "10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 3\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n"}, {"input": "2 5 1\r\n331081 525217 574775 753333 840639\r\n225591 347017 538639 620341 994088\r\n", "output": "10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n3 2\r\n4 2\r\n5 2\r\n4 3\r\n5 3\r\n5 4\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 0\r\n1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2 1\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 2 0\r\n2 1\r\n3 1\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 0\r\n2 1\r\n1 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 1\r\n2 1\r\n3 1\r\n", "output": "1\r\n2 1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: lines = f.read().splitlines() n, m, k = map(int, lines[0].split()) arrays = [list(map(int, line.split())) for line in lines[1:n+1]] try: with open(submission_path) as f: sub_lines = f.read().splitlines() except: print(0) return if not sub_lines: print(0) return try: p = int(sub_lines[0].strip()) if p < 0 or p > m * (m-1) // 2: print(0) return except: print(0) return swaps = [] for line in sub_lines[1:p+1]: parts = line.strip().split() if len(parts) != 2: print(0) return try: i = int(parts[0]) j = int(parts[1]) except: print(0) return if i < 1 or i > m or j < 1 or j > m or i == j: print(0) return swaps.append( (i-1, j-1) ) for arr in arrays: current = arr.copy() for i, j in swaps: if current[i] > current[j]: current[i], current[j] = current[j], current[i] valid = True if k == 0: for x in range(m-1): if current[x] > current[x+1]: valid = False break else: for x in range(m-1): if current[x] < current[x+1]: valid = False break if not valid: print(0) return print(1) if __name__ == "__main__": main()
true
298/A
298
A
Python 3
TESTS
3
62
0
165724169
i=input i() x='.'+i() z='L' if x.count('R'):z='R' a,b=x.find('R'),x.rfind('R') if z=='L':a,b=b,a-1 else:b+=1 print(a,b)
23
92
0
148448686
n = int(input()) road = input() a = road.find('R') + 1 t = 0 if a == 0: a = road.find('L') + 1 t = a - 1 for x in range(1, len(road)): if road[x] != 'R' and road[x-1] == 'R': if road[x] == '.': t = x+1 break else: t = x break print(a, t)
Codeforces Round 180 (Div. 2)
CF
2,013
1
256
Snow Footprints
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
The first line of the input contains integer n (3 ≤ n ≤ 1000). The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
null
The first test sample is the one in the picture.
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
1,300
["greedy", "implementation"]
23
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
false
stdio
import sys from collections import deque def main(input_path, output_path, submission_path): with open(input_path) as f: n = int(f.readline().strip()) road = f.readline().strip() with open(submission_path) as f: line = f.readline().strip() try: s, t = map(int, line.split()) except: print(0) return if s < 1 or s > n or t < 1 or t > n: print(0) return adj = [[] for _ in range(n+1)] for i in range(1, n+1): c = road[i-1] if c == 'R': if i+1 <= n: adj[i].append(i+1) elif c == 'L': if i-1 >= 1: adj[i].append(i-1) visited = [False] * (n+1) q = deque([s]) visited[s] = True while q: u = q.popleft() if u == t: print(1) return for v in adj[u]: if not visited[v]: visited[v] = True q.append(v) print(0) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
482/B
482
B
Python 3
TESTS
5
77
0
197456731
import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) n,m = invr() res = [0 for i in range(n)] c = [] for i in range(m): l,r,q = invr() c.append([l,r,q]) for i in range(30): mask = 1 << i prefix = [0 for i in range(n)] for l,r,q in c: if q&mask > 0: prefix[l-1] += 1 if r < n: prefix[r] -= 1 cur = 0 for i in range(n): cur += prefix[i] if cur > 0: res[i] = res[i] | mask flag = True for l,r,q in c: cur = res[l-1] for i in range(l,r): cur = cur and res[i] if cur !=q: flag = False break if flag: print("YES") s = "" for i in range(n): if i == n-1: s += str(res[i]) else: s += str(res[i]) s += " " print(s) else: print("NO")
38
982
50,278,400
223672399
import sys input = lambda: sys.stdin.readline().rstrip() from collections import Counter class SegTree: def __init__(self, n, arr): self.size = n self.tree = [0]*(n*2) self.build(arr) def build(self, arr): for i in range(self.size): self.tree[self.size+i] = arr[i] for i in range(self.size-1,0,-1): self.tree[i] = self.tree[i<<1]&self.tree[i<<1|1] def update(self,p,v): self.tree[p+self.size] = v p+=self.size i = p while i>1: self.tree[i>>1] = self.tree[i] & self.tree[i^1] i>>=1 # 不包含r def query(self,l,r): l += self.size r += self.size ans = self.tree[l] while l<r: if 1&l: ans &= self.tree[l] l+=1 if r&1: r-=1 ans &= self.tree[r] l>>=1 r>>=1 return ans N,M = map(int, input().split()) Q = [] A = [] for _ in range(M): l,r,q = map(int, input().split()) l-=1;r-=1 Q.append((l,r,q)) A.append((l,q,1)) A.append((r+1,q,-1)) A.sort(reverse=True) #print(A) ans,cur = [0]*N,0 cnt = [0]*30 for i in range(N): while A and A[-1][0]==i: j,c,t = A.pop() for k in range(30): if c&(1<<k): cnt[k]+=t for k in range(30): if cnt[k]>0: ans[i]+=1<<k #print(ans) seg = SegTree(N,ans) for l,r,q in Q: #print(seg.query(l,r+1),q) if seg.query(l,r+1)!=q: exit(print('NO')) print('YES') print(*ans)
Codeforces Round 275 (Div. 1)
CF
2,014
1
256
Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value $$a[l_{i}] \& a[l_{i}+1] \& \ldots \& a[r_{i}]$$ should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".
The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
null
null
[{"input": "3 1\n1 3 3", "output": "YES\n3 3 3"}, {"input": "3 2\n1 3 3\n1 3 2", "output": "NO"}]
1,800
["constructive algorithms", "data structures", "trees"]
38
[{"input": "3 1\r\n1 3 3\r\n", "output": "YES\r\n3 3 3\r\n"}, {"input": "3 2\r\n1 3 3\r\n1 3 2\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n1 2 536870912\r\n2 3 536870911\r\n", "output": "YES\r\n536870912 1073741823 536870911\r\n"}, {"input": "1 1\r\n1 1 10\r\n", "output": "YES\r\n10\r\n"}, {"input": "1 2\r\n1 1 10\r\n1 1 5\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n1 1 1\r\n1 1 3\r\n", "output": "NO\r\n"}]
false
stdio
null
true
296/B
296
B
Python 3
TESTS
3
92
5,529,600
28195381
n, s = int(input()), 0 s1, s2 = str(input()), str(input()) b1, b2 = False, False for i in range(n): if s1[i] != '?' and s2[i] != '?': if ord(s1[i]) < ord(s2[i]): b1 = True if ord(s1[i]) > ord(s2[i]): b2 = True s += (s1[i] == '?') + (s2[i] == '?') if b1 and b2: print((10 ** s) % (1000000007)) elif b1: ans = 1 for i in range(n): if s1[i] == '?' and s2[i] == '?': ans = (ans * 55) % 1000000007 elif s1[i] == '?': ans = (ans * (ord(s1[i]) - ord('0') + 1)) % 1000000007 elif s2[i] == '?': ans = (ans * (10 - ord(s2[i]) + ord('0'))) % 1000000007 print((10 ** s - ans) % 1000000007) elif b2: ans = 1 for i in range(n): if s1[i] == '?' and s2[i] == '?': ans = (ans * 55) % 1000000007 elif s1[i] == '?': ans = (ans * (10 - ord(s1[i]) + ord('0'))) % 1000000007 elif s2[i] == '?': ans = (ans * (ord(s2[i]) - ord('0') + 1)) % 1000000007 print((10 ** s - ans) % 1000000007) else: ans1 = 1 for i in range(n): if s1[i] == '?' and s2[i] == '?': ans1 = (ans1 * 55) % 1000000007 elif s1[i] == '?': ans1 = (ans1 * (ord(s1[i]) - ord('0') + 1)) % 1000000007 elif s2[i] == '?': ans1 = (ans1 * (10 - ord(s2[i]) + ord('0'))) % 1000000007 ans2 = 1 for i in range(n): if s1[i] == '?' and s2[i] == '?': ans2 = (ans2 * 55) % 1000000007 elif s1[i] == '?': ans2 = (ans2 * (10 - ord(s1[i]) + ord('0'))) % 1000000007 elif s2[i] == '?': ans2 = (ans2 * (ord(s2[i]) - ord('0') + 1)) % 1000000007 ans3 = 1 for i in range(n): if s1[i] == '?' and s2[i] == '?': ans3 = (ans3 * 10) % 1000000007 print((10 ** s - ans1 - ans2 + ans3) % 1000000007)
38
528
6,041,600
28195547
n, s = int(input()), 0 s1, s2 = str(input()), str(input()) b1, b2 = False, False for i in range(n): if s1[i] != '?' and s2[i] != '?': if ord(s1[i]) < ord(s2[i]): b1 = True if ord(s1[i]) > ord(s2[i]): b2 = True s += (s1[i] == '?') + (s2[i] == '?') ans1, ans2, ans3 = 1, 1, 1 for i in range(n): if s1[i] == '?' and s2[i] == '?': ans1 = (ans1 * 55) % 1000000007 ans2 = (ans2 * 55) % 1000000007 ans3 = (ans3 * 10) % 1000000007 elif s1[i] == '?': ans1 = (ans1 * (ord(s2[i]) - ord('0') + 1)) % 1000000007 ans2 = (ans2 * (10 - ord(s2[i]) + ord('0'))) % 1000000007 elif s2[i] == '?': ans1 = (ans1 * (10 - ord(s1[i]) + ord('0'))) % 1000000007 ans2 = (ans2 * (ord(s1[i]) - ord('0') + 1)) % 1000000007 print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and not b2) * ans3) % 1000000007)
Codeforces Round 179 (Div. 2)
CF
2,013
2
256
Yaroslav and Two Strings
Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s, similarly, wj represents the j-th digit of string w. A string's template is a string that consists of digits and question marks ("?"). Yaroslav has two string templates, each of them has length n. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers. Help Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007 (109 + 7).
The first line contains integer n (1 ≤ n ≤ 105) — the length of both templates. The second line contains the first template — a string that consists of digits and characters "?". The string's length equals n. The third line contains the second template in the same format.
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).
null
The first test contains no question marks and both strings are incomparable, so the answer is 1. The second test has no question marks, but the given strings are comparable, so the answer is 0.
[{"input": "2\n90\n09", "output": "1"}, {"input": "2\n11\n55", "output": "0"}, {"input": "5\n?????\n?????", "output": "993531194"}]
2,000
["combinatorics", "dp"]
38
[{"input": "2\r\n90\r\n09\r\n", "output": "1\r\n"}, {"input": "2\r\n11\r\n55\r\n", "output": "0\r\n"}, {"input": "5\r\n?????\r\n?????\r\n", "output": "993531194\r\n"}, {"input": "10\r\n104?3?1??3\r\n?1755?1??7\r\n", "output": "91015750\r\n"}, {"input": "10\r\n6276405116\r\n6787?352?9\r\n", "output": "46\r\n"}, {"input": "10\r\n0844033584\r\n0031021311\r\n", "output": "0\r\n"}, {"input": "10\r\n???0?19?01\r\n957461????\r\n", "output": "983368000\r\n"}, {"input": "10\r\n8703870339\r\n994987934?\r\n", "output": "9\r\n"}, {"input": "10\r\n?8?528?91?\r\n45??06???1\r\n", "output": "980398000\r\n"}, {"input": "10\r\n8030456630\r\n83406?6890\r\n", "output": "5\r\n"}, {"input": "1\r\n?\r\n?\r\n", "output": "0\r\n"}, {"input": "2\r\n12\r\n?9\r\n", "output": "1\r\n"}, {"input": "3\r\n??1\r\n?12\r\n", "output": "890\r\n"}, {"input": "3\r\n?12\r\n??1\r\n", "output": "890\r\n"}, {"input": "5\r\n??15?\r\n?32??\r\n", "output": "939500\r\n"}, {"input": "5\r\n??25?\r\n?32??\r\n", "output": "812550\r\n"}, {"input": "5\r\n??55?\r\n?32??\r\n", "output": "872950\r\n"}, {"input": "5\r\n?32??\r\n??55?\r\n", "output": "872950\r\n"}]
false
stdio
null
true
1006/C
1006
C
PyPy 3-64
TESTS
20
171
33,075,200
172291156
import sys from os import path if(path.exists('Input.txt')): sys.stdin = open("Input.txt","r") sys.stdout = open("Output.txt","w") from queue import PriorityQueue from math import gcd,floor,sqrt,log,ceil,inf import math from collections import * from collections import deque from bisect import bisect_left from bisect import bisect_right def li(): return list(map(int, sys.stdin.readline().split())) def mp(): return map(int, sys.stdin.readline().split()) def inp(): return int(sys.stdin.readline()) def st(): return list(sys.stdin.readline().strip()) def out(*l): return print(*l) def pr(n): return sys.stdout.write(str(n)+"\n") def prl(n): return sys.stdout.write(str(n)+" ") M = 1000000007 INF = float('inf') yes, no = "YES", "NO" import operator as op from functools import reduce def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, range(n, n-r, -1), 1) denom = reduce(op.mul, range(1, r+1), 1) return numer // denom def fact(n): return math.factorial(n) def perfect(n): return floor(sqrt(n))==ceil(sqrt(n)) def lcm(a, b): return a * b // gcd(a, b) def binary_search(array, target, start, end): while start <= end: mid = (start + end) // 2 if array[mid] == target: return mid elif array[mid] > target: end = mid - 1 else: start = mid + 1 return -1 def isPrime(n): if (n % 2 == 0 and n != 2) or n < 2: return False i = 3 while i * i <= n: if n % i == 0: return False i += 2 return True def expo(a, n, k): ans = 1 while(n > 0): last_bit = n&1 if(last_bit): ans = (ans*a)%k a = (a*a)%k n = n >> 1 return ans def prefix_sum(arr): n=len(arr) l = [arr[0]] for i in range(1,n): x = l[-1] l.append(x+arr[i]) return l def solve(): n=inp() l=li() a=0 b=n-1 mx = 0 x1=l[a] x2=l[b] while 1: if(x1==x2): mx=max(mx,x1) a+=1 if(a>=b): break x1+=l[a] if(x1>x2): b-=1 if(a>=b): break x2+=l[b] if(x2>x1): a+=1 if(a>=b): break x1+=l[a] pr(mx) for _ in range(1): #prl(f"Case #{_+1}:") solve()
27
109
21,094,400
209576531
n = int(input()) a = [int(i) for i in input().split()] i = 0 j = n - 1 si = a[i] sj = a[j] sum_max = 0 while i < j: if si < sj: i += 1 si += a[i] elif si > sj: j -= 1 sj += a[j] else: sum_max = si i += 1 j -= 1 si += a[i] sj += a[j] print(sum_max)
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Three Parts of the Array
You are given an array $$$d_1, d_2, \dots, d_n$$$ consisting of $$$n$$$ integer numbers. Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible. More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then: $$$$$$sum_1 = \sum\limits_{1 \le i \le a}d_i,$$$$$$ $$$$$$sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,$$$$$$ $$$$$$sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.$$$$$$ The sum of an empty array is $$$0$$$. Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^9$$$) — the elements of the array $$$d$$$.
Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$).
null
In the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$. In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$. In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$.
[{"input": "5\n1 3 1 1 4", "output": "5"}, {"input": "5\n1 3 2 1 4", "output": "4"}, {"input": "3\n4 1 2", "output": "0"}]
1,200
["binary search", "data structures", "two pointers"]
27
[{"input": "5\r\n1 3 1 1 4\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 2 1 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 1 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 3 5 4 5\r\n", "output": "9\r\n"}]
false
stdio
null
true
982/C
982
C
Python 3
TESTS
7
249
9,830,400
146896135
import sys, threading input = sys.stdin.readline def solve(n, graph): stack = [0] parents = {} parents[0] = -1 numNodes = [1]*n while stack: node = stack[-1] isleaf = True for nei in graph[node]: if nei not in parents: parents[nei] = node stack.append(nei) isleaf = False if isleaf: node = stack.pop() if node == 0: break numNodes[parents[node]] += numNodes[node] ans = -1 for nums in numNodes: if nums % 2 == 0: ans += 1 print(ans) n = int(input()) graph = [[] for _ in range(n)] for _ in range(n-1): u, v = map(int, input().split()) graph[u-1].append(v-1) graph[v-1].append(u-1) solve(n, graph)
45
218
15,564,800
177348176
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def bfs(s): q, k = [s], 0 visit = [0] * (n + 1) visit[s] = 1 parent = [-1] * (n + 1) while len(q) ^ k: i = q[k] for j in G[i]: if not visit[j]: visit[j] = 1 parent[j] = i q.append(j) k += 1 return q, parent n = int(input()) G = [[] for _ in range(n + 1)] for _ in range(n - 1): u, v = map(int, input().split()) G[u].append(v) G[v].append(u) p, parent = bfs(1) cnt = [1] * (n + 1) ans = 0 while len(p) ^ 1: i = p.pop() ans += (cnt[i] + 1) % 2 cnt[parent[i]] += cnt[i] if n % 2: ans = -1 print(ans)
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Cut 'em all!
You're given a tree with $$$n$$$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.
The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree.
Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property.
null
In the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$.
[{"input": "4\n2 4\n4 1\n3 1", "output": "1"}, {"input": "3\n1 2\n1 3", "output": "-1"}, {"input": "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "output": "4"}, {"input": "2\n1 2", "output": "0"}]
1,500
["dfs and similar", "dp", "graphs", "greedy", "trees"]
45
[{"input": "4\r\n2 4\r\n4 1\r\n3 1\r\n", "output": "1"}, {"input": "3\r\n1 2\r\n1 3\r\n", "output": "-1"}, {"input": "10\r\n7 1\r\n8 4\r\n8 10\r\n4 7\r\n6 5\r\n9 3\r\n3 5\r\n2 10\r\n2 5\r\n", "output": "4"}, {"input": "2\r\n1 2\r\n", "output": "0"}, {"input": "1\r\n", "output": "-1"}, {"input": "4\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "0"}]
false
stdio
null
true