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.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0 | instruction | 0 | 65,931 | 14 | 131,862 |
Tags: greedy
Correct Solution:
```
import sys
arr_size = sys.stdin.readline()
while arr_size:
arr_size = int(arr_size)
#print('arr_size', arr_size)
array = sys.stdin.readline().split()
#print('array', array)
array = list(map(int, array))
#print(array)
years = [2000 + i + 1 for i in range(len(array))]
#print(years)
"""
ids_ommit = [i for i in range(1, len(array)) if array[i-1] != array[i]-1 ]
ids_ommit_neg = [i for i in range(len(array)) if array[i] <0 ]
print(ids_ommit)
print(ids_ommit_neg)
"""
#ids_ommit = []
last_number = 0
ids_to_choose = []
for i in range(len(array)):
if array[i] > 0:
if array[i]-1 != last_number:
next
else:
ids_to_choose.append(i)
last_number = array[i]
else:
next
#ids_ommit.append(i)
#print(ids_to_choose)
#print(ids_ommit)
if(len(ids_to_choose) > 0):
print(len(ids_to_choose))
str_arr = list(map(str, [years[i] for i in ids_to_choose]))
#print(str_arr)
print(' '.join(str_arr))
#print(' '.join([years[i] for i in ids_to_choose]))
else:
print('0')
#for i in range(len(array)):
# if array[i] > 0:
#print("-----")
arr_size = sys.stdin.readline()
``` | output | 1 | 65,931 | 14 | 131,863 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0 | instruction | 0 | 65,932 | 14 | 131,864 |
Tags: greedy
Correct Solution:
```
i=input
n=i()
m=list(map(int,i().split()))
t=1
p=[]
for x in range(len(m)):
if(t==m[x]):
p.append(2001+x)
t+=1
print(t-1)
for i in range(len(p)):
print(p[i],end=" ")
``` | output | 1 | 65,932 | 14 | 131,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
j = 1
ans = []
for i in range(len(a)):
if a[i] == j:
ans.append(2000 + i + 1)
j += 1
if len(ans) == 0:
print(0)
else:
print(len(ans))
print(*ans)
``` | instruction | 0 | 65,933 | 14 | 131,866 |
Yes | output | 1 | 65,933 | 14 | 131,867 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
_, values,i,years = int(input()),[int(i) for i in input().split(" ")],1,[]
if 1 in values:
years.append(values.index(1))
i=2
while i in values[years[-1]:]:
years.append(values[years[-1]:].index(i)+years[-1])
i+=1
print(len(years))
if len(years) !=0:
for year in years:
print(2001+year,end=" ")
print()
``` | instruction | 0 | 65,934 | 14 | 131,868 |
Yes | output | 1 | 65,934 | 14 | 131,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
n = int(input())
year = []
number = [0]
input_data = str(input())
data = list(map(lambda x: int(x),input_data.split(' ')))
for i in range(0,len(data)):
if data[i] == (number[-1])+1:
number.append(data[i])
year.append(2000+i+1)
if len(year) == 0:
print(0)
else:
print(len(year))
print(' '.join(str(e) for e in year))
``` | instruction | 0 | 65,935 | 14 | 131,870 |
Yes | output | 1 | 65,935 | 14 | 131,871 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
# from dust i have come dust i will be
n=int(input())
a=list(map(int,input().split()))
st=2000
x=1
ans=[]
for i in range(n):
if a[i]==x:
x+=1
ans.append(st+i+1)
print(len(ans))
for i in ans:
print(i,end=' ')
``` | instruction | 0 | 65,936 | 14 | 131,872 |
Yes | output | 1 | 65,936 | 14 | 131,873 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
n = int(input())
container = list(map(int, input().split()))
years = set()
if 1 in container:
indexOfOne = container.index(1)
years.add(2001 + indexOfOne)
while indexOfOne < len(container) - 1:
for j in range(indexOfOne + 1, len(container)):
if (container[indexOfOne] + 1 == container[j]):
years.add(2001 + j)
indexOfOne = j
indexOfOne -= 1
break
if j == len(container)-1:
break
indexOfOne += 1
print(len(years))
for item in years:
print(item, end=" ")
``` | instruction | 0 | 65,937 | 14 | 131,874 |
No | output | 1 | 65,937 | 14 | 131,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
import sys
n = int(sys.stdin.readline())
incomes = list(map(int,sys.stdin.readline().split()))
year = ""
max_l = 1
update = 0
for i in range(n):
count = 1
comp = incomes[i]
years = str(2000+i+1) + " "
for j in range(i+1,n):
current = incomes[j]
if current == comp+1:
count += 1
aux_year = 2000+j+1
years += str(aux_year)+ " "
comp += 1
if j == n:
count += 1
aux_year = 2000+j+2
years += str(aux_year)
if count > max_l:
max_l = count
year = years
update = 1
if update == 0:
print(0)
else:
print(max_l)
print(year)
``` | instruction | 0 | 65,938 | 14 | 131,876 |
No | output | 1 | 65,938 | 14 | 131,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
n = input()
s =input()
l = []
l = s.split()
k = []
for x in l:
k.append(int(x))
maximum = 0
result = []
prevVal = 1
for i in range (0,int(n)):
#print(type(k[i]))
if k[i] == prevVal:
prevVal += 1
result.append(2000 + i + 1)
maximum += 1
print(maximum)
if maximum > 0:
print(result)
``` | instruction | 0 | 65,939 | 14 | 131,878 |
No | output | 1 | 65,939 | 14 | 131,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β to a2 billion, ..., and in the current (2000 + n)-th year β an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the companyβs losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.
Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 β 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.
Input
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers ai ( - 100 β€ ai β€ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output
Output k β the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Examples
Input
10
-2 1 1 3 2 3 4 -10 -2 5
Output
5
2002 2005 2006 2007 2010
Input
3
-1 -2 -3
Output
0
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
Indices = []
l = 0
for i in range(n):
if (i+1) in A[l:]:
Indices.append(A[l:].index(i+1) + l)
l = A[l:].index(i+1) + 1 + l
else:
break
if not Indices:
print(0)
else:
for ind in Indices:
print(2001 + ind, end=' ')
``` | instruction | 0 | 65,940 | 14 | 131,880 |
No | output | 1 | 65,940 | 14 | 131,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,460 | 14 | 132,920 |
Tags: greedy, implementation, sortings
Correct Solution:
```
a=int(input())
l=sorted(list(map(int,input().split())))
k=0
if(len(l)>1):
for i in range(a-1):
k+=l[i+1]-l[i]-1
print(k)
else:
print("0")
``` | output | 1 | 66,460 | 14 | 132,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,461 | 14 | 132,922 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = int(input())
items = list(map(int, input().split()))
maxs=max(items)
mins=min(items)
print((maxs-mins+1)-n)
``` | output | 1 | 66,461 | 14 | 132,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,462 | 14 | 132,924 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
k=max(l)-min(l)+1
print(k-len(l))
``` | output | 1 | 66,462 | 14 | 132,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,463 | 14 | 132,926 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
res = a[-1] - a[0] + 1 - n
print(res)
``` | output | 1 | 66,463 | 14 | 132,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,464 | 14 | 132,928 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = eval(input())
x = list(map(int, input().split()))
x.sort()
a = x[0]
b = x[len(x)-1]
count = b-a+1 - len(x)
print(count)
``` | output | 1 | 66,464 | 14 | 132,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,465 | 14 | 132,930 |
Tags: greedy, implementation, sortings
Correct Solution:
```
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
a.sort()
k = 0 # ΠΊΠΎΠ» ΡΠΊΡΠ°Π΄Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ°Π²Π°ΡΠ°
for i in range(n - 1):
k += a[i + 1] - a[i] - 1
print(k)
``` | output | 1 | 66,465 | 14 | 132,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,466 | 14 | 132,932 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = int(input())
array = input().split(' ')
minv = int(array[0])
maxv = int(array[0])
for i in range(n):
if minv > int(array[i]):
minv = int(array[i])
if maxv < int(array[i]):
maxv = int(array[i])
res = maxv - minv + 1
res = res - n
print(res)
``` | output | 1 | 66,466 | 14 | 132,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x = 10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a_1, a_2, ..., a_n. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.
Input
The first line contains single integer n (1 β€ n β€ 1 000) β the number of keyboards in the store that remained after the heist.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^{9}) β the indices of the remaining keyboards. The integers a_i are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.
In the second example, if x=4 then nothing was stolen during the heist. | instruction | 0 | 66,467 | 14 | 132,934 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input());a=sorted(list(map(int,input().split())));print(sum([a[x]-a[x-1]-1 for x in range(1,n)]))
``` | output | 1 | 66,467 | 14 | 132,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,676 | 14 | 133,352 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
mod=998244353
def ans(A):
LEN=len(A)
ANS=0
for i in range(1<<LEN):
X=[0]*LEN
for j in range(LEN):
if i & (1<<j) != 0:
X[j]=1
C=[]
P=[]
SC=0
SP=0
for i in range(LEN):
if X[i]==1:
C.append(i)
SC+=A[i]
else:
P.append(i)
SP+=A[i]
if SC>=SP:
continue
flag=1
for i in range(2,len(C)):
if C[i-1]-C[i-2]>C[i]-C[i-1]:
flag=0
break
for i in range(2,len(P)):
if P[i-1]-P[i-2]<P[i]-P[i-1]:
flag=0
break
if flag==0:
continue
else:
ANS+=1
return ANS%mod
t=int(input())
for tests in range(t):
n=int(input())
A=list(map(int,input().split()))
S=[0]*(n+1)
S2=[0]*(n+1)
for i in range(n):
S[i+1]=A[i]+S[i]
S2[i+1]=A[i]+S2[i-1]
MAX=(S[-1]-1)//2
#print(S)
if n<=5:
print(ans(A))
continue
ANS=1 # all :P
# right-most
SUMR=A[-1]
ind=n-2
while SUMR<=MAX:
ANS+=1
SUMR+=A[ind]
ind-=1
#print(ANS)
# left-most, even, not use last
indl=0
if (n-1)%2==0:
indr=n-3
else:
indr=n-2
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# left-most, odd, not use last
indl=1
if (n-1)%2==0:
indr=n-2
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# left-most, even, use last
indl=0
if (n-1)%2==0:
indr=n-3
else:
indr=n-4
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# left-most, odd, use last
indl=1
if (n-1)%2==0:
indr=n-4
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]>MAX:
indr-=2
#print("!",indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, even, not use last
indl=2
if (n-1)%2==0:
indr=n-3
else:
indr=n-2
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, odd, not use last
indl=1
if (n-1)%2==0:
indr=n-2
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, even, use last
SUML=A[0]
indl=2
if (n-1)%2==0:
indr=n-3
else:
indr=n-4
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, odd, use last
SUML=A[0]
indl=1
if (n-1)%2==0:
indr=n-4
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
print(ANS%mod)
``` | output | 1 | 66,676 | 14 | 133,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,677 | 14 | 133,354 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
ans = 0
tot_sum = sum(a)
p_sum = 0
for i in a:
p_sum += i
if p_sum > tot_sum - p_sum:
ans += 1
alt_sum = [0]*(n+4)
for i in range(n):
alt_sum[i] = alt_sum[i-2] + a[i]
suffix_sum = [0]*(n+1)
for i in range(n-1,-1,-1):
suffix_sum[i] = suffix_sum[i+1] + a[i]
for start_p in [0, a[0]]:
valid_suffixes = [0] * (n+4)
pointer = [[2, 1], [2, 1]]
for i in range(n-1,-1,-1):
for p_end in range(2):
if p_end == 0 and i == n-1:
continue
if i > pointer[p_end][i&1]:
mx_sum_of_p = start_p + alt_sum[i-2] - alt_sum[pointer[p_end][i&1]-2] + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p:
next_sum_of_p = mx_sum_of_p + alt_sum[pointer[p_end][i&1]-2] - alt_sum[pointer[p_end][i&1]]
while pointer[p_end][i&1] + 2 <= i and next_sum_of_p > tot_sum - next_sum_of_p:
next_sum_of_p += alt_sum[pointer[p_end][i&1]] - alt_sum[pointer[p_end][i&1]+2]
pointer[p_end][i & 1] += 2
valid_suffixes[pointer[p_end][i&1]] += 1
else:
mx_sum_of_p = start_p + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p:
valid_suffixes[i] += 1
if i or start_p == 0:
ans += valid_suffixes[i+1]
valid_suffixes[i-2] += valid_suffixes[i]
print(ans % 998244353)
``` | output | 1 | 66,677 | 14 | 133,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,678 | 14 | 133,356 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
ans = 0
# case of PPPPPPCCCCCCC
tot_sum = sum(a)
p_sum = 0
for i in a:
p_sum += i
if p_sum > tot_sum - p_sum:
ans += 1
# case of P/C CCCCCC... PCPCPC... PPPPP... P/C
alt_sum = [0]*(n+4) # n+4 so that negative indexing becomes default 0
for i in range(n):
alt_sum[i] = alt_sum[i-2] + a[i]
suffix_sum = [0]*(n+1)
for i in range(n-1,-1,-1):
suffix_sum[i] = suffix_sum[i+1] + a[i]
for start_p in [0, a[0]]:
valid_suffixes = [0] * (n+4)
pointer = [[2, 1], [2, 1]] # has p at end, parity of leftbound
for i in range(n-1,-1,-1): # iterate over the prefix of C's from right to left
for p_end in range(2):
if p_end == 0 and i == n-1: # suffix from i is supposed to have at least 1 P
continue
if i > pointer[p_end][i&1]:
# first element if it is P, alternating sum from pointer to i-2, suffix sum - last element if it is C
mx_sum_of_p = start_p + alt_sum[i-2] - alt_sum[pointer[p_end][i&1]-2] + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p: # otherwise this will never be valid (have large enough sum from P's)
# find first pos i from right where sum of p's > sum of c's
next_sum_of_p = mx_sum_of_p + alt_sum[pointer[p_end][i&1]-2] - alt_sum[pointer[p_end][i&1]] # update
while pointer[p_end][i&1] + 2 <= i and next_sum_of_p > tot_sum - next_sum_of_p:
next_sum_of_p += alt_sum[pointer[p_end][i&1]] - alt_sum[pointer[p_end][i&1]+2] # update
pointer[p_end][i & 1] += 2 # since next_sum worked, update pointer
valid_suffixes[pointer[p_end][i&1]] += 1 # earliest position
else:
mx_sum_of_p = start_p + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p:
valid_suffixes[i] += 1 # earliest position
if i or start_p == 0:
ans += valid_suffixes[i+1] # since every suffix at index i starts with a P
valid_suffixes[i-2] += valid_suffixes[i] # carrying over just like a suffix sum
print(ans % 998244353)
``` | output | 1 | 66,678 | 14 | 133,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,679 | 14 | 133,358 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys,io,os
try:Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
except:Z=lambda:sys.stdin.readline().encode()
Y=lambda:map(int,Z().split())
from bisect import bisect
M=998244353
def add(a,b):
v=a+b
if v>=M:v-=M
if v<0:v+=M
return v
def sol(n,a,disp=0):
if n<2:return 1
if n<3:return 2-(a[0]==a[1])
x0=[0];x1=[0];z=1;y=a[-1];s=sum(a)
for i in range(n):
if i&1:x1.append(x1[-1]+a[i])
else:x0.append(x0[-1]+a[i])
c=(s+1)//2
while y<c:z+=1;y+=a[-z]
if disp:print(c)
for i in range(n-1):
if i&1:
aps=x0[i//2+1]
if aps+x1[i//2+1]>=c:break
j=bisect(x1,c-aps-1)
if(n&1)<1:
if j==n//2+1:j-=1
else:
aps=x1[i//2]
if aps+x0[i//2+1]>=c:break
j=bisect(x0,c-aps-1)
if n&1:
if j==n//2+2:j-=1
z=add(z,j-i//2-1)
if disp:print(i,j-i//2-1)
c-=a[-1]
if disp:print(c)
for i in range(n-2):
if i&1:
aps=x0[i//2+1]
if aps+x1[i//2+1]>=c:break
j=bisect(x1,c-aps-1)
if n&1:
if j==n//2+1:j-=1
else:
if j==n//2+1:j-=1
else:
aps=x1[i//2]
if aps+x0[i//2+1]>=c:break
j=bisect(x0,c-aps-1)
if n&1:
if j==n//2+2:j-=1
else:
if j==n//2+1:j-=1
z=add(z,j-i//2-1)
if disp:print(i,j-i//2-1)
c+=a[0]
if disp:print(c)
for i in range(1,n-2):
if i&1:
aps=x0[i//2+1]
if aps+x1[i//2+1]>=c:break
j=bisect(x1,c-aps-1)
if n&1:
if j==n//2+1:j-=1
else:
if j==n//2+1:j-=1
else:
aps=x1[i//2]
if aps+x0[i//2+1]>=c:break
j=bisect(x0,c-aps-1)
if n&1:
if j==n//2+2:j-=1
else:
if j==n//2+1:j-=1
z=add(z,j-i//2-1)
if disp:print(i,j-i//2-1)
c+=a[-1]
if disp:print(c)
for i in range(1,n-1):
if i&1:
aps=x0[i//2+1]
if aps+x1[i//2+1]>=c:break
j=bisect(x1,c-aps-1)
if(n&1)<1:
if j==n//2+1:j-=1
else:
aps=x1[i//2]
if aps+x0[i//2+1]>=c:break
j=bisect(x0,c-aps-1)
if n&1:
if j==n//2+2:j-=1
z=add(z,j-i//2-1)
if disp:print(i,j-i//2-1)
if disp:print(z)
return z
O=[]
for _ in range(int(Z())):O.append(str(sol(int(Z()),[*Y()])))
print('\n'.join(O))
``` | output | 1 | 66,679 | 14 | 133,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,680 | 14 | 133,360 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
ans = 0
# case of PPPPPPCCCCCCC
tot_sum = sum(a)
p_sum = 0
for i in a:
p_sum += i
if p_sum > tot_sum - p_sum:
ans += 1
# case of P/C CCCCCC... PCPC... PPPPP... P/C
alt_sum = [0]*(n+4) # n+4 so that negative indexing becomes default 0
for i in range(n):
alt_sum[i] = alt_sum[i-2] + a[i]
suffix_sum = [0]*(n+1)
for i in range(n-1,-1,-1):
suffix_sum[i] = suffix_sum[i+1] + a[i]
for start_p in [0, a[0]]:
valid_suffixes = [0] * (n+4)
pointer = [[2, 1], [2, 1]] # has p at end, parity of leftbound
for i in range(n-1,-1,-1): # iterate over the prefix of C's from right to left
for p_end in range(2):
if p_end == 0 and i == n-1: # suffix from i is supposed to have at least 1 P
continue
if i > pointer[p_end][i&1]:
# first element if it is P, alternating sum from pointer to i-2, suffix sum - last element if it is C
mx_sum_of_p = start_p + alt_sum[i-2] - alt_sum[pointer[p_end][i&1]-2] + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p: # otherwise this will never be valid (have large enough sum from P's)
# find first pos i from right where sum of p's > sum of c's
next_sum_of_p = mx_sum_of_p + alt_sum[pointer[p_end][i&1]-2] - alt_sum[pointer[p_end][i&1]] # update
while pointer[p_end][i&1] + 2 <= i and next_sum_of_p > tot_sum - next_sum_of_p:
next_sum_of_p += alt_sum[pointer[p_end][i&1]] - alt_sum[pointer[p_end][i&1]+2] # update
pointer[p_end][i & 1] += 2 # since next_sum worked, update pointer
valid_suffixes[pointer[p_end][i&1]] += 1 # earliest position
else:
mx_sum_of_p = start_p + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p:
valid_suffixes[i] += 1 # earliest position
if i or start_p == 0:
ans += valid_suffixes[i+1] # since every suffix at index i starts with a P
valid_suffixes[i-2] += valid_suffixes[i] # carrying over just like a suffix sum
print(ans % 998244353)
``` | output | 1 | 66,680 | 14 | 133,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,681 | 14 | 133,362 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n = int(input()); a = list(map(int,input().split())); ans = 0; tot_sum = sum(a); p_sum = 0
for i in a:
p_sum += i
if p_sum > tot_sum - p_sum: ans += 1
alt_sum = [0]*(n+4)
for i in range(n): alt_sum[i] = alt_sum[i-2] + a[i]
suffix_sum = [0]*(n+1)
for i in range(n-1,-1,-1): suffix_sum[i] = suffix_sum[i+1] + a[i]
for start_p in [0, a[0]]:
valid_suffixes = [0] * (n+4); pointer = [[2, 1], [2, 1]]
for i in range(n-1,-1,-1):
for p_end in range(2):
if p_end == 0 and i == n-1: continue
if i > pointer[p_end][i&1]:
mx_sum_of_p = start_p + alt_sum[i-2] - alt_sum[pointer[p_end][i&1]-2] + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p:
next_sum_of_p = mx_sum_of_p + alt_sum[pointer[p_end][i&1]-2] - alt_sum[pointer[p_end][i&1]]
while pointer[p_end][i&1] + 2 <= i and next_sum_of_p > tot_sum - next_sum_of_p: next_sum_of_p += alt_sum[pointer[p_end][i&1]] - alt_sum[pointer[p_end][i&1]+2]; pointer[p_end][i & 1] += 2
valid_suffixes[pointer[p_end][i&1]] += 1
else:
mx_sum_of_p = start_p + suffix_sum[i] - (p_end^1)*a[-1]
if mx_sum_of_p > tot_sum - mx_sum_of_p: valid_suffixes[i] += 1
if i or start_p == 0: ans += valid_suffixes[i+1]
valid_suffixes[i-2] += valid_suffixes[i]
print(ans % 998244353)
``` | output | 1 | 66,681 | 14 | 133,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.
Let C=\\{c_1,c_2,...,c_m\} (c_1<c_2<β¦ <c_m) be the set of people who hold cardboards of 'C'. Let P=\\{p_1,p_2,...,p_k\} (p_1<p_2<β¦ <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints:
1. Cβͺ P=\{1,2,...,n\}
2. Cβ© P =β
.
3. c_i-c_{i-1}β€ c_{i+1}-c_i(1< i <m).
4. p_i-p_{i-1}β₯ p_{i+1}-p_i(1< i <k).
Given an array a_1,β¦, a_n, please find the number of good photos satisfying the following condition: $$$β_{xβ C} a_x < β_{yβ P} a_y.$$$
The answer can be large, so output it modulo 998 244 353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 200 000). Description of the test cases follows.
The first line of each test case contains a single integer n (1β€ nβ€ 200 000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 200 000.
Output
For each test case, output the answer modulo 998 244 353 in a separate line.
Example
Input
3
5
2 1 2 1 1
4
9 2 2 2
1
998244353
Output
10
7
1
Note
For the first test case, there are 10 possible good photos satisfying the condition: PPPPP, CPPPP, PCPPP, CCPPP, PCCPP, PCPCP, PPPPC, CPPPC, PCPPC, PPPCC.
For the second test case, there are 7 possible good photos satisfying the condition: PPPP, PCPP, PCCP, PPPC, PCPC, PPCC, PCCC. | instruction | 0 | 66,682 | 14 | 133,364 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
mod=998244353
def ans(A):
LEN=len(A)
ANS=0
for i in range(1<<LEN):
X=[0]*LEN
for j in range(LEN):
if i & (1<<j) != 0:
X[j]=1
C=[]
P=[]
SC=0
SP=0
for i in range(LEN):
if X[i]==1:
C.append(i)
SC+=A[i]
else:
P.append(i)
SP+=A[i]
if SC>=SP:
continue
flag=1
for i in range(2,len(C)):
if C[i-1]-C[i-2]>C[i]-C[i-1]:
flag=0
break
for i in range(2,len(P)):
if P[i-1]-P[i-2]<P[i]-P[i-1]:
flag=0
break
if flag==0:
continue
else:
ANS+=1
return ANS%mod
ALIST=[]
t=int(input())
for tests in range(t):
n=int(input())
A=list(map(int,input().split()))
S=[0]*(n+1)
S2=[0]*(n+1)
for i in range(n):
S[i+1]=A[i]+S[i]
S2[i+1]=A[i]+S2[i-1]
MAX=(S[-1]-1)//2
#print(S)
if n<=5:
ALIST.append(ans(A))
continue
ANS=1 # all :P
# right-most
SUMR=A[-1]
ind=n-2
while SUMR<=MAX:
ANS+=1
SUMR+=A[ind]
ind-=1
#print(ANS)
# left-most, even, not use last
indl=0
if (n-1)%2==0:
indr=n-3
else:
indr=n-2
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# left-most, odd, not use last
indl=1
if (n-1)%2==0:
indr=n-2
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# left-most, even, use last
indl=0
if (n-1)%2==0:
indr=n-3
else:
indr=n-4
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# left-most, odd, use last
indl=1
if (n-1)%2==0:
indr=n-4
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]>MAX:
indr-=2
#print("!",indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, even, not use last
indl=2
if (n-1)%2==0:
indr=n-3
else:
indr=n-2
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, odd, not use last
indl=1
if (n-1)%2==0:
indr=n-2
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, even, use last
SUML=A[0]
indl=2
if (n-1)%2==0:
indr=n-3
else:
indr=n-4
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
#print(ANS)
# PCCCCC, left-most, odd, use last
SUML=A[0]
indl=1
if (n-1)%2==0:
indr=n-4
else:
indr=n-3
while indl<=indr:
while indl<=indr and S2[indr+1]+S[indl+1]-S2[indl+1]+A[-1]-A[0]>MAX:
indr-=2
#print(indl,indr)
if indl<=indr:
ANS+=(indr-indl)//2+1
indl+=2
ALIST.append(ANS%mod)
sys.stdout.write("\n".join(map(str,ALIST)))
``` | output | 1 | 66,682 | 14 | 133,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,771 | 14 | 133,542 |
Tags: *special, implementation
Correct Solution:
```
a = list(input().split())
n, T, c = int(a[0]), int(a[1]), float(a[2])
a = list(map(int, input().split()))
m = int(input())
p = list(map(int, input().split()))
sum_a = sum(a[:T - 1])
mean = 0
for t in range(T - 1):
mean = (mean + a[t]/T) / c
i = 0
for t in range(T - 1, p[-1]):
sum_a += a[t]
mean = (mean + a[t]/T) / c
if t == p[i] - 1:
real = sum_a / T
print('%0.6f %0.6f %0.6f' % (real, mean, abs(real - mean) / real))
i += 1
if i == len(p) : break
sum_a -= a[t - T + 1]
``` | output | 1 | 66,771 | 14 | 133,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,772 | 14 | 133,544 |
Tags: *special, implementation
Correct Solution:
```
__author__ = 'ruckus'
n, T, c = input().split()
n = int(n)
T = int(T)
c = float(c)
a = list(map(int, input().split()))
m = int(input())
q = list(map(int, input().split()))
res_a = 0
real = 0
maxi_q = max(q)
q_n = 0
for i in range(q[-1]):
res_a = (res_a + a[i] / T) / c
real += a[i]
if i >= T:
real -= a[i-T]
if q[q_n] == i+1:
q_n += 1
r = real/T
print(r, res_a, abs(r-res_a)/r)
# Made By Mostafa_Khaled
``` | output | 1 | 66,772 | 14 | 133,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,773 | 14 | 133,546 |
Tags: *special, implementation
Correct Solution:
```
import math
s=input().split(' ')
n,tm,k=int(s[0]),int(s[1]),float(s[2])
rq=[int(c) for c in input().split(' ')]
m=int(input())
cr={int(c) for c in input().split(' ')}
sr,ma=0,0
alv=[]
for c,t in enumerate(rq):
ma=(ma+t/tm)/k
sr+=t
l=c-tm
l=rq[l] if l>=0 else 0
sr-=l
if (c+1) in cr:
mr=sr/tm
df=abs((ma-mr)/mr)
alv.append((mr,ma,df))
for c in alv:
print(c[0],c[1],c[2])
``` | output | 1 | 66,773 | 14 | 133,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,774 | 14 | 133,548 |
Tags: *special, implementation
Correct Solution:
```
n, t, c = input().split()
n = int(n)
t = int(t)
c = float(c)
a = list(map(int, input().split()))
m = int(input())
p = list(map(int, input().split()))
sums = [a[0]]
for i in range(1, n):
sums.append(sums[i - 1] + a[i])
approx = [a[0] / (t * c)]
for i in range(1, n):
approx.append((approx[i - 1] + a[i] / t) / c)
for i in range(m):
real = (sums[p[i] - 1] - (0 if p[i] == t else sums[p[i] - t - 1])) / t
appr = (approx[p[i] - 1]) # - (0 if p[i] == t else approx[p[i] - t - 1] / (c ** t)))
print(real, appr, abs(real - appr) / real)
``` | output | 1 | 66,774 | 14 | 133,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,775 | 14 | 133,550 |
Tags: *special, implementation
Correct Solution:
```
__author__ = 'ruckus'
n, T, c = input().split()
n = int(n)
T = int(T)
c = float(c)
a = list(map(int, input().split()))
m = int(input())
q = list(map(int, input().split()))
res_a = 0
real = 0
maxi_q = max(q)
q_n = 0
for i in range(q[-1]):
res_a = (res_a + a[i] / T) / c
real += a[i]
if i >= T:
real -= a[i-T]
if q[q_n] == i+1:
q_n += 1
r = real/T
print(r, res_a, abs(r-res_a)/r)
``` | output | 1 | 66,775 | 14 | 133,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,776 | 14 | 133,552 |
Tags: *special, implementation
Correct Solution:
```
def main():
s = input().split()
n,T,c = int(s[0]), int(s[1]), float(s[2])
a = list(map(int, input().split()))
m = int(input())
q = list(map(int, input().split()))
sumA, approx, mean = [0], [], 0.
for i in range(1, n+1):
mean = (mean+a[i-1]/T)/c
approx.append(mean)
sumA.append(a[i-1] + sumA[i-1])
ans = [(sumA[q[i]]-sumA[q[i]-T])/T for i in range(m)]
for i in range(m):
print('%.6f' % ans[i], '%.6f' % approx[q[i]-1], '%.6f' % (abs(approx[q[i]-1]- ans[i])/ans[i]))
if __name__ == '__main__':
main()
``` | output | 1 | 66,776 | 14 | 133,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,777 | 14 | 133,554 |
Tags: *special, implementation
Correct Solution:
```
n, T, c = input().split()
n = int(n)
T = int(T)
c = float(c)
a = list(map(int, input().split()))
m = int(input())
q = list(map(int, input().split()))
res_a = 0
real = 0
maxi_q = max(q)
q_n = 0
for i in range(q[-1]):
res_a = (res_a + a[i] / T) / c
real += a[i]
if i >= T:
real -= a[i-T]
if q[q_n] == i+1:
q_n += 1
r = real/T
print(r, res_a, abs(r-res_a)/r)
``` | output | 1 | 66,777 | 14 | 133,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 β€ t β€ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.
// setting this constant value correctly can adjust
// the time range for which statistics will be calculated
double c = some constant value;
// as the result of the algorithm's performance this variable will contain
// the mean number of queries for the last
// T seconds by the current moment of time
double mean = 0.0;
for t = 1..n: // at each second, we do the following:
// at is the number of queries that came at the last second;
mean = (mean + at / T) / c;
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 β€ x β€ t.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given n values at, integer T and real number c. Also, you are given m moments pj (1 β€ j β€ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula <image>. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula <image>, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.
Input
The first line contains integer n (1 β€ n β€ 2Β·105), integer T (1 β€ T β€ n) and real number c (1 < c β€ 100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.
The next line contains n integers at (1 β€ at β€ 106) β the number of queries to the service at each moment of time.
The next line contains integer m (1 β€ m β€ n) β the number of moments of time when we are interested in the mean number of queries for the last T seconds.
The next line contains m integers pj (T β€ pj β€ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.
Output
Print m lines. The j-th line must contain three numbers real, approx and error, where:
* <image> is the real mean number of queries for the last T seconds;
* approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);
* <image> is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.
Examples
Input
1 1 2.000000
1
1
1
Output
1.000000 0.500000 0.500000
Input
11 4 1.250000
9 11 7 5 15 6 6 6 6 6 6
8
4 5 6 7 8 9 10 11
Output
8.000000 4.449600 0.443800
9.500000 6.559680 0.309507
8.250000 6.447744 0.218455
8.000000 6.358195 0.205226
8.250000 6.286556 0.237993
6.000000 6.229245 0.038207
6.000000 6.183396 0.030566
6.000000 6.146717 0.024453
Input
13 4 1.250000
3 3 3 3 3 20 3 3 3 3 3 3 3
10
4 5 6 7 8 9 10 11 12 13
Output
3.000000 1.771200 0.409600
3.000000 2.016960 0.327680
7.250000 5.613568 0.225715
7.250000 5.090854 0.297813
7.250000 4.672684 0.355492
7.250000 4.338147 0.401635
3.000000 4.070517 0.356839
3.000000 3.856414 0.285471
3.000000 3.685131 0.228377
3.000000 3.548105 0.182702 | instruction | 0 | 66,778 | 14 | 133,556 |
Tags: *special, implementation
Correct Solution:
```
#!/usr/bin/python3.5
import math
mean=[0.0]*200010
real=[0.0]*200010
n,T,c=input().split()
n=int(n)
T=int(T)
c=float(c)
t=T
a=[0]+[int(x) for x in input().split()]
for i in range(1,n+1):
mean[i]=(mean[i-1]+(a[i]/T))/c
real[i]=real[i-1]+a[i]
m=int(input())
q=[int(x) for x in input().split()]
for i in range(m):
r=(real[q[i]]-real[q[i]-t])/T
ap=mean[q[i]]
print('{:.6f} {:.6f} {:.6f}'.format(r,ap,math.fabs(ap-r)/r))
``` | output | 1 | 66,778 | 14 | 133,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,937 | 14 | 133,874 |
Tags: implementation, strings
Correct Solution:
```
n = int( input() )
L = []
m = {}
names = []
for i in range( n ):
a = input().split()
name = a[0]
names.append( a[0] )
a = a[2:]
if name not in m:
m[ name ] = []
for j in a:
m[ name ].append( j )
names = set( names )
for i in names:
#print( i )
a = list( set(m[i] ) )
#print( a )
good = [True] * len(a)
for j in range( len(a) ):
for k in range( len(a) ):
if j != k:
if a[k].endswith( a[j] ):
good[j] = False
break
ok = False
cnt = 0
for j in range( len(a) ):
if good[j] == True:
cnt += 1
ok = True
if ok == False:
continue
out = i + " " + str(cnt)
for j in range( len(a) ):
if good[j]:
out += " " + a[j]
L.append( out )
print( len(L) )
for i in L:
print( i )
exit(0)
``` | output | 1 | 66,937 | 14 | 133,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,938 | 14 | 133,876 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
d = {}
for i in range(n):
s = list(str(input()).split(" "))
if s[0] not in d:
d[s[0]] = []
for i in range(2, 2+int(s[1])):
d[s[0]] += [s[i][::-1]]
print(len(d))
for name in d:
ph = sorted(d[name])
ans = []
for i in range(len(ph)-1):
li = len(ph[i])
lj = len(ph[i+1])
if li > lj or ph[i] != ph[i+1][:li]:
ans += [ph[i][::-1]]
ans += [ph[-1][::-1]]
print(name, len(ans), " ".join(ans))
``` | output | 1 | 66,938 | 14 | 133,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,939 | 14 | 133,878 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
friends = {}
for _ in range(n):
data = input().split()
name = data[0]
numbers = set(data[2:])
if name in friends:
friends[name].update(numbers)
else:
friends[name] = numbers
print(len(friends))
for friend, numbers in friends.items():
used = numbers.copy()
for x in numbers:
for y in numbers:
if x == y:
continue
if x not in used or y not in used:
continue
if y.endswith(x):
used.remove(x)
print(friend, len(used), *used)
``` | output | 1 | 66,939 | 14 | 133,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,940 | 14 | 133,880 |
Tags: implementation, strings
Correct Solution:
```
# c451
n = int(input())
entries = []
for i in range(n):
entries.append(input().split())
book = {}
for e in entries:
if e[0] in book:
numbers = book[e[0]]
for non_org_number in e[2:]:
issuffix = False
isselfsuffix = False
selfindex = 0
for i in range(len(numbers)):
if numbers[i].endswith(non_org_number):
issuffix = True
break
if non_org_number.endswith(numbers[i]):
isselfsuffix = True
selfindex = i
break
if not issuffix:
if not isselfsuffix:
numbers.append(non_org_number)
if isselfsuffix:
numbers[selfindex] = non_org_number
book[e[0]] = numbers
else:
numbers = []
for non_org_number in e[2:]:
issuffix = False
isselfsuffix = False
selfindex = 0
for i in range(len(numbers)):
if numbers[i].endswith(non_org_number):
issuffix = True
break
if non_org_number.endswith(numbers[i]):
isselfsuffix = True
selfindex = i
break
if not issuffix:
if not isselfsuffix:
numbers.append(non_org_number)
if isselfsuffix:
numbers[selfindex] = non_org_number
book[e[0]] = numbers
print(len(book))
for e in book:
print(e, len(book[e]), " ".join(book[e]))
``` | output | 1 | 66,940 | 14 | 133,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,941 | 14 | 133,882 |
Tags: implementation, strings
Correct Solution:
```
phone_book = {}
friends = 0
n = int(input().strip())
for _ in range(n):
record = input().strip().split()
name = record[0]
num = int(record[1])
if name not in phone_book:
phone_book[name] = []
friends += 1
for i in range(num):
c = record[i + 2]
flag = True
if record[i + 2] in phone_book[name]:
continue
else:
l1 = len(phone_book[name])
for i in range(len(phone_book[name])):
if i == l1:
break
contact = phone_book[name][i]
if(len(contact) > len(c)):
if contact[-1 * len(c):] == c:
flag = False
else:
if c[-1 * len(contact):] == contact:
phone_book[name].remove(contact)
l1 -= 1
i -= 1
if(flag):
phone_book[name].append(c)
print(friends)
for contact in phone_book:
ans = ""
ans += contact
ans += " " + str(len(phone_book[contact]))
for i in phone_book[contact]:
ans += " " + i
print(ans)
``` | output | 1 | 66,941 | 14 | 133,883 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,942 | 14 | 133,884 |
Tags: implementation, strings
Correct Solution:
```
def suff(a, b):
if a == b:
return False
else:
return len(b) > len(a) and b[len(b) - len(a):] == a
n = int(input())
a = {}
for i in range(n):
s = input().split()
name = s[0]
nums = s[2:]
if name in a.keys():
a[name].extend(nums)
else:
a[name] = nums
for name in a.keys():
nums = []
for i in a[name]:
if i not in nums:
fl = False
for j in a[name]:
if suff(i, j):
fl = True
if not fl:
nums.append(i)
a[name] = nums
print(len(a.keys()))
for name in a.keys():
print(name, len(a[name]), end=' ')
for i in a[name]:
print(i, end=' ')
print()
``` | output | 1 | 66,942 | 14 | 133,885 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,943 | 14 | 133,886 |
Tags: implementation, strings
Correct Solution:
```
def fltr(tels):
ts = list(tels)
for i, t in enumerate(ts):
l = len(t)
for i2, t2 in enumerate(ts):
if i == i2: continue
if t == t2 and i < i2:
tels.remove(t)
break
if t != t2 and t2[-l:] == t:
tels.remove(t)
break
t = int(input())
names = {}
for _ in range(t):
e = input().split()
if not e[0] in names:
names[e[0]] = []
for tttt in e[2:]:
names[e[0]].append(tttt)
for tels in names.values():
fltr(tels)
print(len(names))
for name, tels in names.items():
print(' '.join([name, str(len(tels)), ' '.join(tels)]))
``` | output | 1 | 66,943 | 14 | 133,887 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456 | instruction | 0 | 66,944 | 14 | 133,888 |
Tags: implementation, strings
Correct Solution:
```
def check_suffix(s1, s2): # checks if s1 is suffix of s2
l1 = len(s1)
l2 = len(s2)
if l1 >= l2:
return 0
if s2[-l1 : ] == s1:
return 1
return 0
def no_suffix(ls):
ls2 = []
for i in range(len(ls)):
for j in range(len(ls)):
if i == j:
continue
if check_suffix(ls[i], ls[j]) == 1:
ls2.append(ls[i])
for i in ls2:
if i in ls:
ls.remove(i)
return ls
n = int(input())
d = {}
for i in range(n):
ls = input().split()
if ls[0] not in d:
d[ls[0]] = set(ls[2:])
else:
d[ls[0]] |= set(ls[2:])
print(len(d))
for i in d:
print(i, end = ' ')
ls = no_suffix(list(d[i]))
print(len(ls), end = ' ')
for i in ls:
print(i, end = ' ')
print()
``` | output | 1 | 66,944 | 14 | 133,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
n = int(input().strip())
dct = dict()
for i in range(n):
arr = list(input().strip().split())
name = arr[0]
arr = arr[2:]
if name in dct:
dct[name] = dct[name].union( set(arr) )
else:
dct[name] = set(arr)
for name in dct.keys():
tl = list(dct[name])
# tl = [ int(i) for i in tl ]
# sorted(tl)
# tl = tl[::-1]
# tl = [ str(i) for i in tl ]
tmp = [ 1 for _ in range(len(tl)) ]
for i in range(len(tl)):
for j in range(i+1,len(tl)):
if len( tl[i] ) > len( tl[j] ) and tl[i].endswith( tl[j] ):
tmp[j] = 0
elif len( tl[i] ) < len( tl[j] ) and tl[j].endswith( tl[i] ):
tmp[i] = 0
lst = []
for i in range(len(tmp)):
if( tmp[i]==1 ):
lst.append( tl[i] )
dct[name] = lst
print(len(dct))
for name in dct.keys():
s = name + ' ' + str(len(dct[name])) + ' '
x = " ".join(dct[name])
print(s+x)
``` | instruction | 0 | 66,945 | 14 | 133,890 |
Yes | output | 1 | 66,945 | 14 | 133,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
# Main maut ko takiya, aur kafan ko chaadar banakar audhta hoon!
n=int(input())
d={}
for i in range(n):
a=input().split()
if a[0] not in d:
d[a[0]]=[]
for j in a[2:]:
d[a[0]].append(j)
else:
for j in a[2:]:
d[a[0]].append(j)
print(len(d.keys()))
for i in d.keys():
a=[]
b=d[i]
j=0
k=0
while j<len(b):
l=len(b[j])
k=0
while(k<len(b)):
if j!=k:
#print(j,k,b[j],b[k],l,b)
if len(b[k])>=l:
# print(b[k][-1*l:],b[j])
if b[k][-1*l:]==b[j]:
b[j]="a"
break
k+=1
j+=1
a=[]
for k in b:
if k!="a":
a.append(k)
print(i,len(a),*a)
``` | instruction | 0 | 66,946 | 14 | 133,892 |
Yes | output | 1 | 66,946 | 14 | 133,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
# link: https://codeforces.com/problemset/problem/898/C
import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from math import ceil
mod = 10 ** 9 + 7
def get_suffix(numbers):
l = ["" for _ in range(len(numbers))]
for i in range(len(numbers) - 1, -1, -1):
if i == len(numbers) - 1:
l[i] = numbers[i]
else:
l[i] = numbers[i] + l[i+1]
return l
def get_numbers_count(x):
count = 0
for k in x:
if x[k]: count += 1
return (count)
# number of test cases
for _ in range(1):
n = int(input())
info = {}
for i in range(n):
info1 = input().split(" ")
name = info1[0]
if name not in info:
info[name] = {}
for numbers in info1[2:]:
if numbers not in info[name]:
info[name][numbers] = get_suffix(numbers)
for name, values in info.items():
for num, suffixes in info[name].items():
if info[name][num] != 0:
cn = num
for num1, suffixes1 in info[name].items():
if info[name][num1] != 0 and num1 != cn and cn in suffixes1:
info[name][cn] = 0
print(get_numbers_count(info))
for name in info:
print(name, get_numbers_count(info[name]), end = " ")
for k in info[name]:
if info[name][k] != 0:
print(k, end = " ")
print()
``` | instruction | 0 | 66,947 | 14 | 133,894 |
Yes | output | 1 | 66,947 | 14 | 133,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
n = int(input())
a = []
names = set()
numbers = [ [] for i in range(21)]
for i in range(n):
l = list(input().split())
names.add(l[0])
a.append(l)
names = list(names)
for index, i in enumerate(names):
for j in a:
if j[0] == i:
for k in range(2, len(j)):
numbers[index].append(j[k])
for index, i in enumerate(names):
numbers[index] = list(set(numbers[index]))
for a in range(len(numbers[index])):
for b in range(len(numbers[index])):
str_a = str(numbers[index][a])
str_b = str(numbers[index][b])
if str_a == str_b and a != b:
numbers[index][a] = "x"
elif a != b and str_b[len(str_b) - len(str_a):len(str_b)] == str_a:
numbers[index][a] = "x"
print(len(names))
for index, i in enumerate(names):
var = []
for j in range(len(numbers[index])):
if numbers[index][j] != "x":
var.append(str(numbers[index][j]))
print(names[index], len(var), " ".join(var))
``` | instruction | 0 | 66,948 | 14 | 133,896 |
Yes | output | 1 | 66,948 | 14 | 133,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
c = int(input())
d = {}
for i in range(c):
s = input().split()
x = set(s[2:])
q = []
for m in x:
q.append(m)
d[s[0]] = []
for j in range(len(q)):
a = q[j]
z = 0
for k in range(len(q)):
b = q[k]
if j!=k and b.find(a)!=-1:
if len(b) - b.find(a) == len(a):
z = 1
break
if z == 0:
d[s[0]].append(a)
print(len(d))
for i in d:
print(i,len(d[i]),end = ' ')
for j in range(len(d[i])):
print((d[i][j]))
if len(d[i])-1 !=j:
print(end = ' ')
``` | instruction | 0 | 66,949 | 14 | 133,898 |
No | output | 1 | 66,949 | 14 | 133,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
n = int(input())
a = []
names = set()
numbers = [ [] for i in range(n)]
for i in range(n):
l = list(input().split())
names.add(l[0])
a.append(l)
names = list(names)
for index, i in enumerate(names):
for j in a:
if j[0] == i:
for k in range(2, len(j)):
numbers[index].append(j[k])
for index, i in enumerate(names):
numbers[index] = list(set(numbers[index]))
numbers[index].sort()
for a in range(len(numbers[index])):
for b in range(len(numbers[index])):
str_a = str(numbers[index][a])
str_b = str(numbers[index][b])
if len(str_a) < len(str_b):
if str_b[len(str_b) - len(str_a):len(str_b)] == str_a:
numbers[index][a] = -1
print(len(names))
for index, i in enumerate(names):
var = []
for j in range(len(numbers[index])):
if numbers[index][j] != -1:
var.append(str(numbers[index][j]))
print(names[index], len(var), " ".join(var))
``` | instruction | 0 | 66,950 | 14 | 133,900 |
No | output | 1 | 66,950 | 14 | 133,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
n = int(input())
t = {}
while n:
n-= 1
l = input().split()
name = l[0]
try:
tmp = t[name]
except KeyError:
tmp = []
l = list(set(l[2:] + tmp))
l.sort(key=len, reverse=True)
cur = [l[0]]
for i in l[1:]:
try:
for j in cur:
if j.endswith(i): 3/0
cur.append(i)
except ZeroDivisionError:
continue
t[name] = cur
for key in t.keys():
print(key,len(t[key]), ' '.join([str(i) for i in t[key]]))
``` | instruction | 0 | 66,951 | 14 | 133,902 |
No | output | 1 | 66,951 | 14 | 133,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.
Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.
Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.
The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.
Read the examples to understand statement and format of the output better.
Input
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books.
The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Output
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.
The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.
Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456
Submitted Solution:
```
used=[]
def organize(inp):
name=inp[0]
records=inp
res = []
for l in records:
push = True
for k in records:
# print(l,k)
index=min(len(l), len(k))
#print(k[-index:])
if l is k[-index:] and k is not l:
push = False
if k not in res and k not in used:
res.append(k)
used.append(k)
if l not in res and push == True and l not in used:
res.append(l)
used.append(l)
final_res = [name, str(len(res))] + res
print(' '.join(final_res))
return
def solution():
#n=int(input())
#f=open('input.txt', 'r')
n=int(input())
d={}
for l in range(n):
inp = [a for a in input().split(' ')]
name=inp[0]
record=inp[2:]
if name in d:
d[name]+=record
else:
d[name]=record
for k in d:
#print(d[k])
organize([k]+d[k])
#organize(['dasha'] + d['dasha'])
solution()
``` | instruction | 0 | 66,952 | 14 | 133,904 |
No | output | 1 | 66,952 | 14 | 133,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob watches TV every day. He always sets the volume of his TV to b. However, today he is angry to find out someone has changed the volume to a. Of course, Bob has a remote control that can change the volume.
There are six buttons (-5, -2, -1, +1, +2, +5) on the control, which in one press can either increase or decrease the current volume by 1, 2, or 5. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than 0.
As Bob is so angry, he wants to change the volume to b using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given a and b, finds the minimum number of presses to change the TV volume from a to b.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1 β€ T β€ 1 000). Then the descriptions of the test cases follow.
Each test case consists of one line containing two integers a and b (0 β€ a, b β€ 10^{9}) β the current volume and Bob's desired volume, respectively.
Output
For each test case, output a single integer β the minimum number of presses to change the TV volume from a to b. If Bob does not need to change the volume (i.e. a=b), then print 0.
Example
Input
3
4 0
5 14
3 9
Output
2
3
2
Note
In the first example, Bob can press the -2 button twice to reach 0. Note that Bob can not press -5 when the volume is 4 since it will make the volume negative.
In the second example, one of the optimal ways for Bob is to press the +5 twice, then press -1 once.
In the last example, Bob can press the +5 once, then press +1. | instruction | 0 | 67,389 | 14 | 134,778 |
Tags: math
Correct Solution:
```
ii=lambda:int(input())
kk=lambda:map(int,input().split())
ll=lambda:list(kk())
d=[0,1,1,2,2]
for _ in range(ii()):
a,b=kk()
print(abs(a-b)//5+d[abs(a-b)%5])
``` | output | 1 | 67,389 | 14 | 134,779 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.