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.
"Duel!"
Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.
There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.
Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.
Input
The first line contains two integers n and k (1 โค k โค n โค 10^5).
The second line contains a single string of length n that only consists of 0 and 1, representing the situation of these n cards, where the color side of the i-th card faces up if the i-th character is 1, or otherwise, it faces down and the i-th character is 0.
Output
Print "once again" (without quotes) if the total number of their moves can exceed 10^9, which is considered a draw.
In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win.
Note that the output characters are case-sensitive, and any wrong spelling would be rejected.
Examples
Input
4 2
0101
Output
quailty
Input
6 1
010101
Output
once again
Input
6 5
010101
Output
tokitsukaze
Input
4 1
0011
Output
once again
Note
In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.
In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.
In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.
The fourth example can be explained in the same way as the second example does.
Submitted Solution:
```
n = list(map(int, input().split()))
st = input()
i = 0
mas = []
while i < len(st):
mas.insert(i, int(st[i]))
i = i + 1
i = 0
cnt = 0
while i < n[0]:
if(mas[i] == 1):
cnt = cnt + 1
i = i + 1
i = 0
cnt2 = 0
while i < n[1]:
if(mas[i] == 1):
cnt2 = cnt2 + 1
i = i + 1
ans = 0
mn = cnt2
id = 0
izm = 0
if(cnt - cnt2 == n[0] - n[1]) or ((n[0] - cnt) - (n[1] - cnt2) == n[0] - n[1]):
ans = 1
while i < n[0]:
if(mas[i] == 1):
cnt2 = cnt2 + 1
if(mas[i - n[1]] == 1):
cnt2 = cnt2 - 1
if(cnt - cnt2 == n[0] - n[1]) or ((n[0] - cnt) - (n[1] - cnt2) == n[0] - n[1]):
ans = 1
if(cnt2 <= mn):
mn = cnt2
id = i - n[1]
izm = 0
if(n[1] - cnt2 <= mn):
mn = n[1] - cnt2
id = i - n[1]
izm = 1
i = i + 1
if(ans == 1):
print("tokitsukaze")
exit(0)
i = 0
zer = [1000000, -1]
one = [1000000, -1]
10001
while i < n[0]:
if(mas[i] == 1):
one[0] = min(one[0], i)
one[1] = max(one[1], i)
if(mas[i] == 0):
zer[0] = min(zer[0], i)
zer[1] = max(zer[1], i)
i = i + 1
if(one[1] - one[0] - 1 >= n[1]) or(zer[1] - zer[0] -1 >= n[1]):
print("once again")
exit(0)
id2 = id
while id < id2 + n[1]:
mas[id] = izm
id = id + 1
i = 0
cnt2 = 0
cnt = 0
while i < n[0]:
if(mas[i] == 1):
cnt = cnt + 1
i = i + 1
i = 0
while i < n[1]:
if(mas[i] == 1):
cnt2 = cnt2 + 1
i = i + 1
ans = 0
if(cnt - cnt2 == n[0] - n[1]) or ((n[0] - cnt) - (n[1] - cnt2) == n[0] - n[1]):
ans = 1
while i < n[0]:
if(mas[i] == 1):
cnt2 = cnt2 + 1
if(mas[i - n[1]] == 1):
cnt2 = cnt2 - 1
if(cnt - cnt2 == n[0] - n[1]) or ((n[0] - cnt) - (n[1] - cnt2) == n[0] - n[1]):
ans = 1
i = i + 1
if(ans == 1):
print("quailty")
else:
print("once again")
``` | instruction | 0 | 48,918 | 19 | 97,836 |
Yes | output | 1 | 48,918 | 19 | 97,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"Duel!"
Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.
There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.
Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.
Input
The first line contains two integers n and k (1 โค k โค n โค 10^5).
The second line contains a single string of length n that only consists of 0 and 1, representing the situation of these n cards, where the color side of the i-th card faces up if the i-th character is 1, or otherwise, it faces down and the i-th character is 0.
Output
Print "once again" (without quotes) if the total number of their moves can exceed 10^9, which is considered a draw.
In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win.
Note that the output characters are case-sensitive, and any wrong spelling would be rejected.
Examples
Input
4 2
0101
Output
quailty
Input
6 1
010101
Output
once again
Input
6 5
010101
Output
tokitsukaze
Input
4 1
0011
Output
once again
Note
In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.
In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.
In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.
The fourth example can be explained in the same way as the second example does.
Submitted Solution:
```
n, k = map(int, input().split())
s = input()
p0 = s.rfind('0') - s.find('0')
p1 = s.rfind('1') - s.find('1')
if min(p0, p1) < k:
print('tokitsukaze')
elif p0 > k or p1 > k:
print('once again')
else:
c0 = 0
c1 = 0
check = False
for i in s:
if i == 0:
c1 = 0
c0 += 1
if c0 >= k:
check = True
break
else:
c0 = 0
c1 += 1
if c1 >= k:
check = True
break
print('once again' if check else 'quailty')
``` | instruction | 0 | 48,919 | 19 | 97,838 |
No | output | 1 | 48,919 | 19 | 97,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"Duel!"
Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.
There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.
Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.
Input
The first line contains two integers n and k (1 โค k โค n โค 10^5).
The second line contains a single string of length n that only consists of 0 and 1, representing the situation of these n cards, where the color side of the i-th card faces up if the i-th character is 1, or otherwise, it faces down and the i-th character is 0.
Output
Print "once again" (without quotes) if the total number of their moves can exceed 10^9, which is considered a draw.
In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win.
Note that the output characters are case-sensitive, and any wrong spelling would be rejected.
Examples
Input
4 2
0101
Output
quailty
Input
6 1
010101
Output
once again
Input
6 5
010101
Output
tokitsukaze
Input
4 1
0011
Output
once again
Note
In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.
In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.
In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.
The fourth example can be explained in the same way as the second example does.
Submitted Solution:
```
n,k=(map(int,input().split()))
cards=list(map(int,input().split()))
#print(n,k,cards)
if k==1 :
print('once again')
elif k==n or k==n-1:
print('tokitsukaze')
else:
print('quailty')
'''
if l.count(0) >1 or len(set(l))<n-1:
print('cslnb')
else:
x=sum(l)
if x%n !=0:
print('cslnb')
else:
print('sjfnb')
'''
``` | instruction | 0 | 48,920 | 19 | 97,840 |
No | output | 1 | 48,920 | 19 | 97,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"Duel!"
Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.
There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.
Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.
Input
The first line contains two integers n and k (1 โค k โค n โค 10^5).
The second line contains a single string of length n that only consists of 0 and 1, representing the situation of these n cards, where the color side of the i-th card faces up if the i-th character is 1, or otherwise, it faces down and the i-th character is 0.
Output
Print "once again" (without quotes) if the total number of their moves can exceed 10^9, which is considered a draw.
In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win.
Note that the output characters are case-sensitive, and any wrong spelling would be rejected.
Examples
Input
4 2
0101
Output
quailty
Input
6 1
010101
Output
once again
Input
6 5
010101
Output
tokitsukaze
Input
4 1
0011
Output
once again
Note
In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.
In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.
In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.
The fourth example can be explained in the same way as the second example does.
Submitted Solution:
```
import sys
def sum_range(l, r):
if r < l: return 0
if l == 0: return sum[r]
return sum[r] - sum[l - 1]
n, k = map(int, input().split())
cards = input()
sum = [0] * n
for i in range(1, n):
sum[i] += sum[i - 1]
if cards[i] == '1':
sum[i] += 1
min0 = min1 = n
max0 = max1 = -1
for i in range(0, n):
if cards[i] == '1':
min1 = min(min1, i)
max1 = i
else:
min0 = min(min0, i)
max0 = i
toki = False
qual = True
for i in range(0, n - k + 1):
if sum_range(0, i - 1) + sum_range(i + k, n - 1) + k == n:
toki = True
if sum_range(0, i - 1) + sum_range(i + k, n - 1) + 0 == 0:
toki = True
if i - min0 > k or i - min1 > k or max0 - (i + k - 1) > k or max1 - (i + k - 1) > k:
qual = False
if toki == True:
print('tokitsukaze')
elif qual == True:
print('quailty')
else:
print('once again')
``` | instruction | 0 | 48,921 | 19 | 97,842 |
No | output | 1 | 48,921 | 19 | 97,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"Duel!"
Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.
There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.
Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.
Input
The first line contains two integers n and k (1 โค k โค n โค 10^5).
The second line contains a single string of length n that only consists of 0 and 1, representing the situation of these n cards, where the color side of the i-th card faces up if the i-th character is 1, or otherwise, it faces down and the i-th character is 0.
Output
Print "once again" (without quotes) if the total number of their moves can exceed 10^9, which is considered a draw.
In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win.
Note that the output characters are case-sensitive, and any wrong spelling would be rejected.
Examples
Input
4 2
0101
Output
quailty
Input
6 1
010101
Output
once again
Input
6 5
010101
Output
tokitsukaze
Input
4 1
0011
Output
once again
Note
In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.
In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.
In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.
The fourth example can be explained in the same way as the second example does.
Submitted Solution:
```
import sys
n, k = map(int, input().split())
cc = input()
prefix = 1
i = 1
while cc[i] == cc[0]:
prefix +=1
suffix = 1
i = len(cc) - 2
while cc[i] == cc[-1]:
suffix +=1
if k + prefix >= n or k + suffix >= n or (cc[0] == cc[-1] and k+prefix+suffix >= n):
print("tokitsukaze")
sys.exit(0)
if 2 * k > n:
print("quailty")
sys.exit(0)
print("once again")
``` | instruction | 0 | 48,922 | 19 | 97,844 |
No | output | 1 | 48,922 | 19 | 97,845 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,058 | 19 | 98,116 |
Tags: data structures, greedy, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
dec = list(map(int, input().split()))
dic = {}
for i in range(n):
dic[dec[i]] = i
covered_till = n
new_dec = []
for i in range(n, 0, -1):
if dic[i] < covered_till: # we found new max
new_dec += dec[dic[i] : covered_till]
covered_till = dic[i]
print(*new_dec)
``` | output | 1 | 49,058 | 19 | 98,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,059 | 19 | 98,118 |
Tags: data structures, greedy, math
Correct Solution:
```
#####segfunc#####
def segfunc(x, y):
return max(x,y)
#################
#####ide_ele#####
ide_ele =-10**18
#################
class SegTree:
def __init__(self, init_val, segfunc, ide_ele):
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
for i in range(n):
self.tree[self.num + i] = init_val[i]
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
import sys
input = sys.stdin.readline
import math
import copy
t = int(input())
for f in range(t):
n = int(input())
p = list(map(int,input().split()))
ans = []
a = copy.copy(p)
a.reverse()
last = n
seg = SegTree(p,segfunc,ide_ele)
flg = seg.query(0,last)
for i in range(n):
if a[i] == flg:
for j in range(n-i-1,last):
ans.append(p[j])
last = n-i-1
flg = seg.query(0,last)
print(*ans)
``` | output | 1 | 49,059 | 19 | 98,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,060 | 19 | 98,120 |
Tags: data structures, greedy, math
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
p = list(map(int,input().split()))
l2 = []
a = p[0]
s = [0]
for i in range(1,n):
if(p[i]>a):
a = p[i]
s.append(i)
s = s[::-1]
ind = n
for j in s:
l2.extend(p[j:ind])
ind = j
print(*l2)
``` | output | 1 | 49,060 | 19 | 98,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,061 | 19 | 98,122 |
Tags: data structures, greedy, math
Correct Solution:
```
t = int(input())
r = []
for _ in range(t):
n = int(input())
nums = list(input().split())
pos = [0] * n
for i in range(n):
pos[int(nums[i])-1] = i
index = 0
end = n-1
s = ""
for i in range(n-1, -1, -1):
if pos[i] > end:
continue
for j in nums[pos[i]:end+1]:
print(j, end=" ")
end = pos[i] -1
``` | output | 1 | 49,061 | 19 | 98,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,062 | 19 | 98,124 |
Tags: data structures, greedy, math
Correct Solution:
```
#########################################################################################################\
#########################################################################################################
###################################The_Apurv_Rathore#####################################################
#########################################################################################################
#########################################################################################################
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):
"""
L is a list.
The function returns the power set, but as a list of lists.
"""
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)
#the function could stop here closing with
#return powerset
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
t = 1
t = int(input())
for _ in range(t):
n = ii()
l = li()
d = defaultdict(lambda:0)
for i in range(n):
d[l[i]]=i
maxpre = [0]*n
maxpre[0]=l[0]
for i in range(1,n):
maxpre[i]=max(maxpre[i-1],l[i])
# print(maxpre)
ans = []
end = n
i = maxpre[-1]
while(1):
# print(i)
ans+=l[i:end]
if i==0:
break
end = i
i = d[maxpre[i-1]]
print(*ans)
``` | output | 1 | 49,062 | 19 | 98,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,063 | 19 | 98,126 |
Tags: data structures, greedy, math
Correct Solution:
```
# cook your dish here
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
a = [0]*n
t = []
for i in range(n):
a[arr[i]-1] = i
f = n+1
for i in range(len(a)-1,-1,-1):
if(a[i]>f):
continue
t.append(arr[a[i]:f])
f = a[i]
for i in range(len(t)):
for j in range(len(t[i])):
print(t[i][j],end = " ")
print()
``` | output | 1 | 49,063 | 19 | 98,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,064 | 19 | 98,128 |
Tags: data structures, greedy, math
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from decimal import *
getcontext().prec = 25
MOD = pow(10, 9) + 7
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")
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split(" ")))
f = [0]*n
for i in range(n):
f[l[i]-1] = i
r = []
z = n
for i in range(n-1,-1,-1):
if f[i] < z:
r+= l[f[i]:z]
z = f[i]
print(*r)
``` | output | 1 | 49,064 | 19 | 98,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166. | instruction | 0 | 49,065 | 19 | 98,130 |
Tags: data structures, greedy, math
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
li=list(map(int,input().split()))
b={}
li1=[]
for i in range(n):
b[li[i]]=i
ck=n
for i in range(n,0,-1):
if b[i]<=ck:
for j in range(b[i],ck):
li1.append(li[j])
ck=b[i]
print(*li1)
``` | output | 1 | 49,065 | 19 | 98,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
test = int(input())
for _ in range(test):
nn = int(input())
l = list(map(int, input().split()))
mark = [0]*(nn+1)
i = nn-1
j = nn
ans = []
while i >= 0 and j >= 1:
k = i
while l[k]!=j: k-=1
for index in range(k, i+1):
ans.append(l[index])
mark[l[index]] = 1
i = k - 1
while mark[j] == 1: j-=1
for i in ans:
print(i, end = ' ')
print()
``` | instruction | 0 | 49,066 | 19 | 98,132 |
Yes | output | 1 | 49,066 | 19 | 98,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
# DEFINING SOME GOOD STUFF
from math import *
import threading
import sys
from collections import *
mod = 10 ** 9
inf = 10 ** 15
yes = 'YES'
no = 'NO'
# _______________________________________________________________#
def npr(n, r):
return factorial(n) // factorial(n - r) if n >= r else 0
def ncr(n, r):
return factorial(n) // (factorial(r) * factorial(n - r)) if n >= r else 0
def lower_bound(li, num):
answer = -1
start = 0
end = len(li) - 1
while (start <= end):
middle = (end + start) // 2
if li[middle] >= num:
answer = middle
end = middle - 1
else:
start = middle + 1
return answer # min index where x is not less than num
def upper_bound(li, num):
answer = -1
start = 0
end = len(li) - 1
while (start <= end):
middle = (end + start) // 2
if li[middle] <= num:
answer = middle
start = middle + 1
else:
end = middle - 1
return answer # max index where x is not greater than num
def abs(x):
return x if x >= 0 else -x
def binary_search(li, val, lb, ub):
# print(lb, ub, li)
ans = -1
while (lb <= ub):
mid = (lb + ub) // 2
# print('mid is',mid, li[mid])
if li[mid] > val:
ub = mid - 1
elif val > li[mid]:
lb = mid + 1
else:
ans = mid # return index
break
return ans
def kadane(x): # maximum sum contiguous subarray
sum_so_far = 0
current_sum = 0
for i in x:
current_sum += i
if current_sum < 0:
current_sum = 0
else:
sum_so_far = max(sum_so_far, current_sum)
return sum_so_far
def pref(li):
pref_sum = [0]
for i in li:
pref_sum.append(pref_sum[-1] + i)
return pref_sum
def SieveOfEratosthenes(n):
prime = [True for i in range(n + 1)]
p = 2
li = []
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
for p in range(2, len(prime)):
if prime[p]:
li.append(p)
return li
def primefactors(n):
factors = []
while (n % 2 == 0):
factors.append(2)
n //= 2
for i in range(3, int(sqrt(n)) + 1, 2): # only odd factors left
while n % i == 0:
factors.append(i)
n //= i
if n > 2: # incase of prime
factors.append(n)
return factors
def prod(li):
ans = 1
for i in li:
ans *= i
return ans
def dist(a, b):
d = abs(a[1] - b[1]) + abs(a[2] - b[2])
return d
def power_of_n(x, n):
cnt = 0
while (x % n == 0):
cnt += 1
x //= n
return cnt
def ask(l, r):
if l == r:
return -1
print("?", l, r)
sys.stdout.flush()
return int(input())
# _______________________________________________________________#
import itertools
sys.setrecursionlimit(300000)
# threading.stack_size(10**5) # remember it cause mle
# def main():
# for _ in range(1):
for _ in range(int(input()) if True else 1):
n = int(input())
# p,a,b,c = map(int, input().split())
a = list(map(int, input().split()))
# b = list(map(int, input().split()))
# c = list(map(int, input().split()))
# s = input()
# c = list(map(int,input().split()))
# adj = graph(n,m)
# d = defaultdict(int)
d = defaultdict()
for i in range(n):
d[a[i]] = i
ind = []
last = n
for i in range(n,0,-1):
# print('>>',d[i], last)
for j in range(d[i], last):
print(a[j], end=' ')
temp = d[i]
last = temp
print('')
'''
t = threading.Thread(target=main)
t.start()
t.join()
'''
``` | instruction | 0 | 49,067 | 19 | 98,134 |
Yes | output | 1 | 49,067 | 19 | 98,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
t=int(input())
def ceil(a,b):
if a%b:
return a//b+1
return a//b
for _ in range(t):
n=int(input())
l=list(map(int,input().split()))
maxi=[0]
for i in range(n):
maxi.append(max(maxi[-1],l[i]))
del(maxi[0])
j=n-1
ans=[]
while j>=0:
m=1
j-=1
while j>=0 and maxi[j]==maxi[j+1]:
j-=1
m+=1
e=j+1
while m:
ans.append(l[e])
e+=1
m-=1
print(*ans)
``` | instruction | 0 | 49,068 | 19 | 98,136 |
Yes | output | 1 | 49,068 | 19 | 98,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 23 12:30:26 2021
@author: MI
"""
n=int(input())
for i in range(n):
m=int(input())
a=list(map(int, input().split()))
maxrest=[0 for i in range(m)]
for j in range(1, m):
if a[j]>a[maxrest[j-1]]:
maxrest[j]=j
else:
maxrest[j]=maxrest[j-1]
rest=m
while rest!=0:
newrest=maxrest[rest-1]
for j in range(newrest, rest):
print(a[j], end=' ')
rest=newrest
``` | instruction | 0 | 49,069 | 19 | 98,138 |
Yes | output | 1 | 49,069 | 19 | 98,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
arr = list(map(int,input().split()))
out=[]
b=sorted(arr)
#print(b)
ind = []
for i in range(n):
ind.append(arr.index(b[i]))
while(n>0):
k=0
#print(ind)
for i in range(ind[n-1],n):
k+=1
print(arr[i],end=" ")
n=n-(k)
``` | instruction | 0 | 49,070 | 19 | 98,140 |
No | output | 1 | 49,070 | 19 | 98,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
def solution(n, plist):
reslist = []
while plist:
for i in range(len(plist)-1,-1,-1):
if plist[i] == len(plist):
reslist.extend(plist[i:])
plist = plist[:i]
# print(plist)
# print(reslist)
for num in reslist:
print(num, end=' ')
print()
return
t = int(input())
for tt in range(t):
n = int(input())
plist = list(map(int, input().split()))
solution(n, plist)
``` | instruction | 0 | 49,071 | 19 | 98,142 |
No | output | 1 | 49,071 | 19 | 98,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
l=a[a.index(n):]
k=len(a)
a=a[:a.index(n):]
a.reverse()
while a:
if len(a)>=k:
for i in range(k):
l.append(a[i])
else:
l.extend(a)
break
a=a[k:]
for i in l:
print(i,end=' ')
print()
``` | instruction | 0 | 49,072 | 19 | 98,144 |
No | output | 1 | 49,072 | 19 | 98,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as โ_{i = 1}^{n}{n^{n - i} โ
p_i}.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1 โค t โค 1000) โ the number of test cases.
The first line of each test case contains the single integer n (1 โค n โค 10^5) โ the size of deck you have.
The second line contains n integers p_1, p_2,..., p_n (1 โค p_i โค n; p_i โ p_j if i โ j) โ values of card in the deck from bottom to top.
It's guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p': p becomes [1, 2, 3], p' becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p' becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p' becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p' becomes [4, 3, 2, 1].
In result, p' has order equal to 4^3 โ
4 + 4^2 โ
3 + 4^1 โ
2 + 4^0 โ
1 = 256 + 48 + 8 + 1 = 313.
In the second test case, one of the optimal strategies is:
1. take 4 cards from the top of p and move it to p': p becomes [1], p' becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p': p becomes empty, p' becomes [5, 2, 4, 3, 1];
In result, p' has order equal to 5^4 โ
5 + 5^3 โ
2 + 5^2 โ
4 + 5^1 โ
3 + 5^0 โ
1 = 3125 + 250 + 100 + 15 + 1 = 3491.
In the third test case, one of the optimal strategies is:
1. take 2 cards from the top of p and move it to p': p becomes [4, 2, 5, 3], p' becomes [6, 1];
2. take 2 cards from the top of p and move it to p': p becomes [4, 2], p' becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p': p becomes empty, p' becomes [6, 1, 5, 3, 4, 2].
In result, p' has order equal to 6^5 โ
6 + 6^4 โ
1 + 6^3 โ
5 + 6^2 โ
3 + 6^1 โ
4 + 6^0 โ
2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166.
Submitted Solution:
```
testcases = int(input())
for i in range(testcases):
n = int(input())
deck = [int(i) for i in input().split(" ")]
newdeck = []
highestindex = deck.index(n)
deck, rest = deck[:highestindex], deck[highestindex:]
while(len(deck) > 0):
highestindex = deck.index(max(deck))
deck, rest = deck[:highestindex], deck[highestindex:]
newdeck.append(rest)
#for x in rest:
# print(x, end=" ")
newdeck = sum(newdeck, [])
print(*newdeck)
``` | instruction | 0 | 49,073 | 19 | 98,146 |
No | output | 1 | 49,073 | 19 | 98,147 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,429 | 19 | 98,858 |
"Correct Solution:
```
from bisect import *
N=int(input())
A=list(map(int,input().split()))
B=[0]*(N+1)
for i in range(N):
B[A[i]]+=1
C=[0]*(N+1)
for i in range(N+1):
C[B[i]]+=1
D=[0]
E=[0]
for i in range(N+1):
D.append(D[i]+i*C[i])
E.append(E[i]+C[-i-1])
E=E[::-1]
F=[]
for i in range(1,N+1):
F.append((D[i+1]//i)+E[i+1])
F=F[::-1]
for i in range(N):
print(N-bisect_left(F,i+1))
``` | output | 1 | 49,429 | 19 | 98,859 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,430 | 19 | 98,860 |
"Correct Solution:
```
import sys
from collections import Counter
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
c = Counter()
for x in a: c[x] += 1
cv = Counter()
for v in c.values(): cv[v] += 1
#print(cv)
def check(x, K):
t = 0
for k, v in cv.items(): t += min(k, x) * v
return t >= K * x
res = [N] * (N + 1)
for K in range(1, N):
ok = 0
ng = res[K] + 1
while ng - ok > 1:
m = (ok + ng) // 2
if check(m, K + 1): ok = m
else: ng = m
res[K + 1] = ok
for r in res[1: ]: print(r)
``` | output | 1 | 49,430 | 19 | 98,861 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,431 | 19 | 98,862 |
"Correct Solution:
```
#!/usr/bin/env python3
import sys
from collections import Counter
from itertools import accumulate
from collections import defaultdict
INF = float("inf")
def solve(N: int, A: "List[int]"):
c = Counter(A)
d = Counter(c.values())
d = tuple(accumulate([d[i] for i in range(N, -1, -1)]))[::-1]
S = [0]*(N+1)
for i in range(1, N+1):
S[i] = S[i-1] + d[i]
def isOK(y):
return S[y] < K*y
def binary_search(x):
ng = -1
ok = len(x)+1
while abs(ok - ng) > 1:
mid = (ok + ng)//2
if isOK(mid):
ok = mid
else:
ng = mid
return ng, ok
for K in range(1, N+1):
ng, ok = binary_search(range(N))
print(ng)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
A = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
solve(N, A)
if __name__ == '__main__':
main()
``` | output | 1 | 49,431 | 19 | 98,863 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,432 | 19 | 98,864 |
"Correct Solution:
```
from collections import defaultdict
from bisect import bisect
N = int(input())
A = list(map(int,input().split()))
d = defaultdict(lambda:0)
for i in range(N):
d[A[i]] += 1
C = [0]*(N+1)
for i in d:
C[d[i]] += 1
C1 = [0]*(N+1)
C2 = [0]*(N+1)
for i in range(N):
C1[i+1] = C1[i] + (i+1)*C[i+1]
C2[i+1] = C2[i] + C[i+1]
l = [0]*N
for n in range(N):
l[n] = C1[n+1]//(n+1)+C2[N]-C2[n+1]
l[n] *= -1
for i in range(N):
print(bisect(l,-(i+1)))
``` | output | 1 | 49,432 | 19 | 98,865 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,433 | 19 | 98,866 |
"Correct Solution:
```
from collections import Counter
import bisect
def main():
N = int(input())
A = list(map(int, input().split()))
count_list = list(Counter(A).values())
count_list.sort()
cumsum_count = [0] * len(count_list)
cumsum_count[0] = count_list[0]
for i in range(1, len(count_list)):
cumsum_count[i] = cumsum_count[i - 1] + count_list[i]
ans_list = [0] * N
n_max = N
for k in range(1, len(count_list) + 1):
# ๆๅคงใงใ N // k ๅใพใงใ
# ๅใซใผใ n_max ๆใพใงไฝฟใฃใฆ k * n_max ใฎ้ทๆนๅฝข้ ๅใๅ
จ้จๅใๅฐฝใใใใใ่ชฟในใใ
# ๅใๅฐฝใใใๆๅฐใฎ n_max ใ็ญใใ
n_max = min(n_max, N // k)
s = 0
while True:
# n_max ๆไปฅไธใใใซใผใใฏ n_max ๆๅบๅฎ
index = bisect.bisect_left(count_list, n_max)
s = n_max * (len(count_list) - index)
# ใใๆชๆบใฎใซใผใใฏใใฎใพใพๅ
จ้จไฝฟใ
if index > 0:
s += cumsum_count[index - 1]
if s >= k * n_max or n_max == 0:
break
n_max -= 1
ans_list[k - 1] = n_max
for ans in ans_list:
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 49,433 | 19 | 98,867 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,434 | 19 | 98,868 |
"Correct Solution:
```
import bisect
N = int(input())
count = {}
for x in map(int, input().split()):
count[x] = count.get(x, 0) + 1
a = sorted(count.values())
sums = a[:]
n = len(a)
for i in range(1, n):
sums[i] += sums[i-1]
def check(k, x):
pos = bisect.bisect_right(a, x)
if pos == 0:
return x * n >= k * x
return sums[pos-1]+ x * (n - pos) >= k * x
x = 0
res = []
for k in range(N, 0, -1):
while check(k, x+1):
x += 1
res.append(x)
for x in res[::-1]:
print(x)
``` | output | 1 | 49,434 | 19 | 98,869 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,435 | 19 | 98,870 |
"Correct Solution:
```
import bisect as b
n = int(input())
d = [0]*n
for el in map(int, input().split()):
d[el-1] += 1
d.sort()
s = [0]*(n+1)
for i in range(n):
s[i+1] = s[i] + d[i]
for k in range(1, n+1):
l = 0
r = n//k + 1
while(l+1 < r):
c = (l+r)//2
i = b.bisect_left(d, c)
total = s[i] + c * (n - i)
if total >= k * c:
l = c
else:
r = c
print(l)
``` | output | 1 | 49,435 | 19 | 98,871 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0 | instruction | 0 | 49,436 | 19 | 98,872 |
"Correct Solution:
```
from collections import Counter
from bisect import bisect_left
from itertools import accumulate
# https://betrue12.hateblo.jp/entry/2019/10/20/001106
N = int(input())
A = list(map(int, input().split()))
a = list(Counter(A).values())
a.sort()
b = list(accumulate(a))
length = len(a)
for i in range(1, N + 1):
if i == 1:
print(b[-1])
elif len(a) < i:
print(0)
else:
n = b[-1] // i
for j in reversed(range(1, n + 1)):
t = bisect_left(a, j)
if j * i <= b[t - 1] + (length - t) * j:
print(j)
break
``` | output | 1 | 49,436 | 19 | 98,873 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
from collections import Counter as C
N = int(input())
A = sorted(list(C([int(a) for a in input().split()]).values()))
X = [0] * 303030
for a in A:
X[a] += 1
s = 0
c = 0
B = [0] * 303030
for i in range(300030)[::-1]:
s += c
B[i] += s
c += X[i]
def chk(k, a):
if N - B[a] >= k * a: return 1
return 0
for i in range(1, N+1):
l, r = 0, N + 1
while r-l > 1:
m = (l+r) // 2
if chk(i, m):
l = m
else:
r = m
print(l)
``` | instruction | 0 | 49,437 | 19 | 98,874 |
Yes | output | 1 | 49,437 | 19 | 98,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
from itertools import accumulate
from math import floor
from bisect import bisect_left
def main():
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
c = list(c.values())
d = Counter(c)
d_li = [0] * (n + 1)
for k, v in d.items():
d_li[k] = v
dk_acm = list(accumulate(d_li))
kdk_acm = [0]
for k, e in enumerate(d_li[1:], 1):
kdk_acm.append(kdk_acm[-1] + k * e)
def f(x):
kdk_sm = kdk_acm[x]
dk_sm = dk_acm[n] - dk_acm[x]
return floor((kdk_sm + x * dk_sm) / x)
fs = [float("inf")] + [f(x) for x in range(1, n + 1)]
fs = fs[::-1]
for k in range(1, n + 1):
print(n - bisect_left(fs, k))
if __name__ == "__main__":
main()
``` | instruction | 0 | 49,438 | 19 | 98,876 |
Yes | output | 1 | 49,438 | 19 | 98,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string
INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7
n = I()
L = [(0, 0)] + sorted([(k, v) for k, v in Counter(Counter(LI()).values()).items()])
m = len(L)
acc = [0] * m
ret = 0
for j in range(m):
k, v = L[j]
ret += k * v
acc[j] = ret
v_acc = [0] * m
ret = 0
for l in range(m - 1, -1, -1):
ret += L[l][1]
v_acc[l] = ret
v_acc += [0]
for i in range(1, n + 1):
ok = 0
ng = n // i + 1
while ng > ok + 1:
mid = (ng + ok) // 2
lower_num = bisect_right(L, (mid, INF))
if acc[lower_num - 1] + v_acc[lower_num] * mid >= i * mid:
ok = mid
else:
ng = mid
print(ok)
``` | instruction | 0 | 49,439 | 19 | 98,878 |
Yes | output | 1 | 49,439 | 19 | 98,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
from collections import Counter
n=int(input())
A=list(Counter(map(int,input().split())).values())
A.sort()
m=len(A)
S=[0]*(m+1) # cumulative distribution
for i in range(m): S[i+1]=S[i]+A[i]
# output
from bisect import bisect_left
p=n
i=bisect_left(A,p) # pใซๅฟใใฆiใๅฎใพใ
for k in range(1,n+1):
while(S[i]+p*(m-i)<k*p): # p>=0 and ใฏๆธใใชใใฆใใใ
p-=1
i=bisect_left(A,p)
print(p)
resolve()
``` | instruction | 0 | 49,440 | 19 | 98,880 |
Yes | output | 1 | 49,440 | 19 | 98,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
#input
N = int(input())
A = sorted(list(map(int, input().split())))
l = len(A)
lx = len(list(set(A)))
for k in range(1, N + 1):
if k != 1 and lx != l:
print(lx//k)
else:
print(l//k)
``` | instruction | 0 | 49,441 | 19 | 98,882 |
No | output | 1 | 49,441 | 19 | 98,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
n = int(input())
li = [int(x) for x in input().split()]
l2 = set(li)
for k in range(1,n+1):
if k==1:
print(n)
else:
print(len(l2)//k)
``` | instruction | 0 | 49,442 | 19 | 98,884 |
No | output | 1 | 49,442 | 19 | 98,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
N = int(input())
A = list(map(int,input().split()))
A.sort()
c = []
hoge = 1
for i in range(1,N):
if A[i] == A[i-1]:
hoge += 1
else:
c.append(hoge)
hoge = 1
c.sort(reverse = True)
for i in range(N):
print(N)
if N > 0:
N -= C[i]
``` | instruction | 0 | 49,443 | 19 | 98,886 |
No | output | 1 | 49,443 | 19 | 98,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi has N cards. The i-th of these cards has an integer A_i written on it.
Takahashi will choose an integer K, and then repeat the following operation some number of times:
* Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)
For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.
Constraints
* 1 \le N \le 3 \times 10^5
* 1 \le A_i \le N
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_N
Output
Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.
Examples
Input
3
2 1 2
Output
3
1
0
Input
5
1 2 3 4 5
Output
5
2
1
1
1
Input
4
1 3 3 3
Output
4
1
0
0
Submitted Solution:
```
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
from collections import Counter
def binsearch(n: int, k: int, nexs: list):
left = 0
right = n // k + 1
while right - left > 1:
mid = (left + right) // 2
if nexs[mid * k - 1] < k:
right = mid
else:
left = mid
return left
n = ni()
a = list(li())
# ไธฆใณๆฟใ
b = []
cnt = 0
numlist = Counter(a)
while cnt < n:
newnumlist = {}
keys = sorted(list(numlist.keys()))
for key in keys:
val = numlist[key]
b.append(key)
cnt += 1
if val > 1:
newnumlist[key] = val - 1
numlist = newnumlist
# i็ช็ฎใฎๆฐใซใคใใฆใๆฌกใซๅใใใฎใๅบใใพใงใฎๆๅฐ่ท้ข
INF = float("inf")
temp = [INF]*(n+1)
nexs = [INF]*(2*n)
for idx in range(n):
if temp[b[idx]] != INF:
nexs[idx] = idx - temp[b[idx]]
temp[b[idx]] = idx
for i in range(1,len(nexs)):
nexs[i] = min(nexs[i-1], nexs[i])
# kใฎๅๆฐใใคๆฌกใซๅใใใฎใๅบใใพใงใฎ่ท้ขใkไปฅไธใงใใๆๅคงใฎๆฐใๆฑใใ
for k in range(1, n + 1):
if k == 1:
print(n)
else:
print(binsearch(n, k, nexs))
``` | instruction | 0 | 49,444 | 19 | 98,888 |
No | output | 1 | 49,444 | 19 | 98,889 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,445 | 19 | 98,890 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 1 20:46:31 2019
@author: Yamazaki Kenichi
"""
import time
N, A, B, C = map(int,input().split())
mod = 10**9+7
tnow = time.time()
def cmb(n, r, mod):
if (r < 0 or r > n):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
g1 = [1, 1] # ๅ
ใใผใใซ
g2 = [1, 1] # ้ๅ
ใใผใใซ
inv = [0, 1] # ้ๅ
ใใผใใซ่จ็ฎ็จใใผใใซ
for i in range(2, 2*10**5+1):
g1.append((g1[-1] * i) % mod)
inv.append((-inv[mod % i] * (mod//i)) % mod)
g2.append((g2[-1] * inv[-1]) % mod)
#้ๅ
ๆฑใใ
#def g(a,mod):
def g(a):
b, u, v = mod, 1,0
while b:
t = a//b
a -= t * b
u -= t * v
a,b,u,v = b,a,v,u
u %= mod
return u if u > 0 else u + mod
def modpow(a, n, mod):
res = 1
while n > 0:
if n & 1:
res = res * a % mod
a = a * a % mod
n >>= 1
return res
p1,p2 = A * g(A + B) % mod, B * g(A + B) % mod
#p1,p2 = A * g(100) % mod, B * g(100) % mod
p3 = (100 - C) * g(100) % mod
#p1,p2ใฎในใไนใฎใชในใ
P1, P2 = [1], [1]
for i in range(N+1):
P1.append(P1[-1] * p1 % mod)
P2.append(P2[-1] * p2 % mod)
ans = 0
for i in range(N):
ans += (N+i) * cmb(N+i-1, i, mod) * P1[N] * P2[i]
ans += (N+i) * cmb(N+i-1, i, mod) * P1[i] * P2[N]
ans %= mod
ans = ans * g(p3) % mod
#ans = 0
#for i in range(N):
# ans += (N+i)*cmb(N+i-1,i,mod)*((modpow(p1,N,mod) * modpow(p2,i,mod)
# +(modpow(p2,i,mod) * modpow(p2,N,mod)))%mod)
# ans %= mod
#ans = ans * g(p3) % mod
print(ans)
#tnow = time.time()
#pow(p1,100000)
#tnow2 = time.time()
#tnow3 = time.time()
#print(tnow2 - tnow)
#print(tnow3 - tnow2)
``` | output | 1 | 49,445 | 19 | 98,891 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,446 | 19 | 98,892 |
"Correct Solution:
```
N, A, B, C = map(int, input().split())
mod = 10 ** 9 + 7
W = [1] * (2 * N + 1)
for w in range(2 * N):
W[w + 1] = ((w + 1) * W[w]) % mod
def rev(a):
return pow(a, mod - 2, mod)
def comb(a, b):
return (W[a] * rev(W[b]) * rev(W[a - b])) % mod
def f(a, b):
res = 0
for i in range(N):
res += ((i + N) * com[i] * pow(b, i, mod)) % mod
res %= mod
return (pow(a, N, mod) * res) % mod
a = A * rev(A + B)
b = B * rev(A + B)
c = C * rev(100)
com = [comb(i + N - 1, i) for i in range(N)]
w_a = f(a, b)
w_b = f(b, a)
ans = ((w_a + w_b) * rev(1 - c)) % mod
print(ans)
``` | output | 1 | 49,446 | 19 | 98,893 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,447 | 19 | 98,894 |
"Correct Solution:
```
from itertools import*
import math
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)
k,A,B,C = map(int,input().split())
Max = max(2*k+10,101)
#ไบ้
ไฟๆฐใจใใฎ้ๅ
ใใผใใซใไฝใๅๅฆ็
fac = [0]*(Max)
finv = [0]*(Max)
inv = [0]*(Max)
fac[0]=fac[1]=1
finv[0]=finv[1]=1
inv[1]=1
for i in range(2,Max):
fac[i] = fac[i-1] * i % mod
inv[i] = mod - inv[mod%i]*(mod//i)%mod
finv[i] = finv[i-1]*inv[i]%mod
#O(1)ใงmod่จ็ฎใใ็ตๅใๆฐใ่จ็ฎ
def Comb(n,r):
if n < r:
return 0
if n < 0 or r < 0 :
return 0
return fac[n]*(finv[r]*finv[n-r]%mod)%mod
def pow_mod(x,n):
if n==0: return 1
if n%2:return x*pow_mod(x,n-1)%mod
a = pow_mod(x,n//2)
return a*a%mod
M = A+B
a = A*inv[M]%mod
b = B*inv[M]%mod
E = 0
for n in range(k,2*k):
E += (((n)*pow_mod(a,k)%mod)*pow_mod(b,n-k)%mod)*Comb(n-1,k-1)%mod
E += (((n)*pow_mod(b,k)%mod)*pow_mod(a,n-k)%mod)*Comb(n-1,k-1)%mod
E %= mod
E = E*100*inv[100-C]%mod
print(E)
``` | output | 1 | 49,447 | 19 | 98,895 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,448 | 19 | 98,896 |
"Correct Solution:
```
def gcd(a,b):
if b>a:
a,b=b,a
while b:
a,b=b, a%b
return a
def inv(a):
m=10**9+7
b=m
(x, lastx) = (0, 1)
(y, lasty) = (1, 0)
while b != 0:
q = a // b
(a, b) = (b, a % b)
(x, lastx) = (lastx - q * x, x)
(y, lasty) = (lasty - q * y, y)
return lastx % m
def main():
MOD=10**9+7
n,a,b,c=map(int,input().split())
frac=[1]
for i in range(1,2*n):
frac.append(frac[-1]*i%MOD)
fracinv=[inv(frac[i]) for i in range(2*n)]
powa,powb,pow100=[1],[1],[1]
for i in range(n):
powa.append(powa[-1]*a%MOD)
powb.append(powb[-1]*b%MOD)
for i in range(2*n):
pow100.append(pow100[-1]*(100-c)%MOD)
ans=0
for m in range(n,2*n):
tmpp=frac[m-1]*fracinv[n-1]*fracinv[m-n]%MOD*(powa[n]*powb[m-n]+powa[m-n]*powb[n])*m%MOD
tmpq=pow100[m+1]
ans=(ans+tmpp*inv(tmpq))%MOD
print(ans*100%MOD)
if __name__ == "__main__":
main()
``` | output | 1 | 49,448 | 19 | 98,897 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,449 | 19 | 98,898 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
# factorials
_nfact = 2 * (10 ** 5) + 10
facts = [0] * _nfact
facts[0] = 1
for i in range(1, _nfact):
facts[i] = (facts[i - 1] * i) % MOD
def binomial(m, n):
return DIV(facts[m], facts[n] * facts[m - n])
def main():
N, A, B, C = LI()
D = DIV(100, 100 - C)
ans = 0
ap, bp, abp = [1], [1], [POW(A + B, N)]
for _ in range(0, N + 1):
ap.append(ap[-1] * A % MOD)
bp.append(bp[-1] * B % MOD)
abp.append(abp[-1] * (A + B) % MOD)
for m in range(N, 2 * N):
x = binomial(m - 1, N - 1) * DIV(
ap[N] * bp[m - N] + ap[m - N] * bp[N],
abp[m - N]) % MOD
y = (m * D) % MOD
ans = (ans + (x * y) % MOD) % MOD
return ans
print(main())
``` | output | 1 | 49,449 | 19 | 98,899 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,450 | 19 | 98,900 |
"Correct Solution:
```
M=10**9+7
N,A,B,C=map(int,input().split())
f=[1]
for i in range(1,2*N):
f.append(f[-1]*i%M)
t=pow(A+B,M-2,M)
A*=t
B*=t
a=pow(A,N,M)
b=pow(B,N,M)
z=0
for i in range(N):
z+=100*t*f[N+i]*pow(f[N-1]*f[i],M-2,M)*(a+b)
z%=M
a=a*B%M
b=b*A%M
print(z)
``` | output | 1 | 49,450 | 19 | 98,901 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,451 | 19 | 98,902 |
"Correct Solution:
```
class Combinatorics:
def __init__(self, N, mod):
'''
Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N)
over the finite field Z/(mod)Z.
Input:
N (int): maximum n
mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated.
'''
self.mod = mod
self.fact = {i: None for i in range(N+1)} # n!
self.inverse = {i: None for i in range(1, N+1)} # inverse of n in the field Z/(MOD)Z
self.fact_inverse = {i: None for i in range(N+1)} # inverse of n! in the field Z/(MOD)Z
# preprocess
self.fact[0] = self.fact[1] = 1
self.fact_inverse[0] = self.fact_inverse[1] = 1
self.inverse[1] = 1
for i in range(2, N+1):
self.fact[i] = i * self.fact[i-1] % self.mod
q, r = divmod(self.mod, i)
self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod
self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod
def perm(self, n, r):
'''
Calculate nPr = n! / (n-r)! % mod
'''
if n < r or n < 0 or r < 0:
return 0
else:
return (self.fact[n] * self.fact_inverse[n-r]) % self.mod
def binom(self, n, r):
'''
Calculate nCr = n! /(r! (n-r)!) % mod
'''
if n < r or n < 0 or r < 0:
return 0
else:
return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod
def hom(self, n, r):
'''
Calculate nHr = {n+r-1}Cr % mod.
Assign r objects to one of n classes.
Arrangement of r circles and n-1 partitions:
o o o | o o | | | o | | | o o | | o
'''
if n == 0 and r > 0:
return 0
if n >= 0 and r == 0:
return 1
return self.binom(n + r - 1, r)
def extended_euclid(a, b):
x1, y1, m = 1, 0, a
x2, y2, n = 0, 1, b
while m % n != 0:
q, r = divmod(m, n)
x1, y1, m, x2, y2, n = x2, y2, n, x1 - q * x2, y1 - q * y2, r
return (x2, y2, n)
def modular_inverse(a, mod):
x, _, g = extended_euclid(a, mod)
if g != 1:
return None # Modular inverse of a does not exist
else:
return x % mod
N, A, B, C = map(int, input().split())
MOD = 10**9 + 7
com = Combinatorics(2*N, MOD)
pa = A * modular_inverse(A+B, MOD); pb = B * modular_inverse(A+B, MOD);
n_first_AB = (100 * modular_inverse(100 - C, MOD)) % MOD
ans = 0
for m in range(N, 2*N): # E[X] = E_M[ E[X | M] ]
Exp_X_given_M = (m * n_first_AB) % MOD
prob_m = com.binom(m-1, N-1) * ((pow(pa, N, MOD) * pow(pb, m-N, MOD)) % MOD + (pow(pa, m-N, MOD) * pow(pb, N, MOD)) % MOD) % MOD
ans = (ans + Exp_X_given_M * prob_m) % MOD
print(ans)
``` | output | 1 | 49,451 | 19 | 98,903 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146 | instruction | 0 | 49,452 | 19 | 98,904 |
"Correct Solution:
```
def main():
mod = 10**9+7
fact = [1]*200001
for i in range(1, 200001):
fact[i] = fact[i-1]*i % mod
def inv_n(n, mod=10**9+7): return pow(n, mod-2, mod)
def nCr(n, r, mod=10**9 +
7): return inv_n(fact[n-r]*fact[r] % mod, mod)*fact[n] % mod
n, a, b, c = map(int, input().split())
pan = pow(a, n, mod)
pbn = pow(b, n, mod)
ivab = 100*inv_n(a+b) % mod
ans = 0
for i in range(n, 2*n):
ans = (ans + i*ivab*nCr(i-1, n-1)*(pan*pow(b, i-n, mod) +
pow(a, i-n, mod)*pbn)*inv_n(pow(a+b, i, mod))) % mod
print(ans)
main()
``` | output | 1 | 49,452 | 19 | 98,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146
Submitted Solution:
```
N, A, B, C = map(int, input().split())
P = 10**9+7
fa = [1]
al = A * pow(A + B, P-2, P) % P
be = B * pow(A + B, P-2, P) % P
ga = C * pow(100, P-2, P) % P
for i in range(1, N*2+100):
fa.append(fa[-1]*i%P)
ans = 0
for i in range(N):
ans = (ans + fa[N+i-1] * pow((fa[N-1]*fa[i]), P-2, P) * (pow(al, N, P) * pow(be, i, P) + pow(al, i, P) * pow(be, N, P)) * (i+N)) % P
print(ans * pow(1-ga, P-2, P) % P)
``` | instruction | 0 | 49,453 | 19 | 98,906 |
Yes | output | 1 | 49,453 | 19 | 98,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146
Submitted Solution:
```
def cmb(n, r):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9+7 #ๅบๅใฎๅถ้
N = 2*10**5+3
g1 = [1, 1] # ๅ
ใใผใใซ
g2 = [1, 1] #้ๅ
ใใผใใซ
inverse = [0, 1] #้ๅ
ใใผใใซ่จ็ฎ็จใใผใใซ
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
n,a,b,c=map(int,input().split())
ans=0
for i in range(n):#A,B:nๅ,iๅ
ans=(ans+pow(a,n,mod)*pow(b,i,mod)*cmb(n-1+i,i)*(n+i)*pow(pow(a+b,mod-2,mod),n+i,mod))%mod
for i in range(n):
ans=(ans+pow(b,n,mod)*pow(a,i,mod)*cmb(n-1+i,i)*(n+i)*pow(pow(a+b,mod-2,mod),n+i,mod))%mod
print(ans*100*pow(a+b,mod-2,mod)%mod)
``` | instruction | 0 | 49,454 | 19 | 98,908 |
Yes | output | 1 | 49,454 | 19 | 98,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
# factorials
_nfact = 2 * (10 ** 5) + 10
facts = [0] * _nfact
t = facts[0] = 1
for i in range(1, _nfact):
t = (t * i) % MOD
facts[i] = t
ifacts = [0] * _nfact
t = ifacts[-1] = INV(facts[-1])
for i in range(_nfact - 1, 0, -1):
t = (t * i) % MOD
ifacts[i - 1] = t
def binomial(m, n):
return facts[m] * ifacts[n] * ifacts[m - n]
def main():
N, A, B, C = LI()
D = DIV(100, 100 - C)
ans = 0
ap, bp, abp = [1], [1], [POW(A + B, N)]
for _ in range(0, N + 1):
ap.append(ap[-1] * A % MOD)
bp.append(bp[-1] * B % MOD)
abp.append(abp[-1] * (A + B) % MOD)
for m in range(N, 2 * N):
x = binomial(m - 1, N - 1) * DIV(
ap[N] * bp[m - N] + ap[m - N] * bp[N],
abp[m - N]) % MOD
y = (m * D) % MOD
ans = (ans + (x * y) % MOD) % MOD
return ans
print(main())
``` | instruction | 0 | 49,455 | 19 | 98,910 |
Yes | output | 1 | 49,455 | 19 | 98,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146
Submitted Solution:
```
import sys
def solve():
file = sys.stdin.readline
N, A, B, C = map(int, file().split())
mod = 7 + 10 ** 9
nfact = [1] * (2 * N)
nrev = [1] * (2 * N)
Apow, Bpow, Hpow = [A] * (2 * N), [B] * (2 * N), [A + B] * (2 * N)
Apow[0] = 1
Bpow[0] = 1
Hpow[0] = 1
Crev = (pow(100 - C, mod - 2, mod) * 100) % mod
for i in range(2, 2 * N):
nfact[i] = (nfact[i-1] * i) % mod
Apow[i] = (Apow[i-1] * A) % mod
Bpow[i] = (Bpow[i-1] * B) % mod
Hpow[i] = (Hpow[i-1] * (A + B)) % mod
nrev[2 * N - 1] = pow(nfact[2 * N - 1], mod - 2, mod)
Hpow[2 * N - 1] = pow(Hpow[2 * N - 1] , mod - 2, mod)
for i in reversed(range(1, 2 * N - 1)):
nrev[i] = (nrev[i + 1] * (i + 1)) % mod
Hpow[i] = (Hpow[i + 1] * (A + B)) % mod
ans = 0
for i in range(N, 2 * N):
ans += (((nfact[i-1] * nrev[N-1] * nrev[i-N]) % mod) * ((Apow[N]*Bpow[i-N] + Apow[i-N]*Bpow[N]) % mod) * i * Hpow[i] * Crev) % mod
ans %= mod
print(ans)
return
if __name__ == "__main__":
solve()
``` | instruction | 0 | 49,456 | 19 | 98,912 |
Yes | output | 1 | 49,456 | 19 | 98,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi and Aoki will play a game. They will repeatedly play it until one of them have N wins in total.
When they play the game once, Takahashi wins with probability A %, Aoki wins with probability B %, and the game ends in a draw (that is, nobody wins) with probability C %. Find the expected number of games that will be played, and print it as follows.
We can represent the expected value as P/Q with coprime integers P and Q. Print the integer R between 0 and 10^9+6 (inclusive) such that R \times Q \equiv P\pmod {10^9+7}. (Such an integer R always uniquely exists under the constraints of this problem.)
Constraints
* 1 \leq N \leq 100000
* 0 \leq A,B,C \leq 100
* 1 \leq A+B
* A+B+C=100
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B C
Output
Print the expected number of games that will be played, in the manner specified in the statement.
Examples
Input
1 25 25 50
Output
2
Input
4 50 50 0
Output
312500008
Input
1 100 0 0
Output
1
Input
100000 31 41 28
Output
104136146
Submitted Solution:
```
m = 10**9+7
def inv(x):
m = 10**9+7
t = x
y = 1
while True:
if t == 1:
break
y *= m // t
t = m % t
y *= -1
y %= m
return y
import math
def cc(x,y):
return math.factorial(x) // math.factorial(y) // math.factorial(x-y)
n,a,b,c = map(int,input().split())
d = 100 - c
awin = a * inv(d)
bwin = b * inv(d)
asc = 0
bsc = 0
for i in range(n):
p = cc(n-1,i) * awin**(n-1) * bwin**i
asc += p
asc %= m
for i in range(n):
p = cc(n-1,i) * awin**i * bwin**(n-1)
bsc += p
bsc %= m
sc = asc * awin + bsc * bwin
sc %= m
scn = sc * 100 * inv(d)
scn %= m
print(scn)
``` | instruction | 0 | 49,457 | 19 | 98,914 |
No | output | 1 | 49,457 | 19 | 98,915 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.