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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
607/A
|
607
|
A
|
Python 3
|
TESTS
| 10
| 873
| 15,974,400
|
81619958
|
n=int(input())
arr=[0 for i in range(1000001)]
m=0
for i in range(n):
a,b=map(int,input().split())
if a>m:
m=a
arr[a]=b
dp=[0 for i in range(m+1)]
for i in range(m+1):
if i==0:
if arr[i]==0:
dp[i]=0
else:
dp[i]=1
else:
if arr[i]==0:
dp[i]=dp[i-1]
elif arr[i]>=i:
dp[i]=1
else:
dp[i]=max(1+dp[i-arr[i]-1],dp[i-1])
print(n-max(dp))
| 41
| 280
| 9,523,200
|
187487591
|
from sys import stdin
input = stdin.readline
n = int(input())
c = [0]*(10**6+5)
for _ in range(n):
a, b = [int(x) for x in input().split()]
c[a] = b
for i in range(10**6+1):
if c[i]:
if c[i] >= i: c[i] = 1
else: c[i] = c[i-c[i]-1]+1
else:
c[i] = c[i-1]
print(n-max(c))
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
789/A
|
789
|
A
|
PyPy 3
|
TESTS
| 27
| 171
| 10,547,200
|
51799493
|
n, k = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
ch = 0
for i in A:
cnt += (i + k - 1) // k
ch = max(ch, (i + k - 1) // k)
print(max(ch, (cnt + 1) // 2))
| 31
| 77
| 13,721,600
|
216209898
|
import math
mode, capacity = map(int, input().split())
stones = list(map(int, input().split()))
pocket = 0
for i in range(len(stones)):
pocket += math.ceil(stones[i] / capacity)
print(math.ceil(pocket / 2))
|
Codeforces Round 407 (Div. 2)
|
CF
| 2,017
| 1
| 256
|
Anastasia and pebbles
|
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
|
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
|
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
| null |
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
|
[{"input": "3 2\n2 3 4", "output": "3"}, {"input": "5 4\n3 1 8 9 7", "output": "5"}]
| 1,100
|
["implementation", "math"]
| 31
|
[{"input": "3 2\r\n2 3 4\r\n", "output": "3\r\n"}, {"input": "5 4\r\n3 1 8 9 7\r\n", "output": "5\r\n"}, {"input": "1 22\r\n1\r\n", "output": "1\r\n"}, {"input": "3 57\r\n78 165 54\r\n", "output": "3\r\n"}, {"input": "5 72\r\n74 10 146 189 184\r\n", "output": "6\r\n"}, {"input": "9 13\r\n132 87 200 62 168 51 185 192 118\r\n", "output": "48\r\n"}, {"input": "1 1\r\n10000\r\n", "output": "5000\r\n"}, {"input": "10 1\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2 2\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
789/A
|
789
|
A
|
Python 3
|
TESTS
| 27
| 124
| 11,878,400
|
25941711
|
import math
n, k = list(map(int,input().split(" ")))
w = list(map(lambda x: math.ceil(int(x)/k),input().split(" ")))
# print(w)
ans = math.ceil(max(max(w), sum(w)/2))
print(ans)
| 31
| 77
| 13,926,400
|
165484947
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = list(map(int, input().split()))
c = 0
for i in range(n):
c = c + 1 + a[i] // k if a[i] % k else c + a[i] // k
c = c // 2 if c % 2 == 0 else c // 2 + 1
print(c)
|
Codeforces Round 407 (Div. 2)
|
CF
| 2,017
| 1
| 256
|
Anastasia and pebbles
|
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
|
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
|
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
| null |
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
|
[{"input": "3 2\n2 3 4", "output": "3"}, {"input": "5 4\n3 1 8 9 7", "output": "5"}]
| 1,100
|
["implementation", "math"]
| 31
|
[{"input": "3 2\r\n2 3 4\r\n", "output": "3\r\n"}, {"input": "5 4\r\n3 1 8 9 7\r\n", "output": "5\r\n"}, {"input": "1 22\r\n1\r\n", "output": "1\r\n"}, {"input": "3 57\r\n78 165 54\r\n", "output": "3\r\n"}, {"input": "5 72\r\n74 10 146 189 184\r\n", "output": "6\r\n"}, {"input": "9 13\r\n132 87 200 62 168 51 185 192 118\r\n", "output": "48\r\n"}, {"input": "1 1\r\n10000\r\n", "output": "5000\r\n"}, {"input": "10 1\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2 2\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
PyPy 3
|
TESTS
| 30
| 372
| 21,401,600
|
132846848
|
n,k=map(int,input().split())
maximum=-99999999
for i in range(n):
final_time,value_time=map(int,input().split())
if value_time>k:
final_time-=(value_time-k)
maximum=max(maximum,final_time)
print(maximum)
| 35
| 92
| 0
|
154053467
|
n, k = map(int, input().split())
c = -10**10
for _ in range(n):
f, t = map(int, input().split())
if t > k:
f = (f - t + k)
if f > c:
c = f
print(c)
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
702/B
|
702
|
B
|
Python 3
|
TESTS
| 10
| 311
| 12,902,400
|
227580935
|
generate=[]
for i in range(30):
generate.append(1<<i)
def solve():
len1=int(input())
list1=list(map(int,input().split()))
res=0
dict1={list1[0]:1}
for i in range(1,len1):
for j in generate:
if j-list1[i] in dict1:
res+=dict1[j-list1[i]]
if list1[i] in dict1:dict1[list1[i]]+=1
else:dict1[list1[i]]=1
return res
print(solve())
| 43
| 155
| 17,100,800
|
185272711
|
def start():
c=0
n=int(input())
a=list(map(int,input().split()))
d={}
s=[1]
while s[-1]<10**9:s.append(s[-1]*2)
for i in a:
for j in s:
if j-i in d:
c+=d[j-i]
if i in d:d[i]+=1
else:d[i]=1
print(c)
start()
|
Educational Codeforces Round 15
|
ICPC
| 2,016
| 3
| 256
|
Powers of Two
|
You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2x).
|
The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).
|
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.
| null |
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer.
|
[{"input": "4\n7 3 2 1", "output": "2"}, {"input": "3\n1 1 1", "output": "3"}]
| 1,500
|
["brute force", "data structures", "implementation", "math"]
| 43
|
[{"input": "4\r\n7 3 2 1\r\n", "output": "2\r\n"}, {"input": "3\r\n1 1 1\r\n", "output": "3\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "10\r\n2827343 1373647 96204862 723505 796619138 71550121 799843967 5561265 402690754 446173607\r\n", "output": "2\r\n"}, {"input": "10\r\n6 6 7 3 9 14 15 7 2 2\r\n", "output": "9\r\n"}, {"input": "100\r\n3 6 12 1 16 4 9 5 4 4 5 8 12 4 6 14 5 1 2 2 2 1 7 1 9 10 6 13 7 8 3 11 8 11 7 5 15 6 14 10 4 2 10 9 1 8 14 9 5 11 3 4 1 12 6 8 13 4 8 5 4 13 13 1 3 9 14 7 14 10 7 3 12 8 9 8 6 15 9 10 12 14 15 4 16 8 8 4 8 7 5 10 16 4 10 13 6 16 16 5\r\n", "output": "532\r\n"}, {"input": "1\r\n2\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
PyPy 3
|
TESTS
| 10
| 1,247
| 14,336,000
|
15149542
|
import sys
n = int(input())
a = [[0,0] for i in range(0,n)]
f = [0]*n
for i in range(0, n):
inp = input().split()
a[i][0],a[i][1] = int(inp[0]), int(inp[1])
best = n
left = 0
a = sorted(a)
for i in range(0,n):
while left+1<n and a[i][0]-a[i][1]>a[left+1][0]:
left+=1
f[i] = f[left] + i-left-bool(a[i][0]-a[i][1]> a[left][0] or left == i)
best = min(best, n-i + f[i])
print(best)
| 41
| 311
| 15,974,400
|
169177946
|
import sys
import math
import collections
from heapq import heappush, heappop
input = sys.stdin.readline
ints = lambda: list(map(int, input().split()))
n = int(input())
beacons = []
for i in range(n):
a, b = ints()
beacons.append((a, b))
beacons.sort(key=lambda x: x[0])
def bsearch(x):
l, r = 0, n - 1
while l <= r:
m = (l + r) // 2
if beacons[m][0] == x:
return m
elif beacons[m][0] < x:
l = m + 1
else:
r = m - 1
return l
dp = [0] * n
for i in range(n):
limit = bsearch(beacons[i][0] - beacons[i][1])
dp[i] += i - limit
if limit > 0:
dp[i] += dp[limit - 1]
ans = math.inf
for i in range(n + 1):
destroyed = n - i
if i > 0:
destroyed += dp[i - 1]
ans = min(ans, destroyed)
print(ans)
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
PyPy 3
|
TESTS
| 10
| 1,185
| 17,920,000
|
77082235
|
import bisect
from collections import OrderedDict
n=int(input())
l,x=[0]*n,[]
d1={}
for i in range(n):
a,b=map(int,input().split())
d1[a]=b
d=OrderedDict(sorted(d1.items()))
i=0
for a in d:
if len(x)>0:
j=bisect.bisect_left(x,a-d[a])
l[i]=min(len(x)-j+l[j-1],l[i-1]+1)
x.append(a);i+=1
print(l[-1])
| 41
| 327
| 25,907,200
|
155265758
|
from math import inf
from collections import *
import math, os, sys, heapq, bisect, random,threading
from functools import lru_cache
from itertools import *
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())
M,M1=1000000007,998244353
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=inpu()
b=[0]*(10**6+2)
for i in range(n):
a,c=sep()
b[a] = c
dp=[0]*(10**6+2)
for i in range(10**6+1):
if b[i]==0:
dp[i] = dp[i-1]
else:
if b[i]>=i:
dp[i] = 1
else:
dp[i] = dp[i-b[i]-1]+1
print(n-max(dp))
if __name__ == '__main__':
main()
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
698/B
|
698
|
B
|
PyPy 3
|
TESTS
| 0
| 61
| 0
|
112882313
|
class DSU(object):
def __init__(self, n):
self.p = list(range(n))
self.rk = [0] * n
def find(self, u):
while u != self.p[u]:
u = self.p[u]
return u
def unite(self, u, v):
u = self.find(u)
v = self.find(v)
if u == v:
return
if self.rk[u] < self.rk[v]:
u, v = v, u
self.p[v] = u
self.rk[u] += self.rk[u] == self.rk[v]
n = int(input())
p = list(map(int, input().split()))
dsu = DSU(n)
s = None
for u in range(n):
p[u] -= 1
if p[u] == u:
s = u
for u in range(n):
dsu.unite(u, p[u])
ans = 0
rt = [None] * n
vis = [False] * n
for u in range(n):
if dsu.find(u) != u:
continue
v = u
while not vis[v]:
vis[v] = True
v = p[v]
rt[u] = v
if p[v] != v:
p[v] = v
if s is None:
ans += 1
s = v
for u in range(n):
if u != dsu.find(s) and u == dsu.find(u):
p[u] = s
ans += 1
print(ans)
for u in range(n):
p[u] += 1
print(*p)
| 101
| 421
| 18,944,000
|
99957079
|
# Why do we fall ? So we can learn to pick ourselves up.
root = -1
def find(i,time):
parent[i] = time
while not parent[aa[i]]:
i = aa[i]
parent[i] = time
# print(parent,"in",i)
if parent[aa[i]] == time:
global root
if root == -1:
root = i
if aa[i] != root:
aa[i] = root
global ans
ans += 1
n = int(input())
aa = [0]+[int(i) for i in input().split()]
parent = [0]*(n+1)
ans = 0
time = 0
for i in range(1,n+1):
if aa[i] == i:
root = i
break
for i in range(1,n+1):
if not parent[i]:
# print(i,"pp")
time += 1
find(i,time)
# print(parent)
print(ans)
print(*aa[1:])
|
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
|
276/A
|
276
|
A
|
Python 3
|
TESTS
| 30
| 124
| 0
|
170964062
|
n, k = map(int, input().split())
ud = -3200000
for i in range(n):
f, t = map(int, input().split())
if t > k:
f = f -(t - k)
if f > ud:
ud = f
print(ud)
| 35
| 92
| 0
|
158673847
|
a=[int(x) for x in input().split()]
d=[]
for i in range(a[0]):
b=[int(x) for x in input().split()]
if(b[1]>a[1]):
d.append(b[0]-(b[1]-a[1]))
else:
d.append(b[0])
print(max(d))
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
PyPy 3-64
|
TESTS
| 30
| 310
| 6,144,000
|
208940976
|
def main():
n, k = list(map(int, input().split()))
ans = -100000000
for i in range(n):
f, t = list(map(int, input().split()))
if t > k:
ans = max(ans, f - (t - k))
else:
ans = max(ans, f)
print(ans)
if __name__ == "__main__":
main()
| 35
| 92
| 0
|
159096667
|
import sys
k =input().split()
n,m = int(k[0]),int(k[1])
j = -999999999999
for i in range(n):
a = [int(a) for a in input().split()]
if a[1] <= m:
l=a[0]
else:
l=a[0]-(a[1]-m)
if l > j:
j=l
print(j)
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
276/A
|
276
|
A
|
PyPy 3-64
|
TESTS
| 30
| 312
| 6,963,200
|
172644917
|
n,k=map(int,input().split())
l=[]
for _ in range(n):
l.append(list(map(int,input().split())))
ans=-99999999
for i in l:
if i[1]>k:
ans=max(ans,i[0]-i[1]+k)
else:
ans=max(ans,i[0])
print(ans)
| 35
| 92
| 0
|
161479743
|
a,b=map(int,input().split())
lst=[]
for i in range(a):
c,d=map(int,input().split())
if d>b:
lst.append(c-(d-b))
else:
lst.append(c)
print(max(lst))
|
Codeforces Round 169 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Lunch Rush
|
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
|
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
| null | null |
[{"input": "2 5\n3 3\n4 5", "output": "4"}, {"input": "4 6\n5 8\n3 6\n2 3\n2 2", "output": "3"}, {"input": "1 5\n1 7", "output": "-1"}]
| 900
|
["implementation"]
| 35
|
[{"input": "2 5\r\n3 3\r\n4 5\r\n", "output": "4\r\n"}, {"input": "4 6\r\n5 8\r\n3 6\r\n2 3\r\n2 2\r\n", "output": "3\r\n"}, {"input": "1 5\r\n1 7\r\n", "output": "-1\r\n"}, {"input": "4 9\r\n10 13\r\n4 18\r\n13 3\r\n10 6\r\n", "output": "13\r\n"}, {"input": "1 1\r\n1 1000000000\r\n", "output": "-999999998\r\n"}, {"input": "1 1\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000000 1\r\n", "output": "1000000000\r\n"}, {"input": "2 3\r\n1000000000 1\r\n2 2\r\n", "output": "1000000000\r\n"}, {"input": "2 5\r\n1 7\r\n1 1000000000\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
272/B
|
272
|
B
|
PyPy 3
|
TESTS
| 3
| 216
| 21,401,600
|
127216482
|
f = [0 for _ in range(2*10**3+20)]
n = int(input())
a = list(map(int, input().split()))
for x in range(10**3+2):
f[2 * x] = f[x]
f[2 * x + 1] = f[x] + 1
def find_value(x):
while x > 1000:
if x % 2 == 0:
x = x // 2
else:
x -= 1
x // 2
return f[x]
m = [find_value(y) for y in a]
d = {}
for el in m:
if el in d:
d[el] += 1
else:
d[el] = 1
total = 0
for key in d:
total += d[key] * (d[key] - 1) // 2
print(total)
| 42
| 218
| 10,444,800
|
211338092
|
n=int(input())
z=10**5
a=[0]*z
for x in map(int,input().split()):
a[bin(x).count("1")]+=1
ans=0
for x in a:
ans+=x*(x-1)//2
print(ans)
|
Codeforces Round 167 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Sequence
|
Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence:
- f(0) = 0;
- f(2·x) = f(x);
- f(2·x + 1) = f(x) + 1.
Dima wonders, how many pairs of indexes (i, j) (1 ≤ i < j ≤ n) are there, such that f(ai) = f(aj). Help him, count the number of such pairs.
|
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).
The numbers in the lines are separated by single spaces.
|
In a single line print the answer to the problem.
Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample any pair (i, j) will do, so the answer is 3.
In the second sample only pair (1, 2) will do.
|
[{"input": "3\n1 2 4", "output": "3"}, {"input": "3\n5 3 1", "output": "1"}]
| 1,400
|
["implementation", "math"]
| 42
|
[{"input": "3\r\n1 2 4\r\n", "output": "3\r\n"}, {"input": "3\r\n5 3 1\r\n", "output": "1\r\n"}, {"input": "2\r\n469264357 996569493\r\n", "output": "0\r\n"}, {"input": "6\r\n396640239 62005863 473635171 329666981 510631133 207643327\r\n", "output": "2\r\n"}, {"input": "8\r\n851991424 32517099 310793856 776130403 342626527 58796623 49544509 517126753\r\n", "output": "2\r\n"}, {"input": "7\r\n481003311 553247971 728349004 258700257 916143165 398096105 412826266\r\n", "output": "2\r\n"}, {"input": "4\r\n363034183 741262741 657823174 453546052\r\n", "output": "1\r\n"}, {"input": "8\r\n7 1 2 7 6 8 6 5\r\n", "output": "7\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n7 1\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
698/B
|
698
|
B
|
Python 3
|
TESTS
| 4
| 31
| 0
|
145139766
|
n = int(input())
arr = list(map(int,input().split()))
arr.insert(0,0)
par = [i for i in range(0,n+1)]
def find(i):
if i!=par[i]:
par[i] = find(par[i])
return par[i]
def merge(x,y):
if x == y:return True
a = find(x)
b =find(y)
if a == b:
return False
par[a] = b
return True
ans = 0
out = [0]*(len(arr))
for i in range(1,len(arr)):
temp = merge(i,arr[i])
if temp:
out[i] =arr[i]
temp = []
for i in range(1,n+1):
if par[i] == i:
temp.append(i)
ans+=1
ans-=1
print(ans)
for i in range(1,len(temp)):
out[temp[i-1]] = temp[i]
print(*out[1:])
| 101
| 421
| 22,220,800
|
94390383
|
# from debug import debug
import sys; input = sys.stdin.readline
n = int(input())
lis = [0, *map(int , input().split())]
v = [0]*(n+1)
cycles = set()
roots = set()
for i in range(1, n+1):
if v[i] == 0:
node = i
while v[node] == 0:
v[node] = 1
node = lis[node]
if v[node] == 2: continue
start = node
ignore = 0
l = 1
while lis[node] != start:
if v[node] == 2: ignore = 1; break
v[node] = 2
node = lis[node]
l+=1
if ignore: continue
v[node] = 2
if l == 1: roots.add(node)
else: cycles.add(node)
ans = 0
if roots:
base = roots.pop()
for i in roots: lis[i] = base; ans+=1
for i in cycles: lis[i] = base; ans+=1
elif cycles:
base = cycles.pop()
cycles.add(base)
for i in roots: lis[i] = base; ans+=1
for i in cycles: lis[i] = base; ans+=1
print(ans)
print(*lis[1:])
|
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
|
33/A
|
33
|
A
|
Python 3
|
TESTS
| 4
| 92
| 0
|
142031155
|
n, m, k = [int(item) for item in (input().split(' '))]
_sum = 0
_dict = {}
for i in range(n):
_row, health = [int(item) for item in (input().split(' '))]
_dict[_row] = health
if _dict.__contains__(_row) and _dict[_row] > health or _dict.__contains__(_row):
_dict[_row] = health
for i in range(1,len(_dict)+1):
_sum += _dict[i]
print(k if _sum > k else _sum)
| 31
| 92
| 0
|
225573474
|
n, m, k = [int(item) for item in input().split()]
sum = 0
cont = {}
for _len in range(0, n, 1):
row, health = [int(item) for item in input().split()]
if(cont.get(row) != None and cont[row] >= health or cont.get(row) == None):
cont[row] = health
for [key, val] in cont.items():
sum += val
print((lambda: sum, lambda: k)[sum > k]())
# (sum < k)? cout << sum: cout << 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
|
789/A
|
789
|
A
|
Python 3
|
TESTS
| 26
| 311
| 11,878,400
|
25919694
|
""" Created by Shahen Kosyan on 3/29/17 """
if __name__ == "__main__":
n, k = map(int, input().split())
w = [int(x) for x in input().split()]
# w = sorted(w)
days = 0
i = 0
pockets = 0
while i < len(w):
if w[i] >= 2 * k:
d = w[i] // (2 * k)
days += d
w[i] -= 2 * d * k
if 0 < w[i] < 2 * k and pockets < 2:
w[i] -= k
pockets += 1
if w[i] <= 0:
i += 1
if pockets >= 2 or (i == len(w) and w[i - 1] <= 0):
pockets = 0
days += 1
print(days)
| 31
| 77
| 17,612,800
|
184007780
|
s = input().split()
n, k = int(s[0]), int(s[1])
l = []
s = input().split()
for i in range(n):
l.append(int(s[i]))
a = []
for i in range(n):
a.append((int(l[i] / k), l[i] % k))
fullDays = 0
modDays = 0
for i in range(n):
if a[i][0]:
fullDays += a[i][0]
if a[i][1]:
modDays += 1
if fullDays % 2 == 0 and modDays % 2 == 0:
print(int(fullDays / 2) + int(modDays / 2))
else:
print(int(fullDays / 2) + int(modDays / 2) + 1)
|
Codeforces Round 407 (Div. 2)
|
CF
| 2,017
| 1
| 256
|
Anastasia and pebbles
|
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
|
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
|
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
| null |
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
|
[{"input": "3 2\n2 3 4", "output": "3"}, {"input": "5 4\n3 1 8 9 7", "output": "5"}]
| 1,100
|
["implementation", "math"]
| 31
|
[{"input": "3 2\r\n2 3 4\r\n", "output": "3\r\n"}, {"input": "5 4\r\n3 1 8 9 7\r\n", "output": "5\r\n"}, {"input": "1 22\r\n1\r\n", "output": "1\r\n"}, {"input": "3 57\r\n78 165 54\r\n", "output": "3\r\n"}, {"input": "5 72\r\n74 10 146 189 184\r\n", "output": "6\r\n"}, {"input": "9 13\r\n132 87 200 62 168 51 185 192 118\r\n", "output": "48\r\n"}, {"input": "1 1\r\n10000\r\n", "output": "5000\r\n"}, {"input": "10 1\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2 2\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
621/C
|
621
|
C
|
Python 3
|
TESTS
| 3
| 31
| 0
|
205752948
|
n,m=map(int,input().split())
l=[]
prop=1
for i in range(n):
a,b=map(int,input().split())
y=0
prop*=(b-a+1)
if a%m==0:
a-=1
y=1
div=b//m-a//m
if y:
a+=1
ndiv=(b-a)+1-div
l.append([ndiv,div])
suq=0
le=len(l)
pq1=1
for i in range(le):
re=(l[i][1])*(prop//sum(l[i]))
tr=(prop//sum(l[(i+1)%le]))//sum(l[i])
fe=tr*l[(i+1)%le][1]*l[i][0]
suq+=re+fe
print((suq*(n-1)*1000)/prop)
| 94
| 124
| 9,318,400
|
218386639
|
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations
from bisect import *
from heapq import *
from math import ceil,gcd,lcm,floor,comb
alph = 'abcdefghijklmnopqrstuvwxyz'
#pow(x,mod-2,mod)
N,P = map(int,input().split())
k = []
for i in range(N):
l,r = map(int,input().split())
num = r//P-(l-1)//P
k.append(num/(r-l+1))
val = 0
for i in range(N):
val+=4*k[i]
val-=2*k[i]*k[i-1]
print(1000*val)
|
Codeforces Round 341 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Wet Shark and Flowers
|
There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.
Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it's favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.
At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.
|
The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime.
The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.
|
Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
| null |
A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.
Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows:
1. (1, 420, 420420): note that s0·s1 = 420, s1·s2 = 176576400, and s2·s0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars.
2. (1, 420, 420421): now, the product s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1 will receive 2000. The total is 4000.
3. (1, 421, 420420): total is 4000
4. (1, 421, 420421): total is 0.
5. (2, 420, 420420): total is 6000.
6. (2, 420, 420421): total is 6000.
7. (2, 421, 420420): total is 6000.
8. (2, 421, 420421): total is 4000.
The expected value is $${ \frac { 6 0 0 0 + 4 0 0 0 + 4 0 0 0 + 0 + 6 0 0 0 + 6 0 0 0 + 6 0 0 0 + 4 0 0 0 } { 8 } } = 4 5 0 0$$.
In the second sample, no combination of quantities will garner the sharks any money.
|
[{"input": "3 2\n1 2\n420 421\n420420 420421", "output": "4500.0"}, {"input": "3 5\n1 4\n2 3\n11 14", "output": "0.0"}]
| 1,700
|
["combinatorics", "math", "number theory", "probabilities"]
| 94
|
[{"input": "3 2\r\n1 2\r\n420 421\r\n420420 420421\r\n", "output": "4500.0\r\n"}, {"input": "3 5\r\n1 4\r\n2 3\r\n11 14\r\n", "output": "0.0\r\n"}, {"input": "3 3\r\n3 3\r\n2 4\r\n1 1\r\n", "output": "4666.666666666667\r\n"}, {"input": "5 5\r\n5 204\r\n420 469\r\n417 480\r\n442 443\r\n44 46\r\n", "output": "3451.25\r\n"}, {"input": "3 2\r\n2 2\r\n3 3\r\n4 4\r\n", "output": "6000.0\r\n"}, {"input": "6 7\r\n8 13\r\n14 14\r\n8 13\r\n14 14\r\n8 13\r\n14 14\r\n", "output": "12000.0\r\n"}, {"input": "3 7\r\n7 14\r\n700000000 700000007\r\n420 4200\r\n", "output": "2304.2515207617034\r\n"}, {"input": "5 999999937\r\n999999935 999999936\r\n999999937 999999938\r\n999999939 999999940\r\n999999941 999999942\r\n999999943 999999944\r\n", "output": "2000.0\r\n"}, {"input": "5 999999937\r\n1 999999936\r\n1 999999936\r\n1 999999936\r\n1 999999936\r\n1 999999936\r\n", "output": "0.0\r\n"}, {"input": "20 999999937\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n999999936 999999937\r\n999999937 999999938\r\n", "output": "30000.0\r\n"}, {"input": "9 41\r\n40 42\r\n42 44\r\n44 46\r\n82 84\r\n82 83\r\n80 83\r\n40 83\r\n40 82\r\n42 82\r\n", "output": "5503.274377352654\r\n"}, {"input": "3 2\r\n1 1\r\n1 2\r\n1 1\r\n", "output": "2000.0\r\n"}, {"input": "12 3\r\n697806 966852\r\n802746 974920\r\n579567 821770\r\n628655 642480\r\n649359 905832\r\n87506 178848\r\n605628 924780\r\n843338 925533\r\n953514 978612\r\n375312 997707\r\n367620 509906\r\n277106 866177\r\n", "output": "13333.518289809368\r\n"}, {"input": "5 3\r\n67050 461313\r\n927808 989615\r\n169239 201720\r\n595515 756354\r\n392844 781910\r\n", "output": "5555.597086312073\r\n"}, {"input": "6 7\r\n984774 984865\r\n720391 916269\r\n381290 388205\r\n628383 840455\r\n747138 853964\r\n759705 959629\r\n", "output": "3215.6233297395006\r\n"}, {"input": "3 5\r\n99535 124440\r\n24114 662840\r\n529335 875935\r\n", "output": "2160.11317825774\r\n"}, {"input": "4 3\r\n561495 819666\r\n718673 973130\r\n830124 854655\r\n430685 963699\r\n", "output": "4444.521972611004\r\n"}, {"input": "10 3\r\n311664 694971\r\n364840 366487\r\n560148 821101\r\n896470 923613\r\n770019 828958\r\n595743 827536\r\n341418 988218\r\n207847 366132\r\n517968 587855\r\n168695 878142\r\n", "output": "11110.602699850484\r\n"}, {"input": "11 3\r\n66999 737907\r\n499872 598806\r\n560583 823299\r\n579017 838419\r\n214308 914576\r\n31820 579035\r\n373821 695652\r\n438988 889317\r\n181332 513682\r\n740575 769488\r\n597348 980891\r\n", "output": "12222.259608784536\r\n"}, {"input": "12 3\r\n158757 341790\r\n130709 571435\r\n571161 926255\r\n851779 952236\r\n914910 941369\r\n774359 860799\r\n224067 618483\r\n411639 902888\r\n264423 830336\r\n33133 608526\r\n951696 976379\r\n923880 968563\r\n", "output": "13333.377729413933\r\n"}, {"input": "9 2\r\n717582 964152\r\n268030 456147\r\n400022 466269\r\n132600 698200\r\n658890 807357\r\n196658 849497\r\n257020 380298\r\n267729 284534\r\n311978 917744\r\n", "output": "13500.015586135814\r\n"}, {"input": "10 7\r\n978831 984305\r\n843967 844227\r\n454356 748444\r\n219513 623868\r\n472997 698189\r\n542337 813387\r\n867615 918554\r\n413076 997267\r\n79310 138855\r\n195703 296681\r\n", "output": "5303.027968302269\r\n"}]
| false
|
stdio
|
import sys
def read_number(path):
with open(path, 'r') as f:
lines = [line.strip() for line in f.readlines() if line.strip()]
if len(lines) != 1:
return None
try:
return float(lines[0])
except ValueError:
return None
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
a = read_number(submission_output_path)
b = read_number(correct_output_path)
if a is None or b is None:
print(0)
return
error = abs(a - b)
denominator = max(1.0, b)
if error <= 1e-6 * denominator:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
620/D
|
620
|
D
|
PyPy 3-64
|
TESTS
| 8
| 139
| 14,848,000
|
170932164
|
import sys
readline=sys.stdin.readline
write=sys.stdout.write
N=int(readline())
A=list(map(int,readline().split()))
M=int(readline())
B=list(map(int,readline().split()))
sum_A=sum(A)
sum_B=sum(B)
inf=1<<60
AA=[A[i]+A[j] for i in range(N) for j in range(i+1,N)]
BB=[B[i]+B[j] for i in range(M) for j in range(i+1,M)]
AA.sort()
BB.sort(reverse=True)
NN=N*(N-1)//2
MM=M*(M-1)//2
m=0
ans=abs(sum_A-sum_B)
ans_lst=[]
if N>=2 and M>=2:
a,b=None,None
for n in range(NN):
while m!=MM and sum_A-AA[n]*2+BB[m]*2-sum_B<0:
m+=1
if m:
d=abs(sum_A-AA[n]*2+BB[m-1]*2-sum_B)
if d<ans:
ans=d
a,b=AA[n],BB[m-1]
if m!=MM:
d=abs(sum_A-AA[n]*2+BB[m]*2-sum_B)
if d<ans:
ans=d
a,b=AA[n],BB[m]
ans_lst=[]
for ia in range(N):
for ja in range(ia+1,N):
if A[ia]+A[ja]==a:
break
else:
continue
break
for ib in range(M):
for jb in range(ib+1,M):
if B[ib]+B[jb]==b:
break
else:
continue
break
ans_lst=[(ia,ib),(ja,jb)]
for n in range(N):
for m in range(M):
d=abs(sum_A-A[n]*2+B[m]*2-sum_B)
if d<ans:
ans=d
ans_lst=[(n,m)]
print(ans)
print(len(ans_lst))
for i,j in ans_lst:
print(i+1,j+1)
| 24
| 2,995
| 152,166,400
|
92182162
|
from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
sum_a, sum_b = sum(a), sum(b)
delta = sum_b - sum_a
ans = abs(delta)
ans_swap = []
for i in range(n):
for j in range(m):
if abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) < ans:
ans = abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j]))
ans_swap = [(i+1, j+1)]
d = dict()
for i in range(m):
for j in range(i+1, m):
d[b[i]+b[j]] = (i+1, j+1)
minf, inf = -10**13, 10**13
val = [minf, minf] + sorted(d.keys()) + [inf, inf]
for i in range(n):
for j in range(i+1, n):
ap = a[i] + a[j]
req = (delta + ap*2) >> 1
k = bisect_left(val, req)
for k in range(k-1, k+2):
if abs(delta + ap*2 - val[k]*2) < ans:
ans = abs(delta + ap*2 - val[k]*2)
ans_swap = [(i+1, d[val[k]][0]), (j+1, d[val[k]][1])]
print(ans)
print(len(ans_swap))
for x, y in ans_swap:
print(x, y)
|
Educational Codeforces Round 6
|
ICPC
| 2,016
| 3
| 256
|
Professor GukiZ and Two Arrays
|
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.
In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].
Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
|
The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.
The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.
The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.
|
In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.
The second line should contain the number of swaps k (0 ≤ k ≤ 2).
Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.
If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
| null | null |
[{"input": "5\n5 4 3 2 1\n4\n1 1 1 1", "output": "1\n2\n1 1\n4 2"}, {"input": "5\n1 2 3 4 5\n1\n15", "output": "0\n0"}, {"input": "5\n1 2 3 4 5\n4\n1 2 3 4", "output": "1\n1\n3 1"}]
| 2,200
|
["binary search", "two pointers"]
| 24
|
[{"input": "5\r\n5 4 3 2 1\r\n4\r\n1 1 1 1\r\n", "output": "1\r\n2\r\n1 1\r\n4 2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n15\r\n", "output": "0\r\n0\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n4\r\n1 2 3 4\r\n", "output": "1\r\n1\r\n3 1\r\n"}, {"input": "1\r\n-42\r\n1\r\n-86\r\n", "output": "44\r\n0\r\n"}, {"input": "1\r\n-21\r\n10\r\n-43 6 -46 79 -21 93 -36 -38 -67 1\r\n", "output": "1\r\n1\r\n1 3\r\n"}, {"input": "10\r\n87 -92 -67 -100 -88 80 -82 -59 81 -72\r\n10\r\n-50 30 30 77 65 92 -60 -76 -29 -15\r\n", "output": "0\r\n2\r\n4 4\r\n9 6\r\n"}, {"input": "6\r\n1 2 3 4 5 11\r\n1\r\n3\r\n", "output": "7\r\n1\r\n6 1\r\n"}, {"input": "2\r\n-2 -17\r\n2\r\n11 -9\r\n", "output": "5\r\n1\r\n1 1\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().strip().split()))
m = int(f.readline().strip())
b = list(map(int, f.readline().strip().split()))
# Read reference output to get correct minimal v
with open(output_path) as f:
ref_v = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = [line.strip() for line in f if line.strip()]
if not lines:
print(0)
return
# Parse submission's v
try:
sub_v = int(lines[0])
except ValueError:
print(0)
return
if sub_v != ref_v:
print(0)
return
# Parse number of swaps
if len(lines) < 2:
print(0)
return
try:
k = int(lines[1])
except ValueError:
print(0)
return
if k < 0 or k > 2:
print(0)
return
if len(lines) != 2 + k:
print(0)
return
# Parse swaps
swaps = []
for i in range(2, 2 + k):
parts = lines[i].split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except ValueError:
print(0)
return
if x < 1 or x > n or y < 1 or y > m:
print(0)
return
swaps.append((x, y))
# Apply swaps
current_a = a.copy()
current_b = b.copy()
for x, y in swaps:
i = x - 1
j = y - 1
current_a[i], current_b[j] = current_b[j], current_a[i]
# Compute sum difference
sum_a = sum(current_a)
sum_b = sum(current_b)
computed_v = abs(sum_a - sum_b)
if computed_v == sub_v:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
620/D
|
620
|
D
|
PyPy 3
|
TESTS
| 9
| 1,840
| 72,601,600
|
92181127
|
from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
sum_a, sum_b = sum(a), sum(b)
delta = sum_b - sum_a
ans = abs(delta)
ans_swap = []
for i, x in enumerate(a, start=1):
for j, y in enumerate(b, start=1):
if abs((sum_a - x + y) - (sum_b - y + x)) < ans:
ans = abs((sum_a - x + y) - (sum_b - y + x))
ans_swap = [(i, j)]
d = dict()
for i in range(m):
for j in range(i+1, m):
d[b[i]+b[j]] = (i+1, j+1)
minf, inf = -10**18, 10**18
val, pair = zip(*([(minf, (-1, -1)), (minf, (-1, -1))] +
sorted(d.items()) + [(inf, (-1, -1)), (inf, (-1, -1))]))
for i in range(n):
for j in range(i+1, n):
ap = a[i] + a[j]
req = (delta + ap*2) >> 1
k = bisect_left(val, req)
if abs(delta + ap*2 - val[k]*2) < ans:
ans = abs(delta + ap*2 - val[k]*2)
ans_swap = [(i+1, pair[k][0]), (j+1, pair[k][1])]
if abs(delta + ap*2 - val[k+1]*2) < ans:
ans = abs(delta + ap*2 - val[k+1]*2)
ans_swap = [(i+1, pair[k+1][0]), (j+1, pair[k+1][1])]
print(ans)
print(len(ans_swap))
for x, y in ans_swap:
print(x, y)
| 24
| 2,995
| 152,166,400
|
92182162
|
from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
sum_a, sum_b = sum(a), sum(b)
delta = sum_b - sum_a
ans = abs(delta)
ans_swap = []
for i in range(n):
for j in range(m):
if abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) < ans:
ans = abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j]))
ans_swap = [(i+1, j+1)]
d = dict()
for i in range(m):
for j in range(i+1, m):
d[b[i]+b[j]] = (i+1, j+1)
minf, inf = -10**13, 10**13
val = [minf, minf] + sorted(d.keys()) + [inf, inf]
for i in range(n):
for j in range(i+1, n):
ap = a[i] + a[j]
req = (delta + ap*2) >> 1
k = bisect_left(val, req)
for k in range(k-1, k+2):
if abs(delta + ap*2 - val[k]*2) < ans:
ans = abs(delta + ap*2 - val[k]*2)
ans_swap = [(i+1, d[val[k]][0]), (j+1, d[val[k]][1])]
print(ans)
print(len(ans_swap))
for x, y in ans_swap:
print(x, y)
|
Educational Codeforces Round 6
|
ICPC
| 2,016
| 3
| 256
|
Professor GukiZ and Two Arrays
|
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.
In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].
Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
|
The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.
The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.
The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.
|
In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.
The second line should contain the number of swaps k (0 ≤ k ≤ 2).
Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.
If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
| null | null |
[{"input": "5\n5 4 3 2 1\n4\n1 1 1 1", "output": "1\n2\n1 1\n4 2"}, {"input": "5\n1 2 3 4 5\n1\n15", "output": "0\n0"}, {"input": "5\n1 2 3 4 5\n4\n1 2 3 4", "output": "1\n1\n3 1"}]
| 2,200
|
["binary search", "two pointers"]
| 24
|
[{"input": "5\r\n5 4 3 2 1\r\n4\r\n1 1 1 1\r\n", "output": "1\r\n2\r\n1 1\r\n4 2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n15\r\n", "output": "0\r\n0\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n4\r\n1 2 3 4\r\n", "output": "1\r\n1\r\n3 1\r\n"}, {"input": "1\r\n-42\r\n1\r\n-86\r\n", "output": "44\r\n0\r\n"}, {"input": "1\r\n-21\r\n10\r\n-43 6 -46 79 -21 93 -36 -38 -67 1\r\n", "output": "1\r\n1\r\n1 3\r\n"}, {"input": "10\r\n87 -92 -67 -100 -88 80 -82 -59 81 -72\r\n10\r\n-50 30 30 77 65 92 -60 -76 -29 -15\r\n", "output": "0\r\n2\r\n4 4\r\n9 6\r\n"}, {"input": "6\r\n1 2 3 4 5 11\r\n1\r\n3\r\n", "output": "7\r\n1\r\n6 1\r\n"}, {"input": "2\r\n-2 -17\r\n2\r\n11 -9\r\n", "output": "5\r\n1\r\n1 1\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().strip().split()))
m = int(f.readline().strip())
b = list(map(int, f.readline().strip().split()))
# Read reference output to get correct minimal v
with open(output_path) as f:
ref_v = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = [line.strip() for line in f if line.strip()]
if not lines:
print(0)
return
# Parse submission's v
try:
sub_v = int(lines[0])
except ValueError:
print(0)
return
if sub_v != ref_v:
print(0)
return
# Parse number of swaps
if len(lines) < 2:
print(0)
return
try:
k = int(lines[1])
except ValueError:
print(0)
return
if k < 0 or k > 2:
print(0)
return
if len(lines) != 2 + k:
print(0)
return
# Parse swaps
swaps = []
for i in range(2, 2 + k):
parts = lines[i].split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except ValueError:
print(0)
return
if x < 1 or x > n or y < 1 or y > m:
print(0)
return
swaps.append((x, y))
# Apply swaps
current_a = a.copy()
current_b = b.copy()
for x, y in swaps:
i = x - 1
j = y - 1
current_a[i], current_b[j] = current_b[j], current_a[i]
# Compute sum difference
sum_a = sum(current_a)
sum_b = sum(current_b)
computed_v = abs(sum_a - sum_b)
if computed_v == sub_v:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
111/B
|
111
|
B
|
Python 3
|
TESTS
| 4
| 186
| 0
|
50271901
|
import sys
def divisor(n):
div = list()
i = 1
sqrt = n**(1/2)
while i <= sqrt:
if (n % i) == 0:
div.append(i)
if i != (n/i):
div.append(int(n/i))
i += 1
return div
n = int(sys.stdin.readline())
xs = list()
for i in range(n):
x = list(map(int, sys.stdin.readline().split()))
div = divisor(x[0])
if x[1] == 0:
print(len(div))
xs.append(x[0])
continue
ran = xs[i-x[1]:]
for k in ran:
j = 0
while j < len(div):
v = div[j]
if (k % v) == 0:
div.remove(v)
j += 1
xs.append(x[0])
print(len(div))
| 44
| 1,278
| 10,854,400
|
91672692
|
import sys
import collections as cc
input=sys.stdin.buffer.readline
I=lambda:list(map(int,input().split()))
prev=cc.defaultdict(int)
for tc in range(int(input())):
x,y=I()
div=set()
for i in range(1,int(x**0.5)+1):
if x%i==0:
div.add(i)
div.add(x//i)
ans=0
now=tc+1
for i in div:
if now-prev[i]>y:
ans+=1
prev[i]=now
print(ans)
|
Codeforces Beta Round 85 (Div. 1 Only)
|
CF
| 2,011
| 5
| 256
|
Petya and Divisors
|
Little Petya loves looking for numbers' divisors. One day Petya came across the following problem:
You are given n queries in the form "xi yi". For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1. Help him.
|
The first line contains an integer n (1 ≤ n ≤ 105). Each of the following n lines contain two space-separated integers xi and yi (1 ≤ xi ≤ 105, 0 ≤ yi ≤ i - 1, where i is the query's ordinal number; the numeration starts with 1).
If yi = 0 for the query, then the answer to the query will be the number of divisors of the number xi. In this case you do not need to take the previous numbers x into consideration.
|
For each query print the answer on a single line: the number of positive integers k such that $${ x _ { i } \bmod k = 0 \& ( \forall j : i - y _ { i } \leq j < i ) x _ { j } \bmod k \neq 0 }$$
| null |
Let's write out the divisors that give answers for the first 5 queries:
1) 1, 2, 4
2) 3
3) 5
4) 2, 6
5) 9, 18
|
[{"input": "6\n4 0\n3 1\n5 2\n6 2\n18 4\n10000 3", "output": "3\n1\n1\n2\n2\n22"}]
| 1,900
|
["binary search", "data structures", "number theory"]
| 44
|
[{"input": "6\r\n4 0\r\n3 1\r\n5 2\r\n6 2\r\n18 4\r\n10000 3\r\n", "output": "3\r\n1\r\n1\r\n2\r\n2\r\n22\r\n"}, {"input": "5\r\n10 0\r\n10 0\r\n10 0\r\n10 0\r\n10 0\r\n", "output": "4\r\n4\r\n4\r\n4\r\n4\r\n"}, {"input": "12\r\n41684 0\r\n95210 1\r\n60053 1\r\n32438 3\r\n97956 1\r\n21785 2\r\n14594 6\r\n17170 4\r\n93937 6\r\n70764 5\r\n13695 4\r\n14552 6\r\n", "output": "12\r\n6\r\n7\r\n9\r\n22\r\n3\r\n2\r\n13\r\n1\r\n6\r\n13\r\n11\r\n"}, {"input": "10\r\n54972 0\r\n48015 1\r\n7114 1\r\n68273 2\r\n53650 4\r\n1716 1\r\n16165 2\r\n96062 5\r\n57750 1\r\n21071 5\r\n", "output": "24\r\n21\r\n3\r\n3\r\n21\r\n22\r\n6\r\n6\r\n62\r\n3\r\n"}, {"input": "20\r\n68260 0\r\n819 1\r\n54174 1\r\n20460 1\r\n25696 2\r\n81647 4\r\n17736 4\r\n91307 5\r\n5210 4\r\n87730 2\r\n4653 8\r\n11044 6\r\n15776 4\r\n17068 7\r\n73738 7\r\n36004 12\r\n83183 7\r\n75700 12\r\n84270 14\r\n16120 5\r\n", "output": "12\r\n11\r\n6\r\n44\r\n18\r\n1\r\n9\r\n7\r\n6\r\n12\r\n8\r\n8\r\n21\r\n3\r\n14\r\n3\r\n3\r\n13\r\n18\r\n26\r\n"}, {"input": "17\r\n81548 0\r\n69975 1\r\n1234 0\r\n72647 0\r\n81389 4\r\n77930 1\r\n19308 0\r\n86551 6\r\n69023 8\r\n38037 1\r\n133 9\r\n59290 8\r\n1106 11\r\n95012 10\r\n57693 11\r\n8467 6\r\n93732 13\r\n", "output": "24\r\n17\r\n4\r\n2\r\n11\r\n7\r\n12\r\n3\r\n3\r\n7\r\n2\r\n27\r\n4\r\n3\r\n2\r\n1\r\n18\r\n"}, {"input": "15\r\n94836 0\r\n22780 1\r\n48294 0\r\n24834 3\r\n37083 2\r\n57862 0\r\n37231 1\r\n81795 7\r\n32835 2\r\n4696 8\r\n95612 0\r\n7536 6\r\n70084 5\r\n72956 10\r\n41647 7\r\n", "output": "24\r\n21\r\n12\r\n4\r\n6\r\n8\r\n3\r\n27\r\n12\r\n5\r\n24\r\n15\r\n8\r\n21\r\n1\r\n"}, {"input": "12\r\n91771 0\r\n75584 1\r\n95355 1\r\n60669 1\r\n92776 0\r\n37793 3\r\n38802 4\r\n60688 0\r\n80296 5\r\n55003 8\r\n91092 3\r\n55782 8\r\n", "output": "2\r\n13\r\n23\r\n17\r\n8\r\n2\r\n13\r\n10\r\n4\r\n2\r\n9\r\n10\r\n"}, {"input": "11\r\n5059 0\r\n28388 1\r\n42415 2\r\n12856 0\r\n48470 3\r\n34076 2\r\n40374 6\r\n55932 1\r\n44108 2\r\n5310 5\r\n86571 4\r\n", "output": "2\r\n11\r\n7\r\n8\r\n13\r\n9\r\n10\r\n20\r\n3\r\n12\r\n3\r\n"}, {"input": "10\r\n18347 0\r\n81193 1\r\n89475 2\r\n65043 3\r\n4164 0\r\n14007 5\r\n41945 0\r\n51177 1\r\n91569 5\r\n71969 4\r\n", "output": "4\r\n4\r\n11\r\n18\r\n12\r\n13\r\n4\r\n7\r\n6\r\n3\r\n"}]
| false
|
stdio
| null | true
|
390/A
|
390
|
A
|
Python 3
|
TESTS
| 14
| 592
| 0
|
64330370
|
n=int(input())
lv=[];lg=[]
v,g=[0]*2
for i in range(n):
a,b=[int(x) for x in input().split()]
if a not in lv:
v+=1
lv.append(a)
if b not in lg:
g+=1
lg.append(a)
print(min(v,g))
| 19
| 171
| 0
|
159169799
|
h=set()
v=set()
for i in range(int(input())):
k = input().split()
v.add(k[0])
h.add(k[1])
print(min(len(v),len(h)))
|
Codeforces Round 229 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and Alarm Clock
|
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.
The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:
- First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
- Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.
Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
|
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100).
Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
|
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
| null |
In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.
In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.
|
[{"input": "4\n0 0\n0 1\n0 2\n1 0", "output": "2"}, {"input": "4\n0 0\n0 1\n1 0\n1 1", "output": "2"}, {"input": "4\n1 1\n1 2\n2 3\n3 3", "output": "3"}]
| null |
["implementation"]
| 19
|
[{"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 0\r\n", "output": "2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1\r\n1 2\r\n2 3\r\n3 3\r\n", "output": "3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "1\r\n"}, {"input": "42\r\n28 87\r\n26 16\r\n59 90\r\n47 61\r\n28 83\r\n36 30\r\n67 10\r\n6 95\r\n9 49\r\n86 94\r\n52 24\r\n74 9\r\n86 24\r\n28 51\r\n25 99\r\n40 98\r\n57 33\r\n18 96\r\n43 36\r\n3 79\r\n4 86\r\n38 61\r\n25 61\r\n6 100\r\n58 81\r\n28 19\r\n64 4\r\n3 40\r\n2 56\r\n41 49\r\n97 100\r\n86 34\r\n42 36\r\n44 40\r\n14 85\r\n21 60\r\n76 99\r\n64 47\r\n69 13\r\n49 37\r\n97 37\r\n3 70\r\n", "output": "31\r\n"}, {"input": "21\r\n54 85\r\n69 37\r\n42 87\r\n53 18\r\n28 22\r\n13 3\r\n62 97\r\n38 91\r\n67 19\r\n100 79\r\n29 18\r\n48 40\r\n68 84\r\n44 20\r\n37 34\r\n73 53\r\n21 5\r\n20 73\r\n24 94\r\n23 52\r\n7 55\r\n", "output": "20\r\n"}, {"input": "19\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n1 11\r\n1 12\r\n1 13\r\n1 14\r\n1 15\r\n1 16\r\n1 17\r\n1 18\r\n1 19\r\n", "output": "1\r\n"}, {"input": "12\r\n1 1\r\n1 3\r\n1 5\r\n2 1\r\n2 2\r\n2 4\r\n3 1\r\n3 3\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
859/E
|
859
|
E
|
PyPy 3-64
|
TESTS
| 3
| 93
| 4,710,400
|
226374061
|
import sys
import math
from math import ceil
from functools import lru_cache
input = sys.stdin.readline
def womais(i,j):
if i == 0:
return j
elif j == 0:
return i
else:
return min(i,j)
class DisjointSetUnion:
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.cyc = [0] * n
self.num_sets = n
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a != b:
if self.size[a] < self.size[b]:
a, b = b, a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
self.cyc[a] = womais(self.cyc[a], self.cyc[b])
else:
self.cyc[a] = womais(self.cyc[a], 2)
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
def ii():
return int(input())
def li():
return list(map(lambda x: int(x)-1, input().split()))
n = ii()
shit = DisjointSetUnion(2*n)
for _ in range(n):
u,v = li()
if u == v:
shit.cyc[shit.find(u)] = 1
else:
shit.union(u,v)
ans = 1
seen = set()
for i in range(2*n):
a = shit.find(i)
if a not in seen:
seen.add(a)
if shit.cyc[a]:
ans *= shit.cyc[a]
else:
ans *= shit.size[a]
print(ans)
| 36
| 187
| 16,076,800
|
226366340
|
import sys
input = sys.stdin.readline
class DisjointSetUnion:
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.num_sets = n
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a != b:
if self.size[a] < self.size[b]:
a, b = b, a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
cyc = []
n = int(input())
f = [-1] * (2 * n)
zz = [0] * (2 * n)
UF = DisjointSetUnion(2 * n)
bad = []
for _ in range(n):
u, v = map(int, input().split())
u -= 1; v -= 1
f[u] = v
if u == v:
bad.append(u)
if UF.find(u) != UF.find(v):
UF.union(u, v)
else:
cyc.append(u)
for v in cyc:
zz[UF.find(v)] = 1
for v in bad:
zz[UF.find(v)] = 2
out = 1
MOD = 10 ** 9 + 7
for i in range(2 * n):
if UF.find(i) == i:
if zz[i] == 0:
out *= UF.size[i]
elif zz[i] == 1:
out *= 2
out %= MOD
print(out)
|
MemSQL Start[c]UP 3.0 - Round 1
|
CF
| 2,017
| 2
| 256
|
Desk Disorder
|
A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.
How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7.
|
Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers.
N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk.
|
Print the number of possible assignments, modulo 1000000007 = 109 + 7.
| null |
These are the possible assignments for the first example:
- 1 5 3 7
- 1 2 3 7
- 5 2 3 7
- 1 5 7 3
- 1 2 7 3
- 5 2 7 3
|
[{"input": "4\n1 5\n5 2\n3 7\n7 3", "output": "6"}, {"input": "5\n1 10\n2 10\n3 10\n4 10\n5 5", "output": "5"}]
| 2,100
|
["combinatorics", "dfs and similar", "dsu", "graphs", "trees"]
| 36
|
[{"input": "4\r\n1 5\r\n5 2\r\n3 7\r\n7 3\r\n", "output": "6\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 5\r\n", "output": "5\r\n"}, {"input": "1\r\n1 2\r\n", "output": "2\r\n"}, {"input": "30\r\n22 37\r\n12 37\r\n37 58\r\n29 57\r\n43 57\r\n57 58\r\n58 53\r\n45 4\r\n1 4\r\n4 51\r\n35 31\r\n21 31\r\n31 51\r\n51 53\r\n53 48\r\n60 55\r\n52 55\r\n55 33\r\n36 9\r\n10 9\r\n9 33\r\n33 19\r\n5 23\r\n47 23\r\n23 32\r\n50 44\r\n26 44\r\n44 32\r\n32 19\r\n19 48\r\n", "output": "31\r\n"}, {"input": "50\r\n73 1\r\n65 73\r\n16 65\r\n57 65\r\n33 16\r\n34 57\r\n98 16\r\n84 98\r\n55 34\r\n64 84\r\n80 55\r\n75 64\r\n28 75\r\n20 75\r\n42 75\r\n88 42\r\n50 20\r\n48 28\r\n32 48\r\n58 88\r\n92 76\r\n76 53\r\n53 15\r\n15 1\r\n1 10\r\n10 71\r\n71 37\r\n37 95\r\n95 63\r\n63 92\r\n45 97\r\n97 51\r\n51 96\r\n96 12\r\n12 62\r\n62 31\r\n31 5\r\n5 29\r\n29 19\r\n19 49\r\n49 6\r\n6 40\r\n40 18\r\n18 22\r\n22 17\r\n17 46\r\n46 72\r\n72 82\r\n82 14\r\n14 14\r\n", "output": "2\r\n"}, {"input": "10\r\n15 8\r\n8 13\r\n13 3\r\n1 4\r\n14 3\r\n11 17\r\n9 10\r\n10 18\r\n19 20\r\n17 20\r\n", "output": "120\r\n"}, {"input": "4\r\n5 6\r\n6 7\r\n7 8\r\n8 5\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
859/E
|
859
|
E
|
Python 3
|
TESTS
| 3
| 46
| 512,000
|
109511010
|
class UnionFind:
def __init__(self, n):
self._num_of_set = n
self._set_size = [1] * n
self._rank = [0] * n
self._parent = [i for i in range(n)]
def findSet(self, i):
if self._parent[i] != i:
self._parent[i] = self.findSet(self._parent[i])
return self._parent[i]
def isSameSet(self, i, j):
return self.findSet(i) == self.findSet(j)
def numOfDisjointSet(self):
return self._num_of_set
def sizeOfSet(self, i):
return self._set_size[self.findSet(i)]
def unionSet(self, i, j):
if i == j:
self._set_size[self.findSet(i)] = 1
elif self.isSameSet(i, j):
self._set_size[self.findSet(i)] = 2
return
pi, pj = self.findSet(i), self.findSet(j)
if self._rank[pi] > self._rank[pj]:
self._parent[pj] = pi
self._set_size[pi] += self._set_size[pj]
self._set_size[pj] = 1
else:
self._parent[pi] = pj
self._set_size[pj] += self._set_size[pi]
self._set_size[pi] = 1
if self._rank[pi] == self._rank[pj]:
self._rank[pj] += 1
self._num_of_set -= 1
def res(self):
from functools import reduce
from operator import mul
return reduce(mul, self._set_size)
n, M, ans = int(input()), 10**9+7, 1
uf = UnionFind(2*n+1)
for _ in range(n):
u, v = map(int, input().split())
uf.unionSet(u, v)
print(uf.res())
| 36
| 187
| 16,076,800
|
226366340
|
import sys
input = sys.stdin.readline
class DisjointSetUnion:
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.num_sets = n
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a != b:
if self.size[a] < self.size[b]:
a, b = b, a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
cyc = []
n = int(input())
f = [-1] * (2 * n)
zz = [0] * (2 * n)
UF = DisjointSetUnion(2 * n)
bad = []
for _ in range(n):
u, v = map(int, input().split())
u -= 1; v -= 1
f[u] = v
if u == v:
bad.append(u)
if UF.find(u) != UF.find(v):
UF.union(u, v)
else:
cyc.append(u)
for v in cyc:
zz[UF.find(v)] = 1
for v in bad:
zz[UF.find(v)] = 2
out = 1
MOD = 10 ** 9 + 7
for i in range(2 * n):
if UF.find(i) == i:
if zz[i] == 0:
out *= UF.size[i]
elif zz[i] == 1:
out *= 2
out %= MOD
print(out)
|
MemSQL Start[c]UP 3.0 - Round 1
|
CF
| 2,017
| 2
| 256
|
Desk Disorder
|
A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.
How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7.
|
Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers.
N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk.
|
Print the number of possible assignments, modulo 1000000007 = 109 + 7.
| null |
These are the possible assignments for the first example:
- 1 5 3 7
- 1 2 3 7
- 5 2 3 7
- 1 5 7 3
- 1 2 7 3
- 5 2 7 3
|
[{"input": "4\n1 5\n5 2\n3 7\n7 3", "output": "6"}, {"input": "5\n1 10\n2 10\n3 10\n4 10\n5 5", "output": "5"}]
| 2,100
|
["combinatorics", "dfs and similar", "dsu", "graphs", "trees"]
| 36
|
[{"input": "4\r\n1 5\r\n5 2\r\n3 7\r\n7 3\r\n", "output": "6\r\n"}, {"input": "5\r\n1 10\r\n2 10\r\n3 10\r\n4 10\r\n5 5\r\n", "output": "5\r\n"}, {"input": "1\r\n1 2\r\n", "output": "2\r\n"}, {"input": "30\r\n22 37\r\n12 37\r\n37 58\r\n29 57\r\n43 57\r\n57 58\r\n58 53\r\n45 4\r\n1 4\r\n4 51\r\n35 31\r\n21 31\r\n31 51\r\n51 53\r\n53 48\r\n60 55\r\n52 55\r\n55 33\r\n36 9\r\n10 9\r\n9 33\r\n33 19\r\n5 23\r\n47 23\r\n23 32\r\n50 44\r\n26 44\r\n44 32\r\n32 19\r\n19 48\r\n", "output": "31\r\n"}, {"input": "50\r\n73 1\r\n65 73\r\n16 65\r\n57 65\r\n33 16\r\n34 57\r\n98 16\r\n84 98\r\n55 34\r\n64 84\r\n80 55\r\n75 64\r\n28 75\r\n20 75\r\n42 75\r\n88 42\r\n50 20\r\n48 28\r\n32 48\r\n58 88\r\n92 76\r\n76 53\r\n53 15\r\n15 1\r\n1 10\r\n10 71\r\n71 37\r\n37 95\r\n95 63\r\n63 92\r\n45 97\r\n97 51\r\n51 96\r\n96 12\r\n12 62\r\n62 31\r\n31 5\r\n5 29\r\n29 19\r\n19 49\r\n49 6\r\n6 40\r\n40 18\r\n18 22\r\n22 17\r\n17 46\r\n46 72\r\n72 82\r\n82 14\r\n14 14\r\n", "output": "2\r\n"}, {"input": "10\r\n15 8\r\n8 13\r\n13 3\r\n1 4\r\n14 3\r\n11 17\r\n9 10\r\n10 18\r\n19 20\r\n17 20\r\n", "output": "120\r\n"}, {"input": "4\r\n5 6\r\n6 7\r\n7 8\r\n8 5\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2\r\n"}]
| false
|
stdio
| null | true
|
789/A
|
789
|
A
|
Python 3
|
TESTS
| 27
| 93
| 13,824,000
|
217967030
|
# بسم الله (not accepted) ... time limit exceeded
n,k = map(int,input().split(' '))
pebbles_list = input().split(' ')
pebbles_list = [int(item) for item in pebbles_list]
output = 0
for value in pebbles_list :
if n ==1 :
output = 1
break
if value % k == 0 :
output += value/k
elif value % k != 0 :
output += int(value/k) +1
if output% 2 == 0 :
print(int(output/2))
if output % 2 != 0 :
print(int(int(output/2)+1))
| 31
| 78
| 7,372,800
|
146159356
|
n,k = map(int,input().split())
w = list(map(int,input().split()))
cnt = 0
res = 0
for i in w:
cnt += (i+k-1)//k
print((cnt+1)//2)
|
Codeforces Round 407 (Div. 2)
|
CF
| 2,017
| 1
| 256
|
Anastasia and pebbles
|
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
|
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
|
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
| null |
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
|
[{"input": "3 2\n2 3 4", "output": "3"}, {"input": "5 4\n3 1 8 9 7", "output": "5"}]
| 1,100
|
["implementation", "math"]
| 31
|
[{"input": "3 2\r\n2 3 4\r\n", "output": "3\r\n"}, {"input": "5 4\r\n3 1 8 9 7\r\n", "output": "5\r\n"}, {"input": "1 22\r\n1\r\n", "output": "1\r\n"}, {"input": "3 57\r\n78 165 54\r\n", "output": "3\r\n"}, {"input": "5 72\r\n74 10 146 189 184\r\n", "output": "6\r\n"}, {"input": "9 13\r\n132 87 200 62 168 51 185 192 118\r\n", "output": "48\r\n"}, {"input": "1 1\r\n10000\r\n", "output": "5000\r\n"}, {"input": "10 1\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2 2\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
489/A
|
489
|
A
|
PyPy 3-64
|
TESTS
| 0
| 30
| 0
|
205286601
|
n=int(input())
l=list(map(int,input().split()))
ll=[]
while 1:
itr=0
for i in range(n-1):
if l[i]>l[i+1]:
ll.append((i,i+1))
l[i+1],l[i]=l[i],l[i+1]
itr+=1
if itr==0:
break
print(len(ll))
for o,p in ll:
print(o,p)
| 22
| 124
| 7,168,000
|
214607359
|
def min_index(i,arr):
res=i
for j in range(i + 1, n):
if a[j] < a[res]:
res = j
return res
n = int(input())
a = list(map(int, input().split()))
swaps = []
for i in range(n):
m = min_index(i,a)
if i != m:
a[i], a[m] = a[m], a[i]
swaps.append((i, m))
print(len(swaps))
for swap in swaps:
print(swap[0], swap[1])
|
Codeforces Round 277.5 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
SwapSort
|
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
|
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
|
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
| null | null |
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
| 1,200
|
["greedy", "implementation", "sortings"]
| 22
|
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
sub_path = sys.argv[3]
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().split()))
with open(sub_path) as f:
lines = f.readlines()
if not lines:
print(0)
return
try:
k = int(lines[0].strip())
swaps = []
for line in lines[1:1 + k]:
i, j = map(int, line.strip().split())
swaps.append((i, j))
except (ValueError, IndexError):
print(0)
return
if not (0 <= k <= n):
print(0)
return
for i, j in swaps:
if not (0 <= i < n and 0 <= j < n):
print(0)
return
current = arr.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
sorted_arr = sorted(arr)
if current == sorted_arr:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
489/A
|
489
|
A
|
Python 3
|
TESTS
| 5
| 31
| 0
|
210207317
|
length_array = int(input())
array = list(map(int, input().split()))
right_pointer = length_array
max_integer = - 10**9 - 1
position_max_integer = 0
swap_pairs = []
def swap(index_first, index_second, lst):
temp = lst[index_first]
lst[index_first] = lst[index_second]
lst[index_second] = temp
while right_pointer > length_array // 2:
max_integer = - 10 ** 9 - 1
for i in range(0, right_pointer):
if max_integer < array[i]:
max_integer = array[i]
position_max_integer = i
swap(right_pointer - 1, position_max_integer, array)
swap_pairs.append([right_pointer - 1, position_max_integer])
right_pointer -= 1
#print(array)
print(len(swap_pairs))
for i in range(0, len(swap_pairs)):
print(swap_pairs[i][0], swap_pairs[i][1])
| 22
| 140
| 3,891,200
|
223354529
|
n = int(input())
a = list(map(int, input().split()))
IT = []
for i in range(n):
ind = -1
minc = a[i]
for j in range(i + 1, n):
if a[j] < minc:
minc = a[j]
ind = j
if ind != -1:
a[i], a[ind] = a[ind], a[i]
IT.append((i, ind))
print(len(IT))
for i in IT:
print(i[0], i[1])
|
Codeforces Round 277.5 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
SwapSort
|
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
|
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
|
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
| null | null |
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
| 1,200
|
["greedy", "implementation", "sortings"]
| 22
|
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
sub_path = sys.argv[3]
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().split()))
with open(sub_path) as f:
lines = f.readlines()
if not lines:
print(0)
return
try:
k = int(lines[0].strip())
swaps = []
for line in lines[1:1 + k]:
i, j = map(int, line.strip().split())
swaps.append((i, j))
except (ValueError, IndexError):
print(0)
return
if not (0 <= k <= n):
print(0)
return
for i, j in swaps:
if not (0 <= i < n and 0 <= j < n):
print(0)
return
current = arr.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
sorted_arr = sorted(arr)
if current == sorted_arr:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
985/C
|
985
|
C
|
PyPy 3-64
|
TESTS
| 7
| 93
| 13,721,600
|
183459953
|
n, k, r = map(int, input().split())
a = sorted(list(map(int, input().split())))
if (a[n - 1] - a[0]) > r:
print(0)
else:
h = n - 1
u = 0
if k != 1:
for i in range(n, n * k):
if (a[i] - a[0]) > r:
break
else:
u += 1
h = i
w = 0
i = 0
fg = 0
for _ in range(n * k):
if i < (h + 1):
# print(a[i], i)
w += a[i]
if u > 0:
i += k
u -= 1
else:
i += 1
fg += 1
print(w)
| 50
| 155
| 7,884,800
|
152488718
|
R = lambda:map(int,input().split())
n, k, l = R()
a = sorted(R())
s = c = 0
for i in range(n*k-1, -1, -1):
c += 1
if a[i]-a[0] <= l and c>=k:
s += a[i]
c -= k
print((s,0)[c>0])
|
Educational Codeforces Round 44 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Liebig's Barrels
|
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
|
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
|
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
| null |
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
|
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
| 1,500
|
["greedy"]
| 50
|
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
985/C
|
985
|
C
|
PyPy 3-64
|
TESTS
| 7
| 108
| 15,155,200
|
183355520
|
n, k, l = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
var = []
for i in range(n * k):
if a[i] - a[0] <= l:
var.append(a[i])
ans = 0
prosh = min(a[0:k:])
if a[n - 1] - a[0] > l:
print(0)
exit()
if k == 1:
ans = 0
for i in range(n):
ans += var[i]
print(ans)
exit()
x = (len(var) - n) // (k - 1)
ans = 0
nnow = 0
for i in range(0, k * x, k):
ans += var[i]
nnow += 1
for i in range(k * x, len(var)):
if nnow >= n:
break
ans += var[i]
nnow += 1
print(ans)
| 50
| 155
| 8,089,600
|
38548199
|
from bisect import*
R=lambda:map(int,input().split())
n,k,l=R()
a=sorted(R())
i=bisect_right(a,a[0]+l)
if i<n:print(0)
else:
s=sum(a[0:i:k]);n-=(i-1)//k+1
while n:
i-=1
if i%k:
s+=a[i];n-=1
print(s)
|
Educational Codeforces Round 44 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Liebig's Barrels
|
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
|
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
|
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
| null |
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
|
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
| 1,500
|
["greedy"]
| 50
|
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
484/B
|
484
|
B
|
Python 3
|
TESTS
| 11
| 405
| 14,336,000
|
43780439
|
n=int(input())
l=list(map(int,input().split()))
l=sorted(l)
k=0
ma=0
s=l[0]
r=0
for i in range(n-1) :
k+=l[i+1]%l[i]
while k>=s :
k-=l[r+1]%l[r]
r+=1
s=l[r]
ma=max(k,ma)
print(ma)
| 45
| 358
| 45,568,000
|
143541801
|
import bisect
MAXN = 10**6
N = int(input())
exist = [False for i in range(MAXN + 10)]
arr = list(map(int, input().split()))
a = []
for x in arr:
if exist[x] == False:
exist[x] = True
a.append(x)
a.sort()
N = len(a)
ans = 0
for i in range(N - 2, -1, -1):
if ans >= a[i] - 1:
break
j = a[i] + a[i]
while j - a[i] <= a[N - 1]:
id = bisect.bisect_left(a, j, 0, N) - 1
ans = max(ans, a[id] % a[i])
j += a[i]
print(ans)
|
Codeforces Round 276 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Maximum Value
|
You are given a sequence a consisting of n integers. Find the maximum possible value of $$a_i \bmod a_j$$ (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.
|
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).
The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).
|
Print the answer to the problem.
| null | null |
[{"input": "3\n3 4 5", "output": "2"}]
| 2,100
|
["binary search", "math", "sortings", "two pointers"]
| 45
|
[{"input": "3\r\n3 4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n1 2 4\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000 999999\r\n", "output": "1\r\n"}, {"input": "12\r\n4 4 10 13 28 30 41 43 58 61 70 88\r\n", "output": "30\r\n"}, {"input": "7\r\n2 13 22 32 72 91 96\r\n", "output": "27\r\n"}, {"input": "5\r\n5 11 12 109 110\r\n", "output": "10\r\n"}]
| false
|
stdio
| null | true
|
187/A
|
187
|
A
|
Python 3
|
TESTS
| 5
| 62
| 0
|
155371612
|
n = int(input())
p1 = input().split(' ')
p2 = input().split(' ')
count = 0
c = 0
while p1 != p2 and c < 20:
c += 1
x = p1[-1]
for i in range(n):
if p1[:i + 1] != p2[:i + 1]:
p1.pop()
p1.insert(i, x)
count += 1
break
print(count)
| 58
| 842
| 40,755,200
|
31481440
|
n = int(input())
l1 = [int(x) for x in input().split()]
l2 = [int(x) for x in input().split()]
used = set()
j = len(l1)-1
worst = j
for i in range(len(l2)-1, -1, -1):
if l2[i] in used:
continue
if l2[i] == l1[j]:
j-=1
else:
while l2[i] != l1[j]:
used.add(l1[j])
j-=1
j -= 1
worst = j+1
#print(worst)
print(len(l1) - worst - 1)
|
Codeforces Round 119 (Div. 1)
|
CF
| 2,012
| 2
| 256
|
Permutations
|
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
|
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
|
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
| null |
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
- 1 5 2 3 4
- 1 4 5 2 3
- 1 3 4 5 2
- 1 2 3 4 5
|
[{"input": "3\n3 2 1\n1 2 3", "output": "2"}, {"input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1"}, {"input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3"}]
| 1,500
|
["greedy"]
| 58
|
[{"input": "3\r\n3 2 1\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 5 2 3 4\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 2 3 4\r\n1 2 3 4 5\r\n", "output": "3\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "7\r\n6 1 7 3 4 5 2\r\n6 1 7 3 4 5 2\r\n", "output": "0\r\n"}, {"input": "10\r\n5 8 1 10 3 6 2 9 7 4\r\n4 2 6 3 1 9 10 5 8 7\r\n", "output": "8\r\n"}, {"input": "10\r\n1 6 10 3 4 9 2 5 8 7\r\n7 5 1 6 10 3 4 8 9 2\r\n", "output": "3\r\n"}, {"input": "10\r\n2 1 10 3 7 8 5 6 9 4\r\n6 9 2 4 1 10 3 7 8 5\r\n", "output": "3\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "0\r\n"}, {"input": "20\r\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\r\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1\r\n", "output": "19\r\n"}]
| false
|
stdio
| null | true
|
298/A
|
298
|
A
|
PyPy 3
|
TESTS
| 4
| 280
| 0
|
77181342
|
n=int(input())
x=input()
a=list(x)
s=0
t=0
if a.count("L")==0:
s=x.rindex("R")+1
t=s+1
elif a.count("R")==0:
s=a.index("L")+1
t=s-1
else:
if a.count("R")==a.count("L"):
s=a.index("R")+1
t=s+1
elif a.count("L")>a.count("R"):
s=a.index("L")+abs(a.count("L")-a.count("R"))+1
t=s-abs(a.count("L")-a.count("R"))
else:
s=x.rindex("L")+abs(a.count("L")-a.count("R"))+1
t=s-abs(a.count("R"))
print(s,t)
| 23
| 62
| 0
|
145061241
|
n = int(input())
arr = input()
l = []
l[0:] = arr
arr = l
start = 0
end = 0
for i in range(n):
if arr[i] != '.':
start = i
break
for i in reversed(range(n)):
if arr[i] != '.':
end = i
break
if arr[start] == 'R' and arr[end] == 'R':
print(start + 1, end + 2)
elif arr[start] == 'L' and arr[end] == 'L':
print(end + 1, start)
else:
for i in range(start, end):
if arr[i] != arr[i + 1]:
print(start + 1, i + 1)
break
|
Codeforces Round 180 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Snow Footprints
|
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
|
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
|
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
| null |
The first test sample is the one in the picture.
|
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
| 1,300
|
["greedy", "implementation"]
| 23
|
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
road = f.readline().strip()
with open(submission_path) as f:
line = f.readline().strip()
try:
s, t = map(int, line.split())
except:
print(0)
return
if s < 1 or s > n or t < 1 or t > n:
print(0)
return
adj = [[] for _ in range(n+1)]
for i in range(1, n+1):
c = road[i-1]
if c == 'R':
if i+1 <= n:
adj[i].append(i+1)
elif c == 'L':
if i-1 >= 1:
adj[i].append(i-1)
visited = [False] * (n+1)
q = deque([s])
visited[s] = True
while q:
u = q.popleft()
if u == t:
print(1)
return
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
print(0)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
362/A
|
362
|
A
|
Python 3
|
TESTS
| 6
| 46
| 0
|
184401829
|
def dfs(position, end, mark):
mark[position[0]][position[1]] = True
# print(position)
# Possible moves
# Move 1
row = position[0] + 2
col = position[1] + 2
if row >= 0 and row < 8 and col >= 0 and col < 8 and not mark[row][col]:
dfs((row, col), end, mark)
# Move 2
row = position[0] + 2
col = position[1] - 2
if row >= 0 and row < 8 and col >= 0 and col < 8 and not mark[row][col]:
dfs((row, col), end, mark)
# Move 3
row = position[0] - 2
col = position[1] + 2
if row >= 0 and row < 8 and col >= 0 and col < 8 and not mark[row][col]:
dfs((row, col), end, mark)
# Move 4
row = position[0] - 2
col = position[1] - 2
if row >= 0 and row < 8 and col >= 0 and col < 8 and not mark[row][col]:
dfs((row, col), end, mark)
def solve():
t = int(input().strip())
# Reading input
for ti in range(t):
if ti > 0:
_ = input()
knights = []
board = []
mark = []
for i in range(0, 8):
line = input().strip()
board.append([c for c in line])
mark.append([False for c in line])
for j in range(len(line)):
if line[j] == "K":
knights.append((i, j))
# for line in board:
# print(line)
# print(knights)
dfs(knights[0], knights[1], mark)
# for line in mark:
# print(line)
print("YES" if mark[knights[1][0]][knights[1][1]] else "NO")
solve()
| 45
| 62
| 307,200
|
5100480
|
def check(x, y):
return 0 <= x < 8 and 0 <= y < 8
def dfs1(x, y, T=0):
global first, used
if not(check(x, y)) or used[x][y]:
return
used[x][y] = True
first.add((x, y, T))
for pair in (2, 2), (2, -2), (-2, 2), (-2, -2):
dfs1(x + pair[0], y + pair[1], 1 - T)
def dfs2(x, y, T=0):
global second, used
if not(check(x, y)) or used[x][y]:
return
used[x][y] = True
second.add((x, y, T))
for pair in (2, 2), (2, -2), (-2, 2), (-2, -2):
dfs2(x + pair[0], y + pair[1], 1 - T)
t = int(input())
for i in range(t):
if i > 0:
kuzma = input()
board = [input() for i in range(8)]
FoundFirst = False
for i in range(8):
for j in range(8):
if board[i][j] == 'K':
if not(FoundFirst):
First = (i, j)
FoundFirst = True
else:
Second = (i, j)
used = [[0 for i in range(8)] for j in range(8)]
first = set()
dfs1(First[0], First[1])
used = [[0 for i in range(8)] for j in range(8)]
second = set()
dfs2(Second[0], Second[1])
intersection = first & second
IsOk = False
for x, y, t in intersection:
if board[x][y] != '#':
print("YES")
IsOk = True
break
if not(IsOk):
print("NO")
board = []
|
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
|
651/B
|
651
|
B
|
Python 3
|
TESTS
| 5
| 61
| 0
|
109992369
|
n=int(input())
arr=sorted(list(map(int,input().split())))
t=0
for i in arr:
if i<max(arr):
t+=1
print(t)
| 31
| 46
| 0
|
223924642
|
num=input()
arr=input().split(' ')
arr=list(map(int,arr))
freq_arr=dict()
for ele in arr:
if ele in freq_arr:
freq_arr[ele]+=1
else:
freq_arr[ele]=1
arr=list(freq_arr.values())
arr.sort()
ans=0
for indx in range(len(arr)):
for ele in range(indx+1,len(arr)):
arr[ele]-=arr[indx]
ans+=arr[indx]*(len(arr)-indx-1)
print(ans)
|
Codeforces Round 345 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Beautiful Paintings
|
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
|
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
|
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
| null |
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
|
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
| 1,200
|
["greedy", "sortings"]
| 31
|
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
| false
|
stdio
| null | true
|
788/C
|
788
|
C
|
Python 3
|
TESTS
| 2
| 93
| 0
|
52524634
|
n, k = map(int, input().split())
arr = list(map(int, input().split()))
possible = [1000000007]*1001
arr.sort()
for i in arr:
possible[i] = 1
for i in range(1, 1001):
for j in arr:
if(i-j < 1):
break
possible[i] = min(possible[i], 1+possible[i-j])
if(possible[n] == 1000000007):
print("-1")
for i in range(1, 1001):
if(i*n <= 1000 and i == possible[i*n]):
print(i)
exit(0)
print(-1)
| 48
| 467
| 44,851,200
|
98107843
|
from collections import deque
n, k = map(int, input().split())
conc = set(int(x) - n for x in input().split())
q = deque()
q.append(0)
visited = {i : False for i in range(-1000, 1001)}
dist = {i : 0 for i in range(-1000, 1001)}
ans = -1
visited[0] = True
found = False
while q:
u = q.popleft()
for c in conc:
v = c + u
if v == 0:
ans=dist[u]+1
found=True
break
if v<=1000 and v>=-1000 and not visited[v]:
visited[v]=True
dist[v]=dist[u]+1
q.append(v)
if found:
break
print(ans)
|
Codeforces Round 407 (Div. 1)
|
CF
| 2,017
| 1
| 256
|
The Great Mixing
|
Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration $${ \frac { a _ { i } } { 1 0 0 0 } }$$. Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration $$\frac{n}{1000}$$. The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.
Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.
Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration $$\frac{n}{1000}$$. Assume that the friends have unlimited amount of each Coke type.
|
The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.
The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.
|
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration $$\frac{n}{1000}$$, or -1 if it is impossible.
| null |
In the first sample case, we can achieve concentration $$\frac{400}{1000}$$ using one liter of Coke of types $$\frac{300}{1000}$$ and $${ \frac { 5 0 0 } { 1 0 0 0 } }$$: $$\frac{300+500}{1000+1000}=\frac{400}{1000}$$.
In the second case, we can achieve concentration $${ \frac { 5 0 } { 1 0 0 0 } }$$ using two liters of $${ \frac { 2 5 } { 1 0 0 0 } }$$ type and one liter of $${ \frac { 1 0 0 } { 1 0 0 0 } }$$ type: $$\frac{25+25+100}{3 \cdot 1000} = \frac{50}{1000}$$.
|
[{"input": "400 4\n100 300 450 500", "output": "2"}, {"input": "50 2\n100 25", "output": "3"}]
| 2,300
|
["dfs and similar", "graphs", "shortest paths"]
| 48
|
[{"input": "400 4\r\n100 300 450 500\r\n", "output": "2\r\n"}, {"input": "50 2\r\n100 25\r\n", "output": "3\r\n"}, {"input": "500 3\r\n1000 5 5\r\n", "output": "199\r\n"}, {"input": "500 1\r\n1000\r\n", "output": "-1\r\n"}, {"input": "874 3\r\n873 974 875\r\n", "output": "2\r\n"}, {"input": "999 2\r\n1 1000\r\n", "output": "999\r\n"}, {"input": "326 18\r\n684 49 373 57 747 132 441 385 640 575 567 665 323 515 527 656 232 701\r\n", "output": "3\r\n"}, {"input": "314 15\r\n160 769 201 691 358 724 248 47 420 432 667 601 596 370 469\r\n", "output": "4\r\n"}, {"input": "0 1\r\n0\r\n", "output": "1\r\n"}, {"input": "0 1\r\n1000\r\n", "output": "-1\r\n"}, {"input": "345 5\r\n497 135 21 199 873\r\n", "output": "5\r\n"}, {"input": "641 8\r\n807 1000 98 794 536 845 407 331\r\n", "output": "7\r\n"}, {"input": "852 10\r\n668 1000 1000 1000 1000 1000 1000 639 213 1000\r\n", "output": "10\r\n"}, {"input": "710 7\r\n854 734 63 921 921 187 978\r\n", "output": "5\r\n"}, {"input": "134 6\r\n505 10 1 363 344 162\r\n", "output": "4\r\n"}, {"input": "951 15\r\n706 1000 987 974 974 706 792 792 974 1000 1000 987 974 953 953\r\n", "output": "6\r\n"}, {"input": "834 10\r\n921 995 1000 285 1000 166 1000 999 991 983\r\n", "output": "10\r\n"}, {"input": "917 21\r\n999 998 1000 997 1000 998 78 991 964 985 987 78 985 999 83 987 1000 999 999 78 83\r\n", "output": "12\r\n"}, {"input": "971 15\r\n692 1000 1000 997 1000 691 996 691 1000 1000 1000 692 1000 997 1000\r\n", "output": "11\r\n"}, {"input": "971 108\r\n706 706 991 706 988 997 996 997 991 996 706 706 996 706 996 984 1000 991 996 1000 724 724 997 991 997 984 997 1000 984 996 996 997 724 997 997 1000 997 724 984 997 996 988 997 706 706 997 1000 991 706 988 997 724 988 706 996 706 724 997 988 996 991 1000 1000 724 988 996 1000 988 984 996 991 724 706 988 991 724 1000 1000 991 984 984 706 724 706 988 724 984 984 991 988 991 706 997 984 984 1000 706 724 988 984 996 1000 988 997 984 724 991 991\r\n", "output": "10\r\n"}, {"input": "1000 16\r\n536 107 113 397 613 1 535 652 730 137 239 538 764 431 613 273\r\n", "output": "-1\r\n"}, {"input": "998 2\r\n1 1000\r\n", "output": "999\r\n"}, {"input": "998 3\r\n1 999 1000\r\n", "output": "500\r\n"}, {"input": "998 4\r\n1 2 999 1000\r\n", "output": "499\r\n"}, {"input": "500 2\r\n1000 2\r\n", "output": "499\r\n"}, {"input": "508 15\r\n0 998 997 1 1 2 997 1 997 1000 0 3 3 2 4\r\n", "output": "53\r\n"}, {"input": "492 2\r\n706 4\r\n", "output": "351\r\n"}, {"input": "672 5\r\n4 6 1000 995 997\r\n", "output": "46\r\n"}, {"input": "410 4\r\n998 8 990 990\r\n", "output": "54\r\n"}, {"input": "499 2\r\n1000 2\r\n", "output": "998\r\n"}, {"input": "995 5\r\n996 997 998 999 1000\r\n", "output": "-1\r\n"}, {"input": "500 3\r\n499 1000 300\r\n", "output": "7\r\n"}, {"input": "499 2\r\n0 1000\r\n", "output": "1000\r\n"}, {"input": "1000 10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "501 2\r\n1 1000\r\n", "output": "999\r\n"}]
| false
|
stdio
| null | true
|
985/C
|
985
|
C
|
Python 3
|
TESTS
| 6
| 155
| 7,987,200
|
38547223
|
from bisect import*
R=lambda:map(int,input().split())
n,k,l=R()
a=sorted(R())
t=bisect_right(a,a[0]+l)
print(int(t>=n)and a[0]+sum(a[t-n+1:t]))
| 50
| 155
| 8,089,600
|
38632447
|
from bisect import bisect
def main():
n, k, d = map(int, input().split())
l = sorted(map(int, input().split()))
if l[0] + d < l[n - 1]:
print(0)
return
i = bisect(l, l[0] + d)
a = 0 if k == 1 else (n * k - i) // (k - 1)
print(sum(l[:i - a:k]) + sum(l[i - a:i]))
if __name__ == '__main__':
main()
|
Educational Codeforces Round 44 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Liebig's Barrels
|
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
|
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
|
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
| null |
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
|
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
| 1,500
|
["greedy"]
| 50
|
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
985/C
|
985
|
C
|
Python 3
|
TESTS
| 6
| 155
| 8,192,000
|
38651840
|
def main():
n, k, l = map(int, input().split())
arr = sorted(list(map(int, input().split())))
if arr[n - 1] - arr[0] <= l:
print(sum(arr[:n]))
else:
print("0")
if __name__ == "__main__":
main()
| 50
| 171
| 12,902,400
|
133789670
|
n, k, l = map(int, input().split())
L = sorted(map(int, input().split()))
a = []
for i in L:
if i <= L[0] + l:
a.append(i)
l1 = len(a)
if l1 < n:
print(0)
elif l1 == n:
print(sum(a))
else:
s = 0
i = 0
while i < l1:
s += a[i]
n -= 1
i = max(min(i + k, l1 - n), i + 1)
print(s)
|
Educational Codeforces Round 44 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Liebig's Barrels
|
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
|
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
|
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
| null |
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
|
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
| 1,500
|
["greedy"]
| 50
|
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
1004/D
|
1004
|
D
|
Python 3
|
TESTS
| 11
| 1,918
| 63,078,400
|
48306076
|
import math
t = int(input())
L = [int(x) for x in input().split()]
L.sort()
ma = max(L)
S = 2*sum(L)
Div = []
for i in range(1,int(math.sqrt(t))+1):
if t%i == 0:
Div.append(i)
Div.append(t//i)
if len(Div) >= 2:
if Div[-1] == Div[-2]:
Div.pop()
Div.sort()
Candidates = []
for i in range(len(Div)):
n = Div[i]
m = Div[-i-1]
T = m+n-ma
low = 1
high = n
while high - low > 1:
x = (high+low)//2
if m*(x*(x-1)+(n-x)*(n-x+1))+n*((T-x)*(T-x-1)+(m-T+x)*(m-T+x+1)) <= S:
low = x
else:
high = x
x = low
if m*(x*(x-1)+(n-x)*(n-x+1))+n*((T-x)*(T-x-1)+(m-T+x)*(m-T+x+1)) == S:
Candidates.append((n,m,x,T-x))
else:
x = high
if m*(x*(x-1)+(n-x)*(n-x+1))+n*((T-x)*(T-x-1)+(m-T+x)*(m-T+x+1)) == S:
Candidates.append((n,m,x,T-x))
if len(Candidates) == 0:
print(-1)
else:
for i in Candidates:
n,m,x,y = i
temp = []
for j in range(m*n):
temp.append(abs(1+(j%n)-x)+abs(1+(j//n)-y))
temp.sort()
if temp == L:
print(n,m)
print(x,y)
break
else:
print(-1)
| 47
| 358
| 79,564,800
|
142294522
|
import sys
input = sys.stdin.buffer.readline
def test(n, m, x, y, d):
d2 = [0 for i in range(len(d))]
for i in range(n):
for j in range(m):
D = abs(i-x)+abs(j-y)
if D >= len(d2):
return False
d2[D]+=1
return d==d2
def process(A):
t = len(A)
if t==1 and A==[0]:
return [1, 1, 1, 1]
M = max(A)
d = [0 for i in range(M+1)]
for x in A:
d[x]+=1
if d[0] != 1:
return [-1, None, None, None]
first_fail = None
for i in range(1, M+1):
if d[i] != 4*i:
first_fail = i
break
if first_fail is None:
return [-1, None, None, None]
x = first_fail-1
#closest distance of (a, b) to an edge
#is uh first_fail-1
#(n, m, x, y)
#I think we can flip around so farthest corner is at (0, m-1)
#so n-1-x+m-1-y = M
#y = x+m-1-M
#m-y = M-x+1
#and uh
#n*m = t
for m in range(1, t+1):
if t % m==0:
n = t//m
y = n-1-x+m-1-M
if 0 <= x <= n-1 and 0 <= y <= m-1 and M == max(
abs(x)+abs(y), abs(x)+abs(m-1-y), abs(n-1-x)+abs(y), abs(n-1-x)+abs(m-1-y)):
if test(n, m, x, y, d):
return [n, m, x+1, y+1]
return [-1, None, None, None]
t = int(input())
A = [int(x) for x in input().split()]
n, m, x, y = process(A)
if n==-1:
print('-1')
else:
print(f'{n} {m}')
print(f'{x} {y}')
|
Codeforces Round 495 (Div. 2)
|
CF
| 2,018
| 2
| 256
|
Sonya and Matrix
|
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.
Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.
The Manhattan distance between two cells ($$$x_1$$$, $$$y_1$$$) and ($$$x_2$$$, $$$y_2$$$) is defined as $$$|x_1 - x_2| + |y_1 - y_2|$$$. For example, the Manhattan distance between the cells $$$(5, 2)$$$ and $$$(7, 1)$$$ equals to $$$|5-7|+|2-1|=3$$$.
Example of a rhombic matrix.
Note that rhombic matrices are uniquely defined by $$$n$$$, $$$m$$$, and the coordinates of the cell containing the zero.
She drew a $$$n\times m$$$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $$$n\cdot m$$$ numbers). Note that Sonya will not give you $$$n$$$ and $$$m$$$, so only the sequence of numbers in this matrix will be at your disposal.
Write a program that finds such an $$$n\times m$$$ rhombic matrix whose elements are the same as the elements in the sequence in some order.
|
The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^6$$$) — the number of cells in the matrix.
The second line contains $$$t$$$ integers $$$a_1, a_2, \ldots, a_t$$$ ($$$0\leq a_i< t$$$) — the values in the cells in arbitrary order.
|
In the first line, print two positive integers $$$n$$$ and $$$m$$$ ($$$n \times m = t$$$) — the size of the matrix.
In the second line, print two integers $$$x$$$ and $$$y$$$ ($$$1\leq x\leq n$$$, $$$1\leq y\leq m$$$) — the row number and the column number where the cell with $$$0$$$ is located.
If there are multiple possible answers, print any of them. If there is no solution, print the single integer $$$-1$$$.
| null |
You can see the solution to the first example in the legend. You also can choose the cell $$$(2, 2)$$$ for the cell where $$$0$$$ is located. You also can choose a $$$5\times 4$$$ matrix with zero at $$$(4, 2)$$$.
In the second example, there is a $$$3\times 6$$$ matrix, where the zero is located at $$$(2, 3)$$$ there.
In the third example, a solution does not exist.
|
[{"input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4", "output": "4 5\n2 2"}, {"input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1", "output": "3 6\n2 3"}, {"input": "6\n2 1 0 2 1 2", "output": "-1"}]
| 2,300
|
["brute force", "constructive algorithms", "implementation"]
| 47
|
[{"input": "20\r\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\r\n", "output": "4 5\r\n2 2\r\n"}, {"input": "18\r\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\r\n", "output": "3 6\r\n2 3\r\n"}, {"input": "6\r\n2 1 0 2 1 2\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "1 1\r\n1 1\r\n"}, {"input": "7\r\n0 1 2 3 4 2 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n0 0 0 0 0 0\r\n", "output": "-1\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "-1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def read_ints(file):
return list(map(int, file.read().split()))
def main(input_path, output_path, submission_output_path):
with open(input_path) as f:
input_lines = f.read().splitlines()
t = int(input_lines[0])
a = list(map(int, input_lines[1].split()))
a_counts = defaultdict(int)
for num in a:
a_counts[num] += 1
max_d = max(a) if a else 0
zero_count = a_counts.get(0, 0)
with open(submission_output_path) as f:
submission_lines = [line.strip() for line in f.readlines() if line.strip()]
if not submission_lines:
print(0)
return
if submission_lines[0] == '-1':
if zero_count != 1:
print(100)
return
factors = []
for i in range(1, int(t**0.5) + 1):
if t % i == 0:
factors.append((i, t // i))
if i != t // i:
factors.append((t // i, i))
seen = set()
unique_factors = []
for n, m in factors:
if (n, m) not in seen:
seen.add((n, m))
unique_factors.append((n, m))
solution_found = False
for n, m in unique_factors:
if (n + m - 2) < max_d:
continue
a_val = n + m - 2 - max_d
if a_val < 0:
continue
x_plus_y = a_val + 2
x_min = max(1, x_plus_y - m)
x_max = min(n, x_plus_y - 1)
if x_min > x_max:
continue
for x in range(x_min, x_max + 1):
y = x_plus_y - x
if y < 1 or y > m:
continue
expected_counts = defaultdict(int)
expected_counts[0] = 1
for i in range(1, n + 1):
for j in range(1, m + 1):
if i == x and j == y:
continue
d = abs(i - x) + abs(j - y)
expected_counts[d] += 1
valid = True
if expected_counts[0] != zero_count:
valid = False
for d, cnt in expected_counts.items():
if a_counts.get(d, 0) != cnt:
valid = False
break
for d in a_counts:
if d != 0 and a_counts[d] != expected_counts.get(d, 0):
valid = False
break
if valid:
solution_found = True
break
if solution_found:
break
if solution_found:
break
print(0 if solution_found else 100)
return
else:
if len(submission_lines) < 2:
print(0)
return
try:
n, m = map(int, submission_lines[0].split())
x, y = map(int, submission_lines[1].split())
except:
print(0)
return
if n * m != t or x < 1 or x > n or y < 1 or y > m:
print(0)
return
expected_counts = defaultdict(int)
expected_counts[0] = 1
for i in range(1, n + 1):
for j in range(1, m + 1):
if i == x and j == y:
continue
d = abs(i - x) + abs(j - y)
expected_counts[d] += 1
valid = True
if expected_counts[0] != zero_count:
valid = False
for d, cnt in expected_counts.items():
if a_counts.get(d, 0) != cnt:
valid = False
break
for d in a_counts:
if d != 0 and a_counts[d] != expected_counts.get(d, 0):
valid = False
break
print(100 if valid else 0)
return
if __name__ == "__main__":
input_path, output_path, submission_output_path = sys.argv[1:4]
main(input_path, output_path, submission_output_path)
| true
|
985/C
|
985
|
C
|
Python 3
|
TESTS
| 6
| 155
| 8,089,600
|
38592637
|
def main():
n, k, l = map(int, input().split())
a = sorted(map(int, input().split()))[:n]
print(sum(a) * (a[-1] - a[0] <= l))
if __name__ == '__main__':
main()
| 50
| 171
| 17,510,400
|
202628015
|
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import Counter
N,K,L = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
B = []
while len(A)>N and A[-1]>A[0]+L:
B.append(A.pop())
if A[-1]>A[0]+L:
exit(print(0))
# print(A)
# print(B)
ans = 0
while A:
cnt = 0
while B and cnt<K-1:
B.pop()
cnt+=1
tmp = float('inf')
while cnt<K and A:
tmp = min(tmp, A.pop())
cnt+=1
#print(A,B,tmp)
ans+=tmp
print(ans)
|
Educational Codeforces Round 44 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Liebig's Barrels
|
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
|
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
|
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
| null |
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2, 5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
|
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
| 1,500
|
["greedy"]
| 50
|
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
PyPy 3
|
TESTS
| 8
| 327
| 10,649,600
|
78525444
|
from sys import stdin
from collections import defaultdict
def solve():
n = int(stdin.readline())
energy = defaultdict(int)
maxn = 0
for i in range(n):
a, b, = tuple(map(int, stdin.readline().split(" ")))
maxn = max(maxn, a)
energy[a] = b
ans = 0
table = [1] * (maxn + 1)
for i in range(maxn + 1):
if i in energy:
if energy[i] < i:
table[i] = table[i - 1 - energy[i]] + 1
else:
table[i] = 1
else:
table[i] = table[i - 1]
ans = max(ans, table[i])
#print(table, n - ans)
print(n - ans)
if __name__ == "__main__":
solve()
| 41
| 373
| 11,059,200
|
230287206
|
n = int(input())
bb = [0] * 1000001
for _ in range(n):
a_and_b = input()
a, b = a_and_b.split() # n - кол-во целых чисел, k - кол-во элем. заданной посл-ти меньше либо равны x(результат программы)
a = int(a)
b = int(b)
bb[a] = b
a = 0
for i, b in enumerate(bb):
if b > 0:
if i > b:
a = (bb[i - b - 1] + 1)
else:
a = 1
bb[i] = a
print(n - max(bb))
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
Python 3
|
TESTS
| 8
| 358
| 14,028,800
|
123394164
|
from bisect import bisect_left as bl
def f(a,b):
cst=0
dp=[0]*(len(a)+1)
bad=[0]*(len(a))
i=len(a)-1
ans=float("inf")
for i in range(len(a)):
# print("upar i ",i)
l=a[i]
r=b[i]
rr=bl(a,l-r)
# print(i-rr)
if rr==0:
dp[i]=i
else:
dp[i]=i-rr+dp[rr-1]
ans=min(ans,len(a)-1-i+dp[i])
return ans
a=[]
b=[]
for i in range(int(input())):
l,r=map(int,input().strip().split())
a.append(l)
b.append(r)
print(f(a,b))
| 41
| 467
| 10,342,400
|
67523977
|
from sys import stdin
item=lambda:stdin.readline().split()
n=int(stdin.readline())
lst,d=[],{}
for _ in range(n):
a,b=map(int,item())
d[a]=b
lst.append(a)
lst.sort()
res=[0]*n
from bisect import bisect_left as bis
for i,x in enumerate(lst):
elem=bis(lst,x-d[x])-1
if elem==-1:res[i]=i;continue
res[i]=res[elem]+i-elem-1
for i,x in enumerate(lst):
res[i]+=(n-1-i)
print(min(res))
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
463/B
|
463
|
B
|
PyPy 3-64
|
TESTS
| 11
| 62
| 13,516,800
|
185176172
|
n=int(input())
m=list(map(int,input().split()))
sum=0-m[0]
count=0
if(n==1):
print(abs(sum))
else:
for i in range(n - 1):
if (sum < 0):
count += abs(sum)
sum += abs(sum)
sum = sum + (m[i] - m[i + 1])
print(count)
| 49
| 61
| 13,107,200
|
226982327
|
int(input())
a = [int(_) for _ in input().split()]
print(max(a))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
463/B
|
463
|
B
|
Python 3
|
TESTS
| 9
| 265
| 6,348,800
|
70524300
|
c=0
z=int(input())
x=list(map(int,input().split()))
dollers=x[0]
for i in range(z-1):
if (x[i]-x[i+1])<0 and c-abs(x[i]-x[i+1])<0:
dollers+=abs(x[i]-x[i+1])-c
c=0
elif (x[i]-x[i+1])>0:
c+=(x[i]-x[i+1])
elif (x[i]-x[i+1])<0 and c-abs(x[i]-x[i+1])>0:
c-=abs(x[i]-x[i+1])
print(dollers)
| 49
| 62
| 5,529,600
|
170419342
|
print(max(map(int,[*open(0)][1].split())))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
391/B
|
391
|
B
|
PyPy 3
|
TESTS
| 3
| 124
| 0
|
48091695
|
a = str(input())
anss = []
for i in range(len(a)):
anss.append(a.count(a[i]))
print(max(anss)//2+1)
| 25
| 31
| 0
|
177228846
|
import sys
#sys.stdin = open("input.txt", "r")
a = [-1]*91
b = [0]*91
i = 0
for c in input():
c = ord(c)
b[c] += a[c] < 0 or i != a[c]
a[c] = i
i ^= 1
print(max(b))
|
Rockethon 2014
|
ICPC
| 2,014
| 1
| 256
|
Word Folding
|
You will receive 5 points for solving this problem.
Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold. Using this operation, Manao converts the string into a structure that has one more level than there were fold operations performed. See the following examples for clarity.
We will denote the positions of folds with '|' characters. For example, the word "ABRACADABRA" written as "AB|RACA|DAB|RA" indicates that it has been folded three times: first, between the leftmost pair of 'B' and 'R' letters; second, between 'A' and 'D'; and third, between the rightmost pair of 'B' and 'R' letters. Here are several examples of folded strings:
One last example for "ABCD|EFGH|IJ|K":
Manao noticed that each folded string can be viewed as several piles of letters. For instance, in the previous example, there are four piles, which can be read as "AHI", "BGJK", "CF", and "DE" from bottom to top. Manao wonders what is the highest pile of identical letters he can build using fold operations on a given word. Note that the pile should not contain gaps and should start at the bottom level. For example, in the rightmost of the four examples above, none of the piles would be considered valid since each of them has gaps, starts above the bottom level, or both.
|
The input will consist of one line containing a single string of n characters with 1 ≤ n ≤ 1000 and no spaces. All characters of the string will be uppercase letters.
This problem doesn't have subproblems. You will get 5 points for the correct submission.
|
Print a single integer — the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string.
| null |
Consider the first example. Manao can create a pile of three 'A's using the folding "AB|RACAD|ABRA", which results in the following structure:
In the second example, Manao can create a pile of three 'B's using the following folding: "AB|BB|CBDB".
Another way for Manao to create a pile of three 'B's with "ABBBCBDB" is the following folding: "AB|B|BCBDB".
In the third example, there are no folds performed and the string is just written in one line.
|
[{"input": "ABRACADABRA", "output": "3"}, {"input": "ABBBCBDB", "output": "3"}, {"input": "AB", "output": "1"}]
| null |
["brute force"]
| 25
|
[{"input": "ABRACADABRA\r\n", "output": "3\r\n"}, {"input": "ABBBCBDB\r\n", "output": "3\r\n"}, {"input": "AB\r\n", "output": "1\r\n"}, {"input": "ABBCDEFB\r\n", "output": "3\r\n"}, {"input": "THISISATEST\r\n", "output": "3\r\n"}, {"input": "Z\r\n", "output": "1\r\n"}, {"input": "ZZ\r\n", "output": "2\r\n"}, {"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n", "output": "1\r\n"}, {"input": "ABACBCABACACB\r\n", "output": "4\r\n"}, {"input": "LEHLLLLLLHAFGEGLLHAFDLHHLLLLLDGGEHGGHLLLLLLLLDFLCBLLEFLLCBLLCGLEDLGGLECLDGLEHLLLGELLLEGLLLLGDLLLDALD\r\n", "output": "49\r\n"}, {"input": "THISISTHELASTTEST\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
390/A
|
390
|
A
|
Python 3
|
TESTS
| 8
| 452
| 1,945,600
|
120336279
|
j= int(input())
x=[]
y=[]
for i in range(j):
f=list(map(int,input().split(' ')))
x.append(f[0])
y.append(f[1])
x1=[]
y1=[]
for i in range(j):
if x[i] not in x1:
x1.append(x[i])
if y[i] not in y1:
y1.append(y[i])
a=len(x1)
b=len(y1)
if x1>=y1:
print(b)
else:
print(a)
| 19
| 186
| 0
|
176182857
|
numOfAlarms = int(input())
columns = set()
rows = set()
for i in range(numOfAlarms):
x = input().split(' ')
columns.add(x[0])
rows.add(x[1])
print(min(len(columns), len(rows)))
|
Codeforces Round 229 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and Alarm Clock
|
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.
The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:
- First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
- Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.
Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
|
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100).
Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
|
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
| null |
In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.
In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.
|
[{"input": "4\n0 0\n0 1\n0 2\n1 0", "output": "2"}, {"input": "4\n0 0\n0 1\n1 0\n1 1", "output": "2"}, {"input": "4\n1 1\n1 2\n2 3\n3 3", "output": "3"}]
| null |
["implementation"]
| 19
|
[{"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 0\r\n", "output": "2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1\r\n1 2\r\n2 3\r\n3 3\r\n", "output": "3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "1\r\n"}, {"input": "42\r\n28 87\r\n26 16\r\n59 90\r\n47 61\r\n28 83\r\n36 30\r\n67 10\r\n6 95\r\n9 49\r\n86 94\r\n52 24\r\n74 9\r\n86 24\r\n28 51\r\n25 99\r\n40 98\r\n57 33\r\n18 96\r\n43 36\r\n3 79\r\n4 86\r\n38 61\r\n25 61\r\n6 100\r\n58 81\r\n28 19\r\n64 4\r\n3 40\r\n2 56\r\n41 49\r\n97 100\r\n86 34\r\n42 36\r\n44 40\r\n14 85\r\n21 60\r\n76 99\r\n64 47\r\n69 13\r\n49 37\r\n97 37\r\n3 70\r\n", "output": "31\r\n"}, {"input": "21\r\n54 85\r\n69 37\r\n42 87\r\n53 18\r\n28 22\r\n13 3\r\n62 97\r\n38 91\r\n67 19\r\n100 79\r\n29 18\r\n48 40\r\n68 84\r\n44 20\r\n37 34\r\n73 53\r\n21 5\r\n20 73\r\n24 94\r\n23 52\r\n7 55\r\n", "output": "20\r\n"}, {"input": "19\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n1 11\r\n1 12\r\n1 13\r\n1 14\r\n1 15\r\n1 16\r\n1 17\r\n1 18\r\n1 19\r\n", "output": "1\r\n"}, {"input": "12\r\n1 1\r\n1 3\r\n1 5\r\n2 1\r\n2 2\r\n2 4\r\n3 1\r\n3 3\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
390/A
|
390
|
A
|
Python 3
|
TESTS
| 8
| 483
| 1,638,400
|
12220225
|
N=int(input())
Hor=Ver=1
H=[]
V=[]
for I in range(N):
Tmp=list(map(int,input().split()))
H+=[Tmp[0]]
V+=[Tmp[0]]
H.sort()
V.sort()
Tmp1=H[0]
Tmp2=V[0]
for I in range(1,N):
Hor+=1 if not Tmp1==H[I] else 0
Ver+=1 if not Tmp2==V[I] else 0
Tmp1=H[I]
Tmp2=V[I]
print(min(Hor,Ver))
| 19
| 186
| 6,860,800
|
126929669
|
def alarm_clock():
count = int(input())
x_set = set()
y_set = set()
for _ in range(count):
coordinate = input().split()
x_set.add(coordinate[0])
y_set.add(coordinate[1])
moves = min(len(x_set), len(y_set))
print(moves)
alarm_clock()
|
Codeforces Round 229 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and Alarm Clock
|
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.
The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:
- First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
- Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.
Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
|
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100).
Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
|
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
| null |
In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.
In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.
|
[{"input": "4\n0 0\n0 1\n0 2\n1 0", "output": "2"}, {"input": "4\n0 0\n0 1\n1 0\n1 1", "output": "2"}, {"input": "4\n1 1\n1 2\n2 3\n3 3", "output": "3"}]
| null |
["implementation"]
| 19
|
[{"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 0\r\n", "output": "2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1\r\n1 2\r\n2 3\r\n3 3\r\n", "output": "3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "1\r\n"}, {"input": "42\r\n28 87\r\n26 16\r\n59 90\r\n47 61\r\n28 83\r\n36 30\r\n67 10\r\n6 95\r\n9 49\r\n86 94\r\n52 24\r\n74 9\r\n86 24\r\n28 51\r\n25 99\r\n40 98\r\n57 33\r\n18 96\r\n43 36\r\n3 79\r\n4 86\r\n38 61\r\n25 61\r\n6 100\r\n58 81\r\n28 19\r\n64 4\r\n3 40\r\n2 56\r\n41 49\r\n97 100\r\n86 34\r\n42 36\r\n44 40\r\n14 85\r\n21 60\r\n76 99\r\n64 47\r\n69 13\r\n49 37\r\n97 37\r\n3 70\r\n", "output": "31\r\n"}, {"input": "21\r\n54 85\r\n69 37\r\n42 87\r\n53 18\r\n28 22\r\n13 3\r\n62 97\r\n38 91\r\n67 19\r\n100 79\r\n29 18\r\n48 40\r\n68 84\r\n44 20\r\n37 34\r\n73 53\r\n21 5\r\n20 73\r\n24 94\r\n23 52\r\n7 55\r\n", "output": "20\r\n"}, {"input": "19\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n1 11\r\n1 12\r\n1 13\r\n1 14\r\n1 15\r\n1 16\r\n1 17\r\n1 18\r\n1 19\r\n", "output": "1\r\n"}, {"input": "12\r\n1 1\r\n1 3\r\n1 5\r\n2 1\r\n2 2\r\n2 4\r\n3 1\r\n3 3\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
390/A
|
390
|
A
|
Python 3
|
TESTS
| 8
| 311
| 0
|
15468427
|
from math import *
n = int(input())
a = set()
for i in range(n):
x,y = map(int,input().split())
a.add(x)
print(len(a))
| 19
| 187
| 5,734,400
|
152267608
|
def solve():
r=set()
c=set()
for i in arr :
r.add(i[0])
c.add(i[1])
return min(len(r),len(c))
from sys import stdin
input = stdin.readline
n=int(input())
arr=[]
for i in range(n):
arr.append([int(x) for x in input().split()])
print(solve())
'''
for _ in range(int(input())) :
n=int(input())
print(solve())
n=int(input())
x=input().strip()
n,m= [int(x) for x in input().split()]
n=int(input())
arr=[int(x) for x in input().split()]
n,m= [int(x) for x in input().split()]
arr=[]
for i in range(n):
arr.append([int(x) for x in input().split()])
n,m= [int(x) for x in input().split()]
arr=[]
for i in range(n):
arr.append([x for x in input().strip()])
n=int(input())
arr=[]
for i in range(n):
arr.append([int(x) for x in input().split()])
'''
|
Codeforces Round 229 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and Alarm Clock
|
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.
The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:
- First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
- Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.
Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
|
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100).
Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
|
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
| null |
In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.
In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.
|
[{"input": "4\n0 0\n0 1\n0 2\n1 0", "output": "2"}, {"input": "4\n0 0\n0 1\n1 0\n1 1", "output": "2"}, {"input": "4\n1 1\n1 2\n2 3\n3 3", "output": "3"}]
| null |
["implementation"]
| 19
|
[{"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 0\r\n", "output": "2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1\r\n1 2\r\n2 3\r\n3 3\r\n", "output": "3\r\n"}, {"input": "1\r\n0 0\r\n", "output": "1\r\n"}, {"input": "42\r\n28 87\r\n26 16\r\n59 90\r\n47 61\r\n28 83\r\n36 30\r\n67 10\r\n6 95\r\n9 49\r\n86 94\r\n52 24\r\n74 9\r\n86 24\r\n28 51\r\n25 99\r\n40 98\r\n57 33\r\n18 96\r\n43 36\r\n3 79\r\n4 86\r\n38 61\r\n25 61\r\n6 100\r\n58 81\r\n28 19\r\n64 4\r\n3 40\r\n2 56\r\n41 49\r\n97 100\r\n86 34\r\n42 36\r\n44 40\r\n14 85\r\n21 60\r\n76 99\r\n64 47\r\n69 13\r\n49 37\r\n97 37\r\n3 70\r\n", "output": "31\r\n"}, {"input": "21\r\n54 85\r\n69 37\r\n42 87\r\n53 18\r\n28 22\r\n13 3\r\n62 97\r\n38 91\r\n67 19\r\n100 79\r\n29 18\r\n48 40\r\n68 84\r\n44 20\r\n37 34\r\n73 53\r\n21 5\r\n20 73\r\n24 94\r\n23 52\r\n7 55\r\n", "output": "20\r\n"}, {"input": "19\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n1 8\r\n1 9\r\n1 10\r\n1 11\r\n1 12\r\n1 13\r\n1 14\r\n1 15\r\n1 16\r\n1 17\r\n1 18\r\n1 19\r\n", "output": "1\r\n"}, {"input": "12\r\n1 1\r\n1 3\r\n1 5\r\n2 1\r\n2 2\r\n2 4\r\n3 1\r\n3 3\r\n3 5\r\n4 1\r\n4 2\r\n4 3\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
620/D
|
620
|
D
|
PyPy 3-64
|
TESTS
| 7
| 1,153
| 80,998,400
|
175719617
|
from sys import stdin
input=lambda :stdin.readline()[:-1]
n=int(input())
a=list(map(int,input().split()))
m=int(input())
b=list(map(int,input().split()))
sa=sum(a)
sb=sum(b)
S=[]
for i in range(n):
for j in range(i+1,n):
S.append((a[i]+a[j],i,j))
S.sort(key=lambda x:x[0])
T=[]
for i in range(m):
for j in range(i+1,m):
T.append((b[i]+b[j],i,j))
T.sort(key=lambda x:x[0])
tmp=abs(sa-sb)
ans=[]
for i in range(n):
for j in range(m):
score=abs((sa-2*a[i])-(sb-2*b[j]))
if score<tmp:
tmp=score
ans=[[i,j]]
now=0
nS=len(S)
nT=len(T)
for i in range(nS):
while now<nT-1 and ((sa-2*S[i][0])-(sb-2*T[now+1][0]))>=0:
now+=1
if now<nT and 0<=((sa-2*S[i][0])-(sb-2*T[now][0]))<tmp:
tmp=((sa-2*S[i][0])-(sb-2*T[now][0]))
p,q=S[i][1:]
r,s=T[now][1:]
ans=[[p,r],[q,s]]
now=0
for i in range(nT):
while now<nS-1 and ((sb-2*T[i][0])-(sa-2*S[now+1][0]))>=0:
now+=1
if now<nS and 0<=((sb-2*T[i][0])-(sa-2*S[now][0]))<tmp:
tmp=((sb-2*T[i][0])-(sa-2*S[now][0]))
p,q=S[now][1:]
r,s=T[i][1:]
ans=[[p,r],[q,s]]
print(tmp)
print(len(ans))
for i,j in ans:
print(i+1,j+1)
| 24
| 2,995
| 152,166,400
|
92182162
|
from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
sum_a, sum_b = sum(a), sum(b)
delta = sum_b - sum_a
ans = abs(delta)
ans_swap = []
for i in range(n):
for j in range(m):
if abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) < ans:
ans = abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j]))
ans_swap = [(i+1, j+1)]
d = dict()
for i in range(m):
for j in range(i+1, m):
d[b[i]+b[j]] = (i+1, j+1)
minf, inf = -10**13, 10**13
val = [minf, minf] + sorted(d.keys()) + [inf, inf]
for i in range(n):
for j in range(i+1, n):
ap = a[i] + a[j]
req = (delta + ap*2) >> 1
k = bisect_left(val, req)
for k in range(k-1, k+2):
if abs(delta + ap*2 - val[k]*2) < ans:
ans = abs(delta + ap*2 - val[k]*2)
ans_swap = [(i+1, d[val[k]][0]), (j+1, d[val[k]][1])]
print(ans)
print(len(ans_swap))
for x, y in ans_swap:
print(x, y)
|
Educational Codeforces Round 6
|
ICPC
| 2,016
| 3
| 256
|
Professor GukiZ and Two Arrays
|
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.
In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].
Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
|
The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.
The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.
The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.
|
In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.
The second line should contain the number of swaps k (0 ≤ k ≤ 2).
Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.
If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
| null | null |
[{"input": "5\n5 4 3 2 1\n4\n1 1 1 1", "output": "1\n2\n1 1\n4 2"}, {"input": "5\n1 2 3 4 5\n1\n15", "output": "0\n0"}, {"input": "5\n1 2 3 4 5\n4\n1 2 3 4", "output": "1\n1\n3 1"}]
| 2,200
|
["binary search", "two pointers"]
| 24
|
[{"input": "5\r\n5 4 3 2 1\r\n4\r\n1 1 1 1\r\n", "output": "1\r\n2\r\n1 1\r\n4 2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n15\r\n", "output": "0\r\n0\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n4\r\n1 2 3 4\r\n", "output": "1\r\n1\r\n3 1\r\n"}, {"input": "1\r\n-42\r\n1\r\n-86\r\n", "output": "44\r\n0\r\n"}, {"input": "1\r\n-21\r\n10\r\n-43 6 -46 79 -21 93 -36 -38 -67 1\r\n", "output": "1\r\n1\r\n1 3\r\n"}, {"input": "10\r\n87 -92 -67 -100 -88 80 -82 -59 81 -72\r\n10\r\n-50 30 30 77 65 92 -60 -76 -29 -15\r\n", "output": "0\r\n2\r\n4 4\r\n9 6\r\n"}, {"input": "6\r\n1 2 3 4 5 11\r\n1\r\n3\r\n", "output": "7\r\n1\r\n6 1\r\n"}, {"input": "2\r\n-2 -17\r\n2\r\n11 -9\r\n", "output": "5\r\n1\r\n1 1\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
a = list(map(int, f.readline().strip().split()))
m = int(f.readline().strip())
b = list(map(int, f.readline().strip().split()))
# Read reference output to get correct minimal v
with open(output_path) as f:
ref_v = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = [line.strip() for line in f if line.strip()]
if not lines:
print(0)
return
# Parse submission's v
try:
sub_v = int(lines[0])
except ValueError:
print(0)
return
if sub_v != ref_v:
print(0)
return
# Parse number of swaps
if len(lines) < 2:
print(0)
return
try:
k = int(lines[1])
except ValueError:
print(0)
return
if k < 0 or k > 2:
print(0)
return
if len(lines) != 2 + k:
print(0)
return
# Parse swaps
swaps = []
for i in range(2, 2 + k):
parts = lines[i].split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except ValueError:
print(0)
return
if x < 1 or x > n or y < 1 or y > m:
print(0)
return
swaps.append((x, y))
# Apply swaps
current_a = a.copy()
current_b = b.copy()
for x, y in swaps:
i = x - 1
j = y - 1
current_a[i], current_b[j] = current_b[j], current_a[i]
# Compute sum difference
sum_a = sum(current_a)
sum_b = sum(current_b)
computed_v = abs(sum_a - sum_b)
if computed_v == sub_v:
print(1)
else:
print(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: checker.py input_path output_path submission_output_path")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
698/B
|
698
|
B
|
Python 3
|
TESTS
| 4
| 77
| 7,168,000
|
128871677
|
from collections import defaultdict,deque
n=int(input())
a=list(map(int,input().split()))
graph=defaultdict(list)
for i in range(n):
graph[i+1].append(a[i])
graph[a[i]].append((i+1))
vis=[False]*(n+1)
head=-1
for i in range(n):
if a[i]==i+1:
head=i+1
break
b=a.copy()
if head==-1:
head=1
b[0]=1
for i in range(1,n+1):
if vis[i]==False:
tmp_head=-1
last=-1
q=deque()
q.append(i)
vis[i]=True
if i==a[i-1]:
tmp_head=i
while q:
s=q.popleft()
for node in graph[s]:
if node==b[node-1]:
tmp_head=node
if b[s-1]!=node:
if vis[node]==False:
q.append(node)
vis[node]=True
else:
if b[s-1]==node:
last=s
else:
last=node
else:
if vis[node]==False:
q.append(node)
vis[node]=True
if last==-1:
b[tmp_head-1]=head
else:
b[last-1]=head
change=0
for i in range(n):
if a[i]!=b[i]:
change+=1
print(change)
print(*b)
| 101
| 436
| 23,142,400
|
197509194
|
import sys
input=sys.stdin.readline
def ss(n,l):
for i in range(n):
l[i]=l[i]-1
d={i:[] for i in range(n)}
for i in range(n):
d[l[i]].append(i)
parent=-1
t=l[:]
for i in range(n):
if l[i]==i:
if parent==-1:
parent=i
v=[0 for _ in range(n)]
r=[0 for _ in range(n)]
# def isCyclle(node):
# nonlocal t, parent
# v[node]=1
# r[node]=1
# for child in d[node]:
# if v[child]==0:
# if isCyclle(child):
# if parent==-1:
# # print(node)
# parent=child
# # t[child]=parent
# return True
# else:
# if r[child]==1:
# if parent==-1:
# # print(node)
# parent=child
# t[child]=parent
# return True
# r[node]=0
# return False
# t[parent]=parent
for curr in range(n):
flag=0
if v[curr]==0:
# if isCyclle(curr):
# continue
q=[curr]
while q:
node=q.pop(-1)
v[node]=1
r[node]=1
# print(node,q, v, r)
f=0
for child in d[node]:
# print("here",node, child, v,r)
if v[child]==0:
q.append(node)
q.append(child)
f=1
break
elif r[child]==1:
# print("cycle found",node, child)
f=1
if parent==-1:
parent=child
if flag==0:
t[child]=parent
flag=1
if f==0:
r[node]=0
ans=0
for i in range(n):
if l[i]!=t[i]:
ans+=1
for i in range(n):
t[i]+=1
return ans,t
# n=int(input())
# l=list(map(int, input().split()))
# ans,t=ss(n,l)
# print(ans)
# print(*t)
n=int(input())
a=list(map(int,input().split()))
par=[]
for i in range(n):
if a[i]==i+1:
par.append(i)
v=[False for i in range(n)]
for i in par:
v[i]=True
ccl=[]
for i in range(n):
if v[i]:continue
s=[i]
v[i]=True
p=set(s)
t=True
while s and t:
x=s.pop()
j=a[x]-1
if j in p:
ccl.append(j)
t=False
else:
s.append(j)
p.add(j)
if v[j]:t=False
else:v[j]=True
if len(par)==0:
print(len(ccl))
c=ccl[0]
a[c]=c+1
for i in range(1,len(ccl)):
a[ccl[i]]=c+1
print(*a)
else:
print(len(ccl)+len(par)-1)
c=par[0]
for i in range(1,len(par)):
a[par[i]]=c+1
for i in range(len(ccl)):
a[ccl[i]]=c+1
print(*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
|
607/A
|
607
|
A
|
Python 3
|
TESTS
| 8
| 374
| 8,806,400
|
65557418
|
from sys import stdin
import bisect
def d(beacon):
dp = [0]*len(beacon)
for i in range(len(beacon)):
if i == 0:
dp[0] = 1
else:
loc = bisect.bisect_left(beacon, (beacon[i][0]-beacon[i][1], ))
if loc == 0:
dp[i] = 1
else:
dp[i] = dp[loc-1]+1
return min(-dp[i]+len(dp) for i in range(len(dp)))
if __name__ == "__main__":
n = stdin.readline()
beacon = []
for _ in range(int(n)):
x, y = stdin.readline().split(' ')
beacon.append((int(x), int(y)))
# beacon = [(1, 9), (3, 1), (6, 1), (7, 4)]
# beacon = [(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
print(d(beacon))
| 41
| 467
| 11,878,400
|
111761622
|
import sys,os,io
from sys import stdin
from bisect import bisect_left , bisect_right
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 = ii();l = []
for i in range(n):
l.append(tuple(li()))
l.sort();DP = [0]*(n)
for i in range(n):
x = bisect_left(l,(l[i][0]-l[i][1],0))
if x==0:
DP[i]=1
else:
DP[i]=DP[x-1]+1
print(n-max(DP))
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 171
| 13,516,800
|
47612621
|
a = int(input())
l = list(map(int,input().split()))
k = []
ans = 0
cnt = 0
for i in range(a):
ans += 1
if i > 0:
if l[i] == l[i - 1]:
ans -= 2
if ans > cnt :
cnt = ans
if cnt == 0:
print(1)
else:
print(cnt)
| 56
| 124
| 26,316,800
|
195402446
|
n = int(input())
arr = list(map(int , input().split()))
res , curr = 0 , 0
s = set()
for x in arr:
if x not in s : curr += 1
else : curr -= 1
s.add(x)
res = max(res , curr)
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
|
532/B
|
533
|
B
|
Python 3
|
TESTS
| 1
| 61
| 5,529,600
|
28268923
|
"""import sys
try:
FILE = open(str(sys.argv[1]), "r")
except IOError:
print("IOError")
sys.exit(0)
except IndexError:
print("IndexError")
sys.exit(0)
NUM = int(FILE.readline())
"""
NUM = int(input())
class Emp:
"""employee class"""
def __init__(self, employee_num, efficiency):
self.empnum = int(1 if employee_num == -1 else employee_num + 1)
self.eff = int(efficiency)
def print_items(self):
print(self.empnum, self.eff)
def as_list(self):
return [self.empnum, self.eff]
# Input
employees = list()
# change "input()" to "FILE.readline()"
for i in range(NUM):
inp = [int(i) for i in input().split()]
employees.append(Emp(inp[0], inp[1]))
"""
the number of his subordinates within the workgroup should be even.
"""
#print("Output:\n")
graded = list() # 員工分級層列表
# Organizing
for i in range(1, NUM):
graded.append([j for j in employees if j.empnum == i])
"""
print("graded:")
for i in graded:
print([j.as_list() for j in i])
"""
#print("\nlevel pruning")
for i in graded:
try:
#print(i[0].print_items())
if i[0].empnum == 1:
#print("-> manager")
continue
except:
#print("level is empty")
continue
if len(i) % 2 == 1:
#print("--> start pruning")
i.sort(key = lambda ahaha: ahaha.eff, reverse = True)
i.pop()
"""
print("graded:")
for i in graded:
print([j.as_list() for j in i])
"""
sum = 0
for i in graded:
for j in i:
sum+=j.eff
print(sum)
| 41
| 967
| 73,318,400
|
231037237
|
n = int(input())
t = [list(map(int, input().split())) for q in range(n)]
n += 1
u = [-1e7] * n
v = [0] * n
for i, (j, a) in list(enumerate(t, 1))[::-1]:
u[i] = max(u[i], v[i] + a)
v[j], u[j] = max(v[j] + v[i], u[j] + u[i]), max(v[j] + u[i], u[j] + v[i])
print(u[1])
|
VK Cup 2015 - Round 2
|
CF
| 2,015
| 2
| 256
|
Work Group
|
One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.
We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.
To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.
The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.
Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company.
Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 = - 1, for all other people the condition 1 ≤ pi < i is fulfilled.
|
Print a single integer — the maximum possible efficiency of the workgroup.
| null |
In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.
|
[{"input": "7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2", "output": "17"}]
| 2,000
|
[]
| 41
|
[{"input": "7\r\n-1 3\r\n1 2\r\n1 1\r\n1 4\r\n4 5\r\n4 3\r\n5 2\r\n", "output": "17\n"}, {"input": "1\r\n-1 42\r\n", "output": "42\n"}, {"input": "2\r\n-1 3\r\n1 2\r\n", "output": "3\n"}, {"input": "3\r\n-1 3\r\n1 1\r\n1 2\r\n", "output": "6\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n1 3\r\n", "output": "6\n"}, {"input": "3\r\n-1 3\r\n1 2\r\n2 1\r\n", "output": "3\n"}, {"input": "20\r\n-1 100\r\n1 10\r\n2 26\r\n2 33\r\n3 31\r\n2 28\r\n1 47\r\n6 18\r\n6 25\r\n9 2\r\n4 17\r\n6 18\r\n6 2\r\n6 30\r\n13 7\r\n5 25\r\n7 11\r\n11 7\r\n17 40\r\n12 43\r\n", "output": "355\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n2 22\r\n3 28\r\n3 2\r\n4 8\r\n3 17\r\n2 50\r\n5 37\r\n5 25\r\n4 29\r\n9 21\r\n10 16\r\n10 39\r\n11 41\r\n9 28\r\n9 30\r\n12 36\r\n13 26\r\n19 17\r\n", "output": "459\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n1 22\r\n1 28\r\n1 2\r\n1 8\r\n1 17\r\n1 50\r\n5 37\r\n1 25\r\n1 29\r\n5 21\r\n4 16\r\n2 39\r\n1 41\r\n3 28\r\n3 30\r\n2 36\r\n2 26\r\n14 17\r\n", "output": "548\n"}, {"input": "3\r\n-1 1\r\n1 42\r\n1 42\r\n", "output": "85\n"}, {"input": "2\r\n-1 1\r\n1 2\r\n", "output": "2\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n2 3\r\n", "output": "3\n"}, {"input": "4\r\n-1 1\r\n1 42\r\n1 42\r\n1 42\r\n", "output": "126\n"}, {"input": "4\r\n-1 1\r\n1 100\r\n1 100\r\n1 100\r\n", "output": "300\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
Python 3
|
TESTS
| 8
| 358
| 25,088,000
|
215467796
|
import bisect
t = int(input())
mas = []
places = []
dp = [0]*t
bin_search = []
for _ in range(t):
a = list(map(int, input().split()))
mas.append(a)
bin_search.append(a[0])
for i in range(1, t):
ind = bisect.bisect_left(bin_search, bin_search[i]-mas[i][1])
dp[i] = i-ind+dp[ind-1]
ans = 10**21
for i in range(t):
if dp[i]+t-i-1<ans:
ans = dp[i]+t-i-1
print(ans)
| 41
| 467
| 18,534,400
|
20347209
|
import sys
MAX_X = 1000 * 1000 + 10
def main():
n = int(sys.stdin.readline())
arr = [0] * MAX_X
cnt = [0] * MAX_X
dp = [0] * MAX_X
for i in range(n):
x, c = list(map(int, sys.stdin.readline().split()))
arr[x + 1] = c
for i in range(1, MAX_X):
cnt[i] += cnt[i - 1] + (arr[i] != 0)
for i in range(1, MAX_X):
dp[i] = dp[max(0, i - arr[i] - 1)] + cnt[i - 1] - cnt[max(0, i - arr[i] - 1)]
answer = dp[MAX_X - 1]
add = 0
for i in range(MAX_X - 1, -1, -1):
answer = min(answer, add + dp[i])
add += (arr[i] != 0)
sys.stdout.write(str(answer))
main()
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
607/A
|
607
|
A
|
PyPy 3-64
|
TESTS
| 8
| 109
| 10,752,000
|
206353041
|
import sys
input = lambda: sys.stdin.readline().rstrip()
from bisect import *
N = int(input())
A,B = [],[]
for _ in range(N):
a,b = map(int, input().split())
A.append(a)
B.append(b)
dp = [0]*(N+1)
for i in range(N):
a,b = A[i],B[i]
pre = a-b
idx = bisect_left(A, pre)
dp[i+1] = dp[idx]+1
print(N-max(dp))
| 41
| 482
| 15,769,600
|
72127996
|
import sys
input = sys.stdin.readline
from bisect import *
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda k: k[0])
a = [-10**18]+[ai for ai, _ in ab]
dp = [0]*(n+1)
for i in range(1, n+1):
j = bisect_left(a, ab[i-1][0]-ab[i-1][1])-1
dp[i] = dp[j]+i-j-1
ans = 10**18
for i in range(n+1):
ans = min(ans, dp[i]+n-i)
print(ans)
|
Codeforces Round 336 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Chain Reaction
|
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
|
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
|
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
| null |
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
|
[{"input": "4\n1 9\n3 1\n6 1\n7 4", "output": "1"}, {"input": "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "3"}]
| 1,600
|
["binary search", "dp"]
| 41
|
[{"input": "4\r\n1 9\r\n3 1\r\n6 1\r\n7 4\r\n", "output": "1\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 1\r\n7 1\r\n", "output": "3\r\n"}, {"input": "1\r\n0 1\r\n", "output": "0\r\n"}, {"input": "1\r\n0 1000000\r\n", "output": "0\r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n6 6\r\n7 7\r\n", "output": "4\r\n"}, {"input": "5\r\n1 1\r\n3 1\r\n5 1\r\n7 10\r\n8 10\r\n", "output": "2\r\n"}, {"input": "11\r\n110 90\r\n100 70\r\n90 10\r\n80 10\r\n70 1\r\n60 1\r\n50 10\r\n40 1\r\n30 1\r\n10 1\r\n20 1\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
605/C
|
605
|
C
|
Python 3
|
TESTS
| 0
| 61
| 0
|
22165497
|
import sys
jobs, xp, money = map(int, input().split())
vecs = []
maxsum = 0
for i in range(jobs):
x, m = map(int, input().split())
x *= money
m *= xp # ?
vecs.append((x, m))
if x+m > maxsum:
maxsum = x+m
maxvec = (x, m)
target = xp * money
mindays = target / min(maxvec)
for vec in vecs:
if vec[0] <= maxvec[0] and vec[1] <= maxvec[1]:
continue
j = (target*(vec[0]-vec[1])) / (maxvec[1]* vec[0] - vec[1]*maxvec[0])
k = j*((maxvec[1]-maxvec[0])/(vec[0]-vec[1]))
print(maxvec, vec, j, k)
mindays = min(mindays, j+k)
print(mindays)
| 86
| 1,341
| 14,643,200
|
113386000
|
from fractions import Fraction
def higher(x1, y1, x2, y2):
if x1 == 0:
if x2 == 0:
return
def min_days(p, q, pr):
ma = max(a for a, b in pr)
mb = max(b for a, b in pr)
pr.sort(key=lambda t: (t[0], -t[1]))
ch = [(0, mb)]
for a, b in pr:
if a == ch[-1][0]:
continue
while (
len(ch) >= 2
and ((b-ch[-2][1])*(ch[-1][0]-ch[-2][0])
>= (a-ch[-2][0])*(ch[-1][1]-ch[-2][1]))
):
ch.pop()
ch.append((a, b))
ch.append((ma, 0))
a1, b1 = None, None
for a2, b2 in ch:
if a1 is not None:
d = (a2-a1)*q + (b1-b2)*p
s = Fraction(b1*p-a1*q, d)
if 0 <= s <= 1:
return Fraction(d, a2*b1 - a1*b2)
a1, b1 = a2, b2
if __name__ == '__main__':
n, p, q = map(int, input().split())
pr = []
for _ in range(n):
a, b = map(int, input().split())
pr.append((a, b))
print("{:.7f}".format(float(min_days(p, q, pr))))
|
Codeforces Round 335 (Div. 1)
|
CF
| 2,015
| 2
| 256
|
Freelancer's Dreams
|
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site.
He has suggestions to work on n distinct projects. Mikhail has already evaluated that the participation in the i-th project will increase his experience by ai per day and bring bi dollars per day. As freelance work implies flexible working hours, Mikhail is free to stop working on one project at any time and start working on another project. Doing so, he receives the respective share of experience and money. Mikhail is only trying to become a cool programmer, so he is able to work only on one project at any moment of time.
Find the real value, equal to the minimum number of days Mikhail needs to make his dream come true.
For example, suppose Mikhail is suggested to work on three projects and a1 = 6, b1 = 2, a2 = 1, b2 = 3, a3 = 2, b3 = 6. Also, p = 20 and q = 20. In order to achieve his aims Mikhail has to work for 2.5 days on both first and third projects. Indeed, a1·2.5 + a2·0 + a3·2.5 = 6·2.5 + 1·0 + 2·2.5 = 20 and b1·2.5 + b2·0 + b3·2.5 = 2·2.5 + 3·0 + 6·2.5 = 20.
|
The first line of the input contains three integers n, p and q (1 ≤ n ≤ 100 000, 1 ≤ p, q ≤ 1 000 000) — the number of projects and the required number of experience and money.
Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 1 000 000) — the daily increase in experience and daily income for working on the i-th project.
|
Print a real value — the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
| null |
First sample corresponds to the example in the problem statement.
|
[{"input": "3 20 20\n6 2\n1 3\n2 6", "output": "5.000000000000000"}, {"input": "4 1 1\n2 3\n3 2\n2 3\n3 2", "output": "0.400000000000000"}]
| 2,400
|
["geometry"]
| 86
|
[{"input": "3 20 20\r\n6 2\r\n1 3\r\n2 6\r\n", "output": "5.000000000000000\r\n"}, {"input": "4 1 1\r\n2 3\r\n3 2\r\n2 3\r\n3 2\r\n", "output": "0.400000000000000\r\n"}, {"input": "3 12 12\r\n5 1\r\n2 2\r\n1 5\r\n", "output": "4.000000000000000\r\n"}, {"input": "3 12 12\r\n5 1\r\n4 4\r\n1 5\r\n", "output": "3.000000000000000\r\n"}, {"input": "3 1 1\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "1.000000000000000\r\n"}, {"input": "1 4 6\r\n2 3\r\n", "output": "2.000000000000000\r\n"}, {"input": "1 3 4\r\n2 3\r\n", "output": "1.500000000000000\r\n"}, {"input": "2 1 1000000\r\n2 4\r\n5 2\r\n", "output": "250000.000000000000000\r\n"}, {"input": "2 1000000 1\r\n2 4\r\n5 2\r\n", "output": "200000.000000000000000\r\n"}, {"input": "2 1000000 1000000\r\n2 4\r\n5 2\r\n", "output": "312500.000000000000000\r\n"}, {"input": "6 2 2\r\n2 4\r\n5 2\r\n5 2\r\n2 4\r\n2 4\r\n5 2\r\n", "output": "0.625000000000000\r\n"}, {"input": "1 3 5\r\n2 3\r\n", "output": "1.666666666666667\r\n"}, {"input": "2 10 3\r\n2 4\r\n5 2\r\n", "output": "2.000000000000000\r\n"}, {"input": "2 10 4\r\n2 4\r\n5 2\r\n", "output": "2.000000000000000\r\n"}, {"input": "2 10 5\r\n2 4\r\n5 2\r\n", "output": "2.187500000000000\r\n"}, {"input": "2 5 8\r\n2 4\r\n5 2\r\n", "output": "2.125000000000000\r\n"}, {"input": "2 4 8\r\n2 4\r\n5 2\r\n", "output": "2.000000000000000\r\n"}, {"input": "2 3 8\r\n2 4\r\n5 2\r\n", "output": "2.000000000000000\r\n"}, {"input": "2 4 1\r\n2 4\r\n5 2\r\n", "output": "0.800000000000000\r\n"}, {"input": "2 4 2\r\n2 4\r\n5 2\r\n", "output": "0.875000000000000\r\n"}, {"input": "2 4 3\r\n2 4\r\n5 2\r\n", "output": "1.062500000000000\r\n"}, {"input": "2 5 3\r\n2 4\r\n5 2\r\n", "output": "1.187500000000000\r\n"}, {"input": "2 5 4\r\n2 4\r\n5 2\r\n", "output": "1.375000000000000\r\n"}, {"input": "2 4 4\r\n2 4\r\n5 2\r\n", "output": "1.250000000000000\r\n"}, {"input": "2 3 4\r\n2 4\r\n5 2\r\n", "output": "1.125000000000000\r\n"}, {"input": "2 3 3\r\n2 4\r\n5 2\r\n", "output": "0.937500000000000\r\n"}, {"input": "2 2 3\r\n2 4\r\n5 2\r\n", "output": "0.812500000000000\r\n"}, {"input": "2 1 3\r\n2 4\r\n5 2\r\n", "output": "0.750000000000000\r\n"}, {"input": "2 2 4\r\n2 4\r\n5 2\r\n", "output": "1.000000000000000\r\n"}, {"input": "2 5 2\r\n2 4\r\n5 2\r\n", "output": "1.000000000000000\r\n"}, {"input": "2 5 4\r\n2 2\r\n4 3\r\n", "output": "1.333333333333333\r\n"}, {"input": "6 1000000 999999\r\n999999 1\r\n999995 999994\r\n999996 999996\r\n999997 999995\r\n999998 999997\r\n1 999998\r\n", "output": "1.000002000006000\r\n"}, {"input": "7 123456 123459\r\n10 2\r\n3 4\r\n11 3\r\n8 1\r\n5 2\r\n7 1\r\n1 8\r\n", "output": "21786.705882352940534\r\n"}, {"input": "10 123457 123459\r\n5 2\r\n11 4\r\n1 8\r\n11 1\r\n7 1\r\n10 2\r\n8 1\r\n11 3\r\n3 4\r\n11 8\r\n", "output": "15432.375000000000000\r\n"}, {"input": "10 630 764\r\n679 16\r\n34 691\r\n778 366\r\n982 30\r\n177 9\r\n739 279\r\n992 89\r\n488 135\r\n7 237\r\n318 318\r\n", "output": "1.472265278375486\r\n"}, {"input": "10 468662 93838\r\n589910 727627\r\n279516 867207\r\n470524 533177\r\n467834 784167\r\n295605 512137\r\n104422 629804\r\n925609 728473\r\n922365 500342\r\n998983 958315\r\n425628 935048\r\n", "output": "0.469139114479426\r\n"}, {"input": "10 18 25\r\n4 8\r\n16 27\r\n16 13\r\n1 26\r\n8 13\r\n2 14\r\n24 8\r\n4 29\r\n3 19\r\n19 20\r\n", "output": "1.041450777202072\r\n"}, {"input": "10 17 38\r\n6 35\r\n16 37\r\n6 12\r\n16 29\r\n27 15\r\n23 28\r\n4 27\r\n30 12\r\n5 4\r\n40 17\r\n", "output": "1.036423841059603\r\n"}, {"input": "10 36 35\r\n32 37\r\n17 30\r\n20 24\r\n11 21\r\n24 9\r\n25 6\r\n37 23\r\n14 8\r\n32 20\r\n17 39\r\n", "output": "1.072669826224329\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_output_path = sys.argv[3]
# Read correct answer
try:
with open(output_path, 'r') as f:
correct_line = f.readline().strip()
b = float(correct_line)
except:
print(0)
return
# Read submission's answer
try:
with open(submission_output_path, 'r') as f:
submission_line = f.readline().strip()
a = float(submission_line)
except:
print(0)
return
# Compute error
denominator = max(1.0, abs(b))
error = abs(a - b) / denominator
if error <= 1e-6:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
272/B
|
272
|
B
|
PyPy 3
|
TESTS
| 3
| 248
| 0
|
60293122
|
n = int(input())
f = sorted((list(map(int,input().split()))))
res = 0
i = 0
j = 1
c = 1
while i < n and j < n:
if f[i]*2 == f[j] or f[i]*2-1 == f[j]:
i+=1
j+=1
c+=1
else:
res+=c*(c-1)//2
i+=1
j+=1
res+=c*(c-1)//2
print(res)
| 42
| 248
| 11,468,800
|
167457757
|
import sys
input = sys.stdin.readline
from collections import Counter
n = int(input())
w = Counter(map(lambda x:(bin(int(x))[2:]).count('1'), input().split()))
c = 0
for i in w:
c += (w[i]*(w[i]-1))//2
print(c)
|
Codeforces Round 167 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Sequence
|
Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence:
- f(0) = 0;
- f(2·x) = f(x);
- f(2·x + 1) = f(x) + 1.
Dima wonders, how many pairs of indexes (i, j) (1 ≤ i < j ≤ n) are there, such that f(ai) = f(aj). Help him, count the number of such pairs.
|
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).
The numbers in the lines are separated by single spaces.
|
In a single line print the answer to the problem.
Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample any pair (i, j) will do, so the answer is 3.
In the second sample only pair (1, 2) will do.
|
[{"input": "3\n1 2 4", "output": "3"}, {"input": "3\n5 3 1", "output": "1"}]
| 1,400
|
["implementation", "math"]
| 42
|
[{"input": "3\r\n1 2 4\r\n", "output": "3\r\n"}, {"input": "3\r\n5 3 1\r\n", "output": "1\r\n"}, {"input": "2\r\n469264357 996569493\r\n", "output": "0\r\n"}, {"input": "6\r\n396640239 62005863 473635171 329666981 510631133 207643327\r\n", "output": "2\r\n"}, {"input": "8\r\n851991424 32517099 310793856 776130403 342626527 58796623 49544509 517126753\r\n", "output": "2\r\n"}, {"input": "7\r\n481003311 553247971 728349004 258700257 916143165 398096105 412826266\r\n", "output": "2\r\n"}, {"input": "4\r\n363034183 741262741 657823174 453546052\r\n", "output": "1\r\n"}, {"input": "8\r\n7 1 2 7 6 8 6 5\r\n", "output": "7\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n7 1\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
463/D
|
463
|
D
|
Python 3
|
TESTS
| 4
| 108
| 6,963,200
|
79382822
|
def Comparar(largoVuelta, nivelActual):
global largoMax
for vecino in series[0][nivelActual]:
largo = largoVuelta
matchGeneral = True
#print("\nvecino ", vecino)
for num in range(k-1): #reviso match entre series
match = False
#print("num ", num)
for nodo in series[num+1][nivelActual]:
#print("nodo ",nodo)
if nodo == vecino:
match = True
break
if not match:
matchGeneral = False
break
if matchGeneral: #Si todas las series tienen el mismo elemento
largo += 1
if largoMax < largo:
largoMax = largo
Comparar(largo,vecino)
n,k = map(int,input().split())
series= [[[]for j in range(n)] for i in range(k)]
serieBase = ""
#se guardan los arboles
for i in range(k):
entrada = list(map(int,input().split()))
raiz = entrada[0]-1
if serieBase == "":
serieBase = entrada
for elemento in entrada[1:]:
for nodo in series[i][raiz]:
series[i][nodo].append(elemento-1)
series[i][raiz].append(elemento-1)
#Se hace DFS
largoMax = 0
for i in range(n):
elemento = serieBase[i]
if n-i <= largoMax:
break
Comparar(1,elemento-1)
print(largoMax)
'''IDEA:
1.- Hacer los grafos.
2.- Busqueda en profundidad
3.- Comparar caminos de distintos recorridos
'''
| 40
| 93
| 4,403,200
|
231333630
|
n, k = map(int, input().split())
a = [[]]*6
b = []
for i in range(6):
b.append([0]*(n + 1))
for i in range(k):
a[i] = list(map(int, input().split()))
for j in range(n):
b[i][a[i][j]] = j
dp = [1] * n
for i in range(n):
for j in range(i):
key = 1
for t in range(1, k):
if b[t][a[0][j]] > b[t][a[0][i]]:
key = 0
break
if key:
dp[i] = max(dp[i], dp[j] + 1)
print(max(dp))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 2
| 256
|
Gargari and Permutations
|
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?
You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
|
The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.
|
Print the length of the longest common subsequence.
| null |
The answer for the first test sample is subsequence [1, 2, 3].
|
[{"input": "4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3", "output": "3"}]
| 1,900
|
["dfs and similar", "dp", "graphs", "implementation"]
| 40
|
[{"input": "4 3\r\n1 4 2 3\r\n4 1 2 3\r\n1 2 4 3\r\n", "output": "3\r\n"}, {"input": "6 3\r\n2 5 1 4 6 3\r\n5 1 4 3 2 6\r\n5 4 2 6 3 1\r\n", "output": "3\r\n"}, {"input": "41 4\r\n24 15 17 35 13 41 4 14 23 5 8 16 21 18 30 36 6 22 11 29 26 1 40 31 7 3 32 10 28 38 12 20 39 37 34 19 33 27 2 25 9\r\n22 13 25 24 38 35 29 12 15 8 11 37 3 19 4 23 18 32 30 40 36 21 16 34 27 9 5 41 39 2 14 17 31 33 26 7 1 10 20 6 28\r\n31 27 39 16 22 12 13 32 6 10 19 29 37 7 18 33 24 21 1 9 36 4 34 41 25 28 17 40 30 35 23 14 11 8 2 15 38 20 26 5 3\r\n8 18 39 38 7 34 16 31 15 1 40 20 37 4 25 11 17 19 33 26 6 14 13 41 12 32 2 21 10 35 27 9 28 5 30 24 22 23 29 3 36\r\n", "output": "4\r\n"}, {"input": "1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "28 5\r\n3 14 12 16 13 27 20 8 1 10 24 11 5 9 7 18 17 23 22 25 28 19 4 21 26 6 15 2\r\n7 12 23 27 22 26 16 18 19 5 6 9 11 28 25 4 10 3 1 14 8 17 15 2 20 13 24 21\r\n21 20 2 5 19 15 12 4 18 9 23 16 11 14 8 6 25 27 13 17 10 26 7 24 28 1 3 22\r\n12 2 23 11 20 18 25 21 13 27 14 8 4 6 9 16 7 3 10 1 22 15 26 19 5 17 28 24\r\n13 2 6 19 22 23 4 1 28 10 18 17 21 8 9 3 26 11 12 27 14 20 24 25 15 5 16 7\r\n", "output": "3\r\n"}, {"input": "6 3\r\n2 5 1 4 6 3\r\n5 1 4 6 2 3\r\n5 4 2 6 3 1\r\n", "output": "4\r\n"}, {"input": "41 4\r\n24 15 17 35 13 41 4 14 23 5 8 16 21 18 30 36 6 22 11 29 26 1 40 31 7 3 32 10 28 38 12 20 39 37 34 19 33 27 2 25 9\r\n22 13 25 24 38 35 29 12 15 8 11 37 3 19 4 23 18 32 30 40 36 21 16 34 27 9 5 41 39 2 14 17 31 33 26 7 1 10 20 6 28\r\n31 27 39 16 22 12 13 32 6 10 19 29 37 7 18 33 24 21 1 9 36 4 34 41 25 28 17 40 30 35 23 14 11 8 2 15 38 20 26 5 3\r\n8 18 39 38 7 34 16 31 15 1 40 20 37 4 25 11 17 19 33 26 6 14 13 41 12 32 2 21 10 35 27 9 28 5 30 24 22 23 29 3 36\r\n", "output": "4\r\n"}, {"input": "37 3\r\n6 3 19 20 15 4 1 35 8 24 12 21 34 26 18 14 23 33 28 9 36 11 37 31 25 32 29 22 13 27 16 17 10 7 5 30 2\r\n10 3 35 17 34 21 14 8 26 28 11 19 27 7 4 23 24 22 12 13 16 1 25 29 5 31 30 20 32 18 15 9 2 36 37 33 6\r\n19 9 22 32 26 35 29 23 5 6 14 34 33 10 2 28 15 11 24 4 13 7 8 31 37 36 1 27 3 16 30 25 20 21 18 17 12\r\n", "output": "7\r\n"}]
| false
|
stdio
| null | true
|
272/B
|
272
|
B
|
Python 3
|
TESTS
| 3
| 186
| 0
|
54885605
|
n = int(input())
def norm(a):
if a % 2 == 0:
while a % 2 == 0:
a //= 2
if a > 1:
while (a - 1) % 4 == 0:
a = (a - 1) // 2 + 1
return a
m = sorted([norm(int(x)) for x in input().split()])
s = 0
w = 0
for i in range(len(m)-1):
if m[i] == m[i+1]:
w +=1
else:
s+= w*(w+1)//2
w=0
s+= w*(w+1)//2
print(s)
| 42
| 278
| 8,089,600
|
184512393
|
def f(x):
return bin(x).count("1")
from math import comb
def solve():
# Reading input
n = int(input())
a = list(map(int, input().split()))
freq = {}
for ai in a:
ai = f(ai)
freq[ai] = freq.get(ai, 0) + 1
count = sum([comb(f, 2) for f in freq.values()])
print(count)
solve()
|
Codeforces Round 167 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Sequence
|
Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence:
- f(0) = 0;
- f(2·x) = f(x);
- f(2·x + 1) = f(x) + 1.
Dima wonders, how many pairs of indexes (i, j) (1 ≤ i < j ≤ n) are there, such that f(ai) = f(aj). Help him, count the number of such pairs.
|
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).
The numbers in the lines are separated by single spaces.
|
In a single line print the answer to the problem.
Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
| null |
In the first sample any pair (i, j) will do, so the answer is 3.
In the second sample only pair (1, 2) will do.
|
[{"input": "3\n1 2 4", "output": "3"}, {"input": "3\n5 3 1", "output": "1"}]
| 1,400
|
["implementation", "math"]
| 42
|
[{"input": "3\r\n1 2 4\r\n", "output": "3\r\n"}, {"input": "3\r\n5 3 1\r\n", "output": "1\r\n"}, {"input": "2\r\n469264357 996569493\r\n", "output": "0\r\n"}, {"input": "6\r\n396640239 62005863 473635171 329666981 510631133 207643327\r\n", "output": "2\r\n"}, {"input": "8\r\n851991424 32517099 310793856 776130403 342626527 58796623 49544509 517126753\r\n", "output": "2\r\n"}, {"input": "7\r\n481003311 553247971 728349004 258700257 916143165 398096105 412826266\r\n", "output": "2\r\n"}, {"input": "4\r\n363034183 741262741 657823174 453546052\r\n", "output": "1\r\n"}, {"input": "8\r\n7 1 2 7 6 8 6 5\r\n", "output": "7\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "2\r\n7 1\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 139
| 13,516,800
|
147900646
|
# input operation
n=int(input())
order_of_pulling=list(map(int,input().split()))
i=0
exist=1
out=0
while i <n:
if exist>out:
out=exist
if order_of_pulling[i]!=order_of_pulling[i+1]:
exist+=1
else:
exist-=1
i+=1
print(out)
| 56
| 139
| 18,739,200
|
219549762
|
n = int(input())
cur = set()
ans = -1
for x in input().split():
if x not in cur:
cur.add(x)
else:
cur.remove(x)
ans = max(ans, len(cur))
print(ans)
|
Технокубок 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
|
463/B
|
463
|
B
|
Python 3
|
TESTS
| 11
| 155
| 7,065,600
|
62041093
|
if __name__ == '__main__':
n, columns = int(input()), list(map(int, input().split(" ")))
c = columns[0]
for i in range(1, n-1):
if c < columns[i]:
c += columns[i] - c
print(c)
| 49
| 62
| 5,632,000
|
156377247
|
import sys
input = sys.stdin.readline
n = int(input())
print(max((map(int, input().split()))))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
626/F
|
626
|
F
|
Python 3
|
PRETESTS
| 1
| 30
| 0
|
16007751
|
q,w=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
r=0
import math
for i in range(0,q-1):
j=i
while (j<q):
if (a[j]-a[i])<=w:
j+=1
else:
break
m=j-i-1
n=math.factorial(m)
for j in range(1,m+1):
r+=n//(math.factorial(j)*math.factorial(m-j))
print(r+1)
| 66
| 904
| 19,865,600
|
212331798
|
# Problem: F. Group Projects
# Contest: Codeforces - 8VC Venture Cup 2016 - Elimination Round
# URL: https://codeforces.com/contest/626/problem/F
# Memory Limit: 256 MB
# Time Limit: 2000 ms
import sys
import random
from types import GeneratorType
import bisect
import io, os
from bisect import *
from collections import *
from contextlib import redirect_stdout
from itertools import *
from array import *
from functools import lru_cache, reduce
from heapq import *
from math import sqrt, gcd, inf
if sys.version >= '3.8': # ACW没有comb
from math import comb
RI = lambda: map(int, sys.stdin.buffer.readline().split())
RS = lambda: map(bytes.decode, sys.stdin.buffer.readline().strip().split())
RILST = lambda: list(RI())
DEBUG = lambda *x: sys.stderr.write(f'{str(x)}\n')
# print = lambda d: sys.stdout.write(str(d) + "\n") # 打开可以快写,但是无法使用print(*ans,sep=' ')这种语法,需要print(' '.join(map(str, p))),确实会快。
DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上
DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),
(-1, 1)] # →↘↓↙←↖↑↗
RANDOM = random.randrange(2 ** 62)
MOD = 10 ** 9 + 7
# MOD = 998244353
PROBLEM = """https://codeforces.com/contest/626/problem/F
输入 n(1≤n≤200) k(0≤k≤1000) 和长为 n 的数组 a(1≤a[i]≤500)。
有 n 个人,第 i 个人的能力值为 a[i]。
把这 n 个人分成若干组,一个组的不平衡度定义为组内最大值减最小值。
特别地,如果组内只有一个人,那么不平衡度为 0。
要求所有组的不平衡度之和不超过 k。
有多少种分组方案?模 1e9+7。
注:一个组是 a 的子序列,不要求连续。
输入
3 2
2 4 5
输出 3
输入
4 3
7 8 9 10
输出 13
输入
4 0
5 10 20 21
输出 1
"""
"""先排序。
提示 1:把作为最小值的数看成左括号,作为最大值的数看成右括号。由于作为最小值的个数不能低于作为最大值的个数,所以这和括号问题是相似的,可以用解决括号问题的技巧来思考。
提示 2:如何优雅地计算不平衡度呢?假设从小到大有 a b c d 四个数,选了 a c d,那么 d-a = (d-c) + (c-b) + (b-a)。注意这里算上了没有选的 b。
这意味着我们只需要考虑相邻两个数对不平衡度的影响。
提示 3:记忆化搜索,我的实现是从 n-1 开始,递归到 -1。先选最大值,再选最小值。
定义 dfs(i, groups, leftK) 表示前 i 个数中,有最大值但是尚未确定最小值的组有 groups 个,剩余不平衡度为 leftK。
需要考虑四种情况:
1. a[i] 作为一个新的组的最大值(创建一个新的组)。
2. a[i] 作为某个组的最小值(有 groups 种选择方案)。
3. a[i] 单独形成一个组(这个组只有一个数)。
4. a[i] 添加到某个组中(有 groups 种选择方案)。
具体见代码 https://codeforces.com/contest/626/submission/211471819
注:这题用到的思路和我在【1681. 最小不兼容性】这题评论区发的代码是类似的
https://leetcode.cn/problems/minimum-incompatibility/discussion/comments/2051770"""
# ms
def solve():
n, m = RI()
a = RILST()
a.sort()
a = [0] + a
f = [[0] * (m + 1) for _ in range(n + 1)] # f[i][j][k]表示前i个数,有j个段尚未闭合,不平衡度为k时的方案数
f[0][0] = 1
for i in range(1, n + 1):
g = [[0] * (m + 1) for _ in range(n + 1)]
d = a[i] - a[i - 1]
for j in range(n + 1):
for k in range(m + 1):
if j != 0 and k - (j - 1) * d>=0:
g[j][k] += f[j - 1][k - (j - 1) * d]
if j!=n and k - (j + 1) * d>=0:
g[j][k] += f[j + 1][k - (j + 1) * d] * (j + 1)
if k-j*d >= 0:
g[j][k] +=f[j][k-j * d] * (j + 1)
g[j][k] %= MOD
f = g
print(sum(f[0][:m + 1]) % MOD)
if __name__ == '__main__':
t = 0
if t:
t, = RI()
for _ in range(t):
solve()
else:
solve()
|
8VC Venture Cup 2016 - Elimination Round
|
CF
| 2,016
| 2
| 256
|
Group Projects
|
There are n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i-th student ai minutes to finish his/her independent piece.
If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum ai in the group minus the minimum ai in the group. Note that a group containing a single student has an imbalance of 0. How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most k?
Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other.
|
The first line contains two space-separated integers n and k (1 ≤ n ≤ 200, 0 ≤ k ≤ 1000) — the number of students and the maximum total imbalance allowed, respectively.
The second line contains n space-separated integers ai (1 ≤ ai ≤ 500) — the time it takes the i-th student to complete his/her independent piece of work.
|
Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109 + 7.
| null |
In the first sample, we have three options:
- The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2.
- The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1.
- All three students form their own groups. Total imbalance is 0.
In the third sample, the total imbalance must be 0, so each student must work individually.
|
[{"input": "3 2\n2 4 5", "output": "3"}, {"input": "4 3\n7 8 9 10", "output": "13"}, {"input": "4 0\n5 10 20 21", "output": "1"}]
| 2,400
|
["dp"]
| 66
|
[{"input": "3 2\r\n2 4 5\r\n", "output": "3\r\n"}, {"input": "4 3\r\n7 8 9 10\r\n", "output": "13\r\n"}, {"input": "4 0\r\n5 10 20 21\r\n", "output": "1\r\n"}, {"input": "20 1000\r\n50 50 100 100 150 150 200 200 250 250 300 300 350 350 400 400 450 450 500 500\r\n", "output": "97456952\r\n"}, {"input": "5 222\r\n58 369 477 58 90\r\n", "output": "10\r\n"}, {"input": "9 222\r\n304 142 38 334 73 122 252 381 438\r\n", "output": "423\r\n"}, {"input": "9 247\r\n359 350 140 26 293 488 57 481 71\r\n", "output": "414\r\n"}, {"input": "5 341\r\n412 32 189 303 172\r\n", "output": "26\r\n"}, {"input": "200 0\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "380668983\r\n"}, {"input": "121 19\r\n1 1 1 1 2 1 1 2 2 1 1 2 2 2 2 1 1 2 1 1 1 1 2 2 2 2 1 1 2 1 1 2 1 1 1 1 1 2 2 1 2 2 1 2 1 1 2 2 2 1 2 1 1 1 1 2 1 1 2 2 1 1 2 1 2 1 2 1 2 2 2 1 1 1 1 2 1 1 2 1 2 2 2 2 2 1 1 2 2 1 2 2 2 1 2 1 1 1 1 2 2 2 2 2 1 1 2 2 2 2 2 1 1 1 1 1 2 2 1 2 1\r\n", "output": "378568711\r\n"}, {"input": "3 4\r\n10 7 10\r\n", "output": "5\r\n"}, {"input": "1 5\r\n3\r\n", "output": "1\r\n"}, {"input": "1 5\r\n9\r\n", "output": "1\r\n"}, {"input": "5 2\r\n3 10 5 6 5\r\n", "output": "8\r\n"}, {"input": "1 2\r\n2\r\n", "output": "1\r\n"}, {"input": "166 7\r\n9 8 7 2 9 9 7 7 3 1 9 9 9 7 1 5 5 6 6 2 3 2 10 9 3 5 8 8 6 3 10 3 4 8 6 5 1 7 2 9 1 4 9 10 6 8 6 7 8 3 2 1 10 5 6 6 3 7 4 9 10 3 1 10 9 9 2 10 3 2 4 8 9 6 1 9 10 10 10 9 5 8 9 7 9 6 7 5 4 7 8 9 8 5 10 5 4 10 8 5 10 10 10 8 7 3 2 6 3 1 7 5 7 10 7 8 8 8 5 5 8 10 2 10 2 4 10 2 3 1 1 4 5 8 7 9 4 10 2 9 8 1 1 5 9 5 2 1 7 7 9 10 2 2 10 10 6 8 5 5 9 4 3 1 10 5\r\n", "output": "194851520\r\n"}, {"input": "94 17\r\n9 10 10 5 2 7 10 9 5 5 7 7 6 10 4 10 3 7 4 9 2 5 1 5 4 2 9 8 4 3 9 5 7 10 10 6 3 1 9 9 2 8 8 8 7 2 4 5 2 5 7 7 4 9 4 9 4 10 5 10 9 7 3 6 10 3 1 10 6 4 8 9 4 10 7 2 9 8 7 10 2 2 4 1 4 6 10 7 2 4 9 4 8 5\r\n", "output": "650765262\r\n"}, {"input": "14 26\r\n3 7 8 4 7 5 10 8 4 4 1 6 7 7\r\n", "output": "190894282\r\n"}, {"input": "142 24\r\n8 1 10 6 5 3 9 4 4 8 2 7 4 4 1 2 7 4 7 3 3 9 9 6 6 10 8 5 3 2 3 4 7 9 9 8 4 7 8 6 9 1 7 9 10 2 6 1 9 9 1 10 2 10 6 5 10 2 3 8 3 7 1 8 9 10 1 8 10 7 2 5 1 1 4 6 5 7 6 10 4 4 7 4 10 5 10 9 8 7 4 10 4 4 3 4 10 6 1 4 8 5 10 6 3 8 8 4 2 3 2 1 7 5 2 4 2 3 10 7 8 3 10 9 1 7 7 5 5 5 10 8 8 2 6 9 7 2 4 7 7 3\r\n", "output": "287439553\r\n"}, {"input": "166 34\r\n6 5 3 3 4 5 4 6 4 6 2 6 5 1 7 4 5 5 6 1 2 2 6 4 3 7 4 5 1 7 3 1 6 5 1 3 6 4 9 7 6 6 6 5 8 6 2 4 5 6 10 10 4 8 3 6 1 4 7 9 8 5 2 9 8 10 2 2 6 1 3 6 6 9 10 8 10 5 8 10 5 9 2 4 8 2 9 2 1 9 5 9 3 8 1 10 4 1 1 4 9 6 10 6 2 1 4 5 5 8 10 10 5 6 3 10 1 8 5 10 3 3 10 9 7 4 1 9 9 10 8 3 4 2 8 10 6 3 10 10 4 6 8 7 9 7 10 3 1 10 4 10 5 2 7 9 4 10 6 2 6 3 9 10 9 10\r\n", "output": "772974256\r\n"}, {"input": "171 302\r\n64 51 53 35 36 42 67 27 55 85 97 23 47 8 59 69 50 15 28 36 22 12 49 99 54 11 10 91 91 78 59 65 68 5 20 77 42 59 85 65 69 35 59 86 45 96 41 82 89 93 80 25 16 22 68 8 23 57 48 53 16 21 50 44 70 75 33 32 43 32 77 40 8 41 23 82 61 51 26 88 58 23 6 69 11 95 89 41 70 95 81 50 99 81 48 36 62 85 64 58 25 30 23 27 30 87 45 42 67 47 1 1 86 33 43 78 41 57 72 86 55 25 69 36 77 97 48 24 9 20 50 5 2 84 80 62 7 5 49 2 16 3 62 8 40 24 94 60 9 95 22 27 58 20 22 95 16 53 6 8 74 54 94 65 62 90 95 17 77 32 99\r\n", "output": "49555477\r\n"}, {"input": "158 396\r\n10 33 14 7 23 30 23 9 99 41 88 56 70 25 85 27 68 60 73 14 32 87 6 16 71 64 22 66 9 48 46 93 81 9 50 48 80 70 78 76 49 89 56 74 56 40 67 45 3 41 77 49 8 56 55 29 78 69 52 70 55 99 85 6 59 99 24 66 4 23 4 51 84 67 79 65 6 67 80 36 85 47 45 37 75 38 39 59 7 11 81 7 12 79 56 87 9 97 30 32 27 21 42 85 17 50 69 13 51 12 73 60 14 94 93 31 10 9 70 67 52 63 45 38 37 13 46 50 53 29 50 57 49 81 71 79 58 74 19 47 19 14 16 82 18 11 71 90 28 21 48 16 41 52 24 6 4 23\r\n", "output": "757778575\r\n"}, {"input": "169 129\r\n66 70 83 26 65 94 1 56 17 64 58 68 23 73 45 93 30 94 22 55 68 29 73 44 35 39 71 76 76 76 19 98 99 26 43 73 96 6 72 23 8 56 34 17 91 64 17 33 56 92 41 22 92 59 23 96 35 94 82 1 61 41 75 89 10 74 13 64 50 78 49 83 6 62 43 22 61 95 28 4 76 14 54 41 83 81 83 23 13 57 10 2 44 54 89 41 27 58 57 47 26 82 97 82 5 35 27 31 89 6 73 36 94 89 29 96 3 88 82 27 50 56 73 24 17 56 25 9 2 47 71 86 96 79 35 42 31 73 13 89 52 30 88 96 46 91 23 60 79 2 19 7 73 40 6 29 61 29 67 85 75 11 8 34 60 19 87 23 55\r\n", "output": "538924707\r\n"}, {"input": "195 110\r\n3 4 5 1 3 5 4 1 2 4 3 2 4 4 3 2 5 5 5 3 3 3 5 3 5 4 2 5 1 1 2 3 4 5 5 2 2 4 3 4 2 4 4 3 4 2 3 3 3 5 2 1 3 2 5 5 2 2 1 2 2 5 4 2 4 2 4 1 4 2 4 4 4 4 3 5 3 1 2 2 3 4 3 4 4 1 2 1 2 4 5 2 4 3 4 1 4 4 4 5 1 2 4 5 3 5 3 4 2 4 5 2 5 2 5 4 1 5 1 4 2 5 1 2 4 1 3 3 5 5 4 2 3 4 5 4 4 5 2 3 4 2 5 3 2 1 5 3 5 3 5 2 3 2 5 3 5 4 5 1 5 3 3 2 2 5 4 3 3 2 5 5 5 5 2 1 2 3 1 3 5 2 4 5 3 2 2 5 5 2 3 1 3 4 5\r\n", "output": "21311661\r\n"}, {"input": "196 17\r\n4 4 2 2 4 2 2 4 4 3 4 1 5 4 4 5 4 1 1 1 5 1 1 4 3 4 4 1 1 1 5 3 2 4 2 1 5 3 4 2 4 2 5 4 1 4 1 2 3 5 3 5 3 2 5 5 5 2 2 1 1 2 2 2 5 4 5 2 5 5 3 1 5 3 5 5 1 3 3 2 3 2 2 1 5 1 2 5 4 5 4 3 4 4 4 1 5 5 2 2 2 5 3 4 5 3 3 2 4 4 4 3 1 1 1 5 2 5 1 5 1 2 3 3 4 4 5 4 2 5 4 2 3 3 4 5 2 2 4 5 5 2 2 1 3 3 4 3 2 3 4 4 5 2 5 1 4 5 2 3 2 4 4 3 4 4 2 5 5 5 5 4 1 3 2 1 4 5 3 2 3 3 5 4 3 1 4 4 5 2 5 2 2 1 4 3\r\n", "output": "140496580\r\n"}, {"input": "200 558\r\n1 1 1 3 2 1 1 5 1 2 1 1 2 2 1 5 2 5 2 5 3 2 4 1 5 2 3 2 3 1 2 2 1 4 4 2 5 1 4 3 2 2 4 5 4 5 2 5 5 4 3 5 4 5 5 2 3 4 3 1 5 4 3 3 3 3 2 2 3 4 1 3 1 4 5 2 3 4 1 5 2 3 3 5 5 3 3 1 2 5 3 4 2 5 2 3 3 1 3 2 3 5 1 2 1 1 3 4 1 3 2 1 1 4 2 5 1 2 1 2 2 2 2 2 3 4 2 2 4 4 2 1 3 3 2 4 1 3 5 4 5 1 5 2 1 4 2 3 4 1 4 5 1 1 5 2 4 5 5 4 4 5 3 1 1 5 4 2 2 5 1 3 3 3 4 1 1 2 3 4 1 5 2 2 3 1 4 3 5 1 5 3 2 1 3 2 1 1 3 2\r\n", "output": "380668983\r\n"}, {"input": "190 152\r\n2 2 4 4 4 2 2 1 2 3 5 5 4 3 5 1 2 2 2 2 3 3 5 2 1 1 3 4 3 2 2 4 2 3 1 4 2 2 3 2 3 5 3 2 4 1 4 1 2 4 1 3 4 4 3 4 4 4 4 5 2 4 5 3 3 5 4 4 3 4 1 4 1 4 3 3 5 5 2 3 2 2 2 5 4 4 2 4 3 4 2 2 1 4 1 2 3 3 3 5 1 5 5 1 4 3 2 5 2 5 5 5 2 3 3 4 1 1 3 2 5 5 2 5 2 3 5 1 1 5 4 1 1 3 5 2 3 4 3 4 2 1 4 3 5 2 1 1 1 5 2 5 3 4 5 5 2 3 5 5 5 5 1 5 2 5 5 2 4 4 4 3 1 1 2 1 4 4 3 4 2 5 5 3 4 5 5 2 1 4\r\n", "output": "3475416\r\n"}, {"input": "191 640\r\n20 10 14 20 13 9 16 5 14 1 11 18 16 17 7 4 15 18 17 3 3 15 14 20 18 2 4 14 20 17 7 2 3 9 5 10 7 6 7 17 3 5 10 1 18 13 15 4 15 7 19 1 17 6 15 12 4 19 1 9 18 18 9 13 3 15 9 3 17 14 18 4 9 3 9 19 20 15 18 11 3 1 12 8 11 10 20 14 14 6 2 14 16 1 7 2 11 15 1 9 20 4 1 1 3 20 20 4 11 7 19 3 3 6 15 10 18 9 13 14 16 12 3 1 15 10 5 14 19 17 9 10 10 15 12 12 5 2 11 6 5 6 7 14 7 6 5 10 13 10 18 20 18 20 12 7 6 10 4 4 3 13 14 5 9 10 4 6 11 11 15 15 12 19 4 7 20 3 12 4 16 6 4 9 17 10 18 11 13 12 18\r\n", "output": "66598866\r\n"}, {"input": "197 344\r\n5 11 3 17 16 1 12 7 13 5 9 11 15 14 13 7 13 11 5 9 20 11 11 9 19 3 20 4 6 15 2 14 16 5 19 5 5 5 12 12 12 19 18 1 5 17 13 7 17 14 4 5 9 20 14 13 15 3 8 2 13 16 20 10 20 14 8 17 14 4 9 16 8 13 5 2 13 11 9 7 9 5 11 20 3 17 9 12 12 3 9 19 6 3 15 9 5 11 2 3 13 14 15 7 9 19 16 11 6 8 11 18 11 11 16 18 3 5 10 19 10 6 3 19 3 18 16 16 7 3 10 13 13 16 19 13 4 7 1 7 12 9 6 8 6 1 6 20 7 12 9 13 13 12 10 10 10 16 9 6 11 14 14 7 2 1 16 15 12 7 15 18 8 4 6 18 2 17 6 5 13 19 12 7 1 9 15 9 18 5 8 3 7 8 4 15 8\r\n", "output": "132934747\r\n"}, {"input": "200 0\r\n2 5 2 7 6 10 10 4 7 9 1 5 7 1 8 5 9 8 5 2 6 4 9 10 5 4 4 4 8 7 7 5 9 7 7 4 9 8 5 8 10 5 1 2 8 4 3 7 9 6 9 3 9 2 1 9 2 7 4 10 4 7 10 6 1 6 7 4 4 9 10 3 5 5 1 2 8 6 6 2 2 8 6 3 6 1 4 6 10 6 4 8 3 9 6 7 7 8 5 2 10 9 2 7 3 6 10 6 8 9 6 6 8 4 6 9 2 10 9 4 2 3 4 1 3 9 4 2 4 10 10 1 2 3 9 8 2 1 10 7 8 3 10 5 3 10 9 1 9 2 6 7 2 1 10 4 4 9 9 1 8 1 10 9 8 9 9 7 4 3 6 7 10 9 2 7 8 10 2 7 7 6 9 5 9 7 3 1 7 1 5 9 7 3 10 3 10 8 5 7\r\n", "output": "563633437\r\n"}, {"input": "107 59\r\n416 332 455 497 251 13 496 46 176 382 357 268 441 302 305 11 274 61 412 18 225 332 173 371 54 179 378 85 471 176 439 36 81 275 452 212 261 488 166 274 89 183 478 337 313 196 130 87 14 223 341 46 45 306 175 488 113 354 107 411 469 122 436 293 311 60 453 245 184 13 425 360 302 205 151 89 433 285 119 301 274 64 127 496 350 354 262 2 148 232 117 28 11 398 237 460 421 347 142 76 391 317 164 484 35 310 453\r\n", "output": "955755252\r\n"}, {"input": "27 383\r\n161 2 16 478 438 205 151 229 116 230 447 497 456 219 28 57 200 6 161 400 338 11 426 283 275 40 190\r\n", "output": "258971846\r\n"}, {"input": "107 497\r\n218 342 381 296 272 169 321 275 435 461 422 209 413 366 295 332 458 253 302 245 70 353 405 420 439 314 232 466 364 374 4 469 116 291 75 500 212 127 157 440 429 396 53 68 151 264 2 134 73 31 494 148 426 459 27 175 225 287 241 60 14 437 457 446 51 350 233 177 88 455 497 303 107 130 76 125 441 229 325 318 187 459 178 172 226 236 465 289 491 494 146 280 456 475 286 457 277 224 435 365 100 77 145 448 118 454 431\r\n", "output": "480907144\r\n"}, {"input": "27 209\r\n272 116 134 369 255 453 477 162 78 1 12 142 236 283 209 390 476 493 51 23 387 32 262 128 160 71 56\r\n", "output": "415376034\r\n"}, {"input": "85 655\r\n411 473 456 4 14 135 49 240 191 230 60 375 373 115 301 20 421 187 267 347 207 428 81 318 10 370 428 272 247 322 294 477 274 110 238 244 72 399 146 392 207 83 164 87 257 341 97 94 286 375 25 271 177 270 169 149 279 105 387 92 352 342 274 247 236 344 35 336 419 465 169 371 62 112 490 48 36 343 248 428 241 223 369 296 86\r\n", "output": "275193712\r\n"}, {"input": "107 19\r\n2 5 2 5 4 4 1 5 3 3 4 3 2 5 3 1 4 1 4 1 3 1 4 4 1 5 4 1 2 3 3 3 4 2 5 2 3 4 5 2 1 5 3 1 5 5 1 5 3 3 3 5 5 2 4 3 3 4 5 4 2 5 2 4 3 5 2 5 2 1 1 1 1 2 1 4 2 3 4 3 2 4 4 2 2 3 5 5 1 4 1 2 4 4 1 3 3 5 2 3 4 1 2 3 1 5 2\r\n", "output": "114012476\r\n"}, {"input": "186 35\r\n4 4 3 2 4 3 1 2 2 2 4 2 5 3 1 3 1 1 2 4 2 5 5 5 1 3 4 1 5 3 5 5 2 4 5 3 1 1 2 1 2 4 2 3 3 4 4 3 3 5 3 1 4 5 5 4 5 2 3 1 2 2 2 4 3 4 1 4 1 2 1 1 1 5 1 1 4 5 3 5 3 3 4 1 5 1 1 4 5 3 3 2 5 3 5 1 5 2 5 1 4 2 4 5 4 4 4 5 4 4 2 5 2 4 4 5 3 2 5 4 1 1 5 5 5 5 1 3 2 5 5 4 3 2 2 5 5 3 1 4 3 4 3 1 2 5 4 4 2 2 5 3 2 1 2 1 1 3 1 4 1 2 3 2 1 5 5 2 2 1 2 1 5 2 4 4 3 2 5 5 2 3 4 5 5 3\r\n", "output": "273232004\r\n"}, {"input": "150 978\r\n34 20 7 39 15 14 39 49 36 13 12 12 30 40 4 17 8 2 48 10 16 2 33 36 41 30 4 35 32 35 12 14 28 3 7 3 36 46 43 19 7 38 48 24 19 21 9 31 3 3 8 23 21 49 44 29 15 6 11 40 39 12 44 40 41 37 7 39 40 17 34 21 22 19 30 21 14 3 16 50 38 38 27 7 4 33 20 23 27 32 14 50 33 36 38 22 27 27 14 2 27 37 33 6 21 44 25 17 28 22 43 10 33 21 42 4 7 42 10 20 22 49 14 18 26 19 43 4 31 18 13 17 5 46 19 35 31 14 28 29 48 9 9 4 10 15 30 5 9 23\r\n", "output": "338032038\r\n"}, {"input": "115 588\r\n39 133 47 175 120 1 183 148 115 9 196 101 18 156 156 74 43 149 95 56 72 84 32 104 16 188 88 168 164 18 36 105 131 60 26 151 46 160 16 45 76 16 157 190 120 37 102 29 190 57 178 38 89 75 143 2 80 7 11 31 101 28 171 46 93 100 23 163 146 135 12 73 140 144 177 43 19 158 26 20 39 173 97 8 169 139 23 105 7 171 79 11 156 77 164 63 165 124 126 108 125 118 58 129 146 152 31 133 5 160 89 136 174 121 185\r\n", "output": "27195433\r\n"}]
| false
|
stdio
| null | true
|
463/B
|
463
|
B
|
PyPy 3-64
|
TESTS
| 11
| 92
| 13,516,800
|
197665225
|
n = int(input())
a = list(map(int , input().split()))
if(n == 1):
print(a[0])
else:
s = 0
c = 0
x = 0
for el in range(0, n-1) :
y = a[el]
s += x - y
if(s < 0):
c += -s
s = 0
x = y
print(c)
| 49
| 62
| 5,734,400
|
194440094
|
n = int ( input())
s = map (int , input().split())
v = max (s)
print(v)
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
463/B
|
463
|
B
|
PyPy 3-64
|
TESTS
| 11
| 77
| 13,516,800
|
173334414
|
pylonNum = int(input())
heights = list(map(int, input().split()))
heights.insert(0,0)
energy = 0
dollars = 0
if(pylonNum == 1):
dollars = heights[1]
for pylon in range(pylonNum-1):
diff = heights[pylon+1] - heights[pylon]
energy -= diff
if(energy < 0):
dollars = dollars + abs(energy)
energy = 0
print(dollars)
| 49
| 62
| 5,836,800
|
166716627
|
_, m = input(), max(map(int, input().split(' ')))
print(m)
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
463/B
|
463
|
B
|
Python 3
|
TESTS
| 9
| 155
| 6,348,800
|
70585058
|
columns_amount = int(input())
heights = [int(h) for h in input().split(' ')]
energy = 0
dollars = 0
current = 0
# print('heights = {}'.format(heights))
for h in heights:
if current < h:
if energy == 0:
dollars += h - current
else:
diff = h - current
if diff > energy:
dollars += diff - energy
energy = 0
elif diff < energy:
energy -= diff
elif current > h:
energy += current - h
current = h
print(dollars)
| 49
| 62
| 7,065,600
|
187697374
|
n = int(input())
heights = [int(x) for x in input().split()]
print(max(heights))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
532/B
|
533
|
B
|
PyPy 3
|
TESTS
| 6
| 124
| 0
|
98025296
|
import math
n = int(input())
superior = []
costs=[]
cost_total=0
for i in range(n):
a, b = map(int, input().split())
superior.append(a)
costs.append(b)
hijos = [[] for _ in range(n+1)]
for i in range(n):
if superior[i]==-1:
cost_total+=costs[i]
else:
hijos[superior[i]].append(i+1)
for i in range (n):
menor = math.inf
if len(hijos[i]) > 1 :
if len(hijos[i]) % 2 == 0:
for j in hijos[i]:
cost_total+=costs[j-1]
else :
for j in hijos[i]:
if costs[j-1] < menor:
menor = j
for j in hijos[i]:
if j != menor:
cost_total+=costs[j-1]
print(cost_total)
| 41
| 967
| 73,318,400
|
231037237
|
n = int(input())
t = [list(map(int, input().split())) for q in range(n)]
n += 1
u = [-1e7] * n
v = [0] * n
for i, (j, a) in list(enumerate(t, 1))[::-1]:
u[i] = max(u[i], v[i] + a)
v[j], u[j] = max(v[j] + v[i], u[j] + u[i]), max(v[j] + u[i], u[j] + v[i])
print(u[1])
|
VK Cup 2015 - Round 2
|
CF
| 2,015
| 2
| 256
|
Work Group
|
One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.
We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.
To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.
The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.
Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.
|
The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company.
Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 = - 1, for all other people the condition 1 ≤ pi < i is fulfilled.
|
Print a single integer — the maximum possible efficiency of the workgroup.
| null |
In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.
|
[{"input": "7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2", "output": "17"}]
| 2,000
|
[]
| 41
|
[{"input": "7\r\n-1 3\r\n1 2\r\n1 1\r\n1 4\r\n4 5\r\n4 3\r\n5 2\r\n", "output": "17\n"}, {"input": "1\r\n-1 42\r\n", "output": "42\n"}, {"input": "2\r\n-1 3\r\n1 2\r\n", "output": "3\n"}, {"input": "3\r\n-1 3\r\n1 1\r\n1 2\r\n", "output": "6\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n1 3\r\n", "output": "6\n"}, {"input": "3\r\n-1 3\r\n1 2\r\n2 1\r\n", "output": "3\n"}, {"input": "20\r\n-1 100\r\n1 10\r\n2 26\r\n2 33\r\n3 31\r\n2 28\r\n1 47\r\n6 18\r\n6 25\r\n9 2\r\n4 17\r\n6 18\r\n6 2\r\n6 30\r\n13 7\r\n5 25\r\n7 11\r\n11 7\r\n17 40\r\n12 43\r\n", "output": "355\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n2 22\r\n3 28\r\n3 2\r\n4 8\r\n3 17\r\n2 50\r\n5 37\r\n5 25\r\n4 29\r\n9 21\r\n10 16\r\n10 39\r\n11 41\r\n9 28\r\n9 30\r\n12 36\r\n13 26\r\n19 17\r\n", "output": "459\n"}, {"input": "20\r\n-1 100\r\n1 35\r\n1 22\r\n1 28\r\n1 2\r\n1 8\r\n1 17\r\n1 50\r\n5 37\r\n1 25\r\n1 29\r\n5 21\r\n4 16\r\n2 39\r\n1 41\r\n3 28\r\n3 30\r\n2 36\r\n2 26\r\n14 17\r\n", "output": "548\n"}, {"input": "3\r\n-1 1\r\n1 42\r\n1 42\r\n", "output": "85\n"}, {"input": "2\r\n-1 1\r\n1 2\r\n", "output": "2\n"}, {"input": "3\r\n-1 1\r\n1 2\r\n2 3\r\n", "output": "3\n"}, {"input": "4\r\n-1 1\r\n1 42\r\n1 42\r\n1 42\r\n", "output": "126\n"}, {"input": "4\r\n-1 1\r\n1 100\r\n1 100\r\n1 100\r\n", "output": "300\n"}]
| false
|
stdio
| null | true
|
780/A
|
780
|
A
|
Python 3
|
TESTS
| 3
| 93
| 22,630,400
|
231469305
|
a = int(input())
b = input().split()
c = 0
d = 0
e = {}
for i in range(0, a):
if b[i] in e:
if c > d:
d = c
c = 0
del e[b[i]]
else:
c += 1
e[b[i]] = i
if c > d:
d = c
print(d)
| 56
| 140
| 11,980,800
|
194523369
|
input()
h = input().split()
s = set()
m = 0
for i in h :
s ^={i}
if len(s)>m :
m = len(s)
print(m)
|
Технокубок 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
|
463/B
|
463
|
B
|
Python 3
|
TESTS
| 9
| 171
| 6,144,000
|
110041988
|
n = int(input())
a = list(map(int, (input().split())))
energy = '0'
dollar = a[0]
#print(dollar)
for i in range(0,len(a)-1):
diff = a[i+1] - a[i]
#print("ENREGY", energy)
if diff > int(energy):
#print((diff - int(energy)))
dollar += (diff - int(energy))
energy = '0'
# print(energy)
#print(dollar)
elif diff < int(energy):
if diff > 0:
(energy) = int(energy) - diff
#print(energy)
else:
(energy) = int(energy) - diff
print(dollar)
| 49
| 62
| 7,270,400
|
137320758
|
import math
#s = input()
#n= (map(int, input().split()))
#(map(int, input().split()))
#a, b = (map(int, input().split()))
#for i in range(0, int(input())):
n = int(input())
mass = list(map(int, input().split()))
print(max(mass))
|
Codeforces Round 264 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Caisa and Pylons
|
Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
| null |
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
[{"input": "5\n3 4 3 2 4", "output": "4"}, {"input": "3\n4 4 4", "output": "4"}]
| 1,100
|
["brute force", "implementation", "math"]
| 49
|
[{"input": "5\r\n3 4 3 2 4\r\n", "output": "4\r\n"}, {"input": "3\r\n4 4 4\r\n", "output": "4\r\n"}, {"input": "99\r\n1401 2019 1748 3785 3236 3177 3443 3772 2138 1049 353 908 310 2388 1322 88 2160 2783 435 2248 1471 706 2468 2319 3156 3506 2794 1999 1983 2519 2597 3735 537 344 3519 3772 3872 2961 3895 2010 10 247 3269 671 2986 942 758 1146 77 1545 3745 1547 2250 2565 217 1406 2070 3010 3404 404 1528 2352 138 2065 3047 3656 2188 2919 2616 2083 1280 2977 2681 548 4000 1667 1489 1109 3164 1565 2653 3260 3463 903 1824 3679 2308 245 2689 2063 648 568 766 785 2984 3812 440 1172 2730\r\n", "output": "4000\r\n"}, {"input": "68\r\n477 1931 3738 3921 2306 1823 3328 2057 661 3993 2967 3520 171 1739 1525 1817 209 3475 1902 2666 518 3283 3412 3040 3383 2331 1147 1460 1452 1800 1327 2280 82 1416 2200 2388 3238 1879 796 250 1872 114 121 2042 1853 1645 211 2061 1472 2464 726 1989 1746 489 1380 1128 2819 2527 2939 622 678 265 2902 1111 2032 1453 3850 1621\r\n", "output": "3993\r\n"}, {"input": "30\r\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "30\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "1\r\n69\r\n", "output": "69\r\n"}]
| false
|
stdio
| null | true
|
53/D
|
53
|
D
|
Python 3
|
TESTS
| 0
| 92
| 204,800
|
107115104
|
def main():
N = int(input())
order = list(input().split())
initial = list(input().split())
X = []
Y = []
ans = 0
for i in range(N):
if order[i] == initial[i]:
continue
else:
j = i + 1
for j in range(j, N):
X.insert(len(X), j+1)
Y.insert(len(X), j)
if initial[j] == order[i]:
break
initial[i+1:j+1] = initial[i:j]
ans += j-i
print(ans)
for i in range(len(X)):
print(str(Y[i])+" " + str(X[i]))
if __name__ == '__main__':
main()
| 30
| 154
| 614,400
|
174605021
|
import math
import random
from datetime import datetime
now = datetime.now()
def prime_generator(nr_elemente_prime):
vector_prime=[-1]*nr_elemente_prime
vector_rasp=[0]*nr_elemente_prime
vector_prime[1]=1
vector_rasp[1]=1
#primes sieve
contor=2
for i in range(2,nr_elemente_prime):
if vector_prime[i]==-1:
vector_prime[i]=1
vector_rasp[contor]=i
contor=contor+1
for j in range(i+i,nr_elemente_prime,i):
if vector_prime[j]==-1:
vector_prime[j]=i
#print(i,j)
#print(vector_rasp)
set_prime=set(vector_rasp[2:len(vector_rasp)])
set_prime.remove(0)
return list(set_prime)
#print(sume_disponibile)
#res = list(map(int, str(num)))
rasp_final=""
#print(1000000000000000000 - 27322)
#print("aici",173788420792057536 /(769449696/2))
#print(1000000000/ 1024)
dictionar={}
dictionar[0]=0
dictionar[1]=1
palindrom=[]
palindrom.append(0)
palindrom.append(1)
contor=2
#cazuri=int(input())
for tt in range(1):
#p,q=list(map(int,input().split()))
# p=10**18
#q=(10**9)
#print(p,q)
n=int(input())
bloc=list(map(int,input().split()))
lista=list(map(int,input().split()))
cate=0
answ=''
for i in range(n):
if bloc[i]!=lista[i]:
# print("i=",i)
# print(lista)
for j in range(i+1,n):
if lista[j]==bloc[i]:
for z in range(j-1,i-1,-1):
#print("i=",i,"z=",z)
cate+=1
answ+=str(z+1)+" "+str(z+2)+"\n"
# print(z+1,z+2)
c=lista[z+1]
lista[z+1]=lista[z]
lista[z]=c
# print(lista)
break
print(cate)
print(answ)
#print(lista)
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500
|
["sortings"]
| 30
|
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
396/B
|
396
|
B
|
Python 3
|
TESTS
| 1
| 62
| 307,200
|
110490982
|
import math
def prime(n):
root = int(math.sqrt(n))
i=2
while(i<=root):
if(n%i==0):
return 0
i+=1
return 1
def diviseur(num):
root = int(math.sqrt(num))
i = 2
div = []
while(i < root):
if(num % i == 0):
div.append(i)
num /= i
while(num % i == 0):
num /= i
i += 1
return div
def pgcd(a, b):
while b != 0:
r = a % b
a, b = b, r
return a
def reduc(a,b):
pg = pgcd(a,b)
return a/pg,b/pg
nb = int(input())
for i in range(nb):
l = []
a = int(input())
l ,m= a,a+1
"""for x in range(2,a+1):
vn = x
for j in range(x,1,-1):
if(prime(j)==1):
vn = j
break
x+=1
un = x
while(1):
if(prime(x)==1):
un = x
break
x+=1
l.append(un*vn)"""
while(prime(l)==0):
l-=1
while(prime(m)==0):
m+=1
aa = l * m-2*m+2*(a-l+1)
bb = 2*l*m
g = pgcd(aa,bb)
print(str(int(aa/g))+"/"+str(int((bb/g))))
| 30
| 467
| 25,292,800
|
17700795
|
def isPrime(n):
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
def prevPrime(n):
while not isPrime(n):
n -= 1
return n
def nextPrime(n):
n += 1
while not isPrime(n):
n += 1
return n
def gcd(a, b):
while(a):
b %= a
a, b = b, a
return b;
def solve():
n = int(input())
prev = prevPrime(n);
next = nextPrime(n);
num1 = prev - 2;
den1 = prev * 2;
g = gcd(num1, den1);
num1 //= g;
den1 //= g;
num2 = n - prev + 1;
den2 = prev * next;
g = gcd(num2, den2);
num2 //= g;
den2 //= g;
g = gcd(num1 * den2 + num2 * den1, den1 * den2);
x = (num1 * den2 + num2 * den1) // g;
y = den1 * den2 // g;
print('{}/{}'.format(x, y))
t = int(input())
while t:
t -= 1
solve()
|
Codeforces Round 232 (Div. 1)
|
CF
| 2,014
| 2
| 256
|
On Sum of Fractions
|
Let's assume that
- v(n) is the largest prime number, that does not exceed n;
- u(n) is the smallest prime number strictly greater than n.
Find $$\sum_{i=2}^{n}\frac{1}{v(i)u(i)}$$.
|
The first line contains integer t (1 ≤ t ≤ 500) — the number of testscases.
Each of the following t lines of the input contains integer n (2 ≤ n ≤ 109).
|
Print t lines: the i-th of them must contain the answer to the i-th test as an irreducible fraction "p/q", where p, q are integers, q > 0.
| null | null |
[{"input": "2\n2\n3", "output": "1/6\n7/30"}]
| null |
["math", "number theory"]
| 30
|
[{"input": "2\r\n2\r\n3\r\n", "output": "1/6\r\n7/30\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "999999941999999673/1999999887999999118\r\n"}, {"input": "5\r\n3\r\n6\r\n9\r\n10\r\n5\r\n", "output": "7/30\r\n5/14\r\n61/154\r\n9/22\r\n23/70\r\n"}, {"input": "5\r\n5\r\n8\r\n18\r\n17\r\n17\r\n", "output": "23/70\r\n59/154\r\n17/38\r\n287/646\r\n287/646\r\n"}, {"input": "5\r\n7\r\n40\r\n37\r\n25\r\n4\r\n", "output": "57/154\r\n39/82\r\n1437/3034\r\n615/1334\r\n3/10\r\n"}, {"input": "5\r\n72\r\n72\r\n30\r\n75\r\n11\r\n", "output": "71/146\r\n71/146\r\n29/62\r\n5615/11534\r\n119/286\r\n"}, {"input": "5\r\n79\r\n149\r\n136\r\n194\r\n124\r\n", "output": "6393/13114\r\n22199/44998\r\n135/274\r\n37631/76042\r\n14121/28702\r\n"}, {"input": "6\r\n885\r\n419\r\n821\r\n635\r\n63\r\n480\r\n", "output": "781453/1566442\r\n175559/352798\r\n674039/1351366\r\n403199/808942\r\n3959/8174\r\n232303/466546\r\n"}, {"input": "1\r\n649580447\r\n", "output": "421954771415489597/843909545429301074\r\n"}]
| false
|
stdio
| null | true
|
279/C
|
279
|
C
|
PyPy 3
|
TESTS
| 7
| 748
| 10,956,800
|
184862571
|
from math import *
#from heapq import heappop, heappush
from collections import deque
from functools import lru_cache
from collections import defaultdict, Counter
from bisect import *
MOD = 10**9 + 7
import sys
#from sys import stdin,stdout
input=sys.stdin.readline
#n = int(input())
#map(int, input().split())
#list(map(int, input().split()))
def solution():
_, m = map(int, input().split())
arr = list(map(int, input().split()))
running = 0
left_most = []
for i in range(len(arr)):
left_most.append(running)
if 0 < i < len(arr) - 1 and arr[i] < arr [i-1] < arr[i+1]:
running = i
for _ in range(m):
a,b = map(int, input().split())
a -= 1; b -= 1
print(["No","Yes"][a >= left_most[b]])
def main():
#test()
t = 1
#t = int(input())
for _ in range(t):
solution()
#import sys
#import threading
#sys.setrecursionlimit(10**6)
#threading.stack_size(1 << 27)
#thread = threading.Thread(target=main)
#thread.start(); thread.join()
main()
| 35
| 1,466
| 14,028,800
|
213714989
|
import sys
sys.setrecursionlimit(2000000)
from collections import defaultdict
def clc():
n,m= map(int,input().split())
arr = list(map(int,input().split()))
dp0 =[0]*len(arr)
dp1= [0]*len(arr)
dp0[0],dp1[n-1] = 1,1
for i in range(1,n):
if arr[i]<=arr[i-1]:dp0[i] = dp0[i-1]+1
else:dp0[i] = 1
for i in range(n-2,-1,-1):
if arr[i]<=arr[i+1]:dp1[i] = dp1[i+1]+1
else:dp1[i]= 1
#print(dp1,dp0)
for i in range(m):
l,r = map(int,input().split())
l,r = l-1,r-1
if dp1[l]+dp0[r]>=(r-l+1):
print("Yes")
else:
print("No")
return True
cc = clc()
if not cc :
print(-1)
|
Codeforces Round 171 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Ladder
|
You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original array, that is, the sequence of numbers ali, ali + 1, ali + 2, ..., ari. For each query you should check whether the corresponding segment is a ladder.
A ladder is a sequence of integers b1, b2, ..., bk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b1 ≤ b2 ≤ ... ≤ bx ≥ bx + 1 ≥ bx + 2... ≥ bk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of array elements and the number of queries. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where number ai stands for the i-th array element.
The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the subsegment of the initial array.
The numbers in the lines are separated by single spaces.
|
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
| null | null |
[{"input": "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8", "output": "Yes\nYes\nNo\nYes\nNo\nYes"}]
| 1,700
|
["dp", "implementation", "two pointers"]
| 35
|
[{"input": "8 6\r\n1 2 1 3 3 5 2 1\r\n1 3\r\n2 3\r\n2 4\r\n8 8\r\n1 4\r\n5 8\r\n", "output": "Yes\r\nYes\r\nNo\r\nYes\r\nNo\r\nYes\r\n"}, {"input": "1 1\r\n6\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "2 5\r\n1 1\r\n1 2\r\n2 2\r\n2 2\r\n1 2\r\n1 2\r\n", "output": "Yes\r\nYes\r\nYes\r\nYes\r\nYes\r\n"}, {"input": "10 10\r\n4 7 2 6 4 8 1 2 1 9\r\n6 10\r\n1 9\r\n9 9\r\n2 8\r\n9 9\r\n1 1\r\n8 8\r\n4 8\r\n8 8\r\n7 7\r\n", "output": "No\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nYes\r\nNo\r\nYes\r\nYes\r\n"}, {"input": "7 5\r\n13 13 16 12 16 3 19\r\n2 7\r\n3 4\r\n7 7\r\n1 2\r\n4 7\r\n", "output": "No\r\nYes\r\nYes\r\nYes\r\nNo\r\n"}, {"input": "13 6\r\n2 6 1 3 5 2 2 1 6 4 2 5 2\r\n10 13\r\n4 10\r\n4 11\r\n3 5\r\n9 13\r\n3 13\r\n", "output": "No\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\n"}, {"input": "20 20\r\n17 11 7 4 1 17 7 20 12 12 15 14 7 12 5 13 9 16 7 19\r\n9 16\r\n11 11\r\n18 19\r\n1 10\r\n14 19\r\n6 13\r\n5 16\r\n1 17\r\n5 15\r\n5 5\r\n1 13\r\n20 20\r\n20 20\r\n3 18\r\n16 20\r\n16 18\r\n17 18\r\n14 20\r\n13 14\r\n14 15\r\n", "output": "No\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\n"}, {"input": "100 10\r\n53 72 2 58 6 29 65 7 43 9 77 10 58 25 49 95 88 11 7 36 51 25 78 20 15 2 69 76 1 66 17 4 91 66 50 66 69 94 74 31 19 96 35 84 83 15 33 73 39 73 29 53 9 47 3 19 4 16 85 6 49 6 57 70 96 19 66 63 86 61 27 21 33 82 13 98 59 48 85 1 13 65 28 34 93 16 88 32 60 50 33 37 36 57 97 28 18 23 30 70\r\n25 43\r\n20 70\r\n13 51\r\n64 66\r\n1 60\r\n17 86\r\n100 100\r\n94 98\r\n51 66\r\n18 92\r\n", "output": "No\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nNo\r\n"}]
| false
|
stdio
| null | true
|
279/C
|
279
|
C
|
PyPy 3
|
TESTS
| 3
| 154
| 0
|
211365869
|
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
peak = [0] * n
valley = [0] * n
for i in range(1, n - 1):
if a[i] > a[i - 1] and a[i] > a[i + 1]:
peak[i] += 1
elif a[i] < a[i - 1] and a[i] < a[i + 1]:
valley[i] += 1
for i in range(1, n):
peak[i] += peak[i - 1]
valley[i] += valley[i - 1]
for q in range(m):
l, r = map(int, input().split()); l -= 1; r -= 1
if (valley[r - 1] - valley[l] > 0): print("No")
else: print("Yes")
| 35
| 1,558
| 14,233,600
|
211930972
|
n,m=map(int,input().split())
a=list(map(int,input().split()))
b,c=[0]*n,[0]*n
for i in range(1,n):
if a[i]<=a[i-1]: b[i]=b[i-1]+1
for i in range(n-2,-1,-1):
if a[i]<=a[i+1]: c[i]=c[i+1]+1
for i in range(m):
L,R=map(int,input().split())
if abs(R-L)<=c[L-1]+b[R-1]:
print("Yes")
else:
print("No")
|
Codeforces Round 171 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Ladder
|
You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original array, that is, the sequence of numbers ali, ali + 1, ali + 2, ..., ari. For each query you should check whether the corresponding segment is a ladder.
A ladder is a sequence of integers b1, b2, ..., bk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b1 ≤ b2 ≤ ... ≤ bx ≥ bx + 1 ≥ bx + 2... ≥ bk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of array elements and the number of queries. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where number ai stands for the i-th array element.
The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the subsegment of the initial array.
The numbers in the lines are separated by single spaces.
|
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
| null | null |
[{"input": "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8", "output": "Yes\nYes\nNo\nYes\nNo\nYes"}]
| 1,700
|
["dp", "implementation", "two pointers"]
| 35
|
[{"input": "8 6\r\n1 2 1 3 3 5 2 1\r\n1 3\r\n2 3\r\n2 4\r\n8 8\r\n1 4\r\n5 8\r\n", "output": "Yes\r\nYes\r\nNo\r\nYes\r\nNo\r\nYes\r\n"}, {"input": "1 1\r\n6\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "2 5\r\n1 1\r\n1 2\r\n2 2\r\n2 2\r\n1 2\r\n1 2\r\n", "output": "Yes\r\nYes\r\nYes\r\nYes\r\nYes\r\n"}, {"input": "10 10\r\n4 7 2 6 4 8 1 2 1 9\r\n6 10\r\n1 9\r\n9 9\r\n2 8\r\n9 9\r\n1 1\r\n8 8\r\n4 8\r\n8 8\r\n7 7\r\n", "output": "No\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nYes\r\nNo\r\nYes\r\nYes\r\n"}, {"input": "7 5\r\n13 13 16 12 16 3 19\r\n2 7\r\n3 4\r\n7 7\r\n1 2\r\n4 7\r\n", "output": "No\r\nYes\r\nYes\r\nYes\r\nNo\r\n"}, {"input": "13 6\r\n2 6 1 3 5 2 2 1 6 4 2 5 2\r\n10 13\r\n4 10\r\n4 11\r\n3 5\r\n9 13\r\n3 13\r\n", "output": "No\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\n"}, {"input": "20 20\r\n17 11 7 4 1 17 7 20 12 12 15 14 7 12 5 13 9 16 7 19\r\n9 16\r\n11 11\r\n18 19\r\n1 10\r\n14 19\r\n6 13\r\n5 16\r\n1 17\r\n5 15\r\n5 5\r\n1 13\r\n20 20\r\n20 20\r\n3 18\r\n16 20\r\n16 18\r\n17 18\r\n14 20\r\n13 14\r\n14 15\r\n", "output": "No\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\n"}, {"input": "100 10\r\n53 72 2 58 6 29 65 7 43 9 77 10 58 25 49 95 88 11 7 36 51 25 78 20 15 2 69 76 1 66 17 4 91 66 50 66 69 94 74 31 19 96 35 84 83 15 33 73 39 73 29 53 9 47 3 19 4 16 85 6 49 6 57 70 96 19 66 63 86 61 27 21 33 82 13 98 59 48 85 1 13 65 28 34 93 16 88 32 60 50 33 37 36 57 97 28 18 23 30 70\r\n25 43\r\n20 70\r\n13 51\r\n64 66\r\n1 60\r\n17 86\r\n100 100\r\n94 98\r\n51 66\r\n18 92\r\n", "output": "No\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nNo\r\n"}]
| false
|
stdio
| null | true
|
12/C
|
12
|
C
|
Python 3
|
TESTS
| 4
| 62
| 4,812,800
|
26787239
|
def Invertir(A):
B =[]
k = len(A)-1
while k>=0:
B.append(A[k])
k -=1
return B
def SinRepetir(A):
ASP = [0]
for k in range (len(A)):
if k==0:
ASP[0]=A[k]
c = 0
for i in range (len(ASP)):
if A[k]==ASP[i]:
c+=1
if c==0:
ASP.append(A[k])
return ASP
L = input()
L = L.split()
n = int(L[0])
m = int(L[1])
P = input()
P = P.split()
Frutas = []
for k in range (len(P)):
P[k] = int(P[k])
for k in range (m):
F = str(input())
Frutas.append(F)
FrutasSinRepetir = SinRepetir(Frutas)
Contador = [0]*len(FrutasSinRepetir)
for k in range (len(FrutasSinRepetir)):
for i in range (len(Frutas)):
if Frutas[i]==FrutasSinRepetir[k]:
Contador[k] += 1
P.sort()
CInv = Invertir(Contador)
Menor = 0
Mayor = 0
for k in range (len(CInv)):
Menor += CInv[k]*P[k]
j = len(P)-1
i = len(Contador)-1
while i>=0:
Mayor += P[j]*Contador[i]
j -= 1
i -=1
print(Menor, Mayor)
| 25
| 46
| 0
|
165379508
|
n, m = map(int, input().split())
price_tags = list(map(int, input().split()))
price_tags_asc = sorted(price_tags)
price_tags_dsc = sorted(price_tags, reverse=True)
fruits = {}
for _ in range(m):
fruit = input()
if fruit in fruits:
fruits[fruit] += 1
else:
fruits[fruit] = 1
fruits_nums = list(fruits.values())
fruits_nums.sort(reverse=True)
_min = 0
_max = 0
for i in range(len(fruits_nums)):
_min += fruits_nums[i] * price_tags_asc[i]
_max += fruits_nums[i] * price_tags_dsc[i]
print(f"{_min} {_max}")
|
Codeforces Beta Round 12 (Div 2 Only)
|
ICPC
| 2,010
| 1
| 256
|
Fruits
|
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.
When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
|
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
|
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
| null | null |
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
| 1,100
|
["greedy", "implementation", "sortings"]
| 25
|
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
| false
|
stdio
| null | true
|
12/C
|
12
|
C
|
Python 3
|
TESTS
| 4
| 61
| 0
|
117451075
|
a, b = map(int,input().split())
pri = list(map(int,input().split()))
pri.sort()
li = []
new = []
new.sort()
high = 0
low = 0
for x in range(b):
k = input()
if k not in li:
li.append(k)
new.append(1)
else:new[li.index(k)] += 1
for x in range(len(new)):
high += new[-(x + 1)] * pri[-(x + 1)]
low += new[-(x + 1)] * pri[x]
print(low,high)
| 25
| 46
| 0
|
174607853
|
n,m=map(int,input().split())
List=list(map(int,input().split()));List.sort()
list_0=List.copy()
List.reverse()
list_1=List.copy()
want=[input()for i in range(m)]
kind=list(set(want))
mmin=0;mmax=0
ss=[]
for i in kind:
ss.append(want.count(i))
ss.sort(reverse=True)
for i in range(len(ss)):
mmin+=ss[i]*list_0[i]
mmax+=ss[i]*list_1[i]
print(mmin,mmax)
|
Codeforces Beta Round 12 (Div 2 Only)
|
ICPC
| 2,010
| 1
| 256
|
Fruits
|
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.
When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
|
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
|
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
| null | null |
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
| 1,100
|
["greedy", "implementation", "sortings"]
| 25
|
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
| false
|
stdio
| null | true
|
460/C
|
460
|
C
|
PyPy 3-64
|
TESTS
| 6
| 77
| 13,209,600
|
209355184
|
def check(a, n, m, w, up):
ans = m * w
for i in range(n):
if a[i] < up:
ans -= up - a[i]
return ans >= 0
if __name__ == '__main__':
n, m, w = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
left, right, res = min(a), max(a) + m, -1
while left <= right:
mid = left + (right - left) // 2
if check(a, n, m, w, mid):
res = mid
left = mid + 1
else:
right = mid - 1
print(res)
| 43
| 108
| 16,384,000
|
209356182
|
def check(a, diff, n, m, w, up):
preSum, ans = 0, 0
for i in range(1, n + 1):
preSum += diff[i]
if preSum < up:
ans += up - preSum
diff[i] = up - preSum
if i + w <= n:
diff[i + w] -= up - preSum
preSum = up
for i in range(1, n + 1):
diff[i] = a[i] - a[i - 1]
return ans <= m
if __name__ == '__main__':
n, m, w = list(map(int, input().strip().split()))
a = [0] + list(map(int, input().strip().split()))
diff = [0] * (n + 1)
for i in range(1, n + 1):
diff[i] = a[i] - a[i - 1]
left, right, res = min(a[1:]), max(a[1:]) + m, -1
while left <= right:
mid = left + (right - left) // 2
if check(a, diff, n, m, w, mid):
res = mid
left = mid + 1
else:
right = mid - 1
print(res)
|
Codeforces Round 262 (Div. 2)
|
CF
| 2,014
| 2
| 256
|
Present
|
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
|
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
|
Print a single integer — the maximum final height of the smallest flower.
| null |
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
|
[{"input": "6 2 3\n2 2 2 2 1 1", "output": "2"}, {"input": "2 5 1\n5 8", "output": "9"}]
| 1,700
|
["binary search", "data structures", "greedy"]
| 43
|
[{"input": "6 2 3\r\n2 2 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "2 5 1\r\n5 8\r\n", "output": "9\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "2\r\n"}, {"input": "3 2 3\r\n999999998 999999998 999999998\r\n", "output": "1000000000\r\n"}, {"input": "10 8 3\r\n499 498 497 497 497 497 497 497 498 499\r\n", "output": "500\r\n"}, {"input": "11 18 8\r\n4996 4993 4988 4982 4982 4982 4982 4982 4986 4989 4994\r\n", "output": "5000\r\n"}, {"input": "1 100000 1\r\n1000000000\r\n", "output": "1000100000\r\n"}, {"input": "4 100 3\r\n1 100000 100000 1\r\n", "output": "51\r\n"}]
| false
|
stdio
| null | true
|
460/C
|
460
|
C
|
PyPy 3-64
|
TESTS
| 28
| 109
| 18,329,600
|
176204334
|
def check(x):
st = [0] * n
scurr, moves = 0, 0
for i in range(n):
if i - w >= 0:
scurr -= st[i - w]
if a[i] + scurr < x:
st[i] = x - a[i] - scurr
scurr += st[i]
moves += st[i]
if moves > m:
return False
return moves <= m
n, m, w = map(int, input().split())
a = [int(i) for i in input().split()]
l = min(a)
r = 1000100000
while r - l > 1:
md = (r + l) // 2
if check(md):
l = md
else:
r = md
print(l)
| 43
| 108
| 18,329,600
|
208910378
|
n,m,w = map(int, input().split())
a = list(map(int, input().split()))
def check(up: int) -> bool:
diff = [0] * (n+1)
sm = 0
cap = m
for i, x in enumerate(a):
sm += diff[i]
curr = x + sm
if(curr < up):
cap -= up - curr
if(cap < 0):
return False
sm += up - curr
diff[min(n,i+w)] -= up - curr
return True
left, right = min(a), max(a) + m
while(left <= right):
mid = (left + right) // 2
if(check(mid)):
left = mid + 1
else:
right = mid - 1
print(right)
|
Codeforces Round 262 (Div. 2)
|
CF
| 2,014
| 2
| 256
|
Present
|
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
|
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
|
Print a single integer — the maximum final height of the smallest flower.
| null |
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
|
[{"input": "6 2 3\n2 2 2 2 1 1", "output": "2"}, {"input": "2 5 1\n5 8", "output": "9"}]
| 1,700
|
["binary search", "data structures", "greedy"]
| 43
|
[{"input": "6 2 3\r\n2 2 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "2 5 1\r\n5 8\r\n", "output": "9\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "2\r\n"}, {"input": "3 2 3\r\n999999998 999999998 999999998\r\n", "output": "1000000000\r\n"}, {"input": "10 8 3\r\n499 498 497 497 497 497 497 497 498 499\r\n", "output": "500\r\n"}, {"input": "11 18 8\r\n4996 4993 4988 4982 4982 4982 4982 4982 4986 4989 4994\r\n", "output": "5000\r\n"}, {"input": "1 100000 1\r\n1000000000\r\n", "output": "1000100000\r\n"}, {"input": "4 100 3\r\n1 100000 100000 1\r\n", "output": "51\r\n"}]
| false
|
stdio
| null | true
|
400/B
|
400
|
B
|
PyPy 3
|
TESTS
| 4
| 62
| 1,228,800
|
155687535
|
n,m=map(int,input().split())
flag=0
d=dict()
for i in range(n):
x = input()
for j in range(m):
if x[j]=='G':
gidx=j
elif x[j]=='S':
sidx=j
if sidx-gidx<0:
flag=1;break
else:
if x in d:
d[x]+=1
else:
d[x]=1
print(len(d) if not flag else -1)
| 34
| 46
| 204,800
|
195791131
|
import sys
n, m = map(int, input().split())
output = set()
possible = True
for row in range(n):
initialState = input()
dwarfPos = initialState.find("G")
candyPos = initialState.find("S")
if dwarfPos > candyPos:
possible = False
break
elif dwarfPos < candyPos:
output.add(candyPos - dwarfPos)
if possible:
print(len(output))
else:
print("-1")
|
Codeforces Round 234 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Inna and New Matrix of Candies
|
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".
The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:
- some dwarf in one of the chosen lines is located in the rightmost cell of his row;
- some dwarf in the chosen lines is located in the cell with the candy.
The point of the game is to transport all the dwarves to the candy cells.
Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
|
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).
Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
|
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
| null | null |
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
| 1,200
|
["brute force", "implementation", "schedules"]
| 34
|
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
4/D
|
4
|
D
|
PyPy 3
|
TESTS
| 2
| 77
| 0
|
222160531
|
n, w_card, h_card = map(int, input().split())
envelopes = []
for i in range(n):
w, h = map(int, input().split())
envelopes.append((w, h, i + 1))
envelopes.sort()
dp = [1] * n
prev = [-1] * n
for i in range(1, n):
for j in range(i):
if envelopes[i][0] > envelopes[j][0] and envelopes[i][1] > envelopes[j][1]:
if dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
prev[i] = j
max_chain_size = max(dp)
max_chain_end = dp.index(max_chain_size)
chain = [envelopes[max_chain_end][2]]
while prev[max_chain_end] != -1:
max_chain_end = prev[max_chain_end]
chain.append(envelopes[max_chain_end][2])
if w_card < envelopes[chain[-1] - 1][0] and h_card < envelopes[chain[-1] - 1][1]:
print(max_chain_size)
print(*reversed(chain))
else:
print("0")
| 33
| 452
| 8,499,200
|
223575394
|
################################################################################
n,w,h=map(int,input().split())
envelope=[]
for i in range(n):
wcup,hcup=map(int,input().split())
if (wcup>w) and (hcup>h):
envelope.append((wcup,hcup,i+1))
# n=2
# w=1
# h=1
# envelope=[(2,2,1),(2,2,2)]
# n=3
# w=3
# h=3
# envelope=[(5,4,1),(12,11,2),(9,8,3),(5,5,4)]
################################################################################
if len(envelope)==0:
print(0)
exit()
n=len(envelope)
envelope.sort()
dp=[[1,-1] for i in range(n)]
for i in range(n):
for j in range(0,i):
if (envelope[i][0]>envelope[j][0]) and (envelope[i][1]>envelope[j][1]) and (dp[j][0]>=dp[i][0]):
dp[i][0]=dp[j][0]+1
dp[i][1]=j
p,index = max( (v,i) for i,v in enumerate(dp) )
print(p[0])
out=str(envelope[index][2])
while p[1]!=-1:
out=str(envelope[p[1]][2])+' '+out
p=dp[p[1]]
print(out)
|
Codeforces Beta Round 4 (Div. 2 Only)
|
ICPC
| 2,010
| 1
| 64
|
Mysterious Present
|
Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1, a2, ..., an}, where the width and the height of the i-th envelope is strictly higher than the width and the height of the (i - 1)-th envelope respectively. Chain size is the number of envelopes in the chain.
Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes.
Peter has very many envelopes and very little time, this hard task is entrusted to you.
|
The first line contains integers n, w, h (1 ≤ n ≤ 5000, 1 ≤ w, h ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi, hi ≤ 106).
|
In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.
If the card does not fit into any of the envelopes, print number 0 in the single line.
| null | null |
[{"input": "2 1 1\n2 2\n2 2", "output": "1\n1"}, {"input": "3 3 3\n5 4\n12 11\n9 8", "output": "3\n1 3 2"}]
| 1,700
|
["dp", "sortings"]
| 33
|
[{"input": "2 1 1\r\n2 2\r\n2 2\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 3 3\r\n5 4\r\n12 11\r\n9 8\r\n", "output": "3\r\n1 3 2 \r\n"}, {"input": "5 10 10\r\n22 23\r\n17 19\r\n13 17\r\n8 12\r\n2 6\r\n", "output": "3\r\n3 2 1 \r\n"}, {"input": "5 13 13\r\n4 4\r\n10 10\r\n7 7\r\n1 1\r\n13 13\r\n", "output": "0\r\n"}, {"input": "4 12 140\r\n172 60\r\n71 95\r\n125 149\r\n53 82\r\n", "output": "1\r\n3 \r\n"}, {"input": "3 500 789\r\n56 32\r\n64 42\r\n74 55\r\n", "output": "0\r\n"}, {"input": "4 100 100\r\n332 350\r\n232 250\r\n32 50\r\n132 150\r\n", "output": "3\r\n4 2 1 \r\n"}, {"input": "2 10 10\r\n15 15\r\n16 16\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "6 1 1\r\n900000 900000\r\n902400 902400\r\n901200 901200\r\n903600 903600\r\n906000 906000\r\n904800 904800\r\n", "output": "6\r\n1 3 2 4 6 5 \r\n"}, {"input": "5 1000 998\r\n5002 5005\r\n5003 5004\r\n5003 5002\r\n5002 5001\r\n5002 5002\r\n", "output": "2\r\n4 3 \r\n"}, {"input": "3 5 5\r\n6 2\r\n7 8\r\n10 2\r\n", "output": "1\r\n2 \r\n"}, {"input": "14 12 800\r\n166 847\r\n205 889\r\n223 907\r\n93 785\r\n110 803\r\n136 829\r\n189 871\r\n149 839\r\n40 740\r\n48 750\r\n180 857\r\n76 777\r\n125 820\r\n63 766\r\n", "output": "9\r\n5 13 6 8 1 11 7 2 3 \r\n"}, {"input": "15 600 875\r\n1200 451\r\n1664 852\r\n1763 1355\r\n1374 1724\r\n1374 1587\r\n1003 1513\r\n1636 1002\r\n431 367\r\n1632 690\r\n1257 778\r\n410 1632\r\n1045 1279\r\n1762 1763\r\n841 576\r\n1165 705\r\n", "output": "3\r\n6 5 13 \r\n"}, {"input": "30 900 15\r\n1396 562\r\n1265 475\r\n3329 2605\r\n1016 340\r\n2369 1595\r\n2085 1245\r\n2677 1934\r\n1953 1154\r\n3002 2199\r\n1688 855\r\n1147 407\r\n2762 2064\r\n2202 1359\r\n700 30\r\n3265 2488\r\n1884 1034\r\n2571 1836\r\n3067 2269\r\n882 212\r\n3505 2793\r\n1602 785\r\n2292 1460\r\n2442 1712\r\n2889 2127\r\n3187 2361\r\n1770 932\r\n3624 2891\r\n783 104\r\n3410 2704\r\n1521 696\r\n", "output": "27\r\n4 11 2 1 30 21 10 26 16 8 6 13 22 5 23 17 7 12 24 9 18 25 15 3 29 20 27 \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]
input_lines = read_file_lines(input_path)
if not input_lines:
print(0)
return
n, w, h = map(int, input_lines[0].split())
envelopes = []
for line in input_lines[1:n+1]:
parts = line.split()
if len(parts) != 2:
print(0)
return
wi, hi = map(int, parts)
envelopes.append((wi, hi))
ref_lines = read_file_lines(output_path)
if not ref_lines:
print(0)
return
try:
ref_k = int(ref_lines[0])
except:
print(0)
return
sub_lines = read_file_lines(submission_path)
if not sub_lines:
print(0)
return
if len(sub_lines) < 1:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if ref_k == 0:
if len(sub_lines) != 1:
print(0)
return
for wi, hi in envelopes:
if wi > w and hi > h:
print(0)
return
print(1)
return
else:
if len(sub_lines) != 2:
print(0)
return
try:
chain = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(chain) != ref_k:
print(0)
return
seen = set()
for idx in chain:
if idx < 1 or idx > n or idx in seen:
print(0)
return
seen.add(idx)
prev_w, prev_h = None, None
for idx in chain:
wi, hi = envelopes[idx - 1]
if wi <= w or hi <= h:
print(0)
return
if prev_w is not None:
if wi <= prev_w or hi <= prev_h:
print(0)
return
prev_w, prev_h = wi, hi
print(1)
return
if __name__ == "__main__":
main()
| true
|
627/D
|
627
|
D
|
PyPy 3
|
TESTS
| 8
| 4,258
| 41,984,000
|
80969198
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
g = [[] for _ in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
g[u-1].append(v-1)
g[v-1].append(u-1)
stack = [0]
done = [False] * n
par = [0] * n
order = []
while len(stack) > 0:
x = stack.pop()
done[x] = True
order.append(x)
for i in g[x]:
if done[i] == False:
par[i] = x
stack.append(i)
order = order[::-1]
sub = [0] * n
for i in order:
sub[i] = 1
for j in g[i]:
if par[j] == i:
sub[i] += sub[j]
def good(guess):
cnt = [0] * n
for i in order:
if a[i] < guess:
continue
cnt[i] = 1
opt = 0
for j in g[i]:
if par[j] == i:
if cnt[j] == sub[j]:
cnt[i] += cnt[j]
else:
opt = max(opt, cnt[j])
cnt[i] += opt
if cnt[0] >= k:
return True
up = [0] * n
for i in order[::-1]:
if a[i] < guess:
continue
opt, secondOpt = 0, 0
total = 1
for j in g[i]:
val, size = 0, 0
if par[j] == i:
val = cnt[j]
size = sub[j]
else:
val = up[i]
size = n - sub[i]
if val == size:
total += val
else:
if opt > val:
opt, secondOpt = val, opt
elif secondOpt > val:
secondOpt = val
for j in g[i]:
if par[j] == i:
up[j] = total
add = opt
if sub[j] == cnt[j]:
up[j] -= cnt[j]
elif cnt[j] == opt:
add = secondOpt
up[j] += add
for i in range(n):
if a[i] < guess:
continue
total, opt = 1, 0
for j in g[i]:
val, size = 0, 0
if par[j] == i:
val = cnt[j]
size = sub[j]
else:
val = up[i]
size = n - sub[i]
if val == size:
total += val
else:
opt = max(opt, val)
if total + opt >= k:
return True
return False
l, r = 0, max(a)
while l < r:
mid = (l + r + 1) // 2
if good(mid):
l = mid
else:
r = mid - 1
print(l)
| 69
| 5,116
| 50,380,800
|
80969556
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
g = [[] for _ in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
g[u-1].append(v-1)
g[v-1].append(u-1)
stack = [0]
done = [False] * n
par = [0] * n
order = []
while len(stack) > 0:
x = stack.pop()
done[x] = True
order.append(x)
for i in g[x]:
if done[i] == False:
par[i] = x
stack.append(i)
order = order[::-1]
sub = [0] * n
for i in order:
sub[i] = 1
for j in g[i]:
if par[j] == i:
sub[i] += sub[j]
def good(guess):
cnt = [0] * n
for i in order:
if a[i] < guess:
continue
cnt[i] = 1
opt = 0
for j in g[i]:
if par[j] == i:
if cnt[j] == sub[j]:
cnt[i] += cnt[j]
else:
opt = max(opt, cnt[j])
cnt[i] += opt
if cnt[0] >= k:
return True
up = [0] * n
for i in order[::-1]:
if a[i] < guess:
continue
opt, secondOpt = 0, 0
total = 1
for j in g[i]:
val, size = 0, 0
if par[j] == i:
val = cnt[j]
size = sub[j]
else:
val = up[i]
size = n - sub[i]
if val == size:
total += val
else:
if opt < val:
opt, secondOpt = val, opt
elif secondOpt < val:
secondOpt = val
for j in g[i]:
if par[j] == i:
up[j] = total
add = opt
if sub[j] == cnt[j]:
up[j] -= cnt[j]
elif cnt[j] == opt:
add = secondOpt
up[j] += add
for i in range(n):
if a[i] < guess:
continue
total, opt = 1, 0
for j in g[i]:
val, size = 0, 0
if par[j] == i:
val = cnt[j]
size = sub[j]
else:
val = up[i]
size = n - sub[i]
if val == size:
total += val
else:
opt = max(opt, val)
if total + opt >= k:
return True
return False
l, r = 0, max(a)
while l < r:
mid = (l + r + 1) // 2
if good(mid):
l = mid
else:
r = mid - 1
print(l)
|
8VC Venture Cup 2016 - Final Round
|
CF
| 2,016
| 7
| 256
|
Preorder Test
|
For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tree.
Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to search his whole tree to determine this; Jacob knows that she will examine the first k nodes in a DFS-order traversal of the tree. She will then assign Jacob a grade equal to the minimum ai she finds among those k nodes.
Though Jacob does not have enough time to rebuild his model, he can choose the root node that his teacher starts from. Furthermore, he can rearrange the list of neighbors of each node in any order he likes. Help Jacob find the best grade he can get on this assignment.
A DFS-order traversal is an ordering of the nodes of a rooted tree, built by a recursive DFS-procedure initially called on the root of the tree. When called on a given node v, the procedure does the following:
1. Print v.
2. Traverse the list of neighbors of the node v in order and iteratively call DFS-procedure on each one. Do not call DFS-procedure on node u if you came to node v directly from u.
|
The first line of the input contains two positive integers, n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n) — the number of balls in Jacob's tree and the number of balls the teacher will inspect.
The second line contains n integers, ai (1 ≤ ai ≤ 1 000 000), the time Jacob used to build the i-th ball.
Each of the next n - 1 lines contains two integers ui, vi (1 ≤ ui, vi ≤ n, ui ≠ vi) representing a connection in Jacob's tree between balls ui and vi.
|
Print a single integer — the maximum grade Jacob can get by picking the right root of the tree and rearranging the list of neighbors.
| null |
In the first sample, Jacob can root the tree at node 2 and order 2's neighbors in the order 4, 1, 5 (all other nodes have at most two neighbors). The resulting preorder traversal is 2, 4, 1, 3, 5, and the minimum ai of the first 3 nodes is 3.
In the second sample, it is clear that any preorder traversal will contain node 1 as either its first or second node, so Jacob cannot do better than a grade of 1.
|
[{"input": "5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3", "output": "3"}, {"input": "4 2\n1 5 5 5\n1 2\n1 3\n1 4", "output": "1"}]
| 2,600
|
["binary search", "dfs and similar", "dp", "graphs", "greedy", "trees"]
| 69
|
[{"input": "5 3\r\n3 6 1 4 2\r\n1 2\r\n2 4\r\n2 5\r\n1 3\r\n", "output": "3\r\n"}, {"input": "4 2\r\n1 5 5 5\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "1\r\n"}, {"input": "2 1\r\n1 100000\r\n2 1\r\n", "output": "100000\r\n"}, {"input": "2 2\r\n1 1000000\r\n1 2\r\n", "output": "1\r\n"}, {"input": "10 4\r\n104325 153357 265088 777795 337716 557321 702646 734430 464449 744072\r\n9 4\r\n8 1\r\n10 7\r\n8 6\r\n7 9\r\n8 2\r\n3 5\r\n8 3\r\n10 8\r\n", "output": "557321\r\n"}, {"input": "10 3\r\n703660 186846 317819 628672 74457 58472 247014 480113 252764 860936\r\n10 6\r\n7 4\r\n10 9\r\n9 5\r\n6 3\r\n6 2\r\n7 1\r\n10 7\r\n10 8\r\n", "output": "252764\r\n"}, {"input": "10 10\r\n794273 814140 758469 932911 607860 683826 987442 652494 952171 698608\r\n1 3\r\n3 8\r\n2 7\r\n2 1\r\n2 9\r\n3 10\r\n6 4\r\n9 6\r\n3 5\r\n", "output": "607860\r\n"}]
| false
|
stdio
| null | true
|
53/D
|
53
|
D
|
PyPy 3
|
TESTS
| 3
| 124
| 0
|
146262643
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = [0]
for i in range(n):
bi = b[i]
x = a[i:].index(bi) + i
for j in range(x, i, -1):
a[j], a[j - 1] = a[j - 1], a[j]
ans.append((j, j + 1))
ans[0] = str(len(ans) - 1)
for i in range(1, len(ans)):
ans[i] = " ".join(map(str, ans[i]))
sys.stdout.write("\n".join(ans))
| 30
| 186
| 102,400
|
156766195
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = []
y = []
f = 0
for i in range(n):
for j in range(i,n):
if a[i] == b[j]:
f = j
break
# print(f)
for j in range(f, i, -1):
temp = b[j]
b[j] = b[j-1]
b[j-1] = temp
x.append(j-1)
y.append(j)
print(len(x))
for i in range(len(x)):
print(x[i]+1, y[i]+1)
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500
|
["sortings"]
| 30
|
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
53/D
|
53
|
D
|
PyPy 3-64
|
TESTS
| 3
| 124
| 0
|
211348193
|
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations
from bisect import *
from heapq import *
from math import ceil,gcd,lcm,floor,comb
N = int(input())
B = list(map(int,input().split()))
A = list(map(int,input().split()))
ans = []
for i in range(N):
if A[i]!=B[i]:
for j in range(i+1,N):
if B[j]==A[i]:
for z in range(j-1,i-1,-1):
ans.append([z+1,z+2])
B[z],B[z+1] = B[z+1],B[z]
break
print(len(ans))
for i in ans:
print(*i)
| 30
| 186
| 4,096,000
|
146263164
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = [0]
for i in range(n):
ai = a[i]
x = b[i:].index(ai) + i
for j in range(x, i, -1):
b[j], b[j - 1] = b[j - 1], b[j]
ans.append((j, j + 1))
ans[0] = str(len(ans) - 1)
for i in range(1, len(ans)):
ans[i] = " ".join(map(str, ans[i]))
sys.stdout.write("\n".join(ans))
|
Codeforces Beta Round 49 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Physical Education
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
| null | null |
[{"input": "4\n1 2 3 2\n3 2 1 2", "output": "4\n2 3\n1 2\n3 4\n2 3"}, {"input": "2\n1 100500\n1 100500", "output": "0"}]
| 1,500
|
["sortings"]
| 30
|
[{"input": "4\r\n1 2 3 2\r\n3 2 1 2\r\n", "output": "4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n"}, {"input": "2\r\n1 100500\r\n1 100500\r\n", "output": "0\r\n"}, {"input": "3\r\n652586118 652586118 652586118\r\n652586118 652586118 652586118\r\n", "output": "3\r\n2 3\r\n1 2\r\n2 3\r\n"}, {"input": "4\r\n681106577 681106577 675077178 675077178\r\n675077178 681106577 681106577 675077178\r\n", "output": "4\r\n2 3\r\n1 2\r\n2 3\r\n3 4\r\n"}, {"input": "5\r\n470138369 747764103 729004864 491957578 874531368\r\n874531368 729004864 491957578 747764103 470138369\r\n", "output": "9\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n4 5\r\n3 4\r\n2 3\r\n3 4\r\n4 5\r\n"}, {"input": "6\r\n590202194 293471749 259345095 293471749 18056518 293471749\r\n293471749 293471749 293471749 18056518 259345095 590202194\r\n", "output": "12\r\n5 6\r\n4 5\r\n3 4\r\n2 3\r\n1 2\r\n3 4\r\n2 3\r\n5 6\r\n4 5\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "1\r\n873725529\r\n873725529\r\n", "output": "0\r\n"}, {"input": "1\r\n800950546\r\n800950546\r\n", "output": "0\r\n"}, {"input": "2\r\n344379439 344379439\r\n344379439 344379439\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2\r\n305292852 305292852\r\n305292852 305292852\r\n", "output": "1\r\n1 2\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
with open(input_path, 'r') as f_input:
n = int(f_input.readline().strip())
a = list(map(int, f_input.readline().split()))
initial_b = list(map(int, f_input.readline().split()))
with open(submission_path, 'r') as f_submission:
submission_lines = [line.strip() for line in f_submission.readlines()]
if not submission_lines:
print(0)
return
# Parse k
try:
k_line = submission_lines[0]
k = int(k_line)
except:
print(0)
return
if k < 0 or k > 10**6:
print(0)
return
if len(submission_lines) != k + 1:
print(0)
return
swaps = []
for line in submission_lines[1:]:
parts = line.split()
if len(parts) != 2:
print(0)
return
try:
x = int(parts[0])
y = int(parts[1])
except:
print(0)
return
if y != x + 1 or x < 1 or x > n - 1:
print(0)
return
swaps.append((x - 1, x)) # Convert to 0-based indices
# Apply swaps to initial array
current = initial_b.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
if current == a:
print(1)
else:
print(0)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
182/B
|
182
|
B
|
Python 3
|
TESTS
| 7
| 122
| 0
|
113132142
|
import math
d = int(input())
n = int(input())
a = list(map(int,input().split()))
disp = 0
count = 0
'''
everyday get up and clock adds 1
when this happens , if disp +1 > d then disp = 1
then if that same day if disp!= actu then disp+=1
and then also count+=1
disp -> 19900 ---d -> 20000
a[i] = 60
offset = 100
if disp!=0 on new month, i.e. new el of arr,
then we gotta a problem cause that means its
not correct
'''
for i in range(n):
if disp!=0:
offset = d - disp
if offset <= a[i]:
count+=offset
disp = a[i]
elif offset> int(2* a[i]):
disp+= a[i]*2
count+=a[i]
elif offset <= int(2*a[i]):
disp += a[i]*2
disp = disp - d
count += a[i]
else:
disp+=a[i]
continue
print(count)
| 40
| 92
| 0
|
4719186
|
def readln(): return tuple(map(int, input().split()))
d, = readln()
n, = readln()
prev = ans = 0
for cur in readln():
ans += 0 if prev == 0 else d - prev
prev = cur % d
print(ans)
|
Codeforces Round 117 (Div. 2)
|
CF
| 2,012
| 1
| 256
|
Vasya's Calendar
|
Vasya lives in a strange world. The year has n months and the i-th month has ai days. Vasya got a New Year present — the clock that shows not only the time, but also the date.
The clock's face can display any number from 1 to d. It is guaranteed that ai ≤ d for all i from 1 to n. The clock does not keep information about the current month, so when a new day comes, it simply increases the current day number by one. The clock cannot display number d + 1, so after day number d it shows day 1 (the current day counter resets). The mechanism of the clock allows you to increase the day number by one manually. When you execute this operation, day d is also followed by day 1.
Vasya begins each day checking the day number on the clock. If the day number on the clock does not match the actual day number in the current month, then Vasya manually increases it by one. Vasya is persistent and repeats this operation until the day number on the clock matches the actual number of the current day in the current month.
A year passed and Vasya wonders how many times he manually increased the day number by one, from the first day of the first month to the last day of the n-th month inclusive, considering that on the first day of the first month the clock display showed day 1.
|
The first line contains the single number d — the maximum number of the day that Vasya's clock can show (1 ≤ d ≤ 106).
The second line contains a single integer n — the number of months in the year (1 ≤ n ≤ 2000).
The third line contains n space-separated integers: ai (1 ≤ ai ≤ d) — the number of days in each month in the order in which they follow, starting from the first one.
|
Print a single number — the number of times Vasya manually increased the day number by one throughout the last year.
| null |
In the first sample the situation is like this:
- Day 1. Month 1. The clock shows 1. Vasya changes nothing.
- Day 2. Month 1. The clock shows 2. Vasya changes nothing.
- Day 1. Month 2. The clock shows 3. Vasya manually increases the day number by 1. After that the clock shows 4. Vasya increases the day number by 1 manually. After that the clock shows 1.
- Day 2. Month 2. The clock shows 2. Vasya changes nothing.
|
[{"input": "4\n2\n2 2", "output": "2"}, {"input": "5\n3\n3 4 3", "output": "3"}, {"input": "31\n12\n31 28 31 30 31 30 31 31 30 31 30 31", "output": "7"}]
| 1,000
|
["implementation"]
| 40
|
[{"input": "4\r\n2\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5\r\n3\r\n3 4 3\r\n", "output": "3\r\n"}, {"input": "31\r\n12\r\n31 28 31 30 31 30 31 31 30 31 30 31\r\n", "output": "7\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "1\r\n2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2\r\n2\r\n1 1\r\n", "output": "1\r\n"}, {"input": "10\r\n2\r\n10 2\r\n", "output": "0\r\n"}, {"input": "10\r\n3\r\n6 3 6\r\n", "output": "11\r\n"}, {"input": "10\r\n4\r\n8 7 1 5\r\n", "output": "14\r\n"}, {"input": "10\r\n5\r\n2 7 8 4 4\r\n", "output": "19\r\n"}, {"input": "10\r\n6\r\n8 3 4 9 6 1\r\n", "output": "20\r\n"}, {"input": "10\r\n7\r\n10 5 3 1 1 9 1\r\n", "output": "31\r\n"}, {"input": "10\r\n8\r\n6 5 10 6 8 1 3 2\r\n", "output": "31\r\n"}, {"input": "10\r\n9\r\n6 2 7 5 5 4 8 6 2\r\n", "output": "37\r\n"}, {"input": "10\r\n10\r\n1 10 1 10 1 1 7 8 6 7\r\n", "output": "45\r\n"}]
| false
|
stdio
| null | true
|
12/C
|
12
|
C
|
Python 3
|
TESTS
| 3
| 31
| 0
|
230251490
|
n,k = map(int,input().split())
arr = list(map(int,input().split()))
arr.sort()
arr2 = []
for _ in range(k):
fruit = input()
arr2.append(fruit)
arr3 = set(arr2)
min_ =0
i = 0
while i <len(arr3):
min_ += arr[i]
i +=1
j = len(arr)-1
maxx_ = 0
while j > (len(arr)-1-len(arr3)):
maxx_ += arr[j]
j -=1
if len(arr3)<k:
print(min_ + arr[0],maxx_ + arr[-1])
else:
print(min_ , maxx_)
| 25
| 46
| 0
|
178913623
|
import sys; R = sys.stdin.readline
n,m = map(int,R().split())
a = sorted(map(int,R().split()))
dic = {}
for _ in range(m):
s = R()
if s not in dic: dic[s] = 1
else: dic[s] += 1
dic = sorted(dic.values(),reverse=True)
res1,res2 = 0,0
for i in range(len(dic)):
res1 += a[i]*dic[i]
res2 += a[-i-1]*dic[i]
print(res1,res2)
|
Codeforces Beta Round 12 (Div 2 Only)
|
ICPC
| 2,010
| 1
| 256
|
Fruits
|
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.
When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
|
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
|
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
| null | null |
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
| 1,100
|
["greedy", "implementation", "sortings"]
| 25
|
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
| false
|
stdio
| null | true
|
28/A
|
28
|
A
|
Python 3
|
TESTS
| 1
| 124
| 307,200
|
5760049
|
from collections import defaultdict
n, m = map(int, input().split())
a, b = map(int, input().split())
x, y = a, b
s = [0] * n
for i in range(n - 1):
u, v = map(int, input().split())
if u == a: s[i], b = abs(v - b), v
else: s[i], a = abs(u - a), u
s[n - 1] = abs(a - x) + abs(b - y)
a = defaultdict(list)
for i, j in enumerate(map(int, input().split()), 1): a[j].append(i)
b = a.copy()
q = []
for i in range(0, n, 2):
j = s[i] + s[i + 1]
if not a[j]: break
q.append(str(a[j].pop()))
if 2 * len(q) == n: print('YES\n' + ' -1 '.join(q) + ' -1')
else:
q = []
for i in range(0, n, 2):
j = s[i] + s[i - 1]
if b[j]: q.append(str(b[j].pop()))
if 2 * len(q) == n: print('YES\n-1 ' + ' -1 '.join(q))
else: print('NO')
| 51
| 154
| 2,252,800
|
199146404
|
import sys; R = sys.stdin.readline
S = lambda: map(int,R().split())
from collections import Counter
n,m = S()
if m*2<n: print("NO"); exit()
a = [[*S()] for _ in range(n)]
a += a[0],
b = [abs(a[i][0]-a[i+1][0])+abs(a[i][1]-a[i+1][1]) for i in range(n)]
r = [*S()]
cr = Counter(r)
for k in -1,1:
b1 = [b[2*i]+b[2*i+k] for i in range(n//2)]
c1 = Counter(b1)
if all(c1[k]<=cr[k] for k in c1):
print("YES")
w = [-1]*n
d = {}
for i,x in enumerate(r,1):
if x in d: d[x] += i,
else: d[x] = [i]
for j in range(n//2):
w[2*j+(k+1)//2] = d[b1[j]].pop()
print(*w)
exit()
print("NO")
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Bender Problem
|
Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods.
Help Bender to solve this difficult task.
|
The first line contains two positive integers n and m (4 ≤ n ≤ 500, 2 ≤ m ≤ 500, n is even) — the amount of nails and the amount of rods. i-th of the following n lines contains a pair of integers, denoting the coordinates of the i-th nail. Nails should be connected in the same order as they are given in the input. The last line contains m integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200 000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line.
|
If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output n numbers — i-th of them should be the number of rod, which fold place is attached to the i-th nail, or -1, if there is no such rod.
If there are multiple solutions, print any of them.
| null | null |
[{"input": "4 2\n0 0\n0 2\n2 2\n2 0\n4 4", "output": "YES\n1 -1 2 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3", "output": "YES\n1 -1 2 -1 3 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3", "output": "NO"}]
| 1,600
|
["implementation"]
| 51
|
[{"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n4 4\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n3 2 3\r\n", "output": "YES\r\n1 -1 2 -1 3 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2 3\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n0 0\r\n0 1\r\n1 1\r\n1 0\r\n1 1 1 1\r\n", "output": "NO\r\n"}, {"input": "6 2\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "6 3\r\n0 0\r\n2 0\r\n2 2\r\n1 2\r\n1 1\r\n0 1\r\n4 2 2\r\n", "output": "YES\r\n-1 1 -1 2 -1 3 "}, {"input": "4 4\r\n-8423 7689\r\n6902 7689\r\n6902 2402\r\n-8423 2402\r\n20612 20612 91529 35617\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "4 4\r\n1679 -198\r\n9204 -198\r\n9204 -5824\r\n1679 -5824\r\n18297 92466 187436 175992\r\n", "output": "NO\r\n"}, {"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n200000 200000\r\n", "output": "NO\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
with open(input_path) as f:
n, m = map(int, f.readline().split())
nails = [tuple(map(int, f.readline().split())) for _ in range(n)]
rods = list(map(int, f.readline().split()))
with open(output_path) as f:
ref_lines = f.read().splitlines()
with open(submission_path) as f:
sub_lines = f.read().splitlines()
if not sub_lines:
print(0)
return
ref_is_no = ref_lines and ref_lines[0].strip() == "NO"
sub_is_no = sub_lines and sub_lines[0].strip() == "NO"
if ref_is_no:
print(1 if sub_is_no else 0)
return
else:
if not sub_lines or sub_lines[0].strip() != "YES":
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
sub_list = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(sub_list) != n:
print(0)
return
used_rods = set()
for num in sub_list:
if num == -1:
continue
if num < 1 or num > m or num in used_rods:
print(0)
return
used_rods.add(num)
for i in range(n):
rod_num = sub_list[i]
if rod_num == -1:
continue
prev_i = i - 1 if i > 0 else n - 1
next_i = (i + 1) % n
x_prev, y_prev = nails[prev_i]
x_curr, y_curr = nails[i]
x_next, y_next = nails[next_i]
len_prev = abs(y_curr - y_prev) if x_prev == x_curr else abs(x_curr - x_prev)
len_next = abs(y_next - y_curr) if x_curr == x_next else abs(x_next - x_curr)
sum_len = len_prev + len_next
rod_index = rod_num - 1
if rod_index >= len(rods) or rods[rod_index] != sum_len:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
28/A
|
28
|
A
|
Python 3
|
TESTS
| 1
| 156
| 307,200
|
67726217
|
def dist(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def get_sorted_required_pruts(dists):
return sorted([dists[i * 2] + dists[i * 2 + 1] for i in range(len(dists) // 2)])
def get_answer(pruts, required_pruts):
i = 0
j = 0
answer = "YES"
seq = []
while i < len(required_pruts):
if j == len(pruts):
answer = "NO"
return answer, pruts
if pruts[j][1] > required_pruts[i]:
answer = "NO"
return answer, pruts
if pruts[j][1] < required_pruts[i]:
j += 1
else:
seq.append(pruts[j][0] + 1)
seq.append(-1)
i += 1
j += 1
return answer, seq
n, m = map(int,input().split())
gvozdi = [None] * n
for i in range(n):
gvozdi[i] = list(map(int,input().split()))
pruts = list(map(int,input().split()))
pruts = [(i, p) for i, p in enumerate(pruts)]
pruts = sorted(pruts, key=lambda x: x[1])
dists = [dist(gvozdi[i], gvozdi[i + 1]) for i in range(len(gvozdi) - 1)]
dists.append(dist(gvozdi[0], gvozdi[-1]))
even_required_pruts = get_sorted_required_pruts(dists)
odd_required_pruts = get_sorted_required_pruts(dists[-1:] + dists[:-1])
even_answer, even_seq = get_answer(pruts, even_required_pruts)
odd_answer, odd_seq = get_answer(pruts, odd_required_pruts)
if even_answer == "NO" and odd_answer == "NO":
print("NO")
elif even_answer == "YES":
print("YES")
print(" ".join(map(str, even_seq)))
else:
print("YES")
print(" ".join(map(str, odd_seq[-1:] + odd_seq[:-1])))
| 51
| 186
| 1,740,800
|
216108451
|
import sys
def ff(x, y):
return abs(sum(x)-sum(y))
n, m = map(int, input().split())
points = [tuple(map(int, input().split())) for i in range(n)]
points.append(points[0])
segments = []
for i in range(n):
segments.append(ff(points[i], points[i + 1]))
rods = list(map(int, input().split()))
rod_indices = {}
for index, rod in enumerate(rods):
rod_indices.setdefault(rod, []).append(index + 1)
for offset in range(2):
target_indices = rod_indices.copy()
assignment = [-1 for i in range(n)]
for i in range(offset, n, 2):
target = segments[(i - 1) % n] + segments[i]
if target not in target_indices or target_indices[target] == []:
assignment = None
break
assignment[i] = target_indices[target].pop()
if assignment != None:
print('YES')
print(' '.join(map(str, assignment)))
sys.exit()
print('NO')
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Bender Problem
|
Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods.
Help Bender to solve this difficult task.
|
The first line contains two positive integers n and m (4 ≤ n ≤ 500, 2 ≤ m ≤ 500, n is even) — the amount of nails and the amount of rods. i-th of the following n lines contains a pair of integers, denoting the coordinates of the i-th nail. Nails should be connected in the same order as they are given in the input. The last line contains m integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200 000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line.
|
If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output n numbers — i-th of them should be the number of rod, which fold place is attached to the i-th nail, or -1, if there is no such rod.
If there are multiple solutions, print any of them.
| null | null |
[{"input": "4 2\n0 0\n0 2\n2 2\n2 0\n4 4", "output": "YES\n1 -1 2 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3", "output": "YES\n1 -1 2 -1 3 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3", "output": "NO"}]
| 1,600
|
["implementation"]
| 51
|
[{"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n4 4\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n3 2 3\r\n", "output": "YES\r\n1 -1 2 -1 3 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2 3\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n0 0\r\n0 1\r\n1 1\r\n1 0\r\n1 1 1 1\r\n", "output": "NO\r\n"}, {"input": "6 2\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "6 3\r\n0 0\r\n2 0\r\n2 2\r\n1 2\r\n1 1\r\n0 1\r\n4 2 2\r\n", "output": "YES\r\n-1 1 -1 2 -1 3 "}, {"input": "4 4\r\n-8423 7689\r\n6902 7689\r\n6902 2402\r\n-8423 2402\r\n20612 20612 91529 35617\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "4 4\r\n1679 -198\r\n9204 -198\r\n9204 -5824\r\n1679 -5824\r\n18297 92466 187436 175992\r\n", "output": "NO\r\n"}, {"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n200000 200000\r\n", "output": "NO\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
with open(input_path) as f:
n, m = map(int, f.readline().split())
nails = [tuple(map(int, f.readline().split())) for _ in range(n)]
rods = list(map(int, f.readline().split()))
with open(output_path) as f:
ref_lines = f.read().splitlines()
with open(submission_path) as f:
sub_lines = f.read().splitlines()
if not sub_lines:
print(0)
return
ref_is_no = ref_lines and ref_lines[0].strip() == "NO"
sub_is_no = sub_lines and sub_lines[0].strip() == "NO"
if ref_is_no:
print(1 if sub_is_no else 0)
return
else:
if not sub_lines or sub_lines[0].strip() != "YES":
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
sub_list = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(sub_list) != n:
print(0)
return
used_rods = set()
for num in sub_list:
if num == -1:
continue
if num < 1 or num > m or num in used_rods:
print(0)
return
used_rods.add(num)
for i in range(n):
rod_num = sub_list[i]
if rod_num == -1:
continue
prev_i = i - 1 if i > 0 else n - 1
next_i = (i + 1) % n
x_prev, y_prev = nails[prev_i]
x_curr, y_curr = nails[i]
x_next, y_next = nails[next_i]
len_prev = abs(y_curr - y_prev) if x_prev == x_curr else abs(x_curr - x_prev)
len_next = abs(y_next - y_curr) if x_curr == x_next else abs(x_next - x_curr)
sum_len = len_prev + len_next
rod_index = rod_num - 1
if rod_index >= len(rods) or rods[rod_index] != sum_len:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
PyPy 3
|
TESTS
| 25
| 372
| 32,256,000
|
38968630
|
n = int(input())
l = input()
l = [int(i) for i in l.split()]
r = 0
for i in range(0 , len(l)-2):
if l[i]>l[i+1] and l[i+1]<l[i+2]:
print(3)
print(i+1 , i+2 , i+3)
r = 1
break
elif l[i]<l[i+1] and l[i+1]>l[i+2]:
print(3)
print(i+1 , i+2 , i+3)
r = 1 ; break
if r==0:
print(0)
| 37
| 184
| 13,209,600
|
216585228
|
def main():
n = int(input())
a = list(map(int, input().split()))
p1, p2 = True, True
for i in range(1, n):
if a[i] > a[i - 1]:
p1 = False
if a[i] < a[i - 1]:
p2 = False
if not (p1 or p2):
print("3")
print(1, i, i + 1)
return
print("0")
if __name__ == "__main__":
main()# 1690811174.1351662
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
PyPy 3
|
TESTS
| 25
| 498
| 95,846,400
|
89534617
|
# Author: S Mahesh Raju
# Username: maheshraju2020
# Date: 11/08/2020
from sys import stdin, stdout, setrecursionlimit
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda: int(stdin.readline().strip())
is1 = lambda: stdin.readline().strip()
iia = lambda: list(map(int, stdin.readline().strip().split()))
isa = lambda: stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
n = ii1()
arr = iia()
for i in range(2, n):
cur = [arr[i - 2], arr[i - 1], arr[i]]
if sorted(cur) != cur and sorted(cur, reverse=True) != cur:
print(3)
print(i - 1, i, i + 1)
break
else:
print(0)
| 37
| 186
| 13,516,800
|
224073094
|
input()
t = list(map(int, input().split()))
for i in range(2, len(t)):
if (t[i] - t[i - 1]) * (t[i - 1] - t[0]) < 0:
print(3, 1, i, i + 1)
exit()
print(0)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
537/E
|
538
|
E
|
Python 3
|
TESTS
| 2
| 77
| 6,963,200
|
116183467
|
import sys
input = sys.stdin.readline
def solve():
n = int(input())
g = [[] for i in range(n+1)]
for i in range(1, n):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
q = [1]
d = [None]*(n+1)
d[1] = 0
i = 0
while i < len(q):
x = q[i]
#print(x)
i += 1
for v in g[x]:
if d[v] is None:
q.append(v)
d[v] = d[x] + 1
g[v].remove(x)
m = [0]*(n+1)
M = [0]*(n+1)
cnt = 0
for i in range(len(q)-1,-1,-1):
x = q[i]
if len(g[x]) == 0:
m[x] = 1
cnt += 1
elif d[x] % 2 == 0:
c = 0
C = int(1e9)
for v in g[x]:
c += m[v]
for v in g[x]:
C = min(C, m[v])
m[x] = c
M[x] = C
else:
c = int(1e9)
C = 0
for v in g[x]:
c = min(c, m[v])
for v in g[x]:
C += M[v]
m[x] = c
M[x] = C
print(cnt + 1 - M[x], m[1])
solve()
| 35
| 826
| 55,808,000
|
86848147
|
import sys
fin = sys.stdin
n = int(fin.readline())
ut = [-1] * n
vc = [[] for i in range(0, n)]
cvc = [[] for i in range(0, n)]
nr = [0] * n
for i in range(0, n - 1):
a, b = [int(number) for number in fin.readline().split()]
a -= 1
b -= 1
vc[a].append(b)
cvc[b].append(a)
nr[a] += 1
size = [0] * n
mx = [0] * n
def nonzero(x):
if not x:
return 0
return 1
stk = []
size = [0] * n
for i in range(0, n):
if nr[i] == 0:
stk.append(i)
size[i] = 1
order = []
while nonzero(stk):
x = stk.pop(-1)
order.append(x)
for p in cvc[x]:
nr[p] -= 1
if nr[p] == 0:
stk.append(p)
h = [0] * n
for i in range(n - 1, -1, -1):
x = order[i]
for p in vc[x]:
h[p] = h[x] + 1
#parcurg
for x in order:
for p in vc[x]:
size[x] += size[p]
#maximum
def solv(tp, mx):
for x in order:
if h[x] % 2 == tp:
r = 999999999
for p in vc[x]:
r = min(r, size[p] - mx[p] + 1)
if r == 999999999:
r = 1
mx[x] = r
else:
r = 0
for p in vc[x]:
r += size[p] - mx[p]
mx[x] = r + 1
solv(0, mx)
print(size[0] - mx[0] + 1, end = ' ')
solv(1, mx)
print(size[0] - mx[0] + 1)
|
VK Cup 2015 - Wild Card Round 2
|
IOI
| 2,015
| 2
| 256
|
Demiurges Play Again
|
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
|
The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·105).
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
|
Print two space-separated integers — the maximum possible and the minimum possible result of the game.
| null |
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
|
[{"input": "5\n1 2\n1 3\n2 4\n2 5", "output": "3 2"}, {"input": "6\n1 2\n1 3\n3 4\n1 5\n5 6", "output": "3 3"}]
| 2,200
|
["dfs and similar", "dp", "math", "trees"]
| 35
|
[{"input": "5\r\n1 2\r\n1 3\r\n2 4\r\n2 5\r\n", "output": "3 2\r\n"}, {"input": "6\r\n1 2\r\n1 3\r\n3 4\r\n1 5\r\n5 6\r\n", "output": "3 3\r\n"}, {"input": "1\r\n", "output": "1 1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "1 1\r\n"}, {"input": "3\r\n1 2\r\n1 3\r\n", "output": "2 2\r\n"}, {"input": "10\r\n1 2\r\n1 3\r\n3 4\r\n3 5\r\n4 6\r\n1 7\r\n3 8\r\n2 9\r\n6 10\r\n", "output": "5 3\r\n"}, {"input": "50\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\n11 12\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 17\r\n17 18\r\n18 19\r\n19 20\r\n20 21\r\n21 22\r\n22 23\r\n23 24\r\n24 25\r\n25 26\r\n26 27\r\n27 28\r\n28 29\r\n29 30\r\n30 31\r\n31 32\r\n32 33\r\n33 34\r\n34 35\r\n35 36\r\n36 37\r\n37 38\r\n38 39\r\n39 40\r\n40 41\r\n41 42\r\n42 43\r\n43 44\r\n44 45\r\n45 46\r\n46 47\r\n47 48\r\n48 49\r\n49 50\r\n", "output": "1 1\r\n"}, {"input": "22\r\n1 2\r\n2 3\r\n3 4\r\n1 5\r\n1 6\r\n1 7\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n1 12\r\n12 13\r\n12 14\r\n14 15\r\n1 16\r\n16 17\r\n1 18\r\n18 19\r\n1 20\r\n20 21\r\n1 22\r\n", "output": "10 9\r\n"}]
| false
|
stdio
| null | true
|
961/E
|
961
|
E
|
PyPy 3
|
TESTS
| 6
| 296
| 18,124,800
|
50577971
|
N = int(input())
A = list(map(int, input().split()))
def getsum(BITTree,i):
s = 0
i = i+1
while i > 0:
s += BITTree[i]
i -= i & (-i)
return s
def updatebit(BITTree , n , i ,v):
i += 1
while i <= n:
BITTree[i] += v
i += i & (-i)
def construct(arr, n):
BITTree = [0]*(n+1)
for i in range(n):
updatebit(BITTree, n, i, arr[i])
return BITTree
BIT = construct([0]*(N+1), N+1)
ans = 0
for i in range(N):
a = min(A[i],N)
# s = ""
# for j in range(1,N+1):
# s += str(getsum(BIT,j)) + " "
# print(s)
#
k = getsum(BIT,i+1)
# print(i,a,k)
ans += min(a, k)
updatebit(BIT, N+1, 1,1)
updatebit(BIT, N+1, a+1,-1)
print(ans)
| 30
| 498
| 44,236,800
|
128480912
|
from collections import defaultdict
import sys
input = sys.stdin.readline
def make_tree(n):
tree = [0] * (n + 1)
return tree
def get_sum(i):
s = 0
while i > 0:
s += tree[i]
i -= i & -i
return s
def get_sum_segment(s, t):
if s > t:
return 0
ans = get_sum(t) - get_sum(s - 1)
return ans
def add(i, x):
while i <= n:
tree[i] += x
i += i & -i
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(lambda : [])
tree = make_tree(n)
for i in range(n):
a[i] = min(a[i], n)
d[a[i]].append(i)
add(i + 1, 1)
ans = 0
for i in range(n):
ans += get_sum_segment(i + 2, a[i])
for j in d[i + 1]:
add(j + 1, -1)
print(ans)
|
Educational Codeforces Round 41 (Rated for Div. 2)
|
ICPC
| 2,018
| 2
| 256
|
Tufurama
|
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
|
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
|
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
| null |
Possible pairs in the second example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 2, y = 3 (season 2 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 2);
3. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
In the third example:
1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1);
2. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
|
[{"input": "5\n1 2 3 4 5", "output": "0"}, {"input": "3\n8 12 7", "output": "3"}, {"input": "3\n3 2 1", "output": "2"}]
| 1,900
|
["data structures"]
| 30
|
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "3\r\n8 12 7\r\n", "output": "3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 3 4 5 6\r\n", "output": "4\r\n"}, {"input": "8\r\n7 2 6 6 5 1 4 9\r\n", "output": "9\r\n"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "45\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
106/D
|
106
|
D
|
PyPy 3-64
|
TESTS
| 4
| 186
| 2,764,800
|
176909944
|
import sys
import math
import collections
import random
from heapq import heappush, heappop
from functools import reduce
input = sys.stdin.readline
ints = lambda: list(map(int, input().split()))
n, m = ints()
grid = []
for _ in range(n):
line = input().strip()
grid.append(list(line))
prefix_r = []
for i in range(n):
p = [0] if grid[i][0] != "#" else [1]
for j in range(1, m):
if grid[i][j] == "#":
p.append(p[-1] + 1)
else: p.append(p[-1])
p.append(0)
prefix_r.append(p)
prefix_c = []
for i in range(m):
p = [0] if grid[0][i] != "#" else [1]
for j in range(1, n):
if grid[j][i] == "#":
p.append(p[-1] + 1)
else: p.append(p[-1])
p.append(0)
prefix_c.append(p)
k = int(input())
instructions = []
for _ in range(k):
line = input().strip().split()
instructions.append((line[0], int(line[1])))
dirs = {"N": -1, "S": 1, "E": -1, "W": 1}
def possible(i, j):
for d, amt in instructions:
if d == "N" or d == "S":
ni = dirs[d] * amt + i
if 0 <= ni < n:
if ni > i and prefix_c[j][ni] != prefix_c[j][i - 1]: return False
elif prefix_c[j][i] != prefix_c[j][ni - 1]: return False
else:
return False
i = ni
else:
nj = dirs[d] * amt + j
if 0 <= nj < m:
if nj > j and prefix_r[i][nj] != prefix_r[i][j - 1]: return False
elif prefix_r[i][j] != prefix_r[i][nj - 1]: return False
else: return False
j = nj
return True
solutions = []
for i in range(n):
for j in range(m):
if grid[i][j] != "#" and grid[i][j] != ".":
ans = possible(i, j)
if ans:
solutions.append(grid[i][j])
if len(solutions) > 0:
solutions.sort()
print("".join(solutions))
else:
print("no solution")
| 70
| 1,090
| 28,160,000
|
153861321
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
a, pos = [], []
for i in range(n):
a.append(input().rstrip())
for j in range(m):
if a[i][j] != '.' and a[i][j] != '#':
pos.append((i, j))
W = [[0]*(m+3) for i in range(n+3)]
for i in range(n):
for j in range(m):
if a[i][j] != '#':
W[i][j] = W[i][j-1] + 1
E = [[0]*(m+3) for i in range(n+3)]
for i in range(n):
for j in range(m-1, -1, -1):
if a[i][j] != '#':
E[i][j] = E[i][j+1] + 1
N = [[0]*(m+3) for i in range(n+3)]
for j in range(m):
for i in range(n):
if a[i][j] != '#':
N[i][j] = N[i-1][j] + 1
S = [[0]*(m+3) for i in range(n+3)]
for j in range(m):
for i in range(n-1, -1, -1):
if a[i][j] != '#':
S[i][j] = S[i+1][j] + 1
k = int(input())
d, l = [0]*k, [0]*k
for i in range(k):
di, li = input().split()
d[i], l[i] = di, int(li)
kq = []
for x, y in pos:
ok, dut = 1, a[x][y]
for i in range(k):
if d[i] == 'E':
if E[x][y] - 1 < l[i]:
ok = 0
break
else:
y += l[i]
if d[i] == 'W':
if W[x][y] - 1 < l[i]:
ok = 0
break
else:
y -= l[i]
if d[i] == 'N':
if N[x][y] - 1 < l[i]:
ok = 0
break
else:
x -= l[i]
continue
if d[i] == 'S':
if S[x][y] - 1 < l[i]:
ok = 0
break
else:
x += l[i]
if ok == 1:
kq.append(dut)
if len(kq) == 0:
print('no solution')
else:
kq.sort()
print(''.join(kq))
|
Codeforces Beta Round 82 (Div. 2)
|
CF
| 2,011
| 2
| 256
|
Treasure Island
|
Our brave travelers reached an island where pirates had buried treasure. However as the ship was about to moor, the captain found out that some rat ate a piece of the treasure map.
The treasure map can be represented as a rectangle n × m in size. Each cell stands for an islands' square (the square's side length equals to a mile). Some cells stand for the sea and they are impenetrable. All other cells are penetrable (i.e. available) and some of them contain local sights. For example, the large tree on the hills or the cave in the rocks.
Besides, the map also has a set of k instructions. Each instruction is in the following form:
"Walk n miles in the y direction"
The possible directions are: north, south, east, and west. If you follow these instructions carefully (you should fulfill all of them, one by one) then you should reach exactly the place where treasures are buried.
Unfortunately the captain doesn't know the place where to start fulfilling the instructions — as that very piece of the map was lost. But the captain very well remembers that the place contained some local sight. Besides, the captain knows that the whole way goes through the island's penetrable squares.
The captain wants to know which sights are worth checking. He asks you to help him with that.
|
The first line contains two integers n and m (3 ≤ n, m ≤ 1000).
Then follow n lines containing m integers each — the island map's description. "#" stands for the sea. It is guaranteed that all cells along the rectangle's perimeter are the sea. "." stands for a penetrable square without any sights and the sights are marked with uppercase Latin letters from "A" to "Z". Not all alphabet letters can be used. However, it is guaranteed that at least one of them is present on the map. All local sights are marked by different letters.
The next line contains number k (1 ≤ k ≤ 105), after which k lines follow. Each line describes an instruction. Each instruction possesses the form "dir len", where dir stands for the direction and len stands for the length of the way to walk. dir can take values "N", "S", "W" and "E" for North, South, West and East correspondingly. At that, north is to the top, South is to the bottom, west is to the left and east is to the right. len is an integer from 1 to 1000.
|
Print all local sights that satisfy to the instructions as a string without any separators in the alphabetical order. If no sight fits, print "no solution" without the quotes.
| null | null |
[{"input": "6 10\n##########\n#K#..#####\n#.#..##.##\n#..L.#...#\n###D###A.#\n##########\n4\nN 2\nS 1\nE 1\nW 2", "output": "AD"}, {"input": "3 4\n####\n#.A#\n####\n2\nW 1\nN 2", "output": "no solution"}]
| 1,700
|
["brute force", "implementation"]
| 70
|
[{"input": "6 10\r\n##########\r\n#K#..#####\r\n#.#..##.##\r\n#..L.#...#\r\n###D###A.#\r\n##########\r\n4\r\nN 2\r\nS 1\r\nE 1\r\nW 2\r\n", "output": "AD"}, {"input": "3 4\r\n####\r\n#.A#\r\n####\r\n2\r\nW 1\r\nN 2\r\n", "output": "no solution"}, {"input": "10 10\r\n##########\r\n#K#..##..#\r\n##...ZB..#\r\n##.......#\r\n#D..#....#\r\n##...Y..##\r\n#...N...J#\r\n#.G...#.##\r\n#.S.I....#\r\n##########\r\n4\r\nE 2\r\nW 4\r\nS 3\r\nN 4\r\n", "output": "YZ"}, {"input": "6 6\r\n######\r\n#UPWT#\r\n#KJSL#\r\n#VCMA#\r\n#QOGB#\r\n######\r\n5\r\nN 1\r\nW 1\r\nS 1\r\nE 1\r\nN 1\r\n", "output": "ABCGJLMOS"}, {"input": "3 3\r\n###\r\n#A#\r\n###\r\n1\r\nN 1\r\n", "output": "no solution"}, {"input": "7 8\r\n########\r\n#..#O.##\r\n##U#YTI#\r\n##.#####\r\n##R.E.P#\r\n###W#Q##\r\n########\r\n2\r\nN 1\r\nW 1\r\n", "output": "QTUW"}, {"input": "4 9\r\n#########\r\n#AB#CDEF#\r\n#.......#\r\n#########\r\n1\r\nE 3\r\n", "output": "C"}, {"input": "4 9\r\n#########\r\n#AB#CDEF#\r\n#.......#\r\n#########\r\n1\r\nW 3\r\n", "output": "F"}, {"input": "4 9\r\n#########\r\n#AB#CDEF#\r\n#.......#\r\n#########\r\n1\r\nS 1\r\n", "output": "ABCDEF"}, {"input": "9 4\r\n####\r\n#AB#\r\n#C.#\r\n##.#\r\n#D.#\r\n#E.#\r\n#F.#\r\n#G.#\r\n####\r\n1\r\nN 3\r\n", "output": "G"}, {"input": "9 4\r\n####\r\n#AB#\r\n#C.#\r\n##.#\r\n#D.#\r\n#E.#\r\n#F.#\r\n#G.#\r\n####\r\n1\r\nS 3\r\n", "output": "BD"}, {"input": "10 11\r\n###########\r\n#.F.#.P.###\r\n#..#.#....#\r\n#Y#.U....##\r\n##....#...#\r\n##.OV.NK#.#\r\n#H####.#EA#\r\n#R.#....###\r\n##..#MS.D##\r\n###########\r\n3\r\nW 2\r\nE 1\r\nN 1\r\n", "output": "DNV"}]
| false
|
stdio
| null | true
|
28/A
|
28
|
A
|
Python 3
|
TESTS
| 1
| 124
| 0
|
11979634
|
import sys
n, m = map(int, input().split())
points = [tuple(map(int, input().split())) for i in range(n)]
segments = []
x, y = points[-1]
for a, b in points:
if x == a:
segments.append(abs(y - b))
else:
segments.append(abs(x - a))
x, y = a, b
rods = list(map(int, input().split()))
rod_indices = {}
for index, rod in enumerate(rods):
rod_indices.setdefault(rod, []).append(index+1)
for offset in range(2):
target_indices = rod_indices.copy()
assignment = [-1 for i in range(n)]
for i in range(offset, n, 2):
target = segments[(i-1)%n] + segments[i]
if target not in target_indices or target_indices[target] == []:
assignment = None
break
assignment[i] = target_indices[target].pop()
if assignment != None:
print('YES')
print(' '.join(map(str, assignment)))
sys.exit()
print('NO')
| 51
| 186
| 2,457,600
|
228020885
|
n, m = map(int,input().split())
dist = [0 for i in range(n)]
nails = []
for i in range(n):
nails.append(tuple(map(int,input().split())))
rods = list(map(int,input().split()))
rods_d_1 = {}
rods_d_2 = {}
for i in range(m):
if rods[i] in rods_d_1:
rods_d_1[rods[i]].append(i)
rods_d_2[rods[i]].append(i)
else:
rods_d_1[rods[i]] = [i]
rods_d_2[rods[i]] = [i]
for i in range(n):
if i == n-1:
dist[i] = abs(nails[i][0] - nails[0][0]) + abs(nails[i][1] - nails[0][1])
else:
dist[i] = abs(nails[i][0] - nails[i+1][0]) + abs(nails[i][1] - nails[i+1][1])
if sum(dist) > sum(rods):
print('NO')
else:
e_nails = []
o_nails = [dist[0]+dist[-1]]
for i in range(n-1):
if i%2 == 0:
e_nails.append(dist[i] + dist[i+1])
else:
o_nails.append(dist[i] + dist[i+1])
e_ind = []
o_ind = []
for seg in e_nails:
if seg not in rods_d_1:
break
else:
e_ind.append(rods_d_1[seg][0])
rods_d_1[seg].remove(rods_d_1[seg][0])
if len(rods_d_1[seg]) == 0:
del rods_d_1[seg]
for seg in o_nails:
if seg not in rods_d_2:
break
else:
o_ind.append(rods_d_2[seg][0])
rods_d_2[seg].remove(rods_d_2[seg][0])
if len(rods_d_2[seg]) == 0:
del rods_d_2[seg]
if len(e_ind) == n//2:
print('YES')
print(*[-1 if i%2 != 1 else e_ind[(i-1)//2]+1 for i in range(n)])
elif len(o_ind) == n//2:
print('YES')
print(*[-1 if i%2 == 1 else o_ind[i//2]+1 for i in range(n)])
else: print('NO')
|
Codeforces Beta Round 28 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Bender Problem
|
Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods.
Help Bender to solve this difficult task.
|
The first line contains two positive integers n and m (4 ≤ n ≤ 500, 2 ≤ m ≤ 500, n is even) — the amount of nails and the amount of rods. i-th of the following n lines contains a pair of integers, denoting the coordinates of the i-th nail. Nails should be connected in the same order as they are given in the input. The last line contains m integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200 000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line.
|
If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output n numbers — i-th of them should be the number of rod, which fold place is attached to the i-th nail, or -1, if there is no such rod.
If there are multiple solutions, print any of them.
| null | null |
[{"input": "4 2\n0 0\n0 2\n2 2\n2 0\n4 4", "output": "YES\n1 -1 2 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3", "output": "YES\n1 -1 2 -1 3 -1"}, {"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3", "output": "NO"}]
| 1,600
|
["implementation"]
| 51
|
[{"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n4 4\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n3 2 3\r\n", "output": "YES\r\n1 -1 2 -1 3 -1 \r\n"}, {"input": "6 3\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2 3\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n0 0\r\n0 1\r\n1 1\r\n1 0\r\n1 1 1 1\r\n", "output": "NO\r\n"}, {"input": "6 2\r\n0 0\r\n1 0\r\n1 1\r\n2 1\r\n2 2\r\n0 2\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "6 3\r\n0 0\r\n2 0\r\n2 2\r\n1 2\r\n1 1\r\n0 1\r\n4 2 2\r\n", "output": "YES\r\n-1 1 -1 2 -1 3 "}, {"input": "4 4\r\n-8423 7689\r\n6902 7689\r\n6902 2402\r\n-8423 2402\r\n20612 20612 91529 35617\r\n", "output": "YES\r\n1 -1 2 -1 \r\n"}, {"input": "4 4\r\n1679 -198\r\n9204 -198\r\n9204 -5824\r\n1679 -5824\r\n18297 92466 187436 175992\r\n", "output": "NO\r\n"}, {"input": "4 2\r\n0 0\r\n0 2\r\n2 2\r\n2 0\r\n200000 200000\r\n", "output": "NO\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
with open(input_path) as f:
n, m = map(int, f.readline().split())
nails = [tuple(map(int, f.readline().split())) for _ in range(n)]
rods = list(map(int, f.readline().split()))
with open(output_path) as f:
ref_lines = f.read().splitlines()
with open(submission_path) as f:
sub_lines = f.read().splitlines()
if not sub_lines:
print(0)
return
ref_is_no = ref_lines and ref_lines[0].strip() == "NO"
sub_is_no = sub_lines and sub_lines[0].strip() == "NO"
if ref_is_no:
print(1 if sub_is_no else 0)
return
else:
if not sub_lines or sub_lines[0].strip() != "YES":
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
sub_list = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(sub_list) != n:
print(0)
return
used_rods = set()
for num in sub_list:
if num == -1:
continue
if num < 1 or num > m or num in used_rods:
print(0)
return
used_rods.add(num)
for i in range(n):
rod_num = sub_list[i]
if rod_num == -1:
continue
prev_i = i - 1 if i > 0 else n - 1
next_i = (i + 1) % n
x_prev, y_prev = nails[prev_i]
x_curr, y_curr = nails[i]
x_next, y_next = nails[next_i]
len_prev = abs(y_curr - y_prev) if x_prev == x_curr else abs(x_curr - x_prev)
len_next = abs(y_next - y_curr) if x_curr == x_next else abs(x_next - x_curr)
sum_len = len_prev + len_next
rod_index = rod_num - 1
if rod_index >= len(rods) or rods[rod_index] != sum_len:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
459/B
|
459
|
B
|
PyPy 3-64
|
TESTS
| 5
| 92
| 23,552,000
|
184219990
|
n=int(input())
m=list(map(int,input().split()))
x=max(m)
y=min(m)
a=b=0
if(x==y):
print(0,1,sep=' ')
else:
for i in range(n):
if (m[i] == x):
a += 1
if (m[i] == y):
b += 1
print(x - y, a * b, sep=' ')
| 58
| 109
| 17,510,400
|
178082825
|
n=int(input())
a=list(map(int,input().split()))
i,j=min(a),max(a)
print(j-i,[(n*(n-1))//2,a.count(i)*a.count(j)][i!=j])
|
Codeforces Round 261 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Pashmak and Flowers
|
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!
Your task is to write a program which calculates two things:
1. The maximum beauty difference of flowers that Pashmak can give to Parmida.
2. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
|
The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109).
|
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
| null |
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:
1. choosing the first and the second flowers;
2. choosing the first and the fifth flowers;
3. choosing the fourth and the second flowers;
4. choosing the fourth and the fifth flowers.
|
[{"input": "2\n1 2", "output": "1 1"}, {"input": "3\n1 4 5", "output": "4 1"}, {"input": "5\n3 1 2 3 1", "output": "2 4"}]
| 1,300
|
["combinatorics", "implementation", "sortings"]
| 58
|
[{"input": "2\r\n1 2\r\n", "output": "1 1"}, {"input": "3\r\n1 4 5\r\n", "output": "4 1"}, {"input": "5\r\n3 1 2 3 1\r\n", "output": "2 4"}, {"input": "2\r\n1 1\r\n", "output": "0 1"}, {"input": "3\r\n1 1 1\r\n", "output": "0 3"}, {"input": "4\r\n1 1 1 1\r\n", "output": "0 6"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "0 10"}, {"input": "5\r\n2 2 2 2 2\r\n", "output": "0 10"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0 45"}, {"input": "3\r\n2 2 2\r\n", "output": "0 3"}, {"input": "3\r\n3 3 3\r\n", "output": "0 3"}, {"input": "2\r\n10000000 100000000\r\n", "output": "90000000 1"}, {"input": "5\r\n5 5 5 5 5\r\n", "output": "0 10"}, {"input": "5\r\n3 3 3 3 3\r\n", "output": "0 10"}, {"input": "6\r\n1 1 1 1 1 1\r\n", "output": "0 15"}, {"input": "2\r\n5 6\r\n", "output": "1 1"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "0 45"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 45"}, {"input": "4\r\n4 4 4 4\r\n", "output": "0 6"}, {"input": "7\r\n1 1 1 1 1 1 1\r\n", "output": "0 21"}, {"input": "11\r\n1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0 55"}, {"input": "8\r\n8 8 8 8 8 8 8 8\r\n", "output": "0 28"}, {"input": "2\r\n3 2\r\n", "output": "1 1"}]
| false
|
stdio
| null | true
|
898/C
|
898
|
C
|
PyPy 3
|
TESTS
| 2
| 155
| 307,200
|
94495367
|
from collections import defaultdict
n = int(input())
book = defaultdict(lambda : [])
for i in range(n):
s = input().split()
k = s[1]
for j in s[2:]:
book[s[0]].append(j)
for entry in book:
book[entry] = list(set(book[entry]))
for i in book[entry]:
for j in book[entry]:
if i == j:
continue
if i.endswith(j) and j in book[entry]:
book[entry].remove(j)
elif j.endswith(i) and i in book[entry]:
book[entry].remove(i)
print(len(book))
for entry in book:
print(entry, len(book[entry]), ' '.join(book[entry]))
| 59
| 46
| 0
|
160934983
|
n = int(input())
records = dict()
for _ in range(n):
record = list(input().split())
name = record[0]
size = int(record[1])
phone = record[2:]
if name not in records:
records[name] = set()
for num in phone:
records[name].add(num)
print(len(records))
for name, phones in records.items():
ans = []
for num1 in phones:
ok = True
for num2 in phones:
if len(num1) >= len(num2):
continue
if num2[-len(num1):] == num1:
ok = False
if ok:
ans.append(num1)
print(name, len(ans), *ans)
|
Codeforces Round 451 (Div. 2)
|
CF
| 2,017
| 2
| 256
|
Phone Numbers
|
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
|
First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
|
Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
| null | null |
[{"input": "2\nivan 1 00123\nmasha 1 00123", "output": "2\nmasha 1 00123\nivan 1 00123"}, {"input": "3\nkarl 2 612 12\npetr 1 12\nkatya 1 612", "output": "3\nkatya 1 612\npetr 1 12\nkarl 1 612"}, {"input": "4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789", "output": "2\ndasha 2 23 789\nivan 4 789 123 2 456"}]
| 1,400
|
["implementation", "strings"]
| 59
|
[{"input": "2\r\nivan 1 00123\r\nmasha 1 00123\r\n", "output": "2\r\nmasha 1 00123 \r\nivan 1 00123 \r\n"}, {"input": "3\r\nkarl 2 612 12\r\npetr 1 12\r\nkatya 1 612\r\n", "output": "3\r\nkatya 1 612 \r\npetr 1 12 \r\nkarl 1 612 \r\n"}, {"input": "4\r\nivan 3 123 123 456\r\nivan 2 456 456\r\nivan 8 789 3 23 6 56 9 89 2\r\ndasha 2 23 789\r\n", "output": "2\r\ndasha 2 789 23 \r\nivan 4 789 123 456 2 \r\n"}, {"input": "20\r\nnxj 6 7 6 6 7 7 7\r\nnxj 10 8 5 1 7 6 1 0 7 0 6\r\nnxj 2 6 5\r\nnxj 10 6 7 6 6 5 8 3 6 6 8\r\nnxj 10 6 1 7 6 7 1 8 7 8 6\r\nnxj 10 8 5 8 6 5 6 1 9 6 3\r\nnxj 10 8 1 6 4 8 0 4 6 0 1\r\nnxj 9 2 6 6 8 1 1 3 6 6\r\nnxj 10 8 9 0 9 1 3 2 3 2 3\r\nnxj 6 6 7 0 8 1 2\r\nnxj 7 7 7 8 1 3 6 9\r\nnxj 10 2 7 0 1 5 1 9 1 2 6\r\nnxj 6 9 6 9 6 3 7\r\nnxj 9 0 1 7 8 2 6 6 5 6\r\nnxj 4 0 2 3 7\r\nnxj 10 0 4 0 6 1 1 8 8 4 7\r\nnxj 8 4 6 2 6 6 1 2 7\r\nnxj 10 5 3 4 2 1 0 7 0 7 6\r\nnxj 10 9 6 0 6 1 6 2 1 9 6\r\nnxj 4 2 9 0 1\r\n", "output": "1\r\nnxj 10 0 3 2 1 4 7 8 5 9 6 \r\n"}, {"input": "20\r\nl 6 02 02 2 02 02 2\r\nl 8 8 8 8 2 62 13 31 3\r\ne 9 0 91 0 0 60 91 60 2 44\r\ne 9 69 2 1 44 2 91 66 1 70\r\nl 9 7 27 27 3 1 3 7 80 81\r\nl 9 2 1 13 7 2 10 02 3 92\r\ne 9 0 15 3 5 5 15 91 09 44\r\nl 7 2 50 4 5 98 31 98\r\nl 3 26 7 3\r\ne 6 7 5 0 62 65 91\r\nl 8 80 0 4 0 2 2 0 13\r\nl 9 19 13 02 2 1 4 19 26 02\r\nl 10 7 39 7 9 22 22 26 2 90 4\r\ne 7 65 2 36 0 34 57 9\r\ne 8 13 02 09 91 73 5 36 62\r\nl 9 75 0 10 8 76 7 82 8 34\r\nl 7 34 0 19 80 6 4 7\r\ne 5 4 2 5 7 2\r\ne 7 4 02 69 7 07 20 2\r\nl 4 8 2 1 63\r\n", "output": "2\r\ne 18 15 62 07 70 91 57 02 66 65 69 09 13 20 44 73 34 60 36 \r\nl 21 27 50 22 63 75 19 26 90 02 92 62 31 10 76 82 80 98 81 39 34 13 \r\n"}, {"input": "20\r\no 10 6 6 97 45 6 6 6 6 5 6\r\nl 8 5 5 5 19 59 5 8 5\r\nj 9 2 30 58 2 2 1 0 30 4\r\nc 10 1 1 7 51 7 7 51 1 1 1\r\no 9 7 97 87 70 2 19 2 14 6\r\ne 6 26 6 6 6 26 5\r\ng 9 3 3 3 3 3 78 69 8 9\r\nl 8 8 01 1 5 8 41 72 3\r\nz 10 1 2 2 2 9 1 9 1 6 7\r\ng 8 7 78 05 36 7 3 67 9\r\no 5 6 9 9 7 7\r\ne 10 30 2 1 1 2 5 04 0 6 6\r\ne 9 30 30 2 2 0 26 30 79 8\r\nt 10 2 2 9 29 7 7 7 9 2 9\r\nc 7 7 51 1 31 2 7 4\r\nc 9 83 1 6 78 94 74 54 8 32\r\ng 8 4 1 01 9 39 28 6 6\r\nt 7 9 2 01 4 4 9 58\r\nj 5 0 1 58 02 4\r\nw 10 80 0 91 91 06 91 9 9 27 7\r\n", "output": "9\r\nw 5 91 27 06 9 80 \r\nt 6 7 58 4 29 2 01 \r\ne 8 8 79 5 30 2 26 04 1 \r\nl 8 72 3 19 59 41 01 5 8 \r\nj 5 4 30 58 1 02 \r\nz 5 6 1 9 2 7 \r\ng 10 05 39 4 3 36 01 67 69 28 78 \r\no 8 14 87 97 6 19 70 45 2 \r\nc 10 83 51 31 54 74 32 7 94 78 6 \r\n"}, {"input": "1\r\negew 5 3 123 23 1234 134\r\n", "output": "1\r\negew 3 123 134 1234 \r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input and build friends data
friends = defaultdict(set)
with open(input_path, 'r') as f:
n = int(f.readline())
for _ in range(n):
parts = f.readline().strip().split()
name = parts[0]
numbers = parts[2:]
friends[name].update(numbers) # Add all numbers, duplicates in same line are handled via set
# Read submission output
submission_friends = set()
try:
with open(submission_path, 'r') as f:
lines = f.read().splitlines()
except:
print(0)
return
if not lines:
print(0)
return
# Check m line
try:
m_line = lines[0]
m = int(m_line)
except:
print(0)
return
if m != len(friends):
print(0)
return
submission_lines = lines[1:]
if len(submission_lines) != m:
print(0)
return
for line in submission_lines:
parts = line.strip().split()
if len(parts) < 2:
print(0)
return
name = parts[0]
if name not in friends:
print(0)
return
submission_friends.add(name)
try:
k = int(parts[1])
except:
print(0)
return
numbers = parts[2:]
if len(numbers) != k:
print(0)
return
# Check for duplicates in submission's numbers
if len(set(numbers)) != k:
print(0)
return
# Check all numbers are present in friends' merged set
merged_numbers = friends[name]
for num in numbers:
if num not in merged_numbers:
print(0)
return
# Check no two numbers in submission are suffix of each other
for i in range(len(numbers)):
for j in range(i+1, len(numbers)):
a = numbers[i]
b = numbers[j]
if (len(a) >= len(b) and a.endswith(b)) or (len(b) >= len(a) and b.endswith(a)):
print(0)
return
# Check every merged number is either in submission or is a suffix of some submission number
for merged_num in merged_numbers:
if merged_num in numbers:
continue
found = False
for num in numbers:
if len(merged_num) <= len(num) and num.endswith(merged_num):
found = True
break
if not found:
print(0)
return
# Check all friends are present in submission
if submission_friends != set(friends.keys()):
print(0)
return
# All checks passed
print(100)
if __name__ == "__main__":
main()
| true
|
175/B
|
175
|
B
|
Python 3
|
TESTS
| 8
| 62
| 0
|
214349665
|
n = int(input())
name = []
score = []
for _ in range(n):
a,b = input().split()
name.append(a)
score.append(int(b))
print(len(set(name)))
for i in range(n):
for j in range(n):
if i != j and name[i] == name[j]:
x = min(score[i],score[j])
name[score.index(x)] = 0
score.remove(x)
for i in name:
if i == 0:
name.remove(i)
for i in range(len(score)):
ans = 0
for j in range(len(score)):
if score[j] > score[i]:
ans += 1
if ans == 0:
print(name[i],"pro")
else:
if ans/len(name) > 0.5:
print(name[i],"noob")
elif 0.5 >= ans/len(name) > 0.2:
print(name[i],"random")
elif 0.2 >= ans/len(name) > 0.1:
print(name[i],"average")
else:
print(name[i],"hardcore")
| 46
| 124
| 409,600
|
14703186
|
class Player:
def __init__(self, name, score):
self.name = name
self.score = score
class Tier:
def __init__(self, label, percentile):
self.label = label
self.percentile = percentile
tier_data = [ ('pro', 99), ('hardcore', 90),
('average', 80), ('random', 50) ]
tiers = [ Tier(*t) for t in tier_data ]
num_records = int(input())
name_to_score = {}
for i in range(num_records):
tokens = input().split()
name, score = tokens[0], int(tokens[1])
name_to_score[name] = max(name_to_score.setdefault(name, 0), score)
num_players = len(name_to_score)
players = []
for name, score in name_to_score.items():
players.append(Player(name, score))
players.sort(key = lambda player: player.score)
print(num_players)
pos = num_players - 1
while pos >= 0:
player = players[pos]
rank = 'noob'
score = player.score
for tier in tiers:
if 100 * (pos + 1) // num_players >= tier.percentile:
rank = tier.label
break
print(player.name, rank)
pos -= 1
while pos >= 0 and players[pos].score == score:
print(players[pos].name, rank)
pos -= 1
|
Codeforces Round 115
|
CF
| 2,012
| 2
| 256
|
Plane of Tanks: Pro
|
Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results.
A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He has n records in total.
In order to determine a player's category consider the best result obtained by the player and the best results of other players. The player belongs to category:
- "noob" — if more than 50% of players have better results;
- "random" — if his result is not worse than the result that 50% of players have, but more than 20% of players have better results;
- "average" — if his result is not worse than the result that 80% of players have, but more than 10% of players have better results;
- "hardcore" — if his result is not worse than the result that 90% of players have, but more than 1% of players have better results;
- "pro" — if his result is not worse than the result that 99% of players have.
When the percentage is calculated the player himself is taken into account. That means that if two players played the game and the first one gained 100 points and the second one 1000 points, then the first player's result is not worse than the result that 50% of players have, and the second one is not worse than the result that 100% of players have.
Vasya gave you the last year Plane of Tanks results. Help Vasya determine each player's category.
|
The first line contains the only integer number n (1 ≤ n ≤ 1000) — a number of records with the players' results.
Each of the next n lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 characters. The name consists of lowercase Latin letters only. It is guaranteed that any two different players have different names. The amount of points, obtained by the player for the round, is a non-negative integer number and does not exceed 1000.
|
Print on the first line the number m — the number of players, who participated in one round at least.
Each one of the next m lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: "noob", "random", "average", "hardcore" or "pro" (without quotes). The name of each player should be printed only once. Player names with respective categories can be printed in an arbitrary order.
| null |
In the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category "noob". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category "random". igor has best result not worse than the result that 100% of players have (all other players and himself), so he belongs to category "pro".
In the second example both players have the same amount of points, so they have results not worse than 100% players have, so they belong to category "pro".
|
[{"input": "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250", "output": "4\nartem noob\nigor pro\nkolya random\nvasya random"}, {"input": "3\nvasya 200\nkolya 1000\nvasya 1000", "output": "2\nkolya pro\nvasya pro"}]
| 1,400
|
["implementation"]
| 78
|
[{"input": "5\r\nvasya 100\r\nvasya 200\r\nartem 100\r\nkolya 200\r\nigor 250\r\n", "output": "4\r\nartem noob\r\nigor pro\r\nkolya random\r\nvasya random\r\n"}, {"input": "3\r\nvasya 200\r\nkolya 1000\r\nvasya 1000\r\n", "output": "2\r\nkolya pro\r\nvasya pro\r\n"}, {"input": "1\r\nvasya 1000\r\n", "output": "1\r\nvasya pro\r\n"}, {"input": "5\r\nvasya 1000\r\nvasya 100\r\nkolya 200\r\npetya 300\r\noleg 400\r\n", "output": "4\r\nkolya noob\r\noleg random\r\npetya random\r\nvasya pro\r\n"}, {"input": "10\r\na 1\r\nb 2\r\nc 3\r\nd 4\r\ne 5\r\nf 6\r\ng 7\r\nh 8\r\ni 9\r\nj 10\r\n", "output": "10\r\na noob\r\nb noob\r\nc noob\r\nd noob\r\ne random\r\nf random\r\ng random\r\nh average\r\ni hardcore\r\nj pro\r\n"}, {"input": "10\r\nj 10\r\ni 9\r\nh 8\r\ng 7\r\nf 6\r\ne 5\r\nd 4\r\nc 3\r\nb 2\r\na 1\r\n", "output": "10\r\na noob\r\nb noob\r\nc noob\r\nd noob\r\ne random\r\nf random\r\ng random\r\nh average\r\ni hardcore\r\nj pro\r\n"}, {"input": "1\r\ntest 0\r\n", "output": "1\r\ntest pro\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input data
with open(input_path) as f:
n = int(f.readline())
players = {}
for _ in range(n):
name, points = f.readline().strip().split()
points = int(points)
if name not in players or points > players[name]:
players[name] = points
m = len(players)
best_scores = list(players.values())
# Compute correct categories
correct = {}
for name in players:
score = players[name]
cnt = sum(1 for s in best_scores if s > score)
percentage = (cnt * 100.0) / m
if percentage > 50:
cat = 'noob'
elif percentage > 20:
cat = 'random'
elif percentage > 10:
cat = 'average'
elif percentage > 1:
cat = 'hardcore'
else:
cat = 'pro'
correct[name] = cat
# Read submission output
with open(submission_path) as f:
lines = f.read().splitlines()
if not lines:
print(0)
return
try:
m_sub = int(lines[0])
except:
print(0)
return
if m_sub != m:
print(0)
return
submission = {}
for line in lines[1:]:
parts = line.strip().split()
if len(parts) != 2:
print(0)
return
name, cat = parts
if name in submission:
print(0)
return
submission[name] = cat
# Check all players are present and correct
if submission.keys() != correct.keys():
print(0)
return
for name, cat in submission.items():
if correct.get(name) != cat:
print(0)
return
print(1)
if __name__ == "__main__":
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
27/C
|
27
|
C
|
Python 3
|
TESTS
| 25
| 778
| 7,680,000
|
8027210
|
n = int(input())
a = list(map(int, input().split()))
b = list(sorted(a))
c = list(sorted(a, reverse=True))
if a == b or a == c:
print(0)
exit()
for i in range(n - 2):
x = [a[i], a[i + 1], a[i + 2]]
if not (x in (list(sorted(x)), list(sorted(x, reverse=True)))):
print(3)
print(i + 1, i + 2, i + 3)
exit()
print(0)
| 37
| 186
| 13,619,200
|
214206414
|
import sys
input = sys.stdin.readline
n = int(input())
ls = list(map(int,input().split()))
l1 = ls.copy()
l1.sort()
for i in range(n) :
if ls[i] != l1[i] :
break
else :
print(0)
exit()
l1.reverse()
for i in range(n) :
if ls[i] != l1[i] :
break
else :
print(0)
exit()
j = 1
while j+1 < n and ((ls[j] >= ls[0] and ls[j+1] >= ls[j]) or (ls[j] <= ls[0] and ls[j+1] <= ls[j])) :
j += 1
print(3)
print(1,j+1,j+2)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
PyPy 3-64
|
TESTS
| 18
| 156
| 13,516,800
|
195448945
|
import sys; R = sys.stdin.readline
S = lambda: map(int,R().split())
n = int(R())
if n<3: print(0); exit()
a = [*S()]
s,d = [0],0
for i in range(1,n):
g = a[i]-a[s[-1]]
if g>0:
if d==1: s[-1] = i
elif not d: d = 1; s += i,
else: print(3); print(s[0]+1,s[1]+1,i+1); exit()
elif g<0:
if d==-1: s[-1] = i-1
elif not d: d = -1; s += i,
else: print(3); print(s[0]+1,s[1]+1,i+1); exit()
print(0)
| 37
| 186
| 13,926,400
|
231387860
|
n=int(input())
a=list(map(int,input().split()))
for i in range(2,n):
if (a[i]-a[i-1])*(a[i-1]-a[0])<0:
print(3,1,i,i+1)
exit()
print(0)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
Python 3
|
TESTS
| 17
| 156
| 13,926,400
|
225872930
|
# LUOGU_RID: 126705765
input();a=list(map(int,input().split()));p=0;f=1
try:
while 1:
if a[p]>a[p+1]:
q=p+1
while 1:
if a[q]<a[q+1]:print(3);print(q,q+1,q+2);f=0;exit()
else:q+=1
elif a[p]<a[p+1]:
q=p+1
while 1:
if a[q]>a[q+1]:print(3);print(q,q+1,q+2);f=0;exit()
else:q+=1
else:p+=1
except:
if f:print(0)
| 37
| 186
| 14,131,200
|
206998086
|
# LUOGU_RID: 111117503
from sys import stdin
input = stdin.readline
n = int(input())
lst = [0] + list(map(int,input().split()))
for i in range(2, n) :
if (lst[i + 1] > lst[i] and lst[i] < lst[1]) or (lst[i + 1] < lst[i] and lst[i] > lst[1]) :
print(3)
print(1, i, i + 1)
exit()
print(0)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
PyPy 3-64
|
TESTS
| 17
| 186
| 13,516,800
|
214196488
|
import sys
input = sys.stdin.readline
n = int(input())
ls = list(map(int,input().split()))
l1 = ls.copy()
l1.sort()
for i in range(n) :
if ls[i] != l1[i] :
break
else :
print(0)
exit()
l1.reverse()
for i in range(n) :
if ls[i] != l1[i] :
break
else :
print(0)
exit()
j = 0
while j+1 < n and ls[j] == ls[j+1] :
j += 1
k = j+1
while k+1 < n and (ls[j+1] - ls[j])*(ls[k+1] - ls[k]) >= 0 :
k += 1
print(3)
print(j+1,j+2,k+2)
| 37
| 248
| 7,372,800
|
155275806
|
n = int(input())
array = list(map(int, input().split()))
maxx = array[0]
minn = array[0]
positionMin = 0
positionMax = 0
for i in range(n):
if positionMax < positionMin:
if (array[i] > minn) & (array[i] > minn):
print(3)
print(positionMax + 1, positionMin + 1, i + 1)
break
if positionMax > positionMin:
if (array[i] < maxx) & (array[i] < maxx):
print(3)
print(positionMin + 1, positionMax + 1, i + 1)
break
if array[i] < minn:
minn = array[i]
positionMin = i
if array[i] > maxx:
maxx = array[i]
positionMax = i
if i == n - 1:
print(0)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
PyPy 3-64
|
TESTS
| 17
| 186
| 13,619,200
|
214200066
|
import sys
input = sys.stdin.readline
n = int(input())
ls = list(map(int,input().split()))
l1 = ls.copy()
l1.sort()
for i in range(n) :
if ls[i] != l1[i] :
break
else :
print(0)
exit()
l1.reverse()
for i in range(n) :
if ls[i] != l1[i] :
break
else :
print(0)
exit()
print(3)
j = 0
while j+1 < n and ls[j] == ls[j+1] :
j += 1
k = j + 1
if ls[j+1] > ls[j] :
while k < n and ls[k] >= ls[j+1] :
k += 1
else :
while k < n and ls[k] <= ls[j+1] :
k += 1
print(j+1,j+2,k+1)
| 37
| 248
| 11,059,200
|
213489237
|
from sys import stdin
N = int(stdin.readline())
data = [int(w) for w in stdin.readline().split()]
# low , high, low
dp = [N - 1] * N
for i in range(N - 1, 0, -1):
if data[i - 1] < data[dp[i]]:
dp[i - 1] = i
else:
dp[i - 1] = dp[i]
low = 0
for i in range(1, N - 1):
if data[i] > data[low] and data[i] > data[dp[i + 1]]:
print(3)
print(low + 1, i + 1, dp[i + 1] + 1)
exit(0)
if data[i] < data[low]:
low = i
# high, low, high
dp = [N - 1] * N
for i in range(N - 1, 0, -1):
if data[i - 1] > data[dp[i]]:
dp[i - 1] = i
else:
dp[i - 1] = dp[i]
high = 0
for i in range(1, N - 1):
if data[i] < data[high] and data[i] < data[dp[i + 1]]:
print(3)
print(high + 1, i + 1, dp[i + 1] + 1)
exit(0)
if data[i] > data[high]:
high = i
print(0)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
Python 3
|
TESTS
| 17
| 280
| 6,348,800
|
45907733
|
def main():
count=int(input())
arr=input().split(" ")
if count<=2:
print(0)
else:
base=int(arr[0])
bo=True
for x in range(1,count-1):
test=int(arr[x])
if test<base:
smallest=test
small=True
bo=False
break
elif test>base:
biggest=test
small=False
bo=False
break
if bo:
print(0)
else:
if small:
found=False
for y in range(x+1,count):
test=int(arr[y])
if test>smallest:
found=True
break
elif test<smallest:
smallest=test
if found:
print(3)
string="1 "+str(x+1)+" "+str(y+1)
print(string)
else:
print(0)
else:
found=False
for y in range(x+1,count):
test=int(arr[y])
if test<biggest:
found=True
break
elif test>biggest:
biggest=test
if found:
print(3)
string="1 "+str(x+1)+" "+str(y+1)
print(string)
else:
print(0)
main()
| 37
| 310
| 14,131,200
|
123102728
|
s = input().split()
n = int(s[0])
s1 = input().split()
a = []
for i in range(1, n + 1) :
a.append(int(s1[i - 1]))
flag = 1
for i in range(2, n) :
if (((a[i] > a[i - 1]) and (a[i - 1] < a[0])) or ((a[i] < a[i - 1]) and (a[i -1] > a[0]))) :
print(3)
print(1, i, i + 1)
flag = 0
break
if (flag == 1) :
print(0)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
27/C
|
27
|
C
|
Python 3
|
TESTS
| 17
| 342
| 14,540,800
|
80265323
|
n = int(input().strip())
array = list(map(int, input().strip().split()))
def checker(array):
if len(array) <= 2:
print(0)
return
ind = 1
while ind < len(array) and array[ind] == array[0]:
ind += 1
if ind + 1 == len(array):
print(0)
return
ind_two = ind + 1
while ind_two < len(array):
if (array[0] > array[ind] and array[ind] < array[ind_two]) or \
(array[0] < array[ind] and array[ind] > array[ind_two]):
print(3)
print(1, ind + 1, ind_two + 1)
return
ind_two += 1
print(0)
return
checker(array)
| 37
| 310
| 18,329,600
|
151482720
|
t = 1
while t:
t -= 1
q = int(input())
b = list(map(int, input().split()))
prev = -1
flag = 0
sa = []
k = 0
for i in b:
if i != prev:
sa.append([i, k])
prev = i
k += 1
for i in range(1, len(sa) - 1):
if sa[i - 1][0] < sa[i][0] and sa[i + 1][0] < sa[i][0]:
print('3')
print(sa[i - 1][1] + 1, sa[i][1] + 1, sa[i + 1][1] + 1)
flag = 1
break
if sa[i - 1][0] > sa[i][0] and sa[i + 1][0] > sa[i][0]:
print('3')
print(sa[i - 1][1] + 1, sa[i][1] + 1, sa[i + 1][1] + 1)
flag = 1
break
if not flag:
print('0')
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
459/B
|
459
|
B
|
PyPy 3-64
|
TESTS
| 5
| 93
| 22,118,400
|
172364500
|
n = int(input())
flowers = list(map(int, input().split(' ')))
flowers_min = min(flowers)
flowers_max = max(flowers)
summary = flowers_max - flowers_min
if summary:
print(flowers_max - flowers_min, flowers.count(flowers_min) * flowers.count(flowers_max))
else:
print(0, 1)
| 58
| 109
| 19,968,000
|
170468604
|
n=int(input())
L=input().split()
maxm=0
c1=0
minm=float("inf")
c2=0
for i in range(n):
a=int(L[i])
if a<minm:
minm=a
c2=1
elif a==minm:
c2+=1
if a>maxm:
maxm=a
c1=1
elif a==maxm:
c1+=1
if minm==maxm:
print(0,c1*(c1-1)//2)
else:
print(maxm-minm,c1*c2)
|
Codeforces Round 261 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Pashmak and Flowers
|
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!
Your task is to write a program which calculates two things:
1. The maximum beauty difference of flowers that Pashmak can give to Parmida.
2. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
|
The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109).
|
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
| null |
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:
1. choosing the first and the second flowers;
2. choosing the first and the fifth flowers;
3. choosing the fourth and the second flowers;
4. choosing the fourth and the fifth flowers.
|
[{"input": "2\n1 2", "output": "1 1"}, {"input": "3\n1 4 5", "output": "4 1"}, {"input": "5\n3 1 2 3 1", "output": "2 4"}]
| 1,300
|
["combinatorics", "implementation", "sortings"]
| 58
|
[{"input": "2\r\n1 2\r\n", "output": "1 1"}, {"input": "3\r\n1 4 5\r\n", "output": "4 1"}, {"input": "5\r\n3 1 2 3 1\r\n", "output": "2 4"}, {"input": "2\r\n1 1\r\n", "output": "0 1"}, {"input": "3\r\n1 1 1\r\n", "output": "0 3"}, {"input": "4\r\n1 1 1 1\r\n", "output": "0 6"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "0 10"}, {"input": "5\r\n2 2 2 2 2\r\n", "output": "0 10"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0 45"}, {"input": "3\r\n2 2 2\r\n", "output": "0 3"}, {"input": "3\r\n3 3 3\r\n", "output": "0 3"}, {"input": "2\r\n10000000 100000000\r\n", "output": "90000000 1"}, {"input": "5\r\n5 5 5 5 5\r\n", "output": "0 10"}, {"input": "5\r\n3 3 3 3 3\r\n", "output": "0 10"}, {"input": "6\r\n1 1 1 1 1 1\r\n", "output": "0 15"}, {"input": "2\r\n5 6\r\n", "output": "1 1"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1 1\r\n", "output": "0 45"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0 45"}, {"input": "4\r\n4 4 4 4\r\n", "output": "0 6"}, {"input": "7\r\n1 1 1 1 1 1 1\r\n", "output": "0 21"}, {"input": "11\r\n1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0 55"}, {"input": "8\r\n8 8 8 8 8 8 8 8\r\n", "output": "0 28"}, {"input": "2\r\n3 2\r\n", "output": "1 1"}]
| false
|
stdio
| null | true
|
27/C
|
27
|
C
|
Python 3
|
TESTS
| 17
| 434
| 6,144,000
|
39647677
|
n=int(input())
x=input().split()
y=0
while y<n:
x[y]=int(x[y])
y+=1
y=0
ans=[1]
dec=0
inc=0
while y<len(x)-1:
if x[y]>x[y+1]:
ans.append(y+2)
dec=1
break
elif x[y]<x[y+1]:
ans.append(y+2)
inc=1
break
y=y+1
if dec==1:
while y<len(x)-1:
if x[y]<x[y+1]:
ans.append(y+2)
break
y+=1
elif inc==1:
while y<len(x)-1:
if x[y]>x[y+1]:
ans.append(y+2)
break
y+=1
if len(ans)==3:
print('3')
print(ans[0],ans[1],ans[2])
else:
print('0')
| 37
| 342
| 11,161,600
|
145984089
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
mil, mal = list(a), list(a)
for i in range(1, n):
mil[i] = min(mil[i], mil[i - 1])
for i in range(1, n):
mal[i] = max(mal[i], mal[i - 1])
mir, mar = list(a), list(a)
for i in range(n - 2, -1, -1):
mir[i] = min(mir[i], mir[i + 1])
for i in range(n - 2, -1, -1):
mar[i] = max(mar[i], mar[i + 1])
ans = []
for i in range(1, n - 1):
ai = a[i]
if mil[i - 1] < ai > mir[i + 1]:
for j in range(i):
if a[j] < ai:
x = j + 1
break
for j in range(n - 1, i, -1):
if a[j] < ai:
y = j + 1
break
ans = [x, i + 1, y]
break
if mal[i - 1] > ai < mar[i + 1]:
for j in range(i):
if a[j] > ai:
x = j + 1
break
for j in range(n - 1, i, -1):
if a[j] > ai:
y = j + 1
break
ans = [x, i + 1, y]
break
k = len(ans)
print(k)
if not k:
exit()
print(*ans)
|
Codeforces Beta Round 27 (Codeforces format, Div. 2)
|
CF
| 2,010
| 2
| 256
|
Unordered Subsequence
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
| null | null |
[{"input": "5\n67 499 600 42 23", "output": "3\n1 3 5"}, {"input": "3\n1 2 3", "output": "0"}, {"input": "3\n2 3 1", "output": "3\n1 2 3"}]
| 1,900
|
["constructive algorithms", "greedy"]
| 37
|
[{"input": "3\r\n3 1 2\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "1\r\n-895376\r\n", "output": "0\r\n"}, {"input": "2\r\n166442 61629\r\n", "output": "0\r\n"}, {"input": "3\r\n-771740 -255752 -300809\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "4\r\n-227347 -573134 -671045 11011\r\n", "output": "3\r\n2 3 4\r\n"}, {"input": "5\r\n834472 -373089 441294 -633071 -957672\r\n", "output": "3\r\n1 2 3\r\n"}, {"input": "2\r\n7 8\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path, output_path, submission_path = sys.argv[1:4]
# Read input
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().strip().split()))
# Read reference output
with open(output_path) as f:
ref_lines = [line.strip() for line in f.readlines()]
ref_k = int(ref_lines[0]) if ref_lines else -1
# Read submission output
with open(submission_path) as f:
sub_lines = [line.strip() for line in f.readlines()]
if ref_k == 0:
if len(sub_lines) == 1 and sub_lines[0] == '0':
print(1)
else:
print(0)
return
if not sub_lines:
print(0)
return
try:
sub_k = int(sub_lines[0])
except:
print(0)
return
if sub_k != ref_k:
print(0)
return
if len(sub_lines) < 2:
print(0)
return
try:
indices = list(map(int, sub_lines[1].split()))
except:
print(0)
return
if len(indices) != sub_k:
print(0)
return
# Check indices are valid
prev = 0
for idx in indices:
if not (1 <= idx <= n):
print(0)
return
if idx <= prev:
print(0)
return
prev = idx
# Check subsequence
subseq = [arr[i-1] for i in indices]
# Check non-decreasing
non_decr = True
for i in range(len(subseq)-1):
if subseq[i] > subseq[i+1]:
non_decr = False
break
# Check non-increasing
non_incr = True
for i in range(len(subseq)-1):
if subseq[i] < subseq[i+1]:
non_incr = False
break
if non_decr or non_incr:
print(0)
else:
print(1)
if __name__ == "__main__":
main()
| true
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.