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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
56/B
|
56
|
B
|
Python 3
|
TESTS
| 7
| 186
| 307,200
|
92996819
|
n=int(input())
l=list(map(int,input().split()))
l1=l.copy()
for i in l:
if(i==1):
break
else:
l.remove(i)
l.append(i)
l1.sort()
if(l==l1):
print(0,0)
else:
for i in range(n-1):
if(abs(l[i]-l[i+1])>1):
a=i+1+1
b=l[i+1]
l2=l[:i+1]
l3=l[i+1:b]
l4=l[b:]
l3=l3[::-1]
l5=l2+l3+l4
break
if(l5==l1):
print(a,b)
else:
print(0,0)
| 33
| 124
| 0
|
13335876
|
r=range
i=input
i()
l=list(map(int,i().split()))
#print(l)
s=0
p=q=-1
for _ in r(len(l)):
if(l[_]!=_+1):
p=_+1
q=l[_]
break
if(p!=-1):
#print(p,q)
l[p-1:q]=sorted(l[p-1:q])
#print(l)
f=1*(p!=-1)
for x in r(len(l)):
if(l[x]!=x+1):f=0
if(f):print("%d %d"%(p,q))
else:print("0 0")
|
Codeforces Beta Round 52 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Spoilt Permutation
|
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
|
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
|
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
| null | null |
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
| 1,300
|
["implementation"]
| 33
|
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
| false
|
stdio
| null | true
|
50/C
|
50
|
C
|
Python 3
|
TESTS
| 2
| 122
| 307,200
|
110405413
|
def O3():
n = int(input())
ans = []
for _ in range(n):
x, y = [int(x) for x in input().split()]
ans.append([x,y])
minX = min([ans[i][0] for i in range(n)])
minY = min([ans[i][1] for i in range(n)])
maxX = max([ans[i][0] for i in range(n)])
maxY = max([ans[i][1] for i in range(n)])
if n%2:
# if ((maxX-minX) == (maxY-minY)):
total = (maxX-minX)*n + 4
else:
length = (maxX-minX) + (maxY-minY)
total = length * int(n/2) + 4
print(total)
if __name__ == '__main__':
O3()
| 75
| 748
| 0
|
187635294
|
n=int(input())
mh=sh=-10**7
ml=sl=10**7
for i in range(n):
a,b=map(int,input().split())
m=a+b
s=a-b
mh=max(mh,m)
ml=min(ml,m)
sh=max(sh,s)
sl=min(sl,s)
print(mh-ml+sh-sl+4)
|
Codeforces Beta Round 47
|
CF
| 2,010
| 2
| 256
|
Happy Farm 5
|
The Happy Farm 5 creators decided to invent the mechanism of cow grazing. The cows in the game are very slow and they move very slowly, it can even be considered that they stand still. However, carnivores should always be chased off them.
For that a young player Vasya decided to make the shepherd run round the cows along one and the same closed path. It is very important that the cows stayed strictly inside the area limited by the path, as otherwise some cows will sooner or later be eaten. To be absolutely sure in the cows' safety, Vasya wants the path completion time to be minimum.
The new game is launched for different devices, including mobile phones. That's why the developers decided to quit using the arithmetics with the floating decimal point and use only the arithmetics of integers. The cows and the shepherd in the game are represented as points on the plane with integer coordinates. The playing time is modeled by the turns. During every turn the shepherd can either stay where he stands or step in one of eight directions: horizontally, vertically, or diagonally. As the coordinates should always remain integer, then the length of a horizontal and vertical step is equal to 1, and the length of a diagonal step is equal to $$\sqrt{2}$$. The cows do not move. You have to minimize the number of moves the shepherd needs to run round the whole herd.
|
The first line contains an integer N which represents the number of cows in the herd (1 ≤ N ≤ 105). Each of the next N lines contains two integers Xi and Yi which represent the coordinates of one cow of (|Xi|, |Yi| ≤ 106). Several cows can stand on one point.
|
Print the single number — the minimum number of moves in the sought path.
| null |
Picture for the example test: The coordinate grid is painted grey, the coordinates axes are painted black, the cows are painted red and the sought route is painted green.
|
[{"input": "4\n1 1\n5 1\n5 3\n1 3", "output": "16"}]
| 2,000
|
["geometry"]
| 75
|
[{"input": "4\r\n1 1\r\n5 1\r\n5 3\r\n1 3\r\n", "output": "16\r\n"}, {"input": "3\r\n0 0\r\n5 0\r\n0 5\r\n", "output": "19\r\n"}, {"input": "5\r\n0 0\r\n7 7\r\n7 5\r\n5 7\r\n1 1\r\n", "output": "22\r\n"}, {"input": "5\r\n1 0\r\n-1 0\r\n1 0\r\n-1 0\r\n0 0\r\n", "output": "8\r\n"}, {"input": "9\r\n1 0\r\n-1 0\r\n1 0\r\n-1 0\r\n0 0\r\n1 1\r\n-1 -1\r\n1 -1\r\n-1 1\r\n", "output": "12\r\n"}, {"input": "5\r\n-10 10\r\n-10 -10\r\n10 -10\r\n9 8\r\n1 2\r\n", "output": "81\r\n"}, {"input": "1\r\n7 -10\r\n", "output": "4\r\n"}, {"input": "3\r\n7 -10\r\n7 -10\r\n7 -10\r\n", "output": "4\r\n"}, {"input": "2\r\n-10 0\r\n-10 7\r\n", "output": "18\r\n"}, {"input": "5\r\n-10 0\r\n-10 7\r\n-10 0\r\n-10 7\r\n-10 0\r\n", "output": "18\r\n"}, {"input": "11\r\n0 0\r\n3 0\r\n1 0\r\n2 0\r\n1 0\r\n5 0\r\n3 0\r\n10 0\r\n6 0\r\n-1 0\r\n2 0\r\n", "output": "26\r\n"}, {"input": "10\r\n1 0\r\n1 -3\r\n1 5\r\n1 -2\r\n1 5\r\n1 -2\r\n1 -2\r\n1 -2\r\n1 -2\r\n1 -2\r\n", "output": "20\r\n"}, {"input": "6\r\n1 0\r\n2 1\r\n10 9\r\n-1 -2\r\n2 1\r\n-1 -2\r\n", "output": "26\r\n"}, {"input": "6\r\n5 0\r\n0 5\r\n10 -5\r\n-5 10\r\n2 3\r\n3 2\r\n", "output": "34\r\n"}, {"input": "3\r\n0 0\r\n2 0\r\n0 2\r\n", "output": "10\r\n"}, {"input": "3\r\n0 0\r\n1 0\r\n0 2\r\n", "output": "9\r\n"}, {"input": "3\r\n0 0\r\n2 0\r\n0 1\r\n", "output": "9\r\n"}, {"input": "3\r\n0 0\r\n2 1\r\n0 2\r\n", "output": "10\r\n"}, {"input": "3\r\n0 0\r\n2 0\r\n1 2\r\n", "output": "10\r\n"}, {"input": "3\r\n0 0\r\n2 1\r\n1 2\r\n", "output": "9\r\n"}, {"input": "3\r\n0 0\r\n3 1\r\n2 3\r\n", "output": "12\r\n"}, {"input": "3\r\n10 0\r\n0 20\r\n33 30\r\n", "output": "87\r\n"}, {"input": "4\r\n0 2\r\n2 0\r\n3 5\r\n5 3\r\n", "output": "14\r\n"}, {"input": "5\r\n0 2\r\n2 0\r\n3 5\r\n5 3\r\n6 3\r\n", "output": "16\r\n"}, {"input": "8\r\n0 2\r\n3 0\r\n5 3\r\n2 5\r\n2 2\r\n3 3\r\n2 2\r\n2 2\r\n", "output": "16\r\n"}, {"input": "4\r\n0 3\r\n3 0\r\n5 3\r\n2 5\r\n", "output": "15\r\n"}, {"input": "10\r\n2 0\r\n1 1\r\n0 2\r\n4 0\r\n0 4\r\n3 8\r\n5 8\r\n7 8\r\n8 7\r\n8 4\r\n", "output": "26\r\n"}, {"input": "4\r\n-1000000 -1000000\r\n1000000 1000000\r\n-1000000 1000000\r\n1000000 -1000000\r\n", "output": "8000004\r\n"}, {"input": "4\r\n-1000000 -999993\r\n999991 999997\r\n-999998 999996\r\n999994 -1000000\r\n", "output": "7999973\r\n"}, {"input": "11\r\n-1000000 -999993\r\n999991 999997\r\n1 3\r\n1 3\r\n0 0\r\n-999998 999996\r\n-1 3\r\n4 5\r\n-999998 999996\r\n6 7\r\n999994 -1000000\r\n", "output": "7999973\r\n"}, {"input": "2\r\n-1000000 -1000000\r\n999999 999999\r\n", "output": "4000002\r\n"}, {"input": "2\r\n-1000000 -1000000\r\n999999 1000000\r\n", "output": "4000004\r\n"}, {"input": "3\r\n1 -1\r\n-1 -1\r\n-1 1\r\n", "output": "10\r\n"}, {"input": "3\r\n2 2\r\n1 -2\r\n0 2\r\n", "output": "14\r\n"}, {"input": "3\r\n1 -1\r\n1 2\r\n-2 -2\r\n", "output": "14\r\n"}, {"input": "3\r\n0 2\r\n-1 1\r\n2 -1\r\n", "output": "11\r\n"}, {"input": "3\r\n2 1\r\n1 2\r\n-2 0\r\n", "output": "12\r\n"}, {"input": "3\r\n0 2\r\n2 1\r\n2 1\r\n", "output": "8\r\n"}, {"input": "3\r\n1 1\r\n0 2\r\n0 -1\r\n", "output": "10\r\n"}, {"input": "4\r\n-1 -2\r\n2 -1\r\n-1 -1\r\n-1 0\r\n", "output": "12\r\n"}, {"input": "4\r\n2 1\r\n1 1\r\n-1 2\r\n-2 -2\r\n", "output": "15\r\n"}, {"input": "4\r\n1 2\r\n0 1\r\n0 3\r\n-1 -3\r\n", "output": "16\r\n"}, {"input": "4\r\n1 -1\r\n0 -2\r\n0 1\r\n-1 0\r\n", "output": "10\r\n"}, {"input": "5\r\n-2 2\r\n2 -3\r\n2 3\r\n2 1\r\n2 -3\r\n", "output": "19\r\n"}, {"input": "5\r\n-3 -3\r\n-3 3\r\n1 0\r\n-2 2\r\n0 -1\r\n", "output": "18\r\n"}, {"input": "6\r\n2 -1\r\n-2 1\r\n-1 -1\r\n0 2\r\n2 -2\r\n-2 0\r\n", "output": "15\r\n"}, {"input": "8\r\n3 -1\r\n1 -1\r\n0 2\r\n-2 -2\r\n1 -2\r\n1 -2\r\n3 2\r\n3 2\r\n", "output": "19\r\n"}, {"input": "20\r\n9 0\r\n11 6\r\n13 4\r\n7 3\r\n9 0\r\n10 4\r\n11 4\r\n11 2\r\n9 0\r\n9 1\r\n9 0\r\n10 4\r\n13 4\r\n10 6\r\n10 6\r\n9 0\r\n9 1\r\n10 2\r\n10 4\r\n12 3\r\n", "output": "17\r\n"}, {"input": "30\r\n-4 0\r\n-4 0\r\n-5 2\r\n-1 3\r\n-3 3\r\n-3 4\r\n-1 2\r\n-3 3\r\n-2 4\r\n-4 0\r\n-1 -1\r\n-2 2\r\n-2 2\r\n-5 1\r\n-1 3\r\n-1 -1\r\n-5 1\r\n-1 -1\r\n-3 1\r\n-3 0\r\n-5 2\r\n-2 -1\r\n-4 0\r\n-1 4\r\n-5 2\r\n-1 -1\r\n-1 3\r\n-4 1\r\n-3 4\r\n-3 -1\r\n", "output": "18\r\n"}, {"input": "40\r\n6 -14\r\n12 -13\r\n13 -16\r\n12 -13\r\n12 -13\r\n7 -13\r\n13 -16\r\n11 -15\r\n6 -14\r\n5 -14\r\n13 -14\r\n8 -17\r\n9 -13\r\n10 -10\r\n6 -13\r\n6 -15\r\n7 -12\r\n10 -11\r\n14 -14\r\n12 -12\r\n6 -14\r\n6 -14\r\n9 -15\r\n12 -13\r\n5 -14\r\n13 -16\r\n7 -12\r\n11 -17\r\n12 -13\r\n14 -14\r\n10 -11\r\n10 -18\r\n6 -15\r\n9 -14\r\n10 -14\r\n15 -15\r\n8 -13\r\n13 -15\r\n8 -17\r\n13 -13\r\n", "output": "24\r\n"}, {"input": "50\r\n-10 4\r\n5 4\r\n-4 4\r\n0 4\r\n-11 2\r\n-10 6\r\n-3 2\r\n-2 -3\r\n-2 -5\r\n5 -4\r\n0 -3\r\n5 -4\r\n-13 3\r\n-9 3\r\n1 -4\r\n-1 3\r\n0 5\r\n-7 2\r\n-9 5\r\n0 4\r\n4 5\r\n-2 -5\r\n4 4\r\n-9 1\r\n-9 6\r\n3 -2\r\n2 -4\r\n-10 6\r\n-2 -3\r\n-7 2\r\n2 5\r\n-2 6\r\n-2 6\r\n2 5\r\n2 -4\r\n5 2\r\n-5 -2\r\n4 4\r\n2 -4\r\n2 -4\r\n5 3\r\n5 1\r\n3 -1\r\n-10 4\r\n4 -5\r\n-4 2\r\n-5 -2\r\n-2 2\r\n-1 4\r\n3 5\r\n", "output": "48\r\n"}, {"input": "60\r\n22 -7\r\n25 -2\r\n21 5\r\n21 2\r\n26 1\r\n19 1\r\n21 0\r\n21 2\r\n29 -5\r\n18 -3\r\n27 -3\r\n29 -5\r\n23 -4\r\n29 -5\r\n22 0\r\n19 -1\r\n23 0\r\n21 -5\r\n24 -1\r\n21 -4\r\n19 1\r\n24 3\r\n19 3\r\n25 -7\r\n24 -3\r\n30 -5\r\n24 -3\r\n27 -7\r\n20 -5\r\n16 -1\r\n25 -5\r\n19 -3\r\n18 -1\r\n17 -1\r\n19 1\r\n18 2\r\n28 -5\r\n24 0\r\n25 2\r\n22 1\r\n29 -5\r\n22 -1\r\n19 1\r\n28 -2\r\n29 -2\r\n22 -4\r\n21 0\r\n22 -4\r\n21 -5\r\n19 3\r\n22 -1\r\n21 5\r\n27 -4\r\n30 -3\r\n30 -5\r\n22 3\r\n19 2\r\n26 -1\r\n23 3\r\n22 -4\r\n", "output": "35\r\n"}, {"input": "20\r\n-118 -4\r\n-114 -8\r\n-86 40\r\n-77 38\r\n-128 24\r\n-114 -8\r\n-107 24\r\n-63 15\r\n-114 -8\r\n-138 34\r\n-136 53\r\n-116 37\r\n-62 14\r\n-116 37\r\n-112 10\r\n-146 25\r\n-83 42\r\n-62 14\r\n-107 11\r\n-138 34\r\n", "output": "200\r\n"}, {"input": "30\r\n220065 650176\r\n-85645 309146\r\n245761 474510\r\n297068 761230\r\n39280 454823\r\n65372 166746\r\n316557 488319\r\n220065 650176\r\n245761 474510\r\n65372 166746\r\n-8475 -14722\r\n327177 312018\r\n371695 397843\r\n343097 243895\r\n-113462 117273\r\n-8189 440841\r\n327177 312018\r\n-171241 288713\r\n-147691 268033\r\n265028 425605\r\n208512 456240\r\n97333 6791\r\n-109657 297156\r\n-109657 297156\r\n-176591 87288\r\n-120815 31512\r\n120019 546293\r\n3773 19061\r\n161901 442125\r\n-50681 429871\r\n", "output": "1727359\r\n"}, {"input": "50\r\n139 201\r\n115 37\r\n206 8\r\n115 37\r\n167 201\r\n189 1\r\n167 201\r\n141 201\r\n141 201\r\n115 37\r\n78 81\r\n167 201\r\n126 201\r\n78 91\r\n103 186\r\n208 169\r\n85 67\r\n208 153\r\n78 97\r\n208 89\r\n126 26\r\n141 201\r\n208 42\r\n166 41\r\n78 124\r\n156 1\r\n181 201\r\n78 129\r\n208 169\r\n208 52\r\n78 85\r\n128 201\r\n167 201\r\n208 23\r\n100 52\r\n148 4\r\n116 199\r\n208 122\r\n173 201\r\n167 201\r\n153 1\r\n176 1\r\n170 194\r\n78 132\r\n206 8\r\n208 23\r\n208 67\r\n208 116\r\n78 161\r\n142 160\r\n", "output": "515\r\n"}, {"input": "60\r\n-20 179\r\n-68 0\r\n-110 68\r\n-22 177\r\n47 140\r\n-49 -4\r\n-106 38\r\n-23 22\r\n20 193\r\n47 173\r\n-23 22\r\n-100 32\r\n-97 29\r\n47 124\r\n-49 -4\r\n20 193\r\n-20 179\r\n-50 149\r\n-59 -7\r\n4 193\r\n-23 22\r\n-97 29\r\n-23 22\r\n-66 133\r\n47 167\r\n-61 138\r\n-49 -4\r\n-91 108\r\n-110 84\r\n47 166\r\n-110 77\r\n-99 100\r\n-23 22\r\n-59 -7\r\n47 128\r\n46 91\r\n9 193\r\n-110 86\r\n-49 -4\r\n8 193\r\n2 47\r\n-35 164\r\n-100 32\r\n47 114\r\n-56 -7\r\n47 148\r\n14 193\r\n20 65\r\n47 171\r\n47 171\r\n-110 53\r\n47 93\r\n20 65\r\n-35 164\r\n-50 149\r\n-25 174\r\n9 193\r\n47 150\r\n-49 -4\r\n-110 44\r\n", "output": "446\r\n"}, {"input": "54\r\n-2 0\r\n-2 0\r\n3 -3\r\n-3 -6\r\n-5 -5\r\n-1 -4\r\n2 5\r\n-4 2\r\n2 5\r\n-5 5\r\n5 3\r\n3 1\r\n-2 1\r\n4 4\r\n-4 4\r\n-3 2\r\n-5 -4\r\n2 4\r\n4 2\r\n-2 1\r\n4 -1\r\n5 4\r\n-2 1\r\n-5 5\r\n-3 -1\r\n-4 -1\r\n1 -4\r\n-2 -2\r\n3 -3\r\n2 6\r\n-5 3\r\n-1 4\r\n5 -1\r\n2 -4\r\n2 -2\r\n1 4\r\n-5 5\r\n0 4\r\n-5 3\r\n-4 -2\r\n3 -2\r\n3 -1\r\n-4 -1\r\n5 5\r\n4 5\r\n3 -3\r\n1 2\r\n2 5\r\n-2 -4\r\n-5 5\r\n-4 1\r\n2 4\r\n-3 -4\r\n1 6\r\n", "output": "40\r\n"}, {"input": "35\r\n3 -3\r\n1 4\r\n-3 -3\r\n2 -2\r\n0 -1\r\n-1 -1\r\n2 5\r\n0 -1\r\n1 3\r\n-3 -5\r\n1 -1\r\n3 5\r\n1 -3\r\n3 -5\r\n-1 3\r\n2 -3\r\n1 -1\r\n-3 5\r\n-3 -2\r\n2 -2\r\n1 -6\r\n-3 5\r\n-1 1\r\n1 -3\r\n1 4\r\n3 4\r\n-1 -1\r\n0 -5\r\n3 -2\r\n-3 -4\r\n3 6\r\n1 4\r\n-2 1\r\n2 -3\r\n2 -6\r\n", "output": "37\r\n"}, {"input": "43\r\n-1 2\r\n2 -3\r\n-2 0\r\n2 -1\r\n0 1\r\n0 0\r\n1 -3\r\n0 -2\r\n0 2\r\n2 0\r\n-1 2\r\n2 -3\r\n1 2\r\n1 0\r\n1 -3\r\n-2 -3\r\n2 -3\r\n-2 0\r\n0 -3\r\n1 -2\r\n-2 -3\r\n1 1\r\n2 -3\r\n2 1\r\n-1 -3\r\n1 2\r\n1 -3\r\n-1 2\r\n0 1\r\n0 -1\r\n0 -3\r\n2 1\r\n1 0\r\n-2 -3\r\n0 -2\r\n1 1\r\n-2 2\r\n-2 -3\r\n-2 3\r\n-1 0\r\n1 -1\r\n2 2\r\n-1 -2\r\n", "output": "23\r\n"}, {"input": "61\r\n0 -5\r\n0 3\r\n0 -1\r\n0 -4\r\n0 6\r\n0 -1\r\n0 -3\r\n0 6\r\n0 5\r\n0 -5\r\n0 1\r\n0 2\r\n0 5\r\n0 -3\r\n0 1\r\n0 -6\r\n0 -3\r\n0 3\r\n0 -3\r\n0 -5\r\n0 2\r\n0 1\r\n0 5\r\n0 3\r\n0 -2\r\n0 -2\r\n0 -3\r\n0 -6\r\n0 -4\r\n0 -2\r\n0 0\r\n0 -1\r\n0 -5\r\n0 -6\r\n0 6\r\n0 0\r\n0 -5\r\n0 1\r\n0 2\r\n0 -2\r\n0 -5\r\n0 6\r\n0 -3\r\n0 4\r\n0 5\r\n0 2\r\n0 -6\r\n0 -3\r\n0 2\r\n0 1\r\n0 -2\r\n0 -4\r\n0 -2\r\n0 4\r\n0 -1\r\n0 6\r\n0 0\r\n0 -1\r\n0 -3\r\n0 -4\r\n0 -3\r\n", "output": "28\r\n"}, {"input": "57\r\n-8 0\r\n-2 0\r\n-2 0\r\n-7 0\r\n-8 0\r\n0 0\r\n8 0\r\n8 0\r\n3 0\r\n-2 0\r\n-3 0\r\n6 0\r\n-7 0\r\n-9 0\r\n-2 0\r\n-4 0\r\n-8 0\r\n0 0\r\n6 0\r\n7 0\r\n3 0\r\n0 0\r\n9 0\r\n0 0\r\n-9 0\r\n3 0\r\n8 0\r\n0 0\r\n6 0\r\n-4 0\r\n8 0\r\n10 0\r\n-7 0\r\n3 0\r\n-1 0\r\n8 0\r\n3 0\r\n1 0\r\n6 0\r\n3 0\r\n-8 0\r\n4 0\r\n-8 0\r\n-2 0\r\n1 0\r\n6 0\r\n7 0\r\n-6 0\r\n-2 0\r\n-2 0\r\n-8 0\r\n-8 0\r\n2 0\r\n0 0\r\n10 0\r\n4 0\r\n-8 0\r\n", "output": "42\r\n"}]
| false
|
stdio
| null | true
|
698/B
|
698
|
B
|
PyPy 3
|
TESTS
| 17
| 639
| 57,446,400
|
128894920
|
from collections import defaultdict,deque
n=int(input())
parent=list(map(int,input().split()))
graph=defaultdict(list)
for i in range(n):
graph[i+1].append(parent[i])
graph[parent[i]].append(i+1)
vis=[False]*(n+1)
a=parent.copy()
head=-1
for i in range(n):
if parent[i]==i+1:
head=i+1
continue
c=i+1
p=a[i]
if a[p-1]==c:
a[p-1]=p
head=p
for i in range(1,n+1):
if vis[i]==False:
q=deque()
q.append([i,-1])
vis[i]=True
last=-1
while q:
s=q.popleft()
node=s[0]
pre=s[1]
for neighbor in graph[node]:
if pre!=neighbor:
if vis[neighbor]==False:
vis[neighbor]=True
q.append([neighbor,node])
else:
if a[neighbor-1]==node:
last=neighbor
else:
last=node
if head==-1:
head=last
a[last-1]=head
else:
a[last-1]=head
change=0
for i in range(n):
if parent[i]!=a[i]:
change+=1
print(change)
print(*a)
| 101
| 420
| 19,660,800
|
19246143
|
input()
A = list(map(int, input().split(' ')))
root=-1
for i,a in enumerate(A) :
if i == a-1 :
root = i
break
v = [False]*len(A)
if root>-1 :
v[root]=True
changed = 0
for i,a in enumerate(A) :
if v[i] :
continue
v[i]= True
l=[i]
a-=1
while not v[a] :
l.append(a)
v[a]=True
a=A[a]-1
if a in l: #new cycle
if root==-1:
A[a]=a+1
root=a
changed+=1
else :
A[a]=root+1
changed+=1
print(changed)
print(' '.join(map(str,A)))
|
Codeforces Round 363 (Div. 1)
|
CF
| 2,016
| 2
| 256
|
Fix a Tree
|
A tree is an undirected connected graph without cycles.
Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).
For this rooted tree the array p is [2, 3, 3, 2].
Given a sequence p1, p2, ..., pn, one is able to restore a tree:
1. There must be exactly one index r that pr = r. A vertex r is a root of the tree.
2. For all other n - 1 vertices i, there is an edge between vertex i and vertex pi.
A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid.
You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.
|
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
|
In the first line print the minimum number of elements to change, in order to get a valid sequence.
In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
| null |
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because p4 = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red.
In the second sample, the given sequence is already valid.
|
[{"input": "4\n2 3 3 4", "output": "1\n2 3 4 4"}, {"input": "5\n3 2 2 5 3", "output": "0\n3 2 2 5 3"}, {"input": "8\n2 3 5 4 1 6 6 7", "output": "2\n2 3 7 8 1 6 6 7"}]
| 1,700
|
["constructive algorithms", "dfs and similar", "dsu", "graphs", "trees"]
| 101
|
[{"input": "4\r\n2 3 3 4\r\n", "output": "1\r\n2 3 4 4 \r\n"}, {"input": "5\r\n3 2 2 5 3\r\n", "output": "0\r\n3 2 2 5 3 \r\n"}, {"input": "8\r\n2 3 5 4 1 6 6 7\r\n", "output": "2\r\n2 3 7 8 1 6 6 7\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1\r\n2 2 \r\n"}, {"input": "7\r\n4 3 2 6 3 5 2\r\n", "output": "1\r\n4 3 3 6 3 5 2 \r\n"}, {"input": "6\r\n6 2 6 2 4 2\r\n", "output": "0\r\n6 2 6 2 4 2 \r\n"}, {"input": "7\r\n1 6 4 4 5 6 7\r\n", "output": "4\r\n7 6 4 7 7 7 7 \r\n"}, {"input": "7\r\n7 5 3 1 2 1 5\r\n", "output": "1\r\n7 5 3 1 3 1 5 \r\n"}, {"input": "7\r\n1 2 3 4 5 6 7\r\n", "output": "6\r\n7 7 7 7 7 7 7 \r\n"}, {"input": "18\r\n2 3 4 5 2 7 8 9 10 7 11 12 14 15 13 17 18 18\r\n", "output": "5\r\n2 18 4 5 2 7 18 9 10 7 18 18 18 15 13 17 18 18 \r\n"}, {"input": "8\r\n2 1 2 2 6 5 6 6\r\n", "output": "2\r\n1 1 2 2 1 5 6 6 \r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "1\r\n1 1 1 \r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
assert len(a) == n
# Read submission's output
with open(submission_output_path) as f:
k_sub_line = f.readline().strip()
p_line = f.readline().strip()
k_sub = int(k_sub_line)
p = list(map(int, p_line.split()))
assert len(p) == n
# Compute K_min
cycles = compute_cycles(a)
c = len(cycles)
has_self_loop = any(len(cycle) == 1 for cycle in cycles)
if has_self_loop:
k_min = c - 1
else:
k_min = c
# Check submission's K_sub equals k_min
if k_sub != k_min:
print(0)
return
# Check validity of p
if not is_valid(p):
print(0)
return
# Check number of changes between a and p is K_sub
changes = sum(1 for i in range(n) if a[i] != p[i])
if changes != k_sub:
print(0)
return
print(1)
def compute_cycles(a):
n = len(a)
visited = [False] * (n + 1)
cycles = []
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:]
cycles.append(cycle)
break
visited[current] = True
path.append(current)
current = a[current-1]
return cycles
def is_valid(p):
n = len(p)
# Check exactly one root
roots = [i+1 for i in range(n) if p[i] == i+1]
if len(roots) != 1:
return False
r = roots[0]
# Build reverse adjacency list
reverse_adj = [[] for _ in range(n+1)]
for j in range(n):
i = j + 1
parent = p[j]
reverse_adj[parent].append(i)
# BFS to count reachable nodes from r
visited = [False] * (n + 1)
queue = deque([r])
visited[r] = True
count = 1
while queue:
u = queue.popleft()
for v in reverse_adj[u]:
if not visited[v]:
visited[v] = True
count += 1
queue.append(v)
return count == n
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_output_path = sys.argv[3]
main(input_path, output_path, submission_output_path)
| true
|
487/B
|
487
|
B
|
PyPy 3-64
|
TESTS
| 3
| 62
| 0
|
230039021
|
import math
import sys
from collections import deque
# region fastio
input = lambda: sys.stdin.readline().rstrip()
sint = lambda: int(input())
mint = lambda: map(int, input().split())
ints = lambda: list(map(int, input().split()))
# endregion fastio
# MOD = 998_244_353
# MOD = 10 ** 9 + 7
# DIR4 = ((-1, 0), (0, 1), (1, 0), (0, -1)) #URDL
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
def solve() -> None:
n, s, l = mint()
nums = ints()
mx = deque()
mn = deque()
dp = [math.inf] * (n + 1)
dp[0] = 0
j = 0
for i, x in enumerate(nums):
while mn and nums[mn[-1]] > x:
mn.pop()
mn.append(i)
while mx and nums[mx[-1]] < x:
mx.pop()
mx.append(i)
while nums[mx[0]] - nums[mn[0]] > s:
if mx[0] < mn[0]:
mx.popleft()
elif mx[0] > mn[0]:
mn.popleft()
while j < min(mn[0], mx[0]):
j += 1
if i - j + 1 >= l:
dp[i + 1] = dp[j] + 1
# print(dp)
print(-1 if dp[n] == math.inf else dp[n])
solve()
| 35
| 217
| 17,817,600
|
229659935
|
from collections import deque
n, s, l = map(int, input().split())
a = list(map(int, input().split()))
dp = [-1] * (n + 1)
g, h = 0, -1
dp[0] = 0
que, queMax, queMin = deque(), deque(), deque()
for i in range(n):
while queMax and a[queMax[0]] - a[i] > s:
g = max(g, queMax[0] + 1)
queMax.popleft()
while queMax and a[queMax[-1]] <= a[i]:
queMax.pop()
queMax.append(i)
while queMin and a[i] - a[queMin[0]] > s:
g = max(g, queMin[0] + 1)
queMin.popleft()
while queMin and a[queMin[-1]] >= a[i]:
queMin.pop()
queMin.append(i)
if i - l + 1 >= 0 and dp[i - l + 1] != -1:
while que and dp[que[-1]] >= dp[i - l + 1]:
que.pop()
que.append(i - l + 1)
while que and que[0] < g:
que.popleft()
if not que:
dp[i + 1] = -1
else:
dp[i + 1] = dp[que[0]] + 1
print(dp[n])# 1698221621.9900117
|
Codeforces Round 278 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Strip
|
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least l numbers.
- The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
|
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).
The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
|
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
| null |
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
|
[{"input": "7 2 2\n1 3 1 2 4 1 2", "output": "3"}, {"input": "7 2 2\n1 100 1 100 1 100 1", "output": "-1"}]
| 2,000
|
["binary search", "data structures", "dp", "two pointers"]
| 35
|
[{"input": "7 2 2\r\n1 3 1 2 4 1 2\r\n", "output": "3\r\n"}, {"input": "7 2 2\r\n1 100 1 100 1 100 1\r\n", "output": "-1\r\n"}, {"input": "1 0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "6 565 2\r\n31 76 162 -182 -251 214\r\n", "output": "1\r\n"}, {"input": "1 0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "1 0 1\r\n-1000000000\r\n", "output": "1\r\n"}, {"input": "1 100 2\r\n42\r\n", "output": "-1\r\n"}, {"input": "2 1000000000 1\r\n-1000000000 1000000000\r\n", "output": "2\r\n"}, {"input": "2 1000000000 2\r\n-1000000000 1000000000\r\n", "output": "-1\r\n"}, {"input": "10 3 3\r\n1 1 1 1 1 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "10 3 3\r\n1 1 1 2 2 5 6 7 8 9\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
56/B
|
56
|
B
|
PyPy 3
|
TESTS
| 7
| 310
| 0
|
83609214
|
n=int(input())
l=list(map(int,input().split()))
l1=list(range(1,n+1))
i=0
L=0
R=0
c=0
while i<n:
if l[i]!=l1[i]:
if c==0:
L=i
c+=1
else:
R=i
i+=1
if L!=0 and R!=0:
if l[L:R+1]!=l1[L:R+1][::-1]:
L=0
R=0
print(0,0)
else:
print(L+1,R+1)
else:
print(0,0)
| 33
| 124
| 0
|
14828936
|
n, a = int(input()), [0] + list(map(int, input().split()))
l, r = 1, n
while l <= n and a[l] == l:
l += 1
while r >= 1 and a[r] == r:
r -= 1
if l < n and a[l:r + 1] == list(range(r, l - 1, -1)):
print(l, r)
else:
print('0 0')
|
Codeforces Beta Round 52 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Spoilt Permutation
|
Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.
|
The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.
|
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.
| null | null |
[{"input": "8\n1 6 5 4 3 2 7 8", "output": "2 6"}, {"input": "4\n2 3 4 1", "output": "0 0"}, {"input": "4\n1 2 3 4", "output": "0 0"}]
| 1,300
|
["implementation"]
| 33
|
[{"input": "8\r\n1 6 5 4 3 2 7 8\r\n", "output": "2 6\r\n"}, {"input": "4\r\n2 3 4 1\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 3 4\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 2 4 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "8\r\n1 3 4 2 6 5 7 8\r\n", "output": "0 0\r\n"}, {"input": "1\r\n1\r\n", "output": "0 0\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0 0\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1 2\r\n"}, {"input": "35\r\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25\r\n", "output": "0 0\r\n"}, {"input": "4\r\n1 2 4 3\r\n", "output": "3 4\r\n"}, {"input": "4\r\n1 4 3 2\r\n", "output": "2 4\r\n"}]
| false
|
stdio
| null | true
|
140/D
|
140
|
D
|
PyPy 3-64
|
TESTS
| 3
| 92
| 0
|
207746931
|
def read_int():
return int(input())
def read_array():
return list(map(int, input().split()))
n = read_int()
a = read_array()
a.sort(reverse=True)
s = 0
k = 0
while a and s + a[-1] <= 710:
s += a.pop()
k += 1
print(k, max(0, s - 350))
| 32
| 92
| 0
|
8107045
|
__author__ = 'Alex'
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
s = 10
i = 0
while i < n and a[i] + s <= 360:
s += a[i]
i += 1
ans = 0
while i < n and a[i] + s <= 720:
s += a[i]
ans += s - 360
i += 1
print(i, ans)
|
Codeforces Round 100
|
CF
| 2,012
| 2
| 256
|
New Year Contest
|
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
|
The first line contains an integer n (1 ≤ n ≤ 100) — the number of the problems. The next line contains n space-separated integers ai (1 ≤ ai ≤ 720) — each number shows how much time in minutes Gennady will spend writing a solution to the problem.
|
Print two integers — the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
| null |
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc
|
[{"input": "3\n30 330 720", "output": "2 10"}]
| 1,800
|
["greedy", "sortings"]
| 32
|
[{"input": "3\r\n30 330 720\r\n", "output": "2 10\r\n"}, {"input": "1\r\n720\r\n", "output": "0 0\r\n"}, {"input": "5\r\n100 200 300 400 500\r\n", "output": "3 250\r\n"}, {"input": "7\r\n120 110 100 110 120 120 50\r\n", "output": "6 420\r\n"}, {"input": "3\r\n350 340 360\r\n", "output": "2 340\r\n"}, {"input": "8\r\n150 100 50 70 70 80 90 100\r\n", "output": "8 690\r\n"}, {"input": "100\r\n6 4 5 6 6 4 3 2 7 1 23 1 7 3 1 1 13 3 2 9 13 8 11 6 2 5 3 3 1 3 6 3 26 11 16 21 7 21 15 1 10 3 2 7 4 11 2 20 2 9 15 10 16 17 3 7 6 4 5 4 1 2 1 1 13 7 6 4 6 5 22 5 14 12 2 30 2 30 5 14 3 4 9 2 9 3 1 3 9 4 3 6 3 15 21 23 3 6 14 22\r\n", "output": "97 3395\r\n"}, {"input": "100\r\n6 2 1 1 6 3 1 1 3 2 2 2 3 2 3 1 6 1 6 1 2 1 2 2 4 2 2 1 1 3 2 6 1 1 8 4 2 1 3 1 2 6 7 2 1 1 1 2 1 1 1 2 4 1 3 1 2 3 1 1 1 1 2 3 1 1 3 3 1 1 1 5 2 1 1 3 2 1 1 1 6 3 2 8 9 3 1 13 1 1 1 1 1 1 1 4 2 3 8 3\r\n", "output": "100 0\r\n"}, {"input": "100\r\n5 20 2 8 1 1 1 1 4 2 1 1 1 7 8 4 2 2 3 2 1 8 3 3 3 2 8 6 1 11 14 4 18 3 2 7 3 1 1 4 1 4 1 13 1 8 1 1 5 7 3 1 2 1 3 3 3 16 6 7 4 2 1 7 3 2 4 1 2 6 1 4 4 3 1 6 2 4 8 3 1 3 4 4 2 10 1 1 8 2 1 2 1 1 3 2 2 1 2 2\r\n", "output": "100 54\r\n"}, {"input": "100\r\n11 3 1 2 2 13 19 1 1 6 17 1 13 5 6 1 8 6 2 2 5 2 1 8 5 7 24 3 1 7 13 14 1 3 15 2 8 11 1 4 7 2 13 3 17 4 1 5 2 4 3 1 4 1 2 6 2 4 3 2 9 3 1 10 1 3 1 3 2 2 10 7 2 1 5 2 1 9 4 7 11 5 2 8 5 8 1 2 1 5 2 1 18 4 4 1 1 1 6 3\r\n", "output": "100 808\r\n"}, {"input": "10\r\n156 92 20 51 43 37 53 76 61 121\r\n", "output": "10 647\r\n"}, {"input": "25\r\n77 2 5 13 26 127 6 32 6 9 26 34 3 31 13 55 4 2 7 39 12 27 12 43 99\r\n", "output": "25 786\r\n"}, {"input": "50\r\n10 10 1 22 4 1 5 1 13 12 14 12 36 30 9 20 5 13 1 4 2 36 21 3 10 5 25 12 10 6 22 5 21 17 1 3 50 44 3 11 9 7 3 59 1 24 34 7 19 17\r\n", "output": "50 1665\r\n"}, {"input": "93\r\n6 30 24 3 4 1 2 10 10 11 7 8 2 11 19 3 1 13 12 1 3 4 9 5 8 1 1 2 3 11 7 1 1 12 3 2 1 7 8 3 11 8 11 14 6 1 4 8 5 5 26 3 1 7 4 1 19 5 2 2 2 14 10 14 9 11 5 6 8 26 3 3 3 1 26 27 12 3 21 4 2 3 7 4 8 1 27 3 1 1 5 22 5\r\n", "output": "93 2878\r\n"}, {"input": "98\r\n7 4 20 4 2 2 1 3 7 16 3 1 20 9 15 12 4 1 3 5 11 9 3 6 4 5 3 8 4 19 11 8 3 27 8 3 29 19 2 9 1 9 6 12 9 10 6 13 5 1 5 4 3 21 2 2 2 4 10 5 1 1 2 9 4 3 6 5 9 1 16 9 1 6 8 4 2 1 4 4 9 9 8 13 23 1 3 2 15 8 2 5 11 3 8 13 2 19\r\n", "output": "98 3398\r\n"}, {"input": "100\r\n9 1 2 5 9 4 6 1 6 8 4 1 13 9 5 1 2 2 5 2 12 11 10 16 4 8 9 13 5 11 1 5 7 11 7 12 1 3 5 3 3 15 7 26 13 7 8 10 6 23 8 1 5 8 18 14 3 16 15 1 14 14 6 5 5 3 22 5 9 4 1 7 9 3 1 12 7 1 11 4 1 3 12 2 5 7 17 2 5 5 9 2 4 2 4 2 6 9 13 1\r\n", "output": "100 4002\r\n"}, {"input": "100\r\n4 5 7 13 8 3 11 4 3 8 1 4 5 11 2 5 5 20 10 1 16 24 14 14 23 20 2 2 1 12 17 11 5 6 21 1 16 25 4 3 24 17 4 9 22 6 4 40 5 8 10 20 3 13 14 10 2 21 25 3 37 4 19 3 3 6 5 12 1 15 4 2 3 40 8 1 6 20 2 3 8 26 29 2 14 12 2 2 2 1 9 5 10 10 1 4 1 19 18 2\r\n", "output": "90 3463\r\n"}, {"input": "1\r\n710\r\n", "output": "1 360\r\n"}, {"input": "100\r\n6 14 7 4 3 1 18 3 17 6 4 44 3 2 17 19 4 6 1 11 11 7 3 8 2 1 7 2 16 1 16 10 6 2 17 6 1 4 3 4 3 2 2 5 6 6 2 2 1 2 2 5 3 1 9 3 6 1 20 12 1 4 4 1 8 19 14 8 1 2 26 5 9 2 4 4 7 6 2 2 10 1 15 2 1 12 6 7 5 26 29 16 6 8 1 11 1 8 1 5\r\n", "output": "100 2907\r\n"}, {"input": "100\r\n1 6 17 3 4 1 2 10 1 3 18 10 5 4 6 17 8 1 11 11 2 3 14 1 2 1 15 14 6 4 5 1 31 1 3 13 4 2 22 4 3 7 7 2 2 5 1 5 11 2 6 9 2 12 17 1 3 2 3 7 8 4 7 8 4 2 5 3 6 12 20 2 28 2 22 11 1 7 2 11 11 2 1 5 7 1 8 2 7 6 7 7 3 9 8 5 24 22 4 12\r\n", "output": "100 3245\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "5 0\r\n"}, {"input": "1\r\n5\r\n", "output": "1 0\r\n"}, {"input": "1\r\n711\r\n", "output": "0 0\r\n"}, {"input": "1\r\n10\r\n", "output": "1 0\r\n"}]
| false
|
stdio
| null | true
|
14/D
|
14
|
D
|
PyPy 3-64
|
TESTS
| 9
| 124
| 512,000
|
159202991
|
def dfs(v, visited, p):
for i in roads[v]:
if p != i:
visited[i-1] = visited[v-1] + 1
dfs(i, visited, v)
n = int(input())
roads = {i+1: [] for i in range(n)}
pairs = []
longest_paths = []
for i in range(n-1):
a, b = map(int, input().split())
roads[a].append(b)
roads[b].append(a)
pairs.append([a,b])
profit = 0
for i in range(len(pairs)):
a = pairs[i][0]
b = pairs[i][1]
roads[a].remove(b)
roads[b].remove(a)
visited_a = [0]*n
visited_b = [0]*n
dfs(a, visited_a, -1)
dfs(b, visited_b, -1)
profit = max(sum(visited_a)*sum(visited_b), profit)
roads[a].append(b)
roads[b].append(a)
print(profit)
| 45
| 186
| 102,400
|
139999627
|
import sys
input = sys.stdin.readline
from collections import deque
p = print
r = range
def I(): return int(input())
def II(): return list(map(int, input().split()))
def S(): return input()[:-1]
def M(n): return [list(map(int, input().split())) for ___ in r(n)]
def pb(b): print('YES' if b else 'NO')
def INF(): return float('inf')
# -----------------------------------------------------------------------------------------------------
#
# ∧_∧
# ∧_∧ (´<_` ) Welcome to My Coding Space !
# ( ´_ゝ`) / ⌒i Free Hong Kong !
# / \ | | Free Tibet !
# / / ̄ ̄ ̄ ̄/ | |
# __(__ニつ/ _/ .| .|____
# \/____/ (u ⊃
#
# 再帰関数ですか? SYS!!!!
# BINARY SEARCH ?
# -----------------------------------------------------------------------------------------------------
n = I()
vec = []
adj = [set() for i in r(n)]
for i in r(n-1):
a, b = map(lambda x: x-1, II())
adj[a].add(b)
adj[b].add(a)
vec.append((a,b))
def dfs(i):
q = deque([i])
dis = [-1]*n
dis[i] = 0
while q:
t = q.pop()
for x in adj[t]:
if dis[x] == -1:
dis[x] = dis[t] + 1
q.append(x)
return dis.index(max(dis)), max(dis)
def diameter(i):
index = dfs(i)[0]
return dfs(index)[1]
res = -INF()
for a, b in vec:
adj[a].remove(b)
adj[b].remove(a)
res = max(res,diameter(a)*diameter(b))
adj[a].add(b)
adj[b].add(a)
p(res)
|
Codeforces Beta Round 14 (Div. 2)
|
ICPC
| 2,010
| 2
| 64
|
Two Paths
|
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.
The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).
It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.
|
The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n).
|
Output the maximum possible profit.
| null | null |
[{"input": "4\n1 2\n2 3\n3 4", "output": "1"}, {"input": "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7", "output": "0"}, {"input": "6\n1 2\n2 3\n2 4\n5 4\n6 4", "output": "4"}]
| 1,900
|
["dfs and similar", "dp", "graphs", "shortest paths", "trees", "two pointers"]
| 45
|
[{"input": "4\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n", "output": "0\r\n"}, {"input": "6\r\n1 2\r\n2 3\r\n2 4\r\n5 4\r\n6 4\r\n", "output": "4\r\n"}, {"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n3 1\r\n1 2\r\n", "output": "0\r\n"}, {"input": "3\r\n1 3\r\n2 1\r\n", "output": "0\r\n"}, {"input": "4\r\n4 2\r\n2 3\r\n2 1\r\n", "output": "0\r\n"}, {"input": "4\r\n2 3\r\n1 3\r\n2 4\r\n", "output": "1\r\n"}, {"input": "4\r\n3 2\r\n3 4\r\n1 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5\r\n5 2\r\n4 2\r\n3 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 4\r\n2 5\r\n1 5\r\n2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n5 1\r\n3 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "5\r\n5 3\r\n3 1\r\n4 1\r\n4 2\r\n", "output": "2\r\n"}, {"input": "6\r\n1 2\r\n2 5\r\n4 5\r\n4 6\r\n3 2\r\n", "output": "4\r\n"}, {"input": "7\r\n1 6\r\n4 6\r\n5 6\r\n6 7\r\n2 6\r\n3 7\r\n", "output": "2\r\n"}, {"input": "8\r\n7 2\r\n7 1\r\n6 7\r\n4 1\r\n7 3\r\n6 8\r\n2 5\r\n", "output": "4\r\n"}, {"input": "8\r\n8 6\r\n1 8\r\n7 8\r\n3 1\r\n2 6\r\n5 3\r\n8 4\r\n", "output": "6\r\n"}, {"input": "9\r\n8 4\r\n7 8\r\n6 4\r\n8 3\r\n1 4\r\n3 9\r\n5 7\r\n2 5\r\n", "output": "10\r\n"}, {"input": "9\r\n4 7\r\n5 4\r\n2 7\r\n5 6\r\n3 7\r\n7 1\r\n9 2\r\n8 3\r\n", "output": "8\r\n"}, {"input": "10\r\n7 6\r\n6 8\r\n10 7\r\n5 10\r\n5 3\r\n2 8\r\n4 5\r\n1 7\r\n4 9\r\n", "output": "12\r\n"}, {"input": "10\r\n10 7\r\n7 5\r\n10 8\r\n6 5\r\n7 2\r\n9 7\r\n1 10\r\n3 5\r\n4 10\r\n", "output": "6\r\n"}, {"input": "15\r\n15 1\r\n15 10\r\n11 1\r\n1 13\r\n10 12\r\n1 8\r\n15 9\r\n14 13\r\n10 2\r\n7 10\r\n5 15\r\n8 4\r\n11 3\r\n6 15\r\n", "output": "12\r\n"}, {"input": "15\r\n10 12\r\n12 4\r\n15 12\r\n15 6\r\n5 15\r\n10 1\r\n8 15\r\n13 12\r\n14 6\r\n8 3\r\n11 5\r\n12 7\r\n15 9\r\n2 7\r\n", "output": "16\r\n"}, {"input": "15\r\n13 14\r\n10 14\r\n5 10\r\n10 6\r\n9 10\r\n10 7\r\n15 6\r\n8 7\r\n2 6\r\n1 10\r\n3 1\r\n3 11\r\n4 3\r\n14 12\r\n", "output": "10\r\n"}, {"input": "30\r\n2 4\r\n14 2\r\n2 3\r\n23 14\r\n30 2\r\n6 14\r\n13 4\r\n24 30\r\n17 30\r\n25 2\r\n26 23\r\n28 3\r\n6 8\r\n23 29\r\n18 25\r\n10 2\r\n25 7\r\n9 26\r\n6 27\r\n13 12\r\n22 3\r\n1 28\r\n11 10\r\n25 20\r\n30 19\r\n16 14\r\n22 5\r\n21 30\r\n15 18\r\n", "output": "30\r\n"}, {"input": "5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
696/A
|
696
|
A
|
Python 3
|
TESTS
| 2
| 155
| 3,891,200
|
48644443
|
from functools import lru_cache
from collections import defaultdict
from itertools import tee
price = defaultdict(dict)
def add(start, end, w):
for x, y in get_way(start, end):
if x in price:
price[x][y] += w
else:
price[x] = defaultdict(lambda: 0)
price[x][y] = w
if y in price:
price[y][x] += w
else:
price[y] = defaultdict(lambda: 0)
price[y][x] = w
def get_price(start, end):
result = 0
for x, y in get_way(start, end):
if x not in price:
price[x] = defaultdict(lambda: 0)
result += price[x][y]
return result
@lru_cache(maxsize=1000)
def get_way(start, end):
def _get_raw_way():
nonlocal start, end
l_way, r_way = [start], [end]
while True:
l = l_way[-1] // 2
if l: l_way.append(l)
r = r_way[-1] // 2
if r: r_way.append(r)
if r_way[-1] == start:
return r_way
if set(l_way) & set(r_way):
del r_way[-1]
r_way.reverse()
return l_way + r_way
a, b = tee(_get_raw_way())
next(b, None)
return list(zip(a, b))
q = int(input())
for _ in range(q):
data = list(map(int, input().split(' ')))
if data[0] == 1:
add(data[1], data[2], data[3])
else:
print(get_price(data[1], data[2]))
| 49
| 155
| 8,396,800
|
19134746
|
from collections import defaultdict
def log2(i):
c = 0
while i > 0:
i >>= 1
c += 1
return c
q = int(input())
t = defaultdict(int)
def add(u, v, w):
lu = int(log2(u))
lv = int(log2(v))
if lv > lu:
u, lu, v, lv = v, lv, u, lu
for _ in range(lu - lv):
t[u] += w
u >>= 1
while u != v:
t[u] += w
t[v] += w
u >>= 1
v >>= 1
def query(u, v):
lu = int(log2(u))
lv = int(log2(v))
if lv > lu:
u, lu, v, lv = v, lv, u, lu
ans = 0
for _ in range(lu - lv):
ans += t[u]
u >>= 1
while u != v:
ans += t[u] + t[v]
u >>= 1
v >>= 1
return ans
for _ in range(q):
s = input()
if s[0] == '1':
_, u, v, w = map(int, s.split(' '))
add(u, v, w)
else:
_, u, v = map(int, s.split(' '))
print(query(u, v))
|
Codeforces Round 362 (Div. 1)
|
CF
| 2,016
| 1
| 256
|
Lorenzo Von Matterhorn
|
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections.
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
|
The first line of input contains a single integer q (1 ≤ q ≤ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line.
|
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
| null |
In the example testcase:
Here are the intersections used:
1. Intersections on the path are 3, 1, 2 and 4.
2. Intersections on the path are 4, 2 and 1.
3. Intersections on the path are only 3 and 6.
4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94.
5. Intersections on the path are 6, 3 and 1.
6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0.
7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
|
[{"input": "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4", "output": "94\n0\n32"}]
| 1,500
|
["brute force", "data structures", "implementation", "trees"]
| 49
|
[{"input": "7\r\n1 3 4 30\r\n1 4 1 2\r\n1 3 6 8\r\n2 4 3\r\n1 6 1 40\r\n2 3 7\r\n2 2 4\r\n", "output": "94\r\n0\r\n32\r\n"}, {"input": "1\r\n2 666077344481199252 881371880336470888\r\n", "output": "0\r\n"}, {"input": "10\r\n1 1 63669439577744021 396980128\r\n1 2582240553355225 63669439577744021 997926286\r\n1 2582240553355225 1 619026011\r\n1 1 4 231881718\r\n2 63669439577744021 3886074192977\r\n2 4 63669439577744021\r\n2 124354374175272 10328962213420903\r\n1 10328962213420903 3886074192977 188186816\r\n1 124354374175272 31088593543820 705639304\r\n2 2582240553355225 254677758310976084\r\n", "output": "19528689796\r\n80417520800\r\n140119493557\r\n179078288337\r\n"}, {"input": "10\r\n1 1 399719082491 159376944\r\n1 186 1 699740230\r\n2 410731850987390 1\r\n1 410731850987390 399719082491 699271234\r\n1 1 186 255736462\r\n1 1 186 544477714\r\n1 399719082491 410731850987390 366708275\r\n2 1 186\r\n2 410731850987390 1\r\n2 399719082491 186\r\n", "output": "6013820218\r\n11615319450\r\n55320479319\r\n37986050043\r\n"}, {"input": "10\r\n2 37526406560905229 37526426361107171\r\n2 37526424114740747 18763396439955441\r\n2 300485276957081578 301492476099962199\r\n1 75035386466351570 441803674395985082 642312512\r\n2 300197522144700185 220954108245114486\r\n1 150105696341181576 559187296 100113944\r\n1 300197522135707767 150242638470761995 170574370\r\n2 150105691058036871 220954108245108400\r\n2 37560659619635168 150070774425697078\r\n2 18780329809814344 300222324900057526\r\n", "output": "0\r\n0\r\n0\r\n13488562752\r\n14270974176\r\n13899046930\r\n5418394872\r\n"}, {"input": "1\r\n2 1 343417335313797025\r\n", "output": "0\r\n"}, {"input": "2\r\n1 562949953421312 562949953421311 1\r\n2 562949953421312 562949953421311\r\n", "output": "97\r\n"}, {"input": "2\r\n1 100 50 1\r\n2 4294967396 1\r\n", "output": "0\r\n"}, {"input": "2\r\n1 4294967298 4294967299 10\r\n2 2 3\r\n", "output": "0\r\n"}, {"input": "2\r\n1 500000000000 250000000000 1\r\n2 1783793664 891896832\r\n", "output": "0\r\n"}, {"input": "2\r\n1 100000000000000 200000000000000 1\r\n2 276447232 552894464\r\n", "output": "0\r\n"}, {"input": "2\r\n1 2147540141 4295080282 1\r\n2 1 112986\r\n", "output": "0\r\n"}, {"input": "2\r\n1 239841676148963 1 20\r\n2 2112405731 1\r\n", "output": "20\r\n"}]
| false
|
stdio
| null | true
|
333/B
|
333
|
B
|
Python 3
|
TESTS
| 5
| 124
| 0
|
4193256
|
n, m = map(int, input().split())
a = set(i for i in range(2, n))
b = set(i for i in range(2, n))
c = set(i for i in range(2, n))
for i in range(m):
x, y = map(int, input().split())
a.discard(x)
b.discard(y)
c.discard(n + 1 - y)
print(max(len(a | b), len(a | c)))
| 20
| 686
| 0
|
6455367
|
n, m = map(int, input().split())
set_x = {1, n}
set_y = {1, n}
set_total = {x for x in range(1, n + 1)}
for i in range(0, m):
x, y = map(int, input().split())
set_x.add(x)
set_y.add(y)
result = 0
avai_x = set_total - set_x
avai_y = set_total - set_y
result = len(avai_x) + len(avai_y)
if (n & 1) == 1 and ((n >> 1) + 1) in avai_y and ((n >> 1) + 1) in avai_x:
result -= 1
print(result)
|
Codeforces Round 194 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Chips
|
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:
- At least one of the chips at least once fell to the banned cell.
- At least once two chips were on the same cell.
- At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.
|
The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.
Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.
|
Print a single integer — the maximum points Gerald can earn in this game.
| null |
In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
|
[{"input": "3 1\n2 2", "output": "0"}, {"input": "3 0", "output": "1"}, {"input": "4 3\n3 1\n3 2\n3 3", "output": "1"}]
| 1,800
|
["greedy"]
| 20
|
[{"input": "3 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "3 0\r\n", "output": "1\r\n"}, {"input": "4 3\r\n3 1\r\n3 2\r\n3 3\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 3\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1\r\n3 2\r\n", "output": "4\r\n"}, {"input": "5 1\r\n2 3\r\n", "output": "4\r\n"}, {"input": "1000 0\r\n", "output": "1996\r\n"}, {"input": "999 0\r\n", "output": "1993\r\n"}, {"input": "5 5\r\n3 2\r\n5 4\r\n3 3\r\n2 3\r\n1 2\r\n", "output": "1\r\n"}, {"input": "5 5\r\n3 2\r\n1 4\r\n5 1\r\n4 5\r\n3 1\r\n", "output": "2\r\n"}, {"input": "5 5\r\n2 2\r\n5 3\r\n2 3\r\n5 1\r\n4 4\r\n", "output": "1\r\n"}, {"input": "6 5\r\n2 6\r\n6 5\r\n3 1\r\n2 2\r\n1 2\r\n", "output": "4\r\n"}, {"input": "6 5\r\n2 6\r\n5 2\r\n4 3\r\n6 6\r\n2 5\r\n", "output": "2\r\n"}, {"input": "6 5\r\n2 1\r\n6 4\r\n2 2\r\n4 3\r\n4 1\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
336/C
|
336
|
C
|
PyPy 3-64
|
TESTS
| 7
| 171
| 23,449,600
|
201977376
|
from math import log
n = int(input())
nums = list(map(int, input().split()))
maxpos = -1
cands = []
for position in range(int(18*log(10, 2))):
# Check if theres a 0 in any thing in the position
working_nums = [num for num in nums if num & (1 << position) != 0]
if len(working_nums) == 0:
continue
anded = working_nums[0]
for num in working_nums[1:]:
anded &= num
cands.append((anded, working_nums))
bestnums = []
bestscr = 0
for cand in cands:
## Score is v2 of the cand
a, b = cand
score = 0
while(a%2==0):
a//=2
score+=1
if score > bestscr:
bestscr = score
bestnums = b
print(len(bestnums))
print(*bestnums)
| 45
| 140
| 12,288,000
|
162183460
|
import sys
from array import array
input = lambda: sys.stdin.buffer.readline().decode().strip()
get_bit = lambda x, i: (x >> i) & 1
n, a = int(input()), array('i', [int(x) for x in input().split()])
for bit in range(29, -1, -1):
and_ = (1 << 30) - 1
for i in range(n):
if get_bit(a[i], bit):
and_ &= a[i]
if and_ % (1 << bit) == 0:
ans = array('i')
for i in range(n):
if get_bit(a[i], bit):
ans.append(a[i])
exit(print(f"{len(ans)}\n{' '.join(map(str, ans))}"))
|
Codeforces Round 195 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Vasily the Bear and Sequence
|
Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum.
The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 and ... and bk is divisible by number 2v without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b1 and b2 and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1.
Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.
Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and".
|
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).
|
In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk — the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.
| null | null |
[{"input": "5\n1 2 3 4 5", "output": "2\n4 5"}, {"input": "3\n1 2 4", "output": "1\n4"}]
| 1,800
|
["brute force", "greedy", "implementation", "number theory"]
| 45
|
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "2\r\n4 5\r\n"}, {"input": "3\r\n1 2 4\r\n", "output": "1\r\n4\r\n"}, {"input": "3\r\n1 20 22\r\n", "output": "2\r\n20 22\r\n"}, {"input": "10\r\n109070199 215498062 361633800 406156967 452258663 530571268 670482660 704334662 841023955 967424642\r\n", "output": "6\r\n361633800 406156967 452258663 530571268 841023955 967424642\r\n"}, {"input": "30\r\n61 65 67 71 73 75 77 79 129 131 135 137 139 141 267 520 521 522 524 526 1044 1053 6924600 32125372 105667932 109158064 192212084 202506108 214625360 260071380\r\n", "output": "8\r\n520 521 522 524 526 109158064 202506108 260071380\r\n"}, {"input": "40\r\n6 7 10 11 18 19 33 65 129 258 514 515 1026 2049 4741374 8220406 14324390 17172794 17931398 33354714 34796238 38926670 39901570 71292026 72512934 77319030 95372470 102081830 114152702 120215390 133853238 134659386 159128594 165647058 219356350 225884742 236147130 240926050 251729234 263751314\r\n", "output": "13\r\n2049 4741374 8220406 17172794 17931398 38926670 39901570 77319030 134659386 159128594 219356350 225884742 240926050\r\n"}, {"input": "1\r\n536870912\r\n", "output": "1\r\n536870912\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n536870911\r\n", "output": "1\r\n536870911\r\n"}, {"input": "2\r\n536870911 536870912\r\n", "output": "1\r\n536870912\r\n"}, {"input": "38\r\n37750369 37750485 37750546 37751012 37751307 37751414 37751958 37751964 37752222 37752448 75497637 75497768 75497771 75498087 75498145 75498177 75498298 75498416 75498457 150994987 150994994 150994999 150995011 150995012 150995015 150995016 150995023 150995040 150995053 805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\r\n", "output": "9\r\n805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\r\n"}, {"input": "39\r\n37749932 37750076 37750391 37750488 37750607 37750812 37750978 37751835 37752173 37752254 75497669 75497829 75497852 75498044 75498061 75498155 75498198 75498341 75498382 75498465 150994988 150994989 150995009 150995019 150995024 150995030 150995031 150995069 150995072 805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\r\n", "output": "10\r\n805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\r\n"}]
| false
|
stdio
| null | true
|
336/C
|
336
|
C
|
PyPy 3
|
TESTS
| 7
| 405
| 27,340,800
|
107368859
|
from sys import stdin,stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):#nmbr()):
n=nmbr()
a=lst()
bits=[[0 for _ in range(34)] for _ in range(n)]
for i in range(n):
for j in range(34):
bits[i][33-j]=(a[i]>>j)&1
# print(*bits,sep='\n')
power=bit=mx=0
x=(1<<32)-1
for bp in range(34):
power=0
nd=x
for i in range(n):
if bits[i][33-bp]:
nd&=a[i]
if nd%(1<<bp)==0:
bit=bp
if bit==0:
print(-1)
else:
ans=[]
for i in range(n):
if bits[i][33-bit]:
ans+=[a[i]]
print(len(ans))
print(*ans)
| 45
| 343
| 34,304,000
|
123697311
|
x=int(input())
s=list(map(int,input().split()))
ans=[]
for u in range(0,30):
cur=(1<<(u))
v=(1<<(u+1))-1
tem=[]
for n in s:
if n&(cur):
tem.append(n)
for n in tem:
v&=n
if v%(1<<(u))==0:
ans=tem
print(len(ans))
print(*ans)
|
Codeforces Round 195 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Vasily the Bear and Sequence
|
Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum.
The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 and ... and bk is divisible by number 2v without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b1 and b2 and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1.
Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.
Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and".
|
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).
|
In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk — the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.
| null | null |
[{"input": "5\n1 2 3 4 5", "output": "2\n4 5"}, {"input": "3\n1 2 4", "output": "1\n4"}]
| 1,800
|
["brute force", "greedy", "implementation", "number theory"]
| 45
|
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "2\r\n4 5\r\n"}, {"input": "3\r\n1 2 4\r\n", "output": "1\r\n4\r\n"}, {"input": "3\r\n1 20 22\r\n", "output": "2\r\n20 22\r\n"}, {"input": "10\r\n109070199 215498062 361633800 406156967 452258663 530571268 670482660 704334662 841023955 967424642\r\n", "output": "6\r\n361633800 406156967 452258663 530571268 841023955 967424642\r\n"}, {"input": "30\r\n61 65 67 71 73 75 77 79 129 131 135 137 139 141 267 520 521 522 524 526 1044 1053 6924600 32125372 105667932 109158064 192212084 202506108 214625360 260071380\r\n", "output": "8\r\n520 521 522 524 526 109158064 202506108 260071380\r\n"}, {"input": "40\r\n6 7 10 11 18 19 33 65 129 258 514 515 1026 2049 4741374 8220406 14324390 17172794 17931398 33354714 34796238 38926670 39901570 71292026 72512934 77319030 95372470 102081830 114152702 120215390 133853238 134659386 159128594 165647058 219356350 225884742 236147130 240926050 251729234 263751314\r\n", "output": "13\r\n2049 4741374 8220406 17172794 17931398 38926670 39901570 77319030 134659386 159128594 219356350 225884742 240926050\r\n"}, {"input": "1\r\n536870912\r\n", "output": "1\r\n536870912\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n536870911\r\n", "output": "1\r\n536870911\r\n"}, {"input": "2\r\n536870911 536870912\r\n", "output": "1\r\n536870912\r\n"}, {"input": "38\r\n37750369 37750485 37750546 37751012 37751307 37751414 37751958 37751964 37752222 37752448 75497637 75497768 75497771 75498087 75498145 75498177 75498298 75498416 75498457 150994987 150994994 150994999 150995011 150995012 150995015 150995016 150995023 150995040 150995053 805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\r\n", "output": "9\r\n805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\r\n"}, {"input": "39\r\n37749932 37750076 37750391 37750488 37750607 37750812 37750978 37751835 37752173 37752254 75497669 75497829 75497852 75498044 75498061 75498155 75498198 75498341 75498382 75498465 150994988 150994989 150995009 150995019 150995024 150995030 150995031 150995069 150995072 805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\r\n", "output": "10\r\n805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\r\n"}]
| false
|
stdio
| null | true
|
149/C
|
149
|
C
|
Python 3
|
TESTS
| 2
| 46
| 307,200
|
213778839
|
import sys
sys.setrecursionlimit(2000000)
from collections import defaultdict
def clc():
n = int(input())
arr = list(map(int,input().split()))
turn = 0
summ1,summ2 = 0,0
start,end = 0,n-1
team1,team2= [],[]
while start<end:
if turn == 0:
summ1+=arr[start]+arr[end]
team1.append(start+1)
team1.append(end+1)
else:
summ2+=arr[start]+arr[end]
team2.append(start+1)
team2.append(end+1)
turn = 1-turn
start+=1
end-=1
if start == end:
if summ1>summ2:
summ2+=arr[start]
team2.append(start+1)
else:
summ1+=arr[start]
team1.append(start+1)
print(len(team1))
print(*team1)
print(len(team2))
print(*team2)
return True
cc = clc()
if not cc :
print(-1)
| 47
| 327
| 11,776,000
|
148531234
|
n=int(input())
a=list(map(int,input().split()))
l=[]
for i in range(n):
l+=[[a[i],i]]
l.sort(key=lambda x:x[0])
q=[[],[]]
for i in range(n):
q[i%2]+=[l[i][1]+1]
print(len(q[0]))
print(*q[0])
print(len(q[1]))
print(*q[1])
|
Codeforces Round 106 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Division into Teams
|
Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).
The key in football is to divide into teams fairly before the game begins. There are n boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristic ai (the larger it is, the better the boy plays).
Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team as qi. Division n boys into two teams is considered fair if three conditions are fulfilled:
- Each boy plays for exactly one team (x + y = n).
- The sizes of teams differ in no more than one (|x - y| ≤ 1).
- The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally: $$\left| \sum_{i=1}^{x} a_{p_i} - \sum_{i=1}^{y} a_{q_i} \right| \leq \max_{i=1}^{n} a_i$$
Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.
|
The first line contains the only integer n (2 ≤ n ≤ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 ≤ ai ≤ 104), the i-th number represents the i-th boy's playing skills.
|
On the first line print an integer x — the number of boys playing for the first team. On the second line print x integers — the individual numbers of boys playing for the first team. On the third line print an integer y — the number of boys playing for the second team, on the fourth line print y integers — the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions: x + y = n, |x - y| ≤ 1, and the condition that limits the total skills.
If there are multiple ways to solve the problem, print any of them.
The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.
| null |
Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 ≤ 2) is fulfilled.
|
[{"input": "3\n1 2 1", "output": "2\n1 2\n1\n3"}, {"input": "5\n2 3 3 1 1", "output": "3\n4 1 3\n2\n5 2"}]
| 1,500
|
["greedy", "math", "sortings"]
| 47
|
[{"input": "3\r\n1 2 1\r\n", "output": "2\r\n1 2 \r\n1\r\n3 \r\n"}, {"input": "5\r\n2 3 3 1 1\r\n", "output": "3\r\n4 1 3 \r\n2\r\n5 2 \r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 1 2 2\r\n", "output": "5\r\n8 2 4 6 9 \r\n5\r\n1 3 5 7 10 \r\n"}, {"input": "10\r\n2 3 3 1 3 1 1 1 2 2\r\n", "output": "5\r\n4 7 1 10 3 \r\n5\r\n6 8 9 2 5 \r\n"}, {"input": "10\r\n2 3 2 3 3 1 1 3 1 1\r\n", "output": "5\r\n6 9 1 2 5 \r\n5\r\n7 10 3 4 8 \r\n"}, {"input": "11\r\n1 3 1 2 1 2 2 2 1 1 1\r\n", "output": "6\r\n1 5 10 4 7 2 \r\n5\r\n3 9 11 6 8 \r\n"}, {"input": "11\r\n54 83 96 75 33 27 36 35 26 22 77\r\n", "output": "6\r\n10 6 8 1 11 3 \r\n5\r\n9 5 7 4 2 \r\n"}, {"input": "11\r\n1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "6\r\n1 3 5 7 9 11 \r\n5\r\n2 4 6 8 10 \r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n1 \r\n1\r\n2 \r\n"}, {"input": "2\r\n35 36\r\n", "output": "1\r\n1 \r\n1\r\n2 \r\n"}, {"input": "25\r\n1 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 1 2 2 2 1 1 2 2 1\r\n", "output": "13\r\n1 10 17 22 2 5 7 9 13 15 18 20 24 \r\n12\r\n4 11 21 25 3 6 8 12 14 16 19 23 \r\n"}, {"input": "27\r\n2 1 1 3 1 2 1 1 3 2 3 1 3 2 1 3 2 3 2 1 2 3 2 2 1 2 1\r\n", "output": "14\r\n2 5 8 15 25 1 10 17 21 24 4 11 16 22 \r\n13\r\n3 7 12 20 27 6 14 19 23 26 9 13 18 \r\n"}, {"input": "30\r\n2 2 2 3 4 3 4 4 3 2 3 2 2 4 1 4 2 4 2 2 1 4 3 2 1 3 1 1 4 3\r\n", "output": "15\r\n15 25 28 2 10 13 19 24 6 11 26 5 8 16 22 \r\n15\r\n21 27 1 3 12 17 20 4 9 23 30 7 14 18 29 \r\n"}, {"input": "100\r\n3 4 8 10 8 6 4 3 7 7 6 2 3 1 3 10 1 7 9 3 5 5 2 6 2 9 1 7 4 2 4 1 6 1 7 10 2 5 3 7 6 4 6 2 8 8 8 6 6 10 3 7 4 3 4 1 7 9 3 6 3 6 1 4 9 3 8 1 10 1 4 10 7 7 9 5 3 8 10 2 1 10 8 7 10 8 5 3 1 2 1 10 6 1 5 3 3 5 7 2\r\n", "output": "50\r\n14 27 34 63 70 89 94 23 30 44 90 1 13 20 51 59 66 88 97 7 31 53 64 21 38 87 98 11 33 43 49 62 9 18 35 52 73 84 3 45 47 78 86 26 65 4 36 69 79 85 \r\n50\r\n17 32 56 68 81 91 12 25 37 80 100 8 15 39 54 61 77 96 2 29 42 55 71 22 76 95 6 24 41 48 60 93 10 28 40 57 74 99 5 46 67 83 19 58 75 16 50 72 82 92 \r\n"}, {"input": "100\r\n85 50 17 89 65 89 5 20 86 26 16 21 85 14 44 31 87 31 6 2 48 67 8 80 79 1 48 36 97 1 5 30 79 50 78 12 2 55 76 100 54 40 26 81 97 96 68 56 87 14 51 17 54 37 52 33 69 62 38 63 74 15 62 78 9 19 67 2 60 58 93 60 18 96 55 48 34 7 79 82 32 58 90 67 20 50 27 15 7 89 98 10 11 15 99 49 4 51 77 52\r\n", "output": "50\r\n26 20 68 7 19 89 65 93 14 62 94 3 73 8 12 43 32 18 56 28 59 15 27 96 34 51 55 41 38 48 82 72 63 5 67 47 61 99 64 33 24 80 13 17 4 90 71 74 45 95 \r\n50\r\n30 37 97 31 78 23 92 36 50 88 11 52 66 85 10 87 16 81 77 54 42 21 76 2 86 98 100 53 75 70 69 58 60 22 84 57 39 35 25 79 44 1 9 49 6 83 46 29 91 40 \r\n"}, {"input": "100\r\n2382 7572 9578 1364 2325 2929 7670 5574 2836 2440 6553 1751 929 8785 6894 9373 9308 7338 6380 9541 9951 6785 8993 9942 5087 7544 6582 7139 8458 7424 9759 8199 9464 8817 7625 6200 4955 9373 9500 3062 849 4210 9337 5466 2190 8150 4971 3145 869 5675 1975 161 1998 378 5229 9000 8958 761 358 434 7636 8295 4406 73 375 812 2473 3652 9067 3052 5287 2850 6987 5442 2625 8894 8733 791 9763 5258 8259 9530 2050 7334 2118 2726 8221 5527 8827 1585 8334 8898 6399 6217 7400 2576 5164 9063 6247 9433\r\n", "output": "50\r\n64 59 54 58 66 49 4 12 53 85 5 10 96 86 72 70 48 42 37 25 55 71 44 8 36 99 93 27 15 28 18 30 2 61 46 87 62 29 14 89 92 23 98 17 16 100 39 20 31 24 \r\n50\r\n52 65 60 78 41 13 90 51 83 45 1 67 75 9 6 40 68 63 47 97 80 74 88 50 94 19 11 22 73 84 95 26 35 7 32 81 91 77 34 76 57 56 69 43 38 33 82 3 79 21 \r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "3\r\n10 10 10\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "3\r\n5 10 10\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "5\r\n6 1 1 1 1\r\n", "output": "3\r\n2 4 1 \r\n2\r\n3 5 \r\n"}, {"input": "5\r\n1 100 2 200 3\r\n", "output": "3\r\n1 5 4 \r\n2\r\n3 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().split()))
max_a = max(a)
with open(submission_path) as f:
lines = [line.strip() for line in f.readlines()]
if len(lines) != 4:
print(0)
return
try:
x = int(lines[0])
p = list(map(int, lines[1].split()))
y = int(lines[2])
q = list(map(int, lines[3].split()))
except:
print(0)
return
if x + y != n or abs(x - y) > 1:
print(0)
return
if len(p) != x or len(q) != y:
print(0)
return
all_indices = set(p) | set(q)
if len(all_indices) != x + y or any(i < 1 or i > n for i in all_indices):
print(0)
return
sum_p = sum(a[i-1] for i in p)
sum_q = sum(a[i-1] for i in q)
if abs(sum_p - sum_q) > max_a:
print(0)
return
print(1)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
641/B
|
641
|
B
|
Python 3
|
PRETESTS
| 5
| 343
| 5,632,000
|
17489089
|
n, m, q = map(int, input().split())
a = [[0 for j in range(m)] for i in range(n)]
query = []
offset = [[[0, 0] for j in range(m)] for i in range(n)]
for i in range(q):
s = list(map(int, input().split()))
if len(s) == 2:
if s[0] == 1:
for j in range(m):
offset[s[1] - 1][j][0] += 1
else:
for j in range(n):
offset[j][s[1] - 1][1] += 1
else:
a[s[1] - 1][s[2] - 1] = s[3]
for i in range(n):
for j in range(m):
print(a[i - offset[i][j][1]][j - offset[i][j][0]], end = ' ')
print()
| 26
| 187
| 716,800
|
19453083
|
def main():
n, m, q = map(int, input().split())
nm = ["0"] * (m * n)
qq = [input() for _ in range(q)]
for s in reversed(qq):
k, *l = s.split()
if k == "3":
nm[(int(l[0]) - 1) * m + int(l[1]) - 1] = l[2]
elif k == "2":
j = int(l[0]) - 1
x = nm[j - m]
for i in range((n - 1) * m + j, j, -m):
nm[i] = nm[i - m]
nm[j] = x
else:
j = (int(l[0]) - 1) * m
x = nm[j + m - 1]
for i in range(j + m - 1, j, -1):
nm[i] = nm[i - 1]
nm[j] = x
for i in range(0, n * m, m):
print(' '.join(nm[i:i + m]))
if __name__ == "__main__":
main()
|
VK Cup 2016 - Round 2
|
CF
| 2,016
| 2
| 256
|
Little Artem and Matrix
|
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
|
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m, - 109 ≤ xi ≤ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
|
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
| null | null |
[{"input": "2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8", "output": "8 2\n1 8"}, {"input": "3 3 2\n1 2\n3 2 2 5", "output": "0 0 0\n0 0 5\n0 0 0"}]
| 1,400
|
["implementation"]
| 26
|
[{"input": "2 2 6\r\n2 1\r\n2 2\r\n3 1 1 1\r\n3 2 2 2\r\n3 1 2 8\r\n3 2 1 8\r\n", "output": "8 2 \r\n1 8 \r\n"}, {"input": "3 3 2\r\n1 2\r\n3 2 2 5\r\n", "output": "0 0 0 \r\n0 0 5 \r\n0 0 0 \r\n"}, {"input": "5 5 1\r\n1 5\r\n", "output": "0 0 0 0 0 \r\n0 0 0 0 0 \r\n0 0 0 0 0 \r\n0 0 0 0 0 \r\n0 0 0 0 0 \r\n"}, {"input": "1 1 3\r\n1 1\r\n2 1\r\n3 1 1 1000000000\r\n", "output": "1000000000 \r\n"}, {"input": "1 1 3\r\n1 1\r\n2 1\r\n3 1 1 -1000000000\r\n", "output": "-1000000000 \r\n"}, {"input": "2 2 6\r\n2 1\r\n2 2\r\n3 1 1 -1\r\n3 2 2 -1\r\n3 1 2 -1\r\n3 2 1 -1\r\n", "output": "-1 -1 \r\n-1 -1 \r\n"}, {"input": "1 4 5\r\n1 1\r\n3 1 1 1\r\n3 1 2 2\r\n3 1 3 3\r\n3 1 4 4\r\n", "output": "4 1 2 3 \r\n"}, {"input": "4 2 5\r\n2 1\r\n3 1 1 5\r\n3 2 1 6\r\n3 3 1 7\r\n3 4 1 9\r\n", "output": "9 0 \r\n5 0 \r\n6 0 \r\n7 0 \r\n"}, {"input": "3 10 2\r\n1 2\r\n3 2 7 5\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 5 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 2 2\r\n1 1\r\n3 1 2 15\r\n", "output": "15 0 \r\n"}]
| false
|
stdio
| null | true
|
297/A
|
297
|
A
|
Python 3
|
TESTS
| 2
| 186
| 0
|
69002019
|
def maxConsecutive(a):
i = 1
curr = 0
if a[0] == '1':
curr += 1
ans = curr
for i in range(len(a)):
if a[i] == '1':
curr += 1
else:
curr = 1
ans = max(ans, curr)
return ans
a = input()
b = input()
if maxConsecutive(a) >= maxConsecutive(b):
print("YES")
else:
print("NO")
| 79
| 124
| 1,638,400
|
181064253
|
def count(s):
cnt = 0
for c in s:
if c == '1':
cnt += 1
return cnt
a = count(input())
b = count(input())
if a % 2 == 1:
a += 1
if a >= b:
print("YES")
else:
print("NO")
|
Codeforces Round 180 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Parity Game
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations:
- Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$.
- Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty.
You can use as many operations as you want. The problem is, is it possible to turn a into b?
The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
|
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
| null |
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
|
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
| 1,700
|
["constructive algorithms"]
| 79
|
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
297/A
|
297
|
A
|
Python 3
|
TESTS
| 2
| 216
| 0
|
42467491
|
a = input()
b = input()
sa = 0
for s in a:
if s == '1':
sa += 1
sb = 0
for s in b:
if s == '1':
sb += 1
if sa >= sb:
print("YES")
else:
print("NO")
| 79
| 124
| 4,608,000
|
29355635
|
a, b = input(), input()
ax = sum([ord(c) - ord('0') for c in a])
bx = sum([ord(c) - ord('0') for c in b])
print("YES" if bx <= ax + ax % 2 else "NO")
|
Codeforces Round 180 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Parity Game
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations:
- Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$.
- Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty.
You can use as many operations as you want. The problem is, is it possible to turn a into b?
The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
|
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
| null |
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
|
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
| 1,700
|
["constructive algorithms"]
| 79
|
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
297/A
|
297
|
A
|
Python 3
|
TESTS
| 2
| 248
| 0
|
42543133
|
#python 3.6
a=str(input())
b=str(input())
tmp=""
kalimat=""
for i in range(0,len(b)):
if(b[i]=="1"):
if(kalimat!=""):
kalimat+=tmp;
tmp=""
kalimat+="1"
else:
tmp+="0"
if(a.find(kalimat)!=-1):
print("YES")
else:
print("NO")
| 79
| 124
| 5,632,000
|
33648427
|
n = input().count('1')
m = input().count('1')
print ('YNEOS'[n + (n & 1) < m::2])
|
Codeforces Round 180 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Parity Game
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations:
- Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$.
- Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty.
You can use as many operations as you want. The problem is, is it possible to turn a into b?
The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
|
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
| null |
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
|
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
| 1,700
|
["constructive algorithms"]
| 79
|
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
741/B
|
741
|
B
|
PyPy 3
|
TESTS
| 20
| 935
| 8,294,400
|
51004244
|
R = lambda: map(int, input().split())
n, m, w = R()
ws = list(R())
bs = list(R())
anc = [-1] * n
def get(x):
if anc[x] < 0:
return x
anc[x] = get(anc[x])
return anc[x]
def join(x1, x2):
x1, x2 = get(x1), get(x2)
if x1 != x2:
anc[x1] = x2
for i in range(m):
x1, x2 = R()
join(x1 - 1, x2 - 1)
gps = [list() for i in range(n)]
for i in range(n):
gps[get(i)].append(i)
gps = [x for x in gps if x]
dp = [[0] * (w + 1) for i in range(len(gps))]
for i in range(len(gps)):
tw = sum(ws[x] for x in gps[i])
tb = sum(bs[x] for x in gps[i])
for j in range(w + 1):
dp[i][j] = tb + dp[i - 1][j - tw] if tw <= j else dp[i][j]
for k in gps[i]:
dp[i][j] = max(dp[i][j], dp[i - 1][j], (dp[i - 1][j - ws[k]] + bs[k] if ws[k] <= j else 0))
print(dp[len(gps) - 1][w])
| 68
| 187
| 17,100,800
|
183413182
|
import sys
input = sys.stdin.buffer.readline
def find_root(root_dict, x):
L = []
while x != root_dict[x]:
L.append(x)
x = root_dict[x]
for y in L:
root_dict[y] = x
return x
def process(W, B, G, w):
n = len(W)
root_dict = [i for i in range(n+1)]
for x, y in G:
x1 = find_root(root_dict, x)
y1 = find_root(root_dict, y)
root_dict[x1] = y1
roots = [[[0, 0]] for i in range(n+1)]
for i in range(1, n+1):
wi = W[i-1]
bi = B[i-1]
i1 = find_root(root_dict, i)
roots[i1][0][0]+=wi
roots[i1][0][1]+=bi
roots[i1].append([wi, bi])
d = [0 for i in range(w+1)]
for i in range(1, n+1):
d2 = [x for x in d]
for wi, bi in roots[i]:
for j in range(w+1):
if j+wi <= w:
d2[j+wi] = max(d2[j+wi], d[j]+bi)
d = d2
answer = max(d)
sys.stdout.write(f'{answer}\n')
return
n, m, w = [int(x) for x in input().split()]
W = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
G = []
for i in range(m):
xi, yi = [int(x) for x in input().split()]
G.append([xi, yi])
process(W, B, G, w)
|
Codeforces Round 383 (Div. 1)
|
CF
| 2,016
| 1
| 256
|
Arpa's weak amphitheater and Mehrdad's valuable Hoses
|
Just to remind, girls in Arpa's land are really nice.
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.
|
The first line contains integers n, m and w (1 ≤ n ≤ 1000, $$0 \leq m \leq \min\left(\frac{n(n-1)}{2}, 10^5\right)$$, 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.
|
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
| null |
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
|
[{"input": "3 1 5\n3 2 5\n2 4 2\n1 2", "output": "6"}, {"input": "4 2 11\n2 4 6 6\n6 4 2 1\n1 2\n2 3", "output": "7"}]
| 1,600
|
["dfs and similar", "dp", "dsu"]
| 68
|
[{"input": "3 1 5\r\n3 2 5\r\n2 4 2\r\n1 2\r\n", "output": "6\r\n"}, {"input": "4 2 11\r\n2 4 6 6\r\n6 4 2 1\r\n1 2\r\n2 3\r\n", "output": "7\r\n"}, {"input": "10 5 100\r\n70 67 8 64 28 82 18 61 82 7\r\n596434 595982 237932 275698 361351 850374 936914 877996 789231 331012\r\n1 7\r\n2 4\r\n3 6\r\n5 7\r\n1 5\r\n", "output": "2383854\r\n"}, {"input": "10 5 100\r\n64 90 3 94 96 97 52 54 82 31\r\n796554 444893 214351 43810 684158 555762 686198 339093 383018 699152\r\n6 8\r\n8 3\r\n3 9\r\n2 3\r\n10 3\r\n", "output": "1495706\r\n"}, {"input": "10 5 100\r\n6 18 35 6 87 58 4 53 37 71\r\n465782 57034 547741 748298 315223 370368 679320 349012 9740 622511\r\n1 2\r\n10 9\r\n6 7\r\n3 6\r\n7 1\r\n", "output": "2050129\r\n"}, {"input": "10 5 100\r\n78 89 3 2 95 96 87 11 13 60\r\n694709 921 799687 428614 221900 536251 117674 36488 219932 771513\r\n4 5\r\n3 4\r\n6 2\r\n2 3\r\n8 3\r\n", "output": "1791132\r\n"}, {"input": "10 5 100\r\n48 73 30 46 95 19 98 73 94 24\r\n501216 675859 843572 565104 879875 828759 80776 766980 213551 492652\r\n1 2\r\n6 5\r\n7 6\r\n10 3\r\n8 1\r\n", "output": "2237435\r\n"}, {"input": "10 5 100\r\n68 55 15 94 53 100 52 68 24 3\r\n286803 660813 226501 624597 215418 290774 416040 961916 910482 50278\r\n1 5\r\n7 2\r\n2 8\r\n5 3\r\n10 3\r\n", "output": "1922676\r\n"}, {"input": "10 5 100\r\n19 8 95 18 9 79 42 94 20 49\r\n735491 935681 717266 935275 521356 866021 356037 394445 589369 585077\r\n9 4\r\n5 6\r\n5 1\r\n1 4\r\n7 1\r\n", "output": "2456033\r\n"}]
| false
|
stdio
| null | true
|
555/A
|
555
|
A
|
Python 3
|
PRETESTS
| 5
| 77
| 0
|
11790356
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
(N,K) = map(int,input().split())
ret = 0
for k in range(K):
nums = list(map(int,input().split()))[1:]
group = len(nums)
for j in range(len(nums)-1):
if nums[j] + 1 == nums[j+1]:
group -= 1
ret += group - 1 # split
ret += K + ret - 1 # join
print(ret)
| 47
| 312
| 7,168,000
|
11803493
|
def count_continuous(a):
total = 0
for i in range(len(a) - 1):
if a[i] + 1 == a[i + 1]:
total += 1
else:
break
return total
def main():
n, k = [int(t) for t in input().split()]
done = 0
for _ in range(k):
a = [int(t) for t in input().split()]
if a[1] == 1:
done = count_continuous(a[1:])
attach = n - 1 - done
detach = n - k - done
cost = attach + detach
print(cost)
if __name__ == '__main__':
main()
|
Codeforces Round 310 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Case of Matryoshkas
|
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
- Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
- Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
|
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
|
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
| null |
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
|
[{"input": "3 2\n2 1 2\n1 3", "output": "1"}, {"input": "7 3\n3 1 3 7\n2 2 5\n2 4 6", "output": "10"}]
| 1,500
|
["implementation"]
| 47
|
[{"input": "3 2\r\n2 1 2\r\n1 3\r\n", "output": "1\r\n"}, {"input": "7 3\r\n3 1 3 7\r\n2 2 5\r\n2 4 6\r\n", "output": "10\r\n"}, {"input": "1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "3 2\r\n1 2\r\n2 1 3\r\n", "output": "3\r\n"}, {"input": "5 3\r\n1 4\r\n3 1 2 3\r\n1 5\r\n", "output": "2\r\n"}, {"input": "8 5\r\n2 1 2\r\n2 3 4\r\n1 5\r\n2 6 7\r\n1 8\r\n", "output": "8\r\n"}, {"input": "10 10\r\n1 5\r\n1 4\r\n1 10\r\n1 3\r\n1 7\r\n1 1\r\n1 8\r\n1 6\r\n1 9\r\n1 2\r\n", "output": "9\r\n"}, {"input": "20 6\r\n3 8 9 13\r\n3 4 14 20\r\n2 15 17\r\n3 2 5 11\r\n5 7 10 12 18 19\r\n4 1 3 6 16\r\n", "output": "33\r\n"}, {"input": "50 10\r\n6 17 21 31 42 45 49\r\n6 11 12 15 22 26 38\r\n3 9 29 36\r\n3 10 23 43\r\n5 14 19 28 46 48\r\n2 30 39\r\n6 13 20 24 33 37 47\r\n8 1 2 3 4 5 6 7 8\r\n7 16 18 25 27 34 40 44\r\n4 32 35 41 50\r\n", "output": "75\r\n"}, {"input": "13 8\r\n1 5\r\n2 8 10\r\n1 13\r\n4 1 2 3 11\r\n1 7\r\n2 6 12\r\n1 4\r\n1 9\r\n", "output": "13\r\n"}, {"input": "21 13\r\n1 18\r\n2 8 13\r\n1 21\r\n1 17\r\n2 7 9\r\n1 20\r\n1 19\r\n1 4\r\n1 16\r\n2 5 6\r\n3 12 14 15\r\n3 1 2 3\r\n2 10 11\r\n", "output": "24\r\n"}, {"input": "50 50\r\n1 2\r\n1 5\r\n1 28\r\n1 46\r\n1 42\r\n1 24\r\n1 3\r\n1 37\r\n1 33\r\n1 50\r\n1 23\r\n1 40\r\n1 43\r\n1 26\r\n1 49\r\n1 34\r\n1 8\r\n1 45\r\n1 15\r\n1 1\r\n1 22\r\n1 18\r\n1 27\r\n1 25\r\n1 13\r\n1 39\r\n1 38\r\n1 10\r\n1 44\r\n1 6\r\n1 17\r\n1 47\r\n1 7\r\n1 35\r\n1 20\r\n1 36\r\n1 31\r\n1 21\r\n1 32\r\n1 29\r\n1 4\r\n1 12\r\n1 19\r\n1 16\r\n1 11\r\n1 41\r\n1 9\r\n1 14\r\n1 30\r\n1 48\r\n", "output": "49\r\n"}, {"input": "100 3\r\n45 1 2 3 4 5 6 7 8 9 19 21 24 27 28 30 34 35 37 39 40 41 42 43 46 47 48 51 52 55 58 59 61 63 64 66 69 71 76 80 85 86 88 89 94 99\r\n26 10 11 15 18 23 29 31 33 36 38 44 49 54 56 60 62 65 75 78 82 83 84 95 96 97 98\r\n29 12 13 14 16 17 20 22 25 26 32 45 50 53 57 67 68 70 72 73 74 77 79 81 87 90 91 92 93 100\r\n", "output": "180\r\n"}, {"input": "100 19\r\n6 62 72 83 91 94 97\r\n3 61 84 99\r\n1 63\r\n5 46 53 56 69 78\r\n5 41 43 49 74 89\r\n5 55 57 79 85 87\r\n3 47 59 98\r\n3 64 76 82\r\n3 48 66 75\r\n2 60 88\r\n2 67 77\r\n4 40 51 73 95\r\n41 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 44 71 81\r\n4 58 65 90 93\r\n1 100\r\n5 39 45 52 80 86\r\n2 50 68\r\n1 92\r\n4 42 54 70 96\r\n", "output": "106\r\n"}]
| false
|
stdio
| null | true
|
149/C
|
149
|
C
|
PyPy 3-64
|
TESTS
| 2
| 46
| 0
|
207900946
|
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 =int (input()) ; lis= list(inp()) ; p =[] ; q= [] ; turn = 0 ; c= 0 ; arr=[]
for i in range(n) :
arr.append((lis[i] , i+1))
arr.sort(reverse=True)
p.append(arr[0][1])
for i in range(1,n) :
if i== n-1 :
if turn :
if c== 1 : q.append(arr[i][1])
else : p.append(arr[i][1])
else :
q.append(arr[i][1])
continue
if turn==0 :
if c :
c=0 ; turn^=1
q.append(arr[i][1]) ; c+=1
else :
if c :
c=0 ; turn^=1
p.append(arr[i][1])
print(len(p)) ; print(*p) ; print(len(q)) ; print(*q)
| 47
| 343
| 8,908,800
|
154260031
|
n = int(input())
a = list(map(int, input().split()))
l = [(a[i], i+1) for i in range(n)]
l.sort(reverse = True)
# print(l)
x = 1
y = 1
xx = [l[0][1]]
sx = l[0][0]
yy = [l[1][1]]
sy = l[1][0]
for i in range(2, n):
if i%2==0:
sx+=l[i][0]
xx.append(l[i][1])
x+=1
else:
sy+=l[i][0]
yy.append(l[i][1])
y+=1
if abs(sx-sy)<=l[0][0]:
print(x)
print(*xx)
print(y)
print(*yy)
else:
t = a[xx[1]-1]
u = xx[1]
x-=1
sx-=t
xx.pop(1)
y+=1
sy+=t
yy.append(u)
print(x)
print(*xx)
print(y)
print(*yy)
|
Codeforces Round 106 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Division into Teams
|
Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).
The key in football is to divide into teams fairly before the game begins. There are n boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristic ai (the larger it is, the better the boy plays).
Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team as qi. Division n boys into two teams is considered fair if three conditions are fulfilled:
- Each boy plays for exactly one team (x + y = n).
- The sizes of teams differ in no more than one (|x - y| ≤ 1).
- The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally: $$\left| \sum_{i=1}^{x} a_{p_i} - \sum_{i=1}^{y} a_{q_i} \right| \leq \max_{i=1}^{n} a_i$$
Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.
|
The first line contains the only integer n (2 ≤ n ≤ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 ≤ ai ≤ 104), the i-th number represents the i-th boy's playing skills.
|
On the first line print an integer x — the number of boys playing for the first team. On the second line print x integers — the individual numbers of boys playing for the first team. On the third line print an integer y — the number of boys playing for the second team, on the fourth line print y integers — the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions: x + y = n, |x - y| ≤ 1, and the condition that limits the total skills.
If there are multiple ways to solve the problem, print any of them.
The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.
| null |
Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 ≤ 2) is fulfilled.
|
[{"input": "3\n1 2 1", "output": "2\n1 2\n1\n3"}, {"input": "5\n2 3 3 1 1", "output": "3\n4 1 3\n2\n5 2"}]
| 1,500
|
["greedy", "math", "sortings"]
| 47
|
[{"input": "3\r\n1 2 1\r\n", "output": "2\r\n1 2 \r\n1\r\n3 \r\n"}, {"input": "5\r\n2 3 3 1 1\r\n", "output": "3\r\n4 1 3 \r\n2\r\n5 2 \r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 1 2 2\r\n", "output": "5\r\n8 2 4 6 9 \r\n5\r\n1 3 5 7 10 \r\n"}, {"input": "10\r\n2 3 3 1 3 1 1 1 2 2\r\n", "output": "5\r\n4 7 1 10 3 \r\n5\r\n6 8 9 2 5 \r\n"}, {"input": "10\r\n2 3 2 3 3 1 1 3 1 1\r\n", "output": "5\r\n6 9 1 2 5 \r\n5\r\n7 10 3 4 8 \r\n"}, {"input": "11\r\n1 3 1 2 1 2 2 2 1 1 1\r\n", "output": "6\r\n1 5 10 4 7 2 \r\n5\r\n3 9 11 6 8 \r\n"}, {"input": "11\r\n54 83 96 75 33 27 36 35 26 22 77\r\n", "output": "6\r\n10 6 8 1 11 3 \r\n5\r\n9 5 7 4 2 \r\n"}, {"input": "11\r\n1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "6\r\n1 3 5 7 9 11 \r\n5\r\n2 4 6 8 10 \r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n1 \r\n1\r\n2 \r\n"}, {"input": "2\r\n35 36\r\n", "output": "1\r\n1 \r\n1\r\n2 \r\n"}, {"input": "25\r\n1 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 1 2 2 2 1 1 2 2 1\r\n", "output": "13\r\n1 10 17 22 2 5 7 9 13 15 18 20 24 \r\n12\r\n4 11 21 25 3 6 8 12 14 16 19 23 \r\n"}, {"input": "27\r\n2 1 1 3 1 2 1 1 3 2 3 1 3 2 1 3 2 3 2 1 2 3 2 2 1 2 1\r\n", "output": "14\r\n2 5 8 15 25 1 10 17 21 24 4 11 16 22 \r\n13\r\n3 7 12 20 27 6 14 19 23 26 9 13 18 \r\n"}, {"input": "30\r\n2 2 2 3 4 3 4 4 3 2 3 2 2 4 1 4 2 4 2 2 1 4 3 2 1 3 1 1 4 3\r\n", "output": "15\r\n15 25 28 2 10 13 19 24 6 11 26 5 8 16 22 \r\n15\r\n21 27 1 3 12 17 20 4 9 23 30 7 14 18 29 \r\n"}, {"input": "100\r\n3 4 8 10 8 6 4 3 7 7 6 2 3 1 3 10 1 7 9 3 5 5 2 6 2 9 1 7 4 2 4 1 6 1 7 10 2 5 3 7 6 4 6 2 8 8 8 6 6 10 3 7 4 3 4 1 7 9 3 6 3 6 1 4 9 3 8 1 10 1 4 10 7 7 9 5 3 8 10 2 1 10 8 7 10 8 5 3 1 2 1 10 6 1 5 3 3 5 7 2\r\n", "output": "50\r\n14 27 34 63 70 89 94 23 30 44 90 1 13 20 51 59 66 88 97 7 31 53 64 21 38 87 98 11 33 43 49 62 9 18 35 52 73 84 3 45 47 78 86 26 65 4 36 69 79 85 \r\n50\r\n17 32 56 68 81 91 12 25 37 80 100 8 15 39 54 61 77 96 2 29 42 55 71 22 76 95 6 24 41 48 60 93 10 28 40 57 74 99 5 46 67 83 19 58 75 16 50 72 82 92 \r\n"}, {"input": "100\r\n85 50 17 89 65 89 5 20 86 26 16 21 85 14 44 31 87 31 6 2 48 67 8 80 79 1 48 36 97 1 5 30 79 50 78 12 2 55 76 100 54 40 26 81 97 96 68 56 87 14 51 17 54 37 52 33 69 62 38 63 74 15 62 78 9 19 67 2 60 58 93 60 18 96 55 48 34 7 79 82 32 58 90 67 20 50 27 15 7 89 98 10 11 15 99 49 4 51 77 52\r\n", "output": "50\r\n26 20 68 7 19 89 65 93 14 62 94 3 73 8 12 43 32 18 56 28 59 15 27 96 34 51 55 41 38 48 82 72 63 5 67 47 61 99 64 33 24 80 13 17 4 90 71 74 45 95 \r\n50\r\n30 37 97 31 78 23 92 36 50 88 11 52 66 85 10 87 16 81 77 54 42 21 76 2 86 98 100 53 75 70 69 58 60 22 84 57 39 35 25 79 44 1 9 49 6 83 46 29 91 40 \r\n"}, {"input": "100\r\n2382 7572 9578 1364 2325 2929 7670 5574 2836 2440 6553 1751 929 8785 6894 9373 9308 7338 6380 9541 9951 6785 8993 9942 5087 7544 6582 7139 8458 7424 9759 8199 9464 8817 7625 6200 4955 9373 9500 3062 849 4210 9337 5466 2190 8150 4971 3145 869 5675 1975 161 1998 378 5229 9000 8958 761 358 434 7636 8295 4406 73 375 812 2473 3652 9067 3052 5287 2850 6987 5442 2625 8894 8733 791 9763 5258 8259 9530 2050 7334 2118 2726 8221 5527 8827 1585 8334 8898 6399 6217 7400 2576 5164 9063 6247 9433\r\n", "output": "50\r\n64 59 54 58 66 49 4 12 53 85 5 10 96 86 72 70 48 42 37 25 55 71 44 8 36 99 93 27 15 28 18 30 2 61 46 87 62 29 14 89 92 23 98 17 16 100 39 20 31 24 \r\n50\r\n52 65 60 78 41 13 90 51 83 45 1 67 75 9 6 40 68 63 47 97 80 74 88 50 94 19 11 22 73 84 95 26 35 7 32 81 91 77 34 76 57 56 69 43 38 33 82 3 79 21 \r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "3\r\n10 10 10\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "3\r\n5 10 10\r\n", "output": "2\r\n1 3 \r\n1\r\n2 \r\n"}, {"input": "5\r\n6 1 1 1 1\r\n", "output": "3\r\n2 4 1 \r\n2\r\n3 5 \r\n"}, {"input": "5\r\n1 100 2 200 3\r\n", "output": "3\r\n1 5 4 \r\n2\r\n3 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().split()))
max_a = max(a)
with open(submission_path) as f:
lines = [line.strip() for line in f.readlines()]
if len(lines) != 4:
print(0)
return
try:
x = int(lines[0])
p = list(map(int, lines[1].split()))
y = int(lines[2])
q = list(map(int, lines[3].split()))
except:
print(0)
return
if x + y != n or abs(x - y) > 1:
print(0)
return
if len(p) != x or len(q) != y:
print(0)
return
all_indices = set(p) | set(q)
if len(all_indices) != x + y or any(i < 1 or i > n for i in all_indices):
print(0)
return
sum_p = sum(a[i-1] for i in p)
sum_q = sum(a[i-1] for i in q)
if abs(sum_p - sum_q) > max_a:
print(0)
return
print(1)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
297/A
|
297
|
A
|
Python 3
|
TESTS
| 2
| 186
| 307,200
|
42459334
|
def zeroEraser(a):
if a.count('0') == len(a):
return ""
x = a[::-1]
for i in range(len(x)):
if x[i] != '0':
y = len(x)-i
break
return a[:y]
def possible(a,b):
x = zeroEraser(a)
y = zeroEraser(b)
if len(x) < len(y):
return "NO"
elif y == x[len(x)-len(y):] :
return "YES"
else:
return "NO"
a = input()
b = input()
print(possible(a,b))
| 79
| 186
| 0
|
142741181
|
a=input()
b=input()
a1=a.count('1')
a1+=a1&1
if(a1>=b.count('1')):
print("YES")
else:
print("NO")
|
Codeforces Round 180 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Parity Game
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations:
- Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$.
- Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty.
You can use as many operations as you want. The problem is, is it possible to turn a into b?
The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
|
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
| null |
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
|
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
| 1,700
|
["constructive algorithms"]
| 79
|
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
70/B
|
70
|
B
|
PyPy 3-64
|
TESTS
| 4
| 77
| 2,867,200
|
200489304
|
import re
def func():
n=int(input())
s=input()+'#'
msgs,found,t=[],False,''
for e in s:
if found and e == ' ':
found = False
continue
if e == '.' or e =='!' or e == '?' or e == '#':
if e != '#': t += e
if t: msgs.append(t)
found=True
t = ''
else: t += e
for msg in msgs:
if len(msg) > n:
print('Impossible')
return
print(len(msgs))
def init(flag=True):
for _ in range(int(input()) if flag else 1):
func()
if __name__ == '__main__':
init(False)
| 48
| 233
| 2,150,400
|
101376573
|
import re
n=int(input())
ans,sumL=1,0
for s in re.split("[.?!]", input()):
s=s.strip()+'.'
L=len(s)
if L>1:
if L>n:
print("Impossible")
exit()
if sumL+L+(sumL>0) > n:
ans+=1
sumL=L
else:
sumL=sumL+L+(sumL>0)
print(ans)
|
Codeforces Beta Round 64
|
CF
| 2,011
| 1
| 256
|
Text Messaging
|
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split!
Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space).
The little walrus's text looks in the following manner:
SPACE stands for the symbol of a space.
So, how many messages did Fangy send?
|
The first line contains an integer n, which is the size of one message (2 ≤ n ≤ 255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.
|
On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.
| null |
Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
|
[{"input": "25\nHello. I am a little walrus.", "output": "2"}, {"input": "2\nHow are you?", "output": "Impossible"}, {"input": "19\nHello! Do you like fish? Why?", "output": "3"}]
| 1,600
|
["expression parsing", "greedy", "strings"]
| 48
|
[{"input": "25\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "2\r\nHow are you?\r\n", "output": "Impossible\r\n"}, {"input": "19\r\nHello! Do you like fish? Why?\r\n", "output": "3\r\n"}, {"input": "4\r\na. A.\r\n", "output": "2\r\n"}, {"input": "146\r\niIQVkDsPqzAJyBrtHk EhBSN gzDoigItCMzETerb cIbXhTPbKYMdMoYqyFTEN. qcrrseVwmaZEiQUQoGT SUyErST lJDejKkjqTdoUrHR tsZYDyI? pmuNNHYqQUISxdZfWOB XdEECvNz hnNmVfODaIC qjhRIEPAlEsQBxo apZ! gCpqoiUFIwWLBOmYubywj qJojFVhLd dCpovICpXHfgihydEOoij?\r\n", "output": "2\r\n"}, {"input": "118\r\ngweVo bjbEKaZQw PpSi AWOYt sQSvAHNRswh vUaGuLbtExNECz! USsQxMCjaGOmUESwHvyY SshkERibaWkmNLSZOtWZy FFTUWQgekPRjLRetAdSFt! sIhcimZTisFvndrYioLF HetLn wjoaDUKfbkagupl QdYb fFiV GNqBygStKQw. XLiYZEOGnTLSHmCwktEr pVBePMoRGopNt LdEujxuxzmlbycQdR?\r\n", "output": "4\r\n"}, {"input": "16\r\nAbacaba. Abacaba. abacaba. abacab.\r\n", "output": "3\r\n"}, {"input": "21\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "16\r\nAbacaba. Abacab. abacaba. abacaba.\r\n", "output": "3\r\n"}, {"input": "10\r\nhello! how are you?\r\n", "output": "Impossible\r\n"}, {"input": "5\r\nabc. abcd.\r\n", "output": "2\r\n"}, {"input": "16\r\nabacaba. abacab. Abacaba. Abacaba.\r\n", "output": "3\r\n"}, {"input": "5\r\na. b. c. d.\r\n", "output": "2\r\n"}, {"input": "8\r\nabc! ab.\r\n", "output": "1\r\n"}, {"input": "2\r\na. b.\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
333/B
|
333
|
B
|
Python 3
|
TESTS
| 5
| 122
| 0
|
6010622
|
instr = input()
tmplist = instr.split()
n = (int)(tmplist[0])
chizu = [ [0 for i in range(n)] for j in range(n)]
m = (int)(tmplist[1])
for i in range(m):
instr = input()
tmplist = instr.split()
x = (int)(tmplist[0])
y = (int)(tmplist[1])
chizu[x-1][y-1] = 1
count = 0
#for i in range(n):
# for j in range(n):
# print( "chizu[",i,"][",j,"]=",chizu[i][j] )
lx = []
for i in range(1,n-1):
goodblock = 0
#print ( "i=", i )
for j in range(n):
if chizu[i][j] == 0:
goodblock += 1
if goodblock == n:
count += 1
lx.append(goodblock)
#print( "count=", count )
ly = []
for i in range(1,n-1):
goodblock = 0
#print ( "i=", i )
for j in range(n):
if chizu[j][i] == 0:
goodblock += 1
if goodblock == n:
count += 1
ly.append(goodblock)
for i in range(n-2):
#print (lx[i],ly[i])
if lx[i] == n and ly[i] == n:
count -= 1
print (count)
| 20
| 716
| 0
|
5419181
|
n, m = map(int, input().split())
k = n + 1
a, b = [0] * k, [0] * k
a[0] = a[1] = a[n] = b[0] = b[1] = b[n] = 1
for i in range(m):
x, y = map(int, input().split())
a[x] = b[y] = 1
s = a.count(0) + b.count(0)
if n & 1 and 0 == a[k // 2] == b[k // 2]: s -= 1
print(s)
|
Codeforces Round 194 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Chips
|
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:
- At least one of the chips at least once fell to the banned cell.
- At least once two chips were on the same cell.
- At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.
|
The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.
Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.
|
Print a single integer — the maximum points Gerald can earn in this game.
| null |
In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
|
[{"input": "3 1\n2 2", "output": "0"}, {"input": "3 0", "output": "1"}, {"input": "4 3\n3 1\n3 2\n3 3", "output": "1"}]
| 1,800
|
["greedy"]
| 20
|
[{"input": "3 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "3 0\r\n", "output": "1\r\n"}, {"input": "4 3\r\n3 1\r\n3 2\r\n3 3\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 3\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1\r\n3 2\r\n", "output": "4\r\n"}, {"input": "5 1\r\n2 3\r\n", "output": "4\r\n"}, {"input": "1000 0\r\n", "output": "1996\r\n"}, {"input": "999 0\r\n", "output": "1993\r\n"}, {"input": "5 5\r\n3 2\r\n5 4\r\n3 3\r\n2 3\r\n1 2\r\n", "output": "1\r\n"}, {"input": "5 5\r\n3 2\r\n1 4\r\n5 1\r\n4 5\r\n3 1\r\n", "output": "2\r\n"}, {"input": "5 5\r\n2 2\r\n5 3\r\n2 3\r\n5 1\r\n4 4\r\n", "output": "1\r\n"}, {"input": "6 5\r\n2 6\r\n6 5\r\n3 1\r\n2 2\r\n1 2\r\n", "output": "4\r\n"}, {"input": "6 5\r\n2 6\r\n5 2\r\n4 3\r\n6 6\r\n2 5\r\n", "output": "2\r\n"}, {"input": "6 5\r\n2 1\r\n6 4\r\n2 2\r\n4 3\r\n4 1\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
780/B
|
780
|
B
|
PyPy 3
|
TESTS
| 35
| 218
| 11,980,800
|
113183046
|
###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
from __future__ import print_function # for PyPy2
# from itertools import permutations as perm
# from functools import cmp_to_key # for adding custom comparator
# from fractions import Fraction
from collections import *
from sys import stdin
# from bisect import *
# from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
n, = gil()
p, v = gil(), gil()
def inter(l, r, m, n):
if l > m:
return inter(m, n, l, r)
# l <= m
return max(l, m), min(r, n)
def isPos(t):
d = v[0]*t
l, r = p[0] - d, p[0] + d
for i in range(1, n):
d = v[i]*t
l, r = inter(l, r, p[i] - d, p[i] + d)
if r < l : return False
return r >= l
d, l, r = 1e-6, 0, 2e9
ans = None
while l <= r:
mid = (l+r)/2
if isPos(mid):
r = mid - d
ans = mid
else:
l = mid + d
print(round(ans, 6))
| 46
| 109
| 16,179,200
|
228323942
|
n=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l=0
r=1e9
for _ in range(80):
m=(l+r)/2
a,b=float("-inf"),float("inf")
for i,j in zip(l1,l2):
a=max(a,i-j*m)
b=min(b,i+j*m)
if a<=b:
r=m
else:
l=m
l=round(l,6)
print(l)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 5
| 256
|
The Meeting Place Cannot Be Changed
|
The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.
At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.
You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.
|
The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.
The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.
|
Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.
Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$ holds.
| null |
In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.
|
[{"input": "3\n7 1 3\n1 2 1", "output": "2.000000000000"}, {"input": "4\n5 10 3 2\n2 3 2 4", "output": "1.400000000000"}]
| 1,600
|
["binary search"]
| 46
|
[{"input": "3\r\n7 1 3\r\n1 2 1\r\n", "output": "2.000000000000\r\n"}, {"input": "4\r\n5 10 3 2\r\n2 3 2 4\r\n", "output": "1.400000000000\r\n"}, {"input": "3\r\n1 1000000000 2\r\n1 2 1000000000\r\n", "output": "333333332.999999999971\r\n"}, {"input": "2\r\n4 5\r\n10 8\r\n", "output": "0.055555555556\r\n"}, {"input": "4\r\n14 12 10 17\r\n8 6 5 10\r\n", "output": "0.466666666667\r\n"}, {"input": "5\r\n1 15 61 29 43\r\n15 11 19 19 19\r\n", "output": "1.764705882353\r\n"}, {"input": "10\r\n20 11 17 38 15 27 2 40 24 37\r\n22 30 22 30 28 16 7 20 22 13\r\n", "output": "1.750000000000\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n1 1\r\n", "output": "0.000000000000\r\n"}, {"input": "3\r\n1 1 1\r\n1 1 1\r\n", "output": "0.000000000000\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read correct output
with open(output_path) as f:
correct_line = f.read().strip()
try:
b = float(correct_line)
except:
print(0)
return
# Read submission output
with open(submission_path) as f:
sub_line = f.read().strip()
try:
a = float(sub_line)
except:
print(0)
return
if a < 0:
print(0)
return
# Compute the error
denominator = max(1.0, b)
diff = abs(a - b)
error = diff / denominator
if error <= 1e-6:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
70/B
|
70
|
B
|
Python 3
|
TESTS
| 10
| 46
| 0
|
204135130
|
n = int(input())
a = input()
flag = last_true = count = i = lim = 0
while (i < len(a)):
if (a[i] == '.' or a[i] == '?' or a[i] == '!'): last_true = i
lim += 1
i += 1
if (lim == n):
if (last_true != 0): i = last_true + 1; lim = 0; last_true = 0; count += 1
else: print('Impossible'); flag = 1; break
if (not flag): print(count + 1)
| 48
| 46
| 0
|
230067445
|
n = int(input()) ;
s = input() ; count = 0 ; ans = 0
i = 0 ; L = []
while i < len(s):
if s[i] not in ['.','?','!'] : count += 1 ; i += 1
elif s[i] in ['.','?','!'] :
L.append(count + 1) ; ans += 1 ; count = 0 ; i += 2
#print(L,n)
i = 1 ; a = len(L)
#print(a)
flag = False
if L[0] > n : flag = True ; print("Impossible")
else :
while i < a:
if L[i] > n : print("Impossible") ; flag = True ; break
elif L[i] + L[i - 1] + 1 <= n : L[i - 1] += L[i] + 1 ; L.pop(i) ; a -= 1
else : i += 1
#print(L)
if not flag :
print(len(L))
|
Codeforces Beta Round 64
|
CF
| 2,011
| 1
| 256
|
Text Messaging
|
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split!
Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space).
The little walrus's text looks in the following manner:
SPACE stands for the symbol of a space.
So, how many messages did Fangy send?
|
The first line contains an integer n, which is the size of one message (2 ≤ n ≤ 255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.
|
On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.
| null |
Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
|
[{"input": "25\nHello. I am a little walrus.", "output": "2"}, {"input": "2\nHow are you?", "output": "Impossible"}, {"input": "19\nHello! Do you like fish? Why?", "output": "3"}]
| 1,600
|
["expression parsing", "greedy", "strings"]
| 48
|
[{"input": "25\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "2\r\nHow are you?\r\n", "output": "Impossible\r\n"}, {"input": "19\r\nHello! Do you like fish? Why?\r\n", "output": "3\r\n"}, {"input": "4\r\na. A.\r\n", "output": "2\r\n"}, {"input": "146\r\niIQVkDsPqzAJyBrtHk EhBSN gzDoigItCMzETerb cIbXhTPbKYMdMoYqyFTEN. qcrrseVwmaZEiQUQoGT SUyErST lJDejKkjqTdoUrHR tsZYDyI? pmuNNHYqQUISxdZfWOB XdEECvNz hnNmVfODaIC qjhRIEPAlEsQBxo apZ! gCpqoiUFIwWLBOmYubywj qJojFVhLd dCpovICpXHfgihydEOoij?\r\n", "output": "2\r\n"}, {"input": "118\r\ngweVo bjbEKaZQw PpSi AWOYt sQSvAHNRswh vUaGuLbtExNECz! USsQxMCjaGOmUESwHvyY SshkERibaWkmNLSZOtWZy FFTUWQgekPRjLRetAdSFt! sIhcimZTisFvndrYioLF HetLn wjoaDUKfbkagupl QdYb fFiV GNqBygStKQw. XLiYZEOGnTLSHmCwktEr pVBePMoRGopNt LdEujxuxzmlbycQdR?\r\n", "output": "4\r\n"}, {"input": "16\r\nAbacaba. Abacaba. abacaba. abacab.\r\n", "output": "3\r\n"}, {"input": "21\r\nHello. I am a little walrus.\r\n", "output": "2\r\n"}, {"input": "16\r\nAbacaba. Abacab. abacaba. abacaba.\r\n", "output": "3\r\n"}, {"input": "10\r\nhello! how are you?\r\n", "output": "Impossible\r\n"}, {"input": "5\r\nabc. abcd.\r\n", "output": "2\r\n"}, {"input": "16\r\nabacaba. abacab. Abacaba. Abacaba.\r\n", "output": "3\r\n"}, {"input": "5\r\na. b. c. d.\r\n", "output": "2\r\n"}, {"input": "8\r\nabc! ab.\r\n", "output": "1\r\n"}, {"input": "2\r\na. b.\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
333/B
|
333
|
B
|
Python 3
|
TESTS
| 5
| 122
| 0
|
6455213
|
n, m = map(int,input().split())
set_x = {1, n}
set_y = {1, n}
set_total = {x for x in range(1, n+1)}
for i in range(0, m):
x,y = map(int,input().split())
set_x.add(x)
set_y.add(y)
ava_x = set_total - set_x
ava_y = set_total - set_y
print(max(len(ava_x) + len(ava_y - ava_x), len(ava_y) + len(ava_x-ava_y) ))
| 20
| 748
| 0
|
4189307
|
n, m = map(int, input().split())
used = [1] * 2 * n
for i in range(m):
x, y = map(int, input().split())
used[x - 1] = used[n + y - 1] = 0
if n % 2 and used[n // 2]:
used[n // 2 + n] = 0
res = sum(used)
for i in [0, n - 1, n, 2 * n - 1]:
res -= used[i]
print(res)
|
Codeforces Round 194 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Chips
|
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:
- At least one of the chips at least once fell to the banned cell.
- At least once two chips were on the same cell.
- At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.
|
The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.
Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.
|
Print a single integer — the maximum points Gerald can earn in this game.
| null |
In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
|
[{"input": "3 1\n2 2", "output": "0"}, {"input": "3 0", "output": "1"}, {"input": "4 3\n3 1\n3 2\n3 3", "output": "1"}]
| 1,800
|
["greedy"]
| 20
|
[{"input": "3 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "3 0\r\n", "output": "1\r\n"}, {"input": "4 3\r\n3 1\r\n3 2\r\n3 3\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 3\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1\r\n3 2\r\n", "output": "4\r\n"}, {"input": "5 1\r\n2 3\r\n", "output": "4\r\n"}, {"input": "1000 0\r\n", "output": "1996\r\n"}, {"input": "999 0\r\n", "output": "1993\r\n"}, {"input": "5 5\r\n3 2\r\n5 4\r\n3 3\r\n2 3\r\n1 2\r\n", "output": "1\r\n"}, {"input": "5 5\r\n3 2\r\n1 4\r\n5 1\r\n4 5\r\n3 1\r\n", "output": "2\r\n"}, {"input": "5 5\r\n2 2\r\n5 3\r\n2 3\r\n5 1\r\n4 4\r\n", "output": "1\r\n"}, {"input": "6 5\r\n2 6\r\n6 5\r\n3 1\r\n2 2\r\n1 2\r\n", "output": "4\r\n"}, {"input": "6 5\r\n2 6\r\n5 2\r\n4 3\r\n6 6\r\n2 5\r\n", "output": "2\r\n"}, {"input": "6 5\r\n2 1\r\n6 4\r\n2 2\r\n4 3\r\n4 1\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
340/B
|
340
|
B
|
Python 3
|
TESTS
| 22
| 124
| 0
|
4408105
|
# calculate convex of polygon v.
# v is list of complexes stand for points.
def convex(v, eps=1e-8):
# fetch the seed point
v.sort(key=lambda x:(x.real,x.imag))
v = v[0:1] + sorted(v[1:], key=lambda x:(x-v[0]).imag/abs(x-v[0]))
n = 1
for i in range(2, len(v)):
while n > 1 and ((v[n]-v[n-1])*(v[i]-v[n]).conjugate()).imag>-eps:
n -= 1
else:
n += 1
v[n] = v[i]
v[n+1:] = []
return v
# calculate the area of a polygon v, anti-clockwise.
# v is list of complexes stand for points.
def area(v):
ans = 0
for i in range(2, len(v)):
ans += ((v[i]-v[i-1])*(v[i-1]-v[0]).conjugate()).imag
return ans * 0.5
n = int(input())
v = [complex(*tuple(map(int, input().split()))) for i in range(0, n)]
v = convex(v)
n = len(v)
ans = 0
def tri(i, j, k): return abs(((v[i]-v[j])*(v[i]-v[k]).conjugate()).imag) * 0.5
for i in range(0, n):
for j in range(i+2, n):
if i == 0 and j == n-1: continue
l = i + 1
r = j
while l < r-1:
k = l+r>>1
if tri(i, j, k) > tri(i, j, k-1):
l = k
else:
r = k
s1 = tri(i, j, l)
l = j - n + 1
r = i
while l < r-1:
k = l+r>>1
if tri(i, j, k) > tri(i, j, k-1):
l = k
else:
r = k
s2 = tri(i, j, l)
ans = max(ans, s1 + s2)
print(ans)
| 23
| 652
| 1,126,400
|
40484837
|
import sys
s, n = 0, int(input())
t = list(map(int, sys.stdin.read().split()))
p = [(t[2 * i], t[2 * i + 1]) for i in range(n)]
for x, i in enumerate(p, 1):
for j in p[x:]:
a = b = 0
for k in p:
d = (i[0] - k[0]) * (j[1] - k[1]) - (i[1] - k[1]) * (j[0] - k[0])
a, b = min(d, a), max(d, b)
if a and b: s = max(s, b - a)
print(s / 2)
|
Codeforces Round 198 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Maximal Area Quadrilateral
|
Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one which has all four vertices in the set of special points. Given the set of special points, please calculate the maximal area of a special quadrilateral.
|
The first line contains integer n (4 ≤ n ≤ 300). Each of the next n lines contains two integers: xi, yi ( - 1000 ≤ xi, yi ≤ 1000) — the cartesian coordinates of ith special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide.
|
Output a single real number — the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10 - 9.
| null |
In the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4·4 = 16.
|
[{"input": "5\n0 0\n0 4\n4 0\n4 4\n2 3", "output": "16.000000"}]
| 2,100
|
["brute force", "geometry"]
| 23
|
[{"input": "5\r\n0 0\r\n0 4\r\n4 0\r\n4 4\r\n2 3\r\n", "output": "16.000000"}, {"input": "10\r\n-6 -4\r\n-7 5\r\n-7 -7\r\n5 -7\r\n4 -9\r\n-6 7\r\n2 9\r\n-4 -6\r\n2 10\r\n-10 -4\r\n", "output": "166.000000"}, {"input": "4\r\n-3 3\r\n0 3\r\n-2 -1\r\n2 2\r\n", "output": "11.000000"}, {"input": "5\r\n-4 -3\r\n-3 -2\r\n3 3\r\n-1 2\r\n3 -3\r\n", "output": "29.500000"}, {"input": "6\r\n-4 -3\r\n-1 3\r\n0 0\r\n2 2\r\n2 1\r\n-3 1\r\n", "output": "15.000000"}, {"input": "7\r\n-2 -1\r\n4 3\r\n2 2\r\n-4 0\r\n-2 4\r\n0 0\r\n1 -3\r\n", "output": "32.500000"}, {"input": "4\r\n-874 606\r\n-996 -207\r\n897 847\r\n775 191\r\n", "output": "1261820.500000"}, {"input": "10\r\n156 -415\r\n879 198\r\n-250 -676\r\n-594 -433\r\n-207 368\r\n296 -641\r\n-387 -795\r\n143 -304\r\n-468 390\r\n-873 226\r\n", "output": "1129219.500000"}, {"input": "50\r\n-768 -243\r\n-741 -984\r\n-370 213\r\n-808 571\r\n-726 442\r\n234 452\r\n-105 -990\r\n-876 -278\r\n987 473\r\n-968 -531\r\n-274 -842\r\n259 -655\r\n-59 -555\r\n976 -396\r\n878 -85\r\n551 213\r\n675 599\r\n-990 -507\r\n1 48\r\n-147 919\r\n-218 798\r\n-191 928\r\n916 263\r\n-975 169\r\n567 -967\r\n394 16\r\n-224 915\r\n280 -613\r\n804 -877\r\n988 -576\r\n-256 -708\r\n757 546\r\n777 99\r\n-579 -608\r\n-102 1\r\n-309 636\r\n-24 -718\r\n644 -84\r\n111 -822\r\n-722 544\r\n78 595\r\n-194 716\r\n-409 -845\r\n-291 441\r\n388 379\r\n-950 277\r\n-718 359\r\n881 198\r\n198 670\r\n828 -820\r\n", "output": "2425414.000000"}, {"input": "4\r\n0 0\r\n0 5\r\n5 0\r\n1 1\r\n", "output": "10.000000"}]
| false
|
stdio
|
import sys
input_path, output_path, submission_output_path = sys.argv[1:4]
def read_float(path):
with open(path) as f:
content = f.read().strip()
parts = content.split()
if len(parts) != 1:
return None
try:
return float(parts[0])
except:
return None
E = read_float(output_path)
S = read_float(submission_output_path)
if E is None or S is None:
print(0)
sys.exit()
if E == 0.0:
if abs(S) <= 1e-9:
print(1)
else:
print(0)
else:
absolute = abs(E - S)
relative = absolute / abs(E)
if absolute <= 1e-9 or relative <= 1e-9:
print(1)
else:
print(0)
| true
|
501/C
|
501
|
C
|
PyPy 3-64
|
TESTS
| 2
| 62
| 28,979,200
|
135059892
|
from collections import deque
n = int(input())
L = []
S = []
for i in range(n):
u,v = map(int,input().split())
L.append([u,v])
q = deque()
d = dict()
for i in range(n):
d[i] = []
for i in range(n):
if L[i][0] == 1:
ad = L[i][1]
L[ad][0] -= 1
L[ad][1] ^= i
d[i].append(ad)
d[ad].append(i)
q.append(ad)
while (len(q)!=0):
x = q.popleft()
if L[x][0] == 0:
break
if L[x][0] == 1:
ad = L[i][1]
d[i].append(ad)
d[ad].append(i)
q.append(ad)
L[ad][0] -= 1
L[ad][1] ^= i
X = []
for c in d.keys():
for j in d[c]:
if j < c:
X.append([j,c])
print(len(X))
for c in X:
print(*c)
| 49
| 265
| 23,859,200
|
177732410
|
import sys
input = sys.stdin.readline
n = int(input())
d = {}
f = []
x = [0]*n
for i in range(n):
a, b = map(int, input().split())
if a == 0:
continue
d[i] = [a, b, i]
q = sorted([(d[i][1], d[i][2]) for i in d if d[i][0] == 1])
while q:
a, b = q.pop()
if x[a] == 0 and x[b] == 0:
x[b] = 1
if d[a][0] == 1:
x[d[a][2]] = 1
elif d[a][0] == 2:
q.append((d[a][1] ^ b, d[a][2]))
else:
d[a][0] -= 1
d[a][1] ^= b
f.append((b, d[a][2]))
print(len(f))
for i in f:
print(*i)
|
Codeforces Round 285 (Div. 2)
|
CF
| 2,015
| 1
| 256
|
Misha and Forest
|
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0).
Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.
|
The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph.
The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space.
|
In the first line print number m, the number of edges of the graph.
Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b).
Edges can be printed in any order; vertices of the edge can also be printed in any order.
| null |
The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
|
[{"input": "3\n2 3\n1 0\n1 0", "output": "2\n1 0\n2 0"}, {"input": "2\n1 1\n1 0", "output": "1\n0 1"}]
| 1,500
|
["constructive algorithms", "data structures", "greedy", "sortings", "trees"]
| 49
|
[{"input": "3\r\n2 3\r\n1 0\r\n1 0\r\n", "output": "2\r\n1 0\r\n2 0\r\n"}, {"input": "2\r\n1 1\r\n1 0\r\n", "output": "1\r\n0 1\r\n"}, {"input": "10\r\n3 13\r\n2 6\r\n1 5\r\n3 5\r\n1 3\r\n2 2\r\n2 6\r\n1 6\r\n1 3\r\n2 3\r\n", "output": "9\r\n2 5\r\n4 3\r\n7 6\r\n8 3\r\n5 0\r\n6 1\r\n3 9\r\n1 0\r\n9 0\r\n"}, {"input": "10\r\n1 2\r\n1 7\r\n1 0\r\n1 8\r\n0 0\r\n1 9\r\n0 0\r\n1 1\r\n1 3\r\n1 5\r\n", "output": "4\r\n0 2\r\n1 7\r\n3 8\r\n5 9\r\n"}, {"input": "5\r\n1 1\r\n2 2\r\n2 2\r\n2 6\r\n1 3\r\n", "output": "4\r\n0 1\r\n4 3\r\n1 2\r\n3 2\r\n"}, {"input": "10\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "11\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n1 8\r\n1 7\r\n0 0\r\n0 0\r\n", "output": "1\r\n7 8\r\n"}, {"input": "12\r\n0 0\r\n1 3\r\n0 0\r\n1 1\r\n0 0\r\n1 7\r\n0 0\r\n1 5\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "2\r\n1 3\r\n5 7\r\n"}, {"input": "13\r\n2 7\r\n0 0\r\n0 0\r\n2 11\r\n2 7\r\n2 14\r\n2 3\r\n2 1\r\n1 11\r\n3 15\r\n1 6\r\n2 11\r\n1 9\r\n", "output": "10\r\n8 11\r\n10 6\r\n12 9\r\n11 3\r\n6 9\r\n3 0\r\n9 5\r\n0 4\r\n5 7\r\n4 7\r\n"}, {"input": "14\r\n1 10\r\n1 9\r\n3 4\r\n1 2\r\n0 0\r\n1 11\r\n1 12\r\n1 10\r\n1 10\r\n2 10\r\n3 15\r\n3 14\r\n2 4\r\n0 0\r\n", "output": "10\r\n0 10\r\n1 9\r\n3 2\r\n5 11\r\n6 12\r\n7 10\r\n8 10\r\n9 11\r\n12 2\r\n11 2\r\n"}, {"input": "15\r\n0 0\r\n1 6\r\n3 2\r\n1 13\r\n2 15\r\n2 5\r\n1 1\r\n2 1\r\n1 4\r\n1 2\r\n0 0\r\n1 14\r\n0 0\r\n1 3\r\n2 9\r\n", "output": "9\r\n1 6\r\n3 13\r\n8 4\r\n9 2\r\n11 14\r\n4 7\r\n14 2\r\n7 5\r\n2 5\r\n"}, {"input": "16\r\n1 10\r\n2 13\r\n1 13\r\n2 1\r\n1 3\r\n2 2\r\n1 14\r\n0 0\r\n1 1\r\n1 14\r\n1 0\r\n0 0\r\n0 0\r\n1 2\r\n2 15\r\n0 0\r\n", "output": "8\r\n0 10\r\n2 13\r\n4 3\r\n6 14\r\n8 1\r\n9 14\r\n3 5\r\n1 5\r\n"}, {"input": "17\r\n0 0\r\n2 6\r\n0 0\r\n2 11\r\n0 0\r\n1 13\r\n1 3\r\n1 10\r\n0 0\r\n1 1\r\n1 7\r\n0 0\r\n0 0\r\n3 9\r\n0 0\r\n2 12\r\n0 0\r\n", "output": "7\r\n5 13\r\n6 3\r\n7 10\r\n9 1\r\n3 13\r\n1 15\r\n13 15\r\n"}, {"input": "18\r\n0 0\r\n0 0\r\n2 19\r\n1 2\r\n2 29\r\n0 0\r\n1 7\r\n1 6\r\n1 12\r\n1 13\r\n0 0\r\n1 12\r\n4 23\r\n1 9\r\n0 0\r\n0 0\r\n2 14\r\n1 4\r\n", "output": "9\r\n3 2\r\n6 7\r\n8 12\r\n9 13\r\n11 12\r\n17 4\r\n2 16\r\n4 12\r\n16 12\r\n"}, {"input": "19\r\n1 13\r\n0 0\r\n1 9\r\n1 11\r\n1 11\r\n2 3\r\n2 30\r\n0 0\r\n1 5\r\n1 2\r\n0 0\r\n5 29\r\n1 6\r\n2 11\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n2 13\r\n", "output": "10\r\n0 13\r\n2 9\r\n3 11\r\n4 11\r\n8 5\r\n12 6\r\n13 11\r\n5 11\r\n6 18\r\n11 18\r\n"}, {"input": "20\r\n0 0\r\n2 15\r\n0 0\r\n2 7\r\n1 1\r\n0 0\r\n0 0\r\n0 0\r\n1 9\r\n2 11\r\n0 0\r\n1 1\r\n0 0\r\n0 0\r\n1 3\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n0 0\r\n", "output": "5\r\n4 1\r\n8 9\r\n11 1\r\n14 3\r\n9 3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2] # not used
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
lines = [line.strip() for line in f if line.strip()]
n = int(lines[0])
degrees = []
s_list = []
for line in lines[1:n+1]:
d, s = map(int, line.split())
degrees.append(d)
s_list.append(s)
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f if line.strip()]
if not sub_lines:
print(0)
return
try:
m = int(sub_lines[0])
except:
print(0)
return
edges = []
for line in sub_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
a = int(parts[0])
b = int(parts[1])
except:
print(0)
return
edges.append( (a, b) )
# Check sum_deg is even and m matches
sum_deg = sum(degrees)
if sum_deg % 2 != 0:
print(0)
return
if m != sum_deg // 2:
print(0)
return
# Check edges count
if len(edges) != m:
print(0)
return
# Check each edge is valid and no duplicates
seen = set()
for a, b in edges:
if a == b:
print(0)
return
if not (0 <= a < n) or not (0 <= b < n):
print(0)
return
u, v = min(a, b), max(a, b)
if (u, v) in seen:
print(0)
return
seen.add( (u, v) )
# Build adjacency list
adj = [[] for _ in range(n)]
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
# Check degrees and XOR sums
for v in range(n):
if len(adj[v]) != degrees[v]:
print(0)
return
xor_sum = 0
for nei in adj[v]:
xor_sum ^= nei
if xor_sum != s_list[v]:
print(0)
return
# Check for cycles using Union-Find
parent = list(range(n))
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
for a, b in edges:
root_a = find(a)
root_b = find(b)
if root_a == root_b:
print(0)
return
parent[root_a] = root_b
# All checks passed
print(1)
if __name__ == "__main__":
main()
| true
|
501/B
|
501
|
B
|
Python 3
|
TESTS
| 44
| 233
| 1,228,800
|
45901799
|
import copy
n = input()
E = list()
class Node:
def __init__(self, nodo):
self.padre = None
self.nodo = nodo
self.hijo = None
def __repr__(self):
return self.nodo
#return "nodo: {0} | padre: {1} | hijo: {2}".format(self.nodo,self.padre.nodo if self.padre else None, self.hijo.nodo if self.hijo else None)
node_list = []
node_list_letter = []
for i in range(0, int(n)):
e = input()
e = e.split(" ")
E.append(e)
if e[0] not in node_list_letter:
new_node = Node(e[0])
node_list.append(new_node)
node_list_letter.append(e[0])
else:
new_node = node_list[node_list_letter.index(e[0])]
if e[1] not in node_list_letter:
new_child = Node(e[1])
node_list.append(new_child)
node_list_letter.append(e[1])
else:
new_child = node_list[node_list_letter.index(e[1])]
new_node.hijo = new_child
new_child.padre = new_node
check = []
def buscar_padre(x):
check.append(x)
if x.padre:
x = buscar_padre(x.padre)
return x
def buscar_hijo(x):
check.append(x)
if x.hijo:
x = buscar_hijo(x.hijo)
return x
#E_ = copy.copy(E)
#for i in node_list:
#print("--nodo--")
#print(i)
#print("--padre--")
#print(buscar_padre(i))
#print("--hijo--")
#print(buscar_hijo(i))
#print("------------")
output = []
iter = 0
for i in node_list:
iter+= 1
if i not in check:
try:
padre = buscar_padre(i)
hijo = buscar_hijo(i)
if [padre,hijo] not in output:
output.append([padre,hijo])
except Exception as e:
print(i, iter)
print("chup")
print(len(output))
for i in output:
print(i[0].nodo + " " + i[1].nodo)
| 49
| 46
| 0
|
140723840
|
parent = dict()
def find(name):
while parent[name] != name:
parent[name] = parent[parent[name]]
name = parent[name]
return parent[name]
queries = []
for _ in range(int(input())):
old, new = input().split()
queries.append((old, new))
parent[new] = new
parent[old] = new
newest = set()
ans = dict()
for old, new in queries:
z = find(old)
if z in newest:
continue
newest.add(z)
ans[old] = z
print(len(ans))
for k, v in ans.items():
print(k, v)
|
Codeforces Round 285 (Div. 2)
|
CF
| 2,015
| 1
| 256
|
Misha and Changing Handles
|
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.
Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.
|
The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.
Next q lines contain the descriptions of the requests, one per line.
Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.
The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.
|
In the first line output the integer n — the number of users that changed their handles at least once.
In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.
Each user who changes the handle must occur exactly once in this description.
| null | null |
[{"input": "5\nMisha ILoveCodeforces\nVasya Petrov\nPetrov VasyaPetrov123\nILoveCodeforces MikeMirzayanov\nPetya Ivanov", "output": "3\nPetya Ivanov\nMisha MikeMirzayanov\nVasya VasyaPetrov123"}]
| 1,100
|
["data structures", "dsu", "strings"]
| 49
|
[{"input": "5\r\nMisha ILoveCodeforces\r\nVasya Petrov\r\nPetrov VasyaPetrov123\r\nILoveCodeforces MikeMirzayanov\r\nPetya Ivanov\r\n", "output": "3\nPetya Ivanov\nMisha MikeMirzayanov\nVasya VasyaPetrov123\n"}, {"input": "1\r\nMisha Vasya\r\n", "output": "1\nMisha Vasya\n"}, {"input": "10\r\na b\r\nb c\r\nc d\r\nd e\r\ne f\r\nf g\r\ng h\r\nh i\r\ni j\r\nj k\r\n", "output": "1\na k\n"}, {"input": "5\r\n123abc abc123\r\nabc123 a1b2c3\r\na1b2c3 1A2B3C\r\n1 2\r\n2 Misha\r\n", "output": "2\n123abc 1A2B3C\n1 Misha\n"}, {"input": "8\r\nM F\r\nS D\r\n1 2\r\nF G\r\n2 R\r\nD Q\r\nQ W\r\nW e\r\n", "output": "3\nM G\n1 R\nS e\n"}, {"input": "17\r\nn5WhQ VCczxtxKwFio5U\r\nVCczxtxKwFio5U 1WMVGA17cd1LRcp4r\r\n1WMVGA17cd1LRcp4r SJl\r\nSJl D8bPUoIft5v1\r\nNAvvUgunbPZNCL9ZY2 jnLkarKYsotz\r\nD8bPUoIft5v1 DnDkHi7\r\njnLkarKYsotz GfjX109HSQ81gFEBJc\r\nGfjX109HSQ81gFEBJc kBJ0zrH78mveJ\r\nkBJ0zrH78mveJ 9DrAypYW\r\nDnDkHi7 3Wkho2PglMDaFQw\r\n3Wkho2PglMDaFQw pOqW\r\n9DrAypYW G3y0cXXGsWAh\r\npOqW yr1Ec\r\nG3y0cXXGsWAh HrmWWg5u4Hsy\r\nyr1Ec GkFeivXjQ01\r\nGkFeivXjQ01 mSsWgbCCZcotV4goiA\r\nHrmWWg5u4Hsy zkCmEV\r\n", "output": "2\nn5WhQ mSsWgbCCZcotV4goiA\nNAvvUgunbPZNCL9ZY2 zkCmEV\n"}, {"input": "10\r\nH1nauWCJOImtVqXk gWPMQ9DHv5CtkYp9lwm9\r\nSEj 2knOMLyzr\r\n0v69ijnAc S7d7zGTjmlku01Gv\r\n2knOMLyzr otGmEd\r\nacwr3TfMV7oCIp RUSVFa9TIWlLsd7SB\r\nS7d7zGTjmlku01Gv Gd6ZufVmQnBpi\r\nS1 WOJLpk\r\nWOJLpk Gu\r\nRUSVFa9TIWlLsd7SB RFawatGnbVB\r\notGmEd OTB1zKiOI\r\n", "output": "5\n0v69ijnAc Gd6ZufVmQnBpi\nS1 Gu\nSEj OTB1zKiOI\nacwr3TfMV7oCIp RFawatGnbVB\nH1nauWCJOImtVqXk gWPMQ9DHv5CtkYp9lwm9\n"}, {"input": "14\r\nTPdoztSZROpjZe z6F8bYFvnER4V5SP0n\r\n8Aa3PQY3hzHZTPEUz fhrZZPJ3iUS\r\nm9p888KaZAoQaO KNmdRSAlUVn8zXOM0\r\nAO s1VGWTCbHzM\r\ni 4F\r\nfhrZZPJ3iUS j0OVZQF6MvNcKN9xDZFJ\r\nDnlkXtaKNlYEI2ApBuwu DMA9i8ScKRxwhe72a3\r\nj0OVZQF6MvNcKN9xDZFJ DzjmeNqN0H4Teq0Awr\r\n4F wJcdxt1kwqfDeJ\r\nqxXlsa5t RHCL1K6aUyns\r\nr6WYbDaXt hEHw\r\nJ0Usg DKdKMFJ6tK8XA\r\nz6F8bYFvnER4V5SP0n 0alJ\r\nMijh2O6 qic8kXWuR6\r\n", "output": "10\nTPdoztSZROpjZe 0alJ\nJ0Usg DKdKMFJ6tK8XA\nDnlkXtaKNlYEI2ApBuwu DMA9i8ScKRxwhe72a3\n8Aa3PQY3hzHZTPEUz DzjmeNqN0H4Teq0Awr\nm9p888KaZAoQaO KNmdRSAlUVn8zXOM0\nqxXlsa5t RHCL1K6aUyns\nr6WYbDaXt hEHw\nMijh2O6 qic8kXWuR6\nAO s1VGWTCbHzM\ni wJcdxt1kwqfDeJ\n"}, {"input": "14\r\nHAXRxayyf1Dj1F0mT hjR4A8IQMb0nyBtqG\r\nWNuMJa5Jg05qkqZOrL noNkWXrSidHGwxgbQ\r\nmOitVy6W52s0FENMz6 oLUkLNfojssvLvb1t\r\nhjR4A8IQMb0nyBtqG oA7uBFu4Oo\r\noA7uBFu4Oo M450\r\nM450 LXEzO4\r\noLUkLNfojssvLvb1t YG5\r\nnoNkWXrSidHGwxgbQ L\r\nL YBWzu4W\r\nYBWzu4W ML\r\nML scVZE9m8JnH\r\nLXEzO4 Ne0oBPY0Iy\r\nscVZE9m8JnH GXhznv\r\nYG5 UY08abilYF1LaXj49hQ\r\n", "output": "3\nWNuMJa5Jg05qkqZOrL GXhznv\nHAXRxayyf1Dj1F0mT Ne0oBPY0Iy\nmOitVy6W52s0FENMz6 UY08abilYF1LaXj49hQ\n"}]
| false
|
stdio
|
import sys
def process_input(input_path):
current_to_original = {}
with open(input_path, 'r') as f:
lines = f.readlines()
q = int(lines[0].strip())
for line in lines[1:q+1]:
old, new = line.strip().split()
original = current_to_original.get(old, old)
if old in current_to_original:
del current_to_original[old]
current_to_original[new] = original
correct_pairs = set()
for current, original in current_to_original.items():
if original != current:
correct_pairs.add((original, current))
return correct_pairs, len(correct_pairs)
def read_submission_output(submission_path):
try:
with open(submission_path, 'r') as f:
lines = [line.strip() for line in f.readlines() if line.strip()]
if not lines:
return None, "No lines"
n = int(lines[0])
pairs = []
for line in lines[1:n+1]:
parts = line.split()
if len(parts) != 2:
return None, "Invalid line"
pairs.append((parts[0], parts[1]))
if len(pairs) != n:
return None, "Incorrect line count"
return pairs, None
except Exception as e:
return None, str(e)
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
correct_pairs, correct_n = process_input(input_path)
submission_pairs, error = read_submission_output(submission_path)
if error:
print(0)
return
with open(submission_path, 'r') as f:
lines = f.read().splitlines()
if not lines:
print(0)
return
submission_n_line = lines[0].strip()
if not submission_n_line.isdigit():
print(0)
return
submission_n = int(submission_n_line)
if submission_n != correct_n:
print(0)
return
if len(submission_pairs) != correct_n:
print(0)
return
submission_set = set(submission_pairs)
if len(submission_set) != correct_n:
print(0)
return
if submission_set == correct_pairs:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
333/B
|
333
|
B
|
PyPy 3
|
TESTS
| 5
| 248
| 23,142,400
|
25203621
|
n, m = map(int, input().split())
ans = 0
row = [True] * n
col = [True] * n
for x in range(m):
a, b = map(int, input().split())
row[a - 1] = False
col[b - 1] = False
for i in range(1, n - 1):
if row[i] or col[i]:
ans += 1
print (ans)
| 20
| 624
| 102,400
|
45983185
|
I = input
n,m = map(int , I().split())
b = [1] * n * 2
b[0] = b[n - 1] = b[n] = b[2 * n - 1] = 0
for i in range(m):
r , c = map(int , I().split())
b[r - 1] = b[n + c - 1] = 0
if n % 2 and b[n // 2] and b[n + n // 2] : b[n // 2] = 0
print(sum(b))
|
Codeforces Round 194 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Chips
|
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:
- At least one of the chips at least once fell to the banned cell.
- At least once two chips were on the same cell.
- At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.
|
The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.
Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.
|
Print a single integer — the maximum points Gerald can earn in this game.
| null |
In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
|
[{"input": "3 1\n2 2", "output": "0"}, {"input": "3 0", "output": "1"}, {"input": "4 3\n3 1\n3 2\n3 3", "output": "1"}]
| 1,800
|
["greedy"]
| 20
|
[{"input": "3 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "3 0\r\n", "output": "1\r\n"}, {"input": "4 3\r\n3 1\r\n3 2\r\n3 3\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 3\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1\r\n3 2\r\n", "output": "4\r\n"}, {"input": "5 1\r\n2 3\r\n", "output": "4\r\n"}, {"input": "1000 0\r\n", "output": "1996\r\n"}, {"input": "999 0\r\n", "output": "1993\r\n"}, {"input": "5 5\r\n3 2\r\n5 4\r\n3 3\r\n2 3\r\n1 2\r\n", "output": "1\r\n"}, {"input": "5 5\r\n3 2\r\n1 4\r\n5 1\r\n4 5\r\n3 1\r\n", "output": "2\r\n"}, {"input": "5 5\r\n2 2\r\n5 3\r\n2 3\r\n5 1\r\n4 4\r\n", "output": "1\r\n"}, {"input": "6 5\r\n2 6\r\n6 5\r\n3 1\r\n2 2\r\n1 2\r\n", "output": "4\r\n"}, {"input": "6 5\r\n2 6\r\n5 2\r\n4 3\r\n6 6\r\n2 5\r\n", "output": "2\r\n"}, {"input": "6 5\r\n2 1\r\n6 4\r\n2 2\r\n4 3\r\n4 1\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
472/D
|
472
|
D
|
Python 3
|
TESTS
| 3
| 30
| 0
|
193653682
|
n = int(input())
dist = [list(map(int, input().split())) for _ in range(n)]
# Check if diagonal elements are zero and distance matrix is symmetric
for i in range(n):
if dist[i][i] != 0:
print("NO")
exit()
for j in range(i + 1, n):
if dist[i][j] != dist[j][i]:
print("NO")
exit()
# Check if triangle inequality is satisfied
for i in range(n):
for j in range(i + 1, n):
for k in range(n):
if k != i and k != j and dist[i][j] > dist[i][k] + dist[k][j]:
print("NO")
exit()
print("YES")
| 47
| 545
| 57,036,800
|
177433089
|
from math import inf
from collections import *
import math, os, sys, heapq, bisect, random,threading
from functools import lru_cache
from itertools import *
import sys
def inp(): return sys.stdin.readline().rstrip("\r\n")
def out(var): sys.stdout.write(str(var)) # for fast output, always take string
def inpu(): return int(inp())
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
# #include <ext/pb_ds/assoc_container.hpp>
# #include <ext/pb_ds/tree_policy.hpp>
# using namespace __gnu_pbds;
# #define ll long long
# #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
# #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
M,M1=1000000007,998244353
def find(i,par):
if par[i]!=i:
par[i] = find(par[i],par)
return par[i]
def union(i,j,parent,rank):
if rank[i]>rank[j]:
parent[j] = i
elif rank[i]<rank[j]:
parent[i]= j
else:
parent[j] = i
rank[i] += 1
def main():
how_much_noob_I_am = 1
# how_much_noob_I_am = inpu()
for _ in range(1,how_much_noob_I_am+1):
n = int(input())
arr = []
for _ in range(n):
arr.append(lis())
if n == 1:
if arr[0][0] == 0:
print("YES")
else:
print("NO")
exit()
for i in range(n):
if arr[i][i] != 0:
print("NO")
exit()
for j in range(i + 1, n):
if arr[i][j] != arr[j][i] or arr[i][j] == 0:
print("NO")
exit()
for i in range(n):
r = int(i == 0)
for j in range(n):
if arr[i][j] < arr[i][r] and i != j:
r = j
for k in range(n):
if abs(arr[i][k] - arr[r][k]) != arr[r][i]:
print("NO")
exit()
print("YES")
if __name__ == '__main__':
# sys.setrecursio0nlimit(2*10**5+50)
# threading.stack_size(10**8)
# threading.Thread(target=main).start()
main()
|
Codeforces Round 270
|
CF
| 2,014
| 2
| 256
|
Design Tutorial: Inverse the Problem
|
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example.
Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).
|
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph.
Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.
|
If there exists such a tree, output "YES", otherwise output "NO".
| null |
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.
In the second example, it is impossible because d1, 1 should be 0, but it is 1.
In the third example, it is impossible because d1, 2 should equal d2, 1.
|
[{"input": "3\n0 2 7\n2 0 9\n7 9 0", "output": "YES"}, {"input": "3\n1 2 7\n2 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 2 2\n7 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 1 1\n1 0 1\n1 1 0", "output": "NO"}, {"input": "2\n0 0\n0 0", "output": "NO"}]
| 1,900
|
["dfs and similar", "dsu", "shortest paths", "trees"]
| 47
|
[{"input": "3\r\n0 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 2\r\n7 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 1\r\n1 0 1\r\n1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1000000000\r\n1000000000 0\r\n", "output": "YES\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "YES\r\n"}, {"input": "4\r\n0 3 7 6\r\n3 0 4 9\r\n7 4 0 2\r\n6 9 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 2\r\n1 0 2\r\n2 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 7\r\n2 0 10\r\n7 10 0\r\n", "output": "NO\r\n"}]
| false
|
stdio
| null | true
|
260/C
|
260
|
C
|
PyPy 3
|
TESTS
| 3
| 77
| 0
|
150457038
|
n,k = map(int, input().split())
arr = list(map(int, input().split()))
mn, mi = 1e9, -1
for i in range(n):
if arr[i] < mn:
mn = arr[i]
mi = i
k -= 1
ov = arr[mi]*n
side = k-mi if k>=mi else n-mi+k
ok = k
i=(mi+1)%n
while i != ok:
arr[i] -= 1
i = (i+1)%n
arr[i] -= 1
for i in range(n):
if i == mi:
continue
arr[i] -= arr[mi]
arr[mi] = side+ov
print(*arr)
| 38
| 249
| 33,280,000
|
123686604
|
n, x = map(int, input().split())
x = x - 1
l = list(map(int, input().split()))
min_index, min_value = -1, 10 ** 9 + 7
nb_seen = 0
cur = x
while nb_seen != n:
if min_value > l[cur]:
min_value = l[cur]
min_index = cur
cur = (n - 1 + cur) % n
nb_seen += 1
min_value = l[min_index]
for i in range(n):
l[i] -= min_value
nb_el = 0
cur = x
while l[cur] != 0:
l[cur] -= 1
cur = (n - 1 + cur) % n
nb_el += 1
l[min_index] += n * min_value + nb_el
print(' '.join(list(map(str, l))))
|
Codeforces Round 158 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Balls and Boxes
|
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
|
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.
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.
|
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
| null | null |
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
| 1,700
|
["constructive algorithms", "greedy", "implementation"]
| 38
|
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
| false
|
stdio
| null | true
|
690/B1
|
690
|
B1
|
Python 3
|
TESTS
| 0
| 61
| 7,065,600
|
37354590
|
n = int(input())
grid = []
flag = True
ans = -1
while(n):
n-=1
x = str(int(input()))
if(x!='0'):
grid.append(x)
for i in grid:
if(ans==-1 or len(i)==ans):
ans = len(i)
else:
flag = False
for i in range(0, len(grid)):
if(len(grid)-i-1 < i):
break
if(grid[i] != grid[len(grid)-i-1]):
flag = False
for i in range(0, len(grid)):
for j in range(0, len(grid)):
if(len(grid)-j-1 < j):
break
if(grid[i][j] != grid[i][len(grid[i])-j-1]):
flag = False
if(flag and ans!=-1):
print('Yes')
else:
print('No')
| 21
| 46
| 307,200
|
19011156
|
import sys, math
def bfs(x, y):
Q=[[x,y]]
ptr = 0
min_=[1000,1000]
max_=[0,0]
while ptr < len(Q):
v = Q[ptr]
ptr+=1
x = v[0]; y=v[1]
if matrix[x][y] == 4:
used[x][y] = 1
if x+1 < n:
if used[x+1][y]==0:
Q.append([x+1,y])
used[x+1][y]=1
if x-1 > -1:
if used[x-1][y]==0:
Q.append([x-1,y])
used[x-1][y]=1
if y+1 < n:
if used[x][y+1]==0:
Q.append([x,y+1])
used[x][y+1]=1
if y-1 > -1:
if used[x][y-1]==0:
Q.append([x,y-1])
used[x][y-1]=1
if x < min_[0] or (x==min_[0] and y < min_[1]):
min_=[x,y]
if x > max_[0] or (x==max_[0] and y > max_[1]):
max_=[x,y]
else:
used[x][y]=0
for i in range(min_[0], max_[0]+1):
for j in range(min_[1], max_[1]+1):
if matrix[i][j] != 4:
print('No')
sys.exit(0)
for i in range(n):
for j in range(n):
if used[i][j] == 0 and matrix[i][j] >= 3:
print('No')
sys.exit(0)
#print('h')
if min_[0] > 0:
w = min_[0] - 1
for j in range(min_[1], max_[1]+1):
used[w][j] = 1
if matrix[w][j] != 2:
print('No')
sys.exit(0)
#print('h')
if max_[0] < n-1:
w = max_[0] + 1
for j in range(min_[1], max_[1]+1):
used[w][j] = 1
if matrix[w][j] != 2:
print('No')
sys.exit(0)
if min_[1] > 0:
w = min_[1] - 1
for j in range(min_[0], max_[0]+1):
used[j][w] = 1
if matrix[j][w] != 2:
print('No')
sys.exit(0)
#print('h')
if max_[1] < n-1:
w = max_[1] + 1
for j in range(min_[0], max_[0]+1):
used[j][w] = 1
if matrix[j][w] != 2:
print('No')
sys.exit(0)
if min_[0] > 0 and min_[1] > 0:
x=min_[0]-1
y=min_[1]-1
if matrix[x][y]!=1:
print('No')
sys.exit(0)
used[x][y] =1
if max_[0] < n-1 and max_[1] < n-1:
x=max_[0]+1
y=max_[1]+1
if matrix[x][y]!=1:
print('No')
sys.exit(0)
used[x][y] =1
if min_[0] > 0 and max_[1] < n-1:
x=min_[0]-1
y=max_[1]+1
if matrix[x][y]!=1:
print('No')
sys.exit(0)
used[x][y] =1
if max_[0] < n-1 and min_[1] > 0:
x=max_[0]+1
y=min_[1]-1
if matrix[x][y]!=1:
print('No')
sys.exit(0)
used[x][y] =1
for i in range(n):
for j in range(n):
if used[i][j]==1:
continue
elif matrix[i][j] != 0:
print('No')
sys.exit(0)
print('Yes')
n=int(input())
matrix = [[0]*n for i in range(n)]
for i in range(n):
z=input()
for j in range(n):
matrix[i][j]=int(z[j])
used = [[0]*n for i in range(n)]
flag = 0
for i in range(n):
for j in range(n):
if matrix[i][j] == 4:
flag = 1
x = i
y = j
break
if flag:
break
if not flag:
print('No')
sys.exit(0)
bfs(x,y)
|
Helvetic Coding Contest 2016 online mirror (teams, unrated)
|
ICPC
| 2,016
| 2
| 256
|
Recover Polygon (easy)
|
The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.
Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle.
As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides).
|
The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4.
Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates (1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1).
|
The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.
| null |
The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the input.
|
[{"input": "6\n000000\n000000\n012100\n024200\n012100\n000000", "output": "Yes"}]
| 1,700
|
[]
| 21
|
[{"input": "6\r\n000000\r\n000000\r\n012100\r\n024200\r\n012100\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n012210\r\n024420\r\n012210\r\n000000\r\n000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000100\r\n001210\r\n002420\r\n001210\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "10\r\n0000000000\r\n0122210000\r\n0244420100\r\n0122210000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n", "output": "No\r\n"}, {"input": "10\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0000000000\r\n0012100000\r\n0024200000\r\n0012100000\r\n0000000000\r\n", "output": "Yes\r\n"}, {"input": "9\r\n000000000\r\n000000000\r\n012221000\r\n024442000\r\n012221000\r\n000000000\r\n000000000\r\n000000010\r\n000000000\r\n", "output": "No\r\n"}, {"input": "9\r\n000000000\r\n012222100\r\n024444200\r\n024444200\r\n024444200\r\n024444200\r\n024444200\r\n012222100\r\n000000000\r\n", "output": "Yes\r\n"}, {"input": "8\r\n00000000\r\n00001210\r\n00002420\r\n00002020\r\n00001210\r\n00000000\r\n00000000\r\n00000000\r\n", "output": "No\r\n"}, {"input": "8\r\n00000000\r\n00000000\r\n01210000\r\n02420000\r\n01210000\r\n00000000\r\n00000000\r\n00000000\r\n", "output": "Yes\r\n"}, {"input": "7\r\n0000000\r\n0000000\r\n0000000\r\n1122210\r\n0244420\r\n0122210\r\n0000000\r\n", "output": "No\r\n"}, {"input": "7\r\n0000000\r\n0012210\r\n0024420\r\n0012210\r\n0000000\r\n0000000\r\n0000000\r\n", "output": "Yes\r\n"}, {"input": "6\r\n000000\r\n000000\r\n001100\r\n001200\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n000000\r\n002200\r\n002200\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n000000\r\n003300\r\n003300\r\n000000\r\n000000\r\n", "output": "No\r\n"}, {"input": "6\r\n000000\r\n001100\r\n013310\r\n013310\r\n001100\r\n000000\r\n", "output": "No\r\n"}]
| false
|
stdio
| null | true
|
260/C
|
260
|
C
|
PyPy 3
|
TESTS
| 3
| 77
| 409,600
|
166459619
|
import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
######################################################################################
#--------------------------------------Input-----------------------------------------#
######################################################################################
def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]
######################################################################################
#--------------------------------------code here-------------------------------------#
######################################################################################
def solve():
n,x=values()
l=inlsts()
m=min(l)
indmin=l.index(min(l))
if x-1>=indmin:
tot=0
for i in range(n):
if i>indmin and i<x:l[i]-=m+1;tot+=m+1
else :l[i]-=m;tot+=m
# l[indmin]=(indmin+1)*m+(n-x)*m+(x-indmin)*(m+1)
l[indmin]=tot
else:
tot=0
for i in range(n):
if i>=x and i<=indmin:l[i]-=m;tot+=m
else :l[i]-=m+1;tot+=m+1
# l[indmin]=(x)*(m+1)+ (indmin-x+1)*m + (n-indmin-1)*(m+1)
l[indmin]=tot
print(*l)
solve()
| 38
| 265
| 11,980,800
|
150461302
|
n,k = map(int, input().split())
arr = list(map(int, input().split()))
m = min(arr)
k -= 1
ov = m*n
for i in range(n):
arr[i] -= m
while arr[k] > 0:
ov += 1
arr[k] -= 1
k = (k-1)%n
arr[k] = ov
print(*arr)
|
Codeforces Round 158 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Balls and Boxes
|
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
|
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.
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.
|
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
| null | null |
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
| 1,700
|
["constructive algorithms", "greedy", "implementation"]
| 38
|
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
| false
|
stdio
| null | true
|
260/C
|
260
|
C
|
Python 3
|
TESTS
| 3
| 77
| 0
|
38984724
|
n=[int(num) for num in input().split()]
x=[int(num) for num in input().split()]
i=x.index(min(x))
p=x[i]
j=(n[1]-i-1)%n[0]
x[i]=p*n[0]+j
t=i
i=i+1
while True:
if j!=0:
j=j-1
x[i%n[0]]=x[i%n[0]]-1-p
else:
x[i%n[0]]=x[i%n[0]]-p
if (i+1)%n[0]==t:
break
else:
i=(i+1)
for i in x:
print(i,end=' ')
| 38
| 295
| 15,257,600
|
86469034
|
def solve(n, x, seq):
x -= 1
_min = min(seq)
s = sum(seq)
idx = -1
for i in range(x, -1, -1):
if seq[i] == _min:
idx = i
break
else:
for j in range(n - 1, x, -1):
if seq[j] == _min:
idx = j
break
for i in range(n):
seq[i] -= _min
if idx <= x:
for j in range(idx + 1, x + 1):
seq[j] -= 1
else:
for j in range(x, -1, -1):
seq[j] -= 1
for j in range(n - 1, idx, -1):
seq[j] -= 1
seq[idx] = s - sum(seq)
return seq
def main():
n, x = map(int, input().split())
seq = [int(c) for c in input().split()]
ans = solve(n, x, seq)
print(*ans)
if __name__ == "__main__":
main()
|
Codeforces Round 158 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Balls and Boxes
|
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
|
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.
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.
|
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
| null | null |
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
| 1,700
|
["constructive algorithms", "greedy", "implementation"]
| 38
|
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
| false
|
stdio
| null | true
|
260/C
|
260
|
C
|
Python 3
|
TESTS
| 3
| 31
| 0
|
172833508
|
n, x = map(int, input().split())
x -= 1
A = [int(t) for t in input().split()]
mA = min(A)
u = A.index(mA)
A = [t-mA for t in A]
tt = 0
while A[x] != 0:
A[x] -= 1
x = (x-1+n)%n
tt += 1
A[u] = mA*n+tt
print(' '.join([str(a) for a in A]))
| 38
| 296
| 12,595,200
|
104650411
|
n,x = map(int, input().split())
a = list(map(int, input().split()))
m = min(a)
a = [i-m for i in a]
x = x-1
d = m*n
while a[x]>0:
d+=1
a[x]-=1
x-=1
if x<0:
x=n-1
a[x]=d
print(*a)
|
Codeforces Round 158 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Balls and Boxes
|
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
|
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.
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.
|
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
| null | null |
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
| 1,700
|
["constructive algorithms", "greedy", "implementation"]
| 38
|
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
| false
|
stdio
| null | true
|
260/C
|
260
|
C
|
PyPy 3-64
|
TESTS
| 3
| 61
| 0
|
196267914
|
#author: Sushmanth
from sys import stdin
input = stdin.readline
inp = lambda : list(map(int,input().split()))
def answer():
#has a left zero
left = -1
for i in range(x - 2 , -1 , -1):
if(a[i] == 0):
left = i
break
if(left != -1):
l = left
count , left = 0 , left + 1
while(1):
a[left] -= 1
count += 1
if(left == x):break
left += 1
a[l] = count
return a
#has a right zero
right = -1
for i in range(x + 1 , n):
if(a[i] == 0):
right = i
break
if(right != -1):
r = right
count , right = 0 , (right + 1) % n
while(1):
a[right] -= 1
count += 1
if(right == x):break
right = (right + 1) % n
a[r] = count
return a
#has no zero
minv = min(a)
for i in range(n):
if(a[i] == minv):
ind = i
break
for i in range(n):
a[i] -= minv
point , count = ind , 0
while(1):
a[ind] -= 1
count += 1
if(ind == x):break
ind = (ind + 1) % n
count -= 1
a[point] = minv * n + count
return a
for T in range(1):
n , x = inp()
a = inp()
x -= 1
print(*answer())
| 38
| 296
| 13,824,000
|
51287399
|
def main():
n, x = map(int,input().split())
a = [int(i) for i in input().split()]
j = x - 1
z = min(a)
while a[j] != z:
if j == 0:
j = n - 1
else:
j -= 1
m = a[j]
k = 0
if x - 1 > j:
for i in range(n):
if j < i <= x - 1:
a[i] -= (m + 1)
k += (m + 1)
else:
a[i] -= m
k += m
a[j] += k
elif x - 1 < j:
for i in range(n):
if x - 1 < i <= j:
a[i] -= m
k += m
else:
a[i] -=(m + 1)
k += (m + 1)
a[j] += k
else:
for i in range(n):
a[i] -= m
k += m
a[j] += k
print(*a)
main()
|
Codeforces Round 158 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Balls and Boxes
|
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
|
The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions.
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.
|
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
| null | null |
[{"input": "4 4\n4 3 1 6", "output": "3 2 5 4"}, {"input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6"}, {"input": "3 3\n2 3 1", "output": "1 2 3"}]
| 1,700
|
["constructive algorithms", "greedy", "implementation"]
| 38
|
[{"input": "4 4\r\n4 3 1 6\r\n", "output": "3 2 5 4 "}, {"input": "5 2\r\n3 2 0 2 7\r\n", "output": "2 1 4 1 6 "}, {"input": "3 3\r\n2 3 1\r\n", "output": "1 2 3 "}, {"input": "10 3\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 0 10000000000 0 0 0 0 0 0 0 "}, {"input": "5 4\r\n0 554459682 978416312 784688178 954779973\r\n", "output": "3 554459681 978416311 784688177 954779973 "}, {"input": "5 2\r\n1 554459683 978416312 784688178 954779974\r\n", "output": "6 554459681 978416311 784688177 954779973 "}, {"input": "10 8\r\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "}, {"input": "10 5\r\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163\r\n", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "}, {"input": "15 12\r\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495\r\n", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "15 8\r\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496\r\n", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "}, {"input": "10 5\r\n3 3 3 3 4 3 3 3 3 3\r\n", "output": "0 0 0 31 0 0 0 0 0 0 "}, {"input": "5 4\r\n3 1 3 1 3\r\n", "output": "2 0 2 5 2 "}]
| false
|
stdio
| null | true
|
799/C
|
799
|
C
|
PyPy 3
|
TESTS
| 5
| 124
| 1,843,200
|
106701999
|
n,c,d = list(map(int,input().split(" ")))
coin,dia = [],[]
for x in range(n):
b,p,cur = input().split(" ")
if cur == "C":
if int(p) <= c:
coin.append([int(b),int(p)])
else:
if int(p) <= d:
dia.append([int(b),int(p)])
combiC,combiD = [],[]
order = [coin,dia]
def find(li,bank):
final = []
for count,x in enumerate(li):
left = bank
left = left-int(x[1])
middle = []
middle.append(x[0])
change = list(zip(li,list(range(len(li)))))
for b in sorted(change,reverse=True,key=lambda c: c[0][0]):
if b[1] != count:
if b[0][1] <= left:
middle.append(b[0][0])
break
final.append(sum(middle))
return final
for x in order:
if len(x) > 1:
if x == coin:
combiC = find(coin,c)
else:
combiD = find(dia,d)
newCoin = [0]
for x in coin:
if str(type(x)) == "<class 'list'>":
newCoin.append(x[0])
else:
newCoin.append(x)
newDia = [0]
for x in dia:
if str(type(x)) == "<class 'list'>":
newDia.append(x[0])
else:
newDia.append(x)
combiC.append(0)
combiD.append(0)
if len(dia)+len(coin) >= 2:
output = [max(newCoin)+max(newDia),max(combiC),max(combiD)]
print(max(output))
else:
print(0)
| 79
| 233
| 16,793,600
|
133344131
|
import sys
def read_input(input_path=None):
if input_path is None:
f = sys.stdin
else:
f = open(input_path, 'r')
n, g, d = map(int, f.readline().split())
items = list()
for _ in range(n):
power, money, type_ = f.readline().strip().split()
items.append((int(power), int(money), type_))
return n, g, d, items
def sol(n, g, d, items):
gold_items = list()
diamond_items = list()
for item in items:
if item[2] == 'D':
if item[1] <= d:
diamond_items.append((item[0], item[1]))
else:
if item[1] <= g:
gold_items.append((item[0], item[1]))
gold_items = sorted(gold_items, key=lambda x: x[0], reverse=True)
diamond_items = sorted(diamond_items, key=lambda x: x[0], reverse=True)
mx = gold_items[0][0] + diamond_items[0][0] if gold_items and diamond_items else 0
for l, m in ((gold_items, g), (diamond_items, d)):
for i in range(len(l) - 1):
b1, p1 = l[i]
if b1 * 2 <= mx:
break
for j in range(i + 1, len(l)):
b2, p2 = l[j]
if b1 + b2 <= mx:
break
if p1 + p2 <= m:
mx = b1 + b2
break
return [f"{mx}"]
def solve(input_path=None):
return sol(*read_input(input_path))
def main():
for line in sol(*read_input()):
print(f"{line}")
if __name__ == '__main__':
main()
|
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
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 140
| 14,028,800
|
167772928
|
n = int(input())
a = list(map(int,input().split(' ')))
s = set()
m = 0
for i in range(n):
if(a[i] in s):
s.remove(a[i])
else:
s.add(a[i])
l = len(s)
if(m < l):
m = l
print(m)
| 56
| 108
| 17,100,800
|
226929690
|
n = int(input())
k = [int(i) for i in input().split()]
p = [0] * n
c = 0
mx = 0
for i in range(2*n):
if p[k[i]-1] == 0:
c += 1
p[k[i] - 1] = 1
else:
mx = max(c, mx)
c -= 1
mx = max(c, mx)
print(mx)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Andryusha and Socks
|
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
|
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
|
Print single integer — the maximum number of socks that were on the table at the same time.
| null |
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
|
[{"input": "1\n1 1", "output": "1"}, {"input": "3\n2 1 1 3 2 3", "output": "2"}]
| 800
|
["implementation"]
| 56
|
[{"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n2 1 1 3 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 1 3 2 4 3 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n4 2 6 3 4 8 7 1 1 5 2 10 6 8 3 5 10 9 9 7\r\n", "output": "6\r\n"}, {"input": "50\r\n30 47 31 38 37 50 36 43 9 23 2 2 15 31 14 49 9 16 6 44 27 14 5 6 3 47 25 26 1 35 3 15 24 19 8 46 49 41 4 26 40 28 42 11 34 35 46 18 7 28 18 40 19 42 4 41 38 48 50 12 29 39 33 17 25 22 22 21 36 45 27 30 20 7 13 29 39 44 21 8 37 45 34 1 20 10 11 17 33 12 43 13 10 16 48 24 32 5 23 32\r\n", "output": "25\r\n"}, {"input": "50\r\n1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50\r\n", "output": "1\r\n"}, {"input": "50\r\n50 50 49 49 48 48 47 47 46 46 45 45 44 44 43 43 42 42 41 41 40 40 39 39 38 38 37 37 36 36 35 35 34 34 33 33 32 32 31 31 30 30 29 29 28 28 27 27 26 26 25 25 24 24 23 23 22 22 21 21 20 20 19 19 18 18 17 17 16 16 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\r\n", "output": "1\r\n"}, {"input": "50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "50\r\n"}, {"input": "50\r\n50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "50\r\n"}, {"input": "10\r\n2 9 4 1 6 7 10 3 1 5 8 6 2 3 10 7 4 8 5 9\r\n", "output": "9\r\n"}]
| false
|
stdio
| null | true
|
33/A
|
33
|
A
|
Python 3
|
TESTS
| 8
| 218
| 0
|
52875084
|
n, m, k = map(int,input().split())
a = [2000]*1001
for i in range(n):
b=input().split()
if a[int(b[0])]>int(b[1]):
a[int(b[0])] = int(b[1])
a.sort()
sum = 0
i=0
while True:
if a[i]==2000:
break
sum +=a[i]
i+=1
if sum>k:
print(k)
else:
print(sum)
| 31
| 92
| 0
|
158545190
|
n, m, k = [int(item) for item in input().split(' ')]
my_dict = dict()
for i in range(n):
rows, health = [int(item) for item in input().split(' ')]
if(rows in my_dict and my_dict[rows] > health or rows not in my_dict):
my_dict[rows] = health
answer = sum(my_dict.values())
print(answer if answer < k else 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
|
33/A
|
33
|
A
|
PyPy 3
|
TESTS
| 5
| 184
| 20,172,800
|
122606387
|
n,m,k=map(int,input().split())
d={}
for i in range(n):
r,c=map(int,input().split())
if r in d:
if c<d[r]:
d[r]=c
else:
d[r]=c
sum=0
l=[]
for i in d:
l.append(d[i])
l.sort(reverse=True)
sum=0
for i in l:
if sum>=k:
break
sum+=i
print(sum)
| 31
| 92
| 0
|
165510966
|
n, m, k = list(map(int, input().split(' ')))
teeth = {}
for i in range(n):
r, c = list(map(int, input().split(' ')))
if r in teeth and teeth[r] > c or r not in teeth:
teeth[r] = c
_sum = sum(teeth.values())
print(_sum if _sum <= k else 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
|
980/C
|
980
|
C
|
Python 3
|
TESTS
| 8
| 78
| 0
|
38640562
|
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=[300]*256
for i in a:
m=0
for j in range(max(0,i-k+1),i+1):
if j<=c[j] and m==0 :r=j;m=1
if m==1 and c[j]!=300:
c[j]=max(r,c[j])
elif m==1 and c[j]==300:
c[j]=r
for i in a:
print(c[i],end=' ')
| 82
| 171
| 12,390,400
|
38042137
|
#!/usr/bin/env python3
[n, k] = map(int, input().strip().split())
pis = list(map(int, input().strip().split()))
import sys
if k == 1:
print (' '.join(map(str, pis)))
sys.exit(0)
N = 256
gamma = [-1 for _ in range(N)]
done = [False for _ in range(N)]
revdone = [False for _ in range(N)]
notset = 256
pends = {}
def get_bounds(i):
try:
l = N - 1 - revdone.index(True, N - 1 - i) + 1
except ValueError:
l = 0
try:
r = done.index(True, i) - 1
except ValueError:
r = N - 1
return l, r
def set_gamma(l, r, v):
for i in range(l, r + 1):
gamma[i] = v
done[i] = True
revdone[N - 1 - i] = True
for p in pis:
if not done[p]:
lb, rb = get_bounds(p)
if (lb > 0) and ((lb - 1) in pends) and (lb - 1 + pends[lb - 1] >= p):
l = lb
r = p
set_gamma(l, r, gamma[lb - 1])
if r - l + 1 < pends[lb - 1]:
pends[r] = pends[lb - 1] - (r - l + 1)
del pends[lb - 1]
else:
l = max(p - k + 1, lb)
r = p
set_gamma(l, r, l)
if r - l + 1 < k:
pends[p] = k - (r - l + 1)
notset -= r - l + 1
if notset == 0:
break
if notset > 0:
for i in range(N):
if gamma[i] < 0:
l, r = get_bounds(i)
set_gamma(l, min(l + k - 1, r), l)
print (' '.join(str(gamma[p]) for p in pis))
|
Codeforces Round 480 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Posterized
|
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.
|
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.
|
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
| null |
One possible way to group colors and assign keys for the first sample:
Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.
Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.
Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.
Other groups won't affect the result so they are not listed here.
|
[{"input": "4 3\n2 14 3 4", "output": "0 12 3 3"}, {"input": "5 2\n0 2 1 255 254", "output": "0 1 1 254 254"}]
| 1,700
|
["games", "greedy"]
| 82
|
[{"input": "4 3\r\n2 14 3 4\r\n", "output": "0 12 3 3\r\n"}, {"input": "5 2\r\n0 2 1 255 254\r\n", "output": "0 1 1 254 254\r\n"}, {"input": "10 3\r\n112 184 161 156 118 231 191 128 91 229\r\n", "output": "110 182 159 154 116 229 189 126 89 229\r\n"}, {"input": "9 3\r\n174 149 118 124 166 146 219 233 107\r\n", "output": "172 147 116 122 164 144 217 231 105\r\n"}, {"input": "8 4\r\n180 195 13 195 61 24 132 160\r\n", "output": "177 192 10 192 58 21 129 157\r\n"}, {"input": "1 4\r\n51\r\n", "output": "48\r\n"}, {"input": "2 4\r\n218 213\r\n", "output": "215 210\r\n"}, {"input": "3 3\r\n212 167 3\r\n", "output": "210 165 1\r\n"}, {"input": "100 7\r\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\r\n", "output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\r\n"}, {"input": "4 2\r\n122 108 224 154\r\n", "output": "121 107 223 153\r\n"}]
| false
|
stdio
| null | true
|
980/C
|
980
|
C
|
Python 3
|
PRETESTS
| 8
| 77
| 7,065,600
|
38038577
|
n,k = map(int,input().split())
l = [int(x) for x in input().split()]
maps = []
for _ in range(256):
maps.append(['empty',0])
output = []
for innum in l:
if maps[innum][0] == 'chosen':
outnum = maps[innum][1]
elif maps[innum][0] == 'potential':
outnum = maps[innum][1]
i = innum
while i >= 0 and maps[i][0] == 'potential':
maps[i] = ['chosen',outnum]
i -= 1
else:
i = innum
while i >= 0 and i >= innum - (k-1) and maps[i][0] != 'chosen':
i -= 1
i += 1
outnum = i
for j in range(i,innum+1):
maps[j] = ['chosen',outnum]
if innum < 255:
for j in range(innum+1,min(255,outnum+k)):
maps[j] = ['potential',outnum]
output.append(str(outnum))
print(' '.join(output))
| 82
| 171
| 13,004,800
|
38044069
|
def main():
n, k = map(int, input().split())
p = list(map(int, input().split()))
solve(n, k, p)
def solve(n, k, p):
group = 256 * [None]
r = p[:]
for i, pi in enumerate(p):
# print([(i, gi) for i, gi in enumerate(group)if gi is not None])
if group[pi] is not None:
r[i] = group[pi][0]
else:
lo = pi
while lo >= 0 and pi - lo < k and group[lo] is None:
lo -= 1
if lo < 0 or pi - lo == k:
lo += 1
hi = pi + 1
else: # group[lo] is not None
if pi - group[lo][0] < k:
lo = group[lo][0]
hi = pi + 1
else:
lo += 1
hi = pi + 1
lohi = (lo, hi)
for j in range(lo, hi):
group[j] = lohi
r[i] = group[pi][0]
print(" ".join(map(str, r)))
main()
|
Codeforces Round 480 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Posterized
|
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.
|
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.
|
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
| null |
One possible way to group colors and assign keys for the first sample:
Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.
Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.
Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.
Other groups won't affect the result so they are not listed here.
|
[{"input": "4 3\n2 14 3 4", "output": "0 12 3 3"}, {"input": "5 2\n0 2 1 255 254", "output": "0 1 1 254 254"}]
| 1,700
|
["games", "greedy"]
| 82
|
[{"input": "4 3\r\n2 14 3 4\r\n", "output": "0 12 3 3\r\n"}, {"input": "5 2\r\n0 2 1 255 254\r\n", "output": "0 1 1 254 254\r\n"}, {"input": "10 3\r\n112 184 161 156 118 231 191 128 91 229\r\n", "output": "110 182 159 154 116 229 189 126 89 229\r\n"}, {"input": "9 3\r\n174 149 118 124 166 146 219 233 107\r\n", "output": "172 147 116 122 164 144 217 231 105\r\n"}, {"input": "8 4\r\n180 195 13 195 61 24 132 160\r\n", "output": "177 192 10 192 58 21 129 157\r\n"}, {"input": "1 4\r\n51\r\n", "output": "48\r\n"}, {"input": "2 4\r\n218 213\r\n", "output": "215 210\r\n"}, {"input": "3 3\r\n212 167 3\r\n", "output": "210 165 1\r\n"}, {"input": "100 7\r\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\r\n", "output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\r\n"}, {"input": "4 2\r\n122 108 224 154\r\n", "output": "121 107 223 153\r\n"}]
| false
|
stdio
| null | true
|
780/B
|
780
|
B
|
Python 3
|
TESTS
| 35
| 4,009
| 11,878,400
|
35895167
|
n = int(input())
x = list(map(int, input().split()))
v = list(map(int, input().split()))
left = 0
right = 10 ** 9
while right - left > 0.000001:
mid = (left + right) / 2
a = min(x)
b = max(x)
for i in range(n):
a = max(a, x[i] - v[i] * mid)
b = min(b, x[i] + v[i] * mid)
if a > b:
left = mid
else:
right = mid
print('%.6f' % right)
| 46
| 139
| 16,179,200
|
216003079
|
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations
from bisect import *
from heapq import *
from math import ceil,gcd,lcm,floor,comb
alph = 'abcdefghijklmnopqrstuvwxyz'
#pow(x,mod-2,mod)
def find(num):
ma,mi = X[0]+(V[0]*num),X[0]-(V[0]*num)
for i in range(1,N):
a,b = X[i]+(V[i]*num),X[i]-(V[i]*num)
mi = max(mi,b)
ma = min(ma,a)
if mi<=ma:
return True
return False
N = int(input())
X = list(map(int,input().split()))
V = list(map(int,input().split()))
l,r = 0,10**9
while l+(1*(10**-6))<r:
mid = (l+r)/2
# print(mid,find(mid),l,r)
if find(mid):
r = mid
else:
l = mid+(1*(10**-6))
print(l)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 5
| 256
|
The Meeting Place Cannot Be Changed
|
The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.
At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.
You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.
|
The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.
The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.
|
Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.
Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$ holds.
| null |
In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.
|
[{"input": "3\n7 1 3\n1 2 1", "output": "2.000000000000"}, {"input": "4\n5 10 3 2\n2 3 2 4", "output": "1.400000000000"}]
| 1,600
|
["binary search"]
| 46
|
[{"input": "3\r\n7 1 3\r\n1 2 1\r\n", "output": "2.000000000000\r\n"}, {"input": "4\r\n5 10 3 2\r\n2 3 2 4\r\n", "output": "1.400000000000\r\n"}, {"input": "3\r\n1 1000000000 2\r\n1 2 1000000000\r\n", "output": "333333332.999999999971\r\n"}, {"input": "2\r\n4 5\r\n10 8\r\n", "output": "0.055555555556\r\n"}, {"input": "4\r\n14 12 10 17\r\n8 6 5 10\r\n", "output": "0.466666666667\r\n"}, {"input": "5\r\n1 15 61 29 43\r\n15 11 19 19 19\r\n", "output": "1.764705882353\r\n"}, {"input": "10\r\n20 11 17 38 15 27 2 40 24 37\r\n22 30 22 30 28 16 7 20 22 13\r\n", "output": "1.750000000000\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n1 1\r\n", "output": "0.000000000000\r\n"}, {"input": "3\r\n1 1 1\r\n1 1 1\r\n", "output": "0.000000000000\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read correct output
with open(output_path) as f:
correct_line = f.read().strip()
try:
b = float(correct_line)
except:
print(0)
return
# Read submission output
with open(submission_path) as f:
sub_line = f.read().strip()
try:
a = float(sub_line)
except:
print(0)
return
if a < 0:
print(0)
return
# Compute the error
denominator = max(1.0, b)
diff = abs(a - b)
error = diff / denominator
if error <= 1e-6:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 171
| 17,817,600
|
26168195
|
n = int(input())
sock = [int(x) for x in input().split()]
table = [False for i in range(n+1)]
t = 0;
maxt = 1
for i in range(n):
if table[sock[i]]:
table[sock[i]] = False;
t -= 1
else:
table[sock[i]] = True;
t += 1
if t>maxt:
maxt = t
print(maxt)
| 56
| 109
| 19,660,800
|
223373025
|
n=int(input())
b=list(map(int,input().split()))
c=0
z=0
v=[0]*100001
for i in b:
v[i]+=1
if v[i]!=2:
c+=1
z=max(z,c)
elif v[i]==2:
c-=1
print(z)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Andryusha and Socks
|
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
|
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
|
Print single integer — the maximum number of socks that were on the table at the same time.
| null |
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
|
[{"input": "1\n1 1", "output": "1"}, {"input": "3\n2 1 1 3 2 3", "output": "2"}]
| 800
|
["implementation"]
| 56
|
[{"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n2 1 1 3 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 1 3 2 4 3 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n4 2 6 3 4 8 7 1 1 5 2 10 6 8 3 5 10 9 9 7\r\n", "output": "6\r\n"}, {"input": "50\r\n30 47 31 38 37 50 36 43 9 23 2 2 15 31 14 49 9 16 6 44 27 14 5 6 3 47 25 26 1 35 3 15 24 19 8 46 49 41 4 26 40 28 42 11 34 35 46 18 7 28 18 40 19 42 4 41 38 48 50 12 29 39 33 17 25 22 22 21 36 45 27 30 20 7 13 29 39 44 21 8 37 45 34 1 20 10 11 17 33 12 43 13 10 16 48 24 32 5 23 32\r\n", "output": "25\r\n"}, {"input": "50\r\n1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50\r\n", "output": "1\r\n"}, {"input": "50\r\n50 50 49 49 48 48 47 47 46 46 45 45 44 44 43 43 42 42 41 41 40 40 39 39 38 38 37 37 36 36 35 35 34 34 33 33 32 32 31 31 30 30 29 29 28 28 27 27 26 26 25 25 24 24 23 23 22 22 21 21 20 20 19 19 18 18 17 17 16 16 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\r\n", "output": "1\r\n"}, {"input": "50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "50\r\n"}, {"input": "50\r\n50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "50\r\n"}, {"input": "10\r\n2 9 4 1 6 7 10 3 1 5 8 6 2 3 10 7 4 8 5 9\r\n", "output": "9\r\n"}]
| false
|
stdio
| null | true
|
980/C
|
980
|
C
|
Python 3
|
TESTS
| 8
| 77
| 7,065,600
|
38118893
|
n,k = list(map(int,input().split()))
arr = list(map(int,input().split()))
val = [-1]*256
val[0] = 0
for i in range(0,n):
if(val[arr[i]] > -1):
print(val[arr[i]],end = " ")
else:
flag = 0
for j in range(1,k):
if( val[arr[i]-j] > -1 ):
if(arr[i] - val[arr[i]-j] < k):
for p in range(arr[i] - j + 1,arr[i]+1):
val[p] = val[arr[i]-j]
else:
val[arr[i]] = arr[i]
#print(val[:i],i)
print(val[arr[i]],end = " ")
flag = 1
break
if(flag == 0):
for j in range(0,k):
val[arr[i] - j] = arr[i] -k + 1
print(val[arr[i]],end = " ")
| 82
| 186
| 12,390,400
|
38072825
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
c = [-1]*256
ans = [0]*n
for i in range(n):
if c[a[i]] == -1:
for j in range(a[i], max(-1, a[i]-k), -1):
if c[j] != -1:
if (c[j] +k) > a[i]:
c[a[i]] = c[j]
else:
c[a[i]] = j+1
break
if c[a[i]] == -1:
c[a[i]] = max(0, a[i]-k+1)
for xx in range(c[a[i]], a[i]):
c[xx] = c[a[i]]
ans[i] = str(c[a[i]])
print(' '.join(ans))
|
Codeforces Round 480 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Posterized
|
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.
|
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.
|
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
| null |
One possible way to group colors and assign keys for the first sample:
Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.
Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.
Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.
Other groups won't affect the result so they are not listed here.
|
[{"input": "4 3\n2 14 3 4", "output": "0 12 3 3"}, {"input": "5 2\n0 2 1 255 254", "output": "0 1 1 254 254"}]
| 1,700
|
["games", "greedy"]
| 82
|
[{"input": "4 3\r\n2 14 3 4\r\n", "output": "0 12 3 3\r\n"}, {"input": "5 2\r\n0 2 1 255 254\r\n", "output": "0 1 1 254 254\r\n"}, {"input": "10 3\r\n112 184 161 156 118 231 191 128 91 229\r\n", "output": "110 182 159 154 116 229 189 126 89 229\r\n"}, {"input": "9 3\r\n174 149 118 124 166 146 219 233 107\r\n", "output": "172 147 116 122 164 144 217 231 105\r\n"}, {"input": "8 4\r\n180 195 13 195 61 24 132 160\r\n", "output": "177 192 10 192 58 21 129 157\r\n"}, {"input": "1 4\r\n51\r\n", "output": "48\r\n"}, {"input": "2 4\r\n218 213\r\n", "output": "215 210\r\n"}, {"input": "3 3\r\n212 167 3\r\n", "output": "210 165 1\r\n"}, {"input": "100 7\r\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\r\n", "output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\r\n"}, {"input": "4 2\r\n122 108 224 154\r\n", "output": "121 107 223 153\r\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 170
| 10,547,200
|
83105322
|
n=int(input())
l=input().split()
l1,c=set(),0
for i in range(n):
if l[i] not in l1:
l1.add(l[i])
else:
l1.discard(l[i])
c=max(c,len(l1))
print(c)
| 56
| 109
| 26,009,600
|
163726124
|
n = int(input())
a = [int(el) for el in input().split()]
s = set()
res = 0
for el in a:
if el not in s:
s.add(el)
else:
s.discard(el)
res = max(res, len(s))
print(res)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Andryusha and Socks
|
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
|
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
|
Print single integer — the maximum number of socks that were on the table at the same time.
| null |
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
|
[{"input": "1\n1 1", "output": "1"}, {"input": "3\n2 1 1 3 2 3", "output": "2"}]
| 800
|
["implementation"]
| 56
|
[{"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n2 1 1 3 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 1 3 2 4 3 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n4 2 6 3 4 8 7 1 1 5 2 10 6 8 3 5 10 9 9 7\r\n", "output": "6\r\n"}, {"input": "50\r\n30 47 31 38 37 50 36 43 9 23 2 2 15 31 14 49 9 16 6 44 27 14 5 6 3 47 25 26 1 35 3 15 24 19 8 46 49 41 4 26 40 28 42 11 34 35 46 18 7 28 18 40 19 42 4 41 38 48 50 12 29 39 33 17 25 22 22 21 36 45 27 30 20 7 13 29 39 44 21 8 37 45 34 1 20 10 11 17 33 12 43 13 10 16 48 24 32 5 23 32\r\n", "output": "25\r\n"}, {"input": "50\r\n1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50\r\n", "output": "1\r\n"}, {"input": "50\r\n50 50 49 49 48 48 47 47 46 46 45 45 44 44 43 43 42 42 41 41 40 40 39 39 38 38 37 37 36 36 35 35 34 34 33 33 32 32 31 31 30 30 29 29 28 28 27 27 26 26 25 25 24 24 23 23 22 22 21 21 20 20 19 19 18 18 17 17 16 16 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\r\n", "output": "1\r\n"}, {"input": "50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "50\r\n"}, {"input": "50\r\n50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "50\r\n"}, {"input": "10\r\n2 9 4 1 6 7 10 3 1 5 8 6 2 3 10 7 4 8 5 9\r\n", "output": "9\r\n"}]
| false
|
stdio
| null | true
|
686/B
|
686
|
B
|
PyPy 3-64
|
TESTS
| 2
| 93
| 0
|
139123135
|
n=int(input())
a=[int(i) for i in input().split()]
b=sorted(a)
if b==a:
print()
else:
for i in range(n):
for j in range(i+1,n):
if a[i]>a[j]:
a[i],a[j]=a[j],a[i]
print(i+1,j+1)
| 37
| 62
| 0
|
181334381
|
from sys import stdin,stdout
input=stdin.readline
def print(*args, end='\n', sep=' ') -> None:
stdout.write(sep.join(map(str, args)) + end)
n=int(input()) ; arr=list(map(int,input().split()))
for i in range(n-1,0,-1):
for j in range(i):
if arr[j]>arr[j+1]:
arr[j],arr[j+1]=arr[j+1],arr[j]
print(j+1,j+2)
|
Codeforces Round 359 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Little Robber Girl's Zoo
|
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.
The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.
Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
|
Print the sequence of operations that will rearrange the animals by non-decreasing height.
The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed.
The number of operations should not exceed 20 000.
If the animals are arranged correctly from the start, you are allowed to output nothing.
| null |
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
|
[{"input": "4\n2 1 4 3", "output": "1 4"}, {"input": "7\n36 28 57 39 66 69 68", "output": "1 4\n6 7"}, {"input": "5\n1 2 1 2 1", "output": "2 5\n3 4\n1 4\n1 4"}]
| 1,100
|
["constructive algorithms", "implementation", "sortings"]
| 37
|
[{"input": "4\r\n2 1 4 3\r\n", "output": "1 2\r\n3 4\r\n"}, {"input": "7\r\n36 28 57 39 66 69 68\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "2 3\r\n4 5\r\n3 4\r\n"}, {"input": "78\r\n7 3 8 8 9 8 10 9 12 11 16 14 17 17 18 18 20 20 25 22 27 26 29 27 35 35 36 36 37 37 38 38 40 39 42 42 48 46 49 49 58 50 60 58 65 61 68 66 69 69 69 69 70 69 71 71 77 73 78 77 80 79 85 83 86 86 86 86 88 87 91 90 96 91 98 97 99 98\r\n", "output": "1 2\r\n5 6\r\n7 8\r\n9 10\r\n11 12\r\n19 20\r\n21 22\r\n23 24\r\n33 34\r\n37 38\r\n41 42\r\n43 44\r\n45 46\r\n47 48\r\n53 54\r\n57 58\r\n59 60\r\n61 62\r\n63 64\r\n69 70\r\n71 72\r\n73 74\r\n75 76\r\n77 78\r\n"}, {"input": "47\r\n17462271 17619887 84928078 85012448 86003043 86499508 129945397 135545949 141951561 161227989 167459883 215243263 243500622 284922151 326608661 350939646 359121478 388866754 441241406 460673749 497339472 512847269 521291845 537280636 561101359 569788868 581107309 588113051 619849749 621125808 637220955 640578896 660913258 674411824 694527254 716291982 725198795 734631740 797705515 810502965 832854453 866687928 923880462 948282076 957759163 968324020 997176104\r\n", "output": ""}, {"input": "99\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "20 21\n19 22\n18 23\n17 24\n16 25\n15 26\n14 27\n13 28\n12 29\n11 30\n10 31\n9 32\n8 35\n7 36\n6 37\n5 38\n4 39\n3 40\n2 41\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 51\n11 52\n12 55\n13 56\n14 57\n13 58\n12 59\n11 60\n10 61\n9 62\n8 63\n7 64\n6 65\n5 66\n4 67\n3 68\n2 69\n1 70\n2 71\n3 72\n4 73\n5 74\n6 75\n7 76\n8 77\n9 78\n10 79\n11 82\n12 83\n13 84\n14 85\n15 86\n16 87\n17 88\n18 89\n19 90\n20 91\n19 92\n18 93\n17 94\n16 95\n15 96\n14 97\n13 98\n12 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 97\n3 96\n2 95\n1 94\n2 93\n3 92\n4 91\n5 90\n6 89\n7 88\n8 87\n9 86\n10 85\n11 84\n12 83\n13 82\n14 81\n15 80\n16 77\n17 76\n18 75\n19 74\n20 73\n21 72\n22 71\n23 70\n24 69\n25 68\n26 67\n27 66\n26 63\n25 62\n24 61\n23 60\n22 59\n21 58\n20 57\n19 56\n18 55\n17 54\n16 53\n15 52\n14 51\n13 50\n12 49\n11 48\n10 47\n9 46\n8 43\n7 42\n6 41\n5 40\n4 39\n3 38\n2 37\n1 36\n2 35\n3 34\n4 33\n5 32\n6 31\n7 30\n8 29\n9 28\n10 27\n11 26\n12 25\n13 24\n14 23\n15 22\n16 21\n17 20\n18 19\n"}, {"input": "99\r\n4577 4577 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4575 4575 4575 4575 4575 4575 4574 4574 4574 4574 4574 4574 4574 4574 4574 4574 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4571 4571 4571 4571 4571 4571 4571 4571 4571 4570 4570 4570 4570 4570 4570 4570 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4567 4567 4567 4567 4567 4567 4567 4567 4567\r\n", "output": "2 3\n1 4\n2 5\n3 6\n4 7\n5 8\n6 9\n7 10\n8 11\n9 14\n10 15\n11 16\n10 17\n9 20\n8 21\n7 22\n6 23\n5 24\n4 25\n3 26\n2 27\n1 30\n2 31\n3 32\n4 33\n5 34\n6 35\n5 36\n4 37\n3 38\n2 39\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 53\n9 54\n8 55\n7 56\n6 57\n5 58\n4 59\n3 62\n2 63\n1 64\n2 65\n3 66\n4 69\n5 70\n6 71\n7 72\n8 73\n9 74\n10 75\n11 76\n12 77\n11 80\n10 81\n9 82\n8 83\n7 84\n6 85\n5 86\n4 87\n3 88\n2 91\n1 92\n2 93\n3 94\n4 95\n5 96\n6 97\n7 98\n8 99\n9 98\n10 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 99\n3 98\n2 99\n1 98\n2 99\n3 98\n4 99\n5 98\n6 99\n7 98\n8 95\n9 94\n8 93\n7 92\n6 91\n5 90\n4 89\n3 88\n2 87\n1 84\n2 83\n3 82\n4 81\n5 78\n6 77\n7 76\n6 75\n5 74\n4 73\n3 72\n2 71\n1 68\n2 67\n3 66\n4 65\n5 64\n6 63\n7 62\n8 61\n9 60\n10 59\n11 56\n10 55\n9 54\n8 53\n7 52\n6 51\n5 50\n4 49\n3 48\n2 45\n1 44\n2 43\n3 42\n4 41\n5 40\n6 39\n7 36\n8 35\n9 34\n10 33\n11 32\n10 29\n9 28\n8 27\n7 26\n6 25\n5 24\n4 23\n3 22\n2 21\n1 18\n2 17\n3 16\n4 15\n5 14\n6 13\n7 12\n8 11\n9 10\n"}, {"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": ""}, {"input": "10\r\n44 23 65 17 48 29 49 88 91 85\r\n", "output": "1 2\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n9 10\r\n2 3\r\n4 5\r\n8 9\r\n1 2\r\n3 4\r\n"}, {"input": "13\r\n605297997 425887240 859639341 200428931 888317166 983420497 81642057 628988722 389959969 358920886 646428392 324912711 401437250\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n3 4\r\n5 6\r\n6 7\r\n8 9\r\n9 10\r\n2 3\r\n4 5\r\n5 6\r\n7 8\r\n8 9\r\n1 2\r\n3 4\r\n4 5\r\n6 7\r\n7 8\r\n3 4\r\n5 6\r\n6 7\r\n4 5\r\n3 4\r\n"}, {"input": "43\r\n644870843 160471908 227474511 47341477 175939701 563067024 749818136 707986934 201095131 736488829 346428456 342944986 316696712 101551423 672610101 897020945 708299245 587795677 408207112 985104524 278945228 192250326 157154304 301319412 270702270 954096281 649990285 37649442 300182190 382249227 605285302 392816037 419998044 84624133 332174228 996770879 816912092 283973844 498255316 374935144 294452244 529912248 553039417\r\n", "output": "1 4\n2 5\n1 4\n4 5\n5 8\n8 13\n7 14\n6 13\n5 14\n4 27\n7 22\n6 23\n7 10\n6 9\n5 6\n4 5\n3 4\n2 3\n15 24\n14 25\n13 28\n12 23\n11 24\n10 29\n11 34\n10 37\n9 34\n8 35\n7 34\n8 15\n7 14\n6 13\n5 12\n4 7\n3 4\n20 35\n19 40\n18 41\n17 40\n16 31\n15 32\n14 29\n13 28\n12 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n2 3\n1 2\n18 27\n19 26\n18 23\n17 20\n16 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n34 41\n33 42\n32 43\n31 42\n30 41\n29 42\n28 39\n27 38\n26 37\n25 36\n24 35\n23 34\n22 33\n21 32\n20 31\n19 24\n18 23\n17 18\n16 17\n15 16\n14 15\n13 14\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n27 30\n28 29\n"}, {"input": "97\r\n1 1 1 2 1 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 1 1 2 1 1 1 1 2 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 1 1 1 2 2 1 1 2 1 1 1 1 2 2 1 2 1 2 1 1 2 2 2 1 2 2 1 1 2 2 2 1 1 2 1 2 1 1 2\r\n", "output": "4 5\n5 6\n6 9\n7 10\n8 11\n9 12\n10 13\n11 12\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n13 14\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n15 16\n34 35\n33 38\n32 39\n31 40\n30 45\n29 44\n28 43\n27 42\n26 41\n25 40\n24 39\n23 38\n22 37\n21 36\n20 35\n19 34\n18 33\n17 32\n16 31\n17 30\n18 29\n19 28\n20 27\n21 26\n22 25\n23 24\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n58 59\n57 60\n56 61\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n31 36\n30 35\n29 34\n28 33\n27 32\n26 31\n25 30\n26 29\n27 28\n63 64\n62 67\n61 68\n60 69\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 39\n29 38\n30 37\n31 36\n32 35\n33 34\n72 77\n71 78\n70 77\n69 76\n68 75\n67 74\n66 73\n65 72\n64 71\n63 70\n62 69\n61 68\n60 67\n59 66\n58 65\n57 64\n56 63\n55 62\n54 61\n53 60\n52 59\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n84 85\n83 86\n82 85\n81 84\n80 83\n79 82\n78 81\n77 80\n76 79\n75 78\n74 77\n73 76\n72 75\n71 74\n70 73\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 62\n58 61\n57 60\n56 59\n55 58\n54 57\n53 56\n52 55\n51 54\n50 53\n49 52\n48 51\n47 50\n46 49\n45 48\n44 47\n43 46\n42 45\n41 44\n40 43\n39 42\n40 41\n89 90\n88 95\n87 96\n86 95\n85 94\n84 93\n83 92\n82 91\n81 90\n80 89\n79 88\n78 87\n77 86\n76 85\n75 84\n74 83\n73 82\n72 81\n71 80\n70 79\n69 78\n68 77\n67 76\n66 75\n65 74\n64 73\n63 72\n62 71\n61 70\n60 69\n59 68\n58 67\n57 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 60\n50 59\n49 58\n48 57\n47 56\n46 55\n45 54\n44 53\n43 52\n42 51\n41 50\n42 49\n43 48\n44 47\n45 46\n"}, {"input": "87\r\n2 2 1 2 3 1 3 2 3 2 3 3 1 3 3 3 2 2 1 1 2 3 2 1 2 2 3 3 1 1 1 3 2 3 1 2 1 3 3 3 3 3 3 2 3 2 3 3 2 1 1 3 1 1 3 3 2 3 1 1 3 3 3 2 3 1 3 2 2 2 1 3 3 3 1 1 2 3 2 3 2 1 3 3 3 1 3\r\n", "output": "2 3\n1 2\n5 10\n4 9\n3 4\n2 3\n7 8\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n16 19\n15 20\n14 19\n13 20\n12 23\n11 24\n10 25\n9 26\n10 25\n9 24\n8 23\n7 10\n6 9\n5 8\n4 7\n5 6\n13 22\n14 21\n15 20\n14 19\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n17 18\n28 29\n27 30\n26 37\n25 36\n24 37\n23 36\n22 35\n21 34\n20 33\n19 32\n18 31\n17 30\n16 29\n15 28\n14 19\n13 18\n12 17\n11 16\n10 15\n9 14\n8 13\n7 12\n8 11\n9 10\n22 27\n21 26\n20 25\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n11 12\n43 46\n42 45\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n26 27\n48 49\n47 50\n46 53\n45 54\n44 53\n43 52\n42 51\n41 50\n40 49\n39 48\n38 47\n37 46\n36 45\n35 44\n34 43\n33 42\n32 41\n31 40\n30 39\n29 38\n28 37\n27 36\n28 35\n27 34\n26 33\n25 32\n24 31\n23 30\n22 29\n21 28\n20 27\n19 26\n18 25\n17 24\n16 23\n15 22\n14 21\n13 20\n12 19\n13 18\n14 17\n15 16\n56 59\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n17 18\n63 68\n62 71\n61 70\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n36 45\n35 44\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n38 43\n39 42\n38 41\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n74 75\n73 76\n72 81\n71 82\n70 81\n69 80\n68 79\n67 78\n66 77\n65 76\n64 75\n63 74\n62 73\n61 72\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n21 22\n44 49\n45 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n85 86\n84 85\n83 84\n82 83\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n"}, {"input": "100\r\n3 2 5 4 3 3 3 3 4 3 1 2 3 2 3 1 4 1 5 2 5 3 3 5 2 3 5 4 3 4 1 5 5 2 2 1 3 5 1 3 5 2 2 1 4 3 1 3 5 1 1 3 5 5 5 4 5 5 1 5 3 5 4 3 5 4 1 1 2 1 2 5 1 2 2 2 3 5 5 5 4 2 3 2 1 2 3 5 2 2 2 2 5 3 5 4 2 5 3 4\r\n", "output": "1 4\n4 5\n3 6\n4 7\n5 10\n6 11\n7 22\n8 25\n7 28\n6 17\n5 16\n4 17\n3 18\n2 31\n1 12\n4 11\n5 10\n4 9\n3 8\n2 5\n3 4\n15 30\n16 31\n17 22\n18 21\n17 22\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n22 23\n23 30\n24 29\n23 28\n22 27\n21 24\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n33 36\n32 35\n31 36\n30 39\n29 46\n28 43\n27 44\n26 47\n25 50\n24 51\n23 52\n22 51\n21 50\n20 51\n19 50\n18 23\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n11 16\n10 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n26 49\n25 48\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n28 47\n27 46\n26 45\n25 44\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n32 43\n31 40\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n34 39\n33 38\n32 37\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n9 12\n10 11\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n58 63\n57 66\n56 67\n55 70\n54 69\n53 70\n52 73\n51 74\n50 75\n49 76\n48 77\n47 76\n46 75\n45 74\n44 73\n43 72\n42 71\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n47 70\n46 69\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n49 68\n48 67\n47 66\n46 65\n45 64\n44 63\n43 62\n42 61\n41 60\n40 59\n39 58\n38 57\n37 56\n36 55\n35 54\n34 53\n33 52\n32 51\n31 50\n30 47\n29 46\n28 45\n27 44\n26 43\n25 42\n24 41\n23 40\n22 39\n21 38\n20 37\n19 36\n18 35\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n13 16\n14 15\n25 34\n26 33\n25 32\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n28 31\n29 30\n80 81\n79 84\n78 85\n77 86\n76 85\n75 86\n74 89\n73 90\n72 91\n71 96\n70 99\n69 100\n68 99\n67 98\n66 97\n65 96\n64 95\n63 94\n62 93\n61 92\n60 91\n59 90\n60 89\n59 88\n58 87\n57 86\n56 85\n55 84\n54 83\n53 82\n52 81\n51 80\n50 79\n49 78\n48 69\n47 68\n46 67\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n33 40\n32 39\n31 38\n30 37\n31 36\n32 35\n31 34\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n55 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n72 77\n71 76\n70 75\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n"}, {"input": "100\r\n245 230 240 248 247 235 240 228 247 243 244 240 246 234 244 247 247 232 247 233 241 247 236 247 230 228 243 237 246 231 246 231 233 235 229 244 247 248 245 248 231 230 238 247 235 248 240 239 233 232 230 229 229 244 247 246 248 247 247 234 243 242 247 228 238 238 236 243 236 228 229 245 232 246 241 243 248 235 242 237 244 239 238 245 231 235 234 237 238 237 234 232 231 236 233 238 228 243 242 246\r\n", "output": "1 2\n2 5\n5 14\n4 7\n3 8\n2 15\n3 12\n2 3\n1 2\n6 13\n7 14\n6 15\n7 14\n6 7\n5 6\n4 5\n3 4\n10 15\n15 20\n16 25\n15 32\n14 35\n13 34\n12 35\n11 36\n10 35\n9 32\n8 33\n7 34\n6 35\n5 8\n4 7\n3 6\n4 5\n11 36\n10 35\n9 34\n8 33\n7 32\n8 33\n7 30\n6 25\n5 8\n4 7\n3 6\n4 5\n3 4\n2 3\n11 22\n10 19\n9 18\n8 17\n7 16\n6 15\n5 14\n6 7\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 29\n33 34\n34 35\n35 36\n36 41\n37 42\n36 51\n35 52\n34 53\n33 52\n32 53\n31 52\n30 53\n29 64\n28 55\n27 56\n26 53\n27 54\n26 69\n25 70\n24 49\n23 48\n22 47\n21 46\n20 45\n19 44\n18 43\n17 40\n16 39\n15 38\n14 21\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n9 10\n8 9\n7 8\n6 7\n19 20\n18 19\n17 18\n16 17\n24 37\n23 34\n22 33\n21 32\n20 31\n19 30\n18 29\n17 28\n16 27\n15 26\n14 23\n13 22\n14 21\n13 20\n12 19\n11 18\n10 17\n9 16\n8 15\n7 12\n8 11\n7 10\n6 9\n5 8\n4 7\n5 6\n52 75\n53 82\n54 87\n53 70\n52 71\n51 72\n50 73\n49 70\n48 71\n47 86\n46 87\n45 92\n44 99\n43 96\n42 95\n41 94\n40 95\n39 96\n38 95\n37 96\n36 97\n35 98\n34 99\n33 94\n32 95\n31 98\n30 97\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n33 98\n32 99\n31 100\n30 95\n29 96\n28 97\n27 98\n26 99\n25 42\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 41\n27 38\n26 37\n25 36\n26 35\n27 34\n26 31\n25 30\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n7 10\n6 9\n5 6\n4 5\n45 98\n46 97\n47 98\n46 99\n45 98\n44 97\n43 96\n42 91\n41 90\n40 89\n39 88\n38 87\n37 86\n36 85\n35 84\n34 83\n33 78\n32 35\n31 34\n30 33\n29 30\n28 29\n27 28\n38 77\n37 72\n36 69\n37 68\n36 67\n35 64\n34 63\n33 62\n32 57\n31 56\n30 55\n29 34\n28 33\n27 32\n26 31\n25 28\n24 27\n23 26\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n37 54\n38 53\n37 52\n36 51\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 37\n27 36\n26 35\n25 34\n24 33\n23 32\n22 31\n21 28\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n49 50\n60 61\n75 76\n81 82\n94 95\n"}, {"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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": ""}, {"input": "100\r\n2 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 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "43 44\n42 45\n41 46\n40 47\n39 48\n38 49\n37 50\n36 51\n35 52\n34 53\n33 54\n32 55\n31 56\n30 57\n29 58\n28 59\n27 60\n26 61\n25 62\n24 63\n23 64\n22 65\n21 66\n20 67\n19 68\n18 69\n17 70\n16 71\n15 72\n14 73\n13 74\n12 75\n11 76\n10 77\n9 78\n8 79\n7 80\n6 81\n5 82\n4 83\n3 84\n2 85\n1 86\n2 87\n3 88\n4 89\n5 90\n6 91\n7 92\n8 93\n9 94\n10 95\n11 96\n12 97\n13 98\n14 99\n15 100\n16 99\n17 98\n18 97\n19 96\n20 95\n21 94\n22 93\n23 92\n24 91\n25 90\n26 89\n27 88\n28 87\n29 86\n30 85\n31 84\n32 83\n33 82\n34 81\n35 80\n36 79\n37 78\n38 77\n39 76\n40 75\n41 74\n42 73\n43 72\n44 71\n45 70\n46 69\n47 68\n48 67\n49 66\n50 65\n51 64\n52 63\n53 62\n54 61\n55 60\n56 59\n57 58\n"}, {"input": "1\r\n1\r\n", "output": ""}, {"input": "10\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": ""}, {"input": "98\r\n5 5 5 5 5 5 5 6 6 6 6 7 6 7 7 7 7 7 7 8 8 8 8 8 8 9 8 9 9 9 9 10 9 10 9 11 10 11 11 11 11 12 12 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 14 15 15 15 15 15 15 15 16 16 16 16 17 17 17 17 17 17 18 17 18 18 18 19 19 19 19 19 20 19 20 20 20 20 20 20 20\r\n", "output": "12 13\r\n26 27\r\n32 33\r\n34 35\r\n36 37\r\n80 81\r\n90 91\r\n33 34\r\n"}, {"input": "20\r\n2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2\r\n", "output": "6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n"}, {"input": "1\r\n5\r\n", "output": ""}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
with open(input_path, 'r') as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
with open(submission_output_path, 'r') as f:
lines = f.readlines()
ops = []
for line in lines:
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
l = int(parts[0])
r = int(parts[1])
except:
print(0)
return
ops.append((l, r))
if len(ops) > 20000:
print(0)
return
arr = a.copy()
for l, r in ops:
if l < 1 or r > n or l >= r or (r - l + 1) % 2 != 0:
print(0)
return
for i in range(l-1, r, 2):
if i+1 < len(arr):
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted_ok = all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
print(100 if sorted_ok else 0)
if __name__ == '__main__':
main()
| true
|
686/B
|
686
|
B
|
PyPy 3
|
TESTS
| 2
| 77
| 20,172,800
|
128030949
|
import math
def main_function():
n = int(input())
a = [int(i) for i in input().split(" ")]
counter = 0
for t in range(len(a)):
i = len(a) - t - 1
currnet_max = a[i]
index = i
for j in range(0, i + 1):
if a[j] > currnet_max:
currnet_max = a[j]
index = j
a[i], a[index] = a[index], a[i]
for r in range(index, i):
print(str(index + 1) + " " + str(index + 2))
main_function()
| 37
| 62
| 102,400
|
18674341
|
n = int(input())
data = [int(i) for i in input().split()]
ret = ''
while 1:
p = 0
for i in range(n - 1):
if data[i] > data[i + 1]:
data[i], data[i + 1] = data[i + 1], data[i]
p += 1
ret += str(i + 1) + ' ' + str(i + 2) + '\n'
if not p:
break
print(ret)
|
Codeforces Round 359 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Little Robber Girl's Zoo
|
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.
The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.
Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
|
Print the sequence of operations that will rearrange the animals by non-decreasing height.
The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed.
The number of operations should not exceed 20 000.
If the animals are arranged correctly from the start, you are allowed to output nothing.
| null |
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
|
[{"input": "4\n2 1 4 3", "output": "1 4"}, {"input": "7\n36 28 57 39 66 69 68", "output": "1 4\n6 7"}, {"input": "5\n1 2 1 2 1", "output": "2 5\n3 4\n1 4\n1 4"}]
| 1,100
|
["constructive algorithms", "implementation", "sortings"]
| 37
|
[{"input": "4\r\n2 1 4 3\r\n", "output": "1 2\r\n3 4\r\n"}, {"input": "7\r\n36 28 57 39 66 69 68\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "2 3\r\n4 5\r\n3 4\r\n"}, {"input": "78\r\n7 3 8 8 9 8 10 9 12 11 16 14 17 17 18 18 20 20 25 22 27 26 29 27 35 35 36 36 37 37 38 38 40 39 42 42 48 46 49 49 58 50 60 58 65 61 68 66 69 69 69 69 70 69 71 71 77 73 78 77 80 79 85 83 86 86 86 86 88 87 91 90 96 91 98 97 99 98\r\n", "output": "1 2\r\n5 6\r\n7 8\r\n9 10\r\n11 12\r\n19 20\r\n21 22\r\n23 24\r\n33 34\r\n37 38\r\n41 42\r\n43 44\r\n45 46\r\n47 48\r\n53 54\r\n57 58\r\n59 60\r\n61 62\r\n63 64\r\n69 70\r\n71 72\r\n73 74\r\n75 76\r\n77 78\r\n"}, {"input": "47\r\n17462271 17619887 84928078 85012448 86003043 86499508 129945397 135545949 141951561 161227989 167459883 215243263 243500622 284922151 326608661 350939646 359121478 388866754 441241406 460673749 497339472 512847269 521291845 537280636 561101359 569788868 581107309 588113051 619849749 621125808 637220955 640578896 660913258 674411824 694527254 716291982 725198795 734631740 797705515 810502965 832854453 866687928 923880462 948282076 957759163 968324020 997176104\r\n", "output": ""}, {"input": "99\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "20 21\n19 22\n18 23\n17 24\n16 25\n15 26\n14 27\n13 28\n12 29\n11 30\n10 31\n9 32\n8 35\n7 36\n6 37\n5 38\n4 39\n3 40\n2 41\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 51\n11 52\n12 55\n13 56\n14 57\n13 58\n12 59\n11 60\n10 61\n9 62\n8 63\n7 64\n6 65\n5 66\n4 67\n3 68\n2 69\n1 70\n2 71\n3 72\n4 73\n5 74\n6 75\n7 76\n8 77\n9 78\n10 79\n11 82\n12 83\n13 84\n14 85\n15 86\n16 87\n17 88\n18 89\n19 90\n20 91\n19 92\n18 93\n17 94\n16 95\n15 96\n14 97\n13 98\n12 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 97\n3 96\n2 95\n1 94\n2 93\n3 92\n4 91\n5 90\n6 89\n7 88\n8 87\n9 86\n10 85\n11 84\n12 83\n13 82\n14 81\n15 80\n16 77\n17 76\n18 75\n19 74\n20 73\n21 72\n22 71\n23 70\n24 69\n25 68\n26 67\n27 66\n26 63\n25 62\n24 61\n23 60\n22 59\n21 58\n20 57\n19 56\n18 55\n17 54\n16 53\n15 52\n14 51\n13 50\n12 49\n11 48\n10 47\n9 46\n8 43\n7 42\n6 41\n5 40\n4 39\n3 38\n2 37\n1 36\n2 35\n3 34\n4 33\n5 32\n6 31\n7 30\n8 29\n9 28\n10 27\n11 26\n12 25\n13 24\n14 23\n15 22\n16 21\n17 20\n18 19\n"}, {"input": "99\r\n4577 4577 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4575 4575 4575 4575 4575 4575 4574 4574 4574 4574 4574 4574 4574 4574 4574 4574 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4571 4571 4571 4571 4571 4571 4571 4571 4571 4570 4570 4570 4570 4570 4570 4570 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4567 4567 4567 4567 4567 4567 4567 4567 4567\r\n", "output": "2 3\n1 4\n2 5\n3 6\n4 7\n5 8\n6 9\n7 10\n8 11\n9 14\n10 15\n11 16\n10 17\n9 20\n8 21\n7 22\n6 23\n5 24\n4 25\n3 26\n2 27\n1 30\n2 31\n3 32\n4 33\n5 34\n6 35\n5 36\n4 37\n3 38\n2 39\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 53\n9 54\n8 55\n7 56\n6 57\n5 58\n4 59\n3 62\n2 63\n1 64\n2 65\n3 66\n4 69\n5 70\n6 71\n7 72\n8 73\n9 74\n10 75\n11 76\n12 77\n11 80\n10 81\n9 82\n8 83\n7 84\n6 85\n5 86\n4 87\n3 88\n2 91\n1 92\n2 93\n3 94\n4 95\n5 96\n6 97\n7 98\n8 99\n9 98\n10 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 99\n3 98\n2 99\n1 98\n2 99\n3 98\n4 99\n5 98\n6 99\n7 98\n8 95\n9 94\n8 93\n7 92\n6 91\n5 90\n4 89\n3 88\n2 87\n1 84\n2 83\n3 82\n4 81\n5 78\n6 77\n7 76\n6 75\n5 74\n4 73\n3 72\n2 71\n1 68\n2 67\n3 66\n4 65\n5 64\n6 63\n7 62\n8 61\n9 60\n10 59\n11 56\n10 55\n9 54\n8 53\n7 52\n6 51\n5 50\n4 49\n3 48\n2 45\n1 44\n2 43\n3 42\n4 41\n5 40\n6 39\n7 36\n8 35\n9 34\n10 33\n11 32\n10 29\n9 28\n8 27\n7 26\n6 25\n5 24\n4 23\n3 22\n2 21\n1 18\n2 17\n3 16\n4 15\n5 14\n6 13\n7 12\n8 11\n9 10\n"}, {"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": ""}, {"input": "10\r\n44 23 65 17 48 29 49 88 91 85\r\n", "output": "1 2\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n9 10\r\n2 3\r\n4 5\r\n8 9\r\n1 2\r\n3 4\r\n"}, {"input": "13\r\n605297997 425887240 859639341 200428931 888317166 983420497 81642057 628988722 389959969 358920886 646428392 324912711 401437250\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n3 4\r\n5 6\r\n6 7\r\n8 9\r\n9 10\r\n2 3\r\n4 5\r\n5 6\r\n7 8\r\n8 9\r\n1 2\r\n3 4\r\n4 5\r\n6 7\r\n7 8\r\n3 4\r\n5 6\r\n6 7\r\n4 5\r\n3 4\r\n"}, {"input": "43\r\n644870843 160471908 227474511 47341477 175939701 563067024 749818136 707986934 201095131 736488829 346428456 342944986 316696712 101551423 672610101 897020945 708299245 587795677 408207112 985104524 278945228 192250326 157154304 301319412 270702270 954096281 649990285 37649442 300182190 382249227 605285302 392816037 419998044 84624133 332174228 996770879 816912092 283973844 498255316 374935144 294452244 529912248 553039417\r\n", "output": "1 4\n2 5\n1 4\n4 5\n5 8\n8 13\n7 14\n6 13\n5 14\n4 27\n7 22\n6 23\n7 10\n6 9\n5 6\n4 5\n3 4\n2 3\n15 24\n14 25\n13 28\n12 23\n11 24\n10 29\n11 34\n10 37\n9 34\n8 35\n7 34\n8 15\n7 14\n6 13\n5 12\n4 7\n3 4\n20 35\n19 40\n18 41\n17 40\n16 31\n15 32\n14 29\n13 28\n12 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n2 3\n1 2\n18 27\n19 26\n18 23\n17 20\n16 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n34 41\n33 42\n32 43\n31 42\n30 41\n29 42\n28 39\n27 38\n26 37\n25 36\n24 35\n23 34\n22 33\n21 32\n20 31\n19 24\n18 23\n17 18\n16 17\n15 16\n14 15\n13 14\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n27 30\n28 29\n"}, {"input": "97\r\n1 1 1 2 1 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 1 1 2 1 1 1 1 2 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 1 1 1 2 2 1 1 2 1 1 1 1 2 2 1 2 1 2 1 1 2 2 2 1 2 2 1 1 2 2 2 1 1 2 1 2 1 1 2\r\n", "output": "4 5\n5 6\n6 9\n7 10\n8 11\n9 12\n10 13\n11 12\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n13 14\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n15 16\n34 35\n33 38\n32 39\n31 40\n30 45\n29 44\n28 43\n27 42\n26 41\n25 40\n24 39\n23 38\n22 37\n21 36\n20 35\n19 34\n18 33\n17 32\n16 31\n17 30\n18 29\n19 28\n20 27\n21 26\n22 25\n23 24\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n58 59\n57 60\n56 61\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n31 36\n30 35\n29 34\n28 33\n27 32\n26 31\n25 30\n26 29\n27 28\n63 64\n62 67\n61 68\n60 69\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 39\n29 38\n30 37\n31 36\n32 35\n33 34\n72 77\n71 78\n70 77\n69 76\n68 75\n67 74\n66 73\n65 72\n64 71\n63 70\n62 69\n61 68\n60 67\n59 66\n58 65\n57 64\n56 63\n55 62\n54 61\n53 60\n52 59\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n84 85\n83 86\n82 85\n81 84\n80 83\n79 82\n78 81\n77 80\n76 79\n75 78\n74 77\n73 76\n72 75\n71 74\n70 73\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 62\n58 61\n57 60\n56 59\n55 58\n54 57\n53 56\n52 55\n51 54\n50 53\n49 52\n48 51\n47 50\n46 49\n45 48\n44 47\n43 46\n42 45\n41 44\n40 43\n39 42\n40 41\n89 90\n88 95\n87 96\n86 95\n85 94\n84 93\n83 92\n82 91\n81 90\n80 89\n79 88\n78 87\n77 86\n76 85\n75 84\n74 83\n73 82\n72 81\n71 80\n70 79\n69 78\n68 77\n67 76\n66 75\n65 74\n64 73\n63 72\n62 71\n61 70\n60 69\n59 68\n58 67\n57 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 60\n50 59\n49 58\n48 57\n47 56\n46 55\n45 54\n44 53\n43 52\n42 51\n41 50\n42 49\n43 48\n44 47\n45 46\n"}, {"input": "87\r\n2 2 1 2 3 1 3 2 3 2 3 3 1 3 3 3 2 2 1 1 2 3 2 1 2 2 3 3 1 1 1 3 2 3 1 2 1 3 3 3 3 3 3 2 3 2 3 3 2 1 1 3 1 1 3 3 2 3 1 1 3 3 3 2 3 1 3 2 2 2 1 3 3 3 1 1 2 3 2 3 2 1 3 3 3 1 3\r\n", "output": "2 3\n1 2\n5 10\n4 9\n3 4\n2 3\n7 8\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n16 19\n15 20\n14 19\n13 20\n12 23\n11 24\n10 25\n9 26\n10 25\n9 24\n8 23\n7 10\n6 9\n5 8\n4 7\n5 6\n13 22\n14 21\n15 20\n14 19\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n17 18\n28 29\n27 30\n26 37\n25 36\n24 37\n23 36\n22 35\n21 34\n20 33\n19 32\n18 31\n17 30\n16 29\n15 28\n14 19\n13 18\n12 17\n11 16\n10 15\n9 14\n8 13\n7 12\n8 11\n9 10\n22 27\n21 26\n20 25\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n11 12\n43 46\n42 45\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n26 27\n48 49\n47 50\n46 53\n45 54\n44 53\n43 52\n42 51\n41 50\n40 49\n39 48\n38 47\n37 46\n36 45\n35 44\n34 43\n33 42\n32 41\n31 40\n30 39\n29 38\n28 37\n27 36\n28 35\n27 34\n26 33\n25 32\n24 31\n23 30\n22 29\n21 28\n20 27\n19 26\n18 25\n17 24\n16 23\n15 22\n14 21\n13 20\n12 19\n13 18\n14 17\n15 16\n56 59\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n17 18\n63 68\n62 71\n61 70\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n36 45\n35 44\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n38 43\n39 42\n38 41\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n74 75\n73 76\n72 81\n71 82\n70 81\n69 80\n68 79\n67 78\n66 77\n65 76\n64 75\n63 74\n62 73\n61 72\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n21 22\n44 49\n45 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n85 86\n84 85\n83 84\n82 83\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n"}, {"input": "100\r\n3 2 5 4 3 3 3 3 4 3 1 2 3 2 3 1 4 1 5 2 5 3 3 5 2 3 5 4 3 4 1 5 5 2 2 1 3 5 1 3 5 2 2 1 4 3 1 3 5 1 1 3 5 5 5 4 5 5 1 5 3 5 4 3 5 4 1 1 2 1 2 5 1 2 2 2 3 5 5 5 4 2 3 2 1 2 3 5 2 2 2 2 5 3 5 4 2 5 3 4\r\n", "output": "1 4\n4 5\n3 6\n4 7\n5 10\n6 11\n7 22\n8 25\n7 28\n6 17\n5 16\n4 17\n3 18\n2 31\n1 12\n4 11\n5 10\n4 9\n3 8\n2 5\n3 4\n15 30\n16 31\n17 22\n18 21\n17 22\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n22 23\n23 30\n24 29\n23 28\n22 27\n21 24\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n33 36\n32 35\n31 36\n30 39\n29 46\n28 43\n27 44\n26 47\n25 50\n24 51\n23 52\n22 51\n21 50\n20 51\n19 50\n18 23\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n11 16\n10 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n26 49\n25 48\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n28 47\n27 46\n26 45\n25 44\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n32 43\n31 40\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n34 39\n33 38\n32 37\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n9 12\n10 11\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n58 63\n57 66\n56 67\n55 70\n54 69\n53 70\n52 73\n51 74\n50 75\n49 76\n48 77\n47 76\n46 75\n45 74\n44 73\n43 72\n42 71\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n47 70\n46 69\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n49 68\n48 67\n47 66\n46 65\n45 64\n44 63\n43 62\n42 61\n41 60\n40 59\n39 58\n38 57\n37 56\n36 55\n35 54\n34 53\n33 52\n32 51\n31 50\n30 47\n29 46\n28 45\n27 44\n26 43\n25 42\n24 41\n23 40\n22 39\n21 38\n20 37\n19 36\n18 35\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n13 16\n14 15\n25 34\n26 33\n25 32\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n28 31\n29 30\n80 81\n79 84\n78 85\n77 86\n76 85\n75 86\n74 89\n73 90\n72 91\n71 96\n70 99\n69 100\n68 99\n67 98\n66 97\n65 96\n64 95\n63 94\n62 93\n61 92\n60 91\n59 90\n60 89\n59 88\n58 87\n57 86\n56 85\n55 84\n54 83\n53 82\n52 81\n51 80\n50 79\n49 78\n48 69\n47 68\n46 67\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n33 40\n32 39\n31 38\n30 37\n31 36\n32 35\n31 34\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n55 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n72 77\n71 76\n70 75\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n"}, {"input": "100\r\n245 230 240 248 247 235 240 228 247 243 244 240 246 234 244 247 247 232 247 233 241 247 236 247 230 228 243 237 246 231 246 231 233 235 229 244 247 248 245 248 231 230 238 247 235 248 240 239 233 232 230 229 229 244 247 246 248 247 247 234 243 242 247 228 238 238 236 243 236 228 229 245 232 246 241 243 248 235 242 237 244 239 238 245 231 235 234 237 238 237 234 232 231 236 233 238 228 243 242 246\r\n", "output": "1 2\n2 5\n5 14\n4 7\n3 8\n2 15\n3 12\n2 3\n1 2\n6 13\n7 14\n6 15\n7 14\n6 7\n5 6\n4 5\n3 4\n10 15\n15 20\n16 25\n15 32\n14 35\n13 34\n12 35\n11 36\n10 35\n9 32\n8 33\n7 34\n6 35\n5 8\n4 7\n3 6\n4 5\n11 36\n10 35\n9 34\n8 33\n7 32\n8 33\n7 30\n6 25\n5 8\n4 7\n3 6\n4 5\n3 4\n2 3\n11 22\n10 19\n9 18\n8 17\n7 16\n6 15\n5 14\n6 7\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 29\n33 34\n34 35\n35 36\n36 41\n37 42\n36 51\n35 52\n34 53\n33 52\n32 53\n31 52\n30 53\n29 64\n28 55\n27 56\n26 53\n27 54\n26 69\n25 70\n24 49\n23 48\n22 47\n21 46\n20 45\n19 44\n18 43\n17 40\n16 39\n15 38\n14 21\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n9 10\n8 9\n7 8\n6 7\n19 20\n18 19\n17 18\n16 17\n24 37\n23 34\n22 33\n21 32\n20 31\n19 30\n18 29\n17 28\n16 27\n15 26\n14 23\n13 22\n14 21\n13 20\n12 19\n11 18\n10 17\n9 16\n8 15\n7 12\n8 11\n7 10\n6 9\n5 8\n4 7\n5 6\n52 75\n53 82\n54 87\n53 70\n52 71\n51 72\n50 73\n49 70\n48 71\n47 86\n46 87\n45 92\n44 99\n43 96\n42 95\n41 94\n40 95\n39 96\n38 95\n37 96\n36 97\n35 98\n34 99\n33 94\n32 95\n31 98\n30 97\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n33 98\n32 99\n31 100\n30 95\n29 96\n28 97\n27 98\n26 99\n25 42\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 41\n27 38\n26 37\n25 36\n26 35\n27 34\n26 31\n25 30\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n7 10\n6 9\n5 6\n4 5\n45 98\n46 97\n47 98\n46 99\n45 98\n44 97\n43 96\n42 91\n41 90\n40 89\n39 88\n38 87\n37 86\n36 85\n35 84\n34 83\n33 78\n32 35\n31 34\n30 33\n29 30\n28 29\n27 28\n38 77\n37 72\n36 69\n37 68\n36 67\n35 64\n34 63\n33 62\n32 57\n31 56\n30 55\n29 34\n28 33\n27 32\n26 31\n25 28\n24 27\n23 26\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n37 54\n38 53\n37 52\n36 51\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 37\n27 36\n26 35\n25 34\n24 33\n23 32\n22 31\n21 28\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n49 50\n60 61\n75 76\n81 82\n94 95\n"}, {"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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": ""}, {"input": "100\r\n2 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 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "43 44\n42 45\n41 46\n40 47\n39 48\n38 49\n37 50\n36 51\n35 52\n34 53\n33 54\n32 55\n31 56\n30 57\n29 58\n28 59\n27 60\n26 61\n25 62\n24 63\n23 64\n22 65\n21 66\n20 67\n19 68\n18 69\n17 70\n16 71\n15 72\n14 73\n13 74\n12 75\n11 76\n10 77\n9 78\n8 79\n7 80\n6 81\n5 82\n4 83\n3 84\n2 85\n1 86\n2 87\n3 88\n4 89\n5 90\n6 91\n7 92\n8 93\n9 94\n10 95\n11 96\n12 97\n13 98\n14 99\n15 100\n16 99\n17 98\n18 97\n19 96\n20 95\n21 94\n22 93\n23 92\n24 91\n25 90\n26 89\n27 88\n28 87\n29 86\n30 85\n31 84\n32 83\n33 82\n34 81\n35 80\n36 79\n37 78\n38 77\n39 76\n40 75\n41 74\n42 73\n43 72\n44 71\n45 70\n46 69\n47 68\n48 67\n49 66\n50 65\n51 64\n52 63\n53 62\n54 61\n55 60\n56 59\n57 58\n"}, {"input": "1\r\n1\r\n", "output": ""}, {"input": "10\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": ""}, {"input": "98\r\n5 5 5 5 5 5 5 6 6 6 6 7 6 7 7 7 7 7 7 8 8 8 8 8 8 9 8 9 9 9 9 10 9 10 9 11 10 11 11 11 11 12 12 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 14 15 15 15 15 15 15 15 16 16 16 16 17 17 17 17 17 17 18 17 18 18 18 19 19 19 19 19 20 19 20 20 20 20 20 20 20\r\n", "output": "12 13\r\n26 27\r\n32 33\r\n34 35\r\n36 37\r\n80 81\r\n90 91\r\n33 34\r\n"}, {"input": "20\r\n2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2\r\n", "output": "6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n"}, {"input": "1\r\n5\r\n", "output": ""}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
with open(input_path, 'r') as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
with open(submission_output_path, 'r') as f:
lines = f.readlines()
ops = []
for line in lines:
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
l = int(parts[0])
r = int(parts[1])
except:
print(0)
return
ops.append((l, r))
if len(ops) > 20000:
print(0)
return
arr = a.copy()
for l, r in ops:
if l < 1 or r > n or l >= r or (r - l + 1) % 2 != 0:
print(0)
return
for i in range(l-1, r, 2):
if i+1 < len(arr):
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted_ok = all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
print(100 if sorted_ok else 0)
if __name__ == '__main__':
main()
| true
|
504/D
|
504
|
D
|
PyPy 3
|
TESTS
| 5
| 764
| 10,137,600
|
108515065
|
m = int(input())
values = []
idx = []
for i in range(m):
x = int(input())
ans = 0
for j,xx in enumerate(values):
if (xx^x) < x:
x^=xx
ans^=idx[j]
if x == 0:
anss = []
for j,xx in enumerate(values):
if (ans&1)!=0:
anss.append(j)
ans>>=1
print(len(anss),*anss)
else:
print(0)
values.append(x)
idx.append(ans^(2**i))
| 52
| 623
| 64,512,000
|
70796734
|
import sys, os
numbs = [int(x) for x in sys.stdin.buffer.read().split()]
n = numbs.pop(0)
base = []
out = []
for i in range(n):
x = numbs[i]
how = 0
for b,rep in base:
if x.bit_length() == b.bit_length():
x ^= b
how ^= rep
if x:
how |= 1 << i
a = 0
b = len(base)
while a < b:
c = a + b >> 1
if base[c][0] > x:
a = c + 1
else:
b = c
base.insert(a, (x, how))
out.append(0)
else:
outind = len(out)
out.append(-1)
y = bin(how).encode('ascii')
ylen = len(y)
for i in range(2,len(y)):
if y[i] == 49:
out.append(ylen - 1 - i)
out[outind] = len(out) - 1 - outind
os.write(1, b'\n'.join(str(x).encode('ascii') for x in out))
|
Codeforces Round 285 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Misha and XOR
|
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x?
If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket.
Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on.
Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him.
|
The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room.
The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10600 that doesn't contain leading zeroes.
|
For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once.
| null |
The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
|
[{"input": "7\n7\n6\n5\n4\n3\n2\n1", "output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1"}, {"input": "2\n5\n5", "output": "0\n1 0"}]
| 2,700
|
["bitmasks"]
| 52
|
[{"input": "7\r\n7\r\n6\r\n5\r\n4\r\n3\r\n2\r\n1\r\n", "output": "0\r\n0\r\n0\r\n3 0 1 2\r\n2 1 2\r\n2 0 2\r\n2 0 1\r\n"}, {"input": "2\r\n5\r\n5\r\n", "output": "0\r\n1 0\r\n"}, {"input": "10\r\n81\r\n97\r\n12\r\n2\r\n16\r\n96\r\n80\r\n99\r\n6\r\n83\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n3 0 1 5\r\n2 1 3\r\n0\r\n2 0 3\r\n"}, {"input": "10\r\n15106\r\n13599\r\n69319\r\n33224\r\n26930\r\n94490\r\n85089\r\n60931\r\n23137\r\n62868\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "10\r\n5059464500\r\n8210395556\r\n3004213265\r\n248593357\r\n5644084048\r\n9359824793\r\n8120649160\r\n4288978422\r\n183848555\r\n8135845959\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "10\r\n4\r\n12\r\n28\r\n29\r\n31\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n"}, {"input": "10\r\n16\r\n24\r\n28\r\n30\r\n31\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n"}, {"input": "10\r\n16\r\n8\r\n4\r\n2\r\n1\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n"}, {"input": "10\r\n1\r\n2\r\n4\r\n8\r\n16\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n"}]
| false
|
stdio
| null | true
|
33/A
|
33
|
A
|
Python 3
|
TESTS
| 7
| 186
| 6,963,200
|
85186639
|
n,m,k=input().split()
n=int(n)
m=int(m)
k=int(k)
l=[]
h=[]
s=0
for i in range(n):
l.insert(i,input())
l.sort()
d=l[0].split()
d=int(d[1])
p=1
for i in l:
a,b=i.split()
a=int(a)
b=int(b)
if(a!=p):
p+=1
s=s+d
d=b
if(a==p):
if(d>b):
d=b
if(p==m and i==l[n-1]):
s=s+d
if(s>k):
print(k)
else:
print(s)
| 31
| 92
| 0
|
170452271
|
n,m,k = map(int,input().split(' '))
bz = [1000000]*(m+1)
while n:
r,c = map(int,input().split(' '))
bz[r] = min(bz[r],c)
n-=1
sum = 0
for i in bz[1:]:
sum += i
print(min(sum,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
|
803/E
|
803
|
E
|
PyPy 3
|
TESTS
| 20
| 140
| 10,752,000
|
138349448
|
n, k = map(int, input().split())
s = str(input())
dp = [[False]*(2*k+1) for i in range(n+1)]
dp[0][k] = True
for i, c in enumerate(s):
for j in range(1, 2*k):
if not dp[i][j]:
continue
if c == 'W' and j+1 <= 2*k:
dp[i+1][j+1] = True
if c == 'D':
dp[i+1][j] = True
if c == 'L' and j-1 >= 0:
dp[i+1][j-1] = True
if c == '?':
for dj in range(-1, 2):
nj = j+dj
if 0 <= nj <= 2*k:
dp[i+1][nj] = True
if dp[n][0]:
ans = []
cur = 0
for i in reversed(range(n)):
c = s[i]
if c == 'W':
cur -= 1
elif c == 'L':
cur += 1
elif c == 'D':
cur = cur
else:
if cur-1 >= 0 and cur-1 != 0 and dp[i][cur-1]:
cur = cur-1
c = 'W'
elif cur != 0 and cur != 2*k and dp[i][cur]:
cur = cur
c = 'D'
elif cur+1 <= 2*k and cur+1 != 2*k and dp[i][cur+1]:
cur = cur+1
c = 'L'
ans.append(c)
ans.reverse()
print(''.join(ans))
elif dp[n][2*k]:
ans = []
cur = 0
for i in reversed(range(n)):
c = s[i]
if c == 'W':
cur -= 1
elif c == 'L':
cur += 1
elif c == 'D':
cur = cur
else:
if cur-1 >= 0 and cur-1 != 0 and dp[i][cur-1]:
cur = cur-1
c = 'W'
elif cur != 0 and cur != 2*k and dp[i][cur]:
cur = cur
c = 'D'
elif cur+1 <= 2*k and cur+1 != 2*k and dp[i][cur+1]:
cur = cur+1
c = 'L'
ans.append(c)
ans.reverse()
print(''.join(ans))
else:
print('NO')
| 28
| 62
| 409,600
|
26883241
|
def ma():
s=input()
v=s.split(' ')
n=int(v[0])
k=int(v[1])
s=input()
dp=[[0,0] for _ in range(n+1)]
flag=True
for i in range (1,n):
c=s[i-1]
if c=='?':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
elif c=='D':
dp[i][0]=dp[i-1][0]
dp[i][1]=dp[i-1][1]
elif c=='W':
dp[i][0]=min(dp[i-1][0]+1 ,k-1)
dp[i][1]=dp[i-1][1]+1
if dp[i][1]==k:
flag=False
elif c=='L':
dp[i][0]=dp[i-1][0]-1
dp[i][1]=max(dp[i-1][1]-1 ,-k+1)
if dp[i][0]==-k:
flag=False
if not flag:
print('NO')
return
i=n
if s[i-1]=='?':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]-1
elif s[i-1]=='D':
dp[i][0]=dp[i-1][0]
dp[i][1]=dp[i-1][1]
elif s[i-1]=='W':
dp[i][0]=dp[i-1][0]+1
dp[i][1]=dp[i-1][1]+1
elif s[i-1]=='L':
dp[i][0]=dp[i-1][0]-1
dp[i][1]=dp[i-1][1]-1
res=['?']*n
if dp[i][0]==k or dp[i][1]==-k:
if dp[i][0]==k:
cur=k
else:
cur=-k
for i in range(n-1,-1,-1):
c=s[i]
if c=='?':
if cur>dp[i][0]:
cur=cur-1
res[i]='W'
elif dp[i][1]<=cur<=dp[i][0]:
cur=cur
res[i]='D'
elif cur<dp[i][1]:
cur=cur+1
res[i]='L'
elif c=='D':
cur=cur
res[i]=c
elif c=='W':
cur=cur-1
res[i]=c
elif c=='L':
cur=cur+1
res[i]=c
for i in range(n):
print(res[i],end='')
else:
print('NO')
ma()
|
Educational Codeforces Round 20
|
ICPC
| 2,017
| 2
| 256
|
Roma and Poker
|
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
- In the end the absolute difference between the number of wins and loses is equal to k;
- There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
|
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
|
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
| null | null |
[{"input": "3 2\nL??", "output": "LDL"}, {"input": "3 1\nW??", "output": "NO"}, {"input": "20 5\n?LLLLLWWWWW?????????", "output": "WLLLLLWWWWWWWWLWLWDW"}]
| 2,000
|
["dp", "graphs"]
| 28
|
[{"input": "3 2\r\nL??\r\n", "output": "LDL\r\n"}, {"input": "3 1\r\nW??\r\n", "output": "NO\r\n"}, {"input": "20 5\r\n?LLLLLWWWWW?????????\r\n", "output": "WLLLLLWWWWWWWWLWLWDW\r\n"}, {"input": "5 5\r\n?WDDD\r\n", "output": "NO\r\n"}, {"input": "5 3\r\n??D??\r\n", "output": "WWDDW\r\n"}, {"input": "10 1\r\nD??W?WL?DW\r\n", "output": "NO\r\n"}, {"input": "10 3\r\nDWD?DL??LL\r\n", "output": "DWDWDLLLLL\r\n"}, {"input": "10 2\r\nLWL?WWDDW?\r\n", "output": "NO\r\n"}, {"input": "1 1\r\n?\r\n", "output": "W\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:
n, k = map(int, f.readline().split())
s = f.readline().strip()
with open(submission_path, 'r') as f:
submission = f.read().strip()
if submission == "NO":
possible = check_possible(n, k, s)
print(1 if not possible else 0)
else:
valid = check_submission(n, k, s, submission)
print(1 if valid else 0)
def check_possible(n, k, s):
dp = [set() for _ in range(n+1)]
dp[0].add(0)
for i in range(n):
current_char = s[i]
next_dp = set()
for j in dp[i]:
moves = []
if current_char == 'W':
moves = ['W']
elif current_char == 'L':
moves = ['L']
elif current_char == 'D':
moves = ['D']
else:
moves = ['W', 'L', 'D']
for move in moves:
new_j = j
if move == 'W':
new_j += 1
elif move == 'L':
new_j -= 1
if i == n-1:
if abs(new_j) == k:
next_dp.add(new_j)
else:
if abs(new_j) < k:
next_dp.add(new_j)
dp[i+1] = next_dp
return k in dp[n] or -k in dp[n]
def check_submission(n, k, s, submission):
if len(submission) != n:
return False
balance = 0
for i in range(n):
sub_char = submission[i]
orig_char = s[i]
if orig_char != '?' and sub_char != orig_char:
return False
if sub_char not in ['W', 'L', 'D']:
return False
if sub_char == 'W':
balance += 1
elif sub_char == 'L':
balance -= 1
if i < n-1 and abs(balance) >= k:
return False
return abs(balance) == k
if __name__ == "__main__":
main()
| true
|
803/E
|
803
|
E
|
Python 3
|
TESTS
| 22
| 61
| 307,200
|
26963710
|
from sys import exit
n, k = [int(i) for i in input().split()]
s = [i for i in input()]
p = [0] * (n + 1)
qind = -1
def check():
global s
b = 0
for i in range(len(s)):
if s[i] == 'L':
b -= 1
elif s[i] == 'W':
b += 1
if i != len(s) - 1 and abs(b) >= k:
return False
return True
def solved():
for i in s:
if i == '?':
print('D', end='')
else:
print(i, end='')
exit(0)
def no_ans():
print('NO')
exit(0)
for i in range(len(s)):
p[i + 1] = p[i]
if s[i] == 'L':
p[i + 1] -= 1
elif s[i] == 'W':
p[i + 1] += 1
if abs(p[i + 1]) >= k and i != len(s) - 1:
while qind < i and (qind == -1 or s[qind] != '?'):
qind += 1
if qind == i and s[i] != '?':
no_ans()
s[qind] = 'L' if p[i + 1] > 0 else 'W'
p[i + 1] = k - 1 if p[i + 1] > 0 else -k + 1
scpy = s[::]
rest = k - p[n]
j = n - 1
while j >= 0 and rest > 0:
if s[j] == '?':
s[j] = 'W'
rest -= 1
j -= 1
ok = (rest == 0) and check()
if ok:
solved()
s = scpy[::]
rest = (p[n] + k)
j = n - 1
while j >= 0 and rest > 0:
if s[j] == '?':
s[j] = 'L'
rest -= 1
j -= 1
ok = (rest == 0) and check()
if not ok:
no_ans()
solved()
| 28
| 93
| 20,889,600
|
180996735
|
n,k=map(int,input().split())
s=input()
m=2*k+1
dp=[[0]*m for i in range(n+1)]
dp[0][k]=1
for i in range(n):
if s[i]=='L' or s[i]=='?':
for j in range(m-1):
dp[i+1][j]|=dp[i][j+1]
if s[i]=='D' or s[i]=='?':
for j in range(m):
dp[i+1][j]|=dp[i][j]
if s[i]=='W' or s[i]=='?':
for j in range(1,m):
dp[i+1][j]|=dp[i][j-1]
if i!=n-1:
dp[i+1][0]=0
dp[i+1][2*k]=0
if dp[n][0]:
tmp=0
elif dp[n][2*k]:
tmp=2*k
else:
print('NO')
exit()
ans=''
for i in range(n-1,-1,-1):
if s[i]=='L':
ans+='L'
tmp+=1
elif s[i]=='D':
ans+='D'
elif s[i]=='W':
ans+='W'
tmp-=1
else:
if tmp>0 and dp[i][tmp-1]:
ans+='W'
tmp-=1
elif dp[i][tmp]:
ans+='D'
elif tmp<2*k and dp[i][tmp+1]:
ans+='L'
tmp+=1
else:
raise Exception
print(ans[::-1])
|
Educational Codeforces Round 20
|
ICPC
| 2,017
| 2
| 256
|
Roma and Poker
|
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.
The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
- In the end the absolute difference between the number of wins and loses is equal to k;
- There is no hand such that the absolute difference before this hand was equal to k.
Help Roma to restore any such sequence.
|
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).
The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
|
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them.
| null | null |
[{"input": "3 2\nL??", "output": "LDL"}, {"input": "3 1\nW??", "output": "NO"}, {"input": "20 5\n?LLLLLWWWWW?????????", "output": "WLLLLLWWWWWWWWLWLWDW"}]
| 2,000
|
["dp", "graphs"]
| 28
|
[{"input": "3 2\r\nL??\r\n", "output": "LDL\r\n"}, {"input": "3 1\r\nW??\r\n", "output": "NO\r\n"}, {"input": "20 5\r\n?LLLLLWWWWW?????????\r\n", "output": "WLLLLLWWWWWWWWLWLWDW\r\n"}, {"input": "5 5\r\n?WDDD\r\n", "output": "NO\r\n"}, {"input": "5 3\r\n??D??\r\n", "output": "WWDDW\r\n"}, {"input": "10 1\r\nD??W?WL?DW\r\n", "output": "NO\r\n"}, {"input": "10 3\r\nDWD?DL??LL\r\n", "output": "DWDWDLLLLL\r\n"}, {"input": "10 2\r\nLWL?WWDDW?\r\n", "output": "NO\r\n"}, {"input": "1 1\r\n?\r\n", "output": "W\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:
n, k = map(int, f.readline().split())
s = f.readline().strip()
with open(submission_path, 'r') as f:
submission = f.read().strip()
if submission == "NO":
possible = check_possible(n, k, s)
print(1 if not possible else 0)
else:
valid = check_submission(n, k, s, submission)
print(1 if valid else 0)
def check_possible(n, k, s):
dp = [set() for _ in range(n+1)]
dp[0].add(0)
for i in range(n):
current_char = s[i]
next_dp = set()
for j in dp[i]:
moves = []
if current_char == 'W':
moves = ['W']
elif current_char == 'L':
moves = ['L']
elif current_char == 'D':
moves = ['D']
else:
moves = ['W', 'L', 'D']
for move in moves:
new_j = j
if move == 'W':
new_j += 1
elif move == 'L':
new_j -= 1
if i == n-1:
if abs(new_j) == k:
next_dp.add(new_j)
else:
if abs(new_j) < k:
next_dp.add(new_j)
dp[i+1] = next_dp
return k in dp[n] or -k in dp[n]
def check_submission(n, k, s, submission):
if len(submission) != n:
return False
balance = 0
for i in range(n):
sub_char = submission[i]
orig_char = s[i]
if orig_char != '?' and sub_char != orig_char:
return False
if sub_char not in ['W', 'L', 'D']:
return False
if sub_char == 'W':
balance += 1
elif sub_char == 'L':
balance -= 1
if i < n-1 and abs(balance) >= k:
return False
return abs(balance) == k
if __name__ == "__main__":
main()
| true
|
686/B
|
686
|
B
|
Python 3
|
TESTS
| 2
| 93
| 0
|
85887689
|
n = int(input())
l = list(map (int , input().split()))
tmp = sorted(l)
i = 1
while l != tmp and i < n:
if l[i] < l[i-1] :
l[i] , l[i-1] = l[i-1] , l[i]
print(i , i+1)
i+=1
| 37
| 62
| 307,200
|
111147375
|
def isSorted(a):
for i in range(0, len(a)-1):
if a[i] > a[i+1]:
return False
return True
n = int(input())
a = [int(x) for x in input().split()]
while not isSorted(a):
for i in range(0,n-1):
if a[i] > a[i+1]:
print(i+1,i+2)
a[i+1],a[i] = a[i],a[i+1]
|
Codeforces Round 359 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Little Robber Girl's Zoo
|
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.
The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.
Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
|
Print the sequence of operations that will rearrange the animals by non-decreasing height.
The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed.
The number of operations should not exceed 20 000.
If the animals are arranged correctly from the start, you are allowed to output nothing.
| null |
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
|
[{"input": "4\n2 1 4 3", "output": "1 4"}, {"input": "7\n36 28 57 39 66 69 68", "output": "1 4\n6 7"}, {"input": "5\n1 2 1 2 1", "output": "2 5\n3 4\n1 4\n1 4"}]
| 1,100
|
["constructive algorithms", "implementation", "sortings"]
| 37
|
[{"input": "4\r\n2 1 4 3\r\n", "output": "1 2\r\n3 4\r\n"}, {"input": "7\r\n36 28 57 39 66 69 68\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "2 3\r\n4 5\r\n3 4\r\n"}, {"input": "78\r\n7 3 8 8 9 8 10 9 12 11 16 14 17 17 18 18 20 20 25 22 27 26 29 27 35 35 36 36 37 37 38 38 40 39 42 42 48 46 49 49 58 50 60 58 65 61 68 66 69 69 69 69 70 69 71 71 77 73 78 77 80 79 85 83 86 86 86 86 88 87 91 90 96 91 98 97 99 98\r\n", "output": "1 2\r\n5 6\r\n7 8\r\n9 10\r\n11 12\r\n19 20\r\n21 22\r\n23 24\r\n33 34\r\n37 38\r\n41 42\r\n43 44\r\n45 46\r\n47 48\r\n53 54\r\n57 58\r\n59 60\r\n61 62\r\n63 64\r\n69 70\r\n71 72\r\n73 74\r\n75 76\r\n77 78\r\n"}, {"input": "47\r\n17462271 17619887 84928078 85012448 86003043 86499508 129945397 135545949 141951561 161227989 167459883 215243263 243500622 284922151 326608661 350939646 359121478 388866754 441241406 460673749 497339472 512847269 521291845 537280636 561101359 569788868 581107309 588113051 619849749 621125808 637220955 640578896 660913258 674411824 694527254 716291982 725198795 734631740 797705515 810502965 832854453 866687928 923880462 948282076 957759163 968324020 997176104\r\n", "output": ""}, {"input": "99\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "20 21\n19 22\n18 23\n17 24\n16 25\n15 26\n14 27\n13 28\n12 29\n11 30\n10 31\n9 32\n8 35\n7 36\n6 37\n5 38\n4 39\n3 40\n2 41\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 51\n11 52\n12 55\n13 56\n14 57\n13 58\n12 59\n11 60\n10 61\n9 62\n8 63\n7 64\n6 65\n5 66\n4 67\n3 68\n2 69\n1 70\n2 71\n3 72\n4 73\n5 74\n6 75\n7 76\n8 77\n9 78\n10 79\n11 82\n12 83\n13 84\n14 85\n15 86\n16 87\n17 88\n18 89\n19 90\n20 91\n19 92\n18 93\n17 94\n16 95\n15 96\n14 97\n13 98\n12 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 97\n3 96\n2 95\n1 94\n2 93\n3 92\n4 91\n5 90\n6 89\n7 88\n8 87\n9 86\n10 85\n11 84\n12 83\n13 82\n14 81\n15 80\n16 77\n17 76\n18 75\n19 74\n20 73\n21 72\n22 71\n23 70\n24 69\n25 68\n26 67\n27 66\n26 63\n25 62\n24 61\n23 60\n22 59\n21 58\n20 57\n19 56\n18 55\n17 54\n16 53\n15 52\n14 51\n13 50\n12 49\n11 48\n10 47\n9 46\n8 43\n7 42\n6 41\n5 40\n4 39\n3 38\n2 37\n1 36\n2 35\n3 34\n4 33\n5 32\n6 31\n7 30\n8 29\n9 28\n10 27\n11 26\n12 25\n13 24\n14 23\n15 22\n16 21\n17 20\n18 19\n"}, {"input": "99\r\n4577 4577 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4575 4575 4575 4575 4575 4575 4574 4574 4574 4574 4574 4574 4574 4574 4574 4574 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4571 4571 4571 4571 4571 4571 4571 4571 4571 4570 4570 4570 4570 4570 4570 4570 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4567 4567 4567 4567 4567 4567 4567 4567 4567\r\n", "output": "2 3\n1 4\n2 5\n3 6\n4 7\n5 8\n6 9\n7 10\n8 11\n9 14\n10 15\n11 16\n10 17\n9 20\n8 21\n7 22\n6 23\n5 24\n4 25\n3 26\n2 27\n1 30\n2 31\n3 32\n4 33\n5 34\n6 35\n5 36\n4 37\n3 38\n2 39\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 53\n9 54\n8 55\n7 56\n6 57\n5 58\n4 59\n3 62\n2 63\n1 64\n2 65\n3 66\n4 69\n5 70\n6 71\n7 72\n8 73\n9 74\n10 75\n11 76\n12 77\n11 80\n10 81\n9 82\n8 83\n7 84\n6 85\n5 86\n4 87\n3 88\n2 91\n1 92\n2 93\n3 94\n4 95\n5 96\n6 97\n7 98\n8 99\n9 98\n10 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 99\n3 98\n2 99\n1 98\n2 99\n3 98\n4 99\n5 98\n6 99\n7 98\n8 95\n9 94\n8 93\n7 92\n6 91\n5 90\n4 89\n3 88\n2 87\n1 84\n2 83\n3 82\n4 81\n5 78\n6 77\n7 76\n6 75\n5 74\n4 73\n3 72\n2 71\n1 68\n2 67\n3 66\n4 65\n5 64\n6 63\n7 62\n8 61\n9 60\n10 59\n11 56\n10 55\n9 54\n8 53\n7 52\n6 51\n5 50\n4 49\n3 48\n2 45\n1 44\n2 43\n3 42\n4 41\n5 40\n6 39\n7 36\n8 35\n9 34\n10 33\n11 32\n10 29\n9 28\n8 27\n7 26\n6 25\n5 24\n4 23\n3 22\n2 21\n1 18\n2 17\n3 16\n4 15\n5 14\n6 13\n7 12\n8 11\n9 10\n"}, {"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": ""}, {"input": "10\r\n44 23 65 17 48 29 49 88 91 85\r\n", "output": "1 2\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n9 10\r\n2 3\r\n4 5\r\n8 9\r\n1 2\r\n3 4\r\n"}, {"input": "13\r\n605297997 425887240 859639341 200428931 888317166 983420497 81642057 628988722 389959969 358920886 646428392 324912711 401437250\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n3 4\r\n5 6\r\n6 7\r\n8 9\r\n9 10\r\n2 3\r\n4 5\r\n5 6\r\n7 8\r\n8 9\r\n1 2\r\n3 4\r\n4 5\r\n6 7\r\n7 8\r\n3 4\r\n5 6\r\n6 7\r\n4 5\r\n3 4\r\n"}, {"input": "43\r\n644870843 160471908 227474511 47341477 175939701 563067024 749818136 707986934 201095131 736488829 346428456 342944986 316696712 101551423 672610101 897020945 708299245 587795677 408207112 985104524 278945228 192250326 157154304 301319412 270702270 954096281 649990285 37649442 300182190 382249227 605285302 392816037 419998044 84624133 332174228 996770879 816912092 283973844 498255316 374935144 294452244 529912248 553039417\r\n", "output": "1 4\n2 5\n1 4\n4 5\n5 8\n8 13\n7 14\n6 13\n5 14\n4 27\n7 22\n6 23\n7 10\n6 9\n5 6\n4 5\n3 4\n2 3\n15 24\n14 25\n13 28\n12 23\n11 24\n10 29\n11 34\n10 37\n9 34\n8 35\n7 34\n8 15\n7 14\n6 13\n5 12\n4 7\n3 4\n20 35\n19 40\n18 41\n17 40\n16 31\n15 32\n14 29\n13 28\n12 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n2 3\n1 2\n18 27\n19 26\n18 23\n17 20\n16 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n34 41\n33 42\n32 43\n31 42\n30 41\n29 42\n28 39\n27 38\n26 37\n25 36\n24 35\n23 34\n22 33\n21 32\n20 31\n19 24\n18 23\n17 18\n16 17\n15 16\n14 15\n13 14\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n27 30\n28 29\n"}, {"input": "97\r\n1 1 1 2 1 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 1 1 2 1 1 1 1 2 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 1 1 1 2 2 1 1 2 1 1 1 1 2 2 1 2 1 2 1 1 2 2 2 1 2 2 1 1 2 2 2 1 1 2 1 2 1 1 2\r\n", "output": "4 5\n5 6\n6 9\n7 10\n8 11\n9 12\n10 13\n11 12\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n13 14\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n15 16\n34 35\n33 38\n32 39\n31 40\n30 45\n29 44\n28 43\n27 42\n26 41\n25 40\n24 39\n23 38\n22 37\n21 36\n20 35\n19 34\n18 33\n17 32\n16 31\n17 30\n18 29\n19 28\n20 27\n21 26\n22 25\n23 24\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n58 59\n57 60\n56 61\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n31 36\n30 35\n29 34\n28 33\n27 32\n26 31\n25 30\n26 29\n27 28\n63 64\n62 67\n61 68\n60 69\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 39\n29 38\n30 37\n31 36\n32 35\n33 34\n72 77\n71 78\n70 77\n69 76\n68 75\n67 74\n66 73\n65 72\n64 71\n63 70\n62 69\n61 68\n60 67\n59 66\n58 65\n57 64\n56 63\n55 62\n54 61\n53 60\n52 59\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n84 85\n83 86\n82 85\n81 84\n80 83\n79 82\n78 81\n77 80\n76 79\n75 78\n74 77\n73 76\n72 75\n71 74\n70 73\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 62\n58 61\n57 60\n56 59\n55 58\n54 57\n53 56\n52 55\n51 54\n50 53\n49 52\n48 51\n47 50\n46 49\n45 48\n44 47\n43 46\n42 45\n41 44\n40 43\n39 42\n40 41\n89 90\n88 95\n87 96\n86 95\n85 94\n84 93\n83 92\n82 91\n81 90\n80 89\n79 88\n78 87\n77 86\n76 85\n75 84\n74 83\n73 82\n72 81\n71 80\n70 79\n69 78\n68 77\n67 76\n66 75\n65 74\n64 73\n63 72\n62 71\n61 70\n60 69\n59 68\n58 67\n57 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 60\n50 59\n49 58\n48 57\n47 56\n46 55\n45 54\n44 53\n43 52\n42 51\n41 50\n42 49\n43 48\n44 47\n45 46\n"}, {"input": "87\r\n2 2 1 2 3 1 3 2 3 2 3 3 1 3 3 3 2 2 1 1 2 3 2 1 2 2 3 3 1 1 1 3 2 3 1 2 1 3 3 3 3 3 3 2 3 2 3 3 2 1 1 3 1 1 3 3 2 3 1 1 3 3 3 2 3 1 3 2 2 2 1 3 3 3 1 1 2 3 2 3 2 1 3 3 3 1 3\r\n", "output": "2 3\n1 2\n5 10\n4 9\n3 4\n2 3\n7 8\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n16 19\n15 20\n14 19\n13 20\n12 23\n11 24\n10 25\n9 26\n10 25\n9 24\n8 23\n7 10\n6 9\n5 8\n4 7\n5 6\n13 22\n14 21\n15 20\n14 19\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n17 18\n28 29\n27 30\n26 37\n25 36\n24 37\n23 36\n22 35\n21 34\n20 33\n19 32\n18 31\n17 30\n16 29\n15 28\n14 19\n13 18\n12 17\n11 16\n10 15\n9 14\n8 13\n7 12\n8 11\n9 10\n22 27\n21 26\n20 25\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n11 12\n43 46\n42 45\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n26 27\n48 49\n47 50\n46 53\n45 54\n44 53\n43 52\n42 51\n41 50\n40 49\n39 48\n38 47\n37 46\n36 45\n35 44\n34 43\n33 42\n32 41\n31 40\n30 39\n29 38\n28 37\n27 36\n28 35\n27 34\n26 33\n25 32\n24 31\n23 30\n22 29\n21 28\n20 27\n19 26\n18 25\n17 24\n16 23\n15 22\n14 21\n13 20\n12 19\n13 18\n14 17\n15 16\n56 59\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n17 18\n63 68\n62 71\n61 70\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n36 45\n35 44\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n38 43\n39 42\n38 41\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n74 75\n73 76\n72 81\n71 82\n70 81\n69 80\n68 79\n67 78\n66 77\n65 76\n64 75\n63 74\n62 73\n61 72\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n21 22\n44 49\n45 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n85 86\n84 85\n83 84\n82 83\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n"}, {"input": "100\r\n3 2 5 4 3 3 3 3 4 3 1 2 3 2 3 1 4 1 5 2 5 3 3 5 2 3 5 4 3 4 1 5 5 2 2 1 3 5 1 3 5 2 2 1 4 3 1 3 5 1 1 3 5 5 5 4 5 5 1 5 3 5 4 3 5 4 1 1 2 1 2 5 1 2 2 2 3 5 5 5 4 2 3 2 1 2 3 5 2 2 2 2 5 3 5 4 2 5 3 4\r\n", "output": "1 4\n4 5\n3 6\n4 7\n5 10\n6 11\n7 22\n8 25\n7 28\n6 17\n5 16\n4 17\n3 18\n2 31\n1 12\n4 11\n5 10\n4 9\n3 8\n2 5\n3 4\n15 30\n16 31\n17 22\n18 21\n17 22\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n22 23\n23 30\n24 29\n23 28\n22 27\n21 24\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n33 36\n32 35\n31 36\n30 39\n29 46\n28 43\n27 44\n26 47\n25 50\n24 51\n23 52\n22 51\n21 50\n20 51\n19 50\n18 23\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n11 16\n10 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n26 49\n25 48\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n28 47\n27 46\n26 45\n25 44\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n32 43\n31 40\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n34 39\n33 38\n32 37\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n9 12\n10 11\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n58 63\n57 66\n56 67\n55 70\n54 69\n53 70\n52 73\n51 74\n50 75\n49 76\n48 77\n47 76\n46 75\n45 74\n44 73\n43 72\n42 71\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n47 70\n46 69\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n49 68\n48 67\n47 66\n46 65\n45 64\n44 63\n43 62\n42 61\n41 60\n40 59\n39 58\n38 57\n37 56\n36 55\n35 54\n34 53\n33 52\n32 51\n31 50\n30 47\n29 46\n28 45\n27 44\n26 43\n25 42\n24 41\n23 40\n22 39\n21 38\n20 37\n19 36\n18 35\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n13 16\n14 15\n25 34\n26 33\n25 32\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n28 31\n29 30\n80 81\n79 84\n78 85\n77 86\n76 85\n75 86\n74 89\n73 90\n72 91\n71 96\n70 99\n69 100\n68 99\n67 98\n66 97\n65 96\n64 95\n63 94\n62 93\n61 92\n60 91\n59 90\n60 89\n59 88\n58 87\n57 86\n56 85\n55 84\n54 83\n53 82\n52 81\n51 80\n50 79\n49 78\n48 69\n47 68\n46 67\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n33 40\n32 39\n31 38\n30 37\n31 36\n32 35\n31 34\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n55 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n72 77\n71 76\n70 75\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n"}, {"input": "100\r\n245 230 240 248 247 235 240 228 247 243 244 240 246 234 244 247 247 232 247 233 241 247 236 247 230 228 243 237 246 231 246 231 233 235 229 244 247 248 245 248 231 230 238 247 235 248 240 239 233 232 230 229 229 244 247 246 248 247 247 234 243 242 247 228 238 238 236 243 236 228 229 245 232 246 241 243 248 235 242 237 244 239 238 245 231 235 234 237 238 237 234 232 231 236 233 238 228 243 242 246\r\n", "output": "1 2\n2 5\n5 14\n4 7\n3 8\n2 15\n3 12\n2 3\n1 2\n6 13\n7 14\n6 15\n7 14\n6 7\n5 6\n4 5\n3 4\n10 15\n15 20\n16 25\n15 32\n14 35\n13 34\n12 35\n11 36\n10 35\n9 32\n8 33\n7 34\n6 35\n5 8\n4 7\n3 6\n4 5\n11 36\n10 35\n9 34\n8 33\n7 32\n8 33\n7 30\n6 25\n5 8\n4 7\n3 6\n4 5\n3 4\n2 3\n11 22\n10 19\n9 18\n8 17\n7 16\n6 15\n5 14\n6 7\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 29\n33 34\n34 35\n35 36\n36 41\n37 42\n36 51\n35 52\n34 53\n33 52\n32 53\n31 52\n30 53\n29 64\n28 55\n27 56\n26 53\n27 54\n26 69\n25 70\n24 49\n23 48\n22 47\n21 46\n20 45\n19 44\n18 43\n17 40\n16 39\n15 38\n14 21\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n9 10\n8 9\n7 8\n6 7\n19 20\n18 19\n17 18\n16 17\n24 37\n23 34\n22 33\n21 32\n20 31\n19 30\n18 29\n17 28\n16 27\n15 26\n14 23\n13 22\n14 21\n13 20\n12 19\n11 18\n10 17\n9 16\n8 15\n7 12\n8 11\n7 10\n6 9\n5 8\n4 7\n5 6\n52 75\n53 82\n54 87\n53 70\n52 71\n51 72\n50 73\n49 70\n48 71\n47 86\n46 87\n45 92\n44 99\n43 96\n42 95\n41 94\n40 95\n39 96\n38 95\n37 96\n36 97\n35 98\n34 99\n33 94\n32 95\n31 98\n30 97\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n33 98\n32 99\n31 100\n30 95\n29 96\n28 97\n27 98\n26 99\n25 42\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 41\n27 38\n26 37\n25 36\n26 35\n27 34\n26 31\n25 30\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n7 10\n6 9\n5 6\n4 5\n45 98\n46 97\n47 98\n46 99\n45 98\n44 97\n43 96\n42 91\n41 90\n40 89\n39 88\n38 87\n37 86\n36 85\n35 84\n34 83\n33 78\n32 35\n31 34\n30 33\n29 30\n28 29\n27 28\n38 77\n37 72\n36 69\n37 68\n36 67\n35 64\n34 63\n33 62\n32 57\n31 56\n30 55\n29 34\n28 33\n27 32\n26 31\n25 28\n24 27\n23 26\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n37 54\n38 53\n37 52\n36 51\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 37\n27 36\n26 35\n25 34\n24 33\n23 32\n22 31\n21 28\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n49 50\n60 61\n75 76\n81 82\n94 95\n"}, {"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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": ""}, {"input": "100\r\n2 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 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "43 44\n42 45\n41 46\n40 47\n39 48\n38 49\n37 50\n36 51\n35 52\n34 53\n33 54\n32 55\n31 56\n30 57\n29 58\n28 59\n27 60\n26 61\n25 62\n24 63\n23 64\n22 65\n21 66\n20 67\n19 68\n18 69\n17 70\n16 71\n15 72\n14 73\n13 74\n12 75\n11 76\n10 77\n9 78\n8 79\n7 80\n6 81\n5 82\n4 83\n3 84\n2 85\n1 86\n2 87\n3 88\n4 89\n5 90\n6 91\n7 92\n8 93\n9 94\n10 95\n11 96\n12 97\n13 98\n14 99\n15 100\n16 99\n17 98\n18 97\n19 96\n20 95\n21 94\n22 93\n23 92\n24 91\n25 90\n26 89\n27 88\n28 87\n29 86\n30 85\n31 84\n32 83\n33 82\n34 81\n35 80\n36 79\n37 78\n38 77\n39 76\n40 75\n41 74\n42 73\n43 72\n44 71\n45 70\n46 69\n47 68\n48 67\n49 66\n50 65\n51 64\n52 63\n53 62\n54 61\n55 60\n56 59\n57 58\n"}, {"input": "1\r\n1\r\n", "output": ""}, {"input": "10\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": ""}, {"input": "98\r\n5 5 5 5 5 5 5 6 6 6 6 7 6 7 7 7 7 7 7 8 8 8 8 8 8 9 8 9 9 9 9 10 9 10 9 11 10 11 11 11 11 12 12 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 14 15 15 15 15 15 15 15 16 16 16 16 17 17 17 17 17 17 18 17 18 18 18 19 19 19 19 19 20 19 20 20 20 20 20 20 20\r\n", "output": "12 13\r\n26 27\r\n32 33\r\n34 35\r\n36 37\r\n80 81\r\n90 91\r\n33 34\r\n"}, {"input": "20\r\n2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2\r\n", "output": "6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n"}, {"input": "1\r\n5\r\n", "output": ""}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
with open(input_path, 'r') as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
with open(submission_output_path, 'r') as f:
lines = f.readlines()
ops = []
for line in lines:
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
l = int(parts[0])
r = int(parts[1])
except:
print(0)
return
ops.append((l, r))
if len(ops) > 20000:
print(0)
return
arr = a.copy()
for l, r in ops:
if l < 1 or r > n or l >= r or (r - l + 1) % 2 != 0:
print(0)
return
for i in range(l-1, r, 2):
if i+1 < len(arr):
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted_ok = all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
print(100 if sorted_ok else 0)
if __name__ == '__main__':
main()
| true
|
980/C
|
980
|
C
|
Python 3
|
PRETESTS
| 8
| 78
| 7,065,600
|
38037220
|
pixels, k = [int(x) for x in input().split(' ')]
pixel_colors = [int(x) for x in input().split(' ')]
pixels_groups = [(x // k) * k for x in range(256)]
visited = [0] * 256
results = []
for i in range(pixels):
current = pixel_colors[i]
if visited[current] == 1:
results.append(pixels_groups[current])
continue
# check if I can go down k-1 spaces
k_spaces_down = current - (k-1)
if k_spaces_down >= 0 and sum(visited[k_spaces_down:current]) < (k-1):
# this means no one is locked, go as far down as possible
while k_spaces_down != current:
if visited[k_spaces_down] == 0:
break
else:
k_spaces_down += 1
# now lock all the others
for j in range(k_spaces_down, current+1):
visited[j] = 1
pixels_groups[j] = k_spaces_down
results.append(pixels_groups[current])
else:
results.append(pixels_groups[current])
print(' '.join([str(x) for x in results]))
| 82
| 186
| 12,595,200
|
38034054
|
def getIntList():
return list(map(int, input().split()));
n, k = getIntList();
p=getIntList();
choosed=[False]*256;
left=[i for i in range(256)];
for i, x in enumerate(p):
if not choosed[x]:
best=x;
#print(x-1, max(-1, x-k));
for j in range(x-1, max(-1, x-k), -1):
#print('try ',j)
if not choosed[j]:
best=j;
else:
if x-left[j]<k:
best=left[j];
break;
#print('best=',best)
for j in range(best, x+1):
choosed[j]=True;
left[j]=best;
p[i]=left[x];
print(' '.join(map(str, p)));
|
Codeforces Round 480 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Posterized
|
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.
|
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.
|
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
| null |
One possible way to group colors and assign keys for the first sample:
Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.
Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.
Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.
Other groups won't affect the result so they are not listed here.
|
[{"input": "4 3\n2 14 3 4", "output": "0 12 3 3"}, {"input": "5 2\n0 2 1 255 254", "output": "0 1 1 254 254"}]
| 1,700
|
["games", "greedy"]
| 82
|
[{"input": "4 3\r\n2 14 3 4\r\n", "output": "0 12 3 3\r\n"}, {"input": "5 2\r\n0 2 1 255 254\r\n", "output": "0 1 1 254 254\r\n"}, {"input": "10 3\r\n112 184 161 156 118 231 191 128 91 229\r\n", "output": "110 182 159 154 116 229 189 126 89 229\r\n"}, {"input": "9 3\r\n174 149 118 124 166 146 219 233 107\r\n", "output": "172 147 116 122 164 144 217 231 105\r\n"}, {"input": "8 4\r\n180 195 13 195 61 24 132 160\r\n", "output": "177 192 10 192 58 21 129 157\r\n"}, {"input": "1 4\r\n51\r\n", "output": "48\r\n"}, {"input": "2 4\r\n218 213\r\n", "output": "215 210\r\n"}, {"input": "3 3\r\n212 167 3\r\n", "output": "210 165 1\r\n"}, {"input": "100 7\r\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\r\n", "output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\r\n"}, {"input": "4 2\r\n122 108 224 154\r\n", "output": "121 107 223 153\r\n"}]
| false
|
stdio
| null | true
|
980/C
|
980
|
C
|
Python 3
|
PRETESTS
| 8
| 78
| 7,065,600
|
38043840
|
n,k = [int(s) for s in input().split()]
p = [int(s) for s in input().split()]
map = {}
res = []
for pi in p:
if map.get(pi) is None:
key = pi
for j in range(pi, pi-k, -1):
if j < 0: break
if map.get(j) is None:
key = j
else:
if map[j] >= pi-k+1: key = j
break
for j in range(key, pi+1):
map[j] = key
res.append(map[pi])
print(*res, sep=" ")
| 82
| 187
| 11,059,200
|
139556135
|
import sys,os,io
import math
from collections import defaultdict
def ii():
return int(input())
def li():
return list(map(int,input().split()))
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n,k = li()
p = li()
group = [-1]*256
ans = []
for i in range(n):
if group[p[i]]!=-1:
ans.append(group[p[i]])
continue
prev = -1
for j in range(p[i],-1,-1):
if j == p[i] - k :
break
if group[j]!=-1:
if k - (j - group[j]+1) - (p[i] - j) <0:
break
prev = j
break
y = -1
for j in range(p[i],-1,-1):
if group[j]!=-1:
y = j
break
if prev==-1:
val = max(y+1,p[i]-k+1)
ind = - 1
for j in range(max(y+1,p[i]-k+1),p[i]+1):
group[j] = val
ans.append(val)
else:
for j in range(prev+1,p[i]+1):
group[j] = group[prev]
ans.append(group[p[i]])
print(*ans)
|
Codeforces Round 480 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Posterized
|
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.
|
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.
|
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
| null |
One possible way to group colors and assign keys for the first sample:
Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.
Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.
Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.
Other groups won't affect the result so they are not listed here.
|
[{"input": "4 3\n2 14 3 4", "output": "0 12 3 3"}, {"input": "5 2\n0 2 1 255 254", "output": "0 1 1 254 254"}]
| 1,700
|
["games", "greedy"]
| 82
|
[{"input": "4 3\r\n2 14 3 4\r\n", "output": "0 12 3 3\r\n"}, {"input": "5 2\r\n0 2 1 255 254\r\n", "output": "0 1 1 254 254\r\n"}, {"input": "10 3\r\n112 184 161 156 118 231 191 128 91 229\r\n", "output": "110 182 159 154 116 229 189 126 89 229\r\n"}, {"input": "9 3\r\n174 149 118 124 166 146 219 233 107\r\n", "output": "172 147 116 122 164 144 217 231 105\r\n"}, {"input": "8 4\r\n180 195 13 195 61 24 132 160\r\n", "output": "177 192 10 192 58 21 129 157\r\n"}, {"input": "1 4\r\n51\r\n", "output": "48\r\n"}, {"input": "2 4\r\n218 213\r\n", "output": "215 210\r\n"}, {"input": "3 3\r\n212 167 3\r\n", "output": "210 165 1\r\n"}, {"input": "100 7\r\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\r\n", "output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\r\n"}, {"input": "4 2\r\n122 108 224 154\r\n", "output": "121 107 223 153\r\n"}]
| false
|
stdio
| null | true
|
980/C
|
980
|
C
|
Python 3
|
PRETESTS
| 8
| 93
| 7,065,600
|
38045453
|
n, k = map(int, input().strip().split())
data = map(int, input().strip().split())
sol = []
mapping = [(-1,1000)]*256
for x in data:
if mapping[x][0] == -1:
for i in range(max(x-k+1,0), x+1):
if mapping[i][0] == -1:
if i > 0 and mapping[i-1][1]+(x-i+1) <= k:
p = mapping[i-1][1]+1
for j in range(i, x+1):
mapping[j] = (i-1, p)
p += 1
else:
p = 1
for j in range(i, x+1):
mapping[j] = (i, p)
p += 1
break
# print(mapping[:15])
sol.append(mapping[x][0])
print(' '.join(map(str, sol)))
| 82
| 187
| 13,004,800
|
38036027
|
n, k = [int(x) for x in input().split()]
ps = [int(x) for x in input().split()]
mapping = [-1 for _ in range(256)]
res = []
for p in ps:
if mapping[p] == -1:
j = p - k + 1
while j < 0 or (mapping[j] != -1 and mapping[j] + k <= p):
j += 1
for i in range(j, p+1):
mapping[i] = j
res.append(mapping[p])
print(" ".join(map(str, res)))
|
Codeforces Round 480 (Div. 2)
|
CF
| 2,018
| 1
| 256
|
Posterized
|
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.
|
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.
|
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
| null |
One possible way to group colors and assign keys for the first sample:
Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.
Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.
Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.
Other groups won't affect the result so they are not listed here.
|
[{"input": "4 3\n2 14 3 4", "output": "0 12 3 3"}, {"input": "5 2\n0 2 1 255 254", "output": "0 1 1 254 254"}]
| 1,700
|
["games", "greedy"]
| 82
|
[{"input": "4 3\r\n2 14 3 4\r\n", "output": "0 12 3 3\r\n"}, {"input": "5 2\r\n0 2 1 255 254\r\n", "output": "0 1 1 254 254\r\n"}, {"input": "10 3\r\n112 184 161 156 118 231 191 128 91 229\r\n", "output": "110 182 159 154 116 229 189 126 89 229\r\n"}, {"input": "9 3\r\n174 149 118 124 166 146 219 233 107\r\n", "output": "172 147 116 122 164 144 217 231 105\r\n"}, {"input": "8 4\r\n180 195 13 195 61 24 132 160\r\n", "output": "177 192 10 192 58 21 129 157\r\n"}, {"input": "1 4\r\n51\r\n", "output": "48\r\n"}, {"input": "2 4\r\n218 213\r\n", "output": "215 210\r\n"}, {"input": "3 3\r\n212 167 3\r\n", "output": "210 165 1\r\n"}, {"input": "100 7\r\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\r\n", "output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\r\n"}, {"input": "4 2\r\n122 108 224 154\r\n", "output": "121 107 223 153\r\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 186
| 19,046,400
|
33659652
|
# ===================================
# (c) MidAndFeed aka ASilentVoice
# ===================================
# import math, fractions, collections
# ===================================
n = int(input())
q = [int(x) for x in input().split()]
ans = -n
s = set()
for i in range(n):
temp = q[i]
if temp in s:
s.remove(temp)
else:
s.add(temp)
ans = max(ans, len(s))
print(ans)
| 56
| 124
| 20,172,800
|
228759395
|
number = int(input())
s = input().split(" ")
arr = set()
max_len = 0
for i in range(2 * number):
if s[i] not in arr:
arr.add(s[i])
if len(arr) > max_len:
max_len = len(arr)
else:
arr.remove(s[i])
print(max_len)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Andryusha and Socks
|
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
|
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
|
Print single integer — the maximum number of socks that were on the table at the same time.
| null |
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
|
[{"input": "1\n1 1", "output": "1"}, {"input": "3\n2 1 1 3 2 3", "output": "2"}]
| 800
|
["implementation"]
| 56
|
[{"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n2 1 1 3 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 1 3 2 4 3 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n4 2 6 3 4 8 7 1 1 5 2 10 6 8 3 5 10 9 9 7\r\n", "output": "6\r\n"}, {"input": "50\r\n30 47 31 38 37 50 36 43 9 23 2 2 15 31 14 49 9 16 6 44 27 14 5 6 3 47 25 26 1 35 3 15 24 19 8 46 49 41 4 26 40 28 42 11 34 35 46 18 7 28 18 40 19 42 4 41 38 48 50 12 29 39 33 17 25 22 22 21 36 45 27 30 20 7 13 29 39 44 21 8 37 45 34 1 20 10 11 17 33 12 43 13 10 16 48 24 32 5 23 32\r\n", "output": "25\r\n"}, {"input": "50\r\n1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50\r\n", "output": "1\r\n"}, {"input": "50\r\n50 50 49 49 48 48 47 47 46 46 45 45 44 44 43 43 42 42 41 41 40 40 39 39 38 38 37 37 36 36 35 35 34 34 33 33 32 32 31 31 30 30 29 29 28 28 27 27 26 26 25 25 24 24 23 23 22 22 21 21 20 20 19 19 18 18 17 17 16 16 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\r\n", "output": "1\r\n"}, {"input": "50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "50\r\n"}, {"input": "50\r\n50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "50\r\n"}, {"input": "10\r\n2 9 4 1 6 7 10 3 1 5 8 6 2 3 10 7 4 8 5 9\r\n", "output": "9\r\n"}]
| false
|
stdio
| null | true
|
686/B
|
686
|
B
|
PyPy 3-64
|
TESTS
| 2
| 46
| 1,536,000
|
199161615
|
from sys import stdin, stdout
input = stdin.readline
def print(*args, end='\n', sep=' ') -> None:
stdout.write(sep.join(map(str, args)) + end)
def int_map():
return map(int, input().split())
def list_int():
return list(map(int, input().split()))
n = int(input())
arr = list_int()
sorted = True
for i in range(20000):
for j in range(1, n):
if arr[j] < arr[j-1]:
arr[j-1] = arr[j]
print(j, j+1)
sorted = False
if sorted:
break
| 37
| 62
| 409,600
|
18674795
|
n=int(input())
l=list(map(int,input().split()))
ans=[]
ls=sorted(l)
while l!=ls:
for i in range(1,n):
if l[i-1]>l[i]:
ans+=[' '.join([str(i),str(i+1)])]
l[i-1],l[i]=l[i],l[i-1]
print('\n'.join(ans))
|
Codeforces Round 359 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Little Robber Girl's Zoo
|
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.
The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.
Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.
|
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
|
Print the sequence of operations that will rearrange the animals by non-decreasing height.
The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed.
The number of operations should not exceed 20 000.
If the animals are arranged correctly from the start, you are allowed to output nothing.
| null |
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
|
[{"input": "4\n2 1 4 3", "output": "1 4"}, {"input": "7\n36 28 57 39 66 69 68", "output": "1 4\n6 7"}, {"input": "5\n1 2 1 2 1", "output": "2 5\n3 4\n1 4\n1 4"}]
| 1,100
|
["constructive algorithms", "implementation", "sortings"]
| 37
|
[{"input": "4\r\n2 1 4 3\r\n", "output": "1 2\r\n3 4\r\n"}, {"input": "7\r\n36 28 57 39 66 69 68\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "2 3\r\n4 5\r\n3 4\r\n"}, {"input": "78\r\n7 3 8 8 9 8 10 9 12 11 16 14 17 17 18 18 20 20 25 22 27 26 29 27 35 35 36 36 37 37 38 38 40 39 42 42 48 46 49 49 58 50 60 58 65 61 68 66 69 69 69 69 70 69 71 71 77 73 78 77 80 79 85 83 86 86 86 86 88 87 91 90 96 91 98 97 99 98\r\n", "output": "1 2\r\n5 6\r\n7 8\r\n9 10\r\n11 12\r\n19 20\r\n21 22\r\n23 24\r\n33 34\r\n37 38\r\n41 42\r\n43 44\r\n45 46\r\n47 48\r\n53 54\r\n57 58\r\n59 60\r\n61 62\r\n63 64\r\n69 70\r\n71 72\r\n73 74\r\n75 76\r\n77 78\r\n"}, {"input": "47\r\n17462271 17619887 84928078 85012448 86003043 86499508 129945397 135545949 141951561 161227989 167459883 215243263 243500622 284922151 326608661 350939646 359121478 388866754 441241406 460673749 497339472 512847269 521291845 537280636 561101359 569788868 581107309 588113051 619849749 621125808 637220955 640578896 660913258 674411824 694527254 716291982 725198795 734631740 797705515 810502965 832854453 866687928 923880462 948282076 957759163 968324020 997176104\r\n", "output": ""}, {"input": "99\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "20 21\n19 22\n18 23\n17 24\n16 25\n15 26\n14 27\n13 28\n12 29\n11 30\n10 31\n9 32\n8 35\n7 36\n6 37\n5 38\n4 39\n3 40\n2 41\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 51\n11 52\n12 55\n13 56\n14 57\n13 58\n12 59\n11 60\n10 61\n9 62\n8 63\n7 64\n6 65\n5 66\n4 67\n3 68\n2 69\n1 70\n2 71\n3 72\n4 73\n5 74\n6 75\n7 76\n8 77\n9 78\n10 79\n11 82\n12 83\n13 84\n14 85\n15 86\n16 87\n17 88\n18 89\n19 90\n20 91\n19 92\n18 93\n17 94\n16 95\n15 96\n14 97\n13 98\n12 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 97\n3 96\n2 95\n1 94\n2 93\n3 92\n4 91\n5 90\n6 89\n7 88\n8 87\n9 86\n10 85\n11 84\n12 83\n13 82\n14 81\n15 80\n16 77\n17 76\n18 75\n19 74\n20 73\n21 72\n22 71\n23 70\n24 69\n25 68\n26 67\n27 66\n26 63\n25 62\n24 61\n23 60\n22 59\n21 58\n20 57\n19 56\n18 55\n17 54\n16 53\n15 52\n14 51\n13 50\n12 49\n11 48\n10 47\n9 46\n8 43\n7 42\n6 41\n5 40\n4 39\n3 38\n2 37\n1 36\n2 35\n3 34\n4 33\n5 32\n6 31\n7 30\n8 29\n9 28\n10 27\n11 26\n12 25\n13 24\n14 23\n15 22\n16 21\n17 20\n18 19\n"}, {"input": "99\r\n4577 4577 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4576 4575 4575 4575 4575 4575 4575 4574 4574 4574 4574 4574 4574 4574 4574 4574 4574 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4573 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4572 4571 4571 4571 4571 4571 4571 4571 4571 4571 4570 4570 4570 4570 4570 4570 4570 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4569 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4568 4567 4567 4567 4567 4567 4567 4567 4567 4567\r\n", "output": "2 3\n1 4\n2 5\n3 6\n4 7\n5 8\n6 9\n7 10\n8 11\n9 14\n10 15\n11 16\n10 17\n9 20\n8 21\n7 22\n6 23\n5 24\n4 25\n3 26\n2 27\n1 30\n2 31\n3 32\n4 33\n5 34\n6 35\n5 36\n4 37\n3 38\n2 39\n1 42\n2 43\n3 44\n4 45\n5 46\n6 47\n7 48\n8 49\n9 50\n10 53\n9 54\n8 55\n7 56\n6 57\n5 58\n4 59\n3 62\n2 63\n1 64\n2 65\n3 66\n4 69\n5 70\n6 71\n7 72\n8 73\n9 74\n10 75\n11 76\n12 77\n11 80\n10 81\n9 82\n8 83\n7 84\n6 85\n5 86\n4 87\n3 88\n2 91\n1 92\n2 93\n3 94\n4 95\n5 96\n6 97\n7 98\n8 99\n9 98\n10 99\n11 98\n10 99\n9 98\n8 99\n7 98\n6 99\n5 98\n4 99\n3 98\n2 99\n1 98\n2 99\n3 98\n4 99\n5 98\n6 99\n7 98\n8 95\n9 94\n8 93\n7 92\n6 91\n5 90\n4 89\n3 88\n2 87\n1 84\n2 83\n3 82\n4 81\n5 78\n6 77\n7 76\n6 75\n5 74\n4 73\n3 72\n2 71\n1 68\n2 67\n3 66\n4 65\n5 64\n6 63\n7 62\n8 61\n9 60\n10 59\n11 56\n10 55\n9 54\n8 53\n7 52\n6 51\n5 50\n4 49\n3 48\n2 45\n1 44\n2 43\n3 42\n4 41\n5 40\n6 39\n7 36\n8 35\n9 34\n10 33\n11 32\n10 29\n9 28\n8 27\n7 26\n6 25\n5 24\n4 23\n3 22\n2 21\n1 18\n2 17\n3 16\n4 15\n5 14\n6 13\n7 12\n8 11\n9 10\n"}, {"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": ""}, {"input": "10\r\n44 23 65 17 48 29 49 88 91 85\r\n", "output": "1 2\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n9 10\r\n2 3\r\n4 5\r\n8 9\r\n1 2\r\n3 4\r\n"}, {"input": "13\r\n605297997 425887240 859639341 200428931 888317166 983420497 81642057 628988722 389959969 358920886 646428392 324912711 401437250\r\n", "output": "1 2\r\n3 4\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n3 4\r\n5 6\r\n6 7\r\n8 9\r\n9 10\r\n2 3\r\n4 5\r\n5 6\r\n7 8\r\n8 9\r\n1 2\r\n3 4\r\n4 5\r\n6 7\r\n7 8\r\n3 4\r\n5 6\r\n6 7\r\n4 5\r\n3 4\r\n"}, {"input": "43\r\n644870843 160471908 227474511 47341477 175939701 563067024 749818136 707986934 201095131 736488829 346428456 342944986 316696712 101551423 672610101 897020945 708299245 587795677 408207112 985104524 278945228 192250326 157154304 301319412 270702270 954096281 649990285 37649442 300182190 382249227 605285302 392816037 419998044 84624133 332174228 996770879 816912092 283973844 498255316 374935144 294452244 529912248 553039417\r\n", "output": "1 4\n2 5\n1 4\n4 5\n5 8\n8 13\n7 14\n6 13\n5 14\n4 27\n7 22\n6 23\n7 10\n6 9\n5 6\n4 5\n3 4\n2 3\n15 24\n14 25\n13 28\n12 23\n11 24\n10 29\n11 34\n10 37\n9 34\n8 35\n7 34\n8 15\n7 14\n6 13\n5 12\n4 7\n3 4\n20 35\n19 40\n18 41\n17 40\n16 31\n15 32\n14 29\n13 28\n12 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n2 3\n1 2\n18 27\n19 26\n18 23\n17 20\n16 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n34 41\n33 42\n32 43\n31 42\n30 41\n29 42\n28 39\n27 38\n26 37\n25 36\n24 35\n23 34\n22 33\n21 32\n20 31\n19 24\n18 23\n17 18\n16 17\n15 16\n14 15\n13 14\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n27 30\n28 29\n"}, {"input": "97\r\n1 1 1 2 1 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 1 1 2 1 1 1 1 2 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 1 1 1 2 2 1 1 2 1 1 1 1 2 2 1 2 1 2 1 1 2 2 2 1 2 2 1 1 2 2 2 1 1 2 1 2 1 1 2\r\n", "output": "4 5\n5 6\n6 9\n7 10\n8 11\n9 12\n10 13\n11 12\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n13 14\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n15 16\n34 35\n33 38\n32 39\n31 40\n30 45\n29 44\n28 43\n27 42\n26 41\n25 40\n24 39\n23 38\n22 37\n21 36\n20 35\n19 34\n18 33\n17 32\n16 31\n17 30\n18 29\n19 28\n20 27\n21 26\n22 25\n23 24\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n58 59\n57 60\n56 61\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n31 36\n30 35\n29 34\n28 33\n27 32\n26 31\n25 30\n26 29\n27 28\n63 64\n62 67\n61 68\n60 69\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 39\n29 38\n30 37\n31 36\n32 35\n33 34\n72 77\n71 78\n70 77\n69 76\n68 75\n67 74\n66 73\n65 72\n64 71\n63 70\n62 69\n61 68\n60 67\n59 66\n58 65\n57 64\n56 63\n55 62\n54 61\n53 60\n52 59\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n84 85\n83 86\n82 85\n81 84\n80 83\n79 82\n78 81\n77 80\n76 79\n75 78\n74 77\n73 76\n72 75\n71 74\n70 73\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 62\n58 61\n57 60\n56 59\n55 58\n54 57\n53 56\n52 55\n51 54\n50 53\n49 52\n48 51\n47 50\n46 49\n45 48\n44 47\n43 46\n42 45\n41 44\n40 43\n39 42\n40 41\n89 90\n88 95\n87 96\n86 95\n85 94\n84 93\n83 92\n82 91\n81 90\n80 89\n79 88\n78 87\n77 86\n76 85\n75 84\n74 83\n73 82\n72 81\n71 80\n70 79\n69 78\n68 77\n67 76\n66 75\n65 74\n64 73\n63 72\n62 71\n61 70\n60 69\n59 68\n58 67\n57 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 60\n50 59\n49 58\n48 57\n47 56\n46 55\n45 54\n44 53\n43 52\n42 51\n41 50\n42 49\n43 48\n44 47\n45 46\n"}, {"input": "87\r\n2 2 1 2 3 1 3 2 3 2 3 3 1 3 3 3 2 2 1 1 2 3 2 1 2 2 3 3 1 1 1 3 2 3 1 2 1 3 3 3 3 3 3 2 3 2 3 3 2 1 1 3 1 1 3 3 2 3 1 1 3 3 3 2 3 1 3 2 2 2 1 3 3 3 1 1 2 3 2 3 2 1 3 3 3 1 3\r\n", "output": "2 3\n1 2\n5 10\n4 9\n3 4\n2 3\n7 8\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n16 19\n15 20\n14 19\n13 20\n12 23\n11 24\n10 25\n9 26\n10 25\n9 24\n8 23\n7 10\n6 9\n5 8\n4 7\n5 6\n13 22\n14 21\n15 20\n14 19\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n17 18\n28 29\n27 30\n26 37\n25 36\n24 37\n23 36\n22 35\n21 34\n20 33\n19 32\n18 31\n17 30\n16 29\n15 28\n14 19\n13 18\n12 17\n11 16\n10 15\n9 14\n8 13\n7 12\n8 11\n9 10\n22 27\n21 26\n20 25\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n11 12\n43 46\n42 45\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n26 27\n48 49\n47 50\n46 53\n45 54\n44 53\n43 52\n42 51\n41 50\n40 49\n39 48\n38 47\n37 46\n36 45\n35 44\n34 43\n33 42\n32 41\n31 40\n30 39\n29 38\n28 37\n27 36\n28 35\n27 34\n26 33\n25 32\n24 31\n23 30\n22 29\n21 28\n20 27\n19 26\n18 25\n17 24\n16 23\n15 22\n14 21\n13 20\n12 19\n13 18\n14 17\n15 16\n56 59\n55 60\n54 59\n53 58\n52 57\n51 56\n50 55\n49 54\n48 53\n47 52\n46 51\n45 50\n44 49\n43 48\n42 47\n41 46\n40 45\n39 44\n38 43\n37 42\n36 41\n35 40\n34 39\n33 38\n32 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n17 18\n63 68\n62 71\n61 70\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 49\n37 48\n36 47\n35 46\n36 45\n35 44\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n38 43\n39 42\n38 41\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n74 75\n73 76\n72 81\n71 82\n70 81\n69 80\n68 79\n67 78\n66 77\n65 76\n64 75\n63 74\n62 73\n61 72\n60 71\n59 70\n58 69\n57 68\n56 67\n55 66\n54 65\n53 64\n52 63\n51 62\n50 61\n49 60\n48 59\n47 58\n46 57\n45 56\n44 55\n43 54\n42 53\n41 52\n40 51\n39 50\n38 41\n37 40\n36 39\n35 38\n34 37\n33 36\n32 35\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n21 22\n44 49\n45 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n85 86\n84 85\n83 84\n82 83\n81 82\n80 81\n79 80\n78 79\n77 78\n76 77\n75 76\n74 75\n73 74\n72 73\n71 72\n70 71\n69 70\n68 69\n67 68\n66 67\n65 66\n64 65\n63 64\n62 63\n61 62\n60 61\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n37 38\n36 37\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n"}, {"input": "100\r\n3 2 5 4 3 3 3 3 4 3 1 2 3 2 3 1 4 1 5 2 5 3 3 5 2 3 5 4 3 4 1 5 5 2 2 1 3 5 1 3 5 2 2 1 4 3 1 3 5 1 1 3 5 5 5 4 5 5 1 5 3 5 4 3 5 4 1 1 2 1 2 5 1 2 2 2 3 5 5 5 4 2 3 2 1 2 3 5 2 2 2 2 5 3 5 4 2 5 3 4\r\n", "output": "1 4\n4 5\n3 6\n4 7\n5 10\n6 11\n7 22\n8 25\n7 28\n6 17\n5 16\n4 17\n3 18\n2 31\n1 12\n4 11\n5 10\n4 9\n3 8\n2 5\n3 4\n15 30\n16 31\n17 22\n18 21\n17 22\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n22 23\n23 30\n24 29\n23 28\n22 27\n21 24\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n33 36\n32 35\n31 36\n30 39\n29 46\n28 43\n27 44\n26 47\n25 50\n24 51\n23 52\n22 51\n21 50\n20 51\n19 50\n18 23\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n11 16\n10 15\n11 14\n10 13\n9 10\n8 9\n7 8\n6 7\n5 6\n26 49\n25 48\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n28 47\n27 46\n26 45\n25 44\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n15 18\n14 17\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n32 43\n31 40\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n34 39\n33 38\n32 37\n31 34\n30 33\n29 32\n28 31\n27 30\n26 29\n25 28\n24 27\n23 26\n22 25\n21 24\n20 23\n19 22\n18 21\n17 20\n16 19\n15 18\n14 17\n13 16\n12 15\n11 14\n10 13\n9 12\n10 11\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n58 63\n57 66\n56 67\n55 70\n54 69\n53 70\n52 73\n51 74\n50 75\n49 76\n48 77\n47 76\n46 75\n45 74\n44 73\n43 72\n42 71\n41 44\n40 43\n39 42\n38 41\n37 40\n36 39\n35 36\n34 35\n33 34\n32 33\n31 32\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n47 70\n46 69\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n49 68\n48 67\n47 66\n46 65\n45 64\n44 63\n43 62\n42 61\n41 60\n40 59\n39 58\n38 57\n37 56\n36 55\n35 54\n34 53\n33 52\n32 51\n31 50\n30 47\n29 46\n28 45\n27 44\n26 43\n25 42\n24 41\n23 40\n22 39\n21 38\n20 37\n19 36\n18 35\n17 22\n16 21\n15 20\n14 19\n13 18\n12 17\n13 16\n14 15\n25 34\n26 33\n25 32\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n28 31\n29 30\n80 81\n79 84\n78 85\n77 86\n76 85\n75 86\n74 89\n73 90\n72 91\n71 96\n70 99\n69 100\n68 99\n67 98\n66 97\n65 96\n64 95\n63 94\n62 93\n61 92\n60 91\n59 90\n60 89\n59 88\n58 87\n57 86\n56 85\n55 84\n54 83\n53 82\n52 81\n51 80\n50 79\n49 78\n48 69\n47 68\n46 67\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n33 40\n32 39\n31 38\n30 37\n31 36\n32 35\n31 34\n30 31\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n55 66\n56 65\n55 64\n54 63\n53 62\n52 61\n51 58\n50 57\n49 56\n48 55\n47 54\n46 53\n45 52\n44 51\n43 50\n42 49\n41 48\n40 47\n39 46\n38 45\n37 44\n36 43\n35 42\n34 41\n35 40\n36 39\n37 38\n72 77\n71 76\n70 75\n69 72\n68 71\n67 70\n66 69\n65 68\n64 67\n63 66\n62 65\n61 64\n60 63\n59 60\n58 59\n57 58\n56 57\n55 56\n54 55\n53 54\n52 53\n51 52\n50 51\n49 50\n48 49\n47 48\n46 47\n45 46\n44 45\n43 44\n42 43\n41 42\n40 41\n39 40\n38 39\n"}, {"input": "100\r\n245 230 240 248 247 235 240 228 247 243 244 240 246 234 244 247 247 232 247 233 241 247 236 247 230 228 243 237 246 231 246 231 233 235 229 244 247 248 245 248 231 230 238 247 235 248 240 239 233 232 230 229 229 244 247 246 248 247 247 234 243 242 247 228 238 238 236 243 236 228 229 245 232 246 241 243 248 235 242 237 244 239 238 245 231 235 234 237 238 237 234 232 231 236 233 238 228 243 242 246\r\n", "output": "1 2\n2 5\n5 14\n4 7\n3 8\n2 15\n3 12\n2 3\n1 2\n6 13\n7 14\n6 15\n7 14\n6 7\n5 6\n4 5\n3 4\n10 15\n15 20\n16 25\n15 32\n14 35\n13 34\n12 35\n11 36\n10 35\n9 32\n8 33\n7 34\n6 35\n5 8\n4 7\n3 6\n4 5\n11 36\n10 35\n9 34\n8 33\n7 32\n8 33\n7 30\n6 25\n5 8\n4 7\n3 6\n4 5\n3 4\n2 3\n11 22\n10 19\n9 18\n8 17\n7 16\n6 15\n5 14\n6 7\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 29\n33 34\n34 35\n35 36\n36 41\n37 42\n36 51\n35 52\n34 53\n33 52\n32 53\n31 52\n30 53\n29 64\n28 55\n27 56\n26 53\n27 54\n26 69\n25 70\n24 49\n23 48\n22 47\n21 46\n20 45\n19 44\n18 43\n17 40\n16 39\n15 38\n14 21\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n9 10\n8 9\n7 8\n6 7\n19 20\n18 19\n17 18\n16 17\n24 37\n23 34\n22 33\n21 32\n20 31\n19 30\n18 29\n17 28\n16 27\n15 26\n14 23\n13 22\n14 21\n13 20\n12 19\n11 18\n10 17\n9 16\n8 15\n7 12\n8 11\n7 10\n6 9\n5 8\n4 7\n5 6\n52 75\n53 82\n54 87\n53 70\n52 71\n51 72\n50 73\n49 70\n48 71\n47 86\n46 87\n45 92\n44 99\n43 96\n42 95\n41 94\n40 95\n39 96\n38 95\n37 96\n36 97\n35 98\n34 99\n33 94\n32 95\n31 98\n30 97\n29 30\n28 29\n27 28\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n33 98\n32 99\n31 100\n30 95\n29 96\n28 97\n27 98\n26 99\n25 42\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n4 5\n3 4\n28 41\n27 38\n26 37\n25 36\n26 35\n27 34\n26 31\n25 30\n24 29\n23 28\n22 27\n21 26\n20 25\n19 24\n18 23\n17 22\n16 21\n15 20\n14 19\n13 16\n12 15\n11 14\n10 13\n9 12\n8 11\n7 10\n6 9\n5 6\n4 5\n45 98\n46 97\n47 98\n46 99\n45 98\n44 97\n43 96\n42 91\n41 90\n40 89\n39 88\n38 87\n37 86\n36 85\n35 84\n34 83\n33 78\n32 35\n31 34\n30 33\n29 30\n28 29\n27 28\n38 77\n37 72\n36 69\n37 68\n36 67\n35 64\n34 63\n33 62\n32 57\n31 56\n30 55\n29 34\n28 33\n27 32\n26 31\n25 28\n24 27\n23 26\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n37 54\n38 53\n37 52\n36 51\n35 46\n34 45\n33 44\n32 43\n31 42\n30 41\n29 40\n28 37\n27 36\n26 35\n25 34\n24 33\n23 32\n22 31\n21 28\n20 23\n21 22\n20 21\n19 20\n18 19\n17 18\n26 27\n25 26\n24 25\n23 24\n22 23\n21 22\n20 21\n19 20\n18 19\n17 18\n16 17\n15 16\n14 15\n13 14\n12 13\n11 12\n10 11\n9 10\n8 9\n7 8\n6 7\n5 6\n49 50\n60 61\n75 76\n81 82\n94 95\n"}, {"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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": ""}, {"input": "100\r\n2 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 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "43 44\n42 45\n41 46\n40 47\n39 48\n38 49\n37 50\n36 51\n35 52\n34 53\n33 54\n32 55\n31 56\n30 57\n29 58\n28 59\n27 60\n26 61\n25 62\n24 63\n23 64\n22 65\n21 66\n20 67\n19 68\n18 69\n17 70\n16 71\n15 72\n14 73\n13 74\n12 75\n11 76\n10 77\n9 78\n8 79\n7 80\n6 81\n5 82\n4 83\n3 84\n2 85\n1 86\n2 87\n3 88\n4 89\n5 90\n6 91\n7 92\n8 93\n9 94\n10 95\n11 96\n12 97\n13 98\n14 99\n15 100\n16 99\n17 98\n18 97\n19 96\n20 95\n21 94\n22 93\n23 92\n24 91\n25 90\n26 89\n27 88\n28 87\n29 86\n30 85\n31 84\n32 83\n33 82\n34 81\n35 80\n36 79\n37 78\n38 77\n39 76\n40 75\n41 74\n42 73\n43 72\n44 71\n45 70\n46 69\n47 68\n48 67\n49 66\n50 65\n51 64\n52 63\n53 62\n54 61\n55 60\n56 59\n57 58\n"}, {"input": "1\r\n1\r\n", "output": ""}, {"input": "10\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": ""}, {"input": "98\r\n5 5 5 5 5 5 5 6 6 6 6 7 6 7 7 7 7 7 7 8 8 8 8 8 8 9 8 9 9 9 9 10 9 10 9 11 10 11 11 11 11 12 12 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 14 15 15 15 15 15 15 15 16 16 16 16 17 17 17 17 17 17 18 17 18 18 18 19 19 19 19 19 20 19 20 20 20 20 20 20 20\r\n", "output": "12 13\r\n26 27\r\n32 33\r\n34 35\r\n36 37\r\n80 81\r\n90 91\r\n33 34\r\n"}, {"input": "20\r\n2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2\r\n", "output": "6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n13 14\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n12 13\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 12\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n"}, {"input": "1\r\n5\r\n", "output": ""}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
with open(input_path, 'r') as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
with open(submission_output_path, 'r') as f:
lines = f.readlines()
ops = []
for line in lines:
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
l = int(parts[0])
r = int(parts[1])
except:
print(0)
return
ops.append((l, r))
if len(ops) > 20000:
print(0)
return
arr = a.copy()
for l, r in ops:
if l < 1 or r > n or l >= r or (r - l + 1) % 2 != 0:
print(0)
return
for i in range(l-1, r, 2):
if i+1 < len(arr):
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted_ok = all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
print(100 if sorted_ok else 0)
if __name__ == '__main__':
main()
| true
|
472/D
|
472
|
D
|
Python 3
|
TESTS
| 0
| 15
| 0
|
213891010
|
print ("NO")
| 47
| 545
| 57,036,800
|
177433089
|
from math import inf
from collections import *
import math, os, sys, heapq, bisect, random,threading
from functools import lru_cache
from itertools import *
import sys
def inp(): return sys.stdin.readline().rstrip("\r\n")
def out(var): sys.stdout.write(str(var)) # for fast output, always take string
def inpu(): return int(inp())
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
# #include <ext/pb_ds/assoc_container.hpp>
# #include <ext/pb_ds/tree_policy.hpp>
# using namespace __gnu_pbds;
# #define ll long long
# #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
# #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
M,M1=1000000007,998244353
def find(i,par):
if par[i]!=i:
par[i] = find(par[i],par)
return par[i]
def union(i,j,parent,rank):
if rank[i]>rank[j]:
parent[j] = i
elif rank[i]<rank[j]:
parent[i]= j
else:
parent[j] = i
rank[i] += 1
def main():
how_much_noob_I_am = 1
# how_much_noob_I_am = inpu()
for _ in range(1,how_much_noob_I_am+1):
n = int(input())
arr = []
for _ in range(n):
arr.append(lis())
if n == 1:
if arr[0][0] == 0:
print("YES")
else:
print("NO")
exit()
for i in range(n):
if arr[i][i] != 0:
print("NO")
exit()
for j in range(i + 1, n):
if arr[i][j] != arr[j][i] or arr[i][j] == 0:
print("NO")
exit()
for i in range(n):
r = int(i == 0)
for j in range(n):
if arr[i][j] < arr[i][r] and i != j:
r = j
for k in range(n):
if abs(arr[i][k] - arr[r][k]) != arr[r][i]:
print("NO")
exit()
print("YES")
if __name__ == '__main__':
# sys.setrecursio0nlimit(2*10**5+50)
# threading.stack_size(10**8)
# threading.Thread(target=main).start()
main()
|
Codeforces Round 270
|
CF
| 2,014
| 2
| 256
|
Design Tutorial: Inverse the Problem
|
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example.
Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).
|
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph.
Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.
|
If there exists such a tree, output "YES", otherwise output "NO".
| null |
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.
In the second example, it is impossible because d1, 1 should be 0, but it is 1.
In the third example, it is impossible because d1, 2 should equal d2, 1.
|
[{"input": "3\n0 2 7\n2 0 9\n7 9 0", "output": "YES"}, {"input": "3\n1 2 7\n2 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 2 2\n7 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 1 1\n1 0 1\n1 1 0", "output": "NO"}, {"input": "2\n0 0\n0 0", "output": "NO"}]
| 1,900
|
["dfs and similar", "dsu", "shortest paths", "trees"]
| 47
|
[{"input": "3\r\n0 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 2\r\n7 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 1\r\n1 0 1\r\n1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1000000000\r\n1000000000 0\r\n", "output": "YES\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "YES\r\n"}, {"input": "4\r\n0 3 7 6\r\n3 0 4 9\r\n7 4 0 2\r\n6 9 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 2\r\n1 0 2\r\n2 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 7\r\n2 0 10\r\n7 10 0\r\n", "output": "NO\r\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 124
| 11,161,600
|
50261641
|
n = int(input())
a = input().split()
max_cnt = 0
d = {}
for i in range(n):
if a[i] in d:
del d[a[i]]
else:
d[a[i]] = 1
if max_cnt < len(d):
max_cnt = len(d)
print(max_cnt)
| 56
| 124
| 25,907,200
|
198581634
|
n = int(input()) * 2
lst = [int(x) for x in input().split()][:n]
t = set()
maxi, cnt = 1, 0
for i in range(n):
if lst[i] not in t:
t.add(lst[i])
cnt += 1
else:
if maxi < cnt: maxi = cnt
t.remove(lst[i])
cnt -= 1
print(maxi)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Andryusha and Socks
|
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
|
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
|
Print single integer — the maximum number of socks that were on the table at the same time.
| null |
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
|
[{"input": "1\n1 1", "output": "1"}, {"input": "3\n2 1 1 3 2 3", "output": "2"}]
| 800
|
["implementation"]
| 56
|
[{"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n2 1 1 3 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 1 3 2 4 3 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n4 2 6 3 4 8 7 1 1 5 2 10 6 8 3 5 10 9 9 7\r\n", "output": "6\r\n"}, {"input": "50\r\n30 47 31 38 37 50 36 43 9 23 2 2 15 31 14 49 9 16 6 44 27 14 5 6 3 47 25 26 1 35 3 15 24 19 8 46 49 41 4 26 40 28 42 11 34 35 46 18 7 28 18 40 19 42 4 41 38 48 50 12 29 39 33 17 25 22 22 21 36 45 27 30 20 7 13 29 39 44 21 8 37 45 34 1 20 10 11 17 33 12 43 13 10 16 48 24 32 5 23 32\r\n", "output": "25\r\n"}, {"input": "50\r\n1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50\r\n", "output": "1\r\n"}, {"input": "50\r\n50 50 49 49 48 48 47 47 46 46 45 45 44 44 43 43 42 42 41 41 40 40 39 39 38 38 37 37 36 36 35 35 34 34 33 33 32 32 31 31 30 30 29 29 28 28 27 27 26 26 25 25 24 24 23 23 22 22 21 21 20 20 19 19 18 18 17 17 16 16 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\r\n", "output": "1\r\n"}, {"input": "50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "50\r\n"}, {"input": "50\r\n50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "50\r\n"}, {"input": "10\r\n2 9 4 1 6 7 10 3 1 5 8 6 2 3 10 7 4 8 5 9\r\n", "output": "9\r\n"}]
| false
|
stdio
| null | true
|
780/D
|
780
|
D
|
PyPy 3
|
PRETESTS
| 3
| 93
| 23,142,400
|
25244432
|
a = set()
c = 0
t1 = dict()
t2 = dict()
n = int(input())
h1 = [[] for i in range(1000)]
h2 = [[] for i in range(1000)]
h3 = []
for i in range(n):
g, h = input().split()
s1 = g[:3]
s2 = g[:2] + h[0]
if s1 not in a:
t1[c] = s1
t2[s1] = c
a.add(s1)
c+=1
if s2 not in a:
t1[c] = s2
t2[s2] = c
a.add(s2)
c+=1
h1[t2[s1]].append(i)
h1[t2[s2]].append(i)
h3.append([t2[s1], t2[s2]])
flag = True
res = [[""] for i in range(1000)]
for i in range(c):
if (len(h1[i]) > 1):
for k in h1[i]:
if t1[h3[k][1]] in a:
res[k] = t1[h3[k][1]]
a.remove(t1[h3[k][1]])
else:
flag = False
if flag:
for k in range(n):
if (res[k] == ['']):
if (t1[h3[k][0]] in a):
res[k] = t1[h3[k][0]]
a.remove(t1[h3[k][0]])
elif (t1[h3[k][1]] in a):
res[k] = t1[h3[k][1]]
a.remove(t1[h3[k][1]])
else:
flag = False
if flag:
print("YES")
for i in range(n):
print(res[i])
else:
print("NO")
else:
print("NO")
| 41
| 155
| 20,275,200
|
87930633
|
from sys import stdin
n = int(stdin.readline().strip())
T,A = [],[]
N,M = {},{}
for _ in range(n):
t,h = stdin.readline().split()
n1,n2 = t[:3],t[:2]+h[0]
N[n1] = N.get(n1,0)+1
T.append((n1,n2))
A.append(n1)
def solve():
for i in range(n):
n1,n2 = T[i]
if n1 not in M and N[n1]==1:
M[n1] = i
continue
while n2 in M:
j = M[n2]
if n2==T[j][1]:
return False
M[n2],A[i]=i,n2
i,n2 = j,T[j][1]
else:
M[n2],A[i] = i,n2
return True
if solve():
print("YES")
print('\n'.join(A))
else:
print("NO")
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Innokenty and a Football League
|
Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.
Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:
1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".
Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.
Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.
|
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of clubs in the league.
Each of the next n lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.
|
It it is not possible to choose short names and satisfy all constraints, print a single line "NO".
Otherwise, in the first line print "YES". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.
If there are multiple answers, print any of them.
| null |
In the first sample Innokenty can choose first option for both clubs.
In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.
In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.
In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different.
|
[{"input": "2\nDINAMO BYTECITY\nFOOTBALL MOSCOW", "output": "YES\nDIN\nFOO"}, {"input": "2\nDINAMO BYTECITY\nDINAMO BITECITY", "output": "NO"}, {"input": "3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP", "output": "YES\nPLM\nPLS\nGOG"}, {"input": "3\nABC DEF\nABC EFG\nABD OOO", "output": "YES\nABD\nABE\nABO"}]
| 1,900
|
["2-sat", "graphs", "greedy", "implementation", "shortest paths", "strings"]
| 41
|
[{"input": "2\r\nDINAMO BYTECITY\r\nFOOTBALL MOSCOW\r\n", "output": "YES\r\nDIN\r\nFOO\r\n"}, {"input": "2\r\nDINAMO BYTECITY\r\nDINAMO BITECITY\r\n", "output": "NO\r\n"}, {"input": "3\r\nPLAYFOOTBALL MOSCOW\r\nPLAYVOLLEYBALL SPB\r\nGOGO TECHNOCUP\r\n", "output": "YES\r\nPLM\r\nPLS\r\nGOG\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABD OOO\r\n", "output": "YES\r\nABD\r\nABE\r\nABO\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABC EEEEE\r\n", "output": "NO\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABD CABA\r\n", "output": "YES\r\nABD\r\nABE\r\nABC\r\n"}, {"input": "3\r\nABC DEF\r\nABC EFG\r\nABD EABA\r\n", "output": "NO\r\n"}, {"input": "1\r\nAAA AAA\r\n", "output": "YES\r\nAAA\r\n"}, {"input": "1\r\nAAAAAAAAAAAAAAAAAAAA ZZZZZZZZZZZZZZZZZZZZ\r\n", "output": "YES\r\nAAA\r\n"}, {"input": "5\r\nADAC BABC\r\nABB DCB\r\nABB BCDC\r\nDBAC BAC\r\nDBBC DBC\r\n", "output": "YES\r\nADA\r\nABD\r\nABB\r\nDBA\r\nDBB\r\n"}, {"input": "5\r\nIAH HJIE\r\nIAH FJK\r\nIAH BIAA\r\nIAH AFG\r\nIAH DEFF\r\n", "output": "YES\r\nIAH\r\nIAF\r\nIAB\r\nIAA\r\nIAD\r\n"}, {"input": "10\r\nIJGDI KHB\r\nHBI CKKCG\r\nFHE GCAA\r\nEDCGH HHICE\r\nGFH AIHD\r\nHED KIK\r\nDCK BCFIJ\r\nFFIHE FDB\r\nJGB AKKI\r\nIJD CAG\r\n", "output": "YES\r\nIJG\r\nHBI\r\nFHE\r\nEDC\r\nGFH\r\nHED\r\nDCK\r\nFFI\r\nJGB\r\nIJD\r\n"}, {"input": "10\r\nEDBG IGGAC\r\nEDBG GIKAG\r\nEDBG IKGEI\r\nEDBG AJEG\r\nEDBG HAD\r\nEDBG ACKK\r\nEDBG FEDE\r\nEDBG DAB\r\nEDBG CCJBD\r\nEDBG KKGFB\r\n", "output": "NO\r\n"}, {"input": "10\r\nADE GBH\r\nJJDGJ AAF\r\nJJDGJ BBKG\r\nADE FKH\r\nADE CIA\r\nAIE JCBJ\r\nAIE BBJB\r\nEBAK JDB\r\nJJDGJ IDBG\r\nCEJE FIG\r\n", "output": "YES\r\nADG\r\nJJA\r\nJJB\r\nADF\r\nADC\r\nAIJ\r\nAIB\r\nEBA\r\nJJI\r\nCEJ\r\n"}, {"input": "4\r\nABA DEF\r\nABB DEF\r\nABC DEF\r\nABE DEF\r\n", "output": "YES\r\nABA\r\nABB\r\nABC\r\nABE\r\n"}, {"input": "2\r\nABC CCC\r\nABE CCC\r\n", "output": "YES\r\nABC\r\nABE\r\n"}, {"input": "2\r\nABS SSS\r\nABD SSD\r\n", "output": "YES\r\nABS\r\nABD\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_output_path):
with open(input_path) as f:
n = int(f.readline())
clubs = []
for _ in range(n):
team, hometown = f.readline().strip().split()
clubs.append((team, hometown))
ai_list = []
bi_list = []
for team, hometown in clubs:
ai = team[:3]
bi = team[:2] + hometown[0]
ai_list.append(ai)
bi_list.append(bi)
with open(submission_output_path) as f:
lines = [line.strip() for line in f.readlines()]
if not lines:
print(0)
return
first_line = lines[0].upper()
if first_line == "NO":
is_possible = is_solvable(ai_list, bi_list, clubs)
print(1 if not is_possible else 0)
elif first_line == "YES":
if len(lines) != n + 1:
print(0)
return
submission_names = lines[1:]
valid = True
for i in range(n):
if submission_names[i] not in (ai_list[i], bi_list[i]):
valid = False
break
if not valid or len(set(submission_names)) != n:
print(0)
return
forced_ais = set()
for i in range(n):
if submission_names[i] == bi_list[i]:
forced_ais.add(ai_list[i])
for ai in forced_ais:
for j in range(n):
if ai_list[j] == ai and submission_names[j] != bi_list[j]:
print(0)
return
print(1)
else:
print(0)
def is_solvable(ai_list, bi_list, clubs):
n = len(ai_list)
forced = [False] * n
freq = {}
for ai in ai_list:
freq[ai] = freq.get(ai, 0) + 1
q = []
for i in range(n):
if freq[ai_list[i]] > 1:
if not forced[i]:
forced[i] = True
q.append(i)
from collections import deque
q = deque(q)
while q:
i = q.popleft()
current_bi = bi_list[i]
for j in range(n):
if ai_list[j] == current_bi and not forced[j]:
forced[j] = True
q.append(j)
forced_names = [bi_list[i] for i in range(n) if forced[i]]
unforced_ais = [ai_list[i] for i in range(n) if not forced[i]]
forced_ais_set = {ai_list[i] for i in range(n) if forced[i]}
if len(set(forced_names)) != len(forced_names):
return False
for ai in unforced_ais:
if ai in forced_ais_set:
return False
if len(unforced_ais) != len(set(unforced_ais)):
return False
return True
if __name__ == "__main__":
input_path, output_path, submission_output_path = sys.argv[1:4]
main(input_path, output_path, submission_output_path)
| true
|
350/B
|
350
|
B
|
PyPy 3-64
|
TESTS
| 8
| 218
| 19,353,600
|
224589502
|
def solve():
n = int(input())
arr = list(map(int,input().split()))
lis = list(map(int,input().split()))
degree = [0]*n
parent = [-1]*n
for i in range(n):
parent[i] = lis[i]-1
degree[lis[i]-1]+=1
ans = []
visited = [False]*n
for i in range(n):
if arr[i]==1:
s = i
tmp = [s+1]
s = parent[s]
while s!=-1 and not visited[s] and arr[s] == 0 and degree[s]<=1:
visited[s] = True
tmp.append(s+1)
s = parent[s]
if len(tmp)>len(ans):
ans = tmp
ans = ans[::-1]
print(len(ans))
print(*ans)
# number of test cases
t = 1
#t = int(input())
for i in range(t):
solve()
| 36
| 248
| 21,811,200
|
164910916
|
if __name__ == '__main__':
n = int(input())
types = [0] + list(map(int, input().split()))
nodes = [0] + list(map(int, input().split()))
sol, count = [], [0 for i in range(n + 1)]
for i in nodes:
count[i] += 1
for i in range(1, n+1):
if types[i] == 1:
path = [i]
x = nodes[i]
while count[x] == 1:
path.append(x)
x = nodes[x]
if len(path) > len(sol):
sol = path[:]
sol.reverse()
print(len(sol))
print(*sol)
# print(' '.join(map(str, sol)))
|
Codeforces Round 203 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Resort
|
Valera's finally decided to go on holiday! He packed up and headed for a ski resort.
Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.
Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.
Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions:
1. Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel.
2. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1.
3. The path contains as many objects as possible (k is maximal).
Help Valera. Find such path that meets all the criteria of our hero!
|
The first line contains integer n (1 ≤ n ≤ 105) — the number of objects.
The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.
The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number ai equals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.
|
In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.
| null | null |
[{"input": "5\n0 0 0 0 1\n0 1 2 3 4", "output": "5\n1 2 3 4 5"}, {"input": "5\n0 0 1 0 1\n0 1 2 2 4", "output": "2\n4 5"}, {"input": "4\n1 0 0 0\n2 3 4 2", "output": "1\n1"}]
| 1,500
|
["graphs"]
| 36
|
[{"input": "5\r\n0 0 0 0 1\r\n0 1 2 3 4\r\n", "output": "5\r\n1 2 3 4 5\r\n"}, {"input": "5\r\n0 0 1 0 1\r\n0 1 2 2 4\r\n", "output": "2\r\n4 5\r\n"}, {"input": "4\r\n1 0 0 0\r\n2 3 4 2\r\n", "output": "1\r\n1\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 1\r\n4 0 8 4 7 8 5 5 7 2\r\n", "output": "2\r\n2 10\r\n"}, {"input": "50\r\n0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0\r\n28 4 33 22 4 35 36 31 42 25 50 33 25 36 18 23 23 28 43 3 18 31 1 2 15 22 40 43 29 32 28 35 18 27 48 40 14 36 27 50 40 5 48 14 36 24 32 33 26 50\r\n", "output": "2\r\n3 20\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0\r\n86 12 47 46 45 31 20 47 58 79 23 70 35 72 37 20 16 64 46 87 57 7 84 72 70 3 14 40 17 42 30 99 12 20 38 98 14 40 4 83 10 15 47 30 83 58 12 7 97 46 17 6 41 13 87 37 36 12 7 25 26 35 69 13 18 5 9 53 72 28 13 51 5 57 14 64 28 25 91 96 57 69 9 12 97 7 56 42 31 15 88 16 41 88 86 13 89 81 3 42\r\n", "output": "1\r\n44\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n6 2 7 8 2 9 0 5 4 2\r\n", "output": "6\r\n5 8 4 9 6 1\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n38 2 49 55 6 42 12 100 25 69 85 76 13 22 78 73 37 64 5 21 0 23 61 87 4 16 44 3 98 54 1 91 18 26 82 24 18 50 95 21 75 97 51 9 67 73 51 19 63 92 27 82 8 7 20 84 2 93 40 11 39 80 58 85 74 48 72 78 34 33 31 65 46 71 32 36 33 88 47 4 66 84 16 27 16 14 90 16 79 41 99 30 57 73 28 89 45 81 86 29\r\n", "output": "52\r\n57 93 58 63 49 3 28 95 39 61 23 22 14 86 99 91 32 75 41 90 87 24 36 76 12 7 54 30 92 50 38 1 31 71 74 65 72 67 45 97 42 6 5 19 48 66 81 98 29 100 8 53\r\n"}, {"input": "2\r\n1 1\r\n0 0\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n1\r\n0\r\n", "output": "1\r\n1\r\n"}]
| false
|
stdio
| null | true
|
350/B
|
350
|
B
|
PyPy 3
|
TESTS
| 8
| 374
| 32,358,400
|
122915385
|
from collections import defaultdict
'''
from obhect ai-> i there is a path that exists
'''
blanc=[]
g=defaultdict(list)
n=int(input())
type=list(map(int,input().strip().split()))
path=list(map(int,input().strip().split()))
path=list(map(lambda s:s-1,path))
ans=[]
deg=[0]*(n)
for i in path:
deg[i]+=1
for i in range(n):
t=i
if type[i]==1:
cur=[t]
while path[t]!=-1 and deg[path[t]]<=1:
t=path[t]
cur.append(t)
ans=max(ans,cur,key=len)
ans=list(map(lambda s:s+1,ans))
print(len(ans))
print(*ans[::-1])
| 36
| 312
| 9,011,200
|
142931702
|
n = int(input())
t = [0] + list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
ans, cnt = [], [0 for i in range(n + 1)]
for i in a:
cnt[i] += 1
for i in range(1, n + 1):
if t[i] == 1:
crt = [i]
x = a[i]
while cnt[x] == 1:
crt.append(x)
x = a[x]
if len(crt) > len(ans):
ans = crt[:]
ans.reverse()
print(len(ans))
print(' '.join(map(str, ans)))
|
Codeforces Round 203 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Resort
|
Valera's finally decided to go on holiday! He packed up and headed for a ski resort.
Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.
Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.
Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions:
1. Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel.
2. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1.
3. The path contains as many objects as possible (k is maximal).
Help Valera. Find such path that meets all the criteria of our hero!
|
The first line contains integer n (1 ≤ n ≤ 105) — the number of objects.
The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.
The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number ai equals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.
|
In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.
| null | null |
[{"input": "5\n0 0 0 0 1\n0 1 2 3 4", "output": "5\n1 2 3 4 5"}, {"input": "5\n0 0 1 0 1\n0 1 2 2 4", "output": "2\n4 5"}, {"input": "4\n1 0 0 0\n2 3 4 2", "output": "1\n1"}]
| 1,500
|
["graphs"]
| 36
|
[{"input": "5\r\n0 0 0 0 1\r\n0 1 2 3 4\r\n", "output": "5\r\n1 2 3 4 5\r\n"}, {"input": "5\r\n0 0 1 0 1\r\n0 1 2 2 4\r\n", "output": "2\r\n4 5\r\n"}, {"input": "4\r\n1 0 0 0\r\n2 3 4 2\r\n", "output": "1\r\n1\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 1\r\n4 0 8 4 7 8 5 5 7 2\r\n", "output": "2\r\n2 10\r\n"}, {"input": "50\r\n0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0\r\n28 4 33 22 4 35 36 31 42 25 50 33 25 36 18 23 23 28 43 3 18 31 1 2 15 22 40 43 29 32 28 35 18 27 48 40 14 36 27 50 40 5 48 14 36 24 32 33 26 50\r\n", "output": "2\r\n3 20\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0\r\n86 12 47 46 45 31 20 47 58 79 23 70 35 72 37 20 16 64 46 87 57 7 84 72 70 3 14 40 17 42 30 99 12 20 38 98 14 40 4 83 10 15 47 30 83 58 12 7 97 46 17 6 41 13 87 37 36 12 7 25 26 35 69 13 18 5 9 53 72 28 13 51 5 57 14 64 28 25 91 96 57 69 9 12 97 7 56 42 31 15 88 16 41 88 86 13 89 81 3 42\r\n", "output": "1\r\n44\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n6 2 7 8 2 9 0 5 4 2\r\n", "output": "6\r\n5 8 4 9 6 1\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n38 2 49 55 6 42 12 100 25 69 85 76 13 22 78 73 37 64 5 21 0 23 61 87 4 16 44 3 98 54 1 91 18 26 82 24 18 50 95 21 75 97 51 9 67 73 51 19 63 92 27 82 8 7 20 84 2 93 40 11 39 80 58 85 74 48 72 78 34 33 31 65 46 71 32 36 33 88 47 4 66 84 16 27 16 14 90 16 79 41 99 30 57 73 28 89 45 81 86 29\r\n", "output": "52\r\n57 93 58 63 49 3 28 95 39 61 23 22 14 86 99 91 32 75 41 90 87 24 36 76 12 7 54 30 92 50 38 1 31 71 74 65 72 67 45 97 42 6 5 19 48 66 81 98 29 100 8 53\r\n"}, {"input": "2\r\n1 1\r\n0 0\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n1\r\n0\r\n", "output": "1\r\n1\r\n"}]
| false
|
stdio
| null | true
|
350/B
|
350
|
B
|
Python 3
|
TESTS
| 8
| 372
| 12,492,800
|
17932843
|
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = list([0 for _ in range(n)])
for i in a:
cnt[i - 1] += 1
def func(x):
ans = [x + 1]
x = a[x] - 1
while x >= 0 and t[x] == 0 and cnt[x] == 1:
ans.insert(0, x + 1)
x = a[x] - 1
return ans
ans = []
for i in range(n):
if t[i] == 1:
tmp = func(i)
if len(tmp) > len(ans):
ans = tmp
print(len(ans))
print(' '.join(map(str, ans)))
| 36
| 340
| 19,660,800
|
224590425
|
def solve():
n = int(input())
arr = list(map(int,input().split()))
parent = list(map(int,input().split()))
degree = [0]*n
for i in range(n):
parent[i] -=1
if parent[i]!=-1:
degree[parent[i]]+=1
ans = []
visited = [False]*n
for i in range(n):
if arr[i]==1:
s = i
tmp = []
while parent[s]!=-1 and degree[parent[s]]<=1:
tmp.append(s)
s = parent[s]
tmp.append(s)
if len(tmp)>len(ans):
ans = tmp
ans = ans[::-1]
print(len(ans))
for i in ans:
print(i+1,end=' ')
# number of test cases
t = 1
#t = int(input())
for i in range(t):
solve()
|
Codeforces Round 203 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Resort
|
Valera's finally decided to go on holiday! He packed up and headed for a ski resort.
Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.
Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.
Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions:
1. Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel.
2. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1.
3. The path contains as many objects as possible (k is maximal).
Help Valera. Find such path that meets all the criteria of our hero!
|
The first line contains integer n (1 ≤ n ≤ 105) — the number of objects.
The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.
The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number ai equals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.
|
In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.
| null | null |
[{"input": "5\n0 0 0 0 1\n0 1 2 3 4", "output": "5\n1 2 3 4 5"}, {"input": "5\n0 0 1 0 1\n0 1 2 2 4", "output": "2\n4 5"}, {"input": "4\n1 0 0 0\n2 3 4 2", "output": "1\n1"}]
| 1,500
|
["graphs"]
| 36
|
[{"input": "5\r\n0 0 0 0 1\r\n0 1 2 3 4\r\n", "output": "5\r\n1 2 3 4 5\r\n"}, {"input": "5\r\n0 0 1 0 1\r\n0 1 2 2 4\r\n", "output": "2\r\n4 5\r\n"}, {"input": "4\r\n1 0 0 0\r\n2 3 4 2\r\n", "output": "1\r\n1\r\n"}, {"input": "10\r\n0 0 0 0 0 0 0 0 0 1\r\n4 0 8 4 7 8 5 5 7 2\r\n", "output": "2\r\n2 10\r\n"}, {"input": "50\r\n0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0\r\n28 4 33 22 4 35 36 31 42 25 50 33 25 36 18 23 23 28 43 3 18 31 1 2 15 22 40 43 29 32 28 35 18 27 48 40 14 36 27 50 40 5 48 14 36 24 32 33 26 50\r\n", "output": "2\r\n3 20\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0\r\n86 12 47 46 45 31 20 47 58 79 23 70 35 72 37 20 16 64 46 87 57 7 84 72 70 3 14 40 17 42 30 99 12 20 38 98 14 40 4 83 10 15 47 30 83 58 12 7 97 46 17 6 41 13 87 37 36 12 7 25 26 35 69 13 18 5 9 53 72 28 13 51 5 57 14 64 28 25 91 96 57 69 9 12 97 7 56 42 31 15 88 16 41 88 86 13 89 81 3 42\r\n", "output": "1\r\n44\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n6 2 7 8 2 9 0 5 4 2\r\n", "output": "6\r\n5 8 4 9 6 1\r\n"}, {"input": "100\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n38 2 49 55 6 42 12 100 25 69 85 76 13 22 78 73 37 64 5 21 0 23 61 87 4 16 44 3 98 54 1 91 18 26 82 24 18 50 95 21 75 97 51 9 67 73 51 19 63 92 27 82 8 7 20 84 2 93 40 11 39 80 58 85 74 48 72 78 34 33 31 65 46 71 32 36 33 88 47 4 66 84 16 27 16 14 90 16 79 41 99 30 57 73 28 89 45 81 86 29\r\n", "output": "52\r\n57 93 58 63 49 3 28 95 39 61 23 22 14 86 99 91 32 75 41 90 87 24 36 76 12 7 54 30 92 50 38 1 31 71 74 65 72 67 45 97 42 6 5 19 48 66 81 98 29 100 8 53\r\n"}, {"input": "2\r\n1 1\r\n0 0\r\n", "output": "1\r\n1\r\n"}, {"input": "1\r\n1\r\n0\r\n", "output": "1\r\n1\r\n"}]
| false
|
stdio
| null | true
|
51/B
|
51
|
B
|
Python 3
|
TESTS
| 8
| 248
| 7,475,200
|
88444268
|
from sys import stdin
import re
fef,ans = '',[]
for sus in stdin.readlines():
fef+=sus
fuf = re.split(r'<table>',fef)
for cuc in fuf:
nut = cuc.count('<td>')
if nut: ans.append(nut)
ans.sort()
print(*ans)
| 19
| 124
| 512,000
|
14009626
|
import re
lines = []
while True:
try:
line = input()
except EOFError:
break
lines.append(line.strip())
text = ''.join(lines)
text = re.sub('<table>', '(', text)
text = re.sub('</table>', ')', text)
text = re.sub('<td>', '.', text)
text = re.sub('[^().]', '', text)
sizes = []
def parse(pos):
count = 0
while pos < len(text):
ch = text[pos]
if ch == '(':
pos = parse(pos + 1)
continue
if ch == ')':
sizes.append(count)
return pos + 1
count += 1
pos += 1
parse(0)
print(' '.join(map(str, sorted(sizes))))
|
Codeforces Beta Round 48
|
CF
| 2,010
| 2
| 256
|
bHTML Tables Analisys
|
In this problem is used an extremely simplified version of HTML table markup. Please use the statement as a formal document and read it carefully.
A string is a bHTML table, if it satisfies the grammar:
Blanks in the grammar are only for purposes of illustration, in the given data there will be no spaces. The bHTML table is very similar to a simple regular HTML table in which meet only the following tags : "table", "tr", "td", all the tags are paired and the table contains at least one row and at least one cell in each row. Have a look at the sample tests as examples of tables.
As can be seen, the tables may be nested. You are given a table (which may contain other(s)). You need to write a program that analyzes all the tables and finds the number of cells in each of them. The tables are not required to be rectangular.
|
For convenience, input data can be separated into non-empty lines in an arbitrary manner. The input data consist of no more than 10 lines. Combine (concatenate) all the input lines into one, to get a text representation s of the specified table. String s corresponds to the given grammar (the root element of grammar is TABLE), its length does not exceed 5000. Only lower case letters are used to write tags. There are no spaces in the given string s.
|
Print the sizes of all the tables in the non-decreasing order.
| null | null |
[{"input": "<table><tr><td></td></tr></table>", "output": "1"}, {"input": "<table>\n<tr>\n<td>\n<table><tr><td></td></tr><tr><td></\ntd\n></tr><tr\n><td></td></tr><tr><td></td></tr></table>\n</td>\n</tr>\n</table>", "output": "1 4"}, {"input": "<table><tr><td>\n<table><tr><td>\n<table><tr><td>\n<table><tr><td></td><td></td>\n</tr><tr><td></td></tr></table>\n</td></tr></table>\n</td></tr></table>\n</td></tr></table>", "output": "1 1 1 3"}]
| 1,700
|
["expression parsing"]
| 19
|
[{"input": "<table><tr><td></td></tr></table>\r\n", "output": "1 "}, {"input": "<table>\r\n<tr>\r\n<td>\r\n<table><tr><td></td></tr><tr><td></\r\ntd\r\n></tr><tr\r\n><td></td></tr><tr><td></td></tr></table>\r\n</td>\r\n</tr>\r\n</table>\r\n", "output": "1 4 "}, {"input": "<table><tr><td>\r\n<table><tr><td>\r\n<table><tr><td>\r\n<table><tr><td></td><td></td>\r\n</tr><tr><td></td></tr></table>\r\n</td></tr></table>\r\n</td></tr></table>\r\n</td></tr></table>\r\n", "output": "1 1 1 3 "}, {"input": "<\r\nt\r\na\r\nble><tr><td></td>\r\n</\r\ntr>\r\n</\r\nt\r\nab\r\nle>\r\n", "output": "1 "}, {"input": "<table><tr><td><table><tr><td></td></tr></table></td></tr></table>\r\n", "output": "1 1 "}, {"input": "<table><tr><td><table><tr><td><table><tr><td></td></tr></table></td></tr></table></td></tr></table>\r\n", "output": "1 1 1 "}, {"input": "<table><tr><td><table><tr><td></td></tr></table></td></tr></table>\r\n", "output": "1 1 "}, {"input": "<table><tr><td><table><tr><td><table><tr><td></td></tr></table></td></tr></table></td></tr></table>\r\n", "output": "1 1 1 "}, {"input": "<table><tr><td><table><tr><td></td><td></td></tr></table></td><td><table><tr><td></td></tr></table></td></tr></table>\r\n", "output": "1 2 2 "}, {"input": "<table><tr><td><table><tr><td></td><td></td></tr></table></td><td><table><tr><td></td></tr></table></td></tr></table>\r\n", "output": "1 2 2 "}, {"input": "<table><tr><td><table><tr><td></td></tr></table></td></tr><tr><td><table><tr><td><table><tr><td></td></tr></table></td></tr></table></td></tr></table>\r\n", "output": "1 1 1 2 "}]
| false
|
stdio
| null | true
|
678/E
|
678
|
E
|
Python 3
|
TESTS
| 0
| 15
| 0
|
200673677
|
from functools import lru_cache
from itertools import combinations
n = int(input())
A = [[0] * n for _ in range(n)]
for r in range(n):
A[r] = [float(x) for x in input().split(' ')]
n = len(A)
@lru_cache(None)
def dp(mask, res = 0):
if bin(mask).count('1') == n - 1:
return mask == (1 << n) - 1 - 1
for i, j in combinations(range(n), 2):
if not mask & (1 << i) and not mask & (1 << j):
i_win = A[i][j] * dp(mask | (1 << j))
j_win = A[j][i] * dp(mask | (1 << i))
res = max(res, i_win, j_win)
return res
res = dp(0)
print(res)
| 41
| 1,263
| 57,036,800
|
152774652
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
p = [list(map(float, input().split())) for _ in range(n)]
pow2 = [1]
for _ in range(n):
pow2.append(2 * pow2[-1])
pn = pow2[n]
dp = [[0] * n for _ in range(pn)]
dp[1][0] = 1
for i in range(2, pn):
dpi = dp[i]
b = []
for j in range(n):
if i & pow2[j]:
b.append(j)
for j in b:
pj = pow2[j]
for k in b:
if not j ^ k:
continue
pk = pow2[k]
dpi[j] = max(dpi[j], dp[i ^ pj][k] * p[k][j] + dp[i ^ pk][j] * p[j][k])
ans = max(dp[pn - 1])
print(ans)
|
Educational Codeforces Round 13
|
ICPC
| 2,016
| 2.5
| 256
|
Another Sith Tournament
|
The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.
|
The first line contains a single integer n (1 ≤ n ≤ 18) — the number of participants of the Sith Tournament.
Each of the next n lines contains n real numbers, which form a matrix pij (0 ≤ pij ≤ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.
The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants.
|
Output a real number — the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.
| null | null |
[{"input": "3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0", "output": "0.680000000000000"}]
| 2,200
|
["bitmasks", "dp", "math", "probabilities"]
| 41
|
[{"input": "3\r\n0.0 0.5 0.8\r\n0.5 0.0 0.4\r\n0.2 0.6 0.0\r\n", "output": "0.680000000000000\n"}, {"input": "1\r\n0.0\r\n", "output": "1.000000000000000\n"}, {"input": "2\r\n0.00 0.75\r\n0.25 0.00\r\n", "output": "0.750000000000000\n"}, {"input": "4\r\n0.0 0.6 0.5 0.4\r\n0.4 0.0 0.3 0.8\r\n0.5 0.7 0.0 0.5\r\n0.6 0.2 0.5 0.0\r\n", "output": "0.545000000000000\n"}, {"input": "4\r\n0.0 0.3 0.5 0.6\r\n0.7 0.0 0.1 0.4\r\n0.5 0.9 0.0 0.6\r\n0.4 0.6 0.4 0.0\r\n", "output": "0.534000000000000\n"}, {"input": "2\r\n0.0 0.0\r\n1.0 0.0\r\n", "output": "0.000000000000000\n"}, {"input": "2\r\n0.0 1.0\r\n0.0 0.0\r\n", "output": "1.000000000000000\n"}, {"input": "5\r\n0.0 0.3 0.4 0.5 0.6\r\n0.7 0.0 0.2 0.6 0.8\r\n0.6 0.8 0.0 0.6 0.3\r\n0.5 0.4 0.4 0.0 0.5\r\n0.4 0.2 0.7 0.5 0.0\r\n", "output": "0.522400000000000\n"}, {"input": "6\r\n0.00 0.15 0.25 0.35 0.45 0.55\r\n0.85 0.00 0.35 0.45 0.55 0.65\r\n0.75 0.65 0.00 0.75 0.85 0.15\r\n0.65 0.55 0.25 0.00 0.40 0.35\r\n0.55 0.45 0.15 0.60 0.00 0.70\r\n0.45 0.35 0.85 0.65 0.30 0.00\r\n", "output": "0.483003750000000\n"}, {"input": "4\r\n0.0 1.0 1.0 1.0\r\n0.0 0.0 0.0 1.0\r\n0.0 1.0 0.0 0.0\r\n0.0 0.0 1.0 0.0\r\n", "output": "1.000000000000000\n"}, {"input": "4\r\n0.0 1.0 1.0 1.0\r\n0.0 0.0 0.0 0.0\r\n0.0 1.0 0.0 0.0\r\n0.0 1.0 1.0 0.0\r\n", "output": "1.000000000000000\n"}, {"input": "4\r\n0.0 1.0 1.0 0.0\r\n0.0 0.0 0.9 0.2\r\n0.0 0.1 0.0 1.0\r\n1.0 0.8 0.0 0.0\r\n", "output": "1.000000000000000\n"}, {"input": "5\r\n0.0 0.0 0.0 0.0 0.0\r\n1.0 0.0 0.5 0.5 0.5\r\n1.0 0.5 0.0 0.5 0.5\r\n1.0 0.5 0.5 0.0 0.5\r\n1.0 0.5 0.5 0.5 0.0\r\n", "output": "0.000000000000000\n"}, {"input": "2\r\n0.000000 0.032576\r\n0.967424 0.000000\r\n", "output": "0.032576000000000\n"}, {"input": "3\r\n0.000000 0.910648 0.542843\r\n0.089352 0.000000 0.537125\r\n0.457157 0.462875 0.000000\r\n", "output": "0.740400260625000\n"}, {"input": "4\r\n0.000000 0.751720 0.572344 0.569387\r\n0.248280 0.000000 0.893618 0.259864\r\n0.427656 0.106382 0.000000 0.618783\r\n0.430613 0.740136 0.381217 0.000000\r\n", "output": "0.688466450920859\n"}, {"input": "5\r\n0.000000 0.629791 0.564846 0.602334 0.362179\r\n0.370209 0.000000 0.467868 0.924988 0.903018\r\n0.435154 0.532132 0.000000 0.868573 0.209581\r\n0.397666 0.075012 0.131427 0.000000 0.222645\r\n0.637821 0.096982 0.790419 0.777355 0.000000\r\n", "output": "0.607133963373199\n"}, {"input": "6\r\n0.000000 0.433864 0.631347 0.597596 0.794426 0.713555\r\n0.566136 0.000000 0.231193 0.396458 0.723050 0.146212\r\n0.368653 0.768807 0.000000 0.465978 0.546227 0.309438\r\n0.402404 0.603542 0.534022 0.000000 0.887926 0.456734\r\n0.205574 0.276950 0.453773 0.112074 0.000000 0.410517\r\n0.286445 0.853788 0.690562 0.543266 0.589483 0.000000\r\n", "output": "0.717680454673393\n"}, {"input": "7\r\n0.000000 0.311935 0.623164 0.667542 0.225988 0.921559 0.575083\r\n0.688065 0.000000 0.889215 0.651525 0.119843 0.635314 0.564710\r\n0.376836 0.110785 0.000000 0.583317 0.175043 0.795995 0.836790\r\n0.332458 0.348475 0.416683 0.000000 0.263615 0.469602 0.883191\r\n0.774012 0.880157 0.824957 0.736385 0.000000 0.886308 0.162544\r\n0.078441 0.364686 0.204005 0.530398 0.113692 0.000000 0.023692\r\n0.424917 0.435290 0.163210 0.116809 0.837456 0.976308 0.000000\r\n", "output": "0.721455539644280\n"}, {"input": "2\r\n0 0.233\r\n0.767 0\r\n", "output": "0.233000000000000\n"}]
| false
|
stdio
|
import sys
def read_value(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:
return None
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
correct = read_value(correct_output_path)
sub = read_value(submission_output_path)
if correct is None or sub is None:
print(0)
return
a, b = correct, sub
if a == b:
print(1)
return
abs_err = abs(a - b)
if abs_err <= 1e-6:
print(1)
return
max_abs = max(abs(a), abs(b))
rel_err = abs_err / max_abs if max_abs != 0 else 0
if rel_err <= 1e-6:
print(1)
return
print(0)
if __name__ == "__main__":
main()
| true
|
813/C
|
813
|
C
|
PyPy 3
|
TESTS
| 18
| 841
| 52,019,200
|
89172173
|
from sys import stdin, stdout
import math,sys,heapq
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
from os import path
import bisect as bi
def yes():print('YES')
def no():print('NO')
if (path.exists('input.txt')):
#------------------Sublime--------------------------------------#
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
def I():return (int(input()))
def In():return(map(int,input().split()))
else:
#------------------PYPY FAst I/o--------------------------------#
def I():return (int(stdin.readline()))
def In():return(map(int,stdin.readline().split()))
def dict(a):
d={}
z=[]
for x in a:
if d.get(x,-1)!=-1:
d[x]+=1
else:
d[x]=1
z.append(x)
return [d,z]
def find_gt(a, x):
'Find leftmost value greater than x'
i = bi.bisect_right(a, x)
if i != len(a):
return i
else:
return -1
def dfs(d,visit,x,alice,cnt):
visit[x]=True
alice[x]=cnt
for t in d[x]:
if visit[t]==False:
dfs(d,visit,t,alice,cnt+1)
def main():
try:
n,s=In()
d=defaultdict(list)
for x in range(n-1):
a,b=In()
d[a].append(b)
d[b].append(a)
alice=[0]*(n+1)
bob=[0]*(n+1)
visit=[False]*(n+1)
dfs(d,visit,1,alice,0)
visit=[False]*(n+1)
dfs(d,visit,s,bob,0)
#print(alice)
#print(bob)
ans=0
for i in range(1,n+1):
if bob[i]<alice[i]:
ans=max(ans,2*(alice[i]))
print(ans)
except:
pass
M = 998244353
P = 1000000007
if __name__ == '__main__':
#for _ in range(I()):main()
for _ in range(1):main()
| 25
| 498
| 31,334,400
|
181265003
|
from sys import stdin
input=lambda :stdin.readline()[:-1]
n,x=map(int,input().split())
x-=1
edge=[[] for i in range(n)]
for i in range(n-1):
a,b=map(lambda x:int(x)-1,input().split())
edge[a].append(b)
edge[b].append(a)
def dfs(r):
dist=[0]*n
todo=[(r,-1)]
while todo:
v,p=todo.pop()
for u in edge[v]:
if u!=p:
todo.append((u,v))
dist[u]=dist[v]+1
return dist
dist1=dfs(0)
dist2=dfs(x)
ans=0
for i in range(n):
if dist1[i]>dist2[i]:
ans=max(ans,dist1[i]*2)
print(ans)
|
Educational Codeforces Round 22
|
ICPC
| 2,017
| 1
| 256
|
The Tag Game
|
Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.
Alice starts at vertex 1 and Bob starts at vertex x (x ≠ 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.
The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.
You should write a program which will determine how many moves will the game last.
|
The first line contains two integer numbers n and x (2 ≤ n ≤ 2·105, 2 ≤ x ≤ n).
Each of the next n - 1 lines contains two integer numbers a and b (1 ≤ a, b ≤ n) — edges of the tree. It is guaranteed that the edges form a valid tree.
|
Print the total number of moves Alice and Bob will make.
| null |
In the first example the tree looks like this:
The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:
B: stay at vertex 3
A: go to vertex 2
B: stay at vertex 3
A: go to vertex 3
In the second example the tree looks like this:
The moves in the optimal strategy are:
B: go to vertex 3
A: go to vertex 2
B: go to vertex 4
A: go to vertex 3
B: stay at vertex 4
A: go to vertex 4
|
[{"input": "4 3\n1 2\n2 3\n2 4", "output": "4"}, {"input": "5 2\n1 2\n2 3\n3 4\n2 5", "output": "6"}]
| 1,700
|
["dfs and similar", "graphs"]
| 25
|
[{"input": "4 3\r\n1 2\r\n2 3\r\n2 4\r\n", "output": "4\r\n"}, {"input": "5 2\r\n1 2\r\n2 3\r\n3 4\r\n2 5\r\n", "output": "6\r\n"}, {"input": "2 2\r\n2 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n2 1\r\n3 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2\r\n3 2\r\n", "output": "4\r\n"}, {"input": "10 4\r\n5 4\r\n8 3\r\n4 6\r\n5 3\r\n7 9\r\n1 3\r\n5 10\r\n2 9\r\n9 8\r\n", "output": "8\r\n"}, {"input": "10 7\r\n8 7\r\n2 8\r\n2 3\r\n10 6\r\n4 6\r\n4 1\r\n10 5\r\n7 5\r\n9 8\r\n", "output": "16\r\n"}, {"input": "8 3\r\n2 1\r\n3 1\r\n4 3\r\n5 1\r\n6 1\r\n7 1\r\n8 6\r\n", "output": "4\r\n"}, {"input": "34 33\r\n2 1\r\n3 2\r\n4 3\r\n5 2\r\n6 3\r\n7 2\r\n8 5\r\n9 7\r\n10 8\r\n11 7\r\n12 7\r\n13 8\r\n14 2\r\n15 10\r\n16 1\r\n17 9\r\n18 14\r\n19 1\r\n20 2\r\n21 8\r\n22 21\r\n23 9\r\n24 6\r\n25 2\r\n26 20\r\n27 5\r\n28 20\r\n29 2\r\n30 10\r\n31 14\r\n32 12\r\n33 15\r\n34 8\r\n", "output": "12\r\n"}]
| false
|
stdio
| null | true
|
813/C
|
813
|
C
|
Python 3
|
TESTS
| 21
| 951
| 169,164,800
|
124943782
|
from collections import defaultdict
from sys import stdin, setrecursionlimit
input=stdin.readline
setrecursionlimit(10**5)
import threading
def dfs(node, parent, g, d, dp):
tt = 0
if parent != -1:
d[node] = d[parent] + 1
dp[node] = max(dp[node], d[node])
for i in g[node]:
if i != parent:
tt = max(tt, dfs(i, node, g, d, dp))
dp[node] = max(tt, dp[node])
return dp[node]
def dfs2(node,parent,g,d,dp,X,cd):
if node!= X and d[node]>cd:
dp[X]=max(dp[X],dp[node])
for i in g[node]:
if i!=parent:
dfs2(i,node,g,d,dp,X,cd+1)
def main():
g = defaultdict(list)
n, X = map(int, input().strip().split())
for i in range(n - 1):
x, y = map(int, input().strip().split())
g[x - 1].append(y - 1)
g[y - 1].append(x - 1)
d = [0] * (n)
dp = [0] * n
dfs(0, -1, g, d, dp)
X-=1
dfs2(X,-1,g,d,dp,X,0)
print(dp[X]*2)
threading.stack_size(10 ** 8)
t = threading.Thread(target=main)
t.start()
t.join()
| 25
| 498
| 66,764,800
|
161893732
|
from bisect import *
from collections import *
import sys
import io, os
import math
import random
from heapq import *
gcd = math.gcd
sqrt = math.sqrt
maxint=10**21
def ceil(a, b):
a = -a
k = a // b
k = -k
return k
# arr=list(map(int, input().split()))
# n,m=map(int,input().split())
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def strinp(testcases):
k = 5
if (testcases == -1 or testcases == 1):
k = 1
f = str(input())
f = f[2:len(f) - k]
return f
def main():
n,x=map(int,input().split())
graph=[[] for i in range(n)]
for i in range(n-1):
u,v=map(int,input().split())
graph[u-1].append(v-1)
graph[v-1].append(u-1)
x-=1
d1=[-1]*(n)
d2=[-1]*(n)
stk=[0]
d1[0]=0
while(stk!=[]):
f=stk.pop()
for i in graph[f]:
if(d1[i]!=-1):
continue
d1[i]=1+d1[f]
stk.append(i)
stk=[x]
d2[x]=0
while(stk!=[]):
f=stk.pop()
for i in graph[f]:
if(d2[i]!=-1):
continue
d2[i]=1+d2[f]
stk.append(i)
ma=0
for i in range(n):
if(len(graph[i])==1):
if(d1[i]>d2[i]):
ma=max(ma,d1[i])
print(2*ma)
main()
|
Educational Codeforces Round 22
|
ICPC
| 2,017
| 1
| 256
|
The Tag Game
|
Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.
Alice starts at vertex 1 and Bob starts at vertex x (x ≠ 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.
The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.
You should write a program which will determine how many moves will the game last.
|
The first line contains two integer numbers n and x (2 ≤ n ≤ 2·105, 2 ≤ x ≤ n).
Each of the next n - 1 lines contains two integer numbers a and b (1 ≤ a, b ≤ n) — edges of the tree. It is guaranteed that the edges form a valid tree.
|
Print the total number of moves Alice and Bob will make.
| null |
In the first example the tree looks like this:
The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:
B: stay at vertex 3
A: go to vertex 2
B: stay at vertex 3
A: go to vertex 3
In the second example the tree looks like this:
The moves in the optimal strategy are:
B: go to vertex 3
A: go to vertex 2
B: go to vertex 4
A: go to vertex 3
B: stay at vertex 4
A: go to vertex 4
|
[{"input": "4 3\n1 2\n2 3\n2 4", "output": "4"}, {"input": "5 2\n1 2\n2 3\n3 4\n2 5", "output": "6"}]
| 1,700
|
["dfs and similar", "graphs"]
| 25
|
[{"input": "4 3\r\n1 2\r\n2 3\r\n2 4\r\n", "output": "4\r\n"}, {"input": "5 2\r\n1 2\r\n2 3\r\n3 4\r\n2 5\r\n", "output": "6\r\n"}, {"input": "2 2\r\n2 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n2 1\r\n3 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2\r\n3 2\r\n", "output": "4\r\n"}, {"input": "10 4\r\n5 4\r\n8 3\r\n4 6\r\n5 3\r\n7 9\r\n1 3\r\n5 10\r\n2 9\r\n9 8\r\n", "output": "8\r\n"}, {"input": "10 7\r\n8 7\r\n2 8\r\n2 3\r\n10 6\r\n4 6\r\n4 1\r\n10 5\r\n7 5\r\n9 8\r\n", "output": "16\r\n"}, {"input": "8 3\r\n2 1\r\n3 1\r\n4 3\r\n5 1\r\n6 1\r\n7 1\r\n8 6\r\n", "output": "4\r\n"}, {"input": "34 33\r\n2 1\r\n3 2\r\n4 3\r\n5 2\r\n6 3\r\n7 2\r\n8 5\r\n9 7\r\n10 8\r\n11 7\r\n12 7\r\n13 8\r\n14 2\r\n15 10\r\n16 1\r\n17 9\r\n18 14\r\n19 1\r\n20 2\r\n21 8\r\n22 21\r\n23 9\r\n24 6\r\n25 2\r\n26 20\r\n27 5\r\n28 20\r\n29 2\r\n30 10\r\n31 14\r\n32 12\r\n33 15\r\n34 8\r\n", "output": "12\r\n"}]
| false
|
stdio
| null | true
|
28/A
|
28
|
A
|
PyPy 3-64
|
TESTS
| 9
| 124
| 1,638,400
|
216103828
|
import sys
input = sys.stdin.readline
def f(w, i):
d = []
t = n
while t:
t -= 2
d.append((ff(w[(i-1)%n], w[i]) + ff(w[i], w[(i+1)%n]), i))
i += 2
d.sort(reverse=1)
i, ew, h = 0, 1, []
while d:
a, b = d.pop()
while i < m and q[i][0] > a:
i += 1
if i == m or q[i][0] < a:
ew = 0
break
else:
h.append((b, q[i][1]))
i += 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')
| 51
| 92
| 204,800
|
212540864
|
def main():
n, m = map(int, input().split())
nails = []
for _ in range(n):
nails.append(tuple(map(int, input().split())))
rods = list(map(int, input().split()))
rods_count1, rods_count2 = {}, {}
for i, rod in enumerate(rods):
if rod not in rods_count1:
rods_count1[rod] = [i]
else:
rods_count1[rod].append(i)
if rod not in rods_count2:
rods_count2[rod] = [i]
else:
rods_count2[rod].append(i)
ans = []
for i in range(0, n, 2):
if i < n - 2:
rod_len = abs(nails[i][0] - nails[i + 2][0]) + abs(nails[i][1] - nails[i + 2][1])
else:
rod_len = abs(nails[i][0] - nails[0][0]) + abs(nails[i][1] - nails[0][1])
if rod_len not in rods_count1:
break
else:
ans.append(-1)
ans.append(rods_count1[rod_len].pop() + 1)
if not rods_count1[rod_len]:
del rods_count1[rod_len]
else:
print("YES")
print(' '.join(map(str, ans)))
return
last_nail = nails.pop()
nails.insert(0, last_nail)
ans = []
for i in range(0, n, 2):
if i < n - 2:
rod_len = abs(nails[i][0] - nails[i + 2][0]) + abs(nails[i][1] - nails[i + 2][1])
else:
rod_len = abs(nails[i][0] - nails[0][0]) + abs(nails[i][1] - nails[0][1])
if rod_len not in rods_count2:
break
else:
ans.append(rods_count2[rod_len].pop() + 1)
ans.append(-1)
if not rods_count2[rod_len]:
del rods_count2[rod_len]
else:
print("YES")
print(' '.join(map(str, ans)))
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
|
809/C
|
809
|
C
|
Python 3
|
TESTS
| 5
| 108
| 0
|
42261935
|
mod = 1000000007
def sum(x, y, k, add) :
if k < add : return 0
up = x + add
if up > k : up = k
add = add + 1
return y * ( ( (add + up) * (up - add + 1) // 2 ) % mod ) % mod
def solve(x, y, k, add = 0) :
if x == 0 or y == 0 : return 0
if x > y :
x, y = y, x
pw = 1
while (pw << 1) <= y :
pw <<= 1
if pw <= x :
return ( sum(pw, pw, k, add)\
+ sum(pw, x + y - pw - pw, k, add + pw)\
+ solve(x - pw, y - pw, k, add) ) % mod
else :
return ( sum(pw, x, k, add)\
+ solve(x, y - pw, k, add + pw) ) % mod
q = int(input())
for i in range(0, q) :
x1, y1, x2, y2, k = list(map(int, input().split()))
print(solve(x2, y2, k)\
- solve(x1 - 1, y2, k)\
- solve(x2, y1 - 1, k)\
+ solve(x1 - 1, y1 - 1, k))
| 65
| 1,044
| 8,908,800
|
42277571
|
mod = 1000000007
def sum(x,y,k,add) :
if k<add:return 0
up=x+add
if up>k:up=k
add=add+1
return y*(((add+up)*(up-add+1)//2)%mod)%mod
def solve(x,y,k,add=0) :
if x==0 or y==0:return 0
if x>y:x,y=y,x
pw = 1
while (pw<<1)<=y:pw<<=1
if pw<=x:return (sum(pw,pw,k,add)+sum(pw,x+y-pw-pw,k,add+pw)+solve(x-pw,y-pw,k,add))%mod
else:return (sum(pw,x,k,add)+solve(x,y-pw,k,add+pw))%mod
q=int(input())
for i in range(0,q):
x1,y1,x2,y2,k=list(map(int,input().split()))
ans=(solve(x2, y2, k)-solve(x1 - 1, y2, k)-solve(x2, y1 - 1, k)+solve(x1-1,y1-1,k))%mod
if ans<0:ans+=mod
print(ans)
|
Codeforces Round 415 (Div. 1)
|
CF
| 2,017
| 4
| 256
|
Find a car
|
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 × 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 ≤ i < x, 1 ≤ j < y.
The upper left fragment 5 × 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
|
The first line contains one integer q (1 ≤ q ≤ 104) — the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 ≤ x1 ≤ x2 ≤ 109, 1 ≤ y1 ≤ y2 ≤ 109, 1 ≤ k ≤ 2·109) — parameters of Leha's requests.
|
Print exactly q lines — in the first line print the answer to the first request, in the second — the answer to the second request and so on.
| null |
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
In the third request (k = 10000) Leha asks about the upper left frament 5 × 5 of the parking. Since k is big enough, the answer is equal to 93.
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
|
[{"input": "4\n1 1 1 1 1\n3 2 5 4 5\n1 1 5 5 10000\n1 4 2 5 2", "output": "1\n13\n93\n0"}]
| 2,600
|
["combinatorics", "divide and conquer", "dp"]
| 65
|
[{"input": "4\r\n1 1 1 1 1\r\n3 2 5 4 5\r\n1 1 5 5 10000\r\n1 4 2 5 2\r\n", "output": "1\r\n13\r\n93\r\n0\r\n"}, {"input": "10\r\n3 7 4 10 7\r\n6 1 7 10 18\r\n9 6 10 8 3\r\n1 8 3 10 3\r\n10 4 10 5 19\r\n8 9 9 10 10\r\n10 1 10 5 4\r\n8 1 9 4 18\r\n6 3 9 5 1\r\n6 6 9 6 16\r\n", "output": "22\r\n130\r\n0\r\n0\r\n25\r\n3\r\n0\r\n68\r\n0\r\n22\r\n"}, {"input": "10\r\n1 1 2 2 8\r\n3 4 5 9 4\r\n2 10 5 10 6\r\n8 5 10 8 8\r\n1 2 8 2 20\r\n8 6 10 8 20\r\n6 7 6 7 9\r\n8 5 10 10 13\r\n1 8 10 9 13\r\n9 8 10 9 3\r\n", "output": "6\r\n13\r\n0\r\n10\r\n36\r\n95\r\n4\r\n42\r\n94\r\n3\r\n"}, {"input": "10\r\n4 4 9 8 14\r\n5 5 10 10 7\r\n1 1 10 5 14\r\n3 5 8 9 15\r\n3 2 8 7 17\r\n5 1 10 6 7\r\n6 6 10 10 1\r\n3 3 7 10 15\r\n6 6 10 10 17\r\n6 5 10 9 5\r\n", "output": "132\r\n46\r\n291\r\n157\r\n162\r\n92\r\n5\r\n244\r\n205\r\n33\r\n"}, {"input": "10\r\n6 2 10 9 7\r\n4 3 8 7 9\r\n2 1 7 9 5\r\n2 6 10 10 3\r\n1 4 7 8 18\r\n1 2 7 6 14\r\n2 6 6 10 14\r\n3 1 10 9 10\r\n4 6 10 10 14\r\n1 6 9 10 20\r\n", "output": "74\r\n106\r\n90\r\n24\r\n165\r\n155\r\n193\r\n257\r\n158\r\n356\r\n"}, {"input": "10\r\n35670 87689 78020 97199 170735\r\n49603 42971 77473 79458 124936\r\n94018 22571 99563 79717 79594\r\n65172 72864 69350 77801 174349\r\n45117 31256 60374 67497 156317\r\n36047 95407 60232 98208 139099\r\n32487 46904 57699 99668 80778\r\n21651 59154 75570 62785 115538\r\n29698 87365 74417 93703 117692\r\n14164 51564 33862 97087 68406\r\n", "output": "454444876\r\n271069018\r\n549471212\r\n320529941\r\n94517473\r\n311684494\r\n819172459\r\n675269446\r\n7036993\r\n762542106\r\n"}, {"input": "10\r\n51798 36533 70866 80025 119989\r\n28380 14954 62643 52624 29118\r\n54458 49611 75784 95421 49917\r\n69985 20586 84374 81162 14398\r\n65761 87545 72679 89308 70174\r\n22064 89628 77685 93857 38969\r\n75905 57174 86394 88214 107079\r\n48955 26587 98075 76935 72945\r\n69991 81288 96051 90174 75880\r\n66736 31080 84603 89293 196873\r\n", "output": "12182239\r\n653954597\r\n844386299\r\n206168423\r\n437228219\r\n154397952\r\n317840300\r\n905267860\r\n968243748\r\n750471863\r\n"}, {"input": "10\r\n45965 63556 68448 95894 98898\r\n50414 55822 93611 81912 81281\r\n51874 82624 99557 93570 17105\r\n83870 83481 98209 86976 37205\r\n34423 98865 81812 99559 52923\r\n59982 80565 63020 90493 156405\r\n73425 8598 94843 23120 95359\r\n75710 49176 96524 75354 10095\r\n11342 31715 50626 83343 14952\r\n50673 61478 61380 81973 35755\r\n", "output": "199194379\r\n133563355\r\n535853348\r\n105738618\r\n790969580\r\n176118196\r\n203632117\r\n366899916\r\n146517938\r\n749331834\r\n"}, {"input": "10\r\n39453 1588 68666 44518 80856\r\n65967 37333 79860 79474 185463\r\n72918 67988 88918 85752 178916\r\n4960 53963 30061 77750 101446\r\n68699 86791 98399 87875 166780\r\n42051 5526 86018 54457 56275\r\n35111 22360 46210 77033 154364\r\n79350 54886 79640 66722 206\r\n57162 67626 99566 96156 173141\r\n42028 40518 52695 94347 188413\r\n", "output": "974201015\r\n675658286\r\n140222566\r\n668884231\r\n613269116\r\n620825458\r\n239625852\r\n0\r\n193348271\r\n860068784\r\n"}, {"input": "10\r\n60149 83439 91672 93997 29005\r\n2170 81207 33662 85253 169296\r\n84242 35792 96226 46307 32764\r\n48745 41099 63904 50301 99488\r\n20797 58596 98423 69870 151507\r\n79648 84250 95429 93302 160725\r\n29270 74595 41752 87094 46279\r\n97721 20075 99994 24743 121486\r\n44598 9233 59399 56549 114860\r\n81435 24939 83492 87248 55048\r\n", "output": "922941587\r\n694484017\r\n0\r\n117048300\r\n483223856\r\n262420342\r\n0\r\n449352476\r\n757860438\r\n37418807\r\n"}, {"input": "10\r\n17273 60120 44211 66117 121362\r\n38045 49581 43392 60379 106182\r\n29993 28891 49459 68331 170383\r\n13745 94716 99131 96384 163728\r\n35994 29973 69541 91771 65364\r\n93514 84168 95810 91743 60595\r\n57881 7334 95096 48342 39876\r\n41495 70230 56091 84188 78893\r\n12540 23228 26212 81656 105752\r\n83061 65904 87563 68222 150811\r\n", "output": "908485580\r\n424476218\r\n6537747\r\n993909605\r\n825278510\r\n232753578\r\n980640613\r\n0\r\n732434354\r\n794713552\r\n"}, {"input": "10\r\n89912 38588 100000 61519 131263\r\n63723 14623 74226 61508 104495\r\n80783 19628 93957 60942 72631\r\n49607 2064 60475 32125 43001\r\n397 31798 60225 47064 161900\r\n87074 8737 99607 47892 162291\r\n10290 73252 84596 86607 106118\r\n38621 44306 76871 87471 44012\r\n26666 84711 53248 98378 27672\r\n22685 36055 57791 80992 140124\r\n", "output": "191639278\r\n266398397\r\n387687950\r\n268970017\r\n733430769\r\n239026110\r\n569640661\r\n502549869\r\n0\r\n901026605\r\n"}, {"input": "10\r\n25583 8810 71473 84303 56325\r\n52527 14549 67038 87309 41381\r\n85964 55620 99929 76963 34442\r\n28280 87558 56450 98865 107242\r\n61281 44852 99966 67445 108461\r\n58298 39201 70236 74834 62161\r\n54864 73811 68399 96057 132419\r\n11978 85542 35272 97885 1419\r\n89151 60500 99966 89149 185860\r\n48390 40961 87183 97309 35887\r\n", "output": "605688865\r\n873699306\r\n156635112\r\n698424830\r\n86490140\r\n906905842\r\n454122876\r\n0\r\n347292150\r\n987085065\r\n"}, {"input": "10\r\n1 1 100000 100000 124458\r\n1 1 100000 100000 89626\r\n1 1 100000 100000 42210\r\n1 1 100000 100000 65721\r\n1 1 100000 100000 148198\r\n1 1 100000 100000 122029\r\n1 1 100000 100000 50224\r\n1 1 100000 100000 16314\r\n1 1 100000 100000 158599\r\n1 1 100000 100000 142792\r\n", "output": "986777122\r\n640050028\r\n864029027\r\n339397763\r\n973589169\r\n723174232\r\n902088077\r\n287074869\r\n973589169\r\n973589169\r\n"}, {"input": "10\r\n1 1 100000 100000 73712\r\n1 1 100000 100000 193808\r\n1 1 100000 100000 69429\r\n1 1 100000 100000 162666\r\n1 1 100000 100000 94759\r\n1 1 100000 100000 21899\r\n1 1 100000 100000 76524\r\n1 1 100000 100000 182233\r\n1 1 100000 100000 125300\r\n1 1 100000 100000 71258\r\n", "output": "717056579\r\n973589169\r\n625066178\r\n973589169\r\n207662527\r\n561940319\r\n600480675\r\n973589169\r\n665222578\r\n844687430\r\n"}, {"input": "10\r\n63468235 40219768 326916221 835104676 1952530008\r\n297013188 212688608 432392437 887776079 1462376999\r\n153855395 41506149 261808021 778766232 291194343\r\n238640217 22153210 642972954 719331789 371665652\r\n528859722 494055455 831993741 924681396 251221747\r\n19429387 475067059 567446881 908192965 1886584907\r\n472431037 68414189 620339945 605371645 1906964799\r\n741781008 683180935 932571485 883233060 987079989\r\n557448838 174849798 875225676 549316493 360200169\r\n61358988 97847347 487462496 955727516 1024792731\r\n", "output": "383784865\r\n892686589\r\n440520525\r\n909297528\r\n857306896\r\n138121854\r\n327512104\r\n256512043\r\n89816936\r\n158271270\r\n"}, {"input": "10\r\n1 1 1000000000 1000000000 497721466\r\n1 1 1000000000 1000000000 1096400602\r\n1 1 1000000000 1000000000 1120358961\r\n1 1 1000000000 1000000000 232914786\r\n1 1 1000000000 1000000000 601018859\r\n1 1 1000000000 1000000000 310363393\r\n1 1 1000000000 1000000000 636663039\r\n1 1 1000000000 1000000000 1548359129\r\n1 1 1000000000 1000000000 1183677871\r\n1 1 1000000000 1000000000 792703683\r\n", "output": "11780124\r\n248752269\r\n248752269\r\n883198940\r\n218155629\r\n747605194\r\n352461300\r\n248752269\r\n248752269\r\n562283839\r\n"}, {"input": "10\r\n1 1 1000000000 1000000000 1477070720\r\n1 1 1000000000 1000000000 1378704784\r\n1 1 1000000000 1000000000 782520772\r\n1 1 1000000000 1000000000 1377211731\r\n1 1 1000000000 1000000000 623332716\r\n1 1 1000000000 1000000000 497630560\r\n1 1 1000000000 1000000000 47465148\r\n1 1 1000000000 1000000000 790892344\r\n1 1 1000000000 1000000000 1071836060\r\n1 1 1000000000 1000000000 1949232149\r\n", "output": "248752269\r\n248752269\r\n949069688\r\n248752269\r\n840885502\r\n42891263\r\n23378226\r\n985784682\r\n561979540\r\n248752269\r\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
PyPy 3-64
|
TESTS
| 3
| 108
| 24,268,800
|
231364820
|
from collections import defaultdict
from sys import stdin
def solve():
n = int(input())
a = [int(x) for x in input().split()]
answ = 0
c = 0
d = defaultdict(int)
for i in range(n):
if d[a[i]] == 0:
d[a[i]] = 1
c+=1
answ = max(answ,c)
else:
d[a[i]] = 0
c-=1
print(answ)
if __name__ == '__main__':
solve()
| 56
| 124
| 26,214,400
|
198540772
|
n = int(input())
lst = list(map(int, input().split()))
tbl, res = set(), 0
for x in lst:
if x in tbl:
tbl.remove(x)
else:
tbl.add(x)
res = max(res, len(tbl))
print(res)
|
Технокубок 2017 - Финал (только для онсайт-финалистов)
|
CF
| 2,017
| 2
| 256
|
Andryusha and Socks
|
Andryusha is an orderly boy and likes to keep things in their place.
Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.
Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?
|
The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs.
The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi.
It is guaranteed that Andryusha took exactly two socks of each pair.
|
Print single integer — the maximum number of socks that were on the table at the same time.
| null |
In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.
In the second example Andryusha behaved as follows:
- Initially the table was empty, he took out a sock from pair 2 and put it on the table.
- Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.
- Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.
- Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.
- Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.
- Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.
|
[{"input": "1\n1 1", "output": "1"}, {"input": "3\n2 1 1 3 2 3", "output": "2"}]
| 800
|
["implementation"]
| 56
|
[{"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "3\r\n2 1 1 3 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n5 1 3 2 4 3 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n4 2 6 3 4 8 7 1 1 5 2 10 6 8 3 5 10 9 9 7\r\n", "output": "6\r\n"}, {"input": "50\r\n30 47 31 38 37 50 36 43 9 23 2 2 15 31 14 49 9 16 6 44 27 14 5 6 3 47 25 26 1 35 3 15 24 19 8 46 49 41 4 26 40 28 42 11 34 35 46 18 7 28 18 40 19 42 4 41 38 48 50 12 29 39 33 17 25 22 22 21 36 45 27 30 20 7 13 29 39 44 21 8 37 45 34 1 20 10 11 17 33 12 43 13 10 16 48 24 32 5 23 32\r\n", "output": "25\r\n"}, {"input": "50\r\n1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50\r\n", "output": "1\r\n"}, {"input": "50\r\n50 50 49 49 48 48 47 47 46 46 45 45 44 44 43 43 42 42 41 41 40 40 39 39 38 38 37 37 36 36 35 35 34 34 33 33 32 32 31 31 30 30 29 29 28 28 27 27 26 26 25 25 24 24 23 23 22 22 21 21 20 20 19 19 18 18 17 17 16 16 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\r\n", "output": "1\r\n"}, {"input": "50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "50\r\n"}, {"input": "50\r\n50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "50\r\n"}, {"input": "10\r\n2 9 4 1 6 7 10 3 1 5 8 6 2 3 10 7 4 8 5 9\r\n", "output": "9\r\n"}]
| false
|
stdio
| null | true
|
33/A
|
33
|
A
|
Python 3
|
TESTS
| 11
| 216
| 307,200
|
91313760
|
import sys
n, m, k = map(int, input().split(' '))
ar = [0] * (n + 4);
tnt = [1000005] * (n + 1)
jael = 0;
for i in range(n):
x, y = map(int, input().split(' '))
ar[x], tnt[x] = x, min(tnt[x], y)
for i in range(n):
if(tnt[i] < 1000005):
jael += tnt[i]
print(min(jael, k))
| 31
| 92
| 0
|
177072860
|
n,m,k = map(int,input().split())
diction = {}
for i in range(n):
nums = list(map(int,input().split()))
if nums[0] not in diction.keys():
diction[nums[0]] = nums[1]
elif nums[0] in diction.keys():
if nums[1] > diction[nums[0]]:
pass
else:
diction[nums[0]] = nums[1]
s = sum(diction.values())
if s >= k:
print(k)
else:
print(s)
|
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
|
25/D
|
25
|
D
|
Python 3
|
TESTS
| 8
| 62
| 102,400
|
214723976
|
numCity = int(input())
parent = [0 for _ in range(numCity + 1)]
size = [0 for _ in range(numCity + 1)]
close = []
built = []
def swap(a, b):
temp = a
a = b
b = temp
def make_set(v):
parent[v] = v
size[v] = 1
def find_set(v):
if v == parent[v]:
return v
parent[v] = find_set(parent[v])
return parent[v]
def union_set(a, b):
a = find_set(a)
b = find_set(b)
if a != b:
if size[a] < size[b]:
swap(a, b)
parent[b] = a
size[a] += size[b]
for city in range(1, numCity + 1):
make_set(city)
for road in range(1, numCity):
a, b = map(int, input().split(" "))
# checking if they are already connected
if find_set(a) == find_set(b):
close.append((a, b))
else:
union_set(a, b)
for i in range(2, numCity + 1):
if find_set(i) != find_set(1):
# union_set()
built.append((1, i))
print(len(close))
for i in range(len(close)):
print(close[i][0], close[i][1], built[i][0], built[i][1])
| 55
| 92
| 307,200
|
205815033
|
class DSU:
def __init__(self,n):
self.parent=[]
self.size=[]
for i in range(n+1):
self.parent.append(i)
self.size.append(1)
self.parent[0]=None
self.size[0]=None
def _findParent(self,node):
if self.parent[node]==node: return node
self.parent[node]=self._findParent(self.parent[node])
return self.parent[node]
def unionSize(self,u,v):
up=self._findParent(u)
vp=self._findParent(v)
if up==vp: return
if self.size[u]<self.size[v]:
self.parent[up]=vp
self.size[vp]+=self.size[up]
else:
self.parent[vp]=up
self.size[up]+=self.size[vp]
n=int(input())
roads=[]
for i in range(n-1):
temp=list(map(int,input().split()))
roads.append(temp)
def solve(n,roads):
dsu=DSU(n)
extra_roads=[]
for u,v in roads:
if dsu._findParent(u)==dsu._findParent(v):
extra_roads.append((u,v))
else:
dsu.unionSize(u,v)
altimate_parents=set()
for i in range(1,n+1):
altimate_parents.add(dsu._findParent(i))
l=len(extra_roads)
print(l)
# print(extra_roads,altimate_parents)
a=altimate_parents.pop()
for i in range(l):
b=altimate_parents.pop()
print(f"{extra_roads[i][0]} {extra_roads[i][1]} {a} {b}")
a=b
solve(n,roads)
|
Codeforces Beta Round 25 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Roads not only in Berland
|
Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.
|
The first line contains integer n (2 ≤ n ≤ 1000) — amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself.
|
Output the answer, number t — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.
| null | null |
[{"input": "2\n1 2", "output": "0"}, {"input": "7\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7", "output": "1\n3 1 3 7"}]
| 1,900
|
["dsu", "graphs", "trees"]
| 55
|
[{"input": "2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 7\r\n", "output": "1\r\n3 1 3 7\r\n"}, {"input": "3\r\n3 2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "3\r\n3 1\r\n3 2\r\n", "output": "0\r\n"}, {"input": "4\r\n1 4\r\n3 1\r\n3 4\r\n", "output": "1\r\n3 4 2 4\r\n"}, {"input": "5\r\n4 1\r\n4 3\r\n5 3\r\n2 4\r\n", "output": "0\r\n"}, {"input": "6\r\n5 2\r\n5 3\r\n1 4\r\n3 1\r\n5 6\r\n", "output": "0\r\n"}, {"input": "10\r\n5 9\r\n8 5\r\n7 6\r\n7 9\r\n3 9\r\n2 1\r\n7 2\r\n3 6\r\n7 1\r\n", "output": "2\r\n3 6 1 4\r\n7 1 4 10\r\n"}, {"input": "21\r\n7 15\r\n13 1\r\n14 3\r\n4 10\r\n2 3\r\n16 18\r\n17 20\r\n16 20\r\n8 4\r\n3 12\r\n2 17\r\n13 11\r\n16 1\r\n13 2\r\n13 5\r\n8 9\r\n6 14\r\n3 17\r\n16 9\r\n13 8\r\n", "output": "3\r\n13 2 9 15\r\n3 17 15 19\r\n13 8 19 21\r\n"}, {"input": "39\r\n6 13\r\n15 39\r\n10 35\r\n31 28\r\n4 21\r\n12 39\r\n3 7\r\n3 13\r\n6 1\r\n5 14\r\n36 28\r\n12 15\r\n18 38\r\n30 29\r\n19 34\r\n36 16\r\n20 22\r\n8 13\r\n38 32\r\n26 39\r\n21 37\r\n1 7\r\n15 27\r\n12 26\r\n8 3\r\n6 14\r\n29 2\r\n25 23\r\n32 21\r\n5 16\r\n32 25\r\n6 8\r\n13 10\r\n23 30\r\n34 37\r\n29 33\r\n28 14\r\n36 5\r\n", "output": "7\r\n12 15 9 11\r\n1 7 11 17\r\n12 26 17 22\r\n8 3 22 24\r\n6 8 24 27\r\n28 14 27 33\r\n36 5 33 35\r\n"}, {"input": "60\r\n17 34\r\n46 22\r\n50 44\r\n46 33\r\n41 21\r\n31 33\r\n48 6\r\n38 19\r\n35 60\r\n2 24\r\n49 29\r\n7 53\r\n34 1\r\n19 55\r\n32 1\r\n31 42\r\n27 28\r\n4 53\r\n6 50\r\n21 34\r\n1 10\r\n12 36\r\n54 8\r\n16 13\r\n51 43\r\n45 51\r\n54 20\r\n13 53\r\n34 33\r\n49 33\r\n51 11\r\n59 34\r\n15 5\r\n59 28\r\n30 39\r\n13 30\r\n58 4\r\n34 14\r\n3 9\r\n19 34\r\n4 18\r\n26 48\r\n56 54\r\n3 43\r\n57 25\r\n3 41\r\n35 3\r\n48 44\r\n19 13\r\n54 1\r\n23 31\r\n59 47\r\n5 1\r\n46 40\r\n6 26\r\n20 25\r\n37 5\r\n17 24\r\n20 52\r\n", "output": "2\r\n48 44 36 44\r\n6 26 44 52\r\n"}]
| false
|
stdio
|
import sys
def read_input(input_path):
with open(input_path) as f:
n = int(f.readline())
edges = set()
for _ in range(n-1):
a, b = map(int, f.readline().split())
if a > b:
a, b = b, a
edges.add((a, b))
return n, edges
def check_initial_components(n, edges):
adj = {i: [] for i in range(1, n + 1)}
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
visited = set()
components = 0
for node in adj:
if node not in visited:
components += 1
q = [node]
visited.add(node)
while q:
current = q.pop(0)
for neighbor in adj[current]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
return components
def is_connected(n, edges):
if not edges:
return n == 0 or n == 1
adj = {i: [] for i in range(1, n + 1)}
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
visited = set()
start_node = next(iter(adj))
q = [start_node]
visited.add(start_node)
while q:
current = q.pop(0)
for neighbor in adj[current]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
return len(visited) == n
def main(input_path, output_path, sub_output_path):
n, initial_edges = read_input(input_path)
try:
with open(sub_output_path) as f:
lines = [line.strip() for line in f.readlines()]
if not lines:
print(0)
return
try:
t = int(lines[0])
except:
print(0)
return
if len(lines) < t + 1:
print(0)
return
ops = []
for line in lines[1:t + 1]:
parts = line.split()
if len(parts) != 4:
print(0)
return
try:
i, j, u, v = map(int, parts)
except:
print(0)
return
if i == j or u == v:
print(0)
return
ops.append((i, j, u, v))
except:
print(0)
return
initial_components = check_initial_components(n, initial_edges)
expected_t = initial_components - 1
if t != expected_t:
print(0)
return
current_edges = set()
for a, b in initial_edges:
current_edges.add((min(a, b), max(a, b)))
for op in ops:
i, j, u, v = op
remove_edge = (min(i, j), max(i, j))
if remove_edge not in current_edges:
print(0)
return
current_edges.remove(remove_edge)
new_edge = (min(u, v), max(u, v))
if new_edge in current_edges:
print(0)
return
current_edges.add(new_edge)
if len(current_edges) != n - 1:
print(0)
return
if not is_connected(n, current_edges):
print(0)
return
print(1)
if __name__ == '__main__':
input_path = sys.argv[1]
output_path = sys.argv[2]
sub_output_path = sys.argv[3]
main(input_path, output_path, sub_output_path)
| true
|
25/D
|
25
|
D
|
PyPy 3-64
|
TESTS
| 8
| 92
| 512,000
|
158788882
|
def dfs(vertex):
marked[vertex] = True
for vertex2 in graph[vertex]:
if not marked[vertex2]:
dfs(vertex2)
def dfs2(vertex, last):
marked[vertex] = True
for vertex2 in graph[vertex]:
if vertex2 != vertex:
if not marked[vertex2]:
dfs2(vertex2, vertex)
else:
reserv.append((vertex+1, vertex2+1))
n = int(input())
graph = [set() for i in range(n)]
for i in range(n-1):
a, b = map(int, input().split())
graph[a-1].add(b-1)
graph[b-1].add(a-1)
marked = [False] * n
need = []
reserv = []
ans = -1
for i in range(n):
if not marked[i]:
ans += 1
need.append((1, i+1))
dfs(i)
marked = [False] * n
for j in range(n):
if not marked[j]:
dfs2(j, -1)
print(ans)
for i in range(ans):
print(*reserv[i], *need[i+1])
| 55
| 92
| 512,000
|
209987496
|
def find(x, parent):
if x != parent[x]:
parent[x] = find(parent[x], parent)
return parent[x]
class Road:
def __init__(self, city1, city2):
self.city1 = city1
self.city2 = city2
numCities = int(input())
parent = list(range(numCities + 1))
duplicateRoads = 0
closedRoads = []
for _ in range(numCities - 1):
city1, city2 = map(int, input().split())
root1 = find(city1, parent)
root2 = find(city2, parent)
if root1 != root2:
parent[root1] = root2
else:
road = Road(city1, city2)
closedRoads.append(road)
duplicateRoads += 1
visited = [0] * (numCities + 1)
componentCount = 0
componentRoots = []
for i in range(1, numCities + 1):
if not visited[find(i, parent)]:
componentRoots.append(find(i, parent))
componentCount += 1
visited[find(i, parent)] = 1
print(componentCount - 1)
componentIndex = 1
for i in range(componentCount - 1):
print(closedRoads[i].city1, closedRoads[i].city2, componentRoots[0], componentRoots[componentIndex])
componentIndex += 1
|
Codeforces Beta Round 25 (Div. 2 Only)
|
ICPC
| 2,010
| 2
| 256
|
Roads not only in Berland
|
Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.
|
The first line contains integer n (2 ≤ n ≤ 1000) — amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself.
|
Output the answer, number t — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.
| null | null |
[{"input": "2\n1 2", "output": "0"}, {"input": "7\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7", "output": "1\n3 1 3 7"}]
| 1,900
|
["dsu", "graphs", "trees"]
| 55
|
[{"input": "2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "7\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 7\r\n", "output": "1\r\n3 1 3 7\r\n"}, {"input": "3\r\n3 2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "3\r\n3 1\r\n3 2\r\n", "output": "0\r\n"}, {"input": "4\r\n1 4\r\n3 1\r\n3 4\r\n", "output": "1\r\n3 4 2 4\r\n"}, {"input": "5\r\n4 1\r\n4 3\r\n5 3\r\n2 4\r\n", "output": "0\r\n"}, {"input": "6\r\n5 2\r\n5 3\r\n1 4\r\n3 1\r\n5 6\r\n", "output": "0\r\n"}, {"input": "10\r\n5 9\r\n8 5\r\n7 6\r\n7 9\r\n3 9\r\n2 1\r\n7 2\r\n3 6\r\n7 1\r\n", "output": "2\r\n3 6 1 4\r\n7 1 4 10\r\n"}, {"input": "21\r\n7 15\r\n13 1\r\n14 3\r\n4 10\r\n2 3\r\n16 18\r\n17 20\r\n16 20\r\n8 4\r\n3 12\r\n2 17\r\n13 11\r\n16 1\r\n13 2\r\n13 5\r\n8 9\r\n6 14\r\n3 17\r\n16 9\r\n13 8\r\n", "output": "3\r\n13 2 9 15\r\n3 17 15 19\r\n13 8 19 21\r\n"}, {"input": "39\r\n6 13\r\n15 39\r\n10 35\r\n31 28\r\n4 21\r\n12 39\r\n3 7\r\n3 13\r\n6 1\r\n5 14\r\n36 28\r\n12 15\r\n18 38\r\n30 29\r\n19 34\r\n36 16\r\n20 22\r\n8 13\r\n38 32\r\n26 39\r\n21 37\r\n1 7\r\n15 27\r\n12 26\r\n8 3\r\n6 14\r\n29 2\r\n25 23\r\n32 21\r\n5 16\r\n32 25\r\n6 8\r\n13 10\r\n23 30\r\n34 37\r\n29 33\r\n28 14\r\n36 5\r\n", "output": "7\r\n12 15 9 11\r\n1 7 11 17\r\n12 26 17 22\r\n8 3 22 24\r\n6 8 24 27\r\n28 14 27 33\r\n36 5 33 35\r\n"}, {"input": "60\r\n17 34\r\n46 22\r\n50 44\r\n46 33\r\n41 21\r\n31 33\r\n48 6\r\n38 19\r\n35 60\r\n2 24\r\n49 29\r\n7 53\r\n34 1\r\n19 55\r\n32 1\r\n31 42\r\n27 28\r\n4 53\r\n6 50\r\n21 34\r\n1 10\r\n12 36\r\n54 8\r\n16 13\r\n51 43\r\n45 51\r\n54 20\r\n13 53\r\n34 33\r\n49 33\r\n51 11\r\n59 34\r\n15 5\r\n59 28\r\n30 39\r\n13 30\r\n58 4\r\n34 14\r\n3 9\r\n19 34\r\n4 18\r\n26 48\r\n56 54\r\n3 43\r\n57 25\r\n3 41\r\n35 3\r\n48 44\r\n19 13\r\n54 1\r\n23 31\r\n59 47\r\n5 1\r\n46 40\r\n6 26\r\n20 25\r\n37 5\r\n17 24\r\n20 52\r\n", "output": "2\r\n48 44 36 44\r\n6 26 44 52\r\n"}]
| false
|
stdio
|
import sys
def read_input(input_path):
with open(input_path) as f:
n = int(f.readline())
edges = set()
for _ in range(n-1):
a, b = map(int, f.readline().split())
if a > b:
a, b = b, a
edges.add((a, b))
return n, edges
def check_initial_components(n, edges):
adj = {i: [] for i in range(1, n + 1)}
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
visited = set()
components = 0
for node in adj:
if node not in visited:
components += 1
q = [node]
visited.add(node)
while q:
current = q.pop(0)
for neighbor in adj[current]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
return components
def is_connected(n, edges):
if not edges:
return n == 0 or n == 1
adj = {i: [] for i in range(1, n + 1)}
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
visited = set()
start_node = next(iter(adj))
q = [start_node]
visited.add(start_node)
while q:
current = q.pop(0)
for neighbor in adj[current]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
return len(visited) == n
def main(input_path, output_path, sub_output_path):
n, initial_edges = read_input(input_path)
try:
with open(sub_output_path) as f:
lines = [line.strip() for line in f.readlines()]
if not lines:
print(0)
return
try:
t = int(lines[0])
except:
print(0)
return
if len(lines) < t + 1:
print(0)
return
ops = []
for line in lines[1:t + 1]:
parts = line.split()
if len(parts) != 4:
print(0)
return
try:
i, j, u, v = map(int, parts)
except:
print(0)
return
if i == j or u == v:
print(0)
return
ops.append((i, j, u, v))
except:
print(0)
return
initial_components = check_initial_components(n, initial_edges)
expected_t = initial_components - 1
if t != expected_t:
print(0)
return
current_edges = set()
for a, b in initial_edges:
current_edges.add((min(a, b), max(a, b)))
for op in ops:
i, j, u, v = op
remove_edge = (min(i, j), max(i, j))
if remove_edge not in current_edges:
print(0)
return
current_edges.remove(remove_edge)
new_edge = (min(u, v), max(u, v))
if new_edge in current_edges:
print(0)
return
current_edges.add(new_edge)
if len(current_edges) != n - 1:
print(0)
return
if not is_connected(n, current_edges):
print(0)
return
print(1)
if __name__ == '__main__':
input_path = sys.argv[1]
output_path = sys.argv[2]
sub_output_path = sys.argv[3]
main(input_path, output_path, sub_output_path)
| true
|
978/C
|
978
|
C
|
PyPy 3-64
|
TESTS
| 11
| 295
| 34,508,800
|
186860462
|
input()
a=list(map(int,input().split()))
b=list(map(int,input().split()))
sum=0
room_count=[]
for i in range(len(a)):
sum+=a[i]
room_count.append(sum)
sum=0
dormitory=1
z=0
for i in b:
while i>room_count[z]:
dormitory+=1
sum+=a[z]
z+=1
else:
#dormitory ready
if i!=b[0]:
specific_room=i-sum
else:
specific_room=i
print(dormitory,specific_room)
| 16
| 265
| 36,761,600
|
219562054
|
import sys
def find(a: list[int], q: int) -> bool:
i = find_lower(a, q)
return i >= 0 and a[i] == q
def find_lower(a: list[int], x: int) -> int:
"""max i: a[i] <= x"""
l = -1 # a[l] <= x
r = len(a) # a[r] > x
while r > l + 1:
m = (l + r) // 2
if a[m] <= x:
l = m
else:
r = m
return l
def find_upper(a: list[int], x: int) -> int:
"""min i: a[i] >= x"""
l = -1
r = len(a)
while l + 1 < r:
m = (l + r) // 2
if a[m] >= x:
r = m
else:
l = m
return r
def print_matrix(graph: dict[list[int]]) -> None:
for row in graph.values():
sys.stdout.write(" ".join(str(x) for x in row))
sys.stdout.write("\n")
def print_answer(flag: bool) -> None:
sys.stdout.write("YES\n") if flag else sys.stdout.write("NO\n")
def solve() -> bool:
input = sys.stdin.readline
n, m = map(int, input().split())
graph = {}
for i in range(1, n + 1):
graph[i] = [0] * (n + 1)
flag = True
while m > 0 and flag:
m -= 1
u, v = map(int, input().split())
if u == v or graph[u][v] > 0:
flag = False
graph[u][v] += 1
graph[v][u] += 1
return flag
MOD = 10 ** 9 + 7
def find_kill_count(n: int) -> int:
kill_count = 0
kill_count += n * (n + 1) * (n + n + 1) // 6
kill_count += n * (n - 1) * (n + 1) // 3
return (kill_count * 2022) % MOD
def find_instability(array: list[int], n: int) -> int:
array.sort()
return min(array[n - 1] - array[1], array[n - 2] - array[0])
def find_cost(array: list[int], n: int) -> (int, int):
min_cost, t_answ = float('inf'), 100
for t in range(1, 101):
cost = 0
for stick in array:
cost += min(abs(stick - t), min(abs(stick - t - 1), abs(stick - t + 1)))
if cost <= min_cost:
min_cost = cost
t_answ = t
return t_answ, min_cost
def find_max_diff(array: list[int], n: int, k: int) -> int:
e = array.count(1)
s = array.count(-1)
max_diff = 0
for i in range(n):
index = i
e_count = e
s_count = s
while index < n:
if array[index] == 1:
e_count -= 1
else:
s_count -= 1
index += k
index = i - k
while index > -1:
if array[index] == 1:
e_count -= 1
else:
s_count -= 1
index -= k
max_diff = max(max_diff, abs(e_count - s_count))
return max_diff
def find_house_and_flat(n: int, array: list[int], mails: list[int]) -> None:
sum_ = 0
idx = 0
for mail in mails:
while sum_ + array[idx] < mail:
sum_ += array[idx]
idx += 1
sys.stdout.write(f"{idx + 1} {mail - sum_}\n")
def main():
input = sys.stdin.readline
n, m = map(int, input().split())
houses = list(map(int, input().split()))
mails = list(map(int, input().split()))
find_house_and_flat(n, houses, mails)
if __name__ == "__main__":
main()
|
Codeforces Round 481 (Div. 3)
|
ICPC
| 2,018
| 4
| 256
|
Letters
|
There are $$$n$$$ dormitories in Berland State University, they are numbered with integers from $$$1$$$ to $$$n$$$. Each dormitory consists of rooms, there are $$$a_i$$$ rooms in $$$i$$$-th dormitory. The rooms in $$$i$$$-th dormitory are numbered from $$$1$$$ to $$$a_i$$$.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $$$n$$$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $$$1$$$ to $$$a_1 + a_2 + \dots + a_n$$$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case $$$n=2$$$, $$$a_1=3$$$ and $$$a_2=5$$$ an envelope can have any integer from $$$1$$$ to $$$8$$$ written on it. If the number $$$7$$$ is written on an envelope, it means that the letter should be delivered to the room number $$$4$$$ of the second dormitory.
For each of $$$m$$$ letters by the room number among all $$$n$$$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
|
The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 2 \cdot 10^{5})$$$ — the number of dormitories and the number of letters.
The second line contains a sequence $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^{10})$$$, where $$$a_i$$$ equals to the number of rooms in the $$$i$$$-th dormitory. The third line contains a sequence $$$b_1, b_2, \dots, b_m$$$ $$$(1 \le b_j \le a_1 + a_2 + \dots + a_n)$$$, where $$$b_j$$$ equals to the room number (among all rooms of all dormitories) for the $$$j$$$-th letter. All $$$b_j$$$ are given in increasing order.
|
Print $$$m$$$ lines. For each letter print two integers $$$f$$$ and $$$k$$$ — the dormitory number $$$f$$$ $$$(1 \le f \le n)$$$ and the room number $$$k$$$ in this dormitory $$$(1 \le k \le a_f)$$$ to deliver the letter.
| null |
In the first example letters should be delivered in the following order:
- the first letter in room $$$1$$$ of the first dormitory
- the second letter in room $$$9$$$ of the first dormitory
- the third letter in room $$$2$$$ of the second dormitory
- the fourth letter in room $$$13$$$ of the second dormitory
- the fifth letter in room $$$1$$$ of the third dormitory
- the sixth letter in room $$$12$$$ of the third dormitory
|
[{"input": "3 6\n10 15 12\n1 9 12 23 26 37", "output": "1 1\n1 9\n2 2\n2 13\n3 1\n3 12"}, {"input": "2 3\n5 10000000000\n5 6 9999999999", "output": "1 5\n2 1\n2 9999999994"}]
| 1,000
|
["binary search", "implementation", "two pointers"]
| 16
|
[{"input": "3 6\r\n10 15 12\r\n1 9 12 23 26 37\r\n", "output": "1 1\r\n1 9\r\n2 2\r\n2 13\r\n3 1\r\n3 12\r\n"}, {"input": "2 3\r\n5 10000000000\r\n5 6 9999999999\r\n", "output": "1 5\r\n2 1\r\n2 9999999994\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "1 1\r\n"}, {"input": "5 15\r\n10 20 30 20 10\r\n1 6 10 11 15 30 31 54 60 61 76 80 81 84 90\r\n", "output": "1 1\r\n1 6\r\n1 10\r\n2 1\r\n2 5\r\n2 20\r\n3 1\r\n3 24\r\n3 30\r\n4 1\r\n4 16\r\n4 20\r\n5 1\r\n5 4\r\n5 10\r\n"}, {"input": "1 10\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "1 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\n"}, {"input": "5 8\r\n10 1 1 1 10\r\n9 10 11 12 13 14 15 23\r\n", "output": "1 9\r\n1 10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n5 2\r\n5 10\r\n"}, {"input": "1 3\r\n10000\r\n1 4325 10000\r\n", "output": "1 1\r\n1 4325\r\n1 10000\r\n"}, {"input": "4 18\r\n5 6 3 4\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\r\n", "output": "1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 1\r\n2 2\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n3 1\r\n3 2\r\n3 3\r\n4 1\r\n4 2\r\n4 3\r\n4 4\r\n"}, {"input": "3 10\r\n1000000000 1000000000 1000000000\r\n543678543 567869543 1000000000 1000000001 1500000000 1999999999 2000000000 2000000001 2754432345 3000000000\r\n", "output": "1 543678543\r\n1 567869543\r\n1 1000000000\r\n2 1\r\n2 500000000\r\n2 999999999\r\n2 1000000000\r\n3 1\r\n3 754432345\r\n3 1000000000\r\n"}]
| false
|
stdio
| null | true
|
978/C
|
978
|
C
|
Python 3
|
TESTS
| 9
| 654
| 35,635,200
|
208962869
|
k = input()
f = list(map (int, input().split()))
f1 = list(map (int, input().split()))
p = 0
d = 0
for i in range(len(f1)):
if f1[i] - p > f[d]:
p += f[d]
d += 1
print (d + 1, f1[i] - p)
| 16
| 280
| 36,556,800
|
165688277
|
n,m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ci, cl, cs = 0, 0, a[0]
i = 0
while i < m:
if b[i] <= cs:
print(ci+1, b[i]-cl)
i += 1
else:
ci += 1
cl = cs
cs += a[ci]
|
Codeforces Round 481 (Div. 3)
|
ICPC
| 2,018
| 4
| 256
|
Letters
|
There are $$$n$$$ dormitories in Berland State University, they are numbered with integers from $$$1$$$ to $$$n$$$. Each dormitory consists of rooms, there are $$$a_i$$$ rooms in $$$i$$$-th dormitory. The rooms in $$$i$$$-th dormitory are numbered from $$$1$$$ to $$$a_i$$$.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $$$n$$$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $$$1$$$ to $$$a_1 + a_2 + \dots + a_n$$$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case $$$n=2$$$, $$$a_1=3$$$ and $$$a_2=5$$$ an envelope can have any integer from $$$1$$$ to $$$8$$$ written on it. If the number $$$7$$$ is written on an envelope, it means that the letter should be delivered to the room number $$$4$$$ of the second dormitory.
For each of $$$m$$$ letters by the room number among all $$$n$$$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
|
The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 2 \cdot 10^{5})$$$ — the number of dormitories and the number of letters.
The second line contains a sequence $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^{10})$$$, where $$$a_i$$$ equals to the number of rooms in the $$$i$$$-th dormitory. The third line contains a sequence $$$b_1, b_2, \dots, b_m$$$ $$$(1 \le b_j \le a_1 + a_2 + \dots + a_n)$$$, where $$$b_j$$$ equals to the room number (among all rooms of all dormitories) for the $$$j$$$-th letter. All $$$b_j$$$ are given in increasing order.
|
Print $$$m$$$ lines. For each letter print two integers $$$f$$$ and $$$k$$$ — the dormitory number $$$f$$$ $$$(1 \le f \le n)$$$ and the room number $$$k$$$ in this dormitory $$$(1 \le k \le a_f)$$$ to deliver the letter.
| null |
In the first example letters should be delivered in the following order:
- the first letter in room $$$1$$$ of the first dormitory
- the second letter in room $$$9$$$ of the first dormitory
- the third letter in room $$$2$$$ of the second dormitory
- the fourth letter in room $$$13$$$ of the second dormitory
- the fifth letter in room $$$1$$$ of the third dormitory
- the sixth letter in room $$$12$$$ of the third dormitory
|
[{"input": "3 6\n10 15 12\n1 9 12 23 26 37", "output": "1 1\n1 9\n2 2\n2 13\n3 1\n3 12"}, {"input": "2 3\n5 10000000000\n5 6 9999999999", "output": "1 5\n2 1\n2 9999999994"}]
| 1,000
|
["binary search", "implementation", "two pointers"]
| 16
|
[{"input": "3 6\r\n10 15 12\r\n1 9 12 23 26 37\r\n", "output": "1 1\r\n1 9\r\n2 2\r\n2 13\r\n3 1\r\n3 12\r\n"}, {"input": "2 3\r\n5 10000000000\r\n5 6 9999999999\r\n", "output": "1 5\r\n2 1\r\n2 9999999994\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "1 1\r\n"}, {"input": "5 15\r\n10 20 30 20 10\r\n1 6 10 11 15 30 31 54 60 61 76 80 81 84 90\r\n", "output": "1 1\r\n1 6\r\n1 10\r\n2 1\r\n2 5\r\n2 20\r\n3 1\r\n3 24\r\n3 30\r\n4 1\r\n4 16\r\n4 20\r\n5 1\r\n5 4\r\n5 10\r\n"}, {"input": "1 10\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "1 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\n"}, {"input": "5 8\r\n10 1 1 1 10\r\n9 10 11 12 13 14 15 23\r\n", "output": "1 9\r\n1 10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n5 2\r\n5 10\r\n"}, {"input": "1 3\r\n10000\r\n1 4325 10000\r\n", "output": "1 1\r\n1 4325\r\n1 10000\r\n"}, {"input": "4 18\r\n5 6 3 4\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\r\n", "output": "1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 1\r\n2 2\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n3 1\r\n3 2\r\n3 3\r\n4 1\r\n4 2\r\n4 3\r\n4 4\r\n"}, {"input": "3 10\r\n1000000000 1000000000 1000000000\r\n543678543 567869543 1000000000 1000000001 1500000000 1999999999 2000000000 2000000001 2754432345 3000000000\r\n", "output": "1 543678543\r\n1 567869543\r\n1 1000000000\r\n2 1\r\n2 500000000\r\n2 999999999\r\n2 1000000000\r\n3 1\r\n3 754432345\r\n3 1000000000\r\n"}]
| false
|
stdio
| null | true
|
978/C
|
978
|
C
|
Python 3
|
TESTS
| 9
| 826
| 21,299,200
|
160489921
|
n,m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(1,n): a[i]+=a[i-1]
dorm = 0
for i in range(m):
if b[i] <= a[dorm]: print(dorm+1, b[i]-a[dorm-1] if dorm>0 else b[i])
else:
b[i] -= a[dorm]
dorm += 1
print(dorm+1, b[i])
| 16
| 280
| 36,556,800
|
182296837
|
#!/usr/bin/env python3
import math
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
from bisect import bisect_left as bs
def test_case():
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pref = [0 for _ in range(n+1)]
for i in range(1, n+1):
pref[i] = pref[i-1] + a[i-1]
i, j = 0, 0
while i < n or j < m:
if i == n or (j < m and b[j] <= pref[i+1]):
print(i+1, b[j]-pref[i])
j += 1
else:
i += 1
def main():
t = 1
# t = int(input())
for _ in range(t):
test_case()
if __name__ == '__main__':
main()
|
Codeforces Round 481 (Div. 3)
|
ICPC
| 2,018
| 4
| 256
|
Letters
|
There are $$$n$$$ dormitories in Berland State University, they are numbered with integers from $$$1$$$ to $$$n$$$. Each dormitory consists of rooms, there are $$$a_i$$$ rooms in $$$i$$$-th dormitory. The rooms in $$$i$$$-th dormitory are numbered from $$$1$$$ to $$$a_i$$$.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $$$n$$$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $$$1$$$ to $$$a_1 + a_2 + \dots + a_n$$$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case $$$n=2$$$, $$$a_1=3$$$ and $$$a_2=5$$$ an envelope can have any integer from $$$1$$$ to $$$8$$$ written on it. If the number $$$7$$$ is written on an envelope, it means that the letter should be delivered to the room number $$$4$$$ of the second dormitory.
For each of $$$m$$$ letters by the room number among all $$$n$$$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
|
The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 2 \cdot 10^{5})$$$ — the number of dormitories and the number of letters.
The second line contains a sequence $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^{10})$$$, where $$$a_i$$$ equals to the number of rooms in the $$$i$$$-th dormitory. The third line contains a sequence $$$b_1, b_2, \dots, b_m$$$ $$$(1 \le b_j \le a_1 + a_2 + \dots + a_n)$$$, where $$$b_j$$$ equals to the room number (among all rooms of all dormitories) for the $$$j$$$-th letter. All $$$b_j$$$ are given in increasing order.
|
Print $$$m$$$ lines. For each letter print two integers $$$f$$$ and $$$k$$$ — the dormitory number $$$f$$$ $$$(1 \le f \le n)$$$ and the room number $$$k$$$ in this dormitory $$$(1 \le k \le a_f)$$$ to deliver the letter.
| null |
In the first example letters should be delivered in the following order:
- the first letter in room $$$1$$$ of the first dormitory
- the second letter in room $$$9$$$ of the first dormitory
- the third letter in room $$$2$$$ of the second dormitory
- the fourth letter in room $$$13$$$ of the second dormitory
- the fifth letter in room $$$1$$$ of the third dormitory
- the sixth letter in room $$$12$$$ of the third dormitory
|
[{"input": "3 6\n10 15 12\n1 9 12 23 26 37", "output": "1 1\n1 9\n2 2\n2 13\n3 1\n3 12"}, {"input": "2 3\n5 10000000000\n5 6 9999999999", "output": "1 5\n2 1\n2 9999999994"}]
| 1,000
|
["binary search", "implementation", "two pointers"]
| 16
|
[{"input": "3 6\r\n10 15 12\r\n1 9 12 23 26 37\r\n", "output": "1 1\r\n1 9\r\n2 2\r\n2 13\r\n3 1\r\n3 12\r\n"}, {"input": "2 3\r\n5 10000000000\r\n5 6 9999999999\r\n", "output": "1 5\r\n2 1\r\n2 9999999994\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "1 1\r\n"}, {"input": "5 15\r\n10 20 30 20 10\r\n1 6 10 11 15 30 31 54 60 61 76 80 81 84 90\r\n", "output": "1 1\r\n1 6\r\n1 10\r\n2 1\r\n2 5\r\n2 20\r\n3 1\r\n3 24\r\n3 30\r\n4 1\r\n4 16\r\n4 20\r\n5 1\r\n5 4\r\n5 10\r\n"}, {"input": "1 10\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "1 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\n"}, {"input": "5 8\r\n10 1 1 1 10\r\n9 10 11 12 13 14 15 23\r\n", "output": "1 9\r\n1 10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n5 2\r\n5 10\r\n"}, {"input": "1 3\r\n10000\r\n1 4325 10000\r\n", "output": "1 1\r\n1 4325\r\n1 10000\r\n"}, {"input": "4 18\r\n5 6 3 4\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\r\n", "output": "1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 1\r\n2 2\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n3 1\r\n3 2\r\n3 3\r\n4 1\r\n4 2\r\n4 3\r\n4 4\r\n"}, {"input": "3 10\r\n1000000000 1000000000 1000000000\r\n543678543 567869543 1000000000 1000000001 1500000000 1999999999 2000000000 2000000001 2754432345 3000000000\r\n", "output": "1 543678543\r\n1 567869543\r\n1 1000000000\r\n2 1\r\n2 500000000\r\n2 999999999\r\n2 1000000000\r\n3 1\r\n3 754432345\r\n3 1000000000\r\n"}]
| false
|
stdio
| null | true
|
813/D
|
813
|
D
|
PyPy 3
|
TESTS
| 3
| 61
| 409,600
|
155264783
|
from collections import defaultdict
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
dp0 = [1] * n
for i in range(n):
dpi = dp0[i]
ai = a[i]
for j in range(i):
if ai % 7 == a[j] % 7 or abs(ai - a[j]) == 1:
dpi = max(dpi, dp0[j] + 1)
dp0[i] = dpi
dp1 = [[0] * 7 for _ in range(n)]
dp2 = [defaultdict(lambda : 0) for _ in range(n)]
ans = 2
for i in range(n):
dp = [dp0[i] + 1 for _ in range(n)]
ai = a[i]
for j in range(i + 1, n):
aj = a[j]
dp[j] = max(dp[j], max(dp1[i][aj % 7], dp2[i][aj + 1], dp2[i][aj - 1]) + 1)
dp1[i][aj % 7] = max(dp[j], dp1[i][aj % 7])
dp1[j][ai % 7] = max(dp[j], dp1[j][ai % 7])
dp2[i][aj] = max(dp[j], dp2[i][aj])
dp2[j][ai] = max(dp[j], dp2[j][ai])
ans = max(ans, max(dp))
print(ans)
| 35
| 1,434
| 104,243,200
|
112897541
|
import sys
def solve():
n = int(sys.stdin.readline())
a = [0] + [int(i) for i in sys.stdin.readline().split()]
dp = [[0]*(n + 1) for i in range(n + 1)]
ans = 0
maxnum = [0] * (10**5 + 2)
maxmod = [0] * 7
for y in range(n + 1):
maxmod = [0] * 7
for ai in a:
maxnum[ai] = 0
for i in range(y):
maxmod[a[i] % 7] = max(maxmod[a[i] % 7], dp[i][y])
maxnum[a[i]] = max(maxnum[a[i]], dp[i][y])
for x in range(y + 1, n + 1):
dp[x][y] = max(maxmod[a[x] % 7], maxnum[a[x] + 1], maxnum[a[x] - 1], dp[0][y]) + 1
dp[y][x] = dp[x][y]
maxmod[a[x] % 7] = max(maxmod[a[x] % 7], dp[x][y])
maxnum[a[x]] = max(maxnum[a[x]], dp[x][y])
ans = max(ans, dp[x][y])
print(ans)
if __name__ == '__main__':
solve()
|
Educational Codeforces Round 22
|
ICPC
| 2,017
| 2
| 256
|
Two Melodies
|
Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time!
Alice has a sheet with n notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.
|
The first line contains one integer number n (2 ≤ n ≤ 5000).
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.
|
Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.
| null |
In the first example subsequences [1, 2] and [4, 5] give length 4 in total.
In the second example subsequences [62, 48, 49] and [60, 61] give length 5 in total. If you choose subsequence [62, 61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal.
|
[{"input": "4\n1 2 4 5", "output": "4"}, {"input": "6\n62 22 60 61 48 49", "output": "5"}]
| 2,600
|
["dp", "flows"]
| 35
|
[{"input": "4\r\n1 2 4 5\r\n", "output": "4\r\n"}, {"input": "6\r\n62 22 60 61 48 49\r\n", "output": "5\r\n"}, {"input": "2\r\n1 4\r\n", "output": "2\r\n"}, {"input": "2\r\n5 4\r\n", "output": "2\r\n"}, {"input": "10\r\n9 6 8 5 5 2 8 9 2 2\r\n", "output": "9\r\n"}, {"input": "10\r\n7776 32915 1030 71664 7542 72359 65387 75222 95899 40333\r\n", "output": "6\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "10\r\n"}, {"input": "4\r\n15 11 28 17\r\n", "output": "2\r\n"}, {"input": "3\r\n1 36 6\r\n", "output": "3\r\n"}, {"input": "6\r\n3 12 4 12 5 6\r\n", "output": "6\r\n"}, {"input": "6\r\n7 20 21 22 23 28\r\n", "output": "6\r\n"}]
| false
|
stdio
| null | true
|
326/A
|
335
|
A
|
Python 3
|
TESTS
| 6
| 92
| 0
|
202888667
|
##import sys
##input=sys.stdin.readline
s = input()
n = int(input())
st = list(set(s))
l = len(set(s))
if (l > n or n > len(s)):
print("-1")
elif (n == len(s)):
print(s)
elif (n == l):
m = 0
for i in range(len(s)):
cnt = 1
for j in range(i+1, len(s)):
if (s[i] == s[j]):
cnt += 1
if (m < cnt):
m = cnt
print(m, ''.join(st), sep = "\n")
else:
d = dict()
for i in s:
if (i not in d.keys()):
d[i] = 1
else:
d[i] += 1
a = max(d, key = lambda x: d[x])
n -= l
while(n > 0):
for i,j in d.items():
if (j > 1):
st.append(i)
n -= 1
if (n == 0):
break
m = st.count(a)
print(d[a] // m + 1, ''.join(st), sep = "\n")
| 55
| 154
| 307,200
|
111125965
|
s,n,q,a=input(),int(input()),{},""
for i in s:q[i]=[q[i][0]+1,1]if i in q else [1,1]
if len(q)>n:print(-1)
else:
for i in range(n-len(q)):
o=0
for j in q:
m=(q[j][0]+q[j][1]-1)//q[j][1]
if m>o:o,w=m,j
q[w][1]=q[w][1]+1
for i in q:a+=i*q[i][1]
o=0
for i in q:
m=(q[i][0]+q[i][1]-1)//q[i][1]
if m>o:o=m
print(o,"\n",a)
|
MemSQL start[c]up Round 2
|
CF
| 2,013
| 2
| 256
|
Banana
|
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
|
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
| null |
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
|
[{"input": "banana\n4", "output": "2\nbaan"}, {"input": "banana\n3", "output": "3\nnab"}, {"input": "banana\n2", "output": "-1"}]
| 1,400
|
[]
| 55
|
[{"input": "banana\r\n4\r\n", "output": "2\r\nbaan\r\n"}, {"input": "banana\r\n3\r\n", "output": "3\r\nnab\r\n"}, {"input": "banana\r\n2\r\n", "output": "-1\r\n"}, {"input": "p\r\n1000\r\n", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "b\r\n1\r\n", "output": "1\r\nb\r\n"}, {"input": "aba\r\n2\r\n", "output": "2\r\nab\r\n"}, {"input": "aaa\r\n2\r\n", "output": "2\r\naa\r\n"}, {"input": "aa\r\n3\r\n", "output": "1\r\naaa\r\n"}, {"input": "aaaaaaaabbbbbccccccccccccccccccccccccccccccc\r\n7\r\n", "output": "8\r\nabcccca\r\n"}, {"input": "aaaaa\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "baba\r\n3\r\n", "output": "2\r\naba\r\n"}, {"input": "a\r\n1000\r\n", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "aan\r\n5\r\n", "output": "1\r\naanaa\r\n"}, {"input": "banana\r\n5\r\n", "output": "2\r\naabna\r\n"}, {"input": "a\r\n5\r\n", "output": "1\r\naaaaa\r\n"}, {"input": "aaaaaaa\r\n5\r\n", "output": "2\r\naaaaa\r\n"}, {"input": "abc\r\n100\r\n", "output": "1\r\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "zzz\r\n4\r\n", "output": "1\r\nzzza\r\n"}, {"input": "aaabbb\r\n3\r\n", "output": "3\r\naba\r\n"}, {"input": "abc\r\n5\r\n", "output": "1\r\nabcaa\r\n"}, {"input": "abc\r\n10\r\n", "output": "1\r\nabcaaaaaaa\r\n"}, {"input": "aaaaa\r\n100\r\n", "output": "1\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abc\r\n1000\r\n", "output": "1\nabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, {"input": "a\r\n10\r\n", "output": "1\r\naaaaaaaaaa\r\n"}, {"input": "bbbbb\r\n6\r\n", "output": "1\r\nbbbbba\r\n"}, {"input": "bnana\r\n4\r\n", "output": "2\r\nabna\r\n"}, {"input": "aaaaaaabbbbbbb\r\n3\r\n", "output": "7\r\naba\r\n"}, {"input": "aabbbcccc\r\n7\r\n", "output": "2\r\nabbccaa\r\n"}, {"input": "aaa\r\n9\r\n", "output": "1\r\naaaaaaaaa\r\n"}, {"input": "a\r\n2\r\n", "output": "1\r\naa\r\n"}, {"input": "cccbba\r\n10\r\n", "output": "1\r\nabbcccaaaa\r\n"}, {"input": "a\r\n4\r\n", "output": "1\r\naaaa\r\n"}]
| false
|
stdio
|
import sys
from math import ceil
from collections import defaultdict
def main(input_path, output_path, submission_path):
with open(input_path) as f:
s = f.readline().strip()
n = int(f.readline().strip())
s_counts = defaultdict(int)
for c in s:
s_counts[c] += 1
def is_possible(k):
if k == 0:
return False
total = 0
for c in s_counts:
cnt = s_counts[c]
req = (cnt + k - 1) // k
total += req
if total > n:
return False
return total <= n
possible = False
k_min = None
unique_chars = len(s_counts)
if unique_chars > n:
possible = False
else:
left, right = 1, max(s_counts.values()) if s_counts else 0
if is_possible(1):
possible, k_min = True, 1
else:
right = 1
while not is_possible(right):
right *= 2
left = right // 2
while left < right:
mid = (left + right) // 2
if is_possible(mid):
right = mid
else:
left = mid + 1
possible = is_possible(left)
k_min = left if possible else None
with open(submission_path) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if possible:
if len(lines) != 2:
print(0)
return
try:
k_submitted = int(lines[0])
sheet = lines[1]
except:
print(0)
return
if len(sheet) != n or not sheet.islower():
print(0)
return
sheet_chars = set(sheet)
if not set(s_counts.keys()).issubset(sheet_chars):
print(0)
return
sheet_counts = defaultdict(int)
for c in sheet:
sheet_counts[c] += 1
if sum(sheet_counts.values()) != n:
print(0)
return
if k_submitted != k_min:
print(0)
return
valid = True
for c in s_counts:
required = (s_counts[c] + k_submitted - 1) // k_submitted
if sheet_counts[c] < required:
valid = False
break
print(100 if valid else 0)
else:
if len(lines) != 1 or lines[0] != '-1':
print(0)
else:
print(100)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1:4]
main(input_path, output_path, submission_path)
| true
|
71/A
|
71
|
A
|
Python 3
|
TESTS
| 1
| 15
| 0
|
221528018
|
def creat(s):
n=len(s)
if(n>=10):
s2=''
s2+=s[0]
s2+=str(n-2)
s2+=s[n-1]
return s2
else:
return s
n=int(input())
d=[]
for i in range(n):
s=input()
d.append(creat(s))
for i in d:print(i)
| 20
| 31
| 0
|
182375805
|
n = int(input())
for i in range(n):
s = input()
l = len(s)
if l > 10:
a = s[0]
b = s[l - 1]
c = l - 2
print(''.join([a, str(c), b]))
else:
print(s)
|
Codeforces Beta Round 65 (Div. 2)
|
CF
| 2,011
| 1
| 256
|
Way Too Long Words
|
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
|
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
|
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
| null | null |
[{"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis", "output": "word\nl10n\ni18n\np43s"}]
| 800
|
["strings"]
| 20
|
[{"input": "4\r\nword\r\nlocalization\r\ninternationalization\r\npneumonoultramicroscopicsilicovolcanoconiosis\r\n", "output": "word\r\nl10n\r\ni18n\r\np43s\r\n"}, {"input": "5\r\nabcdefgh\r\nabcdefghi\r\nabcdefghij\r\nabcdefghijk\r\nabcdefghijklm\r\n", "output": "abcdefgh\r\nabcdefghi\r\nabcdefghij\r\na9k\r\na11m\r\n"}, {"input": "3\r\nnjfngnrurunrgunrunvurn\r\njfvnjfdnvjdbfvsbdubruvbubvkdb\r\nksdnvidnviudbvibd\r\n", "output": "n20n\r\nj27b\r\nk15d\r\n"}, {"input": "1\r\ntcyctkktcctrcyvbyiuhihhhgyvyvyvyvjvytchjckt\r\n", "output": "t41t\r\n"}, {"input": "24\r\nyou\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nunofficially\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n", "output": "you\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nu10y\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n"}, {"input": "1\r\na\r\n", "output": "a\r\n"}, {"input": "26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n", "output": "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n"}, {"input": "1\r\nabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\r\n", "output": "a98j\r\n"}, {"input": "10\r\ngyartjdxxlcl\r\nfzsck\r\nuidwu\r\nxbymclornemdmtj\r\nilppyoapitawgje\r\ncibzc\r\ndrgbeu\r\nhezplmsdekhhbo\r\nfeuzlrimbqbytdu\r\nkgdco\r\n", "output": "g10l\r\nfzsck\r\nuidwu\r\nx13j\r\ni13e\r\ncibzc\r\ndrgbeu\r\nh12o\r\nf13u\r\nkgdco\r\n"}, {"input": "20\r\nlkpmx\r\nkovxmxorlgwaomlswjxlpnbvltfv\r\nhykasjxqyjrmybejnmeumzha\r\ntuevlumpqbbhbww\r\nqgqsphvrmupxxc\r\ntrissbaf\r\nqfgrlinkzvzqdryckaizutd\r\nzzqtoaxkvwoscyx\r\noswytrlnhpjvvnwookx\r\nlpuzqgec\r\ngyzqfwxggtvpjhzmzmdw\r\nrlxjgmvdftvrmvbdwudra\r\nvsntnjpepnvdaxiporggmglhagv\r\nxlvcqkqgcrbgtgglj\r\nlyxwxbiszyhlsrgzeedzprbmcpduvq\r\nyrmqqvrkqskqukzqrwukpsifgtdc\r\nxpuohcsjhhuhvr\r\nvvlfrlxpvqejngwrbfbpmqeirxlw\r\nsvmasocxdvadmaxtrpakysmeaympy\r\nyuflqboqfdt\r\n", "output": "lkpmx\r\nk26v\r\nh22a\r\nt13w\r\nq12c\r\ntrissbaf\r\nq21d\r\nz13x\r\no17x\r\nlpuzqgec\r\ng18w\r\nr19a\r\nv25v\r\nx15j\r\nl28q\r\ny26c\r\nx12r\r\nv26w\r\ns27y\r\ny9t\r\n"}, {"input": "100\r\nm\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n", "output": "m\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n"}]
| false
|
stdio
| null | true
|
815/C
|
815
|
C
|
PyPy 3
|
TESTS
| 2
| 140
| 1,843,200
|
110012382
|
from collections import defaultdict
def BFGetMaxGood(start,RemPurse,G,Cost,Discount,prevSelected):
M=0
if RemPurse<0:
return -1
if RemPurse==0 or start>len(Cost):
return 0
#Either purchases this element
if (prevSelected,start) in G:#discount applies
M=max(M,1+BFGetMaxGood(start+1,RemPurse-Cost[start]+Discount[start],G,Cost,Discount,start))
else:
if start==1:
M=max(M,1+BFGetMaxGood(start+1,RemPurse-Cost[start]+Discount[start],G,Cost,Discount,start))
else:
M=max(M,1+BFGetMaxGood(start+1,RemPurse-Cost[start],G,Cost,Discount,start))
M=max(M,BFGetMaxGood(start+1,RemPurse,G,Cost,Discount,prevSelected))#or does not purchase this product
return M
N,B=map(int,input().strip().split())
G=set()
Cost={}
Discount={}
for i in range(0,N):
I=input().split(" ")
if len(I)==2:
u,v=int(I[0]),int(I[1])
Discount[i+1]=v
Cost[i+1]=u
else:
u,v,index=int(I[0]),int(I[1]),int(I[2])
Discount[i+1]=v
Cost[i+1]=u
G.add((index,i+1))
Ans=BFGetMaxGood(1,B,G,Cost,Discount,-1)
print(Ans)
| 92
| 1,106
| 206,643,200
|
156376775
|
import sys
readline=sys.stdin.readline
N,B=map(int,readline().split())
D,C=[],[]
c,d=map(int,readline().split())
D.append(d)
C.append(c)
child=[[] for x in range(N)]
for i in range(1,N):
c,d,p=map(int,readline().split())
p-=1
D.append(d)
C.append(c)
child[p].append(i)
queue=[0]
tour=[]
while queue:
x=queue.pop()
tour.append(x)
for y in child[x]:
queue.append(y)
inf=1<<60
dp0,dp1=[None]*N,[None]*N
for x in tour[::-1]:
dp0[x]=[0,C[x]]
dp1[x]=[inf,C[x]-D[x]]
for y in child[x]:
len_x=len(dp0[x])
len_y=len(dp0[y])
prev0=dp0[x]
prev1=dp1[x]
dp0[x]=[inf]*(len_x+len_y-1)
dp1[x]=[inf]*(len_x+len_y-1)
for i in range(len_x):
for j in range(len_y):
dp0[x][i+j]=min(dp0[x][i+j],prev0[i]+dp0[y][j])
dp1[x][i+j]=min(dp1[x][i+j],prev1[i]+min(dp0[y][j],dp1[y][j]))
ans=0
for i in range(N+1):
if dp0[0][i]<=B or dp1[0][i]<=B:
ans=max(ans,i)
print(ans)
|
Codeforces Round 419 (Div. 1)
|
CF
| 2,017
| 2
| 512
|
Karen and Supermarket
|
On the way home, Karen decided to stop by the supermarket to buy some groceries.
She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.
The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.
Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.
There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).
Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?
|
The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.
The next n lines describe the items. Specifically:
- The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
- If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
|
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
| null |
In the first test case, Karen can purchase the following 4 items:
- Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
- Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
- Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
- Buy the sixth item for 2 dollars.
The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.
In the second test case, Karen has enough money to use all the coupons and purchase everything.
|
[{"input": "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5", "output": "4"}, {"input": "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4", "output": "5"}]
| 2,400
|
["brute force", "dp", "trees"]
| 92
|
[{"input": "6 16\r\n10 9\r\n10 5 1\r\n12 2 1\r\n20 18 3\r\n10 2 3\r\n2 1 5\r\n", "output": "4\r\n"}, {"input": "5 10\r\n3 1\r\n3 1 1\r\n3 1 2\r\n3 1 3\r\n3 1 4\r\n", "output": "5\r\n"}, {"input": "13 30\r\n6 4\r\n25 5 1\r\n7 1 2\r\n9 4 2\r\n10 2 1\r\n12 3 1\r\n5 2 3\r\n10 9 6\r\n2 1 1\r\n5 3 9\r\n10 2 10\r\n10 9 6\r\n3 2 11\r\n", "output": "9\r\n"}, {"input": "8 9\r\n4 3\r\n8 3 1\r\n2 1 1\r\n4 2 2\r\n7 2 2\r\n3 1 2\r\n7 3 5\r\n2 1 3\r\n", "output": "4\r\n"}, {"input": "9 15\r\n3 1\r\n6 2 1\r\n8 3 2\r\n4 1 2\r\n2 1 2\r\n3 2 3\r\n8 7 1\r\n6 5 5\r\n8 4 4\r\n", "output": "7\r\n"}, {"input": "15 1000\r\n449 257\r\n881 657 1\r\n182 101 1\r\n733 545 2\r\n277 13 2\r\n991 689 3\r\n360 302 3\r\n965 570 4\r\n502 178 4\r\n43 28 5\r\n446 406 5\r\n484 152 6\r\n451 335 6\r\n874 600 7\r\n602 34 7\r\n", "output": "7\r\n"}, {"input": "9 7\r\n3 1\r\n6 2 1\r\n8 3 2\r\n4 1 2\r\n2 1 2\r\n3 2 3\r\n8 7 1\r\n6 5 5\r\n8 4 4\r\n", "output": "3\r\n"}, {"input": "3 100\r\n100 48\r\n50 1 1\r\n50 1 1\r\n", "output": "2\r\n"}, {"input": "1 1\r\n2 1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "0\r\n"}, {"input": "1 1000000000\r\n1000000000 1\r\n", "output": "1\r\n"}, {"input": "2 1000000000\r\n500000001 1\r\n500000001 1 1\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
977/F
|
977
|
F
|
Python 3
|
TESTS
| 4
| 140
| 24,678,400
|
230591007
|
n=int(input())
*mas,=map(int,input().split())
slov={}
for i in range(n):
if mas[i]-1 not in slov:
slov[mas[i]]=1
else:
slov[mas[i]]=slov.get(mas[i]-1)+1
ans=0
start=0
for key in slov.keys():
if slov[key]>ans:
ans=slov[key]
start=key
print(ans)
for x in range(start-ans+1, start+1):
print(mas.index(x)+1, end=" ")
| 70
| 186
| 37,273,600
|
225724905
|
n = int(input())
nums = [int(x) for x in input().split()]
n = len(nums)
dp = [0]*n
seen = {}
for i in range(n):
num = nums[i]
if num-1 in seen:
idx = seen[num-1]
dp[i] = 1 + dp[idx]
else:
dp[i] = 1
seen[num] = i
mmax = 0
high = -1
for i in range(n):
if dp[i] > mmax:
mmax = dp[i]
high = nums[i]
print(mmax)
low = high-mmax+1
idxs = []
cur = low
for i in range(n):
if len(idxs) == mmax:
break
x = nums[i]
if x == cur:
cur+=1
idxs.append(i+1)
print(*idxs)
|
Codeforces Round 479 (Div. 3)
|
ICPC
| 2,018
| 2
| 256
|
Consecutive Subsequence
|
You are given an integer array of length $$$n$$$.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $$$[x, x + 1, \dots, x + k - 1]$$$ for some value $$$x$$$ and length $$$k$$$.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $$$[5, 3, 1, 2, 4]$$$ the following arrays are subsequences: $$$[3]$$$, $$$[5, 3, 1, 2, 4]$$$, $$$[5, 1, 4]$$$, but the array $$$[1, 3]$$$ is not.
|
The first line of the input containing integer number $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array. The second line of the input containing $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array itself.
|
On the first line print $$$k$$$ — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
| null |
All valid answers for the first example (as sequences of indices):
- $$$[1, 3, 5, 6]$$$
- $$$[2, 3, 5, 6]$$$
All valid answers for the second example:
- $$$[1, 4]$$$
- $$$[2, 5]$$$
- $$$[3, 6]$$$
All valid answers for the third example:
- $$$[1]$$$
- $$$[2]$$$
- $$$[3]$$$
- $$$[4]$$$
All valid answers for the fourth example:
- $$$[1, 2, 3, 7, 8, 9]$$$
|
[{"input": "7\n3 3 4 7 5 6 8", "output": "4\n2 3 5 6"}, {"input": "6\n1 3 5 2 4 6", "output": "2\n1 4"}, {"input": "4\n10 9 8 7", "output": "1\n1"}, {"input": "9\n6 7 8 3 4 5 9 10 11", "output": "6\n1 2 3 7 8 9"}]
| 1,700
|
["dp"]
| 70
|
[{"input": "7\r\n3 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "6\r\n1 3 5 2 4 6\r\n", "output": "2\r\n1 4 \r\n"}, {"input": "4\r\n10 9 8 7\r\n", "output": "1\r\n1 \r\n"}, {"input": "9\r\n6 7 8 3 4 5 9 10 11\r\n", "output": "6\r\n1 2 3 7 8 9 \r\n"}, {"input": "1\r\n1337\r\n", "output": "1\r\n1 \r\n"}, {"input": "2\r\n456 123\r\n", "output": "1\r\n1 \r\n"}, {"input": "7\r\n100 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "7\r\n3 3 4 5 6 7 8\r\n", "output": "6\r\n2 3 4 5 6 7 \r\n"}, {"input": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12\r\n", "output": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12 \r\n"}]
| false
|
stdio
|
import sys
def read_file_lines(path):
with open(path, 'r') as f:
return [line.strip() for line in f.readlines()]
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
input_lines = read_file_lines(input_path)
n = int(input_lines[0])
a = list(map(int, input_lines[1].split()))
# Read reference output (to get k_ref)
ref_lines = read_file_lines(output_path)
k_ref = int(ref_lines[0])
# Read submission output
try:
sub_lines = read_file_lines(submission_path)
if len(sub_lines) < 2:
print(0)
return
k_sub = int(sub_lines[0])
indices_sub = list(map(int, sub_lines[1].split()))
except:
print(0)
return
# Check k matches
if k_sub != k_ref:
print(0)
return
# Check indices are valid
if len(indices_sub) != k_sub:
print(0)
return
# Check indices are unique, in range 1..n, and strictly increasing
prev = -1
seen = set()
for idx in indices_sub:
if idx < 1 or idx > n:
print(0)
return
if idx in seen:
print(0)
return
seen.add(idx)
if idx <= prev:
print(0)
return
prev = idx
# Check elements form consecutive increasing sequence
elements = [a[idx - 1] for idx in indices_sub]
if len(elements) < 1:
print(0)
return
x = elements[0]
for i in range(1, len(elements)):
if elements[i] != x + i:
print(0)
return
# All checks passed
print(1)
if __name__ == "__main__":
main()
| true
|
977/F
|
977
|
F
|
Python 3
|
TESTS
| 4
| 124
| 25,088,000
|
222445678
|
from collections import defaultdict ,Counter
n = int(input())
array = list(map(int,input().split()))
count = Counter()
subSeq = defaultdict(list)
for i in range(n):
if array[i] in count:
continue
count[array[i]] = count[array[i ] - 1] + 1
subSeq[array[i]].extend(subSeq[array[i] - 1])
subSeq[array[i]].append(i + 1)
res = max(count,key = lambda x : count[x])
print(count[res])
print(*subSeq[res])
| 70
| 249
| 39,833,600
|
225092564
|
# ﷽
from bisect import bisect_left
from collections import defaultdict
import sys
input = lambda: sys.stdin.readline().strip()
def inlst():return [int(i) for i in input().split()]
oo=float('inf')
def solve():
n=int(input())
lst=inlst()
dic=defaultdict(int)
k=-1;val=-1
for i in lst:
dic[i]=dic[i-1]+1
if dic[i]>val:
k=i
val=dic[i]
values=[]
for i in range(val):
values.append(k-i)
values=values[::-1]
id=0
ans=[]
for i,j in enumerate(lst):
if j==values[id]:
ans.append(i+1)
id+=1
if id==len(values):break
print(len(ans))
print(*ans)
def main():
# for i in range(int(input())):
solve()
if __name__ == "__main__":
main()
|
Codeforces Round 479 (Div. 3)
|
ICPC
| 2,018
| 2
| 256
|
Consecutive Subsequence
|
You are given an integer array of length $$$n$$$.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $$$[x, x + 1, \dots, x + k - 1]$$$ for some value $$$x$$$ and length $$$k$$$.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $$$[5, 3, 1, 2, 4]$$$ the following arrays are subsequences: $$$[3]$$$, $$$[5, 3, 1, 2, 4]$$$, $$$[5, 1, 4]$$$, but the array $$$[1, 3]$$$ is not.
|
The first line of the input containing integer number $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array. The second line of the input containing $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array itself.
|
On the first line print $$$k$$$ — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
| null |
All valid answers for the first example (as sequences of indices):
- $$$[1, 3, 5, 6]$$$
- $$$[2, 3, 5, 6]$$$
All valid answers for the second example:
- $$$[1, 4]$$$
- $$$[2, 5]$$$
- $$$[3, 6]$$$
All valid answers for the third example:
- $$$[1]$$$
- $$$[2]$$$
- $$$[3]$$$
- $$$[4]$$$
All valid answers for the fourth example:
- $$$[1, 2, 3, 7, 8, 9]$$$
|
[{"input": "7\n3 3 4 7 5 6 8", "output": "4\n2 3 5 6"}, {"input": "6\n1 3 5 2 4 6", "output": "2\n1 4"}, {"input": "4\n10 9 8 7", "output": "1\n1"}, {"input": "9\n6 7 8 3 4 5 9 10 11", "output": "6\n1 2 3 7 8 9"}]
| 1,700
|
["dp"]
| 70
|
[{"input": "7\r\n3 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "6\r\n1 3 5 2 4 6\r\n", "output": "2\r\n1 4 \r\n"}, {"input": "4\r\n10 9 8 7\r\n", "output": "1\r\n1 \r\n"}, {"input": "9\r\n6 7 8 3 4 5 9 10 11\r\n", "output": "6\r\n1 2 3 7 8 9 \r\n"}, {"input": "1\r\n1337\r\n", "output": "1\r\n1 \r\n"}, {"input": "2\r\n456 123\r\n", "output": "1\r\n1 \r\n"}, {"input": "7\r\n100 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "7\r\n3 3 4 5 6 7 8\r\n", "output": "6\r\n2 3 4 5 6 7 \r\n"}, {"input": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12\r\n", "output": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12 \r\n"}]
| false
|
stdio
|
import sys
def read_file_lines(path):
with open(path, 'r') as f:
return [line.strip() for line in f.readlines()]
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
input_lines = read_file_lines(input_path)
n = int(input_lines[0])
a = list(map(int, input_lines[1].split()))
# Read reference output (to get k_ref)
ref_lines = read_file_lines(output_path)
k_ref = int(ref_lines[0])
# Read submission output
try:
sub_lines = read_file_lines(submission_path)
if len(sub_lines) < 2:
print(0)
return
k_sub = int(sub_lines[0])
indices_sub = list(map(int, sub_lines[1].split()))
except:
print(0)
return
# Check k matches
if k_sub != k_ref:
print(0)
return
# Check indices are valid
if len(indices_sub) != k_sub:
print(0)
return
# Check indices are unique, in range 1..n, and strictly increasing
prev = -1
seen = set()
for idx in indices_sub:
if idx < 1 or idx > n:
print(0)
return
if idx in seen:
print(0)
return
seen.add(idx)
if idx <= prev:
print(0)
return
prev = idx
# Check elements form consecutive increasing sequence
elements = [a[idx - 1] for idx in indices_sub]
if len(elements) < 1:
print(0)
return
x = elements[0]
for i in range(1, len(elements)):
if elements[i] != x + i:
print(0)
return
# All checks passed
print(1)
if __name__ == "__main__":
main()
| true
|
247/C
|
250
|
C
|
PyPy 3
|
TESTS
| 20
| 374
| 11,059,200
|
51266323
|
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
s = [0 for i in range(k)]
if n == 100000 and k == 3:
print(1)
else:
for i in range(n):
if i == 0 and a[i] == a[i + 1]:
continue
elif i == 0 and a[i] != a[i + 1]:
s[a[0] - 1] += 1
elif i == n - 1 and a[i] == a[i - 1]:
continue
elif i == n - 1 and a[i] != a[i - 1]:
s[a[n - 1] - 1] += 1
else:
if a[i - 1] == a[i + 1]:
s[a[i] - 1] += 2
else:
s[a[i] - 1] += 1
x = max(s)
print(s.index(x) + 1)
| 44
| 404
| 7,372,800
|
148475565
|
def findMininumStress (newMovies, k):
stress = 0
for i in range(len(newMovies)):
if i < len(newMovies) - 1:
if newMovies[i] != newMovies[i + 1]:
stress = stress + 1
fewerStresses = [stress] * k
minimumValue = stress
minimumPosition = 0
for i in range(len(newMovies)):
if i > 0 and i < len(newMovies) - 1:
if newMovies[i - 1] != newMovies[i + 1]:
fewerStresses[newMovies[i] - 1] = fewerStresses[newMovies[i] - 1] - 1
else:
fewerStresses[newMovies[i] - 1] = fewerStresses[newMovies[i] - 1] - 2
else:
fewerStresses[newMovies[i] - 1] = fewerStresses[newMovies[i] - 1] - 1
for i in range(k):
if fewerStresses[i] < minimumValue:
minimumValue = fewerStresses[i]
minimumPosition = i + 1
print(minimumPosition)
def excludeConsecutiveRepetitions(n, movies, newMovies):
for i in range(n):
if i < n - 2:
if movies[i] != movies[i + 1]:
newMovies.append(movies[i])
elif i == n - 2:
if movies[i] != movies[i+1]:
newMovies.append(movies[i])
newMovies.append(movies[i + 1])
else:
newMovies.append(movies[i])
if __name__ == '__main__':
n, k = map(int, input().split())
movies = list(map(int, input().split()))
newMovies = []
excludeConsecutiveRepetitions(n, movies, newMovies)
findMininumStress (newMovies, k)
|
CROC-MBTU 2012, Final Round
|
CF
| 2,012
| 2
| 256
|
Movie Critics
|
A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre — an integer from 1 to k.
On the i-th day the festival will show a movie of genre ai. We know that a movie of each of k genres occurs in the festival programme at least once. In other words, each integer from 1 to k occurs in the sequence a1, a2, ..., an at least once.
Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site.
As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress.
Valentine can't watch all n movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the k genres and will skip all the movies of this genre. He is sure to visit other movies.
Valentine wants to choose such genre x (1 ≤ x ≤ k), that the total number of after-movie stresses (after all movies of genre x are excluded) were minimum.
|
The first line of the input contains two integers n and k (2 ≤ k ≤ n ≤ 105), where n is the number of movies and k is the number of genres.
The second line of the input contains a sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ k), where ai is the genre of the i-th movie. It is guaranteed that each number from 1 to k occurs at least once in this sequence.
|
Print a single number — the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.
| null |
In the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses.
In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.
|
[{"input": "10 3\n1 1 2 3 2 3 3 1 1 3", "output": "3"}, {"input": "7 3\n3 1 3 2 3 1 2", "output": "1"}]
| 1,600
|
[]
| 44
|
[{"input": "10 3\r\n1 1 2 3 2 3 3 1 1 3\r\n", "output": "3"}, {"input": "7 3\r\n3 1 3 2 3 1 2\r\n", "output": "1"}, {"input": "2 2\r\n1 2\r\n", "output": "1"}, {"input": "10 2\r\n1 2 2 1 1 2 1 1 2 2\r\n", "output": "1"}, {"input": "10 10\r\n5 7 8 2 4 10 1 3 9 6\r\n", "output": "1"}, {"input": "100 10\r\n6 2 8 1 7 1 2 9 2 6 10 4 2 8 7 5 2 9 5 2 3 2 8 3 7 2 4 3 1 8 8 5 7 10 2 1 8 4 1 4 9 4 2 1 9 3 7 2 4 8 4 3 10 3 9 5 7 7 1 2 10 7 7 8 9 7 1 7 4 8 9 4 1 10 2 4 2 10 9 6 10 5 1 4 2 1 3 1 6 9 10 1 8 9 1 9 1 1 7 6\r\n", "output": "1"}, {"input": "74 10\r\n10 5 4 7 1 9 3 5 10 7 1 4 8 8 4 1 3 9 3 3 10 6 10 4 2 8 9 7 3 2 5 3 6 7 10 4 4 7 8 2 3 10 5 10 5 10 7 9 9 6 1 10 8 9 7 8 9 10 3 6 10 9 9 5 10 6 4 3 5 3 6 8 9 3\r\n", "output": "10"}, {"input": "113 3\r\n1 3 2 2 1 3 1 2 2 2 3 1 1 3 1 3 3 1 2 2 1 3 2 3 3 1 3 1 1 3 3 1 2 3 3 1 3 3 2 3 3 1 1 1 1 2 3 2 2 3 3 2 3 1 3 2 1 3 2 1 1 2 2 2 2 2 1 1 3 3 2 1 1 3 2 2 1 3 1 1 1 3 3 2 1 2 2 3 3 1 3 1 2 2 1 2 2 3 3 2 3 1 3 1 1 2 3 2 3 2 3 1 3\r\n", "output": "3"}, {"input": "100 13\r\n1 1 9 10 6 1 12 13 9 5 3 7 3 5 2 2 10 1 3 8 9 4 4 4 2 10 12 11 1 5 7 13 4 12 5 9 3 13 5 10 7 2 1 7 2 2 4 10 3 10 6 11 13 1 4 3 8 8 9 8 13 4 4 3 7 12 5 5 8 13 1 9 8 12 12 10 4 7 7 12 1 4 3 4 9 6 4 13 10 12 10 9 8 13 13 5 6 9 7 13\r\n", "output": "3"}, {"input": "100 12\r\n9 12 3 3 1 3 12 12 7 9 6 5 8 12 10 7 8 3 4 8 5 9 9 10 9 7 4 5 10 7 4 1 11 6 5 9 1 2 9 9 1 10 6 8 9 10 7 9 10 3 6 4 9 12 11 10 4 4 2 12 11 8 4 9 12 6 4 7 5 1 5 2 7 4 10 2 5 6 4 2 5 8 6 9 6 4 8 6 2 11 4 12 3 1 1 11 1 6 1 10\r\n", "output": "9"}]
| false
|
stdio
| null | true
|
71/A
|
71
|
A
|
Python 3
|
TESTS
| 1
| 15
| 0
|
223035188
|
n = int(input())
for _ in range(n):
word = input()
if (len(word) < 10): print(word)
else: print(word[0] + str(len(word)-2) + word[-1])
| 20
| 31
| 0
|
182635365
|
def split(word):
return [char for char in word]
def func(a):
res = []
for i in range(len(a)):
if len(a[i])<=10:
res.append(a[i])
elif len(a[i])>10:
aa = split(a[i])
strr = ''
nuum = str(len(aa)-2)
strr=aa[0]+nuum+aa[-1]
res.append(strr)
return res
n = int(input())
words=[]
for i in range(n):
strt = str(input())
words.append(strt)
aaa = func(words)
for i in aaa:
print(i)
|
Codeforces Beta Round 65 (Div. 2)
|
CF
| 2,011
| 1
| 256
|
Way Too Long Words
|
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
|
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
|
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
| null | null |
[{"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis", "output": "word\nl10n\ni18n\np43s"}]
| 800
|
["strings"]
| 20
|
[{"input": "4\r\nword\r\nlocalization\r\ninternationalization\r\npneumonoultramicroscopicsilicovolcanoconiosis\r\n", "output": "word\r\nl10n\r\ni18n\r\np43s\r\n"}, {"input": "5\r\nabcdefgh\r\nabcdefghi\r\nabcdefghij\r\nabcdefghijk\r\nabcdefghijklm\r\n", "output": "abcdefgh\r\nabcdefghi\r\nabcdefghij\r\na9k\r\na11m\r\n"}, {"input": "3\r\nnjfngnrurunrgunrunvurn\r\njfvnjfdnvjdbfvsbdubruvbubvkdb\r\nksdnvidnviudbvibd\r\n", "output": "n20n\r\nj27b\r\nk15d\r\n"}, {"input": "1\r\ntcyctkktcctrcyvbyiuhihhhgyvyvyvyvjvytchjckt\r\n", "output": "t41t\r\n"}, {"input": "24\r\nyou\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nunofficially\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n", "output": "you\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nu10y\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n"}, {"input": "1\r\na\r\n", "output": "a\r\n"}, {"input": "26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n", "output": "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n"}, {"input": "1\r\nabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\r\n", "output": "a98j\r\n"}, {"input": "10\r\ngyartjdxxlcl\r\nfzsck\r\nuidwu\r\nxbymclornemdmtj\r\nilppyoapitawgje\r\ncibzc\r\ndrgbeu\r\nhezplmsdekhhbo\r\nfeuzlrimbqbytdu\r\nkgdco\r\n", "output": "g10l\r\nfzsck\r\nuidwu\r\nx13j\r\ni13e\r\ncibzc\r\ndrgbeu\r\nh12o\r\nf13u\r\nkgdco\r\n"}, {"input": "20\r\nlkpmx\r\nkovxmxorlgwaomlswjxlpnbvltfv\r\nhykasjxqyjrmybejnmeumzha\r\ntuevlumpqbbhbww\r\nqgqsphvrmupxxc\r\ntrissbaf\r\nqfgrlinkzvzqdryckaizutd\r\nzzqtoaxkvwoscyx\r\noswytrlnhpjvvnwookx\r\nlpuzqgec\r\ngyzqfwxggtvpjhzmzmdw\r\nrlxjgmvdftvrmvbdwudra\r\nvsntnjpepnvdaxiporggmglhagv\r\nxlvcqkqgcrbgtgglj\r\nlyxwxbiszyhlsrgzeedzprbmcpduvq\r\nyrmqqvrkqskqukzqrwukpsifgtdc\r\nxpuohcsjhhuhvr\r\nvvlfrlxpvqejngwrbfbpmqeirxlw\r\nsvmasocxdvadmaxtrpakysmeaympy\r\nyuflqboqfdt\r\n", "output": "lkpmx\r\nk26v\r\nh22a\r\nt13w\r\nq12c\r\ntrissbaf\r\nq21d\r\nz13x\r\no17x\r\nlpuzqgec\r\ng18w\r\nr19a\r\nv25v\r\nx15j\r\nl28q\r\ny26c\r\nx12r\r\nv26w\r\ns27y\r\ny9t\r\n"}, {"input": "100\r\nm\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n", "output": "m\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
Python 3
|
TESTS
| 5
| 92
| 0
|
149596648
|
n=int(input())
s="0"+input()
k=[1]*n
for i in range(1,n):
if s[i]=="R" and k[i]<=k[i-1]:
k[i]=k[i-1]+1
elif s[i]=="L" and k[i-1]<=k[i]:
k[i-1]+=1
elif s[i]=="=":
k[i]=k[i-1]
print(*k)
| 43
| 92
| 0
|
149605126
|
n=int(input())
s="0"+input()
k=[1]*n
for i in range(1,n):
if s[i]=="R" and k[i]<=k[i-1]:
k[i]=k[i-1]+1
elif s[i]=="L" and k[i-1]<=k[i]:
k[i-1]=1
elif s[i]=="=":
k[i]=k[i-1]
s=s[::-1]
k=k[::-1]
for i in range(n-1):
if s[i]=="L" and k[i+1]<=k[i]:
k[i+1]=k[i]+1
elif s[i]=="=":
k[i+1]=k[i]
print(*k[::-1])
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
71/A
|
71
|
A
|
Python 3
|
TESTS
| 1
| 15
| 0
|
226239599
|
a = int(input())
for i in range(a):
m = input()
if len(m) < 10:
print(m)
else:
n = len(m) - 2
print(f"{m[0]}{n}{m[-1]}")
| 20
| 31
| 0
|
183022020
|
test = int(input())
for i in range(test):
words = input()
if len(words) > 10:
arr = [words[0],(len(words)-2),words[ len(words)-1]]
print(arr[0]+""+str(arr[1])+""+arr[2])
else:
print(words)
|
Codeforces Beta Round 65 (Div. 2)
|
CF
| 2,011
| 1
| 256
|
Way Too Long Words
|
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
|
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
|
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
| null | null |
[{"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis", "output": "word\nl10n\ni18n\np43s"}]
| 800
|
["strings"]
| 20
|
[{"input": "4\r\nword\r\nlocalization\r\ninternationalization\r\npneumonoultramicroscopicsilicovolcanoconiosis\r\n", "output": "word\r\nl10n\r\ni18n\r\np43s\r\n"}, {"input": "5\r\nabcdefgh\r\nabcdefghi\r\nabcdefghij\r\nabcdefghijk\r\nabcdefghijklm\r\n", "output": "abcdefgh\r\nabcdefghi\r\nabcdefghij\r\na9k\r\na11m\r\n"}, {"input": "3\r\nnjfngnrurunrgunrunvurn\r\njfvnjfdnvjdbfvsbdubruvbubvkdb\r\nksdnvidnviudbvibd\r\n", "output": "n20n\r\nj27b\r\nk15d\r\n"}, {"input": "1\r\ntcyctkktcctrcyvbyiuhihhhgyvyvyvyvjvytchjckt\r\n", "output": "t41t\r\n"}, {"input": "24\r\nyou\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nunofficially\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n", "output": "you\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nu10y\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n"}, {"input": "1\r\na\r\n", "output": "a\r\n"}, {"input": "26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n", "output": "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n"}, {"input": "1\r\nabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\r\n", "output": "a98j\r\n"}, {"input": "10\r\ngyartjdxxlcl\r\nfzsck\r\nuidwu\r\nxbymclornemdmtj\r\nilppyoapitawgje\r\ncibzc\r\ndrgbeu\r\nhezplmsdekhhbo\r\nfeuzlrimbqbytdu\r\nkgdco\r\n", "output": "g10l\r\nfzsck\r\nuidwu\r\nx13j\r\ni13e\r\ncibzc\r\ndrgbeu\r\nh12o\r\nf13u\r\nkgdco\r\n"}, {"input": "20\r\nlkpmx\r\nkovxmxorlgwaomlswjxlpnbvltfv\r\nhykasjxqyjrmybejnmeumzha\r\ntuevlumpqbbhbww\r\nqgqsphvrmupxxc\r\ntrissbaf\r\nqfgrlinkzvzqdryckaizutd\r\nzzqtoaxkvwoscyx\r\noswytrlnhpjvvnwookx\r\nlpuzqgec\r\ngyzqfwxggtvpjhzmzmdw\r\nrlxjgmvdftvrmvbdwudra\r\nvsntnjpepnvdaxiporggmglhagv\r\nxlvcqkqgcrbgtgglj\r\nlyxwxbiszyhlsrgzeedzprbmcpduvq\r\nyrmqqvrkqskqukzqrwukpsifgtdc\r\nxpuohcsjhhuhvr\r\nvvlfrlxpvqejngwrbfbpmqeirxlw\r\nsvmasocxdvadmaxtrpakysmeaympy\r\nyuflqboqfdt\r\n", "output": "lkpmx\r\nk26v\r\nh22a\r\nt13w\r\nq12c\r\ntrissbaf\r\nq21d\r\nz13x\r\no17x\r\nlpuzqgec\r\ng18w\r\nr19a\r\nv25v\r\nx15j\r\nl28q\r\ny26c\r\nx12r\r\nv26w\r\ns27y\r\ny9t\r\n"}, {"input": "100\r\nm\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n", "output": "m\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n"}]
| false
|
stdio
| null | true
|
71/A
|
71
|
A
|
Python 3
|
TESTS
| 1
| 15
| 0
|
226497407
|
x = int(input())
words = []
process = []
for i in range(x):
word = input()
words.append(word)
for i in range(0, len(words)):
if len(words[i]) - 2 < 10:
process.append(words[i])
if len(words[i]) - 2 >= 10:
f = words[i]
c = f[0] + str(len(words[i])- 2) + f[-1]
process.append(c)
print("\n".join(process))
| 20
| 31
| 0
|
183534986
|
a = int(input())
for i in range(0, a):
b = str(input())
if len(b) > 10:
print(b[0] + str(len(b)-2) + b[-1])
else:
print(b)
|
Codeforces Beta Round 65 (Div. 2)
|
CF
| 2,011
| 1
| 256
|
Way Too Long Words
|
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
|
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
|
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
| null | null |
[{"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis", "output": "word\nl10n\ni18n\np43s"}]
| 800
|
["strings"]
| 20
|
[{"input": "4\r\nword\r\nlocalization\r\ninternationalization\r\npneumonoultramicroscopicsilicovolcanoconiosis\r\n", "output": "word\r\nl10n\r\ni18n\r\np43s\r\n"}, {"input": "5\r\nabcdefgh\r\nabcdefghi\r\nabcdefghij\r\nabcdefghijk\r\nabcdefghijklm\r\n", "output": "abcdefgh\r\nabcdefghi\r\nabcdefghij\r\na9k\r\na11m\r\n"}, {"input": "3\r\nnjfngnrurunrgunrunvurn\r\njfvnjfdnvjdbfvsbdubruvbubvkdb\r\nksdnvidnviudbvibd\r\n", "output": "n20n\r\nj27b\r\nk15d\r\n"}, {"input": "1\r\ntcyctkktcctrcyvbyiuhihhhgyvyvyvyvjvytchjckt\r\n", "output": "t41t\r\n"}, {"input": "24\r\nyou\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nunofficially\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n", "output": "you\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nu10y\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n"}, {"input": "1\r\na\r\n", "output": "a\r\n"}, {"input": "26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n", "output": "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n"}, {"input": "1\r\nabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\r\n", "output": "a98j\r\n"}, {"input": "10\r\ngyartjdxxlcl\r\nfzsck\r\nuidwu\r\nxbymclornemdmtj\r\nilppyoapitawgje\r\ncibzc\r\ndrgbeu\r\nhezplmsdekhhbo\r\nfeuzlrimbqbytdu\r\nkgdco\r\n", "output": "g10l\r\nfzsck\r\nuidwu\r\nx13j\r\ni13e\r\ncibzc\r\ndrgbeu\r\nh12o\r\nf13u\r\nkgdco\r\n"}, {"input": "20\r\nlkpmx\r\nkovxmxorlgwaomlswjxlpnbvltfv\r\nhykasjxqyjrmybejnmeumzha\r\ntuevlumpqbbhbww\r\nqgqsphvrmupxxc\r\ntrissbaf\r\nqfgrlinkzvzqdryckaizutd\r\nzzqtoaxkvwoscyx\r\noswytrlnhpjvvnwookx\r\nlpuzqgec\r\ngyzqfwxggtvpjhzmzmdw\r\nrlxjgmvdftvrmvbdwudra\r\nvsntnjpepnvdaxiporggmglhagv\r\nxlvcqkqgcrbgtgglj\r\nlyxwxbiszyhlsrgzeedzprbmcpduvq\r\nyrmqqvrkqskqukzqrwukpsifgtdc\r\nxpuohcsjhhuhvr\r\nvvlfrlxpvqejngwrbfbpmqeirxlw\r\nsvmasocxdvadmaxtrpakysmeaympy\r\nyuflqboqfdt\r\n", "output": "lkpmx\r\nk26v\r\nh22a\r\nt13w\r\nq12c\r\ntrissbaf\r\nq21d\r\nz13x\r\no17x\r\nlpuzqgec\r\ng18w\r\nr19a\r\nv25v\r\nx15j\r\nl28q\r\ny26c\r\nx12r\r\nv26w\r\ns27y\r\ny9t\r\n"}, {"input": "100\r\nm\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n", "output": "m\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n"}]
| false
|
stdio
| null | true
|
362/A
|
362
|
A
|
Python 3
|
PRETESTS
| 7
| 46
| 307,200
|
5101339
|
N = int(input())
for i in range(N):
if i > 0:
input()
Pos = []
for i in range(8):
S = input().strip()
for j in range(8):
if S[j] == "K":
Pos.append((i, j))
A, B = Pos
if (A[0] + A[1]) % 4 == (B[0] + B[1]) % 4:
if max(abs(A[0] - B[0]) // 2, abs(A[1] - B[1]) // 2) % 2 == 0:
print("YES")
else:
print("NO")
else:
print("NO")
| 45
| 46
| 0
|
155435429
|
def main():
n = int(input())
out = ""
for t in range(n):
knights = [0 for i in range(16)]
valid = [False for i in range(16)]
for i in range(8):
line = input()
#print()
for j in range(8):
#print(get(i, j), end="\t")
if line[j] != '#':
valid[get(i, j)] = True
if line[j] == 'K':
knights[get(i, j)] += 1
for i in range(16):
#print(i, knights[i], valid[i])
if knights[i] == 2 and valid[i]:
out += "YES\n"
break
else:
out += "NO\n"
if t != n-1:
input()
print(out[:-1])
def get(i, j):
return [[0, 1, 2, 3],[4, 5, 6, 7],[8, 9, 10, 11],[12, 13, 14, 15]][i%4][j%4]
if __name__ == "__main__": main()
|
Codeforces Round 212 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Semiknights Meet
|
A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.
Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.
Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.
Please see the test case analysis.
|
The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.
|
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.
| null |
Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).
On the second board the semiknights will never meet.
|
[{"input": "2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#", "output": "YES\nNO"}]
| 1,500
|
["greedy", "math"]
| 45
|
[{"input": "2\r\n........\r\n........\r\n......#.\r\nK..##..#\r\n.......#\r\n...##..#\r\n......#.\r\nK.......\r\n\r\n........\r\n........\r\n..#.....\r\n..#..#..\r\n..####..\r\n...##...\r\n........\r\n....K#K#\r\n", "output": "YES\r\nNO\r\n"}, {"input": "3\r\n........\r\n........\r\n..#.....\r\n..#..#..\r\n..####..\r\n...##...\r\n........\r\n####K#K#\r\n\r\n........\r\nK......K\r\n........\r\n#......#\r\n.#....#.\r\n..####..\r\n........\r\n........\r\n\r\n.#..#...\r\n.##.##..\r\n..###...\r\n..#K###.\r\n..####..\r\n......K.\r\n..#####.\r\n..#####.\r\n", "output": "NO\r\nNO\r\nNO\r\n"}, {"input": "1\r\n...#...#\r\n........\r\n.#...K..\r\n........\r\n...#...#\r\n........\r\n.K...#..\r\n........\r\n", "output": "YES\r\n"}, {"input": "1\r\nK.#....#\r\n...#..#.\r\n..#.....\r\n..#.###.\r\n..#.....\r\n...#....\r\n.#.....#\r\n.#...##K\r\n", "output": "NO\r\n"}, {"input": "2\r\n....#..K\r\n...#....\r\n..##.#..\r\n.#.#.#..\r\n.#.....#\r\n.#......\r\n###.....\r\nK#.#....\r\n\r\nK.#.....\r\n..#...#.\r\n#.....#.\r\n..#.#..#\r\n#.......\r\n..#..#..\r\n....#...\r\nK..##.##\r\n", "output": "NO\r\nNO\r\n"}, {"input": "5\r\n........\r\n...KK...\r\n..####..\r\n...##...\r\n........\r\n..####..\r\n.######.\r\n#......#\r\n\r\n........\r\n.K......\r\n..#.....\r\n...#....\r\n....#...\r\n.....#..\r\n......#.\r\n.......K\r\n\r\n........\r\n...K....\r\n##...##.\r\n#.#.#..#\r\n.##.###.\r\n#..K#..#\r\n.##..##.\r\n........\r\n\r\n........\r\n.K..K...\r\n..##....\r\n..####..\r\n.#....#.\r\n.#.....#\r\n..#####.\r\n........\r\n\r\nK.......\r\n........\r\n........\r\n........\r\n........\r\n........\r\n........\r\n.......K\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "6\r\nK.......\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\n..####.K\r\n\r\n.......K\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\nK.####..\r\n\r\nK.......\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\nK.####..\r\n\r\n.......K\r\n........\r\n.##..##.\r\n#..##..#\r\n........\r\n...##...\r\n.#....#.\r\n..####.K\r\n\r\n........\r\n........\r\n........\r\n.KK.....\r\n........\r\n........\r\n........\r\n........\r\n\r\n........\r\n........\r\n.#...K..\r\n........\r\n...#....\r\n........\r\n.K...#..\r\n........\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "4\r\n#.##..##\r\n###.#.K#\r\n###.####\r\n.####.#.\r\n.#######\r\n######K#\r\n####.#..\r\n#.######\r\n\r\n#.......\r\n....##K.\r\n........\r\n...#.##.\r\n#....#..\r\n..K.#...\r\n...#....\r\n...#....\r\n\r\n.......#\r\n#..K....\r\n#.......\r\n#....#..\r\n..#...#.\r\n...K#.#.\r\n........\r\n#.#...#.\r\n\r\n..##....\r\n........\r\n...#....\r\n...#K...\r\n........\r\n...##...\r\n..#.....\r\n....K...\r\n", "output": "YES\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "5\r\nK###K###\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n\r\nK##K####\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n\r\nK####K##\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n########\r\n\r\n........\r\n........\r\n........\r\n...K....\r\n........\r\n.....K..\r\n........\r\n........\r\n\r\n....#.#.\r\n..K.K...\r\n.......#\r\n....#...\r\n........\r\n........\r\n........\r\n.#......\r\n", "output": "YES\r\nNO\r\nNO\r\nNO\r\nNO\r\n"}]
| false
|
stdio
| null | true
|
977/F
|
977
|
F
|
Python 3
|
TESTS
| 10
| 280
| 28,672,000
|
223056644
|
l = int(input())
nums = list(map(int,input().split()))
lenmax = {n:0 for n in nums}
for n in nums:
if n-1 in lenmax:
lenmax[n] = lenmax[n-1]+1
ans = max(lenmax.values())
last=0
for n in nums:
if lenmax[n]==ans:
last =n
break
res = []
for i in range(l-1,-1,-1):
if nums[i] == last:
res.append(i)
last-=1
print(len(res))
for i in reversed(res):
print(i+1,end=" ")
| 70
| 404
| 32,256,000
|
229090108
|
n=int(input())
arr=[int(i) for i in input().split()]
data={}
maxi=0
last_val=0
for i,e in enumerate(arr):
c=data.get(e-1,0)
data[e]=c+1
if c+1>maxi:
maxi=c+1
last_val=e
print(data[last_val])
start=last_val-data[last_val]+1
for i,e in enumerate(arr):
if e==start:
print(i+1,end=' ')
start+=1
|
Codeforces Round 479 (Div. 3)
|
ICPC
| 2,018
| 2
| 256
|
Consecutive Subsequence
|
You are given an integer array of length $$$n$$$.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $$$[x, x + 1, \dots, x + k - 1]$$$ for some value $$$x$$$ and length $$$k$$$.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $$$[5, 3, 1, 2, 4]$$$ the following arrays are subsequences: $$$[3]$$$, $$$[5, 3, 1, 2, 4]$$$, $$$[5, 1, 4]$$$, but the array $$$[1, 3]$$$ is not.
|
The first line of the input containing integer number $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array. The second line of the input containing $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array itself.
|
On the first line print $$$k$$$ — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
| null |
All valid answers for the first example (as sequences of indices):
- $$$[1, 3, 5, 6]$$$
- $$$[2, 3, 5, 6]$$$
All valid answers for the second example:
- $$$[1, 4]$$$
- $$$[2, 5]$$$
- $$$[3, 6]$$$
All valid answers for the third example:
- $$$[1]$$$
- $$$[2]$$$
- $$$[3]$$$
- $$$[4]$$$
All valid answers for the fourth example:
- $$$[1, 2, 3, 7, 8, 9]$$$
|
[{"input": "7\n3 3 4 7 5 6 8", "output": "4\n2 3 5 6"}, {"input": "6\n1 3 5 2 4 6", "output": "2\n1 4"}, {"input": "4\n10 9 8 7", "output": "1\n1"}, {"input": "9\n6 7 8 3 4 5 9 10 11", "output": "6\n1 2 3 7 8 9"}]
| 1,700
|
["dp"]
| 70
|
[{"input": "7\r\n3 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "6\r\n1 3 5 2 4 6\r\n", "output": "2\r\n1 4 \r\n"}, {"input": "4\r\n10 9 8 7\r\n", "output": "1\r\n1 \r\n"}, {"input": "9\r\n6 7 8 3 4 5 9 10 11\r\n", "output": "6\r\n1 2 3 7 8 9 \r\n"}, {"input": "1\r\n1337\r\n", "output": "1\r\n1 \r\n"}, {"input": "2\r\n456 123\r\n", "output": "1\r\n1 \r\n"}, {"input": "7\r\n100 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "7\r\n3 3 4 5 6 7 8\r\n", "output": "6\r\n2 3 4 5 6 7 \r\n"}, {"input": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12\r\n", "output": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12 \r\n"}]
| false
|
stdio
|
import sys
def read_file_lines(path):
with open(path, 'r') as f:
return [line.strip() for line in f.readlines()]
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
input_lines = read_file_lines(input_path)
n = int(input_lines[0])
a = list(map(int, input_lines[1].split()))
# Read reference output (to get k_ref)
ref_lines = read_file_lines(output_path)
k_ref = int(ref_lines[0])
# Read submission output
try:
sub_lines = read_file_lines(submission_path)
if len(sub_lines) < 2:
print(0)
return
k_sub = int(sub_lines[0])
indices_sub = list(map(int, sub_lines[1].split()))
except:
print(0)
return
# Check k matches
if k_sub != k_ref:
print(0)
return
# Check indices are valid
if len(indices_sub) != k_sub:
print(0)
return
# Check indices are unique, in range 1..n, and strictly increasing
prev = -1
seen = set()
for idx in indices_sub:
if idx < 1 or idx > n:
print(0)
return
if idx in seen:
print(0)
return
seen.add(idx)
if idx <= prev:
print(0)
return
prev = idx
# Check elements form consecutive increasing sequence
elements = [a[idx - 1] for idx in indices_sub]
if len(elements) < 1:
print(0)
return
x = elements[0]
for i in range(1, len(elements)):
if elements[i] != x + i:
print(0)
return
# All checks passed
print(1)
if __name__ == "__main__":
main()
| true
|
66/C
|
66
|
C
|
Python 3
|
TESTS
| 8
| 218
| 0
|
66516380
|
import sys, operator
filePaths = sys.stdin.readlines()
foldercount = 0
directory = {}; fileoccurences = {'C:':0,'D:':0,'E:':0,'F:':0,'G:':0}
for line in filePaths:
path = list(map(str,line.split('\\')))
fileoccurences[path[0]] += 1
newFolder = False
for i in range(1,len(path)-1):
if path[i] not in directory.keys():
if i == 1:
foldercount -= 1
directory.update({path[i]:0})
foldercount+=1
newFolder = True
elif newFolder:
foldercount+=1
print(foldercount,max(fileoccurences.items(),key = operator.itemgetter(1))[1])
| 100
| 248
| 512,000
|
66522249
|
import sys
files,folder_subfolder = {},{}
paths = sys.stdin.readlines()
for line in paths:
path = line.split('\\')
path_length = len(path)
headOFpath = path[0] + path[1]; interim = headOFpath
if headOFpath not in folder_subfolder:
# To collect the folder and the files
folder_subfolder[headOFpath] = []
files[headOFpath] = []
for i in range(2,path_length):
if i+1 == path_length:
# appends the file, with the whole path, to the head of the path
files[headOFpath].append(interim+path[i])
else:
interim+=path[i]
# appends every increment of the path
folder_subfolder[headOFpath].append(interim)
myfiles,myfolders = 0,0
for f in folder_subfolder:
# the sets ensure that a duplicate path doesn't occur
# the length contains the folders and subfolders
myfiles = max(len(set(files[f])),myfiles)
myfolders = max(len(set(folder_subfolder[f])),myfolders)
print (myfolders,myfiles)
|
Codeforces Beta Round 61 (Div. 2)
|
CF
| 2,011
| 3
| 256
|
Petya and File System
|
Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders).
More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder.
For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples.
You are given a few files that Petya has managed to create. The path to each file looks as follows:
diskName:\folder1\folder2\...\ foldern\fileName
- diskName is single capital letter from the set {C,D,E,F,G}.
- folder1, ..., foldern are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (n ≥ 1)
- fileName is a file name in the form of name.extension, where the name and the extension are nonempty sequences of lowercase Latin letters and digits from 0 to 9.
It is also known that there is no file whose path looks like diskName:\fileName. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder.
Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders.
|
Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once.
There is at least one line in the input data.
|
Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders.
| null |
In the first sample we have one folder on the "C" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1.
In the second sample we have several different folders. Consider the "folder1" folder on the "C" disk. This folder directly contains one folder, "folder2". The "folder2" folder contains two more folders — "folder3" and "folder4". Thus, the "folder1" folder on the "C" drive has exactly 3 subfolders. Also this folder contains two files, even though they do not lie directly in the folder, but they are located in subfolders of "folder1".
In the third example we see that the names of some folders and some subfolders are identical. Consider the "file" folder, which lies directly on the "C" disk. That folder contains another "file" folder, which in turn contains another "file" folder, which contains two more folders, "file" and "file2". Thus, the "file" folder, which lies directly on the "C" disk, contains 4 subfolders.
|
[{"input": "C:\n\\\nfolder1\n\\\nfile1.txt", "output": "0 1"}, {"input": "C:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder3\n\\\nfile1.txt\nC:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder4\n\\\nfile1.txt\nD:\n\\\nfolder1\n\\\nfile1.txt", "output": "3 2"}, {"input": "C:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile.txt\nC:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile2\n\\\nfile.txt", "output": "4 2"}]
| 1,800
|
["data structures", "implementation"]
| 100
|
[{"input": "C:\\folder1\\file1.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\folder1\\folder2\\folder3\\file1.txt\r\nC:\\folder1\\folder2\\folder4\\file1.txt\r\nD:\\folder1\\file1.txt\r\n", "output": "3 2\r\n"}, {"input": "C:\\file\\file\\file\\file\\file.txt\r\nC:\\file\\file\\file\\file2\\file.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\file\\file.txt\r\nD:\\file\\file.txt\r\nE:\\file\\file.txt\r\nF:\\file\\file.txt\r\nG:\\file\\file.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\a\\b\\c\\d\\d.txt\r\nC:\\a\\b\\c\\e\\f.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\z\\z.txt\r\nD:\\1\\1.txt\r\nD:\\1\\2.txt\r\n", "output": "0 2\r\n"}, {"input": "D:\\0000\\1.txt\r\nE:\\00000\\1.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\a\\b\\c\\d.txt\r\nC:\\a\\e\\c\\d.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\test1\\test2\\test3\\test.txt\r\nC:\\test1\\test3\\test3\\test4\\test.txt\r\nC:\\test1\\test2\\test3\\test2.txt\r\nD:\\test1\\test2\\test.txt\r\nD:\\test1\\test3\\test4.txt\r\n", "output": "5 3\r\n"}, {"input": "C:\\test1\\test2\\test.txt\r\nC:\\test1\\test2\\test2.txt\r\n", "output": "1 2\r\n"}]
| false
|
stdio
| null | true
|
71/A
|
71
|
A
|
Python 3
|
TESTS
| 1
| 15
| 0
|
220589322
|
case = int(input())
while case > 0:
w = input()
if len(w) >= 10:
print(w[0]+str(len(w)-2)+w[-1])
else:
print(w)
case -= 1
| 20
| 31
| 0
|
183550011
|
#A. Арбуз
##n = int(input())
##if n % 2 == 0 and n != 2:
## print("YES")
##else:
## print("NO")
#A. Слишком длинные слова
n = int(input())
h = []
for i in range(n):
a = input()
if len(a) > 10:
h.append(a[0]+str((len(a)-2))+a[-1])
else:
h.append(a)
for i in h:
print(i, end="\n")
|
Codeforces Beta Round 65 (Div. 2)
|
CF
| 2,011
| 1
| 256
|
Way Too Long Words
|
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
|
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
|
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
| null | null |
[{"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis", "output": "word\nl10n\ni18n\np43s"}]
| 800
|
["strings"]
| 20
|
[{"input": "4\r\nword\r\nlocalization\r\ninternationalization\r\npneumonoultramicroscopicsilicovolcanoconiosis\r\n", "output": "word\r\nl10n\r\ni18n\r\np43s\r\n"}, {"input": "5\r\nabcdefgh\r\nabcdefghi\r\nabcdefghij\r\nabcdefghijk\r\nabcdefghijklm\r\n", "output": "abcdefgh\r\nabcdefghi\r\nabcdefghij\r\na9k\r\na11m\r\n"}, {"input": "3\r\nnjfngnrurunrgunrunvurn\r\njfvnjfdnvjdbfvsbdubruvbubvkdb\r\nksdnvidnviudbvibd\r\n", "output": "n20n\r\nj27b\r\nk15d\r\n"}, {"input": "1\r\ntcyctkktcctrcyvbyiuhihhhgyvyvyvyvjvytchjckt\r\n", "output": "t41t\r\n"}, {"input": "24\r\nyou\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nunofficially\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n", "output": "you\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nu10y\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n"}, {"input": "1\r\na\r\n", "output": "a\r\n"}, {"input": "26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n", "output": "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n"}, {"input": "1\r\nabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\r\n", "output": "a98j\r\n"}, {"input": "10\r\ngyartjdxxlcl\r\nfzsck\r\nuidwu\r\nxbymclornemdmtj\r\nilppyoapitawgje\r\ncibzc\r\ndrgbeu\r\nhezplmsdekhhbo\r\nfeuzlrimbqbytdu\r\nkgdco\r\n", "output": "g10l\r\nfzsck\r\nuidwu\r\nx13j\r\ni13e\r\ncibzc\r\ndrgbeu\r\nh12o\r\nf13u\r\nkgdco\r\n"}, {"input": "20\r\nlkpmx\r\nkovxmxorlgwaomlswjxlpnbvltfv\r\nhykasjxqyjrmybejnmeumzha\r\ntuevlumpqbbhbww\r\nqgqsphvrmupxxc\r\ntrissbaf\r\nqfgrlinkzvzqdryckaizutd\r\nzzqtoaxkvwoscyx\r\noswytrlnhpjvvnwookx\r\nlpuzqgec\r\ngyzqfwxggtvpjhzmzmdw\r\nrlxjgmvdftvrmvbdwudra\r\nvsntnjpepnvdaxiporggmglhagv\r\nxlvcqkqgcrbgtgglj\r\nlyxwxbiszyhlsrgzeedzprbmcpduvq\r\nyrmqqvrkqskqukzqrwukpsifgtdc\r\nxpuohcsjhhuhvr\r\nvvlfrlxpvqejngwrbfbpmqeirxlw\r\nsvmasocxdvadmaxtrpakysmeaympy\r\nyuflqboqfdt\r\n", "output": "lkpmx\r\nk26v\r\nh22a\r\nt13w\r\nq12c\r\ntrissbaf\r\nq21d\r\nz13x\r\no17x\r\nlpuzqgec\r\ng18w\r\nr19a\r\nv25v\r\nx15j\r\nl28q\r\ny26c\r\nx12r\r\nv26w\r\ns27y\r\ny9t\r\n"}, {"input": "100\r\nm\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n", "output": "m\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n"}]
| false
|
stdio
| null | true
|
324/A1
|
331
|
A2
|
PyPy 3
|
TESTS2
| 7
| 310
| 20,275,200
|
86273522
|
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
same = defaultdict(list)
only_positive = [max(0,x) for x in a]
partial_sum = [0 for i in range(n+1)]
for i in range(1,n+1):
partial_sum[i] = partial_sum[i-1]+only_positive[i-1]
for i in range(0,n):
same[a[i]].append(i)
best = -10**18
bl, br = -1, -1
for key in same:
if len(same[key]) >= 2:
l, r = same[key][0], same[key][-1]
cur = key*2 + partial_sum[r]-partial_sum[l+1]
if cur > best:
best = cur
bl, br = l, r
cut = []
for i in range(n):
if not (a[i] >= 0 and bl <= i and i <= br):
cut.append(i+1)
print(best,len(cut))
print(*cut)
| 18
| 154
| 0
|
6763206
|
n, t = int(input()), list(map(int, input().split()))
a, b = {}, {}
for i, j in enumerate(t):
if not j in a: a[j] = i
else: b[j] = i
p = [(a[j], b[j] - 1) for j in b]
s = [j if j > 0 else 0 for j in t]
u = v = 2 * t[p[0][0]] - 1
for i in range(n - 1): s[i + 1] += s[i]
for i, j in p:
u = 2 * t[i] + s[j] - s[i]
if u > v: a, b, v = i, j, u
s = list(range(1, a + 1)) + [i for i, j in enumerate(t[a + 1: b + 1], a + 2) if j < 0] + list(range(b + 3, n + 1))
print(v, len(s))
print(' '.join(map(str, s)))
|
ABBYY Cup 3.0 - Finals
|
ICPC
| 2,013
| 2
| 256
|
Oh Sweet Beaverette
|
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
— Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
- The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
- the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
- and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
|
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
- to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);
- to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).
|
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
| null | null |
[{"input": "5\n1 2 3 1 2", "output": "8 1\n1"}, {"input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5"}]
| 1,400
|
[]
| 18
|
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "8 1\r\n1 "}, {"input": "5\r\n1 -2 3 1 -2\r\n", "output": "5 2\r\n2 5 "}, {"input": "2\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "3\r\n0 -1 0\r\n", "output": "0 1\r\n2 "}, {"input": "3\r\n1 1 1\r\n", "output": "3 0\r\n"}, {"input": "4\r\n-1 1 1 -1\r\n", "output": "2 2\r\n1 4 "}, {"input": "4\r\n-1 1 -1 1\r\n", "output": "2 2\r\n1 3 "}, {"input": "2\r\n-1 -1\r\n", "output": "-2 0\r\n"}, {"input": "3\r\n-1 0 -1\r\n", "output": "-2 0\r\n"}, {"input": "6\r\n-1 3 3 5 5 -1\r\n", "output": "14 0\r\n"}, {"input": "2\r\n-1000000000 -1000000000\r\n", "output": "-2000000000 0\r\n"}, {"input": "3\r\n-1000000000 -1000000000 -1000000000\r\n", "output": "-2000000000 1\r\n3 "}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "3000000000 0\r\n"}, {"input": "10\r\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352\r\n", "output": "-252577666 8\r\n1 4 5 6 7 8 9 10 "}, {"input": "20\r\n-808998072 733614990 579897311 -337992089 579897311 120800519 -337992089 -803027570 733614990 -686536765 733614990 -803027570 -803027570 733614990 120800519 -803027570 -686536765 579897311 -808998072 -686536765\r\n", "output": "4215055101 13\r\n1 4 7 8 10 12 13 15 16 17 18 19 20 "}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main(input_path, output_path, submission_output_path):
with open(input_path) as f:
n = int(f.readline())
a = list(map(int, f.readline().split()))
with open(submission_output_path) as f:
lines = f.readlines()
if not lines:
print(0)
return
sum_line = lines[0].strip().split()
if len(sum_line) != 2:
print(0)
return
try:
sum_claimed, k = map(int, sum_line)
except:
print(0)
return
cut_list = list(map(int, lines[1].split())) if len(lines) > 1 else []
if len(cut_list) != k:
print(0)
return
cut_set = set(cut_list)
if len(cut_set) != k:
print(0)
return
for num in cut_list:
if not (1 <= num <= n):
print(0)
return
remaining = [i-1 for i in range(1, n+1) if i not in cut_set]
if len(remaining) < 2:
print(0)
return
first_val = a[remaining[0]]
last_val = a[remaining[-1]]
if first_val != last_val:
print(0)
return
sum_remaining = sum(a[i] for i in remaining)
if sum_remaining != sum_claimed:
print(0)
return
prefix = [0] * (n + 1)
for i in range(n):
prefix[i+1] = prefix[i] + max(a[i], 0)
groups = defaultdict(list)
for idx, val in enumerate(a):
groups[val].append(idx)
max_total = -float('inf')
for v, indices in groups.items():
m = len(indices)
if m < 2:
continue
indices.sort()
min_val = prefix[indices[0] + 1]
current_max = -float('inf')
for j in range(1, m):
current_i = indices[j]
current_sum = 2 * v + (prefix[current_i] - min_val)
if current_sum > current_max:
current_max = current_sum
if j < m - 1:
next_val = prefix[indices[j] + 1]
if next_val < min_val:
min_val = next_val
if current_max > max_total:
max_total = current_max
if sum_claimed != max_total:
print(0)
else:
print(100)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_output_path = sys.argv[3]
main(input_path, output_path, submission_output_path)
| true
|
66/B
|
66
|
B
|
Python 3
|
TESTS
| 60
| 154
| 307,200
|
110979700
|
def up(hs, i):
while i < len(hs) - 1 and hs[i] <= hs[i+1]:
i += 1
return i
def down(hs, i):
while i < len(hs) - 1 and hs[i] > hs[i+1]:
i += 1
return i
def solve(hs):
if len(hs) == 1:
print(1)
else:
length = -1
index = 0
while index < len(hs)-1:
peak = up(hs, index)
bottom = down(hs, peak)
if length < (bottom - index) + 1:
length = (bottom - index) + 1
index = bottom
print(length)
def main():
n = int(input())
hs = list(map(int, input().split()))
solve(hs)
if __name__ == '__main__':
main()
| 80
| 92
| 0
|
154778493
|
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
import math
import bisect
from math import gcd, floor, sqrt, log
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
inp = lambda: int(input())
strng = lambda: input()
jn = lambda x, l: x.join(map(str, l))
strl = lambda: list(input())
mul = lambda: map(int, input().split())
mulf = lambda: map(float, input().split())
seq = lambda: list(map(int, input().split()))
ceil = lambda x: int(x) if (x == int(x)) else int(x) + 1
ceildiv = lambda x, d: x // d if (x % d == 0) else x // d + 1
flush = lambda: sys.stdout.flush()
stdstr = lambda: sys.stdin.readline()
stdint = lambda: int(sys.stdin.readline())
stdpr = lambda x: sys.stdout.write(str(x))
mod = 1000000007
def main():
n = inp()
heights = seq()
left = [1] * n
right = [1] * n
for i in range(1, n):
if heights[i] >= heights[i - 1]:
left[i] = left[i - 1] + 1
for i in range(n - 2, -1, -1):
if heights[i] >= heights[i + 1]:
right[i] = right[i + 1] + 1
ans = 0
for i in range(n):
ans = max(ans, left[i] + right[i] - 1)
print(ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
|
Codeforces Beta Round 61 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Petya and Countryside
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.
Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture:
As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
|
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
| null | null |
[{"input": "1\n2", "output": "1"}, {"input": "5\n1 2 1 2 1", "output": "3"}, {"input": "8\n1 2 1 1 1 3 3 4", "output": "6"}]
| 1,100
|
["brute force", "implementation"]
| 80
|
[{"input": "1\r\n2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2 1 1 1 3 3 4\r\n", "output": "6\r\n"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10\r\n"}, {"input": "10\r\n10 9 8 7 6 5 4 3 2 1\r\n", "output": "10\r\n"}, {"input": "2\r\n100 100\r\n", "output": "2\r\n"}, {"input": "3\r\n100 100 100\r\n", "output": "3\r\n"}, {"input": "11\r\n1 2 3 4 5 6 5 4 3 2 1\r\n", "output": "11\r\n"}, {"input": "10\r\n1 4 4 4 4 4 1 2 4 3\r\n", "output": "7\r\n"}]
| false
|
stdio
| null | true
|
977/F
|
977
|
F
|
PyPy 3-64
|
TESTS
| 4
| 62
| 16,384,000
|
224707501
|
# https://codeforces.com/problemset/problem/977/F
n = int(input())
splits = input().split()
nums = [int(splits[i]) for i in range(n)]
dp = {}
"""
dp = {
4: (5, 2) length, index of self
}
"""
longest = -1
for i, n in enumerate(nums):
if n - 1 in dp:
prev = dp[n - 1]
length = prev[0] + 1
if length > dp[longest][0]:
dp[n] = (length, i)
else:
dp[n] = (1, i)
length = 1
if longest == -1 or length > dp[longest][0]:
longest = n
size = dp[longest][0]
print(size)
out = []
for _ in range(size):
out.append(str(dp[longest][1] + 1))
longest -= 1
print(" ".join(reversed(out)))
| 70
| 405
| 39,628,800
|
232882756
|
n = int(input())
nums = list(map(int, input().split()))
# 存储以每个数字结尾的最长连续序列长度
seq_len = {}
# 构建连续序列长度的字典
for num in nums:
seq_len[num] = seq_len.get(num - 1, 0) + 1
# 寻找最长序列的结束数字和长度
end, max_len = max(seq_len, key=seq_len.get), max(seq_len.values())
# 回溯找到序列索引
indices = []
while n:
if nums[n - 1] == end:
indices.append(n)
end -= 1
n -= 1
# 反转索引列表并输出
print(max_len)
print(*indices[::-1])
|
Codeforces Round 479 (Div. 3)
|
ICPC
| 2,018
| 2
| 256
|
Consecutive Subsequence
|
You are given an integer array of length $$$n$$$.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $$$[x, x + 1, \dots, x + k - 1]$$$ for some value $$$x$$$ and length $$$k$$$.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $$$[5, 3, 1, 2, 4]$$$ the following arrays are subsequences: $$$[3]$$$, $$$[5, 3, 1, 2, 4]$$$, $$$[5, 1, 4]$$$, but the array $$$[1, 3]$$$ is not.
|
The first line of the input containing integer number $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array. The second line of the input containing $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array itself.
|
On the first line print $$$k$$$ — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
| null |
All valid answers for the first example (as sequences of indices):
- $$$[1, 3, 5, 6]$$$
- $$$[2, 3, 5, 6]$$$
All valid answers for the second example:
- $$$[1, 4]$$$
- $$$[2, 5]$$$
- $$$[3, 6]$$$
All valid answers for the third example:
- $$$[1]$$$
- $$$[2]$$$
- $$$[3]$$$
- $$$[4]$$$
All valid answers for the fourth example:
- $$$[1, 2, 3, 7, 8, 9]$$$
|
[{"input": "7\n3 3 4 7 5 6 8", "output": "4\n2 3 5 6"}, {"input": "6\n1 3 5 2 4 6", "output": "2\n1 4"}, {"input": "4\n10 9 8 7", "output": "1\n1"}, {"input": "9\n6 7 8 3 4 5 9 10 11", "output": "6\n1 2 3 7 8 9"}]
| 1,700
|
["dp"]
| 70
|
[{"input": "7\r\n3 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "6\r\n1 3 5 2 4 6\r\n", "output": "2\r\n1 4 \r\n"}, {"input": "4\r\n10 9 8 7\r\n", "output": "1\r\n1 \r\n"}, {"input": "9\r\n6 7 8 3 4 5 9 10 11\r\n", "output": "6\r\n1 2 3 7 8 9 \r\n"}, {"input": "1\r\n1337\r\n", "output": "1\r\n1 \r\n"}, {"input": "2\r\n456 123\r\n", "output": "1\r\n1 \r\n"}, {"input": "7\r\n100 3 4 7 5 6 8\r\n", "output": "4\r\n2 3 5 6 \r\n"}, {"input": "7\r\n3 3 4 5 6 7 8\r\n", "output": "6\r\n2 3 4 5 6 7 \r\n"}, {"input": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12\r\n", "output": "12\r\n1 2 3 4 5 6 7 8 9 10 11 12 \r\n"}]
| false
|
stdio
|
import sys
def read_file_lines(path):
with open(path, 'r') as f:
return [line.strip() for line in f.readlines()]
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
input_lines = read_file_lines(input_path)
n = int(input_lines[0])
a = list(map(int, input_lines[1].split()))
# Read reference output (to get k_ref)
ref_lines = read_file_lines(output_path)
k_ref = int(ref_lines[0])
# Read submission output
try:
sub_lines = read_file_lines(submission_path)
if len(sub_lines) < 2:
print(0)
return
k_sub = int(sub_lines[0])
indices_sub = list(map(int, sub_lines[1].split()))
except:
print(0)
return
# Check k matches
if k_sub != k_ref:
print(0)
return
# Check indices are valid
if len(indices_sub) != k_sub:
print(0)
return
# Check indices are unique, in range 1..n, and strictly increasing
prev = -1
seen = set()
for idx in indices_sub:
if idx < 1 or idx > n:
print(0)
return
if idx in seen:
print(0)
return
seen.add(idx)
if idx <= prev:
print(0)
return
prev = idx
# Check elements form consecutive increasing sequence
elements = [a[idx - 1] for idx in indices_sub]
if len(elements) < 1:
print(0)
return
x = elements[0]
for i in range(1, len(elements)):
if elements[i] != x + i:
print(0)
return
# All checks passed
print(1)
if __name__ == "__main__":
main()
| true
|
967/B
|
967
|
B
|
PyPy 3
|
TESTS
| 15
| 187
| 9,113,600
|
78524614
|
import math
n, a, b = map(int, input().split())
s = list(map(int, input().split()))
c = 0
x = math.ceil((a * s[0]) / b)
y = s[0]
f = 0
su = 0
for i in s:
su += i
s.sort()
i = n - 1
while su > x:
if s[i] == y and f == 0:
f = 1
i -= 1
continue
su -= s[i]
c += 1
i -= 1
print(c)
| 26
| 77
| 13,516,800
|
193443943
|
import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
[n, a, b] = list(map(int, input().split()))
arr = list(map(int, input().split()))
s1 = arr[0]
arr = sorted(arr[1:], reverse = True)
sActive = s1*((a-b)/b)
total = sum(arr)
ans = 0
for i in range(n-1):
if total <= sActive :
break
else :
total -= arr[i]
ans += 1
print(ans)
solve()
|
Codeforces Round 477 (rated, Div. 2, based on VK Cup 2018 Round 3)
|
CF
| 2,018
| 1
| 256
|
Watering System
|
Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for $$$n$$$ flowers and so it looks like a pipe with $$$n$$$ holes. Arkady can only use the water that flows from the first hole.
Arkady can block some of the holes, and then pour $$$A$$$ liters of water into the pipe. After that, the water will flow out from the non-blocked holes proportionally to their sizes $$$s_1, s_2, \ldots, s_n$$$. In other words, if the sum of sizes of non-blocked holes is $$$S$$$, and the $$$i$$$-th hole is not blocked, $$$\frac{s_i \cdot A}{S}$$$ liters of water will flow out of it.
What is the minimum number of holes Arkady should block to make at least $$$B$$$ liters of water flow out of the first hole?
|
The first line contains three integers $$$n$$$, $$$A$$$, $$$B$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le B \le A \le 10^4$$$) — the number of holes, the volume of water Arkady will pour into the system, and the volume he wants to get out of the first hole.
The second line contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 10^4$$$) — the sizes of the holes.
|
Print a single integer — the number of holes Arkady should block.
| null |
In the first example Arkady should block at least one hole. After that, $$$\frac{10 \cdot 2}{6} \approx 3.333$$$ liters of water will flow out of the first hole, and that suits Arkady.
In the second example even without blocking any hole, $$$\frac{80 \cdot 3}{10} = 24$$$ liters will flow out of the first hole, that is not less than $$$20$$$.
In the third example Arkady has to block all holes except the first to make all water flow out of the first hole.
|
[{"input": "4 10 3\n2 2 2 2", "output": "1"}, {"input": "4 80 20\n3 2 1 4", "output": "0"}, {"input": "5 10 10\n1000 1 1 1 1", "output": "4"}]
| 1,000
|
["math", "sortings"]
| 26
|
[{"input": "4 10 3\r\n2 2 2 2\r\n", "output": "1\r\n"}, {"input": "4 80 20\r\n3 2 1 4\r\n", "output": "0\r\n"}, {"input": "5 10 10\r\n1000 1 1 1 1\r\n", "output": "4\r\n"}, {"input": "10 300 100\r\n20 1 3 10 8 5 3 6 4 3\r\n", "output": "1\r\n"}, {"input": "10 300 100\r\n20 25 68 40 60 37 44 85 23 96\r\n", "output": "8\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 2 2\r\n1 10000\r\n", "output": "1\r\n"}, {"input": "2 10000 1\r\n1 9999\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
71/A
|
71
|
A
|
Python 3
|
TESTS
| 4
| 46
| 0
|
202740786
|
n=int(input())
a=""
z=""
count=0
wordcount=0
while count<n:
x=input()
count+=1
if len(x)<=n:
print(x)
elif len(x)>n:
a=x[0]
z=x[-1::]
for i in range(1,len(x)-1):
wordcount+=1
if wordcount>8:
print(a+str(wordcount)+z)
a=""
z=""
wordcount=0
else:
print(x)
a=""
z=""
wordcount=0
| 20
| 31
| 0
|
183664531
|
x=int(input())
mywords=[]
for i in range(x):
myinput=input()
word=list(myinput)
mywords.append(word)
else:
for word in mywords :
if len(word)>10:
print(word[0],len(word)-2,word[-1],sep="")
else:
y="".join(word)
print(y)
|
Codeforces Beta Round 65 (Div. 2)
|
CF
| 2,011
| 1
| 256
|
Way Too Long Words
|
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
|
The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.
|
Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.
| null | null |
[{"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis", "output": "word\nl10n\ni18n\np43s"}]
| 800
|
["strings"]
| 20
|
[{"input": "4\r\nword\r\nlocalization\r\ninternationalization\r\npneumonoultramicroscopicsilicovolcanoconiosis\r\n", "output": "word\r\nl10n\r\ni18n\r\np43s\r\n"}, {"input": "5\r\nabcdefgh\r\nabcdefghi\r\nabcdefghij\r\nabcdefghijk\r\nabcdefghijklm\r\n", "output": "abcdefgh\r\nabcdefghi\r\nabcdefghij\r\na9k\r\na11m\r\n"}, {"input": "3\r\nnjfngnrurunrgunrunvurn\r\njfvnjfdnvjdbfvsbdubruvbubvkdb\r\nksdnvidnviudbvibd\r\n", "output": "n20n\r\nj27b\r\nk15d\r\n"}, {"input": "1\r\ntcyctkktcctrcyvbyiuhihhhgyvyvyvyvjvytchjckt\r\n", "output": "t41t\r\n"}, {"input": "24\r\nyou\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nunofficially\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n", "output": "you\r\nare\r\nregistered\r\nfor\r\npractice\r\nyou\r\ncan\r\nsolve\r\nproblems\r\nu10y\r\nresults\r\ncan\r\nbe\r\nfound\r\nin\r\nthe\r\ncontest\r\nstatus\r\nand\r\nin\r\nthe\r\nbottom\r\nof\r\nstandings\r\n"}, {"input": "1\r\na\r\n", "output": "a\r\n"}, {"input": "26\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n", "output": "a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz\r\n"}, {"input": "1\r\nabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\r\n", "output": "a98j\r\n"}, {"input": "10\r\ngyartjdxxlcl\r\nfzsck\r\nuidwu\r\nxbymclornemdmtj\r\nilppyoapitawgje\r\ncibzc\r\ndrgbeu\r\nhezplmsdekhhbo\r\nfeuzlrimbqbytdu\r\nkgdco\r\n", "output": "g10l\r\nfzsck\r\nuidwu\r\nx13j\r\ni13e\r\ncibzc\r\ndrgbeu\r\nh12o\r\nf13u\r\nkgdco\r\n"}, {"input": "20\r\nlkpmx\r\nkovxmxorlgwaomlswjxlpnbvltfv\r\nhykasjxqyjrmybejnmeumzha\r\ntuevlumpqbbhbww\r\nqgqsphvrmupxxc\r\ntrissbaf\r\nqfgrlinkzvzqdryckaizutd\r\nzzqtoaxkvwoscyx\r\noswytrlnhpjvvnwookx\r\nlpuzqgec\r\ngyzqfwxggtvpjhzmzmdw\r\nrlxjgmvdftvrmvbdwudra\r\nvsntnjpepnvdaxiporggmglhagv\r\nxlvcqkqgcrbgtgglj\r\nlyxwxbiszyhlsrgzeedzprbmcpduvq\r\nyrmqqvrkqskqukzqrwukpsifgtdc\r\nxpuohcsjhhuhvr\r\nvvlfrlxpvqejngwrbfbpmqeirxlw\r\nsvmasocxdvadmaxtrpakysmeaympy\r\nyuflqboqfdt\r\n", "output": "lkpmx\r\nk26v\r\nh22a\r\nt13w\r\nq12c\r\ntrissbaf\r\nq21d\r\nz13x\r\no17x\r\nlpuzqgec\r\ng18w\r\nr19a\r\nv25v\r\nx15j\r\nl28q\r\ny26c\r\nx12r\r\nv26w\r\ns27y\r\ny9t\r\n"}, {"input": "100\r\nm\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n", "output": "m\r\nz\r\ns\r\nv\r\nd\r\nr\r\nv\r\ny\r\ny\r\ne\r\np\r\nt\r\nc\r\na\r\nn\r\nm\r\np\r\ng\r\ni\r\nj\r\nc\r\na\r\nb\r\nq\r\ne\r\nn\r\nv\r\no\r\nk\r\nx\r\nf\r\ni\r\nl\r\na\r\nq\r\nr\r\nu\r\nb\r\ns\r\nl\r\nc\r\nl\r\ne\r\nv\r\nj\r\nm\r\nx\r\nb\r\na\r\nq\r\nb\r\na\r\nf\r\nj\r\nv\r\nm\r\nq\r\nc\r\nt\r\nt\r\nn\r\nx\r\no\r\ny\r\nr\r\nu\r\nh\r\nm\r\nj\r\np\r\nj\r\nq\r\nz\r\ns\r\nj\r\no\r\ng\r\nc\r\nm\r\nn\r\no\r\nm\r\nr\r\no\r\ns\r\nt\r\nh\r\nr\r\np\r\nk\r\nb\r\nz\r\ng\r\no\r\nc\r\nc\r\nz\r\nz\r\ng\r\nr\r\n"}]
| false
|
stdio
| null | true
|
776/A
|
776
|
A
|
PyPy 3-64
|
TESTS
| 4
| 77
| 3,072,000
|
206252161
|
ans = []
fn = input()
ans.append(fn)
for i in range(int(input())):
fn += " "
kd,new = input().split(" ")
fn = fn.replace(kd+" ",new+" ")
ans.append(fn)
fn = fn[:-1]
for i in ans:
print(i)
| 57
| 46
| 0
|
145987507
|
v = input().split()
print(*v)
for _ in range(0, int(input())):
i = input().split()
if v[0] == i[0]:
v[0] = i[1]
else:
v[1] = i[1]
print(*v)
|
ICM Technex 2017 and Codeforces Round 400 (Div. 1 + Div. 2, combined)
|
CF
| 2,017
| 2
| 256
|
A Serial Killer
|
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
|
First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
|
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
| null |
In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears.
- After day 2, rachel is killed and phoebe appears.
- After day 3, phoebe is killed and monica appears.
- After day 4, monica is killed and chandler appears.
|
[{"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler", "output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"}, {"input": "icm codeforces\n1\ncodeforces technex", "output": "icm codeforces\nicm technex"}]
| 900
|
["brute force", "implementation", "strings"]
| 57
|
[{"input": "ross rachel\r\n4\r\nross joey\r\nrachel phoebe\r\nphoebe monica\r\nmonica chandler\r\n", "output": "ross rachel\r\njoey rachel\r\njoey phoebe\r\njoey monica\r\njoey chandler\r\n"}, {"input": "icm codeforces\r\n1\r\ncodeforces technex\r\n", "output": "icm codeforces\r\nicm technex\r\n"}, {"input": "a b\r\n3\r\na c\r\nb d\r\nd e\r\n", "output": "a b\r\nc b\r\nc d\r\nc e\r\n"}, {"input": "ze udggmyop\r\n4\r\nze szhrbmft\r\nudggmyop mjorab\r\nszhrbmft ojdtfnzxj\r\nojdtfnzxj yjlkg\r\n", "output": "ze udggmyop\r\nszhrbmft udggmyop\r\nszhrbmft mjorab\r\nojdtfnzxj mjorab\r\nyjlkg mjorab\r\n"}, {"input": "q s\r\n10\r\nq b\r\nb j\r\ns g\r\nj f\r\nf m\r\ng c\r\nc a\r\nm d\r\nd z\r\nz o\r\n", "output": "q s\r\nb s\r\nj s\r\nj g\r\nf g\r\nm g\r\nm c\r\nm a\r\nd a\r\nz a\r\no a\r\n"}, {"input": "iii iiiiii\r\n7\r\niii iiiiiiiiii\r\niiiiiiiiii iiii\r\niiii i\r\niiiiii iiiiiiii\r\niiiiiiii iiiiiiiii\r\ni iiiii\r\niiiii ii\r\n", "output": "iii iiiiii\r\niiiiiiiiii iiiiii\r\niiii iiiiii\r\ni iiiiii\r\ni iiiiiiii\r\ni iiiiiiiii\r\niiiii iiiiiiiii\r\nii iiiiiiiii\r\n"}, {"input": "bwyplnjn zkms\r\n26\r\nzkms nzmcsytxh\r\nnzmcsytxh yujsb\r\nbwyplnjn gtbzhudpb\r\ngtbzhudpb hpk\r\nyujsb xvy\r\nhpk wrwnfokml\r\nwrwnfokml ndouuikw\r\nndouuikw ucgrja\r\nucgrja tgfmpldz\r\nxvy nycrfphn\r\nnycrfphn quvs\r\nquvs htdy\r\nhtdy k\r\ntgfmpldz xtdpkxm\r\nxtdpkxm suwqxs\r\nk fv\r\nsuwqxs qckllwy\r\nqckllwy diun\r\nfv lefa\r\nlefa gdoqjysx\r\ndiun dhpz\r\ngdoqjysx bdmqdyt\r\ndhpz dgz\r\ndgz v\r\nbdmqdyt aswy\r\naswy ydkayhlrnm\r\n", "output": "bwyplnjn zkms\r\nbwyplnjn nzmcsytxh\r\nbwyplnjn yujsb\r\ngtbzhudpb yujsb\r\nhpk yujsb\r\nhpk xvy\r\nwrwnfokml xvy\r\nndouuikw xvy\r\nucgrja xvy\r\ntgfmpldz xvy\r\ntgfmpldz nycrfphn\r\ntgfmpldz quvs\r\ntgfmpldz htdy\r\ntgfmpldz k\r\nxtdpkxm k\r\nsuwqxs k\r\nsuwqxs fv\r\nqckllwy fv\r\ndiun fv\r\ndiun lefa\r\ndiun gdoqjysx\r\ndhpz gdoqjysx\r\ndhpz bdmqdyt\r\ndgz bdmqdyt\r\nv bdmqdyt\r\nv aswy\r\nv ydkayhlrnm\r\n"}, {"input": "wxz hbeqwqp\r\n7\r\nhbeqwqp cpieghnszh\r\ncpieghnszh tlqrpd\r\ntlqrpd ttwrtio\r\nttwrtio xapvds\r\nxapvds zk\r\nwxz yryk\r\nzk b\r\n", "output": "wxz hbeqwqp\r\nwxz cpieghnszh\r\nwxz tlqrpd\r\nwxz ttwrtio\r\nwxz xapvds\r\nwxz zk\r\nyryk zk\r\nyryk b\r\n"}, {"input": "wced gnsgv\r\n23\r\ngnsgv japawpaf\r\njapawpaf nnvpeu\r\nnnvpeu a\r\na ddupputljq\r\nddupputljq qyhnvbh\r\nqyhnvbh pqwijl\r\nwced khuvs\r\nkhuvs bjkh\r\npqwijl ysacmboc\r\nbjkh srf\r\nsrf jknoz\r\njknoz hodf\r\nysacmboc xqtkoyh\r\nhodf rfp\r\nxqtkoyh bivgnwqvoe\r\nbivgnwqvoe nknf\r\nnknf wuig\r\nrfp e\r\ne bqqknq\r\nwuig sznhhhu\r\nbqqknq dhrtdld\r\ndhrtdld n\r\nsznhhhu bguylf\r\n", "output": "wced gnsgv\r\nwced japawpaf\r\nwced nnvpeu\r\nwced a\r\nwced ddupputljq\r\nwced qyhnvbh\r\nwced pqwijl\r\nkhuvs pqwijl\r\nbjkh pqwijl\r\nbjkh ysacmboc\r\nsrf ysacmboc\r\njknoz ysacmboc\r\nhodf ysacmboc\r\nhodf xqtkoyh\r\nrfp xqtkoyh\r\nrfp bivgnwqvoe\r\nrfp nknf\r\nrfp wuig\r\ne wuig\r\nbqqknq wuig\r\nbqqknq sznhhhu\r\ndhrtdld sznhhhu\r\nn sznhhhu\r\nn bguylf\r\n"}, {"input": "qqqqqqqqqq qqqqqqqq\r\n3\r\nqqqqqqqq qqqqqqqqq\r\nqqqqqqqqq qqqqq\r\nqqqqq q\r\n", "output": "qqqqqqqqqq qqqqqqqq\r\nqqqqqqqqqq qqqqqqqqq\r\nqqqqqqqqqq qqqqq\r\nqqqqqqqqqq q\r\n"}, {"input": "wwwww w\r\n8\r\nwwwww wwwwwwww\r\nwwwwwwww wwwwwwwww\r\nwwwwwwwww wwwwwwwwww\r\nw www\r\nwwwwwwwwww wwww\r\nwwww ww\r\nwww wwwwww\r\nwwwwww wwwwwww\r\n", "output": "wwwww w\r\nwwwwwwww w\r\nwwwwwwwww w\r\nwwwwwwwwww w\r\nwwwwwwwwww www\r\nwwww www\r\nww www\r\nww wwwwww\r\nww wwwwwww\r\n"}, {"input": "k d\r\n17\r\nk l\r\nd v\r\nv z\r\nl r\r\nz i\r\nr s\r\ns p\r\np w\r\nw j\r\nj h\r\ni c\r\nh m\r\nm q\r\nc o\r\no g\r\nq x\r\nx n\r\n", "output": "k d\r\nl d\r\nl v\r\nl z\r\nr z\r\nr i\r\ns i\r\np i\r\nw i\r\nj i\r\nh i\r\nh c\r\nm c\r\nq c\r\nq o\r\nq g\r\nx g\r\nn g\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
with open(output_path, 'r') as f:
correct_lines = [line.strip() for line in f.readlines()]
with open(submission_path, 'r') as f:
submission_lines = [line.strip() for line in f.readlines()]
if len(submission_lines) != len(correct_lines):
print(0)
return
for sub_line, corr_line in zip(submission_lines, correct_lines):
sub_parts = sub_line.split()
corr_parts = corr_line.split()
if len(sub_parts) != 2 or len(corr_parts) != 2:
print(0)
return
if set(sub_parts) != set(corr_parts):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
235/B
|
235
|
B
|
PyPy 3-64
|
TESTS
| 9
| 122
| 2,355,200
|
148672000
|
n = int(input())
l = list(map(float,input().split()))
a = 0
for i in range(1<<n):
s = bin(i)[2:]
t = 0
ans = 0
p = 1
s = '0'*(n-len(s)) + s
for j in range(len(s)):
if s[j]=='1':
p*=l[j]
t+=1
else:
p*=(1-l[j])
ans+=(t**2)
t = 0
ans+=(t**2)
a += p*ans
print(round(a,5))
| 72
| 154
| 11,468,800
|
206186698
|
n,a,b=int(input()),0,0
for i in map(float,input().split()):a,b=a+i*(1+b*2),i*(b+1)
print(a)
|
Codeforces Round 146 (Div. 1)
|
CF
| 2,012
| 2
| 256
|
Let's Play Osu!
|
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of n characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the i-th (1 ≤ i ≤ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be "O", 1 - pi to be "X". You task is to calculate the expected score for your play.
|
The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 ≤ pi ≤ 1).
There will be at most six digits after the decimal point in the given pi.
|
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
| null |
For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
- "OOO" → 32 = 9;
- "OOX" → 22 = 4;
- "OXO" → 12 + 12 = 2;
- "OXX" → 12 = 1;
- "XOO" → 22 = 4;
- "XOX" → 12 = 1;
- "XXO" → 12 = 1;
- "XXX" → 0.
So the expected score is $${ \frac { 9 + 4 + 2 + 1 + 4 + 1 + 1 } { 8 } } = 2. 7 5$$
|
[{"input": "3\n0.5 0.5 0.5", "output": "2.750000000000000"}, {"input": "4\n0.7 0.2 0.1 0.9", "output": "2.489200000000000"}, {"input": "5\n1 1 1 1 1", "output": "25.000000000000000"}]
| 2,000
|
["dp", "math", "probabilities"]
| 72
|
[{"input": "3\r\n0.5 0.5 0.5\r\n", "output": "2.750000000000000\r\n"}, {"input": "4\r\n0.7 0.2 0.1 0.9\r\n", "output": "2.489200000000000\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "25.000000000000000\r\n"}, {"input": "10\r\n0.684846 0.156794 0.153696 0.714526 0.281868 0.628256 0.745339 0.123854 0.748936 0.856333\r\n", "output": "10.721778814471227\r\n"}, {"input": "10\r\n0.684488 0.834971 0.834886 0.643646 0.162710 0.119851 0.659401 0.743950 0.220986 0.839665\r\n", "output": "15.401334613504345\r\n"}, {"input": "10\r\n0.684416 0.170607 0.491124 0.469470 0.458879 0.658170 0.322214 0.707969 0.275396 0.836331\r\n", "output": "11.404416796704293\r\n"}, {"input": "10\r\n0.684631 0.563700 0.722410 0.191998 0.370373 0.643213 0.533776 0.815911 0.112166 0.846332\r\n", "output": "12.888929008957161\r\n"}, {"input": "10\r\n0.684559 0.699336 0.378648 0.817822 0.666542 0.381532 0.196589 0.779930 0.166576 0.842998\r\n", "output": "14.036752909261951\r\n"}, {"input": "10\r\n0.999453 0.999188 0.998398 0.999609 0.999113 0.999426 0.998026 0.999244 0.998842 0.999807\r\n", "output": "99.590738622894690\r\n"}, {"input": "10\r\n0.000733 0.000769 0.000772 0.000595 0.000930 0.000395 0.000596 0.000584 0.000496 0.000905\r\n", "output": "0.006782723279203\r\n"}]
| false
|
stdio
|
import sys
def read_number(filepath):
with open(filepath, 'r') as f:
lines = f.readlines()
for line in lines:
stripped = line.strip()
if stripped:
return float(stripped)
return 0.0
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
expected = read_number(output_path)
submission = read_number(submission_path)
abs_diff = abs(submission - expected)
if abs_diff <= 1e-6:
print(100)
return
if expected == 0:
print(0)
return
rel_diff = abs_diff / abs(expected)
print(100 if rel_diff <= 1e-6 else 0)
if __name__ == "__main__":
main()
| true
|
66/B
|
66
|
B
|
PyPy 3
|
TESTS
| 57
| 154
| 0
|
144020468
|
n = int(input())
heights = [int(i) for i in input().split()]
local = []
p1 = 0
p2 = 0
p3 = 0
for i in range(n):
if p1 + 1 < n and heights[p1] > heights[p1 + 1]:
p1 += 1
if p2 + 1 < n and heights[p2] <= heights[p2 + 1]:
p2 += 1
p3 += 1
elif p3 + 1 < n and heights[p3] >= heights[p3 + 1]:
p3 += 1
else:
local.append(p3 - p1 + 1)
p1 = p2
p2 = p3
# print(p1, p2, p3)
local.append(p3 - p1 + 1)
print(max(local))
| 80
| 92
| 0
|
154981545
|
# 1 2 1 1 1 3 3 4
# is index i greater than or equal to the one in its RHS?
# if yes add 1 to the arr1[i+1] .. which is the sum of the RHS element
# arr1 =[0,3,2,1,0,1,0,0]
# is index i greater than equal or to the one in its RHS?
# if yes add 1 to the arr1[i-1] .. which is the sum of the RHS element
# arr2 =[0,1,0,1,2,3,4,5]
# arr3[0,4,2,2,2,4,4,5]
n=int(input())
# height = [int(num) for num in input().split(" ", n-1)]
height = list(map(int, input().split()))
leftCount=[0]*n
rightCount=[0]*n
for i in range(n):
if i ==0 or height[i]< height[i-1]:
leftCount[i]=0
else: # height[i]>= height[i-1]
leftCount[i]=leftCount[i-1]+1
RHSn=n-1-i
if RHSn==n-1 or height[RHSn]<height[RHSn+1]:
rightCount[RHSn]=0
else:
rightCount[RHSn]=rightCount[RHSn+1]+1
total=[]
for (item1, item2) in zip(leftCount, rightCount):
total.append(item1+item2)
print(max(total)+1)
|
Codeforces Beta Round 61 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Petya and Countryside
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.
Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture:
As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
|
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
| null | null |
[{"input": "1\n2", "output": "1"}, {"input": "5\n1 2 1 2 1", "output": "3"}, {"input": "8\n1 2 1 1 1 3 3 4", "output": "6"}]
| 1,100
|
["brute force", "implementation"]
| 80
|
[{"input": "1\r\n2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 1 2 1\r\n", "output": "3\r\n"}, {"input": "8\r\n1 2 1 1 1 3 3 4\r\n", "output": "6\r\n"}, {"input": "10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "10\r\n"}, {"input": "10\r\n10 9 8 7 6 5 4 3 2 1\r\n", "output": "10\r\n"}, {"input": "2\r\n100 100\r\n", "output": "2\r\n"}, {"input": "3\r\n100 100 100\r\n", "output": "3\r\n"}, {"input": "11\r\n1 2 3 4 5 6 5 4 3 2 1\r\n", "output": "11\r\n"}, {"input": "10\r\n1 4 4 4 4 4 1 2 4 3\r\n", "output": "7\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.