message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card. The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.
Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.
Constraints
* n ≤ 1000
* The length of the string ≤ 100
Input
In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.
Output
Print the final scores of Taro and Hanako respectively. Put a single space character between them.
Example
Input
3
cat dog
fish fish
lion tiger
Output
1 7
Submitted Solution:
```
n = int(input())
t_score = 0
h_score = 0
for i in range(n):
t, h = input().split()
if len(t) > len(h):
for j in range(len(h)):
if ord(t[j]) > ord(h[j]):
t_score += 3
break
elif ord(t[j]) < ord(h[j]):
h_score += 3
break
else:
h_score += 3
elif len(t) < len(h):
for j in range(len(t)):
if ord(t[j]) > ord(h[j]):
t_score += 3
break
elif ord(t[j]) < ord(h[j]):
h_score += 3
break
else:
t_score += 3
else:
for j in range(len(t)):
if ord(t[j]) > ord(h[j]):
t_score += 3
break
elif ord(t[j]) < ord(h[j]):
h_score += 3
break
else:
t_score += 1
h_score += 1
print("{0} {1}".format(t_score, h_score))
``` | instruction | 0 | 55,599 | 19 | 111,198 |
No | output | 1 | 55,599 | 19 | 111,199 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card. The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.
Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.
Constraints
* n ≤ 1000
* The length of the string ≤ 100
Input
In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.
Output
Print the final scores of Taro and Hanako respectively. Put a single space character between them.
Example
Input
3
cat dog
fish fish
lion tiger
Output
1 7
Submitted Solution:
```
n = int(input())
t_score = 0
h_score = 0
for i in range(n):
t, h = input().split()
if ord(t[0]) > ord(h[0]):
t_score += 3
elif ord(t[0]) < ord(h[0]):
h_score += 3
elif ord(t[0]) == ord(h[0]):
t_score += 1
h_score += 1
print("{0} {1}".format(t_score, h_score))
``` | instruction | 0 | 55,600 | 19 | 111,200 |
No | output | 1 | 55,600 | 19 | 111,201 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card. The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.
Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.
Constraints
* n ≤ 1000
* The length of the string ≤ 100
Input
In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.
Output
Print the final scores of Taro and Hanako respectively. Put a single space character between them.
Example
Input
3
cat dog
fish fish
lion tiger
Output
1 7
Submitted Solution:
```
#!/usr/bin/env python
n = int(input())
tScore = 0
hScore = 0
for i in range(n):
T, H = input().split()
if T == H:
tScore += 1
hScore += 1
else:
j = 0
while True:
if T[j] != H[j]:
if ord(T[j]) > ord(H[j]):
tScore += 3
else:
hScore += 3
break
j += 1
print("%d %d" % (tScore, hScore))
``` | instruction | 0 | 55,601 | 19 | 111,202 |
No | output | 1 | 55,601 | 19 | 111,203 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card. The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.
Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.
Constraints
* n ≤ 1000
* The length of the string ≤ 100
Input
In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.
Output
Print the final scores of Taro and Hanako respectively. Put a single space character between them.
Example
Input
3
cat dog
fish fish
lion tiger
Output
1 7
Submitted Solution:
```
n=int(input())
i=0
Taro=0
Hana=0
while i<n:
l=list(input())
m=l.index(" ")
#if ord(l[0])<ord(l[m+1]):
#Hana+=1
#elif ord(l[0])>ord(l[m+1]):
#Taro+=1
#else:
#ord(l[1])
p=0
while True:
if l[p]>l[m+1+p]:
Taro+=3
break
elif l[p]<l[m+1+p]:
Hana+=3
break
else:
if p<m-1:
p+=1
else:
Taro+=1
Hana+=1
break
i+=1
Ans=str(Taro)+" "+str(Hana)
print(Ans)
``` | instruction | 0 | 55,602 | 19 | 111,204 |
No | output | 1 | 55,602 | 19 | 111,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,765 | 19 | 111,530 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
#!/usr/bin/env python3
import sys
def rint():
return map(int, sys.stdin.readline().split())
#lines = stdin.readlines()
s = list(input())
q = s.count('?')
o = s.count('1')
z = s.count('0')
ans =[]
#00
if z+q >= o+1:
ans.append("00")
#01
tmp = o+q-z - (z+o+q)%2
if tmp%2 == 0:
x = tmp//2
if x >= 0 and x <= q:
if s[-1] == '1' or s[-1] == '?' and q-x > 0:
ans.append("01")
#10
tmp = z+q-o + (z+o+q)%2
if tmp%2 == 0:
x = tmp//2
if x >= 0 and x <= q:
if s[-1] == '0' or s[-1] == '?' and q-x > 0:
ans.append("10")
#11
if o+q >= z+2:
ans.append("11")
for a in ans:
print(a)
``` | output | 1 | 55,765 | 19 | 111,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,766 | 19 | 111,532 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
s = input()
n = len(s)
zeros, ones, other = 0, 0, 0
for x in s:
zeros += x == '0'
ones += x == '1'
other += x == '?'
A = ['00', '01', '10', '11']
pos = [0] * 4
pos[0] = zeros + other > n // 2
pos[3] = zeros < n // 2
if zeros <= n // 2 and zeros + other >= n // 2:
canuse = other - n // 2 + zeros
pos[1] = s[n-1] == '1' or (s[n-1] == '?' and canuse)
canuse = zeros < n // 2
pos[2] = s[n-1] == '0' or (s[n-1] == '?' and canuse)
for i in range(4):
if pos[i]:
print(A[i])
# Made By Mostafa_Khaled
``` | output | 1 | 55,766 | 19 | 111,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,767 | 19 | 111,534 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
a,b,c = 0,0,0
last = ""
for i in input():
if i=='1':
a+=1
elif i=='0':
b+=1
else:
c+=1
last=i
if b+c>a:
print("00")
if last!="0":
a1, b1, c1 = a,b,c
if last=="?":
c1-=1
a1+=1
x=(-a1+b1+c1+(a1+b1+c1)%2)//2
if 0<=x<=c1:
print("01")
if last!="1":
a1, b1, c1 = a, b, c
if last=="?":
c1-=1
b1+=1
x=(-a1+b1+c1+(a1+b1+c1)%2)//2
if 0<=x<=c1:
print("10")
if a+c>b+1:
print("11")
``` | output | 1 | 55,767 | 19 | 111,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,768 | 19 | 111,536 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
s = input()
n = len(s)
zeros, ones, other = 0, 0, 0
for x in s:
zeros += x == '0'
ones += x == '1'
other += x == '?'
A = ['00', '01', '10', '11']
pos = [0] * 4
pos[0] = zeros + other > n // 2
pos[3] = zeros < n // 2
if zeros <= n // 2 and zeros + other >= n // 2:
canuse = other - n // 2 + zeros
pos[1] = s[n-1] == '1' or (s[n-1] == '?' and canuse)
canuse = zeros < n // 2
pos[2] = s[n-1] == '0' or (s[n-1] == '?' and canuse)
for i in range(4):
if pos[i]:
print(A[i])
``` | output | 1 | 55,768 | 19 | 111,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,769 | 19 | 111,538 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
# The first player always removes the 1s, if possible.
# The second player always removes the 0s if possible.
res = input()
possible = []
zero = res.count('0')
one = res.count('1')
question = res.count('?')
if (len(res) - 2) // 2 + 2 <= zero + question:
possible.append('00')
# The optimal play never removes the rightmost character if 0s and 1s are equivalent.
# 111010
if res[-1] == '?':
if (
one + 1 <= (len(res) + 1) // 2 <= one + question
and
zero <= len(res) // 2 <= zero + question - 1
):
possible.append('01')
if (
one <= (len(res) + 1) // 2 <= one + question - 1
and
zero + 1 <= len(res) // 2 <= zero + question
):
possible.append('10')
else:
if (
one <= (len(res) + 1) // 2 <= one + question
and
zero <= len(res) // 2 <= zero + question
):
if res[-1] == '1':
possible.append('01')
if res[-1] == '0':
possible.append('10')
if (len(res) - 1) // 2 + 2 <= one + question:
possible.append('11')
print('\n'.join(possible))
``` | output | 1 | 55,769 | 19 | 111,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,770 | 19 | 111,540 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
s = input()
a, b, c = (s.count(x) for x in "01?")
# print("{} {} {}".format(a, b, c))
if(a + c > b):
print("00")
if(a + c + 2 > b >= a - c):
if(s[-1] != '?'):
if(s[-1] == '0'):
print('10')
else:
print('01')
else:
if(a + c > b):
print("01")
if(b + c > a + 1):
print("10")
if(b + c > a + 1):
print("11")
``` | output | 1 | 55,770 | 19 | 111,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,771 | 19 | 111,542 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
import sys
import math
s=str(input())
n=len(s)
zero,one,qun=0,0,0
for x in s:
zero+=x=='0'
one+=x=='1'
qun+=x=='?'
A=['00','01','10','11']
pos=[0]*4
pos[0]=zero+qun>n//2
pos[3]=zero<n//2
if zero<=n//2 and zero+qun>=n//2:
asdf=qun-n//2+zero
pos[1]=s[n-1]=='1' or (s[n-1]=='?' and asdf)
asdf=zero<n//2
pos[2]=s[n-1]=='0' or (s[n-1]=='?' and asdf)
for i in range(4):
if pos[i]:
print(A[i])
``` | output | 1 | 55,771 | 19 | 111,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | instruction | 0 | 55,772 | 19 | 111,544 |
Tags: constructive algorithms, games, greedy
Correct Solution:
```
def evaluate(a):
c1 = a.count('1')
c0 = a.count('0')
n = len(a)
A = (n - 1) // 2
B = (n - 2) // 2
if c1 <= A:
return '00'
if c0 <= B:
return '11'
p1 = a.rfind('1')
p0 = a.rfind('0')
if p0 < p1:
return '01'
else:
return '10'
a = input()
x = []
x.append(evaluate(a.replace('?', '0')))
x.append(evaluate(a.replace('?', '1')))
n = len(a)
c1 = a.count('1')
c0 = a.count('0')
A = (n - 1) // 2
B = (n - 2) // 2
x.append(evaluate(a.replace('?', '0', B + 1 - c0).replace('?', '1')))
x.append(evaluate(a.replace('?', '1', A + 1 - c1).replace('?', '0')))
for ans in sorted(list(set(x))):
print(ans)
``` | output | 1 | 55,772 | 19 | 111,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
Submitted Solution:
```
a,b,c = 0,0,0
last = ""
for i in input():
if i=='1':
a+=1
elif i=='0':
b+=1
else:
c+=1
last=i
if b+c>a:
print("00")
if last!="0":
a1, b1, c1 = a,b,c
if last=="?":
c1-=1
a1+=1
x=(-a1+b1+c1+(a1+b1+c1)%2)//2
if 0<=x<=c:
print("01")
if last!="1":
a1, b1, c1 = a, b, c
if last=="?":
c1-=1
b1+=1
x=(-a1+b1+c1+(a1+b1+c1)%2)//2
if 0<=x<=c:
print("10")
if a+c>b+1:
print("11")
``` | instruction | 0 | 55,773 | 19 | 111,546 |
No | output | 1 | 55,773 | 19 | 111,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
Submitted Solution:
```
#!/usr/bin/env python3
import sys
def rint():
return map(int, sys.stdin.readline().split())
#lines = stdin.readlines()
s = list(input())
q = s.count('?')
o = s.count('1')
z = s.count('0')
ans =[]
#00
if z+q >= o+1:
ans.append("00")
#01
tmp = o+q-z - (z+o+q)%2
if tmp%2 == 0:
x = tmp//2
if x >= 0 and x <= q:
if s[-1] == '1' or s[-1] == '?':
ans.append("01")
#10
tmp = z+q-o + (z+o+q)%2
if tmp%2 == 0:
x = tmp//2
if x >= 0 and x <= q:
if s[-1] == '0' or s[-1] == '?':
ans.append("10")
#11
if o+q >= z+2:
ans.append("11")
for a in ans:
print(a)
``` | instruction | 0 | 55,774 | 19 | 111,548 |
No | output | 1 | 55,774 | 19 | 111,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
Submitted Solution:
```
# The first player always removes the 1s, if possible.
# The second player always removes the 0s if possible.
res = input()
possible = []
zero = res.count('0')
one = res.count('1')
question = res.count('?')
if (len(res) - 1) // 2 >= one:
possible.append('00')
# The optimal play never removes the rightmost character if 0s and 1s are equivalent.
# ?111111?00?
if res[-1] == '?':
if (
one + 1 <= (len(res) + 1) // 2 <= one + question
and
zero <= len(res) // 2 <= zero + question - 1
):
possible.append('01')
if (
one <= (len(res) + 1) // 2 <= one + question - 1
and
zero + 1 <= len(res) // 2 <= zero + question
):
possible.append('10')
else:
if (
one <= (len(res) + 1) // 2 <= one + question
and
zero <= len(res) // 2 <= zero + question
):
if res[-1] == '1':
possible.append('01')
if res[-1] == '0':
possible.append('10')
if (len(res) - 1) // 2 >= zero:
possible.append('11')
print('\n'.join(possible))
``` | instruction | 0 | 55,775 | 19 | 111,550 |
No | output | 1 | 55,775 | 19 | 111,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
Input
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Output
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
Examples
Input
????
Output
00
01
10
11
Input
1010
Output
10
Input
1?1
Output
01
11
Note
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
Submitted Solution:
```
s = input()
cnt0 = sum([(i == '0') for i in s])
cnt1 = sum([(i == '1') for i in s])
joker = len(s) - cnt0 - cnt1
canRemove2 = (len(s) - 2) // 2
canRemove1 = len(s) - 2 - canRemove2
if canRemove1 >= cnt1:
assert canRemove2 <= cnt0 + joker
print('00')
if s[-1] != '0':
if canRemove2 + 1 <= cnt0 + joker and canRemove1 + 1 <= cnt1 + joker:
print('01')
if s[-1] != '1':
if canRemove1 + 1 <= cnt1 + joker and canRemove2 + 1 <= cnt0 + joker:
print('10')
if canRemove2 >= cnt0:
assert canRemove1 <= cnt1 + joker
print('11')
``` | instruction | 0 | 55,776 | 19 | 111,552 |
No | output | 1 | 55,776 | 19 | 111,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,812 | 19 | 111,624 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left , bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')
def isPrime(x):
for i in range(2,x):
if i*i>x:
break
if (x%i==0):
return False
return True
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def si():
return input()
def prefix_sum(arr):
r = [0] * (len(arr)+1)
for i, el in enumerate(arr):
r[i+1] = r[i] + el
return r
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
def power_set(L):
cardinality=len(L)
n=2 ** cardinality
powerset = []
for i in range(n):
a=bin(i)[2:]
subset=[]
for j in range(len(a)):
if a[-j-1]=='1':
subset.append(L[j])
powerset.append(subset)
powerset_orderred=[]
for k in range(cardinality+1):
for w in powerset:
if len(w)==k:
powerset_orderred.append(w)
return powerset_orderred
def fastPlrintNextLines(a):
# 12
# 3
# 1
#like this
#a is list of strings
print('\n'.join(map(str,a)))
def sortByFirstAndSecond(A):
A = sorted(A,key = lambda x:x[0])
A = sorted(A,key = lambda x:x[1])
return list(A)
#__________________________TEMPLATE__________________OVER_______________________________________________________
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def fun(a,b,m,n):
m = len(b)
d = defaultdict(lambda:0)
for i in a:
d[i]+=1
pre = [0]*m
for i in range(m-1,-1,-1):
if d[b[i]]>0:
pre[i]+=1
if i!=m-1:
pre[i]+=pre[i+1]
m = 0
j = 0
# print(pre)
# print(a)
# print(b)
for i in range(len(b)):
# print(i,j)
while(j<len(a) and a[j]<=b[i]):
# print("j",j)
# print(a[j],b[i])
j+=1
# print("f")
if j==0:
continue
if i<len(b)-1:
temp = i-bisect_left(b,b[i]-j+1)+pre[i+1]+1
else:
temp = i-bisect_left(b,b[i]-j+1)+1
# print(temp,i,pre[i])
m = max(m,temp)
return m
def solve():
n,m= li()
A = li()
b = li()
a = []
for i in A:
if i>0:
a.append(i)
n = len(a)
ans = 0
bb = []
for i in b:
if i>0:
bb.append(i)
bb.sort()
m = len(bb)
if a!=[]:
ans= fun(a,bb,m,len(a))
a = []
for i in A:
if i<0:
a.append(-i)
n = len(a)
a.sort()
b.reverse()
bb = []
for i in b:
if i<0:
bb.append(-i)
bb.sort()
b = bb[:]
if n>0:
ans += fun(a,b,m,len(a))
print(ans)
t = 1
t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 55,812 | 19 | 111,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,813 | 19 | 111,626 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
aset=set()
for i in range(n):
aset.add(a[i])
apos=[]
aneg=[]
bpos=[]
bneg=[]
for i in range(n):
if a[i]>0:
apos.append(a[i])
else:
aneg.append(-a[i])
posdup=[]
negdup=[]
for i in b:
if i in aset:
if i>0:
posdup.append(i)
else:
negdup.append(-i)
posdup.sort()
posdup.reverse()
negdup.sort()
negdup.reverse()
for i in range(m):
if b[i]>0:
bpos.append(b[i])
else:
bneg.append(-b[i])
aneg.reverse()
bneg.reverse()
currbox=0
currend=0
ans1=len(posdup)
for i in range(len(bpos)):
while currbox<len(apos) and currbox+bpos[i]>=apos[currbox]:
currbox+=1
while currend<len(bpos) and bpos[currend]<=currbox-1+bpos[i]:
currend+=1
while posdup and posdup[-1]<=currbox+bpos[i]:
posdup.pop()
ans1=max(ans1,currend-i+len(posdup))
ans2=len(negdup)
currbox=0
currend=0
for i in range(len(bneg)):
while currbox<len(aneg) and currbox+bneg[i]>=aneg[currbox]:
currbox+=1
while currend<len(bneg) and bneg[currend]<=currbox-1+bneg[i]:
currend+=1
while negdup and negdup[-1]<=currbox+bneg[i]:
negdup.pop()
ans2=max(ans2,currend-i+len(negdup))
print(ans1+ans2)
``` | output | 1 | 55,813 | 19 | 111,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,814 | 19 | 111,628 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left , bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')
def isPrime(x):
for i in range(2,x):
if i*i>x:
break
if (x%i==0):
return False
return True
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def si():
return input()
def prefix_sum(arr):
r = [0] * (len(arr)+1)
for i, el in enumerate(arr):
r[i+1] = r[i] + el
return r
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
def power_set(L):
cardinality=len(L)
n=2 ** cardinality
powerset = []
for i in range(n):
a=bin(i)[2:]
subset=[]
for j in range(len(a)):
if a[-j-1]=='1':
subset.append(L[j])
powerset.append(subset)
powerset_orderred=[]
for k in range(cardinality+1):
for w in powerset:
if len(w)==k:
powerset_orderred.append(w)
return powerset_orderred
def fastPlrintNextLines(a):
# 12
# 3
# 1
#like this
#a is list of strings
print('\n'.join(map(str,a)))
def sortByFirstAndSecond(A):
A = sorted(A,key = lambda x:x[0])
A = sorted(A,key = lambda x:x[1])
return list(A)
#__________________________TEMPLATE__________________OVER_______________________________________________________
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def fun(a,b,m,n):
m = len(b)
d = defaultdict(lambda:0)
for i in a:
d[i]+=1
pre = [0]*m
for i in range(m-1,-1,-1):
if d[b[i]]>0:
pre[i]+=1
if i!=m-1:
pre[i]+=pre[i+1]
m = 0
j = 0
# print(pre)
# print(a)
# print(b)
for i in range(len(b)):
# print(i,j)
while(j<len(a) and a[j]<=b[i]):
# print("j",j)
# print(a[j],b[i])
j+=1
# print("f")
if i<len(b)-1:
temp = i-bisect_left(b,b[i]-j+1)+pre[i+1]+1
else:
temp = i-bisect_left(b,b[i]-j+1)+1
# print(temp,i,pre[i])
m = max(m,temp)
return m
def solve():
n,m= li()
A = li()
b = li()
a = []
for i in A:
if i>0:
a.append(i)
n = len(a)
ans = 0
bb = []
for i in b:
if i>0:
bb.append(i)
bb.sort()
m = len(bb)
if a!=[]:
ans= fun(a,bb,m,len(a))
a = []
for i in A:
if i<0:
a.append(-i)
n = len(a)
a.sort()
b.reverse()
bb = []
for i in b:
if i<0:
bb.append(-i)
bb.sort()
b = bb[:]
if n>0:
ans += fun(a,b,m,len(a))
print(ans)
t = 1
t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 55,814 | 19 | 111,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,815 | 19 | 111,630 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
def main():
import sys
import bisect
input = sys.stdin.buffer.readline
t = int(input())
ANS = []
for _ in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a1 = [-a[i] for i in range(n) if a[i] < 0]
a1.sort()
a2 = [a[i] for i in range(n) if a[i] > 0]
b1 = [-b[i] for i in range(m) if b[i] < 0]
b1.sort()
b2 = [b[i] for i in range(m) if b[i] > 0]
c = 0
ans1 = 0
if len(a1) and len(b1):
P = [0]*(len(a1)+1)
j = len(b1)-1
for i in range(len(a1)-1, -1, -1):
P[i] = P[i+1]
while(j > 0 and b1[j] > a1[i]):
j -= 1
if b1[j] == a1[i]:
P[i] += 1
for i, x in enumerate(a1):
c += 1
r = x
j = bisect.bisect_left(b1, x-c+1)
while(j < len(b1) and (i == len(a1)-1 or b1[j]+c-1 < a1[i+1])):
r = b1[j]+c - 1
j2 = bisect.bisect_right(b1, r)
if ans1 < j2-j + P[i+1]:
ans1 = j2-j + P[i+1]
j += 1
# print('ans1', ans1)
ans2 = 0
if len(a2) and len(b2):
P = [0]*(len(a2)+1)
j = len(b2)-1
for i in range(len(a2)-1, -1, -1):
P[i] = P[i+1]
while(j > 0 and b2[j] > a2[i]):
j -= 1
if b2[j] == a2[i]:
P[i] += 1
c = 0
for i, x in enumerate(a2):
c += 1
r = x
j = bisect.bisect_left(b2, x-c+1)
while(j < len(b2) and (i == len(a2)-1 or b2[j]+c-1 < a2[i+1])):
r = b2[j]+c - 1
j2 = bisect.bisect_right(b2, r)
if ans2 < j2-j + P[i+1]:
ans2 = j2-j + P[i+1]
j += 1
# print('ans2', ans2)
ANS.append(str(ans1+ans2))
print("\n".join(ANS))
# 1
# 5 6
# -1 1 5 11 15
# -4 -3 -2 6 7 15
main()
``` | output | 1 | 55,815 | 19 | 111,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,816 | 19 | 111,632 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import bisect
def gift():
for _ in range(t):
n,m = list(map(int,input().split()))
boxs = list(map(int,input().split()))
poss = list(map(int,input().split()))
ap = []
bp = []
am = []
bm = []
for i in boxs:
if i>0:
ap.append(i)
else:
am.append(-1*i)
for i in poss:
if i>0:
bp.append(i)
else:
bm.append(-1*i)
am.reverse()
bm.reverse()
rembox = len(ap)
setbox = 0
pans = 0
for i in range(len(bp)-1,-1,-1):
nb = bp[i]
while len(ap)>0 and ap[-1]>nb:
rembox -= 1
ap.pop()
pans = max(pans, setbox + i - bisect.bisect_right(bp,nb-rembox) + 1)
if len(ap)>0 and nb == ap[-1]:
rembox -= 1
setbox += 1
ap.pop()
mans = 0
ap = am
bp = bm
rembox = len(ap)
setbox = 0
for i in range(len(bp)-1,-1,-1):
nb = bp[i]
while len(ap) > 0 and ap[-1] > nb:
rembox -= 1
ap.pop()
mans = max(mans , setbox + i - bisect.bisect_right(bp,nb-rembox) + 1)
if len(ap) > 0 and nb == ap[-1]:
rembox -= 1
setbox += 1
ap.pop()
yield pans+mans
if __name__ == '__main__':
t= int(input())
ans = gift()
print(*ans,sep='\n')
#"{} {} {}".format(maxele,minele,minele)
# yield " ".join([str(x) for x in ans])
``` | output | 1 | 55,816 | 19 | 111,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,817 | 19 | 111,634 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import sys
from bisect import *
import bisect as bi
import collections as cc
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
for tc in range(int(input())):
n,m=I()
a=I()
b=I()
def ch(a, b):
given = sorted(set(a) & set(b))
best = len(given)
for i in b:
boxes = bisect_right(a, i)
if boxes:
sol = bisect_right(b, i) - bisect_right(b, i - boxes)
sol += len(given) - bisect_right(given, i)
best = max(sol, best)
return best
i=bi.bisect(a,0)
j=bi.bisect(b,0)
pa=a[i:]
pb=b[j:]
na=[abs(i) for i in a[:i]][::-1]
nb=[abs(i) for i in b[:j]][::-1]
#print(pa,pb)
#print(na,nb)
print(ch(pa,pb)+ch(na,nb))
``` | output | 1 | 55,817 | 19 | 111,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,818 | 19 | 111,636 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import sys
from bisect import bisect_left as l
r=range
def s(A,B):
S=set(A);b=len(B);C=[0]*(b+1)
for i in r(b-1,-1,-1):
if B[i] in S:C[i]+=1
C[i]+=C[i+1]
a=C[0];X=0
for i in r(b):
while X<len(A) and A[X]<=B[i]:X+=1
if X>0:a=max(a,l(B,B[i])-l(B,B[i]-X+1)+1+C[i+1])
return a
p=lambda:map(int,sys.stdin.buffer.readline().split())
for t in r(*p()):
N,M=p();V=*p(),;W=*p(),
A=[];X=[];B=[];Y=[]
for i in r(N):
v=V[i]
if v<0:A.append(-v)
else:B.append(v)
for i in r(M):
w=W[i]
if w<0:X.append(-w)
else:Y.append(w)
print(s(A[::-1],X[::-1])+s(B,Y))
``` | output | 1 | 55,818 | 19 | 111,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10. | instruction | 0 | 55,819 | 19 | 111,638 |
Tags: binary search, dp, greedy, implementation, two pointers
Correct Solution:
```
import sys, bisect
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)
ub = bisect.bisect_right
lb = bisect.bisect_left
def solve(a, b):
if len(a)==0 or len(b)==0:
return 0
def aux(i):
prev=-1 ; c=0
while c!=prev:
prev = c ; c = ub(a, b[i]+c)
return c
n = len(b)
sa = set(a)
b_in_a = [b[i] in sa for i in range(n)]
b_acum_a = b_in_a.copy()
for i in range(n-2,-1,-1):
b_acum_a[i] += b_acum_a[i+1]
c = [aux(i) for i in range(n)]
d = [ub(b, b[i]+c[i]-1)-i for i in range(n)]
g = [i+d[i]<n and b_acum_a[i+d[i]] for i in range(n)]
h = [d[i]+g[i] for i in range(n)]
#print(h)
return max(h)
def main():
ntc = next(ints)
for tc in range(1,ntc+1):
n, m = (next(ints) for i in range(2))
a = [next(ints) for i in range(n)]
b = [next(ints) for i in range(m)]
ans = solve([x for x in a if x>0], [x for x in b if x>0])
ans += solve([-x for x in a if x<0][::-1], [-x for x in b if x<0][::-1])
print(ans)
return
main()
``` | output | 1 | 55,819 | 19 | 111,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
from math import ceil, log2
import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict
from bisect import bisect_right
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")
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b =list(map(int,input().split()))
cnt1=0
cnt2=0
vis=defaultdict(lambda:0)
for j in b:
vis[j]=1
i=0
while(i<n):
if a[i]>0 and vis[a[i]]:
cnt1+=1
elif a[i]<0 and vis[a[i]]:
cnt2+=1
i+=1
i=0
tot1=cnt1
j=0
r=0
res=[]
l=0
u=0
cnt=0
while(i<n and j<m):
if a[i]<0:
i+=1
else:
r+=1
if vis[a[i]]:
cnt1+=-1
if i==(n-1):
while(j<m):
cnt+=0
if b[j]>0 and b[j]>=a[i]:
res.append(b[j])
l+=1
j+=1
else:
while(j<m and b[j]<a[i]):
cnt+=1
j+=1
while(j<m and b[j]<a[i+1]):
cnt+=1
res.append(b[j])
l+=1
j+=1
ans = 0
while (u<l):
k = res[u] - r
ind = bisect_right(res, k)
ans = max(ans, u - ind + 1)
u += 1
tot1=max(tot1,ans+cnt1)
i+=1
tot2 = cnt2
i=n-1
j = m-1
r = 0
res=[]
u=0
l=0
while (i >=0 and j >=0):
cnt+=1
if a[i] > 0:
i += -1
else:
r += 1
if vis[a[i]]:
cnt2 += -1
if i == 0:
while (j >=0):
if b[j]<0 and b[j]<=a[i]:
res.append(-b[j])
l+=1
j += -1
else:
while (j>=0 and b[j] > a[i]):
j += -1
while (j>=0 and b[j] > a[i - 1]):
res.append(-b[j])
l+=1
j += -1
ans = 0
while (u < l):
k = res[u] - r
ind = bisect_right(res, k)
ans = max(ans, u - ind + 1)
u += 1
tot2 = max(tot2, cnt2 + ans)
i += -1
print(tot1+tot2)
``` | instruction | 0 | 55,820 | 19 | 111,640 |
Yes | output | 1 | 55,820 | 19 | 111,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
import sys
from bisect import bisect_left as l
r=range
def s(A,B):
S=set(A);b=len(B);C=[0]*(b+1)
for i in r(b-1,-1,-1):
if B[i] in S:C[i]+=1
C[i]+=C[i+1]
a=C[0];X=0
for i in r(b):
while X<len(A) and A[X]<=B[i]:X+=1
if X>0:a=max(a,l(B,B[i])-l(B,B[i]-X+1)+1+C[i+1])
return a
o=[*open(0)]
p=lambda i:map(int,o[i].split())
for i in r(*p(0)):
A=[];X=[];B=[];Y=[]
for v in p(3*i+2):
if v<0:A.append(-v)
else:B.append(v)
for w in p(3*i+3):
if w<0:X.append(-w)
else:Y.append(w)
print(s(A[::-1],X[::-1])+s(B,Y))
``` | instruction | 0 | 55,821 | 19 | 111,642 |
Yes | output | 1 | 55,821 | 19 | 111,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
from bisect import bisect_left, bisect_right
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
a_right = [int(x) for x in a if x > 0]
b_right = [int(x) for x in b if x > 0]
a_right.sort()
b_right.sort()
b_set_right = set(b_right)
initial_special_right = [int(x) for x in a_right if x in b_set_right]
cum_sum = [0]
for i in range(len(b_right)):
cum_sum.append(cum_sum[-1] + 1)
ans_right = 0
for i in range(len(b_right)):
cnt = 0
# i以下の要素の個数
x = bisect_right(a_right, b_right[i])
y = (b_right[i] - x + 1)
y_idx = bisect_left(b_right, y)
cnt = cum_sum[i + 1] - cum_sum[y_idx]
cnt += len(initial_special_right) - bisect_right(initial_special_right, b_right[i])
ans_right = max(ans_right, cnt)
#########################################################################################
a_right = [-int(x) for x in a if x < 0]
b_right = [-int(x) for x in b if x < 0]
a_right.sort()
b_right.sort()
b_set_right = set(b_right)
initial_special_right = [int(x) for x in a_right if x in b_set_right]
cum_sum = [0]
for i in range(len(b_right)):
cum_sum.append(cum_sum[-1] + 1)
ans_left = 0
for i in range(len(b_right)):
cnt = 0
# i以下の要素の個数
x = bisect_right(a_right, b_right[i])
y = (b_right[i] - x + 1)
y_idx = bisect_left(b_right, y)
cnt = cum_sum[i + 1] - cum_sum[y_idx]
cnt += len(initial_special_right) - bisect_right(initial_special_right, b_right[i])
ans_left = max(ans_left, cnt)
print(ans_left + ans_right)
``` | instruction | 0 | 55,822 | 19 | 111,644 |
Yes | output | 1 | 55,822 | 19 | 111,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
def solve(a,b):
n=len(a)
m=len(b)
bSuffixMatchCounts=[0 for _ in range(m)]
i=n-1
cnts=0
for j in range(m-1,-1,-1):
while i>=0 and a[i]>b[j]:
i-=1
if i>=0 and a[i]==b[j]:
cnts+=1
bSuffixMatchCounts[j]=cnts
bSuffixMatchCounts.append(0)
maxMatches=bSuffixMatchCounts[0]
i=-1
for j in range(m):
while i+1<n and a[i+1]<=b[j]:
i+=1
if i==-1:
continue
aCnts=i+1
left=b[j]-aCnts+1
bLeft=-1
zz=m
while zz>0:
while bLeft+zz<m and b[bLeft+zz]<left:
bLeft+=zz
zz//=2
bLeft+=1
matches=(j-bLeft+1)+bSuffixMatchCounts[j+1]
# print('aCnts:{} bLeft:{} bRight=j:{} matches:{}'.format(aCnts,bLeft,j,matches))
maxMatches=max(maxMatches,matches)
# print('a:{} b:{} bSuf:{} maxMatches:{}'.format(a,b,bSuffixMatchCounts,maxMatches))
# print()
return maxMatches
def main():
t=int(input())
allans=[]
for _ in range(t):
n,m=readIntArr()
a=readIntArr()
b=readIntArr()
aleft=[]
bleft=[]
aright=[]
bright=[]
for x in a:
if x<0:
aleft.append(x)
else:
aright.append(x)
for x in b:
if x<0:
bleft.append(x)
else:
bright.append(x)
aleft.reverse()
bleft.reverse()
for i in range(len(aleft)):
aleft[i]*=-1
for i in range(len(bleft)):
bleft[i]*=-1
allans.append(solve(aleft,bleft)+solve(aright,bright))
multiLineArrayPrint(allans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# import sys
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
inf=float('inf')
MOD=10**9+7
main()
``` | instruction | 0 | 55,823 | 19 | 111,646 |
Yes | output | 1 | 55,823 | 19 | 111,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left , bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')
def isPrime(x):
for i in range(2,x):
if i*i>x:
break
if (x%i==0):
return False
return True
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def si():
return input()
def prefix_sum(arr):
r = [0] * (len(arr)+1)
for i, el in enumerate(arr):
r[i+1] = r[i] + el
return r
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
def power_set(L):
cardinality=len(L)
n=2 ** cardinality
powerset = []
for i in range(n):
a=bin(i)[2:]
subset=[]
for j in range(len(a)):
if a[-j-1]=='1':
subset.append(L[j])
powerset.append(subset)
powerset_orderred=[]
for k in range(cardinality+1):
for w in powerset:
if len(w)==k:
powerset_orderred.append(w)
return powerset_orderred
def fastPlrintNextLines(a):
# 12
# 3
# 1
#like this
#a is list of strings
print('\n'.join(map(str,a)))
def sortByFirstAndSecond(A):
A = sorted(A,key = lambda x:x[0])
A = sorted(A,key = lambda x:x[1])
return list(A)
#__________________________TEMPLATE__________________OVER_______________________________________________________
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def fun(a,b,m,n):
m = len(b)
d = defaultdict(lambda:0)
for i in a:
d[i]+=1
pre = [0]*m
for i in range(m-1,-1,-1):
if d[b[i]]>0:
pre[i]+=1
if i!=m-1:
pre[i]+=pre[i+1]
m = 0
j = 0
# print(pre)
# print(a)
# print(b)
for i in range(len(b)):
# print(i,j)
while(j<len(a) and a[j]<=b[i]):
# print("j",j)
# print(a[j],b[i])
j+=1
# print("f")
if i<len(b)-1:
temp = i-bisect_left(b,b[i]-j+1)+pre[i+1]+1
else:
temp = i-bisect_left(b,b[i]-j+1)+1
# print(temp,i,pre[i])
m = max(m,temp)
return m
def solve():
n,m= li()
A = li()
b = li()
a = []
for i in A:
if i>0:
a.append(i)
n = len(a)
ans = 0
bb = []
for i in b:
if i>0:
bb.append(i)
bb.sort()
m = len(bb)
if a!=[]:
ans= fun(a,bb,m,len(a))
a = []
for i in A:
if i<0:
a.append(-i)
n = len(a)
b.reverse()
bb = []
for i in b:
if i<0:
bb.append(-i)
bb.sort()
b = bb[:]
if n>0:
ans += fun(a,b,m,len(a))
print(ans)
t = 1
t = int(input())
for _ in range(t):
solve()
``` | instruction | 0 | 55,824 | 19 | 111,648 |
No | output | 1 | 55,824 | 19 | 111,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
#!/usr/bin/env python3
import sys, getpass
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict, deque
input = sys.stdin.readline # to read input quickly
# available on Google, AtCoder Python3, not available on Codeforces
# import numpy as np
# import scipy
M9 = 10**9 + 7 # 998244353
# d4 = [(1,0),(0,1),(-1,0),(0,-1)]
# d8 = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
# d6 = [(2,0),(1,1),(-1,1),(-2,0),(-1,-1),(1,-1)] # hexagonal layout
MAXINT = sys.maxsize
# if testing locally, print to terminal with a different color
OFFLINE_TEST = getpass.getuser() == "hkmac"
# OFFLINE_TEST = False # codechef does not allow getpass
def log(*args):
if OFFLINE_TEST:
print('\033[36m', *args, '\033[0m', file=sys.stderr)
def solve(*args):
# screen input
if OFFLINE_TEST:
log("----- solving ------")
log(*args)
log("----- ------- ------")
return solve_(*args)
def read_matrix(rows):
return [list(map(int,input().split())) for _ in range(rows)]
def read_strings(rows):
return [input().strip() for _ in range(rows)]
# ---------------------------- template ends here ----------------------------
def solve2(arr, brr):
log()
log(arr)
log(brr)
pos = set(brr)
crr = [int(x in pos) for x in arr]
csum = 0
psum = []
for x in crr:
csum += x
psum.append(csum)
psum.append(csum)
count = sum(crr)
maxres = count
for i,x in enumerate(brr, start=1):
displaced = bisect.bisect_right(arr, x)
occupied_in_range = i - bisect.bisect_right(brr, x-displaced)
res = count - psum[displaced] + occupied_in_range
# log(x, displaced)
# log(res, count, psum[displaced], occupied_in_range)
# log()
maxres = max(maxres, res)
return maxres
def solve_(arr, brr):
# your solution here
res = 0
res += solve2([x for x in arr if x > 0], [x for x in brr if x > 0])
res += solve2([-x for x in arr if x < 0][::-1], [-x for x in brr if x < 0][::-1])
return res
# for case_num in [0]: # no loop over test case
# for case_num in range(100): # if the number of test cases is specified
for case_num in range(int(input())):
# read line as an integer
# k = int(input())
# read line as a string
# srr = input().strip()
# read one line and parse each word as a string
# lst = input().split()
# read one line and parse each word as an integer
n,m = list(map(int,input().split()))
arr = list(map(int,input().split()))
brr = list(map(int,input().split()))
# read multiple rows
# mrr = read_matrix(k) # and return as a list of list of int
# arr = read_strings(k) # and return as a list of str
res = solve(arr, brr) # include input here
# print result
# Google and Facebook - case number required
# print("Case #{}: {}".format(case_num+1, res))
# Other platforms - no case number required
print(res)
# print(len(res))
# print(*res) # print a list with elements
# for r in res: # print each list in a different line
# print(res)
# print(*res)
``` | instruction | 0 | 55,825 | 19 | 111,650 |
No | output | 1 | 55,825 | 19 | 111,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
"""
1
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
1
3 5
-1 1 2
-2 -1 1 2 5
1
3 5
-2 -1 1
-2 -1 1 2 5
1
2 2
-5 5
-1 1
1
5 3
1 2 3 4 5
1 2 3
"""
import traceback
import bisect
def main():
t = int(input())
result = []
for _ in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
#
b0_ix = bisect.bisect_left(b, 0)
cnts = [0] * m
for i in range(m - 1, b0_ix - 1, -1):
pos = bisect.bisect_left(a, b[i])
if pos < n and a[pos] == b[i]:
cnts[i] += 1
if i + 1 < m:
cnts[i] += cnts[i + 1]
for i in range(b0_ix):
pos = bisect.bisect_left(a, b[i])
if pos < n and a[pos] == b[i]:
cnts[i] += 1
if i < n:
cnts[i] += cnts[i + 1]
#
box0_ix = bisect.bisect_left(a, 0)
box_before_0_cnt = box0_ix
r_ans = 0
for i in range(m - 1, b0_ix - 1, -1):
new_box_pos = bisect.bisect_right(a, b[i], lo=box0_ix)
box_cnt = n - (n - new_box_pos) - box_before_0_cnt
place_pos = bisect.bisect_left(b, b[i] + box_cnt)
place_cnt = place_pos - i + (cnts[place_pos] if 0 <= place_pos < m else 0)
r_ans = max(r_ans, place_cnt)
l_ans = 0
for i in range(b0_ix):
new_box_pos = bisect.bisect_left(a, b[i], hi=box0_ix)
box_cnt = box0_ix - new_box_pos
place_pos = bisect.bisect_right(b, b[i] - box_cnt) - 1
place_cnt = i - place_pos + (cnts[place_pos] if 0 <= place_pos < m else 0)
l_ans = max(l_ans, place_cnt)
result.append(str(l_ans + r_ans))
print('\n'.join(result))
if __name__ == '__main__':
try:
main()
except Exception as e:
print(e)
``` | instruction | 0 | 55,826 | 19 | 111,652 |
No | output | 1 | 55,826 | 19 | 111,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
Example
Input
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
Output
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
Submitted Solution:
```
from bisect import *
from sys import *
input = stdin.readline
def pref(b,a):
switch = [0]*len(b)
till = [0]*len(b)
j = 0
for i in range(len(b)):
while j<len(a):
if a[j]>b[i]:
break
j+=1
j= max(j-1,0)
if j<len(a):
switch[i] = int(a[j]==b[i])
till[i] = j+1
return switch,till
def calc(b,a):
eq,currTill = pref(b,a)
s = sum(eq)
ans = 0
for i in range(len(b)):
idx = bisect_right(b,b[i]-currTill[i])
s -= eq[i]
ans = max(ans,i-idx+1 + s)
return ans
for _ in range(int(input())):
n,m = map(int,input().split())
a = list(map(int,input().split()))
idx = bisect_left(a,0)
neg_a = [-a[i] for i in range(idx-1,-1,-1)]
a = a[idx:]
b = list(map(int, input().split()))
idx = bisect_left(b, 0)
neg_b = [-b[i] for i in range(idx - 1, -1, -1)]
b = b[idx:]
c1,c2 = 0,0
if a and b:
c1 += calc(b,a)
if neg_b and neg_a:
c2 += calc(neg_b,neg_a)
print(c1+c2)
``` | instruction | 0 | 55,827 | 19 | 111,654 |
No | output | 1 | 55,827 | 19 | 111,655 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,160 | 19 | 114,320 |
Tags: implementation, math
Correct Solution:
```
n=int(input())
l=[int(x) for x in input().split()]
k=max(l)
su=sum(l)
su2=su*2+1
from math import ceil
print(max(k,ceil(su2/n)))
``` | output | 1 | 57,160 | 19 | 114,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,161 | 19 | 114,322 |
Tags: implementation, math
Correct Solution:
```
n = int(input())
A = input().split()
A = [int(ai) for ai in A]
k = max(A)
v_E = sum(A)
while n * k <= 2 * v_E:
k += 1
print(k)
``` | output | 1 | 57,161 | 19 | 114,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,162 | 19 | 114,324 |
Tags: implementation, math
Correct Solution:
```
# Elections
def elections(arr):
k = max(arr)
while True:
a = sum(arr)
b = 0
for i in arr:
b += (k - i)
if b > a:
return k
k += 1
n = int(input())
arr = list(map(int, input().split()))
print(elections(arr))
``` | output | 1 | 57,162 | 19 | 114,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,163 | 19 | 114,326 |
Tags: implementation, math
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
sum = arr[0]
curmax = arr[0]
for i in range (1,len(arr)):
if arr[i] > curmax:
curmax = arr[i]
sum = sum + arr[i]
newsum = sum + sum + 1
k = newsum//len(arr)
if newsum%len(arr) > 0:
k = k + 1
if curmax > k:
k = curmax
print(k)
``` | output | 1 | 57,163 | 19 | 114,327 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,164 | 19 | 114,328 |
Tags: implementation, math
Correct Solution:
```
#!/usr/bin/python3
import math
import sys
DEBUG = False
def inp():
return sys.stdin.readline().rstrip()
def dprint(*value, sep=' ', end='\n'):
if DEBUG:
print(*value, sep=sep, end=end)
def solve(N, A):
mx = max(A)
sm = sum(A)
return max(2 * sm // N + 1, mx)
def main():
N = int(inp())
A = [int(e) for e in inp().split()]
assert len(A) == N
print(solve(N, A))
if __name__ == '__main__':
main()
``` | output | 1 | 57,164 | 19 | 114,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,165 | 19 | 114,330 |
Tags: implementation, math
Correct Solution:
```
import sys
import math
n=int(input())
lisa=[int(x) for x in input().strip().split()]
suma=sum(lisa)
amax=max(lisa)
for i in range(amax,500):
#print("i=",i)
if(i*n-suma>suma):
print(i)
break
``` | output | 1 | 57,165 | 19 | 114,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,166 | 19 | 114,332 |
Tags: implementation, math
Correct Solution:
```
IL = lambda: list(map(int, input().split()))
n = int(input())
A = IL()
v = sum(A) * 2 + 1
print(max(v // n + (v%n > 0), max(A)))
``` | output | 1 | 57,166 | 19 | 114,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from this person. Of course 0 ≤ k - a_i holds.
Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a_1, a_2, ..., a_n — how many votes for Elodreip each student wants to give. Now he wants to change the number k to win the elections. Of course he knows that bigger k means bigger chance that somebody may notice that he has changed something and then he will be disqualified.
So, Awruk knows a_1, a_2, ..., a_n — how many votes each student will give to his opponent. Help him select the smallest winning number k. In order to win, Awruk needs to get strictly more votes than Elodreip.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of students in the school.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 100) — the number of votes each student gives to Elodreip.
Output
Output the smallest integer k (k ≥ max a_i) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.
Examples
Input
5
1 1 1 5 1
Output
5
Input
5
2 2 3 2 2
Output
5
Note
In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 votes. The smallest possible k is 5 (it surely can't be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 votes for Awruk, which is enough to win.
In the second example, Elodreip gets 11 votes. If k = 4, Awruk gets 9 votes and loses to Elodreip. | instruction | 0 | 57,167 | 19 | 114,334 |
Tags: implementation, math
Correct Solution:
```
input()
numbers = [int(x) for x in input().split()]
opponent = sum(numbers)
k = max(numbers)
count = 0
for val in numbers:
count = count + k - val
while count <= opponent:
k = k + 1
count = count + len(numbers)
print(k)
``` | output | 1 | 57,167 | 19 | 114,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9. | instruction | 0 | 57,255 | 19 | 114,510 |
Tags: brute force, greedy, math
Correct Solution:
```
n = int(input())
a = [0]*n
b = [0]*n
c = [0]*n
for i in range(n):
a[i],b[i],c[i] = map(int,input().split())
for i in range(n):
ans = 0
if c[i]//2 <= b[i]:
ans += 3*(c[i]//2)
b[i] -= c[i]//2
else:
ans += 3*(b[i])
b[i] = 0
if b[i]//2 <= a[i]:
ans += 3*(b[i]//2)
else:
ans += 3*(a[i])
print(ans)
``` | output | 1 | 57,255 | 19 | 114,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9. | instruction | 0 | 57,257 | 19 | 114,514 |
Tags: brute force, greedy, math
Correct Solution:
```
from sys import stdin
def scal(typ=int):
return typ(stdin.readline())
def vec(typ=int):
if isinstance(typ, list):
inp = stdin.readline().split()
return [typ[i](inp[i]) for i in range(len(inp))]
else:
return list(map(typ, stdin.readline().split()))
t = scal()
for _ in range(t):
a, b, c = vec()
n = min(b, c // 2)
b -= n
n += min(a, b // 2)
print(3 * n)
``` | output | 1 | 57,257 | 19 | 114,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
n = int(input())
for i in range(n):
a, b, c = [int(x) for x in input().split()]
q1 = 0
q2 = 0
q1 = min(a,int(b/2))*3
a2 = a - min(a,int(b/2))
b2 = b - 2*min(a,int(b/2))
q1= q1 + min(b2,int(c/2))*3
q2 = min(b,int(c/2))*3
b3 = b - min(b,int(c/2))
c3 = c - 2*min(b,int(c/2))
q2 = q2 + min(a,int(b3/2))*3
if q1 > q2:
print(q1)
else:
print(q2)
``` | instruction | 0 | 57,258 | 19 | 114,516 |
Yes | output | 1 | 57,258 | 19 | 114,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
t = int(input())
o=[0]*t
p=[0]*t
r=[0]*t
for i in range(t):
o[i],p[i],r[i] = [int(x) for x in input().split()]
for i in range(t):
a=o[i]
b=p[i]
c=r[i]
Max=0
for x in range(min(101,a+1)):
for y in range(min(101,int(c/2)+1)):
if 2*x+y<=b and x+y>Max:
Max=x+y
print(3*Max)
``` | instruction | 0 | 57,259 | 19 | 114,518 |
Yes | output | 1 | 57,259 | 19 | 114,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
t = int(input())
for i in range(t):
[a,b,c] = list(map(float,input().split()))
s = 0
s1 = min(b,c//2)
b -= s1
c -= 2*s1
s2 = min(a,b//2)
s = 3*(s1+s2)
print(int(s))
``` | instruction | 0 | 57,260 | 19 | 114,520 |
Yes | output | 1 | 57,260 | 19 | 114,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
for testcases in range(t):
a,b,c=map(int,input().split())
ANS=0
ANS+=min(b,c//2)
b-=ANS
ANS+=min(a,b//2)
print(ANS*3)
``` | instruction | 0 | 57,261 | 19 | 114,522 |
Yes | output | 1 | 57,261 | 19 | 114,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
t=int(input())
a=[]
b=[]
c=[]
for i in range(t):
values=input().split()
abc=[int(x) for x in values]
a.append(abc[0])
b.append(abc[1])
c.append(abc[2])
def stones(t,a,b,c):
stones=0
while t>0:
if c>=2 and b>=1:
c-=2
b-=1
stones+=3
elif b>=2 and a>=1:
b-=2
a-=1
stones+=3
t-=1
return stones
for i in range(t):
print(stones(t,a[i],b[i],c[i]))
``` | instruction | 0 | 57,262 | 19 | 114,524 |
No | output | 1 | 57,262 | 19 | 114,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
t = int(input())
for i in range(t):
a, b, c = list(map(int, input().split()))
if b == 0:
print(0)
elif (b + c) >= (a + b) and b > 0 and c > 1:
count = 2 * (c // 2) + c // 2
b -= c // 2
c -= 2 * (c // 2)
if a > 0 and b > 1:
count += 2 * (b // 2) + b // 2
print(count)
elif (a + b) > (b + c) and a > 0 and b > 1:
count = 2 * (b // 2) + b // 2
a -= b // 2
b -= 2 * (b // 2)
if b > 0 and c > 1:
count += 2 * (c // 2) + c // 2
print(count)
else:
print(0)
``` | instruction | 0 | 57,263 | 19 | 114,526 |
No | output | 1 | 57,263 | 19 | 114,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
n = int(input())
mli = []
for i in range(n):
tli = []
tli = list(map(int, input().split(" ")))
mli.append(tli)
for i in range(n):
init = 0
ind = mli[i].index(max(mli[i][1:3]))
if(mli[i][0] == mli[i][1] and mli[i][1] == mli[i][2]):
while(mli[i][1] >= 1 and mli[i][2] >= 2):
init = init + 3
mli[i][1] = mli[i][1] - 1
mli[i][2] = mli[i][2] - 2
while(mli[i][0] >= 1 and mli[i][1] >= 2):
init = init + 3
mli[i][0] = mli[i][0] - 1
mli[i][1] = mli[i][1] - 2
elif(ind == 2):
while(mli[i][1] >= 1 and mli[i][2] >= 2):
init = init + 3
mli[i][1] = mli[i][1] - 1
mli[i][2] = mli[i][2] - 2
while(mli[i][0] >= 1 and mli[i][1] >= 2):
init = init + 3
mli[i][0] = mli[i][0] - 1
mli[i][1] = mli[i][1] - 2
elif(ind == 1 or ind == 0):
while(mli[i][0] >= 1 and mli[i][1] >= 2):
init = init + 3
mli[i][0] = mli[i][0] - 1
mli[i][1] = mli[i][1] - 2
while(mli[i][1] >= 1 and mli[i][2] >= 2):
init = init + 3
mli[i][1] = mli[i][1] - 1
mli[i][2] = mli[i][2] - 2
print(init)
init = 0
``` | instruction | 0 | 57,264 | 19 | 114,528 |
No | output | 1 | 57,264 | 19 | 114,529 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has 0 stones. Can you help her?
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases. Next t lines describe test cases in the following format:
Line contains three non-negative integers a, b and c, separated by spaces (0 ≤ a,b,c ≤ 100) — the number of stones in the first, the second and the third heap, respectively.
In hacks it is allowed to use only one test case in the input, so t = 1 should be satisfied.
Output
Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.
Example
Input
3
3 4 5
1 0 5
5 3 2
Output
9
0
6
Note
For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.
Submitted Solution:
```
dp = {}
def max_stones(a,b,c):
if(a==0 or b==0 or c==0):
return 0
elif((a,b,c) in dp.keys()):
return dp[(a,b,c)]
else:
opt1,opt2 = 0,0
if(a>=1 and b>=2):
opt1 = 3+max_stones(a-1,b-2,c)
if(b>=1 and c>=2):
opt2 = 3+max_stones(a,b-1,c-2)
dp[(a,b,c)] = max(opt1,opt2)
return dp[(a,b,c)]
#input
t = int(input())
for _ in range(t):
a,b,c = map(int,input().strip().split(' '))
ans = max_stones(a,b,c)
print(ans)
``` | instruction | 0 | 57,265 | 19 | 114,530 |
No | output | 1 | 57,265 | 19 | 114,531 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.