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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
15/C
|
15
|
C
|
PyPy 3
|
TESTS
| 3 | 218 | 102,400 |
199123001
|
import sys
from math import *
from collections import Counter,defaultdict,deque
input=sys.stdin.readline
mod=10**9+7
def get_ints():return map(int,input().split())
def get_int():return int(input())
def get_array():return list(map(int,input().split()))
def input():return sys.stdin.readline().strip()
def getxor(a):
#find xor of all elements upto a inclusive
if a%4==0:
return a
if a%4==1:
return 1
if a%4==2:
return a+1
return 0
n=get_int()
x=[]
m=[]
ans=0
for i in range(n):
a,b=get_ints()
x.append(a)
m.append(b)
ans^=(a-1)
ans^=(a+b-1)
if ans==0:
print('bolik')
else:
print('tolik')
| 25 | 652 | 0 |
176449246
|
def f(x):
if x%4==0:
return x
elif x%4==1:
return 1
elif x%4==2:
return x+1
return 0
n = int(input())
res = 0
for i in range(n):
x,m = input().split()
x,m = int(x),int(m)
res ^= f(x-1)^f(x+m-1)
if res == 0:
print("bolik")
else:
print("tolik")
|
Codeforces Beta Round 15
|
ICPC
| 2,010 | 2 | 64 |
Industrial Nim
|
There are n stone quarries in Petrograd.
Each quarry owns mi dumpers (1 ≤ i ≤ n). It is known that the first dumper of the i-th quarry has xi stones in it, the second dumper has xi + 1 stones in it, the third has xi + 2, and the mi-th dumper (the last for the i-th quarry) has xi + mi - 1 stones in it.
Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses.
Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone «tolik» and the other one «bolik».
|
The first line of the input contains one integer number n (1 ≤ n ≤ 105) — the amount of quarries. Then there follow n lines, each of them contains two space-separated integers xi and mi (1 ≤ xi, mi ≤ 1016) — the amount of stones in the first dumper of the i-th quarry and the number of dumpers at the i-th quarry.
|
Output «tolik» if the oligarch who takes a stone first wins, and «bolik» otherwise.
| null | null |
[{"input": "2\n2 1\n3 2", "output": "tolik"}, {"input": "4\n1 1\n1 1\n1 1\n1 1", "output": "bolik"}]
| 2,000 |
["games"]
| 25 |
[{"input": "2\r\n2 1\r\n3 2\r\n", "output": "tolik\r\n"}, {"input": "4\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "bolik\r\n"}, {"input": "10\r\n2 3\r\n1 4\r\n5 8\r\n4 10\r\n10 8\r\n7 2\r\n1 2\r\n1 7\r\n4 10\r\n5 3\r\n", "output": "tolik\r\n"}, {"input": "20\r\n8 6\r\n6 3\r\n2 9\r\n7 8\r\n9 1\r\n2 4\r\n3 6\r\n6 3\r\n5 6\r\n5 3\r\n6 5\r\n2 10\r\n2 9\r\n6 3\r\n10 6\r\n10 10\r\n10 7\r\n3 9\r\n16 1\r\n1 3\r\n", "output": "bolik\r\n"}, {"input": "30\r\n53 12\r\n13 98\r\n21 60\r\n76 58\r\n39 5\r\n62 58\r\n73 80\r\n13 75\r\n37 45\r\n44 86\r\n1 85\r\n13 33\r\n17 50\r\n12 26\r\n97 48\r\n52 40\r\n2 71\r\n95 79\r\n38 76\r\n24 54\r\n91 39\r\n97 92\r\n94 80\r\n50 61\r\n33 56\r\n22 91\r\n39 94\r\n31 56\r\n28 16\r\n20 44\r\n", "output": "tolik\r\n"}, {"input": "1\r\n3737203222172202 1\r\n", "output": "tolik\r\n"}, {"input": "1\r\n3737203222172202 1\r\n", "output": "tolik\r\n"}]
| false |
stdio
| null | true |
245/A
|
245
|
A
|
Python 3
|
TESTS
| 4 | 186 | 0 |
57317151
|
n=int(input())
t1=0
t2=0
s1=0
s2=0
for i in range(n):
a=list(map(int,input().split()))
if a[0]==1:
t1=t2+1
s1=s1+a[1]
else:
t2=t2+1
s2=s2+a[1]
if 2*s1>=t1*10:
print("LIVE")
else:
print("DEAD")
if 2*s2>=t2*10:
print("LIVE")
else:
print("DEAD")
| 13 | 62 | 0 |
153017830
|
n = int(input())
a_suc = 0
a_fai = 0
b_suc = 0
b_fai = 0
for i in range(n):
s = list(map(int, input().split()))
if s[0] == 1:
a_suc+=s[1]
a_fai+=s[2]
else:
b_suc+=s[1]
b_fai+=s[2]
if a_suc>=a_fai:
print("LIVE")
else:
print("DEAD")
if b_suc>=b_fai:
print("LIVE")
else:
print("DEAD")
|
CROC-MBTU 2012, Elimination Round (ACM-ICPC)
|
ICPC
| 2,012 | 2 | 256 |
System Administrator
|
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
|
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
|
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
| null |
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
[{"input": "2\n1 5 5\n2 6 4", "output": "LIVE\nLIVE"}, {"input": "3\n1 0 10\n2 0 10\n1 10 0", "output": "LIVE\nDEAD"}]
| 800 |
["implementation"]
| 23 |
[{"input": "2\r\n1 5 5\r\n2 6 4\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "3\r\n1 0 10\r\n2 0 10\r\n1 10 0\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "10\r\n1 3 7\r\n2 4 6\r\n1 2 8\r\n2 5 5\r\n2 10 0\r\n2 10 0\r\n1 8 2\r\n2 2 8\r\n2 10 0\r\n1 1 9\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "11\r\n1 8 2\r\n1 6 4\r\n1 9 1\r\n1 7 3\r\n2 0 10\r\n2 0 10\r\n1 8 2\r\n2 2 8\r\n2 6 4\r\n2 7 3\r\n2 9 1\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "12\r\n1 5 5\r\n1 0 10\r\n1 4 6\r\n1 2 8\r\n1 2 8\r\n1 5 5\r\n1 9 1\r\n2 9 1\r\n1 5 5\r\n1 1 9\r\n2 9 1\r\n2 7 3\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "13\r\n1 8 2\r\n1 4 6\r\n1 5 5\r\n1 5 5\r\n2 10 0\r\n2 9 1\r\n1 3 7\r\n2 6 4\r\n2 6 4\r\n2 5 5\r\n1 7 3\r\n2 3 7\r\n2 9 1\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "14\r\n1 7 3\r\n1 0 10\r\n1 7 3\r\n1 1 9\r\n2 2 8\r\n2 0 10\r\n1 1 9\r\n2 8 2\r\n2 6 4\r\n1 3 7\r\n1 3 7\r\n2 6 4\r\n2 1 9\r\n2 7 3\r\n", "output": "DEAD\r\nDEAD\r\n"}]
| false |
stdio
| null | true |
245/A
|
245
|
A
|
PyPy 3
|
TESTS
| 5 | 186 | 0 |
128193309
|
T=int(input())
a=sa=0
b=sb=0
for z in range(T):
t,x,y=[int(v) for v in input().split()]
if t==1:
a+=x
sa+=5
elif t==2:
b+=x
sb+=5
if a<sa: print("DEAD")
elif a>=sb: print("LIVE")
if b<sb: print("DEAD")
elif b>=sb: print("LIVE")
| 13 | 62 | 0 |
153794509
|
n = int(input())
x1, y1, x2, y2 = 0, 0, 0, 0
for i in [0]*n:
t,x,y = list(map(int, input().split()))
if t == 1:
x1 += x
y1 += y
else:
x2 += x
y2 += y
if x1 >= y1:
print("LIVE")
else:
print("DEAD")
if x2 >= y2:
print("LIVE")
else:
print("DEAD")
|
CROC-MBTU 2012, Elimination Round (ACM-ICPC)
|
ICPC
| 2,012 | 2 | 256 |
System Administrator
|
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
|
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
|
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
| null |
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
[{"input": "2\n1 5 5\n2 6 4", "output": "LIVE\nLIVE"}, {"input": "3\n1 0 10\n2 0 10\n1 10 0", "output": "LIVE\nDEAD"}]
| 800 |
["implementation"]
| 23 |
[{"input": "2\r\n1 5 5\r\n2 6 4\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "3\r\n1 0 10\r\n2 0 10\r\n1 10 0\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "10\r\n1 3 7\r\n2 4 6\r\n1 2 8\r\n2 5 5\r\n2 10 0\r\n2 10 0\r\n1 8 2\r\n2 2 8\r\n2 10 0\r\n1 1 9\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "11\r\n1 8 2\r\n1 6 4\r\n1 9 1\r\n1 7 3\r\n2 0 10\r\n2 0 10\r\n1 8 2\r\n2 2 8\r\n2 6 4\r\n2 7 3\r\n2 9 1\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "12\r\n1 5 5\r\n1 0 10\r\n1 4 6\r\n1 2 8\r\n1 2 8\r\n1 5 5\r\n1 9 1\r\n2 9 1\r\n1 5 5\r\n1 1 9\r\n2 9 1\r\n2 7 3\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "13\r\n1 8 2\r\n1 4 6\r\n1 5 5\r\n1 5 5\r\n2 10 0\r\n2 9 1\r\n1 3 7\r\n2 6 4\r\n2 6 4\r\n2 5 5\r\n1 7 3\r\n2 3 7\r\n2 9 1\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "14\r\n1 7 3\r\n1 0 10\r\n1 7 3\r\n1 1 9\r\n2 2 8\r\n2 0 10\r\n1 1 9\r\n2 8 2\r\n2 6 4\r\n1 3 7\r\n1 3 7\r\n2 6 4\r\n2 1 9\r\n2 7 3\r\n", "output": "DEAD\r\nDEAD\r\n"}]
| false |
stdio
| null | true |
408/A
|
408
|
A
|
PyPy 3
|
TESTS
| 4 | 92 | 20,172,800 |
115758687
|
n=int(input())
l=[]
mini=1e9
for i in range(n):
l.append(list(map(int,input().split(' '))))
mini=min(mini,sum(l[i])*5+len(l[i])*15)
print(mini)
| 20 | 31 | 102,400 |
215725327
|
n=int(input())
k=list(map(int,input().split()))
b=[]
for i in range(n):
m=list(map(int,input().split()))
t = sum(m)*5 + len(m)*15
b.append(t)
print(min(b))
|
Codeforces Round 239 (Div. 2)
|
CF
| 2,014 | 1 | 256 |
Line to Cashier
|
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.
There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that:
- the cashier needs 5 seconds to scan one item;
- after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change.
Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.
|
The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier.
The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has.
|
Print a single integer — the minimum number of seconds Vasya needs to get to the cashier.
| null |
In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
|
[{"input": "1\n1\n1", "output": "20"}, {"input": "4\n1 4 3 2\n100\n1 2 2 3\n1 9 1\n7 8", "output": "100"}]
| 900 |
["implementation"]
| 20 |
[{"input": "1\r\n1\r\n1\r\n", "output": "20\r\n"}, {"input": "4\r\n1 4 3 2\r\n100\r\n1 2 2 3\r\n1 9 1\r\n7 8\r\n", "output": "100\r\n"}, {"input": "4\r\n5 4 5 5\r\n3 1 3 1 2\r\n3 1 1 3\r\n1 1 1 2 2\r\n2 2 1 1 3\r\n", "output": "100\r\n"}, {"input": "5\r\n5 3 6 6 4\r\n7 5 3 3 9\r\n6 8 2\r\n1 10 8 5 9 2\r\n9 7 8 5 9 10\r\n9 8 3 3\r\n", "output": "125\r\n"}, {"input": "5\r\n10 10 10 10 10\r\n6 7 8 6 8 5 9 8 10 5\r\n9 6 9 8 7 8 8 10 8 5\r\n8 7 7 8 7 5 6 8 9 5\r\n6 5 10 5 5 10 7 8 5 5\r\n10 9 8 7 6 9 7 9 6 5\r\n", "output": "480\r\n"}, {"input": "10\r\n9 10 10 10 9 5 9 7 8 7\r\n11 6 10 4 4 15 7 15 5\r\n3 9 11 12 11 1 13 13 1 5\r\n6 15 9 12 3 2 8 12 11 10\r\n7 1 1 6 10 2 6 1 14 2\r\n8 14 2 3 6 1 14 1 12\r\n6 10 9 3 5\r\n13 12 12 7 13 4 4 8 10\r\n5 6 4 3 14 9 13\r\n8 12 1 5 7 4 13 1\r\n1 9 5 3 5 1 4\r\n", "output": "240\r\n"}, {"input": "10\r\n5 5 5 5 5 5 5 5 5 5\r\n5 5 4 5 4\r\n6 5 7 7 6\r\n5 4 4 5 5\r\n4 4 5 5 5\r\n7 6 4 5 7\r\n4 6 5 4 5\r\n6 6 7 6 6\r\n4 5 4 4 7\r\n7 5 4 4 5\r\n6 6 7 4 4\r\n", "output": "190\r\n"}, {"input": "1\r\n1\r\n100\r\n", "output": "515\r\n"}, {"input": "1\r\n90\r\n90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90\r\n", "output": "41850\r\n"}]
| false |
stdio
| null | true |
245/A
|
245
|
A
|
Python 3
|
TESTS
| 4 | 216 | 0 |
50897702
|
s1,r1=0,0
s2,r2=0,0
for _ in range(int(input())):
t,s,l=map(int,input().split())
if t==1:
s1+=10
r1+=s
else:
s2+=10
r2+=s
if 2*r1>=s1:
print('LIVE')
else:
print('DEAD')
if 2*r2>=s1:
print('LIVE')
else:
print('DEAD')
| 13 | 62 | 0 |
174286772
|
n_pings = int(input())
packets = [[0, 0], [0, 0]] # ((a_success, a_total), (b_success, b_total))
for _ in range(n_pings):
t, x, y = map(int, input().split())
packets[t - 1][0] += x
packets[t - 1][1] += x + y
if packets[0][1] == 0 or packets[0][0] / packets[0][1] >= 0.5:
print("LIVE")
else:
print("DEAD")
if packets[1][1] == 0 or packets[1][0] / packets[1][1] >= 0.5:
print("LIVE")
else:
print("DEAD")
|
CROC-MBTU 2012, Elimination Round (ACM-ICPC)
|
ICPC
| 2,012 | 2 | 256 |
System Administrator
|
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
|
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
|
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
| null |
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
[{"input": "2\n1 5 5\n2 6 4", "output": "LIVE\nLIVE"}, {"input": "3\n1 0 10\n2 0 10\n1 10 0", "output": "LIVE\nDEAD"}]
| 800 |
["implementation"]
| 23 |
[{"input": "2\r\n1 5 5\r\n2 6 4\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "3\r\n1 0 10\r\n2 0 10\r\n1 10 0\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "10\r\n1 3 7\r\n2 4 6\r\n1 2 8\r\n2 5 5\r\n2 10 0\r\n2 10 0\r\n1 8 2\r\n2 2 8\r\n2 10 0\r\n1 1 9\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "11\r\n1 8 2\r\n1 6 4\r\n1 9 1\r\n1 7 3\r\n2 0 10\r\n2 0 10\r\n1 8 2\r\n2 2 8\r\n2 6 4\r\n2 7 3\r\n2 9 1\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "12\r\n1 5 5\r\n1 0 10\r\n1 4 6\r\n1 2 8\r\n1 2 8\r\n1 5 5\r\n1 9 1\r\n2 9 1\r\n1 5 5\r\n1 1 9\r\n2 9 1\r\n2 7 3\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "13\r\n1 8 2\r\n1 4 6\r\n1 5 5\r\n1 5 5\r\n2 10 0\r\n2 9 1\r\n1 3 7\r\n2 6 4\r\n2 6 4\r\n2 5 5\r\n1 7 3\r\n2 3 7\r\n2 9 1\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "14\r\n1 7 3\r\n1 0 10\r\n1 7 3\r\n1 1 9\r\n2 2 8\r\n2 0 10\r\n1 1 9\r\n2 8 2\r\n2 6 4\r\n1 3 7\r\n1 3 7\r\n2 6 4\r\n2 1 9\r\n2 7 3\r\n", "output": "DEAD\r\nDEAD\r\n"}]
| false |
stdio
| null | true |
245/A
|
245
|
A
|
Python 3
|
TESTS
| 4 | 216 | 0 |
51288414
|
n=int(input())
a,b=0,0
sa,sb=0,0
for i in range(n):
t,x,y=map(int,input().split())
if t==1:
a+=x
sa+=10
else:
b+=x
sb+=10
if a>=sa//2:
print("LIVE")
else:
print("DEAD")
if b>=sa//2:
print("LIVE")
else:
print("DEAD")
| 13 | 78 | 102,400 |
3686050
|
n=int(input())
ta,tb,da,db=[0]*4
for i in range (n):
t,x,y=map(int,input().split())
if t==1:
ta+=(x+y)
da+=y
if (t==2):
tb+=(x+y)
db+=y
if (ta-da>=0.5*ta):
print ('LIVE')
else :
print ('DEAD')
if (tb-db>=0.5*tb):
print ('LIVE')
else :
print ('DEAD')
|
CROC-MBTU 2012, Elimination Round (ACM-ICPC)
|
ICPC
| 2,012 | 2 | 256 |
System Administrator
|
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
|
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
|
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
| null |
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
[{"input": "2\n1 5 5\n2 6 4", "output": "LIVE\nLIVE"}, {"input": "3\n1 0 10\n2 0 10\n1 10 0", "output": "LIVE\nDEAD"}]
| 800 |
["implementation"]
| 23 |
[{"input": "2\r\n1 5 5\r\n2 6 4\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "3\r\n1 0 10\r\n2 0 10\r\n1 10 0\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "10\r\n1 3 7\r\n2 4 6\r\n1 2 8\r\n2 5 5\r\n2 10 0\r\n2 10 0\r\n1 8 2\r\n2 2 8\r\n2 10 0\r\n1 1 9\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "11\r\n1 8 2\r\n1 6 4\r\n1 9 1\r\n1 7 3\r\n2 0 10\r\n2 0 10\r\n1 8 2\r\n2 2 8\r\n2 6 4\r\n2 7 3\r\n2 9 1\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "12\r\n1 5 5\r\n1 0 10\r\n1 4 6\r\n1 2 8\r\n1 2 8\r\n1 5 5\r\n1 9 1\r\n2 9 1\r\n1 5 5\r\n1 1 9\r\n2 9 1\r\n2 7 3\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "13\r\n1 8 2\r\n1 4 6\r\n1 5 5\r\n1 5 5\r\n2 10 0\r\n2 9 1\r\n1 3 7\r\n2 6 4\r\n2 6 4\r\n2 5 5\r\n1 7 3\r\n2 3 7\r\n2 9 1\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "14\r\n1 7 3\r\n1 0 10\r\n1 7 3\r\n1 1 9\r\n2 2 8\r\n2 0 10\r\n1 1 9\r\n2 8 2\r\n2 6 4\r\n1 3 7\r\n1 3 7\r\n2 6 4\r\n2 1 9\r\n2 7 3\r\n", "output": "DEAD\r\nDEAD\r\n"}]
| false |
stdio
| null | true |
900/E
|
900
|
E
|
Python 3
|
TESTS
| 3 | 62 | 5,529,600 |
33391583
|
n = int(input())
s = list(input())
t = int(input())
re = s.copy()
kras = 'ab'
kras *= t
kras = kras[:t]
col = s.count('?')
c = 0
v = 0
q = 0
h = 0
for i in range(2 ** col):
shable = bin(i)[2:]
priv = '0' * (col - len(shable))
priv += shable
shable = priv
for el in range(n):
if s[el] == '?':
if shable[q] == '1':
h += 1
if el % 2 == 0:
s[el] = 'a'
else:
s[el] = 'b'
q += 1
new = ''.join(s).count(kras)
if new > c:
v = h
c = new
s = re.copy()
q = 0
h = 0
print(v)
| 96 | 202 | 18,022,400 |
218078541
|
n = int(input())
s = input()
m = int(input())
dp = [[0, 0] for _ in range(n+1)]
c = [[0, 0] for _ in range(n+1)]
q = [0] * (n+1)
for i in range(1, n+1):
q[i] = q[i-1] + (s[i-1] == '?')
if s[i-1] != 'b':
c[i][1] = c[i-1][0] + 1
if s[i-1] != 'a':
c[i][0] = c[i-1][1] + 1
if c[i][m&1] >= m:
dp[i] = [dp[i-m][0] + 1, dp[i-m][1] - q[i] + q[i-m]]
dp[i] = max(dp[i], dp[i-1])
print(-dp[n][1])
|
Codeforces Round 450 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Maximum Questions
|
Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form "abab...", namely there are letters 'a' on odd positions and letters 'b' on even positions.
Suddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'.
Let's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 ≤ i ≤ n - m + 1 and t1 = si, t2 = si + 1, ..., tm = si + m - 1.
The boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make.
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of s.
The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only.
The third line contains a single integer m (1 ≤ m ≤ 105) — the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions.
|
Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible.
| null |
In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'.
In the second sample using two replacements we can make string equal to "aba?aba??". It is impossible to get more than two occurrences.
|
[{"input": "5\nbb?a?\n1", "output": "2"}, {"input": "9\nab??ab???\n3", "output": "2"}]
| 2,100 |
["data structures", "dp", "strings"]
| 96 |
[{"input": "5\r\nbb?a?\r\n1\r\n", "output": "2\r\n"}, {"input": "9\r\nab??ab???\r\n3\r\n", "output": "2\r\n"}, {"input": "6\r\nab??ab\r\n4\r\n", "output": "2\r\n"}, {"input": "14\r\n?abaa?abb?b?a?\r\n3\r\n", "output": "3\r\n"}, {"input": "17\r\nb??a?abbbaaababba\r\n4\r\n", "output": "1\r\n"}, {"input": "1\r\nb\r\n1\r\n", "output": "0\r\n"}, {"input": "3\r\nb?a\r\n1\r\n", "output": "1\r\n"}, {"input": "12\r\naba?bbaaabbb\r\n1\r\n", "output": "1\r\n"}, {"input": "43\r\n????aabaababa?aaaa?abbbabbb?ab??baabbbbbabb\r\n5\r\n", "output": "4\r\n"}, {"input": "36\r\nbbaa??aab?aabbb?ba?b?bba?bbaa??bb?ab\r\n4\r\n", "output": "4\r\n"}, {"input": "14\r\na?a?b????b?ba?\r\n3\r\n", "output": "7\r\n"}, {"input": "47\r\na??a??abbaaa?a??aaabaa?abbbbb??abb??aa?abb?bbaa\r\n4\r\n", "output": "8\r\n"}, {"input": "29\r\n?bba?ab?b?bbbbaa?a?bba?aab?a?\r\n4\r\n", "output": "5\r\n"}, {"input": "69\r\nbaba??aab????aab??b?aaaaaaab?b?ab?baaabbabba?b??aaabba?aba?bbba?abbb?\r\n3\r\n", "output": "13\r\n"}, {"input": "63\r\nbb??b?a?aaaaaaab?b??abb?a??a?bb??b?b?ab???ab?aaa?bb??ba?abbba?a\r\n5\r\n", "output": "10\r\n"}, {"input": "53\r\n???a?aa?bb?ab???ba?bab????abaa??babbbb?ba?ab?abb??bab\r\n2\r\n", "output": "17\r\n"}, {"input": "46\r\nbbbbaaaaabb?ba?b?????abb?abbbbaaa?b?aab??b?bab\r\n1\r\n", "output": "13\r\n"}, {"input": "219\r\n????aa??bb?abb?a?a?b?abb?a?ba?b?ba?baa?bb?b?b?abba?????aaab??aa?b?a?bbb?a?b?abbb??aa???aabbaabbab?aab?a?b?aa?bb?ababa?aaa?a??b?bab?babbbba?a?a?b?aab?a?a?baabbbbbba??a?aab?baaab??babb?aab?babaabaaab?a?a??bba?bb?a?b?abbba\r\n12\r\n", "output": "4\r\n"}, {"input": "63\r\nbb????aa?b?b?aabaa??b??b?baa?ba??bbbbaab??b?baa??baaa???baa???a\r\n6\r\n", "output": "7\r\n"}, {"input": "228\r\na?aa???aa?a??ba??a?bba?aaabbb?aaa??aabb??abaa?a?a?aaaaaaa??aa?a?baabbaa??aa?aabaab?aba??b??b?a??b????a???baa??b?aaababb????abbababa???ab??babbb?a??babba?a??bbb?bbaa??a??aa??b?bbb?bab?a?b????b??babb??b?b?aaa?abbbba??aaba?baaaaa??\r\n8\r\n", "output": "17\r\n"}, {"input": "112\r\n??????ab????aaab?a?aa?babb??b?b?b?baaab?bbba?ab?a????bbabb?abaa?bab?ab???b??ba???aabbbab??b?ab?bba???abaaaa?aba?\r\n2\r\n", "output": "37\r\n"}, {"input": "340\r\nbaa?b?abab??ab??aaabaa???bbbb??abaaaba?a?b?bb?ab?bbaa??aaaa???aaa?b???ba?a??b?bb?bbbabb?bb?a?a?bbbabbba?b?ababbb?b?a??bbb??bb?ababb?abbbbba??aabbaab?aaa??a???bbaa?bb?bb?babaa?bb?a???b?abbb???bb?a?a??b?b?abbba?b??a?bab??baa?aabaabb?abbbab?aa???bbaab?bbab?ba?aab?b?baabb???aaa??bb?ab?aa?aaa????babbbb???babbab?ab????a??bab?baaa?aaaaaaa?a??aab\r\n1\r\n", "output": "114\r\n"}, {"input": "9\r\n?????aba?\r\n2\r\n", "output": "5\r\n"}]
| false |
stdio
| null | true |
56/C
|
56
|
C
|
Python 3
|
TESTS
| 2 | 218 | 0 |
118316634
|
s = list(input())
tp = []
result = 0
for i in range(len(s) - 1, 0, -1):
if '.' == s[i] and '.' != s[i - 1]:
st = 0
for j in range(i - 1, 0, -1):
if ':' == s[j] or ',' == s[j]:
st = j + 1
break
name = ''.join(s[st:i])
for j in range(i - 1, 0, -1):
if ':' == s[j]:
st = 0
for k in range(j - 1, 0, -1):
if ',' == s[k]:
st = k + 1
break
p_name = ''.join(s[st:j])
if name == p_name:
result += 1
print(result)
| 37 | 124 | 4,608,000 |
21246786
|
def P():
n=a[0];a.pop(0);d={n:1};r=0
while a[0]!='.':
a.pop(0);l,t=P();r+=l
for k in t.keys():
d[k]=d.get(k,0)+t[k]
if k==n:r+=t[k]
a.pop(0)
return r,d
a=[]
x=''
for i in input():
if i in ':.,':
if x:a+=[x]
a+=[i];x=''
else:x+=i
print(P()[0])
|
Codeforces Beta Round 52 (Div. 2)
|
CF
| 2,011 | 2 | 256 |
Corporation Mail
|
The Beroil corporation structure is hierarchical, that is it can be represented as a tree. Let's examine the presentation of this structure as follows:
- employee ::= name. | name:employee1,employee2, ... ,employeek.
- name ::= name of an employee
That is, the description of each employee consists of his name, a colon (:), the descriptions of all his subordinates separated by commas, and, finally, a dot. If an employee has no subordinates, then the colon is not present in his description.
For example, line MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY... is the correct way of recording the structure of a corporation where the director MIKE has subordinates MAX, ARTEM and DMITRY. ARTEM has a subordinate whose name is MIKE, just as the name of his boss and two subordinates of DMITRY are called DMITRY, just like himself.
In the Beroil corporation every employee can only correspond with his subordinates, at that the subordinates are not necessarily direct. Let's call an uncomfortable situation the situation when a person whose name is s writes a letter to another person whose name is also s. In the example given above are two such pairs: a pair involving MIKE, and two pairs for DMITRY (a pair for each of his subordinates).
Your task is by the given structure of the corporation to find the number of uncomfortable pairs in it.
|
The first and single line contains the corporation structure which is a string of length from 1 to 1000 characters. It is guaranteed that the description is correct. Every name is a string consisting of capital Latin letters from 1 to 10 symbols in length.
|
Print a single number — the number of uncomfortable situations in the company.
| null | null |
[{"input": "MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY...", "output": "3"}, {"input": "A:A..", "output": "1"}, {"input": "A:C:C:C:C.....", "output": "6"}]
| 1,700 |
["data structures", "expression parsing", "implementation"]
| 37 |
[{"input": "MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY...\r\n", "output": "3\r\n"}, {"input": "A:A..\r\n", "output": "1\r\n"}, {"input": "A:C:C:C:C.....\r\n", "output": "6\r\n"}, {"input": "CK:CK.,CK.,CK..\r\n", "output": "3\r\n"}, {"input": "FHMULVSDP:FHMULVSDP:FHMULVSDP:FHMULVSDP..,FHMULVSDP...\r\n", "output": "8\r\n"}, {"input": "RHLGWEVBJ:KAWUINWEI:KAWUINWEI..,ZQATMW.,KAWUINWEI.,RSWN..\r\n", "output": "1\r\n"}, {"input": "AELJZZL:AELJZZL:FARF.,FARF:FARF.,TVDWKGTR.,AELJZZL....\r\n", "output": "4\r\n"}, {"input": "GIRRY.\r\n", "output": "0\r\n"}, {"input": "TP:ZIEIN:TP.,ZIEIN:RHHYDAYV....\r\n", "output": "2\r\n"}, {"input": "XGB:QJNGARRAZV:DWGDCCU:ARDKJV:P:MXBLZKLPYI:FKSBDQVXH:FKSBDQVXH:MXBLZKLPYI..,DWGDCCU..,P...,FKSBDQVXH....,ARDKJV..\r\n", "output": "4\r\n"}, {"input": "XVHMYEWTR:XVHMYEWTR:XVHMYEWTR:XVHMYEWTR....\r\n", "output": "6\r\n"}, {"input": "DCMEHQUK:QRTMRD:INZPGQ:ETO:QRTMRD:IK:CLADXUDO:RBXLIZ.,HXZEVZAVQ:HXZEVZAVQ:VLLQPTOJ:QRTMRD:IK:RBXLIZ:CLADXUDO.,DCMEHQUK....,INZPGQ:CLADXUDO:HXZEVZAVQ..,UQZACQ....,INZPGQ:VLLQPTOJ:DCMEHQUK....,XTWJ........\r\n", "output": "13\r\n"}, {"input": "WCBHC:PDNTT:WCBHC:WCBHC:PDNTT:JOVEH:PDNTT:MPQPQVD:MPQPQVD:MSYRLMSCL:WCBHC:PHQUHCZ:PHQUHCZ:JOVEH:VWCWBJRF:WCBHC:VWCWBJRF:WCBHC:JOVEH:JOVEH....................\r\n", "output": "27\r\n"}, {"input": "VEEMU:VEEMU:GTE:GTE:KZZEDTUECE:VEEMU:GTE:CGOZKO:CGOZKO:GTE:ZZNAY.,GTE:MHMBC:GTE:VEEMU:CGOZKO:VEEMU:VEEMU.,CGOZKO:ZZNAY..................\r\n", "output": "36\r\n"}, {"input": "UWEJCOA:PPFWB:GKWVDKH:UWEJCOA..,QINJL.,ZVLULGYCBJ..,D:D..,EFEHJKNH:QINJL.,GKWVDKH..,NLBPAHEH.,PPFWB.,MWRKW.,UWEJCOA.,QINJL..\r\n", "output": "3\r\n"}, {"input": "HINLHUMDSC:HINLHUMDSC.,HINLHUMDSC:HINLHUMDSC..,HINLHUMDSC.,HINLHUMDSC.,HINLHUMDSC..\r\n", "output": "7\r\n"}, {"input": "FWYOOG:NJBFIOD:FWYOOG..,DH.,TSPKXXXE.,YMMMGNYBDC.,YMMMGNYBDC.,YMMMGNYBDC.,YMMMGNYBDC.,NJBFIOD..\r\n", "output": "1\r\n"}]
| false |
stdio
| null | true |
68/B
|
68
|
B
|
PyPy 3-64
|
TESTS
| 2 | 92 | 0 |
206981059
|
n,k=map(int,input().split())
arr=list(map(int,input().split()))
low=0
up=1000
ans=0
while low<=up:
mid=(low+up)/2
extra=0
needed=0
for i in range(n):
if arr[i]>=mid:
extra+=(arr[i]-mid)
else:
needed+=(mid-arr[i])
if abs((extra-((k*extra)/100))-needed)<0.0000001:
ans=max(ans,mid)
low=mid+0.000001
else:
if (extra-((k*extra)/100))-needed>0:
low=mid+0.0000001
else:
up=mid-0.0000001
print(ans)
| 42 | 124 | 4,915,200 |
132894228
|
(n, k), t = map(int, input().split()), sorted(map(int, input().split()))
i, d, s = 1, t[0], sum(t)
while i < n and (100 * s - k * (s - d)) > t[i] * (100 * n - k * (n - i)): d += t[i]; i += 1
print((100 * s - k * (s - d)) / (100 * n - k * (n - i)))
|
Codeforces Beta Round 62
|
CF
| 2,011 | 2 | 256 |
Energy exchange
|
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by $$x - \frac{xk}{100}$$ units.
Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
|
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.
Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
|
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.
The absolute or relative error in the answer should not exceed 10 - 6.
| null | null |
[{"input": "3 50\n4 2 1", "output": "2.000000000"}, {"input": "2 90\n1 11", "output": "1.909090909"}]
| 1,600 |
["binary search"]
| 42 |
[{"input": "3 50\r\n4 2 1\r\n", "output": "2.000000000\r\n"}, {"input": "2 90\r\n1 11\r\n", "output": "1.909090909\r\n"}, {"input": "5 26\r\n42 65 23 43 64\r\n", "output": "45.415178571\r\n"}, {"input": "5 45\r\n964 515 454 623 594\r\n", "output": "594.109756098\r\n"}, {"input": "1 20\r\n784\r\n", "output": "784.000000000\r\n"}, {"input": "10 20\r\n812 896 36 596 709 641 679 778 738 302\r\n", "output": "597.255813953\r\n"}, {"input": "10 83\r\n689 759 779 927 15 231 976 943 604 917\r\n", "output": "406.839285714\r\n"}, {"input": "11 1\r\n235 280 196 670 495 379 391 280 847 875 506\r\n", "output": "467.586301370\r\n"}, {"input": "12 71\r\n472 111 924 103 975 527 807 618 400 523 607 424\r\n", "output": "413.249554367\r\n"}, {"input": "13 89\r\n19 944 341 846 764 676 222 957 953 481 708 920 950\r\n", "output": "361.924390244\r\n"}, {"input": "14 6\r\n256 465 759 589 242 824 638 985 506 128 809 105 301 827\r\n", "output": "523.427098675\r\n"}]
| false |
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read reference output
with open(output_path, 'r') as f:
ref_line = f.readline().strip()
try:
E_ref = float(ref_line)
except:
print(0)
return
# Read submission output
with open(submission_path, 'r') as f:
sub_line = f.readline().strip()
try:
E_sub = float(sub_line)
except:
print(0)
return
abs_err = abs(E_sub - E_ref)
if abs_err <= 1e-6:
print(1)
return
if E_ref == 0:
print(0)
return
rel_err = abs_err / E_ref
if rel_err <= 1e-6:
print(1)
return
print(0)
if __name__ == "__main__":
main()
| true |
629/A
|
629
|
A
|
Python 3
|
TESTS
| 2 | 31 | 0 |
169620810
|
n=int(input())
lst=[]
total=0
for i in range(n):
lst.append(input().lower())
def combination(count):
if count>1:
num=1
for k in range(3,count+1):
num*=k
else:
num=0
return num
for i in lst:
count=0
for j in i:
if j=='c':
count+=1
total+=combination(count)
for i in range(n):
count=0
for j in range(len(lst)):
if lst[j][i]=='c':
count+=1
total+=combination(count)
print(total)
| 48 | 46 | 0 |
136336208
|
def gkd(x):
if x == 2:
return 1
else:
return x * (x - 1) / 2
n = int(input())
s = [input() for i in range(n)]
sum_C = 0
for i in range(n):
row_C = 0 # 行内C
rank_C = 0 # 列内C
for j in range(n):
if s[i][j] == 'C':
row_C += 1
if s[j][i] == 'C':
rank_C += 1
if row_C >= 2:
sum_C += gkd(row_C)
if rank_C >= 2:
sum_C += gkd(rank_C)
print(int(sum_C))
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 1 | 256 |
Far Relative’s Birthday Cake
|
Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!
The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?
Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.
|
In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake.
Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.
|
Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.
| null |
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:
1. (1, 2) and (1, 3)
2. (3, 1) and (3, 3)
1. (2, 1) and (3, 1)
2. (1, 3) and (3, 3)
|
[{"input": "3\n.CC\nC..\nC.C", "output": "4"}, {"input": "4\nCC..\nC..C\n.CC.\n.CC.", "output": "9"}]
| 800 |
["brute force", "combinatorics", "constructive algorithms", "implementation"]
| 48 |
[{"input": "3\r\n.CC\r\nC..\r\nC.C\r\n", "output": "4\r\n"}, {"input": "4\r\nCC..\r\nC..C\r\n.CC.\r\n.CC.\r\n", "output": "9\r\n"}, {"input": "5\r\n.CCCC\r\nCCCCC\r\n.CCC.\r\nCC...\r\n.CC.C\r\n", "output": "46\r\n"}, {"input": "6\r\nC.CC.C\r\n..C..C\r\nC..C.C\r\n.CCC.C\r\nCC....\r\nCC....\r\n", "output": "39\r\n"}, {"input": "7\r\n.CC..CC\r\nCC.C..C\r\nC.C..C.\r\nC...C.C\r\nCCC.CCC\r\n.CC...C\r\n.C.CCC.\r\n", "output": "84\r\n"}, {"input": "8\r\n..C....C\r\nC.CCC.CC\r\n.C..C.CC\r\nCC......\r\nC..C..CC\r\nC.C...C.\r\nC.C..C..\r\nC...C.C.\r\n", "output": "80\r\n"}, {"input": "9\r\n.C...CCCC\r\nC.CCCC...\r\n....C..CC\r\n.CC.CCC..\r\n.C.C..CC.\r\nC...C.CCC\r\nCCC.C...C\r\nCCCC....C\r\n..C..C..C\r\n", "output": "144\r\n"}, {"input": "10\r\n..C..C.C..\r\n..CC..C.CC\r\n.C.C...C.C\r\n..C.CC..CC\r\n....C..C.C\r\n...C..C..C\r\nCC.CC....C\r\n..CCCC.C.C\r\n..CC.CCC..\r\nCCCC..C.CC\r\n", "output": "190\r\n"}, {"input": "11\r\nC.CC...C.CC\r\nCC.C....C.C\r\n.....C..CCC\r\n....C.CC.CC\r\nC..C..CC...\r\nC...C...C..\r\nCC..CCC.C.C\r\n..C.CC.C..C\r\nC...C.C..CC\r\n.C.C..CC..C\r\n.C.C.CC.C..\r\n", "output": "228\r\n"}, {"input": "21\r\n...CCC.....CC..C..C.C\r\n..CCC...CC...CC.CCC.C\r\n....C.C.C..CCC..C.C.C\r\n....CCC..C..C.CC.CCC.\r\n...CCC.C..C.C.....CCC\r\n.CCC.....CCC..C...C.C\r\nCCCC.C...CCC.C...C.CC\r\nC..C...C.CCC..CC..C..\r\nC...CC..C.C.CC..C.CC.\r\nCC..CCCCCCCCC..C....C\r\n.C..CCCC.CCCC.CCC...C\r\nCCC...CCC...CCC.C..C.\r\n.CCCCCCCC.CCCC.CC.C..\r\n.C.C..C....C.CCCCCC.C\r\n...C...C.CCC.C.CC..C.\r\nCCC...CC..CC...C..C.C\r\n.CCCCC...C.C..C.CC.C.\r\n..CCC.C.C..CCC.CCC...\r\n..C..C.C.C.....CC.C..\r\n.CC.C...C.CCC.C....CC\r\n...C..CCCC.CCC....C..\r\n", "output": "2103\r\n"}, {"input": "20\r\nC.C.CCC.C....C.CCCCC\r\nC.CC.C..CCC....CCCC.\r\n.CCC.CC...CC.CCCCCC.\r\n.C...CCCC..C....CCC.\r\n.C..CCCCCCC.C.C.....\r\nC....C.C..CCC.C..CCC\r\n...C.C.CC..CC..CC...\r\nC...CC.C.CCCCC....CC\r\n.CC.C.CCC....C.CCC.C\r\nCC...CC...CC..CC...C\r\nC.C..CC.C.CCCC.C.CC.\r\n..CCCCC.C.CCC..CCCC.\r\n....C..C..C.CC...C.C\r\nC..CCC..CC..C.CC..CC\r\n...CC......C.C..C.C.\r\nCC.CCCCC.CC.CC...C.C\r\n.C.CC..CC..CCC.C.CCC\r\nC..C.CC....C....C...\r\n..CCC..CCC...CC..C.C\r\n.C.CCC.CCCCCCCCC..CC\r\n", "output": "2071\r\n"}, {"input": "17\r\nCCC..C.C....C.C.C\r\n.C.CC.CC...CC..C.\r\n.CCCC.CC.C..CCC.C\r\n...CCC.CC.CCC.C.C\r\nCCCCCCCC..C.CC.CC\r\n...C..C....C.CC.C\r\nCC....CCC...C.CC.\r\n.CC.C.CC..C......\r\n.CCCCC.C.CC.CCCCC\r\n..CCCC...C..CC..C\r\nC.CC.C.CC..C.C.C.\r\nC..C..C..CCC.C...\r\n.C..CCCC..C......\r\n.CC.C...C..CC.CC.\r\nC..C....CC...CC..\r\nC.CC.CC..C.C..C..\r\nCCCC...C.C..CCCC.\r\n", "output": "1160\r\n"}, {"input": "15\r\nCCCC.C..CCC....\r\nCCCCCC.CC.....C\r\n...C.CC.C.C.CC.\r\nCCCCCCC..C..C..\r\nC..CCC..C.CCCC.\r\n.CC..C.C.C.CC.C\r\n.C.C..C..C.C..C\r\n...C...C..CCCC.\r\n.....C.C..CC...\r\nCC.C.C..CC.C..C\r\n..CCCCC..CCC...\r\nCC.CC.C..CC.CCC\r\n..CCC...CC.C..C\r\nCC..C.C..CCC..C\r\n.C.C....CCC...C\r\n", "output": "789\r\n"}, {"input": "1\r\n.\r\n", "output": "0\r\n"}, {"input": "3\r\n.CC\r\nC..\r\nC.C\r\n", "output": "4\r\n"}, {"input": "13\r\nC.C...C.C.C..\r\nCC.CCCC.CC..C\r\n.C.CCCCC.CC..\r\nCCCC..C...C..\r\n...CC.C.C...C\r\n.CC.CCC...CC.\r\nCC.CCCCCC....\r\n.C...C..CC..C\r\nCCCC.CC...C..\r\n.C.CCC..C.CC.\r\n..C...CC..C.C\r\n..C.CCC..CC.C\r\n.C...CCC.CC.C\r\n", "output": "529\r\n"}, {"input": "19\r\nCC.C..CC...CCC.C...\r\n....C...C..C.C..C.C\r\nCC.CC.CCCC..C.CC..C\r\n........CC...CC..C.\r\nCCCCC.C...C..C..CC.\r\n...CC..C...C.C.CC..\r\nCC....C.CC.C..CC.CC\r\n.C.C.CC..CCC...CCCC\r\n.....C..CC..C..C.C.\r\nC.CCC.CCC.C..C.C...\r\nCCCC...CC.......CCC\r\nC.C....CC.CC....CC.\r\nC..CC...CCCC..C.CCC\r\nCCC..C..CC.C.C.CC..\r\nCCCCC.CCCC.CCCCCCC.\r\n.C..C.CCC..C..CCCCC\r\n.CCC.C.CC.CCCC..CC.\r\n..CCCC...C.C.CCCCCC\r\nCCCCCCCC..CC.CCC...\r\n", "output": "1787\r\n"}, {"input": "16\r\n.C.C.C.C.C...C.C\r\n..C..C.CCCCCC...\r\n..C.C.C.C..C..C.\r\n.CC....C.CCC..C.\r\n.C.CCC..C....CCC\r\nCC..C.CC..C.C.CC\r\n...C..C..CC..CC.\r\n.CCC..C.CC.C.C..\r\n.CC.C..........C\r\nC...C....CC..C..\r\nC.CCC.C..C..C...\r\n.CCCCCCCCCCCC..C\r\n..C.C.CC.CC.CCC.\r\nCC..C.C....C..CC\r\nC.CCC..C..C.C.CC\r\n.C.CCC.CC..CCC.C\r\n", "output": "874\r\n"}, {"input": "2\r\nCC\r\nCC\r\n", "output": "4\r\n"}, {"input": "3\r\nC..\r\nC..\r\nC..\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
629/A
|
629
|
A
|
Python 3
|
TESTS
| 2 | 31 | 0 |
171530909
|
def fact(n):
r = 1
for i in range(n):
r *= (i + 1)
return r
n = int(input().strip())
c = [0] * n
l = [0] * n
for i in range(n):
a = list(input().strip())
for j in range(n):
if a[j] == 'C':
l[i] += 1
c[j] += 1
r = 0
for i in range(n):
r += fact(c[i]) // 2 + fact(l[i]) // 2
print(r)
| 48 | 46 | 0 |
136336782
|
a=int(input())
s=[]
n=0
sum=0
for i in range(a):
m=input()
s.append(m)
for i in range(a):
b=0
p=0
for j in range(a):
if s[i][j]=='C':
b=b+1
if s[j][i]=='C':
p=p+1
sum=sum+((b-1)*b)/2
sum=sum+((p-1)*p)/2
print(int(sum))
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 1 | 256 |
Far Relative’s Birthday Cake
|
Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!
The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?
Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.
|
In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake.
Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.
|
Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.
| null |
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:
1. (1, 2) and (1, 3)
2. (3, 1) and (3, 3)
1. (2, 1) and (3, 1)
2. (1, 3) and (3, 3)
|
[{"input": "3\n.CC\nC..\nC.C", "output": "4"}, {"input": "4\nCC..\nC..C\n.CC.\n.CC.", "output": "9"}]
| 800 |
["brute force", "combinatorics", "constructive algorithms", "implementation"]
| 48 |
[{"input": "3\r\n.CC\r\nC..\r\nC.C\r\n", "output": "4\r\n"}, {"input": "4\r\nCC..\r\nC..C\r\n.CC.\r\n.CC.\r\n", "output": "9\r\n"}, {"input": "5\r\n.CCCC\r\nCCCCC\r\n.CCC.\r\nCC...\r\n.CC.C\r\n", "output": "46\r\n"}, {"input": "6\r\nC.CC.C\r\n..C..C\r\nC..C.C\r\n.CCC.C\r\nCC....\r\nCC....\r\n", "output": "39\r\n"}, {"input": "7\r\n.CC..CC\r\nCC.C..C\r\nC.C..C.\r\nC...C.C\r\nCCC.CCC\r\n.CC...C\r\n.C.CCC.\r\n", "output": "84\r\n"}, {"input": "8\r\n..C....C\r\nC.CCC.CC\r\n.C..C.CC\r\nCC......\r\nC..C..CC\r\nC.C...C.\r\nC.C..C..\r\nC...C.C.\r\n", "output": "80\r\n"}, {"input": "9\r\n.C...CCCC\r\nC.CCCC...\r\n....C..CC\r\n.CC.CCC..\r\n.C.C..CC.\r\nC...C.CCC\r\nCCC.C...C\r\nCCCC....C\r\n..C..C..C\r\n", "output": "144\r\n"}, {"input": "10\r\n..C..C.C..\r\n..CC..C.CC\r\n.C.C...C.C\r\n..C.CC..CC\r\n....C..C.C\r\n...C..C..C\r\nCC.CC....C\r\n..CCCC.C.C\r\n..CC.CCC..\r\nCCCC..C.CC\r\n", "output": "190\r\n"}, {"input": "11\r\nC.CC...C.CC\r\nCC.C....C.C\r\n.....C..CCC\r\n....C.CC.CC\r\nC..C..CC...\r\nC...C...C..\r\nCC..CCC.C.C\r\n..C.CC.C..C\r\nC...C.C..CC\r\n.C.C..CC..C\r\n.C.C.CC.C..\r\n", "output": "228\r\n"}, {"input": "21\r\n...CCC.....CC..C..C.C\r\n..CCC...CC...CC.CCC.C\r\n....C.C.C..CCC..C.C.C\r\n....CCC..C..C.CC.CCC.\r\n...CCC.C..C.C.....CCC\r\n.CCC.....CCC..C...C.C\r\nCCCC.C...CCC.C...C.CC\r\nC..C...C.CCC..CC..C..\r\nC...CC..C.C.CC..C.CC.\r\nCC..CCCCCCCCC..C....C\r\n.C..CCCC.CCCC.CCC...C\r\nCCC...CCC...CCC.C..C.\r\n.CCCCCCCC.CCCC.CC.C..\r\n.C.C..C....C.CCCCCC.C\r\n...C...C.CCC.C.CC..C.\r\nCCC...CC..CC...C..C.C\r\n.CCCCC...C.C..C.CC.C.\r\n..CCC.C.C..CCC.CCC...\r\n..C..C.C.C.....CC.C..\r\n.CC.C...C.CCC.C....CC\r\n...C..CCCC.CCC....C..\r\n", "output": "2103\r\n"}, {"input": "20\r\nC.C.CCC.C....C.CCCCC\r\nC.CC.C..CCC....CCCC.\r\n.CCC.CC...CC.CCCCCC.\r\n.C...CCCC..C....CCC.\r\n.C..CCCCCCC.C.C.....\r\nC....C.C..CCC.C..CCC\r\n...C.C.CC..CC..CC...\r\nC...CC.C.CCCCC....CC\r\n.CC.C.CCC....C.CCC.C\r\nCC...CC...CC..CC...C\r\nC.C..CC.C.CCCC.C.CC.\r\n..CCCCC.C.CCC..CCCC.\r\n....C..C..C.CC...C.C\r\nC..CCC..CC..C.CC..CC\r\n...CC......C.C..C.C.\r\nCC.CCCCC.CC.CC...C.C\r\n.C.CC..CC..CCC.C.CCC\r\nC..C.CC....C....C...\r\n..CCC..CCC...CC..C.C\r\n.C.CCC.CCCCCCCCC..CC\r\n", "output": "2071\r\n"}, {"input": "17\r\nCCC..C.C....C.C.C\r\n.C.CC.CC...CC..C.\r\n.CCCC.CC.C..CCC.C\r\n...CCC.CC.CCC.C.C\r\nCCCCCCCC..C.CC.CC\r\n...C..C....C.CC.C\r\nCC....CCC...C.CC.\r\n.CC.C.CC..C......\r\n.CCCCC.C.CC.CCCCC\r\n..CCCC...C..CC..C\r\nC.CC.C.CC..C.C.C.\r\nC..C..C..CCC.C...\r\n.C..CCCC..C......\r\n.CC.C...C..CC.CC.\r\nC..C....CC...CC..\r\nC.CC.CC..C.C..C..\r\nCCCC...C.C..CCCC.\r\n", "output": "1160\r\n"}, {"input": "15\r\nCCCC.C..CCC....\r\nCCCCCC.CC.....C\r\n...C.CC.C.C.CC.\r\nCCCCCCC..C..C..\r\nC..CCC..C.CCCC.\r\n.CC..C.C.C.CC.C\r\n.C.C..C..C.C..C\r\n...C...C..CCCC.\r\n.....C.C..CC...\r\nCC.C.C..CC.C..C\r\n..CCCCC..CCC...\r\nCC.CC.C..CC.CCC\r\n..CCC...CC.C..C\r\nCC..C.C..CCC..C\r\n.C.C....CCC...C\r\n", "output": "789\r\n"}, {"input": "1\r\n.\r\n", "output": "0\r\n"}, {"input": "3\r\n.CC\r\nC..\r\nC.C\r\n", "output": "4\r\n"}, {"input": "13\r\nC.C...C.C.C..\r\nCC.CCCC.CC..C\r\n.C.CCCCC.CC..\r\nCCCC..C...C..\r\n...CC.C.C...C\r\n.CC.CCC...CC.\r\nCC.CCCCCC....\r\n.C...C..CC..C\r\nCCCC.CC...C..\r\n.C.CCC..C.CC.\r\n..C...CC..C.C\r\n..C.CCC..CC.C\r\n.C...CCC.CC.C\r\n", "output": "529\r\n"}, {"input": "19\r\nCC.C..CC...CCC.C...\r\n....C...C..C.C..C.C\r\nCC.CC.CCCC..C.CC..C\r\n........CC...CC..C.\r\nCCCCC.C...C..C..CC.\r\n...CC..C...C.C.CC..\r\nCC....C.CC.C..CC.CC\r\n.C.C.CC..CCC...CCCC\r\n.....C..CC..C..C.C.\r\nC.CCC.CCC.C..C.C...\r\nCCCC...CC.......CCC\r\nC.C....CC.CC....CC.\r\nC..CC...CCCC..C.CCC\r\nCCC..C..CC.C.C.CC..\r\nCCCCC.CCCC.CCCCCCC.\r\n.C..C.CCC..C..CCCCC\r\n.CCC.C.CC.CCCC..CC.\r\n..CCCC...C.C.CCCCCC\r\nCCCCCCCC..CC.CCC...\r\n", "output": "1787\r\n"}, {"input": "16\r\n.C.C.C.C.C...C.C\r\n..C..C.CCCCCC...\r\n..C.C.C.C..C..C.\r\n.CC....C.CCC..C.\r\n.C.CCC..C....CCC\r\nCC..C.CC..C.C.CC\r\n...C..C..CC..CC.\r\n.CCC..C.CC.C.C..\r\n.CC.C..........C\r\nC...C....CC..C..\r\nC.CCC.C..C..C...\r\n.CCCCCCCCCCCC..C\r\n..C.C.CC.CC.CCC.\r\nCC..C.C....C..CC\r\nC.CCC..C..C.C.CC\r\n.C.CCC.CC..CCC.C\r\n", "output": "874\r\n"}, {"input": "2\r\nCC\r\nCC\r\n", "output": "4\r\n"}, {"input": "3\r\nC..\r\nC..\r\nC..\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
629/A
|
629
|
A
|
Python 3
|
TESTS
| 2 | 31 | 0 |
171537347
|
def bolo(matriz, n):
res = 0
for i in range(n):
count = 0
for j in range(n):
if matriz[i][j] == 'C':
count += 1
if count >= 2:
if count % 2 == 0:
res += count // 2
else:
res += count + 2
count = 0
for j in range(n):
if matriz[j][i] == 'C':
count += 1
if count >= 2:
if count % 2 == 0:
res += count // 2
else:
res += count
return res
n = int(input())
matriz = []
for i in range(n):
matriz.append(input())
print(bolo(matriz, n))
| 48 | 46 | 0 |
136732796
|
n=int(input())
lst=[]
m=0
s=0
for i in range(n):
lst.append(list(input()))
for i in range(n):
for j in range(n):
if lst[i][j]=='C':
m+=1
s+=int(m*(m-1)/2)
m=0
continue
for i in range(n):
for j in range(n):
if lst[j][i]=='C':
m+=1
s+=int(m*(m-1)/2)
m=0
continue
print(s)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 1 | 256 |
Far Relative’s Birthday Cake
|
Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!
The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?
Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.
|
In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake.
Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.
|
Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.
| null |
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:
1. (1, 2) and (1, 3)
2. (3, 1) and (3, 3)
1. (2, 1) and (3, 1)
2. (1, 3) and (3, 3)
|
[{"input": "3\n.CC\nC..\nC.C", "output": "4"}, {"input": "4\nCC..\nC..C\n.CC.\n.CC.", "output": "9"}]
| 800 |
["brute force", "combinatorics", "constructive algorithms", "implementation"]
| 48 |
[{"input": "3\r\n.CC\r\nC..\r\nC.C\r\n", "output": "4\r\n"}, {"input": "4\r\nCC..\r\nC..C\r\n.CC.\r\n.CC.\r\n", "output": "9\r\n"}, {"input": "5\r\n.CCCC\r\nCCCCC\r\n.CCC.\r\nCC...\r\n.CC.C\r\n", "output": "46\r\n"}, {"input": "6\r\nC.CC.C\r\n..C..C\r\nC..C.C\r\n.CCC.C\r\nCC....\r\nCC....\r\n", "output": "39\r\n"}, {"input": "7\r\n.CC..CC\r\nCC.C..C\r\nC.C..C.\r\nC...C.C\r\nCCC.CCC\r\n.CC...C\r\n.C.CCC.\r\n", "output": "84\r\n"}, {"input": "8\r\n..C....C\r\nC.CCC.CC\r\n.C..C.CC\r\nCC......\r\nC..C..CC\r\nC.C...C.\r\nC.C..C..\r\nC...C.C.\r\n", "output": "80\r\n"}, {"input": "9\r\n.C...CCCC\r\nC.CCCC...\r\n....C..CC\r\n.CC.CCC..\r\n.C.C..CC.\r\nC...C.CCC\r\nCCC.C...C\r\nCCCC....C\r\n..C..C..C\r\n", "output": "144\r\n"}, {"input": "10\r\n..C..C.C..\r\n..CC..C.CC\r\n.C.C...C.C\r\n..C.CC..CC\r\n....C..C.C\r\n...C..C..C\r\nCC.CC....C\r\n..CCCC.C.C\r\n..CC.CCC..\r\nCCCC..C.CC\r\n", "output": "190\r\n"}, {"input": "11\r\nC.CC...C.CC\r\nCC.C....C.C\r\n.....C..CCC\r\n....C.CC.CC\r\nC..C..CC...\r\nC...C...C..\r\nCC..CCC.C.C\r\n..C.CC.C..C\r\nC...C.C..CC\r\n.C.C..CC..C\r\n.C.C.CC.C..\r\n", "output": "228\r\n"}, {"input": "21\r\n...CCC.....CC..C..C.C\r\n..CCC...CC...CC.CCC.C\r\n....C.C.C..CCC..C.C.C\r\n....CCC..C..C.CC.CCC.\r\n...CCC.C..C.C.....CCC\r\n.CCC.....CCC..C...C.C\r\nCCCC.C...CCC.C...C.CC\r\nC..C...C.CCC..CC..C..\r\nC...CC..C.C.CC..C.CC.\r\nCC..CCCCCCCCC..C....C\r\n.C..CCCC.CCCC.CCC...C\r\nCCC...CCC...CCC.C..C.\r\n.CCCCCCCC.CCCC.CC.C..\r\n.C.C..C....C.CCCCCC.C\r\n...C...C.CCC.C.CC..C.\r\nCCC...CC..CC...C..C.C\r\n.CCCCC...C.C..C.CC.C.\r\n..CCC.C.C..CCC.CCC...\r\n..C..C.C.C.....CC.C..\r\n.CC.C...C.CCC.C....CC\r\n...C..CCCC.CCC....C..\r\n", "output": "2103\r\n"}, {"input": "20\r\nC.C.CCC.C....C.CCCCC\r\nC.CC.C..CCC....CCCC.\r\n.CCC.CC...CC.CCCCCC.\r\n.C...CCCC..C....CCC.\r\n.C..CCCCCCC.C.C.....\r\nC....C.C..CCC.C..CCC\r\n...C.C.CC..CC..CC...\r\nC...CC.C.CCCCC....CC\r\n.CC.C.CCC....C.CCC.C\r\nCC...CC...CC..CC...C\r\nC.C..CC.C.CCCC.C.CC.\r\n..CCCCC.C.CCC..CCCC.\r\n....C..C..C.CC...C.C\r\nC..CCC..CC..C.CC..CC\r\n...CC......C.C..C.C.\r\nCC.CCCCC.CC.CC...C.C\r\n.C.CC..CC..CCC.C.CCC\r\nC..C.CC....C....C...\r\n..CCC..CCC...CC..C.C\r\n.C.CCC.CCCCCCCCC..CC\r\n", "output": "2071\r\n"}, {"input": "17\r\nCCC..C.C....C.C.C\r\n.C.CC.CC...CC..C.\r\n.CCCC.CC.C..CCC.C\r\n...CCC.CC.CCC.C.C\r\nCCCCCCCC..C.CC.CC\r\n...C..C....C.CC.C\r\nCC....CCC...C.CC.\r\n.CC.C.CC..C......\r\n.CCCCC.C.CC.CCCCC\r\n..CCCC...C..CC..C\r\nC.CC.C.CC..C.C.C.\r\nC..C..C..CCC.C...\r\n.C..CCCC..C......\r\n.CC.C...C..CC.CC.\r\nC..C....CC...CC..\r\nC.CC.CC..C.C..C..\r\nCCCC...C.C..CCCC.\r\n", "output": "1160\r\n"}, {"input": "15\r\nCCCC.C..CCC....\r\nCCCCCC.CC.....C\r\n...C.CC.C.C.CC.\r\nCCCCCCC..C..C..\r\nC..CCC..C.CCCC.\r\n.CC..C.C.C.CC.C\r\n.C.C..C..C.C..C\r\n...C...C..CCCC.\r\n.....C.C..CC...\r\nCC.C.C..CC.C..C\r\n..CCCCC..CCC...\r\nCC.CC.C..CC.CCC\r\n..CCC...CC.C..C\r\nCC..C.C..CCC..C\r\n.C.C....CCC...C\r\n", "output": "789\r\n"}, {"input": "1\r\n.\r\n", "output": "0\r\n"}, {"input": "3\r\n.CC\r\nC..\r\nC.C\r\n", "output": "4\r\n"}, {"input": "13\r\nC.C...C.C.C..\r\nCC.CCCC.CC..C\r\n.C.CCCCC.CC..\r\nCCCC..C...C..\r\n...CC.C.C...C\r\n.CC.CCC...CC.\r\nCC.CCCCCC....\r\n.C...C..CC..C\r\nCCCC.CC...C..\r\n.C.CCC..C.CC.\r\n..C...CC..C.C\r\n..C.CCC..CC.C\r\n.C...CCC.CC.C\r\n", "output": "529\r\n"}, {"input": "19\r\nCC.C..CC...CCC.C...\r\n....C...C..C.C..C.C\r\nCC.CC.CCCC..C.CC..C\r\n........CC...CC..C.\r\nCCCCC.C...C..C..CC.\r\n...CC..C...C.C.CC..\r\nCC....C.CC.C..CC.CC\r\n.C.C.CC..CCC...CCCC\r\n.....C..CC..C..C.C.\r\nC.CCC.CCC.C..C.C...\r\nCCCC...CC.......CCC\r\nC.C....CC.CC....CC.\r\nC..CC...CCCC..C.CCC\r\nCCC..C..CC.C.C.CC..\r\nCCCCC.CCCC.CCCCCCC.\r\n.C..C.CCC..C..CCCCC\r\n.CCC.C.CC.CCCC..CC.\r\n..CCCC...C.C.CCCCCC\r\nCCCCCCCC..CC.CCC...\r\n", "output": "1787\r\n"}, {"input": "16\r\n.C.C.C.C.C...C.C\r\n..C..C.CCCCCC...\r\n..C.C.C.C..C..C.\r\n.CC....C.CCC..C.\r\n.C.CCC..C....CCC\r\nCC..C.CC..C.C.CC\r\n...C..C..CC..CC.\r\n.CCC..C.CC.C.C..\r\n.CC.C..........C\r\nC...C....CC..C..\r\nC.CCC.C..C..C...\r\n.CCCCCCCCCCCC..C\r\n..C.C.CC.CC.CCC.\r\nCC..C.C....C..CC\r\nC.CCC..C..C.C.CC\r\n.C.CCC.CC..CCC.C\r\n", "output": "874\r\n"}, {"input": "2\r\nCC\r\nCC\r\n", "output": "4\r\n"}, {"input": "3\r\nC..\r\nC..\r\nC..\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
558/D
|
558
|
D
|
Python 3
|
TESTS
| 4 | 233 | 2,150,400 |
31050327
|
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
"""
created by shhuan at 2017/10/6 09:03
"""
H, Q = map(int, input().split())
M1 = []
M2 = []
for i in range(Q):
i, l, r, a = map(int, input().split())
if a:
M1.append((i, l, r))
else:
M2.append((i, l, r))
M1.sort(reverse=True)
M2.sort(reverse=True)
L, R = 2**(H-1), 2**H-1
for i, l, r in M1:
left = l*2**(H-i)
right = r
for h in range(i, H):
right = 2*right+1
if L > right or R < left:
print("Game cheated!")
exit(0)
L = max(L, left)
R = min(R, right)
LR = [(L,R)]
for i, l, r in M2:
if not LR:
print("Game cheated!")
exit(0)
left = l * 2 ** (H - i)
right = r
for h in range(i, H):
right = 2 * right + 1
# merge [L, R] and [1, left-1]
# merge [L, R] and [right+1, 2**H-1]
nextlr = []
for L,R in LR:
if 1 <= R < left:
nextlr.append((1, R))
elif L <= left-1 <= R:
nextlr.append((L, left-1))
if L <= right+1 <= R:
nextlr.append((right+1, R))
elif L >= right+1:
nextlr.append((L, R))
LR = nextlr
# print(LR)
if len(LR) == 0:
print("Game cheated!")
elif len(LR) == 1 and LR[0][0] == LR[0][1]:
print(LR[0][0])
else:
print("Data not sufficient!")
| 90 | 779 | 50,688,000 |
155784759
|
import sys
input = sys.stdin.buffer.readline
def process(h, Q):
L1, R1 = 2**(h-1), 2**h-1
Q1 = []
n = 0
for i, L, R, ans in Q:
L2, R2 = 2**(h-i)*L, 2**(h-i)*R+2**(h-i)-1
if ans==0:
Q1.append([L2, 0, n])
Q1.append([R2, 1, n])
n+=1
else:
L1 = max(L1, L2)
R1 = min(R1, R2)
if L1 > R1:
sys.stdout.write('Game cheated!\n')
return
good = []
seen = 0
Q1.sort()
curr = 2**(h-1)
for x, t, i in Q1:
if t==0:
if seen==0 and x-1 >= curr:
l2 = max(curr, L1)
r2 = min(x-1, R1)
if l2 <= r2:
good.append([l2, r2])
seen+=1
curr = None
else:
seen-=1
if seen==0:
curr = x+1
if curr <= 2**h-1:
l2 = max(curr, L1)
r2= min(2**h-1, R1)
if l2 <= r2:
good.append([l2, r2])
if len(good)==0:
sys.stdout.write('Game cheated!\n')
return
if len(good) > 1:
sys.stdout.write('Data not sufficient!\n')
return
l, r = good[0]
if l != r:
sys.stdout.write('Data not sufficient!\n')
return
sys.stdout.write(f'{l}\n')
return
h, q = [int(x) for x in input().split()]
Q = []
for I in range(q):
i, L, R, ans = [int(x) for x in input().split()]
Q.append([i, L, R, ans])
process(h, Q)
|
Codeforces Round 312 (Div. 2)
|
CF
| 2,015 | 2 | 256 |
Guess Your Way Out! II
|
Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the nodes of the tree such that
- The root is number 1
- Each internal node i (i ≤ 2h - 1 - 1) will have a left child with index = 2i and a right child with index = 2i + 1
The level of a node is defined as 1 for a root, or 1 + level of parent of the node otherwise. The vertices of the level h are called leaves. The exit to the maze is located at some leaf node n, the player doesn't know where the exit is so he has to guess his way out!
In the new version of the game the player is allowed to ask questions on the format "Does the ancestor(exit, i) node number belong to the range [L, R]?". Here ancestor(v, i) is the ancestor of a node v that located in the level i. The game will answer with "Yes" or "No" only. The game is designed such that it doesn't always answer correctly, and sometimes it cheats to confuse the player!.
Amr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn't defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory.
|
The first line contains two integers h, q (1 ≤ h ≤ 50, 0 ≤ q ≤ 105), the height of the tree and the number of questions respectively.
The next q lines will contain four integers each i, L, R, ans (1 ≤ i ≤ h, 2i - 1 ≤ L ≤ R ≤ 2i - 1, $$ans \in \{0, 1\}$$), representing a question as described in the statement with its answer (ans = 1 if the answer is "Yes" and ans = 0 if the answer is "No").
|
If the information provided by the game is contradictory output "Game cheated!" without the quotes.
Else if you can uniquely identify the exit to the maze output its index.
Otherwise output "Data not sufficient!" without the quotes.
| null |
Node u is an ancestor of node v if and only if
- u is the same node as v,
- u is the parent of node v,
- or u is an ancestor of the parent of node v.
In the first sample test there are 4 leaf nodes 4, 5, 6, 7. The first question says that the node isn't in the range [4, 6] so the exit is node number 7.
In the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10, 14]. After the second and the third questions only node number 14 is correct. Check the picture below to fully understand.
|
[{"input": "3 1\n3 4 6 0", "output": "7"}, {"input": "4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1", "output": "14"}, {"input": "4 2\n3 4 6 1\n4 12 15 1", "output": "Data not sufficient!"}, {"input": "4 2\n3 4 5 1\n2 3 3 1", "output": "Game cheated!"}]
| 2,300 |
["data structures", "implementation", "sortings"]
| 90 |
[{"input": "3 1\r\n3 4 6 0\r\n", "output": "7"}, {"input": "4 3\r\n4 10 14 1\r\n3 6 6 0\r\n2 3 3 1\r\n", "output": "14"}, {"input": "4 2\r\n3 4 6 1\r\n4 12 15 1\r\n", "output": "Data not sufficient!"}, {"input": "4 2\r\n3 4 5 1\r\n2 3 3 1\r\n", "output": "Game cheated!"}, {"input": "1 0\r\n", "output": "1"}, {"input": "1 1\r\n1 1 1 0\r\n", "output": "Game cheated!"}]
| false |
stdio
| null | true |
558/D
|
558
|
D
|
Python 3
|
TESTS
| 9 | 654 | 10,342,400 |
31102809
|
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
"""
created by shhuan at 2017/10/6 09:03
"""
H, Q = map(int, input().split())
LL = [2**(i-1) for i in range(H+1)]
LR = [2**i-1 for i in range(H+1)]
M1 = []
M2 = []
for i in range(Q):
i, l, r, a = map(int, input().split())
ll = l * 2 ** (H - i)
rl = (r + 1) * 2 ** (H - i) - 1
if a:
if ll > rl:
print("Game cheated!")
exit(0)
M1.append((ll, rl))
else:
if 2**(H-1) <= ll <= rl <= 2**H-1:
M2.append((ll, rl))
# M1.sort()
# M2.sort(reverse=True)
L, R = 2**(H-1), 2**H-1
for ll, lr in M1:
L = max(L, ll)
R = min(R, lr)
if L > R:
print("Game cheated!")
exit(0)
LR = {(L, R)}
for ll, lr in M2:
# merge [L, R] and [1, ll-1]
# merge [L, R] and [lr+1, 2**H-1]
nextlr = set()
for L, R in LR:
if R < ll or L > lr:
nextlr.add((L, R))
elif L <= ll-1 <= R:
nextlr.add((L, ll-1))
elif L <= lr+1 <= R:
nextlr.add((lr+1, R))
LR = nextlr
LR = list(LR)
if len(LR) == 0:
print("Game cheated!")
elif len(LR) == 1 and LR[0][0] == LR[0][1]:
print(LR[0][0])
else:
print("Data not sufficient!")
| 90 | 1,076 | 16,998,400 |
12062622
|
h,q=map(int,input().split())
d=[(2**h,0),(2**(h-1),0)]
for _ in range(q):
i,l,r,a=map(int,input().split())
l,r=l*2**(h-i),(r+1)*2**(h-i)
d.extend([[(l,1),(r,-1)],[(0,1),(l,-1),(r,1)]][a])
s=0
l=0
d=sorted(d)
for (a,x),(b,_) in zip(d,d[1:]):
s+=x
if a!=b and s==0:q=a;l+=b-a
print(("Game cheated!",q,"Data not sufficient!")[min(l,2)])
|
Codeforces Round 312 (Div. 2)
|
CF
| 2,015 | 2 | 256 |
Guess Your Way Out! II
|
Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the nodes of the tree such that
- The root is number 1
- Each internal node i (i ≤ 2h - 1 - 1) will have a left child with index = 2i and a right child with index = 2i + 1
The level of a node is defined as 1 for a root, or 1 + level of parent of the node otherwise. The vertices of the level h are called leaves. The exit to the maze is located at some leaf node n, the player doesn't know where the exit is so he has to guess his way out!
In the new version of the game the player is allowed to ask questions on the format "Does the ancestor(exit, i) node number belong to the range [L, R]?". Here ancestor(v, i) is the ancestor of a node v that located in the level i. The game will answer with "Yes" or "No" only. The game is designed such that it doesn't always answer correctly, and sometimes it cheats to confuse the player!.
Amr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn't defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory.
|
The first line contains two integers h, q (1 ≤ h ≤ 50, 0 ≤ q ≤ 105), the height of the tree and the number of questions respectively.
The next q lines will contain four integers each i, L, R, ans (1 ≤ i ≤ h, 2i - 1 ≤ L ≤ R ≤ 2i - 1, $$ans \in \{0, 1\}$$), representing a question as described in the statement with its answer (ans = 1 if the answer is "Yes" and ans = 0 if the answer is "No").
|
If the information provided by the game is contradictory output "Game cheated!" without the quotes.
Else if you can uniquely identify the exit to the maze output its index.
Otherwise output "Data not sufficient!" without the quotes.
| null |
Node u is an ancestor of node v if and only if
- u is the same node as v,
- u is the parent of node v,
- or u is an ancestor of the parent of node v.
In the first sample test there are 4 leaf nodes 4, 5, 6, 7. The first question says that the node isn't in the range [4, 6] so the exit is node number 7.
In the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10, 14]. After the second and the third questions only node number 14 is correct. Check the picture below to fully understand.
|
[{"input": "3 1\n3 4 6 0", "output": "7"}, {"input": "4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1", "output": "14"}, {"input": "4 2\n3 4 6 1\n4 12 15 1", "output": "Data not sufficient!"}, {"input": "4 2\n3 4 5 1\n2 3 3 1", "output": "Game cheated!"}]
| 2,300 |
["data structures", "implementation", "sortings"]
| 90 |
[{"input": "3 1\r\n3 4 6 0\r\n", "output": "7"}, {"input": "4 3\r\n4 10 14 1\r\n3 6 6 0\r\n2 3 3 1\r\n", "output": "14"}, {"input": "4 2\r\n3 4 6 1\r\n4 12 15 1\r\n", "output": "Data not sufficient!"}, {"input": "4 2\r\n3 4 5 1\r\n2 3 3 1\r\n", "output": "Game cheated!"}, {"input": "1 0\r\n", "output": "1"}, {"input": "1 1\r\n1 1 1 0\r\n", "output": "Game cheated!"}]
| false |
stdio
| null | true |
557/D
|
557
|
D
|
Python 3
|
PRETESTS
| 2 | 62 | 204,800 |
11865621
|
__author__ = 'Andrey'
import sys
sys.setrecursionlimit(100500)
n, m = map(int, input().split())
g = dict()
for i in range(1, n + 1):
g[i] = set()
for i in range(m):
a, b = map(int, input().split())
g[a].add(b)
g[b].add(a)
bad_graph = False
color = [0] * (n + 1)
def dfs(u):
global bad_graph
for v in g[u]:
if color[v] == 0:
color[v] = 3 - color[u]
dfs(v)
elif color[u] == color[v]:
bad_graph = True
for u in range(1, n + 1):
if color[u] == 0:
color[u] = 1
dfs(u)
if bad_graph:
print(0, 1)
else:
nothing = 0
ans = -1
total = 0
set_1 = set()
set_2 = set()
for i in range(1, n + 1):
c = 0
c_1 = 0
if color[i] == 1:
for u in g[i]:
c += 1
if u in set_1:
c_1 += 1
set_1.add(u)
if color[i] == 2:
for u in g[i]:
c += 1
if u in set_2:
c_1 += 1
set_2.add(u)
if c == 0:
nothing += 1
if ans == 1:
if c >= 2:
total += c * (c - 1) // 2 - c_1 * (c_1 - 1) // 2
if ans == 2:
if c >= 2:
total = c * (c - 1) // 2 - c_1 * (c_1 - 1) // 2
ans = 1
if c == 1 and len(g[i]) == 1:
total += n - 2
if ans == -1:
if c >= 2:
ans = 1
total = c * (c - 1) // 2 - c_1 * (c_1 - 1) // 2
if c == 1 and len(g[i]) == 1:
ans = 2
total = n - 2
if nothing == n:
print(n * (n - 1) * (n - 2) // 6)
else:
print(ans, total)
| 50 | 686 | 15,769,600 |
15261645
|
n, m = [int(x) for x in input().split()]
E = {i:[] for i in range(n)}
for i in range(m):
u, v = [int(x)-1 for x in input().split()]
E[v].append(u)
E[u].append(v)
def dfs():
visited = [False for i in range(n)]
colour = [0 for i in range(n)]
ans = 0
for v in range(n):
if visited[v]: continue
stack = [(v, 0)]
part = [0, 0]
while stack:
node, c = stack.pop()
if not visited[node]:
part[c] += 1
visited[node] = True
colour[node] = c
stack.extend((u,c^1) for u in E[node])
elif c != colour[node]:
return (0, 1)
ans += (part[0]*(part[0] - 1) + part[1]*(part[1] - 1)) // 2
return (1, ans)
if m == 0:
print(3, n*(n-1)*(n-2)//6)
elif max(len(E[v]) for v in E) == 1:
print(2, m*(n-2))
else:
ans = dfs()
print(ans[0], ans[1])
|
Codeforces Round 311 (Div. 2)
|
CF
| 2,015 | 1 | 256 |
Vitaly and Cycle
|
After Vitaly was expelled from the university, he became interested in the graph theory.
Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.
Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w — the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.
Two ways to add edges to the graph are considered equal if they have the same sets of added edges.
Since Vitaly does not study at the university, he asked you to help him with this task.
|
The first line of the input contains two integers n and m ($$3 \leq n \leq 10^5, 0 \leq m \leq \min\left(\frac{n(n-1)}{2}, 10^5\right)$$ — the number of vertices in the graph and the number of edges in the graph.
Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n) — the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.
It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.
|
Print in the first line of the output two space-separated integers t and w — the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.
| null |
The simple cycle is a cycle that doesn't contain any vertex twice.
|
[{"input": "4 4\n1 2\n1 3\n4 2\n4 3", "output": "1 2"}, {"input": "3 3\n1 2\n2 3\n3 1", "output": "0 1"}, {"input": "3 0", "output": "3 1"}]
| 2,000 |
["combinatorics", "dfs and similar", "graphs", "math"]
| 50 |
[{"input": "4 4\r\n1 2\r\n1 3\r\n4 2\r\n4 3\r\n", "output": "1 2\r\n"}, {"input": "3 3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "0 1\r\n"}, {"input": "3 0\r\n", "output": "3 1\r\n"}, {"input": "6 3\r\n1 2\r\n4 3\r\n6 5\r\n", "output": "2 12\r\n"}, {"input": "100000 0\r\n", "output": "3 166661666700000\r\n"}, {"input": "5 4\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n", "output": "1 6\r\n"}, {"input": "6 3\r\n1 2\r\n2 3\r\n4 5\r\n", "output": "1 1\r\n"}, {"input": "5 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "0 1\r\n"}, {"input": "59139 0\r\n", "output": "3 34470584559489\r\n"}, {"input": "9859 0\r\n", "output": "3 159667007809\r\n"}, {"input": "25987 0\r\n", "output": "3 2924603876545\r\n"}, {"input": "9411 0\r\n", "output": "3 138872935265\r\n"}, {"input": "25539 0\r\n", "output": "3 2775935665889\r\n"}, {"input": "59139 1\r\n10301 5892\r\n", "output": "2 59137\r\n"}, {"input": "9859 1\r\n1721 9478\r\n", "output": "2 9857\r\n"}, {"input": "76259 0\r\n", "output": "3 73910302948209\r\n"}, {"input": "92387 0\r\n", "output": "3 131421748719345\r\n"}, {"input": "6 4\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n", "output": "0 1\r\n"}]
| false |
stdio
| null | true |
69/C
|
69
|
C
|
Python 3
|
TESTS
| 3 | 186 | 307,200 |
52240941
|
k, n, m, q = [int(i) for i in input().split()]
basic = [input() for i in range(n)]
composite = {}
for i in range(m):
name, items = input().split(":")
composite[name] = {}
for item in items.split(","):
component, cnt = item.split()
composite[name][component] = int(cnt)
friends = {}
for i in range(1, k+1):
friends[str(i)] = {}
for artifact in basic:
friends[str(i)][artifact] = 0
for artifact in composite.keys():
friends[str(i)][artifact] = 0
for i in range(q):
ai, artifact = input().split()
friends[ai][artifact] += 1
for artifact, combo in composite.items():
if all(friends[ai][basic]==cnt for basic, cnt in combo.items()):
for basic, cnt in combo.items():
friends[ai][basic] -= cnt
friends[ai][artifact] += 1
break
for i in range(1, k+1):
print(sum(friends[str(i)].values()))
for artifact in sorted(friends[str(i)].keys()):
if friends[str(i)][artifact] > 0:
print(artifact, friends[str(i)][artifact])
| 47 | 154 | 5,324,800 |
16947235
|
k,m,n,q = map(int, input().split())
basic = [input() for _ in range(m)]
from collections import Counter, defaultdict
recipes, accounts = defaultdict(Counter), defaultdict(Counter)
for _ in range(n):
composite, components = input().split(': ')
for unit in components.split(', '):
x, y = unit.split()
recipes[composite][x] += int(y)
for _ in range(q):
player, artifact = input().split()
p = int(player)
accounts[p][artifact] += 1
for composite, recipe in recipes.items():
if all(num <= accounts[p][a] for a, num in recipe.items()):
for basic, num in recipe.items():
accounts[p][basic] -= num
accounts[p][composite] += 1
break
for player in range(1, k+1):
artifacts = sorted((+accounts[player]).keys())
print(len(artifacts))
for artifact in artifacts:
num = accounts[player][artifact]
print(artifact, num)
|
Codeforces Beta Round 63 (Div. 2)
|
CF
| 2,011 | 2 | 256 |
Game
|
In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game "HaresButtle", which differs from all other games in this genre. It has a huge number of artifacts. As we know, artifacts are divided into basic and composite ones. Only the basic artifacts are available on sale. More powerful composite artifacts are collected from some number of basic artifacts.
After the composing composite artifact, all the components disappear.
Kostya is the head of the alliance, so he has to remember, what artifacts has not only himself, but also his allies. You must identify by sequence of artifacts purchased by Kostya and his allies, how many and which artifacts has been collected by each of them. It is believed that initially no one has any artifacts.
|
The first line has 4 natural numbers: k (1 ≤ k ≤ 100) — the number of Kostya's allies, n (1 ≤ n ≤ 50) — the number of basic artifacts, m (0 ≤ m ≤ 50) — the number of composite artifacts, q (1 ≤ q ≤ 500) — the number of his friends' purchases. The following n lines contain the names of basic artifacts. After them m lines contain the descriptions of composite artifacts in the following format:
<Art. Name>: <Art. №1> <Art. №1 Number>, <Art. №2> <Art. №2 Number>, ... <Art. №X> <Art. №Х Number>
All the numbers are natural numbers not exceeding 100 (1 ≤ X ≤ n).
The names of all artifacts are different, they are composed of lowercase Latin letters, and the length of each name is from 1 to 100 characters inclusive. All the words in the format of the description of a composite artifact are separated by exactly one space. It is guaranteed that all components of the new artifact are different and have already been met in the input data as the names of basic artifacts.
Next, each of the following q lines is characterized by the number ai, the number of a friend who has bought the artifact (1 ≤ ai ≤ k), and the name of the purchased basic artifact. Let's assume that the backpacks of the heroes are infinitely large and any artifact bought later can fit in there.
It is guaranteed that after the i-th purchase no more than one opportunity to collect the composite artifact appears. If such an opportunity arose, the hero must take advantage of it.
|
The output file should consist of k blocks. The first line should contain number bi — the number of different artifacts the i-th ally has. Then the block should contain bi lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the lexicographical order of the names of the artifacts. In each block all the artifacts must be different, and all the numbers except the bi should be positive.
| null | null |
[{"input": "2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: perseverance 2\n1 desolator\n2 perseverance\n1 refresher\n2 desolator\n2 perseverance", "output": "1\nvanguard 1\n2\ndesolator 1\nmaelstorm 1"}]
| 2,000 |
["implementation"]
| 47 |
[{"input": "2 3 2 5\r\ndesolator\r\nrefresher\r\nperseverance\r\nvanguard: desolator 1, refresher 1\r\nmaelstorm: perseverance 2\r\n1 desolator\r\n2 perseverance\r\n1 refresher\r\n2 desolator\r\n2 perseverance\r\n", "output": "1\r\nvanguard 1\r\n2\r\ndesolator 1\r\nmaelstorm 1\r\n"}, {"input": "2 3 2 5\r\na\r\nb\r\nc\r\nd: a 1, b 1\r\ne: c 2\r\n1 a\r\n2 c\r\n1 b\r\n2 c\r\n2 c\r\n", "output": "1\r\nd 1\r\n2\r\nc 1\r\ne 1\r\n"}, {"input": "10 1 1 3\r\na\r\nb: a 2\r\n1 a\r\n1 a\r\n1 a\r\n", "output": "2\r\na 1\r\nb 1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "2 3 2 5\r\ndesolator\r\nrefresher\r\nperseverance\r\nvanguard: desolator 1, refresher 1\r\nmaelstorm: refresher 2\r\n1 desolator\r\n2 perseverance\r\n1 refresher\r\n1 refresher\r\n2 perseverance\r\n", "output": "2\r\nrefresher 1\r\nvanguard 1\r\n1\r\nperseverance 2\r\n"}, {"input": "1 1 1 1\r\na\r\nb: a 1\r\n1 a\r\n", "output": "1\r\nb 1\r\n"}, {"input": "2 3 2 5\r\ndesolator\r\nrefresher\r\nperseverance\r\nvanguard: desolator 1, refresher 3\r\nmaelstorm: refresher 2\r\n1 desolator\r\n2 perseverance\r\n1 refresher\r\n1 refresher\r\n2 perseverance\r\n", "output": "2\r\ndesolator 1\r\nmaelstorm 1\r\n1\r\nperseverance 2\r\n"}, {"input": "1 2 0 2\r\nb\r\na\r\n1 a\r\n1 b\r\n", "output": "2\r\na 1\r\nb 1\r\n"}, {"input": "2 3 2 5\r\na\r\nb\r\nc\r\nd: a 1, b 21\r\ne: c 2\r\n1 a\r\n2 c\r\n1 b\r\n2 c\r\n2 c\r\n", "output": "2\r\na 1\r\nb 1\r\n2\r\nc 1\r\ne 1\r\n"}, {"input": "1 1 2 2\r\na\r\nb: a 2\r\nc: a 1\r\n1 a\r\n1 a\r\n", "output": "1\r\nc 2\r\n"}]
| false |
stdio
| null | true |
689/D
|
689
|
D
|
PyPy 3
|
TESTS
| 3 | 217 | 30,515,200 |
129883529
|
#!/usr/bin/env python3
# from typing import *
import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq
def input():
return sys.stdin.readline()[:-1]
# sys.setrecursionlimit(1000000)
# _INPUT = """3
# 1 1 1
# 1 1 1
# """
# sys.stdin = io.StringIO(_INPUT)
INF = 10**10
class SparseTable_Min:
def __init__(self, a):
n = len(a)
self.a = a
self.LogTable = [0, 0]
for i in range(2, n+1):
self.LogTable.append(self.LogTable[i>>1] + 1)
max_k = self.LogTable[n]
self.Table = [[0] * (max_k+1) for _ in range(n)]
for i in range(n):
self.Table[i][0] = i
for k in range(1, max_k+1):
for i in range(n-(1<<k)+1):
i1 = self.Table[i][k-1]
i2 = self.Table[i+(1<<(k-1))][k-1]
if a[i1] < a[i2]:
self.Table[i][k] = i1
else:
self.Table[i][k] = i2
def query_rmin(self, l, r):
k = self.LogTable[r-l]
i1 = self.Table[l][k]
i2 = self.Table[r-(1<<k)][k]
return min(self.a[i1], self.a[i2])
class SparseTable_Max:
def __init__(self, a):
n = len(a)
self.a = a
self.LogTable = [0, 0]
for i in range(2, n+1):
self.LogTable.append(self.LogTable[i>>1] + 1)
max_k = self.LogTable[n]
self.Table = [[0] * (max_k+1) for _ in range(n)]
for i in range(n):
self.Table[i][0] = i
for k in range(1, max_k+1):
for i in range(n-(1<<k)+1):
i1 = self.Table[i][k-1]
i2 = self.Table[i+(1<<(k-1))][k-1]
if a[i1] > a[i2]:
self.Table[i][k] = i1
else:
self.Table[i][k] = i2
def query_rmax(self, l, r):
k = self.LogTable[r-l]
i1 = self.Table[l][k]
i2 = self.Table[r-(1<<k)][k]
return max(self.a[i1], self.a[i2])
def check1(i, k):
v1 = sp_table_1.query_rmax(i, k) # seg_tree_1.query_rmax(i, k) #max(A[i:k])
v2 = sp_table_2.query_rmin(i, k) # seg_tree_2.query_rmin(i, k) #min(B[i:k])
return (v1 <= v2)
def check2(i, k):
v1 = sp_table_1.query_rmax(i, k) # seg_tree_1.query_rmax(i, k) #max(A[i:k])
v2 = sp_table_2.query_rmin(i, k) # seg_tree_2.query_rmin(i, k) #min(B[i:k])
return (v1 > v2)
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
sp_table_1 = SparseTable_Max(A)
sp_table_2 = SparseTable_Min(B)
for i in range(N):
if A[i] > B[i]:
continue
if sp_table_1.query_rmax(i, N) < sp_table_2.query_rmin(i, N):
continue
if A[i] == B[i]:
i1 = i
else:
ok = i
ng = N
while abs(ng - ok) > 1:
mid = (ok+ng) // 2
if check1(i, mid):
ok = mid
else:
ng = mid
i1 = ok
if sp_table_1.query_rmax(i, i1+1) == sp_table_2.query_rmin(i, i1+1):
ok = N
ng = i
while abs(ng - ok) > 1:
mid = (ok+ng) // 2
if check2(i, mid):
ok = mid
else:
ng = mid
if sp_table_1.query_rmax(i, ok) == sp_table_2.query_rmin(i, ok):
i2 = ok-1
else:
i2 = ok-2
if i2-i1+1 > 0:
n = i2-i1+1
ans += n
# print(i, (i1, i2), n)
print(ans)
| 94 | 780 | 29,798,400 |
159329328
|
import bisect
import copy
import gc
import itertools
from array import array
from fractions import Fraction
import heapq
import math
import operator
import os, sys
import profile
import cProfile
import random
import re
import string
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
from functools import reduce, lru_cache
from io import IOBase, BytesIO
from itertools import count, groupby, accumulate, permutations, combinations_with_replacement, product
from math import gcd
from operator import xor, add
from typing import List
input = lambda: sys.stdin.readline().rstrip("\r\n")
# print = lambda d: sys.stdout.write(str(d)+"\n")
def read_int_list(): return list(map(int, input().split()))
def read_int_tuple(): return tuple(map(int, input().split()))
def read_int(): return int(input())
# endregion
if 'AW' in os.environ['COMPUTERNAME']:
f = open('inputs', 'r')
def input(): return f.readline().rstrip("\r\n")
def solve(n, a, b):
mx, mn = deque(), deque()
j = res = 0
for i, (x, y) in enumerate(zip(a, b)):
while mx and a[mx[-1]] <= x:
mx.pop()
mx.append(i)
while mn and b[mn[-1]] >= y:
mn.pop()
mn.append(i)
while j <= i and a[mx[0]] > b[mn[0]]:
j += 1
while mx and mx[0] < j: mx.popleft()
while mn and mn[0] < j: mn.popleft()
if mx and mn and a[mx[0]] == b[mn[0]]:
res += min(mx[0], mn[0]) - j + 1
return res
def main():
for _ in range(1):
n = read_int()
a = read_int_list()
b = read_int_list()
print(solve(n, a, b))
if __name__ == "__main__":
main()
# cProfile.run("main()")
|
Codeforces Round 361 (Div. 2)
|
CF
| 2,016 | 2 | 512 |
Friends and Subsequences
|
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?
Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of $$\max_{i=l}^{r} a_i$$ while !Mike can instantly tell the value of $$\min_{i=l} b_i$$.
Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs $$\max_{i=l}^{r}a_i = \min_{i=l}^{r}b_i$$ is satisfied.
How many occasions will the robot count?
|
The first line contains only integer n (1 ≤ n ≤ 200 000).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.
The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.
|
Print the only integer number — the number of occasions the robot will count, thus for how many pairs $$\max_{i=l}^{r}a_i = \min_{i=l}^{r}b_i$$ is satisfied.
| null |
The occasions in the first sample case are:
1.l = 4,r = 4 since max{2} = min{2}.
2.l = 4,r = 5 since max{2, 1} = min{2, 3}.
There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
|
[{"input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2", "output": "2"}, {"input": "3\n3 3 3\n1 1 1", "output": "0"}]
| 2,100 |
["binary search", "data structures"]
| 94 |
[{"input": "6\r\n1 2 3 2 1 4\r\n6 7 1 2 3 2\r\n", "output": "2\r\n"}, {"input": "3\r\n3 3 3\r\n1 1 1\r\n", "output": "0\r\n"}, {"input": "17\r\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388097 -959271262\r\n-417715348 -959271262 -959271262 714413739 -189641729 571055593 571055593 571055593 -417715348 -417715348 192457837 -745891378 536388097 571055593 -189641729 571055593 -670860364\r\n", "output": "1\r\n"}, {"input": "1\r\n509658558\r\n509658558\r\n", "output": "1\r\n"}, {"input": "1\r\n509658558\r\n-544591380\r\n", "output": "0\r\n"}, {"input": "3\r\n1 1 1\r\n2 2 2\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
689/D
|
689
|
D
|
Python 3
|
TESTS
| 3 | 109 | 0 |
58705779
|
def solve():
n=int(input())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
maxi=a[0]
mini=b[0]
count=0
mxi=0
mni=0
if maxi==mini:
count+=1
flag=0
for i in range(1,n):
if flag==1:
maxi=a[i]
mini=b[i]
mxi=i;mni=i
flag=0
#print(mini,maxi,a[i])
if a[i]==b[i] :
count+=1
#print("lol")
if a[i]>b[i] and i<n-1:
flag=1
elif a[i]>maxi:
maxi=a[i]
elif b[i]<mini :
#print(mini,maxi)
mini=b[i]
if maxi==mini :
count+=i-mni
#print("FOL",a[i],b[i])
#print(maxi,mini)
print(count)
solve()
| 94 | 982 | 456,908,800 |
159169002
|
dx=[[0]*(1<<20) for _ in range(20)]
dy=[[0]*(1<<20) for _ in range(20)]
lg=[0]*(1<<20)
def mx(l,r):
p=lg[r-l+1]
return max(dx[p][l],dx[p][r-(1<<p)+1])
def mn(l,r):
p=lg[r-l+1]
return min(dy[p][l],dy[p][r-(1<<p)+1])
if __name__ == '__main__':
n=int(input())
dx[0][1:n]=[int(x) for x in input().split()]
dy[0][1:n]=[int(x) for x in input().split()]
for p in range(1,20):
if (1<<p)>n:
break
for i in range(1,n+2-(1<<p)):
dx[p][i]=max(dx[p-1][i],dx[p-1][i+(1<<(p-1))])
dy[p][i]=min(dy[p-1][i],dy[p-1][i+(1<<(p-1))])
for i in range(2,n+1):
lg[i]=lg[i>>1]+1
ans=0
for i in range(1,n+1):
if mx(i,i)<=mn(i,i):
l=i-1
r=i
p=1<<lg[n-i+1]
while p:
if p+l<=n and mx(i,l+p)<mn(i,l+p):l+=p
if p+r<=n and mx(i,r+p)<=mn(i,r+p):r+=p
p>>=1
ans+=r-l
print(ans)
|
Codeforces Round 361 (Div. 2)
|
CF
| 2,016 | 2 | 512 |
Friends and Subsequences
|
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?
Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of $$\max_{i=l}^{r} a_i$$ while !Mike can instantly tell the value of $$\min_{i=l} b_i$$.
Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs $$\max_{i=l}^{r}a_i = \min_{i=l}^{r}b_i$$ is satisfied.
How many occasions will the robot count?
|
The first line contains only integer n (1 ≤ n ≤ 200 000).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.
The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.
|
Print the only integer number — the number of occasions the robot will count, thus for how many pairs $$\max_{i=l}^{r}a_i = \min_{i=l}^{r}b_i$$ is satisfied.
| null |
The occasions in the first sample case are:
1.l = 4,r = 4 since max{2} = min{2}.
2.l = 4,r = 5 since max{2, 1} = min{2, 3}.
There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
|
[{"input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2", "output": "2"}, {"input": "3\n3 3 3\n1 1 1", "output": "0"}]
| 2,100 |
["binary search", "data structures"]
| 94 |
[{"input": "6\r\n1 2 3 2 1 4\r\n6 7 1 2 3 2\r\n", "output": "2\r\n"}, {"input": "3\r\n3 3 3\r\n1 1 1\r\n", "output": "0\r\n"}, {"input": "17\r\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388097 -959271262\r\n-417715348 -959271262 -959271262 714413739 -189641729 571055593 571055593 571055593 -417715348 -417715348 192457837 -745891378 536388097 571055593 -189641729 571055593 -670860364\r\n", "output": "1\r\n"}, {"input": "1\r\n509658558\r\n509658558\r\n", "output": "1\r\n"}, {"input": "1\r\n509658558\r\n-544591380\r\n", "output": "0\r\n"}, {"input": "3\r\n1 1 1\r\n2 2 2\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
455/B
|
455
|
B
|
Python 3
|
TESTS
| 4 | 31 | 0 |
221607145
|
n, k = map(int, input().split())
if k % 2 == 0:
print('Second')
else:
print('First')
| 75 | 920 | 6,451,200 |
7386129
|
"""
Codeforces Contest 260 Div 1 Problem B
Author : chaotic_iak
Language: Python 3.3.4
"""
def main():
n,k = read()
s = set()
for i in range(n): s.add(read(0))
s = list(s)
s.sort()
s = treeify(s)
res = solve(s)
if res == 0: # neither: second player win
print("Second")
if res == 1: # odd: first player win if k is odd
print("First" if k % 2 else "Second")
if res == 2: # even: second player win
print("Second")
if res == 3: # both: first player win
print("First")
def treeify(s):
res = [[] for _ in range(26)]
for i in s:
if i: res[ord(i[0]) - 97].append(i[1:])
fin = []
for i in range(26):
if res[i]: fin.append(treeify(res[i]))
return fin
def solve(s, parity=2):
for i in range(len(s)):
if isinstance(s[i], list): s[i] = solve(s[i], 3-parity)
if not s: return parity # no possible move: current parity
if 0 in s: return 3 # any neither: both
if 1 in s and 2 in s: return 3 # any odd and any even: both
if 1 in s: return 1 # any odd: odd
if 2 in s: return 2 # any even: even
return 0 # all both: neither
################################### NON-SOLUTION STUFF BELOW
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0: return inputs
if mode == 1: return inputs.split()
if mode == 2: return map(int, inputs.split())
def write(s="\n"):
if isinstance(s, list): s = " ".join(map(str, s))
s = str(s)
print(s, end="")
main()
|
Codeforces Round 260 (Div. 1)
|
CF
| 2,014 | 1 | 256 |
A Lot of Games
|
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players.
Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move.
Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.
|
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109).
Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
|
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
| null | null |
[{"input": "2 3\na\nb", "output": "First"}, {"input": "3 1\na\nb\nc", "output": "First"}, {"input": "1 2\nab", "output": "Second"}]
| 1,900 |
["dfs and similar", "dp", "games", "implementation", "strings", "trees"]
| 75 |
[{"input": "2 3\r\na\r\nb\r\n", "output": "First\r\n"}, {"input": "3 1\r\na\r\nb\r\nc\r\n", "output": "First\r\n"}, {"input": "1 2\r\nab\r\n", "output": "Second\r\n"}, {"input": "5 6\r\nabas\r\ndsfdf\r\nabacaba\r\ndartsidius\r\nkolobok\r\n", "output": "Second\r\n"}, {"input": "4 2\r\naaaa\r\nbbbb\r\nccccc\r\ndumbavumba\r\n", "output": "First\r\n"}, {"input": "3 8\r\nso\r\nbad\r\ntest\r\n", "output": "First\r\n"}, {"input": "5 2\r\nwelcome\r\nto\r\nthe\r\nmatrix\r\nneo\r\n", "output": "First\r\n"}, {"input": "6 4\r\ndog\r\ncat\r\ncow\r\nhot\r\nice\r\nlol\r\n", "output": "Second\r\n"}, {"input": "4 8\r\nla\r\na\r\nz\r\nka\r\n", "output": "First\r\n"}, {"input": "3 2\r\nop\r\nhop\r\ncop\r\n", "output": "First\r\n"}, {"input": "3 3\r\nabasdfabab\r\nabaaasdfdsf\r\nasdfaba\r\n", "output": "Second\r\n"}, {"input": "2 2\r\naba\r\naa\r\n", "output": "Second\r\n"}, {"input": "4 1\r\naa\r\naba\r\nba\r\nbba\r\n", "output": "Second\r\n"}, {"input": "1 3\r\nab\r\n", "output": "Second\r\n"}, {"input": "3 3\r\naa\r\nabb\r\ncc\r\n", "output": "Second\r\n"}]
| false |
stdio
| null | true |
22/D
|
22
|
D
|
Python 3
|
TESTS
| 1 | 31 | 0 |
149816631
|
n = int(input())
intervale = []
for _ in range(n):
x, y = input().split()
intervale.append((int(x), int(y)))
# Inversarea capetelor
for i, interval in enumerate(intervale):
st, dr = interval
if st > dr:
interval = (dr, st)
intervale[i] = interval
# Sortare dupa capatul din dreapta
intervale.sort(key=lambda tuplu: tuplu[1])
prev = intervale[0][1]
nr_cuie = 1
cuie = [prev]
for i in range(1, len(intervale)):
st, dr = intervale[i] # capetele
if st <= prev <= dr:
continue
else:
prev = dr
cuie.append(prev)
nr_cuie += 1
print(nr_cuie)
print(cuie if len(cuie) > 1 else cuie[0])
| 40 | 78 | 0 |
203572165
|
def main():
n = int(input())
segments = []
for _ in range(n):
segments.append(tuple(sorted(map(int, input().split()))))
nails = []
segments.sort(key=lambda x: x[1])
while segments:
nail = segments[0][1]
nails.append(nail)
segments = [segment for segment in segments if segment[0] > nail]
print(len(nails))
print(' '.join(map(str, nails)))
if __name__ == "__main__":
main()
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010 | 1 | 256 |
Segments
|
You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
|
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
|
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
| null | null |
[{"input": "2\n0 2\n2 5", "output": "1\n2"}, {"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n7 10 3"}]
| 1,900 |
["greedy", "sortings"]
| 40 |
[{"input": "2\r\n0 2\r\n2 5\r\n", "output": "1\r\n2 "}, {"input": "5\r\n0 3\r\n4 2\r\n4 8\r\n8 10\r\n7 7\r\n", "output": "3\r\n3 7 10 "}, {"input": "3\r\n40 -83\r\n52 -80\r\n-21 -4\r\n", "output": "1\r\n-4 "}, {"input": "4\r\n67 -88\r\n37 -62\r\n-26 91\r\n-99 -50\r\n", "output": "2\r\n-50 91 "}, {"input": "5\r\n45 58\r\n22 6\r\n-32 36\r\n49 -37\r\n43 62\r\n", "output": "2\r\n22 58 "}, {"input": "10\r\n47 -85\r\n71 40\r\n-87 64\r\n76 73\r\n52 -51\r\n-20 -57\r\n-14 -77\r\n-45 -11\r\n-56 -48\r\n81 20\r\n", "output": "4\r\n-48 -11 71 76 "}, {"input": "1\r\n0 0\r\n", "output": "1\r\n0 "}, {"input": "4\r\n1 1\r\n0 0\r\n10000 10000\r\n-10000 -10000\r\n", "output": "4\r\n-10000 0 1 10000 "}, {"input": "3\r\n55 55\r\n55 55\r\n55 55\r\n", "output": "1\r\n55 "}, {"input": "4\r\n55 55\r\n55 55\r\n55 55\r\n55 56\r\n", "output": "1\r\n55 "}, {"input": "1\r\n-2244 5023\r\n", "output": "1\r\n5023 "}]
| false |
stdio
| null | true |
802/B
|
802
|
B
|
PyPy 3
|
TESTS
| 3 | 92 | 102,400 |
106896922
|
import sys
input = sys.stdin.readline
from collections import deque, defaultdict
from heapq import heappush, heappop
n, k = map(int, input().split())
A = list(map(int, input().split()))
dic = defaultdict(deque)
for i, a in enumerate(A):
dic[a].append(i)
S = set()
ans = 0
hp = []
for d in dic:
heappush(hp, (-dic[d][0], d))
for i, a in enumerate(A):
# for d in dic:
# while dic[d] and dic[d][0] <= i:
# dic[d].popleft()
# if dic[d]: heappush(hp, (-dic[d][0], d))
if a not in S:
if len(S) < k:
S.add(a)
ans += 1
else:
while hp and -hp[0][0] <= i:
heappop(hp)
idx = hp[0][1] if hp else -1
S.discard(idx)
S.add(a)
ans += 1
if a in dic:
dic[a].popleft()
if dic[a]: heappush(hp, (-dic[a][0], a))
else: heappush(hp, (float("-inf"), a))
else:
heappush(hp, (float("-inf"), a))
print(ans)
| 50 | 2,744 | 91,443,200 |
60347194
|
# https://codeforces.com/problemset/problem/802/B
import heapq
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
pos = {}
Q = []
cnt = 0
for i, x in enumerate(a):
if x not in pos:
pos[x] = []
pos[x].append(i)
for i, x in enumerate(a):
if x not in d:
cnt += 1
if len(d) == k:
pos_, x_ = heapq.heappop(Q)
del d[x_]
d[x] = 1
pos[x].pop(0)
if len(pos[x]) > 0:
heapq.heappush(Q, (-pos[x][0], x))
else:
heapq.heappush(Q, (-float('inf'), x))
print(cnt)
|
Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)
|
ICPC
| 2,017 | 10 | 256 |
Heidi and Library (medium)
|
Whereas humans nowadays read fewer and fewer books on paper, book readership among marmots has surged. Heidi has expanded the library and is now serving longer request sequences.
|
Same as the easy version, but the limits have changed: 1 ≤ n, k ≤ 400 000.
|
Same as the easy version.
| null | null |
[{"input": "4 100\n1 2 2 1", "output": "2"}, {"input": "4 1\n1 2 2 1", "output": "3"}, {"input": "4 2\n1 2 3 1", "output": "3"}]
| 1,800 |
["data structures", "greedy"]
| 50 |
[{"input": "4 100\r\n1 2 2 1\r\n", "output": "2\r\n"}, {"input": "4 1\r\n1 2 2 1\r\n", "output": "3\r\n"}, {"input": "4 2\r\n1 2 3 1\r\n", "output": "3\r\n"}, {"input": "11 1\r\n1 2 3 5 1 10 10 1 1 3 5\r\n", "output": "9\r\n"}, {"input": "5 2\r\n1 2 3 1 2\r\n", "output": "4\r\n"}, {"input": "4 2\r\n1 2 3 2\r\n", "output": "3\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}]
| false |
stdio
| null | true |
799/C
|
799
|
C
|
PyPy 3
|
TESTS
| 3 | 124 | 0 |
97239176
|
inven = str(input()).split(" ")
coins = []
diamonds = []
smuk = []
for x in range(int(inven[0])):
tain = input().split(" ")
if str(tain[2]) == "C":
coins.append(tain)
if str(tain[2]) == "D":
diamonds.append(tain)
coinsAvai = []
diamondsAvai = []
for x in coins:
if int(x[1]) <= int(inven[1]):
coinsAvai.append(x)
for b in diamonds:
if int(b[1]) <= int(inven[2]):
diamondsAvai.append(b)
coinsAvai.sort(key=lambda x: x[0])
diamondsAvai.sort(key=lambda x: x[0])
collection = []
remainCoin = int(inven[1])
for x in coinsAvai:
if int(x[1]) < remainCoin or int(x[1]) == remainCoin:
remainCoin = remainCoin - int(x[1])
collection.append([1])
smuk.append(int(x[0]))
remainDiamond = int(inven[2])
for x in diamondsAvai:
if int(x[1]) < remainDiamond or int(x[1]) == remainDiamond:
remainDiamond = remainCoin - int(x[1])
collection.append([1])
smuk.append(int(x[0]))
if len(collection) > 1:
print(sum(smuk))
else:
print(0)
| 79 | 748 | 13,107,200 |
212867129
|
import operator
fc = []
fd = []
n, c, d = map(int,input().split())
for _ in range(n):
b, p, m = input().split()
b, p = int(b), int(p)
if m == 'C':
if p <= c:
fc.append((b, p))
else:
if p <= d:
fd.append((b, p))
fc.sort(key=operator.itemgetter(0), reverse=True)
fd.sort(key=operator.itemgetter(0), reverse=True)
mx = fc[0][0] + fd[0][0] if fc and fd else 0
for l, m in ((fc, c), (fd, d)):
for i in range(len(l) - 1):
b1, p1 = l[i]
if b1 * 2 <= mx:
break
for j in range(i + 1, len(l)):
b2, p2 = l[j]
if b1 + b2 <= mx:
break
if p1 + p2 <= m:
mx = b1 + b2
break
print(mx)
|
Playrix Codescapes Cup (Codeforces Round 413, rated, Div. 1 + Div. 2)
|
CF
| 2,017 | 2 | 256 |
Fountains
|
Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed.
Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.
|
The first line contains three integers n, c and d (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) — the number of fountains, the number of coins and diamonds Arkady has.
The next n lines describe fountains. Each of these lines contain two integers bi and pi (1 ≤ bi, pi ≤ 100 000) — the beauty and the cost of the i-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain i: in coins or in diamonds, respectively.
|
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
| null |
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.
In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.
|
[{"input": "3 7 6\n10 8 C\n4 3 C\n5 6 D", "output": "9"}, {"input": "2 4 5\n2 5 C\n2 1 D", "output": "0"}, {"input": "3 10 10\n5 5 C\n5 5 C\n10 11 D", "output": "10"}]
| 1,800 |
["binary search", "data structures", "implementation"]
| 79 |
[{"input": "3 7 6\r\n10 8 C\r\n4 3 C\r\n5 6 D\r\n", "output": "9\r\n"}, {"input": "2 4 5\r\n2 5 C\r\n2 1 D\r\n", "output": "0\r\n"}, {"input": "3 10 10\r\n5 5 C\r\n5 5 C\r\n10 11 D\r\n", "output": "10\r\n"}, {"input": "6 68 40\r\n1 18 D\r\n6 16 D\r\n11 16 D\r\n7 23 D\r\n16 30 D\r\n2 20 D\r\n", "output": "18\r\n"}, {"input": "6 4 9\r\n6 6 D\r\n1 4 D\r\n6 7 C\r\n7 6 D\r\n5 7 D\r\n2 5 D\r\n", "output": "3\r\n"}, {"input": "52 38 22\r\n9 25 D\r\n28 29 C\r\n29 25 D\r\n4 28 D\r\n23 29 D\r\n24 25 D\r\n17 12 C\r\n11 19 C\r\n13 14 C\r\n12 15 D\r\n7 25 C\r\n2 25 C\r\n6 17 C\r\n2 20 C\r\n15 23 D\r\n8 21 C\r\n13 15 D\r\n29 15 C\r\n25 20 D\r\n22 20 C\r\n2 13 D\r\n13 22 D\r\n27 20 C\r\n1 21 D\r\n22 17 C\r\n14 21 D\r\n4 25 D\r\n5 23 C\r\n9 21 C\r\n2 20 C\r\n14 18 C\r\n29 24 C\r\n14 29 D\r\n9 27 C\r\n23 21 D\r\n18 26 D\r\n7 23 C\r\n13 25 C\r\n21 26 C\r\n30 24 C\r\n21 24 C\r\n28 22 C\r\n8 29 C\r\n3 12 C\r\n21 22 D\r\n22 26 C\r\n13 17 D\r\n12 12 D\r\n11 11 C\r\n18 24 D\r\n7 13 D\r\n3 11 C\r\n", "output": "57\r\n"}, {"input": "6 68 40\r\n6 16 D\r\n11 16 D\r\n1 18 D\r\n2 20 D\r\n7 23 D\r\n16 30 D\r\n", "output": "18\r\n"}, {"input": "2 1 1\r\n1 1 C\r\n1 1 D\r\n", "output": "2\r\n"}, {"input": "2 100000 100000\r\n100000 100000 C\r\n100000 100000 D\r\n", "output": "200000\r\n"}, {"input": "4 15 9\r\n5 10 C\r\n5 10 D\r\n6 10 D\r\n7 5 C\r\n", "output": "12\r\n"}]
| false |
stdio
| null | true |
689/E
|
689
|
E
|
PyPy 3
|
TESTS
| 6 | 140 | 33,587,200 |
129502134
|
p = int(1e9+7)
N = 200000
fact= [1]*(N+1)
for i in range(2,N+1):
fact[i]=(fact[i-1]*i)%p
def bpow(a,b):
global p
c = 1
while b>0:
if b&1: c=(c*a)%p
a = (a*a)%p
b = b>>1
return c
def inv(a):
global p
return bpow(a,p-2)
def bino(n,k):
global fact,p
return fact[n]*inv(fact[k])%p*inv(fact[n-k])%p
def solv(ll,k):
global p
l = []
for s in ll:
l.append((s[0],0)) #to enter
l.append((s[1],1)) #to exit
l.sort(key=lambda t:(t[0],t[1]))
ss = set()
n = len(l)
sl = l[0][0] #segment left
sc = 1 #segment count
ans = 0
for i in range(1,n):
t = l[i]
if t[1]==1: #exit
f = t[0]-sl+1
ans = (ans+f*bino(sc,k)%p)%p
sc -= 1
sl = None if sc==0 else t[0]+1
else:
if sl is not None and t[0]!=sl:
f = t[0]-sl
ans = (ans+f*bino(sc,k)%p)%p
sc += 1
sl = t[0]
return ans
n,k = map(int,input().split())
ll = [list(map(int,input().split())) for _ in range(n)]
print(solv(ll,k))
| 69 | 810 | 80,281,600 |
197273749
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def comb(n, r):
return fact[n] * inv[r] % mod * inv[n - r] % mod if n >= r >= 0 else 0
n, k = map(int, input().split())
mod = pow(10, 9) + 7
l = n + 5
fact = [1] * (l + 1)
for i in range(1, l + 1):
fact[i] = i * fact[i - 1] % mod
inv = [1] * (l + 1)
inv[l] = pow(fact[l], mod - 2, mod)
for i in range(l - 1, -1, -1):
inv[i] = (i + 1) * inv[i + 1] % mod
lr = [tuple(map(int, input().split())) for _ in range(n)]
s = set()
for l, r in lr:
s.add(l)
s.add(r + 1)
s = list(s)
s.sort()
d = dict()
m = len(s)
for i in range(m):
d[s[i]] = i
cnt = [0] * m
for l, r in lr:
cnt[d[l]] += 1
cnt[d[r + 1]] -= 1
for i in range(1, m):
cnt[i] += cnt[i - 1]
ans = 0
for i in range(m - 1):
ans += comb(cnt[i], k) * (s[i + 1] - s[i]) % mod
ans %= mod
print(ans)
|
Codeforces Round 361 (Div. 2)
|
CF
| 2,016 | 3 | 256 |
Mike and Geometry Problem
|
Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to be the number of integer points in the segment [l, r] with l ≤ r (say that $$f(\varnothing) = 0$$). You are given two integers n and k and n closed intervals [li, ri] on OX axis and you have to find:
$$\sum_{1 \leq i_1 < i_2 < \ldots < i_k \leq n} f([l_{i_1}, r_{i_1}] \cap [l_{i_2}, r_{i_2}] \cap \ldots \cap [l_{i_k}, r_{i_k}])$$
In other words, you should find the sum of the number of integer points in the intersection of any k of the segments.
As the answer may be very large, output it modulo 1000000007 (109 + 7).
Mike can't solve this problem so he needs your help. You will help him, won't you?
|
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of segments and the number of segments in intersection groups respectively.
Then n lines follow, the i-th line contains two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109), describing i-th segment bounds.
|
Print one integer number — the answer to Mike's problem modulo 1000000007 (109 + 7) in the only line.
| null |
In the first example:
$$f([1,2]\cap[1,3])=f([1,2])=2$$;
$$f([1,2]\cap[2,3])=f([2,2])=1$$;
$$f([1,3]\cap[2,3])=f([2,3])=2$$.
So the answer is 2 + 1 + 2 = 5.
|
[{"input": "3 2\n1 2\n1 3\n2 3", "output": "5"}, {"input": "3 3\n1 3\n1 3\n1 3", "output": "3"}, {"input": "3 1\n1 2\n2 3\n3 4", "output": "6"}]
| 2,000 |
["combinatorics", "data structures", "dp", "geometry", "implementation"]
| 69 |
[{"input": "3 2\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "5\r\n"}, {"input": "3 3\r\n1 3\r\n1 3\r\n1 3\r\n", "output": "3\r\n"}, {"input": "3 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "6\r\n"}, {"input": "1 1\r\n45 70\r\n", "output": "26\r\n"}, {"input": "1 1\r\n-35 -8\r\n", "output": "28\r\n"}, {"input": "1 1\r\n-79 -51\r\n", "output": "29\r\n"}, {"input": "2 2\r\n26 99\r\n-56 40\r\n", "output": "15\r\n"}, {"input": "9 6\r\n-44 -29\r\n-11 85\r\n11 84\r\n-63 1\r\n75 89\r\n-37 61\r\n14 73\r\n78 88\r\n-22 -18\r\n", "output": "0\r\n"}, {"input": "2 2\r\n-93 -22\r\n12 72\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
802/J
|
802
|
J
|
PyPy 3-64
|
TESTS
| 29 | 61 | 1,638,400 |
211584191
|
from sys import stdin,stdout
# from bisect import bisect_left,bisect
# from heapq import heapify,heappop,heappush
# from sys import setrecursionlimit
# from collections import defaultdict,Counter
# from itertools import permutations
# from math import gcd,ceil,sqrt,factorial
# setrecursionlimit(int(1e5))
input,print = stdin.readline,stdout.write
n = int(input())
conn = [[0 for i in range(n)] for i in range(n)]
for i in range(n-1):
u,v,c = list(map(int,input().split()))
conn[u][v] = c
conn[v][u] = c
vis = [0 for i in range(n)]
for i in range(n):
for j in range(i):
if conn[i][j]!=0:
vis[i] = max(vis[i],vis[j]+conn[i][j])
ans = max(vis)
print(str(ans)+"\n")
| 32 | 46 | 0 |
196223605
|
def solve():
n = int(input())
a = [0] * n
for i in range(n - 1):
a[i] = list(map(int, input().split()))
p = [-2] * n
ct = [0] * n
p[0] = -1
for j in range(n - 1):
for i in range(n - 1):
u, v, c = a[i]
if p[u] != -2 and p[v] == -2:
p[v] = u
ct[v] = c
elif p[v] != -2 and p[u] == -2:
p[u] = v
ct[u] = c
cnt = [0] * n
lst = []
for i in range(n):
if p[i] != -1:
cnt[p[i]] += 1
for i in range(n):
if cnt[i] == 0:
lst.append(i)
ans = 0
for l in lst:
cost = 0
t = l
while t != -1:
cost += ct[t]
t = p[t]
if cost > ans:
ans = cost
print(ans)
return
solve()
|
Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)
|
ICPC
| 2,017 | 1 | 256 |
Send the Fool Further! (easy)
|
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
|
The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
|
Output a single integer – the maximum sum of costs.
| null |
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
|
[{"input": "4\n0 1 4\n0 2 2\n2 3 3", "output": "5"}, {"input": "6\n1 2 3\n0 2 100\n1 4 2\n0 3 7\n3 5 10", "output": "105"}, {"input": "11\n1 0 1664\n2 0 881\n3 2 4670\n4 2 1555\n5 1 1870\n6 2 1265\n7 2 288\n8 7 2266\n9 2 1536\n10 6 3378", "output": "5551"}]
| 1,400 |
["dfs and similar", "graphs", "trees"]
| 32 |
[{"input": "4\r\n0 1 4\r\n0 2 2\r\n2 3 3\r\n", "output": "5\r\n"}, {"input": "3\r\n1 0 5987\r\n2 0 8891\r\n", "output": "8891\r\n"}, {"input": "10\r\n1 0 518\r\n2 0 4071\r\n3 1 121\r\n4 2 3967\r\n5 3 9138\r\n6 2 9513\r\n7 3 3499\r\n8 2 2337\r\n9 4 7647\r\n", "output": "15685\r\n"}, {"input": "11\r\n1 0 6646\r\n2 0 8816\r\n3 2 9375\r\n4 2 5950\r\n5 1 8702\r\n6 2 2657\r\n7 2 885\r\n8 7 2660\r\n9 2 5369\r\n10 6 3798\r\n", "output": "18191\r\n"}, {"input": "10\r\n0 1 7171\r\n0 2 2280\r\n1 3 9126\r\n2 4 2598\r\n5 4 8320\r\n6 2 1855\r\n3 7 9204\r\n7 8 2145\r\n4 9 10\r\n", "output": "27646\r\n"}, {"input": "6\r\n1 2 3\r\n0 2 100\r\n1 4 2\r\n0 3 7\r\n3 5 10\r\n", "output": "105\r\n"}, {"input": "11\r\n1 0 1664\r\n2 0 881\r\n3 2 4670\r\n4 2 1555\r\n5 1 1870\r\n6 2 1265\r\n7 2 288\r\n8 7 2266\r\n9 2 1536\r\n10 6 3378\r\n", "output": "5551\r\n"}]
| false |
stdio
| null | true |
22/D
|
22
|
D
|
PyPy 3-64
|
TESTS
| 1 | 46 | 0 |
223732007
|
arr=[]
n=int(input())
for _ in range(n):
l,r=map(int,input().split())
arr.append([l,r])
arr.sort(key=lambda x:(x[1]))
ans=0
p=-float("inf")
res=[]
for el in arr:
l,r=el[0],el[1]
l,r=min(l,r),max(l,r)
if l>p:
ans+=1
res.append(r)
p=max(p,r)
print(ans)
print(*res)
| 40 | 93 | 1,945,600 |
145964552
|
from collections import defaultdict
import sys
input = sys.stdin.readline
n = int(input())
x = defaultdict(lambda : [])
for _ in range(n):
l, r = map(int, input().split())
if l > r:
l, r = r, l
x[r].append(l)
ans = []
u = -10005
for r in sorted(x.keys()):
for l in x[r]:
if u < l:
u = r
ans.append(r)
c = len(ans)
print(c)
print(*ans)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010 | 1 | 256 |
Segments
|
You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
|
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
|
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
| null | null |
[{"input": "2\n0 2\n2 5", "output": "1\n2"}, {"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n7 10 3"}]
| 1,900 |
["greedy", "sortings"]
| 40 |
[{"input": "2\r\n0 2\r\n2 5\r\n", "output": "1\r\n2 "}, {"input": "5\r\n0 3\r\n4 2\r\n4 8\r\n8 10\r\n7 7\r\n", "output": "3\r\n3 7 10 "}, {"input": "3\r\n40 -83\r\n52 -80\r\n-21 -4\r\n", "output": "1\r\n-4 "}, {"input": "4\r\n67 -88\r\n37 -62\r\n-26 91\r\n-99 -50\r\n", "output": "2\r\n-50 91 "}, {"input": "5\r\n45 58\r\n22 6\r\n-32 36\r\n49 -37\r\n43 62\r\n", "output": "2\r\n22 58 "}, {"input": "10\r\n47 -85\r\n71 40\r\n-87 64\r\n76 73\r\n52 -51\r\n-20 -57\r\n-14 -77\r\n-45 -11\r\n-56 -48\r\n81 20\r\n", "output": "4\r\n-48 -11 71 76 "}, {"input": "1\r\n0 0\r\n", "output": "1\r\n0 "}, {"input": "4\r\n1 1\r\n0 0\r\n10000 10000\r\n-10000 -10000\r\n", "output": "4\r\n-10000 0 1 10000 "}, {"input": "3\r\n55 55\r\n55 55\r\n55 55\r\n", "output": "1\r\n55 "}, {"input": "4\r\n55 55\r\n55 55\r\n55 55\r\n55 56\r\n", "output": "1\r\n55 "}, {"input": "1\r\n-2244 5023\r\n", "output": "1\r\n5023 "}]
| false |
stdio
| null | true |
22/D
|
22
|
D
|
Python 3
|
TESTS
| 1 | 46 | 0 |
150060011
|
n = int(input())
ls = []
batut = []
cuie = 0
for i in range(n):
ls.append(list(map(int,input().split())))
for x in ls:
if x[0]>x[1]:
(x[0],x[1])=(x[1],x[0])
ls_sorted = sorted(ls,key=lambda x: (int(x[0]),int(x[1])))
intersection = list(ls_sorted[0])
if n>1:
for i in range(1,n):
if intersection[0]<=ls_sorted[i][0]<=ls_sorted[i][1]<=intersection[1]:
intersection=ls_sorted[i]
if i == n-1:
cuie += 1
batut.append(intersection[1])
elif intersection[0]<=ls_sorted[i][0]<=intersection[1]:
intersection[0] = ls_sorted[i][0]
if i == n-1:
cuie += 1
batut.append(intersection[1])
elif intersection[0]<=ls_sorted[i][1]<=intersection[1]:
intersection[1]=ls_sorted[i][1]
if i == n-1:
cuie += 1
batut.append(intersection[1])
else:
batut.append(intersection[1])
intersection = ls_sorted[i]
if i == n-1:
batut.append(intersection[1])
print(int(cuie))
print(*batut)
else:
print(1)
print(intersection[1])
| 40 | 93 | 3,174,400 |
195774981
|
intervals = []
n = int(input())
for x in range(n):
a,b = [int(x) for x in input().split()]
if a > b:
intervals.append([b,a])
else:
intervals.append([a,b])
intervals.sort()
st = intervals[0][0]
ed = intervals[0][1]
ans = []
for x in range(1, n):
if intervals[x][0] <= ed:
st = intervals[x][0]
ed = min(intervals[x][1], ed)
elif intervals[x][0] > ed:
ans.append(st)
st = intervals[x][0]
ed = intervals[x][1]
ans.append(st)
print(len(ans))
print(*ans)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010 | 1 | 256 |
Segments
|
You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
|
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
|
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
| null | null |
[{"input": "2\n0 2\n2 5", "output": "1\n2"}, {"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n7 10 3"}]
| 1,900 |
["greedy", "sortings"]
| 40 |
[{"input": "2\r\n0 2\r\n2 5\r\n", "output": "1\r\n2 "}, {"input": "5\r\n0 3\r\n4 2\r\n4 8\r\n8 10\r\n7 7\r\n", "output": "3\r\n3 7 10 "}, {"input": "3\r\n40 -83\r\n52 -80\r\n-21 -4\r\n", "output": "1\r\n-4 "}, {"input": "4\r\n67 -88\r\n37 -62\r\n-26 91\r\n-99 -50\r\n", "output": "2\r\n-50 91 "}, {"input": "5\r\n45 58\r\n22 6\r\n-32 36\r\n49 -37\r\n43 62\r\n", "output": "2\r\n22 58 "}, {"input": "10\r\n47 -85\r\n71 40\r\n-87 64\r\n76 73\r\n52 -51\r\n-20 -57\r\n-14 -77\r\n-45 -11\r\n-56 -48\r\n81 20\r\n", "output": "4\r\n-48 -11 71 76 "}, {"input": "1\r\n0 0\r\n", "output": "1\r\n0 "}, {"input": "4\r\n1 1\r\n0 0\r\n10000 10000\r\n-10000 -10000\r\n", "output": "4\r\n-10000 0 1 10000 "}, {"input": "3\r\n55 55\r\n55 55\r\n55 55\r\n", "output": "1\r\n55 "}, {"input": "4\r\n55 55\r\n55 55\r\n55 55\r\n55 56\r\n", "output": "1\r\n55 "}, {"input": "1\r\n-2244 5023\r\n", "output": "1\r\n5023 "}]
| false |
stdio
| null | true |
898/C
|
898
|
C
|
PyPy 3
|
TESTS
| 6 | 156 | 1,331,200 |
81941643
|
d={}
for _ in range(int(input())):
v=list(map(str,input().split()))
ss=v[0]
d[ss]=d.get(ss,set())
d[ss]|=set(v[2:])
print(len(d))
for ss in d:
l2=list(d[ss])
for i in range(len(l2)):
for j in range(len(l2)):
s=str(l2[j])
if len(l2[j])>len(l2[i]) and s[-len(l2[i]):]==l2[i]:
l2[i]="-1"
# print(len(l2))
l2=list(set(l2))
if "-1" in l2:
l2.remove("-1")
l3=[ss,len(l2)]+l2
print(*l3)
| 59 | 62 | 0 |
177467140
|
""" Prositka
10.10.2022"""
n=int(input())
d=dict()
for i in range(n):
a=input().split()
if a[0] not in d:
d[a[0]]=[]
for j in range(2, len(a)):
a[j]=a[j][::-1]
d[a[0]]+=a[2:]
print(len(d))
for x in d:
a=d[x]
a.sort(reverse=True)
b=[a[0]]
for i in range(1, len(a)):
if a[i-1][:len(a[i])]!=a[i]:
b+=[a[i]]
for i in range(len(b)):
b[i]=b[i][::-1]
print(x,len(b),*b)
|
Codeforces Round 451 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Phone Numbers
|
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
|
First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
|
Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
| null | null |
[{"input": "2\nivan 1 00123\nmasha 1 00123", "output": "2\nmasha 1 00123\nivan 1 00123"}, {"input": "3\nkarl 2 612 12\npetr 1 12\nkatya 1 612", "output": "3\nkatya 1 612\npetr 1 12\nkarl 1 612"}, {"input": "4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789", "output": "2\ndasha 2 23 789\nivan 4 789 123 2 456"}]
| 1,400 |
["implementation", "strings"]
| 59 |
[{"input": "2\r\nivan 1 00123\r\nmasha 1 00123\r\n", "output": "2\r\nmasha 1 00123 \r\nivan 1 00123 \r\n"}, {"input": "3\r\nkarl 2 612 12\r\npetr 1 12\r\nkatya 1 612\r\n", "output": "3\r\nkatya 1 612 \r\npetr 1 12 \r\nkarl 1 612 \r\n"}, {"input": "4\r\nivan 3 123 123 456\r\nivan 2 456 456\r\nivan 8 789 3 23 6 56 9 89 2\r\ndasha 2 23 789\r\n", "output": "2\r\ndasha 2 789 23 \r\nivan 4 789 123 456 2 \r\n"}, {"input": "20\r\nnxj 6 7 6 6 7 7 7\r\nnxj 10 8 5 1 7 6 1 0 7 0 6\r\nnxj 2 6 5\r\nnxj 10 6 7 6 6 5 8 3 6 6 8\r\nnxj 10 6 1 7 6 7 1 8 7 8 6\r\nnxj 10 8 5 8 6 5 6 1 9 6 3\r\nnxj 10 8 1 6 4 8 0 4 6 0 1\r\nnxj 9 2 6 6 8 1 1 3 6 6\r\nnxj 10 8 9 0 9 1 3 2 3 2 3\r\nnxj 6 6 7 0 8 1 2\r\nnxj 7 7 7 8 1 3 6 9\r\nnxj 10 2 7 0 1 5 1 9 1 2 6\r\nnxj 6 9 6 9 6 3 7\r\nnxj 9 0 1 7 8 2 6 6 5 6\r\nnxj 4 0 2 3 7\r\nnxj 10 0 4 0 6 1 1 8 8 4 7\r\nnxj 8 4 6 2 6 6 1 2 7\r\nnxj 10 5 3 4 2 1 0 7 0 7 6\r\nnxj 10 9 6 0 6 1 6 2 1 9 6\r\nnxj 4 2 9 0 1\r\n", "output": "1\r\nnxj 10 0 3 2 1 4 7 8 5 9 6 \r\n"}, {"input": "20\r\nl 6 02 02 2 02 02 2\r\nl 8 8 8 8 2 62 13 31 3\r\ne 9 0 91 0 0 60 91 60 2 44\r\ne 9 69 2 1 44 2 91 66 1 70\r\nl 9 7 27 27 3 1 3 7 80 81\r\nl 9 2 1 13 7 2 10 02 3 92\r\ne 9 0 15 3 5 5 15 91 09 44\r\nl 7 2 50 4 5 98 31 98\r\nl 3 26 7 3\r\ne 6 7 5 0 62 65 91\r\nl 8 80 0 4 0 2 2 0 13\r\nl 9 19 13 02 2 1 4 19 26 02\r\nl 10 7 39 7 9 22 22 26 2 90 4\r\ne 7 65 2 36 0 34 57 9\r\ne 8 13 02 09 91 73 5 36 62\r\nl 9 75 0 10 8 76 7 82 8 34\r\nl 7 34 0 19 80 6 4 7\r\ne 5 4 2 5 7 2\r\ne 7 4 02 69 7 07 20 2\r\nl 4 8 2 1 63\r\n", "output": "2\r\ne 18 15 62 07 70 91 57 02 66 65 69 09 13 20 44 73 34 60 36 \r\nl 21 27 50 22 63 75 19 26 90 02 92 62 31 10 76 82 80 98 81 39 34 13 \r\n"}, {"input": "20\r\no 10 6 6 97 45 6 6 6 6 5 6\r\nl 8 5 5 5 19 59 5 8 5\r\nj 9 2 30 58 2 2 1 0 30 4\r\nc 10 1 1 7 51 7 7 51 1 1 1\r\no 9 7 97 87 70 2 19 2 14 6\r\ne 6 26 6 6 6 26 5\r\ng 9 3 3 3 3 3 78 69 8 9\r\nl 8 8 01 1 5 8 41 72 3\r\nz 10 1 2 2 2 9 1 9 1 6 7\r\ng 8 7 78 05 36 7 3 67 9\r\no 5 6 9 9 7 7\r\ne 10 30 2 1 1 2 5 04 0 6 6\r\ne 9 30 30 2 2 0 26 30 79 8\r\nt 10 2 2 9 29 7 7 7 9 2 9\r\nc 7 7 51 1 31 2 7 4\r\nc 9 83 1 6 78 94 74 54 8 32\r\ng 8 4 1 01 9 39 28 6 6\r\nt 7 9 2 01 4 4 9 58\r\nj 5 0 1 58 02 4\r\nw 10 80 0 91 91 06 91 9 9 27 7\r\n", "output": "9\r\nw 5 91 27 06 9 80 \r\nt 6 7 58 4 29 2 01 \r\ne 8 8 79 5 30 2 26 04 1 \r\nl 8 72 3 19 59 41 01 5 8 \r\nj 5 4 30 58 1 02 \r\nz 5 6 1 9 2 7 \r\ng 10 05 39 4 3 36 01 67 69 28 78 \r\no 8 14 87 97 6 19 70 45 2 \r\nc 10 83 51 31 54 74 32 7 94 78 6 \r\n"}, {"input": "1\r\negew 5 3 123 23 1234 134\r\n", "output": "1\r\negew 3 123 134 1234 \r\n"}]
| false |
stdio
|
import sys
from collections import defaultdict
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input and build friends data
friends = defaultdict(set)
with open(input_path, 'r') as f:
n = int(f.readline())
for _ in range(n):
parts = f.readline().strip().split()
name = parts[0]
numbers = parts[2:]
friends[name].update(numbers) # Add all numbers, duplicates in same line are handled via set
# Read submission output
submission_friends = set()
try:
with open(submission_path, 'r') as f:
lines = f.read().splitlines()
except:
print(0)
return
if not lines:
print(0)
return
# Check m line
try:
m_line = lines[0]
m = int(m_line)
except:
print(0)
return
if m != len(friends):
print(0)
return
submission_lines = lines[1:]
if len(submission_lines) != m:
print(0)
return
for line in submission_lines:
parts = line.strip().split()
if len(parts) < 2:
print(0)
return
name = parts[0]
if name not in friends:
print(0)
return
submission_friends.add(name)
try:
k = int(parts[1])
except:
print(0)
return
numbers = parts[2:]
if len(numbers) != k:
print(0)
return
# Check for duplicates in submission's numbers
if len(set(numbers)) != k:
print(0)
return
# Check all numbers are present in friends' merged set
merged_numbers = friends[name]
for num in numbers:
if num not in merged_numbers:
print(0)
return
# Check no two numbers in submission are suffix of each other
for i in range(len(numbers)):
for j in range(i+1, len(numbers)):
a = numbers[i]
b = numbers[j]
if (len(a) >= len(b) and a.endswith(b)) or (len(b) >= len(a) and b.endswith(a)):
print(0)
return
# Check every merged number is either in submission or is a suffix of some submission number
for merged_num in merged_numbers:
if merged_num in numbers:
continue
found = False
for num in numbers:
if len(merged_num) <= len(num) and num.endswith(merged_num):
found = True
break
if not found:
print(0)
return
# Check all friends are present in submission
if submission_friends != set(friends.keys()):
print(0)
return
# All checks passed
print(100)
if __name__ == "__main__":
main()
| true |
898/C
|
898
|
C
|
PyPy 3
|
TESTS
| 17 | 108 | 1,945,600 |
120272370
|
n = int(input())
d = {}
for i in range(n):
name, t, *ages = list(map(str, input().split()))
for i in ages:
if name not in d:
d[name] = [i]
else:
d[name] += [i]
k = {}
for i in d:
for j in range(1,len(d[i])):
for z in d[i]:
if(len(d[i][j]) < len(z)):
if(z[len(z) - len(d[i][j]):] == d[i][j]):
if i not in k:
k[i] = [d[i][j]]
else:
k[i] += [d[i][j]]
print(len(d))
for i in d:
print(i, end = ' ')
l = set()
for j in d[i]:
if i in k:
if j not in k[i]:
l.add(j)
else:
l.add(j)
print(len(l), end = ' ')
for z in l:
print(z, end = ' ')
print()
| 59 | 62 | 1,638,400 |
205044505
|
n = int(input())
my_dict = {}
for _ in range(n):
mix = input()
lis = []
name, nop, *lis = mix.split(" ")
if name in my_dict:
my_dict[name].extend(lis)
else:
my_dict[name] = lis
print(len(my_dict))
for name, lis in my_dict.items():
lis.sort(key=len)
i = 0
f = 0
while i < (len(lis) - 1):
f = 1
j = i + 1
while j < len(lis):
if lis[j].endswith(lis[i]):
f = 0
del lis[i]
break
j = j + 1
if (f):
i = i + 1
print(f"{name} {len(lis)}", end="")
for x in lis:
print(f" {x}", end="")
print()
|
Codeforces Round 451 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Phone Numbers
|
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
|
First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
|
Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
| null | null |
[{"input": "2\nivan 1 00123\nmasha 1 00123", "output": "2\nmasha 1 00123\nivan 1 00123"}, {"input": "3\nkarl 2 612 12\npetr 1 12\nkatya 1 612", "output": "3\nkatya 1 612\npetr 1 12\nkarl 1 612"}, {"input": "4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789", "output": "2\ndasha 2 23 789\nivan 4 789 123 2 456"}]
| 1,400 |
["implementation", "strings"]
| 59 |
[{"input": "2\r\nivan 1 00123\r\nmasha 1 00123\r\n", "output": "2\r\nmasha 1 00123 \r\nivan 1 00123 \r\n"}, {"input": "3\r\nkarl 2 612 12\r\npetr 1 12\r\nkatya 1 612\r\n", "output": "3\r\nkatya 1 612 \r\npetr 1 12 \r\nkarl 1 612 \r\n"}, {"input": "4\r\nivan 3 123 123 456\r\nivan 2 456 456\r\nivan 8 789 3 23 6 56 9 89 2\r\ndasha 2 23 789\r\n", "output": "2\r\ndasha 2 789 23 \r\nivan 4 789 123 456 2 \r\n"}, {"input": "20\r\nnxj 6 7 6 6 7 7 7\r\nnxj 10 8 5 1 7 6 1 0 7 0 6\r\nnxj 2 6 5\r\nnxj 10 6 7 6 6 5 8 3 6 6 8\r\nnxj 10 6 1 7 6 7 1 8 7 8 6\r\nnxj 10 8 5 8 6 5 6 1 9 6 3\r\nnxj 10 8 1 6 4 8 0 4 6 0 1\r\nnxj 9 2 6 6 8 1 1 3 6 6\r\nnxj 10 8 9 0 9 1 3 2 3 2 3\r\nnxj 6 6 7 0 8 1 2\r\nnxj 7 7 7 8 1 3 6 9\r\nnxj 10 2 7 0 1 5 1 9 1 2 6\r\nnxj 6 9 6 9 6 3 7\r\nnxj 9 0 1 7 8 2 6 6 5 6\r\nnxj 4 0 2 3 7\r\nnxj 10 0 4 0 6 1 1 8 8 4 7\r\nnxj 8 4 6 2 6 6 1 2 7\r\nnxj 10 5 3 4 2 1 0 7 0 7 6\r\nnxj 10 9 6 0 6 1 6 2 1 9 6\r\nnxj 4 2 9 0 1\r\n", "output": "1\r\nnxj 10 0 3 2 1 4 7 8 5 9 6 \r\n"}, {"input": "20\r\nl 6 02 02 2 02 02 2\r\nl 8 8 8 8 2 62 13 31 3\r\ne 9 0 91 0 0 60 91 60 2 44\r\ne 9 69 2 1 44 2 91 66 1 70\r\nl 9 7 27 27 3 1 3 7 80 81\r\nl 9 2 1 13 7 2 10 02 3 92\r\ne 9 0 15 3 5 5 15 91 09 44\r\nl 7 2 50 4 5 98 31 98\r\nl 3 26 7 3\r\ne 6 7 5 0 62 65 91\r\nl 8 80 0 4 0 2 2 0 13\r\nl 9 19 13 02 2 1 4 19 26 02\r\nl 10 7 39 7 9 22 22 26 2 90 4\r\ne 7 65 2 36 0 34 57 9\r\ne 8 13 02 09 91 73 5 36 62\r\nl 9 75 0 10 8 76 7 82 8 34\r\nl 7 34 0 19 80 6 4 7\r\ne 5 4 2 5 7 2\r\ne 7 4 02 69 7 07 20 2\r\nl 4 8 2 1 63\r\n", "output": "2\r\ne 18 15 62 07 70 91 57 02 66 65 69 09 13 20 44 73 34 60 36 \r\nl 21 27 50 22 63 75 19 26 90 02 92 62 31 10 76 82 80 98 81 39 34 13 \r\n"}, {"input": "20\r\no 10 6 6 97 45 6 6 6 6 5 6\r\nl 8 5 5 5 19 59 5 8 5\r\nj 9 2 30 58 2 2 1 0 30 4\r\nc 10 1 1 7 51 7 7 51 1 1 1\r\no 9 7 97 87 70 2 19 2 14 6\r\ne 6 26 6 6 6 26 5\r\ng 9 3 3 3 3 3 78 69 8 9\r\nl 8 8 01 1 5 8 41 72 3\r\nz 10 1 2 2 2 9 1 9 1 6 7\r\ng 8 7 78 05 36 7 3 67 9\r\no 5 6 9 9 7 7\r\ne 10 30 2 1 1 2 5 04 0 6 6\r\ne 9 30 30 2 2 0 26 30 79 8\r\nt 10 2 2 9 29 7 7 7 9 2 9\r\nc 7 7 51 1 31 2 7 4\r\nc 9 83 1 6 78 94 74 54 8 32\r\ng 8 4 1 01 9 39 28 6 6\r\nt 7 9 2 01 4 4 9 58\r\nj 5 0 1 58 02 4\r\nw 10 80 0 91 91 06 91 9 9 27 7\r\n", "output": "9\r\nw 5 91 27 06 9 80 \r\nt 6 7 58 4 29 2 01 \r\ne 8 8 79 5 30 2 26 04 1 \r\nl 8 72 3 19 59 41 01 5 8 \r\nj 5 4 30 58 1 02 \r\nz 5 6 1 9 2 7 \r\ng 10 05 39 4 3 36 01 67 69 28 78 \r\no 8 14 87 97 6 19 70 45 2 \r\nc 10 83 51 31 54 74 32 7 94 78 6 \r\n"}, {"input": "1\r\negew 5 3 123 23 1234 134\r\n", "output": "1\r\negew 3 123 134 1234 \r\n"}]
| false |
stdio
|
import sys
from collections import defaultdict
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input and build friends data
friends = defaultdict(set)
with open(input_path, 'r') as f:
n = int(f.readline())
for _ in range(n):
parts = f.readline().strip().split()
name = parts[0]
numbers = parts[2:]
friends[name].update(numbers) # Add all numbers, duplicates in same line are handled via set
# Read submission output
submission_friends = set()
try:
with open(submission_path, 'r') as f:
lines = f.read().splitlines()
except:
print(0)
return
if not lines:
print(0)
return
# Check m line
try:
m_line = lines[0]
m = int(m_line)
except:
print(0)
return
if m != len(friends):
print(0)
return
submission_lines = lines[1:]
if len(submission_lines) != m:
print(0)
return
for line in submission_lines:
parts = line.strip().split()
if len(parts) < 2:
print(0)
return
name = parts[0]
if name not in friends:
print(0)
return
submission_friends.add(name)
try:
k = int(parts[1])
except:
print(0)
return
numbers = parts[2:]
if len(numbers) != k:
print(0)
return
# Check for duplicates in submission's numbers
if len(set(numbers)) != k:
print(0)
return
# Check all numbers are present in friends' merged set
merged_numbers = friends[name]
for num in numbers:
if num not in merged_numbers:
print(0)
return
# Check no two numbers in submission are suffix of each other
for i in range(len(numbers)):
for j in range(i+1, len(numbers)):
a = numbers[i]
b = numbers[j]
if (len(a) >= len(b) and a.endswith(b)) or (len(b) >= len(a) and b.endswith(a)):
print(0)
return
# Check every merged number is either in submission or is a suffix of some submission number
for merged_num in merged_numbers:
if merged_num in numbers:
continue
found = False
for num in numbers:
if len(merged_num) <= len(num) and num.endswith(merged_num):
found = True
break
if not found:
print(0)
return
# Check all friends are present in submission
if submission_friends != set(friends.keys()):
print(0)
return
# All checks passed
print(100)
if __name__ == "__main__":
main()
| true |
261/A
|
261
|
A
|
Python 3
|
TESTS
| 3 | 218 | 0 |
56447570
|
class CodeforcesTask261ASolution:
def __init__(self):
self.result = ''
self.discounts = []
self.items_count = 0
self.prices = []
def read_input(self):
input()
self.discounts = [int(x) for x in input().split(" ")]
self.items_count = int(input())
self.prices = [int(x) for x in input().split(" ")]
def process_task(self):
self.discounts.sort()
self.prices.sort(reverse=True)
price = 0
discount = self.discounts[0]
disc_mode = False
disc_prog = 0
while self.prices:
disc_prog += 1
if disc_prog > discount:
disc_prog = -2
disc_mode = True
if disc_prog >= 0:
disc_mode = False
if not disc_mode:
price += self.prices[0]
del self.prices[0]
self.result = str(price)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask261ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
| 45 | 248 | 19,046,400 |
180432352
|
m = int(input())
q = list(map(int, input().split()))
n = int(input())
l = sorted(list(map(int, input().split())))
cost, mini = 0, min(q)
while len(l)!=0:
for i in range(min(len(l), mini)): cost+=l.pop() #Buy Items
for i in range(min(len(l), 2)): l.pop() # get Items in discount
print(cost)
|
Codeforces Round 160 (Div. 1)
|
CF
| 2,013 | 2 | 256 |
Maxim and Discounts
|
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.
Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
|
The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).
The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices.
The numbers in the lines are separated by single spaces.
|
In a single line print a single integer — the answer to the problem.
| null |
In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.
|
[{"input": "1\n2\n4\n50 50 100 100", "output": "200"}, {"input": "2\n2 3\n5\n50 50 50 50 50", "output": "150"}, {"input": "1\n1\n7\n1 1 1 1 1 1 1", "output": "3"}]
| 1,400 |
["greedy", "sortings"]
| 45 |
[{"input": "1\r\n2\r\n4\r\n50 50 100 100\r\n", "output": "200\r\n"}, {"input": "2\r\n2 3\r\n5\r\n50 50 50 50 50\r\n", "output": "150\r\n"}, {"input": "1\r\n1\r\n7\r\n1 1 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "18\r\n16 16 20 12 13 10 14 15 4 5 6 8 4 11 12 11 16 7\r\n15\r\n371 2453 905 1366 6471 4331 4106 2570 4647 1648 7911 2147 1273 6437 3393\r\n", "output": "38578\r\n"}, {"input": "2\r\n12 4\r\n28\r\n5366 5346 1951 3303 1613 5826 8035 7079 7633 6155 9811 9761 3207 4293 3551 5245 7891 4463 3981 2216 3881 1751 4495 96 671 1393 1339 4241\r\n", "output": "89345\r\n"}, {"input": "57\r\n3 13 20 17 18 18 17 2 17 8 20 2 11 12 11 14 4 20 9 20 14 19 20 4 4 8 8 18 17 16 18 10 4 7 9 8 10 8 20 4 11 8 12 16 16 4 11 12 16 1 6 14 11 12 19 8 20\r\n7\r\n5267 7981 1697 826 6889 1949 2413\r\n", "output": "11220\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n3\r\n3 1 1\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
689/D
|
689
|
D
|
Python 3
|
TESTS
| 6 | 1,638 | 38,092,800 |
19186358
|
from bisect import bisect
HISENTINEL = 10**9 + 1
LOSENTINEL = -HISENTINEL
def main():
length = int(input())
a = [int(fld) for fld in input().strip().split()]
b = [int(fld) for fld in input().strip().split()]
print(countmaxminsubseq(a, b))
def countmaxminsubseq(a, b):
leq, lgt = getleftbounds(a, b)
req, rgt = getleftbounds(reversed(a), reversed(b))
req = reverseindex(req)
rgt = reverseindex(rgt)
count = 0
for i, (leq1, lgt1, req1, rgt1) in enumerate(zip(leq, lgt, req, rgt)):
count += (leq1 - lgt1)*(rgt1 - i) + (i - leq1)*(rgt1 - req1)
return count
def getleftbounds(a, b):
astack = [(HISENTINEL, -1)]
bstack = [(LOSENTINEL, -1)]
leqarr, lgtarr = [], []
for i, (aelt, belt) in enumerate(zip(a, b)):
while astack[-1][0] < aelt:
astack.pop()
lgt = astack[-1][1]
while bstack[-1][0] > belt:
bstack.pop()
if belt < aelt:
leq = lgt = i
elif belt == aelt:
leq = i
istack = bisect(bstack, (aelt, -2)) - 1
lgt = max(lgt, bstack[istack][1])
else:
istack = bisect(bstack, (aelt, i)) - 1
val, pos = bstack[istack]
if val < aelt:
lgt = leq = max(lgt, pos)
else:
leq = pos
istack = bisect(bstack, (aelt, -2)) - 1
val, pos = bstack[istack]
lgt = max(lgt, pos)
leq = max(leq, lgt)
leqarr.append(leq)
lgtarr.append(lgt)
astack.append((aelt, i))
bstack.append((belt, i))
return leqarr, lgtarr
def reverseindex(rind):
pivot = len(rind) - 1
return [pivot - i for i in reversed(rind)]
main()
| 94 | 780 | 29,798,400 |
159329328
|
import bisect
import copy
import gc
import itertools
from array import array
from fractions import Fraction
import heapq
import math
import operator
import os, sys
import profile
import cProfile
import random
import re
import string
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
from functools import reduce, lru_cache
from io import IOBase, BytesIO
from itertools import count, groupby, accumulate, permutations, combinations_with_replacement, product
from math import gcd
from operator import xor, add
from typing import List
input = lambda: sys.stdin.readline().rstrip("\r\n")
# print = lambda d: sys.stdout.write(str(d)+"\n")
def read_int_list(): return list(map(int, input().split()))
def read_int_tuple(): return tuple(map(int, input().split()))
def read_int(): return int(input())
# endregion
if 'AW' in os.environ['COMPUTERNAME']:
f = open('inputs', 'r')
def input(): return f.readline().rstrip("\r\n")
def solve(n, a, b):
mx, mn = deque(), deque()
j = res = 0
for i, (x, y) in enumerate(zip(a, b)):
while mx and a[mx[-1]] <= x:
mx.pop()
mx.append(i)
while mn and b[mn[-1]] >= y:
mn.pop()
mn.append(i)
while j <= i and a[mx[0]] > b[mn[0]]:
j += 1
while mx and mx[0] < j: mx.popleft()
while mn and mn[0] < j: mn.popleft()
if mx and mn and a[mx[0]] == b[mn[0]]:
res += min(mx[0], mn[0]) - j + 1
return res
def main():
for _ in range(1):
n = read_int()
a = read_int_list()
b = read_int_list()
print(solve(n, a, b))
if __name__ == "__main__":
main()
# cProfile.run("main()")
|
Codeforces Round 361 (Div. 2)
|
CF
| 2,016 | 2 | 512 |
Friends and Subsequences
|
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?
Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of $$\max_{i=l}^{r} a_i$$ while !Mike can instantly tell the value of $$\min_{i=l} b_i$$.
Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs $$\max_{i=l}^{r}a_i = \min_{i=l}^{r}b_i$$ is satisfied.
How many occasions will the robot count?
|
The first line contains only integer n (1 ≤ n ≤ 200 000).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.
The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.
|
Print the only integer number — the number of occasions the robot will count, thus for how many pairs $$\max_{i=l}^{r}a_i = \min_{i=l}^{r}b_i$$ is satisfied.
| null |
The occasions in the first sample case are:
1.l = 4,r = 4 since max{2} = min{2}.
2.l = 4,r = 5 since max{2, 1} = min{2, 3}.
There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
|
[{"input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2", "output": "2"}, {"input": "3\n3 3 3\n1 1 1", "output": "0"}]
| 2,100 |
["binary search", "data structures"]
| 94 |
[{"input": "6\r\n1 2 3 2 1 4\r\n6 7 1 2 3 2\r\n", "output": "2\r\n"}, {"input": "3\r\n3 3 3\r\n1 1 1\r\n", "output": "0\r\n"}, {"input": "17\r\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388097 -959271262\r\n-417715348 -959271262 -959271262 714413739 -189641729 571055593 571055593 571055593 -417715348 -417715348 192457837 -745891378 536388097 571055593 -189641729 571055593 -670860364\r\n", "output": "1\r\n"}, {"input": "1\r\n509658558\r\n509658558\r\n", "output": "1\r\n"}, {"input": "1\r\n509658558\r\n-544591380\r\n", "output": "0\r\n"}, {"input": "3\r\n1 1 1\r\n2 2 2\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
545/E
|
545
|
E
|
PyPy 3-64
|
TESTS
| 7 | 2,261 | 139,059,200 |
228111453
|
from collections import deque
from heapq import *
mod = 1000000007
time = 0
def solve():
n,m = map(int,input().split())
adj = [[]for i in range(n+1)]
edges = []
for i in range(m):
a,b,c = map(int,input().split())
edges.append((a,b,c))
adj[a].append((b,c))
adj[b].append((a,c))
u = int(input())
pq = []
dist = [mod]*(n+1)
dist[u] = 0
heappush(pq,(0,u))
parent = [-1]*(n+1)
edg = [-1]*(n+1)
while pq:
d,s = heappop(pq)
for i,j in adj[s]:
if dist[i]>d+j:
dist[i] = d+j
parent[i] = s
edg[i] = j
heappush(pq,(dist[i],i))
elif dist[i]==d+j:
if edg[i]>j:
parent[i] = s
edg[i] = j
se = set()
visited = [False]*(n+1)
visited[u] = True
for i in range(1,n+1):
if not visited[i]:
s = i
while not visited[s]:
se.add((s,parent[s]))
visited[s] = True
s = parent[s]
c = 1
ans = []
res = 0
for i,j,k in edges:
if (i,j) in se or (j,i) in se:
ans.append(c)
res+=k
c+=1
print(res)
print(*ans)
t = 1
#t = int(input())
for i in range(t):
solve()
| 62 | 2,635 | 76,288,000 |
184261586
|
import heapq
n, m = map(int, input().split())
g = [[] for _ in range(n + 1)]
for i in range(1, m + 1):
u, v, w = map(int, input().split())
g[u].append((i, v, w))
g[v].append((i, u, w))
src = int(input())
pq = [(0, 0, src, -1)]
mk = [0] * (n + 1)
t = []
s = 0
while pq:
d, w, u, e = heapq.heappop(pq)
if mk[u]:
continue
mk[u] = 1
s += w
t.append(e)
for e, v, w in g[u]:
if not mk[v]:
heapq.heappush(pq, (d + w, w, v, e))
print(s)
print(*t[1:])
|
Codeforces Round 303 (Div. 2)
|
CF
| 2,015 | 3 | 256 |
Paths and Trees
|
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1 are the same.
You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.
|
The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.
Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.
|
In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them.
| null |
In the first sample there are two possible shortest path trees:
- with edges 1 – 3 and 2 – 3 (the total weight is 3);
- with edges 1 – 2 and 2 – 3 (the total weight is 2);
And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.
|
[{"input": "3 3\n1 2 1\n2 3 1\n1 3 2\n3", "output": "2\n1 2"}, {"input": "4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4", "output": "4\n2 3 4"}]
| 2,000 |
["graphs", "greedy", "shortest paths"]
| 62 |
[{"input": "3 3\r\n1 2 1\r\n2 3 1\r\n1 3 2\r\n3\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "4 4\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n4 1 2\r\n4\r\n", "output": "4\r\n2 3 4 \r\n"}, {"input": "4 5\r\n1 2 1\r\n1 3 1\r\n2 4 1\r\n3 4 1\r\n2 3 10\r\n1\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "6 8\r\n1 2 30\r\n1 3 20\r\n2 3 50\r\n4 2 100\r\n2 5 40\r\n3 5 10\r\n3 6 50\r\n5 6 60\r\n4\r\n", "output": "230\r\n1 4 5 6 7 \r\n"}, {"input": "1 0\r\n1\r\n", "output": "0\r\n\r\n"}, {"input": "2 1\r\n1 2 1000000000\r\n2\r\n", "output": "1000000000\r\n1 \r\n"}]
| false |
stdio
|
import sys
import heapq
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path, 'r') as f:
n, m = map(int, f.readline().split())
edges = []
adj = [[] for _ in range(n+1)]
for idx in range(m):
u, v, w = map(int, f.readline().split())
edges.append( (u, v, w) )
adj[u].append( (v, w) )
adj[v].append( (u, w) )
u = int(f.readline())
# Read submission output
with open(submission_path, 'r') as f:
sum_submission = int(f.readline().strip())
submission_edges = list(map(int, f.readline().split()))
# Check edge indices are valid
if len(submission_edges) != n-1:
print(0)
return
seen = set()
for idx in submission_edges:
if idx < 1 or idx > m or idx in seen:
print(0)
return
seen.add(idx)
# Check sum of edges matches submission's sum
sum_edges = sum(edges[idx-1][2] for idx in submission_edges)
if sum_edges != sum_submission:
print(0)
return
# Compute shortest distances using Dijkstra
INF = float('inf')
distance = [INF] * (n+1)
distance[u] = 0
heap = [(0, u)]
visited = [False] * (n+1)
while heap:
dist, node = heapq.heappop(heap)
if visited[node]:
continue
visited[node] = True
for neighbor, w in adj[node]:
if not visited[neighbor] and distance[neighbor] > dist + w:
distance[neighbor] = dist + w
heapq.heappush(heap, (distance[neighbor], neighbor))
# Compute minimal sum
min_edges = [INF] * (n+1)
for a, b, w in edges:
if distance[a] + w == distance[b] and w < min_edges[b]:
min_edges[b] = w
if distance[b] + w == distance[a] and w < min_edges[a]:
min_edges[a] = w
sum_min = sum(w for v, w in enumerate(min_edges) if v != u and v != 0)
if sum_min != sum_submission:
print(0)
return
# Check each submission edge is part of a shortest path
submission_edges_info = [edges[idx-1] for idx in submission_edges]
for a, b, w in submission_edges_info:
valid = (distance[a] + w == distance[b]) or (distance[b] + w == distance[a])
if not valid:
print(0)
return
# Check tree distances and connectivity
tree_adj = [[] for _ in range(n+1)]
for a, b, w in submission_edges_info:
tree_adj[a].append( (b, w) )
tree_adj[b].append( (a, w) )
visited = [False] * (n+1)
q = [u]
visited[u] = True
dist_tree = [INF] * (n+1)
dist_tree[u] = 0
while q:
current = q.pop(0)
for neighbor, w in tree_adj[current]:
if not visited[neighbor]:
dist_tree[neighbor] = dist_tree[current] + w
visited[neighbor] = True
q.append(neighbor)
if sum(visited[1:]) != n or any(dist_tree[v] != distance[v] for v in range(1, n+1)):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true |
545/E
|
545
|
E
|
Python 3
|
TESTS
| 7 | 2,058 | 100,966,400 |
198526083
|
import sys, heapq
input=sys.stdin.readline
print=sys.stdout.write
class C:
INF = 2**30
class E:
def __init__(self, v, w, idx):
self.v = v
self.w = w
self.idx = idx
def __lt__(self, other):
return self.w < other.w
def solve():
def dijkstra():
heap = []
vis = [False] * n
minCost = [C.INF] * n
heapq.heappush(heap, (0, E(origin, 0, -1)))
minCost[origin] = 0
while heap:
cost, e = heapq.heappop(heap)
u = e.v
if vis[u] or minCost[u] < cost:
continue
vis[u] = True
tree.append(e)
for e in g[u]:
if vis[e.v]:
continue
newCost = cost + e.w
if newCost <= minCost[e.v]:
minCost[e.v] = newCost
heapq.heappush(heap, (newCost, e))
tree = []
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(m):
u, v, w = map(int, input().split())
u -= 1
v -= 1
g[u].append(E(v, w, i+1))
g[v].append(E(u, w, i+1))
origin = int(input()) - 1
dijkstra()
totalCost = sum([e.w for e in tree])
print(f'{totalCost}\n')
for e in tree:
if e.idx == -1:
continue
print(f'{e.idx} ')
print('\n')
solve()
| 62 | 2,635 | 76,288,000 |
184261586
|
import heapq
n, m = map(int, input().split())
g = [[] for _ in range(n + 1)]
for i in range(1, m + 1):
u, v, w = map(int, input().split())
g[u].append((i, v, w))
g[v].append((i, u, w))
src = int(input())
pq = [(0, 0, src, -1)]
mk = [0] * (n + 1)
t = []
s = 0
while pq:
d, w, u, e = heapq.heappop(pq)
if mk[u]:
continue
mk[u] = 1
s += w
t.append(e)
for e, v, w in g[u]:
if not mk[v]:
heapq.heappush(pq, (d + w, w, v, e))
print(s)
print(*t[1:])
|
Codeforces Round 303 (Div. 2)
|
CF
| 2,015 | 3 | 256 |
Paths and Trees
|
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1 are the same.
You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.
|
The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.
Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.
|
In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them.
| null |
In the first sample there are two possible shortest path trees:
- with edges 1 – 3 and 2 – 3 (the total weight is 3);
- with edges 1 – 2 and 2 – 3 (the total weight is 2);
And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.
|
[{"input": "3 3\n1 2 1\n2 3 1\n1 3 2\n3", "output": "2\n1 2"}, {"input": "4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4", "output": "4\n2 3 4"}]
| 2,000 |
["graphs", "greedy", "shortest paths"]
| 62 |
[{"input": "3 3\r\n1 2 1\r\n2 3 1\r\n1 3 2\r\n3\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "4 4\r\n1 2 1\r\n2 3 1\r\n3 4 1\r\n4 1 2\r\n4\r\n", "output": "4\r\n2 3 4 \r\n"}, {"input": "4 5\r\n1 2 1\r\n1 3 1\r\n2 4 1\r\n3 4 1\r\n2 3 10\r\n1\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "6 8\r\n1 2 30\r\n1 3 20\r\n2 3 50\r\n4 2 100\r\n2 5 40\r\n3 5 10\r\n3 6 50\r\n5 6 60\r\n4\r\n", "output": "230\r\n1 4 5 6 7 \r\n"}, {"input": "1 0\r\n1\r\n", "output": "0\r\n\r\n"}, {"input": "2 1\r\n1 2 1000000000\r\n2\r\n", "output": "1000000000\r\n1 \r\n"}]
| false |
stdio
|
import sys
import heapq
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path, 'r') as f:
n, m = map(int, f.readline().split())
edges = []
adj = [[] for _ in range(n+1)]
for idx in range(m):
u, v, w = map(int, f.readline().split())
edges.append( (u, v, w) )
adj[u].append( (v, w) )
adj[v].append( (u, w) )
u = int(f.readline())
# Read submission output
with open(submission_path, 'r') as f:
sum_submission = int(f.readline().strip())
submission_edges = list(map(int, f.readline().split()))
# Check edge indices are valid
if len(submission_edges) != n-1:
print(0)
return
seen = set()
for idx in submission_edges:
if idx < 1 or idx > m or idx in seen:
print(0)
return
seen.add(idx)
# Check sum of edges matches submission's sum
sum_edges = sum(edges[idx-1][2] for idx in submission_edges)
if sum_edges != sum_submission:
print(0)
return
# Compute shortest distances using Dijkstra
INF = float('inf')
distance = [INF] * (n+1)
distance[u] = 0
heap = [(0, u)]
visited = [False] * (n+1)
while heap:
dist, node = heapq.heappop(heap)
if visited[node]:
continue
visited[node] = True
for neighbor, w in adj[node]:
if not visited[neighbor] and distance[neighbor] > dist + w:
distance[neighbor] = dist + w
heapq.heappush(heap, (distance[neighbor], neighbor))
# Compute minimal sum
min_edges = [INF] * (n+1)
for a, b, w in edges:
if distance[a] + w == distance[b] and w < min_edges[b]:
min_edges[b] = w
if distance[b] + w == distance[a] and w < min_edges[a]:
min_edges[a] = w
sum_min = sum(w for v, w in enumerate(min_edges) if v != u and v != 0)
if sum_min != sum_submission:
print(0)
return
# Check each submission edge is part of a shortest path
submission_edges_info = [edges[idx-1] for idx in submission_edges]
for a, b, w in submission_edges_info:
valid = (distance[a] + w == distance[b]) or (distance[b] + w == distance[a])
if not valid:
print(0)
return
# Check tree distances and connectivity
tree_adj = [[] for _ in range(n+1)]
for a, b, w in submission_edges_info:
tree_adj[a].append( (b, w) )
tree_adj[b].append( (a, w) )
visited = [False] * (n+1)
q = [u]
visited[u] = True
dist_tree = [INF] * (n+1)
dist_tree[u] = 0
while q:
current = q.pop(0)
for neighbor, w in tree_adj[current]:
if not visited[neighbor]:
dist_tree[neighbor] = dist_tree[current] + w
visited[neighbor] = True
q.append(neighbor)
if sum(visited[1:]) != n or any(dist_tree[v] != distance[v] for v in range(1, n+1)):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true |
629/C
|
629
|
C
|
Python 3
|
PRETESTS
| 3 | 31 | 0 |
16242807
|
n,m = map(int,input().split())
s = input()
if n % 2 == 0:
p = 0
q = 0
p2 = -10000000
for i in range(m):
if s[i] == "(":
p -= 1
q += 1
else:
p += 1
q -= 1
if p > p2:
p2 = p
num = 0
if p2 > 0:
num += p2
q += p2
if q > 0:
num += q
if m + num > n:
print(0)
elif m + num == n:
print(1)
else:
print(((n-num-m)//2)*(num+3))
else:
print(0)
| 60 | 171 | 39,014,400 |
174350635
|
import sys,os,io
input = sys.stdin.readline # for strings
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # for non-strings
from collections import defaultdict
import bisect
import random
INF = float('inf')
MOD = 1000000007
# MOD = 998244353
def bin32(num):
return '{0:032b}'.format(num)
def gcd(x,y):
if y == 0:
return x
return gcd(y,x%y)
def lcm(x,y):
return (x*y)//gcd(x,y)
def power(x,y):
res = 1
x%=MOD
while y!=0:
if y&1 :
res = (res*x)%MOD
y>>=1
x = (x*x)%MOD
return res
def mod_inv(n):
return power(n,MOD-2)
def prob(p,q):
return (p*power(q,MOD-2))%MOD
n , m = [int(i) for i in input().split()]
rem = n - m
s = input().strip()
mini = 0
stack = 0
for i in s:
if i == '(':stack+=1
else : stack-=1
mini = min(mini , stack)
result = 0
dp = [[0 for i in range(rem + 1)] for j in range(rem + 1)]
dp[0][0] = 1
for i in range(1 , rem + 1):
for j in range(i+1):
if j != 0:
dp[i][j] +=dp[i-1][j-1]
if j <= i - 2:
dp[i][j] += dp[i-1][j+1]
dp[i][j]%=MOD
for left in range(rem + 1):
right = rem - left
for i in range(left + 1):
if i + mini >= 0 and stack + i <= rem:
result += dp[left][i]*dp[right][stack + i]
result %= MOD
print(result)
# print(want , rem , stack)
# *rdp[right][want]
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
22/D
|
22
|
D
|
Python 3
|
TESTS
| 2 | 30 | 0 |
198716298
|
import heapq
n = int(input())
l = []
for _ in range(n):
linie = input()
start = int(linie.split()[0])
end = int(linie.split()[1])
if end < start:
start, end = end, start
l.append( (end, start) )
heapq.heapify(l)
cuie = []
nrcuie = 0
while l:
minim = heapq.heappop(l)[0]
cuie.append(minim)
nrcuie += 1
for seg in l:
if seg[0] >= minim and seg[1] <= minim:
l.remove(seg)
print(nrcuie)
print(*cuie)
| 40 | 93 | 3,174,400 |
219256708
|
n = int(input())
segments = []
for _ in range(n):
l, r = sorted(map(int, input().split()))
segments.append([l, r])
segments.sort(key = lambda s: s[1])
must_pined = False
pins = []
for l, r in segments:
if len(pins) == 0 or l > pins[-1]:
pins.append(r)
print(len(pins))
print(*pins)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010 | 1 | 256 |
Segments
|
You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
|
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
|
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
| null | null |
[{"input": "2\n0 2\n2 5", "output": "1\n2"}, {"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n7 10 3"}]
| 1,900 |
["greedy", "sortings"]
| 40 |
[{"input": "2\r\n0 2\r\n2 5\r\n", "output": "1\r\n2 "}, {"input": "5\r\n0 3\r\n4 2\r\n4 8\r\n8 10\r\n7 7\r\n", "output": "3\r\n3 7 10 "}, {"input": "3\r\n40 -83\r\n52 -80\r\n-21 -4\r\n", "output": "1\r\n-4 "}, {"input": "4\r\n67 -88\r\n37 -62\r\n-26 91\r\n-99 -50\r\n", "output": "2\r\n-50 91 "}, {"input": "5\r\n45 58\r\n22 6\r\n-32 36\r\n49 -37\r\n43 62\r\n", "output": "2\r\n22 58 "}, {"input": "10\r\n47 -85\r\n71 40\r\n-87 64\r\n76 73\r\n52 -51\r\n-20 -57\r\n-14 -77\r\n-45 -11\r\n-56 -48\r\n81 20\r\n", "output": "4\r\n-48 -11 71 76 "}, {"input": "1\r\n0 0\r\n", "output": "1\r\n0 "}, {"input": "4\r\n1 1\r\n0 0\r\n10000 10000\r\n-10000 -10000\r\n", "output": "4\r\n-10000 0 1 10000 "}, {"input": "3\r\n55 55\r\n55 55\r\n55 55\r\n", "output": "1\r\n55 "}, {"input": "4\r\n55 55\r\n55 55\r\n55 55\r\n55 56\r\n", "output": "1\r\n55 "}, {"input": "1\r\n-2244 5023\r\n", "output": "1\r\n5023 "}]
| false |
stdio
| null | true |
22/D
|
22
|
D
|
PyPy 3-64
|
TESTS
| 2 | 46 | 0 |
215623628
|
def find_smallest_number_of_nails(segments):
segments.sort(key=lambda x: x[1]) # Sort segments based on the right endpoint
nails = []
last_nail = -10001 # Initialize the last driven nail position
for segment in segments:
start, end = segment
if start > last_nail:
nails.append(end)
last_nail = end
return len(nails), nails
# Read input
n = int(input().strip())
segments = []
for _ in range(n):
a, b = map(int, input().strip().split())
segments.append((a, b))
# Output the result
num_nails, nails = find_smallest_number_of_nails(segments)
print(num_nails)
print(*nails)
| 40 | 108 | 2,969,600 |
176881828
|
array = []
n = int(input())
for x in range(n):
e1, e2 = map(int, input().split())
less = min(e1, e2)
great = max(e1, e2)
array.append([less, great])
array.sort()
points = [array[0][1]]
range = array[0]
for i in array:
if i[0] <= points[-1] and i[1] >= points[-1]:
continue
elif i[1] <= range[1]:
points.pop()
points.append(i[1])
range = i
else:
points.append(i[1])
range = i
print(len(points))
# print(array)
print(*points)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010 | 1 | 256 |
Segments
|
You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
|
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
|
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
| null | null |
[{"input": "2\n0 2\n2 5", "output": "1\n2"}, {"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n7 10 3"}]
| 1,900 |
["greedy", "sortings"]
| 40 |
[{"input": "2\r\n0 2\r\n2 5\r\n", "output": "1\r\n2 "}, {"input": "5\r\n0 3\r\n4 2\r\n4 8\r\n8 10\r\n7 7\r\n", "output": "3\r\n3 7 10 "}, {"input": "3\r\n40 -83\r\n52 -80\r\n-21 -4\r\n", "output": "1\r\n-4 "}, {"input": "4\r\n67 -88\r\n37 -62\r\n-26 91\r\n-99 -50\r\n", "output": "2\r\n-50 91 "}, {"input": "5\r\n45 58\r\n22 6\r\n-32 36\r\n49 -37\r\n43 62\r\n", "output": "2\r\n22 58 "}, {"input": "10\r\n47 -85\r\n71 40\r\n-87 64\r\n76 73\r\n52 -51\r\n-20 -57\r\n-14 -77\r\n-45 -11\r\n-56 -48\r\n81 20\r\n", "output": "4\r\n-48 -11 71 76 "}, {"input": "1\r\n0 0\r\n", "output": "1\r\n0 "}, {"input": "4\r\n1 1\r\n0 0\r\n10000 10000\r\n-10000 -10000\r\n", "output": "4\r\n-10000 0 1 10000 "}, {"input": "3\r\n55 55\r\n55 55\r\n55 55\r\n", "output": "1\r\n55 "}, {"input": "4\r\n55 55\r\n55 55\r\n55 55\r\n55 56\r\n", "output": "1\r\n55 "}, {"input": "1\r\n-2244 5023\r\n", "output": "1\r\n5023 "}]
| false |
stdio
| null | true |
22/D
|
22
|
D
|
PyPy 3
|
TESTS
| 2 | 93 | 307,200 |
203003997
|
from collections import deque
n = int(input())
segs = [tuple(map(int, input().split())) for _ in range(n)]
segs.sort(key=lambda x: x[1])
min_end = None
nails = 0
loc = deque()
for s in segs:
if min_end == None:
min_end = s[1]
elif s[0] > min_end:
loc.append(min_end)
min_end = s[1]
nails += 1
else:
min_end = min(s[1], min_end)
loc.append(min_end)
nails += 1
print(nails)
print(*list(loc))
| 40 | 109 | 307,200 |
74792082
|
n=int(input())
c=[]
for i in range(n):
x,y=map(int,input().split())
if x>y: x,y=y,x
c.append([x,0,i])
c.append([y,1,i])
c.sort()
ans=0
stack=[]
b=[]
for z in c:
if z[1]==0:
stack.append(z[2])
else:
if z[2] in stack:
ans+=1
b.append(z[0])
stack=[]
print(ans)
print(*b)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010 | 1 | 256 |
Segments
|
You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
|
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
|
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
| null | null |
[{"input": "2\n0 2\n2 5", "output": "1\n2"}, {"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n7 10 3"}]
| 1,900 |
["greedy", "sortings"]
| 40 |
[{"input": "2\r\n0 2\r\n2 5\r\n", "output": "1\r\n2 "}, {"input": "5\r\n0 3\r\n4 2\r\n4 8\r\n8 10\r\n7 7\r\n", "output": "3\r\n3 7 10 "}, {"input": "3\r\n40 -83\r\n52 -80\r\n-21 -4\r\n", "output": "1\r\n-4 "}, {"input": "4\r\n67 -88\r\n37 -62\r\n-26 91\r\n-99 -50\r\n", "output": "2\r\n-50 91 "}, {"input": "5\r\n45 58\r\n22 6\r\n-32 36\r\n49 -37\r\n43 62\r\n", "output": "2\r\n22 58 "}, {"input": "10\r\n47 -85\r\n71 40\r\n-87 64\r\n76 73\r\n52 -51\r\n-20 -57\r\n-14 -77\r\n-45 -11\r\n-56 -48\r\n81 20\r\n", "output": "4\r\n-48 -11 71 76 "}, {"input": "1\r\n0 0\r\n", "output": "1\r\n0 "}, {"input": "4\r\n1 1\r\n0 0\r\n10000 10000\r\n-10000 -10000\r\n", "output": "4\r\n-10000 0 1 10000 "}, {"input": "3\r\n55 55\r\n55 55\r\n55 55\r\n", "output": "1\r\n55 "}, {"input": "4\r\n55 55\r\n55 55\r\n55 55\r\n55 56\r\n", "output": "1\r\n55 "}, {"input": "1\r\n-2244 5023\r\n", "output": "1\r\n5023 "}]
| false |
stdio
| null | true |
257/D
|
257
|
D
|
Python 3
|
TESTS
| 2 | 186 | 0 |
62387955
|
if __name__ == "__main__":
n = int(input())
array = input()
print('+'*(n-1) + '-')
| 38 | 622 | 11,878,400 |
77732187
|
n=int(input())
a=list(map(int,input().split(' ')))
temp_sgn=1
sgns=[]
curr_sum=0
for i in range(n):
if(curr_sum>=a[n-i-1]):
sgns.append(1)
sgns.append(-1)
curr_sum-=a[n-i-1]
else:
sgns.append(-1)
sgns.append(1)
curr_sum-=a[n-i-1]
curr_sum*=-1
sgns.reverse()
ans=[]
for i in range(2*n):
if(i%2==0):
ans.append(temp_sgn*sgns[i])
else:
temp_sgn*=sgns[i]
for x in ans:
if(x==1):
print('+',end='')
else:
print('-',end='')
|
Codeforces Round 159 (Div. 2)
|
CF
| 2,013 | 2 | 256 |
Sum
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai ≤ ai + 1 ≤ 2·ai for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
|
The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the original array.
It is guaranteed that the condition ai ≤ ai + 1 ≤ 2·ai fulfills for any positive integer i (i < n).
|
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a1. If there are multiple solutions, you are allowed to print any of them.
| null | null |
[{"input": "4\n1 2 3 5", "output": "+++-"}, {"input": "3\n3 3 5", "output": "++-"}]
| 1,900 |
["greedy", "math"]
| 38 |
[{"input": "4\r\n1 2 3 5\r\n", "output": "+++-"}, {"input": "3\r\n3 3 5\r\n", "output": "++-"}, {"input": "4\r\n2 4 5 6\r\n", "output": "-++-"}, {"input": "6\r\n3 5 10 11 12 20\r\n", "output": "++-++-"}, {"input": "10\r\n10 14 17 22 43 72 74 84 88 93\r\n", "output": "++---++--+"}, {"input": "11\r\n3 6 7 11 13 16 26 52 63 97 97\r\n", "output": "++--+-++--+"}, {"input": "12\r\n3 3 4 7 14 26 51 65 72 72 85 92\r\n", "output": "+-+--++-+--+"}, {"input": "40\r\n3 3 3 6 10 10 18 19 34 66 107 150 191 286 346 661 1061 1620 2123 3679 5030 8736 10539 19659 38608 47853 53095 71391 135905 255214 384015 694921 1357571 1364832 2046644 2595866 2918203 3547173 4880025 6274651\r\n", "output": "+-++-+-+-+-++-++-+-++--++--++--+-+-+-++-"}, {"input": "41\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "-----------------------------------------"}, {"input": "42\r\n2 2 2 3 6 8 14 22 37 70 128 232 330 472 473 784 1481 2008 3076 4031 7504 8070 8167 11954 17832 24889 27113 41190 48727 92327 148544 186992 247329 370301 547840 621571 868209 1158781 1725242 3027208 4788036 5166155\r\n", "output": "-++-+-++--+-+-+-+-+-+-+-+--++-+-++--+--++-"}, {"input": "43\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "-------------------------------------------"}, {"input": "44\r\n4 6 8 14 28 36 43 76 78 151 184 217 228 245 469 686 932 1279 2100 2373 4006 4368 8173 10054 18409 28333 32174 53029 90283 161047 293191 479853 875055 1206876 1423386 1878171 2601579 3319570 4571631 4999760 6742654 12515994 22557290 29338426\r\n", "output": "+-+-+-+--++-+-+-++--+-+--+--+++--++--+-+-++-"}, {"input": "45\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "---------------------------------------------"}, {"input": "1\r\n1000000000\r\n", "output": "+"}, {"input": "2\r\n5 8\r\n", "output": "-+"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "++-"}]
| false |
stdio
|
import sys
def main(input_path, output_path, sub_path):
with open(input_path) as f:
lines = f.readlines()
n = int(lines[0].strip())
a = list(map(int, lines[1].strip().split()))
assert len(a) == n, "Input array length mismatch"
with open(sub_path) as f:
sub = f.read().strip()
if len(sub) != n:
print(0)
return
for c in sub:
if c not in '+-':
print(0)
return
total = 0
for i in range(n):
total += a[i] if sub[i] == '+' else -a[i]
if 0 <= total <= a[0]:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path, output_path, sub_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, sub_path)
| true |
257/D
|
257
|
D
|
Python 3
|
TESTS
| 2 | 92 | 0 |
206800882
|
def get_expression(n, arr):
expression = []
current_sum = 0
for i in range(n - 1, -1, -1):
if arr[i] >= current_sum:
expression.append('-')
else:
expression.append('+')
current_sum += arr[i]
expression.reverse()
return expression
n = int(input())
arr = list(map(int, input().split()))
result = get_expression(n, arr)
print(''.join(result))
| 38 | 248 | 9,728,000 |
5475356
|
n = int(input())
t = list(map(int, input().split()))
t.reverse()
s, p = 0, [0] * n
for i, j in enumerate(t):
if s > 0:
p[i] = 1
s -= j
else: s += j
p.reverse()
if s < 0: print(''.join('-+'[i] for i in p))
else: print(''.join('+-'[i] for i in p))
|
Codeforces Round 159 (Div. 2)
|
CF
| 2,013 | 2 | 256 |
Sum
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai ≤ ai + 1 ≤ 2·ai for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
|
The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the original array.
It is guaranteed that the condition ai ≤ ai + 1 ≤ 2·ai fulfills for any positive integer i (i < n).
|
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a1. If there are multiple solutions, you are allowed to print any of them.
| null | null |
[{"input": "4\n1 2 3 5", "output": "+++-"}, {"input": "3\n3 3 5", "output": "++-"}]
| 1,900 |
["greedy", "math"]
| 38 |
[{"input": "4\r\n1 2 3 5\r\n", "output": "+++-"}, {"input": "3\r\n3 3 5\r\n", "output": "++-"}, {"input": "4\r\n2 4 5 6\r\n", "output": "-++-"}, {"input": "6\r\n3 5 10 11 12 20\r\n", "output": "++-++-"}, {"input": "10\r\n10 14 17 22 43 72 74 84 88 93\r\n", "output": "++---++--+"}, {"input": "11\r\n3 6 7 11 13 16 26 52 63 97 97\r\n", "output": "++--+-++--+"}, {"input": "12\r\n3 3 4 7 14 26 51 65 72 72 85 92\r\n", "output": "+-+--++-+--+"}, {"input": "40\r\n3 3 3 6 10 10 18 19 34 66 107 150 191 286 346 661 1061 1620 2123 3679 5030 8736 10539 19659 38608 47853 53095 71391 135905 255214 384015 694921 1357571 1364832 2046644 2595866 2918203 3547173 4880025 6274651\r\n", "output": "+-++-+-+-+-++-++-+-++--++--++--+-+-+-++-"}, {"input": "41\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "-----------------------------------------"}, {"input": "42\r\n2 2 2 3 6 8 14 22 37 70 128 232 330 472 473 784 1481 2008 3076 4031 7504 8070 8167 11954 17832 24889 27113 41190 48727 92327 148544 186992 247329 370301 547840 621571 868209 1158781 1725242 3027208 4788036 5166155\r\n", "output": "-++-+-++--+-+-+-+-+-+-+-+--++-+-++--+--++-"}, {"input": "43\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "-------------------------------------------"}, {"input": "44\r\n4 6 8 14 28 36 43 76 78 151 184 217 228 245 469 686 932 1279 2100 2373 4006 4368 8173 10054 18409 28333 32174 53029 90283 161047 293191 479853 875055 1206876 1423386 1878171 2601579 3319570 4571631 4999760 6742654 12515994 22557290 29338426\r\n", "output": "+-+-+-+--++-+-+-++--+-+--+--+++--++--+-+-++-"}, {"input": "45\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "---------------------------------------------"}, {"input": "1\r\n1000000000\r\n", "output": "+"}, {"input": "2\r\n5 8\r\n", "output": "-+"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "++-"}]
| false |
stdio
|
import sys
def main(input_path, output_path, sub_path):
with open(input_path) as f:
lines = f.readlines()
n = int(lines[0].strip())
a = list(map(int, lines[1].strip().split()))
assert len(a) == n, "Input array length mismatch"
with open(sub_path) as f:
sub = f.read().strip()
if len(sub) != n:
print(0)
return
for c in sub:
if c not in '+-':
print(0)
return
total = 0
for i in range(n):
total += a[i] if sub[i] == '+' else -a[i]
if 0 <= total <= a[0]:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path, output_path, sub_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, sub_path)
| true |
261/A
|
261
|
A
|
Python 3
|
TESTS
| 3 | 186 | 307,200 |
90405130
|
m=int(input())
q=list(map(int,input().split()))
n=int(input())
a=list(map(int,input().split()))
q.sort()
a.sort()
j=n-1
i=0
ans=0
while j>=0:
if j==n-1:
for i in range(q[0]):
ans=ans+a[j]
j=j-1
if j<0:
break
j=j-2
else:
ans=ans+a[j]
j=j-2
print(ans)
| 45 | 278 | 7,372,800 |
177939891
|
rd = lambda: list(map(int, input().split()))
input()
z = min(rd())
input()
a = sorted(rd(), reverse=True)
print(sum(v for i, v in enumerate(a) if i%(z+2)<z))
|
Codeforces Round 160 (Div. 1)
|
CF
| 2,013 | 2 | 256 |
Maxim and Discounts
|
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.
Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
|
The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).
The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices.
The numbers in the lines are separated by single spaces.
|
In a single line print a single integer — the answer to the problem.
| null |
In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.
|
[{"input": "1\n2\n4\n50 50 100 100", "output": "200"}, {"input": "2\n2 3\n5\n50 50 50 50 50", "output": "150"}, {"input": "1\n1\n7\n1 1 1 1 1 1 1", "output": "3"}]
| 1,400 |
["greedy", "sortings"]
| 45 |
[{"input": "1\r\n2\r\n4\r\n50 50 100 100\r\n", "output": "200\r\n"}, {"input": "2\r\n2 3\r\n5\r\n50 50 50 50 50\r\n", "output": "150\r\n"}, {"input": "1\r\n1\r\n7\r\n1 1 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "18\r\n16 16 20 12 13 10 14 15 4 5 6 8 4 11 12 11 16 7\r\n15\r\n371 2453 905 1366 6471 4331 4106 2570 4647 1648 7911 2147 1273 6437 3393\r\n", "output": "38578\r\n"}, {"input": "2\r\n12 4\r\n28\r\n5366 5346 1951 3303 1613 5826 8035 7079 7633 6155 9811 9761 3207 4293 3551 5245 7891 4463 3981 2216 3881 1751 4495 96 671 1393 1339 4241\r\n", "output": "89345\r\n"}, {"input": "57\r\n3 13 20 17 18 18 17 2 17 8 20 2 11 12 11 14 4 20 9 20 14 19 20 4 4 8 8 18 17 16 18 10 4 7 9 8 10 8 20 4 11 8 12 16 16 4 11 12 16 1 6 14 11 12 19 8 20\r\n7\r\n5267 7981 1697 826 6889 1949 2413\r\n", "output": "11220\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n3\r\n3 1 1\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
261/A
|
261
|
A
|
Python 3
|
TESTS
| 3 | 124 | 204,800 |
21916773
|
import math
def sort(arr):
swap=True
i=0
while swap==True or i>len(arr):
swap=False
for j in range(len(arr)-i-1):
if arr[j]>arr[j+1]:
temp=arr[j]
arr[j]=arr[j+1]
arr[j+1]=temp
swap=True
i+=1
need=[]
items=[]
m=input()
need=input().split()
for i in range(len(need)):
need[i]=int(need[i])
n=eval(input())
items=input().split()
for i in range(len(items)):
items[i]=int(items[i])
need.sort()
items.sort()
start=0
total=0
while start!=n:
if math.trunc((n-start)/(need[0]+2))>0:
for j in range(need[0]):
total+=items[n-1-j]
n=n-int(need[0])
start=start+2
elif math.trunc((n-start)/(need[0]+1))>0:
for j in range(need[0]):
total+=items[n-1-j]
n=n-int(need[0])
start=start+1
else:
for j in range(n-start):
total+=items[n-1-j]
n=n-(n-start)
print(total)
| 45 | 278 | 142,233,600 |
187108306
|
from collections import defaultdict, Counter, deque
from math import inf
from functools import lru_cache
from heapq import heappop, heappush
import sys
#input=sys.stdin.readline
def solution():
# use the minimam one you will be
m = int(input())
ln = min(map(int, input().split()))
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
res = 0
while arr:
for _ in range(ln):
if arr:
res += arr.pop()
for _ in range(2):
if arr: arr.pop()
return print(res)
def main():
t = 1
#t = int(input())
for _ in range(t):
solution()
import sys
import threading
sys.setrecursionlimit(10**6)
threading.stack_size(1 << 27)
thread = threading.Thread(target=main)
thread.start(); thread.join()
#main()
|
Codeforces Round 160 (Div. 1)
|
CF
| 2,013 | 2 | 256 |
Maxim and Discounts
|
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.
Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
|
The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).
The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices.
The numbers in the lines are separated by single spaces.
|
In a single line print a single integer — the answer to the problem.
| null |
In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.
|
[{"input": "1\n2\n4\n50 50 100 100", "output": "200"}, {"input": "2\n2 3\n5\n50 50 50 50 50", "output": "150"}, {"input": "1\n1\n7\n1 1 1 1 1 1 1", "output": "3"}]
| 1,400 |
["greedy", "sortings"]
| 45 |
[{"input": "1\r\n2\r\n4\r\n50 50 100 100\r\n", "output": "200\r\n"}, {"input": "2\r\n2 3\r\n5\r\n50 50 50 50 50\r\n", "output": "150\r\n"}, {"input": "1\r\n1\r\n7\r\n1 1 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "18\r\n16 16 20 12 13 10 14 15 4 5 6 8 4 11 12 11 16 7\r\n15\r\n371 2453 905 1366 6471 4331 4106 2570 4647 1648 7911 2147 1273 6437 3393\r\n", "output": "38578\r\n"}, {"input": "2\r\n12 4\r\n28\r\n5366 5346 1951 3303 1613 5826 8035 7079 7633 6155 9811 9761 3207 4293 3551 5245 7891 4463 3981 2216 3881 1751 4495 96 671 1393 1339 4241\r\n", "output": "89345\r\n"}, {"input": "57\r\n3 13 20 17 18 18 17 2 17 8 20 2 11 12 11 14 4 20 9 20 14 19 20 4 4 8 8 18 17 16 18 10 4 7 9 8 10 8 20 4 11 8 12 16 16 4 11 12 16 1 6 14 11 12 19 8 20\r\n7\r\n5267 7981 1697 826 6889 1949 2413\r\n", "output": "11220\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n3\r\n3 1 1\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
690/B1
|
690
|
B1
|
Python 3
|
TESTS
| 2 | 77 | 7,065,600 |
37352791
|
n = int(input())
big = 0
while(n):
row = input()
for i in row:
big = max(big, int(i))
n-=1
print('Yes')
| 21 | 62 | 0 |
19580571
|
def check_cell(i, j, n):
k = 0
if i > 1 and field[i-1][j]: k += 1
if i < n-1 and field[i+1][j]: k += 1
if j > 1 and field[i][j-1]: k += 1
if j < n-1 and field[i][j+1]: k += 1
if k < 2:
return 0
elif k < 4:
return k-1
else:
return 4
n = int(input())
field = []
for i in range(n):
temp = [int(x) for x in input()]
field.append(temp)
def main():
for i in range(n):
for j in range(n):
if field[i][j] and not field[i][j] == check_cell(i, j, n):
print('No')
return 0
print('Yes')
main()
|
Helvetic Coding Contest 2016 online mirror (teams, unrated)
|
ICPC
| 2,016 | 2 | 256 |
Recover Polygon (easy)
|
The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.
Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle.
As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides).
|
The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4.
Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates (1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1).
|
The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.
| null |
The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the input.
|
[{"input": "6\n000000\n000000\n012100\n024200\n012100\n000000", "output": "Yes"}]
| 1,700 |
[]
| 21 |
[{"input": "6\r\n000000\r\n000000\r\n012100\r\n024200\r\n012100\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n012210\r\n024420\r\n012210\r\n000000\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000100\r\n001210\r\n002420\r\n001210\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "10\r\n0000000000\r\n0122210000\r\n0244420100\r\n0122210000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n", "output": "No\r\n"}, {"input": "10\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0012100000\r\n0024200000\r\n0012100000\r\n0000000000\r\n", "output": "Yes\r\n"}, {"input": "9\r\n000000000\r\n000000000\r\n012221000\r\n024442000\r\n012221000\r\n000000000\r\n000000000\r\n000000010\r\n000000000\r\n", "output": "No\r\n"}, {"input": "9\r\n000000000\r\n012222100\r\n024444200\r\n024444200\r\n024444200\r\n024444200\r\n024444200\r\n012222100\r\n000000000\r\n", "output": "Yes\r\n"}, {"input": "8\r\n00000000\r\n00001210\r\n00002420\r\n00002020\r\n00001210\r\n00000000\r\n00000000\r\n00000000\r\n", "output": "No\r\n"}, {"input": "8\r\n00000000\r\n00000000\r\n01210000\r\n02420000\r\n01210000\r\n00000000\r\n00000000\r\n00000000\r\n", "output": "Yes\r\n"}, {"input": "7\r\n0000000\r\n0000000\r\n0000000\r\n1122210\r\n0244420\r\n0122210\r\n0000000\r\n", "output": "No\r\n"}, {"input": "7\r\n0000000\r\n0012210\r\n0024420\r\n0012210\r\n0000000\r\n0000000\r\n0000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n000000\r\n001100\r\n001200\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n000000\r\n002200\r\n002200\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n000000\r\n003300\r\n003300\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n001100\r\n013310\r\n013310\r\n001100\r\n000000\r\n", "output": "No\r\n"}]
| false |
stdio
| null | true |
261/A
|
261
|
A
|
PyPy 3-64
|
TESTS
| 3 | 92 | 0 |
180424082
|
m = int(input())
l1 = list(map(int, input().split()))
l1 = sorted(l1, reverse = True)
n = int(input())
l = list(map(int, input().split()))
l = sorted(l, reverse = True)
basket = 0
i = 0
pickedup = 0
cost = 0
while pickedup <n:
if n-(i+l1[basket]) <2 and basket+1<m:
while basket+1<m and n-(i+l1[basket])<2:
basket+=1
inbasket = 0
# print("start =", i)
while i<n and inbasket < l1[basket]:
cost+=l[i]
# print("buying item", i, l[i])
i+=1
inbasket+=1
pickedup+=1
# print(pickedup, i)
discount_items = 0
while i<n and discount_items <2:
# print("discount item", i, l[i])
i+=1
discount_items += 1
pickedup += 1
# print(pickedup, i)
print(cost)
| 45 | 310 | 7,680,000 |
21184899
|
def main():
input()
q = min(map(int, input().split()))
input()
aa = sorted(map(int, input().split()), reverse=True)
print(sum(aa) - sum(aa[q::q + 2]) - sum(aa[q + 1::q + 2]))
if __name__ == "__main__":
main()
|
Codeforces Round 160 (Div. 1)
|
CF
| 2,013 | 2 | 256 |
Maxim and Discounts
|
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.
Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
|
The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).
The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices.
The numbers in the lines are separated by single spaces.
|
In a single line print a single integer — the answer to the problem.
| null |
In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.
|
[{"input": "1\n2\n4\n50 50 100 100", "output": "200"}, {"input": "2\n2 3\n5\n50 50 50 50 50", "output": "150"}, {"input": "1\n1\n7\n1 1 1 1 1 1 1", "output": "3"}]
| 1,400 |
["greedy", "sortings"]
| 45 |
[{"input": "1\r\n2\r\n4\r\n50 50 100 100\r\n", "output": "200\r\n"}, {"input": "2\r\n2 3\r\n5\r\n50 50 50 50 50\r\n", "output": "150\r\n"}, {"input": "1\r\n1\r\n7\r\n1 1 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "18\r\n16 16 20 12 13 10 14 15 4 5 6 8 4 11 12 11 16 7\r\n15\r\n371 2453 905 1366 6471 4331 4106 2570 4647 1648 7911 2147 1273 6437 3393\r\n", "output": "38578\r\n"}, {"input": "2\r\n12 4\r\n28\r\n5366 5346 1951 3303 1613 5826 8035 7079 7633 6155 9811 9761 3207 4293 3551 5245 7891 4463 3981 2216 3881 1751 4495 96 671 1393 1339 4241\r\n", "output": "89345\r\n"}, {"input": "57\r\n3 13 20 17 18 18 17 2 17 8 20 2 11 12 11 14 4 20 9 20 14 19 20 4 4 8 8 18 17 16 18 10 4 7 9 8 10 8 20 4 11 8 12 16 16 4 11 12 16 1 6 14 11 12 19 8 20\r\n7\r\n5267 7981 1697 826 6889 1949 2413\r\n", "output": "11220\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n3\r\n3 1 1\r\n", "output": "3\r\n"}]
| false |
stdio
| null | true |
175/B
|
175
|
B
|
Python 3
|
TESTS
| 11 | 92 | 102,400 |
214309783
|
def f(temp):
if (k-temp)/k < 0.5:
return "noob"
elif 0.8>(k-temp)/k >= 0.5:
return "random"
elif 0.9>(k-temp)/k >= 0.8:
return "average"
elif 0.99>(k-temp)/k >= 0.9:
return "hardcore"
elif (k-temp)/k >= 0.99:
return "pro"
n = int(input())
hashtable = {}
for i in range(n):
s,point = input().split()
point = int(point)
if point > hashtable.get(s,0):
hashtable[s] = point
k = len(hashtable)
# print(hashtable)
print(k)
lpoint = []
for i,key in enumerate(hashtable):
lpoint.append([key,hashtable[key]])
lpoint.sort(key=lambda x:-x[1])
# print(lpoint)
temp = -1
last = 0
same = 0
for i in range(k):
if lpoint[i][1] == last:
same += 1
else:
temp += same + 1
same = 0
rank = f(temp)
# print(temp,k)
print(lpoint[i][0],rank)
last = lpoint[i][1]
| 46 | 184 | 409,600 |
14904733
|
def t(g, n):
for x, y in ((50, 'noob'), (20, 'random'), (10, 'average'), (1, 'hardcore')):
if g > x * n // 100:
return y
return 'pro'
p, st = {}, {}
for i in range(int(input())):
n, s = input().split()
if n not in p or int(s) > p[n]:
p[n] = int(s)
for i, si in enumerate(sorted(p.values(), reverse=True)):
if si not in st:
st[si] = t(i, len(p))
print(len(p))
print(*sorted(k + ' ' + st[p[k]] for k in p), sep='\n')
|
Codeforces Round 115
|
CF
| 2,012 | 2 | 256 |
Plane of Tanks: Pro
|
Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results.
A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He has n records in total.
In order to determine a player's category consider the best result obtained by the player and the best results of other players. The player belongs to category:
- "noob" — if more than 50% of players have better results;
- "random" — if his result is not worse than the result that 50% of players have, but more than 20% of players have better results;
- "average" — if his result is not worse than the result that 80% of players have, but more than 10% of players have better results;
- "hardcore" — if his result is not worse than the result that 90% of players have, but more than 1% of players have better results;
- "pro" — if his result is not worse than the result that 99% of players have.
When the percentage is calculated the player himself is taken into account. That means that if two players played the game and the first one gained 100 points and the second one 1000 points, then the first player's result is not worse than the result that 50% of players have, and the second one is not worse than the result that 100% of players have.
Vasya gave you the last year Plane of Tanks results. Help Vasya determine each player's category.
|
The first line contains the only integer number n (1 ≤ n ≤ 1000) — a number of records with the players' results.
Each of the next n lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 characters. The name consists of lowercase Latin letters only. It is guaranteed that any two different players have different names. The amount of points, obtained by the player for the round, is a non-negative integer number and does not exceed 1000.
|
Print on the first line the number m — the number of players, who participated in one round at least.
Each one of the next m lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: "noob", "random", "average", "hardcore" or "pro" (without quotes). The name of each player should be printed only once. Player names with respective categories can be printed in an arbitrary order.
| null |
In the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category "noob". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category "random". igor has best result not worse than the result that 100% of players have (all other players and himself), so he belongs to category "pro".
In the second example both players have the same amount of points, so they have results not worse than 100% players have, so they belong to category "pro".
|
[{"input": "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250", "output": "4\nartem noob\nigor pro\nkolya random\nvasya random"}, {"input": "3\nvasya 200\nkolya 1000\nvasya 1000", "output": "2\nkolya pro\nvasya pro"}]
| 1,400 |
["implementation"]
| 78 |
[{"input": "5\r\nvasya 100\r\nvasya 200\r\nartem 100\r\nkolya 200\r\nigor 250\r\n", "output": "4\r\nartem noob\r\nigor pro\r\nkolya random\r\nvasya random\r\n"}, {"input": "3\r\nvasya 200\r\nkolya 1000\r\nvasya 1000\r\n", "output": "2\r\nkolya pro\r\nvasya pro\r\n"}, {"input": "1\r\nvasya 1000\r\n", "output": "1\r\nvasya pro\r\n"}, {"input": "5\r\nvasya 1000\r\nvasya 100\r\nkolya 200\r\npetya 300\r\noleg 400\r\n", "output": "4\r\nkolya noob\r\noleg random\r\npetya random\r\nvasya pro\r\n"}, {"input": "10\r\na 1\r\nb 2\r\nc 3\r\nd 4\r\ne 5\r\nf 6\r\ng 7\r\nh 8\r\ni 9\r\nj 10\r\n", "output": "10\r\na noob\r\nb noob\r\nc noob\r\nd noob\r\ne random\r\nf random\r\ng random\r\nh average\r\ni hardcore\r\nj pro\r\n"}, {"input": "10\r\nj 10\r\ni 9\r\nh 8\r\ng 7\r\nf 6\r\ne 5\r\nd 4\r\nc 3\r\nb 2\r\na 1\r\n", "output": "10\r\na noob\r\nb noob\r\nc noob\r\nd noob\r\ne random\r\nf random\r\ng random\r\nh average\r\ni hardcore\r\nj pro\r\n"}, {"input": "1\r\ntest 0\r\n", "output": "1\r\ntest pro\r\n"}]
| false |
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input data
with open(input_path) as f:
n = int(f.readline())
players = {}
for _ in range(n):
name, points = f.readline().strip().split()
points = int(points)
if name not in players or points > players[name]:
players[name] = points
m = len(players)
best_scores = list(players.values())
# Compute correct categories
correct = {}
for name in players:
score = players[name]
cnt = sum(1 for s in best_scores if s > score)
percentage = (cnt * 100.0) / m
if percentage > 50:
cat = 'noob'
elif percentage > 20:
cat = 'random'
elif percentage > 10:
cat = 'average'
elif percentage > 1:
cat = 'hardcore'
else:
cat = 'pro'
correct[name] = cat
# Read submission output
with open(submission_path) as f:
lines = f.read().splitlines()
if not lines:
print(0)
return
try:
m_sub = int(lines[0])
except:
print(0)
return
if m_sub != m:
print(0)
return
submission = {}
for line in lines[1:]:
parts = line.strip().split()
if len(parts) != 2:
print(0)
return
name, cat = parts
if name in submission:
print(0)
return
submission[name] = cat
# Check all players are present and correct
if submission.keys() != correct.keys():
print(0)
return
for name, cat in submission.items():
if correct.get(name) != cat:
print(0)
return
print(1)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true |
629/C
|
629
|
C
|
PyPy 3
|
TESTS
| 3 | 217 | 18,636,800 |
43877512
|
from sys import stdin
n,m=map(int, stdin.readline().strip().split())
s=stdin.readline().strip()
dp=[[0 for i in range(2004)] for j in range(2004)]
mod=10**9+7
d=n-m
ans=0
b=0
d1=0
for i in s:
if i=='(':
b+=1
else:
b-=1
if b<d1:
d1=b
d1=-d1
dp[0][0]=1
d+=1
for i in range(1,d):
dp[i][0]=(dp[i-1][1]%mod)
for j in range(1,d):
dp[i][j]=(dp[i-1][j-1]%mod+dp[i-1][j+1]%mod)%mod
for i in range(d):
x=n-(i+m)
for j in range(d1,d):
ans+=dp[i][j]*dp[x][b+j]
print(ans)
| 60 | 186 | 19,558,400 |
199862678
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def f(u, v):
return u * (l + 1) + v
n, m = map(int, input().split())
mod = pow(10, 9) + 7
s = list(input().rstrip())
l = n - m
dp = [0] * ((l + 1) * (l + 1))
dp[0] = 1
for i in range(l):
for j in range(i % 2, i + 1, 2):
for k in [j - 1, j + 1]:
if 0 <= k <= l:
dp[f(i + 1, k)] = (dp[f(i, j)] + dp[f(i + 1, k)]) % mod
mi, k = 0, 0
for i in s:
k += 1 if not i & 1 else -1
mi = min(mi, k)
ans = 0
for i in range(l + 1):
for j in range(i % 2, i + 1, 2):
if j + mi < 0 or l - i < j + k:
continue
ans += dp[f(i, j)] * dp[f(l - i, j + k)] % mod
ans %= mod
print(ans)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
629/C
|
629
|
C
|
Python 3
|
TESTS
| 3 | 171 | 6,246,400 |
16250626
|
from _collections import defaultdict
n, m = input().split()
n, m = int(n), int(m)
dp = [None] * (n-m+1)
for _ in range(n-m+1):
dp[_] = defaultdict(int)
dp[0][0] = 1
for i in range(n-m):
for k, v in dp[i].items():
if k > 0:
dp[i+1][k-1] += dp[i][k]
dp[i+1][k+1] += dp[i][k]
balance = 0
min_interm = 0
for ch in input():
if ch == "(":
balance += 1
else:
balance -= 1
min_interm = min(balance, min_interm)
ans = 0
for spl in range(0, n-m+1):
for bal, comb in dp[spl].items():
if bal < -min_interm:
continue
ans += comb * dp[n-m-spl][bal+balance]
print(ans)
| 60 | 218 | 10,137,600 |
16250216
|
n, m = map(int, input().split())
s = input()
mod = 10 ** 9 + 7
c = b = 0
for x in s:
c += (x == '(') * 2 - 1
b = min(c, b)
d = [[1]]
for i in range(n - m):
nd = d[-1][1:] + [0] * 2
for j in range(1, i + 2):
nd[j] = (nd[j] + d[-1][j-1]) % mod
d.append(nd)
ans = 0
for i in range(n - m + 1):
l = n - m - i
for j in range(-b, min(l - c, i) + 1):
ans = (ans + d[i][j] * d[l][j + c]) % mod
print(ans)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
629/C
|
629
|
C
|
Python 3
|
TESTS
| 3 | 514 | 7,168,000 |
16967898
|
n, m = list(map(int, input().split()))
s = input()
f = [ [0 for i in range(n - m + 1)] for i in range(n - m + 1)]
f[0][0] = 1
mod = 1000000007
for i in range(1, n - m + 1):
for j in range(0, n - m + 1):
if j < n - m:
f[i][j] = (f[i][j] + f[i - 1][j + 1]) % mod
if j > 0:
f[i][j] = (f[i][j] + f[i - 1][j - 1]) % mod
cnt = 0
for x in s:
if x == '(':
cnt += 1
else:
cnt -= 1
ans = 0
for i in range(0, n - m + 1):
for j in range(0, n - m + 1):
if 0 <= j + cnt <= n - m:
ans = (ans + f[i][j] * f[n - m - i][j + cnt]) % mod;
print(ans)
| 60 | 234 | 10,137,600 |
16250282
|
n, m = map(int, input().split())
s = input()
mod = 10 ** 9 + 7
c, b, ans, d, k = 0, 0, 0, [[1]], n - m
for i in s:
c += (i == '(') * 2 - 1
b = min(c, b)
for i in range(n - m):
nd = d[-1][1:] + [0] * 2
for j in range(1, i + 2):
nd[j] = (nd[j] + d[-1][j - 1]) % mod
d.append(nd)
for i in range(k + 1):
for j in range(-b, min(k - i - c, i) + 1):
ans = (ans + d[i][j] * d[k - i][j + c]) % mod
print(ans)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
629/C
|
629
|
C
|
Python 3
|
TESTS
| 3 | 467 | 4,300,800 |
17296115
|
def mp(): return map(int,input().split())
def lt(): return list(map(int,input().split()))
def pt(x): print(x)
def ip(): return input()
def it(): return int(input())
def sl(x): return [t for t in x]
def spl(x): return x.split()
def aj(liste, item): liste.append(item)
def bin(x): return "{0:b}".format(x)
def listring(l): return ' '.join([str(x) for x in l])
def printlist(l): print(' '.join([str(x) for x in l]))
n,m = mp()
s = ip()
dp = [[0 for j in range(n-m+1)]for i in range(n-m+1)]
dp[0][0] = 1
for i in range(1,n-m+1):
for j in range(n-m+1):
if j + 1 <= n-m and i - 1 >= 0:
dp[i][j] = dp[i-1][j+1] + dp[i-1][j-1]
elif i - 1 >= 0:
dp[i][j] = dp[i-1][j-1]
mini = 0
a = 0
for i in s:
if i == "(":
a += 1
else:
a -= 1
mini = min(mini,a)
result = 0
for c in range(n-m+1):
for d in range(n-m+1):
if -mini <= d:
result += dp[c][d]*(a+d <= n-m and dp[n-m-c][a+d])
print(result)
| 60 | 280 | 41,676,800 |
125105127
|
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
d=n-m
s=list(input().rstrip())
cnt=0
min_c=0
for i in range(m):
if s[i]=="(":
cnt+=1
else:
cnt-=1
min_c=min(min_c,cnt)
mod=10**9+7
dp=[[0]*(d+1) for i in range(d+1)]
dp[0][0]=1
for i in range(1,d+1):
for j in range(d+1):
if j>0:
dp[i][j]+=dp[i-1][j-1]
if j+1<=d:
dp[i][j]+=dp[i-1][j+1]
dp[i][j]%=mod
ans=0
for l in range(d+1):
r=d-l
for i in range(l+1):
if i+min_c<0 or i+cnt>d:
continue
ans+=dp[l][i]*dp[r][i+cnt]
ans%=mod
print(ans)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
629/C
|
629
|
C
|
PyPy 3-64
|
TESTS
| 3 | 327 | 37,273,600 |
209058799
|
# if you win, you live. you cannot win unless you fight.
import string
import sys
from sys import stdin, setrecursionlimit
# fd=open("cses.txt")
# sys.stdin=fd
input = stdin.readline
rd = lambda: map(lambda s: int(s), input().strip().split())
rdone = lambda: map(lambda s: int(s) - 1, input().strip().split())
ri = lambda: int(input())
rs = lambda: input().strip()
from collections import defaultdict as unsafedict, deque, Counter as unsafecounter
from bisect import bisect_left as bl, bisect_right as br
n,m=rd()
s=rs()
pref,mn=0,float("inf")
for i in s:
pref+=[-1,1][i=="("]
mn=min(mn,pref)
dp=unsafedict(lambda :-1)
mod=10**9+7
dif=n-m
def rec(i,type,bal):
if bal>2000 or bal<0 or i>dif:
return 0
if i==dif and type==1:
return bal==0
if dp[(i,type,bal)]!=-1:
return dp[(i,type,bal)]
ans=0
if type==0:
if mn>=0:
ans=rec(i,1,bal+pref)
elif mn<0 and (bal>abs(mn)):
ans=rec(i,1,bal+pref)
ans+=rec(i+1,0,bal-1)+rec(i+1,0,bal+1)
else:
ans+=rec(i+1,1,bal+1)+rec(i+1,1,bal-1)
ans%=mod
dp[(i,type,bal)]=ans
return ans
print(rec(0,0,0))
| 60 | 171 | 39,014,400 |
174350635
|
import sys,os,io
input = sys.stdin.readline # for strings
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # for non-strings
from collections import defaultdict
import bisect
import random
INF = float('inf')
MOD = 1000000007
# MOD = 998244353
def bin32(num):
return '{0:032b}'.format(num)
def gcd(x,y):
if y == 0:
return x
return gcd(y,x%y)
def lcm(x,y):
return (x*y)//gcd(x,y)
def power(x,y):
res = 1
x%=MOD
while y!=0:
if y&1 :
res = (res*x)%MOD
y>>=1
x = (x*x)%MOD
return res
def mod_inv(n):
return power(n,MOD-2)
def prob(p,q):
return (p*power(q,MOD-2))%MOD
n , m = [int(i) for i in input().split()]
rem = n - m
s = input().strip()
mini = 0
stack = 0
for i in s:
if i == '(':stack+=1
else : stack-=1
mini = min(mini , stack)
result = 0
dp = [[0 for i in range(rem + 1)] for j in range(rem + 1)]
dp[0][0] = 1
for i in range(1 , rem + 1):
for j in range(i+1):
if j != 0:
dp[i][j] +=dp[i-1][j-1]
if j <= i - 2:
dp[i][j] += dp[i-1][j+1]
dp[i][j]%=MOD
for left in range(rem + 1):
right = rem - left
for i in range(left + 1):
if i + mini >= 0 and stack + i <= rem:
result += dp[left][i]*dp[right][stack + i]
result %= MOD
print(result)
# print(want , rem , stack)
# *rdp[right][want]
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
629/C
|
629
|
C
|
PyPy 3-64
|
TESTS
| 3 | 280 | 37,990,400 |
209058426
|
# if you win, you live. you cannot win unless you fight.
import string
import sys
from sys import stdin, setrecursionlimit
# fd=open("cses.txt")
# sys.stdin=fd
input = stdin.readline
rd = lambda: map(lambda s: int(s), input().strip().split())
rdone = lambda: map(lambda s: int(s) - 1, input().strip().split())
ri = lambda: int(input())
rs = lambda: input().strip()
from collections import defaultdict as unsafedict, deque, Counter as unsafecounter
from bisect import bisect_left as bl, bisect_right as br
n,m=rd()
s=rs()
pref,mn=0,0
for i in s:
pref+=[-1,1][i=="("]
mn=min(mn,pref)
dp=unsafedict(lambda :-1)
mod=10**9+7
dif=n-m
def rec(i,type,bal):
if i==dif and type==1:
return bal==0
if bal>2000 or bal<0 or i>dif:
return 0
if dp[(i,type,bal)]!=-1:
return dp[(i,type,bal)]
ans=0
if type==0:
if mn>=0:
ans=rec(i,1,bal+pref)
elif mn<0 and (bal>abs(mn)):
ans=rec(i,1,bal+pref)
ans+=rec(i+1,0,bal-1)+rec(i+1,0,bal+1)
else:
ans+=rec(i+1,1,bal+1)+rec(i+1,1,bal-1)
ans%=mod
dp[(i,type,bal)]=ans
return ans
print(rec(0,0,0))
| 60 | 186 | 19,558,400 |
199862678
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def f(u, v):
return u * (l + 1) + v
n, m = map(int, input().split())
mod = pow(10, 9) + 7
s = list(input().rstrip())
l = n - m
dp = [0] * ((l + 1) * (l + 1))
dp[0] = 1
for i in range(l):
for j in range(i % 2, i + 1, 2):
for k in [j - 1, j + 1]:
if 0 <= k <= l:
dp[f(i + 1, k)] = (dp[f(i, j)] + dp[f(i + 1, k)]) % mod
mi, k = 0, 0
for i in s:
k += 1 if not i & 1 else -1
mi = min(mi, k)
ans = 0
for i in range(l + 1):
for j in range(i % 2, i + 1, 2):
if j + mi < 0 or l - i < j + k:
continue
ans += dp[f(i, j)] * dp[f(l - i, j + k)] % mod
ans %= mod
print(ans)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
629/C
|
629
|
C
|
Python 3
|
TESTS
| 3 | 218 | 307,200 |
16622254
|
def ij(s):
i = 0
j = 0
for c in s:
if c == ')':
if j > 0:
j -= 1
else:
i += 1
else:
j += 1
return i, j
def ways(n, s):
I, J = ij(s)
f = n - len(s) - I - J
if f < 0 or f%2:
return 0
E = f//2
C = [1]
for n in range(E):
C.append(C[n] * 2 * (2*n + 1) // (n+2))
iset = set()
for ii in I, J:
i = ii;
while i:
iset.add(i)
if i%2:
i //= 2
else:
i -= 1
W = [None for i in range(max(I, J)+1)]
W[0] = [C[e] for e in range(E+1)]
for i in sorted(iset):
W[i] = [None for e in range(E+1)]
if i%2:
j = i//2
for e in range(E+1):
W[i][e] = sum(W[j][k] * W[j][e-k] for k in range(e+1))
else:
for e in range(E+1):
W[i][e] = sum(C[k] * W[i-1][e-k] for k in range(e+1))
else:
return sum(W[I][e] * W[J][f-e] for f in range(E+1) for e in range(f+1))
if __name__ == '__main__':
n, m = map(int, input().split())
s = input()
print(ways(n, s) % (10**9 + 7))
| 60 | 218 | 10,137,600 |
16250216
|
n, m = map(int, input().split())
s = input()
mod = 10 ** 9 + 7
c = b = 0
for x in s:
c += (x == '(') * 2 - 1
b = min(c, b)
d = [[1]]
for i in range(n - m):
nd = d[-1][1:] + [0] * 2
for j in range(1, i + 2):
nd[j] = (nd[j] + d[-1][j-1]) % mod
d.append(nd)
ans = 0
for i in range(n - m + 1):
l = n - m - i
for j in range(-b, min(l - c, i) + 1):
ans = (ans + d[i][j] * d[l][j + c]) % mod
print(ans)
|
Codeforces Round 343 (Div. 2)
|
CF
| 2,016 | 2 | 256 |
Famil Door and Brackets
|
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
|
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
|
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
| null |
In the first sample there are four different valid pairs:
1. p = "(", q = "))"
2. p = "()", q = ")"
3. p = "", q = "())"
4. p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
|
[{"input": "4 1\n(", "output": "4"}, {"input": "4 4\n(())", "output": "1"}, {"input": "4 3\n(((", "output": "0"}]
| 2,000 |
["dp", "strings"]
| 60 |
[{"input": "4 1\r\n(\r\n", "output": "4\r\n"}, {"input": "4 4\r\n(())\r\n", "output": "1\r\n"}, {"input": "4 3\r\n(((\r\n", "output": "0\r\n"}, {"input": "875 50\r\n)))((())()))((()(())))))())))((((((()))))))()(((((\r\n", "output": "0\r\n"}, {"input": "1980 464\r\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())\r\n", "output": "854368836\r\n"}, {"input": "1542 282\r\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()\r\n", "output": "631927032\r\n"}, {"input": "2 2\r\n)(\r\n", "output": "0\r\n"}, {"input": "2 2\r\n))\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
178/A1
|
178
|
A3
|
PyPy 3-64
|
TESTS3
| 13 | 218 | 4,403,200 |
168439563
|
n = int(input())
a = list(map(int, input().split()))
max_power = 9
while n - 1 - 2 ** max_power < 0:
max_power -= 1
for i in range(1, n):
steps = 0
nums = a[:i]
for num_index, num in enumerate(nums):
power = max_power
times = 0
step = 2 ** power
while num_index < i:
if num_index + step < n:
num_index += step
times += 1
else:
power -= 1
step = 2 ** power
steps += num * times
print(steps)
| 12 | 62 | 0 |
165771559
|
n=int(input())
a=[*map(int,input().split())]
s=0
for i in range(n-1):
p=1
while i+(p<<1)<n:p<<=1
a[i+p]+=a[i]
print(s+a[i])
s+=a[i]
|
ABBYY Cup 2.0 - Hard
|
ICPC
| 2,012 | 2 | 256 |
Educational Game
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
|
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
- 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
- 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
- 1 ≤ n ≤ 105
|
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
| null | null |
[{"input": "4\n1 0 1 2", "output": "1\n1\n3"}, {"input": "8\n1 2 3 4 5 6 7 8", "output": "1\n3\n6\n10\n16\n24\n40"}]
| 1,000 |
[]
| 12 |
[{"input": "4\r\n1 0 1 2\r\n", "output": "1\r\n1\r\n3\r\n"}, {"input": "8\r\n1 2 3 4 5 6 7 8\r\n", "output": "1\r\n3\r\n6\r\n10\r\n16\r\n24\r\n40\r\n"}, {"input": "5\r\n4 1 4 7 6\r\n", "output": "4\r\n5\r\n9\r\n17\r\n"}, {"input": "9\r\n13 13 7 11 3 9 3 5 5\r\n", "output": "13\r\n26\r\n33\r\n44\r\n47\r\n69\r\n79\r\n117\r\n"}, {"input": "30\r\n8 17 20 15 18 15 20 10 5 13 5 4 15 9 11 14 18 15 7 16 18 9 17 7 10 9 5 13 17 16\r\n", "output": "8\r\n25\r\n45\r\n60\r\n78\r\n93\r\n113\r\n123\r\n128\r\n141\r\n146\r\n150\r\n165\r\n174\r\n185\r\n199\r\n225\r\n257\r\n284\r\n315\r\n351\r\n375\r\n423\r\n454\r\n495\r\n549\r\n634\r\n713\r\n907\r\n"}, {"input": "80\r\n72 66 82 46 44 22 63 92 71 65 5 30 45 84 29 73 9 90 25 19 26 15 12 29 33 19 85 92 91 66 83 39 100 53 20 99 11 81 26 41 36 51 21 72 28 100 34 3 24 58 11 85 73 18 4 45 90 99 42 85 26 71 58 49 76 32 88 13 40 98 57 95 20 36 70 66 75 12 54 96\r\n", "output": "72\n138\n220\n266\n310\n332\n395\n487\n558\n623\n628\n658\n703\n787\n816\n889\n898\n988\n1013\n1032\n1058\n1073\n1085\n1114\n1147\n1166\n1251\n1343\n1434\n1500\n1583\n1622\n1722\n1775\n1795\n1894\n1905\n1986\n2012\n2053\n2089\n2140\n2161\n2233\n2261\n2361\n2395\n2398\n2431\n2579\n2615\n2719\n2818\n2851\n2867\n2941\n3064\n3182\n3309\n3486\n3603\n3740\n3881\n3969\n4250\n4549\n4775\n5037\n5231\n5465\n5627\n5929\n6460\n7029\n7478\n8085\n9075\n10211\n12070\n"}]
| false |
stdio
| null | true |
460/C
|
460
|
C
|
PyPy 3-64
|
TESTS
| 4 | 62 | 614,400 |
218070291
|
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(height):
l = []
for i in range(N):
if height-A[i]>=1:
l.append([i,height-A[i]])
if not l:return True
x,num = l.pop()
ans = 0
cnt = num
pre = deque([[x-W+1,num]])
ans+=num
while l:
if pre and pre[-1][0]>=x:
x,num = pre.pop()
cnt-=num
x,num = l.pop()
# print(x,num,cnt,pre)
if num-cnt>0:
ans+=(num-cnt)
cnt+=(num-cnt)
pre.appendleft([x-W+1,num-cnt])
# print(ans)
if ans<=M:return True
return False
N,M,W = map(int,input().split())
A = list(map(int,input().split()))
l,r = 0,10**18+1
while l+1<r:
mid = (l+r)//2
if find(mid):
l = mid
else:
r = mid
if find(r):
print(r)
else:
print(l)
# print(l,r,find(10))
| 43 | 124 | 18,534,400 |
208845663
|
import sys
input = lambda: sys.stdin.readline().strip()
n, m, w = list(map(int, input().split()))
a = list(map(int, input().split()))
l, r = 1, 1000100001 # [)
while l < r - 1:
mid = (l + r) // 2
diff = [0] * n
cnt = 0
tmp = 0
for i in range(n):
tmp += diff[i]
need = mid - tmp - a[i]
if need > 0:
cnt += need
if cnt > m:
break
tmp += need
if i + w < n:
diff[i + w] -= need
if cnt > m:
r = mid
else:
l = mid
print(l)
|
Codeforces Round 262 (Div. 2)
|
CF
| 2,014 | 2 | 256 |
Present
|
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
|
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
|
Print a single integer — the maximum final height of the smallest flower.
| null |
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
|
[{"input": "6 2 3\n2 2 2 2 1 1", "output": "2"}, {"input": "2 5 1\n5 8", "output": "9"}]
| 1,700 |
["binary search", "data structures", "greedy"]
| 43 |
[{"input": "6 2 3\r\n2 2 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "2 5 1\r\n5 8\r\n", "output": "9\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "2\r\n"}, {"input": "3 2 3\r\n999999998 999999998 999999998\r\n", "output": "1000000000\r\n"}, {"input": "10 8 3\r\n499 498 497 497 497 497 497 497 498 499\r\n", "output": "500\r\n"}, {"input": "11 18 8\r\n4996 4993 4988 4982 4982 4982 4982 4982 4986 4989 4994\r\n", "output": "5000\r\n"}, {"input": "1 100000 1\r\n1000000000\r\n", "output": "1000100000\r\n"}, {"input": "4 100 3\r\n1 100000 100000 1\r\n", "output": "51\r\n"}]
| false |
stdio
| null | true |
460/C
|
460
|
C
|
PyPy 3-64
|
TESTS
| 3 | 61 | 0 |
196540734
|
# author: cholebhature lover
from collections import *
from bisect import *
from heapq import *
from math import *
import sys
def input():
return sys.stdin.readline().rstrip('\r\n')
def maximizeMinHeight(n, k, w, a):
i = 1
j = 10**6
ans = 10**9
while i <= j:
mid = i+(j-i)//2
arr = [0]*(n+1)
curr = 0
moves = 0
for ii in range(n):
if ii-w >= 0:
curr -= arr[ii-w]
if a[ii]+curr <= mid:
arr[ii] = mid-a[ii]-curr
curr += arr[ii]
moves += arr[ii]
if moves <= k:
ans = mid
i = mid+1
else:
j = mid-1
return ans
n, k, w = map(int, input().split())
a = list(map(int, input().split()))
print(maximizeMinHeight(n, k, w, a))
| 43 | 140 | 18,534,400 |
208854903
|
n,m,w = list(map(int,input().strip().split()))
a = list(map(int,input().strip().split()))
left = 0
right = 10**10
def check(x):
sub = [0]*(n + 1)
cnt = 0
for i in range(n):
sub[i] += sub[i - 1]
if a[i] + sub[i] < x:
diff = x - a[i] - sub[i]
cnt += diff
sub[i] += diff
sub[min(i + w,n)] -= diff
return cnt <= m
while left <= right:
mid = (left + right)//2
if check(mid):
left = mid + 1
else:
right = mid - 1
print(right)
|
Codeforces Round 262 (Div. 2)
|
CF
| 2,014 | 2 | 256 |
Present
|
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
|
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
|
Print a single integer — the maximum final height of the smallest flower.
| null |
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
|
[{"input": "6 2 3\n2 2 2 2 1 1", "output": "2"}, {"input": "2 5 1\n5 8", "output": "9"}]
| 1,700 |
["binary search", "data structures", "greedy"]
| 43 |
[{"input": "6 2 3\r\n2 2 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "2 5 1\r\n5 8\r\n", "output": "9\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "2\r\n"}, {"input": "3 2 3\r\n999999998 999999998 999999998\r\n", "output": "1000000000\r\n"}, {"input": "10 8 3\r\n499 498 497 497 497 497 497 497 498 499\r\n", "output": "500\r\n"}, {"input": "11 18 8\r\n4996 4993 4988 4982 4982 4982 4982 4982 4986 4989 4994\r\n", "output": "5000\r\n"}, {"input": "1 100000 1\r\n1000000000\r\n", "output": "1000100000\r\n"}, {"input": "4 100 3\r\n1 100000 100000 1\r\n", "output": "51\r\n"}]
| false |
stdio
| null | true |
789/A
|
789
|
A
|
Python 3
|
PRETESTS
| 6 | 108 | 11,673,600 |
25902847
|
n,k=[int(i)for i in input().split()]
w=[int(i)for i in input().split()]
i=0
x=0
for i in w:
if i%k==0:
x+=i//k
else :
x+=i//k+1
print(x//2 if x%k==0 else x//2+1)
| 31 | 92 | 7,372,800 |
195015992
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for x in a:
ans += x // k + (1 if x % k != 0 else 0)
print(ans // 2 + ans % 2)
|
Codeforces Round 407 (Div. 2)
|
CF
| 2,017 | 1 | 256 |
Anastasia and pebbles
|
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
|
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
|
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
| null |
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
|
[{"input": "3 2\n2 3 4", "output": "3"}, {"input": "5 4\n3 1 8 9 7", "output": "5"}]
| 1,100 |
["implementation", "math"]
| 31 |
[{"input": "3 2\r\n2 3 4\r\n", "output": "3\r\n"}, {"input": "5 4\r\n3 1 8 9 7\r\n", "output": "5\r\n"}, {"input": "1 22\r\n1\r\n", "output": "1\r\n"}, {"input": "3 57\r\n78 165 54\r\n", "output": "3\r\n"}, {"input": "5 72\r\n74 10 146 189 184\r\n", "output": "6\r\n"}, {"input": "9 13\r\n132 87 200 62 168 51 185 192 118\r\n", "output": "48\r\n"}, {"input": "1 1\r\n10000\r\n", "output": "5000\r\n"}, {"input": "10 1\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2 2\r\n", "output": "1\r\n"}]
| false |
stdio
| null | true |
789/A
|
789
|
A
|
PyPy 3-64
|
TESTS
| 5 | 61 | 14,028,800 |
184479098
|
from sys import stdin; inp = stdin.readline
from math import dist, ceil, floor, sqrt, log
def IA(): return list(map(int, inp().split()))
def FA(): return list(map(float, inp().split()))
def SA(): return inp().split()
def I(): return int(inp())
def F(): return float(inp())
def S(): return inp()
def main():
n,k = IA()
a = IA()
c = 0
for n in a:
if n <= k:
c += 0.5
elif k < n < 2*k:
c += 1
else:
c+= n//(2*k)
rem = n%(2*k)
if rem:
c += 1
return ceil(c)
if __name__ == '__main__':
print(main())
| 31 | 92 | 11,161,600 |
212566831
|
import math
n,k = (int(x) for x in input().split())
w = [int(x) for x in input().split()]
d = 0
for x in w: d += math.ceil(x/k)
print(math.ceil(d/2))
|
Codeforces Round 407 (Div. 2)
|
CF
| 2,017 | 1 | 256 |
Anastasia and pebbles
|
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
|
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
|
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
| null |
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
|
[{"input": "3 2\n2 3 4", "output": "3"}, {"input": "5 4\n3 1 8 9 7", "output": "5"}]
| 1,100 |
["implementation", "math"]
| 31 |
[{"input": "3 2\r\n2 3 4\r\n", "output": "3\r\n"}, {"input": "5 4\r\n3 1 8 9 7\r\n", "output": "5\r\n"}, {"input": "1 22\r\n1\r\n", "output": "1\r\n"}, {"input": "3 57\r\n78 165 54\r\n", "output": "3\r\n"}, {"input": "5 72\r\n74 10 146 189 184\r\n", "output": "6\r\n"}, {"input": "9 13\r\n132 87 200 62 168 51 185 192 118\r\n", "output": "48\r\n"}, {"input": "1 1\r\n10000\r\n", "output": "5000\r\n"}, {"input": "10 1\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2 2\r\n", "output": "1\r\n"}]
| false |
stdio
| null | true |
38/D
|
38
|
D
|
Python 3
|
TESTS
| 3 | 216 | 0 |
59274172
|
n = int(input())
a = list(map(int,input().split()))
k = 1
ob = r = 0
for i in range(n-1):
b = list(map(int,input().split()))
if b[1]==a[1]:
if a[0]<b[0] and a[2]<b[2]:
if a[2]-b[0]>=b[2]-a[2]:
k+=1
else: break
else:
if b[2]-a[0]>=a[0]-b[0]:
k+=1
else:
break
else:
if a[1]<b[1] and a[3]<b[3]:
if a[3]-b[1]>=b[3]-a[3]:
k+=1
else: break
else:
if b[3]-a[1]>=a[1]-b[1]:
k+=1
else:
break
a = b
print(k)
| 64 | 342 | 22,220,800 |
86059864
|
n = int(input())
l = []
m = []
for i in range(n):
X1, Y1, X2, Y2 = map(int, input().split())
m1 = (abs(X2 - X1)) ** 3
l.append([X1, Y1, X2, Y2])
m.append(m1)
X1, Y1, X2, Y2 = l[0][0], l[0][1], l[0][2], l[0][3]
if X1 > X2: X2, X1 = X1, X2
if Y1 > Y2: Y1, Y2 = Y2, Y1
ans = n
for i in range(1, n):
for j in range(i, 0, -1):
x1, y1, x2, y2 = l[j - 1][0], l[j - 1][1], l[j - 1][2], l[j - 1][3]
if x1 > x2: x2, x1 = x1, x2
if y1 > y2: y1, y2 = y2, y1
numx, numy, den = 0, 0 , 0
for k in range(i, j - 1, -1):
a = (l[k][0] + l[k][2]) / 2
b = (l[k][1] + l[k][3]) / 2
m = (abs(l[k][2] - l[k][0])) ** 3
numx += m * a
numy += m * b
den += m
xcm = numx / den
ycm = numy / den
if x1 <= xcm <= x2 and y1 <= ycm <= y2: pass
else:
ans = i
break
if ans != n: break
print(ans)
|
School Personal Contest #1 (Winter Computer School 2010/11) - Codeforces Beta Round 38 (ACM-ICPC Rules)
|
ICPC
| 2,010 | 2 | 256 |
Vasya the Architect
|
Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart.
Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides of the bricks he has already used. Let's introduce a Cartesian coordinate system on the horizontal plane, where Vasya puts the first brick. Then the projection of brick number i on the plane is a square with sides parallel to the axes of coordinates with opposite corners in points (xi, 1, yi, 1) and (xi, 2, yi, 2). The bricks are cast from homogeneous plastic and the weight of a brick a × a × a is a3 grams.
It is guaranteed that Vasya puts any brick except the first one on the previous one, that is the area of intersection of the upper side of the previous brick and the lower side of the next brick is always positive.
We (Vasya included) live in a normal world where the laws of physical statics work. And that is why, perhaps, if we put yet another brick, the tower will collapse under its own weight. Vasya puts the cubes consecutively one on top of the other until at least one cube loses the balance and falls down. If it happens, Vasya gets upset and stops the construction. Print the number of bricks in the maximal stable tower, that is the maximal number m satisfying the condition that all the towers consisting of bricks 1, 2, ..., k for every integer k from 1 to m remain stable.
|
The first input file contains an integer n (1 ≤ n ≤ 100) which is the number of bricks. Each of the next n lines contains four numbers xi, 1, yi, 1, xi, 2, yi, 2 (xi, 1 ≠ xi, 2, |xi, 1 - xi, 2| = |yi, 1 - yi, 2|) which are the coordinates of the opposite angles of the base of the brick number i. The coordinates are integers and their absolute value does not exceed 50.
The cubes are given in the order Vasya puts them. It is guaranteed that the area of intersection of the upper side of the brick number i - 1 and the lower side of the brick number i is strictly strictly greater than zero for all i ≥ 2.
|
Print the number of bricks in the maximal stable tower.
| null | null |
[{"input": "2\n0 0 3 3\n1 0 4 3", "output": "2"}, {"input": "2\n0 0 3 3\n2 0 5 3", "output": "1"}, {"input": "3\n0 0 3 3\n1 0 4 3\n2 0 5 3", "output": "3"}]
| 1,900 |
["implementation"]
| 64 |
[{"input": "2\r\n0 0 3 3\r\n1 0 4 3\r\n", "output": "2\r\n"}, {"input": "2\r\n0 0 3 3\r\n2 0 5 3\r\n", "output": "1\r\n"}, {"input": "3\r\n0 0 3 3\r\n1 0 4 3\r\n2 0 5 3\r\n", "output": "3\r\n"}, {"input": "5\r\n7 -10 -8 5\r\n4 -7 -5 2\r\n2 -5 -3 0\r\n-9 48 50 -11\r\n50 -4 -2 48\r\n", "output": "3\r\n"}, {"input": "5\r\n6 -2 13 -9\r\n17 2 2 -13\r\n13 -2 6 -9\r\n4 0 15 -11\r\n8 -7 11 -4\r\n", "output": "5\r\n"}, {"input": "7\r\n4 6 1 9\r\n-1 4 6 11\r\n8 13 -3 2\r\n-5 0 45 50\r\n-2 50 45 3\r\n45 50 -4 1\r\n45 50 -1 4\r\n", "output": "3\r\n"}, {"input": "7\r\n24 6 11 -7\r\n23 -6 12 5\r\n16 -2 19 1\r\n13 4 22 -5\r\n12 -6 23 5\r\n13 32 50 -5\r\n50 32 11 -7\r\n", "output": "5\r\n"}, {"input": "7\r\n-2 23 -10 15\r\n2 11 -14 27\r\n-12 25 0 13\r\n-8 17 -4 21\r\n1 26 -13 12\r\n-8 17 -4 21\r\n-7 20 -5 18\r\n", "output": "7\r\n"}, {"input": "10\r\n27 -3 15 9\r\n18 0 24 6\r\n28 -4 14 10\r\n19 1 23 5\r\n27 -3 15 9\r\n19 32 50 1\r\n50 32 14 -4\r\n19 32 50 1\r\n15 32 50 -3\r\n16 -2 50 32\r\n", "output": "5\r\n"}, {"input": "10\r\n-9 -14 -1 -6\r\n-2 -13 -8 -7\r\n0 -15 -10 -5\r\n0 -15 -10 -5\r\n-11 -4 1 -16\r\n-8 -13 -2 -7\r\n1 -16 -11 -4\r\n-13 -2 3 -18\r\n-11 -16 1 -4\r\n-13 -18 3 -2\r\n", "output": "10\r\n"}, {"input": "15\r\n5 16 -6 27\r\n28 21 -1 50\r\n-2 20 28 50\r\n28 14 -8 50\r\n28 19 -3 50\r\n-2 50 28 20\r\n-5 17 28 50\r\n-7 15 28 50\r\n-4 50 28 18\r\n28 50 -1 21\r\n-3 50 28 19\r\n-4 50 28 18\r\n28 50 -8 14\r\n28 50 -6 16\r\n28 15 -7 50\r\n", "output": "1\r\n"}, {"input": "15\r\n-14 27 -1 14\r\n-10 23 -5 18\r\n1 29 -16 12\r\n-9 19 -6 22\r\n-10 18 -5 23\r\n-4 17 -11 24\r\n22 50 -13 15\r\n-13 50 22 15\r\n22 50 -11 17\r\n-9 19 22 50\r\n-10 18 22 50\r\n22 19 -9 50\r\n-14 14 22 50\r\n-14 14 22 50\r\n-11 17 22 50\r\n", "output": "6\r\n"}, {"input": "15\r\n-8 11 1 20\r\n-5 14 -2 17\r\n-1 18 -6 13\r\n0 19 -7 12\r\n-5 14 -2 17\r\n-11 8 4 23\r\n-2 14 -5 17\r\n2 21 -9 10\r\n-12 24 5 7\r\n-10 22 3 9\r\n31 50 -11 8\r\n-8 11 31 50\r\n-7 50 31 12\r\n-7 12 31 50\r\n-10 9 31 50\r\n", "output": "10\r\n"}, {"input": "20\r\n9 -3 16 4\r\n11 2 14 -1\r\n6 -6 19 7\r\n16 -3 9 4\r\n5 -7 50 38\r\n50 38 5 -7\r\n50 38 12 0\r\n50 38 11 -1\r\n10 38 50 -2\r\n5 -7 50 38\r\n50 38 7 -5\r\n50 38 10 -2\r\n50 38 12 0\r\n50 38 10 -2\r\n10 38 50 -2\r\n50 -5 7 38\r\n50 -7 5 38\r\n11 -1 50 38\r\n10 -2 50 38\r\n7 -5 50 38\r\n", "output": "4\r\n"}, {"input": "20\r\n12 23 -1 10\r\n12 23 -1 10\r\n11 22 0 11\r\n7 18 4 15\r\n12 10 -1 23\r\n9 20 2 13\r\n1 21 10 12\r\n0 11 11 22\r\n39 50 2 13\r\n3 14 39 50\r\n39 10 -1 50\r\n4 15 39 50\r\n39 50 4 15\r\n39 50 -2 9\r\n39 50 1 12\r\n-2 9 39 50\r\n2 50 39 13\r\n0 11 39 50\r\n-2 9 39 50\r\n1 12 39 50\r\n", "output": "8\r\n"}, {"input": "20\r\n26 16 11 1\r\n11 1 26 16\r\n23 4 14 13\r\n13 14 24 3\r\n20 10 17 7\r\n25 15 12 2\r\n16 6 21 11\r\n25 15 12 2\r\n10 0 27 17\r\n22 12 15 5\r\n23 4 14 13\r\n22 5 15 12\r\n25 2 12 15\r\n12 15 25 2\r\n22 12 15 5\r\n22 5 15 12\r\n21 11 16 6\r\n26 16 11 1\r\n17 7 20 10\r\n24 3 13 14\r\n", "output": "20\r\n"}, {"input": "5\r\n-6 6 6 -6\r\n7 -8 -5 4\r\n12 -11 2 -1\r\n50 39 -1 -12\r\n-3 -16 50 37\r\n", "output": "3\r\n"}, {"input": "5\r\n-12 12 12 -12\r\n4 -12 8 -8\r\n10 -5 2 -13\r\n12 -5 -2 -19\r\n-7 -18 15 4\r\n", "output": "5\r\n"}, {"input": "7\r\n1 1 -1 -1\r\n6 7 -6 -5\r\n10 -11 -12 11\r\n-12 -11 49 50\r\n-7 43 50 -14\r\n-17 50 37 -4\r\n-14 50 47 -11\r\n", "output": "3\r\n"}, {"input": "7\r\n-9 9 9 -9\r\n10 -5 8 -7\r\n7 -6 9 -8\r\n20 5 -2 -17\r\n8 -15 10 -17\r\n50 25 3 -22\r\n50 28 7 -15\r\n", "output": "5\r\n"}, {"input": "7\r\n-9 -9 9 9\r\n-8 3 0 -5\r\n-16 -8 6 14\r\n14 -11 -6 9\r\n5 -5 1 -9\r\n-11 -21 13 3\r\n-10 5 10 -15\r\n", "output": "6\r\n"}, {"input": "10\r\n-7 7 7 -7\r\n-8 -11 8 5\r\n-1 -1 1 1\r\n-2 1 2 -3\r\n-12 -12 10 10\r\n47 50 3 6\r\n46 5 1 50\r\n50 50 -10 -10\r\n-9 50 50 -9\r\n-6 -6 50 50\r\n", "output": "5\r\n"}, {"input": "10\r\n-7 -7 7 7\r\n-16 -18 6 4\r\n-12 -8 4 8\r\n5 -13 -7 -1\r\n5 4 -13 -14\r\n7 11 -13 -9\r\n4 10 8 6\r\n8 6 0 14\r\n7 7 9 5\r\n-2 -9 16 9\r\n", "output": "9\r\n"}, {"input": "15\r\n1 1 -1 -1\r\n50 -8 -7 49\r\n45 50 0 5\r\n49 50 -8 -7\r\n-11 50 49 -10\r\n0 9 41 50\r\n50 50 -6 -6\r\n-1 46 50 -5\r\n1 49 50 0\r\n50 -8 -8 50\r\n7 41 50 -2\r\n-9 50 50 -9\r\n50 10 10 50\r\n9 41 50 0\r\n-4 -5 50 49\r\n", "output": "1\r\n"}, {"input": "15\r\n-9 -9 9 9\r\n-2 -18 16 0\r\n15 -3 5 -13\r\n6 -7 4 -9\r\n17 4 -7 -20\r\n6 -15 4 -17\r\n50 -18 3 29\r\n50 -22 -1 29\r\n-3 -23 50 30\r\n-6 -26 50 30\r\n50 -26 -5 29\r\n50 21 11 -18\r\n50 23 6 -21\r\n50 28 6 -16\r\n50 -24 -3 29\r\n", "output": "6\r\n"}, {"input": "15\r\n-7 -7 7 7\r\n-10 -16 14 8\r\n13 -12 5 -4\r\n12 -6 4 -14\r\n3 -17 17 -3\r\n21 -11 7 3\r\n14 0 2 -12\r\n-3 0 17 -20\r\n-2 -12 18 8\r\n9 -14 13 -10\r\n7 -15 50 28\r\n0 -20 50 30\r\n50 -14 6 30\r\n-4 -21 50 33\r\n50 33 -3 -20\r\n", "output": "10\r\n"}, {"input": "6\r\n-9 -9 9 9\r\n-16 9 0 -7\r\n7 8 -17 -16\r\n49 -18 -19 50\r\n-19 49 50 -20\r\n50 -17 -14 47\r\n", "output": "3\r\n"}, {"input": "6\r\n-5 5 5 -5\r\n9 7 -3 -5\r\n4 0 -8 12\r\n-7 2 -9 0\r\n-9 -1 -5 3\r\n42 -9 -17 50\r\n", "output": "5\r\n"}, {"input": "8\r\n12 -12 -12 12\r\n1 13 -13 -1\r\n-18 0 2 20\r\n31 -1 -20 50\r\n29 50 -30 -9\r\n-15 -9 44 50\r\n-21 -14 43 50\r\n45 50 -16 -11\r\n", "output": "3\r\n"}, {"input": "8\r\n-2 2 2 -2\r\n-2 6 6 -2\r\n5 -2 -1 4\r\n9 -7 -9 11\r\n13 10 -9 -12\r\n-10 -16 50 44\r\n32 50 -15 3\r\n-13 50 34 3\r\n", "output": "5\r\n"}, {"input": "8\r\n-7 -7 7 7\r\n-1 -5 -3 -3\r\n5 4 -11 -12\r\n8 -4 2 -10\r\n3 -5 1 -3\r\n10 -15 -10 5\r\n7 -14 -11 4\r\n-2 -10 50 42\r\n", "output": "5\r\n"}, {"input": "11\r\n12 12 -12 -12\r\n11 8 -11 -14\r\n-15 -11 -3 1\r\n-16 -3 -14 -1\r\n-7 -8 -21 6\r\n37 -6 -19 50\r\n-21 -12 41 50\r\n48 -13 -15 50\r\n48 -14 -16 50\r\n-21 -18 47 50\r\n40 50 -15 -5\r\n", "output": "4\r\n"}, {"input": "11\r\n-4 4 4 -4\r\n-2 -3 0 -5\r\n-8 -10 6 4\r\n-12 6 8 -14\r\n-4 -12 12 4\r\n-1 -11 1 -9\r\n-5 -14 5 -4\r\n-11 2 11 -20\r\n10 -10 12 -8\r\n7 -12 13 -6\r\n-6 41 50 -15\r\n", "output": "6\r\n"}, {"input": "1\r\n0 0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n-50 -50 50 50\r\n", "output": "1\r\n"}]
| false |
stdio
| null | true |
952/E
|
952
|
E
|
PyPy 3
|
TESTS
| 2 | 124 | 0 |
70402735
|
n = int(input())
ans = 0
for _ in range(n):
name = input().split(" ")[0]
for ch in name:
if ch == "m":
ans += 1
print(ans)
| 40 | 78 | 7,065,600 |
36828940
|
def main():
n = int(input())
b, w = 0, 0
for x in range(n):
s = input().split()[1]
if s == 'soft':
b += 1
else:
w += 1
for x in range(1, 100):
a = x ** 2
a1 = a // 2
a2 = a - a1
if max(b, w) <= a2 and min(b, w) <= a1:
print(x)
return
main()
|
April Fools Contest 2018
|
ICPC
| 2,018 | 1 | 256 |
Cheese Board
|
Not to be confused with chessboard.
|
The first line of input contains a single integer N (1 ≤ N ≤ 100) — the number of cheeses you have.
The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either "soft" or "hard. All cheese names are distinct.
|
Output a single number.
| null | null |
[{"input": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard", "output": "3"}, {"input": "6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard", "output": "4"}]
| 2,000 |
[]
| 40 |
[{"input": "9\r\nbrie soft\r\ncamembert soft\r\nfeta soft\r\ngoat soft\r\nmuenster soft\r\nasiago hard\r\ncheddar hard\r\ngouda hard\r\nswiss hard\r\n", "output": "3\r\n"}, {"input": "6\r\nparmesan hard\r\nemmental hard\r\nedam hard\r\ncolby hard\r\ngruyere hard\r\nasiago hard\r\n", "output": "4\r\n"}, {"input": "9\r\ngorgonzola soft\r\ncambozola soft\r\nmascarpone soft\r\nricotta soft\r\nmozzarella soft\r\nbryndza soft\r\njarlsberg soft\r\nhavarti soft\r\nstilton soft\r\n", "output": "5\r\n"}, {"input": "1\r\nprovolone hard\r\n", "output": "1\r\n"}, {"input": "4\r\nemmental hard\r\nfeta soft\r\ngoat soft\r\nroquefort hard\r\n", "output": "2\r\n"}, {"input": "1\r\ncamembert soft\r\n", "output": "1\r\n"}, {"input": "2\r\nmuenster soft\r\nasiago hard\r\n", "output": "2\r\n"}, {"input": "32\r\nauhwslzn soft\r\nkpq hard\r\neukw soft\r\nsinenrsz soft\r\najuoe soft\r\ngapj soft\r\nuyuhqv hard\r\nifldxi hard\r\npgy soft\r\njnjhh hard\r\nbyswtu soft\r\nhdr hard\r\njamqcp hard\r\nmrknxch soft\r\nghktedrf hard\r\nutley hard\r\nreinr hard\r\nvbhk hard\r\neuft soft\r\nxspriqy soft\r\ntrooa soft\r\nuylbj soft\r\nkgt soft\r\nlhc hard\r\nrwxhlux soft\r\nsuoku soft\r\ndhhoae soft\r\nlisv soft\r\nwlco hard\r\nbhmptm soft\r\nualppum soft\r\nlpxizrhr soft\r\n", "output": "7\r\n"}, {"input": "18\r\nbcvyeeap soft\r\nubb hard\r\nsrbb hard\r\nemcmg hard\r\nmelqan hard\r\nuenps soft\r\ncpyminr hard\r\ndpx soft\r\nglkj hard\r\nmsozshuy soft\r\nxnvrcozn soft\r\ntftctb soft\r\ncija hard\r\ngxl hard\r\npjoja soft\r\ndhzze hard\r\niyvl soft\r\nctrszg hard\r\n", "output": "5\r\n"}, {"input": "31\r\npevkjopz soft\r\nsmqei hard\r\nxhfmuqua soft\r\ngtmbnvn hard\r\nkdvztv soft\r\ncziuxm hard\r\ngdswd hard\r\nnawkigiz soft\r\neehdplwt hard\r\nizhivjj soft\r\ntvnkqkc hard\r\nwefwgi hard\r\nuxczrz hard\r\njdqudhgp soft\r\nhmyzqb soft\r\nwwlc soft\r\ndsax soft\r\nslefe soft\r\nahfitc hard\r\nlztbmai soft\r\nzcatg soft\r\nhwlubzmy soft\r\njkbl soft\r\nbfdfh soft\r\nzshdiuce hard\r\neobyco soft\r\nckg hard\r\nahcwzw soft\r\nvtaujlke soft\r\niwfdcik hard\r\nitb soft\r\n", "output": "7\r\n"}, {"input": "27\r\ndbilglfh hard\r\niecrbay hard\r\ncpunhmf hard\r\nszvvz soft\r\nqsbg hard\r\nvdzexx hard\r\naiuvj soft\r\nfuccez hard\r\ndvscmzd hard\r\ngps soft\r\ndev hard\r\nnwlfdh soft\r\nnrlglw soft\r\nypff hard\r\nwig hard\r\njvgtfo hard\r\nzyp soft\r\ncpgbws soft\r\nxjsyjgi hard\r\nizizf hard\r\nizrwozx hard\r\nwau hard\r\ngzq hard\r\nffqa hard\r\nnajmkxn soft\r\nvqtw hard\r\nmymaoi hard\r\n", "output": "7\r\n"}, {"input": "17\r\nqojmshqd soft\r\ncwbg hard\r\nxomz soft\r\nymxfk soft\r\nusledpbg hard\r\nhaaw hard\r\nimwjce soft\r\naioff soft\r\nsumpqbzx soft\r\nzffbvrq hard\r\nqosengs soft\r\nkbori soft\r\nkxsnrkc soft\r\nwzsxh hard\r\nisibmmg soft\r\nhrfnj soft\r\nhdaavekw soft\r\n", "output": "5\r\n"}, {"input": "18\r\nzpvpfze soft\r\nsdlnere soft\r\nkwkvgz soft\r\nzla soft\r\ndxlx hard\r\nkpmnsooq soft\r\nlomen soft\r\nvywn soft\r\nwfrc hard\r\nmiash soft\r\nkrbjwpyw hard\r\ngpeksveq soft\r\njhbfqs soft\r\nkfncick hard\r\nnwkqbsv soft\r\nlywaxy soft\r\nhbxh soft\r\nbba hard\r\n", "output": "5\r\n"}, {"input": "21\r\nazjrptg hard\r\nynvyfw hard\r\ncpoe hard\r\njqbglg hard\r\nsqh hard\r\nynya hard\r\naldaolkg soft\r\ndrf hard\r\nesdsm hard\r\nfjyua hard\r\nvzlnckg hard\r\nyxjfqjd hard\r\nvkyay hard\r\nebhhke hard\r\nmsibo hard\r\nvvmkenyh hard\r\nxzk hard\r\nlggl hard\r\nvrb hard\r\niep hard\r\nrsseijey hard\r\n", "output": "7\r\n"}, {"input": "42\r\nquxukow soft\r\nwcn soft\r\npbwg soft\r\nlrp hard\r\nphdvfz soft\r\nidkvymji soft\r\nobq soft\r\nyhx soft\r\nijygw soft\r\nztzz soft\r\nuwdhnwu soft\r\ndgnuuej hard\r\nhntyyzr soft\r\nqxf hard\r\nztg soft\r\nhnpq soft\r\nuhznu soft\r\nitelgl hard\r\nggceadhw hard\r\nrxq soft\r\nkznmshem hard\r\nlri hard\r\ndalh soft\r\ngyzzuht hard\r\nzvx soft\r\nbjffln soft\r\nwnjwrvi hard\r\nxudeknru hard\r\nmql soft\r\ninoddzbf hard\r\npdg soft\r\ngtfk soft\r\nhyv soft\r\nxkv soft\r\nwajqepw soft\r\ndgc soft\r\nsefwhuoa soft\r\nbliirvj soft\r\nhqea soft\r\nped soft\r\nyjgwc soft\r\natlyha soft\r\n", "output": "8\r\n"}, {"input": "17\r\ngewvfeq soft\r\noaximz hard\r\nxkfscel soft\r\nnbxdbggw soft\r\ngxgsscq hard\r\nmqbu hard\r\nbtpzl soft\r\npsv soft\r\niov soft\r\nhliudz soft\r\nbmiu soft\r\nqqegoe hard\r\nufq soft\r\nmgx soft\r\nawjthx hard\r\nonjmhee soft\r\nxoarup soft\r\n", "output": "5\r\n"}]
| false |
stdio
| null | true |
952/E
|
952
|
E
|
PyPy 3
|
TESTS
| 2 | 124 | 0 |
74886692
|
def count(string):
x = 0
for char in string:
if char == 'm':
x += 1
return x
x = int(input())
final = ""
while x > 0:
data = input().split()
final += data[0]
x -= 1
print(count(final))
| 40 | 78 | 7,065,600 |
36840633
|
N = int(input())
# n, m = map(int, input().split())
hard = 0
soft = 0
for _ in range(N):
_, tp = input().split()
if tp == 'hard':
hard += 1
else:
soft += 1
top = max(soft, hard)
total = soft + hard
i = 1
while True:
size = i * i
val = size // 2
if (i % 2) != 0:
val += 1
if val >= top and size >= total:
print(i)
break
i += 1
|
April Fools Contest 2018
|
ICPC
| 2,018 | 1 | 256 |
Cheese Board
|
Not to be confused with chessboard.
|
The first line of input contains a single integer N (1 ≤ N ≤ 100) — the number of cheeses you have.
The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either "soft" or "hard. All cheese names are distinct.
|
Output a single number.
| null | null |
[{"input": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard", "output": "3"}, {"input": "6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard", "output": "4"}]
| 2,000 |
[]
| 40 |
[{"input": "9\r\nbrie soft\r\ncamembert soft\r\nfeta soft\r\ngoat soft\r\nmuenster soft\r\nasiago hard\r\ncheddar hard\r\ngouda hard\r\nswiss hard\r\n", "output": "3\r\n"}, {"input": "6\r\nparmesan hard\r\nemmental hard\r\nedam hard\r\ncolby hard\r\ngruyere hard\r\nasiago hard\r\n", "output": "4\r\n"}, {"input": "9\r\ngorgonzola soft\r\ncambozola soft\r\nmascarpone soft\r\nricotta soft\r\nmozzarella soft\r\nbryndza soft\r\njarlsberg soft\r\nhavarti soft\r\nstilton soft\r\n", "output": "5\r\n"}, {"input": "1\r\nprovolone hard\r\n", "output": "1\r\n"}, {"input": "4\r\nemmental hard\r\nfeta soft\r\ngoat soft\r\nroquefort hard\r\n", "output": "2\r\n"}, {"input": "1\r\ncamembert soft\r\n", "output": "1\r\n"}, {"input": "2\r\nmuenster soft\r\nasiago hard\r\n", "output": "2\r\n"}, {"input": "32\r\nauhwslzn soft\r\nkpq hard\r\neukw soft\r\nsinenrsz soft\r\najuoe soft\r\ngapj soft\r\nuyuhqv hard\r\nifldxi hard\r\npgy soft\r\njnjhh hard\r\nbyswtu soft\r\nhdr hard\r\njamqcp hard\r\nmrknxch soft\r\nghktedrf hard\r\nutley hard\r\nreinr hard\r\nvbhk hard\r\neuft soft\r\nxspriqy soft\r\ntrooa soft\r\nuylbj soft\r\nkgt soft\r\nlhc hard\r\nrwxhlux soft\r\nsuoku soft\r\ndhhoae soft\r\nlisv soft\r\nwlco hard\r\nbhmptm soft\r\nualppum soft\r\nlpxizrhr soft\r\n", "output": "7\r\n"}, {"input": "18\r\nbcvyeeap soft\r\nubb hard\r\nsrbb hard\r\nemcmg hard\r\nmelqan hard\r\nuenps soft\r\ncpyminr hard\r\ndpx soft\r\nglkj hard\r\nmsozshuy soft\r\nxnvrcozn soft\r\ntftctb soft\r\ncija hard\r\ngxl hard\r\npjoja soft\r\ndhzze hard\r\niyvl soft\r\nctrszg hard\r\n", "output": "5\r\n"}, {"input": "31\r\npevkjopz soft\r\nsmqei hard\r\nxhfmuqua soft\r\ngtmbnvn hard\r\nkdvztv soft\r\ncziuxm hard\r\ngdswd hard\r\nnawkigiz soft\r\neehdplwt hard\r\nizhivjj soft\r\ntvnkqkc hard\r\nwefwgi hard\r\nuxczrz hard\r\njdqudhgp soft\r\nhmyzqb soft\r\nwwlc soft\r\ndsax soft\r\nslefe soft\r\nahfitc hard\r\nlztbmai soft\r\nzcatg soft\r\nhwlubzmy soft\r\njkbl soft\r\nbfdfh soft\r\nzshdiuce hard\r\neobyco soft\r\nckg hard\r\nahcwzw soft\r\nvtaujlke soft\r\niwfdcik hard\r\nitb soft\r\n", "output": "7\r\n"}, {"input": "27\r\ndbilglfh hard\r\niecrbay hard\r\ncpunhmf hard\r\nszvvz soft\r\nqsbg hard\r\nvdzexx hard\r\naiuvj soft\r\nfuccez hard\r\ndvscmzd hard\r\ngps soft\r\ndev hard\r\nnwlfdh soft\r\nnrlglw soft\r\nypff hard\r\nwig hard\r\njvgtfo hard\r\nzyp soft\r\ncpgbws soft\r\nxjsyjgi hard\r\nizizf hard\r\nizrwozx hard\r\nwau hard\r\ngzq hard\r\nffqa hard\r\nnajmkxn soft\r\nvqtw hard\r\nmymaoi hard\r\n", "output": "7\r\n"}, {"input": "17\r\nqojmshqd soft\r\ncwbg hard\r\nxomz soft\r\nymxfk soft\r\nusledpbg hard\r\nhaaw hard\r\nimwjce soft\r\naioff soft\r\nsumpqbzx soft\r\nzffbvrq hard\r\nqosengs soft\r\nkbori soft\r\nkxsnrkc soft\r\nwzsxh hard\r\nisibmmg soft\r\nhrfnj soft\r\nhdaavekw soft\r\n", "output": "5\r\n"}, {"input": "18\r\nzpvpfze soft\r\nsdlnere soft\r\nkwkvgz soft\r\nzla soft\r\ndxlx hard\r\nkpmnsooq soft\r\nlomen soft\r\nvywn soft\r\nwfrc hard\r\nmiash soft\r\nkrbjwpyw hard\r\ngpeksveq soft\r\njhbfqs soft\r\nkfncick hard\r\nnwkqbsv soft\r\nlywaxy soft\r\nhbxh soft\r\nbba hard\r\n", "output": "5\r\n"}, {"input": "21\r\nazjrptg hard\r\nynvyfw hard\r\ncpoe hard\r\njqbglg hard\r\nsqh hard\r\nynya hard\r\naldaolkg soft\r\ndrf hard\r\nesdsm hard\r\nfjyua hard\r\nvzlnckg hard\r\nyxjfqjd hard\r\nvkyay hard\r\nebhhke hard\r\nmsibo hard\r\nvvmkenyh hard\r\nxzk hard\r\nlggl hard\r\nvrb hard\r\niep hard\r\nrsseijey hard\r\n", "output": "7\r\n"}, {"input": "42\r\nquxukow soft\r\nwcn soft\r\npbwg soft\r\nlrp hard\r\nphdvfz soft\r\nidkvymji soft\r\nobq soft\r\nyhx soft\r\nijygw soft\r\nztzz soft\r\nuwdhnwu soft\r\ndgnuuej hard\r\nhntyyzr soft\r\nqxf hard\r\nztg soft\r\nhnpq soft\r\nuhznu soft\r\nitelgl hard\r\nggceadhw hard\r\nrxq soft\r\nkznmshem hard\r\nlri hard\r\ndalh soft\r\ngyzzuht hard\r\nzvx soft\r\nbjffln soft\r\nwnjwrvi hard\r\nxudeknru hard\r\nmql soft\r\ninoddzbf hard\r\npdg soft\r\ngtfk soft\r\nhyv soft\r\nxkv soft\r\nwajqepw soft\r\ndgc soft\r\nsefwhuoa soft\r\nbliirvj soft\r\nhqea soft\r\nped soft\r\nyjgwc soft\r\natlyha soft\r\n", "output": "8\r\n"}, {"input": "17\r\ngewvfeq soft\r\noaximz hard\r\nxkfscel soft\r\nnbxdbggw soft\r\ngxgsscq hard\r\nmqbu hard\r\nbtpzl soft\r\npsv soft\r\niov soft\r\nhliudz soft\r\nbmiu soft\r\nqqegoe hard\r\nufq soft\r\nmgx soft\r\nawjthx hard\r\nonjmhee soft\r\nxoarup soft\r\n", "output": "5\r\n"}]
| false |
stdio
| null | true |
326/A
|
335
|
A
|
Python 3
|
TESTS
| 10 | 92 | 0 |
164601029
|
s = input()
n = int(input())
import math
my_dict = {}
for l in s:
if l in my_dict:
my_dict[l] += 1
else:
my_dict[l] = 1
for i in range(1, 1000):
good = 0
for key, num in my_dict.items():
good += math.ceil(num/i)
if good <= n:
print(i)
s = ''
for key, num in my_dict.items():
s += key * math.ceil(num/i)
print(s)
break
else:
print(-1)
| 55 | 154 | 2,150,400 |
170897071
|
import sys
import math
import collections
import random
from heapq import heappush, heappop
from functools import reduce
input = sys.stdin.readline
ints = lambda: list(map(int, input().split()))
s = input().strip()
n = int(input())
occ = collections.defaultdict(int)
for c in s:
occ[c] += 1
def works(x):
ans = ""
for key in occ.keys():
copies = math.ceil(occ[key] / x)
ans += key * copies
return ans
l, r = 1, 1001
while l <= r:
m = (l + r) // 2
st = works(m)
if len(st) <= n:
r = m - 1
else:
l = m + 1
if l == 1002:
print("-1")
else:
print(l)
ans = works(l)
while len(ans) != n:
ans += "a"
print(ans)
|
MemSQL start[c]up Round 2
|
CF
| 2,013 | 2 | 256 |
Banana
|
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
|
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
| null |
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
|
[{"input": "banana\n4", "output": "2\nbaan"}, {"input": "banana\n3", "output": "3\nnab"}, {"input": "banana\n2", "output": "-1"}]
| 1,400 |
[]
| 55 |
[{"input": "banana\r\n4\r\n", "output": "2\r\nbaan\r\n"}, {"input": "banana\r\n3\r\n", "output": "3\r\nnab\r\n"}, {"input": "banana\r\n2\r\n", "output": "-1\r\n"}, {"input": "p\r\n1000\r\n", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "b\r\n1\r\n", "output": "1\r\nb\r\n"}, {"input": "aba\r\n2\r\n", "output": "2\r\nab\r\n"}, {"input": "aaa\r\n2\r\n", "output": "2\r\naa\r\n"}, {"input": "aa\r\n3\r\n", "output": "1\r\naaa\r\n"}, {"input": "aaaaaaaabbbbbccccccccccccccccccccccccccccccc\r\n7\r\n", "output": "8\r\nabcccca\r\n"}, {"input": "aaaaa\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "baba\r\n3\r\n", "output": "2\r\naba\r\n"}, {"input": "a\r\n1000\r\n", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "aan\r\n5\r\n", "output": "1\r\naanaa\r\n"}, {"input": "banana\r\n5\r\n", "output": "2\r\naabna\r\n"}, {"input": "a\r\n5\r\n", "output": "1\r\naaaaa\r\n"}, {"input": "aaaaaaa\r\n5\r\n", "output": "2\r\naaaaa\r\n"}, {"input": "abc\r\n100\r\n", "output": "1\r\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "zzz\r\n4\r\n", "output": "1\r\nzzza\r\n"}, {"input": "aaabbb\r\n3\r\n", "output": "3\r\naba\r\n"}, {"input": "abc\r\n5\r\n", "output": "1\r\nabcaa\r\n"}, {"input": "abc\r\n10\r\n", "output": "1\r\nabcaaaaaaa\r\n"}, {"input": "aaaaa\r\n100\r\n", "output": "1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abc\r\n1000\r\n", "output": "1\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "a\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "bbbbb\r\n6\r\n", "output": "1\r\nbbbbba\r\n"}, {"input": "bnana\r\n4\r\n", "output": "2\r\nabna\r\n"}, {"input": "aaaaaaabbbbbbb\r\n3\r\n", "output": "7\r\naba\r\n"}, {"input": "aabbbcccc\r\n7\r\n", "output": "2\r\nabbccaa\r\n"}, {"input": "aaa\r\n9\r\n", "output": "1\r\naaaaaaaaa\r\n"}, {"input": "a\r\n2\r\n", "output": "1\r\naa\r\n"}, {"input": "cccbba\r\n10\r\n", "output": "1\r\nabbcccaaaa\r\n"}, {"input": "a\r\n4\r\n", "output": "1\r\naaaa\r\n"}]
| false |
stdio
|
import sys
from math import ceil
from collections import defaultdict
def main(input_path, output_path, submission_path):
with open(input_path) as f:
s = f.readline().strip()
n = int(f.readline().strip())
s_counts = defaultdict(int)
for c in s:
s_counts[c] += 1
def is_possible(k):
if k == 0:
return False
total = 0
for c in s_counts:
cnt = s_counts[c]
req = (cnt + k - 1) // k
total += req
if total > n:
return False
return total <= n
possible = False
k_min = None
unique_chars = len(s_counts)
if unique_chars > n:
possible = False
else:
left, right = 1, max(s_counts.values()) if s_counts else 0
if is_possible(1):
possible, k_min = True, 1
else:
right = 1
while not is_possible(right):
right *= 2
left = right // 2
while left < right:
mid = (left + right) // 2
if is_possible(mid):
right = mid
else:
left = mid + 1
possible = is_possible(left)
k_min = left if possible else None
with open(submission_path) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if possible:
if len(lines) != 2:
print(0)
return
try:
k_submitted = int(lines[0])
sheet = lines[1]
except:
print(0)
return
if len(sheet) != n or not sheet.islower():
print(0)
return
sheet_chars = set(sheet)
if not set(s_counts.keys()).issubset(sheet_chars):
print(0)
return
sheet_counts = defaultdict(int)
for c in sheet:
sheet_counts[c] += 1
if sum(sheet_counts.values()) != n:
print(0)
return
if k_submitted != k_min:
print(0)
return
valid = True
for c in s_counts:
required = (s_counts[c] + k_submitted - 1) // k_submitted
if sheet_counts[c] < required:
valid = False
break
print(100 if valid else 0)
else:
if len(lines) != 1 or lines[0] != '-1':
print(0)
else:
print(100)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1:4]
main(input_path, output_path, submission_path)
| true |
53/D
|
53
|
D
|
PyPy 3
|
TESTS
| 0 | 216 | 0 |
63969774
|
n = int(input())
*a, = map(int, input().split())
*b, = map(int, input().split())
ans, s, k = [], 0, 0
while a:
x = a.index(b[k])
a.remove(b[k])
ans.append((x + k + 1, k + 1))
s += x
k += 1
print(s)
for i in ans:
for j in range(i[0], i[1], -1):
print(j, j - 1)
| 30 | 186 | 8,499,200 |
208562183
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = []
i1 = -1
for i in range(n):
if a[i] == b[i]:
continue
else:
for j in range(i+1, n):
if a[i] == b[j]:
i1 = j
break
# print('i->', i, ' i1->', i1)
for j in range(i1, i, -1):
# print('j->', j)
l.append(j)
l.append(j+1)
b[j], b[j-1] = b[j-1], b[j]
print(len(l)//2)
for i in range(0, len(l)-1, 2):
print(l[i], l[i+1])
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011 | 2 | 256 |
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500 |
["sortings"]
| 30 |
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false |
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true |
952/E
|
952
|
E
|
Python 3
|
TESTS
| 2 | 108 | 0 |
75699460
|
n = int(input())
ans = 0
for _ in range(n):
name = input().split()[0]
for c in name:
if c=='m':
ans += 1
print(ans)
| 40 | 78 | 7,065,600 |
36845632
|
N = int(input())
w, b = 0, 0
for n in range(N):
cheese, color = input().split()
if color == 'hard':
b += 1
else:
w += 1
for k in range(1, 16):
if b <= k**2 // 2 and w <= k**2 // 2 or \
k % 2 == 1 and max(b, w) <= (k**2 + 1) // 2 and b + w <= k**2:
print(k)
break
|
April Fools Contest 2018
|
ICPC
| 2,018 | 1 | 256 |
Cheese Board
|
Not to be confused with chessboard.
|
The first line of input contains a single integer N (1 ≤ N ≤ 100) — the number of cheeses you have.
The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either "soft" or "hard. All cheese names are distinct.
|
Output a single number.
| null | null |
[{"input": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard", "output": "3"}, {"input": "6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard", "output": "4"}]
| 2,000 |
[]
| 40 |
[{"input": "9\r\nbrie soft\r\ncamembert soft\r\nfeta soft\r\ngoat soft\r\nmuenster soft\r\nasiago hard\r\ncheddar hard\r\ngouda hard\r\nswiss hard\r\n", "output": "3\r\n"}, {"input": "6\r\nparmesan hard\r\nemmental hard\r\nedam hard\r\ncolby hard\r\ngruyere hard\r\nasiago hard\r\n", "output": "4\r\n"}, {"input": "9\r\ngorgonzola soft\r\ncambozola soft\r\nmascarpone soft\r\nricotta soft\r\nmozzarella soft\r\nbryndza soft\r\njarlsberg soft\r\nhavarti soft\r\nstilton soft\r\n", "output": "5\r\n"}, {"input": "1\r\nprovolone hard\r\n", "output": "1\r\n"}, {"input": "4\r\nemmental hard\r\nfeta soft\r\ngoat soft\r\nroquefort hard\r\n", "output": "2\r\n"}, {"input": "1\r\ncamembert soft\r\n", "output": "1\r\n"}, {"input": "2\r\nmuenster soft\r\nasiago hard\r\n", "output": "2\r\n"}, {"input": "32\r\nauhwslzn soft\r\nkpq hard\r\neukw soft\r\nsinenrsz soft\r\najuoe soft\r\ngapj soft\r\nuyuhqv hard\r\nifldxi hard\r\npgy soft\r\njnjhh hard\r\nbyswtu soft\r\nhdr hard\r\njamqcp hard\r\nmrknxch soft\r\nghktedrf hard\r\nutley hard\r\nreinr hard\r\nvbhk hard\r\neuft soft\r\nxspriqy soft\r\ntrooa soft\r\nuylbj soft\r\nkgt soft\r\nlhc hard\r\nrwxhlux soft\r\nsuoku soft\r\ndhhoae soft\r\nlisv soft\r\nwlco hard\r\nbhmptm soft\r\nualppum soft\r\nlpxizrhr soft\r\n", "output": "7\r\n"}, {"input": "18\r\nbcvyeeap soft\r\nubb hard\r\nsrbb hard\r\nemcmg hard\r\nmelqan hard\r\nuenps soft\r\ncpyminr hard\r\ndpx soft\r\nglkj hard\r\nmsozshuy soft\r\nxnvrcozn soft\r\ntftctb soft\r\ncija hard\r\ngxl hard\r\npjoja soft\r\ndhzze hard\r\niyvl soft\r\nctrszg hard\r\n", "output": "5\r\n"}, {"input": "31\r\npevkjopz soft\r\nsmqei hard\r\nxhfmuqua soft\r\ngtmbnvn hard\r\nkdvztv soft\r\ncziuxm hard\r\ngdswd hard\r\nnawkigiz soft\r\neehdplwt hard\r\nizhivjj soft\r\ntvnkqkc hard\r\nwefwgi hard\r\nuxczrz hard\r\njdqudhgp soft\r\nhmyzqb soft\r\nwwlc soft\r\ndsax soft\r\nslefe soft\r\nahfitc hard\r\nlztbmai soft\r\nzcatg soft\r\nhwlubzmy soft\r\njkbl soft\r\nbfdfh soft\r\nzshdiuce hard\r\neobyco soft\r\nckg hard\r\nahcwzw soft\r\nvtaujlke soft\r\niwfdcik hard\r\nitb soft\r\n", "output": "7\r\n"}, {"input": "27\r\ndbilglfh hard\r\niecrbay hard\r\ncpunhmf hard\r\nszvvz soft\r\nqsbg hard\r\nvdzexx hard\r\naiuvj soft\r\nfuccez hard\r\ndvscmzd hard\r\ngps soft\r\ndev hard\r\nnwlfdh soft\r\nnrlglw soft\r\nypff hard\r\nwig hard\r\njvgtfo hard\r\nzyp soft\r\ncpgbws soft\r\nxjsyjgi hard\r\nizizf hard\r\nizrwozx hard\r\nwau hard\r\ngzq hard\r\nffqa hard\r\nnajmkxn soft\r\nvqtw hard\r\nmymaoi hard\r\n", "output": "7\r\n"}, {"input": "17\r\nqojmshqd soft\r\ncwbg hard\r\nxomz soft\r\nymxfk soft\r\nusledpbg hard\r\nhaaw hard\r\nimwjce soft\r\naioff soft\r\nsumpqbzx soft\r\nzffbvrq hard\r\nqosengs soft\r\nkbori soft\r\nkxsnrkc soft\r\nwzsxh hard\r\nisibmmg soft\r\nhrfnj soft\r\nhdaavekw soft\r\n", "output": "5\r\n"}, {"input": "18\r\nzpvpfze soft\r\nsdlnere soft\r\nkwkvgz soft\r\nzla soft\r\ndxlx hard\r\nkpmnsooq soft\r\nlomen soft\r\nvywn soft\r\nwfrc hard\r\nmiash soft\r\nkrbjwpyw hard\r\ngpeksveq soft\r\njhbfqs soft\r\nkfncick hard\r\nnwkqbsv soft\r\nlywaxy soft\r\nhbxh soft\r\nbba hard\r\n", "output": "5\r\n"}, {"input": "21\r\nazjrptg hard\r\nynvyfw hard\r\ncpoe hard\r\njqbglg hard\r\nsqh hard\r\nynya hard\r\naldaolkg soft\r\ndrf hard\r\nesdsm hard\r\nfjyua hard\r\nvzlnckg hard\r\nyxjfqjd hard\r\nvkyay hard\r\nebhhke hard\r\nmsibo hard\r\nvvmkenyh hard\r\nxzk hard\r\nlggl hard\r\nvrb hard\r\niep hard\r\nrsseijey hard\r\n", "output": "7\r\n"}, {"input": "42\r\nquxukow soft\r\nwcn soft\r\npbwg soft\r\nlrp hard\r\nphdvfz soft\r\nidkvymji soft\r\nobq soft\r\nyhx soft\r\nijygw soft\r\nztzz soft\r\nuwdhnwu soft\r\ndgnuuej hard\r\nhntyyzr soft\r\nqxf hard\r\nztg soft\r\nhnpq soft\r\nuhznu soft\r\nitelgl hard\r\nggceadhw hard\r\nrxq soft\r\nkznmshem hard\r\nlri hard\r\ndalh soft\r\ngyzzuht hard\r\nzvx soft\r\nbjffln soft\r\nwnjwrvi hard\r\nxudeknru hard\r\nmql soft\r\ninoddzbf hard\r\npdg soft\r\ngtfk soft\r\nhyv soft\r\nxkv soft\r\nwajqepw soft\r\ndgc soft\r\nsefwhuoa soft\r\nbliirvj soft\r\nhqea soft\r\nped soft\r\nyjgwc soft\r\natlyha soft\r\n", "output": "8\r\n"}, {"input": "17\r\ngewvfeq soft\r\noaximz hard\r\nxkfscel soft\r\nnbxdbggw soft\r\ngxgsscq hard\r\nmqbu hard\r\nbtpzl soft\r\npsv soft\r\niov soft\r\nhliudz soft\r\nbmiu soft\r\nqqegoe hard\r\nufq soft\r\nmgx soft\r\nawjthx hard\r\nonjmhee soft\r\nxoarup soft\r\n", "output": "5\r\n"}]
| false |
stdio
| null | true |
792/F
|
792
|
F
|
Python 3
|
TESTS
| 34 | 966 | 5,529,600 |
26344675
|
def check_battle(t, h, m, spell_1, spell_2):
x1, y1 = spell_1
x2, y2 = spell_2
if y1 * h <= x1 * m and h <= x1 * t:
return True
if y2 * h <= x2 * m and h <= x2 * t:
return True
return False
def main():
q, m = map(int, input().split())
spell_1 = None
spell_2 = None
j = 0
for i in range(q):
kind, a, b = map(int, input().split())
a = (a + j) % int(1e6) + 1
b = (b + j) % int(1e6) + 1
if kind == 1:
x, y = a, b
if spell_1 is None and spell_2 is None:
spell_1 = spell_2 = (x, y)
else:
x1, y1 = spell_1
if x > x1:
spell_1 = (x, y)
x2, y2 = spell_2
if x * y2 > x2 * y:
spell_2 = (x, y)
else:
t, h = a, b
if spell_1 is None and spell_2 is None:
print("NO")
else:
result = check_battle(t, h, m, spell_1, spell_2)
if result:
print("YES")
j = i + 1
else:
print("NO")
if __name__ == '__main__':
# import sys
# sys.stdin = open("F.txt")
main()
| 36 | 873 | 15,564,800 |
37535361
|
#!/usr/bin/env python3
# solution after hint
# (instead of best hit/mana spell store convex hull of spells)
# O(n^2) instead of O(n log n)
[q, m] = map(int, input().strip().split())
qis = [tuple(map(int, input().strip().split())) for _ in range(q)]
mod = 10**6
j = 0
spell_chull = [(0, 0)] # lower hull _/
def is_right(xy0, xy1, xy):
(x0, y0) = xy0
(x1, y1) = xy1
(x, y) = xy
return (x0 - x) * (y1 - y) >= (x1 - x) * (y0 - y)
def in_chull(x, y):
i = 0
if x > spell_chull[-1][0]:
return False
while spell_chull[i][0] < x:
i += 1
if spell_chull[i][0] == x:
return spell_chull[i][1] <= y
else:
return is_right(spell_chull[i - 1], spell_chull[i], (x, y))
def add_spell(x, y):
global spell_chull
if in_chull(x, y):
return
i_left = 0
while i_left < len(spell_chull) - 1 and not is_right(spell_chull[i_left + 1], spell_chull[i_left], (x, y)):
i_left += 1
i_right = i_left + 1
while i_right < len(spell_chull) - 1 and is_right(spell_chull[i_right + 1], spell_chull[i_right], (x, y)):
i_right += 1
if i_right == len(spell_chull) - 1 and x >= spell_chull[-1][0]:
i_right += 1
spell_chull = spell_chull[:i_left + 1] + [(x, y)] + spell_chull[i_right:]
for i, qi in enumerate(qis):
(k, a, b) = qi
x = (a + j) % mod + 1
y = (b + j) % mod + 1
if k == 1:
add_spell(x, y)
else: #2
if in_chull(y / x, m / x):
print ('YES')
j = i + 1
else:
print ('NO')
|
Educational Codeforces Round 18
|
ICPC
| 2,017 | 2 | 256 |
Mages and Monsters
|
Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells.
Vova's character can learn new spells during the game. Every spell is characterized by two values xi and yi — damage per second and mana cost per second, respectively. Vova doesn't have to use a spell for an integer amount of seconds. More formally, if he uses a spell with damage x and mana cost y for z seconds, then he will deal x·z damage and spend y·z mana (no rounding). If there is no mana left (mana amount is set in the start of the game and it remains the same at the beginning of every fight), then character won't be able to use any spells. It is prohibited to use multiple spells simultaneously.
Also Vova can fight monsters. Every monster is characterized by two values tj and hj — monster kills Vova's character in tj seconds and has hj health points. Mana refills after every fight (or Vova's character revives with full mana reserve), so previous fights have no influence on further ones.
Vova's character kills a monster, if he deals hj damage to it in no more than tj seconds using his spells (it is allowed to use more than one spell in a fight) and spending no more mana than he had at the beginning of the fight. If monster's health becomes zero exactly in tj seconds (it means that the monster and Vova's character kill each other at the same time), then Vova wins the fight.
You have to write a program which can answer two types of queries:
- 1 x y — Vova's character learns new spell which deals x damage per second and costs y mana per second.
- 2 t h — Vova fights the monster which kills his character in t seconds and has h health points.
Note that queries are given in a different form. Also remember that Vova's character knows no spells at the beginning of the game.
For every query of second type you have to determine if Vova is able to win the fight with corresponding monster.
|
The first line contains two integer numbers q and m (2 ≤ q ≤ 105, 1 ≤ m ≤ 1012) — the number of queries and the amount of mana at the beginning of every fight.
i-th of each next q lines contains three numbers ki, ai and bi (1 ≤ ki ≤ 2, 1 ≤ ai, bi ≤ 106).
Using them you can restore queries this way: let j be the index of the last query of second type with positive answer (j = 0 if there were none of these).
- If ki = 1, then character learns spell with x = (ai + j) mod 106 + 1, y = (bi + j) mod 106 + 1.
- If ki = 2, then you have to determine if Vova is able to win the fight against monster with t = (ai + j) mod 106 + 1, h = (bi + j) mod 106 + 1.
|
For every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise.
| null |
In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with only 100 mana.
|
[{"input": "3 100\n1 4 9\n2 19 49\n2 19 49", "output": "YES\nNO"}]
| 3,100 |
["data structures", "geometry"]
| 36 |
[{"input": "3 100\r\n1 4 9\r\n2 19 49\r\n2 19 49\r\n", "output": "YES\r\nNO\r\n"}, {"input": "10 442006988299\r\n2 10 47\r\n1 9 83\r\n1 15 24\r\n2 19 47\r\n2 75 99\r\n2 85 23\r\n2 8 33\r\n2 9 82\r\n1 86 49\r\n2 71 49\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "2 424978864039\r\n2 7 3\r\n2 10 8\r\n", "output": "NO\r\nNO\r\n"}, {"input": "2 1000000000000\r\n1 235726 255577\r\n1 959304 206090\r\n", "output": ""}, {"input": "3 10\r\n1 1 1\r\n2 1 1\r\n2 999999 999999\r\n", "output": "YES\r\nYES\r\n"}, {"input": "12 100\r\n1 8 8\r\n2 200 101\r\n2 10 99\r\n1 9 9\r\n2 10 99\r\n2 200 101\r\n1 14 4\r\n2 194 195\r\n2 194 194\r\n2 990 290\r\n2 999991 11\r\n2 999991 10\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "15 100\r\n1 8 8\r\n2 200 101\r\n2 10 99\r\n1 9 9\r\n2 10 99\r\n2 200 101\r\n1 14 4\r\n2 194 195\r\n2 194 194\r\n2 990 290\r\n1 2 999992\r\n2 6 256\r\n2 7 256\r\n1 2 999988\r\n2 2 252\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3 12\r\n1 99 9\r\n1 49 1\r\n2 1 149\r\n", "output": "YES\r\n"}]
| false |
stdio
| null | true |
53/D
|
53
|
D
|
PyPy 3
|
TESTS
| 0 | 216 | 0 |
46990074
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = 0
swap = []
for i in range(n - 1, -1, -1):
if b[i] != a[i]:
num += 1
right = a[i]
ind = b.index(right)
s = b[i]
b[i] = a[i]
b[ind] = s
li = [str(ind + 1),str(i + 1)]
swap.append(li)
print(num)
if num != 0:
for i in range(num):
print(' '.join(swap[i]))
| 30 | 216 | 8,908,800 |
177776370
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = []
for i in range(n):
x = b.index(a[i])
for j in range(x+i, i, -1):
d.append((j, j+1))
b = b[:x] + b[x+1:]
print(len(d))
for i in d:
print(*i)
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011 | 2 | 256 |
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500 |
["sortings"]
| 30 |
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false |
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true |
326/A
|
335
|
A
|
Python 3
|
TESTS
| 10 | 92 | 102,400 |
168198338
|
from collections import Counter
from math import ceil
# one list = many stickers
s = input()
n = int(input())
counter_str = Counter(s)
def get_sticker_len(list_num, counter: dict):
alpha_per_sticker = [ceil(count / list_num) for alpha, count in counter.items()]
sticker_len = sum(alpha_per_sticker)
return sticker_len
def get_sticker(list_num, counter):
alphas = [alpha * ceil(count / list_num) for alpha, count in counter.items()]
sticker = ''.join(alphas)
return sticker
def is_sticker_with_cur_list_num_fitted(list_num):
true_len = get_sticker_len(list_num, counter_str)
# print(list_num, 'len', true_len)
return int(true_len <= n)
def bin_search(f, l, r):
while l + 1 < r:
mid = (l + r) // 2
# print(mid, f(mid))
if f(mid) == 0:
l = mid
else:
r = mid
return r
ans = bin_search(is_sticker_with_cur_list_num_fitted, 0, len(s)+1)
if ans == len(s)+1:
print(-1)
else:
print(ans)
print(get_sticker(ans, counter_str))
| 55 | 186 | 0 |
197610547
|
s = input()
n = int(input())
d = {}
r = 0
for a in s:
d.setdefault(a, 0)
d[a] += 1
if(d[a] > r):
r = d[a]
if (len(d) > n):
print(-1)
else:
l = 0
while r - l > 1:
k = (l + r) // 2
cur = 0
for x in d.values():
cur += (x+k-1) // k
if cur > n:
l = k
else:
r = k
print(r)
s = ''
for a in d.keys():
s += a * ((d[a] + r - 1) // r)
l=len(s)
s += 'a' * (n-len(s))
print(s)
|
MemSQL start[c]up Round 2
|
CF
| 2,013 | 2 | 256 |
Banana
|
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
|
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
| null |
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
|
[{"input": "banana\n4", "output": "2\nbaan"}, {"input": "banana\n3", "output": "3\nnab"}, {"input": "banana\n2", "output": "-1"}]
| 1,400 |
[]
| 55 |
[{"input": "banana\r\n4\r\n", "output": "2\r\nbaan\r\n"}, {"input": "banana\r\n3\r\n", "output": "3\r\nnab\r\n"}, {"input": "banana\r\n2\r\n", "output": "-1\r\n"}, {"input": "p\r\n1000\r\n", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "b\r\n1\r\n", "output": "1\r\nb\r\n"}, {"input": "aba\r\n2\r\n", "output": "2\r\nab\r\n"}, {"input": "aaa\r\n2\r\n", "output": "2\r\naa\r\n"}, {"input": "aa\r\n3\r\n", "output": "1\r\naaa\r\n"}, {"input": "aaaaaaaabbbbbccccccccccccccccccccccccccccccc\r\n7\r\n", "output": "8\r\nabcccca\r\n"}, {"input": "aaaaa\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "baba\r\n3\r\n", "output": "2\r\naba\r\n"}, {"input": "a\r\n1000\r\n", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "aan\r\n5\r\n", "output": "1\r\naanaa\r\n"}, {"input": "banana\r\n5\r\n", "output": "2\r\naabna\r\n"}, {"input": "a\r\n5\r\n", "output": "1\r\naaaaa\r\n"}, {"input": "aaaaaaa\r\n5\r\n", "output": "2\r\naaaaa\r\n"}, {"input": "abc\r\n100\r\n", "output": "1\r\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "zzz\r\n4\r\n", "output": "1\r\nzzza\r\n"}, {"input": "aaabbb\r\n3\r\n", "output": "3\r\naba\r\n"}, {"input": "abc\r\n5\r\n", "output": "1\r\nabcaa\r\n"}, {"input": "abc\r\n10\r\n", "output": "1\r\nabcaaaaaaa\r\n"}, {"input": "aaaaa\r\n100\r\n", "output": "1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abc\r\n1000\r\n", "output": "1\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "a\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "bbbbb\r\n6\r\n", "output": "1\r\nbbbbba\r\n"}, {"input": "bnana\r\n4\r\n", "output": "2\r\nabna\r\n"}, {"input": "aaaaaaabbbbbbb\r\n3\r\n", "output": "7\r\naba\r\n"}, {"input": "aabbbcccc\r\n7\r\n", "output": "2\r\nabbccaa\r\n"}, {"input": "aaa\r\n9\r\n", "output": "1\r\naaaaaaaaa\r\n"}, {"input": "a\r\n2\r\n", "output": "1\r\naa\r\n"}, {"input": "cccbba\r\n10\r\n", "output": "1\r\nabbcccaaaa\r\n"}, {"input": "a\r\n4\r\n", "output": "1\r\naaaa\r\n"}]
| false |
stdio
|
import sys
from math import ceil
from collections import defaultdict
def main(input_path, output_path, submission_path):
with open(input_path) as f:
s = f.readline().strip()
n = int(f.readline().strip())
s_counts = defaultdict(int)
for c in s:
s_counts[c] += 1
def is_possible(k):
if k == 0:
return False
total = 0
for c in s_counts:
cnt = s_counts[c]
req = (cnt + k - 1) // k
total += req
if total > n:
return False
return total <= n
possible = False
k_min = None
unique_chars = len(s_counts)
if unique_chars > n:
possible = False
else:
left, right = 1, max(s_counts.values()) if s_counts else 0
if is_possible(1):
possible, k_min = True, 1
else:
right = 1
while not is_possible(right):
right *= 2
left = right // 2
while left < right:
mid = (left + right) // 2
if is_possible(mid):
right = mid
else:
left = mid + 1
possible = is_possible(left)
k_min = left if possible else None
with open(submission_path) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if possible:
if len(lines) != 2:
print(0)
return
try:
k_submitted = int(lines[0])
sheet = lines[1]
except:
print(0)
return
if len(sheet) != n or not sheet.islower():
print(0)
return
sheet_chars = set(sheet)
if not set(s_counts.keys()).issubset(sheet_chars):
print(0)
return
sheet_counts = defaultdict(int)
for c in sheet:
sheet_counts[c] += 1
if sum(sheet_counts.values()) != n:
print(0)
return
if k_submitted != k_min:
print(0)
return
valid = True
for c in s_counts:
required = (s_counts[c] + k_submitted - 1) // k_submitted
if sheet_counts[c] < required:
valid = False
break
print(100 if valid else 0)
else:
if len(lines) != 1 or lines[0] != '-1':
print(0)
else:
print(100)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1:4]
main(input_path, output_path, submission_path)
| true |
785/A
|
785
|
A
|
Python 3
|
TESTS
| 1 | 30 | 0 |
230913379
|
n=int(input())
s=[]
b=0
for i in range(n):
a=input()
s.append(a)
if "Icosahedron" in s:
b=b+20
if "Cube" in s:
b=b+6
if "Tetrahedron" in s:
b=b+4
if "Dodecahedron" in s:
b=b+12
if "Octahedron" in s:
b=b+8
print(b)
| 30 | 78 | 0 |
219701900
|
from sys import stdin
input()
count =0
s = {"T":4, "C":6, "O":8, "D":12, "I":20}
for x in stdin:
count += s[x[0]]
print(count)
|
Codeforces Round 404 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Anton and Polyhedrons
|
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:
- Tetrahedron. Tetrahedron has 4 triangular faces.
- Cube. Cube has 6 square faces.
- Octahedron. Octahedron has 8 triangular faces.
- Dodecahedron. Dodecahedron has 12 pentagonal faces.
- Icosahedron. Icosahedron has 20 triangular faces.
All five kinds of polyhedrons are shown on the picture below:
Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection.
Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:
- "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.
- "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.
- "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.
- "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.
- "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
|
Output one number — the total number of faces in all the polyhedrons in Anton's collection.
| null |
In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.
|
[{"input": "4\nIcosahedron\nCube\nTetrahedron\nDodecahedron", "output": "42"}, {"input": "3\nDodecahedron\nOctahedron\nOctahedron", "output": "28"}]
| 800 |
["implementation", "strings"]
| 30 |
[{"input": "4\r\nIcosahedron\r\nCube\r\nTetrahedron\r\nDodecahedron\r\n", "output": "42\r\n"}, {"input": "3\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\n", "output": "28\r\n"}, {"input": "25\r\nIcosahedron\r\nOctahedron\r\nTetrahedron\r\nDodecahedron\r\nCube\r\nIcosahedron\r\nOctahedron\r\nCube\r\nTetrahedron\r\nIcosahedron\r\nIcosahedron\r\nTetrahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nOctahedron\r\nIcosahedron\r\nTetrahedron\r\nDodecahedron\r\nTetrahedron\r\nOctahedron\r\nCube\r\nCube\r\nDodecahedron\r\nTetrahedron\r\n", "output": "256\r\n"}, {"input": "1\r\nTetrahedron\r\n", "output": "4\r\n"}, {"input": "1\r\nCube\r\n", "output": "6\r\n"}, {"input": "1\r\nOctahedron\r\n", "output": "8\r\n"}, {"input": "1\r\nDodecahedron\r\n", "output": "12\r\n"}, {"input": "1\r\nIcosahedron\r\n", "output": "20\r\n"}, {"input": "28\r\nOctahedron\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nCube\r\nDodecahedron\r\nCube\r\nTetrahedron\r\nCube\r\nCube\r\nTetrahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\n", "output": "340\r\n"}]
| false |
stdio
| null | true |
326/A
|
335
|
A
|
PyPy 3
|
TESTS
| 10 | 154 | 20,172,800 |
130019868
|
def check(x):
cnt=0
for i in range(26):
cnt+=(ltrs[i] + x - 1) // x
if(cnt > n):
return False
return True
a = input()
ltrs = [0] * 26
for i in a:
ltrs[ord(i) - ord('a')]+=1
n = int(input())
s = 0
for j in ltrs:
s+=int(j != 0)
if(s > n):
print(-1)
exit(0)
l = 0
r = len(a)
check(2)
while(l < r - 1):
mid = (l + r) // 2
if(check(mid)):
r = mid
else:
l = mid
print(r)
p = []
for i in range(26):
p+=chr(i + ord('a')) * ((ltrs[i] + r - 1) // r)
print(*p, sep = '')
| 55 | 218 | 0 |
41984211
|
s = input()
n = int(input())
d = {}
for c in s:
if c not in d: d[c] = 0
d[c] += 1
dcl = d.values()
found = False
for x in range(1,1001):
if sum([(dc-1)//x+1 for dc in dcl]) <= n:
found = True
print(x)
s = ''.join([key*((d[key]-1)//x+1) for key in sorted(d.keys())])
s += (n-len(s))*'a'
print(s)
break
if not found:
print(-1)
|
MemSQL start[c]up Round 2
|
CF
| 2,013 | 2 | 256 |
Banana
|
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
|
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
| null |
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
|
[{"input": "banana\n4", "output": "2\nbaan"}, {"input": "banana\n3", "output": "3\nnab"}, {"input": "banana\n2", "output": "-1"}]
| 1,400 |
[]
| 55 |
[{"input": "banana\r\n4\r\n", "output": "2\r\nbaan\r\n"}, {"input": "banana\r\n3\r\n", "output": "3\r\nnab\r\n"}, {"input": "banana\r\n2\r\n", "output": "-1\r\n"}, {"input": "p\r\n1000\r\n", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "b\r\n1\r\n", "output": "1\r\nb\r\n"}, {"input": "aba\r\n2\r\n", "output": "2\r\nab\r\n"}, {"input": "aaa\r\n2\r\n", "output": "2\r\naa\r\n"}, {"input": "aa\r\n3\r\n", "output": "1\r\naaa\r\n"}, {"input": "aaaaaaaabbbbbccccccccccccccccccccccccccccccc\r\n7\r\n", "output": "8\r\nabcccca\r\n"}, {"input": "aaaaa\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "baba\r\n3\r\n", "output": "2\r\naba\r\n"}, {"input": "a\r\n1000\r\n", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "aan\r\n5\r\n", "output": "1\r\naanaa\r\n"}, {"input": "banana\r\n5\r\n", "output": "2\r\naabna\r\n"}, {"input": "a\r\n5\r\n", "output": "1\r\naaaaa\r\n"}, {"input": "aaaaaaa\r\n5\r\n", "output": "2\r\naaaaa\r\n"}, {"input": "abc\r\n100\r\n", "output": "1\r\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "zzz\r\n4\r\n", "output": "1\r\nzzza\r\n"}, {"input": "aaabbb\r\n3\r\n", "output": "3\r\naba\r\n"}, {"input": "abc\r\n5\r\n", "output": "1\r\nabcaa\r\n"}, {"input": "abc\r\n10\r\n", "output": "1\r\nabcaaaaaaa\r\n"}, {"input": "aaaaa\r\n100\r\n", "output": "1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abc\r\n1000\r\n", "output": "1\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "a\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "bbbbb\r\n6\r\n", "output": "1\r\nbbbbba\r\n"}, {"input": "bnana\r\n4\r\n", "output": "2\r\nabna\r\n"}, {"input": "aaaaaaabbbbbbb\r\n3\r\n", "output": "7\r\naba\r\n"}, {"input": "aabbbcccc\r\n7\r\n", "output": "2\r\nabbccaa\r\n"}, {"input": "aaa\r\n9\r\n", "output": "1\r\naaaaaaaaa\r\n"}, {"input": "a\r\n2\r\n", "output": "1\r\naa\r\n"}, {"input": "cccbba\r\n10\r\n", "output": "1\r\nabbcccaaaa\r\n"}, {"input": "a\r\n4\r\n", "output": "1\r\naaaa\r\n"}]
| false |
stdio
|
import sys
from math import ceil
from collections import defaultdict
def main(input_path, output_path, submission_path):
with open(input_path) as f:
s = f.readline().strip()
n = int(f.readline().strip())
s_counts = defaultdict(int)
for c in s:
s_counts[c] += 1
def is_possible(k):
if k == 0:
return False
total = 0
for c in s_counts:
cnt = s_counts[c]
req = (cnt + k - 1) // k
total += req
if total > n:
return False
return total <= n
possible = False
k_min = None
unique_chars = len(s_counts)
if unique_chars > n:
possible = False
else:
left, right = 1, max(s_counts.values()) if s_counts else 0
if is_possible(1):
possible, k_min = True, 1
else:
right = 1
while not is_possible(right):
right *= 2
left = right // 2
while left < right:
mid = (left + right) // 2
if is_possible(mid):
right = mid
else:
left = mid + 1
possible = is_possible(left)
k_min = left if possible else None
with open(submission_path) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if possible:
if len(lines) != 2:
print(0)
return
try:
k_submitted = int(lines[0])
sheet = lines[1]
except:
print(0)
return
if len(sheet) != n or not sheet.islower():
print(0)
return
sheet_chars = set(sheet)
if not set(s_counts.keys()).issubset(sheet_chars):
print(0)
return
sheet_counts = defaultdict(int)
for c in sheet:
sheet_counts[c] += 1
if sum(sheet_counts.values()) != n:
print(0)
return
if k_submitted != k_min:
print(0)
return
valid = True
for c in s_counts:
required = (s_counts[c] + k_submitted - 1) // k_submitted
if sheet_counts[c] < required:
valid = False
break
print(100 if valid else 0)
else:
if len(lines) != 1 or lines[0] != '-1':
print(0)
else:
print(100)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1:4]
main(input_path, output_path, submission_path)
| true |
326/A
|
335
|
A
|
Python 3
|
TESTS
| 10 | 280 | 409,600 |
93626775
|
from math import ceil
from collections import Counter
s = input(); n = int(input())
c = Counter(s)
def f(x):
m = n
for i in c:
m -= ceil(c[i]/x)
if m < 0:
return(False)
return(True)
if n < len(c):print(-1);exit()
ans = 1
while not f(ans):
ans += 1
# ans -= 1
print(ans)
arr = [i*max(1, ceil(c[i]/ans)) for i in c]
print(("").join(arr))
| 55 | 218 | 0 |
48184018
|
s = input()
n = int(input())
cnt = {}
for c in s:
if cnt.get(c) == None:
cnt[c] = 1
else:
cnt[c] += 1
if (n < len(cnt)):
print(-1)
else:
ansNum = 0;
while(True):
ansNum+=1
l = 0;
char = []
for c, v in cnt.items():
need = (v + ansNum -1)//ansNum
l+= need
char.append((c, need))
if (l > n):
continue
print(ansNum)
ans = "".join([str(c[0])*c[1] for c in char])
ans = ans + 'a'*(n - len(ans))
print(ans)
break
|
MemSQL start[c]up Round 2
|
CF
| 2,013 | 2 | 256 |
Banana
|
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
|
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
| null |
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
|
[{"input": "banana\n4", "output": "2\nbaan"}, {"input": "banana\n3", "output": "3\nnab"}, {"input": "banana\n2", "output": "-1"}]
| 1,400 |
[]
| 55 |
[{"input": "banana\r\n4\r\n", "output": "2\r\nbaan\r\n"}, {"input": "banana\r\n3\r\n", "output": "3\r\nnab\r\n"}, {"input": "banana\r\n2\r\n", "output": "-1\r\n"}, {"input": "p\r\n1000\r\n", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "b\r\n1\r\n", "output": "1\r\nb\r\n"}, {"input": "aba\r\n2\r\n", "output": "2\r\nab\r\n"}, {"input": "aaa\r\n2\r\n", "output": "2\r\naa\r\n"}, {"input": "aa\r\n3\r\n", "output": "1\r\naaa\r\n"}, {"input": "aaaaaaaabbbbbccccccccccccccccccccccccccccccc\r\n7\r\n", "output": "8\r\nabcccca\r\n"}, {"input": "aaaaa\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "baba\r\n3\r\n", "output": "2\r\naba\r\n"}, {"input": "a\r\n1000\r\n", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "aan\r\n5\r\n", "output": "1\r\naanaa\r\n"}, {"input": "banana\r\n5\r\n", "output": "2\r\naabna\r\n"}, {"input": "a\r\n5\r\n", "output": "1\r\naaaaa\r\n"}, {"input": "aaaaaaa\r\n5\r\n", "output": "2\r\naaaaa\r\n"}, {"input": "abc\r\n100\r\n", "output": "1\r\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "zzz\r\n4\r\n", "output": "1\r\nzzza\r\n"}, {"input": "aaabbb\r\n3\r\n", "output": "3\r\naba\r\n"}, {"input": "abc\r\n5\r\n", "output": "1\r\nabcaa\r\n"}, {"input": "abc\r\n10\r\n", "output": "1\r\nabcaaaaaaa\r\n"}, {"input": "aaaaa\r\n100\r\n", "output": "1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abc\r\n1000\r\n", "output": "1\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "a\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "bbbbb\r\n6\r\n", "output": "1\r\nbbbbba\r\n"}, {"input": "bnana\r\n4\r\n", "output": "2\r\nabna\r\n"}, {"input": "aaaaaaabbbbbbb\r\n3\r\n", "output": "7\r\naba\r\n"}, {"input": "aabbbcccc\r\n7\r\n", "output": "2\r\nabbccaa\r\n"}, {"input": "aaa\r\n9\r\n", "output": "1\r\naaaaaaaaa\r\n"}, {"input": "a\r\n2\r\n", "output": "1\r\naa\r\n"}, {"input": "cccbba\r\n10\r\n", "output": "1\r\nabbcccaaaa\r\n"}, {"input": "a\r\n4\r\n", "output": "1\r\naaaa\r\n"}]
| false |
stdio
|
import sys
from math import ceil
from collections import defaultdict
def main(input_path, output_path, submission_path):
with open(input_path) as f:
s = f.readline().strip()
n = int(f.readline().strip())
s_counts = defaultdict(int)
for c in s:
s_counts[c] += 1
def is_possible(k):
if k == 0:
return False
total = 0
for c in s_counts:
cnt = s_counts[c]
req = (cnt + k - 1) // k
total += req
if total > n:
return False
return total <= n
possible = False
k_min = None
unique_chars = len(s_counts)
if unique_chars > n:
possible = False
else:
left, right = 1, max(s_counts.values()) if s_counts else 0
if is_possible(1):
possible, k_min = True, 1
else:
right = 1
while not is_possible(right):
right *= 2
left = right // 2
while left < right:
mid = (left + right) // 2
if is_possible(mid):
right = mid
else:
left = mid + 1
possible = is_possible(left)
k_min = left if possible else None
with open(submission_path) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if possible:
if len(lines) != 2:
print(0)
return
try:
k_submitted = int(lines[0])
sheet = lines[1]
except:
print(0)
return
if len(sheet) != n or not sheet.islower():
print(0)
return
sheet_chars = set(sheet)
if not set(s_counts.keys()).issubset(sheet_chars):
print(0)
return
sheet_counts = defaultdict(int)
for c in sheet:
sheet_counts[c] += 1
if sum(sheet_counts.values()) != n:
print(0)
return
if k_submitted != k_min:
print(0)
return
valid = True
for c in s_counts:
required = (s_counts[c] + k_submitted - 1) // k_submitted
if sheet_counts[c] < required:
valid = False
break
print(100 if valid else 0)
else:
if len(lines) != 1 or lines[0] != '-1':
print(0)
else:
print(100)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1:4]
main(input_path, output_path, submission_path)
| true |
326/A
|
335
|
A
|
PyPy 3
|
TESTS
| 10 | 248 | 1,638,400 |
105237797
|
from collections import defaultdict
s = input()
n = int(input())
if len(set(s)) > n:
print(-1)
else:
hist = defaultdict(int)
for c in s:
hist[c] += 1
for ans in range(1, 2000):
cur_need = 0
for c in hist:
# ans * t >= hist[c]
# t >= hist[c] // ans
cur_need += (hist[c] + ans - 1) // ans
if cur_need > n:
continue
print(ans)
s = ''
for c in hist:
s += c * ((hist[c] + ans - 1) // ans)
print(s)
break
| 55 | 248 | 5,222,400 |
4223318
|
import math
from fractions import Decimal
S=input()
N=int(input())
Srep={}
ansrep={}
for item in "abcdefghijklmnopqrstuvwxyz":
Srep[item]=0
ansrep[item]=0
for item in S:
Srep[item]+=1
ansrep[item]+=1
Q=list(set(S))
if(len(Q)>N):
print(-1)
else:
n=len(Q)
ans=list(S)
num=1
req=1
n=len(ans)
while(len(ans)>N):
n=len(ans)
minn=req+1005
removal=ans[0]
k=True
for item in ans:
if(ansrep[item]==1):
continue
if(math.ceil(Srep[item]/(ansrep[item]-1))>req):
if(minn>math.ceil(Srep[item]/(ansrep[item]-1))):
minn=math.ceil(Srep[item]/(ansrep[item]-1))
removal=str(item)
continue
else:
ansrep[item]-=1
ans.remove(item)
k=False
break
if(k):
ansrep[removal]-=1
req=math.ceil(Srep[removal]/ansrep[removal])
ans.remove(removal)
g=""
if(len(ans)<N):
g=S[0]*(N-len(ans))
print(req)
for item in ans:
print(item,end="")
print(g)
|
MemSQL start[c]up Round 2
|
CF
| 2,013 | 2 | 256 |
Banana
|
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
|
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
| null |
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
|
[{"input": "banana\n4", "output": "2\nbaan"}, {"input": "banana\n3", "output": "3\nnab"}, {"input": "banana\n2", "output": "-1"}]
| 1,400 |
[]
| 55 |
[{"input": "banana\r\n4\r\n", "output": "2\r\nbaan\r\n"}, {"input": "banana\r\n3\r\n", "output": "3\r\nnab\r\n"}, {"input": "banana\r\n2\r\n", "output": "-1\r\n"}, {"input": "p\r\n1000\r\n", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "b\r\n1\r\n", "output": "1\r\nb\r\n"}, {"input": "aba\r\n2\r\n", "output": "2\r\nab\r\n"}, {"input": "aaa\r\n2\r\n", "output": "2\r\naa\r\n"}, {"input": "aa\r\n3\r\n", "output": "1\r\naaa\r\n"}, {"input": "aaaaaaaabbbbbccccccccccccccccccccccccccccccc\r\n7\r\n", "output": "8\r\nabcccca\r\n"}, {"input": "aaaaa\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "baba\r\n3\r\n", "output": "2\r\naba\r\n"}, {"input": "a\r\n1000\r\n", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "aan\r\n5\r\n", "output": "1\r\naanaa\r\n"}, {"input": "banana\r\n5\r\n", "output": "2\r\naabna\r\n"}, {"input": "a\r\n5\r\n", "output": "1\r\naaaaa\r\n"}, {"input": "aaaaaaa\r\n5\r\n", "output": "2\r\naaaaa\r\n"}, {"input": "abc\r\n100\r\n", "output": "1\r\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "zzz\r\n4\r\n", "output": "1\r\nzzza\r\n"}, {"input": "aaabbb\r\n3\r\n", "output": "3\r\naba\r\n"}, {"input": "abc\r\n5\r\n", "output": "1\r\nabcaa\r\n"}, {"input": "abc\r\n10\r\n", "output": "1\r\nabcaaaaaaa\r\n"}, {"input": "aaaaa\r\n100\r\n", "output": "1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abc\r\n1000\r\n", "output": "1\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "a\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "bbbbb\r\n6\r\n", "output": "1\r\nbbbbba\r\n"}, {"input": "bnana\r\n4\r\n", "output": "2\r\nabna\r\n"}, {"input": "aaaaaaabbbbbbb\r\n3\r\n", "output": "7\r\naba\r\n"}, {"input": "aabbbcccc\r\n7\r\n", "output": "2\r\nabbccaa\r\n"}, {"input": "aaa\r\n9\r\n", "output": "1\r\naaaaaaaaa\r\n"}, {"input": "a\r\n2\r\n", "output": "1\r\naa\r\n"}, {"input": "cccbba\r\n10\r\n", "output": "1\r\nabbcccaaaa\r\n"}, {"input": "a\r\n4\r\n", "output": "1\r\naaaa\r\n"}]
| false |
stdio
|
import sys
from math import ceil
from collections import defaultdict
def main(input_path, output_path, submission_path):
with open(input_path) as f:
s = f.readline().strip()
n = int(f.readline().strip())
s_counts = defaultdict(int)
for c in s:
s_counts[c] += 1
def is_possible(k):
if k == 0:
return False
total = 0
for c in s_counts:
cnt = s_counts[c]
req = (cnt + k - 1) // k
total += req
if total > n:
return False
return total <= n
possible = False
k_min = None
unique_chars = len(s_counts)
if unique_chars > n:
possible = False
else:
left, right = 1, max(s_counts.values()) if s_counts else 0
if is_possible(1):
possible, k_min = True, 1
else:
right = 1
while not is_possible(right):
right *= 2
left = right // 2
while left < right:
mid = (left + right) // 2
if is_possible(mid):
right = mid
else:
left = mid + 1
possible = is_possible(left)
k_min = left if possible else None
with open(submission_path) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if possible:
if len(lines) != 2:
print(0)
return
try:
k_submitted = int(lines[0])
sheet = lines[1]
except:
print(0)
return
if len(sheet) != n or not sheet.islower():
print(0)
return
sheet_chars = set(sheet)
if not set(s_counts.keys()).issubset(sheet_chars):
print(0)
return
sheet_counts = defaultdict(int)
for c in sheet:
sheet_counts[c] += 1
if sum(sheet_counts.values()) != n:
print(0)
return
if k_submitted != k_min:
print(0)
return
valid = True
for c in s_counts:
required = (s_counts[c] + k_submitted - 1) // k_submitted
if sheet_counts[c] < required:
valid = False
break
print(100 if valid else 0)
else:
if len(lines) != 1 or lines[0] != '-1':
print(0)
else:
print(100)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1:4]
main(input_path, output_path, submission_path)
| true |
785/A
|
785
|
A
|
PyPy 3-64
|
TESTS
| 1 | 46 | 0 |
208853226
|
x=int(input())
c=0
L=[]
d={"Tetrahedron":4,"Cube":6,"Octahedron":8,"Dodecahedron":12,"Icosahedron":20}
for i in range(x):
a=input()
L.append(a)
for i in d:
if i in L:
c+=d[i]
print(c)
| 30 | 93 | 0 |
206068158
|
from sys import stdin
def solve1():
s = {"T":4, "C":6, "O":8, "D":12, "I":20}
c = 0
for i in range(int(input())):
c += s[stdin.readline().strip()[0]]
print(c)
solve1()
|
Codeforces Round 404 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Anton and Polyhedrons
|
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:
- Tetrahedron. Tetrahedron has 4 triangular faces.
- Cube. Cube has 6 square faces.
- Octahedron. Octahedron has 8 triangular faces.
- Dodecahedron. Dodecahedron has 12 pentagonal faces.
- Icosahedron. Icosahedron has 20 triangular faces.
All five kinds of polyhedrons are shown on the picture below:
Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection.
Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:
- "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.
- "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.
- "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.
- "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.
- "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
|
Output one number — the total number of faces in all the polyhedrons in Anton's collection.
| null |
In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.
|
[{"input": "4\nIcosahedron\nCube\nTetrahedron\nDodecahedron", "output": "42"}, {"input": "3\nDodecahedron\nOctahedron\nOctahedron", "output": "28"}]
| 800 |
["implementation", "strings"]
| 30 |
[{"input": "4\r\nIcosahedron\r\nCube\r\nTetrahedron\r\nDodecahedron\r\n", "output": "42\r\n"}, {"input": "3\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\n", "output": "28\r\n"}, {"input": "25\r\nIcosahedron\r\nOctahedron\r\nTetrahedron\r\nDodecahedron\r\nCube\r\nIcosahedron\r\nOctahedron\r\nCube\r\nTetrahedron\r\nIcosahedron\r\nIcosahedron\r\nTetrahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nOctahedron\r\nIcosahedron\r\nTetrahedron\r\nDodecahedron\r\nTetrahedron\r\nOctahedron\r\nCube\r\nCube\r\nDodecahedron\r\nTetrahedron\r\n", "output": "256\r\n"}, {"input": "1\r\nTetrahedron\r\n", "output": "4\r\n"}, {"input": "1\r\nCube\r\n", "output": "6\r\n"}, {"input": "1\r\nOctahedron\r\n", "output": "8\r\n"}, {"input": "1\r\nDodecahedron\r\n", "output": "12\r\n"}, {"input": "1\r\nIcosahedron\r\n", "output": "20\r\n"}, {"input": "28\r\nOctahedron\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nCube\r\nDodecahedron\r\nCube\r\nTetrahedron\r\nCube\r\nCube\r\nTetrahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\n", "output": "340\r\n"}]
| false |
stdio
| null | true |
180/A
|
180
|
A
|
Python 3
|
TESTS
| 3 | 124 | 5,632,000 |
35485996
|
"""
Brandt Smith, Peter Haddad and Lemuel Gorion
codeforces.com
Problem 180A
"""
c, f = input().split(' ')
hd = [0] * int(c)
total_count = 0
zeros = []
moves = []
for i in range(int(f)):
file = input().split()
for x in range(1,len(file)):
hd[int(file[x]) - 1] = [i + 1,x]
for x in range(len(hd)):
if hd[x] == 0:
zeros.append(x)
while True:
count = 0
for i in range(len(hd) - 1):
if hd[i] == 0 and hd[i + 1] == 0:
continue
elif hd[i] != 0 and hd[i + 1] == 0:
continue
elif hd[i] == 0 and hd[i + 1] != 0:
moves.append([i + 2, i + 1])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 1
elif hd[i][0] > hd[i + 1][0]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
zeros.append(t)
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
elif hd[i][0] == hd[i + 1][0] and hd[i][1] > hd[i + 1][1]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
zeros.append(t)
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
total_count += count
if count == 0:
break
print(total_count)
if total_count > 0:
for move in moves:
print(move[0], move[1])
| 24 | 248 | 512,000 |
73525986
|
import sys
n, m, *inp = map(int, sys.stdin.read().split())
inp.reverse()
f = [[0 for x in range(201)] for y in range(201)] #2D Array
c = [(0,0)]*201
f_size = [0]*201
def putData(f_id, s_id, c_id):
global f, c
f[f_id][s_id] = c_id
c[c_id] = (f_id, s_id)
for f_id in range(1, m+1):
f_size[f_id] = inp.pop()
for s_id in range(1, f_size[f_id]+1):
c_id = inp.pop()
putData(f_id, s_id, c_id)
e_id = c[1:].index((0,0))+1
next_id = 1
op = []
for f_id in range(1, m+1):
for s_id in range(1, f_size[f_id]+1):
if c[next_id]==(f_id, s_id):
next_id += 1
continue
if c[next_id] != (0, 0):
op.append((next_id, e_id))
putData(c[next_id][0], c[next_id][1], e_id)
e_id = f[f_id][s_id]
c[e_id] = (0,0)
op.append((e_id, next_id))
putData(f_id, s_id, next_id)
next_id += 1
print(len(op))
for p in op:
print("%d %d" % p)
|
Codeforces Round 116 (Div. 2, ACM-ICPC Rules)
|
ICPC
| 2,012 | 1 | 256 |
Defragmentation
|
In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecutively on the disk, but the order in which they are given corresponds to their sequence in the file (cluster ai, 1 contains the first fragment of the i-th file, cluster ai, 2 has the second fragment, etc.). Also the disc must have one or several clusters which are free from files.
You are permitted to perform operations of copying the contents of cluster number i to cluster number j (i and j must be different). Moreover, if the cluster number j used to keep some information, it is lost forever. Clusters are not cleaned, but after the defragmentation is complete, some of them are simply declared unusable (although they may possibly still contain some fragments of files).
Your task is to use a sequence of copy operations to ensure that each file occupies a contiguous area of memory. Each file should occupy a consecutive cluster section, the files must follow one after another from the beginning of the hard disk. After defragmentation all free (unused) clusters should be at the end of the hard disk. After defragmenting files can be placed in an arbitrary order. Clusters of each file should go consecutively from first to last. See explanatory examples in the notes.
Print the sequence of operations leading to the disk defragmentation. Note that you do not have to minimize the number of operations, but it should not exceed 2n.
|
The first line contains two integers n and m (1 ≤ n, m ≤ 200) — the number of clusters and the number of files, correspondingly. Next m lines contain descriptions of the files. The first number in the line is ni (ni ≥ 1), the number of clusters occupied by the i-th file. Then follow ni numbers ai, 1, ai, 2, ..., ai, ni (1 ≤ ai, j ≤ n). It is guaranteed that each cluster number occurs not more than once and $$\sum_{i=1}^{m} n_i < n$$, that is, there exists at least one unused cluster. Numbers on each line are separated by spaces.
|
In the first line print a single integer k (0 ≤ k ≤ 2n) — the number of operations needed to defragment the disk. Next k lines should contain the operations' descriptions as "i j" (copy the contents of the cluster number i to the cluster number j).
| null |
Let's say that a disk consists of 8 clusters and contains two files. The first file occupies two clusters and the second file occupies three clusters. Let's look at examples of correct and incorrect positions of files after defragmentation.
$$$$
Example 2: each file must occupy a contiguous area of memory.
Example 3: the order of files to each other is not important, at first the second file can be written, and then — the first one.
Example 4: violating the order of file fragments to each other is not allowed.
Example 5: unused clusters should be located at the end, and in this example the unused clusters are 3, 7, 8.
|
[{"input": "7 2\n2 1 2\n3 3 4 5", "output": "0"}, {"input": "7 2\n2 1 3\n3 2 4 5", "output": "3\n2 6\n3 2\n6 3"}]
| 1,800 |
["implementation"]
| 24 |
[{"input": "7 2\r\n2 1 2\r\n3 3 4 5\r\n", "output": "0\r\n"}, {"input": "7 2\r\n2 1 3\r\n3 2 4 5\r\n", "output": "3\r\n2 6\r\n3 2\r\n6 3\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "1\r\n2 1\r\n"}, {"input": "3 1\r\n2 3 1\r\n", "output": "2\r\n1 2\r\n3 1\r\n"}, {"input": "3 2\r\n1 3\r\n1 2\r\n", "output": "1\r\n3 1\r\n"}, {"input": "5 3\r\n1 2\r\n1 4\r\n1 5\r\n", "output": "3\r\n2 1\r\n4 2\r\n5 3\r\n"}, {"input": "7 3\r\n1 7\r\n2 6 5\r\n3 4 3 2\r\n", "output": "7\r\n7 1\r\n2 7\r\n6 2\r\n3 6\r\n5 3\r\n6 5\r\n7 6\r\n"}, {"input": "7 2\r\n3 1 3 5\r\n3 2 4 6\r\n", "output": "5\r\n2 7\r\n3 2\r\n5 3\r\n4 5\r\n7 4\r\n"}, {"input": "10 1\r\n5 7 4 6 9 2\r\n", "output": "7\r\n7 1\r\n2 3\r\n4 2\r\n3 4\r\n6 3\r\n4 5\r\n9 4\r\n"}, {"input": "7 2\r\n2 2 1\r\n3 3 4 5\r\n", "output": "3\r\n1 6\r\n2 1\r\n6 2\r\n"}, {"input": "7 2\r\n2 1 2\r\n3 4 5 6\r\n", "output": "3\r\n4 3\r\n5 4\r\n6 5\r\n"}, {"input": "20 5\r\n7 12 16 14 17 20 4 15\r\n2 7 8\r\n1 5\r\n5 3 6 18 11 2\r\n4 9 13 19 1\r\n", "output": "36\r\n1 10\r\n12 1\r\n2 12\r\n16 2\r\n3 16\r\n14 3\r\n4 14\r\n17 4\r\n5 17\r\n20 5\r\n6 20\r\n14 6\r\n7 14\r\n15 7\r\n8 15\r\n14 8\r\n9 14\r\n15 9\r\n10 15\r\n17 10\r\n11 17\r\n16 11\r\n12 16\r\n20 12\r\n13 20\r\n18 13\r\n14 18\r\n17 14\r\n15 17\r\n16 15\r\n18 16\r\n17 18\r\n20 17\r\n18 20\r\n19 18\r\n20 19\r\n"}, {"input": "20 10\r\n1 2\r\n2 14 11\r\n2 9 15\r\n2 16 8\r\n2 3 13\r\n3 17 12 6\r\n1 5\r\n1 7\r\n2 20 19\r\n1 1\r\n", "output": "25\r\n1 4\r\n2 1\r\n14 2\r\n3 10\r\n11 3\r\n4 11\r\n9 4\r\n5 9\r\n15 5\r\n6 14\r\n16 6\r\n7 15\r\n8 7\r\n10 8\r\n9 10\r\n13 9\r\n10 13\r\n17 10\r\n11 16\r\n12 11\r\n14 12\r\n15 14\r\n20 15\r\n16 17\r\n19 16\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, 'r') as f:
lines = f.readlines()
n, m = map(int, lines[0].split())
files = []
S = set()
for line in lines[1:m+1]:
parts = list(map(int, line.split()))
ni = parts[0]
clusters = parts[1:]
files.append(clusters)
for c in clusters:
S.add(c)
sum_ni = sum(len(f) for f in files)
s = sum_ni
# Read submission's output
with open(submission_path, 'r') as f:
sub_lines = f.readlines()
if not sub_lines:
print(0)
return
k = int(sub_lines[0].strip())
if k > 2 * n or k < 0:
print(0)
return
ops = []
for line in sub_lines[1:k+1]:
try:
i, j = map(int, line.strip().split())
if i == j:
print(0)
return
ops.append((i, j))
except:
print(0)
return
# Simulate operations
arr = [0] * (n + 1) # 1-based
for j in range(1, n+1):
arr[j] = j
for i, j in ops:
if i < 1 or i > n or j < 1 or j > n:
print(0)
return
arr[j] = arr[i]
# Check first s clusters
final_order = arr[1:s+1]
# Check that all elements are in S and each exactly once
if len(final_order) != s:
print(0)
return
if set(final_order) != S:
print(0)
return
if len(set(final_order)) != s:
print(0)
return
# Build cluster info: map from cluster to (file_idx, pos_in_file)
cluster_info = {}
for file_idx, clusters in enumerate(files):
for pos, c in enumerate(clusters):
cluster_info[c] = (file_idx, pos)
# For each file, collect positions in final_order and check
file_sequences = {}
for pos_in_final, c in enumerate(final_order):
file_idx, pos_in_file = cluster_info[c]
if file_idx not in file_sequences:
file_sequences[file_idx] = []
file_sequences[file_idx].append( (pos_in_final, pos_in_file) )
# Check each file
for file_idx, seq in file_sequences.items():
clusters = files[file_idx]
ni = len(clusters)
# Check that all clusters are accounted for
if len(seq) != ni:
print(0)
return
# Compute start for each cluster and check they are the same
starts = [ pos_in_final - pos_in_file for (pos_in_final, pos_in_file) in seq ]
if len(set(starts)) != 1:
print(0)
return
start = starts[0]
# Check that the start is valid
if start < 0 or start + ni > s:
print(0)
return
# Check that the sequence of pos_in_final is start, start+1, ..., start+ni-1
expected_positions = list(range(start, start + ni))
actual_positions = [ pos_in_final for (pos_in_final, _) in seq ]
if actual_positions != expected_positions:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true |
463/B
|
463
|
B
|
PyPy 3-64
|
TESTS
| 7 | 62 | 1,843,200 |
197665107
|
n = int(input())
a = list(map(int , input().split()))
s = 0
c = 0
x = 0
for el in range(0, n-1) :
y = a[el]
s += x - y
if(s < 0):
c += -s
s = 0
x = y
print(c)
| 49 | 62 | 7,270,400 |
176981889
|
l=int(input())
pylon=list(map(int,(input().split())))
print(max(pylon))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014 | 1 | 256 |
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100 |
["brute force", "implementation", "math"]
| 49 |
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false |
stdio
| null | true |
785/A
|
785
|
A
|
Python 3
|
TESTS
| 3 | 46 | 0 |
226594718
|
polyhedrons = int(input())
shape = 0
count = 0
for a in range(polyhedrons):
shape = input()
if 'u' in shape:
count += 6
elif 'i' in shape:
count += 20
elif shape == 'Tetrahedon':
count += 4
elif shape == 'Octahedron':
count += 8
else:
count += 12
print(count)
| 30 | 93 | 3,379,200 |
201229057
|
from sys import stdin
input = stdin.readline
def main():
n = int(input())
res = 0
for _ in range(0, n):
s = input()[:-1]
if s == "Tetrahedron":
res += 4
elif s == "Cube":
res += 6
elif s == "Octahedron":
res += 8
elif s == "Dodecahedron":
res += 12
else:
res += 20
print(res)
main()
|
Codeforces Round 404 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Anton and Polyhedrons
|
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:
- Tetrahedron. Tetrahedron has 4 triangular faces.
- Cube. Cube has 6 square faces.
- Octahedron. Octahedron has 8 triangular faces.
- Dodecahedron. Dodecahedron has 12 pentagonal faces.
- Icosahedron. Icosahedron has 20 triangular faces.
All five kinds of polyhedrons are shown on the picture below:
Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection.
Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:
- "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.
- "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.
- "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.
- "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.
- "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
|
Output one number — the total number of faces in all the polyhedrons in Anton's collection.
| null |
In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.
|
[{"input": "4\nIcosahedron\nCube\nTetrahedron\nDodecahedron", "output": "42"}, {"input": "3\nDodecahedron\nOctahedron\nOctahedron", "output": "28"}]
| 800 |
["implementation", "strings"]
| 30 |
[{"input": "4\r\nIcosahedron\r\nCube\r\nTetrahedron\r\nDodecahedron\r\n", "output": "42\r\n"}, {"input": "3\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\n", "output": "28\r\n"}, {"input": "25\r\nIcosahedron\r\nOctahedron\r\nTetrahedron\r\nDodecahedron\r\nCube\r\nIcosahedron\r\nOctahedron\r\nCube\r\nTetrahedron\r\nIcosahedron\r\nIcosahedron\r\nTetrahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nOctahedron\r\nIcosahedron\r\nTetrahedron\r\nDodecahedron\r\nTetrahedron\r\nOctahedron\r\nCube\r\nCube\r\nDodecahedron\r\nTetrahedron\r\n", "output": "256\r\n"}, {"input": "1\r\nTetrahedron\r\n", "output": "4\r\n"}, {"input": "1\r\nCube\r\n", "output": "6\r\n"}, {"input": "1\r\nOctahedron\r\n", "output": "8\r\n"}, {"input": "1\r\nDodecahedron\r\n", "output": "12\r\n"}, {"input": "1\r\nIcosahedron\r\n", "output": "20\r\n"}, {"input": "28\r\nOctahedron\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nCube\r\nDodecahedron\r\nCube\r\nTetrahedron\r\nCube\r\nCube\r\nTetrahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\n", "output": "340\r\n"}]
| false |
stdio
| null | true |
785/A
|
785
|
A
|
Python 3
|
TESTS
| 1 | 31 | 0 |
207761223
|
n = int(input())
d = {};c=0
r={"Tetrahedron":4,"Cube":6,"Octahedron":8,"Dodecahedron":12}
for i in range(n):
x = input()
d[x] = d.get(x,0) + 1
c = 0
for k,v in d.items():
c += r.get(k,20)
print(c)
| 30 | 93 | 3,481,600 |
224754672
|
import sys
#f=open("C:/Users/MAHAMUD MATIN/Desktop/input.txt", "r")
f=sys.stdin
n=int(f.readline())
total=0
for i in range(n):
s=f.readline()
if s[0]=="T":
total+=4
elif s[0]=="C":
total+=6
elif s[0]=="O":
total+=8
elif s[0]=="D":
total+=12
else:
total+=20
print(total)
|
Codeforces Round 404 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Anton and Polyhedrons
|
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:
- Tetrahedron. Tetrahedron has 4 triangular faces.
- Cube. Cube has 6 square faces.
- Octahedron. Octahedron has 8 triangular faces.
- Dodecahedron. Dodecahedron has 12 pentagonal faces.
- Icosahedron. Icosahedron has 20 triangular faces.
All five kinds of polyhedrons are shown on the picture below:
Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection.
Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:
- "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.
- "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.
- "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.
- "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.
- "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
|
Output one number — the total number of faces in all the polyhedrons in Anton's collection.
| null |
In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.
|
[{"input": "4\nIcosahedron\nCube\nTetrahedron\nDodecahedron", "output": "42"}, {"input": "3\nDodecahedron\nOctahedron\nOctahedron", "output": "28"}]
| 800 |
["implementation", "strings"]
| 30 |
[{"input": "4\r\nIcosahedron\r\nCube\r\nTetrahedron\r\nDodecahedron\r\n", "output": "42\r\n"}, {"input": "3\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\n", "output": "28\r\n"}, {"input": "25\r\nIcosahedron\r\nOctahedron\r\nTetrahedron\r\nDodecahedron\r\nCube\r\nIcosahedron\r\nOctahedron\r\nCube\r\nTetrahedron\r\nIcosahedron\r\nIcosahedron\r\nTetrahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nOctahedron\r\nIcosahedron\r\nTetrahedron\r\nDodecahedron\r\nTetrahedron\r\nOctahedron\r\nCube\r\nCube\r\nDodecahedron\r\nTetrahedron\r\n", "output": "256\r\n"}, {"input": "1\r\nTetrahedron\r\n", "output": "4\r\n"}, {"input": "1\r\nCube\r\n", "output": "6\r\n"}, {"input": "1\r\nOctahedron\r\n", "output": "8\r\n"}, {"input": "1\r\nDodecahedron\r\n", "output": "12\r\n"}, {"input": "1\r\nIcosahedron\r\n", "output": "20\r\n"}, {"input": "28\r\nOctahedron\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nCube\r\nDodecahedron\r\nCube\r\nTetrahedron\r\nCube\r\nCube\r\nTetrahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\n", "output": "340\r\n"}]
| false |
stdio
| null | true |
785/A
|
785
|
A
|
Python 3
|
TESTS
| 1 | 31 | 0 |
225039134
|
n = int(input())
t = 0
c = 0
o = 0
d = 0
ic = 0
for i in range(n):
s = input()
if s== "Tetrahedron":
t = 4
elif s == "Cube":
c = 6
elif s == "Octahedron":
o = 8
elif s == "Dodecahedron":
d = 12
elif s == "Icosahedron":
ic = 20
print(t+c+o+d+ic)
| 30 | 93 | 3,584,000 |
230657126
|
import sys
input = sys.stdin.readline
n = int(input())
count = 0
for _ in range(n):
string = str(input())[:-1] # Eliminating the newline character
if string == "Tetrahedron":
count += 4
elif string == "Cube":
count += 6
elif string == "Octahedron":
count += 8
elif string == "Dodecahedron":
count += 12
elif string == "Icosahedron":
count += 20
sys.stdout.write(f"{count}")
|
Codeforces Round 404 (Div. 2)
|
CF
| 2,017 | 2 | 256 |
Anton and Polyhedrons
|
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:
- Tetrahedron. Tetrahedron has 4 triangular faces.
- Cube. Cube has 6 square faces.
- Octahedron. Octahedron has 8 triangular faces.
- Dodecahedron. Dodecahedron has 12 pentagonal faces.
- Icosahedron. Icosahedron has 20 triangular faces.
All five kinds of polyhedrons are shown on the picture below:
Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection.
Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:
- "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.
- "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.
- "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.
- "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.
- "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
|
Output one number — the total number of faces in all the polyhedrons in Anton's collection.
| null |
In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.
|
[{"input": "4\nIcosahedron\nCube\nTetrahedron\nDodecahedron", "output": "42"}, {"input": "3\nDodecahedron\nOctahedron\nOctahedron", "output": "28"}]
| 800 |
["implementation", "strings"]
| 30 |
[{"input": "4\r\nIcosahedron\r\nCube\r\nTetrahedron\r\nDodecahedron\r\n", "output": "42\r\n"}, {"input": "3\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\n", "output": "28\r\n"}, {"input": "25\r\nIcosahedron\r\nOctahedron\r\nTetrahedron\r\nDodecahedron\r\nCube\r\nIcosahedron\r\nOctahedron\r\nCube\r\nTetrahedron\r\nIcosahedron\r\nIcosahedron\r\nTetrahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nOctahedron\r\nIcosahedron\r\nTetrahedron\r\nDodecahedron\r\nTetrahedron\r\nOctahedron\r\nCube\r\nCube\r\nDodecahedron\r\nTetrahedron\r\n", "output": "256\r\n"}, {"input": "1\r\nTetrahedron\r\n", "output": "4\r\n"}, {"input": "1\r\nCube\r\n", "output": "6\r\n"}, {"input": "1\r\nOctahedron\r\n", "output": "8\r\n"}, {"input": "1\r\nDodecahedron\r\n", "output": "12\r\n"}, {"input": "1\r\nIcosahedron\r\n", "output": "20\r\n"}, {"input": "28\r\nOctahedron\r\nDodecahedron\r\nOctahedron\r\nOctahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nCube\r\nDodecahedron\r\nCube\r\nTetrahedron\r\nCube\r\nCube\r\nTetrahedron\r\nDodecahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\nDodecahedron\r\nIcosahedron\r\nDodecahedron\r\nDodecahedron\r\nIcosahedron\r\nIcosahedron\r\n", "output": "340\r\n"}]
| false |
stdio
| null | true |
463/B
|
463
|
B
|
Python 3
|
TESTS
| 7 | 109 | 614,400 |
85656454
|
n=int(input())
l=list(map(int,input().split()))
k=-l[0]
t=0
for i in range(1,n):
if k<=0:
t+=k
k=0
k+=(l[i-1]-l[i])
print(abs(t))
| 49 | 62 | 7,372,800 |
138479829
|
n = int(input())
list1 = list(map(int, input().strip().split()))
print(max(list1))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014 | 1 | 256 |
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100 |
["brute force", "implementation", "math"]
| 49 |
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false |
stdio
| null | true |
526/B
|
526
|
B
|
Python 3
|
TESTS
| 1 | 62 | 0 |
10623894
|
def balance(node):
if node >= (2 ** n):
return 0
else:
c1 = balance(2*node)
c2 = balance(2*node+1)
#print(node)
a = abs(c1 - c2)
b = abs(tree[2*node - 2] - tree[2*node - 1])
global count
count += (a+b)
if c1 > c2:
tree[2*node - 1] += (a+b)
elif c1 < c2:
tree[2*node - 2] += (a+b)
return tree[2*node - 1]
n = int(input())
s = input()
s = s.split()
tree = [int(i) for i in s]
count = 0
balance(1)
print(count)
| 38 | 46 | 0 |
11142882
|
# 526B
__author__ = 'artyom'
n = int(input())
a = [0, 0] + list(map(int, input().split()))
comp = 0
for i in range(2 ** (n + 1) - 1, 1, -2):
diff = abs(a[i] - a[i - 1])
comp += diff
a[i // 2] += max(a[i], a[i - 1])
print(comp)
|
ZeptoLab Code Rush 2015
|
CF
| 2,015 | 1 | 256 |
Om Nom and Dark Park
|
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.
The park consists of 2n + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2n, 2n + 1, ..., 2n + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square i (2 ≤ i < 2n + 1) there is a road to the square $$\frac{i}{2}$$. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads.
Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe.
He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.
|
The first line contains integer n (1 ≤ n ≤ 10) — the number of roads on the path from the entrance to any exit.
The next line contains 2n + 1 - 2 numbers a2, a3, ... a2n + 1 - 1 — the initial numbers of street lights on each road of the park. Here ai is the number of street lights on the road between squares i and $$\frac{i}{2}$$. All numbers ai are positive integers, not exceeding 100.
|
Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
| null |
Picture for the sample test. Green color denotes the additional street lights.
|
[{"input": "2\n1 2 3 4 5 6", "output": "5"}]
| 1,400 |
["dfs and similar", "greedy", "implementation"]
| 38 |
[{"input": "2\r\n1 2 3 4 5 6\r\n", "output": "5\r\n"}, {"input": "2\r\n1 2 3 3 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n39 52\r\n", "output": "13\r\n"}, {"input": "2\r\n59 96 34 48 8 72\r\n", "output": "139\r\n"}, {"input": "3\r\n87 37 91 29 58 45 51 74 70 71 47 38 91 89\r\n", "output": "210\r\n"}, {"input": "5\r\n39 21 95 89 73 90 9 55 85 32 30 21 68 59 82 91 20 64 52 70 6 88 53 47 30 47 34 14 11 22 42 15 28 54 37 48 29 3 14 13 18 77 90 58 54 38 94 49 45 66 13 74 11 14 64 72 95 54 73 79 41 35\r\n", "output": "974\r\n"}, {"input": "1\r\n49 36\r\n", "output": "13\r\n"}, {"input": "1\r\n77 88\r\n", "output": "11\r\n"}, {"input": "1\r\n1 33\r\n", "output": "32\r\n"}, {"input": "2\r\n72 22 81 23 14 75\r\n", "output": "175\r\n"}, {"input": "2\r\n100 70 27 1 68 52\r\n", "output": "53\r\n"}, {"input": "2\r\n24 19 89 82 22 21\r\n", "output": "80\r\n"}, {"input": "3\r\n86 12 92 91 3 68 57 56 76 27 33 62 71 84\r\n", "output": "286\r\n"}, {"input": "3\r\n14 56 53 61 57 45 40 44 31 9 73 2 61 26\r\n", "output": "236\r\n"}, {"input": "3\r\n35 96 7 43 10 14 16 36 95 92 16 50 59 55\r\n", "output": "173\r\n"}, {"input": "4\r\n1 97 18 48 96 65 24 91 17 45 36 27 74 93 78 86 39 55 53 21 26 68 31 33 79 63 80 92 1 26\r\n", "output": "511\r\n"}, {"input": "4\r\n25 42 71 29 50 30 99 79 77 24 76 66 68 23 97 99 65 17 75 62 66 46 48 4 40 71 98 57 21 92\r\n", "output": "603\r\n"}, {"input": "4\r\n49 86 17 7 3 6 86 71 36 10 27 10 58 64 12 16 88 67 93 3 15 20 58 87 97 91 11 6 34 62\r\n", "output": "470\r\n"}, {"input": "5\r\n16 87 36 16 81 53 87 35 63 56 47 91 81 95 80 96 91 7 58 99 25 28 47 60 7 69 49 14 51 52 29 30 83 23 21 52 100 26 91 14 23 94 72 70 40 12 50 32 54 52 18 74 5 15 62 3 48 41 24 25 56 43\r\n", "output": "1060\r\n"}, {"input": "5\r\n40 27 82 94 38 22 66 23 18 34 87 31 71 28 95 5 14 61 76 52 66 6 60 40 68 77 70 63 64 18 47 13 82 55 34 64 30 1 29 24 24 9 65 17 29 96 61 76 72 23 32 26 90 39 54 41 35 66 71 29 75 48\r\n", "output": "1063\r\n"}, {"input": "5\r\n64 72 35 68 92 95 45 15 77 16 26 74 61 65 18 22 32 19 98 97 14 84 70 23 29 1 87 28 88 89 73 79 69 88 43 60 64 64 66 39 17 27 46 71 18 83 73 20 90 77 49 70 84 63 50 72 26 87 26 37 78 65\r\n", "output": "987\r\n"}, {"input": "6\r\n35 61 54 77 70 50 53 70 4 66 58 47 76 100 78 5 43 50 55 93 13 93 59 92 30 74 22 23 98 70 19 56 90 92 19 7 28 53 45 77 42 91 71 56 19 83 100 53 13 93 37 13 70 60 16 13 76 3 12 22 17 26 50 6 63 7 25 41 92 29 36 80 11 4 10 14 77 75 53 82 46 24 56 46 82 36 80 75 8 45 24 22 90 34 45 76 18 38 86 43 7 49 80 56 90 53 12 51 98 47 44 58 32 4 2 6 3 60 38 72 74 46 30 86 1 98\r\n", "output": "2499\r\n"}, {"input": "6\r\n63 13 100 54 31 15 29 58 59 44 2 99 70 33 97 14 70 12 73 42 65 71 68 67 87 83 43 84 18 41 37 22 81 24 27 11 57 28 83 92 39 1 56 15 16 67 16 97 31 52 50 65 63 89 8 52 55 20 71 27 28 35 86 92 94 60 10 65 83 63 89 71 34 20 78 40 34 62 2 86 100 81 87 69 25 4 52 17 57 71 62 38 1 3 54 71 34 85 20 60 80 23 82 47 4 19 7 18 14 18 28 27 4 55 26 71 45 9 2 40 67 28 32 19 81 92\r\n", "output": "2465\r\n"}, {"input": "6\r\n87 62 58 32 81 92 12 50 23 27 38 39 64 74 16 35 84 59 91 87 14 48 90 47 44 95 64 45 31 11 67 5 80 60 36 15 91 3 21 2 40 24 37 69 5 50 23 37 49 19 68 21 49 9 100 94 45 41 22 31 31 48 25 70 25 25 95 88 82 1 37 53 49 31 57 74 94 45 55 93 43 37 13 85 59 72 15 68 3 90 96 55 100 64 63 69 43 33 66 84 57 97 87 34 23 89 97 77 39 89 8 92 68 13 50 36 95 61 71 96 73 13 30 49 57 89\r\n", "output": "2513\r\n"}]
| false |
stdio
| null | true |
53/D
|
53
|
D
|
PyPy 3-64
|
TESTS
| 0 | 60 | 0 |
209350101
|
n=int(input())
l=list(map(int,input().split()))
l1=list(map(int,input().split()))
ans=[]
for i in range(n):
if(l[i]==l1[i]):
continue
for j in range(i,n):
if(l[j]==l1[i]):
break
for k in range(j,i,-1):
ans.append([k,k-1])
l[k-1],l[k]=l[k],l[k-1]
print(len(ans))
for i,j in ans:
print(i+1,j+1)
| 30 | 216 | 9,113,600 |
208526211
|
n = int(input())
ls1 = list(map(int,input().split()))
ls2 = list(map(int,input().split()))
out = []
i, j = 0, 0
while i < n :
while j < n and ls2[j] != ls1[i] :
j += 1
while j != i :
temp = ls2[j]
ls2[j] = ls2[j-1]
ls2[j-1] = temp
out.append([j,j+1])
j -= 1
i += 1
j += 1
print(len(out))
for i in out :
print(i[0],i[1])
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011 | 2 | 256 |
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500 |
["sortings"]
| 30 |
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false |
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true |
53/D
|
53
|
D
|
Python 3
|
TESTS
| 0 | 62 | 0 |
136226434
|
_ = input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
res = []
for i, item in enumerate(b):
ii = i+a[i:].index(item)
if i == ii:
continue
for shift in range(ii-i):
res.append(f'{i+(ii-i)-shift+1} {i+(ii-i)-shift}')
c += 1
a1 = a[i+(ii-i)-shift]
a2 = a[i+(ii-i)-shift-1]
a[i+(ii-i)-shift-1] = a1
a[i+(ii-i)-shift] = a2
print(c)
if res:
print('\n'.join(res))
| 30 | 218 | 512,000 |
105056041
|
n = int(input())
original = list(input().split())
given = list(input().split())
pre = []
last = []
result = 0
for i in range(n):
if original[i] == given[i]:
continue
else:
j = i + 1
index = len(pre)
for j in range(i + 1, n):
pre.insert(index, j + 1)
last.insert(index, j)
if given[j] == original[i]:
break
given[i + 1: j + 1] = given[i:j]
result += j - i
print(result)
for i in range(len(pre)):
print(str(last[i]) + " " + str(pre[i]))
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011 | 2 | 256 |
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500 |
["sortings"]
| 30 |
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false |
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true |
393/B
|
393
|
B
|
Python 3
|
TESTS
| 2 | 140 | 7,884,800 |
115450187
|
import sys
from os import path
from collections import Counter
if(path.exists("inp.txt")):
sys.stdin = open("inp.txt",'r')
sys.stdout = open("out.txt",'w')
import math
n=int(input())
w=[]
li=[0]*((2*n)-1)
for i in range(n):
temp=list(map(int,input().split()))
w.append(temp)
c=0
for i in range(n):
for j in range(n):
li[i+j]=li[i+j]+w[i][j]
ans=[[0 for i in range(n)] for j in range(n)]
ans1=[[0 for i in range(n)] for j in range(n)]
b=n
x=[n]
while(n!=1):
x.insert(0,n-1)
x.append(n-1)
n-=1
for i in range(b):
for j in range(b):
ans[i][j]=li[i+j]/x[i+j]
for i in range(b):
for j in range(b):
ans1[i][j]=w[i][j]-ans[i][j]
for i in ans:
print(*i)
for i in ans1:
print(*i)
| 40 | 140 | 2,048,000 |
180742857
|
n = int(input())
W = []
for j in range(n):
k = input().split()
for i in range(n):
k[i] = int(k[i])
W.append(k)
A = []
for j in range(n):
A.append([])
for i in range(n):
a = (W[j][i]+W[i][j])/2
A[j].append(a)
B = []
for j in range(n):
B.append([])
for i in range(n):
b = (W[j][i]-W[i][j])/2
B[j].append(b)
for i in A:
print(*i)
for i in B:
print(*i)
|
Codeforces Round 230 (Div. 2)
|
CF
| 2,014 | 1 | 256 |
Three matrices
|
Chubby Yang is studying linear equations right now. He came up with a nice problem. In the problem you are given an n × n matrix W, consisting of integers, and you should find two n × n matrices A and B, all the following conditions must hold:
- Aij = Aji, for all i, j (1 ≤ i, j ≤ n);
- Bij = - Bji, for all i, j (1 ≤ i, j ≤ n);
- Wij = Aij + Bij, for all i, j (1 ≤ i, j ≤ n).
Can you solve the problem?
|
The first line contains an integer n (1 ≤ n ≤ 170). Each of the following n lines contains n integers. The j-th integer in the i-th line is Wij (0 ≤ |Wij| < 1717).
|
The first n lines must contain matrix A. The next n lines must contain matrix B. Print the matrices in the format equal to format of matrix W in input. It is guaranteed that the answer exists. If there are multiple answers, you are allowed to print any of them.
The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 4.
| null | null |
[{"input": "2\n1 4\n3 2", "output": "1.00000000 3.50000000\n3.50000000 2.00000000\n0.00000000 0.50000000\n-0.50000000 0.00000000"}, {"input": "3\n1 2 3\n4 5 6\n7 8 9", "output": "1.00000000 3.00000000 5.00000000\n3.00000000 5.00000000 7.00000000\n5.00000000 7.00000000 9.00000000\n0.00000000 -1.00000000 -2.00000000\n1.00000000 0.00000000 -1.00000000\n2.00000000 1.00000000 0.00000000"}]
| null |
[]
| 40 |
[{"input": "2\r\n1 4\r\n3 2\r\n", "output": "1.00000000 3.50000000\r\n3.50000000 2.00000000\r\n0.00000000 0.50000000\r\n-0.50000000 0.00000000\r\n"}, {"input": "3\r\n1 2 3\r\n4 5 6\r\n7 8 9\r\n", "output": "1.00000000 3.00000000 5.00000000\r\n3.00000000 5.00000000 7.00000000\r\n5.00000000 7.00000000 9.00000000\r\n0.00000000 -1.00000000 -2.00000000\r\n1.00000000 0.00000000 -1.00000000\r\n2.00000000 1.00000000 0.00000000\r\n"}, {"input": "8\r\n62 567 1382 1279 728 1267 1262 568\r\n77 827 717 1696 774 248 822 1266\r\n563 612 995 424 1643 1197 338 1141\r\n1579 806 1254 468 184 1571 716 772\r\n1087 182 1312 772 605 1674 720 1349\r\n1393 988 873 157 403 301 1519 1192\r\n1085 625 1395 1087 847 1360 1004 594\r\n1368 1056 916 839 472 840 53 1238\r\n", "output": "62.000000000 322.000000000 972.500000000 1429.000000000 907.500000000 1330.000000000 1173.500000000 968.000000000\n322.000000000 827.000000000 664.500000000 1251.000000000 478.000000000 618.000000000 723.500000000 1161.000000000\n972.500000000 664.500000000 995.000000000 839.000000000 1477.500000000 1035.000000000 866.500000000 1028.500000000\n1429.000000000 1251.000000000 839.000000000 468.000000000 478.000000000 864.000000000 901.500000000 805.500000000\n907.500000000 478.000000000 1477.500000000 478.000000000 605.000000000 1038.500000000 783.500000000 910.500000000\n1330.000000000 618.000000000 1035.000000000 864.000000000 1038.500000000 301.000000000 1439.500000000 1016.000000000\n1173.500000000 723.500000000 866.500000000 901.500000000 783.500000000 1439.500000000 1004.000000000 323.500000000\n968.000000000 1161.000000000 1028.500000000 805.500000000 910.500000000 1016.000000000 323.500000000 1238.000000000\n0.000000000 245.000000000 409.500000000 -150.000000000 -179.500000000 -63.000000000 88.500000000 -400.000000000\n-245.000000000 0.000000000 52.500000000 445.000000000 296.000000000 -370.000000000 98.500000000 105.000000000\n-409.500000000 -52.500000000 0.000000000 -415.000000000 165.500000000 162.000000000 -528.500000000 112.500000000\n150.000000000 -445.000000000 415.000000000 0.000000000 -294.000000000 707.000000000 -185.500000000 -33.500000000\n179.500000000 -296.000000000 -165.500000000 294.000000000 0.000000000 635.500000000 -63.500000000 438.500000000\n63.000000000 370.000000000 -162.000000000 -707.000000000 -635.500000000 0.000000000 79.500000000 176.000000000\n-88.500000000 -98.500000000 528.500000000 185.500000000 63.500000000 -79.500000000 0.000000000 270.500000000\n400.000000000 -105.000000000 -112.500000000 33.500000000 -438.500000000 -176.000000000 -270.500000000 0.000000000\n"}, {"input": "7\r\n926 41 1489 72 749 375 940\r\n464 1148 858 1010 285 1469 1506\r\n1112 1087 225 917 480 511 1090\r\n759 945 627 230 220 1456 529\r\n318 83 203 134 1192 1167 6\r\n440 1158 1614 683 1358 1140 1196\r\n1175 900 126 1562 1220 813 148\r\n", "output": "926.000000000 252.500000000 1300.500000000 415.500000000 533.500000000 407.500000000 1057.500000000\n252.500000000 1148.000000000 972.500000000 977.500000000 184.000000000 1313.500000000 1203.000000000\n1300.500000000 972.500000000 225.000000000 772.000000000 341.500000000 1062.500000000 608.000000000\n415.500000000 977.500000000 772.000000000 230.000000000 177.000000000 1069.500000000 1045.500000000\n533.500000000 184.000000000 341.500000000 177.000000000 1192.000000000 1262.500000000 613.000000000\n407.500000000 1313.500000000 1062.500000000 1069.500000000 1262.500000000 1140.000000000 1004.500000000\n1057.500000000 1203.000000000 608.000000000 1045.500000000 613.000000000 1004.500000000 148.000000000\n0.000000000 -211.500000000 188.500000000 -343.500000000 215.500000000 -32.500000000 -117.500000000\n211.500000000 0.000000000 -114.500000000 32.500000000 101.000000000 155.500000000 303.000000000\n-188.500000000 114.500000000 0.000000000 145.000000000 138.500000000 -551.500000000 482.000000000\n343.500000000 -32.500000000 -145.000000000 0.000000000 43.000000000 386.500000000 -516.500000000\n-215.500000000 -101.000000000 -138.500000000 -43.000000000 0.000000000 -95.500000000 -607.000000000\n32.500000000 -155.500000000 551.500000000 -386.500000000 95.500000000 0.000000000 191.500000000\n117.500000000 -303.000000000 -482.000000000 516.500000000 607.000000000 -191.500000000 0.000000000\n"}, {"input": "1\r\n1\r\n", "output": "1.00000000\r\n0.00000000\r\n"}, {"input": "1\r\n0\r\n", "output": "0.00000000\r\n0.00000000\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "0.00000000 0.00000000\r\n0.00000000 0.00000000\r\n0.00000000 0.00000000\r\n0.00000000 0.00000000\r\n"}, {"input": "2\r\n0 1\r\n0 1\r\n", "output": "0.00000000 0.50000000\r\n0.50000000 1.00000000\r\n0.00000000 0.50000000\r\n-0.50000000 0.00000000\r\n"}]
| false |
stdio
|
import sys
def is_close(a, b):
abs_tol = 1e-4
rel_tol = 1e-4
diff = abs(a - b)
if diff <= abs_tol:
return True
max_val = max(abs(a), abs(b))
return diff <= rel_tol * max_val
def main(input_path, output_path, submission_path):
with open(input_path) as f:
lines = f.read().splitlines()
n = int(lines[0])
w = []
for line in lines[1:1+n]:
row = list(map(int, line.split()))
w.append(row)
with open(submission_path) as f:
submission_lines = f.read().splitlines()
if len(submission_lines) != 2 * n:
print(0)
return
A = []
B = []
for i in range(n):
if i >= len(submission_lines):
print(0)
return
parts = submission_lines[i].split()
if len(parts) != n:
print(0)
return
try:
row = list(map(float, parts))
except:
print(0)
return
A.append(row)
for i in range(n, 2 * n):
if i >= len(submission_lines):
print(0)
return
parts = submission_lines[i].split()
if len(parts) != n:
print(0)
return
try:
row = list(map(float, parts))
except:
print(0)
return
B.append(row)
for i in range(n):
for j in range(n):
if not is_close(A[i][j], A[j][i]):
print(0)
return
for i in range(n):
for j in range(n):
if not is_close(B[i][j], -B[j][i]):
print(0)
return
for i in range(n):
for j in range(n):
sum_ab = A[i][j] + B[i][j]
expected = w[i][j]
if not is_close(sum_ab, expected):
print(0)
return
print(1)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true |
526/B
|
526
|
B
|
Python 3
|
TESTS
| 2 | 109 | 307,200 |
97854958
|
n=int(input())
a=list(map(int,input().split()))
if n==1:
print(0)
else:
dp=[a[0],a[1]]
m=(2**(n+1))-2
for j in range(2,m):
dp.append(a[j]+dp[(j//2)-1])
c=0
k=2**n+1
m=0
for j in range(1,k,2):
m=max(dp[-j],dp[-j-1],m)
mi=min(dp[-j],dp[-j-1])
c=c+mi
k-=1
k=k//2
ans=(m*k)-c
print(ans)
| 38 | 46 | 0 |
147599018
|
a = []
n = 0
ans = 0
def DFS(x, h):
global a, n, ans
if h < n:
DFS(2*x+1, h+1)
DFS(2*x+2, h+1)
a[2*x] += max(a[2*(2*x+1)],a[2*(2*x+1)+1])
a[2*x+1] += max(a[2*(2*x+2)],a[2*(2*x+2)+1])
ans += abs(a[2*x]-a[x*2+1])
n = int(input())
s = input()
s = s.split()
for i in s:
a.append(int(i))
DFS(0, 1)
print(ans)
|
ZeptoLab Code Rush 2015
|
CF
| 2,015 | 1 | 256 |
Om Nom and Dark Park
|
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.
The park consists of 2n + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2n, 2n + 1, ..., 2n + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square i (2 ≤ i < 2n + 1) there is a road to the square $$\frac{i}{2}$$. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads.
Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe.
He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.
|
The first line contains integer n (1 ≤ n ≤ 10) — the number of roads on the path from the entrance to any exit.
The next line contains 2n + 1 - 2 numbers a2, a3, ... a2n + 1 - 1 — the initial numbers of street lights on each road of the park. Here ai is the number of street lights on the road between squares i and $$\frac{i}{2}$$. All numbers ai are positive integers, not exceeding 100.
|
Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
| null |
Picture for the sample test. Green color denotes the additional street lights.
|
[{"input": "2\n1 2 3 4 5 6", "output": "5"}]
| 1,400 |
["dfs and similar", "greedy", "implementation"]
| 38 |
[{"input": "2\r\n1 2 3 4 5 6\r\n", "output": "5\r\n"}, {"input": "2\r\n1 2 3 3 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n39 52\r\n", "output": "13\r\n"}, {"input": "2\r\n59 96 34 48 8 72\r\n", "output": "139\r\n"}, {"input": "3\r\n87 37 91 29 58 45 51 74 70 71 47 38 91 89\r\n", "output": "210\r\n"}, {"input": "5\r\n39 21 95 89 73 90 9 55 85 32 30 21 68 59 82 91 20 64 52 70 6 88 53 47 30 47 34 14 11 22 42 15 28 54 37 48 29 3 14 13 18 77 90 58 54 38 94 49 45 66 13 74 11 14 64 72 95 54 73 79 41 35\r\n", "output": "974\r\n"}, {"input": "1\r\n49 36\r\n", "output": "13\r\n"}, {"input": "1\r\n77 88\r\n", "output": "11\r\n"}, {"input": "1\r\n1 33\r\n", "output": "32\r\n"}, {"input": "2\r\n72 22 81 23 14 75\r\n", "output": "175\r\n"}, {"input": "2\r\n100 70 27 1 68 52\r\n", "output": "53\r\n"}, {"input": "2\r\n24 19 89 82 22 21\r\n", "output": "80\r\n"}, {"input": "3\r\n86 12 92 91 3 68 57 56 76 27 33 62 71 84\r\n", "output": "286\r\n"}, {"input": "3\r\n14 56 53 61 57 45 40 44 31 9 73 2 61 26\r\n", "output": "236\r\n"}, {"input": "3\r\n35 96 7 43 10 14 16 36 95 92 16 50 59 55\r\n", "output": "173\r\n"}, {"input": "4\r\n1 97 18 48 96 65 24 91 17 45 36 27 74 93 78 86 39 55 53 21 26 68 31 33 79 63 80 92 1 26\r\n", "output": "511\r\n"}, {"input": "4\r\n25 42 71 29 50 30 99 79 77 24 76 66 68 23 97 99 65 17 75 62 66 46 48 4 40 71 98 57 21 92\r\n", "output": "603\r\n"}, {"input": "4\r\n49 86 17 7 3 6 86 71 36 10 27 10 58 64 12 16 88 67 93 3 15 20 58 87 97 91 11 6 34 62\r\n", "output": "470\r\n"}, {"input": "5\r\n16 87 36 16 81 53 87 35 63 56 47 91 81 95 80 96 91 7 58 99 25 28 47 60 7 69 49 14 51 52 29 30 83 23 21 52 100 26 91 14 23 94 72 70 40 12 50 32 54 52 18 74 5 15 62 3 48 41 24 25 56 43\r\n", "output": "1060\r\n"}, {"input": "5\r\n40 27 82 94 38 22 66 23 18 34 87 31 71 28 95 5 14 61 76 52 66 6 60 40 68 77 70 63 64 18 47 13 82 55 34 64 30 1 29 24 24 9 65 17 29 96 61 76 72 23 32 26 90 39 54 41 35 66 71 29 75 48\r\n", "output": "1063\r\n"}, {"input": "5\r\n64 72 35 68 92 95 45 15 77 16 26 74 61 65 18 22 32 19 98 97 14 84 70 23 29 1 87 28 88 89 73 79 69 88 43 60 64 64 66 39 17 27 46 71 18 83 73 20 90 77 49 70 84 63 50 72 26 87 26 37 78 65\r\n", "output": "987\r\n"}, {"input": "6\r\n35 61 54 77 70 50 53 70 4 66 58 47 76 100 78 5 43 50 55 93 13 93 59 92 30 74 22 23 98 70 19 56 90 92 19 7 28 53 45 77 42 91 71 56 19 83 100 53 13 93 37 13 70 60 16 13 76 3 12 22 17 26 50 6 63 7 25 41 92 29 36 80 11 4 10 14 77 75 53 82 46 24 56 46 82 36 80 75 8 45 24 22 90 34 45 76 18 38 86 43 7 49 80 56 90 53 12 51 98 47 44 58 32 4 2 6 3 60 38 72 74 46 30 86 1 98\r\n", "output": "2499\r\n"}, {"input": "6\r\n63 13 100 54 31 15 29 58 59 44 2 99 70 33 97 14 70 12 73 42 65 71 68 67 87 83 43 84 18 41 37 22 81 24 27 11 57 28 83 92 39 1 56 15 16 67 16 97 31 52 50 65 63 89 8 52 55 20 71 27 28 35 86 92 94 60 10 65 83 63 89 71 34 20 78 40 34 62 2 86 100 81 87 69 25 4 52 17 57 71 62 38 1 3 54 71 34 85 20 60 80 23 82 47 4 19 7 18 14 18 28 27 4 55 26 71 45 9 2 40 67 28 32 19 81 92\r\n", "output": "2465\r\n"}, {"input": "6\r\n87 62 58 32 81 92 12 50 23 27 38 39 64 74 16 35 84 59 91 87 14 48 90 47 44 95 64 45 31 11 67 5 80 60 36 15 91 3 21 2 40 24 37 69 5 50 23 37 49 19 68 21 49 9 100 94 45 41 22 31 31 48 25 70 25 25 95 88 82 1 37 53 49 31 57 74 94 45 55 93 43 37 13 85 59 72 15 68 3 90 96 55 100 64 63 69 43 33 66 84 57 97 87 34 23 89 97 77 39 89 8 92 68 13 50 36 95 61 71 96 73 13 30 49 57 89\r\n", "output": "2513\r\n"}]
| false |
stdio
| null | true |
961/E
|
961
|
E
|
PyPy 3
|
TESTS
| 4 | 93 | 0 |
109471890
|
n = int(input())
a = list(map(int, input().split()))
cnt = 0
tmp = 0
L = [0]*(n+1)
if a[0] <= n:
L[a[0]] = 1
tmp = 1
for i in range(1, n):
tmp -= L[i]
cnt += tmp
if a[i] <= n:
L[a[i]] += 1
tmp += 1
print(cnt)
| 30 | 732 | 24,268,800 |
58653185
|
from sys import stdin
from sys import setrecursionlimit as SRL; SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
n = int(input())
a = list(rrd())
bit = [0]*(n+100)
def ins(x):
while x<=n:
bit[x] += 1
x += x&(-x)
def get(l):
tot = 0
while l:
tot += bit[l]
l -= l&(-l)
return tot
ta = []
pans = 0
for i in range(len(a)):
if a[i] > i:
pans += 1
ta.append([i+1, a[i]])
ta.sort(key=lambda x: x[1])
ans = 0
now = 0
for x in ta:
while now < min(n,x[1]):
if a[now] <= n:
ins(a[now])
now += 1
ans += now - get(x[0]-1)
print((ans-pans)//2)
|
Educational Codeforces Round 41 (Rated for Div. 2)
|
ICPC
| 2,018 | 2 | 256 |
Tufurama
|
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
|
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
|
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
| null |
Possible pairs in the second example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 2, y = 3 (season 2 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 2);
3. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
In the third example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
|
[{"input": "5\n1 2 3 4 5", "output": "0"}, {"input": "3\n8 12 7", "output": "3"}, {"input": "3\n3 2 1", "output": "2"}]
| 1,900 |
["data structures"]
| 30 |
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "3\r\n8 12 7\r\n", "output": "3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 3 4 5 6\r\n", "output": "4\r\n"}, {"input": "8\r\n7 2 6 6 5 1 4 9\r\n", "output": "9\r\n"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "45\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
959/D
|
959
|
D
|
PyPy 3
|
TESTS
| 5 | 249 | 32,665,600 |
123057752
|
m=2*10**5
prime=[0 for i in range(m)]
n=int(input())
arr=list(map(int,input().split()))
s=set(arr)
i=0
for i in range(n):
jump =arr[i]
if prime[jump] ==1:
for k in range(jump,m):
if prime[k] ==0:
arr[i] = k
for l in range(k,m,k):
prime[l] =1
break
break
s=set()
l=2
while l*l <=arr[i]:
while jump %l ==0:
jump //=l
s.add(l)
l+=1
if jump >1:
s.add(jump)
for p in s:
for j in range(p,m,p):
prime[j] =1
i+=1
for k in range(2,m):
if i ==n:
break
if prime[k] ==0:
arr[i] =k
for l in range(k,m,k):
prime[l] =1
i+=1
print(*arr)
| 55 | 951 | 52,633,600 |
208257020
|
# https://codeforces.com/contest/959
import sys
input = lambda: sys.stdin.readline().rstrip() # faster!
class PrimesUtil:
def __init__(self, mx):
assert 1 <= mx
self.mx = mx
self.prime_numbers = []
self.prime = [False] * 2 + [True] * (mx - 1)
self.factor = [0] * (mx + 1)
self._run_sieve()
def _run_sieve(self):
"""Runs the sieve of Eratosthenes (fast version from stackoverflow)."""
if self.mx >= 2: # 2
self.prime_numbers += [2]
self.prime[4::2] = [False] * ((self.mx - 4) // 2 + 1)
self.factor[2] = 2
self.factor[4::2] = [2] * ((self.mx - 4) // 2 + 1)
for i in range(3, self.mx + 1, 2): # 3 onwards
if self.prime[i]:
self.prime_numbers += [i]
self.prime[i * i::2 * i] = [False] * (((self.mx + 1) - i * i - 1) // (2 * i) + 1)
self.factor[i] = i
self.factor[i * i::2 * i] = [i] * (((self.mx + 1) - i * i - 1) // (2 * i) + 1)
def is_prime(self, n):
"""Checks whether a number is prime. For numbers larger than self.mx, the Miller-Rabin test is used."""
assert 1 <= n < 3_317_044_064_679_887_385_961_981
return self.prime[n] if n <= self.mx else PrimesUtil.is_prime_miller_rabin(n)
@classmethod
def is_prime_miller_rabin(cls, n):
"""Checks whether a number is prime according to the Miller–Rabin primality test."""
assert 1 <= n < 3_317_044_064_679_887_385_961_981
if n <= 4:
return n == 2 or n == 3
d, pow2 = n - 1, 0
while d % 2 == 0:
d //= 2
pow2 += 1
if n < 2_152_302_898_747:
bases = [2, 3, 5, 7, 11]
elif n < 341_550_071_728_321:
bases = [2, 3, 5, 7, 11, 13, 17]
else:
bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
for b in bases:
x = pow(b, d, n)
if x == 0 or x == 1 or x == n - 1:
continue
for _ in range(1, pow2):
x = (x * x) % n
if x == 1:
return False
if x == n - 1:
break
if x != n - 1:
return False
return True
def factorise(self, n):
"""Returns a list of tuples (prime, exponent) representing the prime factorisation of n.
Performance depends on whether the number is from [1, self.mx] or from [self.mx+1, self.max**2]."""
assert 1 <= n <= self.mx ** 2
prime_factorisation = []
if n <= self.mx:
while n > 1:
prime_number, exp = self.factor[n], 0
while n % prime_number == 0:
exp += 1
n //= prime_number
prime_factorisation += [(prime_number, exp)]
else:
for prime_number in self.prime_numbers:
if prime_number ** 2 > n:
break
if n % prime_number == 0:
exp = 0
while n % prime_number == 0:
exp += 1
n //= prime_number
prime_factorisation += [(prime_number, exp)]
if n > 1:
prime_factorisation += [(n, 1)]
return prime_factorisation
def divisors(self, n):
"""Returns all divisors of a number as an unsorted list.
Performance depends on whether number is from [1, self.mx] or from [self.mx+1, self.max**2]."""
assert 1 <= n <= self.mx ** 2
from itertools import product
from math import prod
div = []
prime_factorisation = self.factorise(n)
exp_ranges = [list(range(exp + 1)) for (_, exp) in prime_factorisation]
for exponents in product(*exp_ranges):
div += [prod(prime_factorisation[i][0] ** exponents[i] for i in range(len(prime_factorisation)))]
return div
n = int(input()) # 1 <= n <= 10**5
a = list(map(int, input().split())) # 2 <= n <= 10**5
b = [0] * n
PU = PrimesUtil(10 ** 6)
primes_used = set()
larger = False
next_number = 1
for i, v in enumerate(a):
if not larger:
pf = PU.factorise(v)
ok = all(p not in primes_used for p, _ in pf)
if ok:
b[i] = v
primes_used |= {p for p, _ in pf}
else:
x = v
while True:
x += 1
pf = PU.factorise(x)
ok = all(p not in primes_used for p, _ in pf)
if ok:
b[i] = x
larger = True
primes_used |= {p for p, _ in pf}
break
else:
while True:
next_number += 1
pf = PU.factorise(next_number)
ok = all(p not in primes_used for p, _ in pf)
if ok:
b[i] = next_number
primes_used |= {p for p, _ in pf}
break
print(*b)
|
Codeforces Round 473 (Div. 2)
|
CF
| 2,018 | 3 | 256 |
Mahmoud and Ehab and another array construction task
|
Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same length such that:
- b is lexicographically greater than or equal to a.
- bi ≥ 2.
- b is pairwise coprime: for every 1 ≤ i < j ≤ n, bi and bj are coprime, i. e. GCD(bi, bj) = 1, where GCD(w, z) is the greatest common divisor of w and z.
Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it?
An array x is lexicographically greater than an array y if there exists an index i such than xi > yi and xj = yj for all 1 ≤ j < i. An array x is equal to an array y if xi = yi for all 1 ≤ i ≤ n.
|
The first line contains an integer n (1 ≤ n ≤ 105), the number of elements in a and b.
The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 105), the elements of a.
|
Output n space-separated integers, the i-th of them representing bi.
| null |
Note that in the second sample, the array is already pairwise coprime so we printed it.
|
[{"input": "5\n2 3 5 4 13", "output": "2 3 5 7 11"}, {"input": "3\n10 3 7", "output": "10 3 7"}]
| 1,900 |
["constructive algorithms", "greedy", "math", "number theory"]
| 55 |
[{"input": "5\r\n2 3 5 4 13\r\n", "output": "2 3 5 7 11 "}, {"input": "3\r\n10 3 7\r\n", "output": "10 3 7 "}, {"input": "5\r\n7 10 2 5 5\r\n", "output": "7 10 3 11 13 "}, {"input": "7\r\n20 9 7 6 7 9 15\r\n", "output": "20 9 7 11 13 17 19 "}, {"input": "10\r\n5 3 2 2 3 3 3 4 2 5\r\n", "output": "5 3 2 7 11 13 17 19 23 29 "}, {"input": "3\r\n3 18 2\r\n", "output": "3 19 2 "}]
| false |
stdio
| null | true |
103/A
|
103
|
A
|
Python 3
|
TESTS
| 3 | 92 | 0 |
186348050
|
n = int(input())
x = [int(i) for i in input().split()]
c = 0
for i in range(n):
c += x[i]
if x[i] != 1:
c += i
print(c)
| 25 | 92 | 0 |
150953602
|
n=int(input())
c=0
a=[int(x) for x in input().split()]
for i in range(n-1,-1,-1):
if c==0:
c+=a[i]
prev=a[i]
else:
x=a[i]-1+prev
prev=x
c+=x
print(c)
|
Codeforces Beta Round 80 (Div. 1 Only)
|
CF
| 2,011 | 2 | 256 |
Testing Pants for Sadness
|
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".
The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.
A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.
Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
|
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
|
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
| null |
Note to the second sample. In the worst-case scenario you will need five clicks:
- the first click selects the first variant to the first question, this answer turns out to be wrong.
- the second click selects the second variant to the first question, it proves correct and we move on to the second question;
- the third click selects the first variant to the second question, it is wrong and we go back to question 1;
- the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
- the fifth click selects the second variant to the second question, it proves correct, the test is finished.
|
[{"input": "2\n1 1", "output": "2"}, {"input": "2\n2 2", "output": "5"}, {"input": "1\n10", "output": "10"}]
| 1,100 |
["greedy", "implementation", "math"]
| 25 |
[{"input": "2\r\n1 1\r\n", "output": "2"}, {"input": "2\r\n2 2\r\n", "output": "5"}, {"input": "1\r\n10\r\n", "output": "10"}, {"input": "3\r\n2 4 1\r\n", "output": "10"}, {"input": "4\r\n5 5 3 1\r\n", "output": "22"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2999999999"}, {"input": "10\r\n5 7 8 1 10 3 6 4 10 6\r\n", "output": "294"}, {"input": "100\r\n5 7 5 3 5 4 6 5 3 6 4 6 6 2 1 9 6 5 3 8 4 10 1 9 1 3 7 6 5 5 8 8 7 7 8 9 2 10 3 5 4 2 6 10 2 6 9 6 1 9 3 7 7 8 3 9 9 5 10 10 3 10 7 8 3 9 8 3 2 4 10 2 1 1 7 3 9 10 4 6 9 8 2 1 4 10 1 10 6 8 7 5 3 3 6 2 7 10 3 8\r\n", "output": "24212"}, {"input": "10\r\n12528238 329065023 620046219 303914458 356423530 751571368 72944261 883971060 123105651 868129460\r\n", "output": "27409624352"}, {"input": "1\r\n84355694\r\n", "output": "84355694"}, {"input": "2\r\n885992042 510468669\r\n", "output": "1906929379"}, {"input": "100\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "100"}, {"input": "100\r\n2 1 2 2 2 2 1 2 2 1 2 2 2 1 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 2 1 1 2 1 2 1 1 1 2 2 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 1 1 1 2 2 1 1 2 1 1 2 2 2 2 1 2 2 2 1 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1\r\n", "output": "2686"}, {"input": "100\r\n1 3 2 1 1 2 1 3 2 2 3 1 1 1 2 2 1 3 3 1 1 2 2 3 2 1 3 1 3 2 1 1 3 3 2 1 2 2 2 3 2 2 3 2 2 3 2 1 3 1 1 2 1 3 2 2 1 1 1 1 1 1 3 1 2 3 1 1 1 1 1 2 3 3 1 1 1 1 2 3 3 1 3 2 2 3 2 1 3 2 2 3 1 1 3 2 3 2 3 1\r\n", "output": "4667"}]
| false |
stdio
| null | true |
327/C
|
327
|
C
|
PyPy 3-64
|
TESTS
| 4 | 124 | 0 |
175012679
|
import sys
n = input(); m = 1000000007
ans = 0; r = pow(2,len(n),m); c = len(n);
d = int(sys.stdin.readline()); u = pow(r,d,m)
for x in range(c) :
if not int(n[x])%5 : ans = (ans + (1-(u)) // (1-r) * pow(2,x))
print(ans%m)
| 33 | 124 | 102,400 |
219438060
|
t, k = input(), int(input())
s, n, d = 0, 1, 1000000007
for i in t:
if i in '05': s += n
n = (n << 1) % d
p = (pow(n, k, d) - 1) * pow(n - 1, d - 2, d)
print(((p % d) * (s % d)) % d)
|
Codeforces Round 191 (Div. 2)
|
CF
| 2,013 | 1 | 256 |
Magic Five
|
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
|
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
|
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
| null |
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
|
[{"input": "1256\n1", "output": "4"}, {"input": "13990\n2", "output": "528"}, {"input": "555\n2", "output": "63"}]
| 1,700 |
["combinatorics", "math"]
| 33 |
[{"input": "1256\r\n1\r\n", "output": "4\r\n"}, {"input": "13990\r\n2\r\n", "output": "528\r\n"}, {"input": "555\r\n2\r\n", "output": "63\r\n"}, {"input": "14\r\n178\r\n", "output": "0\r\n"}, {"input": "27755776656210607832788619414635535178188775623838313967013958143619017005079991285469853503718562504927535176713879737569375166451462839457844835806559098448980069427607\r\n151\r\n", "output": "319271478\r\n"}, {"input": "205831218776360805549796263726315728152440389522084825015113219980083245807721536032762703389\r\n161\r\n", "output": "97770312\r\n"}, {"input": "58761716080256287618526160263668449282855983776878138369871377531384\r\n100\r\n", "output": "48078375\r\n"}, {"input": "28626813825922172933379733204622160613220115755143268169598722697537715419\r\n184\r\n", "output": "43220279\r\n"}, {"input": "0\r\n1000000000\r\n", "output": "140625000\r\n"}, {"input": "079797897977\r\n1\r\n", "output": "1\r\n"}]
| false |
stdio
| null | true |
621/C
|
621
|
C
|
PyPy 3
|
TESTS
| 3 | 62 | 0 |
138280238
|
def countDivisibles(A, B, M):
if (A % M == 0):
return ((B // M) - (A // M)) + 1
return ((B // M) - (A // M))
n,p = map(int,input().split())
A = []
B = []
tot = 1
for i in range(n):
l,r = map(int,input().split())
x = countDivisibles(l,r,p)
B.append(x)
A.append(r-l+1)
tot*=(r-l+1)
res = 0
for i in range(n):
res += B[i]*2*(tot//A[i])
left_div = B[(i-1+n)%n]
right_div = B[(i+1)%n]
res += (left_div*right_div*2 + left_div*(A[(i+1)%n] - right_div) + right_div*(A[(i-1+n)%n] - left_div))*(tot//(A[(i-1+n)%n]*A[i]*A[(i+1)%n]))
res*= 1000
print(res/tot)
| 94 | 171 | 11,059,200 |
174827688
|
import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
######################################################################################
#--------------------------------------funs here-------------------------------------#
######################################################################################
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(sys.stdin.readline())
def instr(): return sys.stdin.readline().strip()
def words(): return [i for i in sys.stdin.readline().strip().split()]
def chars(): return [i for i in sys.stdin.readline().strip()]
######################################################################################
#--------------------------------------code here-------------------------------------#
######################################################################################
def solve():
n,p=values()
l=[]
for i in range(n):
a,b=sorted(values())
ln=b-a+1
d=b//p -(a-1)//p
l.append((ln,d))
l.append(l[0])
ans=0
for i in range(n):
n1,a1=l[i]
n2,a2=l[i+1]
ans+=1-(n1-a1)*(n2-a2)/(n1*n2)
print(ans*2000)
if __name__ == "__main__":
# for i in range(inp()):
solve()
# n, p = [int(x) for x in input().split()]
# Arr = []
# for i in range(n):
# tmp1, tmp2 = [int(x) for x in input().split()]
# cnt = tmp2 // p - (tmp1 - 1) // p
# Arr.append(cnt / (tmp2 - tmp1 + 1))
# Arr.append(Arr[0])
# ans = 0.0
# for i in range(n):
# p1 = Arr[i]
# p2 = Arr[i + 1]
# ans += p1 + p2 - p1 * p2
# print(ans * 2000)
|
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 |
28/A
|
28
|
A
|
Python 3
|
TESTS
| 6 | 92 | 0 |
12998854
|
def main():
n, m = map(int, input().split())
if m * 2 < n:
print("NO")
return
tmp = list(tuple(map(int, input().split())) for _ in range(n))
tmp += tmp[:2]
nails = [abs(a - c) + abs(b - d) for (a, b), (c, d) in zip(tmp, tmp[2:])]
segments = list(map(int, input().split()))
iseg = sorted(range(m), key=segments.__getitem__, reverse=True)
for shift in (-1, 0):
res = [-1] * n
for nailidx, segidx in zip(sorted(range(shift, n + shift, 2), key=nails.__getitem__, reverse=True), iseg):
res[(nailidx + 1) % n] = segidx + 1
if nails[nailidx] > segments[segidx]:
break
else:
print("YES")
print(" ".join(map(str, res)))
return
print("NO")
if __name__ == '__main__':
main()
| 51 | 216 | 307,200 |
68238752
|
n,m = map(int,input().split())
s = []
for i in range(n):
a = map(int,input().split())
a = list(a)
s.append(a)
s_chet = []
for i in range(1,n-1,2): #Проход по четным гвоздям
q = abs((s[i][0]-s[i-1][0])+(s[i][1]-s[i-1][1])) + abs((s[i][0]-s[i+1][0])+(s[i][1]-s[i+1][1]))
s_chet.append((i + 1, q))
q = abs((s[-1][0]-s[-2][0])+(s[-1][1]-s[-2][1])) + abs((s[-1][0]-s[0][0])+(s[-1][1]-s[0][1]))
s_chet.append((n, q))
s_nechet = []
for i in range(2,n-1,2): #Проход по нечетным гвоздям
q = abs((s[i][0]-s[i-1][0])+(s[i][1]-s[i-1][1])) + abs((s[i][0]-s[i+1][0])+(s[i][1]-s[i+1][1]))
s_nechet.append((i + 1, q))
q = abs((s[-1][0]-s[0][0])+(s[-1][1]-s[0][1])) + abs((s[1][0]-s[0][0])+(s[1][1]-s[0][1]))
s_nechet.append((1, q))
ss = map(int, input().split())
ss = [[i + 1, ss_i] for i, ss_i in enumerate(ss)]
s_chet.sort(key=lambda x: x[1])
s_nechet.sort(key=lambda x: x[1])
ss.sort(key=lambda x: x[1])
prut_chet = [-1] * n
chet = True
j = 0
for i in range(len(s_chet)):
while j < len(ss) and ss[j][1] < s_chet[i][1]:
j += 1
if j == len(ss) or ss[j][1] > s_chet[i][1]:
chet = False
break
if s_chet[i][1] == ss[j][1]:
prut_chet[s_chet[i][0] - 1] = ss[j][0]
j += 1
continue
if chet:
print('YES')
print(" ".join(map(str,prut_chet)))
else:
prut_nechet = [-1] * n
nechet = True
j = 0
for i in range(len(s_nechet)):
while j < len(ss) and ss[j][1] < s_nechet[i][1]:
j += 1
if j == len(ss) or ss[j][1] > s_nechet[i][1]:
nechet = False
break
if s_nechet[i][1] == ss[j][1]:
prut_nechet[s_nechet[i][0] - 1] = ss[j][0]
j += 1
continue
if nechet:
print('YES')
print(" ".join(map(str, prut_nechet)))
else:
print('NO')
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010 | 2 | 256 |
Bender Problem
|
Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods.
Help Bender to solve this difficult task.
|
The first line contains two positive integers n and m (4 ≤ n ≤ 500, 2 ≤ m ≤ 500, n is even) — the amount of nails and the amount of rods. i-th of the following n lines contains a pair of integers, denoting the coordinates of the i-th nail. Nails should be connected in the same order as they are given in the input. The last line contains m integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200 000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line.
|
If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output n numbers — i-th of them should be the number of rod, which fold place is attached to the i-th nail, or -1, if there is no such rod.
If there are multiple solutions, print any of them.
| null | null |
[{"input": "4 2\n0 0\n0 2\n2 2\n2 0\n4 4", "output": "YES\n1 -1 2 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3", "output": "YES\n1 -1 2 -1 3 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3", "output": "NO"}]
| 1,600 |
["implementation"]
| 51 |
[{"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n4 4\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n3 2 3\r\n", "output": "YES\r\n1 -1 2 -1 3 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2 3\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n0 0\r\n0 1\r\n1 1\r\n1 0\r\n1 1 1 1\r\n", "output": "NO\r\n"}, {"input": "6 2\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "6 3\r\n0 0\r\n2 0\r\n2 2\r\n1 2\r\n1 1\r\n0 1\r\n4 2 2\r\n", "output": "YES\r\n-1 1 -1 2 -1 3 "}, {"input": "4 4\r\n-8423 7689\r\n6902 7689\r\n6902 2402\r\n-8423 2402\r\n20612 20612 91529 35617\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "4 4\r\n1679 -198\r\n9204 -198\r\n9204 -5824\r\n1679 -5824\r\n18297 92466 187436 175992\r\n", "output": "NO\r\n"}, {"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n200000 200000\r\n", "output": "NO\r\n"}]
| false |
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
with open(input_path) as f:
n, m = map(int, f.readline().split())
nails = [tuple(map(int, f.readline().split())) for _ in range(n)]
rods = list(map(int, f.readline().split()))
with open(output_path) as f:
ref_lines = f.read().splitlines()
with open(submission_path) as f:
sub_lines = f.read().splitlines()
if not sub_lines:
print(0)
return
ref_is_no = ref_lines and ref_lines[0].strip() == "NO"
sub_is_no = sub_lines and sub_lines[0].strip() == "NO"
if ref_is_no:
print(1 if sub_is_no else 0)
return
else:
if not sub_lines or sub_lines[0].strip() != "YES":
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
sub_list = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(sub_list) != n:
print(0)
return
used_rods = set()
for num in sub_list:
if num == -1:
continue
if num < 1 or num > m or num in used_rods:
print(0)
return
used_rods.add(num)
for i in range(n):
rod_num = sub_list[i]
if rod_num == -1:
continue
prev_i = i - 1 if i > 0 else n - 1
next_i = (i + 1) % n
x_prev, y_prev = nails[prev_i]
x_curr, y_curr = nails[i]
x_next, y_next = nails[next_i]
len_prev = abs(y_curr - y_prev) if x_prev == x_curr else abs(x_curr - x_prev)
len_next = abs(y_next - y_curr) if x_curr == x_next else abs(x_next - x_curr)
sum_len = len_prev + len_next
rod_index = rod_num - 1
if rod_index >= len(rods) or rods[rod_index] != sum_len:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true |
313/D
|
313
|
D
|
PyPy 3-64
|
TESTS
| 3 | 154 | 3,788,800 |
216826114
|
from collections import deque
import math
mod = 10**9 + 7
fact = [1] * (2*10**5+10)
inf = 10**18
def gcd(a, b):
while a > 0 and b > 0:
if a >= b:
a %= b
else:
b %= a
return max(a, b)
def nok(a, b):
return a * b // gcd(a, b)
def is_prime(a):
for i in range(2, int(a**(1/2)) + 1):
if a % i == 0:
return False
return True
def razl(a):
res = []
for i in range(2, int(a**(1/2)) + 1):
while a % i == 0:
res.append(i)
a //= i
if a != 1:
res.append(a)
return res
def is_prime_big(a):
if pow(a-1, a-1, a) == 1:
return True
else:
return False
def get_ret(n, mod):
return pow(n, mod-2, mod)
for i in range(2, len(fact)):
fact[i] = (fact[i-1] * i) % mod
def ck(n, k):
return fact[n] * get_ret(fact[n-k] * fact[k])
def prog(start, finish, d):
kol = (finish - start) // d + 1
return (start + finish) * kol // 2
def to_bin(a):
res = []
while a > 0:
res.append(a % 2)
a //= 2
res.reverse()
return res
def to_dec(a):
return sum([a[i] * pow(2, len(a) - i - 1) for i in range(len(a))])
def I():
return int(input())
def II():
return [int(i) for i in input().split()]
def IS():
return [int(i) for i in input()]
def main():
n, m, k = II()
comp, dp = {i:[] for i in range(n)}, [[inf for j in range(n+1)] for i in range(n+1)]
for i in range(m):
li, ri, ci = II()
comp[li-1].append([ri-1, ci])
for i in range(n+1):
dp[i][0] = 0
for i in list(comp.keys()):
for j in range(1, n+1):
for l in range(len(comp[i])):
r, c = comp[i][l][0], comp[i][l][1]
lenght = r - i + 1
if j >= lenght:
for z in range(i+1):
dp[r+1][j] = min(dp[r+1][j], dp[z][j-lenght] + c)
ans = inf
# for i in range(n):
# for j in range(n+1):
# if dp[i][j] == inf:
# print("inf", end=' ')
# else:
# print(dp[i][j], end=' ')
# print()
for i in range(n):
for j in range(k, n+1):
ans = min(ans, dp[i][j])
if ans == inf:
print(-1)
else:
print(ans)
if __name__ == "__main__":
main()
| 77 | 1,932 | 10,240,000 |
232738001
|
INF = 10**18
n, m, k = map(int, input().split())
cost = [[INF]*(n+1) for i in range(n+1)]
dp = [[INF]*(n+1) for i in range(n+1)]
for i in range(m):
l, r, c = map(int, input().split())
for j in range(l, r+1):
cost[j][r] = min(cost[j][r], c)
dp[0][0] = 0
for i in range(n):
for j in range(k+1):
dp[i+1][j] = min(dp[i+1][j], dp[i][j])
for len in range(n+1):
if i+len <= n and j+len <= k:
dp[i+len][j+len] = min(dp[i+len][j+len], dp[i][j] + cost[i+1][i+len])
if dp[n][k] == INF : print(-1)
else : print(dp[n][k])
|
Codeforces Round 186 (Div. 2)
|
CF
| 2,013 | 3 | 256 |
Ilya and Roads
|
Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as n holes in a row. We will consider the holes numbered from 1 to n, from left to right.
Ilya is really keep on helping his city. So, he wants to fix at least k holes (perharps he can fix more) on a single ZooVille road.
The city has m building companies, the i-th company needs ci money units to fix a road segment containing holes with numbers of at least li and at most ri. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment.
Determine the minimum money Ilya will need to fix at least k holes.
|
The first line contains three integers n, m, k (1 ≤ n ≤ 300, 1 ≤ m ≤ 105, 1 ≤ k ≤ n). The next m lines contain the companies' description. The i-th line contains three integers li, ri, ci (1 ≤ li ≤ ri ≤ n, 1 ≤ ci ≤ 109).
|
Print a single integer — the minimum money Ilya needs to fix at least k holes.
If it is impossible to fix at least k holes, print -1.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null | null |
[{"input": "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6", "output": "17"}, {"input": "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13", "output": "2"}, {"input": "10 1 9\n5 10 14", "output": "-1"}]
| 2,100 |
["dp"]
| 77 |
[{"input": "10 4 6\r\n7 9 11\r\n6 9 13\r\n7 7 7\r\n3 5 6\r\n", "output": "17\r\n"}, {"input": "10 7 1\r\n3 4 15\r\n8 9 8\r\n5 6 8\r\n9 10 6\r\n1 4 2\r\n1 4 10\r\n8 10 13\r\n", "output": "2\r\n"}, {"input": "10 1 9\r\n5 10 14\r\n", "output": "-1\r\n"}, {"input": "10 6 9\r\n6 8 7\r\n2 8 11\r\n2 6 10\r\n8 10 9\r\n2 5 8\r\n2 3 8\r\n", "output": "20\r\n"}, {"input": "10 6 8\r\n3 6 7\r\n1 4 3\r\n2 7 10\r\n4 7 4\r\n7 10 15\r\n4 7 7\r\n", "output": "18\r\n"}, {"input": "10 4 10\r\n1 1 11\r\n7 7 15\r\n2 3 11\r\n2 8 6\r\n", "output": "-1\r\n"}, {"input": "10 3 7\r\n4 6 6\r\n5 7 1\r\n2 10 15\r\n", "output": "15\r\n"}, {"input": "10 5 3\r\n2 10 10\r\n3 6 10\r\n5 5 7\r\n2 7 4\r\n2 7 6\r\n", "output": "4\r\n"}, {"input": "10 5 4\r\n2 8 3\r\n4 7 15\r\n1 1 13\r\n7 9 10\r\n10 10 2\r\n", "output": "3\r\n"}, {"input": "1 1 1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "10 2 6\r\n1 7 1123\r\n2 10 33\r\n", "output": "33\r\n"}, {"input": "5 2 5\r\n1 3 1\r\n2 5 1\r\n", "output": "2\r\n"}, {"input": "1 3 1\r\n1 1 5\r\n1 1 3\r\n1 1 12\r\n", "output": "3\r\n"}, {"input": "3 3 3\r\n1 2 1000000000\r\n2 3 1000000000\r\n1 1 1000000000\r\n", "output": "2000000000\r\n"}]
| false |
stdio
| null | true |
523/A
|
523
|
A
|
Python 3
|
TESTS
| 0 | 78 | 0 |
92510844
|
w,h=map(int,input().split())
q,r=[],[[]for i in range(w)]
for i in range(h):
q.append(list(input()))
for i in range(w):
for j in range(h):
r[i].append(q[j][i]*2)
for i in r:
print(*i)
print(*i)
| 24 | 61 | 0 |
13001492
|
def main():
w, h = map(int, input().split())
h *= 2
l = [None] * h
for i in range(0, h, 2):
l[i] = l[i + 1] = input()
l = [''.join(_) for _ in zip(*l)]
for s in l:
print(s)
print(s)
if __name__ == '__main__':
main()
|
VK Cup 2015 - Qualification Round 2
|
CF
| 2,015 | 2 | 256 |
Rotate, Flip and Zoom
|
Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.
He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).
Implement this feature to help Polycarp test his editor.
|
The first line contains two integers, w and h (1 ≤ w, h ≤ 100) — the width and height of an image in pixels. The picture is given in h lines, each line contains w characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters "." and "*", as the image is monochrome.
|
Print 2w lines, each containing 2h characters — the result of consecutive implementing of the three transformations, described above.
| null | null |
[{"input": "3 2\n.*.\n.*.", "output": "....\n....\n****\n****\n....\n...."}, {"input": "9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"}]
| 1,200 |
["*special", "implementation"]
| 24 |
[{"input": "3 2\r\n.*.\r\n.*.\r\n", "output": "....\r\n....\r\n****\r\n****\r\n....\r\n....\r\n"}, {"input": "9 20\r\n**.......\r\n****.....\r\n******...\r\n*******..\r\n..******.\r\n....****.\r\n......***\r\n*.....***\r\n*********\r\n*********\r\n*********\r\n*********\r\n....**...\r\n...****..\r\n..******.\r\n.********\r\n****..***\r\n***...***\r\n**.....**\r\n*.......*\r\n", "output": "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********\n"}, {"input": "1 100\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n", "output": "........................................................................................................................................................................................................\r\n........................................................................................................................................................................................................\r\n"}, {"input": "1 100\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n", "output": "********************************************************************************************************************************************************************************************************\r\n********************************************************************************************************************************************************************************************************\r\n"}, {"input": "1 100\r\n.\r\n*\r\n.\r\n.\r\n.\r\n*\r\n.\r\n.\r\n.\r\n*\r\n*\r\n*\r\n.\r\n.\r\n.\r\n.\r\n.\r\n.\r\n*\r\n.\r\n.\r\n.\r\n*\r\n.\r\n*\r\n.\r\n.\r\n*\r\n*\r\n.\r\n*\r\n.\r\n.\r\n*\r\n.\r\n.\r\n*\r\n*\r\n.\r\n.\r\n.\r\n.\r\n.\r\n*\r\n.\r\n*\r\n.\r\n*\r\n.\r\n.\r\n.\r\n.\r\n*\r\n*\r\n*\r\n.\r\n.\r\n.\r\n.\r\n*\r\n.\r\n.\r\n*\r\n*\r\n*\r\n*\r\n.\r\n*\r\n*\r\n*\r\n*\r\n*\r\n.\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n.\r\n.\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n*\r\n.\r\n.\r\n*\r\n.\r\n.\r\n*\r\n*\r\n.\r\n", "output": "..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n..**......**......******............**......**..**....****..**....**....****..........**..**..**........******........**....********..**********..********************....**************....**....****..\r\n"}, {"input": "100 1\r\n....................................................................................................\r\n", "output": "..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n"}, {"input": "100 1\r\n****************************************************************************************************\r\n", "output": "**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n"}, {"input": "100 1\r\n*...***.....**.*...*.*.**.************.**..**.*..**..**.*.**...***.*...*.*..*.*.*......**..*..*...**\r\n", "output": "**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n**\n**\n..\n..\n..\n..\n..\n..\n**\n**\n**\n**\n"}, {"input": "1 1\r\n.\r\n", "output": "..\r\n..\r\n"}, {"input": "1 1\r\n*\r\n", "output": "**\r\n**\r\n"}, {"input": "2 2\r\n.*\r\n*.\r\n", "output": "..**\r\n..**\r\n**..\r\n**..\r\n"}, {"input": "1 2\r\n*\r\n.\r\n", "output": "**..\r\n**..\r\n"}, {"input": "2 1\r\n*.\r\n", "output": "**\r\n**\r\n..\r\n..\r\n"}]
| false |
stdio
| null | true |
961/E
|
961
|
E
|
PyPy 3
|
TESTS
| 4 | 140 | 102,400 |
93962819
|
import sys
from collections import defaultdict
class BIT_RSQ(object):
__slots__ = ['nodes', 'size']
def __init__(self, size: int):
self.nodes = [0]*(size+1)
self.size = size+1
def add(self, index: int, value: int):
while index < self.size:
self.nodes[index] += value
index += index & -index
def sum(self, right: int):
result = 0
while right:
result += self.nodes[right]
right -= right & -right
return result
n = int(input())
a = list(map(int, input().split()))
bit = BIT_RSQ(n+10)
remove = defaultdict(list)
ans = 0
for i, x in enumerate(a, start=1):
ans += bit.sum(min(i, x))
bit.add(i, 1)
remove[min(n+1, x)].append(i)
for j in remove[i]:
bit.add(j, -1)
print(ans)
| 30 | 1,465 | 77,004,800 |
36972134
|
import sys
class RangeBit:
def __init__(self, n):
sz = 1
while n >= sz:
sz *= 2
self.size = sz
self.dataAdd = [0 for _ in range(sz)]
self.dataMul = [0 for _ in range(sz)]
def sum(self, i):
assert i > 0
add, mul, start = 0, 0, i
while i > 0:
add += self.dataAdd[i]
mul += self.dataMul[i]
i -= i & -i
return mul * start + add
def add(self, left, right, by):
assert 0 < left <= right
self._add(left, by, -by * (left - 1))
self._add(right, -by, by * right)
def _add(self, i, mul, add):
assert i > 0
while i < self.size:
self.dataAdd[i] += add
self.dataMul[i] += mul
i += i & -i
n = int(input())
l = list(map(int, sys.stdin.readline().split()))
queries = []
for i in range(n):
if min(l[i], n) >= i+2:
queries.append((i+2, min(l[i], n), i+1))
result = 0
a = sorted(list(zip(range(1, n+1), l)) + queries, key=lambda x:(-x[-1], len(x)))
ft = RangeBit(n+1)
for el in a:
#print(el)
if len(el) == 2: #update
ind, val = el
ft.add(ind, ind, 1)
else: #query
fr, to, val = el
# print(fr, to, val)
# print(ft.sum(to) - (ft.sum(fr - 1) if fr > 1 else 0))
result += ft.sum(to) - (ft.sum(fr - 1) if fr > 1 else 0)
print(result)
|
Educational Codeforces Round 41 (Rated for Div. 2)
|
ICPC
| 2,018 | 2 | 256 |
Tufurama
|
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
|
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
|
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
| null |
Possible pairs in the second example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 2, y = 3 (season 2 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 2);
3. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
In the third example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
|
[{"input": "5\n1 2 3 4 5", "output": "0"}, {"input": "3\n8 12 7", "output": "3"}, {"input": "3\n3 2 1", "output": "2"}]
| 1,900 |
["data structures"]
| 30 |
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "3\r\n8 12 7\r\n", "output": "3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 3 4 5 6\r\n", "output": "4\r\n"}, {"input": "8\r\n7 2 6 6 5 1 4 9\r\n", "output": "9\r\n"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "45\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
961/E
|
961
|
E
|
PyPy 3
|
TESTS
| 4 | 108 | 20,275,200 |
128478540
|
from collections import defaultdict
import sys
input = sys.stdin.readline
def make_tree(n):
tree = [0] * (n + 1)
return tree
def get_sum(i):
s = 0
while i > 0:
s += tree[i]
i -= i & -i
return s
def add(i, x):
while i <= n:
tree[i] += x
i += i & -i
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(lambda : [])
tree = make_tree(n)
for i in range(n):
a[i] = min(a[i], n)
d[a[i]].append(i)
add(a[i], 1)
ans = 0
m = n - 1
for i in range(n):
add(a[i], -1)
ans += m - get_sum(i)
m -= 1
tree = make_tree(n)
for i in range(n, 0, -1):
for j in d[i]:
ans -= (n - i) - get_sum(j)
add(a[i - 1], 1)
print(ans)
| 30 | 498 | 44,236,800 |
128480912
|
from collections import defaultdict
import sys
input = sys.stdin.readline
def make_tree(n):
tree = [0] * (n + 1)
return tree
def get_sum(i):
s = 0
while i > 0:
s += tree[i]
i -= i & -i
return s
def get_sum_segment(s, t):
if s > t:
return 0
ans = get_sum(t) - get_sum(s - 1)
return ans
def add(i, x):
while i <= n:
tree[i] += x
i += i & -i
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(lambda : [])
tree = make_tree(n)
for i in range(n):
a[i] = min(a[i], n)
d[a[i]].append(i)
add(i + 1, 1)
ans = 0
for i in range(n):
ans += get_sum_segment(i + 2, a[i])
for j in d[i + 1]:
add(j + 1, -1)
print(ans)
|
Educational Codeforces Round 41 (Rated for Div. 2)
|
ICPC
| 2,018 | 2 | 256 |
Tufurama
|
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
|
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
|
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
| null |
Possible pairs in the second example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 2, y = 3 (season 2 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 2);
3. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
In the third example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
|
[{"input": "5\n1 2 3 4 5", "output": "0"}, {"input": "3\n8 12 7", "output": "3"}, {"input": "3\n3 2 1", "output": "2"}]
| 1,900 |
["data structures"]
| 30 |
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "3\r\n8 12 7\r\n", "output": "3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 3 4 5 6\r\n", "output": "4\r\n"}, {"input": "8\r\n7 2 6 6 5 1 4 9\r\n", "output": "9\r\n"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "45\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
961/E
|
961
|
E
|
PyPy 3
|
TESTS
| 4 | 93 | 20,172,800 |
123026758
|
def queries(indx1,indx2):
count =0
while indx1 <=indx2:
if indx1 %2==1:
count +=sgn_tree[indx1]
indx1 +=1
if indx2 %2==0:
count +=sgn_tree[indx2]
indx2 -=1
indx1 =indx1 >>1
indx2 =indx2 >>1
return count
def update(indx):
sgn_tree[indx] +=1
indx =indx >>1
while indx >=1:
sgn_tree[indx] =sgn_tree[2*indx] +sgn_tree[2*indx +1]
indx =indx >>1
n=int(input())
arr=list(map(int,input().split()))
sgn_tree=[0 for i in range(2*n)]
total =0
for i in range(n):
total +=queries(n-1+i+1,2*n-1)
if arr[i] >n:
arr[i] =n
update(n-1 +arr[i])
print(total)
| 30 | 655 | 22,528,000 |
100875670
|
from bisect import bisect_left, bisect_right
n = int(input())
a = list(map(int, input().split()))
ans = 0
#ans = slow_test()
class fenwick():
"""
This Tree Data Structure speeds up caliculating summations of partial sum
and also updating subsets of sequences. Both queries finish in logarithmic times.
"""
# 1-indexed
def __init__(self, n):
self.n = n
self.data = [0]*(n+1)
def to_sum(self, i):
# return sigma(a_j) (0<=j<=i)
s = 0
while i > 0:
s += self.data[i]
i -= (i & -i)
return s
def add(self, i, x):
#a_i -> a_i + x
while i <= self.n:
self.data[i] += x
i += (i & -i)
def get(self, i, j):
# return sigma(a_k) (i<=k<=j)
# assert 1<=i<=j<= N
return self.to_sum(j)-self.to_sum(i-1)
Q = fenwick(n + 10)
BIGGER = n + 5
seq = [(a[i], i) for i in range(n)]
A = a.copy()
for i in range(n):
if i < a[i]:
ans -= 1
a.sort()
seq.sort(key=lambda x: x[0])
lindex = 0
for a_r, r in seq:
if a_r > n:
ans += n - bisect_right(a, r)
continue
else:
for i in range(lindex, a_r):
if A[i] <= n:
Q.add(A[i], 1)
else:
Q.add(BIGGER, 1)
ans += Q.get(r+1, BIGGER + 1)
lindex = a_r
ans //= 2
print(ans)
|
Educational Codeforces Round 41 (Rated for Div. 2)
|
ICPC
| 2,018 | 2 | 256 |
Tufurama
|
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
|
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
|
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
| null |
Possible pairs in the second example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 2, y = 3 (season 2 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 2);
3. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
In the third example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
|
[{"input": "5\n1 2 3 4 5", "output": "0"}, {"input": "3\n8 12 7", "output": "3"}, {"input": "3\n3 2 1", "output": "2"}]
| 1,900 |
["data structures"]
| 30 |
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "3\r\n8 12 7\r\n", "output": "3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 3 4 5 6\r\n", "output": "4\r\n"}, {"input": "8\r\n7 2 6 6 5 1 4 9\r\n", "output": "9\r\n"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "45\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false |
stdio
| null | true |
963/B
|
963
|
B
|
Python 3
|
TESTS
| 6 | 108 | 7,577,600 |
37630613
|
from collections import defaultdict
class DesOfTree:
def main(self):
n = int(input())
tree = [int(x) for x in input().split()]
tree.insert(0,0)
self.graph = defaultdict(list)
for i in range(1, n+1):
dot.add_edge(i, tree[i])
for i in range(1, n+1):
if self.graph[i] != i:
self.graph[i].append(i)
if n % 2 != 0:
print("YES")
dot.dfs()
else:
print("NO")
def dfs(self):
v = len(self.graph)
visited = [False]*(v+1)
for i in range(1,v+1):
if visited[i] == False:
dot.solve_dfs(i, visited)
def solve_dfs(self, i, visited):
visited[i] = True
print(i)
for j in self.graph[i]:
if visited[j] == False :
dot.solve_dfs(j,visited)
def add_edge(self,u,v):
if v == 0:
return
else:
self.graph[v].append(int(u))
dot = DesOfTree()
dot.main()
| 95 | 826 | 48,742,400 |
101823725
|
from collections import defaultdict,deque
import sys
import bisect
import math
input=sys.stdin.readline
mod=1000000007
def bfs(root,count):
q=deque([root])
vis.add(root)
while q:
vertex=q.popleft()
for child in graph[vertex]:
if ans[child]==0:
ans[child]=count+1
count+=1
if child not in vis:
q.append(child)
vis.add(child)
graph=defaultdict(list)
n=int(input())
p=[int(i) for i in input().split() if i!='\n']
if n&1:
for i in range(n):
if p[i]!=0:
graph[p[i]].append(i+1)
graph[i+1].append(p[i])
length=[0]*(n+1)
for i in graph:
length[i]=len(graph[i])
CHECK,OBSERVE=1,0
stack=[(OBSERVE,1,0)]
ans=[0]*(n+1)
count=0
while stack:
state,vertex,parent=stack.pop()
if state==OBSERVE:
stack.append((CHECK,vertex,parent))
for child in graph[vertex]:
if child != parent:
stack.append((OBSERVE,child,vertex))
else:
if length[vertex]%2==0:
count+=1
ans[vertex]=count
length[parent]-=1
vis=set()
bfs(1,count)
out=[0]*(n)
for i in range(1,n+1):
out[ans[i]-1]=i
print('YES')
for i in out:
sys.stdout.write(str(i)+'\n')
else:
print('NO')
|
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
|
CF
| 2,018 | 1 | 256 |
Destruction of a Tree
|
You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).
A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.
Destroy all vertices in the given tree or determine that it is impossible.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — number of vertices in a tree.
The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n). If pi ≠ 0 there is an edge between vertices i and pi. It is guaranteed that the given graph is a tree.
|
If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes).
If it's possible to destroy all vertices, in the next n lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.
| null |
In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.
|
[{"input": "5\n0 1 2 1 2", "output": "YES\n1\n2\n3\n5\n4"}, {"input": "4\n0 1 2 3", "output": "NO"}]
| 2,000 |
["constructive algorithms", "dfs and similar", "dp", "greedy", "trees"]
| 95 |
[{"input": "5\r\n0 1 2 1 2\r\n", "output": "YES\r\n1\r\n2\r\n3\r\n5\r\n4\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n1\r\n"}, {"input": "8\r\n3 1 4 0 4 2 4 5\r\n", "output": "NO\r\n"}, {"input": "100\r\n81 96 65 28 4 40 5 49 5 89 48 70 94 70 17 58 58 1 61 19 45 33 46 19 22 83 56 67 62 82 57 16 29 36 84 71 42 66 78 54 73 45 82 80 67 88 79 69 61 66 5 36 24 60 96 21 77 67 68 29 87 37 91 34 78 43 0 69 49 62 16 2 68 79 57 1 60 12 39 99 14 37 30 92 47 18 14 75 73 39 94 12 43 87 90 22 91 59 54 71\r\n", "output": "NO\r\n"}, {"input": "100\r\n57 85 27 81 41 27 73 10 73 95 91 90 89 41 86 44 6 20 9 13 46 73 56 19 37 32 40 42 79 76 96 5 6 8 76 52 14 86 33 69 100 95 58 87 43 47 17 39 48 28 77 65 100 100 41 39 87 5 61 67 94 64 61 88 32 23 79 44 0 67 44 23 48 96 48 56 86 75 90 2 17 46 4 75 42 90 17 77 5 33 87 91 27 28 58 95 58 47 33 6\r\n", "output": "NO\r\n"}, {"input": "21\r\n11 10 12 3 6 0 8 6 16 14 5 9 7 19 1 13 15 21 4 2 20\r\n", "output": "YES\r\n21\r\n18\r\n2\r\n20\r\n14\r\n10\r\n4\r\n19\r\n12\r\n3\r\n16\r\n9\r\n7\r\n13\r\n6\r\n8\r\n11\r\n5\r\n15\r\n17\r\n1\r\n"}, {"input": "61\r\n10 42 20 50 4 24 18 55 19 5 57 13 3 35 58 48 31 46 40 45 15 53 14 25 43 41 22 23 54 39 38 44 16 37 12 34 32 28 26 30 59 47 21 9 8 52 1 0 33 49 36 51 17 11 29 7 48 61 6 27 2\r\n", "output": "YES\r\n27\r\n60\r\n53\r\n22\r\n31\r\n17\r\n28\r\n38\r\n14\r\n23\r\n12\r\n35\r\n3\r\n13\r\n45\r\n20\r\n55\r\n8\r\n54\r\n29\r\n57\r\n11\r\n16\r\n48\r\n49\r\n33\r\n4\r\n50\r\n10\r\n5\r\n7\r\n56\r\n46\r\n18\r\n51\r\n52\r\n34\r\n36\r\n32\r\n37\r\n9\r\n44\r\n40\r\n19\r\n39\r\n30\r\n41\r\n26\r\n6\r\n59\r\n25\r\n24\r\n21\r\n43\r\n58\r\n15\r\n2\r\n61\r\n47\r\n42\r\n1\r\n"}, {"input": "21\r\n11 19 4 19 6 0 13 7 6 2 5 3 16 10 1 9 15 21 9 21 2\r\n", "output": "YES\r\n7\r\n8\r\n16\r\n13\r\n10\r\n14\r\n2\r\n21\r\n18\r\n20\r\n3\r\n12\r\n19\r\n4\r\n6\r\n9\r\n11\r\n5\r\n15\r\n17\r\n1\r\n"}, {"input": "61\r\n47 61 20 5 10 59 46 55 44 1 57 13 3 35 21 48 31 7 9 45 43 53 14 6 42 39 22 23 54 40 45 37 16 36 12 44 34 28 25 19 26 33 25 39 33 36 42 0 50 4 52 46 17 11 29 7 48 15 41 27 58\r\n", "output": "YES\r\n6\r\n24\r\n41\r\n59\r\n40\r\n30\r\n9\r\n19\r\n37\r\n32\r\n52\r\n51\r\n46\r\n7\r\n18\r\n56\r\n36\r\n34\r\n61\r\n2\r\n15\r\n58\r\n43\r\n21\r\n25\r\n39\r\n26\r\n44\r\n55\r\n8\r\n54\r\n29\r\n57\r\n11\r\n16\r\n48\r\n28\r\n38\r\n14\r\n23\r\n12\r\n35\r\n3\r\n13\r\n27\r\n60\r\n53\r\n22\r\n31\r\n17\r\n45\r\n20\r\n42\r\n33\r\n50\r\n49\r\n5\r\n4\r\n1\r\n47\r\n10\r\n"}, {"input": "79\r\n0 56 56 42 56 56 56 56 4 56 56 22 56 56 56 48 56 56 56 56 56 24 56 16 56 56 56 9 56 56 56 56 56 56 56 56 56 55 56 56 12 20 56 28 56 56 56 38 56 56 56 56 56 56 44 1 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56\r\n", "output": "YES\r\n12\r\n41\r\n24\r\n22\r\n48\r\n16\r\n55\r\n38\r\n28\r\n44\r\n4\r\n9\r\n20\r\n42\r\n56\r\n2\r\n3\r\n5\r\n6\r\n7\r\n8\r\n10\r\n11\r\n13\r\n14\r\n15\r\n17\r\n18\r\n19\r\n21\r\n23\r\n25\r\n26\r\n27\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n39\r\n40\r\n43\r\n45\r\n46\r\n47\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67\r\n68\r\n69\r\n70\r\n71\r\n72\r\n73\r\n74\r\n75\r\n76\r\n77\r\n78\r\n79\r\n1\r\n"}, {"input": "121\r\n110 31 57 33 45 33 33 33 91 102 79 33 61 72 107 101 117 10 118 33 33 64 24 94 117 76 33 23 33 49 5 52 95 78 33 39 33 92 17 33 25 33 56 33 3 88 33 108 62 15 28 111 67 33 33 11 96 33 36 70 46 98 80 104 33 19 60 33 112 51 33 2 33 33 121 59 33 41 50 81 105 33 115 34 33 18 84 32 33 33 87 13 86 103 16 119 33 63 30 43 83 53 26 100 69 33 14 38 33 75 66 120 33 33 9 99 0 93 1 48 116\r\n", "output": "YES\r\n33\r\n4\r\n6\r\n7\r\n8\r\n12\r\n20\r\n21\r\n27\r\n29\r\n35\r\n37\r\n40\r\n42\r\n44\r\n47\r\n54\r\n55\r\n58\r\n65\r\n68\r\n71\r\n73\r\n74\r\n77\r\n82\r\n85\r\n89\r\n90\r\n97\r\n106\r\n109\r\n113\r\n114\r\n16\r\n95\r\n83\r\n101\r\n9\r\n115\r\n87\r\n91\r\n34\r\n84\r\n41\r\n78\r\n117\r\n25\r\n39\r\n17\r\n59\r\n36\r\n26\r\n76\r\n94\r\n103\r\n23\r\n24\r\n51\r\n28\r\n60\r\n70\r\n53\r\n67\r\n10\r\n102\r\n86\r\n18\r\n118\r\n93\r\n66\r\n19\r\n52\r\n111\r\n88\r\n32\r\n61\r\n46\r\n92\r\n13\r\n108\r\n38\r\n120\r\n48\r\n69\r\n112\r\n81\r\n105\r\n63\r\n80\r\n62\r\n98\r\n30\r\n49\r\n116\r\n99\r\n75\r\n121\r\n64\r\n22\r\n100\r\n104\r\n56\r\n43\r\n79\r\n11\r\n15\r\n50\r\n14\r\n107\r\n2\r\n72\r\n5\r\n31\r\n3\r\n45\r\n96\r\n57\r\n1\r\n110\r\n119\r\n"}, {"input": "21\r\n5 20 9 19 8 0 13 6 13 19 5 3 8 10 1 9 1 20 3 10 18\r\n", "output": "YES\r\n18\r\n21\r\n20\r\n2\r\n10\r\n14\r\n19\r\n4\r\n3\r\n12\r\n9\r\n16\r\n13\r\n7\r\n8\r\n6\r\n5\r\n11\r\n1\r\n15\r\n17\r\n"}, {"input": "61\r\n5 61 20 5 50 59 56 29 44 1 48 13 20 35 61 33 38 52 30 8 43 17 35 43 24 59 22 23 11 26 38 37 48 36 13 37 44 23 30 19 26 1 15 19 8 18 42 0 50 33 52 36 17 11 29 18 48 15 24 22 42\r\n", "output": "YES\r\n56\r\n7\r\n18\r\n46\r\n52\r\n51\r\n36\r\n34\r\n37\r\n32\r\n44\r\n9\r\n19\r\n40\r\n30\r\n39\r\n26\r\n41\r\n59\r\n6\r\n24\r\n25\r\n43\r\n21\r\n15\r\n58\r\n61\r\n2\r\n42\r\n47\r\n1\r\n5\r\n4\r\n50\r\n49\r\n33\r\n16\r\n48\r\n11\r\n29\r\n8\r\n20\r\n3\r\n13\r\n12\r\n35\r\n14\r\n23\r\n28\r\n38\r\n17\r\n22\r\n27\r\n60\r\n53\r\n31\r\n45\r\n55\r\n54\r\n57\r\n10\r\n"}, {"input": "21\r\n18 18 18 18 18 0 18 18 18 18 18 18 18 18 18 18 18 6 18 18 18\r\n", "output": "YES\r\n18\r\n2\r\n3\r\n4\r\n5\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n6\r\n19\r\n20\r\n21\r\n1\r\n"}, {"input": "61\r\n56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 0 56 56 56 56 56 56 56 48 56 56 56 56 56\r\n", "output": "YES\r\n56\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n48\r\n57\r\n58\r\n59\r\n60\r\n61\r\n1\r\n"}, {"input": "21\r\n15 6 13 7 15 21 8 0 7 16 16 21 12 6 12 12 13 6 15 16 7\r\n", "output": "YES\r\n6\r\n2\r\n14\r\n18\r\n7\r\n4\r\n8\r\n9\r\n16\r\n10\r\n11\r\n20\r\n15\r\n5\r\n12\r\n21\r\n13\r\n3\r\n17\r\n19\r\n1\r\n"}, {"input": "61\r\n58 39 45 57 31 43 11 24 8 18 56 54 47 37 50 40 19 16 29 10 1 23 36 28 21 48 52 55 27 42 2 33 46 25 53 6 15 26 14 17 9 44 56 34 5 61 38 12 30 7 49 32 20 41 51 0 3 4 60 35 13\r\n", "output": "YES\r\n23\r\n22\r\n6\r\n36\r\n56\r\n43\r\n7\r\n11\r\n15\r\n50\r\n14\r\n37\r\n2\r\n39\r\n5\r\n31\r\n3\r\n45\r\n4\r\n57\r\n60\r\n59\r\n53\r\n35\r\n10\r\n20\r\n16\r\n18\r\n17\r\n40\r\n29\r\n19\r\n52\r\n27\r\n33\r\n32\r\n61\r\n46\r\n47\r\n13\r\n26\r\n38\r\n12\r\n48\r\n41\r\n54\r\n8\r\n9\r\n28\r\n24\r\n51\r\n55\r\n30\r\n49\r\n44\r\n42\r\n25\r\n34\r\n1\r\n58\r\n21\r\n"}, {"input": "21\r\n21 6 4 20 14 1 13 10 11 0 10 18 10 12 4 1 2 2 8 2 13\r\n", "output": "YES\r\n8\r\n19\r\n11\r\n9\r\n21\r\n13\r\n7\r\n10\r\n14\r\n5\r\n18\r\n12\r\n20\r\n4\r\n3\r\n15\r\n2\r\n17\r\n1\r\n6\r\n16\r\n"}, {"input": "61\r\n17 19 8 53 10 38 59 60 46 25 49 28 46 15 25 56 53 60 60 54 18 49 10 53 29 19 11 61 24 11 17 52 32 54 29 55 0 1 14 56 25 14 33 53 47 56 8 6 53 55 16 46 47 9 24 37 3 52 25 37 26\r\n", "output": "YES\r\n15\r\n14\r\n39\r\n42\r\n59\r\n7\r\n3\r\n57\r\n18\r\n21\r\n28\r\n12\r\n26\r\n61\r\n19\r\n2\r\n16\r\n51\r\n9\r\n54\r\n20\r\n34\r\n33\r\n43\r\n37\r\n56\r\n40\r\n46\r\n13\r\n52\r\n32\r\n58\r\n8\r\n60\r\n47\r\n45\r\n6\r\n48\r\n1\r\n17\r\n53\r\n4\r\n24\r\n29\r\n25\r\n10\r\n5\r\n23\r\n41\r\n35\r\n55\r\n36\r\n50\r\n44\r\n49\r\n11\r\n27\r\n30\r\n22\r\n31\r\n38\r\n"}, {"input": "21\r\n18 0 18 2 21 2 9 15 3 5 8 2 8 21 6 10 21 13 9 1 13\r\n", "output": "YES\r\n3\r\n9\r\n7\r\n19\r\n6\r\n2\r\n4\r\n12\r\n10\r\n16\r\n21\r\n5\r\n14\r\n17\r\n13\r\n8\r\n15\r\n11\r\n1\r\n18\r\n20\r\n"}, {"input": "61\r\n45 48 30 23 15 47 8 3 35 56 54 35 17 47 35 56 32 42 14 37 36 44 6 44 1 44 41 46 43 0 33 3 44 54 43 3 47 57 7 32 29 60 36 36 43 61 36 47 3 48 18 8 17 29 3 54 3 6 43 43 56\r\n", "output": "YES\r\n41\r\n27\r\n46\r\n28\r\n56\r\n10\r\n16\r\n61\r\n29\r\n54\r\n11\r\n34\r\n15\r\n5\r\n7\r\n39\r\n8\r\n52\r\n57\r\n38\r\n33\r\n31\r\n44\r\n22\r\n24\r\n26\r\n23\r\n4\r\n6\r\n58\r\n14\r\n19\r\n37\r\n20\r\n47\r\n48\r\n2\r\n50\r\n18\r\n51\r\n60\r\n42\r\n43\r\n35\r\n9\r\n12\r\n36\r\n21\r\n3\r\n30\r\n32\r\n17\r\n13\r\n53\r\n40\r\n49\r\n55\r\n59\r\n1\r\n45\r\n25\r\n"}]
| false |
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
n = int(f.readline())
p = list(map(int, f.readline().split()))
# Read reference output
with open(output_path) as f_ref:
ref_first_line = f_ref.readline().strip()
# Read submission output
with open(submission_path) as f_sub:
lines = [line.strip() for line in f_sub.readlines()]
# Check first line
if not lines:
print(0)
return
submission_first = lines[0]
if submission_first != ref_first_line:
print(0)
return
if ref_first_line == "NO":
if len(lines) != 1:
print(0)
return
else:
print(1)
return
# Check YES case
if len(lines) != n + 1:
print(0)
return
sequence = lines[1:]
if len(sequence) != n:
print(0)
return
try:
sequence = list(map(int, sequence))
except:
print(0)
return
if any(x < 1 or x > n for x in sequence):
print(0)
return
if len(set(sequence)) != n:
print(0)
return
# Build adjacency list
adj = [[] for _ in range(n + 1)]
for i in range(1, n + 1):
pi = p[i - 1]
if pi != 0:
adj[i].append(pi)
adj[pi].append(i)
current_degree = [0] * (n + 1)
for i in range(1, n + 1):
current_degree[i] = len(adj[i])
alive = [True] * (n + 1)
# Simulate destruction
for node in sequence:
if not alive[node]:
print(0)
return
if current_degree[node] % 2 != 0:
print(0)
return
for neighbor in adj[node]:
if alive[neighbor]:
current_degree[neighbor] -= 1
alive[node] = False
current_degree[node] = 0
if all(not alive[i] for i in range(1, n + 1)):
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true |
965/E
|
965
|
E
|
PyPy 3
|
TESTS
| 4 | 93 | 21,401,600 |
37889385
|
import sys
line=lambda:sys.stdin.buffer.readline()
n=int(line())
t=[None]*26
def dfs(p,d,x):
ans=0;m=None;j=-1
if len(p)==28:
ans+=d
if p[26]==x+1: return ans
x-=p[27]
for i in range(26):
if p[i]:
k=dfs(p[i],d+1,x)-(p[i][26]-x)
if m==None or k>m: j,m=i,k
for i in range(26):
if p[i]: ans+=dfs(p[i],d+1,x+(i==j))
return ans
for i in range(n):
s=line().strip();p=t
for c in s:
v=c-ord('a')
if p[v]==None: p[v]=[None]*26+[0]+[0]
p=p[v];p[26]+=1
p[27]+=1
print(dfs(t,0,0))
| 24 | 156 | 22,937,600 |
177672519
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def trie():
G, cnt = [[-1] * 26], [0]
return G, cnt
def insert(s):
j = 0
for i in s:
if not G[j][i - 97] ^ -1:
G[j][i - 97] = len(cnt)
G.append([-1] * 26)
cnt.append(0)
j = len(cnt) - 1
else:
j = G[j][i - 97]
cnt[j] += 1
return
n = int(input())
G, cnt = trie()
for _ in range(n):
s = list(input().rstrip())
insert(s)
m = len(cnt)
inf = pow(10, 9) + 1
dist = [inf] * m
dist[0] = 0
parent = [-1] * m
q, k = [0], 0
while len(q) ^ k:
i = q[k]
di = dist[i]
for j in G[i]:
if j ^ -1:
q.append(j)
dist[j] = di + 1
parent[j] = i
k += 1
ans = 0
for i in range(m):
if cnt[i]:
ans += dist[i]
for i in q[::-1]:
if not cnt[i]:
continue
u = i
while parent[u]:
u = parent[u]
if not cnt[u]:
break
if not cnt[u]:
cnt[u] = 1
ans -= dist[i] - dist[u]
print(ans)
|
Codeforces Round 476 (Div. 2) [Thanks, Telegram!]
|
CF
| 2,018 | 1 | 256 |
Short Code
|
Arkady's code contains $$$n$$$ variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code.
He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names.
A string $$$a$$$ is a prefix of a string $$$b$$$ if you can delete some (possibly none) characters from the end of $$$b$$$ and obtain $$$a$$$.
Please find this minimum possible total length of new names.
|
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of variables.
The next $$$n$$$ lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than $$$10^5$$$. The variable names are distinct.
|
Print a single integer — the minimum possible total length of new variable names.
| null |
In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c".
In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names.
|
[{"input": "3\ncodeforces\ncodehorses\ncode", "output": "6"}, {"input": "5\nabba\nabb\nab\naa\naacada", "output": "11"}, {"input": "3\ntelegram\ndigital\nresistance", "output": "3"}]
| 2,200 |
["data structures", "dp", "greedy", "strings", "trees"]
| 24 |
[{"input": "3\r\ncodeforces\r\ncodehorses\r\ncode\r\n", "output": "6\r\n"}, {"input": "5\r\nabba\r\nabb\r\nab\r\naa\r\naacada\r\n", "output": "11\r\n"}, {"input": "3\r\ntelegram\r\ndigital\r\nresistance\r\n", "output": "3\r\n"}, {"input": "1\r\na\r\n", "output": "1\r\n"}, {"input": "10\r\naaaba\r\nbabba\r\nbbba\r\naaabb\r\nabba\r\na\r\nbbb\r\nbaa\r\naaba\r\naa\r\n", "output": "23\r\n"}, {"input": "10\r\naaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaa\r\naaaaaaaa\r\naaa\r\naaaaaaaaaaaa\r\naa\r\naaaaaaa\r\naaaaaaaaa\r\naaaaaaaaaaaaa\r\naaaaaaaaaa\r\n", "output": "55\r\n"}, {"input": "26\r\ni\r\nm\r\nz\r\na\r\nv\r\nu\r\nq\r\nt\r\nj\r\nn\r\nr\r\nk\r\nd\r\ng\r\ns\r\nc\r\no\r\ne\r\np\r\nl\r\ny\r\nw\r\nh\r\nf\r\nb\r\nx\r\n", "output": "26\r\n"}]
| false |
stdio
| null | true |
767/E
|
767
|
E
|
PyPy 3
|
TESTS
| 0 | 77 | 0 |
121567378
|
from heapq import *
DEBUG = 0
if DEBUG==1:
data = open('sample3.in', 'r')
def readline():
if DEBUG == 1:
return data.readline().strip('\n')
else:
try:
return input()
except EOFError:
return ''
def readi():
return [int(t) for t in readline().split()]
n,m = readi()
c = readi()
w = readi()
plan = [[0,0] for i in range(n)]
cost_tot = 0
Q = []
for i in range(n):
q = c[i]//100
r = c[i]%100
if r>0:
if m>=r:
m -= r
plan[i][0] = q
plan[i][1] = r
heappush(Q, [(100-r)*w[i],i,q])
else:
cost = (100-r)*w[i]
cost1 = 2**32
if len(Q)>0:
cost1,i1,q1 = heappop(Q)
if cost1<cost:
cost_tot += cost1
m += 100
heappush(Q, [(100-r)*w[i],i])
plan[i1][0] = q1+1
plan[i1][1] = 0
else:
cost_tot += cost
m += (100-r)
plan[i][0] = (q+1)
plan[i][1] = 0
else:
plan[i][0] = q
plan[i][1] = r
print(cost_tot)
for i in range(n):
print(plan[i][0],plan[i][1])
if DEBUG==1:
data.close()
| 53 | 421 | 35,328,000 |
164359819
|
from heapq import *
import sys
input = sys.stdin.buffer.readline
def sol(n, m, money_per_day, dissatisfaction):
# insatisfacción que resulta de los pagos que se realicen cada día
payments = [[0, 0] for _ in range(n)]
# heap donde se insertarán los pagos que se realizaron sin necesitar cambio
h = []
# por cada día ir pagando, de manera que si no se tiene para dar el dinero exacto en un día,
# modificar el pago de los días anteriores que resulten en una menor instaisfacción
total_diss = 0
for i, c in enumerate(money_per_day):
# cantidad de billetes a pagar
notes = c // 100
# cantidad de monedas a pagar
coins = c % 100
# primero se asume que se pagará sin necesitar cambio
payments[i][0] = notes
payments[i][1] = coins
if coins > 0:
m -= coins
# insertar el pago recién realizado en el heap, que se asumió que no necesitaría cambio
new_diss = (100 - coins) * dissatisfaction[i]
heappush(h, [new_diss, i])
if m < 0:
# escoger el día en el que se obtuvo menor insatisfacción de los ya transcurridos y
# obtener las monedas que se habían gastado en hacer el pago exacto y emplearlas en el día $i$
# de manera que en el día day se realice un pago inexacto
diss, day = heappop(h)
m += 100
total_diss += diss
payments[day][0] += 1
payments[day][1] = 0
return total_diss, payments
def main():
# Leer caso de prueba
# n: cantidad de días
# m: cantidad de monedas
n, m = map(int, input().split())
# guardar la cantidad de dinero que debe gastar cada día
money_per_day = list(map(int, input().split()))
# guardar la insatisfacción del dependiente por día
dissatisfaction = list(map(int, input().split()))
diss, payments = sol(n, m, money_per_day, dissatisfaction)
# imprimir menor insatisfacción
print(str(diss))
for notes, coins in payments:
print(f"{str(notes)} {str(coins)}")
main()
|
Codeforces Round 398 (Div. 2)
|
CF
| 2,017 | 1 | 256 |
Change-free
|
Student Arseny likes to plan his life for n days ahead. He visits a canteen every day and he has already decided what he will order in each of the following n days. Prices in the canteen do not change and that means Arseny will spend ci rubles during the i-th day.
There are 1-ruble coins and 100-ruble notes in circulation. At this moment, Arseny has m coins and a sufficiently large amount of notes (you can assume that he has an infinite amount of them). Arseny loves modern technologies, so he uses his credit card everywhere except the canteen, but he has to pay in cash in the canteen because it does not accept cards.
Cashier always asks the student to pay change-free. However, it's not always possible, but Arseny tries to minimize the dissatisfaction of the cashier. Cashier's dissatisfaction for each of the days is determined by the total amount of notes and coins in the change. To be precise, if the cashier gives Arseny x notes and coins on the i-th day, his dissatisfaction for this day equals x·wi. Cashier always gives change using as little coins and notes as possible, he always has enough of them to be able to do this.
"Caution! Angry cashier"
Arseny wants to pay in such a way that the total dissatisfaction of the cashier for n days would be as small as possible. Help him to find out how he needs to pay in each of the n days!
Note that Arseny always has enough money to pay, because he has an infinite amount of notes. Arseny can use notes and coins he received in change during any of the following days.
|
The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 109) — the amount of days Arseny planned his actions for and the amount of coins he currently has.
The second line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105) — the amounts of money in rubles which Arseny is going to spend for each of the following days.
The third line contains a sequence of integers w1, w2, ..., wn (1 ≤ wi ≤ 105) — the cashier's dissatisfaction coefficients for each of the following days.
|
In the first line print one integer — minimum possible total dissatisfaction of the cashier.
Then print n lines, the i-th of then should contain two numbers — the amount of notes and the amount of coins which Arseny should use to pay in the canteen on the i-th day.
Of course, the total amount of money Arseny gives to the casher in any of the days should be no less than the amount of money he has planned to spend. It also shouldn't exceed 106 rubles: Arseny never carries large sums of money with him.
If there are multiple answers, print any of them.
| null | null |
[{"input": "5 42\n117 71 150 243 200\n1 1 1 1 1", "output": "79\n1 17\n1 0\n2 0\n2 43\n2 0"}, {"input": "3 0\n100 50 50\n1 3 2", "output": "150\n1 0\n1 0\n0 50"}, {"input": "5 42\n117 71 150 243 200\n5 4 3 2 1", "output": "230\n1 17\n1 0\n1 50\n3 0\n2 0"}]
| 2,400 |
["greedy"]
| 53 |
[{"input": "5 42\r\n117 71 150 243 200\r\n1 1 1 1 1\r\n", "output": "79\r\n1 17\r\n1 0\r\n2 0\r\n2 43\r\n2 0\r\n"}, {"input": "3 0\r\n100 50 50\r\n1 3 2\r\n", "output": "150\r\n1 0\r\n1 0\r\n0 50\r\n"}, {"input": "5 42\r\n117 71 150 243 200\r\n5 4 3 2 1\r\n", "output": "230\r\n1 17\r\n1 0\r\n1 50\r\n3 0\r\n2 0\r\n"}, {"input": "5 32\r\n83 13 61 34 31\r\n7 5 6 8 8\r\n", "output": "353\r\n1 0\r\n0 13\r\n1 0\r\n0 34\r\n0 31\r\n"}, {"input": "14 138\r\n479 330 487 127 170 55 122 480 331 434 447 246 104 263\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "166\r\n5 0\r\n3 30\r\n5 0\r\n1 27\r\n2 0\r\n1 0\r\n1 22\r\n5 0\r\n3 31\r\n4 34\r\n4 47\r\n2 46\r\n1 4\r\n3 0\r\n"}, {"input": "1 0\r\n49\r\n2\r\n", "output": "102\r\n1 0\r\n"}, {"input": "1 100\r\n51\r\n2\r\n", "output": "0\r\n0 51\r\n"}, {"input": "1 50\r\n51\r\n100000\r\n", "output": "4900000\r\n1 0\r\n"}, {"input": "20 148\r\n469 474 336 120 365 172 159 297 301 477 335 471 320 300 486 279 449 215 384 297\r\n29 63 60 16 35 31 55 97 66 97 55 19 6 33 26 21 71 4 16 30\r\n", "output": "6265\r\n5 0\r\n4 74\r\n3 36\r\n2 0\r\n4 0\r\n2 0\r\n1 59\r\n3 0\r\n3 1\r\n4 77\r\n3 35\r\n5 0\r\n3 20\r\n3 0\r\n5 0\r\n3 0\r\n4 49\r\n2 15\r\n4 0\r\n3 0\r\n"}, {"input": "20 20\r\n209 457 118 182 443 455 336 128 225 295 186 64 230 30 259 407 39 378 341 44\r\n10 6 6 5 9 2 9 5 5 9 88334 75309 89717 85121 58293 77761 79913 57434 93590 54508\r\n", "output": "3709\r\n3 0\r\n5 0\r\n2 0\r\n2 0\r\n5 0\r\n5 0\r\n4 0\r\n2 0\r\n3 0\r\n3 0\r\n1 86\r\n0 64\r\n2 30\r\n0 30\r\n2 59\r\n4 7\r\n0 39\r\n3 78\r\n3 41\r\n0 44\r\n"}, {"input": "20 20\r\n209 457 118 182 443 455 336 128 225 295 186 64 230 30 259 407 39 378 341 44\r\n10 96444 6 88786 9 54221 9 69661 5 89056 3 75309 3 85121 5 77761 5 57434 2 54508\r\n", "output": "3746\r\n3 0\r\n4 57\r\n2 0\r\n1 82\r\n5 0\r\n4 55\r\n4 0\r\n1 28\r\n3 0\r\n2 95\r\n2 0\r\n0 64\r\n3 0\r\n0 30\r\n3 0\r\n4 7\r\n1 0\r\n3 78\r\n4 0\r\n0 44\r\n"}, {"input": "20 431\r\n417 327 307 311 485 355 412 383 368 66 237 201 171 266 317 33 297 204 380 462\r\n5 4 3 8 7 1 9 6 1 2 6 7 6 3 6 4 10 7 4 1\r\n", "output": "255\r\n4 17\r\n3 27\r\n3 7\r\n3 11\r\n4 85\r\n4 0\r\n4 12\r\n3 83\r\n4 0\r\n1 0\r\n2 37\r\n2 1\r\n1 71\r\n2 66\r\n3 17\r\n0 33\r\n3 0\r\n2 4\r\n4 0\r\n4 62\r\n"}, {"input": "20 16\r\n5 8 13 6 20 13 18 8 12 16 5 3 2 11 9 3 6 9 9 20\r\n5 9 6 7 8 10 10 2 5 6 4 2 7 1 6 10 4 10 4 8\r\n", "output": "659\r\n1 0\r\n0 8\r\n0 13\r\n0 6\r\n0 20\r\n0 13\r\n0 18\r\n1 0\r\n0 12\r\n0 16\r\n0 5\r\n0 3\r\n0 2\r\n0 11\r\n0 9\r\n0 3\r\n0 6\r\n0 9\r\n0 9\r\n0 20\r\n"}, {"input": "40 0\r\n450 450 350 350 450 350 450 450 450 450 150 450 150 450 150 350 150 150 450 350 350 150 450 350 350 350 150 250 450 450 150 250 450 350 150 250 450 250 250 250\r\n99998 99997 99996 99996 100000 100000 99995 99999 99995 99999 99996 100000 99997 99995 99997 99995 99999 99998 100000 99995 99996 99999 99995 99998 99998 100000 99999 99998 99997 99998 99999 99999 100000 99998 99997 99998 100000 99996 99999 99999\r\n", "output": "99996350\r\n5 0\r\n5 0\r\n4 0\r\n4 0\r\n4 50\r\n3 50\r\n5 0\r\n4 50\r\n5 0\r\n4 50\r\n2 0\r\n4 50\r\n2 0\r\n5 0\r\n2 0\r\n4 0\r\n1 50\r\n1 50\r\n4 50\r\n4 0\r\n4 0\r\n1 50\r\n5 0\r\n3 50\r\n3 50\r\n3 50\r\n1 50\r\n3 0\r\n5 0\r\n5 0\r\n1 50\r\n2 50\r\n4 50\r\n3 50\r\n2 0\r\n3 0\r\n4 50\r\n3 0\r\n2 50\r\n2 50\r\n"}, {"input": "50 135\r\n111 438 6 371 492 211 134 405 258 420 103 438 78 238 127 421 81 57 351 434 380 138 105 335 409 459 229 298 92 106 244 244 19 465 312 69 26 372 498 285 291 199 7 144 367 37 496 227 115 208\r\n5 57 97 77 38 83 96 85 53 29 79 23 89 91 75 47 62 49 64 55 91 47 32 68 39 61 17 25 52 23 74 68 75 99 5 92 62 90 12 85 97 35 47 47 58 53 15 43 45 79\r\n", "output": "26974\r\n2 0\r\n4 38\r\n0 6\r\n4 0\r\n5 0\r\n2 11\r\n1 34\r\n4 5\r\n3 0\r\n5 0\r\n1 3\r\n5 0\r\n1 0\r\n2 38\r\n1 27\r\n4 21\r\n1 0\r\n1 0\r\n3 51\r\n4 34\r\n4 0\r\n1 38\r\n1 5\r\n3 35\r\n4 9\r\n5 0\r\n3 0\r\n3 0\r\n1 0\r\n2 0\r\n2 44\r\n2 44\r\n0 19\r\n4 65\r\n4 0\r\n0 69\r\n0 26\r\n3 72\r\n5 0\r\n3 0\r\n3 0\r\n2 0\r\n0 7\r\n1 44\r\n4 0\r\n0 37\r\n5 0\r\n2 27\r\n1 15\r\n2 8\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:
input_lines = f.readlines()
n, m = map(int, input_lines[0].split())
c = list(map(int, input_lines[1].split()))
w = list(map(int, input_lines[2].split()))
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f]
if not sub_lines:
print(0)
return
try:
S_sub = int(sub_lines[0])
except:
print(0)
return
sub_payments = []
for line in sub_lines[1:1+n]:
if not line.strip():
continue
try:
a, b = map(int, line.split())
sub_payments.append( (a, b) )
except:
print(0)
return
# Read reference output's total S_ref
with open(output_path) as f:
try:
ref_S = int(f.readline().strip())
except:
print(0)
return
# Check S_sub matches reference
if S_sub != ref_S:
print(0)
return
# Check number of payment lines
if len(sub_payments) != n:
print(0)
return
current_coins = m
total_diss = 0
for i in range(n):
a, b = sub_payments[i]
ci = c[i]
wi = w[i]
# Check a and b are non-negative
if a < 0 or b < 0:
print(0)
return
given = 100 * a + b
if given < ci:
print(0)
return
if given > 10**6:
print(0)
return
# Check coins available
if b > current_coins:
print(0)
return
delta = given - ci
notes_change = delta // 100
coins_change = delta % 100
# Compute dissatisfaction
diss = (notes_change + coins_change) * wi
total_diss += diss
# Update current coins
current_coins = current_coins - b + coins_change
# Check total_diss matches S_sub
if total_diss != S_sub:
print(0)
return
# All checks passed
print(1)
if __name__ == "__main__":
main()
| true |
963/B
|
963
|
B
|
PyPy 3
|
TESTS
| 6 | 77 | 20,172,800 |
126190123
|
import sys
input = sys.stdin.readline
n = int(input())
if n%2 == 0:
print("NO")
exit()
parent = list(map(int,input().split()))
print("YES")
parity = [0]*n
G = [[] for _ in range(n)]
for i in range(n):
parent[i] -= 1
if parent[i] != -1:
G[parent[i]].append(i)
parity[parent[i]] += 1
parity[i] += 1
parity[i] %= 2
parity[parent[i]] %= 2
stk = [0]
while stk:
node = stk.pop()
if parity[node]%2 == 0:
print(node+1)
for node0 in G[node]:
parity[node0] -= 1
if parent[node] != -1:
parity[parent[node]] -= 1
for node0 in G[node]:
stk.append(node0)
| 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 |
103/A
|
103
|
A
|
Python 3
|
TESTS
| 3 | 92 | 0 |
12771556
|
N = int(input())
A = list(int(i) for i in input().split())
total = 0
for i in range(N):
clicksToReach = i
if(A[i]>1):
total+=A[i]-1+clicksToReach
total+=N
print(total)
| 25 | 92 | 0 |
157957695
|
import sys
input = sys.stdin.readline
n = int(input())
w = list(map(int, input().split()))
s, c = 1, 0
for i in range(n):
c += s*(w[i]-1)
s += 1
c += n
print(c)
|
Codeforces Beta Round 80 (Div. 1 Only)
|
CF
| 2,011 | 2 | 256 |
Testing Pants for Sadness
|
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".
The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.
A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.
Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
|
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
|
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
| null |
Note to the second sample. In the worst-case scenario you will need five clicks:
- the first click selects the first variant to the first question, this answer turns out to be wrong.
- the second click selects the second variant to the first question, it proves correct and we move on to the second question;
- the third click selects the first variant to the second question, it is wrong and we go back to question 1;
- the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
- the fifth click selects the second variant to the second question, it proves correct, the test is finished.
|
[{"input": "2\n1 1", "output": "2"}, {"input": "2\n2 2", "output": "5"}, {"input": "1\n10", "output": "10"}]
| 1,100 |
["greedy", "implementation", "math"]
| 25 |
[{"input": "2\r\n1 1\r\n", "output": "2"}, {"input": "2\r\n2 2\r\n", "output": "5"}, {"input": "1\r\n10\r\n", "output": "10"}, {"input": "3\r\n2 4 1\r\n", "output": "10"}, {"input": "4\r\n5 5 3 1\r\n", "output": "22"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2999999999"}, {"input": "10\r\n5 7 8 1 10 3 6 4 10 6\r\n", "output": "294"}, {"input": "100\r\n5 7 5 3 5 4 6 5 3 6 4 6 6 2 1 9 6 5 3 8 4 10 1 9 1 3 7 6 5 5 8 8 7 7 8 9 2 10 3 5 4 2 6 10 2 6 9 6 1 9 3 7 7 8 3 9 9 5 10 10 3 10 7 8 3 9 8 3 2 4 10 2 1 1 7 3 9 10 4 6 9 8 2 1 4 10 1 10 6 8 7 5 3 3 6 2 7 10 3 8\r\n", "output": "24212"}, {"input": "10\r\n12528238 329065023 620046219 303914458 356423530 751571368 72944261 883971060 123105651 868129460\r\n", "output": "27409624352"}, {"input": "1\r\n84355694\r\n", "output": "84355694"}, {"input": "2\r\n885992042 510468669\r\n", "output": "1906929379"}, {"input": "100\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "100"}, {"input": "100\r\n2 1 2 2 2 2 1 2 2 1 2 2 2 1 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 2 1 1 2 1 2 1 1 1 2 2 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 1 1 1 2 2 1 1 2 1 1 2 2 2 2 1 2 2 2 1 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1\r\n", "output": "2686"}, {"input": "100\r\n1 3 2 1 1 2 1 3 2 2 3 1 1 1 2 2 1 3 3 1 1 2 2 3 2 1 3 1 3 2 1 1 3 3 2 1 2 2 2 3 2 2 3 2 2 3 2 1 3 1 1 2 1 3 2 2 1 1 1 1 1 1 3 1 2 3 1 1 1 1 1 2 3 3 1 1 1 1 2 3 3 1 3 2 2 3 2 1 3 2 2 3 1 1 3 2 3 2 3 1\r\n", "output": "4667"}]
| false |
stdio
| null | true |
103/A
|
103
|
A
|
PyPy 3-64
|
TESTS
| 3 | 92 | 0 |
218699002
|
n=int(input())
l=list(map(int,input().split()))
ans=0
for i in range(len(l)):
if l[i]==1:
ans=ans+1
else:
ans=ans+l[i]+i
print(ans)
| 25 | 92 | 0 |
158977181
|
n = int(input())
lis = list(map(int,input().split()))
ans = 0
for i in range(n):
if i == 0:
ans += lis[i]
else:
ans += (i-0)*(lis[i]-1) + lis[i]
print(ans)
|
Codeforces Beta Round 80 (Div. 1 Only)
|
CF
| 2,011 | 2 | 256 |
Testing Pants for Sadness
|
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".
The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.
A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.
Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
|
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
|
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
| null |
Note to the second sample. In the worst-case scenario you will need five clicks:
- the first click selects the first variant to the first question, this answer turns out to be wrong.
- the second click selects the second variant to the first question, it proves correct and we move on to the second question;
- the third click selects the first variant to the second question, it is wrong and we go back to question 1;
- the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
- the fifth click selects the second variant to the second question, it proves correct, the test is finished.
|
[{"input": "2\n1 1", "output": "2"}, {"input": "2\n2 2", "output": "5"}, {"input": "1\n10", "output": "10"}]
| 1,100 |
["greedy", "implementation", "math"]
| 25 |
[{"input": "2\r\n1 1\r\n", "output": "2"}, {"input": "2\r\n2 2\r\n", "output": "5"}, {"input": "1\r\n10\r\n", "output": "10"}, {"input": "3\r\n2 4 1\r\n", "output": "10"}, {"input": "4\r\n5 5 3 1\r\n", "output": "22"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2999999999"}, {"input": "10\r\n5 7 8 1 10 3 6 4 10 6\r\n", "output": "294"}, {"input": "100\r\n5 7 5 3 5 4 6 5 3 6 4 6 6 2 1 9 6 5 3 8 4 10 1 9 1 3 7 6 5 5 8 8 7 7 8 9 2 10 3 5 4 2 6 10 2 6 9 6 1 9 3 7 7 8 3 9 9 5 10 10 3 10 7 8 3 9 8 3 2 4 10 2 1 1 7 3 9 10 4 6 9 8 2 1 4 10 1 10 6 8 7 5 3 3 6 2 7 10 3 8\r\n", "output": "24212"}, {"input": "10\r\n12528238 329065023 620046219 303914458 356423530 751571368 72944261 883971060 123105651 868129460\r\n", "output": "27409624352"}, {"input": "1\r\n84355694\r\n", "output": "84355694"}, {"input": "2\r\n885992042 510468669\r\n", "output": "1906929379"}, {"input": "100\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "100"}, {"input": "100\r\n2 1 2 2 2 2 1 2 2 1 2 2 2 1 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 2 1 1 2 1 2 1 1 1 2 2 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 1 1 1 2 2 1 1 2 1 1 2 2 2 2 1 2 2 2 1 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1\r\n", "output": "2686"}, {"input": "100\r\n1 3 2 1 1 2 1 3 2 2 3 1 1 1 2 2 1 3 3 1 1 2 2 3 2 1 3 1 3 2 1 1 3 3 2 1 2 2 2 3 2 2 3 2 2 3 2 1 3 1 1 2 1 3 2 2 1 1 1 1 1 1 3 1 2 3 1 1 1 1 1 2 3 3 1 1 1 1 2 3 3 1 3 2 2 3 2 1 3 2 2 3 1 1 3 2 3 2 3 1\r\n", "output": "4667"}]
| false |
stdio
| null | true |
963/B
|
963
|
B
|
PyPy 3
|
TESTS
| 6 | 109 | 151,449,600 |
126190927
|
import sys
sys.setrecursionlimit(200005)
input = sys.stdin.readline
n = int(input())
if n%2 == 0:
print("NO")
exit()
parent = list(map(int,input().split()))
print("YES")
parity = [0]*n
G = [[] for _ in range(n)]
for i in range(n):
parent[i] -= 1
if parent[i] != -1:
G[parent[i]].append(i)
parity[parent[i]] += 1
parity[i] += 1
parity[i] %= 2
parity[parent[i]] %= 2
def dfs(node):
for node0 in G[node]:
if parity[node0]%2 == 0:
dfs(node0)
print(node+1)
for node0 in G[node]:
if parity[node0]%2 == 1:
dfs(node0)
dfs(0)
| 95 | 826 | 48,742,400 |
101823725
|
from collections import defaultdict,deque
import sys
import bisect
import math
input=sys.stdin.readline
mod=1000000007
def bfs(root,count):
q=deque([root])
vis.add(root)
while q:
vertex=q.popleft()
for child in graph[vertex]:
if ans[child]==0:
ans[child]=count+1
count+=1
if child not in vis:
q.append(child)
vis.add(child)
graph=defaultdict(list)
n=int(input())
p=[int(i) for i in input().split() if i!='\n']
if n&1:
for i in range(n):
if p[i]!=0:
graph[p[i]].append(i+1)
graph[i+1].append(p[i])
length=[0]*(n+1)
for i in graph:
length[i]=len(graph[i])
CHECK,OBSERVE=1,0
stack=[(OBSERVE,1,0)]
ans=[0]*(n+1)
count=0
while stack:
state,vertex,parent=stack.pop()
if state==OBSERVE:
stack.append((CHECK,vertex,parent))
for child in graph[vertex]:
if child != parent:
stack.append((OBSERVE,child,vertex))
else:
if length[vertex]%2==0:
count+=1
ans[vertex]=count
length[parent]-=1
vis=set()
bfs(1,count)
out=[0]*(n)
for i in range(1,n+1):
out[ans[i]-1]=i
print('YES')
for i in out:
sys.stdout.write(str(i)+'\n')
else:
print('NO')
|
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
|
CF
| 2,018 | 1 | 256 |
Destruction of a Tree
|
You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).
A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.
Destroy all vertices in the given tree or determine that it is impossible.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — number of vertices in a tree.
The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n). If pi ≠ 0 there is an edge between vertices i and pi. It is guaranteed that the given graph is a tree.
|
If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes).
If it's possible to destroy all vertices, in the next n lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.
| null |
In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.
|
[{"input": "5\n0 1 2 1 2", "output": "YES\n1\n2\n3\n5\n4"}, {"input": "4\n0 1 2 3", "output": "NO"}]
| 2,000 |
["constructive algorithms", "dfs and similar", "dp", "greedy", "trees"]
| 95 |
[{"input": "5\r\n0 1 2 1 2\r\n", "output": "YES\r\n1\r\n2\r\n3\r\n5\r\n4\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n1\r\n"}, {"input": "8\r\n3 1 4 0 4 2 4 5\r\n", "output": "NO\r\n"}, {"input": "100\r\n81 96 65 28 4 40 5 49 5 89 48 70 94 70 17 58 58 1 61 19 45 33 46 19 22 83 56 67 62 82 57 16 29 36 84 71 42 66 78 54 73 45 82 80 67 88 79 69 61 66 5 36 24 60 96 21 77 67 68 29 87 37 91 34 78 43 0 69 49 62 16 2 68 79 57 1 60 12 39 99 14 37 30 92 47 18 14 75 73 39 94 12 43 87 90 22 91 59 54 71\r\n", "output": "NO\r\n"}, {"input": "100\r\n57 85 27 81 41 27 73 10 73 95 91 90 89 41 86 44 6 20 9 13 46 73 56 19 37 32 40 42 79 76 96 5 6 8 76 52 14 86 33 69 100 95 58 87 43 47 17 39 48 28 77 65 100 100 41 39 87 5 61 67 94 64 61 88 32 23 79 44 0 67 44 23 48 96 48 56 86 75 90 2 17 46 4 75 42 90 17 77 5 33 87 91 27 28 58 95 58 47 33 6\r\n", "output": "NO\r\n"}, {"input": "21\r\n11 10 12 3 6 0 8 6 16 14 5 9 7 19 1 13 15 21 4 2 20\r\n", "output": "YES\r\n21\r\n18\r\n2\r\n20\r\n14\r\n10\r\n4\r\n19\r\n12\r\n3\r\n16\r\n9\r\n7\r\n13\r\n6\r\n8\r\n11\r\n5\r\n15\r\n17\r\n1\r\n"}, {"input": "61\r\n10 42 20 50 4 24 18 55 19 5 57 13 3 35 58 48 31 46 40 45 15 53 14 25 43 41 22 23 54 39 38 44 16 37 12 34 32 28 26 30 59 47 21 9 8 52 1 0 33 49 36 51 17 11 29 7 48 61 6 27 2\r\n", "output": "YES\r\n27\r\n60\r\n53\r\n22\r\n31\r\n17\r\n28\r\n38\r\n14\r\n23\r\n12\r\n35\r\n3\r\n13\r\n45\r\n20\r\n55\r\n8\r\n54\r\n29\r\n57\r\n11\r\n16\r\n48\r\n49\r\n33\r\n4\r\n50\r\n10\r\n5\r\n7\r\n56\r\n46\r\n18\r\n51\r\n52\r\n34\r\n36\r\n32\r\n37\r\n9\r\n44\r\n40\r\n19\r\n39\r\n30\r\n41\r\n26\r\n6\r\n59\r\n25\r\n24\r\n21\r\n43\r\n58\r\n15\r\n2\r\n61\r\n47\r\n42\r\n1\r\n"}, {"input": "21\r\n11 19 4 19 6 0 13 7 6 2 5 3 16 10 1 9 15 21 9 21 2\r\n", "output": "YES\r\n7\r\n8\r\n16\r\n13\r\n10\r\n14\r\n2\r\n21\r\n18\r\n20\r\n3\r\n12\r\n19\r\n4\r\n6\r\n9\r\n11\r\n5\r\n15\r\n17\r\n1\r\n"}, {"input": "61\r\n47 61 20 5 10 59 46 55 44 1 57 13 3 35 21 48 31 7 9 45 43 53 14 6 42 39 22 23 54 40 45 37 16 36 12 44 34 28 25 19 26 33 25 39 33 36 42 0 50 4 52 46 17 11 29 7 48 15 41 27 58\r\n", "output": "YES\r\n6\r\n24\r\n41\r\n59\r\n40\r\n30\r\n9\r\n19\r\n37\r\n32\r\n52\r\n51\r\n46\r\n7\r\n18\r\n56\r\n36\r\n34\r\n61\r\n2\r\n15\r\n58\r\n43\r\n21\r\n25\r\n39\r\n26\r\n44\r\n55\r\n8\r\n54\r\n29\r\n57\r\n11\r\n16\r\n48\r\n28\r\n38\r\n14\r\n23\r\n12\r\n35\r\n3\r\n13\r\n27\r\n60\r\n53\r\n22\r\n31\r\n17\r\n45\r\n20\r\n42\r\n33\r\n50\r\n49\r\n5\r\n4\r\n1\r\n47\r\n10\r\n"}, {"input": "79\r\n0 56 56 42 56 56 56 56 4 56 56 22 56 56 56 48 56 56 56 56 56 24 56 16 56 56 56 9 56 56 56 56 56 56 56 56 56 55 56 56 12 20 56 28 56 56 56 38 56 56 56 56 56 56 44 1 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56\r\n", "output": "YES\r\n12\r\n41\r\n24\r\n22\r\n48\r\n16\r\n55\r\n38\r\n28\r\n44\r\n4\r\n9\r\n20\r\n42\r\n56\r\n2\r\n3\r\n5\r\n6\r\n7\r\n8\r\n10\r\n11\r\n13\r\n14\r\n15\r\n17\r\n18\r\n19\r\n21\r\n23\r\n25\r\n26\r\n27\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n39\r\n40\r\n43\r\n45\r\n46\r\n47\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67\r\n68\r\n69\r\n70\r\n71\r\n72\r\n73\r\n74\r\n75\r\n76\r\n77\r\n78\r\n79\r\n1\r\n"}, {"input": "121\r\n110 31 57 33 45 33 33 33 91 102 79 33 61 72 107 101 117 10 118 33 33 64 24 94 117 76 33 23 33 49 5 52 95 78 33 39 33 92 17 33 25 33 56 33 3 88 33 108 62 15 28 111 67 33 33 11 96 33 36 70 46 98 80 104 33 19 60 33 112 51 33 2 33 33 121 59 33 41 50 81 105 33 115 34 33 18 84 32 33 33 87 13 86 103 16 119 33 63 30 43 83 53 26 100 69 33 14 38 33 75 66 120 33 33 9 99 0 93 1 48 116\r\n", "output": "YES\r\n33\r\n4\r\n6\r\n7\r\n8\r\n12\r\n20\r\n21\r\n27\r\n29\r\n35\r\n37\r\n40\r\n42\r\n44\r\n47\r\n54\r\n55\r\n58\r\n65\r\n68\r\n71\r\n73\r\n74\r\n77\r\n82\r\n85\r\n89\r\n90\r\n97\r\n106\r\n109\r\n113\r\n114\r\n16\r\n95\r\n83\r\n101\r\n9\r\n115\r\n87\r\n91\r\n34\r\n84\r\n41\r\n78\r\n117\r\n25\r\n39\r\n17\r\n59\r\n36\r\n26\r\n76\r\n94\r\n103\r\n23\r\n24\r\n51\r\n28\r\n60\r\n70\r\n53\r\n67\r\n10\r\n102\r\n86\r\n18\r\n118\r\n93\r\n66\r\n19\r\n52\r\n111\r\n88\r\n32\r\n61\r\n46\r\n92\r\n13\r\n108\r\n38\r\n120\r\n48\r\n69\r\n112\r\n81\r\n105\r\n63\r\n80\r\n62\r\n98\r\n30\r\n49\r\n116\r\n99\r\n75\r\n121\r\n64\r\n22\r\n100\r\n104\r\n56\r\n43\r\n79\r\n11\r\n15\r\n50\r\n14\r\n107\r\n2\r\n72\r\n5\r\n31\r\n3\r\n45\r\n96\r\n57\r\n1\r\n110\r\n119\r\n"}, {"input": "21\r\n5 20 9 19 8 0 13 6 13 19 5 3 8 10 1 9 1 20 3 10 18\r\n", "output": "YES\r\n18\r\n21\r\n20\r\n2\r\n10\r\n14\r\n19\r\n4\r\n3\r\n12\r\n9\r\n16\r\n13\r\n7\r\n8\r\n6\r\n5\r\n11\r\n1\r\n15\r\n17\r\n"}, {"input": "61\r\n5 61 20 5 50 59 56 29 44 1 48 13 20 35 61 33 38 52 30 8 43 17 35 43 24 59 22 23 11 26 38 37 48 36 13 37 44 23 30 19 26 1 15 19 8 18 42 0 50 33 52 36 17 11 29 18 48 15 24 22 42\r\n", "output": "YES\r\n56\r\n7\r\n18\r\n46\r\n52\r\n51\r\n36\r\n34\r\n37\r\n32\r\n44\r\n9\r\n19\r\n40\r\n30\r\n39\r\n26\r\n41\r\n59\r\n6\r\n24\r\n25\r\n43\r\n21\r\n15\r\n58\r\n61\r\n2\r\n42\r\n47\r\n1\r\n5\r\n4\r\n50\r\n49\r\n33\r\n16\r\n48\r\n11\r\n29\r\n8\r\n20\r\n3\r\n13\r\n12\r\n35\r\n14\r\n23\r\n28\r\n38\r\n17\r\n22\r\n27\r\n60\r\n53\r\n31\r\n45\r\n55\r\n54\r\n57\r\n10\r\n"}, {"input": "21\r\n18 18 18 18 18 0 18 18 18 18 18 18 18 18 18 18 18 6 18 18 18\r\n", "output": "YES\r\n18\r\n2\r\n3\r\n4\r\n5\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n6\r\n19\r\n20\r\n21\r\n1\r\n"}, {"input": "61\r\n56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 0 56 56 56 56 56 56 56 48 56 56 56 56 56\r\n", "output": "YES\r\n56\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n48\r\n57\r\n58\r\n59\r\n60\r\n61\r\n1\r\n"}, {"input": "21\r\n15 6 13 7 15 21 8 0 7 16 16 21 12 6 12 12 13 6 15 16 7\r\n", "output": "YES\r\n6\r\n2\r\n14\r\n18\r\n7\r\n4\r\n8\r\n9\r\n16\r\n10\r\n11\r\n20\r\n15\r\n5\r\n12\r\n21\r\n13\r\n3\r\n17\r\n19\r\n1\r\n"}, {"input": "61\r\n58 39 45 57 31 43 11 24 8 18 56 54 47 37 50 40 19 16 29 10 1 23 36 28 21 48 52 55 27 42 2 33 46 25 53 6 15 26 14 17 9 44 56 34 5 61 38 12 30 7 49 32 20 41 51 0 3 4 60 35 13\r\n", "output": "YES\r\n23\r\n22\r\n6\r\n36\r\n56\r\n43\r\n7\r\n11\r\n15\r\n50\r\n14\r\n37\r\n2\r\n39\r\n5\r\n31\r\n3\r\n45\r\n4\r\n57\r\n60\r\n59\r\n53\r\n35\r\n10\r\n20\r\n16\r\n18\r\n17\r\n40\r\n29\r\n19\r\n52\r\n27\r\n33\r\n32\r\n61\r\n46\r\n47\r\n13\r\n26\r\n38\r\n12\r\n48\r\n41\r\n54\r\n8\r\n9\r\n28\r\n24\r\n51\r\n55\r\n30\r\n49\r\n44\r\n42\r\n25\r\n34\r\n1\r\n58\r\n21\r\n"}, {"input": "21\r\n21 6 4 20 14 1 13 10 11 0 10 18 10 12 4 1 2 2 8 2 13\r\n", "output": "YES\r\n8\r\n19\r\n11\r\n9\r\n21\r\n13\r\n7\r\n10\r\n14\r\n5\r\n18\r\n12\r\n20\r\n4\r\n3\r\n15\r\n2\r\n17\r\n1\r\n6\r\n16\r\n"}, {"input": "61\r\n17 19 8 53 10 38 59 60 46 25 49 28 46 15 25 56 53 60 60 54 18 49 10 53 29 19 11 61 24 11 17 52 32 54 29 55 0 1 14 56 25 14 33 53 47 56 8 6 53 55 16 46 47 9 24 37 3 52 25 37 26\r\n", "output": "YES\r\n15\r\n14\r\n39\r\n42\r\n59\r\n7\r\n3\r\n57\r\n18\r\n21\r\n28\r\n12\r\n26\r\n61\r\n19\r\n2\r\n16\r\n51\r\n9\r\n54\r\n20\r\n34\r\n33\r\n43\r\n37\r\n56\r\n40\r\n46\r\n13\r\n52\r\n32\r\n58\r\n8\r\n60\r\n47\r\n45\r\n6\r\n48\r\n1\r\n17\r\n53\r\n4\r\n24\r\n29\r\n25\r\n10\r\n5\r\n23\r\n41\r\n35\r\n55\r\n36\r\n50\r\n44\r\n49\r\n11\r\n27\r\n30\r\n22\r\n31\r\n38\r\n"}, {"input": "21\r\n18 0 18 2 21 2 9 15 3 5 8 2 8 21 6 10 21 13 9 1 13\r\n", "output": "YES\r\n3\r\n9\r\n7\r\n19\r\n6\r\n2\r\n4\r\n12\r\n10\r\n16\r\n21\r\n5\r\n14\r\n17\r\n13\r\n8\r\n15\r\n11\r\n1\r\n18\r\n20\r\n"}, {"input": "61\r\n45 48 30 23 15 47 8 3 35 56 54 35 17 47 35 56 32 42 14 37 36 44 6 44 1 44 41 46 43 0 33 3 44 54 43 3 47 57 7 32 29 60 36 36 43 61 36 47 3 48 18 8 17 29 3 54 3 6 43 43 56\r\n", "output": "YES\r\n41\r\n27\r\n46\r\n28\r\n56\r\n10\r\n16\r\n61\r\n29\r\n54\r\n11\r\n34\r\n15\r\n5\r\n7\r\n39\r\n8\r\n52\r\n57\r\n38\r\n33\r\n31\r\n44\r\n22\r\n24\r\n26\r\n23\r\n4\r\n6\r\n58\r\n14\r\n19\r\n37\r\n20\r\n47\r\n48\r\n2\r\n50\r\n18\r\n51\r\n60\r\n42\r\n43\r\n35\r\n9\r\n12\r\n36\r\n21\r\n3\r\n30\r\n32\r\n17\r\n13\r\n53\r\n40\r\n49\r\n55\r\n59\r\n1\r\n45\r\n25\r\n"}]
| false |
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
n = int(f.readline())
p = list(map(int, f.readline().split()))
# Read reference output
with open(output_path) as f_ref:
ref_first_line = f_ref.readline().strip()
# Read submission output
with open(submission_path) as f_sub:
lines = [line.strip() for line in f_sub.readlines()]
# Check first line
if not lines:
print(0)
return
submission_first = lines[0]
if submission_first != ref_first_line:
print(0)
return
if ref_first_line == "NO":
if len(lines) != 1:
print(0)
return
else:
print(1)
return
# Check YES case
if len(lines) != n + 1:
print(0)
return
sequence = lines[1:]
if len(sequence) != n:
print(0)
return
try:
sequence = list(map(int, sequence))
except:
print(0)
return
if any(x < 1 or x > n for x in sequence):
print(0)
return
if len(set(sequence)) != n:
print(0)
return
# Build adjacency list
adj = [[] for _ in range(n + 1)]
for i in range(1, n + 1):
pi = p[i - 1]
if pi != 0:
adj[i].append(pi)
adj[pi].append(i)
current_degree = [0] * (n + 1)
for i in range(1, n + 1):
current_degree[i] = len(adj[i])
alive = [True] * (n + 1)
# Simulate destruction
for node in sequence:
if not alive[node]:
print(0)
return
if current_degree[node] % 2 != 0:
print(0)
return
for neighbor in adj[node]:
if alive[neighbor]:
current_degree[neighbor] -= 1
alive[node] = False
current_degree[node] = 0
if all(not alive[i] for i in range(1, n + 1)):
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true |
103/A
|
103
|
A
|
Python 3
|
TESTS
| 4 | 156 | 6,656,000 |
86223128
|
n=int(input())
l=list(map(int,input().split()))
i=0
x=0
for j in l:
x=x+j+i
if(j>1):
i=i+1
print(x)
| 25 | 92 | 0 |
183690622
|
a=int(input());b=[int(x) for x in input().split()];c=b[0]
for i in range(1,a):
c+=b[i]+i*(b[i]-1)
print(c)
|
Codeforces Beta Round 80 (Div. 1 Only)
|
CF
| 2,011 | 2 | 256 |
Testing Pants for Sadness
|
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".
The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.
A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.
Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
|
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
|
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
| null |
Note to the second sample. In the worst-case scenario you will need five clicks:
- the first click selects the first variant to the first question, this answer turns out to be wrong.
- the second click selects the second variant to the first question, it proves correct and we move on to the second question;
- the third click selects the first variant to the second question, it is wrong and we go back to question 1;
- the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
- the fifth click selects the second variant to the second question, it proves correct, the test is finished.
|
[{"input": "2\n1 1", "output": "2"}, {"input": "2\n2 2", "output": "5"}, {"input": "1\n10", "output": "10"}]
| 1,100 |
["greedy", "implementation", "math"]
| 25 |
[{"input": "2\r\n1 1\r\n", "output": "2"}, {"input": "2\r\n2 2\r\n", "output": "5"}, {"input": "1\r\n10\r\n", "output": "10"}, {"input": "3\r\n2 4 1\r\n", "output": "10"}, {"input": "4\r\n5 5 3 1\r\n", "output": "22"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2999999999"}, {"input": "10\r\n5 7 8 1 10 3 6 4 10 6\r\n", "output": "294"}, {"input": "100\r\n5 7 5 3 5 4 6 5 3 6 4 6 6 2 1 9 6 5 3 8 4 10 1 9 1 3 7 6 5 5 8 8 7 7 8 9 2 10 3 5 4 2 6 10 2 6 9 6 1 9 3 7 7 8 3 9 9 5 10 10 3 10 7 8 3 9 8 3 2 4 10 2 1 1 7 3 9 10 4 6 9 8 2 1 4 10 1 10 6 8 7 5 3 3 6 2 7 10 3 8\r\n", "output": "24212"}, {"input": "10\r\n12528238 329065023 620046219 303914458 356423530 751571368 72944261 883971060 123105651 868129460\r\n", "output": "27409624352"}, {"input": "1\r\n84355694\r\n", "output": "84355694"}, {"input": "2\r\n885992042 510468669\r\n", "output": "1906929379"}, {"input": "100\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "100"}, {"input": "100\r\n2 1 2 2 2 2 1 2 2 1 2 2 2 1 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 2 1 1 2 1 2 1 1 1 2 2 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 1 1 1 2 2 1 1 2 1 1 2 2 2 2 1 2 2 2 1 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1\r\n", "output": "2686"}, {"input": "100\r\n1 3 2 1 1 2 1 3 2 2 3 1 1 1 2 2 1 3 3 1 1 2 2 3 2 1 3 1 3 2 1 1 3 3 2 1 2 2 2 3 2 2 3 2 2 3 2 1 3 1 1 2 1 3 2 2 1 1 1 1 1 1 3 1 2 3 1 1 1 1 1 2 3 3 1 1 1 1 2 3 3 1 3 2 2 3 2 1 3 2 2 3 1 1 3 2 3 2 3 1\r\n", "output": "4667"}]
| false |
stdio
| null | true |
103/A
|
103
|
A
|
PyPy 3
|
TESTS
| 4 | 186 | 0 |
108099327
|
n = int(input())
c = list(map(int, input().split()))
k = 1
for i in range(len(c)):
k *= c[i]
k += (n-1)
print(k)
| 25 | 92 | 0 |
190986998
|
n = int(input())
l = [int(i) for i in input().split()]
c = len(l)
for index, i in enumerate(l):
c += (i-1)*(index+1)
print(c)
|
Codeforces Beta Round 80 (Div. 1 Only)
|
CF
| 2,011 | 2 | 256 |
Testing Pants for Sadness
|
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".
The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.
A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.
Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?
|
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.
|
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
| null |
Note to the second sample. In the worst-case scenario you will need five clicks:
- the first click selects the first variant to the first question, this answer turns out to be wrong.
- the second click selects the second variant to the first question, it proves correct and we move on to the second question;
- the third click selects the first variant to the second question, it is wrong and we go back to question 1;
- the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
- the fifth click selects the second variant to the second question, it proves correct, the test is finished.
|
[{"input": "2\n1 1", "output": "2"}, {"input": "2\n2 2", "output": "5"}, {"input": "1\n10", "output": "10"}]
| 1,100 |
["greedy", "implementation", "math"]
| 25 |
[{"input": "2\r\n1 1\r\n", "output": "2"}, {"input": "2\r\n2 2\r\n", "output": "5"}, {"input": "1\r\n10\r\n", "output": "10"}, {"input": "3\r\n2 4 1\r\n", "output": "10"}, {"input": "4\r\n5 5 3 1\r\n", "output": "22"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2999999999"}, {"input": "10\r\n5 7 8 1 10 3 6 4 10 6\r\n", "output": "294"}, {"input": "100\r\n5 7 5 3 5 4 6 5 3 6 4 6 6 2 1 9 6 5 3 8 4 10 1 9 1 3 7 6 5 5 8 8 7 7 8 9 2 10 3 5 4 2 6 10 2 6 9 6 1 9 3 7 7 8 3 9 9 5 10 10 3 10 7 8 3 9 8 3 2 4 10 2 1 1 7 3 9 10 4 6 9 8 2 1 4 10 1 10 6 8 7 5 3 3 6 2 7 10 3 8\r\n", "output": "24212"}, {"input": "10\r\n12528238 329065023 620046219 303914458 356423530 751571368 72944261 883971060 123105651 868129460\r\n", "output": "27409624352"}, {"input": "1\r\n84355694\r\n", "output": "84355694"}, {"input": "2\r\n885992042 510468669\r\n", "output": "1906929379"}, {"input": "100\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "100"}, {"input": "100\r\n2 1 2 2 2 2 1 2 2 1 2 2 2 1 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 2 1 1 2 1 2 1 1 1 2 2 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 1 1 1 2 2 1 1 2 1 1 2 2 2 2 1 2 2 2 1 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1\r\n", "output": "2686"}, {"input": "100\r\n1 3 2 1 1 2 1 3 2 2 3 1 1 1 2 2 1 3 3 1 1 2 2 3 2 1 3 1 3 2 1 1 3 3 2 1 2 2 2 3 2 2 3 2 2 3 2 1 3 1 1 2 1 3 2 2 1 1 1 1 1 1 3 1 2 3 1 1 1 1 1 2 3 3 1 1 1 1 2 3 3 1 3 2 2 3 2 1 3 2 2 3 1 1 3 2 3 2 3 1\r\n", "output": "4667"}]
| false |
stdio
| null | true |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.