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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
847/H
|
847
|
H
|
Python 3
|
TESTS
| 8
| 62
| 5,529,600
|
31653549
|
n = int(input())
t = list(map(int, input().split()))
s = a = b = 0
for i in range(n):
a = max(a, t[i] - i)
b = max(b, t[-1 - i] - i)
x = (b - a + n) // 2
y = 0
for i in range(x):
s += max(y - t[i], 0)
y = max(t[i], y) + 1
y = 0
for i in range(n - x):
s += max(y - t[-1 - i], 0)
y = max(t[-1 - i], y) + 1
print(s)
| 39
| 124
| 19,046,400
|
211256726
|
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
N = int(input())
A = list(map(int,input().split()))
R,L = A[:],A[:]
r,l = [0,0],[0,0]
for i in range(N-2,-1,-1):
if R[i]<=R[i+1]:
r.append(r[-1]+((R[i+1]+1)-R[i]))
R[i] = R[i+1]+1
else:
r.append(r[-1])
r.pop()
r = r[::-1]
for i in range(1,N):
if L[i]<=L[i-1]:
l.append(l[-1]+((L[i-1]+1)-L[i]))
L[i] = L[i-1]+1
else:
l.append(l[-1])
l.pop()
# print(L,R,l,r)
ans = float('inf')
for i in range(N):
ans = min(ans,(max(R[i],L[i])-A[i])+r[i]+l[i])
print(ans)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
631/A
|
631
|
A
|
PyPy 3
|
TESTS
| 11
| 156
| 1,433,600
|
102280244
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tmp = 0
ans1, ans2 = 0, 0
for i in range(len(a)-1):
tmp = a[i] | a[i+1]
ans1 |= tmp
for i in range(len(b)-1):
tmp = b[i] | b[i+1]
ans2 |= tmp
print(ans1 + ans2)
| 27
| 46
| 0
|
167839946
|
def maximize(arr1,arr2):
value1 = 0
value2= 0
for num in arr1:
value1|=num
for num in arr2:
value2 |= num
return value2 + value1
n = int(input())
arr1 = list(map(int,input().split()))
arr2 = list(map(int,input().split()))
print(maximize(arr1,arr2))
|
Codeforces Round 344 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Interview
|
Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.
We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays.
The second line contains n integers ai (0 ≤ ai ≤ 109).
The third line contains n integers bi (0 ≤ bi ≤ 109).
|
Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.
| null |
Bitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.
In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.
In the second sample, the maximum value is obtained for l = 1 and r = 9.
|
[{"input": "5\n1 2 4 3 2\n2 3 3 12 1", "output": "22"}, {"input": "10\n13 2 7 11 8 4 9 8 5 1\n5 7 18 9 2 3 0 11 8 6", "output": "46"}]
| 900
|
["brute force", "implementation"]
| 27
|
[{"input": "5\r\n1 2 4 3 2\r\n2 3 3 12 1\r\n", "output": "22"}, {"input": "10\r\n13 2 7 11 8 4 9 8 5 1\r\n5 7 18 9 2 3 0 11 8 6\r\n", "output": "46"}, {"input": "25\r\n12 30 38 109 81 124 80 33 38 48 29 78 96 48 96 27 80 77 102 65 80 113 31 118 35\r\n25 64 95 13 12 6 111 80 85 16 61 119 23 65 73 65 20 95 124 18 28 79 125 106 116\r\n", "output": "254"}, {"input": "20\r\n64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64\r\n64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64\r\n", "output": "128"}, {"input": "1\r\n1000000000\r\n1000000000\r\n", "output": "2000000000"}, {"input": "1\r\n0\r\n0\r\n", "output": "0"}, {"input": "2\r\n7 16\r\n16 7\r\n", "output": "46"}, {"input": "4\r\n6 0 0 0\r\n0 0 0 1\r\n", "output": "7"}, {"input": "8\r\n1 2 4 8 16 32 64 128\r\n1 2 4 8 16 32 64 128\r\n", "output": "510"}, {"input": "1\r\n2\r\n3\r\n", "output": "5"}, {"input": "1\r\n4\r\n3\r\n", "output": "7"}, {"input": "1\r\n1\r\n1\r\n", "output": "2"}]
| false
|
stdio
| null | true
|
29/A
|
29
|
A
|
Python 3
|
TESTS
| 13
| 186
| 0
|
53426601
|
c={}
y=False
for i in range(int(input())):
x,s=map(int,input().split())
if x+s in c:
if c[x+s]+x+s==x:
print("YES")
y=True
c[x]=s
if y==False:
print("NO")
| 30
| 62
| 0
|
156054951
|
import sys
input = sys.stdin.readline
n = int(input())
w = [list(map(int, input().split())) for _ in range(n)]
ans = "NO"
for i in w:
for j in w:
if j[0] == sum(i) and sum(j) == i[0]:
ans = "YES"
break
if ans == "YES":
break
print(ans)
|
Codeforces Beta Round 29 (Div. 2, Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Spit Problem
|
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.
The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
|
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
|
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
| null | null |
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
| 1,000
|
["brute force"]
| 30
|
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
103/A
|
103
|
A
|
PyPy 3-64
|
TESTS
| 4
| 92
| 0
|
150953173
|
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]
else:
c+=a[i]-1+a[i+1]
print(c)
| 25
| 92
| 0
|
9732586
|
n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(len(a)):
res += (1 + (i+1)*(a[i]-1))
print(res)
|
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
|
29/A
|
29
|
A
|
Python 3
|
TESTS
| 13
| 248
| 0
|
51448472
|
data = {}
for _ in range(int(input())):
a,b = map(int,str(input()).split(" "))
data[a] = a+b
if set(data.keys()).intersection(set(data.values())).__len__()!=2:
print("NO")
else:
print("YES")
| 30
| 92
| 0
|
9383286
|
n = int(input())
s = set()
for i in range(n):
x, d = [int(x) for x in input().split()]
if (x + d, -d) in s:
print('YES')
exit()
s.add((x, d))
print('NO')
|
Codeforces Beta Round 29 (Div. 2, Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Spit Problem
|
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.
The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
|
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
|
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
| null | null |
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
| 1,000
|
["brute force"]
| 30
|
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
282/B
|
282
|
B
|
Python 3
|
TESTS
| 2
| 62
| 0
|
230848284
|
n = int(input())
eggs = []
for _ in range(n):
a, g = map(int, input().split())
eggs.append((a, g))
Sa = 0
Sg = 0
distribution = ""
for a, g in eggs:
if Sa + a <= Sg + g + 500:
Sa += a
distribution += "A"
else:
Sg += g
distribution += "G"
if abs(Sa - Sg) > 500:
print("-1")
else:
print(distribution)
| 54
| 4,116
| 149,708,800
|
226850439
|
n = int(input())
eggs = []
for i in range(n):
a, g = map(int, input().split())
eggs.append((a, g))
distribution = [''] * n
Sa = 0 # Total money paid to A
Sg = 0 # Total money paid to G
for i in range(n):
if abs(Sa + eggs[i][0] - Sg) <= 500:
distribution[i] = 'A'
Sa += eggs[i][0]
else:
distribution[i] = 'G'
Sg += eggs[i][1]
if abs(Sa - Sg) <= 500:
print(''.join(distribution))
else:
print("-1")
|
Codeforces Round 173 (Div. 2)
|
CF
| 2,013
| 5
| 256
|
Painting Eggs
|
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
|
The first line contains integer n (1 ≤ n ≤ 106) — the number of eggs.
Next n lines contain two integers ai and gi each (0 ≤ ai, gi ≤ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
|
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| ≤ 500.
If there are several solutions, you are allowed to print any of them.
| null | null |
[{"input": "2\n1 999\n999 1", "output": "AG"}, {"input": "3\n400 600\n400 600\n400 600", "output": "AGA"}]
| 1,500
|
["greedy", "math"]
| 54
|
[{"input": "2\r\n1 999\r\n999 1\r\n", "output": "AG\r\n"}, {"input": "3\r\n400 600\r\n400 600\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "2\r\n500 500\r\n500 500\r\n", "output": "AG\r\n"}, {"input": "1\r\n1 999\r\n", "output": "A\r\n"}, {"input": "10\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n", "output": "AAAAAAAAAA\r\n"}, {"input": "2\r\n499 501\r\n501 499\r\n", "output": "AG\r\n"}, {"input": "3\r\n500 500\r\n1 999\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "1\r\n0 1000\r\n", "output": "A\r\n"}, {"input": "1\r\n500 500\r\n", "output": "A\r\n"}, {"input": "1\r\n1000 0\r\n", "output": "G\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input data
with open(input_path) as f:
n = int(f.readline())
eggs = [tuple(map(int, line.split())) for line in f]
# Read reference and submission outputs
with open(output_path) as f:
ref_output = f.read().strip()
with open(submission_path) as f:
sub_output = f.read().strip()
# Check if reference says it's impossible
if ref_output == "-1":
return 1 if sub_output == "-1" else 0
# Check submission is not -1
if sub_output == "-1":
return 0
# Validate submission format
if len(sub_output) != n or any(c not in 'AG' for c in sub_output):
return 0
# Calculate Sa and Sg
sa, sg = 0, 0
for i in range(n):
a, g = eggs[i]
if sub_output[i] == 'A':
sa += a
else:
sg += g
# Check the absolute difference condition
if abs(sa - sg) > 500:
return 0
return 1
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
score = main(input_path, output_path, submission_path)
print(score)
| true
|
965/D
|
965
|
D
|
Python 3
|
TESTS
| 8
| 124
| 7,782,400
|
53069118
|
w,l=map(int,input().split())
a=list(map(int,input().split()))
s,mn=0,100000000
for i in range(l):s+=a[i]
mn=min(mn,s)
for i in range(l,w-1):
s+=a[i];s-=a[i-l]
mn=min(mn,s)
print(mn)
| 16
| 77
| 13,312,000
|
216694136
|
def ok(cnt):
for i in range(l, w):
if pref[i] - pref[i - l] < cnt:
return False
return True
w, l = map(int, input().split())
a = list(map(int, input().split()))
pref = [0] * w
for i in range(1, w):
pref[i] = pref[i - 1] + a[i - 1]
li, ri = 1, pref[-1]
ans = 0
while li <= ri:
m = (li + ri) // 2
if ok(m):
ans = m
li = m + 1
else:
ri = m - 1
print(ans)# 1690885996.9007297
|
Codeforces Round 476 (Div. 2) [Thanks, Telegram!]
|
CF
| 2,018
| 1
| 256
|
Single-use Stones
|
A lot of frogs want to cross a river. A river is $$$w$$$ units width, but frogs can only jump $$$l$$$ units long, where $$$l < w$$$. Frogs can also jump on lengths shorter than $$$l$$$. but can't jump longer. Hopefully, there are some stones in the river to help them.
The stones are located at integer distances from the banks. There are $$$a_i$$$ stones at the distance of $$$i$$$ units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water.
What is the maximum number of frogs that can cross the river, given that then can only jump on the stones?
|
The first line contains two integers $$$w$$$ and $$$l$$$ ($$$1 \le l < w \le 10^5$$$) — the width of the river and the maximum length of a frog's jump.
The second line contains $$$w - 1$$$ integers $$$a_1, a_2, \ldots, a_{w-1}$$$ ($$$0 \le a_i \le 10^4$$$), where $$$a_i$$$ is the number of stones at the distance $$$i$$$ from the bank the frogs are currently at.
|
Print a single integer — the maximum number of frogs that can cross the river.
| null |
In the first sample two frogs can use the different stones at the distance $$$5$$$, and one frog can use the stones at the distances $$$3$$$ and then $$$8$$$.
In the second sample although there are two stones at the distance $$$5$$$, that does not help. The three paths are: $$$0 \to 3 \to 6 \to 9 \to 10$$$, $$$0 \to 2 \to 5 \to 8 \to 10$$$, $$$0 \to 1 \to 4 \to 7 \to 10$$$.
|
[{"input": "10 5\n0 0 1 0 2 0 0 1 0", "output": "3"}, {"input": "10 3\n1 1 1 1 2 1 1 1 1", "output": "3"}]
| 1,900
|
["binary search", "flows", "greedy", "two pointers"]
| 16
|
[{"input": "10 5\r\n0 0 1 0 2 0 0 1 0\r\n", "output": "3\r\n"}, {"input": "10 3\r\n1 1 1 1 2 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "2 1\r\n0\r\n", "output": "0\r\n"}, {"input": "2 1\r\n5\r\n", "output": "5\r\n"}, {"input": "10 4\r\n0 0 6 2 7 1 6 4 0\r\n", "output": "8\r\n"}, {"input": "100 15\r\n0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 0\r\n", "output": "5\r\n"}, {"input": "10 4\r\n10 10 10 10 10 10 10 10 10\r\n", "output": "40\r\n"}, {"input": "100 34\r\n16 0 10 11 12 13 0 5 4 14 6 15 4 9 1 20 19 14 1 7 14 11 10 20 6 9 12 8 3 19 20 4 17 17 8 11 14 18 5 20 17 0 3 18 14 12 11 12 5 5 11 7 9 17 4 8 4 10 0 0 12 9 15 3 15 14 19 12 6 8 17 19 4 18 19 3 8 3 9 1 6 15 4 16 1 18 13 16 3 5 20 11 10 9 9 17 20 15 12\r\n", "output": "312\r\n"}]
| false
|
stdio
| null | true
|
29/A
|
29
|
A
|
Python 3
|
TESTS
| 9
| 62
| 0
|
205011202
|
n =int(input())
data = []
for i in range(n) :
s = list(map(int, input().split()))
data.append(s)
res = "NO"
for i in data :
for j in data :
if i[1] + j[1] == 0 and i[0] <j[0] :
res = "YES"
break
print(res)
| 30
| 92
| 0
|
132202664
|
n=int(input())
k = []
for i in range(n):
x,d=map(int,input().split())
k.append([x,x+d])
ok=False
for i in range(n):
for j in range(i+1,n):
if k[i][0] == k[j][1] and k[i][1] == k[j][0]:
ok = True
if ok:
print("YES")
else:
print("NO")
|
Codeforces Beta Round 29 (Div. 2, Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Spit Problem
|
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.
The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
|
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
|
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
| null | null |
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
| 1,000
|
["brute force"]
| 30
|
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
29/A
|
29
|
A
|
Python 3
|
TESTS
| 9
| 124
| 0
|
116967000
|
s=[]
for _ in range(int(input())):
x,d=map(int,input().split())
s.append(d)
for i in range(len(s)):
for j in range(len(s)):
if s[i]+s[j]==0:
print('YES')
exit()
print('NO')
| 30
| 92
| 0
|
137137744
|
#!/usr/bin/env python
# coding=utf-8
'''
Author: Deean
Date: 2021-11-27 22:24:08
LastEditTime: 2021-11-27 22:31:19
Description: Spit Problem
FilePath: CF29A.py
'''
def func():
n = int(input())
point, distance = [0] * n, [0] * n
for i in range(n):
point[i], distance[i] = map(int, input().strip().split())
for i in range(n - 1):
for j in range(i + 1, n):
if point[i] + distance[i] == point[j] and point[j] + distance[j] == point[i]:
print("YES")
return
else:
print("NO")
if __name__ == '__main__':
func()
|
Codeforces Beta Round 29 (Div. 2, Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Spit Problem
|
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.
The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
|
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
|
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
| null | null |
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
| 1,000
|
["brute force"]
| 30
|
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
29/A
|
29
|
A
|
PyPy 3
|
TESTS
| 13
| 278
| 0
|
61237531
|
n = int(input())
a = []
b = []
for i in range(n):
x, d = map(int, input().split())
a.append(x)
b.append(d)
f = False
for i in range(n):
if a[i] + b[i] in a:
for j in range(i+1, n):
if a[j] + b[j] == a[i] and a[j] == a[i] + b[i]:
print('YES')
f = True
break
if not f:
print('NO')
| 30
| 92
| 0
|
138567702
|
n = int(input())
dict = {}
ans = "NO"
for i in range(n):
pos, spit = [int(item) for item in input().split(' ')]
dict[pos] = spit
for key in dict:
secCamel = key + dict[key]
if secCamel in dict and key == secCamel + dict[secCamel]:
ans = "YES"
break
print(ans)
|
Codeforces Beta Round 29 (Div. 2, Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Spit Problem
|
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.
The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
|
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
|
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
| null | null |
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
| 1,000
|
["brute force"]
| 30
|
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
29/A
|
29
|
A
|
PyPy 3
|
TESTS
| 13
| 310
| 1,228,800
|
84404851
|
n = int(input())
a = []
flag = 1
for _ in range(n) :
x,d = map(int,input().split())
a.append([x,d])
for i in range(n) :
for j in range(i+1,n) :
if a[i][0]+a[i][1]==a[j][0] and a[j][0]+a[j][1]==a[i][0] :
print("YES")
flag = 0
break
if flag==1 :
print("NO")
| 30
| 92
| 0
|
140951907
|
n = int(input())
s = set()
for i in range(n):
x, d = [int(x) for x in input().split()]
if (x + d, -d) in s:
print('YES')
exit()
s.add((x, d))
print('NO')
|
Codeforces Beta Round 29 (Div. 2, Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Spit Problem
|
In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.
The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.
|
The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.
|
If there are two camels, which spitted at each other, output YES. Otherwise, output NO.
| null | null |
[{"input": "2\n0 1\n1 -1", "output": "YES"}, {"input": "3\n0 1\n1 1\n2 -2", "output": "NO"}, {"input": "5\n2 -10\n3 10\n0 5\n5 -5\n10 1", "output": "YES"}]
| 1,000
|
["brute force"]
| 30
|
[{"input": "2\r\n0 1\r\n1 -1\r\n", "output": "YES\r\n"}, {"input": "3\r\n0 1\r\n1 1\r\n2 -2\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 -10\r\n3 10\r\n0 5\r\n5 -5\r\n10 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n-9897 -1144\r\n-4230 -6350\r\n2116 -3551\r\n-3635 4993\r\n3907 -9071\r\n-2362 4120\r\n-6542 984\r\n5807 3745\r\n7594 7675\r\n-5412 -6872\r\n", "output": "NO\r\n"}, {"input": "11\r\n-1536 3809\r\n-2406 -8438\r\n-1866 395\r\n5636 -490\r\n-6867 -7030\r\n7525 3575\r\n-6796 2908\r\n3884 4629\r\n-2862 -6122\r\n-8984 6122\r\n7137 -326\r\n", "output": "YES\r\n"}, {"input": "12\r\n-9765 1132\r\n-1382 -215\r\n-9405 7284\r\n-2040 3947\r\n-9360 3150\r\n6425 9386\r\n806 -2278\r\n-2121 -7284\r\n5663 -1608\r\n-8377 9297\r\n6245 708\r\n8470 6024\r\n", "output": "YES\r\n"}, {"input": "15\r\n8122 -9991\r\n-4068 -3386\r\n8971 3731\r\n3458 5161\r\n-8700 7562\r\n2691 8735\r\n-1510 -3892\r\n5183 -3753\r\n-7018 6637\r\n-7454 3386\r\n-818 -6377\r\n6771 -8647\r\n-7357 -1246\r\n-6186 1922\r\n9889 -3627\r\n", "output": "YES\r\n"}, {"input": "20\r\n-5264 6424\r\n-3664 -7459\r\n-2780 -9859\r\n-3317 6842\r\n5681 -8092\r\n1555 1904\r\n-6684 1414\r\n6593 -1253\r\n-5708 -1202\r\n335 1733\r\n-926 7579\r\n3459 -1904\r\n-4486 4006\r\n6201 3616\r\n2847 -5255\r\n8438 7057\r\n8171 6042\r\n-9102 3545\r\n7731 -233\r\n6264 6563\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
774/D
|
774
|
D
|
Python 3
|
TESTS
| 17
| 187
| 14,950,400
|
26147115
|
lmap = lambda f, x: list(map(f, x))
n,l,r = lmap(int, input().split())
l-=1
a = lmap(int, input().split())
b = lmap(int, input().split())
a1 = a[l:r]
a2 = b[l:r]
good = (sorted(a1)==sorted(a2))
if good:
print('TRUTH')
else:
print("LIE")
| 52
| 93
| 15,155,200
|
26152432
|
S = input()
list = []
list = S.split(" ")
n = int(list[0])
l = int(list[1])
r = int(list[2])
was = input().split(" ")
bec = input().split(" ")
f = True
for i in range(0, l-1):
if was[i] != bec[i]:
print("LIE")
f = False
break
if f == True:
for i in range(r,n):
if was[i] != bec[i]:
print("LIE")
f = False
break
if f == True:
print("TRUTH")
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 2
| 256
|
Lie or Truth
|
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
|
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
|
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
| null |
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
|
[{"input": "5 2 4\n3 4 2 3 1\n3 2 3 4 1", "output": "TRUTH"}, {"input": "3 1 2\n1 2 3\n3 1 2", "output": "LIE"}, {"input": "4 2 4\n1 1 1 1\n1 1 1 1", "output": "TRUTH"}]
| 1,500
|
["*special", "constructive algorithms", "implementation", "sortings"]
| 52
|
[{"input": "5 2 4\r\n3 4 2 3 1\r\n3 2 3 4 1\r\n", "output": "TRUTH\r\n"}, {"input": "3 1 2\r\n1 2 3\r\n3 1 2\r\n", "output": "LIE\r\n"}, {"input": "4 2 4\r\n1 1 1 1\r\n1 1 1 1\r\n", "output": "TRUTH\r\n"}, {"input": "5 1 3\r\n2 2 2 1 2\r\n2 2 2 1 2\r\n", "output": "TRUTH\r\n"}, {"input": "7 1 4\r\n2 5 5 5 4 3 4\r\n2 5 5 5 4 3 4\r\n", "output": "TRUTH\r\n"}, {"input": "10 1 10\r\n6 7 6 1 10 10 9 5 3 9\r\n7 10 9 6 1 5 9 3 10 6\r\n", "output": "TRUTH\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "TRUTH\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n2 1 3 4\r\n", "output": "LIE\r\n"}, {"input": "7 2 4\r\n1 2 3 4 5 7 6\r\n1 2 3 4 5 6 7\r\n", "output": "LIE\r\n"}, {"input": "5 1 2\r\n1 2 3 4 5\r\n1 2 3 5 4\r\n", "output": "LIE\r\n"}, {"input": "8 3 6\r\n5 3 1 1 1 1 3 5\r\n3 3 1 1 1 1 5 5\r\n", "output": "LIE\r\n"}, {"input": "4 2 2\r\n2 1 2 2\r\n1 2 2 2\r\n", "output": "LIE\r\n"}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
PyPy 3
|
TESTS
| 8
| 93
| 0
|
116883962
|
n = int(input())
arr = list(map(int, input().split()))
forward = arr[:]
cnt = arr[0] + 1
for i in range(1, n):
forward[i] = max(cnt, arr[i])
cnt = max(cnt, arr[i]) + 1
backward = arr[:]
cnt = arr[n - 1] + 1
for i in range(n - 2, -1,-1):
backward[i] = max(cnt, arr[i])
cnt = max(cnt, arr[i]) + 1
ans = 0
for i in range(n):
ans += min(forward[i], backward[i]) - arr[i]
print(ans)
| 39
| 187
| 11,161,600
|
198082420
|
n = int(input())
p = list(map(int, input().split()))
a = [0] * n
t = s = f = 0
for i in range(n):
if p[i] <= t: a[i] = t - p[i] + 1
t = max(p[i], t+1)
for i in range(n-1, 0, -1):
if p[i] <= s: a[i] = min(s - p[i] + 1, a[i])
else: a[i] = 0
s = max(p[i], s + 1)
for i in range(n):
p[i] += a[i]
f |= i > 1 and p[i] == p[i-1]
print(sum(a[:]) + f)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
PyPy 3
|
TESTS
| 4
| 77
| 0
|
120303176
|
#kaunsi range increasing aur kaunsi decreasing
def f(a):
n=len(a)
pref=[0]*(n)
left=[0]*(n) #values
left[0]=a[0]
for i in range(1,n):
left[i]=max(left[i-1]+1,a[i])
pref[i]=pref[i-1]+left[i]-a[i] #increasing shit
suf=[0]*(n)
right=[0]*(n)
right[n-1]=a[n-1]
for i in range(n-2,-1,-1):
right[i]=max(right[i+1]+1,a[i])
suf[i]=suf[i+1]+right[i]-a[i]
# ======> <=======
ans=float("inf")
for i in range(n-1):
ans=min(ans,pref[i]+suf[i+1]+max(0,right[i+1]-left[i]+1 )) # ye last vala max vo case ke liye jab dec vale ka first part
if ans==float("inf"):
return 0
return ans # greater than increasing ka last :okayge:
n=input()
lst=list(map(int,input().strip().split()))
print(f(lst))
| 39
| 202
| 11,161,600
|
197611088
|
n=int(input())
p=list(map(int,input().split()))
a=[0]*n
t=s=f=0
for i in range(n):
if p[i]<=t:a[i]=t-p[i]+1
t=max(p[i],t+1)
for i in range(n-1,0,-1):
if p[i] <= s: a[i] = min(s - p[i] + 1,a[i])
else:a[i]=0
s = max(p[i], s + 1)
for i in range(n):p[i]+=a[i];f|=i>1and p[i]==p[i-1]
print(sum(a[:])+f)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
390/A
|
390
|
A
|
Python 3
|
TESTS
| 14
| 311
| 1,843,200
|
52450631
|
n = int(input())
x = []
y = []
for _ in range(n):
a,b = map(int, input().split())
x.append(a)
y.append(b)
if max(x) > max(y):
print(len(set(y)))
else:
print((len(set(x))))
| 19
| 156
| 0
|
216395918
|
n=int(input())
v,h=set(),set()
for i in range(n):
x,y=map(int,input().split())
v.add(x)
h.add(y)
print(min(len(v),len(h)))
|
Codeforces Round 229 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and Alarm Clock
|
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.
The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:
- First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
- Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.
Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
|
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100).
Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
|
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
| null |
In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.
In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.
|
[{"input": "4\n0 0\n0 1\n0 2\n1 0", "output": "2"}, {"input": "4\n0 0\n0 1\n1 0\n1 1", "output": "2"}, {"input": "4\n1 1\n1 2\n2 3\n3 3", "output": "3"}]
| null |
["implementation"]
| 19
|
[{"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 0\r\n", "output": "2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1\r\n1 2\r\n2 3\r\n3 3\r\n", "output": "3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "1\r\n"}, {"input": "42\r\n28 87\r\n26 16\r\n59 90\r\n47 61\r\n28 83\r\n36 30\r\n67 10\r\n6 95\r\n9 49\r\n86 94\r\n52 24\r\n74 9\r\n86 24\r\n28 51\r\n25 99\r\n40 98\r\n57 33\r\n18 96\r\n43 36\r\n3 79\r\n4 86\r\n38 61\r\n25 61\r\n6 100\r\n58 81\r\n28 19\r\n64 4\r\n3 40\r\n2 56\r\n41 49\r\n97 100\r\n86 34\r\n42 36\r\n44 40\r\n14 85\r\n21 60\r\n76 99\r\n64 47\r\n69 13\r\n49 37\r\n97 37\r\n3 70\r\n", "output": "31\r\n"}, {"input": "21\r\n54 85\r\n69 37\r\n42 87\r\n53 18\r\n28 22\r\n13 3\r\n62 97\r\n38 91\r\n67 19\r\n100 79\r\n29 18\r\n48 40\r\n68 84\r\n44 20\r\n37 34\r\n73 53\r\n21 5\r\n20 73\r\n24 94\r\n23 52\r\n7 55\r\n", "output": "20\r\n"}, {"input": "19\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n1 11\r\n1 12\r\n1 13\r\n1 14\r\n1 15\r\n1 16\r\n1 17\r\n1 18\r\n1 19\r\n", "output": "1\r\n"}, {"input": "12\r\n1 1\r\n1 3\r\n1 5\r\n2 1\r\n2 2\r\n2 4\r\n3 1\r\n3 3\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
39/B
|
39
|
B
|
Python 3
|
TESTS
| 0
| 62
| 0
|
211827198
|
n = int(input())
l = list(map(int ,input().split()))
res = []
ref = 1
for i in range(len(l)) :
if l[i] == ref :
res.append(2000 + i+1 )
ref += 1
if res == [] :
print(0)
else:
print(*res)
| 35
| 122
| 6,758,400
|
131729603
|
n=int(input())
a=list(map(int,input().split()))
res = []
d = 1
for i in range(n):
if a[i]==d:
d+=1
res.append(2000+i+1)
print(len(res))
for i in res:
print(i,end=" ")
|
School Team Contest 1 (Winter Computer School 2010/11)
|
ICPC
| 2,010
| 2
| 64
|
Company Income Growth
|
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 — to a2 billion, ..., and in the current (2000 + n)-th year — an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year — 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the company’s losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 — 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
|
The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers ai ( - 100 ≤ ai ≤ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
|
Output k — the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
| null | null |
[{"input": "10\n-2 1 1 3 2 3 4 -10 -2 5", "output": "5\n2002 2005 2006 2007 2010"}, {"input": "3\n-1 -2 -3", "output": "0"}]
| 1,300
|
["greedy"]
| 35
|
[{"input": "10\r\n-2 1 1 3 2 3 4 -10 -2 5\r\n", "output": "5\r\n2002 2005 2006 2007 2010 "}, {"input": "3\r\n-1 -2 -3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 1\r\n", "output": "1\r\n2002 "}, {"input": "2\r\n-1 1\r\n", "output": "1\r\n2002 "}, {"input": "2\r\n-2 0\r\n", "output": "0\r\n"}, {"input": "2\r\n3 -3\r\n", "output": "0\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "1\r\n2001 "}, {"input": "3\r\n-2 -2 1\r\n", "output": "1\r\n2003 "}, {"input": "4\r\n-4 2 3 -1\r\n", "output": "0\r\n"}, {"input": "5\r\n-3 -3 -4 2 -2\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n2001 "}, {"input": "2\r\n1 2\r\n", "output": "2\r\n2001 2002 "}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 3 1 3 3 2 1 2 1 2\r\n", "output": "2\r\n2003 2006 "}, {"input": "15\r\n4 1 4 6 3 2 1 1 3 2 4 4 1 4 1\r\n", "output": "4\r\n2002 2006 2009 2011 "}, {"input": "15\r\n3 3 3 2 2 2 1 1 1 2 2 2 4 4 4\r\n", "output": "2\r\n2007 2010 "}, {"input": "15\r\n6 5 2 3 4 1 3 2 4 5 1 2 6 4 4\r\n", "output": "2\r\n2006 2008 "}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
Python 3
|
TESTS
| 8
| 108
| 0
|
43115327
|
n = int(input())
arr = list(map(int, input().split()))
starts = [0 for _ in range(n + 1)]
ends = [0 for _ in range(n + 1)]
starts[1] = arr[0]
ends[-2] = arr[-1]
for i in range(1, n):
starts[i + 1] = max(arr[i], starts[i] + 1)
ends[-i - 2] = max(arr[-i - 1], ends[-i - 1] + 1)
for i in range(n):
starts[i + 1] -= arr[i]
ends[-i - 2] -= arr[-i - 1]
for i in range(1, n):
starts[i + 1] += starts[i]
ends[-i - 2] += ends[-i - 1]
bst = min(starts[0] + ends[0], starts[-1] + ends[-1])
for i in range(1, n):
tmp = starts[i] + ends[i]
bst = min(bst, tmp)
print(bst)
| 39
| 217
| 10,956,800
|
116888053
|
n = int(input())
arr = list(map(int, input().split()))
forward = arr[:]
cnt = arr[0] + 1
for i in range(1, n):
forward[i] = max(cnt, arr[i])
cnt = max(cnt, arr[i]) + 1
backward = arr[:]
cnt = arr[n - 1] + 1
for i in range(n - 2, -1, -1):
backward[i] = max(cnt, arr[i])
cnt = max(cnt, arr[i]) + 1
ans = 0
for i in range(n):
if forward[i] < backward[i]:
ans += forward[i] - arr[i]
else:
if abs(backward[i] - forward[i]) == 1:
ans += 1
ans += backward[i] - arr[i]
print(ans)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
Python 3
|
TESTS
| 6
| 62
| 0
|
30857791
|
n = int(input())
l = [int(i) for i in input().split(" ")]
l_up = l[:]
l_down = l[::-1]
for i in range(n - 1):
if l_up[i+1] <= l_up[i]:
l_up[i+1] = l_up[i] + 1
for i in range(n - 1):
if l_down[i+1] <= l_down[i]:
l_down[i+1] = l_down[i] + 1
l_down = l_down[::-1]
index = 0
add = False
for index in range(n-1):
if l_up[index] < l_down[index] and l_up[index+1] >= l_down[index+1]:
if l_up[index+1] == l_down[index+1]:
break
else:
add = True
break
if add == False:
l_final = l_up[:index+1] + l_down[index+1:]
result = sum(l_final) - sum(l)
else:
l_final = l_up[:index+1] + l_down[index+1:]
result = sum(l_final) - sum(l) + 1
# print(index)
# print(l_up)
# print(l_down)
# print(result1)
# print(result2)
# print(l_final1)
# print(l_final2)
print(result)
| 39
| 296
| 14,131,200
|
31659774
|
s, l, r = 0, 0, int(input()) - 1
t = list(map(int, input().split()))
while 1:
while l < r and t[l] < t[l + 1]: l += 1
while l < r and t[r] < t[r - 1]: r -= 1
if l == r: break
if t[l] < t[r]:
s += t[l] - t[l + 1] + 1
t[l + 1] = t[l] + 1
else:
s += t[r] - t[r - 1] + 1
t[r - 1] = t[r] + 1
print(s)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
Python 3
|
TESTS
| 8
| 62
| 204,800
|
30479876
|
n = int(input())
a = list(map(int,input().strip().split()))
if n > 1:
li = [0]*n
ri = [0]*n
lm = a[0]
c = [0]*n
b = [0]*n
b[0] = a[0]
for i in range(1,n):
if lm >= a[i]:
li[i] = li[i-1] + (lm+1-a[i])
lm = lm+1
else:
li[i] = li[i-1]
lm = a[i]
b[i] = lm
lm = a[n-1]
c[n-1] = a[n-1]
for i in range(n-2,-1,-1):
if lm >= a[i]:
ri[i] = ri[i+1] + (lm+1-a[i])
lm = lm+1
else:
ri[i] = ri[i+1]
lm = a[i]
c[i] = a[i]
ans = 1<<64
for i in range(n):
if i == 0:
ans = min(ans,ri[i],ri[i+1])
elif i== n-1:
ans = min(ans,li[i],li[i-1])
else:
val = min(li[i] + ri[i+1],ri[i] + li[i-1])
ans = min(ans,val)
print(ans)
else:
print(0)
| 39
| 483
| 19,558,400
|
30470965
|
n = int(input())
a = list(map(int, input().split()))
lp,rp = [0 for i in range(n)],[0 for i in range(n)]
lnr, rnr = [a[i] for i in range(n)],[a[i] for i in range(n)]
mx = a[0]
for i in range(1,n):
if a[i] > mx:
mx = a[i]
lp[i] = lp[i-1]
else:
mx += 1
lp[i] = lp[i-1] + mx - a[i]
lnr[i] = mx
mx = a[-1]
for i in range(n-2,-1,-1):
if a[i] > mx:
mx = a[i]
rp[i] = rp[i+1]
else:
mx += 1
rp[i] = rp[i+1] + mx - a[i]
rnr[i] = mx
ans = min(rp[0], lp[-1])
for i in range(1,n-1):
ca = lp[i-1] + rp[i+1]
if max(lnr[i-1], rnr[i+1]) + 1 > a[i]:
ca += max(lnr[i-1], rnr[i+1]) + 1 - a[i]
ans = min(ans, ca)
print(ans)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
PyPy 3
|
TESTS
| 8
| 93
| 0
|
116885031
|
n = int(input())
arr = list(map(int, input().split()))
forward = arr[:]
cnt = arr[0] + 1
for i in range(1, n):
forward[i] = max(cnt, arr[i])
cnt = max(cnt, arr[i]) + 1
backward = arr[:]
cnt = arr[n - 1] + 1
for i in range(n - 2, -1,-1):
backward[i] = max(cnt, arr[i])
cnt = max(cnt, arr[i]) + 1
pref = [0 for i in range(n)]
suf = [0 for i in range(n)]
pref[0] = forward[0] - arr[0]
for i in range(1,n):
pref[i] = pref[i - 1] + (forward[i] - arr[i])
suf[-1] = backward[-1] - arr[-1]
for i in range(n - 2, -1,-1):
suf[i] = suf[i + 1] + (backward[i] - arr[i])
ans = min(pref[n - 1], suf[0])
for i in range(n - 1):
ans = min(ans,pref[i] + suf[i + 1])
print(ans)
| 39
| 592
| 15,360,000
|
43116711
|
n = int(input())
arr = list(map(int, input().split()))
starts = [0 for _ in range(n)]
ends = [0 for _ in range(n)]
starts[0] = arr[0]
ends[-1] = arr[-1]
for i in range(1, n):
starts[i] = max(arr[i], starts[i - 1] + 1)
ends[-i - 1] = max(arr[-i - 1], ends[-i] + 1)
sts = starts[:]
eds = ends[:]
for i in range(n):
starts[i] -= arr[i]
ends[-i - 1] -= arr[-i - 1]
for i in range(1, n):
starts[i] += starts[i - 1]
ends[-i - 1] += ends[-i]
bst = 10**30
for i in range(n):
score = max(sts[i], eds[i]) - arr[i]
if i > 0:
score += starts[i - 1]
if i < n - 1:
score += ends[i + 1]
bst = min(bst, score)
print(bst)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
774/D
|
774
|
D
|
Python 3
|
TESTS
| 17
| 124
| 9,625,600
|
174840898
|
n, l, r = list(map(int, input().rstrip().split()))
a = list(map(int, input().rstrip().split()))
b = list(map(int, input().rstrip().split()))
a1=a[l-1:r]
a1.sort()
b1=b[l-1:r]
b1.sort()
print("TRUTH" if a1==b1 else "LIE")
| 52
| 93
| 15,155,200
|
26154633
|
N, l, r = input().split(' ')
N = int(N)
l = int(l)
r = int(r)
A = input().split(' ')
B = input().split(' ')
truth = True
for i in range(0,l-1,1):
if A[i] != B[i]:
truth = False
for i in range(N-1,r-1,-1):
if A[i] != B[i]:
truth = False
if truth == True:
print("TRUTH")
else:
print("LIE")
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 2
| 256
|
Lie or Truth
|
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
|
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
|
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
| null |
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
|
[{"input": "5 2 4\n3 4 2 3 1\n3 2 3 4 1", "output": "TRUTH"}, {"input": "3 1 2\n1 2 3\n3 1 2", "output": "LIE"}, {"input": "4 2 4\n1 1 1 1\n1 1 1 1", "output": "TRUTH"}]
| 1,500
|
["*special", "constructive algorithms", "implementation", "sortings"]
| 52
|
[{"input": "5 2 4\r\n3 4 2 3 1\r\n3 2 3 4 1\r\n", "output": "TRUTH\r\n"}, {"input": "3 1 2\r\n1 2 3\r\n3 1 2\r\n", "output": "LIE\r\n"}, {"input": "4 2 4\r\n1 1 1 1\r\n1 1 1 1\r\n", "output": "TRUTH\r\n"}, {"input": "5 1 3\r\n2 2 2 1 2\r\n2 2 2 1 2\r\n", "output": "TRUTH\r\n"}, {"input": "7 1 4\r\n2 5 5 5 4 3 4\r\n2 5 5 5 4 3 4\r\n", "output": "TRUTH\r\n"}, {"input": "10 1 10\r\n6 7 6 1 10 10 9 5 3 9\r\n7 10 9 6 1 5 9 3 10 6\r\n", "output": "TRUTH\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "TRUTH\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n2 1 3 4\r\n", "output": "LIE\r\n"}, {"input": "7 2 4\r\n1 2 3 4 5 7 6\r\n1 2 3 4 5 6 7\r\n", "output": "LIE\r\n"}, {"input": "5 1 2\r\n1 2 3 4 5\r\n1 2 3 5 4\r\n", "output": "LIE\r\n"}, {"input": "8 3 6\r\n5 3 1 1 1 1 3 5\r\n3 3 1 1 1 1 5 5\r\n", "output": "LIE\r\n"}, {"input": "4 2 2\r\n2 1 2 2\r\n1 2 2 2\r\n", "output": "LIE\r\n"}]
| false
|
stdio
| null | true
|
292/A
|
292
|
A
|
Python 3
|
TESTS
| 0
| 62
| 0
|
168977239
|
n = eval(input())
cnt = 0
lt = 0
mn = 0
for i in range(n):
t, c = input().split()
t, c = int(t), int(c)
n -= t - lt
lt = t
if n < 0:
n = 0
n += c
if n > mn:
mn = n
print(n + t, mn)
| 38
| 92
| 0
|
4440878
|
n = int(input())
mt = 0
mq = 0
t = 0
q = 0
for i in range(n):
(ti, ci) = map(int, input().split())
q = max(0, q-ti+t)
t = ti
q += ci
mq = max(mq, q)
mt = t+q
print(mt, mq)
|
Croc Champ 2013 - Round 1
|
CF
| 2,013
| 2
| 256
|
SMSC
|
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC.
For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly.
Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows:
1. If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x.
2. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue.
Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.
|
The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly.
It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).
|
In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.
| null |
In the first test sample:
- second 1: the first message has appeared in the queue, the queue's size is 1;
- second 2: the first message is sent, the second message has been received, the queue's size is 1;
- second 3: the second message is sent, the queue's size is 0,
Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
|
[{"input": "2\n1 1\n2 1", "output": "3 1"}, {"input": "1\n1000000 10", "output": "1000010 10"}, {"input": "3\n3 3\n4 3\n5 3", "output": "12 7"}]
| 1,100
|
["implementation"]
| 38
|
[{"input": "2\r\n1 1\r\n2 1\r\n", "output": "3 1\r\n"}, {"input": "1\r\n1000000 10\r\n", "output": "1000010 10\r\n"}, {"input": "3\r\n3 3\r\n4 3\r\n5 3\r\n", "output": "12 7\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2 1\r\n"}, {"input": "2\r\n1 11\r\n100 10\r\n", "output": "110 11\r\n"}, {"input": "4\r\n1 10\r\n2 9\r\n3 8\r\n40 3\r\n", "output": "43 25\r\n"}, {"input": "5\r\n2 1\r\n5 2\r\n6 1\r\n7 1\r\n8 1\r\n", "output": "10 2\r\n"}, {"input": "4\r\n10 1000\r\n99998 20\r\n99999 10\r\n1000000 100\r\n", "output": "1000100 1000\r\n"}, {"input": "6\r\n10 10\r\n100 500\r\n200 500\r\n500 1\r\n999995 4\r\n999996 15\r\n", "output": "1000014 900\r\n"}, {"input": "10\r\n1 5\r\n2 5\r\n3 10\r\n4 8\r\n5 5\r\n6 4\r\n7 8\r\n8 9\r\n9 2\r\n10 10\r\n", "output": "67 57\r\n"}, {"input": "10\r\n26 4\r\n85 97\r\n86 62\r\n87 74\r\n92 8\r\n93 81\r\n97 12\r\n98 25\r\n99 31\r\n100 3\r\n", "output": "478 378\r\n"}, {"input": "10\r\n964416 3980\r\n987048 334\r\n999576 6922\r\n999684 2385\r\n999896 6558\r\n999948 3515\r\n999966 1517\r\n999984 2233\r\n999988 7242\r\n999994 91\r\n", "output": "1030039 30045\r\n"}, {"input": "5\r\n987640 52\r\n994481 69\r\n995526 50\r\n996631 75\r\n999763 22\r\n", "output": "999785 75\r\n"}, {"input": "23\r\n5 1045\r\n12 703\r\n16 26\r\n23 3384\r\n28 4563\r\n30 4501\r\n34 1033\r\n35 1393\r\n36 4095\r\n37 1279\r\n38 1787\r\n39 770\r\n40 5362\r\n41 4569\r\n42 3148\r\n43 2619\r\n44 5409\r\n45 3919\r\n46 732\r\n47 1297\r\n48 4512\r\n49 3231\r\n50 5169\r\n", "output": "64551 64501\r\n"}]
| false
|
stdio
| null | true
|
103/A
|
103
|
A
|
PyPy 3
|
TESTS
| 4
| 248
| 20,172,800
|
86968182
|
n = int(input())
t= list(map(int,input().split()))
p=t[0]
if n>1:
for j in range(1,n):
p+=1+(t[j]-1)*2
print(p)
| 25
| 92
| 0
|
139923210
|
n=int(input())
l=list(map(int,input().split()))
ans=0
for i in range(n):
ans+=l[i]+ i*(l[i]-1)
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
|
103/A
|
103
|
A
|
PyPy 3
|
TESTS
| 4
| 248
| 20,172,800
|
87017308
|
n=int(input())
a=list(map(int,input().split()))
s=a[0]
for i in range(1,len(a)):
s=s+(a[i]-1)+a[i]
print(s)
| 25
| 92
| 0
|
142539758
|
r=input;r();print(sum(int(v)*i-i+1for i,v in enumerate(r().split(),1)))
|
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
|
965/D
|
965
|
D
|
PyPy 3
|
TESTS
| 8
| 155
| 10,444,800
|
85766576
|
# D - Single-use Stones
w,l = [int(i) for i in input().split(' ')]
a = [1E6]*(w+1)
entrada = [int(i) for i in input().split(' ')]
for i in range (1, w):
a[i] = entrada[i-1]
aux = [0]*len(a)
num = 0
for j in range (1, w+1):
if(a[j] <= 0):
continue
if(j <= l):
aux[j] = a[j]
continue
d = j
for k in range(j - l, d):
if(aux[k] < (a[j] - aux[j])):
num = aux[k]
else:
num = a[j] - aux[j]
aux[k] = aux[k] - num
aux[j] = aux[j] + num
if(aux[j]>= a[j]):
break
print(aux[-1])
# print (w)
# print (l)
# print (a)
#############CPP################
#include <cstdio>
#include <vector>
# int main(){
# long w, l; scanf("%ld %ld", &w, &l);
# std::vector<long> a(w + 1, 2e9);
# for(long p = 1; p < w; p++){scanf("%ld", &a[p]);}
# std::vector<long> b(w + 1, 0);
# for(long p = 1; p <= w; p++){
# if(a[p] <= 0){continue;}
# if(p <= l){b[p] = a[p]; continue;}
# for(long u = p - l; u < p; u++){
# long num = (b[u] < (a[p] - b[p])) ? b[u] : (a[p] - b[p]);
# b[u] -= mv; b[p] += mv;
# if(b[p] >= a[p]){break;}
# }
# }
# printf("%ld\n", b.back());
# return 0;
# }
| 16
| 77
| 13,619,200
|
211191021
|
# Lê a largura w e o comprimento mínimo l
w, l = map(int, input().split())
# Lê os valores das pedras disponíveis
a = list(map(int, input().split()))
s = [0] # Inicializa uma lista s com o valor inicial 0
for i in a:
# Calcula as somas cumulativas
s.append(s[-1] + i)
# Cada elemento de s é a soma dos valores das pedras desde
# o início até a posição atual
# Inicializa a variável de resposta com um valor muito grande
inf = pow(10, 9) + 1
ans = inf
# Loop que percorre as possíveis posições de início da subsequência
# dentro da sequência original
for i in range(l, w):
dif = s[i] - s[i - l] # Diferença entre as somas cumulativas s(i) e s(i-l)
# Atualiza a resposta com o menor entre o atual e a nova diferença
ans = min(ans, dif)
# Imprime a resposta final, que é o valor mínimo encontrado para
# a soma da subsequência desejada
print(ans)
|
Codeforces Round 476 (Div. 2) [Thanks, Telegram!]
|
CF
| 2,018
| 1
| 256
|
Single-use Stones
|
A lot of frogs want to cross a river. A river is $$$w$$$ units width, but frogs can only jump $$$l$$$ units long, where $$$l < w$$$. Frogs can also jump on lengths shorter than $$$l$$$. but can't jump longer. Hopefully, there are some stones in the river to help them.
The stones are located at integer distances from the banks. There are $$$a_i$$$ stones at the distance of $$$i$$$ units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water.
What is the maximum number of frogs that can cross the river, given that then can only jump on the stones?
|
The first line contains two integers $$$w$$$ and $$$l$$$ ($$$1 \le l < w \le 10^5$$$) — the width of the river and the maximum length of a frog's jump.
The second line contains $$$w - 1$$$ integers $$$a_1, a_2, \ldots, a_{w-1}$$$ ($$$0 \le a_i \le 10^4$$$), where $$$a_i$$$ is the number of stones at the distance $$$i$$$ from the bank the frogs are currently at.
|
Print a single integer — the maximum number of frogs that can cross the river.
| null |
In the first sample two frogs can use the different stones at the distance $$$5$$$, and one frog can use the stones at the distances $$$3$$$ and then $$$8$$$.
In the second sample although there are two stones at the distance $$$5$$$, that does not help. The three paths are: $$$0 \to 3 \to 6 \to 9 \to 10$$$, $$$0 \to 2 \to 5 \to 8 \to 10$$$, $$$0 \to 1 \to 4 \to 7 \to 10$$$.
|
[{"input": "10 5\n0 0 1 0 2 0 0 1 0", "output": "3"}, {"input": "10 3\n1 1 1 1 2 1 1 1 1", "output": "3"}]
| 1,900
|
["binary search", "flows", "greedy", "two pointers"]
| 16
|
[{"input": "10 5\r\n0 0 1 0 2 0 0 1 0\r\n", "output": "3\r\n"}, {"input": "10 3\r\n1 1 1 1 2 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "2 1\r\n0\r\n", "output": "0\r\n"}, {"input": "2 1\r\n5\r\n", "output": "5\r\n"}, {"input": "10 4\r\n0 0 6 2 7 1 6 4 0\r\n", "output": "8\r\n"}, {"input": "100 15\r\n0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 0\r\n", "output": "5\r\n"}, {"input": "10 4\r\n10 10 10 10 10 10 10 10 10\r\n", "output": "40\r\n"}, {"input": "100 34\r\n16 0 10 11 12 13 0 5 4 14 6 15 4 9 1 20 19 14 1 7 14 11 10 20 6 9 12 8 3 19 20 4 17 17 8 11 14 18 5 20 17 0 3 18 14 12 11 12 5 5 11 7 9 17 4 8 4 10 0 0 12 9 15 3 15 14 19 12 6 8 17 19 4 18 19 3 8 3 9 1 6 15 4 16 1 18 13 16 3 5 20 11 10 9 9 17 20 15 12\r\n", "output": "312\r\n"}]
| false
|
stdio
| null | true
|
774/D
|
774
|
D
|
PyPy 3-64
|
TESTS
| 17
| 92
| 20,275,200
|
183861208
|
from sys import stdin
n,l,r = map(int, stdin.readline().rstrip().split(' '))
org = list(map(int, stdin.readline().rstrip().split(' ')))
swapped = list(map(int, stdin.readline().rstrip().split(' ')))
s = org[l-1]
for i in range(l, r):
s ^= org[i]
swapped_s = swapped[l-1]
for i in range(l,r):
swapped_s ^= swapped[i]
if s == swapped_s:
print('TRUTH')
else:
print('LIE')
| 52
| 93
| 19,763,200
|
232571022
|
def solve():
line1=input()
line2=input()
line3=input()
n,l,r=[int(x) for x in line1.split()]
before=[int(x) for x in line2.split()]
after=[int(x) for x in line3.split()]
if before[:l - 1] == after[:l - 1] and before[r:] == after[r:]:
print("TRUTH")
else:
print("LIE")
# main
if __name__ == "__main__":
solve()
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 2
| 256
|
Lie or Truth
|
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
|
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
|
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
| null |
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
|
[{"input": "5 2 4\n3 4 2 3 1\n3 2 3 4 1", "output": "TRUTH"}, {"input": "3 1 2\n1 2 3\n3 1 2", "output": "LIE"}, {"input": "4 2 4\n1 1 1 1\n1 1 1 1", "output": "TRUTH"}]
| 1,500
|
["*special", "constructive algorithms", "implementation", "sortings"]
| 52
|
[{"input": "5 2 4\r\n3 4 2 3 1\r\n3 2 3 4 1\r\n", "output": "TRUTH\r\n"}, {"input": "3 1 2\r\n1 2 3\r\n3 1 2\r\n", "output": "LIE\r\n"}, {"input": "4 2 4\r\n1 1 1 1\r\n1 1 1 1\r\n", "output": "TRUTH\r\n"}, {"input": "5 1 3\r\n2 2 2 1 2\r\n2 2 2 1 2\r\n", "output": "TRUTH\r\n"}, {"input": "7 1 4\r\n2 5 5 5 4 3 4\r\n2 5 5 5 4 3 4\r\n", "output": "TRUTH\r\n"}, {"input": "10 1 10\r\n6 7 6 1 10 10 9 5 3 9\r\n7 10 9 6 1 5 9 3 10 6\r\n", "output": "TRUTH\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "TRUTH\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n2 1 3 4\r\n", "output": "LIE\r\n"}, {"input": "7 2 4\r\n1 2 3 4 5 7 6\r\n1 2 3 4 5 6 7\r\n", "output": "LIE\r\n"}, {"input": "5 1 2\r\n1 2 3 4 5\r\n1 2 3 5 4\r\n", "output": "LIE\r\n"}, {"input": "8 3 6\r\n5 3 1 1 1 1 3 5\r\n3 3 1 1 1 1 5 5\r\n", "output": "LIE\r\n"}, {"input": "4 2 2\r\n2 1 2 2\r\n1 2 2 2\r\n", "output": "LIE\r\n"}]
| false
|
stdio
| null | true
|
713/A
|
713
|
A
|
PyPy 3
|
TESTS
| 25
| 530
| 11,059,200
|
92339531
|
from sys import stdout,stdin
d={}
for _ in range(int(input())):
t=stdin.readline()
t=str(t)
s=t[0]
n=t[2:]
b="0"*(18-len(n))
for i in n:
if i!="\n":
if int(i)%2==0:
b+="0"
else:
b+="1"
if s=="+":
if b in d:
d[b]+=1
else:
d[b]=1
elif s=="-":
d[b]-=1
else:
if b in d:
stdout.write(str(d[b]))
print()
else:
print(0)
| 37
| 233
| 23,449,600
|
167818193
|
import sys
from collections import defaultdict
input = sys.stdin.readline
n = int(input())
d = defaultdict(int)
for i in range(n):
s = input().split()
if(s[0] == '?'):
a = '0' * (18 - len(s[1])) + s[1]
print(d[a])
else:
a = ''
m = int(s[1])
while(m > 0):
c = m % 10
if(c % 2):
a += '1'
else:
a += '0'
m //= 10
a = a + '0' * (18 - len(s[1]))
a = a[ :: -1]
if(s[0] == '+'):
d[a] += 1
else:
d[a] -= 1
|
Codeforces Round 371 (Div. 1)
|
CF
| 2,016
| 1
| 256
|
Sonya and Queries
|
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type:
1. + ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.
2. - ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset.
3. ? s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.
For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not.
|
The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform.
Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.
It's guaranteed that there will be at least one query of type '?'.
It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.
|
For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.
| null |
Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.
1. 1 and 241.
2. 361.
3. 101 and 361.
4. 361.
5. 4000.
|
[{"input": "12\n+ 1\n+ 241\n? 1\n+ 361\n- 241\n? 0101\n+ 101\n? 101\n- 101\n? 101\n+ 4000\n? 0", "output": "2\n1\n2\n1\n1"}, {"input": "4\n+ 200\n+ 200\n- 200\n? 0", "output": "1"}]
| 1,400
|
["data structures", "implementation"]
| 37
|
[{"input": "12\r\n+ 1\r\n+ 241\r\n? 1\r\n+ 361\r\n- 241\r\n? 0101\r\n+ 101\r\n? 101\r\n- 101\r\n? 101\r\n+ 4000\r\n? 0\r\n", "output": "2\r\n1\r\n2\r\n1\r\n1\r\n"}, {"input": "4\r\n+ 200\r\n+ 200\r\n- 200\r\n? 0\r\n", "output": "1\r\n"}, {"input": "20\r\n+ 61\r\n+ 99\r\n+ 51\r\n+ 70\r\n+ 7\r\n+ 34\r\n+ 71\r\n+ 86\r\n+ 68\r\n+ 39\r\n+ 78\r\n+ 81\r\n+ 89\r\n? 10\r\n? 00\r\n? 10\r\n? 01\r\n? 01\r\n? 00\r\n? 00\r\n", "output": "3\r\n2\r\n3\r\n4\r\n4\r\n2\r\n2\r\n"}, {"input": "20\r\n+ 13\r\n+ 50\r\n+ 9\r\n? 0\r\n+ 24\r\n? 0\r\n- 24\r\n? 0\r\n+ 79\r\n? 11\r\n- 13\r\n? 11\r\n- 50\r\n? 10\r\n? 1\r\n- 9\r\n? 1\r\n? 11\r\n- 79\r\n? 11\r\n", "output": "0\r\n1\r\n0\r\n2\r\n1\r\n0\r\n1\r\n0\r\n1\r\n0\r\n"}, {"input": "10\r\n+ 870566619432760298\r\n+ 869797178280285214\r\n+ 609920823721618090\r\n+ 221159591436767023\r\n+ 730599542279836538\r\n? 101001100111001011\r\n? 001111010101010011\r\n? 100010100011101110\r\n? 100110010110001100\r\n? 110000011101110011\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "10\r\n+ 96135\r\n? 10111\r\n+ 63322\r\n? 10111\r\n+ 44490\r\n? 10111\r\n+ 69312\r\n? 10111\r\n? 01100\r\n+ 59396\r\n", "output": "1\r\n1\r\n1\r\n1\r\n1\r\n"}, {"input": "10\r\n+ 2\r\n- 2\r\n+ 778\r\n+ 3\r\n+ 4\r\n- 4\r\n+ 1\r\n+ 617\r\n? 011\r\n? 011\r\n", "output": "1\r\n1\r\n"}, {"input": "20\r\n+ 8\r\n+ 39532\r\n+ 813\r\n- 39532\r\n? 00011\r\n? 00000\r\n? 00011\r\n+ 70424\r\n- 8\r\n? 00011\r\n- 70424\r\n? 00011\r\n+ 29\r\n? 00001\r\n+ 6632\r\n+ 3319\r\n? 00001\r\n+ 3172\r\n? 01111\r\n- 29\r\n", "output": "1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n"}]
| false
|
stdio
| null | true
|
545/B
|
545
|
B
|
PyPy 3-64
|
TESTS
| 2
| 30
| 1,945,600
|
208637078
|
def solve(s1,s2):
n = len(s1)
c = 0
for i in range(n):
if s1[i]!=s2[i]:
c+=1
if c%2==1:
return('impossible')
else:
r = ""
idx = 0
while c>0:
if s1[idx]=='0':
r+='1'
else:
r+='0'
c -= 2
r = r + s1[len(r):]
return r
s1 = input()
s2 = input()
print(solve(s1,s2))
| 54
| 62
| 307,200
|
201770920
|
def girl(s, t):
new_str = ""
chet = True
for i in range(len(s)):
if s[i] != t[i]:
if chet:
new_str += s[i]
chet = False
else:
new_str += t[i]
chet = True
else: new_str += s[i]
if chet: print(new_str)
else: print("impossible")
s = input()
t = input()
girl(s, t)
|
Codeforces Round 303 (Div. 2)
|
CF
| 2,015
| 1
| 256
|
Equidistant String
|
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti.
As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.
It's time for Susie to go to bed, help her find such string p or state that it is impossible.
|
The first line contains string s of length n.
The second line contains string t of length n.
The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.
|
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
| null |
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
|
[{"input": "0001\n1011", "output": "0011"}, {"input": "000\n111", "output": "impossible"}]
| 1,100
|
["greedy"]
| 54
|
[{"input": "0001\r\n1011\r\n", "output": "0011\r\n"}, {"input": "000\r\n111\r\n", "output": "impossible\r\n"}, {"input": "1010101011111110111111001111111111111111111111101101110111111111111110110110101011111110110111111101\r\n0101111111000100010100001100010101000000011000000000011011000001100100001110111011111000001110011111\r\n", "output": "1111101111101100110110001110110111010101011101001001010011101011101100100110111011111100100110111111\r\n"}, {"input": "0000000001000000000000100000100001000000\r\n1111111011111101111111111111111111111111\r\n", "output": "0101010011010100101010110101101011010101\r\n"}, {"input": "10101000101001001101010010000101100011010011000011001001001111110010100110000001111111\r\n01001011110111111101111011011111110000000111111001000011010101001010000111101010000101\r\n", "output": "11101010111101101101110011001101110010010111010001001011000111011010100111001000101101\r\n"}, {"input": "1111111111111111111111111110111111111111111111111111111111111110111111101111111111111111111111111111\r\n1111111111111111111001111111110010111111111111111111001111111111111111111111111111111111111111111111\r\n", "output": "1111111111111111111101111110110110111111111111111111101111111110111111111111111111111111111111111111\r\n"}, {"input": "0000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111\r\n1111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0101010101010101010101000000101010101010101010101010101010101010101010101010101010101010101010101010\r\n"}, {"input": "00001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "01011110101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n"}, {"input": "0\r\n0\r\n", "output": "0\r\n"}, {"input": "0\r\n1\r\n", "output": "impossible\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n0\r\n", "output": "impossible\r\n"}, {"input": "1111\r\n0000\r\n", "output": "1010\r\n"}, {"input": "1111\r\n1001\r\n", "output": "1101\r\n"}, {"input": "0000\r\n1111\r\n", "output": "0101\r\n"}, {"input": "1010\r\n0101\r\n", "output": "1111\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
with open(input_path, 'r') as f:
s = f.readline().strip()
t = f.readline().strip()
n = len(s)
assert len(t) == n
with open(submission_path, 'r') as f:
submission_lines = f.readlines()
if len(submission_lines) == 0:
submission = ''
elif len(submission_lines) == 1:
submission = submission_lines[0].rstrip('\n')
else:
print(0)
return
if submission == 'impossible':
d = sum(c1 != c2 for c1, c2 in zip(s, t))
if d % 2 == 1:
print(1)
else:
print(0)
return
else:
if len(submission) != n:
print(0)
return
if any(c not in {'0', '1'} for c in submission):
print(0)
return
d_s = sum(c != pc for c, pc in zip(s, submission))
d_t = sum(c != pc for c, pc in zip(t, submission))
if d_s == d_t:
print(1)
else:
print(0)
return
if __name__ == '__main__':
main()
| true
|
545/B
|
545
|
B
|
Python 3
|
TESTS
| 2
| 61
| 307,200
|
208637591
|
def solve(s1,s2):
n = len(s1)
c = 0
for i in range(n):
if s1[i]!=s2[i]:
c+=1
if c%2==1:
return('impossible')
else:
return s1[:c] + s2[c:]
def calc_distance(s1,s2):
c = 0
n = len(s1)
for i in range(n):
if s1[i]!=s2[i]:
c+=1
return c
s1 = input()
s2 = input()
if len(s1) > 100:
print(calc_distance(s1,s2))
nn = solve(s1,s2)
print(calc_distance(s1,nn))
print(calc_distance(nn,s2))
nn = solve(s1,s2)
print(nn)
| 54
| 77
| 10,854,400
|
208637374
|
# Enter your code here. Read input from STDIN. Print output to STDOUT
import math
if __name__ == '__main__':
a = input().strip()
b = input().strip()
c = []
swap = False
sc = 0
for x, y in zip(a, b):
if x == y:
c.append(x)
else:
if swap:
c.append(y)
else:
c.append(x)
swap = not swap
sc += 1
if sc % 2 == 0:
print("".join(c))
else:
print("impossible")
|
Codeforces Round 303 (Div. 2)
|
CF
| 2,015
| 1
| 256
|
Equidistant String
|
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti.
As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.
It's time for Susie to go to bed, help her find such string p or state that it is impossible.
|
The first line contains string s of length n.
The second line contains string t of length n.
The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.
|
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
| null |
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
|
[{"input": "0001\n1011", "output": "0011"}, {"input": "000\n111", "output": "impossible"}]
| 1,100
|
["greedy"]
| 54
|
[{"input": "0001\r\n1011\r\n", "output": "0011\r\n"}, {"input": "000\r\n111\r\n", "output": "impossible\r\n"}, {"input": "1010101011111110111111001111111111111111111111101101110111111111111110110110101011111110110111111101\r\n0101111111000100010100001100010101000000011000000000011011000001100100001110111011111000001110011111\r\n", "output": "1111101111101100110110001110110111010101011101001001010011101011101100100110111011111100100110111111\r\n"}, {"input": "0000000001000000000000100000100001000000\r\n1111111011111101111111111111111111111111\r\n", "output": "0101010011010100101010110101101011010101\r\n"}, {"input": "10101000101001001101010010000101100011010011000011001001001111110010100110000001111111\r\n01001011110111111101111011011111110000000111111001000011010101001010000111101010000101\r\n", "output": "11101010111101101101110011001101110010010111010001001011000111011010100111001000101101\r\n"}, {"input": "1111111111111111111111111110111111111111111111111111111111111110111111101111111111111111111111111111\r\n1111111111111111111001111111110010111111111111111111001111111111111111111111111111111111111111111111\r\n", "output": "1111111111111111111101111110110110111111111111111111101111111110111111111111111111111111111111111111\r\n"}, {"input": "0000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111\r\n1111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0101010101010101010101000000101010101010101010101010101010101010101010101010101010101010101010101010\r\n"}, {"input": "00001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "01011110101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n"}, {"input": "0\r\n0\r\n", "output": "0\r\n"}, {"input": "0\r\n1\r\n", "output": "impossible\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n0\r\n", "output": "impossible\r\n"}, {"input": "1111\r\n0000\r\n", "output": "1010\r\n"}, {"input": "1111\r\n1001\r\n", "output": "1101\r\n"}, {"input": "0000\r\n1111\r\n", "output": "0101\r\n"}, {"input": "1010\r\n0101\r\n", "output": "1111\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
with open(input_path, 'r') as f:
s = f.readline().strip()
t = f.readline().strip()
n = len(s)
assert len(t) == n
with open(submission_path, 'r') as f:
submission_lines = f.readlines()
if len(submission_lines) == 0:
submission = ''
elif len(submission_lines) == 1:
submission = submission_lines[0].rstrip('\n')
else:
print(0)
return
if submission == 'impossible':
d = sum(c1 != c2 for c1, c2 in zip(s, t))
if d % 2 == 1:
print(1)
else:
print(0)
return
else:
if len(submission) != n:
print(0)
return
if any(c not in {'0', '1'} for c in submission):
print(0)
return
d_s = sum(c != pc for c, pc in zip(s, submission))
d_t = sum(c != pc for c, pc in zip(t, submission))
if d_s == d_t:
print(1)
else:
print(0)
return
if __name__ == '__main__':
main()
| true
|
175/C
|
175
|
C
|
PyPy 3
|
TESTS
| 2
| 216
| 1,843,200
|
109802617
|
import sys
import math
from heapq import *;
input = sys.stdin.readline
from functools import cmp_to_key;
def pi():
return(int(input()))
def pl():
return(int(input(), 16))
def ti():
return(list(map(int,input().split())))
def ts():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
mod = 1000000007;
f = [];
def fact(n,m):
global f;
f = [1 for i in range(n+1)];
f[0] = 1;
for i in range(1,n+1):
f[i] = (f[i-1]*i)%m;
def fast_mod_exp(a,b,m):
res = 1;
while b > 0:
if b & 1:
res = (res*a)%m;
a = (a*a)%m;
b = b >> 1;
return res;
def inverseMod(n,m):
return fast_mod_exp(n,m-2,m);
def ncr(n,r,m):
if n < 0 or r < 0 or r > n: return 0;
if r == 0: return 1;
return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m;
def main():
C();
def cmp(a,b):
if a[1] > b[1]: return -1;
if a[1] < b[1]: return 1;
return 0;
def C():
n = pi();
k,c = [],[];
for i in range(n):
[x,y] = ti();
k.append(x);
c.append(y);
t,p = pi(),ti();
kc = [[] for i in range(n)];
for i in range(n):
kc[i] = [k[i],c[i]];
kc.sort(key=cmp_to_key(cmp));
i,j = 0,0;
d = p[0];
ans = 0;
while j < n:
if kc[j][0] < d:
ans += kc[j][0]*(i+1)*kc[j][1];
d -= kc[j][0];
j += 1;
else:
ans += d*(i+1)*kc[j][1];
kc[j][0] -= d;
i += 1;
if i < t:
d = p[i]-p[i-1];
print(ans);
main();
| 90
| 310
| 0
|
73499042
|
n = int(input())
fig = [tuple(map(int, input().split()))[::-1] for _ in range(n)]
fig.sort()
t = int(input())
a = list(map(int, input().split()))
res, curr = 0, 0
i, j = 0, 0
while i < n:
if j < t and curr + fig[i][1] >= a[j]:
take = a[j] - curr
curr += take
fig[i] = (fig[i][0], fig[i][1] - take)
res += take * (j + 1) * fig[i][0]
j += 1
else:
take = fig[i][1]
curr += take
res += take * (j + 1) * fig[i][0]
i += 1
print(res)
|
Codeforces Round 115
|
CF
| 2,012
| 2
| 256
|
Geometry Horse
|
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ci·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 ≤ i ≤ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.
Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
|
The first line contains the only integer number n (1 ≤ n ≤ 100) — the number of figure types.
Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000), separated with space — the number of figures of the i-th type and the cost of one i-type figure, correspondingly.
The next line contains the only integer number t (1 ≤ t ≤ 100) — the number that describe the factor's changes.
The next line contains t integer numbers pi (1 ≤ p1 < p2 < ... < pt ≤ 1012), separated with spaces.
Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
|
Print the only number — the maximum number of points Vasya can get.
| null |
In the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points.
In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8 + 5·10)·1 = 74 points.
|
[{"input": "1\n5 10\n2\n3 6", "output": "70"}, {"input": "2\n3 8\n5 10\n1\n20", "output": "74"}]
| 1,600
|
["greedy", "implementation", "sortings", "two pointers"]
| 90
|
[{"input": "1\r\n5 10\r\n2\r\n3 6\r\n", "output": "70"}, {"input": "2\r\n3 8\r\n5 10\r\n1\r\n20\r\n", "output": "74"}, {"input": "3\r\n10 3\r\n20 2\r\n30 1\r\n3\r\n30 50 60\r\n", "output": "200"}, {"input": "1\r\n100 1000\r\n1\r\n1\r\n", "output": "199000"}, {"input": "1\r\n1 1000\r\n1\r\n1\r\n", "output": "1000"}, {"input": "1\r\n1 1000\r\n1\r\n2\r\n", "output": "1000"}, {"input": "2\r\n1000000000 1000\r\n1 1\r\n1\r\n10\r\n", "output": "1999999991001"}, {"input": "6\r\n5 9\r\n63 3\r\n30 4\r\n25 6\r\n48 2\r\n29 9\r\n8\r\n105 137 172 192 632 722 972 981\r\n", "output": "2251"}, {"input": "7\r\n9902 9\r\n5809 6\r\n2358 0\r\n6868 7\r\n9630 2\r\n8302 10\r\n9422 3\r\n4\r\n2148 4563 8488 9575\r\n", "output": "1481866"}, {"input": "9\r\n60129 6\r\n44235 10\r\n13131 8\r\n2012 2\r\n27536 4\r\n38950 6\r\n39080 2\r\n13892 3\r\n48709 0\r\n1\r\n23853\r\n", "output": "2751752"}, {"input": "10\r\n3466127 4\r\n3477072 1\r\n9690039 9\r\n9885165 6\r\n2559197 4\r\n3448456 3\r\n9169542 1\r\n6915866 2\r\n1702896 10\r\n8934261 5\r\n6\r\n3041416 5811699 5920083 8250213 8694306 8899250\r\n", "output": "1843409345"}, {"input": "4\r\n4059578 5\r\n20774712 1\r\n64867825 7\r\n5606945 8\r\n1\r\n337246111\r\n", "output": "540002937"}, {"input": "1\r\n555 100\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "605000"}, {"input": "1\r\n1 1\r\n1\r\n100000000000\r\n", "output": "1"}, {"input": "12\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n1000000000 11\r\n1000000000 12\r\n1\r\n10000000000\r\n", "output": "101000000000"}, {"input": "11\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n1000000000 11\r\n1\r\n10000000000\r\n", "output": "77000000000"}, {"input": "1\r\n10 10\r\n3\r\n1 2 3\r\n", "output": "340"}, {"input": "1\r\n1000000000 1000\r\n2\r\n3 6\r\n", "output": "2999999991000"}, {"input": "1\r\n100 100\r\n3\r\n3 6 9\r\n", "output": "38200"}, {"input": "1\r\n10 1\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "55"}, {"input": "1\r\n10 10\r\n5\r\n1 2 3 4 5\r\n", "output": "450"}, {"input": "10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n1\r\n1\r\n", "output": "1990"}, {"input": "1\r\n10 10\r\n2\r\n3 6\r\n", "output": "210"}, {"input": "10\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1\r\n1000000\r\n", "output": "10000000"}]
| false
|
stdio
| null | true
|
175/C
|
175
|
C
|
Python 3
|
TESTS
| 2
| 124
| 0
|
11767931
|
import sys
def solve():
n, = rv()
figures = list()
before = 0
for i in range(n):
number, cost, = rv()
figures.append((cost, number, before + 1, before + number))
before += number
figures.sort()
t, = rv()
p = [0] + list(map(int, input().split()))
res = 0
for i in range(1, t + 1):
for f in range(n):
left = max(figures[f][2], p[i - 1] + 1)
right = min(figures[f][3], p[i])
num = right - left + 1
res += num * i * figures[f][0]
# print(left, right, num, i * figures[f][0])
print(res)
def prt(l): return print(''.join(l))
def rv(): return map(int, input().split())
def rl(n): return [list(map(int, input().split())) for _ in range(n)]
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
solve()
| 90
| 310
| 8,908,800
|
167194152
|
from cmath import inf
from operator import itemgetter
from typing import *
from collections import deque, Counter, defaultdict
from bisect import *
import heapq, math
from functools import cmp_to_key, reduce
from itertools import groupby, islice, zip_longest
# from sortedcontainers import SortedSet, SortedList
def main():
n = int(input())
num, val = zip(*(map(int, input().split()) for _ in range(n)))
T = int(input())
p = list(map(int, input().split()))
tmp = sum(num)
while len(p) > 0 and p[-1] > tmp:
p.pop(-1)
p.append(tmp)
num, val = zip(*sorted(zip(num, val), key=itemgetter(1)))
count, ans, factor = 0, 0, 1
for x, y in zip(num, val):
while x != 0:
tmp = min(x, p[0] - count)
x -= tmp
count += tmp
ans += tmp * factor * y
if p[0] == count:
p.pop(0)
factor += 1
print(ans)
main()
|
Codeforces Round 115
|
CF
| 2,012
| 2
| 256
|
Geometry Horse
|
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ci·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 ≤ i ≤ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.
Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
|
The first line contains the only integer number n (1 ≤ n ≤ 100) — the number of figure types.
Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000), separated with space — the number of figures of the i-th type and the cost of one i-type figure, correspondingly.
The next line contains the only integer number t (1 ≤ t ≤ 100) — the number that describe the factor's changes.
The next line contains t integer numbers pi (1 ≤ p1 < p2 < ... < pt ≤ 1012), separated with spaces.
Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
|
Print the only number — the maximum number of points Vasya can get.
| null |
In the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points.
In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8 + 5·10)·1 = 74 points.
|
[{"input": "1\n5 10\n2\n3 6", "output": "70"}, {"input": "2\n3 8\n5 10\n1\n20", "output": "74"}]
| 1,600
|
["greedy", "implementation", "sortings", "two pointers"]
| 90
|
[{"input": "1\r\n5 10\r\n2\r\n3 6\r\n", "output": "70"}, {"input": "2\r\n3 8\r\n5 10\r\n1\r\n20\r\n", "output": "74"}, {"input": "3\r\n10 3\r\n20 2\r\n30 1\r\n3\r\n30 50 60\r\n", "output": "200"}, {"input": "1\r\n100 1000\r\n1\r\n1\r\n", "output": "199000"}, {"input": "1\r\n1 1000\r\n1\r\n1\r\n", "output": "1000"}, {"input": "1\r\n1 1000\r\n1\r\n2\r\n", "output": "1000"}, {"input": "2\r\n1000000000 1000\r\n1 1\r\n1\r\n10\r\n", "output": "1999999991001"}, {"input": "6\r\n5 9\r\n63 3\r\n30 4\r\n25 6\r\n48 2\r\n29 9\r\n8\r\n105 137 172 192 632 722 972 981\r\n", "output": "2251"}, {"input": "7\r\n9902 9\r\n5809 6\r\n2358 0\r\n6868 7\r\n9630 2\r\n8302 10\r\n9422 3\r\n4\r\n2148 4563 8488 9575\r\n", "output": "1481866"}, {"input": "9\r\n60129 6\r\n44235 10\r\n13131 8\r\n2012 2\r\n27536 4\r\n38950 6\r\n39080 2\r\n13892 3\r\n48709 0\r\n1\r\n23853\r\n", "output": "2751752"}, {"input": "10\r\n3466127 4\r\n3477072 1\r\n9690039 9\r\n9885165 6\r\n2559197 4\r\n3448456 3\r\n9169542 1\r\n6915866 2\r\n1702896 10\r\n8934261 5\r\n6\r\n3041416 5811699 5920083 8250213 8694306 8899250\r\n", "output": "1843409345"}, {"input": "4\r\n4059578 5\r\n20774712 1\r\n64867825 7\r\n5606945 8\r\n1\r\n337246111\r\n", "output": "540002937"}, {"input": "1\r\n555 100\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "605000"}, {"input": "1\r\n1 1\r\n1\r\n100000000000\r\n", "output": "1"}, {"input": "12\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n1000000000 11\r\n1000000000 12\r\n1\r\n10000000000\r\n", "output": "101000000000"}, {"input": "11\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n1000000000 11\r\n1\r\n10000000000\r\n", "output": "77000000000"}, {"input": "1\r\n10 10\r\n3\r\n1 2 3\r\n", "output": "340"}, {"input": "1\r\n1000000000 1000\r\n2\r\n3 6\r\n", "output": "2999999991000"}, {"input": "1\r\n100 100\r\n3\r\n3 6 9\r\n", "output": "38200"}, {"input": "1\r\n10 1\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "55"}, {"input": "1\r\n10 10\r\n5\r\n1 2 3 4 5\r\n", "output": "450"}, {"input": "10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n1\r\n1\r\n", "output": "1990"}, {"input": "1\r\n10 10\r\n2\r\n3 6\r\n", "output": "210"}, {"input": "10\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1\r\n1000000\r\n", "output": "10000000"}]
| false
|
stdio
| null | true
|
282/A
|
282
|
A
|
Python 3
|
TESTS
| 5
| 30
| 0
|
227798035
|
n = int(input())
l = []
z = 0
while z < n:
y = input("")
l.append(y)
z += 1
x = 0
x += l.count("++X") or l.count("X++")
x -= l.count("--X") or l.count("X--")
print(x)
| 36
| 31
| 0
|
227665766
|
t=int(input())
k=0
for i in range(t):
s=input()
if s[1]=='+':
k+=1
else:
k-=1
print(k)
|
Codeforces Round 173 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Bit++
|
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.
The language is that peculiar as it has exactly one variable, called x. Also, there are two operations:
- Operation ++ increases the value of variable x by 1.
- Operation -- decreases the value of variable x by 1.
A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.
A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.
You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).
|
The first line contains a single integer n (1 ≤ n ≤ 150) — the number of statements in the programme.
Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter «X»). Thus, there are no empty statements. The operation and the variable can be written in any order.
|
Print a single integer — the final value of x.
| null | null |
[{"input": "1\n++X", "output": "1"}, {"input": "2\nX++\n--X", "output": "0"}]
| 800
|
["implementation"]
| 36
|
[{"input": "1\r\n++X\r\n", "output": "1\r\n"}, {"input": "2\r\nX++\r\n--X\r\n", "output": "0\r\n"}, {"input": "3\r\n++X\r\n++X\r\n++X\r\n", "output": "3\r\n"}, {"input": "2\r\n--X\r\n--X\r\n", "output": "-2\r\n"}, {"input": "5\r\n++X\r\n--X\r\n++X\r\n--X\r\n--X\r\n", "output": "-1\r\n"}, {"input": "28\r\nX--\r\n++X\r\nX++\r\nX++\r\nX++\r\n--X\r\n--X\r\nX++\r\nX--\r\n++X\r\nX++\r\n--X\r\nX--\r\nX++\r\nX--\r\n++X\r\n++X\r\nX++\r\nX++\r\nX++\r\nX++\r\n--X\r\n++X\r\n--X\r\n--X\r\n--X\r\n--X\r\nX++\r\n", "output": "4\r\n"}, {"input": "94\r\nX++\r\nX++\r\n++X\r\n++X\r\nX--\r\n--X\r\nX++\r\n--X\r\nX++\r\n++X\r\nX++\r\n++X\r\n--X\r\n--X\r\n++X\r\nX++\r\n--X\r\nX--\r\nX--\r\n--X\r\nX--\r\nX--\r\n--X\r\n++X\r\n--X\r\nX--\r\nX--\r\nX++\r\n++X\r\n--X\r\nX--\r\n++X\r\n--X\r\n--X\r\nX--\r\nX--\r\nX++\r\nX++\r\nX--\r\nX++\r\nX--\r\nX--\r\nX--\r\n--X\r\nX--\r\nX--\r\nX--\r\nX++\r\n++X\r\nX--\r\n++X\r\nX++\r\n--X\r\n--X\r\n--X\r\n--X\r\n++X\r\nX--\r\n--X\r\n--X\r\n++X\r\nX--\r\nX--\r\nX++\r\n++X\r\nX++\r\n++X\r\n--X\r\n--X\r\nX--\r\n++X\r\nX--\r\nX--\r\n++X\r\n++X\r\n++X\r\n++X\r\nX++\r\n++X\r\n--X\r\nX++\r\n--X\r\n--X\r\n++X\r\n--X\r\nX++\r\n++X\r\nX++\r\n--X\r\nX--\r\nX--\r\n--X\r\n++X\r\nX++\r\n", "output": "-10\r\n"}, {"input": "56\r\n--X\r\nX--\r\n--X\r\n--X\r\nX--\r\nX--\r\n--X\r\nX++\r\n++X\r\n--X\r\nX++\r\nX--\r\n--X\r\n++X\r\n--X\r\nX--\r\nX--\r\n++X\r\nX--\r\nX--\r\n--X\r\n++X\r\n--X\r\n++X\r\n--X\r\nX++\r\n++X\r\nX++\r\n--X\r\n++X\r\nX++\r\nX++\r\n--X\r\nX++\r\nX--\r\n--X\r\nX--\r\n--X\r\nX++\r\n++X\r\n--X\r\n++X\r\nX++\r\nX--\r\n--X\r\n--X\r\n++X\r\nX--\r\nX--\r\n--X\r\nX--\r\n--X\r\nX++\r\n--X\r\n++X\r\n--X\r\n", "output": "-14\r\n"}, {"input": "59\r\nX--\r\n--X\r\nX++\r\n++X\r\nX--\r\n--X\r\n--X\r\n++X\r\n++X\r\n++X\r\n++X\r\nX++\r\n++X\r\n++X\r\nX++\r\n--X\r\nX--\r\nX++\r\n++X\r\n--X\r\nX++\r\n--X\r\n++X\r\nX++\r\n--X\r\n--X\r\nX++\r\nX++\r\n--X\r\nX++\r\nX++\r\nX++\r\nX--\r\nX--\r\n--X\r\nX++\r\nX--\r\nX--\r\n++X\r\nX--\r\nX++\r\n--X\r\nX++\r\nX--\r\nX--\r\nX--\r\nX--\r\n++X\r\n--X\r\nX++\r\nX++\r\nX--\r\nX++\r\n++X\r\nX--\r\nX++\r\nX--\r\nX--\r\n++X\r\n", "output": "3\r\n"}, {"input": "87\r\n--X\r\n++X\r\n--X\r\nX++\r\n--X\r\nX--\r\n--X\r\n++X\r\nX--\r\n++X\r\n--X\r\n--X\r\nX++\r\n--X\r\nX--\r\nX++\r\n++X\r\n--X\r\n++X\r\n++X\r\n--X\r\n++X\r\n--X\r\nX--\r\n++X\r\n++X\r\nX--\r\nX++\r\nX++\r\n--X\r\n--X\r\n++X\r\nX--\r\n--X\r\n++X\r\n--X\r\nX++\r\n--X\r\n--X\r\nX--\r\n++X\r\n++X\r\n--X\r\nX--\r\nX--\r\nX--\r\nX--\r\nX--\r\nX++\r\n--X\r\n++X\r\n--X\r\nX++\r\n++X\r\nX++\r\n++X\r\n--X\r\nX++\r\n++X\r\nX--\r\n--X\r\nX++\r\n++X\r\nX++\r\nX++\r\n--X\r\n--X\r\n++X\r\n--X\r\nX++\r\nX++\r\n++X\r\nX++\r\nX++\r\nX++\r\nX++\r\n--X\r\n--X\r\n--X\r\n--X\r\n--X\r\n--X\r\n--X\r\nX--\r\n--X\r\n++X\r\n++X\r\n", "output": "-5\r\n"}, {"input": "101\r\nX++\r\nX++\r\nX++\r\n++X\r\n--X\r\nX--\r\nX++\r\nX--\r\nX--\r\n--X\r\n--X\r\n++X\r\nX++\r\n++X\r\n++X\r\nX--\r\n--X\r\n++X\r\nX++\r\nX--\r\n++X\r\n--X\r\n--X\r\n--X\r\n++X\r\n--X\r\n++X\r\nX++\r\nX++\r\n++X\r\n--X\r\nX++\r\nX--\r\nX++\r\n++X\r\n++X\r\nX--\r\nX--\r\nX--\r\nX++\r\nX++\r\nX--\r\nX--\r\nX++\r\n++X\r\n++X\r\n++X\r\n--X\r\n--X\r\n++X\r\nX--\r\nX--\r\n--X\r\n++X\r\nX--\r\n++X\r\nX++\r\n++X\r\nX--\r\nX--\r\n--X\r\n++X\r\n--X\r\n++X\r\n++X\r\n--X\r\nX++\r\n++X\r\nX--\r\n++X\r\nX--\r\n++X\r\nX++\r\nX--\r\n++X\r\nX++\r\n--X\r\nX++\r\nX++\r\n++X\r\n--X\r\n++X\r\n--X\r\nX++\r\n--X\r\nX--\r\n--X\r\n++X\r\n++X\r\n++X\r\n--X\r\nX--\r\nX--\r\nX--\r\nX--\r\n--X\r\n--X\r\n--X\r\n++X\r\n--X\r\n--X\r\n", "output": "1\r\n"}, {"input": "63\r\n--X\r\nX--\r\n++X\r\n--X\r\n++X\r\nX++\r\n--X\r\n--X\r\nX++\r\n--X\r\n--X\r\nX++\r\nX--\r\nX--\r\n--X\r\n++X\r\nX--\r\nX--\r\nX++\r\n++X\r\nX++\r\nX++\r\n--X\r\n--X\r\n++X\r\nX--\r\nX--\r\nX--\r\n++X\r\nX++\r\nX--\r\n--X\r\nX--\r\n++X\r\n++X\r\nX++\r\n++X\r\nX++\r\nX++\r\n--X\r\nX--\r\n++X\r\nX--\r\n--X\r\nX--\r\nX--\r\nX--\r\n++X\r\n++X\r\n++X\r\n++X\r\nX++\r\nX++\r\n++X\r\n--X\r\n--X\r\n++X\r\n++X\r\n++X\r\nX--\r\n++X\r\n++X\r\nX--\r\n", "output": "1\r\n"}, {"input": "45\r\n--X\r\n++X\r\nX--\r\n++X\r\n++X\r\nX++\r\n--X\r\n--X\r\n--X\r\n--X\r\n--X\r\n--X\r\n--X\r\nX++\r\n++X\r\nX--\r\n++X\r\n++X\r\nX--\r\nX++\r\nX--\r\n--X\r\nX--\r\n++X\r\n++X\r\n--X\r\n--X\r\nX--\r\nX--\r\n--X\r\n++X\r\nX--\r\n--X\r\n++X\r\n++X\r\n--X\r\n--X\r\nX--\r\n++X\r\n++X\r\nX++\r\nX++\r\n++X\r\n++X\r\nX++\r\n", "output": "-3\r\n"}, {"input": "21\r\n++X\r\nX++\r\n--X\r\nX--\r\nX++\r\n++X\r\n--X\r\nX--\r\nX++\r\nX--\r\nX--\r\nX--\r\nX++\r\n++X\r\nX++\r\n++X\r\n--X\r\nX--\r\n--X\r\nX++\r\n++X\r\n", "output": "1\r\n"}, {"input": "100\r\n--X\r\n++X\r\nX++\r\n++X\r\nX--\r\n++X\r\nX--\r\nX++\r\n--X\r\nX++\r\nX--\r\nX--\r\nX--\r\n++X\r\nX--\r\nX++\r\nX++\r\n++X\r\nX++\r\nX++\r\nX++\r\nX++\r\n++X\r\nX++\r\n++X\r\nX--\r\n--X\r\n++X\r\nX--\r\n--X\r\n++X\r\n++X\r\nX--\r\nX++\r\nX++\r\nX++\r\n++X\r\n--X\r\n++X\r\nX++\r\nX--\r\n++X\r\n++X\r\n--X\r\n++X\r\nX--\r\nX--\r\nX--\r\nX++\r\nX--\r\nX--\r\nX++\r\nX++\r\n--X\r\nX++\r\nX++\r\n--X\r\nX--\r\n--X\r\n++X\r\n--X\r\n++X\r\n++X\r\nX--\r\n--X\r\n++X\r\n++X\r\n--X\r\n--X\r\n++X\r\nX++\r\nX--\r\nX++\r\nX--\r\nX++\r\nX++\r\n--X\r\nX--\r\nX--\r\n++X\r\nX--\r\n--X\r\n--X\r\nX++\r\n--X\r\n--X\r\nX--\r\nX--\r\n++X\r\n++X\r\nX--\r\n++X\r\nX++\r\n--X\r\n++X\r\n++X\r\nX++\r\n--X\r\n--X\r\nX++\r\n", "output": "8\r\n"}, {"input": "17\r\nX++\r\nX++\r\n++X\r\n--X\r\n--X\r\n++X\r\n++X\r\n--X\r\nX--\r\nX++\r\nX--\r\n--X\r\n--X\r\nX--\r\n++X\r\nX--\r\nX++\r\n", "output": "-1\r\n"}, {"input": "77\r\n++X\r\nX++\r\n--X\r\nX--\r\n--X\r\n--X\r\nX--\r\nX++\r\nX--\r\nX++\r\nX--\r\n++X\r\n--X\r\n--X\r\n--X\r\n--X\r\n++X\r\nX--\r\nX++\r\nX--\r\n--X\r\nX--\r\n--X\r\nX--\r\n++X\r\n--X\r\n++X\r\n++X\r\nX++\r\nX++\r\nX--\r\n--X\r\nX--\r\nX--\r\nX++\r\n--X\r\n--X\r\n++X\r\nX--\r\nX--\r\n++X\r\nX++\r\nX--\r\n++X\r\n--X\r\nX++\r\nX--\r\n++X\r\n++X\r\n++X\r\nX--\r\nX--\r\nX--\r\n--X\r\n++X\r\n++X\r\n++X\r\nX++\r\n--X\r\n--X\r\n++X\r\n--X\r\nX--\r\nX++\r\n++X\r\nX++\r\n++X\r\nX--\r\nX++\r\nX++\r\n--X\r\nX++\r\nX++\r\nX++\r\n--X\r\nX++\r\nX--\r\n", "output": "-5\r\n"}, {"input": "21\r\nX--\r\n++X\r\n--X\r\nX--\r\n++X\r\nX--\r\n++X\r\nX--\r\n--X\r\n++X\r\nX++\r\n++X\r\nX++\r\n++X\r\nX--\r\n--X\r\nX++\r\nX++\r\nX--\r\n++X\r\nX--\r\n", "output": "1\r\n"}, {"input": "1\r\nX--\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
321/C
|
321
|
C
|
Python 3
|
TESTS
| 8
| 248
| 0
|
42550678
|
class Node:
def __init__(self, nr):
self.info = nr
self.neighbours = []
self.count = 1
self.parent = None
def addn(self, node):
self.neighbours.append(node)
node.parent = self
def addc(self, nr):
self.count += nr
n = int(input())
neighbours = [[]]
visited = []
visiteddec = []
count = []
nodechr = []
for i in range(n + 1):
neighbours.append([])
visited.append(False)
visiteddec.append(False)
count.append(1)
nodechr.append('')
for i in range(n - 1):
edge = input().split()
i = int(edge[0])
j = int(edge[1])
neighbours[i].append(j)
neighbours[j].append(i)
def dfcount(v):
visited[v] = True
r = Node(v)
for i in neighbours[v]:
if visited[i] == False:
d = dfcount(i)
r.addn(d)
r.addc(d.count)
return r
def dfnode(node):
visited[node.info] = True
node.count = 1
for i in node.neighbours:
if visited[i.info] == False and visiteddec[i.info] == False:
cn = dfnode(i)
node.count += cn
return node.count
def centroid(node, n):
visited[node.info] = True
for i in node.neighbours:
if i.count > n / 2 and visited[i.info] == False and visiteddec[i.info] == False:
return centroid(i, n)
return node
def centroiddec(node, n, ch):
cent = centroid(node, n)
visiteddec[cent.info] = True
nodechr[cent.info] = ch
if node != cent:
for i in range(n):
visited[i + 1] = False
dfnode(node)
for i in range(n):
visited[i + 1] = False
centroiddec(node, node.count, chr(ord(ch) + 1))
for ng in cent.neighbours:
if visiteddec[ng.info] == False:
for i in range(n):
visited[i + 1] = False
dfnode(ng)
for i in range(n):
visited[i + 1] = False
centroiddec(ng, ng.count, chr(ord(ch) + 1))
node = dfcount(1)
for i in range(n):
visited[i + 1] = False
centroiddec(node, n, 'A')
s = ''
for i in range(n):
s += nodechr[i + 1] + ' '
print(s)
| 44
| 842
| 46,080,000
|
188288006
|
import sys
# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353
# 重心を再帰的に求める
def centroid_finder(to, root=0):
centroids = []
pre_cent = []
subtree_size = []
n = len(to)
roots = [(root, -1, 1)]
size = [1]*n
is_removed = [0]*n
parent = [-1]*n
while roots:
root, pc, update = roots.pop()
parent[root] = -1
if update:
stack = [root]
dfs_order = []
while stack:
u = stack.pop()
size[u] = 1
dfs_order.append(u)
for v in to[u]:
if v == parent[u] or is_removed[v]: continue
parent[v] = u
stack.append(v)
for u in dfs_order[::-1]:
if u == root: break
size[parent[u]] += size[u]
c = root
while 1:
mx, u = size[root]//2, -1
for v in to[c]:
if v == parent[c] or is_removed[v]: continue
if size[v] > mx: mx, u = size[v], v
if u == -1: break
c = u
centroids.append(c)
pre_cent.append(pc)
subtree_size.append(size[root])
is_removed[c] = 1
for v in to[c]:
if is_removed[v]: continue
roots.append((v, c, v == parent[c]))
return centroids, pre_cent, subtree_size
n=II()
to=[[] for _ in range(n)]
for _ in range(n-1):
u,v=LI1()
to[u].append(v)
to[v].append(u)
cc,pp,ss=centroid_finder(to)
ans=[64]*n
for c,p in zip(cc,pp):
ans[c]=ans[p]+1
print(" ".join(chr(a) for a in ans))
|
Codeforces Round 190 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Ciel the Commander
|
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undirected roads, and for any two cities there always exists a path between them.
Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost.
There are enough officers of each rank. But there is a special rule must obey: if x and y are two distinct cities and their officers have the same rank, then on the simple path between x and y there must be a city z that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer.
Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".
|
The first line contains an integer n (2 ≤ n ≤ 105) — the number of cities in Tree Land.
Each of the following n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — they mean that there will be an undirected road between a and b. Consider all the cities are numbered from 1 to n.
It guaranteed that the given graph will be a tree.
|
If there is a valid plane, output n space-separated characters in a line — i-th character is the rank of officer in the city with number i.
Otherwise output "Impossible!".
| null |
In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.
|
[{"input": "4\n1 2\n1 3\n1 4", "output": "A B B B"}, {"input": "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10", "output": "D C B A D C B D C D"}]
| 2,100
|
["constructive algorithms", "dfs and similar", "divide and conquer", "greedy", "trees"]
| 44
|
[{"input": "4\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "A B B B\r\n"}, {"input": "10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n", "output": "D C B A D C B D C D\r\n"}, {"input": "6\r\n1 2\r\n2 4\r\n4 5\r\n6 4\r\n3 2\r\n", "output": "B A B B C C\r\n"}, {"input": "2\r\n2 1\r\n", "output": "A B\r\n"}, {"input": "15\r\n1 2\r\n1 3\r\n2 4\r\n2 5\r\n3 6\r\n3 7\r\n4 8\r\n4 9\r\n5 10\r\n5 11\r\n6 12\r\n6 13\r\n7 14\r\n7 15\r\n", "output": "A B B C C C C D D D D D D D D\r\n"}, {"input": "30\r\n23 13\r\n10 23\r\n21 10\r\n17 21\r\n2 17\r\n4 2\r\n14 4\r\n1 14\r\n19 1\r\n26 19\r\n11 26\r\n15 11\r\n25 15\r\n3 25\r\n28 3\r\n5 28\r\n18 5\r\n8 18\r\n30 8\r\n27 30\r\n22 27\r\n29 22\r\n9 29\r\n20 9\r\n7 20\r\n6 7\r\n12 6\r\n24 12\r\n16 24\r\n", "output": "E D A E D C E C E E E E E B D E E E D D C E D D E C D E B E\r\n"}, {"input": "30\r\n12 8\r\n22 8\r\n26 8\r\n19 8\r\n24 8\r\n30 8\r\n5 8\r\n27 8\r\n28 8\r\n9 8\r\n18 8\r\n2 8\r\n7 8\r\n25 8\r\n21 8\r\n11 8\r\n15 8\r\n13 8\r\n20 8\r\n3 8\r\n14 8\r\n4 8\r\n1 8\r\n29 8\r\n16 8\r\n17 8\r\n23 8\r\n6 8\r\n10 8\r\n", "output": "B B B B B B B A B B B B B B B B B B B B B B B B B B B B B B\r\n"}, {"input": "30\r\n2 29\r\n17 2\r\n21 17\r\n24 21\r\n22 24\r\n8 22\r\n18 8\r\n15 18\r\n16 15\r\n27 16\r\n5 27\r\n4 5\r\n28 4\r\n14 28\r\n20 14\r\n12 20\r\n10 12\r\n6 10\r\n26 6\r\n23 26\r\n11 23\r\n13 11\r\n19 13\r\n9 19\r\n3 9\r\n30 3\r\n1 30\r\n7 1\r\n25 7\r\n", "output": "E D E E C E D E D D E E B E E D E B E A C D D E E C E D E C\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]
with open(input_path) as f:
input_lines = f.read().splitlines()
with open(submission_path) as f:
submission_output = f.read().strip()
with open(output_path) as f:
reference_output = f.read().strip()
n = int(input_lines[0])
edges = []
for line in input_lines[1:n]:
a, b = map(int, line.split())
edges.append((a, b))
if submission_output == "Impossible!":
if reference_output == "Impossible!":
print(1)
else:
print(0)
return
ranks = submission_output.split()
if len(ranks) != n:
print(0)
return
for r in ranks:
if len(r) != 1 or not ('A' <= r <= 'Z'):
print(0)
return
# Check adjacent nodes have different ranks
adj = [[] for _ in range(n + 1)]
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
for u in range(1, n + 1):
current_rank = ranks[u - 1]
for v in adj[u]:
if v > u and ranks[v - 1] == current_rank:
print(0)
return
# Precompute rank values
rank_values = [0] * (n + 1)
for i in range(n):
rank_values[i + 1] = ord(ranks[i]) - ord('A')
present_ranks = set(ranks)
for r_char in present_ranks:
r_val = ord(r_char) - ord('A')
eligible_nodes = set()
nodes_r = []
for u in range(1, n + 1):
if ranks[u - 1] == r_char:
nodes_r.append(u)
if rank_values[u] >= r_val:
eligible_nodes.add(u)
if len(nodes_r) < 2:
continue
parent = {u: u for u in eligible_nodes}
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
def union(u, v):
pu = find(u)
pv = find(v)
if pu != pv:
parent[pu] = pv
for a, b in edges:
if a in eligible_nodes and b in eligible_nodes:
union(a, b)
seen = set()
for u in nodes_r:
pu = find(u)
if pu in seen:
print(0)
return
seen.add(pu)
print(1)
if __name__ == "__main__":
main()
| true
|
774/B
|
774
|
B
|
PyPy 3
|
TESTS
| 5
| 109
| 23,142,400
|
26155506
|
class Mcl:
def __init__(self, a, b):
self.c = a
self.w = b
def __lt__(a, b):
return a.c < b.c or a.c == b.c and a.w > b.w
def __gt__(a, b):
return a.c > b.c or a.c == b.c and a.w < b.w
def __repr__(self):
return "(" + str(self.c) + ", " + str(self.w) + ")"
def main():
n, m, d = tuple(map(int, input().split()))
a = [Mcl(0, 0) for _ in range(n)]
b = [Mcl(0, 0) for _ in range(m)]
for i in range(n):
tpl = tuple(map(int, input().split()))
a[i] = Mcl(tpl[0], tpl[1])
for i in range(m):
tpl = tuple(map(int, input().split()))
b[i] = Mcl(tpl[0], tpl[1])
cur_width = d
ans = 0
a.sort()
b.sort()
idx = n - 1
jdx = m - 1
if a[idx].w + b[jdx].w <= cur_width:
cur_width -= a[idx].w
cur_width -= b[jdx].w
ans += a[idx].c
ans += b[jdx].c
idx -= 1
jdx -= 1
else:
print(0)
return
t_cur_width = cur_width
t_idx = idx
t_jdx = jdx
ans1 = 0
while idx >= 0 and cur_width > 0:
ans1 += a[idx].c
cur_width -= a[idx].w
idx -= 1
ans2 = 0
cur_width = t_cur_width
while jdx >= 0 and cur_width > 0:
ans2 += b[jdx].c
cur_width -= b[jdx].w
jdx -= 1
idx = t_idx
jdx = t_jdx
cur_width = t_cur_width
while idx >= 0 and jdx >= 0 and cur_width > 0:
if a[idx].w <= cur_width and b[jdx].w <= cur_width:
if a[idx].c > b[jdx].c:
cur_width -= a[idx].w
ans += a[idx].c
idx -= 1
elif a[idx].c < b[jdx].c:
cur_width -= b[jdx].w
ans += b[jdx].c
jdx -= 1
else:
if a[idx].w < b[jdx].w:
cur_width -= a[idx].w
ans += a[idx].c
idx -= 1
else:
cur_width -= b[jdx].w
ans += b[jdx].c
jdx -= 1
elif a[idx].w <= cur_width:
cur_width -= a[idx].w
ans += a[idx].c
idx -= 1
elif b[jdx].w <= cur_width:
cur_width -= b[jdx].w
ans += b[jdx].c
jdx -= 1
else:
break
print(max(ans, ans1, ans2))
main()
| 44
| 1,278
| 30,310,400
|
26151161
|
n, m, d = list(map(int, input().split()))
a = []
b = []
for i in range(n):
a.append(list(map(int, input().split())))
for i in range(m):
b.append(list(map(int, input().split())))
a = sorted(a, key=lambda x: x[0] + (1- x[1] * 1e-10))
b = sorted(b, key=lambda x: x[0] + (1- x[1] * 1e-10))
tc, td = 0, 0
tc += a[-1][0]
tc += b[-1][0]
td += a[-1][1]
td += b[-1][1]
ai = n - 1
bi = m - 1
if td > d:
print(0)
exit()
while ai > 0:
t = ai - 1
if td + a[t][1] <= d:
td += a[t][1]
tc += a[t][0]
ai -= 1
continue
else:
break
cmax = tc
while bi > 0:
bi -= 1
tc += b[bi][0]
td += b[bi][1]
while td > d and ai < n:
tc -= a[ai][0]
td -= a[ai][1]
ai += 1
if ai == n:
break
if td <= d:
cmax = max(cmax, tc)
print(cmax)
|
VK Cup 2017 - Wild Card Round 1
|
ICPC
| 2,017
| 3
| 256
|
Significant Cups
|
Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance ci and width wi.
Stepan decided to expose some of his cups on a shelf with width d in such a way, that:
- there is at least one Physics cup and at least one Informatics cup on the shelf,
- the total width of the exposed cups does not exceed d,
- from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too).
Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.
|
The first line contains three integers n, m and d (1 ≤ n, m ≤ 100 000, 1 ≤ d ≤ 109) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf.
Each of the following n lines contains two integers ci and wi (1 ≤ ci, wi ≤ 109) — significance and width of the i-th cup for Physics olympiads.
Each of the following m lines contains two integers cj and wj (1 ≤ cj, wj ≤ 109) — significance and width of the j-th cup for Informatics olympiads.
|
Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement.
If there is no way to expose cups on the shelf, then print 0.
| null |
In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.
|
[{"input": "3 1 8\n4 2\n5 5\n4 2\n3 2", "output": "8"}, {"input": "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4", "output": "11"}, {"input": "2 2 2\n5 3\n6 3\n4 2\n8 1", "output": "0"}]
| 2,100
|
["*special", "binary search", "data structures", "two pointers"]
| 44
|
[{"input": "3 1 8\r\n4 2\r\n5 5\r\n4 2\r\n3 2\r\n", "output": "8\r\n"}, {"input": "4 3 12\r\n3 4\r\n2 4\r\n3 5\r\n3 4\r\n3 5\r\n5 2\r\n3 4\r\n", "output": "11\r\n"}, {"input": "2 2 2\r\n5 3\r\n6 3\r\n4 2\r\n8 1\r\n", "output": "0\r\n"}, {"input": "10 10 229\r\n15 17\r\n5 4\r\n4 15\r\n4 17\r\n15 11\r\n7 6\r\n5 19\r\n14 8\r\n4 1\r\n10 12\r\n20 13\r\n20 14\r\n16 13\r\n7 15\r\n2 16\r\n11 11\r\n19 20\r\n6 7\r\n4 11\r\n14 16\r\n", "output": "198\r\n"}, {"input": "10 20 498\r\n40 12\r\n23 25\r\n20 9\r\n8 1\r\n23 8\r\n31 24\r\n33 2\r\n22 33\r\n4 13\r\n25 20\r\n40 5\r\n27 5\r\n17 6\r\n8 5\r\n4 19\r\n33 23\r\n30 19\r\n27 12\r\n13 22\r\n16 32\r\n28 36\r\n20 18\r\n36 38\r\n9 24\r\n21 35\r\n20 9\r\n33 29\r\n29 33\r\n18 25\r\n11 8\r\n", "output": "644\r\n"}, {"input": "20 10 761\r\n42 41\r\n47 7\r\n35 6\r\n22 40\r\n15 2\r\n47 28\r\n46 47\r\n3 45\r\n12 19\r\n44 41\r\n46 2\r\n49 23\r\n9 8\r\n7 41\r\n5 3\r\n16 42\r\n12 50\r\n17 22\r\n25 9\r\n45 12\r\n41 44\r\n34 47\r\n33 35\r\n32 47\r\n49 6\r\n27 18\r\n43 36\r\n23 6\r\n39 22\r\n38 45\r\n", "output": "900\r\n"}, {"input": "1 1 1000000000\r\n4 500000000\r\n6 500000000\r\n", "output": "10\r\n"}, {"input": "4 2 8\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n1000000000 2\r\n", "output": "4000000000\r\n"}, {"input": "1 1 1000000000\r\n1 1000000000\r\n1 1000000000\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
546/B
|
546
|
B
|
Python 3
|
TESTS
| 9
| 46
| 204,800
|
222668715
|
n=int(input())
x=sorted(list(map(int,input().split())))
ans=0
for i in range(n):
if x[i]!=i+1:
ans+=x[i]-i+1
print(ans)
| 49
| 62
| 307,200
|
11212168
|
#!/usr/bin/env python
# -.- coding: utf-8 -.-
n = int(input())
cool_factors = [int(item) for item in input().strip().split(" ")]
cool_factors.sort()
acc = 0
for i in range(1, len(cool_factors)):
if cool_factors[i] <= cool_factors[i - 1]:
diff = cool_factors[i - 1] - cool_factors[i] + 1
cool_factors[i] = cool_factors[i - 1] + 1
acc += diff
print(acc)
|
Codeforces Round 304 (Div. 2)
|
CF
| 2,015
| 3
| 256
|
Soldier and Badges
|
Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.
For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.
Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.
|
First line of input consists of one integer n (1 ≤ n ≤ 3000).
Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.
|
Output single integer — minimum amount of coins the colonel has to pay.
| null |
In first sample test we can increase factor of first badge by 1.
In second sample test we can increase factors of the second and the third badge by 1.
|
[{"input": "4\n1 3 1 4", "output": "1"}, {"input": "5\n1 2 3 2 5", "output": "2"}]
| 1,200
|
["brute force", "greedy", "implementation", "sortings"]
| 49
|
[{"input": "4\r\n1 3 1 4\r\n", "output": "1"}, {"input": "5\r\n1 2 3 2 5\r\n", "output": "2"}, {"input": "5\r\n1 5 3 2 4\r\n", "output": "0"}, {"input": "10\r\n1 1 2 3 4 5 6 7 8 9\r\n", "output": "9"}, {"input": "11\r\n9 2 10 3 1 5 7 1 4 8 6\r\n", "output": "10"}, {"input": "4\r\n4 3 2 2\r\n", "output": "3"}, {"input": "1\r\n1\r\n", "output": "0"}, {"input": "50\r\n49 37 30 2 18 48 14 48 50 27 1 43 46 5 21 28 44 2 24 17 41 38 25 18 43 28 25 21 28 23 26 27 4 31 50 18 23 11 13 28 44 47 1 26 43 25 22 46 32 45\r\n", "output": "170"}, {"input": "50\r\n37 31 19 46 45 1 9 37 15 19 15 10 17 16 38 13 26 25 36 13 7 21 12 41 46 19 3 50 14 49 49 40 29 41 47 29 3 42 13 21 10 21 9 33 38 30 24 40 5 26\r\n", "output": "135"}, {"input": "50\r\n18 13 50 12 23 29 31 44 28 29 33 31 17 38 27 37 36 34 40 4 27 2 8 27 50 27 21 28 11 13 47 25 15 26 9 15 22 3 22 45 9 12 5 5 46 44 23 34 12 25\r\n", "output": "138"}, {"input": "50\r\n24 44 39 44 11 20 6 43 4 21 43 12 41 3 25 25 24 7 16 36 32 2 2 29 34 30 33 9 18 3 14 28 26 49 29 5 5 36 44 21 36 37 1 25 46 10 10 24 10 39\r\n", "output": "128"}, {"input": "50\r\n7 5 18 2 7 12 8 20 41 4 7 3 7 10 22 1 19 9 20 10 23 3 6 3 30 13 6 18 3 3 18 38 9 7 2 1 2 5 25 10 13 1 8 34 1 26 13 8 13 2\r\n", "output": "699"}, {"input": "50\r\n2 19 24 3 12 4 14 9 10 19 6 1 26 6 11 1 4 34 17 1 3 35 17 2 17 17 5 5 12 1 24 35 2 5 43 23 21 4 18 3 11 5 1 21 3 3 3 1 10 10\r\n", "output": "692"}, {"input": "50\r\n2 2 4 19 5 7 2 35 3 12 1 18 17 16 40 4 15 36 1 11 13 3 14 1 4 10 1 12 43 7 9 9 4 3 28 9 12 12 1 33 3 23 11 24 20 20 2 4 26 4\r\n", "output": "660"}, {"input": "50\r\n5 3 25 6 30 6 39 15 3 19 1 38 1 3 17 3 8 13 4 10 14 3 2 3 20 1 21 21 27 31 6 6 14 28 3 13 49 8 12 6 17 13 45 1 6 18 12 7 31 14\r\n", "output": "574"}, {"input": "50\r\n10 25 27 13 28 35 40 39 3 6 18 29 44 1 26 2 45 36 9 46 41 12 33 19 8 22 15 48 34 20 11 32 1 47 43 23 7 5 14 30 31 21 38 42 24 49 4 37 16 17\r\n", "output": "49"}, {"input": "50\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\r\n", "output": "1225"}, {"input": "50\r\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50\r\n", "output": "1225"}, {"input": "3\r\n1 3 3\r\n", "output": "1"}, {"input": "10\r\n4 4 4 4 4 4 5 5 5 5\r\n", "output": "41"}, {"input": "4\r\n1 4 4 4\r\n", "output": "3"}, {"input": "3\r\n1 1 1\r\n", "output": "3"}, {"input": "3\r\n3 3 3\r\n", "output": "3"}]
| false
|
stdio
| null | true
|
713/C
|
713
|
C
|
Python 3
|
TESTS
| 5
| 77
| 921,600
|
21611261
|
import math
import random
def IsRW (River):
t=1
for i in range(len(River)-1):
if River[i+1]<=River[i]:
t=0
return t
def SwitchRivers (River,L,R,k):
for i in range (len(River)-2):
j=i+1
if (River[j]<River[j-1]) and (River[j]<River[j+1]) and (River[j+1]>River[j-1]):
River[j]=River[j-1]+1
else:
if (River[j]>River[j-1]) and (River[j]>River[j+1]) and (River[j+1]>River[j-1]):
River[j]=River[j+1]-1
a=[]
k=0
for i in range (R-L+1):
k=k+River[L+i]
n=int(k/(R-L+1))
# M=River[L]
# for i in range ((R-L+1)):
# if abs(River[i]-n)<abs(M-n):
# M=River[i]
l=L
r=R
M=n
m = l+int((r-l+(r-l)%2)/2)
k=k+abs(River[m]-M)
if (IsRW(River)==0):
River[m]=M
for i in range(m-l):
if (River[l+i]>=M) and (IsRW(River)==0):
c=River[l+i]-(M-(m-l-i))
River[l+i]=River[l+i]-c
k=k+c
for i in range(r-m):
if (River[m+i+1]<=M) and (IsRW(River)==0):
c=(M+i+1)-River[m+i+1]
River[m+i+1]=River[m+i+1]+c
k=k+c
if (m>L) and (m<=R) and (IsRW(River)==0):
River=SwitchRivers (River,L,m-1,k)
if (m>=L) and (m<R) and (IsRW(River)==0):
River=SwitchRivers (River,m+1,R,k)
return (River)
Rivers = []
River = []
#River = [2,1,5,11,5,9,11]
#River = [5,4,3,2,1]
#River = [6,7,0,99,23,53]
n=input()
n=int(n)
s=input()
b=''
for i in range (len(s)):
a=s[i]
if a!=' ':
b=b+a
else:
c = int(b)
b=''
River.append(c)
c = int(b)
River.append(c)
for i in range(len(River)):
Rivers.append(River[i])
SwitchRivers (River,0,len(River)-1,0)
k=0
for i in range (len(River)):
k=k+abs(River[i]-Rivers[i])
print(k)
| 57
| 140
| 9,625,600
|
208735914
|
import sys
readline=sys.stdin.readline
import heapq
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
heap.append(item)
heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
if heap and item < heap[0]:
item, heap[0] = heap[0], item
heapq._siftup_max(heap, 0)
return item
class Slope_Trick:
def __init__(self,L=False,R=False,median=False,label=False,f=None,f_inve=None,e=None):
self.queueL=[]
self.queueR=[]
self.L=L
self.R=R
self.median=median
if self.median:
self.median_value=None
self.label=label
self.shiftL=0
self.shiftR=0
self.min_f=0
if self.label:
self.f=f
self.f_inve=f_inve
self.e=e
self.labelL=self.e
self.labelR=self.e
def heappush(self,x):
heappush(self.queueR,x-self.shiftR)
if self.label:
self.labelR=self.f(self.labelR,x)
def heappop(self):
x=heappop(self.queueR)+self.shiftR
if self.label:
self.labelR=self.f_inve(self.labelR,x)
return x
def heappushpop(self,x):
y=heappushpop(self.queueR,x-self.shiftR)+self.shiftR
if self.label:
self.labelR=self.f(self.labelR,x)
self.labelR=self.f_inve(self.labelR,y)
return y
def _heappush_max(self,x):
_heappush_max(self.queueL,x-self.shiftL)
if self.label:
self.labelL=self.f(self.labelL,x)
def _heappop_max(self):
x=_heappop_max(self.queueL)+self.shiftL
if self.label:
self.labelL=self.f_inve(self.labelL,x)
return x
def _heappushpop_max(self,x):
y=_heappushpop_max(self.queueL,x-self.shiftL)+self.shiftL
if self.label:
self.labelL=self.f(self.labelL,x)
self.labelL=self.f_inve(self.labelL,y)
return y
def push_Left(self,x):
if self.queueR:
self.min_f+=max(x-(self.queueR[0]+self.shiftR),0)
self._heappush_max(self.heappushpop(x))
def push_Right(self,x):
if self.queueL:
self.min_f+=max((self.queueL[0]+self.shiftL)-x,0)
self.heappush(self._heappushpop_max(x))
def pop_Left(self):
if self.queueL:
retu=self._heappop_max()
if self.queueR:
self.min_f+=max(retu-(self.queueR[0]+self.shiftR),0)
else:
retu=None
return retu
def pop_Right(self):
if self.queueR:
retu=self.heappop()
if self.queueL:
self.min_f+=max((self.queueL[0]+self.shiftL)-retu,0)
else:
retu=None
return retu
def push(self,x):
if self.L:
if len(self.queueL)<self.L:
self.push_Left(x)
else:
self.push_Right(x)
if self.R:
if len(self.queueR)<self.R:
self.push_Right(x)
else:
self.push_Left(x)
if self.median:
if self.median_value==None:
if self.queueL and x<self.queueL[0]:
self.median_value=self._heappushpop_max(x)
elif self.queueR and self.queueR[0]<x:
self.median_value=self.heappushpop(x)
else:
self.median_value=x
else:
if self.median_value<=x:
self._heappush_max(self.median_value)
self.heappush(x)
else:
self._heappush_max(x)
self.heappush(self.median_value)
self.median_value=None
def pop(self):
if self.L:
if len(self.queueL)==self.L:
retu=self._heappop_max()
if self.queueR:
self._heappush_max(self.heappop())
else:
retu=None
if self.R:
if len(self.queueR)==self.R:
retu=self.heappop()
if self.queueL:
self.heappush(self._heappop_max())
else:
retu=None
if self.median:
if self.median_value==None:
retu=(self.pop_Left(),self.pop_Right())
else:
retu=(self.median_value,None)
self.median_value=None
return retu
def top(self):
if self.L:
if len(self.queueL)==self.L:
retu=self.queueL[0]
else:
retu=None
if self.R:
if len(self.queueR)==self.R:
retu=self.queueR[0]
else:
retu=None
if self.median:
if self.median_value==None:
if self.queueL:
retu=(self.queueL[0],self.queueR[0])
else:
retu=(None,None)
else:
retu=(self.median_value,None)
return retu
def top_Left(self):
if self.queueL:
return self.queueL[0]+self.shiftL
else:
return None
def top_Right(self):
if self.queueR:
return self.queueR[0]+self.shiftR
else:
return None
def Shift_Left(self,a):
self.shiftL+=a
def Shift_Right(self,a):
self.shiftR+=a
def Label_Left(self):
return self.labelL
def Label_Right(self):
return self.labelR
def Cumultive_min(self):
self.queueR=[]
def Cumultive_max(self):
self.queueL=[]
def __len__(self):
retu=len(self.queueL)+len(self.queueR)
if self.median and self.median_value!=None:
retu+=1
return retu
def __call__(self,x):
return sum(max((l+self.shiftL)-x,0) for l in self.queueL)+sum(max(x-(r+self.shiftR),0) for r in self.queueR)+self.min_f
def __str__(self):
if self.median:
if self.median_value==None:
return "["+", ".join(map(str,sorted([x+self.shiftL for x in self.queueL])))+"]+["+", ".join(map(str,sorted([x+self.shiftR for x in self.queueR])))+"]"
else:
return "["+", ".join(map(str,sorted([x+self.shiftL for x in self.queueL])))+"]+"+str(self.median_value)+"+["+", ".join(map(str,sorted([x+self.shiftR for x in self.queueR])))+"]"
else:
return "["+", ".join(map(str,sorted([x+self.shiftL for x in self.queueL])))+"]+["+", ".join(map(str,sorted([x+self.shiftR for x in self.queueR])))+"]"
N=int(readline())
A=list(map(int,readline().split()))
ST=Slope_Trick()
for a in A:
ST.Cumultive_min()
ST.Shift_Left(1)
ST.push_Right(a)
ST.push_Left(a)
ans=ST.min_f
print(ans)
|
Codeforces Round 371 (Div. 1)
|
CF
| 2,016
| 5
| 256
|
Sonya and Problem Wihtout a Legend
|
Sonya was unable to think of a story for this problem, so here comes the formal description.
You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.
Next line contains n integer ai (1 ≤ ai ≤ 109).
|
Print the minimum number of operation required to make the array strictly increasing.
| null |
In the first sample, the array is going to look as follows:
2 3 5 6 7 9 11
|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9
And for the second sample:
1 2 3 4 5
|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
|
[{"input": "7\n2 1 5 11 5 9 11", "output": "9"}, {"input": "5\n5 4 3 2 1", "output": "12"}]
| 2,300
|
["dp", "sortings"]
| 57
|
[{"input": "7\r\n2 1 5 11 5 9 11\r\n", "output": "9\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "12\r\n"}, {"input": "2\r\n1 1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "5\r\n100 80 60 70 90\r\n", "output": "54\r\n"}, {"input": "10\r\n10 16 17 11 1213 1216 1216 1209 3061 3062\r\n", "output": "16\r\n"}, {"input": "20\r\n103 103 110 105 107 119 113 121 116 132 128 124 128 125 138 137 140 136 154 158\r\n", "output": "43\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 1 2 3\r\n", "output": "3\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "50\r\n499 780 837 984 481 526 944 482 862 136 265 605 5 631 974 967 574 293 969 467 573 845 102 224 17 873 648 120 694 996 244 313 404 129 899 583 541 314 525 496 443 857 297 78 575 2 430 137 387 319\r\n", "output": "12423\r\n"}, {"input": "75\r\n392 593 98 533 515 448 220 310 386 79 539 294 208 828 75 534 875 493 94 205 656 105 546 493 60 188 222 108 788 504 809 621 934 455 307 212 630 298 938 62 850 421 839 134 950 256 934 817 209 559 866 67 990 835 534 672 468 768 757 516 959 893 275 315 692 927 321 554 801 805 885 12 67 245 495\r\n", "output": "17691\r\n"}, {"input": "10\r\n26 723 970 13 422 968 875 329 234 983\r\n", "output": "2546\r\n"}, {"input": "20\r\n245 891 363 6 193 704 420 447 237 947 664 894 512 194 513 616 671 623 686 378\r\n", "output": "3208\r\n"}, {"input": "5\r\n850 840 521 42 169\r\n", "output": "1485\r\n"}]
| false
|
stdio
| null | true
|
965/D
|
965
|
D
|
PyPy 3-64
|
TESTS
| 14
| 93
| 11,571,200
|
201385075
|
import sys
from array import array
input = lambda: sys.stdin.buffer.readline().decode().rstrip()
inp = lambda dtype: [dtype(x) for x in input().split()]
debug = lambda *x: print(*x, file=sys.stderr)
ceil_ = lambda a, b: (a + b - 1) // b
sum_n = lambda n: (n * (n + 1)) // 2
get_bit = lambda x, i: (x >> i) & 1
Mint, Mlong, out = 2 ** 30 - 1, 2 ** 62 - 1, []
for _ in range(1):
w, l = inp(int)
a = array('i', inp(int) + [Mint])
frogs, lst = a[:], l
for i in range(l, w): frogs[i] = 0
for i in range(w - 1):
while lst - i <= l:
mi = min(frogs[i], a[lst])
frogs[i] -= mi
a[lst] -= mi
frogs[lst] += mi
if a[lst]: break
lst += 1
out.append(frogs[-1])
print('\n'.join(map(str, out)))
| 16
| 77
| 13,721,600
|
211782265
|
w, l = map(int, input().split())
stones = list(map(int, input().split()))
prefixSums = [0] * (w - l)
prefixSums[0] = sum(stones[:l])
for i in range (w - 1 - l):
prefixSums[i + 1] += prefixSums[i] + stones[i + l] - stones[i]
print(min(prefixSums))
|
Codeforces Round 476 (Div. 2) [Thanks, Telegram!]
|
CF
| 2,018
| 1
| 256
|
Single-use Stones
|
A lot of frogs want to cross a river. A river is $$$w$$$ units width, but frogs can only jump $$$l$$$ units long, where $$$l < w$$$. Frogs can also jump on lengths shorter than $$$l$$$. but can't jump longer. Hopefully, there are some stones in the river to help them.
The stones are located at integer distances from the banks. There are $$$a_i$$$ stones at the distance of $$$i$$$ units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water.
What is the maximum number of frogs that can cross the river, given that then can only jump on the stones?
|
The first line contains two integers $$$w$$$ and $$$l$$$ ($$$1 \le l < w \le 10^5$$$) — the width of the river and the maximum length of a frog's jump.
The second line contains $$$w - 1$$$ integers $$$a_1, a_2, \ldots, a_{w-1}$$$ ($$$0 \le a_i \le 10^4$$$), where $$$a_i$$$ is the number of stones at the distance $$$i$$$ from the bank the frogs are currently at.
|
Print a single integer — the maximum number of frogs that can cross the river.
| null |
In the first sample two frogs can use the different stones at the distance $$$5$$$, and one frog can use the stones at the distances $$$3$$$ and then $$$8$$$.
In the second sample although there are two stones at the distance $$$5$$$, that does not help. The three paths are: $$$0 \to 3 \to 6 \to 9 \to 10$$$, $$$0 \to 2 \to 5 \to 8 \to 10$$$, $$$0 \to 1 \to 4 \to 7 \to 10$$$.
|
[{"input": "10 5\n0 0 1 0 2 0 0 1 0", "output": "3"}, {"input": "10 3\n1 1 1 1 2 1 1 1 1", "output": "3"}]
| 1,900
|
["binary search", "flows", "greedy", "two pointers"]
| 16
|
[{"input": "10 5\r\n0 0 1 0 2 0 0 1 0\r\n", "output": "3\r\n"}, {"input": "10 3\r\n1 1 1 1 2 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "2 1\r\n0\r\n", "output": "0\r\n"}, {"input": "2 1\r\n5\r\n", "output": "5\r\n"}, {"input": "10 4\r\n0 0 6 2 7 1 6 4 0\r\n", "output": "8\r\n"}, {"input": "100 15\r\n0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 0\r\n", "output": "5\r\n"}, {"input": "10 4\r\n10 10 10 10 10 10 10 10 10\r\n", "output": "40\r\n"}, {"input": "100 34\r\n16 0 10 11 12 13 0 5 4 14 6 15 4 9 1 20 19 14 1 7 14 11 10 20 6 9 12 8 3 19 20 4 17 17 8 11 14 18 5 20 17 0 3 18 14 12 11 12 5 5 11 7 9 17 4 8 4 10 0 0 12 9 15 3 15 14 19 12 6 8 17 19 4 18 19 3 8 3 9 1 6 15 4 16 1 18 13 16 3 5 20 11 10 9 9 17 20 15 12\r\n", "output": "312\r\n"}]
| false
|
stdio
| null | true
|
321/C
|
321
|
C
|
PyPy 3-64
|
TESTS
| 8
| 156
| 3,276,800
|
153502796
|
#Problem E
import sys, collections,string
input = lambda: sys.stdin.readline()[:-1]
get_int = lambda: int(input())
get_int_iter = lambda: map(int,input().split())
get_int_list = lambda: list(map(int,input().split()))
n = get_int()
edges = collections.defaultdict(list)
for edge in range(n-1):
a,b = get_int_iter()
edges[a-1].append(b-1)
edges[b-1].append(a-1)
visited = [True] + [False]*(n-1)
visit_count = 1
step = [0]
while visit_count < n:
next_step = []
for item in step:
for edge in edges[item]:
if not visited[edge]:
visited[edge] = True
next_step.append(edge)
visit_count += 1
step = next_step
head = step[0]
#print(head)
visited = [False]*n
dist = [0]*n
ancestor = [0]*n
visit_count = 1
curr_dist = 1
step = [head]
visited[head] = True
while visit_count < n:
next_step = []
for item in step:
for edge in edges[item]:
if not visited[edge]:
visited[edge] = True
dist[edge] = curr_dist
ancestor[edge] = item
next_step.append(edge)
visit_count += 1
step = next_step
curr_dist += 1
diameter = dist[step[0]]
#print(diameter)
if diameter > 26 * 2 - 1:
print("Impossible!")
else:
mid = step[0]
for _ in range(diameter//2):
mid = ancestor[mid]
dist = [0]*n
visited = [False]*n
visited[mid] = True
visit_count = 1
curr_dist = 1
step = [mid]
while visit_count < n:
next_step = []
for item in step:
for edge in edges[item]:
if not visited[edge]:
visited[edge] = True
dist[edge] = curr_dist
next_step.append(edge)
visit_count += 1
step = next_step
curr_dist += 1
ans = [string.ascii_uppercase[dist[edge]] for edge in range(n)]
print(' '.join(ans))
| 44
| 966
| 28,364,800
|
141254513
|
def centroid_tree_decomposition(graph):
n = len(graph)
graph = [c[:] for c in graph] # copy
bfs = [0]
for node in bfs:
bfs += graph[node]
for nei in graph[node]:
graph[nei].remove(node)
size = [0] * n
for node in reversed(bfs):
size[node] = 1 + sum(size[child] for child in graph[node])
decomposition_tree = [[] for _ in range(n)]
def centroid_reroot(u):
N = size[u]
while True:
for v in graph[u]:
if size[v] > N // 2:
size[u] = N - size[v]
graph[u].remove(v)
graph[v].append(u)
u = v
break
else: # u is the centroid
for v in graph[u]:
decomposition_tree[u].append(centroid_reroot(v))
return u
decomposition_root = centroid_reroot(0)
return decomposition_tree, decomposition_root
import sys
inp = [int(x) for x in sys.stdin.buffer.read().split()]; ii = 0
n = inp[ii]; ii += 1
graph = [[] for _ in range(n)]
for _ in range(n - 1):
u = inp[ii] - 1; ii += 1
v = inp[ii] - 1; ii += 1
graph[u].append(v)
graph[v].append(u)
decomposition_tree, root = centroid_tree_decomposition(graph)
labels = [0] * n
bfs = [root]
for node in bfs:
for child in decomposition_tree[node]:
labels[child] = labels[node] + 1
bfs.append(child)
alpha = ''.join(chr(ord('A') + i) for i in range(26))
print(' '.join(alpha[label] for label in labels))
|
Codeforces Round 190 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Ciel the Commander
|
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undirected roads, and for any two cities there always exists a path between them.
Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost.
There are enough officers of each rank. But there is a special rule must obey: if x and y are two distinct cities and their officers have the same rank, then on the simple path between x and y there must be a city z that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer.
Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".
|
The first line contains an integer n (2 ≤ n ≤ 105) — the number of cities in Tree Land.
Each of the following n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — they mean that there will be an undirected road between a and b. Consider all the cities are numbered from 1 to n.
It guaranteed that the given graph will be a tree.
|
If there is a valid plane, output n space-separated characters in a line — i-th character is the rank of officer in the city with number i.
Otherwise output "Impossible!".
| null |
In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.
|
[{"input": "4\n1 2\n1 3\n1 4", "output": "A B B B"}, {"input": "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10", "output": "D C B A D C B D C D"}]
| 2,100
|
["constructive algorithms", "dfs and similar", "divide and conquer", "greedy", "trees"]
| 44
|
[{"input": "4\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "A B B B\r\n"}, {"input": "10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n", "output": "D C B A D C B D C D\r\n"}, {"input": "6\r\n1 2\r\n2 4\r\n4 5\r\n6 4\r\n3 2\r\n", "output": "B A B B C C\r\n"}, {"input": "2\r\n2 1\r\n", "output": "A B\r\n"}, {"input": "15\r\n1 2\r\n1 3\r\n2 4\r\n2 5\r\n3 6\r\n3 7\r\n4 8\r\n4 9\r\n5 10\r\n5 11\r\n6 12\r\n6 13\r\n7 14\r\n7 15\r\n", "output": "A B B C C C C D D D D D D D D\r\n"}, {"input": "30\r\n23 13\r\n10 23\r\n21 10\r\n17 21\r\n2 17\r\n4 2\r\n14 4\r\n1 14\r\n19 1\r\n26 19\r\n11 26\r\n15 11\r\n25 15\r\n3 25\r\n28 3\r\n5 28\r\n18 5\r\n8 18\r\n30 8\r\n27 30\r\n22 27\r\n29 22\r\n9 29\r\n20 9\r\n7 20\r\n6 7\r\n12 6\r\n24 12\r\n16 24\r\n", "output": "E D A E D C E C E E E E E B D E E E D D C E D D E C D E B E\r\n"}, {"input": "30\r\n12 8\r\n22 8\r\n26 8\r\n19 8\r\n24 8\r\n30 8\r\n5 8\r\n27 8\r\n28 8\r\n9 8\r\n18 8\r\n2 8\r\n7 8\r\n25 8\r\n21 8\r\n11 8\r\n15 8\r\n13 8\r\n20 8\r\n3 8\r\n14 8\r\n4 8\r\n1 8\r\n29 8\r\n16 8\r\n17 8\r\n23 8\r\n6 8\r\n10 8\r\n", "output": "B B B B B B B A B B B B B B B B B B B B B B B B B B B B B B\r\n"}, {"input": "30\r\n2 29\r\n17 2\r\n21 17\r\n24 21\r\n22 24\r\n8 22\r\n18 8\r\n15 18\r\n16 15\r\n27 16\r\n5 27\r\n4 5\r\n28 4\r\n14 28\r\n20 14\r\n12 20\r\n10 12\r\n6 10\r\n26 6\r\n23 26\r\n11 23\r\n13 11\r\n19 13\r\n9 19\r\n3 9\r\n30 3\r\n1 30\r\n7 1\r\n25 7\r\n", "output": "E D E E C E D E D D E E B E E D E B E A C D D E E C E D E C\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]
with open(input_path) as f:
input_lines = f.read().splitlines()
with open(submission_path) as f:
submission_output = f.read().strip()
with open(output_path) as f:
reference_output = f.read().strip()
n = int(input_lines[0])
edges = []
for line in input_lines[1:n]:
a, b = map(int, line.split())
edges.append((a, b))
if submission_output == "Impossible!":
if reference_output == "Impossible!":
print(1)
else:
print(0)
return
ranks = submission_output.split()
if len(ranks) != n:
print(0)
return
for r in ranks:
if len(r) != 1 or not ('A' <= r <= 'Z'):
print(0)
return
# Check adjacent nodes have different ranks
adj = [[] for _ in range(n + 1)]
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
for u in range(1, n + 1):
current_rank = ranks[u - 1]
for v in adj[u]:
if v > u and ranks[v - 1] == current_rank:
print(0)
return
# Precompute rank values
rank_values = [0] * (n + 1)
for i in range(n):
rank_values[i + 1] = ord(ranks[i]) - ord('A')
present_ranks = set(ranks)
for r_char in present_ranks:
r_val = ord(r_char) - ord('A')
eligible_nodes = set()
nodes_r = []
for u in range(1, n + 1):
if ranks[u - 1] == r_char:
nodes_r.append(u)
if rank_values[u] >= r_val:
eligible_nodes.add(u)
if len(nodes_r) < 2:
continue
parent = {u: u for u in eligible_nodes}
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
def union(u, v):
pu = find(u)
pv = find(v)
if pu != pv:
parent[pu] = pv
for a, b in edges:
if a in eligible_nodes and b in eligible_nodes:
union(a, b)
seen = set()
for u in nodes_r:
pu = find(u)
if pu in seen:
print(0)
return
seen.add(pu)
print(1)
if __name__ == "__main__":
main()
| true
|
799/C
|
799
|
C
|
Python 3
|
TESTS
| 6
| 77
| 0
|
121333151
|
from bisect import bisect_right as br
def f(Coin,Diamond,c,d):
Coin=sorted(Coin,key=lambda s:s[1])
Coin=sorted(Coin,key=lambda s:s[0])
Diamond=sorted(Diamond,key=lambda s:s[1])
Diamond=sorted(Diamond,key=lambda s:s[0])
c1=[]
c2=[]
for i in range(len(Coin)):
t=c
if Coin[i][0]>c:
break
rr=br(Coin,(t-Coin[i][0],float("inf")),lo=i+1)
if rr>=0:
t=Coin[i+1:rr]
if t:
mx=max(t,key=lambda s:s[1])
c2.append(mx[1]+Coin[i][1])
c1.append(Coin[i][1])
else:
c1.append(Coin[i][1])
# print(c1,c2)
d1=[]
d2=[]
for i in range(len(Diamond)):
t=d
if Diamond[i][0]>c:
break
rr=br(Diamond,(t-Diamond[i][0],float("inf")),lo=i+1)
# print(Coin[i+1:rr])
if rr>=0:
t=Diamond[i+1:rr]
if t:
mx=max(t,key=lambda s:s[1])
d2.append(mx[1]+Diamond[i][1])
d1.append(Diamond[i][1])
else:
d1.append(Diamond[i][1])
ans=0
if d2:
ans=max(ans,max(d2))
if c2:
ans=max(ans,max(c2))
if c1 and d1:
ans=max(ans,max(c1)+max(d1))
return ans
n,c,d=map(int,input().strip().split())
Coin=[]
Diamond=[]
for _ in range(n):
l=list(input().split())
if l[2]=="C":
Coin.append((int(l[1]),int(l[0]))) # cost, beauty
else:
Diamond.append((int(l[1]), int(l[0])))
print(f(Coin,Diamond,c,d))
| 79
| 468
| 27,136,000
|
203455020
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def sparse_table(a):
table = []
s0, l = a, 1
table.append(s0)
while 2 * l <= len(a):
s = [max(s0[i], s0[i + l]) for i in range(len(s0) - l)]
table.append(list(s))
s0 = s
l *= 2
return table
def get_max(l, r, table):
d = (r - l + 1).bit_length() - 1
d = len(bin(r - l + 1)) - 3
ans = max(table[d][l], table[d][r - pow2[d] + 1])
return ans
n, c, d = map(int, input().split())
l = pow(10, 5) + 5
x, y = [0] * l, [0] * l
ans = 0
for _ in range(n):
b, p, t = input().rstrip().decode().split()
b, p = int(b), int(p)
if t == "C":
if x[p] and 2 * p <= c:
ans = max(ans, x[p] + b)
x[p] = max(x[p], b)
else:
if y[p] and 2 * p <= d:
ans = max(ans, y[p] + b)
y[p] = max(y[p], b)
pow2 = [1]
for _ in range(20):
pow2.append(2 * pow2[-1])
mx = sparse_table(x)
my = sparse_table(y)
for i in range(1, c):
if not x[i]:
continue
l, r = 1, c - i
if l <= i <= r:
u = [(l, i - 1), (i + 1, r)]
else:
u = [(l, r)]
ma = 0
for l, r in u:
if l <= r:
ma = max(ma, get_max(l, r, mx))
if ma:
ans = max(ans, x[i] + ma)
for i in range(1, d):
if not y[i]:
continue
l, r = 1, d - i
if l <= i <= r:
u = [(l, i - 1), (i + 1, r)]
else:
u = [(l, r)]
ma = 0
for l, r in u:
if l <= r:
ma = max(ma, get_max(l, r, my))
if ma:
ans = max(ans, y[i] + ma)
u, v = get_max(1, c, mx), get_max(1, d, my)
if min(u, v):
ans = max(ans, u + v)
print(ans)
|
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
|
847/H
|
847
|
H
|
Python 3
|
TESTS
| 8
| 46
| 0
|
169523391
|
from sys import stdin
import math
def main():
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
res = 0
l = 0
r = n-1
while l < n-1 and a[l+1] > a[l]:
l += 1
while r > 0 and a[r-1] > a[r]:
r -= 1
while l < r:
ll = a[l] - a[l+1] + 1
rr = a[r] - a[r-1] + 1
if ll < 0:
l += 1
continue
if rr < 0:
r -= 1
continue
if ll < rr:
a[l+1] += ll
res += ll
l += 1
else:
a[r-1] += rr
res += rr
r -= 1
print(res)
main()
| 39
| 109
| 20,582,400
|
182942535
|
import sys
input = sys.stdin.readline
n = int(input())
w = list(map(int, input().split()))
d = [w[0]]
e = [w[-1]]
for i in w[1:]:
d.append(max(d[-1]+1, i))
for i in w[:-1][::-1]:
e.append(max(e[-1]+1, i))
e = e[::-1]
c = 10**18
f = -1
for i in range(n):
a = max(d[i], e[i])
b = (2*a-i)*(i+1) + (2*a-(n-i-1))*(n-i)
if b//2 < c:
f = i
c = b//2
c = 0
for i in range(1, f+1):
a = max(w[i-1]+1, w[i])
c += a - w[i]
w[i] = a
for i in range(n-2, f-1, -1):
a = max(w[i+1]+1, w[i])
c += a - w[i]
w[i] = a
print(c)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
847/H
|
847
|
H
|
Python 3
|
TESTS
| 8
| 46
| 5,734,400
|
33505680
|
n=int(input())
p=list(map(int,input().split()))
a=[0]*n
t=-1
for i in range(n):
if p[i]<=t:a[i]=t-p[i]+1
t=max(p[i],t+1)
t=-1
for i in range(n-1,0,-1):
if p[i] <= t: a[i] = min(t - p[i] + 1,a[i])
else:a[i]=0
t = max(p[i], t + 1)
print(sum(a[1:n-1]))
| 39
| 124
| 19,046,400
|
211256726
|
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
N = int(input())
A = list(map(int,input().split()))
R,L = A[:],A[:]
r,l = [0,0],[0,0]
for i in range(N-2,-1,-1):
if R[i]<=R[i+1]:
r.append(r[-1]+((R[i+1]+1)-R[i]))
R[i] = R[i+1]+1
else:
r.append(r[-1])
r.pop()
r = r[::-1]
for i in range(1,N):
if L[i]<=L[i-1]:
l.append(l[-1]+((L[i-1]+1)-L[i]))
L[i] = L[i-1]+1
else:
l.append(l[-1])
l.pop()
# print(L,R,l,r)
ans = float('inf')
for i in range(N):
ans = min(ans,(max(R[i],L[i])-A[i])+r[i]+l[i])
print(ans)
|
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
|
ICPC
| 2,017
| 1
| 256
|
Load Testing
|
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.
Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
|
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.
|
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
| null |
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the third minute, so the answer is 1.
In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
|
[{"input": "5\n1 4 3 2 5", "output": "6"}, {"input": "5\n1 2 2 2 1", "output": "1"}, {"input": "7\n10 20 40 50 70 90 30", "output": "0"}]
| 1,600
|
["greedy"]
| 39
|
[{"input": "5\r\n1 4 3 2 5\r\n", "output": "6\r\n"}, {"input": "5\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7\r\n10 20 40 50 70 90 30\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 15\r\n", "output": "0\r\n"}, {"input": "4\r\n36 54 55 9\r\n", "output": "0\r\n"}, {"input": "5\r\n984181411 215198610 969039668 60631313 85746445\r\n", "output": "778956192\r\n"}, {"input": "10\r\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916\r\n", "output": "1982580029\r\n"}, {"input": "100\r\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805\r\n", "output": "478217\r\n"}]
| false
|
stdio
| null | true
|
283/B
|
283
|
B
|
PyPy 3
|
TESTS
| 5
| 278
| 0
|
97800282
|
INF = float('inf')
n = int(input())
aaa = [0, 0] + list(map(int, input().split()))
dp = [[0 for _ in range(n+1)] for i in range(2)]
vis = [[0 for _ in range(n+1)] for i in range(2)]
rs = [0] * (n-1)
def di(d): return 0 if d == 1 else 1
def solve(x, d):
if dp[di(d)][x]:
return dp[di(d)][x]
if vis[di(d)][x]:
return INF
ni = x + aaa[x] * d
if ni < 1 or ni > n:
return aaa[x]
vis[di(d)][x] = 1
r = aaa[x] + solve(ni, -d)
vis[di(d)][x] = 0
return r
for D in [1, -1]:
for x in range(2, n+1):
d = D
if dp[di(d)][x]:
continue
ni = x + aaa[x] * d
path = [x]
values = [aaa[x]]
while ni > 1 and ni <= n:
path.append(ni)
if dp[di(d)][ni]:
values.append(dp[di(d)][ni])
d *= -1
break
if vis[di(d)][ni]:
values.append(INF)
d *= -1
break
vis[di(d)][ni] = 1
values.append(aaa[ni])
d *= -1
ni = ni + aaa[ni] * d
if ni == 1:
continue
for i in range(len(values)-2, -1, -1):
values[i] = values[i] + values[i+1]
while path:
dp[di(d)][path.pop()] = values.pop()
d *= -1
for i in range(1, n):
aaa[1] = i
res = solve(1, 1)
rs[i-1] = res if res < INF else -1
print('\n'.join(map(str, rs)))
| 50
| 466
| 29,696,000
|
171032684
|
n = int(input())
t = [0, 0]
t += list(map(int, input().split()))
n += 1
a = [0] * n
b = [0] * n
n -= 1
a[1] = b[1] = -1
#print(t, a, b)
def calc(s, a, b, l):
global t
l.reverse()
j = 0
n = len(l)
while True:
s += t[l[j]]
a[l[j]] = s
j += 1
if j == n:
return
s += t[l[j]]
b[l[j]] = s
j += 1
if j == n:
return
def calc2(i, k):
global a, b
l = []
if k:
a[i] = -1
l.append(i)
i += t[i]
while True:
if i > n:
return calc(0, a, b, l)
if b[i] > 0:
return calc(b[i], a, b, l)
if b[i] == -1:
return
b[i] = -1
l.append(i)
i -= t[i]
if i < 1:
return calc(0, b, a, l)
if a[i] > 0:
return calc(a[i], b, a, l)
if a[i] == -1:
return
a[i] -= 1
l.append(i)
i += t[i]
for i in range(2, n + 1):
if a[i] == 0:
calc2(i, True)
if b[i] == 0:
calc2(i, False)
for i in range(1, n):
if b[i + 1] > 0:
t[i] = i + b[i + 1]
else:
t[i] = -1
#print(t, a, b)
print('\n'.join(map(str, t[1:n])))
|
Codeforces Round 174 (Div. 1)
|
CF
| 2,013
| 2
| 256
|
Cow Program
|
Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, ..., an of positive integers:
1. Initially, x = 1 and y = 0. If, after any step, x ≤ 0 or x > n, the program immediately terminates.
2. The program increases both x and y by a value equal to ax simultaneously.
3. The program now increases y by ax while decreasing x by ax.
4. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on.
The cows are not very good at arithmetic though, and they want to see how the program works. Please help them!
You are given the sequence a2, a3, ..., an. Suppose for each i (1 ≤ i ≤ n - 1) we run the program on the sequence i, a2, a3, ..., an. For each such run output the final value of y if the program terminates or -1 if it does not terminate.
|
The first line contains a single integer, n (2 ≤ n ≤ 2·105). The next line contains n - 1 space separated integers, a2, a3, ..., an (1 ≤ ai ≤ 109).
|
Output n - 1 lines. On the i-th line, print the requested value when the program is run on the sequence i, a2, a3, ...an.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample
1. For i = 1, x becomes $$1 \rightarrow 2 \rightarrow 0$$ and y becomes 1 + 2 = 3.
2. For i = 2, x becomes $$1 \rightarrow 3 \rightarrow - 1$$ and y becomes 2 + 4 = 6.
3. For i = 3, x becomes $$1 \rightarrow 4 \rightarrow 3 \rightarrow 7$$ and y becomes 3 + 1 + 4 = 8.
|
[{"input": "4\n2 4 1", "output": "3\n6\n8"}, {"input": "3\n1 2", "output": "-1\n-1"}]
| 1,700
|
["dfs and similar", "dp", "graphs"]
| 50
|
[{"input": "4\r\n2 4 1\r\n", "output": "3\r\n6\r\n8\r\n"}, {"input": "3\r\n1 2\r\n", "output": "-1\r\n-1\r\n"}, {"input": "5\r\n2 2 1 3\r\n", "output": "3\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "2\r\n1\r\n", "output": "-1\r\n"}, {"input": "8\r\n7 6 2 6 2 6 6\r\n", "output": "8\r\n8\r\n12\r\n10\r\n-1\r\n-1\r\n20\r\n"}, {"input": "8\r\n4 5 3 2 3 3 3\r\n", "output": "5\r\n7\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "3\r\n1 1\r\n", "output": "-1\r\n-1\r\n"}, {"input": "5\r\n3 2 4 2\r\n", "output": "4\r\n-1\r\n7\r\n-1\r\n"}, {"input": "10\r\n6 7 5 3 1 5 2 4 6\r\n", "output": "7\r\n9\r\n8\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "8\r\n6 311942309 3 1 3 2 2\r\n", "output": "7\r\n311942311\r\n-1\r\n311942323\r\n311942317\r\n311942321\r\n12\r\n"}, {"input": "8\r\n2 3 1 2 2 3 3\r\n", "output": "3\r\n5\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "6\r\n2 1 2 2 3\r\n", "output": "3\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "23\r\n20 1 3 3 13 11 9 7 5 3 1 7 2 4 6 8 10 12 14 16 12 5\r\n", "output": "21\r\n-1\r\n-1\r\n-1\r\n18\r\n17\r\n16\r\n-1\r\n26\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n48\r\n-1\r\n37\r\n"}, {"input": "71\r\n28 11 39 275858941 64 69 66 18 468038892 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 25 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 701366631 51 25 11 11 49 33 67 43 57\r\n", "output": "29\n13\n42\n275858945\n69\n75\n73\n26\n468038901\n59\n58\n57\n56\n55\n54\n53\n52\n51\n50\n49\n48\n47\n-1\n-1\n113\n468038935\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n701366692\n-1\n-1\n111\n114\n-1\n-1\n-1\n-1\n-1\n"}, {"input": "23\r\n11 6 21 9 13 11 9 7 5 3 1 8 2 4 6 8 10 12 14 935874687 21 1\r\n", "output": "12\r\n8\r\n24\r\n13\r\n18\r\n17\r\n16\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n935874707\r\n-1\r\n44\r\n"}, {"input": "71\r\n2 50 62 41 50 16 65 6 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 26 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 14 6 67 54 54 620768469 637608010 27 54 18 49\r\n", "output": "3\n52\n65\n45\n55\n22\n72\n801\n58\n57\n56\n55\n54\n53\n52\n51\n50\n49\n48\n47\n46\n45\n831\n1067\n87\n1147\n891\n671\n487\n339\n227\n151\n111\n105\n109\n117\n129\n145\n165\n189\n217\n249\n285\n325\n369\n417\n469\n525\n585\n649\n717\n789\n865\n945\n1029\n1117\n1209\n1305\n1405\n543\n109\n129\n1413\n1317\n620768534\n637608076\n843\n973\n121\n515\n"}]
| false
|
stdio
| null | true
|
127/A
|
127
|
A
|
PyPy 3-64
|
TESTS
| 1
| 92
| 512,000
|
136274303
|
import math
## B - Wasted Time
def calculate_distance(v1, v2):
x1, y1 = v1
x2, y2 = v2
x = abs(x2 - x1)
y = abs(y2 - y1)
return math.sqrt(x**2 + y**2)
n_lines, n_signed_papers = [int(n) for n in input().split(" ")]
coordinates = []
for n in range(n_lines):
x, y = [int(n) for n in input().split(" ")]
coordinates.append((x, y))
# print(coordinates)
total_dist = 0
coord1 = coordinates[0]
for coord in coordinates[1:]:
total_dist += calculate_distance(coord1, coord)
# print(total_dist)
# print(total_dist * n_signed_papers)
total_dist = total_dist * n_signed_papers
total_time = total_dist / 50
print("{:.9f}".format(total_time))
| 42
| 62
| 0
|
151050037
|
# t = int(input())
# for i in range(t):
# n = int(input())
# a = sorted([int(y) for y in input().split()])
# print(a[-1]-a[0])
# t = int(input())
# for i in range(t):
# a = [int(y) for y in input().split()]
# if a == [1, 1]:
# print(0)
# elif a[0] == 1 or a[-1] == 1:
# print(1)
# else:
# print(2)
# t = int(input())
# for i in range(t):
# l = sorted([int(y) for y in input().split()], reverse = 1)
# if l[0] == l[1] + l[2]:
# print('YES')
# elif l[0] == l[1]:
# if l[2] % 2 == 0:
# print('YES')
# else:
# print('NO')
# elif l[1] == l[2]:
# if l[0] % 2 == 0:
# print('YES')
# else:
# print('NO')
# else:
# print('NO')
# t = int(input())
# for i in range(t):
# a, b, c = [int(y) for y in input().split()]
# if (b - (c - b)) % a == 0 and (b - (c - b)) / a > 0:
# print('YES')
# elif ((c+a)/2) % b == 0 and ((c+a)/2) / b > 0:
# print('YES')
# elif (b+b-a) % c == 0 and (b+b-a) / c > 0:
# print('YES')
# else:
# print('NO')
# t = int(input())
# for i in range(t):
# n, s = [int(y) for y in input().split()]
# r = s // n**2
# if n == 0 or s == 0:
# print(0)
# else:
# print(r)
# a = ''
# for i in range(int(input())):
# x1, p1 = [int(y) for y in input().split()]
# x2, p2 = [int(r) for r in input().split()]
# if p1-p2 > 6:
# a += '>\n'
# elif p2-p1 > 6:
# a += '<\n'
# else:
# if p1 > p2:
# x1 = x1 * 10**(p1-p2)
# elif p1 < p2:
# x2 = x2 * 10 ** (p2 - p1)
# if x1 == x2:
# a += '=\n'
# elif x1 < x2:
# a += '<\n'
# else:
# a += '>\n'
# print(a)
# for i in range(int(input())):
# n = int(input())
# print(-1*(n-1), n)
# for i in range(int(input())):
# l, r, a = [int(r) for r in input().split()]
# t = r%a
# d = r//a
# if t == a-1 or t == a-2:
# print(t+d)
# else:
# if r-t-1 >= l:
# print((a-1)+(d-1))
# else:
# print(t+d)
# for i in range(int(input())):
# x, n = [int(r) for r in input().split()]
# n1 = n % 4
# if n1 > 0:
# for j in range(n - n1 + 1, n+1):
# if x % 2 != 0:
# x += j
# else:
# x -= j
# print(x)
# else:
# print(x)
# import math
# for i in range(int(input())):
# n, k = [int(r) for r in input().split()]
# if n >= k:
# if n % k == 0:
# print(1)
# else:
# print(2)
# else:
# print(math.ceil(k/n))
# import math
# for i in range(int(input())):
# w, h = [int(r) for r in input().split()]
# x = [int(r) for r in input().split()[1:]]
# x2 = [int(r) for r in input().split()[1:]]
# y = [int(r) for r in input().split()[1:]]
# y2 =[int(r) for r in input().split()[1:]]
# xy = [
# [[min(x),0], [max(x), 0]],
# [[0, min(y)], [0, max(y)]],
# [[min(x2), h], [max(x2), h]],
# [[w, min(y2)], [w, max(y2)]]
# ]
# s_total = 0
# for j in range(len(xy)):
# k = (j+2)%4
# for f in range(2):
# a = math.sqrt((xy[j][0][0] - xy[j][1][0])**2 + (xy[j][0][1] - xy[j][1][1])**2)
# b = math.sqrt((xy[j][0][0] - xy[k][f][0])**2 + (xy[j][0][1] - xy[k][f][1])**2)
# c = math.sqrt((xy[j][1][0] - xy[k][f][0])**2 + (xy[j][1][1] - xy[k][f][1])**2)
# p = (a+b+c) / 2
# s = math.sqrt(p*(p-a)*(p-b)*(p-c))
# if s > s_total:
# s_total = s
# print(round(s_total*2))
# s = 0
# for i in range(int(input())):
# x, y = [int(r) for r in input().split()]
# if x+y > s:
# s = x+y
# print(s)
# for i in range(int(input())):
# n = int(input())
# p = [int(r) for r in input().split()]
# m = int(input())
# q = [int(r) for r in input().split()]
# ch1 = 0
# nch1 = 0
# ch2 = 0
# nch2 = 0
# for j in p:
# if j % 2 == 0:
# ch1 += 1
# else:
# nch1 += 1
# for f in q:
# if f % 2 == 0:
# ch2 += 1
# else:
# nch2 += 1
# print(ch1*ch2+nch1*nch2)
# h, l = [int(r) for r in input().split()]
# print((l**2+h**2)/(2*h)-h)
# import math
# n = int(input())
# n1 = math.floor(math.sqrt(n))
# n2 = n1**2
# n3 = n - n2
# n4 = math.ceil(n3/n1)
# print(2*(n1+n1+n4))
import math
n, k = [int(r) for r in input().split()]
c = []
for i in range(n):
x, y = [int(r) for r in input().split()]
c.append([x, y])
td = 0
for j in range(n-1):
d = math.sqrt((c[j][0]-c[j+1][0])**2+(c[j][1]-c[j+1][1])**2)
td += d
print((td/50)*k)
|
Codeforces Beta Round 93 (Div. 2 Only)
|
CF
| 2,011
| 2
| 256
|
Wasted Time
|
Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.
Mr. Scrooge's signature can be represented as a polyline A1A2... An. Scrooge signs like that: first it places a pen at the point A1, then draws a segment from point A1 to point A2, then he draws a segment from point A2 to point A3 and so on to point An, where he stops signing and takes the pen off the paper. At that the resulting line can intersect with itself and partially repeat itself but Scrooge pays no attention to it and never changes his signing style. As Scrooge makes the signature, he never takes the pen off the paper and his writing speed is constant — 50 millimeters per second.
Scrooge signed exactly k papers throughout his life and all those signatures look the same.
Find the total time Scrooge wasted signing the papers.
|
The first line contains two integers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai — integers xi and yi, separated by a space.
All points Ai are different. The absolute value of all coordinates does not exceed 20. The coordinates are measured in millimeters.
|
Print one real number — the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6.
| null | null |
[{"input": "2 1\n0 0\n10 0", "output": "0.200000000"}, {"input": "5 10\n3 1\n-5 6\n-2 -1\n3 2\n10 0", "output": "6.032163204"}, {"input": "6 10\n5 0\n4 0\n6 0\n3 0\n7 0\n2 0", "output": "3.000000000"}]
| 900
|
["geometry"]
| 42
|
[{"input": "2 1\r\n0 0\r\n10 0\r\n", "output": "0.200000000"}, {"input": "5 10\r\n3 1\r\n-5 6\r\n-2 -1\r\n3 2\r\n10 0\r\n", "output": "6.032163204"}, {"input": "6 10\r\n5 0\r\n4 0\r\n6 0\r\n3 0\r\n7 0\r\n2 0\r\n", "output": "3.000000000"}, {"input": "10 95\r\n-20 -5\r\n2 -8\r\n14 13\r\n10 3\r\n17 11\r\n13 -12\r\n-6 11\r\n14 -15\r\n-13 14\r\n19 8\r\n", "output": "429.309294877"}, {"input": "30 1000\r\n4 -13\r\n14 13\r\n-14 -16\r\n-9 18\r\n17 11\r\n2 -8\r\n2 15\r\n8 -1\r\n-9 13\r\n8 -12\r\n-2 20\r\n11 -12\r\n19 8\r\n9 -15\r\n-20 -5\r\n-18 20\r\n-13 14\r\n-12 -17\r\n-4 3\r\n13 -12\r\n11 -10\r\n18 7\r\n-6 11\r\n10 13\r\n10 3\r\n6 -14\r\n-1 10\r\n14 -15\r\n2 11\r\n-8 10\r\n", "output": "13629.282573522"}, {"input": "2 1\r\n-20 -10\r\n-10 -6\r\n", "output": "0.215406592"}, {"input": "2 13\r\n13 -10\r\n-3 -2\r\n", "output": "4.651021393"}, {"input": "2 21\r\n13 8\r\n14 10\r\n", "output": "0.939148551"}, {"input": "2 75\r\n-3 12\r\n1 12\r\n", "output": "6.000000000"}, {"input": "2 466\r\n10 16\r\n-6 -3\r\n", "output": "231.503997374"}, {"input": "2 999\r\n6 16\r\n-17 -14\r\n", "output": "755.286284531"}, {"input": "2 1000\r\n-17 -14\r\n-14 -8\r\n", "output": "134.164078650"}, {"input": "3 384\r\n-4 -19\r\n-17 -2\r\n3 4\r\n", "output": "324.722285390"}, {"input": "5 566\r\n-11 8\r\n2 -7\r\n7 0\r\n-7 -9\r\n-7 5\r\n", "output": "668.956254495"}, {"input": "7 495\r\n-10 -13\r\n-9 -5\r\n4 9\r\n8 13\r\n-4 2\r\n2 10\r\n-18 15\r\n", "output": "789.212495576"}, {"input": "10 958\r\n7 13\r\n20 19\r\n12 -7\r\n10 -10\r\n-13 -15\r\n-10 -7\r\n20 -5\r\n-11 19\r\n-7 3\r\n-4 18\r\n", "output": "3415.618464093"}, {"input": "13 445\r\n-15 16\r\n-8 -14\r\n8 7\r\n4 15\r\n8 -13\r\n15 -11\r\n-12 -4\r\n2 -13\r\n-5 0\r\n-20 -14\r\n-8 -7\r\n-10 -18\r\n18 -5\r\n", "output": "2113.552527680"}, {"input": "18 388\r\n11 -8\r\n13 10\r\n18 -17\r\n-15 3\r\n-13 -15\r\n20 -7\r\n1 -10\r\n-13 -12\r\n-12 -15\r\n-17 -8\r\n1 -2\r\n3 -20\r\n-8 -9\r\n15 -13\r\n-19 -6\r\n17 3\r\n-17 2\r\n6 6\r\n", "output": "2999.497312668"}, {"input": "25 258\r\n-5 -3\r\n-18 -14\r\n12 3\r\n6 11\r\n4 2\r\n-19 -3\r\n19 -7\r\n-15 19\r\n-19 -12\r\n-11 -10\r\n-5 17\r\n10 15\r\n-4 1\r\n-3 -20\r\n6 16\r\n18 -19\r\n11 -19\r\n-17 10\r\n-17 17\r\n-2 -17\r\n-3 -9\r\n18 13\r\n14 8\r\n-2 -5\r\n-11 4\r\n", "output": "2797.756635934"}, {"input": "29 848\r\n11 -10\r\n-19 1\r\n18 18\r\n19 -19\r\n0 -5\r\n16 10\r\n-20 -14\r\n7 15\r\n6 8\r\n-15 -16\r\n9 3\r\n16 -20\r\n-12 12\r\n18 -1\r\n-11 14\r\n18 10\r\n11 -20\r\n-20 -16\r\n-1 11\r\n13 10\r\n-6 13\r\n-7 -10\r\n-11 -10\r\n-10 3\r\n15 -13\r\n-4 11\r\n-13 -11\r\n-11 -17\r\n11 -5\r\n", "output": "12766.080247922"}]
| false
|
stdio
|
import sys
import math
def read_points(n, input_file):
points = []
for _ in range(n):
x, y = map(int, input_file.readline().split())
points.append((x, y))
return points
def compute_total_time(n, k, points):
total_length = 0.0
for i in range(1, n):
x0, y0 = points[i-1]
x1, y1 = points[i]
dx = x1 - x0
dy = y1 - y0
total_length += math.hypot(dx, dy)
return (total_length * k) / 50.0
def main(input_path, output_path, submission_path):
with open(input_path) as f_input:
n, k = map(int, f_input.readline().split())
points = read_points(n, f_input)
correct = compute_total_time(n, k, points)
with open(submission_path) as f_sub:
submission_line = f_sub.readline().strip()
try:
submission = float(submission_line)
except:
print(0)
return
absolute_error = abs(submission - correct)
if correct == 0:
is_correct = absolute_error <= 1e-6
else:
relative_error = absolute_error / correct
is_correct = absolute_error <= 1e-6 or relative_error <= 1e-6
print(100 if is_correct else 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
|
28/B
|
28
|
B
|
Python 3
|
TESTS
| 1
| 62
| 0
|
167819672
|
n=int(input())
vt=[[] for i in range(n)]
p=input().split(' ')
d=input().split(' ')
for i in range(len(p)):
p[i]=int(p[i])-1
for i in range(len(d)):
d[i]=int(d[i])
vt[i].append(d[i])
vt[d[i]].append(i)
def r_able(x,y,fax):
if x==y:
return True
for i in vt[x]:
if i!=fax and r_able(i,y,x):
return True
return False
q='YES'
for i in range(n):
if not r_able(i,p[i],-1):
q='NO'
print(q)
| 33
| 124
| 0
|
194483465
|
import sys
input = sys.stdin.readline
n = int(input())
w = list(map(int, input().split()))
qq = list(map(int, input().split()))
d = [set() for i in range(n)]
for i, j in enumerate(qq):
if i-j > -1:
d[i-j].add(i)
d[i].add(i-j)
if i+j < n:
d[i+j].add(i)
d[i].add(i+j)
x = [0]*n
for i in range(n):
if x[i] == 0:
k = {i}
q = [i]
while q:
a = q.pop()
for j in d[a]:
if x[j] == 0:
x[j] = 1
k.add(j)
q.append(j)
a = [i+1 for i in k]
if sorted(a) != sorted([w[i] for i in k]):
print('NO')
exit()
print('YES')
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
pSort
|
One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.
The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.
|
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.
|
If the given state is reachable in the described game, output YES, otherwise NO.
| null | null |
[{"input": "5\n5 4 3 2 1\n1 1 1 1 1", "output": "YES"}, {"input": "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1", "output": "NO"}, {"input": "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1", "output": "YES"}]
| 1,600
|
["dfs and similar", "dsu", "graphs"]
| 33
|
[{"input": "5\r\n5 4 3 2 1\r\n1 1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "7\r\n4 3 5 1 2 7 6\r\n4 6 6 1 6 6 1\r\n", "output": "NO\r\n"}, {"input": "7\r\n4 2 5 1 3 7 6\r\n4 6 6 1 6 6 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n3 2 1 4 6 5\r\n3 6 1 6 6 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n3 5 1 4 6 2\r\n3 6 1 6 6 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 2 3 4\r\n1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "13\r\n13 1 12 4 6 10 7 8 9 3 11 5 2\r\n6 12 12 7 11 7 10 3 1 5 2 2 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 3 2 4 5\r\n1 4 1 2 4\r\n", "output": "YES\r\n"}, {"input": "10\r\n6 2 9 4 8 7 5 10 3 1\r\n2 9 7 9 4 5 1 5 3 2\r\n", "output": "YES\r\n"}, {"input": "8\r\n5 2 3 4 8 6 1 7\r\n6 7 7 7 7 7 2 7\r\n", "output": "YES\r\n"}, {"input": "17\r\n1 11 3 4 5 6 7 8 9 10 2 12 13 14 15 16 17\r\n13 9 16 5 16 12 11 4 4 7 12 16 2 7 14 6 3\r\n", "output": "YES\r\n"}, {"input": "19\r\n7 2 17 18 4 16 1 9 12 10 8 11 6 13 14 19 3 5 15\r\n12 4 9 1 13 18 14 10 18 2 17 16 12 3 16 6 5 7 7\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n2 1\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "2\r\n2 1\r\n1 1\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
401/B
|
401
|
B
|
PyPy 3
|
TESTS
| 0
| 140
| 0
|
78330681
|
x, k = map(int, input().split())
v = [0 for i in range(x+1)]
v[0] = 1
v[x] += 1
for i in range(k):
p = [int(i) for i in input().split()]
v[p[1]]+=1
if p[0] == 1:
v[p[2]]+=1
zer = 0
for i in v:
if i == 0:
zer+=1
print(int(zer/2)+1, zer)
| 47
| 77
| 0
|
5981653
|
#!/usr/bin/env python3
def read_ints():
return map(int, input().strip().split())
t, k = read_ints()
all = []
for _ in range(k):
avail = list(read_ints())
for x in avail[1:]:
all.append(x)
all.sort()
res_min, res_max = 0, 0
last = 0
for num in all:
d = num-last
res_min += (d)//2
res_max += d-1
last = num
d = t-last
res_min += (d)//2
res_max += d-1
print(res_min, res_max)
|
Codeforces Round 235 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Sereja and Contests
|
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.
Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.
Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed.
Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.
|
The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in.
Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num2 num1" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2 num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.
|
Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.
| null |
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round.
The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds.
|
[{"input": "3 2\n2 1\n2 2", "output": "0 0"}, {"input": "9 3\n1 2 3\n2 8\n1 4 5", "output": "2 3"}, {"input": "10 0", "output": "5 9"}]
| 1,200
|
["greedy", "implementation", "math"]
| 47
|
[{"input": "3 2\r\n2 1\r\n2 2\r\n", "output": "0 0"}, {"input": "9 3\r\n1 2 3\r\n2 8\r\n1 4 5\r\n", "output": "2 3"}, {"input": "10 0\r\n", "output": "5 9"}, {"input": "10 2\r\n1 1 2\r\n1 8 9\r\n", "output": "3 5"}, {"input": "9 3\r\n1 4 5\r\n1 1 2\r\n1 6 7\r\n", "output": "2 2"}, {"input": "7 2\r\n2 3\r\n1 5 6\r\n", "output": "2 3"}, {"input": "81 28\r\n1 77 78\r\n1 50 51\r\n2 9\r\n1 66 67\r\n1 12 13\r\n1 20 21\r\n1 28 29\r\n1 34 35\r\n1 54 55\r\n2 19\r\n1 70 71\r\n1 45 46\r\n1 36 37\r\n2 47\r\n2 7\r\n2 76\r\n2 6\r\n2 31\r\n1 16 17\r\n1 4 5\r\n1 73 74\r\n1 64 65\r\n2 62\r\n2 22\r\n2 1\r\n1 48 49\r\n2 24\r\n2 40\r\n", "output": "22 36"}, {"input": "12 8\r\n1 4 5\r\n1 9 10\r\n2 3\r\n1 6 7\r\n2 1\r\n2 2\r\n2 8\r\n2 11\r\n", "output": "0 0"}, {"input": "54 25\r\n1 40 41\r\n2 46\r\n2 32\r\n2 8\r\n1 51 52\r\n2 39\r\n1 30 31\r\n2 53\r\n1 33 34\r\n1 42 43\r\n1 17 18\r\n1 21 22\r\n1 44 45\r\n2 50\r\n2 49\r\n2 15\r\n1 3 4\r\n1 27 28\r\n1 19 20\r\n1 47 48\r\n2 13\r\n1 37 38\r\n1 6 7\r\n2 35\r\n2 26\r\n", "output": "10 14"}, {"input": "90 35\r\n2 83\r\n2 86\r\n2 46\r\n1 61 62\r\n2 11\r\n1 76 77\r\n2 37\r\n2 9\r\n1 18 19\r\n2 79\r\n1 35 36\r\n1 3 4\r\n2 78\r\n2 72\r\n1 44 45\r\n2 31\r\n2 38\r\n2 65\r\n1 32 33\r\n1 13 14\r\n2 75\r\n2 42\r\n2 51\r\n2 80\r\n2 29\r\n1 22 23\r\n1 5 6\r\n2 53\r\n1 7 8\r\n1 24 25\r\n1 54 55\r\n2 84\r\n1 27 28\r\n2 26\r\n2 12\r\n", "output": "25 40"}, {"input": "98 47\r\n1 48 49\r\n2 47\r\n1 25 26\r\n2 29\r\n1 38 39\r\n1 20 21\r\n2 75\r\n2 68\r\n2 95\r\n2 6\r\n1 1 2\r\n1 84 85\r\n2 66\r\n1 88 89\r\n2 19\r\n2 32\r\n1 93 94\r\n1 45 46\r\n2 50\r\n1 15 16\r\n1 63 64\r\n1 23 24\r\n1 53 54\r\n1 43 44\r\n2 97\r\n1 12 13\r\n2 86\r\n2 74\r\n2 42\r\n1 40 41\r\n1 30 31\r\n1 34 35\r\n1 27 28\r\n2 81\r\n1 8 9\r\n2 73\r\n1 70 71\r\n2 67\r\n2 60\r\n2 72\r\n1 76 77\r\n1 90 91\r\n1 17 18\r\n2 11\r\n1 82 83\r\n1 58 59\r\n2 55\r\n", "output": "18 24"}, {"input": "56 34\r\n2 22\r\n2 27\r\n1 18 19\r\n1 38 39\r\n2 49\r\n1 10 11\r\n1 14 15\r\n2 40\r\n2 34\r\n1 32 33\r\n2 17\r\n1 24 25\r\n2 23\r\n2 52\r\n1 45 46\r\n2 28\r\n2 7\r\n1 4 5\r\n1 30 31\r\n2 21\r\n2 6\r\n1 47 48\r\n1 43 44\r\n1 54 55\r\n2 13\r\n1 8 9\r\n1 2 3\r\n2 41\r\n1 35 36\r\n1 50 51\r\n2 1\r\n2 29\r\n2 16\r\n2 53\r\n", "output": "5 5"}, {"input": "43 27\r\n1 40 41\r\n1 2 3\r\n1 32 33\r\n1 35 36\r\n1 27 28\r\n1 30 31\r\n1 7 8\r\n2 11\r\n1 5 6\r\n2 1\r\n1 15 16\r\n1 38 39\r\n2 12\r\n1 20 21\r\n1 22 23\r\n1 24 25\r\n1 9 10\r\n2 26\r\n2 14\r\n1 18 19\r\n2 17\r\n2 4\r\n2 34\r\n2 37\r\n2 29\r\n2 42\r\n2 13\r\n", "output": "0 0"}, {"input": "21 13\r\n1 6 7\r\n2 12\r\n1 8 9\r\n2 19\r\n1 4 5\r\n1 17 18\r\n2 3\r\n2 20\r\n1 10 11\r\n2 14\r\n1 15 16\r\n1 1 2\r\n2 13\r\n", "output": "0 0"}, {"input": "66 1\r\n1 50 51\r\n", "output": "32 63"}, {"input": "62 21\r\n2 34\r\n1 39 40\r\n1 52 53\r\n1 35 36\r\n2 27\r\n1 56 57\r\n2 43\r\n1 7 8\r\n2 28\r\n1 44 45\r\n1 41 42\r\n1 32 33\r\n2 58\r\n1 47 48\r\n2 10\r\n1 21 22\r\n2 51\r\n1 15 16\r\n1 19 20\r\n1 3 4\r\n2 25\r\n", "output": "16 27"}, {"input": "83 56\r\n2 24\r\n2 30\r\n1 76 77\r\n1 26 27\r\n1 73 74\r\n1 52 53\r\n2 82\r\n1 36 37\r\n2 13\r\n2 4\r\n2 68\r\n1 31 32\r\n1 65 66\r\n1 16 17\r\n1 56 57\r\n2 60\r\n1 44 45\r\n1 80 81\r\n1 28 29\r\n2 23\r\n1 54 55\r\n2 9\r\n2 1\r\n1 34 35\r\n2 5\r\n1 78 79\r\n2 40\r\n2 42\r\n1 61 62\r\n2 49\r\n2 22\r\n2 25\r\n1 7 8\r\n1 20 21\r\n1 38 39\r\n2 43\r\n2 12\r\n1 46 47\r\n2 70\r\n1 71 72\r\n2 3\r\n1 10 11\r\n2 75\r\n2 59\r\n1 18 19\r\n2 69\r\n2 48\r\n1 63 64\r\n2 33\r\n1 14 15\r\n1 50 51\r\n2 6\r\n2 41\r\n2 2\r\n2 67\r\n2 58\r\n", "output": "0 0"}, {"input": "229 27\r\n2 7\r\n1 64 65\r\n1 12 13\r\n2 110\r\n1 145 146\r\n2 92\r\n2 28\r\n2 39\r\n1 16 17\r\n2 164\r\n2 137\r\n1 95 96\r\n2 125\r\n1 48 49\r\n1 115 116\r\n1 198 199\r\n1 148 149\r\n1 225 226\r\n1 1 2\r\n2 24\r\n2 103\r\n1 87 88\r\n2 124\r\n2 89\r\n1 178 179\r\n1 160 161\r\n2 184\r\n", "output": "98 187"}, {"input": "293 49\r\n2 286\r\n2 66\r\n2 98\r\n1 237 238\r\n1 136 137\r\n1 275 276\r\n2 152\r\n1 36 37\r\n2 26\r\n2 40\r\n2 79\r\n2 274\r\n1 205 206\r\n1 141 142\r\n1 243 244\r\n2 201\r\n1 12 13\r\n1 123 124\r\n1 165 166\r\n1 6 7\r\n2 64\r\n1 22 23\r\n2 120\r\n1 138 139\r\n1 50 51\r\n2 15\r\n2 67\r\n2 45\r\n1 288 289\r\n1 261 262\r\n1 103 104\r\n2 249\r\n2 32\r\n2 153\r\n2 248\r\n1 162 163\r\n2 89\r\n1 94 95\r\n2 21\r\n1 48 49\r\n1 56 57\r\n2 102\r\n1 271 272\r\n2 269\r\n1 232 233\r\n1 70 71\r\n1 42 43\r\n1 267 268\r\n2 292\r\n", "output": "121 217"}, {"input": "181 57\r\n1 10 11\r\n1 4 5\r\n1 170 171\r\n2 86\r\n2 97\r\n1 91 92\r\n2 162\r\n2 115\r\n1 98 99\r\n2 134\r\n1 100 101\r\n2 168\r\n1 113 114\r\n1 37 38\r\n2 81\r\n2 169\r\n1 173 174\r\n1 165 166\r\n2 108\r\n2 121\r\n1 31 32\r\n2 67\r\n2 13\r\n2 50\r\n2 157\r\n1 27 28\r\n1 19 20\r\n2 109\r\n1 104 105\r\n2 46\r\n1 126 127\r\n1 102 103\r\n2 158\r\n2 133\r\n2 93\r\n2 68\r\n1 70 71\r\n2 125\r\n2 36\r\n1 48 49\r\n2 117\r\n1 131 132\r\n2 79\r\n2 23\r\n1 75 76\r\n2 107\r\n2 138\r\n1 94 95\r\n2 54\r\n1 87 88\r\n2 41\r\n1 153 154\r\n1 14 15\r\n2 60\r\n2 148\r\n1 159 160\r\n2 58\r\n", "output": "61 98"}, {"input": "432 5\r\n1 130 131\r\n2 108\r\n1 76 77\r\n1 147 148\r\n2 137\r\n", "output": "214 423"}, {"input": "125 45\r\n2 70\r\n2 111\r\n2 52\r\n2 3\r\n2 97\r\n2 104\r\n1 47 48\r\n2 44\r\n2 88\r\n1 117 118\r\n2 82\r\n1 22 23\r\n1 53 54\r\n1 38 39\r\n1 114 115\r\n2 93\r\n2 113\r\n2 102\r\n2 30\r\n2 95\r\n2 36\r\n2 73\r\n2 51\r\n2 87\r\n1 15 16\r\n2 55\r\n2 80\r\n2 121\r\n2 26\r\n1 31 32\r\n1 105 106\r\n1 1 2\r\n1 10 11\r\n2 91\r\n1 78 79\r\n1 7 8\r\n2 120\r\n2 75\r\n1 45 46\r\n2 94\r\n2 72\r\n2 25\r\n1 34 35\r\n1 17 18\r\n1 20 21\r\n", "output": "40 62"}, {"input": "48 35\r\n1 17 18\r\n2 20\r\n1 7 8\r\n2 13\r\n1 1 2\r\n2 23\r\n1 25 26\r\n1 14 15\r\n2 3\r\n1 45 46\r\n1 35 36\r\n2 47\r\n1 27 28\r\n2 30\r\n1 5 6\r\n2 11\r\n2 9\r\n1 32 33\r\n2 19\r\n2 24\r\n2 16\r\n1 42 43\r\n1 21 22\r\n2 37\r\n2 34\r\n2 40\r\n2 31\r\n2 10\r\n2 44\r\n2 39\r\n2 12\r\n2 29\r\n2 38\r\n2 4\r\n2 41\r\n", "output": "0 0"}, {"input": "203 55\r\n2 81\r\n2 65\r\n1 38 39\r\n1 121 122\r\n2 48\r\n2 83\r\n1 23 24\r\n2 165\r\n1 132 133\r\n1 143 144\r\n2 35\r\n2 85\r\n2 187\r\n1 19 20\r\n2 137\r\n2 150\r\n2 202\r\n2 156\r\n2 178\r\n1 93 94\r\n2 73\r\n2 167\r\n1 56 57\r\n1 100 101\r\n1 26 27\r\n1 51 52\r\n2 74\r\n2 4\r\n2 79\r\n2 113\r\n1 181 182\r\n2 75\r\n2 157\r\n2 25\r\n2 124\r\n1 68 69\r\n1 135 136\r\n1 110 111\r\n1 153 154\r\n2 123\r\n2 134\r\n1 36 37\r\n1 145 146\r\n1 141 142\r\n1 86 87\r\n2 10\r\n1 5 6\r\n2 131\r\n2 116\r\n2 70\r\n1 95 96\r\n1 174 175\r\n2 108\r\n1 91 92\r\n2 152\r\n", "output": "71 123"}, {"input": "51 38\r\n2 48\r\n2 42\r\n2 20\r\n2 4\r\n2 37\r\n2 22\r\n2 9\r\n2 13\r\n1 44 45\r\n1 33 34\r\n2 8\r\n1 18 19\r\n1 2 3\r\n2 27\r\n1 5 6\r\n1 40 41\r\n1 24 25\r\n2 16\r\n2 39\r\n2 50\r\n1 31 32\r\n1 46 47\r\n2 15\r\n1 29 30\r\n1 10 11\r\n2 49\r\n2 14\r\n1 35 36\r\n2 23\r\n2 7\r\n2 38\r\n2 26\r\n2 1\r\n2 17\r\n2 43\r\n2 21\r\n2 12\r\n2 28\r\n", "output": "0 0"}, {"input": "401 40\r\n1 104 105\r\n2 368\r\n1 350 351\r\n1 107 108\r\n1 4 5\r\n1 143 144\r\n2 369\r\n1 337 338\r\n2 360\r\n2 384\r\n2 145\r\n1 102 103\r\n1 88 89\r\n1 179 180\r\n2 202\r\n1 234 235\r\n2 154\r\n1 9 10\r\n1 113 114\r\n2 398\r\n1 46 47\r\n1 35 36\r\n1 174 175\r\n1 273 274\r\n1 237 238\r\n2 209\r\n1 138 139\r\n1 33 34\r\n1 243 244\r\n1 266 267\r\n1 294 295\r\n2 219\r\n2 75\r\n2 340\r\n1 260 261\r\n1 245 246\r\n2 210\r\n1 221 222\r\n1 328 329\r\n1 164 165\r\n", "output": "177 333"}, {"input": "24 16\r\n1 16 17\r\n1 1 2\r\n1 8 9\r\n1 18 19\r\n1 22 23\r\n1 13 14\r\n2 15\r\n2 6\r\n2 11\r\n2 20\r\n2 3\r\n1 4 5\r\n2 10\r\n2 7\r\n2 21\r\n2 12\r\n", "output": "0 0"}, {"input": "137 37\r\n2 108\r\n1 55 56\r\n2 20\r\n1 33 34\r\n2 112\r\n2 48\r\n2 120\r\n2 38\r\n2 74\r\n2 119\r\n2 27\r\n1 13 14\r\n2 8\r\n1 88 89\r\n1 44 45\r\n2 124\r\n2 76\r\n2 123\r\n2 104\r\n1 58 59\r\n2 52\r\n2 47\r\n1 3 4\r\n1 65 66\r\n2 28\r\n1 102 103\r\n2 81\r\n2 86\r\n2 116\r\n1 69 70\r\n1 11 12\r\n2 84\r\n1 25 26\r\n2 100\r\n2 90\r\n2 83\r\n1 95 96\r\n", "output": "52 86"}, {"input": "1155 50\r\n1 636 637\r\n1 448 449\r\n2 631\r\n2 247\r\n1 1049 1050\r\n1 1103 1104\r\n1 816 817\r\n1 1127 1128\r\n2 441\r\n2 982\r\n1 863 864\r\n2 186\r\n1 774 775\r\n2 793\r\n2 173\r\n2 800\r\n1 952 953\r\n1 492 493\r\n1 796 797\r\n2 907\r\n2 856\r\n2 786\r\n2 921\r\n1 558 559\r\n2 1090\r\n1 307 308\r\n1 1152 1153\r\n1 578 579\r\n1 944 945\r\n1 707 708\r\n2 968\r\n1 1005 1006\r\n1 1100 1101\r\n2 402\r\n1 917 918\r\n1 237 238\r\n1 191 192\r\n2 460\r\n1 1010 1011\r\n2 960\r\n1 1018 1019\r\n2 296\r\n1 958 959\r\n2 650\r\n2 395\r\n1 1124 1125\r\n2 539\r\n2 152\r\n1 385 386\r\n2 464\r\n", "output": "548 1077"}, {"input": "1122 54\r\n2 1031\r\n1 363 364\r\n1 14 15\r\n1 902 903\r\n1 1052 1053\r\n2 170\r\n2 36\r\n2 194\r\n1 340 341\r\n1 1018 1019\r\n1 670 671\r\n1 558 559\r\n2 431\r\n2 351\r\n2 201\r\n1 1104 1105\r\n2 1056\r\n2 823\r\n1 274 275\r\n2 980\r\n1 542 543\r\n1 807 808\r\n2 157\r\n2 895\r\n1 505 506\r\n2 658\r\n1 484 485\r\n1 533 534\r\n1 384 385\r\n2 779\r\n2 888\r\n1 137 138\r\n1 198 199\r\n2 762\r\n1 451 452\r\n1 248 249\r\n2 294\r\n2 123\r\n2 948\r\n2 1024\r\n2 771\r\n2 922\r\n1 566 567\r\n1 707 708\r\n1 1037 1038\r\n2 63\r\n1 208 209\r\n1 738 739\r\n2 648\r\n1 491 492\r\n1 440 441\r\n2 651\r\n1 971 972\r\n1 93 94\r\n", "output": "532 1038"}, {"input": "2938 48\r\n2 1519\r\n2 1828\r\n1 252 253\r\n1 2275 2276\r\n1 1479 1480\r\n2 751\r\n2 972\r\n2 175\r\n2 255\r\n1 1837 1838\r\n1 1914 1915\r\n2 198\r\n1 1686 1687\r\n1 950 951\r\n2 61\r\n1 840 841\r\n2 277\r\n1 52 53\r\n1 76 77\r\n2 795\r\n2 1680\r\n1 2601 2602\r\n2 2286\r\n2 2188\r\n2 2521\r\n2 1166\r\n2 1171\r\n2 2421\r\n1 1297 1298\r\n1 1736 1737\r\n1 991 992\r\n1 1048 1049\r\n2 756\r\n2 2054\r\n1 2878 2879\r\n1 1445 1446\r\n1 2539 2540\r\n2 1334\r\n2 2233\r\n2 494\r\n2 506\r\n1 1942 1943\r\n2 2617\r\n1 1991 1992\r\n2 1501\r\n1 2488 2489\r\n1 752 753\r\n2 2623\r\n", "output": "1444 2867"}, {"input": "2698 39\r\n2 710\r\n1 260 261\r\n2 174\r\n2 1697\r\n2 915\r\n1 2029 2030\r\n2 916\r\n2 2419\r\n2 323\r\n1 2130 2131\r\n2 1350\r\n1 64 65\r\n1 763 764\r\n1 939 940\r\n2 1693\r\n2 659\r\n2 2281\r\n2 761\r\n2 909\r\n1 1873 1874\r\n1 1164 1165\r\n2 2308\r\n2 504\r\n1 1035 1036\r\n1 2271 2272\r\n1 1085 1086\r\n1 1757 1758\r\n2 1818\r\n1 1604 1605\r\n1 517 518\r\n1 2206 2207\r\n2 636\r\n1 519 520\r\n2 1928\r\n1 1894 1895\r\n2 573\r\n2 2313\r\n1 42 43\r\n2 1529\r\n", "output": "1327 2640"}, {"input": "3999 0\r\n", "output": "1999 3998"}, {"input": "1 0\r\n", "output": "0 0"}, {"input": "10 5\r\n1 1 2\r\n2 3\r\n2 8\r\n1 4 5\r\n1 6 7\r\n", "output": "1 1"}, {"input": "4000 0\r\n", "output": "2000 3999"}]
| false
|
stdio
| null | true
|
46/C
|
46
|
C
|
PyPy 3-64
|
TESTS
| 7
| 216
| 0
|
141725107
|
n = int(input())
s = list(str(input()))
inverses = 0
for i in range(n):
if s[i] != s[(i + 1) % n]:
inverses += 1
print((inverses + 1) // 4)
| 27
| 92
| 0
|
218371186
|
def min_swaps_for_arrangement(n, arrangement):
num_hamsters = arrangement.count('H')
min_swaps = float('inf')
window = arrangement[:num_hamsters]
num_tigers_in_window = window.count('T')
min_swaps = min(min_swaps, num_tigers_in_window)
for i in range(1, n):
if arrangement[i - 1] == 'T':
num_tigers_in_window -= 1
if arrangement[(i + num_hamsters - 1) % n] == 'T':
num_tigers_in_window += 1
min_swaps = min(min_swaps, num_tigers_in_window)
return min_swaps
# Read input
n = int(input())
arrangement = input()
# Calculate and print the result
result = min_swaps_for_arrangement(n, arrangement)
print(result)
#bhbhf
|
School Personal Contest #2 (Winter Computer School 2010/11) - Codeforces Beta Round 43 (ACM-ICPC Rules)
|
ICPC
| 2,010
| 2
| 256
|
Hamsters and Tigers
|
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
|
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
|
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
| null |
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
|
[{"input": "3\nHTH", "output": "0"}, {"input": "9\nHTHTHTHHT", "output": "2"}]
| 1,600
|
["two pointers"]
| 27
|
[{"input": "3\r\nHTH\r\n", "output": "0\r\n"}, {"input": "9\r\nHTHTHTHHT\r\n", "output": "2\r\n"}, {"input": "2\r\nTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTHT\r\n", "output": "1\r\n"}, {"input": "7\r\nTTTHTTT\r\n", "output": "0\r\n"}, {"input": "8\r\nHHTHHTHH\r\n", "output": "1\r\n"}, {"input": "13\r\nHTTTHHHTTTTHH\r\n", "output": "3\r\n"}, {"input": "20\r\nTTHTHTHHTHTTHHTTTHHH\r\n", "output": "4\r\n"}, {"input": "35\r\nTTTTTTHTTHTTTTTHTTTTTTTTTTTHTHTTTTT\r\n", "output": "3\r\n"}, {"input": "120\r\nTTTTTTTHTHTHTTTTTHTHTTTTHTTTTTTTTTTTTTTTTTTTTHTTHTTTTHTTHTTTTTTTTTTTTTTTHTTTTTTHTHTTHTTTTTTHTTTTTTTTTHTTHTTTTHTTTHTTTTTH\r\n", "output": "14\r\n"}, {"input": "19\r\nHHHHHHHHHHHHHTTTHHH\r\n", "output": "0\r\n"}, {"input": "87\r\nHTHHTTHHHHTHHHHHTTTHHTHHHHTTTTHHHTTHHTHTHTHHTTHTHHTHTHTTHHHTTTTTHTTHHHHHHTHHTHHTHTTHTHH\r\n", "output": "17\r\n"}, {"input": "178\r\nTHHHTHTTTHTTHTTHHHHHTTTHTTHHTHTTTHTHTTTTTHHHTHTHHHTHHHTTTTTTTTHHHHTTHHTHHHHTHTTTHHHHHHTHHTHTTHTHTTTTTTTTTHHTTHHTHTTHHTHHHHHTTHHTTHHTTHHHTTHHTTTTHTHHHTHTTHTHTTTHHHHTHHTHHHTHTTTTTT\r\n", "output": "40\r\n"}]
| false
|
stdio
| null | true
|
46/C
|
46
|
C
|
PyPy 3
|
TESTS
| 4
| 312
| 20,172,800
|
88392825
|
n = int(input())
s = input()
H = s.count("H")
mn = int(10e3)
for i in range(n-H):
cnt = s[i:i+H-1].count("T")
mn = min(cnt,mn)
print(mn)
| 27
| 92
| 0
|
218382700
|
n=int(input())
s=input()
h=s.count('H')
print (min((s+s)[i:i+h].count('T') for i in range(n)))
|
School Personal Contest #2 (Winter Computer School 2010/11) - Codeforces Beta Round 43 (ACM-ICPC Rules)
|
ICPC
| 2,010
| 2
| 256
|
Hamsters and Tigers
|
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
|
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
|
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
| null |
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
|
[{"input": "3\nHTH", "output": "0"}, {"input": "9\nHTHTHTHHT", "output": "2"}]
| 1,600
|
["two pointers"]
| 27
|
[{"input": "3\r\nHTH\r\n", "output": "0\r\n"}, {"input": "9\r\nHTHTHTHHT\r\n", "output": "2\r\n"}, {"input": "2\r\nTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTHT\r\n", "output": "1\r\n"}, {"input": "7\r\nTTTHTTT\r\n", "output": "0\r\n"}, {"input": "8\r\nHHTHHTHH\r\n", "output": "1\r\n"}, {"input": "13\r\nHTTTHHHTTTTHH\r\n", "output": "3\r\n"}, {"input": "20\r\nTTHTHTHHTHTTHHTTTHHH\r\n", "output": "4\r\n"}, {"input": "35\r\nTTTTTTHTTHTTTTTHTTTTTTTTTTTHTHTTTTT\r\n", "output": "3\r\n"}, {"input": "120\r\nTTTTTTTHTHTHTTTTTHTHTTTTHTTTTTTTTTTTTTTTTTTTTHTTHTTTTHTTHTTTTTTTTTTTTTTTHTTTTTTHTHTTHTTTTTTHTTTTTTTTTHTTHTTTTHTTTHTTTTTH\r\n", "output": "14\r\n"}, {"input": "19\r\nHHHHHHHHHHHHHTTTHHH\r\n", "output": "0\r\n"}, {"input": "87\r\nHTHHTTHHHHTHHHHHTTTHHTHHHHTTTTHHHTTHHTHTHTHHTTHTHHTHTHTTHHHTTTTTHTTHHHHHHTHHTHHTHTTHTHH\r\n", "output": "17\r\n"}, {"input": "178\r\nTHHHTHTTTHTTHTTHHHHHTTTHTTHHTHTTTHTHTTTTTHHHTHTHHHTHHHTTTTTTTTHHHHTTHHTHHHHTHTTTHHHHHHTHHTHTTHTHTTTTTTTTTHHTTHHTHTTHHTHHHHHTTHHTTHHTTHHHTTHHTTTTHTHHHTHTTHTHTTTHHHHTHHTHHHTHTTTTTT\r\n", "output": "40\r\n"}]
| false
|
stdio
| null | true
|
61/B
|
61
|
B
|
Python 3
|
TESTS
| 32
| 62
| 102,400
|
138348237
|
import collections
s1=input()
s2=input()
s3=input()
l2=[]
s1=s1.replace(";","")
s1=s1.replace("-","")
s1=s1.replace("_","")
s2=s2.replace(";","")
s2=s2.replace("-","")
s2=s2.replace("_","")
s3=s3.replace(";","")
s3=s3.replace("-","")
s3=s3.replace("_","")
s1=s1.lower()
s2=s2.lower()
s3=s3.lower()
l2.append(s1)
l2.append(s2)
l2.append(s3)
l2=list(set(l2))
q=''.join(l2)
n=int(input())
for i in range(n):
l1=[]
s=input()
s=s.replace(";","")
s=s.replace("-","")
s=s.replace("_","")
s=s.lower()
l1.append(s)
q2=''.join(l1)
l1.clear()
if q2.find(s1)!=-1 and q2.find(s2)!=-1 and q2.find(s3)!=-1 and len(q)==len(q2):
print("ACC")
else:
print("WA")
| 43
| 46
| 512,000
|
152405590
|
def no(a):
return ''.join([x for x in a.lower() if (x not in ";-_")])
a=no(input())
b=no(input())
c=no(input())
n=int(input())
opt=[a+b+c,a+c+b,b+c+a,b+a+c,c+b+a,c+a+b]
for i in range(0,n):
k=no(input())
print("ACC" if k in opt else "WA")
|
Codeforces Beta Round 57 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Hard Work
|
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
- As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
- As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
- The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
- My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
- Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
- You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
- I should remind you that none of the strings (initial strings or answers) are empty.
- Finally, do these as soon as possible. You have less than 2 hours to complete this.
|
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 ≤ n ≤ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
|
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
| null | null |
[{"input": "Iran_\nPersian;\nW_o;n;d;e;r;f;u;l;\n7\nWonderfulPersianIran\nwonderful_PersIAN_IRAN;;_\nWONDERFUL___IRAN__PERSIAN__;;\nIra__Persiann__Wonderful\nWonder;;fulPersian___;I;r;a;n;\n__________IranPersianWonderful__________\nPersianIran_is_Wonderful", "output": "ACC\nACC\nACC\nWA\nACC\nACC\nWA"}, {"input": "Shapur;;\nis___\na_genius\n3\nShapur__a_is___geniUs\nis___shapur___a__Genius;\nShapur;;is;;a;;geni;;us;;", "output": "WA\nACC\nACC"}]
| 1,300
|
["strings"]
| 43
|
[{"input": "Iran_\r\nPersian;\r\nW_o;n;d;e;r;f;u;l;\r\n7\r\nWonderfulPersianIran\r\nwonderful_PersIAN_IRAN;;_\r\nWONDERFUL___IRAN__PERSIAN__;;\r\nIra__Persiann__Wonderful\r\nWonder;;fulPersian___;I;r;a;n;\r\n__________IranPersianWonderful__________\r\nPersianIran_is_Wonderful\r\n", "output": "ACC\r\nACC\r\nACC\r\nWA\r\nACC\r\nACC\r\nWA\r\n"}, {"input": "Shapur;;\r\nis___\r\na_genius\r\n3\r\nShapur__a_is___geniUs\r\nis___shapur___a__Genius;\r\nShapur;;is;;a;;geni;;us;;\r\n", "output": "WA\r\nACC\r\nACC\r\n"}, {"input": "rr\r\nrrx\r\nab\r\n1\r\nrabrrrx\r\n", "output": "WA\r\n"}, {"input": "AB\r\nBC\r\nCD\r\n1\r\nABCDZZ\r\n", "output": "WA\r\n"}, {"input": "aa\r\naaa\r\nz\r\n1\r\naazaaa\r\n", "output": "ACC\r\n"}, {"input": "aa\r\naaa\r\nz\r\n1\r\naaazaa\r\n", "output": "ACC\r\n"}, {"input": "as\r\nav\r\nax\r\n1\r\n-------\r\n", "output": "WA\r\n"}, {"input": "a\r\nab\r\nb\r\n1\r\nabcd\r\n", "output": "WA\r\n"}, {"input": "c\r\naba\r\ncc\r\n2\r\nccabac\r\nabcacc\r\n", "output": "ACC\r\nWA\r\n"}, {"input": "ab\r\na\r\nb\r\n1\r\nabcd\r\n", "output": "WA\r\n"}, {"input": "ACB\r\nTB\r\nAC\r\n1\r\nATBACBC\r\n", "output": "WA\r\n"}, {"input": "cc\r\naba\r\ncc\r\n1\r\nccabaxx\r\n", "output": "WA\r\n"}, {"input": "a\r\na\r\na\r\n0\r\n", "output": ""}]
| false
|
stdio
| null | true
|
621/C
|
621
|
C
|
PyPy 3-64
|
TESTS
| 3
| 62
| 0
|
229615189
|
n,p=map(int,input().split())
tot=[]
div=[]
while n>0:
l,r=map(int,input().split())
tot.append(r-l+1)
div.append(int(r/p)-int((l-1)/p))
n-=1
sz=len(div)
ans=0
for i in range(sz):
vl=((tot[i]-div[i])/tot[i])
j=i+1
j%=sz
vl*=((tot[j]-div[j])/tot[j])
ans+=(1-vl)*3000
print(2*ans/sz)
| 94
| 124
| 9,113,600
|
212066500
|
from sys import stdin ,stdout
input=stdin.readline
inp = lambda : map(int,input().split())
def print(*args, end='\n', sep=' ') -> None:
stdout.write(sep.join(map(str, args)) + end)
n , p = inp() ; arr=[] ; ans= 0
for i in range(n) :
l , r = inp()
arr.append( 1- (r//p - (l-1)//p) / (r-l+1))
for i in range(n) :
a= i ; b= (i+1)%n
ans += 1- arr[a] * arr[b]
print("{:.6f}".format(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
|
794/C
|
794
|
C
|
Python 3
|
TESTS
| 6
| 31
| 0
|
189399821
|
# modified from original but this sol is still wrong
s1 = input().strip()
s2 = input().strip()
c = len(s1)
k = ''
a = True
while len(k) < c:
if a:
k += min(s1)
s1 = s1.replace(min(s1), '', 1)
a = False
continue
k += max(s2)
s2 = s2.replace(max(s2), '', 1)
a = True
s = ''
for i in range(c//2):
s += k[i*2+1] + k[i*2]
if c % 2 == 1:
s += k[-1]
if k > s:
print(s)
else:
print(k)
| 53
| 358
| 6,348,800
|
27083146
|
import sys
def main():
s=sys.stdin.readline().rstrip()
t=sys.stdin.readline().rstrip()
result=[]
iisl=0
iisr=len(s)//2+len(s)%2-1
iitl=0
iitr=len(s)//2-1
aas=list(sorted(list(s)))
aat=list(sorted(list(t),reverse=True))
rl=0
rr=len(s)-1
result=['?']*len(s)
for i in range(len(s)):
if i%2==0:
if aas[iisl]<aat[iitl]:
result[rl]=aas[iisl]
iisl+=1
rl+=1
else:
result[rr]=aas[iisr]
iisr-=1
rr-=1
else:
if aat[iitl]>aas[iisl]:
result[rl]=aat[iitl]
iitl+=1
rl+=1
else:
result[rr]=aat[iitr]
iitr-=1
rr-=1
sys.stdout.write(''.join(result)+'\n')
main()
|
Tinkoff Challenge - Final Round (Codeforces Round 414, rated, Div. 1 + Div. 2)
|
CF
| 2,017
| 2
| 256
|
Naming Company
|
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
|
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
|
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
| null |
One way to play optimally in the first sample is as follows :
- Initially, the company name is ???????.
- Oleg replaces the first question mark with 'f'. The company name becomes f??????.
- Igor replaces the second question mark with 'z'. The company name becomes fz?????.
- Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
- Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
- Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
- Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
- Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx.
|
[{"input": "tinkoff\nzscoder", "output": "fzfsirk"}, {"input": "xxxxxx\nxxxxxx", "output": "xxxxxx"}, {"input": "ioi\nimo", "output": "ioi"}]
| 1,800
|
["games", "greedy", "sortings"]
| 53
|
[{"input": "tinkoff\r\nzscoder\r\n", "output": "fzfsirk\r\n"}, {"input": "xxxxxx\r\nxxxxxx\r\n", "output": "xxxxxx\r\n"}, {"input": "ioi\r\nimo\r\n", "output": "ioi\r\n"}, {"input": "abc\r\naaa\r\n", "output": "aab\r\n"}, {"input": "reddit\r\nabcdef\r\n", "output": "dfdeed\r\n"}, {"input": "cbxz\r\naaaa\r\n", "output": "abac\r\n"}, {"input": "bcdef\r\nabbbc\r\n", "output": "bccdb\r\n"}, {"input": "z\r\ny\r\n", "output": "z\r\n"}, {"input": "y\r\nz\r\n", "output": "y\r\n"}]
| false
|
stdio
| null | true
|
794/C
|
794
|
C
|
Python 3
|
PRETESTS
| 6
| 62
| 204,800
|
27082911
|
a = list(input())
b = list(input())
n = len(a)
a.sort()
b.sort()
a = a[:(len(a) + 1) // 2]
b = b[len(b) // 2:]
sa = 0
ea = len(a) - 1
sb = 0
eb = len(b) - 1
stb = 0
ste = n - 1
st = [""] * n
for i in range(n):
if i % 2 == 0:
if a[sa] < b[eb]:
st[stb] = a[sa]
sa += 1
stb += 1
else:
st[ste] = a[ea]
ea -= 1
ste -= 1
else:
if eb == sb:
st[stb] = b[eb]
break
if b[eb] > a[sa]:
st[stb] = b[eb]
eb -= 1
stb += 1
else:
st[ste] = b[sb]
ste -= 1
sb += 1
for i in range(len(st)):
print(st[i], end="")
| 53
| 358
| 7,884,800
|
179281758
|
'''from random import *
test = 10
for i in range(test):
new_in = open('0' * (3 - len(str(i + 1))) + str(i + 1) + ".in", 'w')
new_out = open('0' * (3 - len(str(i + 1))) + str(i + 1) + ".out", 'w')
n = randint(1, 10)'''
a = [x for x in input()]
b = [x for x in input()]
p = [''] * 300003
n = len(a)
a.sort()
b.sort(reverse=True)
'''for i in range(n):
a.append(chr(randint(0, 25) + 97))
b.append(chr(randint(0, 25) + 97))
'''
x = 0
y = (n+1) // 2
r = n
u = 0
v = n // 2
l = 0
for ii in range(n):
if (ii % 2):
if a[x] < b[u]:
p[l] = b[u]
l, u = l + 1, u + 1
else:
r -= 1
v -= 1
p[r] = b[v]
else:
if a[x] < b[u]:
p[l] = a[x]
l += 1
x += 1
else:
r -= 1
y -= 1
p[r] = a[y]
'''new_out.write(''.join([p[jj] for jj in range(n)]))
new_in.write(str(''.join(a)) + '\n' + str(''.join(b)))
new_in.close()
new_out.close()'''
print(''.join([p[jj] for jj in range(n)]))
|
Tinkoff Challenge - Final Round (Codeforces Round 414, rated, Div. 1 + Div. 2)
|
CF
| 2,017
| 2
| 256
|
Naming Company
|
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
|
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
|
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
| null |
One way to play optimally in the first sample is as follows :
- Initially, the company name is ???????.
- Oleg replaces the first question mark with 'f'. The company name becomes f??????.
- Igor replaces the second question mark with 'z'. The company name becomes fz?????.
- Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
- Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
- Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
- Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
- Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx.
|
[{"input": "tinkoff\nzscoder", "output": "fzfsirk"}, {"input": "xxxxxx\nxxxxxx", "output": "xxxxxx"}, {"input": "ioi\nimo", "output": "ioi"}]
| 1,800
|
["games", "greedy", "sortings"]
| 53
|
[{"input": "tinkoff\r\nzscoder\r\n", "output": "fzfsirk\r\n"}, {"input": "xxxxxx\r\nxxxxxx\r\n", "output": "xxxxxx\r\n"}, {"input": "ioi\r\nimo\r\n", "output": "ioi\r\n"}, {"input": "abc\r\naaa\r\n", "output": "aab\r\n"}, {"input": "reddit\r\nabcdef\r\n", "output": "dfdeed\r\n"}, {"input": "cbxz\r\naaaa\r\n", "output": "abac\r\n"}, {"input": "bcdef\r\nabbbc\r\n", "output": "bccdb\r\n"}, {"input": "z\r\ny\r\n", "output": "z\r\n"}, {"input": "y\r\nz\r\n", "output": "y\r\n"}]
| false
|
stdio
| null | true
|
543/D
|
543
|
D
|
PyPy 3-64
|
TESTS
| 9
| 795
| 46,489,600
|
214689817
|
# Problem: D. Road Improvement
# Contest: Codeforces - Codeforces Round 302 (Div. 1)
# URL: https://codeforces.com/problemset/problem/543/D
# Memory Limit: 256 MB
# Time Limit: 2000 ms
import sys
import random
from types import GeneratorType
import bisect
import io, os
from bisect import *
from collections import *
from contextlib import redirect_stdout
from itertools import *
from array import *
from functools import lru_cache, reduce
from heapq import *
from math import sqrt, gcd, inf
if sys.version >= '3.8': # ACW没有comb
from math import comb
RI = lambda: map(int, sys.stdin.buffer.readline().split())
RS = lambda: map(bytes.decode, sys.stdin.buffer.readline().strip().split())
RILST = lambda: list(RI())
DEBUG = lambda *x: sys.stderr.write(f'{str(x)}\n')
# print = lambda d: sys.stdout.write(str(d) + "\n") # 打开可以快写,但是无法使用print(*ans,sep=' ')这种语法,需要print(' '.join(map(str, p))),确实会快。
DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上
DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),
(-1, 1)] # →↘↓↙←↖↑↗
RANDOM = random.randrange(2 ** 62)
MOD = 10 ** 9 + 7
# MOD = 998244353
PROBLEM = """https://codeforces.com/problemset/problem/543/D
输入 n(2≤n≤2e5) 和 n-1 个数 p2,p3,...,pn,表示一棵 n 个节点的无根树,节点编号从 1 开始,i 与 pi(1≤pi≤i-1) 相连。
定义 a(x) 表示以 x 为根时的合法标记方案数,模 1e9+7。其中【合法标记】定义为:对树的某些边做标记,使得 x 到任意点的简单路径上,至多有一条边是被标记的。
输出 a(1),a(2),...,a(n)。
输入
3
1 1
输出
4 3 3
输入
5
1 2 3 4
输出
5 8 9 8 5
"""
"""换根 DP。不了解的同学请看右边的链接。
先来计算 a(1),此时 1 为树根。
定义 f(i) 表示子树 i 的合法标记方案数。
对于 i 的儿子 j,考虑 i-j 这条边是否标记:
- 标记:那么子树 j 的所有边都不能标记,方案数为 1。
- 不标记:那么方案数就是 f(j)。
i 的每个儿子互相独立,所以根据乘法原理有
f(i) = (f(j1)+1) * (f(j2)+1) * ... * (f(jm)+1)
其中 j1,j2,...,jm 是 i 的儿子。
然后来计算其余 a(i)。
考虑把根从 i 换到 j:
对于 j 来说,方案数需要在 f(j) 的基础上,再乘上【父亲 i】这棵子树的方案数,即 a(i) / (f(j)+1)。
所以 a(j) = f(j) * (a(i)/(f(j)+1) + 1)
本题的一个易错点是,f(j)+1 可能等于 M=1e9+7,取模会变成 0,但是 0 没有逆元。
处理方式有很多,我的做法是定义二元组 (k,x) 表示 M^k * x % M,在这基础上定义:
- 乘法运算:(k1, x1) * (k2, x2) = (k1+k2, x1*x2%M)
- 除法运算:(k1, x1) / (k2, x2) = (k1-k2, x1*inv(x2)%M) 这里 k1>=k2
- 加一运算:请读者思考(需要分类讨论,具体见代码)
当 k>0 时,(k,x) 的实际值为 0;当 k=0 时,(k,x) 的实际值为 x。
https://codeforces.com/contest/543/submission/214621886"""
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
# ms
def solve():
n, = RI()
a = RILST()
g = [[] for _ in range(n)]
for i, p in enumerate(a, start=1):
g[i].append(p - 1)
g[p - 1].append(i)
down = [1] * n # 每个节点向下子树至少画一下的方案数
@bootstrap
def dfs(u, fa):
for v in g[u]:
if v == fa: continue
yield dfs(v, u)
down[u] = down[u] * (1 + down[v]) % MOD
yield
dfs(0, -1)
# print(g)
ans = down[:]
@bootstrap
def reroot(u, fa):
for v in g[u]:
if v == fa: continue
# ans[v] = (ans[u] // (down[v] + 1) + 1) * down[v]
ans[v] = (ans[u] * pow(down[v] + 1, MOD - 2, MOD) + 1) * down[v]%MOD
yield reroot(v, u)
yield
reroot(0, -1)
print(' '.join(map(str, ans)))
if __name__ == '__main__':
t = 0
if t:
t, = RI()
for _ in range(t):
solve()
else:
solve()
| 42
| 451
| 54,988,800
|
214692343
|
# Problem: D. Road Improvement
# Contest: Codeforces - Codeforces Round 302 (Div. 1)
# URL: https://codeforces.com/problemset/problem/543/D
# Memory Limit: 256 MB
# Time Limit: 2000 ms
import sys
import random
from types import GeneratorType
import bisect
import io, os
from bisect import *
from collections import *
from contextlib import redirect_stdout
from itertools import *
from array import *
from functools import lru_cache, reduce
from heapq import *
from math import sqrt, gcd, inf
if sys.version >= '3.8': # ACW没有comb
from math import comb
RI = lambda: map(int, sys.stdin.buffer.readline().split())
RS = lambda: map(bytes.decode, sys.stdin.buffer.readline().strip().split())
RILST = lambda: list(RI())
DEBUG = lambda *x: sys.stderr.write(f'{str(x)}\n')
# print = lambda d: sys.stdout.write(str(d) + "\n") # 打开可以快写,但是无法使用print(*ans,sep=' ')这种语法,需要print(' '.join(map(str, p))),确实会快。
DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上
DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),
(-1, 1)] # →↘↓↙←↖↑↗
RANDOM = random.randrange(2 ** 62)
MOD = 10 ** 9 + 7
# MOD = 998244353
PROBLEM = """https://codeforces.com/problemset/problem/543/D
输入 n(2≤n≤2e5) 和 n-1 个数 p2,p3,...,pn,表示一棵 n 个节点的无根树,节点编号从 1 开始,i 与 pi(1≤pi≤i-1) 相连。
定义 a(x) 表示以 x 为根时的合法标记方案数,模 1e9+7。其中【合法标记】定义为:对树的某些边做标记,使得 x 到任意点的简单路径上,至多有一条边是被标记的。
输出 a(1),a(2),...,a(n)。
输入
3
1 1
输出
4 3 3
输入
5
1 2 3 4
输出
5 8 9 8 5
"""
"""换根 DP。不了解的同学请看右边的链接。
先来计算 a(1),此时 1 为树根。
定义 f(i) 表示子树 i 的合法标记方案数。
对于 i 的儿子 j,考虑 i-j 这条边是否标记:
- 标记:那么子树 j 的所有边都不能标记,方案数为 1。
- 不标记:那么方案数就是 f(j)。
i 的每个儿子互相独立,所以根据乘法原理有
f(i) = (f(j1)+1) * (f(j2)+1) * ... * (f(jm)+1)
其中 j1,j2,...,jm 是 i 的儿子。
然后来计算其余 a(i)。
考虑把根从 i 换到 j:
对于 j 来说,方案数需要在 f(j) 的基础上,再乘上【父亲 i】这棵子树的方案数,即 a(i) / (f(j)+1)。
所以 a(j) = f(j) * (a(i)/(f(j)+1) + 1)
本题的一个易错点是,f(j)+1 可能等于 M=1e9+7,取模会变成 0,但是 0 没有逆元。
处理方式有很多,我的做法是定义二元组 (k,x) 表示 M^k * x % M,在这基础上定义:
- 乘法运算:(k1, x1) * (k2, x2) = (k1+k2, x1*x2%M)
- 除法运算:(k1, x1) / (k2, x2) = (k1-k2, x1*inv(x2)%M) 这里 k1>=k2
- 加一运算:请读者思考(需要分类讨论,具体见代码)
当 k>0 时,(k,x) 的实际值为 0;当 k=0 时,(k,x) 的实际值为 x。
https://codeforces.com/contest/543/submission/214621886"""
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
# ms
def solve():
from typing import Callable, Generic, List, TypeVar
T = TypeVar("T")
E = Callable[[int], T]
"""identify element of op, and answer of leaf"""
Op = Callable[[T, T], T]
"""merge value of child node"""
Composition = Callable[[T, int, int, int], T]
"""return value from child node to parent node"""
class Rerooting(Generic[T]):
__slots__ = ("g", "_n", "_decrement", "_root", "_parent", "_order")
def __init__(self, n: int, decrement: int = 0, edges=None):
"""
n: 节点个数
decrement: 节点id可能需要偏移 (1-indexed则-1, 0-indexed则0)
"""
self.g = g = [[] for _ in range(n)]
self._n = n
self._decrement = decrement
self._root = None # 一开始的根
if edges:
for u, v in edges:
u -= decrement
v -= decrement
g[u].append(v)
g[v].append(u)
def add_edge(self, u: int, v: int):
"""
无向树加边
"""
u -= self._decrement
v -= self._decrement
self.g[u].append(v)
self.g[v].append(u)
def rerooting(
self, e: E["T"], op: Op["T"], composition: Composition["T"], root=0
) -> List["T"]:
"""
- e: 初始化每个节点的价值
(root) -> res
mergeの単位元
例:求最长路径 e=0
- op: 两个子树答案如何组合或取舍
(childRes1,childRes2) -> newRes
例:求最长路径 return max(childRes1,childRes2)
- composition: 知道子子树答案和节点值,如何更新子树答案
(from_res,fa,u,use_fa) -> new_res
use_fa: 0表示用u更新fa的dp1,1表示用fa更新u的dp2
例:最长路径return from_res+1
- root: 可能要设置初始根,默认是0
<概要> 换根DP模板,用线性时间获取以每个节点为根整颗树的情况。
注意最终返回的dp[u]代表以u为根时,u的所有子树的最优情况(不包括u节点本身),因此如果要整颗子树情况,还要再额外计算。
1. 记录dp1,dp2。其中:
dp1[u] 代表 以u为根的子树,它的孩子子树的最优值,即u节点本身不参与计算。注意,和我们一般定义的f[u]代表以u为根的子树2情况不同。
dp2[v] 代表 除了v以外,它的兄弟子树的最优值。依然注意,v不参与,同时u也不参与(u是v的父节点)。
建议画图理解。
2. dp2[v]的含义后边将进行一次变动,变更为v的兄弟、u的父过来的路径,merge上u节点本身最后得出来的值。即v以父亲为邻居向外延伸的最优值(不含v,但含父)。
3. 同时dp1[u]的含义更新为目标的含义:以u为根,u的子节点们所在子树的最优情况。
4. 这样dp1,dp2将分别代表u的向下子树的最优,u除了向下子树以外的最优(一定从父节点来,但父节点可能从兄弟来或祖宗来)
<步骤>
1. 先从任意root出发(一般是0),获取bfs层序。这里是为了方便dp,或者直接dfs树形DP其实也是可以的,但可能会爆栈。
2. 自底向上dp,用自身子树情况更新dp1,除自己外的兄弟子树情况更新dp2。
3. 自顶向下dp,变更dp2和dp1的含义。这时对于u来说存在三种子树(强烈建议画图观察):
① u本身的子树,它们的最优解已经存在于之前的dp1[u]。
② u的兄弟子树+fa,它们的最优解=composition(dp2[u],fa,u,use_fa=1)。
③ 连接到fa的最优子树+fa,最优解=composition(dp2[fa],fa,u,use_fa=1)。
注意这里的dp2含义已变更,由于我们是自顶向下计算,因此dp2[fa]已更新。
②和③可以写一起来更新dp2[u]
計算量 O(|V|) (Vは頂点数)
参照 https://qiita.com/keymoon/items/2a52f1b0fb7ef67fb89e
"""
# step1
root -= self._decrement
assert 0 <= root < self._n
self._root = root
g = self.g
_fas = self._parent = [-1] * self._n # 记录每个节点的父节点
_order = self._order = [root] # bfs记录遍历层序,便于后续dp
q = deque([root])
while q:
u = q.popleft()
for v in g[u]:
if v == _fas[u]:
continue
_fas[v] = u
_order.append(v)
q.append(v)
# step2
dp1 = [e(i) for i in range(self._n)] # !子树部分的dp值,假设u是当前子树的根,vs是第一层儿子(它的非父邻居),则dp1[u]=op(dp1(vs))
dp2 = [e(i) for i in
range(
self._n)] # !非子树部分的dp值,假设u是当前子树的根,vs={v1,v2..vi..}是第一层儿子(它的非父邻居),则dp2[vi]=op(dp1(vs-vi)),即他的兄弟们
for u in _order[::-1]: # 从下往上拓扑序dp
res = e(u)
for v in g[u]:
if _fas[u] == v:
continue
dp2[v] = res
res = op(res, composition(dp1[v], u, v, 0)) # op从下往上更新dp1
# 由于最大可能在后边,因此还得倒序来一遍
res = e(u)
for v in g[u][::-1]:
if _fas[u] == v:
continue
dp2[v] = op(res, dp2[v])
res = op(res, composition(dp1[v], u, v, 0))
dp1[u] = res
# step3 自顶向下计算每个节点作为根时的dp1,dp2的含义变更为:dp2[u]为u的兄弟+父。这样对v来说dp1[u] = op(dp1[fa],dp1[u])
for u in _order[1:]:
fa = _fas[u]
dp2[u] = composition(
op(dp2[u], dp2[fa]), fa, u, 1
) # op从上往下更新dp2
dp1[u] = op(dp1[u], dp2[u])
return dp1
n, = RI()
a = RILST()
r = Rerooting(n)
for i, p in enumerate(a, start=1):
r.add_edge(i, p - 1)
def e(root: int) -> int:
# 转移时单个点不管相邻子树的贡献
# 例:最も遠い点までの距離を求める場合 e=0
return 1
def op(child_res1: int, child_res2: int) -> int:
# 如何组合/取舍两个子树的答案
# 例:求最长路径 return max(childRes1,childRes2)
return (child_res1) * (child_res2) % MOD
def composition(from_res: int, fa: int, u: int, use_fa: int = 0) -> int:
# 知道子树的每个子树和节点值,如何更新子树答案;
# 例子:求最长路径 return from_res+1
return (1 + from_res) % MOD
ans = r.rerooting(e, op, composition)
print(' '.join(map(str, ans)))
# 0么有逆元 ms
def solvewa():
n, = RI()
a = RILST()
g = [[] for _ in range(n)]
for i, p in enumerate(a, start=1):
g[i].append(p - 1)
g[p - 1].append(i)
down = [1] * n # 每个节点向下子树至少画一下的方案数
@bootstrap
def dfs(u, fa):
for v in g[u]:
if v == fa: continue
yield dfs(v, u)
down[u] = down[u] * (1 + down[v]) % MOD + MOD
yield
dfs(0, -1)
# print(g)
ans = down[:]
@bootstrap
def reroot(u, fa):
for v in g[u]:
if v == fa: continue
# ans[v] = (ans[u] // (down[v] + 1) + 1) * down[v]
ans[v] = (ans[u] * pow(down[v] + 1, MOD - 2, MOD) + 1) * down[v] % MOD + MOD
yield reroot(v, u)
yield
reroot(0, -1)
print(' '.join(map(str, ans)))
if __name__ == '__main__':
t = 0
if t:
t, = RI()
for _ in range(t):
solve()
else:
solve()
|
Codeforces Round 302 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Road Improvement
|
The country has n cities and n - 1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to n inclusive.
All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvement if the path from the capital located in city x to any other city contains at most one bad road.
Your task is — for every possible x determine the number of ways of improving the quality of some roads in order to meet the citizens' condition. As those values can be rather large, you need to print each value modulo 1 000 000 007 (109 + 7).
|
The first line of the input contains a single integer n (2 ≤ n ≤ 2·105) — the number of cities in the country. Next line contains n - 1 positive integers p2, p3, p4, ..., pn (1 ≤ pi ≤ i - 1) — the description of the roads in the country. Number pi means that the country has a road connecting city pi and city i.
|
Print n integers a1, a2, ..., an, where ai is the sought number of ways to improve the quality of the roads modulo 1 000 000 007 (109 + 7), if the capital of the country is at city number i.
| null | null |
[{"input": "3\n1 1", "output": "4 3 3"}, {"input": "5\n1 2 3 4", "output": "5 8 9 8 5"}]
| 2,300
|
["dp", "trees"]
| 42
|
[{"input": "3\r\n1 1\r\n", "output": "4 3 3"}, {"input": "5\r\n1 2 3 4\r\n", "output": "5 8 9 8 5"}, {"input": "31\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\r\n", "output": "73741817 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913"}, {"input": "29\r\n1 2 2 4 4 6 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28\r\n", "output": "191 380 191 470 236 506 254 506 504 500 494 486 476 464 450 434 416 396 374 350 324 296 266 234 200 164 126 86 44"}, {"input": "70\r\n1 2 2 4 4 6 6 8 9 9 11 11 13 13 15 15 17 17 19 19 21 22 22 24 24 26 27 27 29 29 31 31 33 34 34 36 37 37 39 39 41 42 42 44 44 46 47 47 49 50 50 52 52 54 54 56 57 57 59 60 60 62 63 63 65 65 67 68 68\r\n", "output": "0 1000000005 0 499999996 249999999 749999986 374999994 874999963 999999938 499999970 62499881 531249945 93749781 546874895 109374581 554687295 117186681 558593345 121092131 560546070 123043656 124995179 562497594 125968539 562984274 126450416 126932291 563466150 127163621 563581815 127260071 563630040 127269866 127279659 563639834 127207694 127135727 563567868 126946019 563473014 126543716 126141411 563070710 125325359 562662684 123687534 122049707 561024858 118771194 115492679 557746344 108934221 554467115 95816591 547908300 69580974 43345355 521672682 990873947 938402530 469201266 833459609 728516686 364258344 518630798 259315400 98859001 679087209 839543609 839543609 "}, {"input": "59\r\n1 2 2 4 4 5 7 7 8 8 10 10 11 11 15 15 9 18 18 20 20 21 23 22 22 26 6 6 28 30 30 31 31 33 34 34 32 32 38 40 39 39 29 44 44 45 47 47 46 46 50 52 52 54 51 51 57 58\r\n", "output": "0 1000000005 0 499999996 499259752 500131906 498519506 453903141 456877573 963122521 230821046 981561265 981561265 115410524 784656845 892328427 892328427 415235638 207617820 331951678 748963765 998815735 165975843 582987926 999407872 332543823 666271916 492735403 494450227 485338898 330005231 366989446 553336825 864004193 776668417 932002101 932002101 775242091 893591565 183494727 591747368 946795787 946795787 488768546 73973791 454675898 659179041 829589525 829589525 147841416 181934138 841006939 920503474 227337959 613668984 90967070 636450610 90967073 545483541 "}, {"input": "2\r\n1\r\n", "output": "2 2"}, {"input": "3\r\n1 2\r\n", "output": "3 4 3"}, {"input": "69\r\n1 1 3 3 5 5 7 8 8 10 10 12 12 14 14 16 16 18 18 20 21 21 23 23 25 26 26 28 28 30 30 32 33 33 35 36 36 38 38 40 41 41 43 43 45 46 46 48 49 49 51 51 53 53 55 56 56 58 59 59 61 62 62 64 64 66 67 67\r\n", "output": "1000000006 500000004 499999999 750000004 749999993 875000001 874999978 999999961 999999985 62499920 31249961 93749852 46874927 109374716 54687359 117186944 58593473 121092650 60546326 123044687 124996722 62498362 125971106 62985554 126455031 126938954 63469478 127174380 63587191 127279022 63639512 127305201 127331378 63665690 127292181 127252982 63626492 127128810 63564406 126857579 126586346 63293174 126032438 63016220 124918901 123805362 61902682 121575425 119345486 59672744 114884180 57442091 105960854 52980428 88113845 70266834 35133418 34572635 998878441 999439225 927489952 856101461 928050735 713324437 856662223 427770368 142216297 571108153 571108153 "}, {"input": "137\r\n1 1 3 3 5 5 7 8 8 10 10 12 12 14 14 16 16 18 18 20 21 21 23 23 25 26 26 28 28 30 30 32 33 33 35 36 36 38 38 40 41 41 43 43 45 46 46 48 49 49 51 51 53 53 55 56 56 58 59 59 61 62 62 64 64 66 67 67 1 1 71 71 73 73 75 76 76 78 78 80 80 82 82 84 84 86 86 88 89 89 91 91 93 94 94 96 96 98 98 100 101 101 103 104 104 106 106 108 109 109 111 111 113 114 114 116 117 117 119 119 121 121 123 124 124 126 127 127 129 130 130 132 132 134 135 135\r\n", "output": "1 500000005 500000005 750000007 750000007 875000008 875000008 0 1 62499998 31250000 93749994 46874998 109374986 54687494 117187470 58593736 121093688 60546845 123046749 124999808 62499905 125976240 62988121 126464261 126952280 63476141 127195898 63597950 127316924 63658463 127375871 127434816 63717409 127461155 127487492 63743747 127494392 63747197 127485305 127476216 63738109 127446596 63723299 127381635 127316672 63658337 127183887 127051100 63525551 126784098 63392050 126249380 63124691 125179587 124109792 62054897 121970025 119830256 59915129 115550631 111271004 55635503 102711708 51355855 85593095 68474480 34237241 34237241 500000005 500000005 750000007 750000007 875000008 875000008 0 1 62499998 31250000 93749994 46874998 109374986 54687494 117187470 58593736 121093688 60546845 123046749 124999808 62499905 125976240 62988121 126464261 126952280 63476141 127195898 63597950 127316924 63658463 127375871 127434816 63717409 127461155 127487492 63743747 127494392 63747197 127485305 127476216 63738109 127446596 63723299 127381635 127316672 63658337 127183887 127051100 63525551 126784098 63392050 126249380 63124691 125179587 124109792 62054897 121970025 119830256 59915129 115550631 111271004 55635503 102711708 51355855 85593095 68474480 34237241 34237241 "}, {"input": "150\r\n1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 28 29 29 29 29 30 30 30 30 31 31 31 31 31 32 32 32 32 32 33 33 33 33 33 33 34 34 34 34 34 34 35 35 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 37 37 37 37 37 37\r\n", "output": "0 1000000005 0 0 0 0 800000008 800000008 800000008 800000008 800000008 800000008 800000008 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 705882372 705882372 705882372 878787915 878787915 61538524 61538524 596899355 596899355 196881603 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 939393962 939393962 939393962 939393962 939393962 939393962 939393962 939393962 939393962 939393962 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 598440806 598440806 598440806 598440806 598440806 598440806 598440806 598440806 598440806 "}]
| false
|
stdio
| null | true
|
28/A
|
28
|
A
|
PyPy 3-64
|
TESTS
| 5
| 124
| 2,355,200
|
145107595
|
import copy
n, m = map(int, input().split())
nails = []
for i in range(n):
nails.append(list(map(int, input().split())))
lens = list(map(int, input().split()))
locs = []
for i in range(1, m+1):
locs.append(i)
locs_c = copy.deepcopy(locs)
lens_c = copy.deepcopy(lens)
flag = 0
out_ = []
status = []
i = 0
while i != n:
len1 = abs(nails[i-1][0] - nails[i][0]) + abs(nails[i-1][1] - nails[i][1])
len2 = abs(nails[i+1][0] - nails[i][0]) + abs(nails[i+1][1] - nails[i][1])
len_ = len1+len2
if len_ in lens:
a = lens.index(len_)
out_.append(locs[a])
out_.append(-1)
del(lens[a])
del(locs[a])
i += 2
else:
flag = 1
break
if flag == 1:
out_ = []
i = -1
while i != n-1:
len1 = abs(nails[i - 1][0] - nails[i][0]) + abs(nails[i - 1][1] - nails[i][1])
len2 = abs(nails[i + 1][0] - nails[i][0]) + abs(nails[i + 1][1] - nails[i][1])
len_ = len1 + len2
if len_ in lens_c:
a = lens_c.index(len_)
out_.append(locs_c[a])
out_.append(-1)
del (lens_c[a])
del (locs_c[a])
i += 2
else:
flag = 2
break
if flag != 2:
print("YES")
for i in out_:
print(i, end=" ")
else:
print("NO")
| 51
| 122
| 614,400
|
218740676
|
from collections import defaultdict
num_nails, num_segments = map(int, input().split())
nail_coordinates = [tuple(map(int, input().split())) for _ in range(num_nails)]
nail_distances = [abs(a - c) + abs(b - d) for (a, b), (c, d) in zip(nail_coordinates, nail_coordinates[2:] + nail_coordinates[:2])]
segment_indices = defaultdict(list)
for idx, segment_length in enumerate(map(int, input().split()), 1):
segment_indices[segment_length].append(idx)
for shift in -1, 0:
result = [-1] * num_nails
for nail_idx in range(shift, num_nails + shift, 2):
nail_distance = nail_distances[nail_idx]
if nail_distance in segment_indices and segment_indices[nail_distance]:
result[(nail_idx + 1) % num_nails] = segment_indices[nail_distance].pop()
else:
break
else:
print("YES")
print(" ".join(map(str, result)))
exit(0)
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
|
28/A
|
28
|
A
|
Python 3
|
TESTS
| 9
| 216
| 307,200
|
67647357
|
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(sum(s[i])-sum(s[i-1])) + abs(sum(s[i])-sum(s[i+1]))
s_chet.append(q)
q1 = abs(sum(s[-1])-sum(s[-2])) + abs(sum(s[-1])-sum(s[0]))
s_chet.append(q1)
s_nechet = []
for i in range(2,n-1,2): #Проход по нечетным гвоздям
qq = abs(sum(s[i])-sum(s[i-1])) + abs(sum(s[i])-sum(s[i+1]))
s_nechet.append(qq)
qq1 = abs(sum(s[-1])-sum(s[0])) + abs(sum(s[1])-sum(s[0]))
s_nechet.append(qq1)
s_chet.sort()
s_nechet.sort()
ss = map(int,input().split())
ss = list(ss)
ss.sort()
if s_chet == ss:
print('YES')
sss = []
for i in range(1,m+1):
sss.append(-1)
sss.append(i)
print(" ".join(map(str,sss)))
elif s_nechet == ss:
print('YES')
sss = []
for i in range(1,m+1):
sss.append(i)
sss.append(-1)
print(" ".join(map(str,sss)))
else:
print('NO')
| 51
| 124
| 1,843,200
|
216109513
|
import sys
input = sys.stdin.readline
def f(w, i):
d = []
while i < n:
d.append((ff(w[(i-1)%n], w[i]) + ff(w[i], w[(i+1)%n]), i))
i += 2
d.sort()
i, j, ew, h = 0, 0, 1, []
for i in range(len(d)):
if j == m:
ew = 0
break
while d[i][0] != q[j][0]:
j += 1
if j == m:
ew = 0
break
if j == m:
ew = 0
break
h.append((d[i][1], q[j][1]))
j += 1
return (ew, h)
def ff(x, y):
return abs(sum(x)-sum(y))
n, m = map(int, input().split())
g = [list(map(int, input().split())) for _ in range(n)]
q = sorted([(j, i+1) for i, j in enumerate(map(int, input().split()))])
for i, j in [f(g, 0), f(g, 1)]:
if i:
print('YES')
d = [-1]*n
for u, v in j:
d[u] = v
print(' '.join(map(str, d)))
break
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
|
543/D
|
543
|
D
|
PyPy 3-64
|
TESTS
| 9
| 717
| 46,387,200
|
214698660
|
import sys
from collections import Counter
import functools
import math
import random
import sys
import os
from bisect import bisect_left,bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# RSI = lambda: int(sys.stdin.buffer.readline())
RI = lambda: list(map(int, sys.stdin.buffer.readline().strip().split()))
RS = lambda: sys.stdin.buffer.readline().strip().decode('ascii')
RLS = lambda: sys.stdin.buffer.readline().strip().decode('ascii').split()
DEBUG = lambda *x: sys.stderr.write(f'{str(x)}\n')
# print = lambda d: sys.stdout.write(str(d) + "\n")
if sys.hexversion == 50924784:
sys.stdin = open('./data')
"""
参考样例
输入
3
1 1
输出
4 3 3
输入
5
1 2 3 4
输出
5 8 9 8 5
题意简述:
https://codeforces.com/problemset/problem/543/D
输入 n(2≤n≤2e5) 和 n-1 个数 p2,p3,...,pn,表示一棵 n 个节点的无根树,节点编号从 1 开始,i 与 pi(1≤pi≤i-1) 相连。
定义 a(x) 表示以 x 为根时的合法标记方案数,模 1e9+7。其中【合法标记】定义为:对树的某些边做标记,使得 x 到任意点的简单路径上,至多有一条边是被标记的。
输出 a(1),a(2),...,a(n)。
思路:
"""
M = int(1e9) + 7
def solve():
n, = RI()
p = [0] + RI()
ch = [[] for _ in range(n)]
for i in range(n):
p[i] -= 1
if i:
ch[p[i]].append(i)
res = [-1] * (n + 1)
sta = [0]
while sta:
for ne in ch[sta[-1]]:
sta.append(ne)
if not ch[sta[-1]]:
res[sta[-1]] = 1
c = sta.pop()
while sta and sta[-1] == p[c]:
c = sta.pop()
r = 1
for ne in ch[c]:
r *= res[ne] + 1
r %= M
res[c] = r
ans = [0] * n
ans[0] = res[0]
dq = deque([(0, 0)])
while dq:
c, pc = dq.popleft()
for ne in ch[c]:
nep = res[c] * (pc + 1) * pow(res[ne] + 1, M - 2, M) % M
ans[ne] = (ans[c] + res[ne] - nep) % M
dq.append((ne, nep))
print(*ans)
return 0
# for t in range(RI()[0] - 1):
# solve()
solve()
| 42
| 452
| 21,913,600
|
219474089
|
import sys
MODULO = int(1e9) + 7
def solve(n, p, out_stream):
prod = [1] * n
modpow = [0] * n
for i in range(n - 1, 0, -1):
j = p[i]
if modpow[i] == 0 and prod[i] + 1 == MODULO:
modpow[j] += 1
else:
prod[j] = (prod[j] * ((prod[i] if modpow[i] == 0 else 0) + 1)) % MODULO
if modpow[0] == 0:
out_stream.write(str(prod[0]))
else:
out_stream.write("0")
for i in range(1, n):
j = p[i]
pr = prod[j]
prpow = modpow[j]
if modpow[i] == 0 and prod[i] + 1 == MODULO:
prpow -= 1
else:
pr = (pr * pow((prod[i] if modpow[i] == 0 else 0) + 1, MODULO - 2, MODULO)) % MODULO
if prpow > 0:
pr = 0
if pr + 1 == MODULO:
modpow[i] += 1
else:
prod[i] = (prod[i] * (pr + 1)) % MODULO
out_stream.write(' ')
if modpow[i] == 0:
out_stream.write(str(prod[i]))
else:
out_stream.write("0")
out_stream.write("\n")
def inv(x):
if x == 1:
return 1
return pow(x, MODULO - 2, MODULO)
def pow(x, k, MOD):
if k == 0:
return 1
if k % 2 == 0:
return pow((x * x) % MOD, k // 2, MOD)
return (x * pow(x, k - 1, MOD)) % MOD
def main():
try:
while True:
n = int(input())
p = [-1] + [int(x) - 1 for x in input().split()]
solve(n, p, sys.stdout)
except EOFError:
pass
if __name__ == "__main__":
main()# 1692375930.364086
|
Codeforces Round 302 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Road Improvement
|
The country has n cities and n - 1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to n inclusive.
All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvement if the path from the capital located in city x to any other city contains at most one bad road.
Your task is — for every possible x determine the number of ways of improving the quality of some roads in order to meet the citizens' condition. As those values can be rather large, you need to print each value modulo 1 000 000 007 (109 + 7).
|
The first line of the input contains a single integer n (2 ≤ n ≤ 2·105) — the number of cities in the country. Next line contains n - 1 positive integers p2, p3, p4, ..., pn (1 ≤ pi ≤ i - 1) — the description of the roads in the country. Number pi means that the country has a road connecting city pi and city i.
|
Print n integers a1, a2, ..., an, where ai is the sought number of ways to improve the quality of the roads modulo 1 000 000 007 (109 + 7), if the capital of the country is at city number i.
| null | null |
[{"input": "3\n1 1", "output": "4 3 3"}, {"input": "5\n1 2 3 4", "output": "5 8 9 8 5"}]
| 2,300
|
["dp", "trees"]
| 42
|
[{"input": "3\r\n1 1\r\n", "output": "4 3 3"}, {"input": "5\r\n1 2 3 4\r\n", "output": "5 8 9 8 5"}, {"input": "31\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\r\n", "output": "73741817 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913"}, {"input": "29\r\n1 2 2 4 4 6 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28\r\n", "output": "191 380 191 470 236 506 254 506 504 500 494 486 476 464 450 434 416 396 374 350 324 296 266 234 200 164 126 86 44"}, {"input": "70\r\n1 2 2 4 4 6 6 8 9 9 11 11 13 13 15 15 17 17 19 19 21 22 22 24 24 26 27 27 29 29 31 31 33 34 34 36 37 37 39 39 41 42 42 44 44 46 47 47 49 50 50 52 52 54 54 56 57 57 59 60 60 62 63 63 65 65 67 68 68\r\n", "output": "0 1000000005 0 499999996 249999999 749999986 374999994 874999963 999999938 499999970 62499881 531249945 93749781 546874895 109374581 554687295 117186681 558593345 121092131 560546070 123043656 124995179 562497594 125968539 562984274 126450416 126932291 563466150 127163621 563581815 127260071 563630040 127269866 127279659 563639834 127207694 127135727 563567868 126946019 563473014 126543716 126141411 563070710 125325359 562662684 123687534 122049707 561024858 118771194 115492679 557746344 108934221 554467115 95816591 547908300 69580974 43345355 521672682 990873947 938402530 469201266 833459609 728516686 364258344 518630798 259315400 98859001 679087209 839543609 839543609 "}, {"input": "59\r\n1 2 2 4 4 5 7 7 8 8 10 10 11 11 15 15 9 18 18 20 20 21 23 22 22 26 6 6 28 30 30 31 31 33 34 34 32 32 38 40 39 39 29 44 44 45 47 47 46 46 50 52 52 54 51 51 57 58\r\n", "output": "0 1000000005 0 499999996 499259752 500131906 498519506 453903141 456877573 963122521 230821046 981561265 981561265 115410524 784656845 892328427 892328427 415235638 207617820 331951678 748963765 998815735 165975843 582987926 999407872 332543823 666271916 492735403 494450227 485338898 330005231 366989446 553336825 864004193 776668417 932002101 932002101 775242091 893591565 183494727 591747368 946795787 946795787 488768546 73973791 454675898 659179041 829589525 829589525 147841416 181934138 841006939 920503474 227337959 613668984 90967070 636450610 90967073 545483541 "}, {"input": "2\r\n1\r\n", "output": "2 2"}, {"input": "3\r\n1 2\r\n", "output": "3 4 3"}, {"input": "69\r\n1 1 3 3 5 5 7 8 8 10 10 12 12 14 14 16 16 18 18 20 21 21 23 23 25 26 26 28 28 30 30 32 33 33 35 36 36 38 38 40 41 41 43 43 45 46 46 48 49 49 51 51 53 53 55 56 56 58 59 59 61 62 62 64 64 66 67 67\r\n", "output": "1000000006 500000004 499999999 750000004 749999993 875000001 874999978 999999961 999999985 62499920 31249961 93749852 46874927 109374716 54687359 117186944 58593473 121092650 60546326 123044687 124996722 62498362 125971106 62985554 126455031 126938954 63469478 127174380 63587191 127279022 63639512 127305201 127331378 63665690 127292181 127252982 63626492 127128810 63564406 126857579 126586346 63293174 126032438 63016220 124918901 123805362 61902682 121575425 119345486 59672744 114884180 57442091 105960854 52980428 88113845 70266834 35133418 34572635 998878441 999439225 927489952 856101461 928050735 713324437 856662223 427770368 142216297 571108153 571108153 "}, {"input": "137\r\n1 1 3 3 5 5 7 8 8 10 10 12 12 14 14 16 16 18 18 20 21 21 23 23 25 26 26 28 28 30 30 32 33 33 35 36 36 38 38 40 41 41 43 43 45 46 46 48 49 49 51 51 53 53 55 56 56 58 59 59 61 62 62 64 64 66 67 67 1 1 71 71 73 73 75 76 76 78 78 80 80 82 82 84 84 86 86 88 89 89 91 91 93 94 94 96 96 98 98 100 101 101 103 104 104 106 106 108 109 109 111 111 113 114 114 116 117 117 119 119 121 121 123 124 124 126 127 127 129 130 130 132 132 134 135 135\r\n", "output": "1 500000005 500000005 750000007 750000007 875000008 875000008 0 1 62499998 31250000 93749994 46874998 109374986 54687494 117187470 58593736 121093688 60546845 123046749 124999808 62499905 125976240 62988121 126464261 126952280 63476141 127195898 63597950 127316924 63658463 127375871 127434816 63717409 127461155 127487492 63743747 127494392 63747197 127485305 127476216 63738109 127446596 63723299 127381635 127316672 63658337 127183887 127051100 63525551 126784098 63392050 126249380 63124691 125179587 124109792 62054897 121970025 119830256 59915129 115550631 111271004 55635503 102711708 51355855 85593095 68474480 34237241 34237241 500000005 500000005 750000007 750000007 875000008 875000008 0 1 62499998 31250000 93749994 46874998 109374986 54687494 117187470 58593736 121093688 60546845 123046749 124999808 62499905 125976240 62988121 126464261 126952280 63476141 127195898 63597950 127316924 63658463 127375871 127434816 63717409 127461155 127487492 63743747 127494392 63747197 127485305 127476216 63738109 127446596 63723299 127381635 127316672 63658337 127183887 127051100 63525551 126784098 63392050 126249380 63124691 125179587 124109792 62054897 121970025 119830256 59915129 115550631 111271004 55635503 102711708 51355855 85593095 68474480 34237241 34237241 "}, {"input": "150\r\n1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 28 29 29 29 29 30 30 30 30 31 31 31 31 31 32 32 32 32 32 33 33 33 33 33 33 34 34 34 34 34 34 35 35 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 37 37 37 37 37 37\r\n", "output": "0 1000000005 0 0 0 0 800000008 800000008 800000008 800000008 800000008 800000008 800000008 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 705882372 705882372 705882372 878787915 878787915 61538524 61538524 596899355 596899355 196881603 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 111111116 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 352941187 939393962 939393962 939393962 939393962 939393962 939393962 939393962 939393962 939393962 939393962 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 30769263 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 798449682 598440806 598440806 598440806 598440806 598440806 598440806 598440806 598440806 598440806 "}]
| false
|
stdio
| null | true
|
541/E
|
542
|
E
|
PyPy 3
|
TESTS
| 6
| 155
| 2,252,800
|
78242776
|
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(m):
p, q = map(int, input().split())
g[p - 1].append(q - 1)
g[q - 1].append(p - 1)
def shortest(root):
dist = [-1] * n
q = [0] * n
left, right = 0, 1
q[left] = root
dist[root] = 0
good = True
while left < right:
x = q[left]
left = left + 1
for i in g[x]:
if dist[i] is -1:
dist[i] = 1 + dist[x]
q[right] = i
right = right + 1
elif dist[i] == dist[x]:
good = False
return good, dist
def getDiameter(root):
good, dist = shortest(root)
far = root
for i in range(n):
if far < dist[i]:
far = i
dia = 0
_, dist = shortest(far)
for i in range(n):
if dia < dist[i]:
dia = dist[i]
return good, dia, dist
vis = [False] * n
good = True
ans = 0
for i in range(n):
if vis[i] is False:
_, add, dist = getDiameter(i)
if _ is False: good = False
ans = ans + add
for j in range(n):
if dist[j] is not -1:
vis[j] = True
if good is False: print('-1')
else: print(ans)
| 50
| 2,168
| 10,956,800
|
78243037
|
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(m):
p, q = map(int, input().split())
g[p - 1].append(q - 1)
g[q - 1].append(p - 1)
comp = [-1] * n
def shortest(root):
dist = [-1] * n
q = [0] * n
left, right = 0, 1
q[left] = root
dist[root] = 0
good = True
while left < right:
x = q[left]
left = left + 1
for i in g[x]:
if dist[i] is -1:
dist[i] = 1 + dist[x]
q[right] = i
right = right + 1
elif dist[i] == dist[x]:
good = False
far = 0
for i in dist:
if far < i:
far = i
return good, far, dist
arr = [0] * n
good = True
for i in range(n):
_, opt, dist = shortest(i)
if _ is False: good = False
if comp[i] is -1:
for j in range(n):
if dist[j] is not -1: comp[j] = i
if arr[comp[i]] < opt:
arr[comp[i]] = opt
if good is False: print('-1')
else: print(sum(arr))
|
VK Cup 2015 - Раунд 3
|
CF
| 2,015
| 3
| 256
|
Playing on Graph
|
Vova and Marina love offering puzzles to each other. Today Marina offered Vova to cope with the following task.
Vova has a non-directed graph consisting of n vertices and m edges without loops and multiple edges. Let's define the operation of contraction two vertices a and b that are not connected by an edge. As a result of this operation vertices a and b are deleted and instead of them a new vertex x is added into the graph, and also edges are drawn from it to all vertices that were connected with a or with b (specifically, if the vertex was connected with both a and b, then also exactly one edge is added from x to it). Thus, as a result of contraction again a non-directed graph is formed, it contains no loops nor multiple edges, and it contains (n - 1) vertices.
Vova must perform the contraction an arbitrary number of times to transform the given graph into a chain of the maximum length. A chain of length k (k ≥ 0) is a connected graph whose vertices can be numbered with integers from 1 to k + 1 so that the edges of the graph connect all pairs of vertices (i, i + 1) (1 ≤ i ≤ k) and only them. Specifically, the graph that consists of one vertex is a chain of length 0. The vertices that are formed as a result of the contraction are allowed to be used in the following operations of contraction.
The picture illustrates the contraction of two vertices marked by red.
Help Vova cope with his girlfriend's task. Find the maximum length of the chain that can be obtained from the resulting graph or else determine that it is impossible to obtain the chain.
|
The first line contains two integers n, m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the original graph.
Next m lines contain the descriptions of edges in the format ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which means that there is an edge between vertices ai and bi. It is guaranteed that there is at most one edge between each pair of vertexes.
|
If it is impossible to obtain a chain from the given graph, print - 1. Otherwise, print the maximum possible number of edges in the resulting chain.
| null |
In the first sample test you can contract vertices 4 and 5 and obtain a chain of length 3.
In the second sample test it is initially impossible to contract any pair of vertexes, so it is impossible to achieve the desired result.
In the third sample test you can contract vertices 1 and 2 and obtain a chain of length 2.
|
[{"input": "5 4\n1 2\n2 3\n3 4\n3 5", "output": "3"}, {"input": "4 6\n1 2\n2 3\n1 3\n3 4\n2 4\n1 4", "output": "-1"}, {"input": "4 2\n1 3\n2 4", "output": "2"}]
| 2,600
|
[]
| 50
|
[{"input": "5 4\r\n1 2\r\n2 3\r\n3 4\r\n3 5\r\n", "output": "3\r\n"}, {"input": "4 6\r\n1 2\r\n2 3\r\n1 3\r\n3 4\r\n2 4\r\n1 4\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n1 3\r\n2 4\r\n", "output": "2\r\n"}, {"input": "1 0\r\n", "output": "0\r\n"}, {"input": "1000 0\r\n", "output": "0\r\n"}, {"input": "1000 4\r\n100 200\r\n200 300\r\n300 400\r\n400 100\r\n", "output": "2\r\n"}, {"input": "14 30\r\n12 10\r\n1 7\r\n12 13\r\n7 3\r\n14 10\r\n3 12\r\n11 1\r\n2 12\r\n2 5\r\n14 3\r\n14 1\r\n14 4\r\n6 7\r\n12 6\r\n9 5\r\n7 10\r\n8 5\r\n6 14\r\n13 7\r\n4 12\r\n9 10\r\n1 9\r\n14 5\r\n1 8\r\n2 13\r\n5 11\r\n8 6\r\n4 9\r\n9 13\r\n2 4\r\n", "output": "-1\r\n"}, {"input": "59 24\r\n40 3\r\n14 10\r\n17 5\r\n40 15\r\n22 40\r\n9 40\r\n46 41\r\n17 24\r\n20 15\r\n49 46\r\n17 50\r\n14 25\r\n8 14\r\n11 36\r\n59 40\r\n7 36\r\n16 46\r\n20 35\r\n20 49\r\n58 20\r\n17 49\r\n26 46\r\n59 14\r\n38 40\r\n", "output": "10\r\n"}]
| false
|
stdio
| null | true
|
962/E
|
962
|
E
|
PyPy 3
|
TESTS
| 4
| 124
| 0
|
94124267
|
import sys
def kruskal(v_count: int, edges: list) -> int:
edges.sort()
tree = [-1]*v_count
def get_root(x) -> int:
if tree[x] < 0:
return x
tree[x] = get_root(tree[x])
return tree[x]
def unite(x, y) -> bool:
x, y = get_root(x), get_root(y)
if x != y:
big, small = (x, y) if tree[x] < tree[y] else (y, x)
tree[big] += tree[small]
tree[small] = big
return x != y
cost = 0
for w, s, t in edges:
if unite(s, t):
cost += w
v_count -= 1
if v_count == 1:
break
return cost
n = int(sys.stdin.buffer.readline().decode('utf-8'))
pos, type_ = [0]*n, ['']*n
for i, (x, c) in enumerate(line.decode('utf-8').split() for line in sys.stdin.buffer):
pos[i] = int(x)
type_[i] = c
r_cnt = type_.count('R')
b_cnt = type_.count('B')
p_cnt = n - r_cnt - b_cnt
inf = 2100000000
if r_cnt == 0 or b_cnt == 0:
print(pos[-1] - pos[0])
exit()
if p_cnt == 0:
r_min, r_max = inf, -inf
b_min, b_max = inf, -inf
for i in range(n):
if type_[i] == 'R':
r_min = min(pos[i], r_min)
r_max = max(pos[i], r_max)
else:
b_min = min(pos[i], b_min)
b_max = max(pos[i], b_max)
print(r_max - r_min + b_max - b_min)
exit()
r_prev = b_prev = p_prev = inf
ri, bi = 0, 0
pi, pj = inf, -inf
r_edges = []
b_edges = []
for i in range(n):
if type_[i] == 'B':
bi += 1
if b_prev != inf:
b_edges.append((pos[i]-b_prev, bi-1, bi))
if p_prev != inf:
b_edges.append((pos[i]-p_prev, 0, bi))
b_prev = pos[i]
elif type_[i] == 'R':
ri += 1
if r_prev != inf:
r_edges.append((pos[i]-r_prev, ri-1, ri))
if p_prev != inf:
r_edges.append((pos[i]-r_prev, 0, ri))
r_prev = pos[i]
else:
if b_prev != inf:
b_edges.append((pos[i]-b_prev, 0, bi))
if r_prev != inf:
r_edges.append((pos[i]-r_prev, 0, ri))
p_prev = pos[i]
pi = min(pi, i)
pj = max(pj, i)
ans = pos[pj] - pos[pi]
ans += kruskal(ri+1, r_edges)
ans += kruskal(bi+1, b_edges)
print(ans)
| 45
| 405
| 13,516,800
|
94132083
|
import sys
n = int(sys.stdin.buffer.readline().decode('utf-8'))
pos, type_ = [0]*n, ['']*n
for i, (x, c) in enumerate(line.decode('utf-8').split() for line in sys.stdin.buffer):
pos[i] = int(x)
type_[i] = c
r_cnt = type_.count('R')
b_cnt = type_.count('B')
p_cnt = n - r_cnt - b_cnt
inf = 2100000000
if p_cnt == 0:
r_min, r_max = inf, inf
b_min, b_max = inf, inf
for i in range(n):
if type_[i] == 'R':
r_min = min(pos[i], r_min)
r_max = pos[i]
else:
b_min = min(pos[i], b_min)
b_max = pos[i]
print(r_max - r_min + b_max - b_min)
exit()
p_index = [i for i in range(n) if type_[i] == 'P']
ans = 0
for c in 'RB':
for i in range(p_index[0]):
if type_[i] == c:
ans += pos[p_index[0]] - pos[i]
break
for i in range(n-1, p_index[-1], -1):
if type_[i] == c:
ans += pos[i] - pos[p_index[-1]]
break
for i, j in zip(p_index, p_index[1:]):
r, b = [], []
r_prev = b_prev = pos[i]
for k in range(i+1, j):
if type_[k] == 'R':
r.append(pos[k] - r_prev)
r_prev = pos[k]
else:
b.append(pos[k] - b_prev)
b_prev = pos[k]
r.append(pos[j] - r_prev)
b.append(pos[j] - b_prev)
r.sort()
b.sort()
ans += min(
pos[j]-pos[i] + sum(r) - r[-1] + sum(b) - b[-1],
sum(r) + sum(b)
)
print(ans)
|
Educational Codeforces Round 42 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Byteland, Berland and Disputed Cities
|
The cities of Byteland and Berland are located on the axis $$$Ox$$$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $$$Ox$$$ there are three types of cities:
- the cities of Byteland,
- the cities of Berland,
- disputed cities.
Recently, the project BNET has been launched — a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.
The countries agreed to connect the pairs of cities with BNET cables in such a way that:
- If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables,
- If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables.
Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.
The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.
Each city is a point on the line $$$Ox$$$. It is technically possible to connect the cities $$$a$$$ and $$$b$$$ with a cable so that the city $$$c$$$ ($$$a < c < b$$$) is not connected to this cable, where $$$a$$$, $$$b$$$ and $$$c$$$ are simultaneously coordinates of the cities $$$a$$$, $$$b$$$ and $$$c$$$.
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^{5}$$$) — the number of cities.
The following $$$n$$$ lines contains an integer $$$x_i$$$ and the letter $$$c_i$$$ ($$$-10^{9} \le x_i \le 10^{9}$$$) — the coordinate of the city and its type. If the city belongs to Byteland, $$$c_i$$$ equals to 'B'. If the city belongs to Berland, $$$c_i$$$ equals to «R». If the city is disputed, $$$c_i$$$ equals to 'P'.
All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
|
Print the minimal total length of such set of cables, that if we delete all Berland cities ($$$c_i$$$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($$$c_i$$$='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.
| null |
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $$$5 + 3 + 4 = 12$$$.
In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates $$$10, 21, 32$$$, so to connect them you need two cables of length $$$11$$$ and $$$11$$$. The cities of Byteland have coordinates $$$14$$$ and $$$16$$$, so to connect them you need one cable of length $$$2$$$. Thus, the total length of all cables is $$$11 + 11 + 2 = 24$$$.
|
[{"input": "4\n-5 R\n0 P\n3 P\n7 B", "output": "12"}, {"input": "5\n10 R\n14 B\n16 B\n21 R\n32 R", "output": "24"}]
| 2,200
|
["constructive algorithms", "greedy"]
| 45
|
[{"input": "4\r\n-5 R\r\n0 P\r\n3 P\r\n7 B\r\n", "output": "12\r\n"}, {"input": "5\r\n10 R\r\n14 B\r\n16 B\r\n21 R\r\n32 R\r\n", "output": "24\r\n"}, {"input": "10\r\n66 R\r\n67 R\r\n72 R\r\n73 R\r\n76 R\r\n78 B\r\n79 B\r\n83 B\r\n84 B\r\n85 P\r\n", "output": "26\r\n"}, {"input": "10\r\n61 R\r\n64 R\r\n68 R\r\n71 R\r\n72 R\r\n73 R\r\n74 P\r\n86 P\r\n87 B\r\n90 B\r\n", "output": "29\r\n"}, {"input": "15\r\n-9518 R\r\n-6858 P\r\n-6726 B\r\n-6486 R\r\n-4496 P\r\n-4191 P\r\n-772 B\r\n-258 R\r\n-194 P\r\n1035 R\r\n2297 P\r\n4816 B\r\n5779 R\r\n9342 B\r\n9713 B\r\n", "output": "25088\r\n"}, {"input": "6\r\n-8401 R\r\n-5558 P\r\n-3457 P\r\n-2361 R\r\n6966 P\r\n8140 B\r\n", "output": "17637\r\n"}, {"input": "2\r\n1 R\r\n2 R\r\n", "output": "1\r\n"}, {"input": "2\r\n-1000000000 B\r\n1000000000 R\r\n", "output": "0\r\n"}, {"input": "2\r\n-1000000000 P\r\n1000000000 P\r\n", "output": "2000000000\r\n"}, {"input": "2\r\n-1000000000 B\r\n1000000000 P\r\n", "output": "2000000000\r\n"}, {"input": "9\r\n-105 R\r\n-81 B\r\n-47 P\r\n-25 R\r\n-23 B\r\n55 P\r\n57 R\r\n67 B\r\n76 P\r\n", "output": "272\r\n"}, {"input": "6\r\n-13 R\r\n-10 P\r\n-6 R\r\n-1 P\r\n4 R\r\n10 P\r\n", "output": "32\r\n"}, {"input": "8\r\n-839 P\r\n-820 P\r\n-488 P\r\n-334 R\r\n-83 B\r\n187 R\r\n380 B\r\n804 P\r\n", "output": "2935\r\n"}, {"input": "8\r\n-12 P\r\n-9 B\r\n-2 R\r\n-1 R\r\n2 B\r\n8 B\r\n9 R\r\n15 P\r\n", "output": "54\r\n"}, {"input": "6\r\n0 B\r\n3 P\r\n7 B\r\n9 B\r\n11 P\r\n13 B\r\n", "output": "17\r\n"}]
| false
|
stdio
| null | true
|
522/A
|
522
|
A
|
PyPy 3-64
|
TESTS
| 5
| 31
| 0
|
193980161
|
l=[]
c=[]
for _ in range(int(input())):
a,b=input().lower().replace(" ","").split("reposted")
if b in l:
x=l.index(b)
c[x]+=1
l[x]=a
else:
l.append(a)
c.append(2)
print(max(c))
| 36
| 31
| 0
|
145937584
|
dic = {"polycarp":1}
for i in range(int(input())):
z = input().split()
a = z[0].lower()
b = z[-1].lower()
dic[a] = dic[b]+1
xyz = list(dic.values())
print(max(xyz))
|
VK Cup 2015 - Qualification Round 1
|
CF
| 2,015
| 1
| 256
|
Reposts
|
One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.
These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.
Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.
|
The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.
We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.
|
Print a single integer — the maximum length of a repost chain.
| null | null |
[{"input": "5\ntourist reposted Polycarp\nPetr reposted Tourist\nWJMZBMR reposted Petr\nsdya reposted wjmzbmr\nvepifanov reposted sdya", "output": "6"}, {"input": "6\nMike reposted Polycarp\nMax reposted Polycarp\nEveryOne reposted Polycarp\n111 reposted Polycarp\nVkCup reposted Polycarp\nCodeforces reposted Polycarp", "output": "2"}, {"input": "1\nSoMeStRaNgEgUe reposted PoLyCaRp", "output": "2"}]
| 1,200
|
["*special", "dfs and similar", "dp", "graphs", "trees"]
| 36
|
[{"input": "5\r\ntourist reposted Polycarp\r\nPetr reposted Tourist\r\nWJMZBMR reposted Petr\r\nsdya reposted wjmzbmr\r\nvepifanov reposted sdya\r\n", "output": "6\r\n"}, {"input": "6\r\nMike reposted Polycarp\r\nMax reposted Polycarp\r\nEveryOne reposted Polycarp\r\n111 reposted Polycarp\r\nVkCup reposted Polycarp\r\nCodeforces reposted Polycarp\r\n", "output": "2\r\n"}, {"input": "1\r\nSoMeStRaNgEgUe reposted PoLyCaRp\r\n", "output": "2\r\n"}, {"input": "1\r\niuNtwVf reposted POlYcarP\r\n", "output": "2\r\n"}, {"input": "10\r\ncs reposted poLYCaRp\r\nAFIkDrY7Of4V7Mq reposted CS\r\nsoBiwyN7KOvoFUfbhux reposted aFikDry7Of4v7MQ\r\nvb6LbwA reposted sObIWYN7KOvoFufBHUx\r\nDtWKIcVwIHgj4Rcv reposted vb6lbwa\r\nkt reposted DTwKicvwihgJ4rCV\r\n75K reposted kT\r\njKzyxx1 reposted 75K\r\nuoS reposted jkZyXX1\r\npZJskHTCIqE3YyZ5ME reposted uoS\r\n", "output": "11\r\n"}, {"input": "10\r\nvxrUpCXvx8Isq reposted pOLYcaRP\r\nICb1 reposted vXRUpCxvX8ISq\r\nJFMt4b8jZE7iF2m8by7y2 reposted Icb1\r\nqkG6ZkMIf9QRrBFQU reposted ICb1\r\nnawsNfcR2palIMnmKZ reposted pOlYcaRP\r\nKksyH reposted jFMT4b8JzE7If2M8by7y2\r\nwJtWwQS5FvzN0h8CxrYyL reposted NawsNfcR2paLIMnmKz\r\nDpBcBPYAcTXEdhldI6tPl reposted NaWSnFCr2pALiMnmkZ\r\nlEnwTVnlwdQg2vaIRQry reposted kKSYh\r\nQUVFgwllaWO reposted Wjtwwqs5FVzN0H8cxRyyl\r\n", "output": "6\r\n"}, {"input": "10\r\nkkuLGEiHv reposted POLYcArp\r\n3oX1AoUqyw1eR3nCADY9hLwd reposted kkuLGeIHV\r\nwf97dqq5bx1dPIchCoT reposted 3OX1AOuQYW1eR3ncAdY9hLwD\r\nWANr8h reposted Wf97dQQ5bx1dpIcHcoT\r\n3Fb736lkljZK2LtSbfL reposted wANR8h\r\n6nq9xLOn reposted 3fB736lKlJZk2LtSbFL\r\nWL reposted 3Fb736lKLjZk2LTSbfl\r\ndvxn4Xtc6SBcvKf1 reposted wF97DQq5bX1dPiChCOt\r\nMCcPLIMISqxDzrj reposted 6nQ9XLOn\r\nxsQL4Z2Iu reposted MCcpLiMiSqxdzrj\r\n", "output": "9\r\n"}, {"input": "10\r\nsMA4 reposted pOLyCARP\r\nlq3 reposted pOlycARp\r\nEa16LSFTQxLJnE reposted polYcARp\r\nkvZVZhJwXcWsnC7NA1DV2WvS reposted polYCArp\r\nEYqqlrjRwddI reposted pOlyCArP\r\nsPqQCA67Y6PBBbcaV3EhooO reposted ea16LSFTqxLJne\r\njjPnneZdF6WLZ3v reposted Ea16LSFTqxLjNe\r\nWEoi6UpnfBUx79 reposted ea16LSFtqXljNe\r\nqi4yra reposted eYqqlRJrWDDI\r\ncw7E1UCSUD reposted eYqqLRJRwDdI\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
46/C
|
46
|
C
|
PyPy 3-64
|
TESTS
| 7
| 122
| 512,000
|
145802779
|
n = int(input())
init = list(input())
beg = init[0]
end = init[n-1]
i = 1
j = n-2
counts = 0
if beg == end:
it = init.count(beg)
while j-i > n-it:
if init[i] == beg:
i += 1
elif init[j] == end:
j -= 1
else:
tmp = init[i]
init[i] = init[j]
init[j] = tmp
counts += 1
i += 1
j -= 1
else:
while i < j:
if init[i] == beg:
i += 1
elif init[j] == end:
j -= 1
else:
tmp = init[i]
init[i] = init[j]
init[j] = tmp
counts += 1
i += 1
j -= 1
print(counts)
| 27
| 92
| 0
|
218382997
|
num=int(input())
st=input()
h=st.count('H')
print (min((st+st)[i:i+h].count('T') for i in range(num)))
|
School Personal Contest #2 (Winter Computer School 2010/11) - Codeforces Beta Round 43 (ACM-ICPC Rules)
|
ICPC
| 2,010
| 2
| 256
|
Hamsters and Tigers
|
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
|
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
|
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
| null |
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
|
[{"input": "3\nHTH", "output": "0"}, {"input": "9\nHTHTHTHHT", "output": "2"}]
| 1,600
|
["two pointers"]
| 27
|
[{"input": "3\r\nHTH\r\n", "output": "0\r\n"}, {"input": "9\r\nHTHTHTHHT\r\n", "output": "2\r\n"}, {"input": "2\r\nTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTHT\r\n", "output": "1\r\n"}, {"input": "7\r\nTTTHTTT\r\n", "output": "0\r\n"}, {"input": "8\r\nHHTHHTHH\r\n", "output": "1\r\n"}, {"input": "13\r\nHTTTHHHTTTTHH\r\n", "output": "3\r\n"}, {"input": "20\r\nTTHTHTHHTHTTHHTTTHHH\r\n", "output": "4\r\n"}, {"input": "35\r\nTTTTTTHTTHTTTTTHTTTTTTTTTTTHTHTTTTT\r\n", "output": "3\r\n"}, {"input": "120\r\nTTTTTTTHTHTHTTTTTHTHTTTTHTTTTTTTTTTTTTTTTTTTTHTTHTTTTHTTHTTTTTTTTTTTTTTTHTTTTTTHTHTTHTTTTTTHTTTTTTTTTHTTHTTTTHTTTHTTTTTH\r\n", "output": "14\r\n"}, {"input": "19\r\nHHHHHHHHHHHHHTTTHHH\r\n", "output": "0\r\n"}, {"input": "87\r\nHTHHTTHHHHTHHHHHTTTHHTHHHHTTTTHHHTTHHTHTHTHHTTHTHHTHTHTTHHHTTTTTHTTHHHHHHTHHTHHTHTTHTHH\r\n", "output": "17\r\n"}, {"input": "178\r\nTHHHTHTTTHTTHTTHHHHHTTTHTTHHTHTTTHTHTTTTTHHHTHTHHHTHHHTTTTTTTTHHHHTTHHTHHHHTHTTTHHHHHHTHHTHTTHTHTTTTTTTTTHHTTHHTHTTHHTHHHHHTTHHTTHHTTHHHTTHHTTTTHTHHHTHTTHTHTTTHHHHTHHTHHHTHTTTTTT\r\n", "output": "40\r\n"}]
| false
|
stdio
| null | true
|
60/E
|
60
|
E
|
Python 3
|
TESTS
| 5
| 186
| 307,200
|
91180410
|
import sys
I = lambda: int(input())
RL = readline = lambda: sys.stdin.readline().strip('\n')
RM = readmap = lambda x = int: map(x,readline().split(' '))
#E
'''
solution:
sum grows exponentially if the sum of array at 0th min is s and the sum of
start and end is e then sum in the next minute is s + (2*s - e)
after x days and rearragning them the start is same but the end is
different if x>0 and is equal to a[-2] + x*a[-1]
or same if x==0 i.e the end is a[-1]
'''
n,x,y,p = RM()
a = [*RM()]
s = temps = sum(a)
ends = a[0] + a[-1]
for i in range(x):
temps = (3*temps - ends)%p
ends = a[0] + ((a[-2] + x*a[-1]) if x>0 else a[-1])
for i in range(y):
temps = (3*temps - ends)%p
print(temps)
'''
4 0 8 78731972
1 52 76 81
'''
| 58
| 780
| 120,320,000
|
222999862
|
def power(a, b, p):
c = 1
while b:
if b & 1:
c = (c * a) % p
a = (a * a) % p
b //= 2
return c
def get3(b, p):
if b == 0:
return 0
if b == 1:
return 1
if b & 1:
x = get3(b - 1, p)
return (3 * x + 1) % p
else:
x = get3(b // 2, p)
return (2 * x * (x + 1)) % p
def fib(b, p):
e = [[1, 0], [0, 1]]
g = [[1, 1], [1, 0]]
while b:
if b & 1:
w = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
w[i][j] = (w[i][j] + e[i][k] * g[k][j]) % p
for i in range(2):
for j in range(2):
e[i][j] = w[i][j]
w = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
w[i][j] = (w[i][j] + g[i][k] * g[k][j]) % p
for i in range(2):
for j in range(2):
g[i][j] = w[i][j]
b //= 2
return (e[0][0], e[0][1])
n, x, y, p = map(int, input().split())
a = list(map(int, input().split()))
sum_a = sum(a) % p
if n == 1:
print(sum_a % p)
exit()
ans = (power(3, x, p) * sum_a - get3(x, p) * (a[0] + a[-1])) % p
tmp = fib(x, p)
A = a[0]
B = (a[-1] * tmp[0] + a[-2] * tmp[1]) % p
ans = (power(3, y, p) * ans - get3(y, p) * (A + B)) % p
if ans < 0:
ans += p
print(ans)# 1694458405.418199
|
Codeforces Beta Round 56
|
CF
| 2,011
| 3
| 256
|
Mushroom Gnomes
|
Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible that between every two neighboring mushrooms every minute grew another mushroom with the weight equal to the sum of weights of two neighboring ones.
The mushroom gnomes loved it when everything was in order, that's why they always planted the mushrooms in one line in the order of their weights' increasing. Well... The gnomes planted the mushrooms and went to eat. After x minutes they returned and saw that new mushrooms had grown up, so that the increasing order had been violated. The gnomes replanted all the mushrooms in the correct order, that is, they sorted the mushrooms in the order of the weights' increasing. And went to eat again (those gnomes were quite big eaters). What total weights modulo p will the mushrooms have in another y minutes?
|
The first line contains four integers n, x, y, p (1 ≤ n ≤ 106, 0 ≤ x, y ≤ 1018, x + y > 0, 2 ≤ p ≤ 109) which represent the number of mushrooms, the number of minutes after the first replanting, the number of minutes after the second replanting and the module. The next line contains n integers ai which represent the mushrooms' weight in the non-decreasing order (0 ≤ ai ≤ 109).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
|
The answer should contain a single number which is the total weights of the mushrooms modulo p in the end after x + y minutes.
| null | null |
[{"input": "2 1 0 657276545\n1 2", "output": "6"}, {"input": "2 1 1 888450282\n1 2", "output": "14"}, {"input": "4 5 0 10000\n1 2 3 4", "output": "1825"}]
| 2,600
|
["math", "matrices"]
| 58
|
[{"input": "2 1 0 657276545\r\n1 2\r\n", "output": "6\r\n"}, {"input": "2 1 1 888450282\r\n1 2\r\n", "output": "14\r\n"}, {"input": "4 5 0 10000\r\n1 2 3 4\r\n", "output": "1825\r\n"}, {"input": "4 0 8 78731972\r\n1 52 76 81\r\n", "output": "1108850\r\n"}, {"input": "4 0 8 414790855\r\n1 88 97 99\r\n", "output": "1541885\r\n"}, {"input": "11 10 6 560689961\r\n2 17 20 24 32 37 38 39 40 61 86\r\n", "output": "9840917\r\n"}, {"input": "8 4 9 371687114\r\n1 7 22 31 35 38 62 84\r\n", "output": "1827639\r\n"}, {"input": "4 8 6 398388678\r\n21 22 78 88\r\n", "output": "338926799\r\n"}, {"input": "33 93 37 411512841\r\n71 76 339 357 511 822 1564 1747 1974 2499 2763 3861 3950 4140 4306 4992 5056 5660 5694 5773 6084 6512 6742 6898 7133 8616 8772 8852 8918 9046 9572 9679 9708\r\n", "output": "158919800\r\n"}, {"input": "1 1 0 2\r\n1\r\n", "output": "1\r\n"}, {"input": "1 1 1 2\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "1 1 1 2\r\n0\r\n", "output": "0\r\n"}, {"input": "1 1 1 2\r\n2\r\n", "output": "0\r\n"}, {"input": "1 0 1 1000000000\r\n0\r\n", "output": "0\r\n"}, {"input": "1 1 1 1000000000\r\n1\r\n", "output": "1\r\n"}, {"input": "1 0 1 1000000000\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2 1 1 1000\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2 1000000000 1000000000000 10000\r\n1 2\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
400/B
|
400
|
B
|
PyPy 3-64
|
TESTS
| 4
| 61
| 2,048,000
|
193219056
|
n, m = map(int, input().split())
map_dict = dict()
for i in range(n):
word = input()
if word.rfind('S') < word.rfind('G'):
print(-1)
break
s = []
for j, k in enumerate(word):
s = list(map(lambda x: (x[0], x[1] + 1), s))
if k == 'G':
s.append((j, 0))
elif k == 'S' and s:
map_dict[s[-1][0]] = map_dict.get(s[-1][0], set()) | {s[-1][1]}
else:
out = 0
while map_dict:
# print(map_dict)
out += 1
key = list(sorted(map_dict.keys()))[0]
points = map_dict[key]
min_point = min(points)
points -= {min_point}
del map_dict[key]
if points:
points = map(lambda x: x - min_point, points)
map_dict[key + min_point] = map_dict.get(key + min_point, set()) | set(points)
print(out)
| 34
| 46
| 204,800
|
160226508
|
a, b = map(int, input().split())
array = [0] * a
for i in range(a):
temp = input()
array[i] = temp.find("S") - temp.find("G")
array.sort()
if array[0] > 0:
if len(array) != 1:
count = 1
for i in range(1, a):
if array[i] != array[i-1]:
count+= 1
print(count)
else:
print(1)
else:
print(-1)
|
Codeforces Round 234 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and New Matrix of Candies
|
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".
The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:
- some dwarf in one of the chosen lines is located in the rightmost cell of his row;
- some dwarf in the chosen lines is located in the cell with the candy.
The point of the game is to transport all the dwarves to the candy cells.
Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
|
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).
Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
|
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
| null | null |
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
| 1,200
|
["brute force", "implementation", "schedules"]
| 34
|
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
523/A
|
523
|
A
|
Python 3
|
TESTS
| 1
| 31
| 0
|
189247879
|
str = ''
col, row = [*map(int, input().split())]
lst = [input() for i in range(row)]
for i in range(col):
str = ""
for j in range(row):
str += lst[-j - 1][-i - 1] * 2
print(str)
print(str)
| 24
| 61
| 0
|
10274243
|
def mash(pole):
ans = []
for i in range(len(pole)):
s = pole[i]
new = ''
for i in s:
new += (i * 2)
ans.append(new)
ans.append(new)
return ans
def change(pole, a, b):
x = len(pole[0]) % 2
fir = []
sec = []
new = []
for i in range(b):
s = ''
for j in range(a):
s += pole[j][i]
if i + 1 <= len(pole[0]) // 2:
fir.append(s)
continue
if i + 1 >= (len(pole[0]) // 2 + x):
sec.append(s)
for i in range(len(sec[0])):
s = ''
for j in range(len(sec) + len(fir)):
if j < len(sec):
s += sec[j][i]
else:
s += fir[j - len(sec)][i]
new.append(s)
return new
def pov(pole, a, b):
new = []
for i in range(a):
s = ''
for j in range(b):
s += pole[j][i]
new.append(s)
return new
a, b = map(int, input().split())
pole = []
for i in range(b):
s = input()
pole.append(s)
new = mash(pov(pole, a, b))
for i in new:
print(i)
|
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
|
28/B
|
28
|
B
|
Python 3
|
TESTS
| 1
| 92
| 0
|
167818781
|
import random
print(random.choice(["YES","NO"]))
| 33
| 124
| 0
|
199147462
|
import sys; R = sys.stdin.readline
S = lambda: map(int,R().split())
from collections import deque
n = int(R())
a = [0]+[*S()]
b = [*S()]
e = [[] for _ in range(n+1)]
for i,x in enumerate(b,1):
if i+x<=n: e[i] += i+x,; e[i+x] += i,
if 1<=i-x: e[i] += i-x,; e[i-x] += i,
vv = [0]*(n+1)
for i in range(1,n+1):
if not vv[i]:
vv[i] = 1
c,d = [i],[a[i]]
q = deque([i])
while q:
u = q.popleft()
for v in e[u]:
if not vv[v]:
vv[v] = 1
c += v,; d += a[v],; q += v,
if sorted(c)!=sorted(d): print("NO"); exit()
print("YES")
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
pSort
|
One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.
The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.
|
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.
|
If the given state is reachable in the described game, output YES, otherwise NO.
| null | null |
[{"input": "5\n5 4 3 2 1\n1 1 1 1 1", "output": "YES"}, {"input": "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1", "output": "NO"}, {"input": "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1", "output": "YES"}]
| 1,600
|
["dfs and similar", "dsu", "graphs"]
| 33
|
[{"input": "5\r\n5 4 3 2 1\r\n1 1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "7\r\n4 3 5 1 2 7 6\r\n4 6 6 1 6 6 1\r\n", "output": "NO\r\n"}, {"input": "7\r\n4 2 5 1 3 7 6\r\n4 6 6 1 6 6 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n3 2 1 4 6 5\r\n3 6 1 6 6 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n3 5 1 4 6 2\r\n3 6 1 6 6 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 2 3 4\r\n1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "13\r\n13 1 12 4 6 10 7 8 9 3 11 5 2\r\n6 12 12 7 11 7 10 3 1 5 2 2 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 3 2 4 5\r\n1 4 1 2 4\r\n", "output": "YES\r\n"}, {"input": "10\r\n6 2 9 4 8 7 5 10 3 1\r\n2 9 7 9 4 5 1 5 3 2\r\n", "output": "YES\r\n"}, {"input": "8\r\n5 2 3 4 8 6 1 7\r\n6 7 7 7 7 7 2 7\r\n", "output": "YES\r\n"}, {"input": "17\r\n1 11 3 4 5 6 7 8 9 10 2 12 13 14 15 16 17\r\n13 9 16 5 16 12 11 4 4 7 12 16 2 7 14 6 3\r\n", "output": "YES\r\n"}, {"input": "19\r\n7 2 17 18 4 16 1 9 12 10 8 11 6 13 14 19 3 5 15\r\n12 4 9 1 13 18 14 10 18 2 17 16 12 3 16 6 5 7 7\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n2 1\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "2\r\n2 1\r\n1 1\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
28/A
|
28
|
A
|
Python 3
|
TESTS
| 9
| 218
| 307,200
|
67650423
|
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(sum(s[i])-sum(s[i-1])) + abs(sum(s[i])-sum(s[i+1]))
s_chet.append(q)
q1 = abs(sum(s[-1])-sum(s[-2])) + abs(sum(s[-1])-sum(s[0]))
s_chet.append(q1)
s_ch = s_chet.copy()
s_nechet = []
for i in range(2,n-1,2): #Проход по нечетным гвоздям
qq = abs(sum(s[i])-sum(s[i-1])) + abs(sum(s[i])-sum(s[i+1]))
s_nechet.append(qq)
qq1 = abs(sum(s[-1])-sum(s[0])) + abs(sum(s[1])-sum(s[0]))
s_nechet.append(qq1)
s_n = s_nechet.copy()
ss = map(int,input().split())
ss = list(ss)
ss1 = ss.copy()
ss2 = ss.copy()
for i in s_chet:
if i in ss1:
s_ch.remove(i)
ss1.remove(i)
if len(s_ch) == 0:
print('YES')
sss = []
for i in range(1,m+1):
sss.append(-1)
sss.append(i)
print(" ".join(map(str,sss)))
else:
for i in s_nechet:
if i in ss2:
s_n.remove(i)
ss2.remove(i)
if len(s_n) == 0:
print('YES')
sss = []
for i in range(1,m+1):
sss.append(i)
sss.append(-1)
print(" ".join(map(str,sss)))
else:
print('NO')
| 51
| 154
| 409,600
|
13001422
|
from collections import defaultdict
def main():
n, m = map(int, input().split())
tmp = list(tuple(map(int, input().split())) for _ in range(n))
nails = [abs(a - c) + abs(b - d) for (a, b), (c, d) in zip(tmp, tmp[2:] + tmp[:2])]
segments = defaultdict(list)
for i, s in enumerate(map(int, input().split()), 1):
segments[s].append(i)
for shift in -1, 0:
res = [-1] * n
for nailidx in range(shift, n + shift, 2):
nail = nails[nailidx]
if nail in segments and segments[nail]:
res[(nailidx + 1) % n] = segments[nail].pop()
else:
break
else:
print("YES")
print(" ".join(map(str, res)))
return
print("NO")
if __name__ == '__main__':
main()
|
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
|
1212/D
|
977
|
D
|
PyPy 3-64
|
TESTS
| 0
| 61
| 0
|
227965901
|
def getTuple(x):
count_power_of_three = sum(1 for i in range(60) if x % (3**i) == 0)
return (count_power_of_three, -x)
n = int(input())
a = list(map(int, input().split()))
start = max(a, key=getTuple)
ans = [start]
a.remove(start)
while len(ans) < n:
next_value = ans[-1] // 3 if ans[-1] % 3 == 0 else ans[-1] * 2
if next_value in a:
ans.append(next_value)
a.remove(next_value)
else:
break # If the next value is not in 'a', break the loop.
# If the 'ans' list is not of length 'n', it means it's not possible to form the required sequence.
if len(ans) == n:
print(*ans)
else:
print("It's not possible to form the required sequence.")
| 26
| 46
| 6,144,000
|
225452589
|
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
def top(mp=defaultdict(int),x=1,n=1):
if n==1 and mp[x]==1 or n==0:
return [x]
if mp[x]==0:
return -1
mp[x]-=1
k=top(mp,2*x,n-1)
mp[x]+=1
if k!=-1:
return k+[x]
if x%3==0:
mp[x]-=1
k=top(mp,x//3,n-1)
mp[x]+=1
return k if k==-1 else k+[x]
return -1
mp=defaultdict(int)
for i in a:
mp[i]+=1
for i in a:
arr=top(mp,i,n)
# print(arr)
if arr!=-1:
print(*arr[::-1])
break
# print(mp)
|
Kotlin Heroes: Practice 2
|
ICPC
| 2,019
| 1
| 256
|
Divide by three, multiply by two
|
Polycarp likes to play with numbers. He takes some integer number $$$x$$$, writes it down on the board, and then performs with it $$$n - 1$$$ operations of the two kinds:
- divide the number $$$x$$$ by $$$3$$$ ($$$x$$$ must be divisible by $$$3$$$);
- multiply the number $$$x$$$ by $$$2$$$.
After each operation, Polycarp writes down the result on the board and replaces $$$x$$$ by the result. So there will be $$$n$$$ numbers on the board after all.
You are given a sequence of length $$$n$$$ — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
|
The first line of the input contatins an integer number $$$n$$$ ($$$2 \le n \le 100$$$) — the number of the elements in the sequence. The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 3 \cdot 10^{18}$$$) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
|
Print $$$n$$$ integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
| null |
In the first example the given sequence can be rearranged in the following way: $$$[9, 3, 6, 12, 4, 8]$$$. It can match possible Polycarp's game which started with $$$x = 9$$$.
|
[{"input": "6\n4 8 6 3 12 9", "output": "9 3 6 12 4 8"}, {"input": "4\n42 28 84 126", "output": "126 42 84 28"}, {"input": "2\n1000000000000000000 3000000000000000000", "output": "3000000000000000000 1000000000000000000"}]
| 1,400
|
["*special", "math"]
| 26
|
[{"input": "6\r\n4 8 6 3 12 9\r\n", "output": "9 3 6 12 4 8 \r\n"}, {"input": "4\r\n42 28 84 126\r\n", "output": "126 42 84 28 \r\n"}, {"input": "2\r\n1000000000000000000 3000000000000000000\r\n", "output": "3000000000000000000 1000000000000000000 \r\n"}, {"input": "19\r\n46875000000000000 732421875000000 5859375000000000 11444091796875 2929687500000000 187500000000000000 91552734375000 11718750000000000 3000000000000000000 22888183593750 1464843750000000 375000000000000000 45776367187500 183105468750000 93750000000000000 366210937500000 23437500000000000 750000000000000000 1500000000000000000\r\n", "output": "11444091796875 22888183593750 45776367187500 91552734375000 183105468750000 366210937500000 732421875000000 1464843750000000 2929687500000000 5859375000000000 11718750000000000 23437500000000000 46875000000000000 93750000000000000 187500000000000000 375000000000000000 750000000000000000 1500000000000000000 3000000000000000000 \r\n"}, {"input": "6\r\n558 744 1488 279 2232 1116\r\n", "output": "279 558 1116 2232 744 1488 \r\n"}, {"input": "17\r\n2985984 2239488 7077888 5971968 10616832 746496 28311552 3538944 7962624 3145728 15925248 1492992 14155776 5308416 3981312 11943936 9437184\r\n", "output": "2239488 746496 1492992 2985984 5971968 11943936 3981312 7962624 15925248 5308416 10616832 3538944 7077888 14155776 28311552 9437184 3145728 \r\n"}, {"input": "18\r\n47775744 7077888 5971968 3538944 4478976 3145728 2985984 4718592 1572864 5308416 1048576 1492992 23887872 10616832 2239488 11943936 15925248 14155776\r\n", "output": "2239488 4478976 1492992 2985984 5971968 11943936 23887872 47775744 15925248 5308416 10616832 3538944 7077888 14155776 4718592 1572864 3145728 1048576 \r\n"}, {"input": "3\r\n9000 1000 3000\r\n", "output": "9000 3000 1000 \r\n"}, {"input": "2\r\n3000 9000\r\n", "output": "9000 3000 \r\n"}, {"input": "2\r\n3000000000000000000 1000000000000000000\r\n", "output": "3000000000000000000 1000000000000000000 \r\n"}, {"input": "2\r\n1 3\r\n", "output": "3 1 \r\n"}, {"input": "2\r\n1500000000000000000 3000000000000000000\r\n", "output": "1500000000000000000 3000000000000000000 \r\n"}, {"input": "3\r\n4 1 2\r\n", "output": "1 2 4 \r\n"}, {"input": "2\r\n2000000000000000004 1000000000000000002\r\n", "output": "1000000000000000002 2000000000000000004 \r\n"}, {"input": "2\r\n2999999999999999997 999999999999999999\r\n", "output": "2999999999999999997 999999999999999999 \r\n"}, {"input": "2\r\n999999999999999999 1999999999999999998\r\n", "output": "999999999999999999 1999999999999999998 \r\n"}, {"input": "2\r\n1999999999999999998 999999999999999999\r\n", "output": "999999999999999999 1999999999999999998 \r\n"}, {"input": "2\r\n10 5\r\n", "output": "5 10 \r\n"}]
| false
|
stdio
| null | true
|
794/C
|
794
|
C
|
Python 3
|
PRETESTS
| 6
| 46
| 0
|
27084734
|
a = sorted(input())
b = sorted(input())
l = len(a)
result = ""
if a[0] > b[-1]:
ai = (l + 1) // 2 - 1
bi = l - l // 2
result += a[ai]
ai -= 1
while len(result) < len(a):
result = b[bi] + result
bi += 1
if len(result) == len(a):
break
result = a[ai] + result
ai -= 1
else:
ai = 0
bi = l - 1
while len(result) < len(a):
result += a[ai]
ai += 1
if len(result) == len(a):
break
result += b[bi]
bi -= 1
print(result)
| 53
| 389
| 25,907,200
|
48483922
|
a = sorted(input())
b = sorted(input(), reverse=True)
n = len(a)
a = ''.join(a[:(n+1)//2])
b = ''.join(b[:n//2])
name = ['']*(len(a)+len(b))
ia = ib = ic = 0
ja = len(a)-1
jb = len(b)-1
jc = len(name)-1
turn = 1
while ic <= jc:
if turn == 1:
if ib > jb: name[ic] = a[ia]; ic+= 1
elif a[ia] < b[ib]: name[ic] = a[ia]; ia+= 1; ic+= 1
else: name[jc] = a[ja]; ja-= 1; jc-= 1
else:
if ia > ja: name[ic] = b[ib]; ic+= 1
elif b[ib] > a[ia]: name[ic] = b[ib]; ib+= 1; ic+= 1
else: name[jc] = b[jb]; jb-= 1; jc-= 1
turn = 3-turn
print(''.join(name))
|
Tinkoff Challenge - Final Round (Codeforces Round 414, rated, Div. 1 + Div. 2)
|
CF
| 2,017
| 2
| 256
|
Naming Company
|
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
|
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
|
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
| null |
One way to play optimally in the first sample is as follows :
- Initially, the company name is ???????.
- Oleg replaces the first question mark with 'f'. The company name becomes f??????.
- Igor replaces the second question mark with 'z'. The company name becomes fz?????.
- Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
- Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
- Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
- Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
- Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx.
|
[{"input": "tinkoff\nzscoder", "output": "fzfsirk"}, {"input": "xxxxxx\nxxxxxx", "output": "xxxxxx"}, {"input": "ioi\nimo", "output": "ioi"}]
| 1,800
|
["games", "greedy", "sortings"]
| 53
|
[{"input": "tinkoff\r\nzscoder\r\n", "output": "fzfsirk\r\n"}, {"input": "xxxxxx\r\nxxxxxx\r\n", "output": "xxxxxx\r\n"}, {"input": "ioi\r\nimo\r\n", "output": "ioi\r\n"}, {"input": "abc\r\naaa\r\n", "output": "aab\r\n"}, {"input": "reddit\r\nabcdef\r\n", "output": "dfdeed\r\n"}, {"input": "cbxz\r\naaaa\r\n", "output": "abac\r\n"}, {"input": "bcdef\r\nabbbc\r\n", "output": "bccdb\r\n"}, {"input": "z\r\ny\r\n", "output": "z\r\n"}, {"input": "y\r\nz\r\n", "output": "y\r\n"}]
| false
|
stdio
| null | true
|
794/C
|
794
|
C
|
Python 3
|
TESTS
| 6
| 46
| 204,800
|
27113154
|
a=list(input())
b=list(input())
y=len(a)
l=int(y//2)+y%2
r=y-l+y%2
a.sort()
b.sort()
al=a[:l]
br=b[r:]
t=["?"]*y
wq=0
wp=y-1
i=0
while "?" in t:
if i%2==0:
if t.count("?")==1:
t[t.index("?")]=al[0]
elif min(al)>=max(br):
t[wp]=max(al)
al.remove(max(al))
wp-=1
else:
t[wq]=min(al)
al.remove(min(al))
wq+=1
else:
if t.count("?")==1:
t[t.index("?")]=br[0]
elif min(al)>=max(br):
t[wp]=min(br)
b.remove(min(br))
wp-=1
else:
t[wq]=max(b)
b.remove(max(b))
wq+=1
i+=1
print("".join(t))
| 53
| 420
| 5,222,400
|
27156642
|
str1 = list(input())
str2 = list(input())
str1.sort()
str2.sort(reverse=True)
n = len(str1)
str = ['?'] * n
i, j, ni, nj = 0, 0, (n + 1) // 2 - 1, n // 2 - 1
front, rear = 0, n - 1
for cur in range(n):
if cur & 1 == 0:
if (str1[i] < str2[j]):
str[front] = str1[i]
i += 1
front += 1
else:
str[rear] = str1[ni]
ni -= 1
rear -= 1
else:
if (str1[i] < str2[j]):
str[front] = str2[j]
j += 1
front += 1
else:
str[rear] = str2[nj]
nj -= 1
rear -= 1
print(''.join(str))
|
Tinkoff Challenge - Final Round (Codeforces Round 414, rated, Div. 1 + Div. 2)
|
CF
| 2,017
| 2
| 256
|
Naming Company
|
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
|
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
|
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
| null |
One way to play optimally in the first sample is as follows :
- Initially, the company name is ???????.
- Oleg replaces the first question mark with 'f'. The company name becomes f??????.
- Igor replaces the second question mark with 'z'. The company name becomes fz?????.
- Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
- Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
- Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
- Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
- Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx.
|
[{"input": "tinkoff\nzscoder", "output": "fzfsirk"}, {"input": "xxxxxx\nxxxxxx", "output": "xxxxxx"}, {"input": "ioi\nimo", "output": "ioi"}]
| 1,800
|
["games", "greedy", "sortings"]
| 53
|
[{"input": "tinkoff\r\nzscoder\r\n", "output": "fzfsirk\r\n"}, {"input": "xxxxxx\r\nxxxxxx\r\n", "output": "xxxxxx\r\n"}, {"input": "ioi\r\nimo\r\n", "output": "ioi\r\n"}, {"input": "abc\r\naaa\r\n", "output": "aab\r\n"}, {"input": "reddit\r\nabcdef\r\n", "output": "dfdeed\r\n"}, {"input": "cbxz\r\naaaa\r\n", "output": "abac\r\n"}, {"input": "bcdef\r\nabbbc\r\n", "output": "bccdb\r\n"}, {"input": "z\r\ny\r\n", "output": "z\r\n"}, {"input": "y\r\nz\r\n", "output": "y\r\n"}]
| false
|
stdio
| null | true
|
28/B
|
28
|
B
|
Python 3
|
TESTS
| 2
| 124
| 6,963,200
|
115965535
|
size = int(input())
if size < 1 or size > 100:
exit()
permutations_dict = {}
array = list(map(int, input().split(" ")))
ready = list(map(int, input().split(" ")))
permutations = ready.copy()
for i in range(len(permutations)):
permutations[i] = [permutations[i]]
for j in range(len(array)):
if i == j:
permutations[i].append(array[j])
value, key = permutations[i][0], permutations[i][1]
if key in permutations_dict:
permutations_dict[key].append(value)
else:
permutations_dict[key] = [value]
#print(permutations)
#print(permutations_dict)
while (array != ready):
#print("array: ", array, " ", ready)
for i in range(len(array)):
try:
if (array[i] - array[i + 1]) in permutations_dict[array[i]]:
array[i] = array[i] - permutations_dict[array[i]][0]
break
except IndexError:
print("NO")
exit()
print("YES")
exit()
| 33
| 124
| 0
|
210278725
|
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import defaultdict,Counter
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = [i for i in range(1,N+1)]
P = [[] for _ in range(N)]
for i in range(N):
b = B[i]
if i-b>=0:
P[i].append(i-b)
P[i-b].append(i)
if i+b<N:
P[i].append(i+b)
P[i+b].append(i)
seen = [0]*N
def paint(idx, color):
v = [idx]
while v:
i = v.pop()
if seen[i]:continue
seen[i] = color
for j in P[i]:
if seen[j]:continue
v.append(j)
color = 0
for i in range(N):
if seen[i]==0:
color+=1
paint(i,color)
lib = defaultdict(Counter)
for i in range(N):
lib[seen[i]][A[i]]+=1
lib[seen[i]][D[i]]+=1
for k,v in lib.items():
for k2,v2 in v.items():
if v2!=2:
exit(print('NO'))
print('YES')
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
pSort
|
One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.
The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.
|
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.
|
If the given state is reachable in the described game, output YES, otherwise NO.
| null | null |
[{"input": "5\n5 4 3 2 1\n1 1 1 1 1", "output": "YES"}, {"input": "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1", "output": "NO"}, {"input": "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1", "output": "YES"}]
| 1,600
|
["dfs and similar", "dsu", "graphs"]
| 33
|
[{"input": "5\r\n5 4 3 2 1\r\n1 1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "7\r\n4 3 5 1 2 7 6\r\n4 6 6 1 6 6 1\r\n", "output": "NO\r\n"}, {"input": "7\r\n4 2 5 1 3 7 6\r\n4 6 6 1 6 6 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n3 2 1 4 6 5\r\n3 6 1 6 6 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n3 5 1 4 6 2\r\n3 6 1 6 6 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 2 3 4\r\n1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "13\r\n13 1 12 4 6 10 7 8 9 3 11 5 2\r\n6 12 12 7 11 7 10 3 1 5 2 2 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 3 2 4 5\r\n1 4 1 2 4\r\n", "output": "YES\r\n"}, {"input": "10\r\n6 2 9 4 8 7 5 10 3 1\r\n2 9 7 9 4 5 1 5 3 2\r\n", "output": "YES\r\n"}, {"input": "8\r\n5 2 3 4 8 6 1 7\r\n6 7 7 7 7 7 2 7\r\n", "output": "YES\r\n"}, {"input": "17\r\n1 11 3 4 5 6 7 8 9 10 2 12 13 14 15 16 17\r\n13 9 16 5 16 12 11 4 4 7 12 16 2 7 14 6 3\r\n", "output": "YES\r\n"}, {"input": "19\r\n7 2 17 18 4 16 1 9 12 10 8 11 6 13 14 19 3 5 15\r\n12 4 9 1 13 18 14 10 18 2 17 16 12 3 16 6 5 7 7\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n2 1\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "2\r\n2 1\r\n1 1\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
279/C
|
279
|
C
|
PyPy 3-64
|
TESTS
| 20
| 1,746
| 16,896,000
|
202446013
|
import sys
input = sys.stdin.readline
from bisect import bisect
n, m = map(int, input().split())
w = [0] + list(map(int, input().split()))
d = [0]
a = 1
for i in range(1, n):
if a == 1:
if w[i] > w[i+1]:
d.append(i)
a = 0
else:
if w[i] < w[i+1]:
d.append(i)
a = 1
for i in range(m):
a, b = map(int, input().split())
a1 = bisect(d, a)
a2 = bisect(d, b)
if d[a2-1] == b:
a2 -= 1
if a1 >= a2 or (a2-a1 == 1 and a1 % 2):
print('Yes')
else:
print('No')
| 35
| 748
| 15,769,600
|
202449796
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
w = list(map(int, input().split()))
a = [i for i in range(n)]
b = [i for i in range(n)]
for i in range(1, n):
if w[i] >= w[i-1]:
a[i] = a[i-1]
if w[i] <= w[i-1]:
b[i] = b[i-1]
for i in range(m):
x, y = map(lambda z:int(z)-1, input().split())
if a[b[y]] <= x:
print('Yes')
else:
print('No')
|
Codeforces Round 171 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Ladder
|
You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original array, that is, the sequence of numbers ali, ali + 1, ali + 2, ..., ari. For each query you should check whether the corresponding segment is a ladder.
A ladder is a sequence of integers b1, b2, ..., bk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b1 ≤ b2 ≤ ... ≤ bx ≥ bx + 1 ≥ bx + 2... ≥ bk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of array elements and the number of queries. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where number ai stands for the i-th array element.
The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the subsegment of the initial array.
The numbers in the lines are separated by single spaces.
|
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
| null | null |
[{"input": "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8", "output": "Yes\nYes\nNo\nYes\nNo\nYes"}]
| 1,700
|
["dp", "implementation", "two pointers"]
| 35
|
[{"input": "8 6\r\n1 2 1 3 3 5 2 1\r\n1 3\r\n2 3\r\n2 4\r\n8 8\r\n1 4\r\n5 8\r\n", "output": "Yes\r\nYes\r\nNo\r\nYes\r\nNo\r\nYes\r\n"}, {"input": "1 1\r\n6\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "2 5\r\n1 1\r\n1 2\r\n2 2\r\n2 2\r\n1 2\r\n1 2\r\n", "output": "Yes\r\nYes\r\nYes\r\nYes\r\nYes\r\n"}, {"input": "10 10\r\n4 7 2 6 4 8 1 2 1 9\r\n6 10\r\n1 9\r\n9 9\r\n2 8\r\n9 9\r\n1 1\r\n8 8\r\n4 8\r\n8 8\r\n7 7\r\n", "output": "No\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nYes\r\nNo\r\nYes\r\nYes\r\n"}, {"input": "7 5\r\n13 13 16 12 16 3 19\r\n2 7\r\n3 4\r\n7 7\r\n1 2\r\n4 7\r\n", "output": "No\r\nYes\r\nYes\r\nYes\r\nNo\r\n"}, {"input": "13 6\r\n2 6 1 3 5 2 2 1 6 4 2 5 2\r\n10 13\r\n4 10\r\n4 11\r\n3 5\r\n9 13\r\n3 13\r\n", "output": "No\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\n"}, {"input": "20 20\r\n17 11 7 4 1 17 7 20 12 12 15 14 7 12 5 13 9 16 7 19\r\n9 16\r\n11 11\r\n18 19\r\n1 10\r\n14 19\r\n6 13\r\n5 16\r\n1 17\r\n5 15\r\n5 5\r\n1 13\r\n20 20\r\n20 20\r\n3 18\r\n16 20\r\n16 18\r\n17 18\r\n14 20\r\n13 14\r\n14 15\r\n", "output": "No\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\n"}, {"input": "100 10\r\n53 72 2 58 6 29 65 7 43 9 77 10 58 25 49 95 88 11 7 36 51 25 78 20 15 2 69 76 1 66 17 4 91 66 50 66 69 94 74 31 19 96 35 84 83 15 33 73 39 73 29 53 9 47 3 19 4 16 85 6 49 6 57 70 96 19 66 63 86 61 27 21 33 82 13 98 59 48 85 1 13 65 28 34 93 16 88 32 60 50 33 37 36 57 97 28 18 23 30 70\r\n25 43\r\n20 70\r\n13 51\r\n64 66\r\n1 60\r\n17 86\r\n100 100\r\n94 98\r\n51 66\r\n18 92\r\n", "output": "No\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nNo\r\n"}]
| false
|
stdio
| null | true
|
33/A
|
33
|
A
|
Python 3
|
TESTS
| 9
| 124
| 0
|
12088804
|
n, m, k = map(int, input().split())
max_teeth = [1000 for i in range(m)]
for i in range(n):
r, c = map(int, input().split())
r -= 1
max_teeth[r] = min(max_teeth[r], c)
print(min(k, sum(max_teeth)))
| 31
| 92
| 0
|
196495425
|
n, m, k = map(int, input().split())
rows_of_teeth = {}
for i in range(n):
r, t = map(int, input().split())
if r not in rows_of_teeth:
rows_of_teeth[r] = [t]
else:
rows_of_teeth[r].append(t)
j = sum(min(rows_of_teeth[i]) for i in rows_of_teeth)
print(min(j, k))
|
Codeforces Beta Round 33 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
What is for dinner?
|
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are "relaxing".
For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap).
It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again.
Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative.
As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner.
We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one.
|
The first line contains three integers n, m, k (1 ≤ m ≤ n ≤ 1000, 0 ≤ k ≤ 106) — total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 ≤ r ≤ m) — index of the row, where belongs the corresponding tooth, and c (0 ≤ c ≤ 106) — its residual viability.
It's guaranteed that each tooth row has positive amount of teeth.
|
In the first line output the maximum amount of crucians that Valerie can consume for dinner.
| null | null |
[{"input": "4 3 18\n2 3\n1 2\n3 6\n2 3", "output": "11"}, {"input": "2 2 13\n1 13\n2 12", "output": "13"}]
| 1,200
|
["greedy", "implementation"]
| 31
|
[{"input": "4 3 18\r\n2 3\r\n1 2\r\n3 6\r\n2 3\r\n", "output": "11\r\n"}, {"input": "2 2 13\r\n1 13\r\n2 12\r\n", "output": "13\r\n"}, {"input": "5 4 8\r\n4 6\r\n4 5\r\n1 3\r\n2 0\r\n3 3\r\n", "output": "8\r\n"}, {"input": "1 1 0\r\n1 3\r\n", "output": "0\r\n"}, {"input": "7 1 30\r\n1 8\r\n1 15\r\n1 5\r\n1 17\r\n1 9\r\n1 16\r\n1 16\r\n", "output": "5\r\n"}, {"input": "4 2 8\r\n1 9\r\n1 10\r\n1 4\r\n2 6\r\n", "output": "8\r\n"}, {"input": "10 4 14\r\n2 6\r\n1 5\r\n2 8\r\n2 6\r\n2 5\r\n4 1\r\n4 0\r\n2 4\r\n3 4\r\n1 0\r\n", "output": "8\r\n"}, {"input": "1 1 1000000\r\n1 1000000\r\n", "output": "1000000\r\n"}, {"input": "4 3 181818\r\n3 1299\r\n1 1694\r\n3 1164\r\n2 1278\r\n", "output": "4136\r\n"}, {"input": "19 12 199\r\n7 1\r\n8 6\r\n6 14\r\n1 7\r\n4 1\r\n6 6\r\n3 4\r\n1 5\r\n9 2\r\n5 3\r\n11 3\r\n9 4\r\n1 12\r\n4 7\r\n7 3\r\n12 14\r\n2 1\r\n10 8\r\n6 12\r\n", "output": "54\r\n"}]
| false
|
stdio
| null | true
|
53/D
|
53
|
D
|
PyPy 3
|
TESTS
| 4
| 154
| 0
|
137875401
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = []
for l in range(n):
if a[l] != b[l]:
for j in range(l + 1, n):
if a[j] == b[l]:
ind = j
break
while ind != l:
b[ind], b[ind - 1] = b[ind - 1], b[ind]
res.append([ind, ind + 1])
ind -= 1
print(len(res))
for i in res:
print(i[0], i[1])
| 30
| 124
| 512,000
|
156825993
|
def vasya_and_physcult(count, a_str, b_str):
size = int(count)
a = list(map(int, a_str.split()))
b = list(map(int, b_str.split()))
changes_count = 0
result = ""
for i in range(size):
current_index = i
for j in range(i,size):
if b[j] == a[i]:
current_index = j
break
while current_index>i:
b[current_index], b[current_index-1] = b[current_index-1], b[current_index-1]
result += "\n" + f"{current_index} {current_index+1}"
changes_count+=1
current_index-=1
return str(changes_count) + result
count = input()
a = input()
b = input()
print(vasya_and_physcult(count, a, b))
|
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
|
12/C
|
12
|
C
|
Python 3
|
TESTS
| 4
| 31
| 0
|
143960629
|
x, y = map(int, input().split())
a=sorted(map(int, input().split()))
b=[]
d=[]
q1=0
q2=0
for _ in range(y):
b.append(input())
c=list(set(b))
for i in range(len(c)):
d.append(b.count(c[i]))
d=d[::-1]
for i in range(len(d)):
q1+=d[i]*a[i]
print(q1)
a=a[::-1]
for i in range(len(d)):
q2+=d[i]*a[i]
print(q2)
| 25
| 46
| 0
|
150931843
|
n,m = map(int,input().split(" "))
dict = {}
pricetags = list(map(int,input().split(" ")))
names = []
for i in range(m):
names.append(input())
pricetags.sort()
for i in names:
if(i in dict):
dict[i] += 1
else:
dict[i] = 1
count = []
for i in dict:
count.append(dict[i])
count.sort()
maxprice = 0
minprice = 0
i = 0
j = 0
plen = len(pricetags)
clen = len(count)
while i < plen and j < clen:
minprice += pricetags[i] * count[clen-j-1]
maxprice += pricetags[plen-i-1] * count[clen-j-1]
i+=1
j+=1
print(str(minprice) + " " + str(maxprice))
|
Codeforces Beta Round 12 (Div 2 Only)
|
ICPC
| 2,010
| 1
| 256
|
Fruits
|
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.
When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
|
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
|
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
| null | null |
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
| 1,100
|
["greedy", "implementation", "sortings"]
| 25
|
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
| false
|
stdio
| null | true
|
12/C
|
12
|
C
|
Python 3
|
TESTS
| 1
| 31
| 0
|
174601758
|
n,m=map(int,input().split())
List=list(map(int,input().split()));List.sort()
list_0=List.copy()
List.reverse()
list_1=List.copy()
want=[input()for i in range(m)]
kind=list(set(want))
mmin=0;mmax=0
l=len(kind)
for i in range(l):
mmin+=list_0[i]*want.count(kind[i])
mmax+=list_1[i]*want.count(kind[i])
print(mmin,mmax)
| 25
| 46
| 0
|
161631413
|
n, m = map(int, input().split())
i = 0
j = 0
k = 0
min_res = 0
max_res = 0
arr = []
arr.append(0)
x = list(map(int,input().strip().split()))[:n]
for i in range (0, n) :
arr.append(x[i])
arr.sort()
arr.append(0)
s = []
s.append("")
for i in range (0, m) :
s.append(input())
s.sort()
s.append("")
temp = []
for i in range (1, m+1) :
k+=1
if (s[i] != s[i+1]) :
temp.append(k)
k = 0
j += 1
temp.sort()
for i in range (0, j) :
min_res += arr[i+1] * temp[j-i-1]
max_res += arr[n-i] * temp[j-i-1]
print(min_res, max_res)
|
Codeforces Beta Round 12 (Div 2 Only)
|
ICPC
| 2,010
| 1
| 256
|
Fruits
|
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.
When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
|
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
|
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
| null | null |
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
| 1,100
|
["greedy", "implementation", "sortings"]
| 25
|
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
| false
|
stdio
| null | true
|
789/A
|
789
|
A
|
Python 3
|
TESTS
| 27
| 171
| 10,547,200
|
26257893
|
import heapq
from collections import deque
import sys
#
# f = open('input.txt')
# sys.stdin = f
_, k = map(int, input().split())
a = 0
b = 0
for i in map(int, input().split()):
c = (i + k - 1) // k
a += c
b = max(b, c)
print(max((a+1)//2,b))
| 31
| 77
| 5,734,400
|
183822605
|
n,k=map(int,input().split())
d=0
t = sum(((i-1)//k + 1) for i in map(int,input().split()))
print((t-1)//2 + 1)
|
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
|
103/A
|
103
|
A
|
PyPy 3-64
|
TESTS
| 4
| 122
| 0
|
132272294
|
n=int(input())
s=0
l=list(map(int,input().split()))
penalty=(sum(l)-l[0])-(n-1)
clicks=sum(l)+penalty
print(clicks)
| 25
| 92
| 0
|
143368752
|
n = int(input())
s = input().split()
a = [int(i) for i in s]
res = 0
for i in range(n):
res += a[i] + (i*(a[i]-1))
print(res)
|
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
|
400/B
|
400
|
B
|
Python 3
|
TESTS
| 17
| 46
| 4,812,800
|
133176194
|
def max_moves(n,m):
max_moves = -1
out = []
for i in range(n):
matrix = input()
if matrix[-1] == 'G':
return -1
else:
g = matrix.find('G')
s = matrix.find('S')
if g - s not in out:
out.append(g - s)
return len(out)
n,m = map(int,input().split())
print(max_moves(n,m))
| 34
| 46
| 204,800
|
162682808
|
from sys import stdin
def main():
count = 0
bandera = 0
l = []
[n,m] = [int (x) for x in stdin.readline().split()]
for cont in range(n):
line = stdin.readline()
g = line.index("G")
s = line.index("S")
if s<g:
print(-1)
bandera = 1
break
if s-g not in l:
l.append(s-g)
count += 1
if bandera == 0:
print(count)
main()
|
Codeforces Round 234 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and New Matrix of Candies
|
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".
The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:
- some dwarf in one of the chosen lines is located in the rightmost cell of his row;
- some dwarf in the chosen lines is located in the cell with the candy.
The point of the game is to transport all the dwarves to the candy cells.
Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
|
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).
Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
|
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
| null | null |
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
| 1,200
|
["brute force", "implementation", "schedules"]
| 34
|
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
400/B
|
400
|
B
|
Python 3
|
TESTS
| 4
| 46
| 0
|
121826916
|
n, m = map(int, input().split())
end = m - 1
MAX = float('inf')
arr = []
for i in range(n):
inp = input()
# last G, S if they exists
last_g = inp.rfind('G')
if last_g == -1:
last_g = MAX
last_s = inp.rfind('S')
if last_s == -1:
last_s = MAX
if last_s != MAX and last_g != MAX and last_s > last_g:
arr.append(last_s - last_g)
if last_s < last_g:
print(-1)
quit()
sol = 0
while arr:
mn = min(arr)
sol += 1
arr.remove(mn)
for i in arr:
i -= mn
print(sol - 1)
| 34
| 46
| 204,800
|
168008818
|
n,m=list(map(int,input().split()));set1=set()
for i in range(n):
x=input();candy=x.index("S");dwarf=x.index("G")
if dwarf>candy:
print(-1)
break
else: set1.add(candy-dwarf)
else:print(len(set1))
|
Codeforces Round 234 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and New Matrix of Candies
|
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".
The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:
- some dwarf in one of the chosen lines is located in the rightmost cell of his row;
- some dwarf in the chosen lines is located in the cell with the candy.
The point of the game is to transport all the dwarves to the candy cells.
Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
|
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).
Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
|
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
| null | null |
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
| 1,200
|
["brute force", "implementation", "schedules"]
| 34
|
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
38/E
|
38
|
E
|
PyPy 3-64
|
TESTS
| 39
| 684
| 77,209,600
|
175497645
|
n = int(input())
vp = []
for i in range(n):
x, c = map(int, input().split())
vp.append([x,c])
vp.sort(key = lambda x:x[0])
dp = [[0] * 3005 for _ in range(3005)]
dp[1][1] = vp[0][1]
presum = [0] * 3005
for i in range(1, n + 1):
presum[i] = presum[i - 1] + vp[i - 1][0]
mn = [0x3f3f3f3f] * 3005
mn[1] = vp[0][1]
for i in range(2, n + 1):
for j in range(1, i + 1):
if j < i:
dp[i][j] = dp[j][j] + presum[i] - presum[j] - (i - j) * vp[j - 1][0]
else:
dp[i][j] = mn[i - 1] + vp[i - 1][1]
mn[i] = min(mn[i], dp[i][j])
print(mn[n])
| 50
| 218
| 3,276,800
|
174556350
|
import sys
input = sys.stdin.readline
from operator import itemgetter
n=int(input())
M=[tuple(map(int,input().split())) for i in range(n)]
M.sort(key=itemgetter(0))
DP=[1<<60]*n
DP[0]=M[0][1]
for i in range(1,n):
x,c=M[i]
MIN=1<<60
for j in range(i):
MIN=min(MIN,DP[j])
DP[j]+=abs(x-M[j][0])
DP[i]=MIN+c
print(min(DP))
|
School Personal Contest #1 (Winter Computer School 2010/11) - Codeforces Beta Round 38 (ACM-ICPC Rules)
|
ICPC
| 2,010
| 2
| 256
|
Let's Go Rolling!
|
On a number axis directed from the left rightwards, n marbles with coordinates x1, x2, ..., xn are situated. Let's assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in the marble number i is equal to ci, number ci may be negative. After you choose and stick the pins you need, the marbles will start to roll left according to the rule: if a marble has a pin stuck in it, then the marble doesn't move, otherwise the marble rolls all the way up to the next marble which has a pin stuck in it and stops moving there. If there is no pinned marble on the left to the given unpinned one, it is concluded that the marble rolls to the left to infinity and you will pay an infinitely large fine for it. If no marble rolled infinitely to the left, then the fine will consist of two summands:
- the sum of the costs of stuck pins;
- the sum of the lengths of the paths of each of the marbles, that is the sum of absolute values of differences between their initial and final positions.
Your task is to choose and pin some marbles in the way that will make the fine for you to pay as little as possible.
|
The first input line contains an integer n (1 ≤ n ≤ 3000) which is the number of marbles. The next n lines contain the descriptions of the marbles in pairs of integers xi, ci ( - 109 ≤ xi, ci ≤ 109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions.
|
Output the single number — the least fine you will have to pay.
| null | null |
[{"input": "3\n2 3\n3 4\n1 2", "output": "5"}, {"input": "4\n1 7\n3 1\n5 10\n6 1", "output": "11"}]
| 1,800
|
["dp", "sortings"]
| 50
|
[{"input": "3\r\n2 3\r\n3 4\r\n1 2\r\n", "output": "5\r\n"}, {"input": "4\r\n1 7\r\n3 1\r\n5 10\r\n6 1\r\n", "output": "11\r\n"}, {"input": "1\r\n-454809824 14\r\n", "output": "14\r\n"}, {"input": "5\r\n-635157449 322\r\n-635157444 528\r\n-635157441 576\r\n-635157437 406\r\n-635157436 396\r\n", "output": "360\r\n"}, {"input": "10\r\n918236080 1\r\n918236085 3\r\n918236115 3\r\n918236124 1\r\n918236144 1\r\n918236145 1\r\n918236146 2\r\n918236147 3\r\n918236148 2\r\n918236149 1\r\n", "output": "16\r\n"}, {"input": "15\r\n-241250505 -2\r\n-241250503 0\r\n-241250502 -5\r\n-241250501 -1\r\n-241250500 -3\r\n-241250499 2\r\n-241250498 -2\r\n-241250497 6\r\n-241250496 0\r\n-241250495 0\r\n-241250494 8\r\n-241250493 -5\r\n-241250492 7\r\n-241250491 -4\r\n-241250490 2\r\n", "output": "-17\r\n"}, {"input": "1\r\n837757580 0\r\n", "output": "0\r\n"}, {"input": "1\r\n-512686695 0\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
400/B
|
400
|
B
|
Python 3
|
TESTS
| 4
| 31
| 0
|
228256689
|
def find_longest_ascending_sequence(sorted_list):
current_sequence = [sorted_list[0]]
longest_sequence = []
for i in range(1, len(sorted_list)):
if sorted_list[i] >= sorted_list[i - 1]:
current_sequence.append(sorted_list[i])
else:
# If the current element is not greater or equal, start a new sequence
if len(current_sequence) > len(longest_sequence):
longest_sequence = current_sequence
current_sequence = [sorted_list[i]]
# Check if the last sequence is the longest
if len(current_sequence) > len(longest_sequence):
longest_sequence = current_sequence
return len(longest_sequence)
n, m = map(int, input().split())
lst = []
for i in range(n):
temp = list(map(str, input().split()))
lst.append(temp)
dist = []
for i in lst:
candy, idx, dwarf = False, 0, False
for el in i:
if el == "G":
dwarf = True
if not candy and dwarf:
idx += 1
if el == "S":
candy = True
dist.append(idx)
dist.sort()
count = find_longest_ascending_sequence(dist) - 1
if count == 0:
count = -1
print(count)
| 34
| 46
| 204,800
|
190314380
|
x, y = input().split()
x = int(x)
y = int(y)
ans = 1
uni = []
for i in range(x):
s = input()
d = s.find('G') + 1
c = s.find('S') + 1
# print(d, c)
if c - d >= 0:
if c-d not in uni:
uni.append(c-d)
else:
ans = 0
break
if ans:
print(len(uni))
else:
print(-1)
|
Codeforces Round 234 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and New Matrix of Candies
|
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".
The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:
- some dwarf in one of the chosen lines is located in the rightmost cell of his row;
- some dwarf in the chosen lines is located in the cell with the candy.
The point of the game is to transport all the dwarves to the candy cells.
Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
|
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).
Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
|
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
| null | null |
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
| 1,200
|
["brute force", "implementation", "schedules"]
| 34
|
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
789/A
|
789
|
A
|
PyPy 3-64
|
TESTS
| 5
| 77
| 13,004,800
|
215380126
|
from math import ceil
n, k = map(int, input().split())
w = list(map(int, input().split()))
cnt = 0
for i in range(n):
cnt += 0.5
if w[i] > k:
cnt += 0.5*(w[i] // k)
print(ceil(cnt))
| 31
| 77
| 13,004,800
|
209110707
|
import math
def solve():
n,k=list(map(int,input().split(" ")))
lst=list(map(int,input().split(" ")))
s=0
for i in lst:
s+=math.ceil(i/k)
print(math.ceil(s/2))
solve();
|
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
|
393/B
|
393
|
B
|
Python 3
|
TESTS
| 36
| 217
| 716,800
|
70211428
|
n = int(input())
array = [0]*n
for i in range(n):
arr = input().split()
arr = list(map(lambda x: int(x) if x.isdigit() else 0, arr))
array[i] = arr
for i in range(n):
for j in range(n):
if(i == j):
print(array[i][j],end=' ')
else:
print((array[i][j]+array[j][i])/2,end=' ')
print('')
for i in range(n):
for j in range(n):
if(i == j):
print('0',end=' ')
else:
if(i>j):
print((array[i][j]-array[j][i])/2,end=' ')
else:
print((array[i][j]-array[j][i])/2,end=' ')
print('')
| 40
| 140
| 1,945,600
|
50765911
|
if __name__ == '__main__':
n = int(input())
w = []
for i in range(n):
w.append(list(map(int, input().split())))
a=[]
b=[]
for i in range(n):
a.append([0.0]*n)
b.append([0.0]*n)
for i in range(n):
for j in range(i, n):
a[i][j] = (w[i][j] + w[j][i]) / 2
a[j][i] = a[i][j]
b[i][j] = w[i][j] - a[i][j]
b[j][i] = w[j][i] - a[j][i]
for i in range(n):
print(" ".join(list(map(str, a[i]))))
for i in range(n):
print(" ".join(list(map(str, b[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
|
216/E
|
216
|
E
|
Python 3
|
TESTS
| 2
| 124
| 0
|
121438112
|
Line1 = list(map(int,input().split()))
List = list(map(int,input().split()))
def MartianLuck2(k,b,n,digit_list):
reminder = b % (k-1)
LuckyNumbersCounter = 0
ActualNumber = 0
Count = 0
while Count < n :
for digit_index in range(Count,n):
ActualNumber = (ActualNumber + digit_list[digit_index]) % (k-1)
if b == 0:
if ActualNumber == 0:
LuckyNumbersCounter += 1
elif (ActualNumber == reminder and not ((digit_index - Count) == 0 and ActualNumber == 0)):
LuckyNumbersCounter +=1
ActualNumber = 0
Count +=1
return LuckyNumbersCounter
print(MartianLuck2(Line1[0],Line1[1],Line1[2],List))
| 41
| 466
| 34,508,800
|
125885363
|
from sys import stdin
inp = stdin.readline
k, b, n = map(int, inp().split())
s = [int(i) for i in inp().split()]
a = b % (k-1)
sums = {0: 1}
def zero():
i, count2 = 0, 0
s.append(1)
while i < n:
if s[i] == 0:
x = 0
while s[i] == 0:
x += 1
i += 1
count2 += int(x*(x+1)/2)
i += 1
return count2
if b != 0:
curr, count = 0, 0
for i in range(n):
curr = (curr + s[i]) % (k-1)
x = (curr - a) % (k-1)
count += sums.get(x, 0)
sums[curr] = sums.get(curr, 0) + 1
if b == k-1:
count -= zero()
print(count)
else:
print(zero())
|
Codeforces Round 133 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Martian Luck
|
You know that the Martians use a number system with base k. Digit b (0 ≤ b < k) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year b (by Martian chronology).
A digital root d(x) of number x is a number that consists of a single digit, resulting after cascading summing of all digits of number x. Word "cascading" means that if the first summing gives us a number that consists of several digits, then we sum up all digits again, and again, until we get a one digit number.
For example, d(35047) = d((3 + 5 + 0 + 4)7) = d(157) = d((1 + 5)7) = d(67) = 67. In this sample the calculations are performed in the 7-base notation.
If a number's digital root equals b, the Martians also call this number lucky.
You have string s, which consists of n digits in the k-base notation system. Your task is to find, how many distinct substrings of the given string are lucky numbers. Leading zeroes are permitted in the numbers.
Note that substring s[i... j] of the string s = a1a2... an (1 ≤ i ≤ j ≤ n) is the string aiai + 1... aj. Two substrings s[i1... j1] and s[i2... j2] of the string s are different if either i1 ≠ i2 or j1 ≠ j2.
|
The first line contains three integers k, b and n (2 ≤ k ≤ 109, 0 ≤ b < k, 1 ≤ n ≤ 105).
The second line contains string s as a sequence of n integers, representing digits in the k-base notation: the i-th integer equals ai (0 ≤ ai < k) — the i-th digit of string s. The numbers in the lines are space-separated.
|
Print a single integer — the number of substrings that are lucky numbers.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample the following substrings have the sought digital root: s[1... 2] = "3 2", s[1... 3] = "3 2 0", s[3... 4] = "0 5", s[4... 4] = "5" and s[2... 6] = "2 0 5 6 1".
|
[{"input": "10 5 6\n3 2 0 5 6 1", "output": "5"}, {"input": "7 6 4\n3 5 0 4", "output": "1"}, {"input": "257 0 3\n0 0 256", "output": "3"}]
| 2,000
|
["math", "number theory"]
| 41
|
[{"input": "10 5 6\r\n3 2 0 5 6 1\r\n", "output": "5"}, {"input": "7 6 4\r\n3 5 0 4\r\n", "output": "1"}, {"input": "257 0 3\r\n0 0 256\r\n", "output": "3"}, {"input": "2 1 1\r\n0\r\n", "output": "0"}, {"input": "2 0 20\r\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1\r\n", "output": "22"}, {"input": "100 29 33\r\n28 89 23 14 97 97 66 56 55 60 47 29 9 79 26 80 63 78 83 60 41 29 52 10 82 26 47 88 99 75 52 1 31\r\n", "output": "10"}, {"input": "3 2 100\r\n2 0 0 0 0 2 1 1 1 2 0 1 1 1 1 2 0 0 1 0 1 1 2 0 2 0 1 0 1 0 0 2 0 0 0 1 2 0 2 2 0 2 0 2 1 0 1 1 1 1 2 0 0 0 1 0 2 0 2 0 2 1 2 2 1 1 0 1 1 2 1 1 0 0 1 1 2 2 1 2 2 0 1 2 2 1 2 2 0 2 0 2 2 0 2 2 1 2 0 0\r\n", "output": "2451"}, {"input": "5 4 102\r\n3 2 2 3 3 2 2 0 3 1 2 4 0 1 3 4 3 2 3 0 4 1 0 0 0 0 4 4 1 2 3 3 4 0 1 2 2 3 3 1 3 1 0 0 3 0 4 0 2 4 2 3 0 1 4 3 0 2 3 3 2 2 1 0 1 3 0 3 4 4 4 1 0 1 2 1 4 2 4 4 4 4 4 2 3 3 0 3 0 0 0 4 1 3 0 4 2 1 2 0 3 0\r\n", "output": "1293"}, {"input": "7 4 104\r\n4 3 0 6 6 5 3 4 4 5 0 1 2 5 5 1 3 4 1 5 3 5 4 4 2 4 3 5 4 2 2 3 1 1 0 5 4 3 2 5 2 1 3 1 6 4 1 3 0 2 5 2 5 3 3 6 1 2 2 2 4 5 6 0 5 4 5 3 5 3 4 3 1 0 2 4 5 5 5 5 3 3 6 1 6 1 3 6 6 5 3 3 1 3 2 0 4 4 3 3 4 0 5 6\r\n", "output": "938"}, {"input": "20 15 1\r\n6\r\n", "output": "0"}, {"input": "20 19 2\r\n16 13\r\n", "output": "0"}, {"input": "30 24 30\r\n7 24 3 20 8 24 0 6 15 22 20 21 16 26 28 6 6 28 19 2 12 22 6 12 15 17 24 13 12 16\r\n", "output": "20"}, {"input": "2 0 100\r\n0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "331"}]
| false
|
stdio
| null | true
|
115/B
|
115
|
B
|
Python 3
|
TESTS
| 0
| 31
| 0
|
220400162
|
n, m = map(int, input().split())
garden = [input() for _ in range(n)]
moves = 0
for i in range(n - 1, -1, -1):
if i % 2 == 0:
for j in range(m):
if garden[i][j] == 'W':
moves += 1
if j < m - 1:
moves += 1
else:
for j in range(m - 1, -1, -1):
if garden[i][j] == 'W':
moves += 1
if j > 0:
moves += 1
print(moves)
| 124
| 77
| 3,276,800
|
216357199
|
rows, cols = map(int, input().split())
total_moves = 0
current_row, current_col = 0, 0
for i in range(rows):
row_data = input()
direction = 1 - i % 2 * 2
col = (i % 2) * (cols - 1)
while col >= 0 and col < cols:
if row_data[col] == 'G':
col += direction
else:
total_moves += abs(current_row - i) + abs(current_col - col)
current_row, current_col = i, col
col += direction
print(total_moves)# 1690657572.5011148
|
Codeforces Beta Round 87 (Div. 1 Only)
|
CF
| 2,011
| 2
| 256
|
Lawnmower
|
You have a garden consisting entirely of grass and weeds. Your garden is described by an n × m grid, with rows numbered 1 to n from top to bottom, and columns 1 to m from left to right. Each cell is identified by a pair (r, c) which means that the cell is located at row r and column c. Each cell may contain either grass or weeds. For example, a 4 × 5 garden may look as follows (empty cells denote grass):
You have a land-mower with you to mow all the weeds. Initially, you are standing with your lawnmower at the top-left corner of the garden. That is, at cell (1, 1). At any moment of time you are facing a certain direction — either left or right. And initially, you face right.
In one move you can do either one of these:
1) Move one cell in the direction that you are facing.
- if you are facing right: move from cell (r, c) to cell (r, c + 1)
- if you are facing left: move from cell (r, c) to cell (r, c - 1)
- if you were facing right previously, you will face left
- if you were facing left previously, you will face right
You are not allowed to leave the garden. Weeds will be mowed if you and your lawnmower are standing at the cell containing the weeds (your direction doesn't matter). This action isn't counted as a move.
What is the minimum number of moves required to mow all the weeds?
|
The first line contains two integers n and m (1 ≤ n, m ≤ 150) — the number of rows and columns respectively. Then follow n lines containing m characters each — the content of the grid. "G" means that this cell contains grass. "W" means that this cell contains weeds.
It is guaranteed that the top-left corner of the grid will contain grass.
|
Print a single number — the minimum number of moves required to mow all the weeds.
| null |
For the first example, this is the picture of the initial state of the grid:
A possible solution is by mowing the weeds as illustrated below:
|
[{"input": "4 5\nGWGGW\nGGWGG\nGWGGG\nWGGGG", "output": "11"}, {"input": "3 3\nGWW\nWWW\nWWG", "output": "7"}, {"input": "1 1\nG", "output": "0"}]
| 1,500
|
["greedy", "sortings"]
| 124
|
[{"input": "4 5\r\nGWGGW\r\nGGWGG\r\nGWGGG\r\nWGGGG\r\n", "output": "11\r\n"}, {"input": "3 3\r\nGWW\r\nWWW\r\nWWG\r\n", "output": "7\r\n"}, {"input": "1 1\r\nG\r\n", "output": "0\r\n"}, {"input": "4 3\r\nGWW\r\nWWW\r\nWWW\r\nWWG\r\n", "output": "11\r\n"}, {"input": "6 5\r\nGWWWW\r\nWWWWW\r\nWWWWW\r\nWWWWW\r\nWWWWW\r\nWWWWW\r\n", "output": "29\r\n"}, {"input": "3 5\r\nGGWWW\r\nWWWWW\r\nWWWGG\r\n", "output": "12\r\n"}, {"input": "20 1\r\nG\r\nG\r\nW\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nG\r\nW\r\nG\r\nW\r\nG\r\nG\r\n", "output": "17\r\n"}, {"input": "2 2\r\nGG\r\nGW\r\n", "output": "2\r\n"}, {"input": "1 20\r\nGGGGWGGGGWWWWGGGWGGG\r\n", "output": "16\r\n"}, {"input": "1 150\r\nGGWGGGGGGGGGGGGGGGGGGGWGGGGGGWGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGWGGGWGGGGGGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGWGGGGGGGGWGGGGGGGGGWWGGGGGWGGGGGGGGG\r\n", "output": "140\r\n"}, {"input": "1 150\r\nGGGWGGGWWWWWWWGWWWGGWWWWWGGWWGGWWWWWWWWGWWGWWWWWWGWGWGWWWWWGWGWWGWWWWGWWGWGWWWWWWWWGGGGWWWWWGGGWWWWGGGWWWWGGWWWWGWWWWGGGWWWWWWWGWGGWGWWWGWGGWWGWGWWWGW\r\n", "output": "149\r\n"}, {"input": "1 1\r\nG\r\n", "output": "0\r\n"}, {"input": "1 1\r\nG\r\n", "output": "0\r\n"}, {"input": "1 150\r\nGGGGWWGGWWWGGWGWGGWWGGWGGGGGWWWGWWGWWGWWWGWGGWGWGWWGWGGWWGWGGWWWGGWGGGWGWGWGGGWGWWGGWGWGWWWWGGWWGWWWGWGWGGGWGWGGWWGWGGGWWGGGWWWWWWWWWWWGGGGWGWWGWGWGGG\r\n", "output": "146\r\n"}, {"input": "3 3\r\nGWG\r\nGGG\r\nGGW\r\n", "output": "4\r\n"}, {"input": "3 3\r\nGGG\r\nGGG\r\nGGG\r\n", "output": "0\r\n"}, {"input": "2 4\r\nGWWG\r\nGGWW\r\n", "output": "5\r\n"}, {"input": "5 2\r\nGG\r\nGG\r\nWW\r\nGW\r\nWG\r\n", "output": "6\r\n"}, {"input": "2 5\r\nGWGGG\r\nGWGGW\r\n", "output": "8\r\n"}, {"input": "3 3\r\nGGG\r\nGGG\r\nGGW\r\n", "output": "4\r\n"}, {"input": "6 3\r\nGGW\r\nGGG\r\nGGG\r\nGGG\r\nGGG\r\nWGG\r\n", "output": "9\r\n"}, {"input": "3 3\r\nGWG\r\nWGW\r\nGWG\r\n", "output": "7\r\n"}, {"input": "6 4\r\nGWWW\r\nWWGG\r\nGGGG\r\nGGGG\r\nGGGW\r\nWWGG\r\n", "output": "17\r\n"}, {"input": "2 3\r\nGGG\r\nGGG\r\n", "output": "0\r\n"}, {"input": "10 10\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\nGGGGGGGGGG\r\n", "output": "0\r\n"}, {"input": "3 3\r\nGWW\r\nGWW\r\nWWG\r\n", "output": "7\r\n"}, {"input": "5 5\r\nGWGGG\r\nGGGGG\r\nGGGGG\r\nWGGGW\r\nGGGGG\r\n", "output": "11\r\n"}, {"input": "3 5\r\nGWGGW\r\nGGGGG\r\nWGGWG\r\n", "output": "13\r\n"}]
| false
|
stdio
| null | true
|
461/B
|
461
|
B
|
Python 3
|
TESTS
| 6
| 62
| 0
|
7599108
|
def getlist():
return list(map(int, input().split()))
n = int(input())
par = getlist()
g = [[] for i in range(n)]
for i in range(n - 1):
g[par[i]].append(i + 1)
col = getlist()
dp = [[0, 0] for i in range(n)]
used = [0] * n
def dfs(v):
dp[v][0] = 1
dp[v][1] = 0
used[v] = 1
for i in g[v]:
if used[i] == 0:
dfs(i)
dp[v][1] *= dp[i][0]
dp[v][1] += dp[v][0] * dp[i][1]
dp[v][0] *= dp[i][0]
if col[v] == 1:
dp[v][1] = dp[v][0]
else:
dp[v][0] += dp[v][1]
dfs(0)
print(dp[0][1])
| 23
| 265
| 12,595,200
|
173824533
|
MOD = 1000000007
n = int(input())
p = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
children = [[] for x in range(n)]
for i in range(1,n):
children[p[i-1]].append(i)
count = [(0,0) for i in range(n)]
for i in reversed(range(n)):
prod = 1
for ch in children[i]:
prod *= count[ch][0]+count[ch][1]
if x[i]:
count[i] = (0,prod % MOD)
else:
tot = 0
for ch in children[i]:
cur = count[ch][1]*prod // (count[ch][0]+count[ch][1])
tot += cur
count[i] = (prod % MOD, tot % MOD)
print(count[0][1])
|
Codeforces Round 263 (Div. 1)
|
CF
| 2,014
| 2
| 256
|
Appleman and Tree
|
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
|
The first line contains an integer n (2 ≤ n ≤ 105) — the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
|
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
| null | null |
[{"input": "3\n0 0\n0 1 1", "output": "2"}, {"input": "6\n0 1 1 0 4\n1 1 0 0 1 0", "output": "1"}, {"input": "10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1", "output": "27"}]
| 2,000
|
["dfs and similar", "dp", "trees"]
| 23
|
[{"input": "3\r\n0 0\r\n0 1 1\r\n", "output": "2\r\n"}, {"input": "6\r\n0 1 1 0 4\r\n1 1 0 0 1 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 2 1 4 4 4 0 8\r\n0 0 0 1 0 1 1 0 0 1\r\n", "output": "27\r\n"}, {"input": "5\r\n0 1 1 3\r\n0 0 0 1 1\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 1 2 4 3 3 3 2\r\n1 0 1 1 1 0 0 1 1 0\r\n", "output": "3\r\n"}, {"input": "100\r\n0 0 2 2 0 3 5 0 6 2 0 4 0 2 3 7 8 3 15 19 13 8 18 19 3 14 23 9 6 3 6 17 26 24 20 6 4 27 8 5 14 5 35 31 27 3 41 25 20 14 25 31 49 40 0 1 10 5 50 13 29 58 1 6 8 1 40 52 30 15 50 8 66 52 29 71 25 68 36 7 80 60 6 2 11 43 62 27 84 86 71 38 14 50 88 4 8 95 53\r\n1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 1\r\n", "output": "9523200\r\n"}, {"input": "2\r\n0\r\n1 0\r\n", "output": "1\r\n"}, {"input": "115\r\n0 0 1 2 0 4 1 3 4 1 4 5 4 5 0 0 3 1 2 3 3 0 5 1 3 4 1 5 2 0 1 3 3 1 3 5 0 4 5 1 3 0 0 1 3 1 1 3 3 3 2 3 1 3 0 2 5 5 1 1 2 2 1 1 3 2 1 2 3 1 5 4 2 1 2 1 1 2 3 4 3 1 5 0 2 4 4 5 2 5 0 2 4 5 5 5 5 0 3 1 1 4 2 2 4 3 3 0 3 3 0 2 0 0\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
461/B
|
461
|
B
|
Python 3
|
TESTS
| 15
| 155
| 6,860,800
|
140105651
|
import threading
import sys
sys.setrecursionlimit(3 * 10 ** 5 + 200)
threading.stack_size(3 * 10 ** 5 + 100)
MOD = 10 ** 9 + 7
def dfs(v):
if c[v]: # black
dp0v = 0
dp1v = 1
for u in edges[v]:
dp0u, dp1u = dfs(u)
dp1v *= dp0u + dp1u
dp1v %= MOD
else:
dp0v = 1
dp1v = 0
for u in edges[v]:
dp0u, dp1u = dfs(u)
dp1v *= dp0u + dp1u
dp1v += dp0v * dp1u
dp0v *= dp0u + dp1u
dp0v %= MOD
dp1v %= MOD
return dp0v, dp1v
n = int(input())
*p, = [int(x) for x in input().split()]
*c, = [int(x) for x in input().split()]
edges = [[] for _ in range(n)]
for i in range(n - 1):
edges[p[i]].append(i + 1)
if n > 10 ** 4:
dp0, dp1 = 0, 0
def calculate():
global dp0, dp1
dp0, dp1 = dfs(0)
t = threading.Thread(target=calculate)
t.start()
t.join()
else:
dp0, dp1 = dfs(0)
print(dp1)
| 23
| 327
| 10,752,000
|
140184449
|
import threading
import sys
sys.setrecursionlimit(3 * 10 ** 5 + 200)
threading.stack_size(3 * 10 ** 5 + 100)
MOD = 10 ** 9 + 7
dp0u, dp1u = -1, -1
def dfs(v):
if c[v]: # black
dp0v = 0
dp1v = 1
for u in edges[v]:
dp0u, dp1u = yield u
dp1v *= dp0u + dp1u
dp1v %= MOD
else:
dp0v = 1
dp1v = 0
for u in edges[v]:
dp0u, dp1u = yield u
dp1v *= dp0u + dp1u
dp1v += dp0v * dp1u
dp0v *= dp0u + dp1u
dp0v %= MOD
dp1v %= MOD
yield v
yield dp0v, dp1v
# def dfs_interact(v):
# stack = [(v, dfs(v))]
# p = 0
# while stack:
#
#
# for u in stack[-1]:
# if u == stack[-1][0]:
# # all needed values were supplied
#
# break
# else:
# stack.append((u, dfs(u)))
# else:
# dp0u, dp1u = next(stack[-1][1])
# stack.pop()
n = int(input())
*p, = [int(x) for x in input().split()]
*c, = [int(x) for x in input().split()]
edges = [[] for _ in range(n)]
for i in range(n - 1):
edges[p[i]].append(i + 1)
dp = [[0] * n, [0] * n]
for v in range(n)[::-1]:
gen = dfs(v)
u = gen.send(None)
while u != v:
u = gen.send((dp[0][u], dp[1][u]))
dp[0][v], dp[1][v] = gen.send(None)
print(dp[1][0])
|
Codeforces Round 263 (Div. 1)
|
CF
| 2,014
| 2
| 256
|
Appleman and Tree
|
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
|
The first line contains an integer n (2 ≤ n ≤ 105) — the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
|
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
| null | null |
[{"input": "3\n0 0\n0 1 1", "output": "2"}, {"input": "6\n0 1 1 0 4\n1 1 0 0 1 0", "output": "1"}, {"input": "10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1", "output": "27"}]
| 2,000
|
["dfs and similar", "dp", "trees"]
| 23
|
[{"input": "3\r\n0 0\r\n0 1 1\r\n", "output": "2\r\n"}, {"input": "6\r\n0 1 1 0 4\r\n1 1 0 0 1 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 2 1 4 4 4 0 8\r\n0 0 0 1 0 1 1 0 0 1\r\n", "output": "27\r\n"}, {"input": "5\r\n0 1 1 3\r\n0 0 0 1 1\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 1 2 4 3 3 3 2\r\n1 0 1 1 1 0 0 1 1 0\r\n", "output": "3\r\n"}, {"input": "100\r\n0 0 2 2 0 3 5 0 6 2 0 4 0 2 3 7 8 3 15 19 13 8 18 19 3 14 23 9 6 3 6 17 26 24 20 6 4 27 8 5 14 5 35 31 27 3 41 25 20 14 25 31 49 40 0 1 10 5 50 13 29 58 1 6 8 1 40 52 30 15 50 8 66 52 29 71 25 68 36 7 80 60 6 2 11 43 62 27 84 86 71 38 14 50 88 4 8 95 53\r\n1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 1\r\n", "output": "9523200\r\n"}, {"input": "2\r\n0\r\n1 0\r\n", "output": "1\r\n"}, {"input": "115\r\n0 0 1 2 0 4 1 3 4 1 4 5 4 5 0 0 3 1 2 3 3 0 5 1 3 4 1 5 2 0 1 3 3 1 3 5 0 4 5 1 3 0 0 1 3 1 1 3 3 3 2 3 1 3 0 2 5 5 1 1 2 2 1 1 3 2 1 2 3 1 5 4 2 1 2 1 1 2 3 4 3 1 5 0 2 4 4 5 2 5 0 2 4 5 5 5 5 0 3 1 1 4 2 2 4 3 3 0 3 3 0 2 0 0\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
Python 3
|
TESTS
| 8
| 92
| 0
|
165768254
|
n, k = map(int, input().split())
ans = -100
for _ in range(n):
f, t = map(int, input().split())
pleasure = None
if t > k:
pleasure = f-t+k
else:
pleasure = f
if pleasure > ans:
ans = pleasure
print(ans)
| 35
| 92
| 0
|
145380846
|
n, k = input().split()
n, k = int(n), int(k)
ma = None
for i in range(n):
a, b = input().split()
a, b = int(a), int(b)
if b > k:
joy = a - b + k
else:
joy = a
if ma is None:
ma = joy
else:
if ma < joy:
ma = joy
print(ma)
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
Python 3
|
TESTS
| 8
| 92
| 0
|
138927278
|
n, k = map(int, input().split())
amax = -1
for _ in range(n):
f, t = map(int, input().split())
if t >= k:
f = f - (t - k)
amax = max(amax, f)
print(amax)
| 35
| 92
| 0
|
145738418
|
import math
def main():
# t = int(input())
# for _ in range(t):
print(case())
def case():
n, k = map(int, input().split())
mm = None
for _ in range(n):
f, t = map(int, input().split())
if t > k:
fun = f - (t-k)
else:
fun = f
if mm == None:
mm = fun
continue
mm = max(mm, fun)
return mm
main()
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
705/B
|
705
|
B
|
PyPy 3
|
TESTS
| 2
| 93
| 0
|
228309986
|
n = int(input())
arr = [*map(int, input().split())]
for num in arr:
if num == 1 or (num - 1) % 4 == 0:
print(2)
else:
print(1)
| 38
| 108
| 12,800,000
|
219359107
|
n = int(input())
a = list(map(lambda x: int(x)-1, input().split()))
s = a[0]
for i in range(1, n):
s += a[i]
a[i] = s
print("\n".join((('2', '1')[a[i] % 2] for i in range(n))))
|
Codeforces Round 366 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Spider Man
|
Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex.
Initially there are k cycles, i-th of them consisting of exactly vi vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, x vertices) among all available cycles and replace it by two cycles with p and x - p vertices where 1 ≤ p < x is chosen by the player. The player who cannot make a move loses the game (and his life!).
Peter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the i-th test he adds a cycle with ai vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins?
Peter is pretty good at math, but now he asks you to help.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of tests Peter is about to make.
The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), i-th of them stands for the number of vertices in the cycle added before the i-th test.
|
Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise.
| null |
In the first sample test:
In Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses.
In his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can't make any move and loses.
In his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player's only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins.
In the second sample test:
Having cycles of size 1 is like not having them (because no one can make a move on them).
In Peter's third test: There a cycle of size 5 (others don't matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3.
- If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player's only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can't make any move and loses.
- If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins.
So, either way first player loses.
|
[{"input": "3\n1 2 3", "output": "2\n1\n1"}, {"input": "5\n1 1 5 1 1", "output": "2\n2\n2\n2\n2"}]
| 1,100
|
["games", "math"]
| 38
|
[{"input": "3\r\n1 2 3\r\n", "output": "2\r\n1\r\n1\r\n"}, {"input": "5\r\n1 1 5 1 1\r\n", "output": "2\r\n2\r\n2\r\n2\r\n2\r\n"}, {"input": "1\r\n167959139\r\n", "output": "2\r\n"}, {"input": "10\r\n802030518 598196518 640274071 983359971 71550121 96204862 799843967 446173607 796619138 402690754\r\n", "output": "1\r\n2\r\n2\r\n2\r\n2\r\n1\r\n1\r\n1\r\n2\r\n1\r\n"}, {"input": "1\r\n1\r\n", "output": "2\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n2 4\r\n", "output": "1\r\n2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "2\r\n2\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1\r\n1\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "2\r\n2\r\n2\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "2\r\n1\r\n1\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "2\r\n2\r\n1\r\n"}, {"input": "10\r\n9 8 5 4 1 1 2 1 1 1\r\n", "output": "2\r\n1\r\n1\r\n2\r\n2\r\n2\r\n1\r\n1\r\n1\r\n1\r\n"}, {"input": "1\r\n2\r\n", "output": "1\r\n"}, {"input": "2\r\n3 3\r\n", "output": "2\r\n2\r\n"}, {"input": "5\r\n2 2 2 1 1\r\n", "output": "1\r\n2\r\n1\r\n1\r\n1\r\n"}, {"input": "5\r\n1 1 1 2 1\r\n", "output": "2\r\n2\r\n2\r\n1\r\n1\r\n"}, {"input": "5\r\n2 1 1 1 1\r\n", "output": "1\r\n1\r\n1\r\n1\r\n1\r\n"}, {"input": "4\r\n1 2 1 1\r\n", "output": "2\r\n1\r\n1\r\n1\r\n"}, {"input": "5\r\n5 4 4 4 1\r\n", "output": "2\r\n1\r\n2\r\n1\r\n1\r\n"}, {"input": "2\r\n3 1\r\n", "output": "2\r\n2\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n1\r\n1\r\n"}, {"input": "5\r\n1 1 4 1 1\r\n", "output": "2\r\n2\r\n1\r\n1\r\n1\r\n"}]
| false
|
stdio
| null | true
|
22/E
|
22
|
E
|
PyPy 3
|
TESTS
| 9
| 124
| 1,331,200
|
153492206
|
N=int(input())
A=list(map(int,input().split()))
LAST=1
USE=[0]*(N+1)
USE[1]=1
while True:
LAST=A[LAST-1]
if USE[LAST]==1:
break
else:
USE[LAST]=1
# UnionFind
Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数
def find(x):
while Group[x] != x:
x=Group[x]
return x
def Union(x,y):
if find(x) != find(y):
if Nodes[find(x)] < Nodes[find(y)]:
Nodes[find(y)] += Nodes[find(x)]
Nodes[find(x)] = 0
Group[find(x)] = find(y)
else:
Nodes[find(x)] += Nodes[find(y)]
Nodes[find(y)] = 0
Group[find(y)] = find(x)
for i in range(N):
Union(i+1,A[i])
USE=[0]*(N+1)
for a in A:
USE[a]=1
ANS=[]
for i in range(1,N+1):
if USE[i]==0:
ANS.append((LAST,i))
Union(LAST,i)
for i in range(1,N+1):
if Nodes[find(i)]==N:
break
if find(i)==find(LAST):
continue
ANS.append((LAST,i))
Union(LAST,i)
print(len(ANS))
for x,y in ANS:
print(x,y)
| 40
| 592
| 12,800,000
|
98755646
|
n = int(input())
g = [int(i) - 1 for i in input().split()]
def solve(n, g):
vertex_id = [-1]*n
current_id = 0
starts_a_cycle = [-1]*n
cycle_vertex_sample = []
start_on_cycle = []
cycle_index_by_ID = []
cycle_count = 0
for v in range(n):
if vertex_id[v] != -1: continue
current_vertex = v
cycle_found = False
while vertex_id[current_vertex] == -1:
vertex_id[current_vertex] = current_id
current_vertex = g[current_vertex]
if vertex_id[current_vertex] == current_id:
cycle_found = True
cycle_count += 1
cycle_vertex_sample.append(current_vertex)
start_on_cycle.append(current_vertex == v)
if not cycle_found:
if starts_a_cycle[current_vertex] != -1:
starts_a_cycle[v] = starts_a_cycle[current_vertex]
starts_a_cycle[current_vertex] = -1
cycle_index_by_ID.append(cycle_index_by_ID[vertex_id[current_vertex]])
cycle = cycle_index_by_ID[-1]
if start_on_cycle[cycle]:
starts_a_cycle[cycle_vertex_sample[cycle]] = -1
start_on_cycle[cycle] = False
starts_a_cycle[v] = cycle
else:
starts_a_cycle[v] = cycle_count - 1
cycle_index_by_ID.append(cycle_count - 1)
current_id += 1
expanded = []
for i in range(n):
if starts_a_cycle[i] != -1:
cycle = starts_a_cycle[i]
v = cycle_vertex_sample[cycle]
expanded.append([v, i])
if len(expanded) == 1 and expanded[0][0] == expanded[0][1]:
print(0)
return
print(len(expanded))
for i in range(len(expanded)-1):
v, u = expanded[i][0], expanded[i+1][1]
print('{0} {1}'.format(v+1, u+1))
print('{0} {1}'.format(expanded[-1][0] + 1, expanded[0][1] + 1))
solve(n, g)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Scheme
|
To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?
|
The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.
|
In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.
| null | null |
[{"input": "3\n3 3 2", "output": "1\n3 1"}, {"input": "7\n2 3 1 3 4 4 1", "output": "3\n2 5\n2 6\n3 7"}]
| 2,300
|
["dfs and similar", "graphs", "trees"]
| 40
|
[{"input": "3\r\n3 3 2\r\n", "output": "1\r\n3 1\r\n"}, {"input": "7\r\n2 3 1 3 4 4 1\r\n", "output": "3\r\n1 5\r\n1 6\r\n1 7\r\n"}, {"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "4\r\n2 4 4 3\r\n", "output": "1\r\n4 1\r\n"}, {"input": "5\r\n5 3 5 2 3\r\n", "output": "2\r\n5 1\r\n5 4\r\n"}, {"input": "9\r\n2 5 6 7 4 1 9 6 8\r\n", "output": "1\r\n1 3\r\n"}, {"input": "20\r\n20 10 16 14 9 20 6 20 14 19 17 13 16 13 14 8 8 8 8 19\r\n", "output": "10\r\n20 1\r\n20 2\r\n20 3\r\n20 4\r\n20 5\r\n20 7\r\n20 11\r\n20 12\r\n20 15\r\n20 18\r\n"}, {"input": "7\r\n3 1 2 5 6 7 4\r\n", "output": "2\r\n1 4\r\n4 1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
f_list = list(map(int, f.readline().split()))
# Read submission output
with open(submission_output_path) as f:
try:
k_line = f.readline().strip()
if not k_line:
print(0)
return
k = int(k_line)
edges = []
for _ in range(k):
parts = f.readline().split()
if len(parts) != 2:
print(0)
return
xi = int(parts[0])
yi = int(parts[1])
if not (1 <= xi <= n and 1 <= yi <= n):
print(0)
return
edges.append( (xi, yi) )
except (ValueError, IndexError) as e:
print(0)
return
# Find components in original graph
visited = [False] * (n + 1)
components = []
node_to_component = {}
for i in range(1, n+1):
if not visited[i]:
path = []
current = i
while True:
if visited[current]:
if current in path:
idx = path.index(current)
cycle = path[idx:]
# Add cycle component
components.append( (True, cycle) )
comp_idx = len(components) - 1
for node in cycle:
visited[node] = True
node_to_component[node] = comp_idx
# Add non-cycle part as singleton components
for node in path[:idx]:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
else:
# The path is part of another component, add as singletons
for node in path:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
break
visited[current] = True
path.append(current)
current = f_list[current - 1] # f_list is 1-based
# Compute component_incoming
component_incoming = [0] * len(components)
for v in range(1, n+1):
comp_v = node_to_component[v]
f_v = f_list[v-1]
comp_fv = node_to_component[f_v]
if comp_v != comp_fv:
component_incoming[comp_fv] += 1
# Compute S and C
S = sum(1 for i in range(len(components)) if component_incoming[i] == 0)
C = sum(1 for is_cycle, _ in components if is_cycle)
if len(components) == 1:
minimal_k = 0
else:
minimal_k = max(S, C)
# Check K
if k != minimal_k:
print(0)
return
# Build the modified graph
adj = defaultdict(list)
for u in range(1, n+1):
original = f_list[u-1]
adj[u].append(original)
for xi, yi in edges:
adj[xi].append(yi)
# Check if the graph is strongly connected using Kosaraju's algorithm
visited = [False] * (n + 1)
order = []
def dfs1(u):
stack = [(u, False)]
while stack:
node, processed = stack.pop()
if processed:
order.append(node)
continue
if visited[node]:
continue
visited[node] = True
stack.append( (node, True) )
for v in adj[node]:
if not visited[v]:
stack.append( (v, False) )
for u in range(1, n+1):
if not visited[u]:
dfs1(u)
visited = [False] * (n + 1)
reversed_adj = defaultdict(list)
for u in adj:
for v in adj[u]:
reversed_adj[v].append(u)
count = 0
for u in reversed(order):
if not visited[u]:
count += 1
if count > 1:
print(0)
return
stack = [u]
visited[u] = True
while stack:
node = stack.pop()
for v in reversed_adj[node]:
if not visited[v]:
visited[v] = True
stack.append(v)
if count == 1:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
22/E
|
22
|
E
|
PyPy 3
|
TESTS
| 9
| 154
| 1,945,600
|
153492532
|
N=int(input())
A=list(map(int,input().split()))
LAST=1
USE=[0]*(N+1)
USE[1]=1
while True:
LAST=A[LAST-1]
if USE[LAST]==1:
break
else:
USE[LAST]=1
E=[[] for i in range(N)]
E_INV=[[] for i in range(N)]
for i in range(N):
x,y=i,A[i]-1
E[x].append(y)
E_INV[y].append(x)
def Top_sort(E):
Parent=[-1]*N
USEIND=[0]*N
TOP=[]
for ROOT in range(N):
if Parent[ROOT]!=-1:
continue
Parent[ROOT]=ROOT
NOW=ROOT
while NOW!=ROOT or USEIND[ROOT]!=len(E[ROOT]):
if USEIND[NOW]==len(E[NOW]):
TOP.append(NOW)
NOW=Parent[NOW]
elif E[NOW][USEIND[NOW]]==Parent[NOW]:
USEIND[NOW]+=1
else:
NEXT=E[NOW][USEIND[NOW]]
USEIND[NOW]+=1
if Parent[NEXT]==-1:
Parent[NEXT]=NOW
NOW=NEXT
TOP.append(ROOT)
return TOP[::-1]
USE=[0]*N
SCC=[]
def dfs2(x):
Q=[x]
USE[x]=1
ANS=[]
while Q:
x=Q.pop()
ANS.append(x)
for to in E_INV[x]:
if USE[to]==0:
USE[to]=1
Q.append(to)
return ANS
TOP_SORT=Top_sort(E)
for x in TOP_SORT:
if USE[x]==0:
SCC.append(dfs2(x))
# UnionFind
Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数
def find(x):
while Group[x] != x:
x=Group[x]
return x
def Union(x,y):
if find(x) != find(y):
if Nodes[find(x)] < Nodes[find(y)]:
Nodes[find(y)] += Nodes[find(x)]
Nodes[find(x)] = 0
Group[find(x)] = find(y)
else:
Nodes[find(x)] += Nodes[find(y)]
Nodes[find(y)] = 0
Group[find(y)] = find(x)
for i in range(N):
Union(i+1,A[i])
USE=[0]*(N+1)
for a in A:
USE[a]=1
ANS=[]
for i in range(1,N+1):
if USE[i]==0:
ANS.append((LAST,i))
Union(LAST,i)
for scc in SCC:
for x in scc:
x+=1
if Nodes[find(x)]==N:
break
if find(x)==find(LAST):
continue
ANS.append((LAST,x))
Union(LAST,x)
print(len(ANS))
for x,y in ANS:
print(x,y)
| 40
| 1,714
| 90,931,200
|
157748457
|
order = [] # Psuedo-topological ordering (we don't care about cycles)
visited = set()
next_id = 0
nodes = {} # Maps from node to component id
components = [] # Maps from component id to a set of nodes
def dfs3(start, get_neighbors):
'''
Iterative implementation of DFS. Doing DFS with recrusion in Python seems
to cause TLE errors a decent amount of the time
'''
q = [(start, False)]
visited.add(start)
while len(q) > 0:
node, indicator = q.pop()
if indicator:
order.append(node)
continue
q.append((node, True))
for neighbor in get_neighbors(node):
if neighbor not in visited:
visited.add(neighbor)
q.append((neighbor, False))
def dfs1(start, get_neighbors):
'''
First round of DFS performs a reverse topological sort to order the nodes
by decreasing time of exit
'''
visited.add(start)
for neighbor in get_neighbors(start):
if neighbor not in visited:
dfs1(neighbor, get_neighbors)
order.append(start)
def dfs2(start, get_rev_neighbors):
'''
Second round of DFS finds connected components in the reversed graph.
Performing this by starting at the node with the highest exit time produces
strongly connected components
'''
q = [start]
nodes[start] = next_id
components[next_id].add(start)
while len(q) > 0:
node = q.pop()
for neighbor in get_rev_neighbors(node):
if neighbor not in nodes:
nodes[neighbor] = next_id
components[next_id].add(neighbor)
q.append(neighbor)
def find_components(all_nodes, get_neighbors, get_rev_neighbors):
'''
Note: the condensation graph is a set of acyclic graphs
'''
global next_id, order
for node in all_nodes:
if node not in visited:
dfs3(node, get_neighbors)
order = order[::-1]
for node in order:
if node not in nodes:
components.append(set())
dfs2(node, get_rev_neighbors)
next_id += 1
cond_graph = [[] for i in range(len(components))]
for node in all_nodes:
for neighbor in get_neighbors(node):
first_comp, second_comp = nodes[node], nodes[neighbor]
# If we find a node and neighbor pair in different components, then
# we found an edge in the condensation graph
if first_comp != second_comp:
cond_graph[first_comp].append(second_comp)
return cond_graph
# Example use. This TLEs on the following, but the core algorithm should be correct
# https://codeforces.com/contest/22/problem/E
def set_peek(s):
val = s.pop()
s.add(val)
return val
n = int(input())
graph = [None]
graph_rev = [[] for i in range(n + 1)]
for i, num in enumerate(map(int, input().split())):
graph.append([num])
graph_rev[num].append(i + 1)
# Step 1: build the condensation graph to get an acyclic graph
cond_graph = find_components(range(1, n + 1), lambda x: graph[x], lambda x: graph_rev[x])
# Step 2: identify all the "roots" in this graph as those with no neighbors
roots = [i for i in range(len(cond_graph)) if len(cond_graph[i]) == 0]
# Step 3: reverse the condensation graph to form a tree
cond_graph_rev = [[] for _ in range(len(cond_graph))]
for i, neighbors in enumerate(cond_graph):
for neighbor in neighbors:
cond_graph_rev[neighbor].append(i)
# Step 4: Use DFS to find the leaves of each tree
leaves = []
for root in roots:
these_leaves = []
q = [root]
while len(q) > 0:
node = q.pop()
# Note that a root can be its own leaf
if len(cond_graph_rev[node]) == 0:
these_leaves.append(node)
continue
for neighbor in cond_graph_rev[node]:
q.append(neighbor)
leaves.append(these_leaves)
# Step 5: connect each root to leaves of the next tree
ans = []
if len(roots) >= 2:
for i in range(len(roots)):
root = roots[i]
root_rep = set_peek(components[root])
ls = leaves[(i + 1) % len(roots)]
for l in ls:
l_rep = set_peek(components[l])
ans.append((root_rep, l_rep))
else:
root_rep = set_peek(components[roots[0]])
ls = leaves[0]
for l in ls:
if l == root: continue
l_rep = set_peek(components[l])
ans.append((root_rep, l_rep))
print(len(ans))
for a, b in ans:
print(a, b)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Scheme
|
To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?
|
The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.
|
In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.
| null | null |
[{"input": "3\n3 3 2", "output": "1\n3 1"}, {"input": "7\n2 3 1 3 4 4 1", "output": "3\n2 5\n2 6\n3 7"}]
| 2,300
|
["dfs and similar", "graphs", "trees"]
| 40
|
[{"input": "3\r\n3 3 2\r\n", "output": "1\r\n3 1\r\n"}, {"input": "7\r\n2 3 1 3 4 4 1\r\n", "output": "3\r\n1 5\r\n1 6\r\n1 7\r\n"}, {"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "4\r\n2 4 4 3\r\n", "output": "1\r\n4 1\r\n"}, {"input": "5\r\n5 3 5 2 3\r\n", "output": "2\r\n5 1\r\n5 4\r\n"}, {"input": "9\r\n2 5 6 7 4 1 9 6 8\r\n", "output": "1\r\n1 3\r\n"}, {"input": "20\r\n20 10 16 14 9 20 6 20 14 19 17 13 16 13 14 8 8 8 8 19\r\n", "output": "10\r\n20 1\r\n20 2\r\n20 3\r\n20 4\r\n20 5\r\n20 7\r\n20 11\r\n20 12\r\n20 15\r\n20 18\r\n"}, {"input": "7\r\n3 1 2 5 6 7 4\r\n", "output": "2\r\n1 4\r\n4 1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
f_list = list(map(int, f.readline().split()))
# Read submission output
with open(submission_output_path) as f:
try:
k_line = f.readline().strip()
if not k_line:
print(0)
return
k = int(k_line)
edges = []
for _ in range(k):
parts = f.readline().split()
if len(parts) != 2:
print(0)
return
xi = int(parts[0])
yi = int(parts[1])
if not (1 <= xi <= n and 1 <= yi <= n):
print(0)
return
edges.append( (xi, yi) )
except (ValueError, IndexError) as e:
print(0)
return
# Find components in original graph
visited = [False] * (n + 1)
components = []
node_to_component = {}
for i in range(1, n+1):
if not visited[i]:
path = []
current = i
while True:
if visited[current]:
if current in path:
idx = path.index(current)
cycle = path[idx:]
# Add cycle component
components.append( (True, cycle) )
comp_idx = len(components) - 1
for node in cycle:
visited[node] = True
node_to_component[node] = comp_idx
# Add non-cycle part as singleton components
for node in path[:idx]:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
else:
# The path is part of another component, add as singletons
for node in path:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
break
visited[current] = True
path.append(current)
current = f_list[current - 1] # f_list is 1-based
# Compute component_incoming
component_incoming = [0] * len(components)
for v in range(1, n+1):
comp_v = node_to_component[v]
f_v = f_list[v-1]
comp_fv = node_to_component[f_v]
if comp_v != comp_fv:
component_incoming[comp_fv] += 1
# Compute S and C
S = sum(1 for i in range(len(components)) if component_incoming[i] == 0)
C = sum(1 for is_cycle, _ in components if is_cycle)
if len(components) == 1:
minimal_k = 0
else:
minimal_k = max(S, C)
# Check K
if k != minimal_k:
print(0)
return
# Build the modified graph
adj = defaultdict(list)
for u in range(1, n+1):
original = f_list[u-1]
adj[u].append(original)
for xi, yi in edges:
adj[xi].append(yi)
# Check if the graph is strongly connected using Kosaraju's algorithm
visited = [False] * (n + 1)
order = []
def dfs1(u):
stack = [(u, False)]
while stack:
node, processed = stack.pop()
if processed:
order.append(node)
continue
if visited[node]:
continue
visited[node] = True
stack.append( (node, True) )
for v in adj[node]:
if not visited[v]:
stack.append( (v, False) )
for u in range(1, n+1):
if not visited[u]:
dfs1(u)
visited = [False] * (n + 1)
reversed_adj = defaultdict(list)
for u in adj:
for v in adj[u]:
reversed_adj[v].append(u)
count = 0
for u in reversed(order):
if not visited[u]:
count += 1
if count > 1:
print(0)
return
stack = [u]
visited[u] = True
while stack:
node = stack.pop()
for v in reversed_adj[node]:
if not visited[v]:
visited[v] = True
stack.append(v)
if count == 1:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
22/E
|
22
|
E
|
PyPy 3
|
TESTS
| 9
| 154
| 1,638,400
|
145968215
|
import sys
input = sys.stdin.readline
def get_root(s):
v = []
while not s == root[s]:
v.append(s)
s = root[s]
for i in v:
root[i] = s
return s
def unite(s, t):
rs, rt = get_root(s), get_root(t)
if not rs ^ rt:
return
if rank[s] == rank[t]:
rank[rs] += 1
if rank[s] >= rank[t]:
root[rt] = rs
size[rs] += size[rt]
else:
root[rs] = rt
size[rt] += size[rs]
return
def same(s, t):
return True if get_root(s) == get_root(t) else False
def get_size(s):
return size[get_root(s)]
n = int(input())
f = [0] + list(map(int, input().split()))
visit = [0] * (n + 1)
s = set(f)
u = []
v = []
root = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]
size = [1 for _ in range(n + 1)]
for i in range(1, n + 1):
if i in s:
continue
u.append(i)
c = 0
j = i
while not visit[j]:
c += 1
visit[j] = 1
unite(j, f[j])
j = f[j]
if not get_size(i) ^ c:
v.append(j)
ans = []
for i in range(len(v) - 1):
ans.append((v[i], v[-1]))
for i in u:
ans.append((v[-1], i))
c = len(ans)
print(c)
if not c:
exit()
for i in range(c):
ans[i] = " ".join(map(str, ans[i]))
sys.stdout.write("\n".join(ans))
| 40
| 592
| 12,800,000
|
98755646
|
n = int(input())
g = [int(i) - 1 for i in input().split()]
def solve(n, g):
vertex_id = [-1]*n
current_id = 0
starts_a_cycle = [-1]*n
cycle_vertex_sample = []
start_on_cycle = []
cycle_index_by_ID = []
cycle_count = 0
for v in range(n):
if vertex_id[v] != -1: continue
current_vertex = v
cycle_found = False
while vertex_id[current_vertex] == -1:
vertex_id[current_vertex] = current_id
current_vertex = g[current_vertex]
if vertex_id[current_vertex] == current_id:
cycle_found = True
cycle_count += 1
cycle_vertex_sample.append(current_vertex)
start_on_cycle.append(current_vertex == v)
if not cycle_found:
if starts_a_cycle[current_vertex] != -1:
starts_a_cycle[v] = starts_a_cycle[current_vertex]
starts_a_cycle[current_vertex] = -1
cycle_index_by_ID.append(cycle_index_by_ID[vertex_id[current_vertex]])
cycle = cycle_index_by_ID[-1]
if start_on_cycle[cycle]:
starts_a_cycle[cycle_vertex_sample[cycle]] = -1
start_on_cycle[cycle] = False
starts_a_cycle[v] = cycle
else:
starts_a_cycle[v] = cycle_count - 1
cycle_index_by_ID.append(cycle_count - 1)
current_id += 1
expanded = []
for i in range(n):
if starts_a_cycle[i] != -1:
cycle = starts_a_cycle[i]
v = cycle_vertex_sample[cycle]
expanded.append([v, i])
if len(expanded) == 1 and expanded[0][0] == expanded[0][1]:
print(0)
return
print(len(expanded))
for i in range(len(expanded)-1):
v, u = expanded[i][0], expanded[i+1][1]
print('{0} {1}'.format(v+1, u+1))
print('{0} {1}'.format(expanded[-1][0] + 1, expanded[0][1] + 1))
solve(n, g)
|
Codeforces Beta Round 22 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Scheme
|
To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?
|
The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.
|
In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.
| null | null |
[{"input": "3\n3 3 2", "output": "1\n3 1"}, {"input": "7\n2 3 1 3 4 4 1", "output": "3\n2 5\n2 6\n3 7"}]
| 2,300
|
["dfs and similar", "graphs", "trees"]
| 40
|
[{"input": "3\r\n3 3 2\r\n", "output": "1\r\n3 1\r\n"}, {"input": "7\r\n2 3 1 3 4 4 1\r\n", "output": "3\r\n1 5\r\n1 6\r\n1 7\r\n"}, {"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "4\r\n2 4 4 3\r\n", "output": "1\r\n4 1\r\n"}, {"input": "5\r\n5 3 5 2 3\r\n", "output": "2\r\n5 1\r\n5 4\r\n"}, {"input": "9\r\n2 5 6 7 4 1 9 6 8\r\n", "output": "1\r\n1 3\r\n"}, {"input": "20\r\n20 10 16 14 9 20 6 20 14 19 17 13 16 13 14 8 8 8 8 19\r\n", "output": "10\r\n20 1\r\n20 2\r\n20 3\r\n20 4\r\n20 5\r\n20 7\r\n20 11\r\n20 12\r\n20 15\r\n20 18\r\n"}, {"input": "7\r\n3 1 2 5 6 7 4\r\n", "output": "2\r\n1 4\r\n4 1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
f_list = list(map(int, f.readline().split()))
# Read submission output
with open(submission_output_path) as f:
try:
k_line = f.readline().strip()
if not k_line:
print(0)
return
k = int(k_line)
edges = []
for _ in range(k):
parts = f.readline().split()
if len(parts) != 2:
print(0)
return
xi = int(parts[0])
yi = int(parts[1])
if not (1 <= xi <= n and 1 <= yi <= n):
print(0)
return
edges.append( (xi, yi) )
except (ValueError, IndexError) as e:
print(0)
return
# Find components in original graph
visited = [False] * (n + 1)
components = []
node_to_component = {}
for i in range(1, n+1):
if not visited[i]:
path = []
current = i
while True:
if visited[current]:
if current in path:
idx = path.index(current)
cycle = path[idx:]
# Add cycle component
components.append( (True, cycle) )
comp_idx = len(components) - 1
for node in cycle:
visited[node] = True
node_to_component[node] = comp_idx
# Add non-cycle part as singleton components
for node in path[:idx]:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
else:
# The path is part of another component, add as singletons
for node in path:
components.append( (False, [node]) )
visited[node] = True
node_to_component[node] = len(components) - 1
break
visited[current] = True
path.append(current)
current = f_list[current - 1] # f_list is 1-based
# Compute component_incoming
component_incoming = [0] * len(components)
for v in range(1, n+1):
comp_v = node_to_component[v]
f_v = f_list[v-1]
comp_fv = node_to_component[f_v]
if comp_v != comp_fv:
component_incoming[comp_fv] += 1
# Compute S and C
S = sum(1 for i in range(len(components)) if component_incoming[i] == 0)
C = sum(1 for is_cycle, _ in components if is_cycle)
if len(components) == 1:
minimal_k = 0
else:
minimal_k = max(S, C)
# Check K
if k != minimal_k:
print(0)
return
# Build the modified graph
adj = defaultdict(list)
for u in range(1, n+1):
original = f_list[u-1]
adj[u].append(original)
for xi, yi in edges:
adj[xi].append(yi)
# Check if the graph is strongly connected using Kosaraju's algorithm
visited = [False] * (n + 1)
order = []
def dfs1(u):
stack = [(u, False)]
while stack:
node, processed = stack.pop()
if processed:
order.append(node)
continue
if visited[node]:
continue
visited[node] = True
stack.append( (node, True) )
for v in adj[node]:
if not visited[v]:
stack.append( (v, False) )
for u in range(1, n+1):
if not visited[u]:
dfs1(u)
visited = [False] * (n + 1)
reversed_adj = defaultdict(list)
for u in adj:
for v in adj[u]:
reversed_adj[v].append(u)
count = 0
for u in reversed(order):
if not visited[u]:
count += 1
if count > 1:
print(0)
return
stack = [u]
visited[u] = True
while stack:
node = stack.pop()
for v in reversed_adj[node]:
if not visited[v]:
visited[v] = True
stack.append(v)
if count == 1:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
532/B
|
533
|
B
|
PyPy 3
|
TESTS
| 6
| 140
| 0
|
57161654
|
from operator import itemgetter
def match_subs(tree):
for i, v in enumerate(tree):
if v[0] > 0:
tree[v[0] - 1][2].append(i + 1)
tree[v[0] - 1][3] += 1
def explore_tree(tree):
to_visit = [(tree[0][4], 0)]
while to_visit:
visiting = to_visit.pop(0)
level = visiting[1]
visiting = tree[visiting[0]]
to_visit += [(x - 1, level + 1) for x in tree[visiting[4]][2]]
tree[visiting[4]][5] = level
#print(visiting[4])
#print(to_visit)
class CodeforcesTask533BSolution:
def __init__(self):
self.result = ''
self.workers_count = 0
self.workers = []
def read_input(self):
self.workers_count = int(input())
for x in range(self.workers_count):
self.workers.append([int(x) for x in input().split(" ")] + [[], 0, x, 0, [], 0])
def process_task(self):
match_subs(self.workers)
#print(self.workers)
explore_tree(self.workers)
tree2 = self.workers.copy()
tree2.sort(key=itemgetter(5), reverse=True)
for v in tree2:
if v[6]:
if len(v[6]) % 2:
v[6].sort(reverse=True)
v[7] = sum(v[6][:-1])
else:
v[7] = sum(v[6])
if v[0] > 0:
self.workers[v[0] - 1][6].append(v[7] + v[1])
self.result = str(self.workers[0][7] + self.workers[0][1])
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask533BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
| 41
| 967
| 73,318,400
|
231037237
|
n = int(input())
t = [list(map(int, input().split())) for q in range(n)]
n += 1
u = [-1e7] * n
v = [0] * n
for i, (j, a) in list(enumerate(t, 1))[::-1]:
u[i] = max(u[i], v[i] + a)
v[j], u[j] = max(v[j] + v[i], u[j] + u[i]), max(v[j] + u[i], u[j] + v[i])
print(u[1])
|
VK Cup 2015 - Round 2
|
CF
| 2,015
| 2
| 256
|
Work Group
|
One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.
We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.
To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.
The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.
Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company.
Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 = - 1, for all other people the condition 1 ≤ pi < i is fulfilled.
|
Print a single integer — the maximum possible efficiency of the workgroup.
| null |
In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.
|
[{"input": "7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2", "output": "17"}]
| 2,000
|
[]
| 41
|
[{"input": "7\r\n-1 3\r\n1 2\r\n1 1\r\n1 4\r\n4 5\r\n4 3\r\n5 2\r\n", "output": "17\n"}, {"input": "1\r\n-1 42\r\n", "output": "42\n"}, {"input": "2\r\n-1 3\r\n1 2\r\n", "output": "3\n"}, {"input": "3\r\n-1 3\r\n1 1\r\n1 2\r\n", "output": "6\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n1 3\r\n", "output": "6\n"}, {"input": "3\r\n-1 3\r\n1 2\r\n2 1\r\n", "output": "3\n"}, {"input": "20\r\n-1 100\r\n1 10\r\n2 26\r\n2 33\r\n3 31\r\n2 28\r\n1 47\r\n6 18\r\n6 25\r\n9 2\r\n4 17\r\n6 18\r\n6 2\r\n6 30\r\n13 7\r\n5 25\r\n7 11\r\n11 7\r\n17 40\r\n12 43\r\n", "output": "355\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n2 22\r\n3 28\r\n3 2\r\n4 8\r\n3 17\r\n2 50\r\n5 37\r\n5 25\r\n4 29\r\n9 21\r\n10 16\r\n10 39\r\n11 41\r\n9 28\r\n9 30\r\n12 36\r\n13 26\r\n19 17\r\n", "output": "459\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n1 22\r\n1 28\r\n1 2\r\n1 8\r\n1 17\r\n1 50\r\n5 37\r\n1 25\r\n1 29\r\n5 21\r\n4 16\r\n2 39\r\n1 41\r\n3 28\r\n3 30\r\n2 36\r\n2 26\r\n14 17\r\n", "output": "548\n"}, {"input": "3\r\n-1 1\r\n1 42\r\n1 42\r\n", "output": "85\n"}, {"input": "2\r\n-1 1\r\n1 2\r\n", "output": "2\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n2 3\r\n", "output": "3\n"}, {"input": "4\r\n-1 1\r\n1 42\r\n1 42\r\n1 42\r\n", "output": "126\n"}, {"input": "4\r\n-1 1\r\n1 100\r\n1 100\r\n1 100\r\n", "output": "300\n"}]
| false
|
stdio
| null | true
|
461/B
|
461
|
B
|
PyPy 3-64
|
TESTS
| 6
| 77
| 3,481,600
|
214738405
|
def II():
return(int(input()))
def LMI():
return(list(map(int,input().split())))
def I():
return(input())
def MII():
return(map(int,input().split()))
import sys
input=sys.stdin.readline
# import io,os
# input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from collections import Counter
# int(math.log(len(L)))
# import math
# from collections import defaultdict
# mod=10**9+7
from collections import deque
for _ in range(1):
n=II()
G=[[] for _ in range(n)]
T=LMI()
for i in range(n-1):
G[i+1].append(T[i])
G[T[i]].append(i+1)
B=LMI()
dp=[[0,0] for _ in range(n)]
D=deque()
D.append(0)
seq=[]
Parent=[0]*(n)
Parent[0]=0
while D:
a=D.popleft()
seq.append(a)
for j in G[a]:
if j!=Parent[a]:
Parent[j]=a
D.append(j)
for i in seq[::-1]:
dp[i][1]=0
dp[i][0]=1
for j in G[i]:
if j!=Parent[i]:
dp[i][1]*=dp[j][0]
dp[i][1]+=dp[i][0]*dp[j][1]
dp[i][0]*=dp[j][0]
if B[i]==1:
dp[i][1]=dp[i][0]
else:
dp[i][0]+=dp[i][1]
print(dp[0][1])
# if __name__=="__main__":
# for _ in range(II()):
# t()
# t()
| 23
| 343
| 14,643,200
|
7589394
|
MOD = 1000000007
n = int(input())
p = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
children = [[] for x in range(n)]
for i in range(1,n):
children[p[i-1]].append(i)
#print(children)
count = [(0,0) for i in range(n)]
for i in reversed(range(n)):
prod = 1
for ch in children[i]:
prod *= count[ch][0]+count[ch][1]
if x[i]:
count[i] = (0,prod % MOD)
else:
tot = 0
for ch in children[i]:
cur = count[ch][1]*prod // (count[ch][0]+count[ch][1])
tot += cur
count[i] = (prod % MOD, tot % MOD)
print(count[0][1])
|
Codeforces Round 263 (Div. 1)
|
CF
| 2,014
| 2
| 256
|
Appleman and Tree
|
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
|
The first line contains an integer n (2 ≤ n ≤ 105) — the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
|
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
| null | null |
[{"input": "3\n0 0\n0 1 1", "output": "2"}, {"input": "6\n0 1 1 0 4\n1 1 0 0 1 0", "output": "1"}, {"input": "10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1", "output": "27"}]
| 2,000
|
["dfs and similar", "dp", "trees"]
| 23
|
[{"input": "3\r\n0 0\r\n0 1 1\r\n", "output": "2\r\n"}, {"input": "6\r\n0 1 1 0 4\r\n1 1 0 0 1 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 2 1 4 4 4 0 8\r\n0 0 0 1 0 1 1 0 0 1\r\n", "output": "27\r\n"}, {"input": "5\r\n0 1 1 3\r\n0 0 0 1 1\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 1 2 4 3 3 3 2\r\n1 0 1 1 1 0 0 1 1 0\r\n", "output": "3\r\n"}, {"input": "100\r\n0 0 2 2 0 3 5 0 6 2 0 4 0 2 3 7 8 3 15 19 13 8 18 19 3 14 23 9 6 3 6 17 26 24 20 6 4 27 8 5 14 5 35 31 27 3 41 25 20 14 25 31 49 40 0 1 10 5 50 13 29 58 1 6 8 1 40 52 30 15 50 8 66 52 29 71 25 68 36 7 80 60 6 2 11 43 62 27 84 86 71 38 14 50 88 4 8 95 53\r\n1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 1\r\n", "output": "9523200\r\n"}, {"input": "2\r\n0\r\n1 0\r\n", "output": "1\r\n"}, {"input": "115\r\n0 0 1 2 0 4 1 3 4 1 4 5 4 5 0 0 3 1 2 3 3 0 5 1 3 4 1 5 2 0 1 3 3 1 3 5 0 4 5 1 3 0 0 1 3 1 1 3 3 3 2 3 1 3 0 2 5 5 1 1 2 2 1 1 3 2 1 2 3 1 5 4 2 1 2 1 1 2 3 4 3 1 5 0 2 4 4 5 2 5 0 2 4 5 5 5 5 0 3 1 1 4 2 2 4 3 3 0 3 3 0 2 0 0\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
532/B
|
533
|
B
|
PyPy 3
|
TESTS
| 6
| 77
| 409,600
|
201708850
|
import io,os
from collections import deque
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main(t):
n = int(input())
children = [[] for _ in range(n)]
dp = [[0,0] for _ in range(n)]
arr = [0]*n
for i in range(n):
u,a = map(int,input().split())
if i>0: children[u-1].append(i)
arr[i] = a
seq = []
queue = deque()
queue.append(0)
while queue:
index = queue.popleft()
seq.append(index)
for nextindex in children[index]:
queue.append(nextindex)
for index in seq[::-1]:
maxnum = 0
mindiff = float('inf')
maxcount = 0
if len(children[index])==0:
dp[index][1] = arr[index]
continue
for nextindex in children[index]:
if dp[nextindex][0] < dp[nextindex][1]: maxcount = 1 - maxcount
maxnum += max(dp[nextindex])
mindiff = min(mindiff, abs(dp[nextindex][0]-dp[nextindex][1]))
if maxcount == 0:
dp[index][1] = maxnum + arr[index]
dp[index][0] = maxnum
else:
dp[index][1] = maxnum-mindiff + arr[index]
dp[index][0] = maxnum-mindiff
# print(dp)
ans = max(dp[0])
print(ans)
T = 1 #int(input())
t = 1
while t<=T:
main(t)
t += 1
| 41
| 967
| 73,318,400
|
231037237
|
n = int(input())
t = [list(map(int, input().split())) for q in range(n)]
n += 1
u = [-1e7] * n
v = [0] * n
for i, (j, a) in list(enumerate(t, 1))[::-1]:
u[i] = max(u[i], v[i] + a)
v[j], u[j] = max(v[j] + v[i], u[j] + u[i]), max(v[j] + u[i], u[j] + v[i])
print(u[1])
|
VK Cup 2015 - Round 2
|
CF
| 2,015
| 2
| 256
|
Work Group
|
One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.
We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.
To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.
The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.
Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company.
Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 = - 1, for all other people the condition 1 ≤ pi < i is fulfilled.
|
Print a single integer — the maximum possible efficiency of the workgroup.
| null |
In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.
|
[{"input": "7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2", "output": "17"}]
| 2,000
|
[]
| 41
|
[{"input": "7\r\n-1 3\r\n1 2\r\n1 1\r\n1 4\r\n4 5\r\n4 3\r\n5 2\r\n", "output": "17\n"}, {"input": "1\r\n-1 42\r\n", "output": "42\n"}, {"input": "2\r\n-1 3\r\n1 2\r\n", "output": "3\n"}, {"input": "3\r\n-1 3\r\n1 1\r\n1 2\r\n", "output": "6\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n1 3\r\n", "output": "6\n"}, {"input": "3\r\n-1 3\r\n1 2\r\n2 1\r\n", "output": "3\n"}, {"input": "20\r\n-1 100\r\n1 10\r\n2 26\r\n2 33\r\n3 31\r\n2 28\r\n1 47\r\n6 18\r\n6 25\r\n9 2\r\n4 17\r\n6 18\r\n6 2\r\n6 30\r\n13 7\r\n5 25\r\n7 11\r\n11 7\r\n17 40\r\n12 43\r\n", "output": "355\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n2 22\r\n3 28\r\n3 2\r\n4 8\r\n3 17\r\n2 50\r\n5 37\r\n5 25\r\n4 29\r\n9 21\r\n10 16\r\n10 39\r\n11 41\r\n9 28\r\n9 30\r\n12 36\r\n13 26\r\n19 17\r\n", "output": "459\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n1 22\r\n1 28\r\n1 2\r\n1 8\r\n1 17\r\n1 50\r\n5 37\r\n1 25\r\n1 29\r\n5 21\r\n4 16\r\n2 39\r\n1 41\r\n3 28\r\n3 30\r\n2 36\r\n2 26\r\n14 17\r\n", "output": "548\n"}, {"input": "3\r\n-1 1\r\n1 42\r\n1 42\r\n", "output": "85\n"}, {"input": "2\r\n-1 1\r\n1 2\r\n", "output": "2\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n2 3\r\n", "output": "3\n"}, {"input": "4\r\n-1 1\r\n1 42\r\n1 42\r\n1 42\r\n", "output": "126\n"}, {"input": "4\r\n-1 1\r\n1 100\r\n1 100\r\n1 100\r\n", "output": "300\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
Python 3
|
TESTS
| 20
| 124
| 0
|
153693126
|
n,k = map(int,input().split())
maxi=-1000000
for i in range(n):
f,t = map(int,input().split())
if t <= k:
if maxi <=f:
maxi = f
else:
if t >k:
if f - (t-k) > maxi:
maxi = f - (t-k)
print(maxi)
| 35
| 92
| 0
|
146026896
|
n,k=[int(x) for x in input().split()]
maxm=-1000000000
for i in range(n):
f,t=[int(x) for x in input().split()]
if t>k:
if f-(t-k)>maxm:
maxm=f-(t-k)
elif f>maxm:
maxm=f
print(maxm)
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
PyPy 3
|
TESTS
| 20
| 404
| 5,017,600
|
162594342
|
#A. Lunch Rush
n,k = [int(x) for x in input().split()]
r = []
for _ in range(n):
r.append([int(x) for x in input().split()])
max_hap = -999999
for i in range(n):
curr_hap = 0
if r[i][1] > k: curr_hap = r[i][0] - (r[i][1] - k)
else: curr_hap = r[i][0]
if curr_hap>max_hap:
max_hap=curr_hap
print((max_hap))
| 35
| 92
| 0
|
146944862
|
n , k = map(int,input().split())
a = []
for i in range(n):
f,t = map(int,input().split())
if t <= k:
a.append(f)
if t > k:
a.append(f-(t-k))
print(max(a))
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
Python 3
|
TESTS
| 20
| 92
| 0
|
190982217
|
n, k = map(int, input().split())
ans = -1000000
for i in range(n):
f, t = [int(x) for x in input().split()]
if t > k:
joy = f - (t - k)
else:
joy = f
if ans < joy: ans = joy
print(ans)
| 35
| 92
| 0
|
149697219
|
n, k = map(int, input().split())
Max = -9999999999
for _ in range(n):
f, t = map(int, input().split())
if t > k:
Max = max(Max, f-t+k)
else:
Max = max(Max, f)
print(Max)
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
Python 3
|
TESTS
| 8
| 92
| 0
|
227516304
|
n, k = map(int, input().split())
max = -10e00000
for i in range(n):
f, t = map(int, input().split())
if t <= k and f > max:
max = f
elif t > k and f - (t - k) > max:
max = f - (t - k)
print(max)
| 35
| 92
| 0
|
149995833
|
n, k = [int(i) for i in input().split()]
output = None
for i in range(n):
f1, t1 = [int(i) for i in input().split()]
if t1 > k:
joy = f1 - (t1 - k)
else:
joy = f1
if output == None or joy > output:
output = joy
print(output)
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
PyPy 3-64
|
TESTS
| 17
| 1,044
| 27,852,800
|
230267642
|
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2 # Найдем средний индекс
if arr[mid] == target:
return mid # Элемент найден, возвращаем его индекс
elif arr[mid] < target:
left = mid + 1 # Искомый элемент находится в правой половине
else:
right = mid - 1 # Искомый элемент находится в левой половине
return left
def solve():
n = int(input())
array_not_sort = []
for _ in range(n):
x, b = map(int, input().split())
array_not_sort.append([x, b])
array_not_sort.sort(key=lambda item: item[0])
array = [array_not_sort[0] + [0]]
for i in range(1, n):
x, b = array_not_sort[i]
array.append([x, b, 0])
x_destroy = x - b
index = binary_search(array, [x_destroy])
count_destroy = len(array) - 1 - index
array[-1][2] = count_destroy
good_b = 0
step = 0
current_destroy = 0
destroyed = 0
for i in range(n-1, -1, -1):
step += 1
if not destroyed:
current_destroy += array[i][2]
destroyed = array[i][2] + 1
destroyed -= 1
if step < current_destroy:
good_b = step
current_destroy = good_b
destroyed = 0
return current_destroy
print(solve())
| 41
| 124
| 19,763,200
|
165624620
|
import sys
input = sys.stdin.readline
M = 10 ** 6 + 5
n = int(input())
dp = [0] * M
b = [0] * M
for i in range(n):
a, c = map(int, input().split())
b[a] = c
if(b[0] > 0):
dp[0] = 1
ans = 0
for i in range(1, M):
if(b[i] == 0):
dp[i] = dp[i - 1]
else:
if(b[i] >= i):
dp[i] = 1
else:
dp[i] = dp[i - b[i] - 1] + 1
ans = max(ans, dp[i])
print(n - ans)
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
PyPy 3
|
TESTS
| 10
| 888
| 14,028,800
|
80949846
|
n = int(input())
lis=[0]*(1000004)
dp=[0]*(1000004)
for i in range(n):
a,b = map(int,input().split())
lis[a]=b
if lis[0]>0:
dp[0]=1
for i in range(1,1000002):
if lis[i]>0:
dp[i]=max(dp[i-1],dp[max(-1,i-lis[i]-1)]+1)
else:
dp[i]=dp[i-1]
print(n-max(dp))
| 41
| 265
| 26,726,400
|
158373448
|
import sys
input = sys.stdin.read
MAX = int(1e6+7)
b = [0 for _ in range(MAX)]
d = [0 for _ in range(MAX)]
mx = 0
r = map(int,input().split())
n = next(r)
for i in range(n):
a = next(r)
b[a] = next(r)
if b[0] > 0:
d[0] = 1
else:
d[0] = 0
for i in range(1,MAX):
if b[i] == 0:
d[i] = d[i-1]
elif i <= b[i]:
d[i] = 1
else:
d[i] = d[i - b[i] - 1] + 1
mx = max(mx,d[i])
print(n - mx)
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.