message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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. | instruction | 0 | 60,289 | 14 | 120,578 |
Tags: implementation, math
Correct Solution:
```
import math
n,k=map(int,input().split())
w=list(map(int,input().split()))
c=0
for i in range(n):
c+=(w[i]+k-1)//k
print((c+1)//2)
``` | output | 1 | 60,289 | 14 | 120,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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. | instruction | 0 | 60,290 | 14 | 120,580 |
Tags: implementation, math
Correct Solution:
```
s = input()
(n,k) = s.split()
n = int(n)
k = int(k)
w = list()
ret = 0
s = input()
w = s.split()
for i in w:
t = int(i)
ret += (t+k-1)//k
ret = (ret+1)//2
print(ret)
``` | output | 1 | 60,290 | 14 | 120,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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. | instruction | 0 | 60,291 | 14 | 120,582 |
Tags: implementation, math
Correct Solution:
```
import math
# import bisect
import sys
# from collections import OrderedDict
input = sys.stdin.readline
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(s[:len(s)-1])
def invr():
return(map(int,input().split()))
N, K = invr()
W = inlt()
W.sort()
n = 0
out = 0
while n < N-1:
if W[n] <= K:
W[n] = 0
if W[n+1] <= K:
W[n+1] = 0
n += 1
else:
W[n+1] -= K
n += 1
out += 1
else:
curr = W[n]//K
out += curr
W[n] -= K*curr
W[n+1] -= K*curr
if W[n] == 0:
n += 1
if W[n] == 0:
n += 1
# print(W)
# print(out)
# out += W[n]//K
# print(out)
# if W[n]%K == 1:
# out += 1
if n == N-1 and W[n] != 0:
out += W[n]//(K*2)
if W[n]%(K*2) != 0:
out += 1
print(out)
``` | output | 1 | 60,291 | 14 | 120,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
n, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
res = 0
for i in a:
res += (i+k-1) // k
print((res+1)//2)
``` | instruction | 0 | 60,292 | 14 | 120,584 |
Yes | output | 1 | 60,292 | 14 | 120,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
n, k = map(int, input().split())
arr = list(map(int, input().split()))
cnt = 0
for i in range(n):
cnt += arr[i] // k
if arr[i] % k > 0:
cnt += 1
print((cnt+1)//2)
``` | instruction | 0 | 60,293 | 14 | 120,586 |
Yes | output | 1 | 60,293 | 14 | 120,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
import sys
import math
import bisect
def solve(A, m):
n = len(A)
cnt = 0
for i in range(n):
val = (A[i] + m - 1) // m
cnt += val
ans = (cnt + 1) // 2
return ans
def main():
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans = solve(A, m)
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 60,294 | 14 | 120,588 |
Yes | output | 1 | 60,294 | 14 | 120,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
import math as mt
import sys
import bisect
input=sys.stdin.readline
#t=int(input())
def takeSecond(elem):
return elem[1]
def gcd(a,b):
if (b == 0):
return a
return gcd(b, a%b)
t=1
for _ in range(t):
#n=int(input())
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=0
for i in range(n):
ans+=mt.ceil(l[i]/k)
print(mt.ceil(ans/2))
``` | instruction | 0 | 60,295 | 14 | 120,590 |
Yes | output | 1 | 60,295 | 14 | 120,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
from math import ceil as ce
from bisect import bisect as bl
n,k=map(int,input().split())
w=sorted(list(map(int,input().split())))
ans=0
``` | instruction | 0 | 60,296 | 14 | 120,592 |
No | output | 1 | 60,296 | 14 | 120,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
import math
n,k = map(int, input().split())
l = list(map(int, input().split()))
nbr_days=0
for i in range(n-1):
while(l[i]>0):
l[i] = l[i]-k
l[i+1] = l[i+1]-k
nbr_days+=1
if(l[n-1]>0):
t = l[n-1]/(2*k)
t = math.ceil(t)
nbr_days += t
print(nbr_days, end="")
``` | instruction | 0 | 60,297 | 14 | 120,594 |
No | output | 1 | 60,297 | 14 | 120,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
v = [int(i) for i in input().split()]
n = v[0]
p = v[1]
T = [int(i) for i in input().split()]
T.sort()
T.reverse()
i = 0
while (T != []):
test = 0
T[0] = T[0] - p
if T[0] <= 0 :
T[0] = 0
if T[0] != 0 :
T[0] = T[0] - p
test = True
if T[0] <= 0 :
T[0] = 0
del T[0]
if not test :
T[0] = T[0] - p
if T[0] <= 0 :
T[0] = 0
del T[0]
i = i + 1
print(i)
``` | instruction | 0 | 60,298 | 14 | 120,596 |
No | output | 1 | 60,298 | 14 | 120,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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.
Input
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.
Output
The only line of output contains one integer β the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
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.
Submitted Solution:
```
def mapit():
temp=list(map(int,input().split()))
return temp
def checker(nm,k):
if nm>2*k:
return 'too'
if nm>k and nm<=2*k:
return 'two'
if nm<=k:
return 'one'
def solution():
n,k=mapit()
arr=mapit()
one=0
two=0
too=[]
ans=0
for i in range(n):
verd=checker(arr[i], k)
if verd=='too':
too.append(arr[i])
elif verd=='two':
two+=1
else:
one+=1
for nm in too:
ans+=nm//(2*k)
rem=ans%(2*k)
verd=checker(rem,k)
if verd=='two':
two+=1
else:
one+=1
ans+=two
ans+=one//2
if one%2:
ans+=1
print(ans)
return
# t=int(input())
# while t:
# t-=1
solution()
``` | instruction | 0 | 60,299 | 14 | 120,598 |
No | output | 1 | 60,299 | 14 | 120,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,753 | 14 | 121,506 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
array = list(map(int, input().strip().split()))
s = []
value = 0
d = {}
already_visited = set()
for i in array:
value += 1
if i > 0:
if i not in d and i not in already_visited:
d[i] = 1
else:
print(-1)
exit(0)
else:
i = abs(i)
if i not in d:
print(-1)
exit(0)
else:
already_visited.add(i)
del d[i]
if len(d) == 0:
s.append(value)
value = 0
already_visited = set()
if len(d) == 0:
print(len(s))
print(' '.join(map(str, s)))
else:
print(-1)
``` | output | 1 | 60,753 | 14 | 121,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,754 | 14 | 121,508 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
l = list(map(int,input().split()))
mat = []
sumka = 0
stan = [0] * (10**6+1)
dasie = True
weszli = {}
for i in range(n):
if i > 0 and sumka == 0:
weszli = {}
mat.append(i)
stan[abs(l[i])] += abs(l[i])/l[i]
try:
weszli[l[i]] += 1
except Exception:
weszli[l[i]] = 1
if stan[abs(l[i])] < 0:
dasie = False
break
if weszli[l[i]] > 1:
dasie = False
break
sumka += abs(l[i])/l[i]
if sumka != 0 or stan != [0]*(10**6+1):
dasie = False
if sumka == 0:
mat.append(n)
if dasie:
a = [mat[0]]
for i in range(1, len(mat)):
a.append(mat[i]-mat[i-1])
print(len(a))
print(*a)
else:
print(-1)
``` | output | 1 | 60,754 | 14 | 121,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,755 | 14 | 121,510 |
Tags: greedy, implementation
Correct Solution:
```
import math,sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili=lambda:list(map(int,sys.stdin.readlines()))
li = lambda:list(map(int,input().split()))
#for deque append(),pop(),appendleft(),popleft(),count()
I=lambda:int(input())
S=lambda:input()
n=I()
a=li()
b=[0]*((10**6)+1)
p=[]
s=0
c=0
flag=0
d=defaultdict(int)
h=defaultdict(int)
for i in range(0,n):
if(a[i]<0):
if(b[abs(a[i])]==0):
flag=1
break
else:
b[abs(a[i])]-=1
s-=1
c+=1
else:
if(a[i] in d):
flag=1
break
else:
d[a[i]]=1
b[a[i]]+=1
s+=1
c+=1
if(s==0):
p.append(c)
c=0
d.clear()
#print(d)
if(flag==1):
print(-1)
elif(s!=0):
print(-1)
else:
print(len(p))
print(*p)
``` | output | 1 | 60,755 | 14 | 121,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,756 | 14 | 121,512 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
ans = []
days = -1
counter = 0
set_in = set()
set_out = set()
for i in range(n):
if a[i] > 0:
if a[i] in set_in or (-1 * a[i]) in set_out:
print(-1)
exit(0)
set_in.add(a[i])
if a[i] < 0:
if a[i] in set_out or abs(a[i]) not in set_in:
print(-1)
exit(0)
set_out.add(a[i])
set_in.remove(abs(a[i]))
counter += 1
if len(set_in) == 0:
days += 1
set_out = set()
ans.append(counter)
counter = 0
if len(set_in) != 0:
print(-1)
exit(0)
print(days + 1)
print(*ans)
``` | output | 1 | 60,756 | 14 | 121,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,757 | 14 | 121,514 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
array = list(map(int, input().split()))
days, flag = 0, 1
ans, day, arrived = [], set(), set()
for i in array:
if i > 0:
if i in arrived:
flag = 0
break
else:
day.add(i)
arrived.add(i)
else:
j = -i
if j in day:
day.remove(j)
else:
flag = 0
break
if len(day) == 0:
days += 1
ans.append(len(arrived)*2)
day, arrived = set(), set()
if flag == 1 and n == sum(ans):
print(days)
print(*ans)
else:
print(-1)
``` | output | 1 | 60,757 | 14 | 121,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,758 | 14 | 121,516 |
Tags: greedy, implementation
Correct Solution:
```
input()
t={0}
i=s=0
r=[0]
for x in map(int, input().split()):
if(x>0)&(x in t)|(x<0)^(-abs(x)in t):r=-1,;break
if x>0:t|={x,-x}
else:t-={x}
i+=1;s+=x
if s==0:r[0]+=1;r+=i,;t={0};i=0
if s:r=-1,
print(r[0])
print(*r[1:])
``` | output | 1 | 60,758 | 14 | 121,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,759 | 14 | 121,518 |
Tags: greedy, implementation
Correct Solution:
```
a = int(input())
b = list(map(int, input().split()))
def solve(a,b):
checkedin = set()
blocked = set()
ans = []
if a % 2 != 0:
return -1
for i in b:
if i > 0:
if i in blocked or i in checkedin:
return -1
else:
checkedin.add(i)
else:
if -i in checkedin:
blocked.add(-i)
checkedin.remove(-i)
else:
return -1
if len(checkedin) == 0:
ans.append(len(blocked)*2)
blocked.clear()
if len(checkedin) != 0:
return -1
return ans
c = solve(a,b)
if c == -1:
print(-1)
else:
print(len(c))
print(" ".join([str(x) for x in c]))
``` | output | 1 | 60,759 | 14 | 121,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.
The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.
The company has some strict rules about access to its office:
* An employee can enter the office at most once per day.
* He obviously can't leave the office if he didn't enter it earlier that day.
* In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.
Some examples of valid or invalid days:
* [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
* [2, -2, 3, -3] is also a valid day.
* [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day.
* [-4, 4] is not a valid day, because 4 left the office without being in it.
* [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day.
There are n events a_1, a_2, β¦, a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.
You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.
For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].
Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (-10^6 β€ a_i β€ 10^6 and a_i β 0).
Output
If there is no valid partition, print -1. Otherwise, print any valid partition in the following format:
* On the first line print the number d of days (1 β€ d β€ n).
* On the second line, print d integers c_1, c_2, β¦, c_d (1 β€ c_i β€ n and c_1 + c_2 + β¦ + c_d = n), where c_i is the number of events in the i-th day.
If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.
Examples
Input
6
1 7 -7 3 -1 -3
Output
1
6
Input
8
1 -1 1 2 -1 -2 3 -3
Output
2
2 6
Input
6
2 5 -5 5 -5 -2
Output
-1
Input
3
-8 1 1
Output
-1
Note
In the first example, the whole array is a valid day.
In the second example, one possible valid solution is to split the array into [1, -1] and [1, 2, -1, -2, 3, -3] (d = 2 and c = [2, 6]). The only other valid solution would be to split the array into [1, -1], [1, 2, -1, -2] and [3, -3] (d = 3 and c = [2, 4, 2]). Both solutions are accepted.
In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. | instruction | 0 | 60,760 | 14 | 121,520 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
a=[0]*(10**6+1)
use={}
ans=[0]
t=0
false=0
for i in range(n):
if l[i]>0:
if l[i] not in use:
a[l[i]]=1
t+=1
use[l[i]]=1
else:
false=1
break
if l[i]<0:
if a[-l[i]]==1:
a[-l[i]]=0
t-=1
else:
false=1
break
if t==0:
ans.append(i+1)
use={}
if t!=0:
false=1
if false==1:
print(-1)
else:
print(len(ans)-1)
for i in range(0,len(ans)-1):
print(ans[i+1]-ans[i],end=' ')
``` | output | 1 | 60,760 | 14 | 121,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,817 | 14 | 121,634 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
t = list(map(int, input().split()))
graph = [[] for i in range(n)]
for a, b in info:
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
t = sorted([(val, i) for i, val in enumerate(t)])
ans = [0] * n
for val, v in t:
set_ = set([])
for nxt_v in graph[v]:
set_.add(ans[nxt_v])
for i in range(1, val):
if i not in set_:
print(-1)
exit()
if val in set_:
print(-1)
exit()
ans[v] = val
print(*[t[i][1] + 1 for i in range(n)])
``` | output | 1 | 60,817 | 14 | 121,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,818 | 14 | 121,636 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from collections import defaultdict
graph=defaultdict(list)
n,m=map(int,input().split())
for i in range(m):
a,b=map(int,input().split())
graph[a].append(b)
graph[b].append(a)
#print(graph)
visited=[0]*(n+1)
topics=[int(i) for i in input().split()]
for i in range(n):
topics[i]=[topics[i],i+1]
copy=topics[:]
topics.sort()
ans=[]
#print(topics,copy)
for i in topics:
seta=set()
for j in graph[i[1]]:
if visited[j]==1:
seta.add(copy[j-1][0])
if sum(seta)==(i[0]*(i[0]-1))//2:
ans.append(i[1])
visited[i[1]]=1
else:
print(-1)
exit()
ans=' '.join(map(str,ans))
sys.stdout.write(ans+'\n')
``` | output | 1 | 60,818 | 14 | 121,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,819 | 14 | 121,638 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
import os,io
def read_list():
return list(map(int,input().split(' ')))
def print_list(l):
print(' '.join(map(str,l)))
# import heapq
# import bisect
# from collections import deque
from collections import defaultdict
# import math
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# f = open('test.py')
# def input():
# return f.readline().replace('\n','')
n,m = map(int,input().split())
dic_neig = defaultdict(list)
for _ in range(m):
a,b = map(int,input().split())
dic_neig[a].append(b)
dic_neig[b].append(a)
t = list(map(int,input().split()))
for blog in dic_neig:
dic_neig[blog] = set(t[i-1] for i in dic_neig[blog])
flag = True
for blog in range(1,n+1):
topic = t[blog-1]
if len(dic_neig[blog])<topic-1:
flag = False
break
if not dic_neig[blog]:
continue
if topic in dic_neig[blog]:
flag = False
break
if not set(range(1,topic))<=dic_neig[blog]:
flag = False
break
if not flag:
print(-1)
else:
dic_topic = defaultdict(list)
for i in range(n):
dic_topic[t[i]].append(i+1)
res = []
for i in range(1,n+1):
if not dic_topic[i]:
break
res+=dic_topic[i]
print_list(res)
``` | output | 1 | 60,819 | 14 | 121,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,820 | 14 | 121,640 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
input=sys.stdin.buffer.readline
#3 3
#1 2
#2 3
#3 1
#1 1 1
#n=2 #test
n,m=[int(x) for x in input().split()]
neighbours=dict() #[blog:[neighbouring blogs]]
maxInNeighbours=dict() #{blog:max in neighbours so far}
for i in range(1,n+1):
neighbours[i]=[]
maxInNeighbours[i]=0
for _ in range(m):
a,b=[int(x) for x in input().split()] #blog links
neighbours[a].append(b);neighbours[b].append(a)
desiredTopic=[-1]+[int(x) for x in input().split()] #desiredTopic[blog]
blogs=list(range(1,n+1))
blogs.sort(key=lambda blog:desiredTopic[blog]) #sort according to topics asc
ok=True #run a check the the sorted order can work
for blog in blogs:
blogTopic=desiredTopic[blog]
if maxInNeighbours[blog]+1!=blogTopic:
ok=False
break
else: #update neighbours
for neighbour in neighbours[blog]:
if maxInNeighbours[neighbour]==blogTopic-1: #if not,it will never be updated. As topic increases (blog is sorted by topic asc), the gap will remain.
maxInNeighbours[neighbour]=blogTopic
if ok:
print(' '.join([str(x) for x in blogs]))
else:
print('-1')
``` | output | 1 | 60,820 | 14 | 121,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,821 | 14 | 121,642 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
r=sys.stdin.buffer.readline
n,m=map(int,r().split())
network=[[] for _ in range(n)]
for _ in range(m):
a,b=map(int,r().split())
network[a-1].append(b-1)
network[b-1].append(a-1)
lst=list(map(int,r().split()))
arr=list(range(n))
arr.sort(key=lambda x: lst[x])
ret=[1]*n
res=[]
for i in arr:
t=lst[i]
if t==ret[i]:
for j in network[i]:
if ret[j]==t:
ret[j]+=1
res.append(i+1)
else:
print(-1)
exit(0)
print(*res)
``` | output | 1 | 60,821 | 14 | 121,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,822 | 14 | 121,644 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def f():
n, m = [int(s) for s in input().split()]
neibors = [[] for _ in range(n)]
for _ in range(m):
a, b = [int(s)-1 for s in input().split()]
neibors[a].append(b)
neibors[b].append(a)
t = [int(s) for s in input().split()]
# print(neibors)
def countSmaller(nbs,e):
s = {t[x] for x in nbs}
ans = 0
for x in s:
if x <= e:
ans += 1
return ans
smlCount = [countSmaller(neibors[i],t[i]) for i in range(n)]
# print(smlCount)
ind = list(range(n))
ind.sort(key=lambda x:t[x])
for i in ind:
if t[i]-1 != smlCount[i]:
print(-1)
return
print(' '.join(str(i+1) for i in ind))
f()
``` | output | 1 | 60,822 | 14 | 121,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,823 | 14 | 121,646 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def main():
n, m = map(int,input().split())
G = [[] for k in range(n)]
for k in range(m):
a, b = map(int,input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
t = list(map(int,input().split()))
u = sorted([[t[k],k] for k in range(n)])
# print(*u,sep="\n")
visited = [0]*n
for k in range(n):
visited[u[k][1]] = 1
seen = set([])
for e in G[u[k][1]]:
if visited[e] == 1:
if t[e] >= t[u[k][1]]:
print(-1)
exit(0)
else:
seen.add(t[e])
if u[k][0] != len(seen)+1:
print(-1)
exit(0)
print(*[u[k][1]+1 for k in range(n)])
if __name__ == '__main__':
main()
``` | output | 1 | 60,823 | 14 | 121,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2. | instruction | 0 | 60,824 | 14 | 121,648 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
input=sys.stdin.buffer.readline
n,m=[int(x) for x in input().split()]
neighbours=dict() #[blog:[neighbouring blogs]]
for i in range(1,n+1):
neighbours[i]=[]
for _ in range(m):
a,b=[int(x) for x in input().split()] #blog links
neighbours[a].append(b);neighbours[b].append(a)
desiredTopic=[-1]+[int(x) for x in input().split()] #desiredTopic[blog]
blogs=list(range(1,n+1))
blogs.sort(key=lambda blog:desiredTopic[blog]) #sort according to topics asc
ok=True #run a check the the sorted order can work
for blog in blogs:
blogTopic=desiredTopic[blog]
smallerTopicSet=set()
for neighbour in neighbours[blog]:
if desiredTopic[neighbour]<blogTopic: #only the neighbours with smaller topics have gone earlier
smallerTopicSet.add(desiredTopic[neighbour])
if desiredTopic[neighbour]==blogTopic: #neighbour cannot have same topic
ok=False
break
if len(smallerTopicSet)+1!=blogTopic: #there must be no "gaps" before blogTopic
ok=False
if ok==False:
break
if ok:
print(' '.join([str(x) for x in blogs]))
else:
print('-1')
``` | output | 1 | 60,824 | 14 | 121,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from collections import defaultdict,deque
def bfs(node):
vis[node]=1
q=deque([node])
while q:
cur=q.popleft()
dict1={}
for j in edge[cur]:
if vis[j]==0:
vis[j]=1
q.append(j)
if topic[j-1]==topic[cur-1]:
return False
if topic[j-1]<topic[cur-1]:
dict1[topic[j-1]]=1
b=len(dict1.keys())
if b!=topic[cur-1]-1:
return False
return True
n,m=list(map(int,input().split()))
edge=defaultdict(list)
for i in range(m):
u,v=list(map(int,input().split()))
edge[u].append(v)
edge[v].append(u)
topic=list(map(int,input().split()))
vis=[0]*(n+1)
s=0
for i in range(1,n+1):
if vis[i]==0:
temp=bfs(i)
if temp==False:
s+=1
break
if s==1:
print(-1)
else:
ans=[]
for i in range(n):
ans.append([topic[i],i+1])
ans.sort(key=lambda x:x[0],reverse=False)
num=[]
for i in range(n):
num.append(ans[i][1])
print(" ".join(str(x) for x in num))
``` | instruction | 0 | 60,825 | 14 | 121,650 |
Yes | output | 1 | 60,825 | 14 | 121,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
n,m=map(int,input().split())
con=[[] for _ in range(n)]
for _ in range(m):
a,b=map(int,input().split())
con[a-1].append(b-1)
con[b-1].append(a-1)
t=[int(x) for x in input().split()]
od=sorted(list(range(n)),key=lambda f:t[f])
cn=[1]*n
ans=[]
for ii in od:
tt=t[ii]
if cn[ii]!=tt:
print(-1)
exit()
for jj in con[ii]:
if cn[jj]==tt:
cn[jj]+=1
ans.append(ii+1)
print(" ".join(map(str,ans)))
``` | instruction | 0 | 60,826 | 14 | 121,652 |
Yes | output | 1 | 60,826 | 14 | 121,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,m = map(int,input().split())
edgeList = []
for _ in range(n):
edgeList.append([])
for _ in range(m):
a,b = map(int,input().split())
edgeList[a-1].append(b-1)
edgeList[b-1].append(a-1)
topic = list(map(int,input().split()))
isConfigPossible = True
for i in range(n):
topicCovered = []
for j in edgeList[i]:
topicCovered.append(topic[j])
topicCovered.sort()
curTopic = 1
for elem in topicCovered:
if curTopic < elem:
break
elif curTopic == elem:
curTopic += 1
if curTopic != topic[i]:
isConfigPossible = False
break
if isConfigPossible:
topicWithIndex = []
for i in range(n):
topicWithIndex.append((topic[i],i))
topicWithIndex.sort(key = lambda x: x[0])
res = []
for elem in topicWithIndex:
res.append(str(elem[1] + 1))
print(' '.join(res))
else:
print(-1)
``` | instruction | 0 | 60,827 | 14 | 121,654 |
Yes | output | 1 | 60,827 | 14 | 121,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from operator import itemgetter
n,m=map(int,input().split())
E=[[] for i in range(n+1)]
for i in range(m):
x,y=map(int,input().split())
E[x].append(y)
E[y].append(x)
T=list(map(int,input().split()))
T_INV=[(T[i],i+1) for i in range(n)]
T_INV.sort(key=itemgetter(0))
LOWEST=[1]*(n+1)
for top,ind in T_INV:
if LOWEST[ind]==top:
for to in E[ind]:
if LOWEST[to]==top:
LOWEST[to]+=1
else:
print(-1)
break
else:
ANS=[T_INV[i][1] for i in range(n)]
print(*ANS)
``` | instruction | 0 | 60,828 | 14 | 121,656 |
Yes | output | 1 | 60,828 | 14 | 121,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import sys
from collections import defaultdict
r=sys.stdin.readline
n,m=map(int,r().split())
network=defaultdict(set)
for _ in range(m):
a,b=map(int,r().split())
network[a].add(b)
network[b].add(a)
tb=defaultdict(list)
for blog,topic in enumerate(map(int,r().split()),start=1):
tb[topic].append(blog)
res=[]
write=dict()
arr=[1]*(n+1)
for i in sorted(tb.keys()): #topic i 1 to n
for blog in tb[i]: #blog want to write topic i
if arr[blog]==i: #topic candidate arr
for negibor in network[blog]:
arr[negibor]+=1
else:
print(-1)
exit(0)
res.append(blog)
print(*res)
``` | instruction | 0 | 60,829 | 14 | 121,658 |
No | output | 1 | 60,829 | 14 | 121,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
input = sys.stdin.readline
from bisect import *
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
rls= lambda: list(map(str, input().split()))
def check(i,val):
for j in d[i]:
if(blog[j]<val):
return -1
if(vis[j]==0):
vis[j]=1
if(check(j,blog[j])==-1):
return -1
return 1
# t=int(input())
# for _ in range(0,t):
n,m=rl()
d=[]
for i in range(0,n+1):
d.append([])
for i in range(0,m):
a,b=rl()
d[a].append(b)
d[b].append(a)
a=rl()
l=[]
for i in range(1,n+1):
l.append([a[i-1],i])
l.sort(key= lambda x:x[0])
#print(l)
blog=[9999999]*(n+1)
vis=[0]*(n+1)
f=1
for i in range(0,n):
vis = [0] * (n + 1)
s=(l[i][0]*(l[i][0]+1))//2
c=0
for j in d[l[i][1]]:
if(blog[j]<=l[i][0]):
c=c+1
# if (vis[blog[j]] == 0):
# vis[bog[j]] = 1
# s = s-blog[j]
# print(i,j,blog[j],l[i][0])
# f=0
# break
if(l[i][0]-1!=c):
f=0
break
blog[l[i][1]]=l[i][0]
if(f):
for i in range(0,n):
print(l[i][1],end=" ")
print()
else:
print(-1)
``` | instruction | 0 | 60,830 | 14 | 121,660 |
No | output | 1 | 60,830 | 14 | 121,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from collections import defaultdict,deque
def bfs(node):
vis[node]=1
q=deque([node])
while q:
cur=q.popleft()
a=len(edge[cur])+1
lis=[0]*a
if topic[cur-1]<=a:
lis[topic[cur-1]-1]=1
for j in edge[cur]:
if vis[j]==0:
vis[j]=1
q.append(j)
if topic[j-1]<=a:
lis[topic[j-1]-1]=1
b=lis.count(1)
if b==a:
array[cur-1]=1
for j in edge[cur]:
array[j-1]=1
n,m=list(map(int,input().split()))
edge=defaultdict(list)
for i in range(m):
u,v=list(map(int,input().split()))
edge[u].append(v)
edge[v].append(u)
topic=list(map(int,input().split()))
vis=[0]*(n+1)
array=[0]*n
for i in range(1,n+1):
if vis[i]==0:
temp=bfs(i)
if min(array)==0:
print(-1)
else:
ans=[]
for i in range(n):
ans.append([topic[i],i+1])
ans.sort(key=lambda x:x[0],reverse=False)
num=[]
for i in range(n):
num.append(ans[i][1])
print(" ".join(str(x) for x in num))
``` | instruction | 0 | 60,831 | 14 | 121,662 |
No | output | 1 | 60,831 | 14 | 121,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Today Johnny wants to increase his contribution. His plan assumes writing n blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
There are n different topics, numbered from 1 to n sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most n - 1 neighbors.
For example, if already written neighbors of the current blog have topics number 1, 3, 1, 5, and 2, Johnny will choose the topic number 4 for the current blog, because topics number 1, 2 and 3 are already covered by neighbors and topic number 4 isn't covered.
As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
Input
The first line contains two integers n (1 β€ n β€ 5 β
10^5) and m (0 β€ m β€ 5 β
10^5) β the number of blogs and references, respectively.
Each of the following m lines contains two integers a and b (a β b; 1 β€ a, b β€ n), which mean that there is a reference between blogs a and b. It's guaranteed that the graph doesn't contain multiple edges.
The last line contains n integers t_1, t_2, β¦, t_n, i-th of them denotes desired topic number of the i-th blog (1 β€ t_i β€ n).
Output
If the solution does not exist, then write -1. Otherwise, output n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
Examples
Input
3 3
1 2
2 3
3 1
2 1 3
Output
2 1 3
Input
3 3
1 2
2 3
3 1
1 1 1
Output
-1
Input
5 3
1 2
2 3
4 5
2 1 2 2 1
Output
2 5 1 3 4
Note
In the first example, Johnny starts with writing blog number 2, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 1, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 3, it has references to blogs number 1 and 2 so it receives the third topic.
Second example: There does not exist any permutation fulfilling given conditions.
Third example: First Johnny writes blog 2, it receives the topic 1. Then he writes blog 5, it receives the topic 1 too because it doesn't have reference to single already written blog 2. Then he writes blog number 1, it has reference to blog number 2 with topic 1, so it receives the topic 2. Then he writes blog number 3 which has reference to blog 2, so it receives the topic 2. Then he ends with writing blog number 4 which has reference to blog 5 and receives the topic 2.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
class Graph(object):
"""docstring for Graph"""
def __init__(self,n,d): # Number of nodes and d is True if directed
self.n = n
self.graph = [[] for i in range(n)]
self.parent = [-1 for i in range(n)]
self.directed = d
def addEdge(self,x,y):
self.graph[x].append(y)
if not self.directed:
self.graph[y].append(x)
def bfs(self, root): # NORMAL BFS
self.parent = [-1 for i in range(self.n)]
queue = [root]
queue = deque(queue)
vis = [0]*self.n
while len(queue)!=0:
element = queue.popleft()
vis[element] = 1
for i in self.graph[element]:
if vis[i]==0:
queue.append(i)
self.parent[i] = element
def dfs(self, root, ans): # Iterative DFS
stack=[root]
vis=[0]*self.n
stack2=[]
while len(stack)!=0: # INITIAL TRAVERSAL
element = stack.pop()
if vis[element]:
continue
vis[element] = 1
stack2.append(element)
for i in self.graph[element]:
if vis[i]==0:
self.parent[i] = element
stack.append(i)
while len(stack2)!=0: # BACKTRACING. Modify the loop according to the question
element = stack2.pop()
m = 0
for i in self.graph[element]:
if i!=self.parent[element]:
m += ans[i]
ans[element] = m
return ans
def shortestpath(self, source, dest): # Calculate Shortest Path between two nodes
self.bfs(source)
path = [dest]
while self.parent[path[-1]]!=-1:
path.append(parent[path[-1]])
return path[::-1]
def ifcycle(self):
self.bfs(0)
queue = [0]
vis = [0]*n
queue = deque(queue)
while len(queue)!=0:
element = queue.popleft()
vis[element] = 1
for i in graph[element]:
if vis[i]==1 and i!=parent[element]:
return True
if vis[i]==0:
queue.append(i)
vis[i] = 1
return False
def reroot(self, root, ans):
stack = [root]
vis = [0]*n
while len(stack)!=0:
e = stack[-1]
if vis[e]:
stack.pop()
# Reverse_The_Change()
continue
vis[e] = 1
for i in graph[e]:
if not vis[e]:
stack.append(i)
if self.parent[e]==-1:
continue
# Change_The_Answers()
def check(self, node, blog):
done = {}
for i in self.graph[node]:
if i in b and b[i]<blog:
done[i] = 1
if i in b and b[i]==blog:
return False
if len(done)==blog-1:
return True
return False
n,m = map(int,input().split())
g = Graph(n+1,False)
for i in range(m):
a,b = map(int,input().split())
g.addEdge(a,b)
t = list(map(int,input().split()))
l = {}
for i in range(1,n+1):
if t[i-1] not in l:
l[t[i-1]] = [i]
else:
l[t[i-1]].append(i)
d = {}
b = {}
ans = []
i = 1
flag = 0
# print (g.graph)
# print (l)
while len(d)!=n:
for j in l[i]:
if not g.check(j,i):
flag = 1
break
else:
d[j] = 1
b[j] = i
ans.append(j)
if flag:
break
i += 1
if flag:
print (-1)
exit()
print (*ans)
``` | instruction | 0 | 60,832 | 14 | 121,664 |
No | output | 1 | 60,832 | 14 | 121,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,865 | 14 | 121,730 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import io,os,sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import math
def upper_bound(nums, target):
l, r = 0, len(nums) - 1
while l <= r:
mid = l + (r - l) // 2
if nums[mid] > target:
r = mid - 1
else:
l = mid + 1
return l
def isPrime(n):
if(n<=1): return 1;
else:
i=2;
while(i*i<=n):
if(n%i==0): return 0;
i+=1;
return 1;
def seive(n):
a = [1]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
a.append(p)
return(a)
primeList = seive(int(1e6+5));
t = int(input())
#y = [int(i) for i in input().split()]
y = list(map(int,input().split()))
#print("done")
r=""
for i in range(t):
x = y[i]
xx = math.sqrt(x);
maxIndex = upper_bound(primeList,x);
lowIndex = upper_bound(primeList,int(xx));
#r+=str(maxIndex - lowIndex +1) + '\n'
sys.stdout.write(str((maxIndex - lowIndex +1)) + "\n")
#print(r)
#sys.stdout.write(r)
``` | output | 1 | 60,865 | 14 | 121,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,866 | 14 | 121,732 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import sys, os, io
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math,datetime,functools,itertools,operator,bisect,fractions,statistics
from collections import deque,defaultdict,OrderedDict,Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max
def main():
starttime=datetime.datetime.now()
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
m=1000005
spf = [1] * m
for i in range(2, m):
if spf[i] == 1:
spf[i] = i
for j in range(i, m, i):
if spf[j] == 1:
spf[j] = i
for _ in range(1):
t=ri()
a=ria()
ans=[]
d={}
k={}
c=0
for i in range(1,len(spf)):
if spf[i] not in d:
d[spf[i]]=1
c+=1
else:
if spf[i] not in k:
c-=1
k[spf[i]]=1
ans.append(c)
for i in a:
print(ans[i-1])
#<--Solving Area Ends
endtime=datetime.datetime.now()
time=(endtime-starttime).total_seconds()*1000
if(os.path.exists('input.txt')):
print("Time:",time,"ms")
class FastReader(io.IOBase):
newlines = 0
def __init__(self, fd, chunk_size=1024 * 8):
self._fd = fd
self._chunk_size = chunk_size
self.buffer = io.BytesIO()
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self, size=-1):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
class FastWriter(io.IOBase):
def __init__(self, fd):
self._fd = fd
self.buffer = io.BytesIO()
self.write = self.buffer.write
def flush(self):
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class FastStdin(io.IOBase):
def __init__(self, fd=0):
self.buffer = FastReader(fd)
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
class FastStdout(io.IOBase):
def __init__(self, fd=1):
self.buffer = FastWriter(fd)
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.flush = self.buffer.flush
if __name__ == '__main__':
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()
``` | output | 1 | 60,866 | 14 | 121,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,867 | 14 | 121,734 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import io,os,sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import math
def upper_bound(nums, target):
l, r = 0, len(nums) - 1
while l <= r:
mid = l + (r - l) // 2
if nums[mid] > target:
r = mid - 1
else:
l = mid + 1
return l
def isPrime(n):
if(n<=1): return 1;
else:
i=2;
while(i*i<=n):
if(n%i==0): return 0;
i+=1;
return 1;
def seive(n):
a = [1]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
a.append(p)
return(a)
####
primeList = seive(int(1e6+5));
t = int(input())
#y = [int(i) for i in input().split()]
y = list(map(int,input().split()))
#print("done")
r=""
for i in range(t):
x = y[i]
xx = math.sqrt(x);
maxIndex = upper_bound(primeList,x);
lowIndex = upper_bound(primeList,int(xx));
#r+=str(maxIndex - lowIndex +1) + '\n'
sys.stdout.write(str((maxIndex - lowIndex +1)) + "\n")
#print(r)
#sys.stdout.write(r)
``` | output | 1 | 60,867 | 14 | 121,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,868 | 14 | 121,736 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import math
import sys
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
pre=[0]
for j in range(1,n+1):
if prime[j]:
pre.append(pre[-1]+1)
else:
pre.append(pre[-1])
return pre
pre=SieveOfEratosthenes(10**6+1)
t=int(input())
b=list(map(int,input().split()))
for j in b:
print(pre[j]-pre[int(j**0.5)]+1)
``` | output | 1 | 60,868 | 14 | 121,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,870 | 14 | 121,740 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import math
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def fill_sieve():
X = math.ceil(math.sqrt(N))
for j in range(2 * 2, N + 1, 2):
sieve[j] = False
i = 3
while i <= X:
if sieve[i]:
for j in range(i * i, N + 1, 2 * i):
sieve[j] = False
i += 2
N = 10 ** 6
sieve = [False] * 2 + [True] * N
P = [0, 0]
fill_sieve()
for i in range(2, N + 1):
P.append(P[-1] + int(sieve[i]))
t = int(input())
q = [int(x) for x in input().split(' ')]
r = []
for m in q:
r.append(1 + P[m] - P[math.floor(m ** 0.5)])
print(*r)
``` | output | 1 | 60,870 | 14 | 121,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,871 | 14 | 121,742 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
import bisect
import heapq
def main():
pass
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
def countcon(s, i):
c = 0
ch = s[i]
for i in range(i, len(s)):
if (s[i] == ch):
c += 1
else:
break
return (c)
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
def isSubSequence(str1, str2):
m = len(str1)
n = len(str2)
j = 0
i = 0
while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1
return j == m
def maxfac(n):
root = int(n ** 0.5)
for i in range(2, root + 1):
if (n % i == 0):
return (n // i)
return (n)
def p2(n):
c=0
while(n%2==0):
n//=2
c+=1
return c
def seive(n):
primes=[True]*(n+1)
primes[1]=primes[0]=False
for i in range(2,n+1):
if(primes[i]):
for j in range(i+i,n+1,i):
primes[j]=False
p=[]
for i in range(0,n+1):
if(primes[i]):
p.append(i)
return(p)
pr=seive(1000000)
n=int(input())
l=list(map(int,input().split()))
for i in l:
if(i==1):
print(1)
else:
t=i
ind=bisect.bisect(pr,int(i**0.5))
indr=bisect.bisect(pr,i)
print(1+indr-ind)
``` | output | 1 | 60,871 | 14 | 121,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely. | instruction | 0 | 60,872 | 14 | 121,744 |
Tags: binary search, math, number theory, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
print(' '.join(map(str,l)))
# import sys
# sys.setrecursionlimit(5010)
# from heapq import *
# from collections import deque as dq
from math import ceil,floor,sqrt,pow
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
def judgePrime(n):
if n < 2:
return []
else:
output = [1] * n
output[0],output[1] = 0,0
for i in range(2,int(n**0.5)+1):
if output[i] == 1:
output[i*i:n:i] = [0] * len(output[i*i:n:i])
return output
prime = judgePrime(1000005)
s = [0]
for i in range(1,1000005):
s.append(s[-1]+prime[i])
t = N()
a = RL()
for n in a:
print(s[n]-s[int(sqrt(n))]+1)
``` | output | 1 | 60,872 | 14 | 121,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely.
Submitted Solution:
```
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def sieve():
n=10**6+2
dp=[1]*n
dp[0],dp[1]=0,0
i=2
while (i*i)<=n:
if dp[i]:
j=i
for jj in range(2*j,n,j):
dp[jj]=0
i+=1
for k in range(1,n):
dp[k]+=dp[k-1]
return dp
dp=sieve()
c=int(input())
l=list(map(int,input().split()))
for i in l:
k=int(i**0.5)
print(dp[i]-dp[k]+1)
``` | instruction | 0 | 60,873 | 14 | 121,746 |
Yes | output | 1 | 60,873 | 14 | 121,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely.
Submitted Solution:
```
import os,io
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
mx_test = (10**6)+3
pri = [1]*mx_test
st = 2
while st**2<=mx_test:
if pri[st]:
t = st*2
while t<=mx_test:
pri[t] = 0
t+=st
st+=1
tot =[0,0]
for j in range(2,mx_test):
tot.append(tot[-1]+pri[j])
n = input()
for test in list(map(int,input().split())):
print(tot[test]+1 - tot[int(test**(0.5))])
``` | instruction | 0 | 60,874 | 14 | 121,748 |
Yes | output | 1 | 60,874 | 14 | 121,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely.
Submitted Solution:
```
import math
#print(math.gcd(5, 15))
t=int(input())
nums=list(map(int, input().rstrip().split()))
m=max(nums)
prime_nums=[1, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
'''
for i in range(10, 10**3+1):
condition=True
for j in range(2, int(math.sqrt(i))+1):
if i%j==0:
condition=False
break
if condition:
prime_nums.append(i)
'''
#print(prime_nums)
for i in range(len(nums)):
total=1
for j in range(1, len(prime_nums)):
if prime_nums[j]<=nums[i] and prime_nums[j]**2>nums[i]:
total+=1
if prime_nums[j]>nums[i]:
break
print(total)
``` | instruction | 0 | 60,877 | 14 | 121,754 |
No | output | 1 | 60,877 | 14 | 121,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely.
Submitted Solution:
```
from sys import stdin, stdout
import math,sys,heapq
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
from os import path
import random
import bisect as bi
def yes():print('YES')
def no():print('NO')
if (path.exists('input.txt')):
#------------------Sublime--------------------------------------#
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
def I():return (int(input()))
def In():return(map(int,input().split()))
else:
#------------------PYPY FAst I/o--------------------------------#
def I():return (int(stdin.readline()))
def In():return(map(int,stdin.readline().split()))
#sys.setrecursionlimit(1500)
def dict(a):
d={}
for x in a:
if d.get(x,-1)!=-1:
d[x]+=1
else:
d[x]=1
return d
def find_gt(a, x):
'Find leftmost value greater than x'
i = bi.bisect_left(a, x)
if i != len(a):
return i
else:
return -1
def cal(n):
dp=[0 for x in range(n+1)]
for i in range(2,(n//2)+1):
t=2
if i*2<n:
while t*i<=n:
#print(t*i,i)
dp[t*i]=1
t+=1
dp[i]=1
else:
continue
#ans=[0]
#print(dp)
count=0
for x in range(1,n+1):
if dp[x]==0:
count+=1
return(count)
def main():
try:
n=I()
l=list(In())
ans=[]
for x in l:
print(cal(x))
except:
pass
M = 998244353
P = 1000000007
if __name__ == '__main__':
#for _ in range(I()):main()
for _ in range(1):main()
``` | instruction | 0 | 60,879 | 14 | 121,758 |
No | output | 1 | 60,879 | 14 | 121,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b + c > a and c + a > b.
In a group of numbers, a number is lonely if it doesn't have any friends in that group.
Given a group of numbers containing all numbers from 1, 2, 3, ..., n, how many numbers in that group are lonely?
Input
The first line contains a single integer t (1 β€ t β€ 10^6) - number of test cases.
On next line there are t numbers, n_i (1 β€ n_i β€ 10^6) - meaning that in case i you should solve for numbers 1, 2, 3, ..., n_i.
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1, 2, 3, ..., n_i.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 is the only number and therefore lonely.
For second test case where n=5, numbers 1, 3 and 5 are lonely.
For third test case where n=10, numbers 1, 5 and 7 are lonely.
Submitted Solution:
```
#include <CodeforcesSolutions.h>
#include <ONLINE_JUDGE <solution.cf(contestID = "1422",questionID = "A",method = "GET")>.h>
"""
Author : thekushalghosh
Team : CodeDiggers
I prefer Python language over the C++ language :p :D
Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time,collections
start_time = time.time()
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################
def seive(n):
a = [1]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
a.append(p)
return(a)
def solve():
q = seive(10 ** 6)
w = [0] * (10 ** 6)
w[0] = 1
j = 1
qw = 0
for i in range(1,10 ** 6):
if qw == 1 or q[j] > i + 1:
w[i] = w[i - 1]
else:
w[i] = w[i - 1] + 1
j = j + 1
if j > len(q) - 1:
j = len(q) - 1
qw = 1
qq = [0] * (10 ** 6)
qq[0] = 4
qqqq = 1
while qq[qqqq - 1] <= 10 ** 6:
qq[qqqq] = q[qqqq + 1] ** 2
qqqq = qqqq + 1
qqqq = qq.index(0)
ww = [0] * (10 ** 6)
ww[3] = 1
j = 1
qw = 0
for i in range(4,10 ** 6):
if qw == 1 or qq[j] > i + 1:
ww[i] = ww[i - 1]
else:
ww[i] = ww[i - 1] + 1
j = j + 1
if j > qqqq - 1:
j = qqqq - 1
qw = 1
a = inlt()
a = [10 ** 6] * (10 ** 6)
qw = [0] * len(a)
for i in range(len(a)):
qw[i] = w[a[i] - 1] - ww[a[i] - 1]
print(*qw,sep = "\n")
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
def main():
global tt
if not ONLINE_JUDGE:
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
t = 1
t = inp()
t = 1
for tt in range(1,t + 1):
solve()
if not ONLINE_JUDGE:
print("Time Elapsed :",time.time() - start_time,"seconds")
sys.stdout.close()
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
return(input().strip())
def invr():
return(map(int,input().split()))
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def counter(a):
q = [0] * max(a)
for i in range(len(a)):
q[a[i] - 1] = q[a[i] - 1] + 1
return(q)
def counter_elements(a):
q = dict()
for i in range(len(a)):
if a[i] not in q:
q[a[i]] = 0
q[a[i]] = q[a[i]] + 1
return(q)
def string_counter(a):
q = [0] * 26
for i in range(len(a)):
q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
return(q)
def factorial(n,m = 1000000007):
q = 1
for i in range(n):
q = (q * (i + 1)) % m
return(q)
def factors(n):
q = []
for i in range(1,int(n ** 0.5) + 1):
if n % i == 0: q.append(i); q.append(n // i)
return(list(sorted(list(set(q)))))
def prime_factors(n):
q = []
while n % 2 == 0: q.append(2); n = n // 2
for i in range(3,int(n ** 0.5) + 1,2):
while n % i == 0: q.append(i); n = n // i
if n > 2: q.append(n)
return(list(sorted(q)))
def transpose(a):
n,m = len(a),len(a[0])
b = [[0] * n for i in range(m)]
for i in range(m):
for j in range(n):
b[i][j] = a[j][i]
return(b)
def power_two(x):
return (x and (not(x & (x - 1))))
def ceil(a, b):
return -(-a // b)
#-----------------------------------------------------------------------#
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
import io,os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
#input = sys.stdin.readline
main()
``` | instruction | 0 | 60,880 | 14 | 121,760 |
No | output | 1 | 60,880 | 14 | 121,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively "Loves" and "Doesn't love", at that Marina always starts with "Loves". There are n camomiles growing in the field, possessing the numbers of petals equal to a1, a2, ... an. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be "Loves". Help her do that; find the maximal number of petals possible in the bouquet.
Input
The first line contains an integer n (1 β€ n β€ 100), which is the number of flowers growing in the field. The second line contains n integers ai (1 β€ ai β€ 100) which represent the number of petals on a given i-th camomile.
Output
Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in "Loves". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.
Examples
Input
1
1
Output
1
Input
1
2
Output
0
Input
3
5 6 7
Output
13 | instruction | 0 | 61,110 | 14 | 122,220 |
Tags: implementation, number theory
Correct Solution:
```
n = int(input())
petals = list(map(int, input().split()))
odd = 0
for i in range(n):
petals = sorted(petals)
if petals[i] % 2 != 0:
odd = petals[i]
break
if sum(petals)%2==0:
if odd == 0:
print(0)
else:
print(sum(petals)-odd)
else:
print(sum(petals))
``` | output | 1 | 61,110 | 14 | 122,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively "Loves" and "Doesn't love", at that Marina always starts with "Loves". There are n camomiles growing in the field, possessing the numbers of petals equal to a1, a2, ... an. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be "Loves". Help her do that; find the maximal number of petals possible in the bouquet.
Input
The first line contains an integer n (1 β€ n β€ 100), which is the number of flowers growing in the field. The second line contains n integers ai (1 β€ ai β€ 100) which represent the number of petals on a given i-th camomile.
Output
Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in "Loves". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.
Examples
Input
1
1
Output
1
Input
1
2
Output
0
Input
3
5 6 7
Output
13 | instruction | 0 | 61,111 | 14 | 122,222 |
Tags: implementation, number theory
Correct Solution:
```
y=(input())
x=list(map(int,input().split()))
even=[]
odd=[]
for i in x:
if i%2==0:
even+=[i]
else:
odd+=[i]
w=0
if sum(x)%2==1:
w=sum(x)
else:
if len(odd)>=1:
odd.sort()
w=sum(even)+sum(odd[1:])
print(w)
``` | output | 1 | 61,111 | 14 | 122,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively "Loves" and "Doesn't love", at that Marina always starts with "Loves". There are n camomiles growing in the field, possessing the numbers of petals equal to a1, a2, ... an. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be "Loves". Help her do that; find the maximal number of petals possible in the bouquet.
Input
The first line contains an integer n (1 β€ n β€ 100), which is the number of flowers growing in the field. The second line contains n integers ai (1 β€ ai β€ 100) which represent the number of petals on a given i-th camomile.
Output
Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in "Loves". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.
Examples
Input
1
1
Output
1
Input
1
2
Output
0
Input
3
5 6 7
Output
13 | instruction | 0 | 61,112 | 14 | 122,224 |
Tags: implementation, number theory
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
a = list(map(int, input().split()))
odd, even = [], []
for x in a:
if x % 2:
odd.append(x)
else:
even.append(x)
odd.sort()
if odd:
if len(odd) % 2:
print(sum(odd) + sum(even))
else:
print(sum(odd[1:]) + sum(even))
else:
print(0)
``` | output | 1 | 61,112 | 14 | 122,225 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.