description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | res = []
for _ in range(int(input())):
n = int(input())
S = list(map(int, input().split()))
result = 0
for i in range(n):
rejumps = 0
for j in range(i):
rejumps += max(0, S[j] - (i - j))
result = max(result, S[i] + rejumps - 1)
res.append(result)
print(*res, sep="\n") | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | for _ in range(int(input())):
n = int(input())
s = [int(i) for i in input().split()]
passes = 0
jumps = [0] * (n + 1)
for ind in range(n):
jump = jumps[ind]
if s[ind] - 1 > jump:
passes += s[ind] - 1 - jump
jump += s[ind] - 1 - jump
jumps[ind + 1] += jump - s[ind] + 1
for inner_ind in range(ind + 2, min(n, ind + 1 + s[ind])):
jumps[inner_ind] += 1
print(passes) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | t = int(input())
for _ in range(t):
n = int(input())
nums = list(map(int, input().split()))
minus = [0] * n
ans = 0
for i in range(n):
ans += nums[i] - minus[i] - 1
cur = 0
for j in range(i + 2, n):
if j <= i + nums[i]:
cur += 1
if not cur:
break
if nums[j] - minus[j] - cur >= 1:
minus[j] += cur
cur = 0
else:
p = nums[j] - minus[j] - 1
minus[j] += p
cur -= p
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
n = int(input())
a = list(map(int, input().split()))
l = [0] * n
ans = 0
for i in range(n):
x = a[i] - 1 - l[i]
ans += max(x, 0)
for j in range(min(a[i], n - i - 1) - 1):
l[i + 2 + j] += 1
if x < 0:
x = abs(x)
if i < n - 1:
l[i + 1] += x
print(ans)
t -= 1 | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def solve():
t = int(input())
listOfArr = []
for x in range(t):
n = int(input())
listOfArr.append(list(map(int, input().split())))
for x in range(t):
currentArr = listOfArr[x]
ans = 0
for y in range(len(currentArr) - 1, -1, -1):
for z in range(y + 2, min(len(currentArr) - 1, y + currentArr[y]) + 1):
currentArr[z] -= 1
for i in range(len(currentArr) - 1):
if currentArr[i] <= 0:
currentArr[i + 1] -= abs(currentArr[i]) + 1
ans += max(0, currentArr[i] - 1)
ans += max(0, currentArr[-1] - 1)
print(ans)
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | t = int(input())
for _ in range(t):
n = int(input())
a = [0] + [int(i) for i in input().split()]
dp = [0] * (n + 5)
res = 0
cnt = 0
s = 0
for i in range(1, n + 1):
s += dp[i]
cnt += s
if cnt >= a[i] - 1:
cnt -= a[i]
cnt += 1
else:
res += a[i]
res -= cnt
res -= 1
cnt = 0
dp[i + 2] += 1
mi = min(i + a[i] + 1, n + 1)
dp[mi] -= 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
g = [0] * (n + 5)
las = 0
for i in range(n):
tmp = g[i] + las
las = tmp
if tmp > a[i] - 1:
g[i + 1] += tmp - a[i] + 1
g[i + 2] -= tmp - a[i] + 1
else:
ans += a[i] - 1 - tmp
if i + 2 <= min(n - 1, i + a[i]):
g[i + 2] += 1
g[min(n - 1, i + a[i]) + 1] -= 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def read_list() -> list:
return [int(i) for i in input().strip().split()]
def read_num() -> int:
return int(input().strip())
t = read_num()
for _ in range(t):
n = read_num()
arr = read_list()
ans = 0
cnt_change = [0] * (n + 5)
cnt = 0
for i in range(n):
cnt += cnt_change[i]
ans += max(arr[i] - 1 - cnt, 0)
cnt_change[i + 2] += 1
cnt_change[min(n - 1, i + arr[i]) + 1] -= 1
tmp = max(cnt - arr[i] + 1, 0)
cnt_change[i + 1] += tmp
cnt_change[i + 2] -= tmp
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
def solution(n, a):
s_s_p = [(0) for _ in range(n)]
s_s_p[-1] = a[-1] - 1
if n > 1:
s_s_p[-2] = a[-2] - 1
for p in range(n - 3, -1, -1):
val = a[p]
if val > 1:
if p + val > n - 1:
s_s_p[p] += p + val - (n - 1)
carry = 0
for r in range(p + 2, n):
if r <= p + val:
carry += 1
if carry >= s_s_p[r]:
carry -= s_s_p[r]
s_s_p[p] += s_s_p[r]
s_s_p[r] = 0
else:
s_s_p[r] -= carry
s_s_p[p] += carry
carry = 0
s_s_p[p] += carry
print(sum(s_s_p))
T = int(input())
for t in range(T):
n = int(input())
a = list(map(int, input().split()))
solution(n, a) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | t = int(input().strip())
for _ in range(t):
n = int(input().strip())
res = 0
jump = [0] * (n + 1)
for i, s in enumerate(map(int, input().strip().split())):
if s != 1:
m = min(n + 1, i + s + 1)
res += s - m + i + 1
for x in range(i + 2, m):
jump[x] += 1
jump[i] = max(0, jump[i] + 1 - s)
jump[i + 1] += jump[i]
print(res + jump[-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N = int(input())
A = [int(a) for a in input().split()]
B = [0] * N
ans = 0
for i in range(N):
a = A[i]
ans += max(a - 1 - B[i], 0)
for j in range(2, min(a + 1, N - i)):
B[i + j] += 1
if i + 1 < N:
B[i + 1] += max(B[i] - (a - 1), 0)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def solve(N, nums):
ans = 0
for i in range(N):
temp = 0
for j in range(i):
temp += max(0, nums[j] - (i - j))
ans = max(ans, temp + nums[i] - 1)
return ans
T = int(input())
for _ in range(T):
N = int(input())
nums = list(map(int, input().split()))
ans = solve(N, nums)
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | for _ in range(int(input())):
n = int(input())
arr = [int(x) for x in input().split()]
extra = [0] * n
cost = 0
for i in range(n):
if arr[i] == 1:
if extra[i] > 0:
if i != n - 1:
extra[i + 1] += extra[i]
elif extra[i] + 1 < arr[i]:
cost += max(0, arr[i] - extra[i] - 1)
for j in range(min(n - 1, i + arr[i]), min(n - 1, i + 1), -1):
extra[j] += 1
else:
for j in range(min(n - 1, i + arr[i]), min(n - 1, i + 1), -1):
extra[j] += 1
if i != n - 1:
extra[i + 1] += extra[i] + 1 - arr[i]
print(cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def array(f):
return list(map(f, input().split()))
def slv():
n = int(input())
a = array(int)
rabbit = [0] * (n + 1)
ans = 0
for i, v in enumerate(a):
if v - rabbit[i] > 1:
new_rabbit = v - rabbit[i] - 1
rabbit[i] += new_rabbit
ans += new_rabbit
for j in range(max(i + v - n, 0), rabbit[i]):
if i + v - j <= i:
rabbit[i + 1] += rabbit[i] - j
break
rabbit[min(n, i + v - j)] += 1
print(ans)
return
def main():
T = int(input())
for _ in range(T):
slv()
main() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def main():
t = int(input())
result = []
for _ in range(t):
n = int(input())
s = list(map(int, input().split()))
ans = 0
jumps_cnt = [0] * (n + 1)
for i in range(n):
curr_jumps = jumps_cnt[i]
if curr_jumps < s[i] - 1:
ans += s[i] - 1 - curr_jumps
curr_jumps += s[i] - 1 - curr_jumps
jumps_cnt[i + 1] += curr_jumps - (s[i] - 1)
for j in range(i + 2, min(n, i + s[i] + 1)):
jumps_cnt[j] += 1
result.append(str(ans))
print("\n".join(result))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def solution(n):
pref = [(0) for _ in range(n + 1)]
s = [int(i) for i in input().split()]
for i in range(n):
if s[i] > 1:
pref[min(i + 2, n)] += 1
pref[min(s[i] + 1 + i, n)] -= 1
for i in range(n):
pref[i + 1] += pref[i]
for i in range(n):
if pref[i] - s[i] + 1 > 0:
pref[i + 1] += pref[i] - s[i] + 1
ans = 0
for i in range(n):
ans += max(s[i] - pref[i] - 1, 0)
print(ans)
for t in range(int(input())):
n = int(input())
solution(n) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
s = [int(x) for x in input().split()]
a = []
for i in range(n):
a.append((i + 1, i))
a.append((s[i] + i, i))
a.sort()
j = 0
o = set()
ans = 0
b = []
for i in range(n):
while a[j][0] < i:
if a[j][1] in o:
o.remove(a[j][1])
else:
o.add(a[j][1])
j += 1
b.append(s[i] - 1 - len(o))
for i in range(len(b) - 1):
if b[i] < 0:
b[i + 1] -= abs(b[i])
ans = 0
for i in b:
if i > 0:
ans += i
print(ans) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
S = list(map(int, input().split()))
todo = [0] * n
out = 0
for i in range(n):
need = S[i] - 1
smol = todo[i]
act = max(smol, need)
out += act - smol
for j in range(2, S[i] + 1):
if i + j >= n:
break
todo[i + j] += 1
if i != n - 1:
todo[i + 1] += act - need
print(out) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
pin = sys.stdin.readline
def aupdate(n, v):
while n < len(tree):
tree[n] += v
n += n & -n
def update(l, d, v):
aupdate(l, v)
aupdate(l + d, -v)
def query(n):
ans = 0
while n:
ans += tree[n]
n -= n & -n
return ans
for T in range(int(pin())):
N = int(pin())
A = [*map(int, pin().split())]
tree = [(0) for _ in range(N + 2)]
S = 0
for n in range(1, N + 1):
C = A[n - 1] - query(n)
S += max(C - 1, 0)
if C < 1:
update(n + 1, 1, 1 - C)
if A[n - 1] == 1:
continue
else:
update(n + 2, A[n - 1] - 1, 1)
print(S) | IMPORT ASSIGN VAR VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
ar = list(map(int, input().split()))
li = [0] * n
ans = 0
for i in range(n):
xx = ar[i] - 1 - li[i]
tem = max(xx, 0)
ans += tem
if xx < 0 and i + 1 < n:
li[i + 1] += abs(xx)
for j in range(i + 2, min(i + ar[i] + 1, n)):
li[j] += 1
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
def main():
n = int(input())
slst = list(map(int, input().split()))
ans = 0
minus = [0] * (n + 1)
for i, s in enumerate(slst):
tmp = s - 1
for j in range(2, s + 1):
pos = i + j
try:
minus[pos] += 1
except:
break
if tmp - minus[i] > 0:
ans += tmp - minus[i]
else:
minus[i + 1] += minus[i] - tmp
print(ans)
for _ in range(int(input())):
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def solve():
n = int(input())
s = list(map(int, input().split()))
on = [0] * n
ans = 0
for i, x in enumerate(s):
ans += max(0, x - on[i] - 1)
on[i] += max(0, x - on[i] - 1)
for j in range(2, min(n - i, x + 1)):
on[i + j] += 1
on[i] -= x - 1
if i < n - 1 and on[i] > 0:
on[i + 1] += on[i]
print(ans)
t = int(input())
for _ in range(t):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
dp = []
dp.append([max(0, a[0] - 1)])
dp[0].append(0)
for i in range(1, n):
temp = 0
for j in range(0, i - 1):
if a[j] + j >= i:
temp += 1
temp += dp[i - 1][1]
if temp >= a[i] - 1:
dp.append([dp[i - 1][0]])
dp[i].append(temp + 1 - a[i])
else:
dp.append([dp[i - 1][0] + a[i] - 1 - temp])
dp[i].append(0)
print(dp[n - 1][0]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split()))
mod = 10**9 + 7
Mod = 998244353
INF = float("inf")
tc = 1
(tc,) = inp()
for _ in range(tc):
(n,) = inp()
a = inp()
ans = 0
free = [0] * n
for i in range(n):
a[i] -= free[i]
ans += max(0, a[i] - 1)
for j in range(i + 2, min(n, i + a[i] + free[i] + 1)):
free[j] += 1 * (a[i] + free[i] > 1)
if i + 1 < n:
free[i + 1] += max(0, 1 - a[i])
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def func(A):
n = len(A)
C = [(0) for x in range(n)]
res = 0
for i in range(n):
if A[i] - C[i] > 1:
res += A[i] - 1 - C[i]
C[i] = A[i] - 1
try:
C[i + 1] += C[i] - A[i] + 1
except:
pass
for j in range(i + 2, min(n - 1, i + A[i]) + 1):
C[j] += 1
return res
t = int(input())
for i in range(t):
n = int(input())
A = list(map(int, input().split()))
print(func(A)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
pre = [(0) for i in range(n)]
ans = 0
for i in range(n):
jumps = [(0) for j in range(n)]
jumps[0] = pre[0]
for j in range(1, n):
jumps[j] = jumps[j - 1] + pre[j]
if a[i]:
prev_jumps = jumps[i]
ans += max(0, a[i] - 1 - prev_jumps)
all = prev_jumps + max(0, a[i] - 1 - prev_jumps)
if all > a[i] - 1:
extra = all - (a[i] - 1)
if i + 1 < n:
pre[i + 1] += extra
if i + 2 < n:
pre[i + 2] -= extra
start = i + 2
if start < n:
pre[start] += 1
end = min(i + a[i], n - 1)
if end + 1 < n:
pre[end + 1] -= 1
print(ans) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
s = [int(x) for x in input().split()]
a = [0] * (n + 1)
b = []
ans = 0
for i in range(n):
b.append(s[i] - 1 - a[i])
if s[i] - 1 - a[i] < 0:
a[i + 1] += abs(s[i] - 1 - a[i])
else:
ans += s[i] - 1 - a[i]
for j in range(2, s[i] + 1):
if i + j >= n:
break
a[i + j] += 1
print(ans) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | TC = int(input().strip())
for tc in range(TC):
n = int(input().strip())
arr = [int(i) for i in input().strip().split()]
curr = [0] * (n + 1)
ans = 0
for x in range(n):
temp = curr[x]
if temp < arr[x] - 1:
ans += arr[x] - 1 - temp
y = arr[x] - 1 - temp
temp += y
curr[x + 1] += temp - arr[x] + 1
for y in range(x + 2, min(n, x + arr[x] + 1)):
curr[y] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
s = [0] * (n + 1)
for i in range(n):
if a[i] == 1:
continue
if i + a[i] >= n:
for j in range(i + 2, n):
s[j] += 1
else:
for j in range(i + 2, i + a[i] + 1):
s[j] += 1
ans = 0
for i in range(n):
if a[i] - 1 - s[i] > 0:
ans += a[i] - 1 - s[i]
else:
s[i + 1] += s[i] - a[i] + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | test = int(input())
for t in range(test):
n = int(input())
s = list(map(int, input().split()))
r = [0] * n
res = 0
for i in range(n):
b = i + 2
e = min(i + s[i], n - 1)
if e >= b:
for i2 in range(b, e + 1):
r[i2] += 1
if i < n - 1 and r[i] > s[i] - 1:
r[i + 1] += r[i] - s[i] + 1
s[i] -= r[i]
if s[i] < 1:
s[i] = 1
res += s[i] - 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
prev = [0] * n
res = 0
for i in range(n):
a[i] -= 1
for i in range(n - 1):
if prev[i] > a[i]:
prev[i + 1] += prev[i] - a[i]
if a[i] > n - i - 2:
res += a[i] - (n - i - 2)
a[i] = n - i - 2
for j in range(i + 2, i + a[i] + 2):
prev[j] += 1
res += max(prev[-1], a[-1])
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | for _ in range(int(input())):
n = int(input())
s = list(map(int, input().split()))
d = 0
debt = 0
k = 0
kmap = dict()
for i in range(n):
k -= kmap.get(i, 0)
d += max(s[i] - (debt + k), 0)
debt = max(debt + k - s[i], 0)
k += 1
kmap[i + s[i] + 1] = kmap.get(i + s[i] + 1, 0) + 1
print(d - 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | p = lambda: list(map(int, input().split()))
exec(
"N=p()[0];s=p();print(max(s[i]+sum([max(0,s[j]-i+j)for j in range(i)])-1for i in range(N)));"
* p()[0]
) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR NUMBER |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
for _ in range(II()):
n = II()
aa = LI()
bb = [0] * (n + 1)
ans = 0
for i in range(n):
ans += max(0, aa[i] - 1 - bb[i])
for j in range(i + 2, min(n, i + aa[i] + 1)):
bb[j] += 1
rem = bb[i] - aa[i] + 1
if rem > 0:
bb[i + 1] += rem
print(ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
for nt in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = 0
c = [0] * n
for i in range(n):
if a[i] > c[i] + 1:
ans += a[i] - c[i] - 1
c[i] += a[i] - c[i] - 1
if i != n - 1:
c[i + 1] += c[i] - a[i] + 1
for j in range(min(i + a[i], n - 1), i + 1, -1):
c[j] += 1
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | def main():
t = int(input())
for i in range(t):
print(solve())
def add_range(pos, _range, array, diff):
n = len(array)
start, end = _range
if pos + start < n:
array[pos + start] += diff
if pos + end < n:
array[pos + end] -= diff
return array
def solve():
n = int(input())
strength = list(map(int, input().split()))
diff = [(0) for _ in range(n)]
ans = 0
deq = 0
for i in range(n):
deq += diff[i]
s = strength[i]
ans += max(s - 1 - deq, 0)
diff = add_range(i, [2, s + 1], diff, 1)
if s - 1 < deq:
diff = add_range(i, [1, 2], diff, deq - s + 1)
return ans
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
def slow_solve(n, Ss):
ans = 0
for i in range(n):
if Ss[i] >= n - i:
ans += Ss[i] - (n - i)
Ss[i] = n - i
while Ss[i] > 1:
ans += 1
cur = i
while cur < n:
pre = cur
cur += Ss[cur]
if Ss[pre] > 1:
Ss[pre] -= 1
return ans
def solve(n, Ss):
ans = 0
jumps = [0] * n
for i in range(n - 1):
if Ss[i] >= n - i - 1:
spare = min(jumps[i], Ss[i] - (n - i - 1))
ans += Ss[i] - (n - i - 1) - spare
jumps[i] -= spare
Ss[i] = n - i - 1
if Ss[i] > 1:
ans += max(Ss[i] - 1 - jumps[i], 0)
for t in range(i + 2, i + Ss[i] + 1):
jumps[t] += 1
if Ss[i] - 1 < jumps[i]:
jumps[i + 1] += jumps[i] - (Ss[i] - 1)
ans += max(Ss[-1] - 1 - jumps[-1], 0)
return ans
t = int(input())
answers = []
for _ in range(t):
n = int(input())
Ss = list(map(int, input().split()))
answers.append(solve(n, Ss))
print("\n".join(map(str, answers))) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
class BinaryIndexedTree:
def __init__(self, size):
self.data = [0] * (size + 1)
self.msb = 1 << size.bit_length() - 1
def _add(self, i, w):
i += 1
while i < len(self.data):
self.data[i] += w
i += i & -i
def _get_sum(self, i):
res = 0
while i > 0:
res += self.data[i]
i -= i & -i
return res
def __getitem__(self, i):
if isinstance(i, slice):
a = self._get_sum(len(self.data) - 1 if i.stop is None else i.stop)
b = self._get_sum(0 if i.start is None else i.start)
return a - b
else:
return 0
__setitem__ = _add
def bisect_left(self, v):
i = 0
k = self.msb
l = len(self.data)
while k > 0:
i += k
if i < l and self.data[i] < v:
v -= self.data[i]
else:
i -= k
k >>= 1
return i
def bisect_right(self, v):
i = 0
k = self.msb
l = len(self.data)
while k > 0:
i += k
if i < l and self.data[i] <= v:
v -= self.data[i]
else:
i -= k
k >>= 1
return i
bisect = bisect_right
def solve(s):
n = len(s)
bit = BinaryIndexedTree(n)
for i, v in enumerate(s):
bit[i] += int(v > 1)
res = 0
m = bit[:]
start = 0
while m:
if s[start] == 1:
start = bit.bisect_right(bit[:start])
i = start
if s[i] + i > n:
t = s[i] + i - n - 1
res += t
s[i] -= t
while i < n:
if s[i] == 1:
i = bit.bisect_right(bit[:i])
else:
t = s[i]
s[i] -= 1
if s[i] == 1:
bit[i] += -1
m -= 1
i += t
res += 1
return res
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
m = map(int, read().split())
t = next(m)
tests = []
for _ in range(t):
n = next(m)
tests.append([v for i, v in zip(range(n), m)])
for s in tests:
print(solve(s)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NONE NUMBER VAR RETURN BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | from sys import *
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
l = [int(x) for x in stdin.readline().split()]
ind = 0
ans = 0
sp = [0] * n
while ind < n and l[ind] == 1:
ind += 1
if ind == n:
print(0)
continue
while ind < n:
if l[ind] == 1:
if sp[ind] > 0 and ind < n - 1:
sp[ind + 1] += sp[ind]
ind += 1
continue
if l[ind] - sp[ind] < 1:
t = sp[ind] - l[ind] + 1
if ind < n - 1:
sp[ind + 1] += t
for i in range(min(n, ind + 2), min(n, ind + l[ind] + 1)):
sp[i] += 1
ans += max(0, l[ind] - 1 - sp[ind])
ind += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
def solve():
n = int(input())
s = list(map(int, input().split()))
z = list(range(n))
r = 0
for i in range(n):
v = s[i]
if v + i > n - 1:
v = max(n - 1 - i, 1)
if s[i] != v:
r += s[i] - v
s[i] = v
while s[i] > 1:
r += 1
p = i
while p < n:
p = z[p]
v = s[p]
if v == 1:
f = n - 1
j = p
while j < f:
if s[j] != 1:
f = j
break
elif z[j] == j:
z[j] = j + 1
j += 1
else:
j = z[j]
k = p
while k < f:
v = z[k]
z[k] = f
k = v
p = f
v = s[p]
if v > 1:
s[p] = v - 1
p += v
return r
t = int(input())
r = [0] * t
for z in range(t):
r[z] = str(solve())
print("\n".join(r)) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | t = int(input())
for _ in range(t):
n = int(input())
arr = [int(i) for i in input().split()]
cur = [0] * (n + 10)
ans = 0
for i in range(n):
diff = cur[i] - arr[i]
if diff >= 0:
cur[i + 1] += diff + 1
else:
ans += abs(diff)
cur[i + 1] += 1
for j in range(i + 2, min(n, i + arr[i] + 1)):
cur[j] += 1
print(ans - 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
ans = 0
s = list(map(int, input().split()))
l = [0] * (n + 1)
for i in range(n):
if s[i] <= l[i]:
l[i + 1] += l[i] - s[i] + 1
ans += max(0, s[i] - l[i] - 1)
for j in range(min(s[i], n - i - 1), 1, -1):
l[i + j] += 1
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
arr = [int(x) for x in input().split()]
temp = [(0) for i in range(n)]
ans = 0
for i in range(n):
curr = temp[i]
if temp[i] + 1 < arr[i]:
ans += arr[i] - (curr + 1)
curr += arr[i] - (curr + 1)
if i != n - 1:
temp[i + 1] += curr - arr[i] + 1
for j in range(i + 2, min(n, arr[i] + i + 1)):
temp[j] += 1
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | n = int(input())
while n > 0:
n -= 1
counter = 0
num = int(input())
arr = list(map(int, input().split()))
z = [0] * (num + 1)
for i in range(len(arr)):
current = z[i]
if current < arr[i] - 1:
counter += arr[i] - current - 1
current += arr[i] - current - 1
z[i + 1] += current - arr[i] + 1
for j in range(i + 2, min(num, i + arr[i] + 1)):
z[j] += 1
print(counter) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | t = int(input())
while t:
t -= 1
n = int(input())
arr = list(map(int, input().split()))
c = [(0) for i in range(n)]
ans = 0
for i in range(n):
if i > 0:
c[i] += c[i - 1]
if c[i] <= arr[i] - 1:
ans += arr[i] - 1 - c[i]
if arr[i] > 1:
if i + 2 < n:
c[i + 2] += 1
if i + arr[i] + 1 < n:
c[i + arr[i] + 1] += -1
if c[i] > arr[i] - 1:
extra = c[i] - arr[i] + 1
if i + 1 < n:
c[i + 1] += extra
if i + 2 < n:
c[i + 2] += -extra
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
There is a trampoline park with $n$ trampolines in a line. The $i$-th of which has strength $S_i$.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline $i$, the trampoline will launch her to position $i + S_i$, and $S_i$ will become equal to $\max(S_i-1,1)$. In other words, $S_i$ will decrease by $1$, except of the case $S_i=1$, when $S_i$ will remain equal to $1$.
If there is no trampoline in position $i + S_i$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $i + S_i$ by the same rule as above.
Pekora can't stop jumping during the pass until she lands at the position larger than $n$ (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $S_i$ to $1$. What is the minimum number of passes she needs to reduce all $S_i$ to $1$?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5000$) β the number of trampolines.
The second line of each test case contains $n$ integers $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 10^9$), where $S_i$ is the strength of the $i$-th trampoline.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $5000$.
-----Output-----
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all $S_i$ to $1$.
-----Examples-----
Input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
Output
4
3
0
-----Note-----
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$
$[1,\textbf{4},1,2,1,\textbf{2},1]$
$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$
$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$
For the second test case, the optimal series of passes is show below.
$[\textbf{2},3]$
$[1,\textbf{3}]$
$[1,\textbf{2}]$
For the third test case, all $S_i$ are already equal to $1$. | import sys
input = sys.stdin.readline
flush = sys.stdout.flush
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
B = [0] * n
ans = 0
for i in range(n):
if A[i] - B[i] > 1:
ans += A[i] - B[i] - 1
B[i] = A[i] - 1
for j in range(i + 2, min(n, i + A[i] + 1)):
B[j] += 1
if i + 1 < n:
B[i + 1] += B[i] - (A[i] - 1)
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | q = int(input())
for _ in range(q):
t = input()
n = int(input())
S = [input() for i in range(n)]
cur = 0
mx = -1
ans = []
mxid = 0
flag_bad = False
for i in range(len(t)):
for ids in range(len(S)):
if i + len(S[ids]) <= len(t) and t[i : i + len(S[ids])] == S[ids]:
if i + len(S[ids]) - 1 > mx:
mx = i + len(S[ids]) - 1
mxid = ids
if cur == i:
if mx < i:
print(-1)
flag_bad = True
break
else:
ans.append((mxid + 1, mx - len(S[mxid]) + 2))
cur = mx + 1
if not flag_bad:
print(len(ans))
for el in ans:
print(el[0], el[1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | for _ in range(int(input())):
st = input()
case = []
length = len(st)
for i in range(int(input())):
case.append(input())
tas = []
dp = [0] + [float("inf")] * length
dic = {}
dic2 = {}
bre = False
for i in range(0, length):
for x in range(len(case)):
leng = len(case[x])
if i + leng <= length:
sub = st[i : i + leng]
if st[i : i + leng] == case[x]:
for y in range(i, i + leng):
mini = min(dp[y + 1], dp[i] + 1)
dp[y + 1] = mini
if mini <= dp[i + leng]:
if mini in dic2:
if i + leng > dic2[mini]:
dic[mini] = [str(x + 1), str(i + 1)]
dic2[mini] = i + leng
else:
dic[mini] = [str(x + 1), str(i + 1)]
dic2[mini] = i + leng
if dp[-1] != float("inf"):
print(dp[-1])
for x in dic.values():
print(" ".join(x))
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | q = int(input())
def find(index, text, arr, n):
id = -1
mx_length = 0
for i in range(n):
if text[index:].find(arr[i]) == 0 and len(arr[i]) > mx_length:
mx_length = len(arr[i])
id = i + 1
return id
def solve():
t = input()
n = int(input())
arr = []
for i in range(n):
arr += [input()]
start = 0
ans = 0
result = []
idx = find(start, t, arr, n)
if idx == -1:
print(-1)
else:
ans += 1
result += [[idx, start + 1]]
right_most_colored = len(arr[idx - 1]) - 1
while right_most_colored < len(t) - 1:
idx = -1
cnt = -1
for i in range(start + 1, right_most_colored + 2):
longest_covering_string = find(i, t, arr, n)
if longest_covering_string != -1:
if (
i + len(arr[longest_covering_string - 1]) - 1
> right_most_colored
):
right_most_colored = (
i + len(arr[longest_covering_string - 1]) - 1
)
idx = longest_covering_string
cnt = i
if idx == -1:
print(-1)
return
ans += 1
result += [[idx, cnt + 1]]
start = cnt
print(ans)
for i in range(ans):
print(*result[i])
while q:
solve()
q -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR LIST LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER VAR LIST LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | from itertools import islice
t = int(input())
for i in range(t):
text = input()
m = int(input())
strings = []
indexes = {}
for i in range(m):
strings.append(input())
indices = [-1] * (len(text) + 1)
for index, string in enumerate(strings):
i = 0
while i + len(string) <= len(text):
if text[i : i + len(string)] == string:
if indices[i] < i + len(string):
indices[i] = i + len(string)
indexes[i] = index
i += 1
i = 0
ans = []
while i < len(text):
max_index, max_value = max(
enumerate(islice(indices, i + 1)), key=lambda k: k[1]
)
if max_value <= i:
print(-1)
break
else:
ans.append((indexes[max_index], max_index))
i = max_value
else:
print(len(ans))
for a, b in ans:
print(a + 1, b + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | def main():
t = input()
n = len(t)
ans = []
ok = True
substrings = []
for _ in range(int(input())):
substrings.append(input())
s = sorted(substrings, key=len, reverse=True)
i = 0
while i < n:
max_len = i
index = 0
pos = 0
for w in s:
lw = len(w)
for j in range(lw):
start = i - j
end = i - j + lw
if start > -1 and end <= n and t[start:end] == w:
if end > max_len:
max_len = end
index = substrings.index(w)
pos = i - j
if max_len == i:
ok = False
break
ans.append([index, pos])
i = max_len
if ok:
print(len(ans))
for l in ans:
print(f"{l[0] + 1} {l[1] + 1}")
else:
print(-1)
n = int(input())
for _ in range(n):
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | def solve():
t = input()
n = int(input())
strings = []
for i in range(n):
strings.append(input())
intervals = []
for i in range(n):
for j in range(len(t)):
if t[j : j + len(strings[i])] == strings[i]:
intervals.append((j, j + len(strings[i]), i))
intervals.sort()
if len(intervals) == 0:
return -1
s, e, ind = intervals[0]
if s != 0:
return -1
mmax = 0
used = []
ec = -1
for s, e, ind in intervals:
if s <= mmax:
if e > ec:
sc, ec, indc = s, e, ind
else:
used.append((sc, ec, indc))
mmax = ec
sc, ec, indc = s, e, ind
if mmax != len(t):
for s, e, ind in intervals:
if e == len(t):
used.append((s, e, ind))
break
covered = [(0) for _ in range(len(t))]
for s, e, ind in used:
for i in range(s, e):
covered[i] = 1
if sum(covered) == len(t):
return used
else:
return -1
q = int(input())
sol = []
for _ in range(q):
sol.append(solve())
for so in sol:
if so == -1:
print(-1)
else:
print(len(so))
for s, e, ind in so:
print(ind + 1, s + 1) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | def find(a, b, t, strs, ans):
maxi, id, pos = 0, -1, -1
for i in range(a, b + 1):
for j in range(len(strs)):
s = strs[j]
c = i + len(s)
if c > len(t) or c <= b:
continue
if t[i:c] == s and c > maxi:
maxi = c
id = j
pos = i
if id == -1:
return []
else:
ans.append((id, pos))
if maxi == len(t):
return ans
else:
return find(max(pos + 1, b + 1), maxi, t, strs, ans)
T = int(input())
for _ in range(T):
t = input()
n = int(input())
strs = []
for _ in range(n):
s = input()
strs.append(s)
ans = find(0, 0, t, strs, [])
if len(ans) == 0:
print(-1)
else:
print(len(ans))
for a, b in ans:
print("{} {}".format(a + 1, b + 1)) | FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN LIST EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
You are given some text $t$ and a set of $n$ strings $s_1, s_2, \dots, s_n$.
In one step, you can choose any occurrence of any string $s_i$ in the text $t$ and color the corresponding characters of the text in red. For example, if $t={bababa}$ and $s_1={ba}$, $s_2={aba}$, you can get $t={{ba}}{baba}$, $t={b}{{aba}}{ba}$ or $t={bab}{{aba}}$ in one step.
You want to color all the letters of the text $t$ in red. When you color a letter in red again, it stays red.
In the example above, three steps are enough:
Let's color $t[2 \dots 4]=s_2={aba}$ in red, we get $t={b}{{aba}}{ba}$;
Let's color $t[1 \dots 2]=s_1={ba}$ in red, we get $t={{baba}}{ba}$;
Let's color $t[4 \dots 6]=s_2={aba}$ in red, we get $t={{bababa}}$.
Each string $s_i$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.
Determine the minimum number of steps needed to color all letters $t$ in red and how to do it. If it is impossible to color all letters of the text $t$ in red, output -1.
-----Input-----
The first line of the input contains an integer $q$ ($1 \le q \le 100$) βthe number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case contains the text $t$ ($1 \le |t| \le 100$), consisting only of lowercase Latin letters, where $|t|$ is the length of the text $t$.
The second line of each test case contains a single integer $n$ ($1 \le n \le 10$) β the number of strings in the set.
This is followed by $n$ lines, each containing a string $s_i$ ($1 \le |s_i| \le 10$) consisting only of lowercase Latin letters, where $|s_i|$ β the length of string $s_i$.
-----Output-----
For each test case, print the answer on a separate line.
If it is impossible to color all the letters of the text in red, print a single line containing the number -1.
Otherwise, on the first line, print the number $m$ β the minimum number of steps it will take to turn all the letters $t$ red.
Then in the next $m$ lines print pairs of indices: $w_j$ and $p_j$ ($1 \le j \le m$), which denote that the string with index $w_j$ was used as a substring to cover the occurrences starting in the text $t$ from position $p_j$. The pairs can be output in any order.
If there are several answers, output any of them.
-----Examples-----
Input
6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb
Output
3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13
-----Note-----
The first test case is explained in the problem statement.
In the second test case, it is impossible to color all the letters of the text in red. | ans = 0
ok = True
def Find(a, b, t, s_list, match):
global ans, ok
Max, id, pos = 0, -1, -1
for i in range(a, b + 1):
for j in range(len(s_list)):
s = s_list[j]
if i + len(s) > len(t) or i + len(s) <= b:
continue
if t[i : i + len(s)] == s:
if i + len(s) > Max:
Max = i + len(s)
id = j
pos = i
if id == -1:
ok = False
return
else:
match.append((id, pos))
ans += 1
if Max == len(t):
return
else:
Find(b + 1, Max, t, s_list, match)
def solve():
global ans, ok
ans = 0
ok = True
t = input()
n = int(input())
s_list = []
match = []
for _ in range(n):
s_list.append(input())
Find(0, 0, t, s_list, match)
if not ok:
print(-1)
else:
print(ans)
for p in match:
print(p[0] + 1, p[1] + 1)
t = int(input())
for _ in range(t):
solve() | ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().strip().split()]
s = []
curr = 0
for i in range(n):
curr += a[i]
s.append(curr)
ans = 0
l = []
for i in range(n):
if a[i] == 0:
l.append(i)
a[i] = ans
if s[i] == 0:
ans += 1
already = 0
d = {}
maxf = 0
if l:
j = len(l) - 1
for i in range(n - 1, -1, -1):
d[s[i]] = d.get(s[i], 0) + 1
maxf = max(maxf, d[s[i]])
if i == l[j]:
j -= 1
d = {}
already += maxf
maxf = 0
print(max(ans, a[l[0]] + already))
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR DICT VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | t = int(input())
while t > 0:
a = int(input())
b = list(map(int, input().split()))
ans = 0
sum = 0
d = {}
maxCount = 0
for i in range(len(b) - 1, -1, -1):
if b[i] == 0:
zeroCount = 0
if 0 in d:
zeroCount = d[0]
ans += max(maxCount, 1 + zeroCount)
sum = 0
d = {}
maxCount = 0
else:
sum += b[i]
if sum in d:
d[sum] += 1
else:
d[sum] = 1
if d[sum] > maxCount:
maxCount = d[sum]
if len(d) != 0:
ans += d[sum] - 1
if sum == 0:
ans += 1
print(ans)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
ls = [int(x) for x in input().split()]
dp = [0] * (n + 1)
for i in range(1, n + 1):
dp[i] = dp[i - 1] + ls[i - 1]
s = 0
d = {}
for i in reversed(range(n)):
d[dp[i + 1]] = d.get(dp[i + 1], 0) + 1
if ls[i] == 0:
s += max(d.values())
d.clear()
s += d.get(0, 0)
print(s) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | from itertools import groupby
def Most_Common(lst):
lst.sort()
m = 0
for k, g in groupby(lst):
tmp = len(list(g))
if tmp > m:
m = tmp
return m
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
pr_sum = []
zero_ind = []
S = 0
for i in range(n):
x = a[i]
S += x
pr_sum.append(S)
if x == 0:
zero_ind.append(i)
zero_ind.append(n)
answer = 0
answer += pr_sum[: zero_ind[0]].count(0)
N = len(zero_ind)
for k in range(N - 1):
i = zero_ind[k + 1]
j = zero_ind[k]
answer += Most_Common(pr_sum[j:i])
print(answer) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
Z = []
if not A[0]:
Z.append(0)
C = [A[0]]
for i in range(1, n):
C.append(C[-1] + A[i])
if not A[i]:
Z.append(i)
z = len(Z)
Z.append(n)
total = C[: Z[0]].count(0)
sum_zeros = 0
for i in range(z):
if i < z - 1:
next_zi = Z[i + 1]
else:
next_zi = n
if next_zi - Z[i] == 1:
total += 1
continue
counter = {val: (0) for val in set(C[Z[i] : next_zi])}
max_app = 0
for j in range(Z[i], next_zi):
counter[C[j]] += 1
max_app = max(max_app, counter[C[j]])
total += max_app
print(total) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
first_zero = False
s = 0
poss = []
res = 0
for i in range(len(arr)):
if arr[i] == 0:
if first_zero == True:
count = {}
for e in poss:
count[e] = count.get(e, 0) + 1
best_val = max(count, key=lambda x: count[x])
res += count[best_val]
s += best_val
poss = []
elif first_zero == False:
first_zero = True
s += arr[i]
if first_zero == False and s == 0:
res += 1
if first_zero:
poss.append(-s)
if len(poss) > 0:
count = {}
for e in poss:
count[e] = count.get(e, 0) + 1
best_val = max(count, key=lambda x: count[x])
res += count[best_val]
s += best_val
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The score of an array $v_1,v_2,\ldots,v_n$ is defined as the number of indices $i$ ($1 \le i \le n$) such that $v_1+v_2+\ldots+v_i = 0$.
You are given an array $a_1,a_2,\ldots,a_n$ of length $n$. You can perform the following operation multiple times:
select an index $i$ ($1 \le i \le n$) such that $a_i=0$;
then replace $a_i$ by an arbitrary integer.
What is the maximum possible score of $a$ that can be obtained by performing a sequence of such operations?
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$) β array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible score of the array $a$ after performing a sequence of operations.
-----Examples-----
Input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
Output
3
1
4
4
5
-----Note-----
In the first test case, it is optimal to change the value of $a_2$ to $-2$ in one operation.
The resulting array $a$ will be $[2,-2,1,-1,0]$, with a score of $3$:
$a_1+a_2=2-2=0$;
$a_1+a_2+a_3+a_4=2-2+1-1=0$;
$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$.
In the second test case, it is optimal to change the value of $a_3$ to $-2000000000$, giving us an array with a score of $1$.
In the third test case, it is not necessary to perform any operations. | def max_beauty(arr):
cumsum = [(0) for i in range(len(arr) + 1)]
not_first = False
first_beauty = 0
for i in range(len(arr)):
if arr[i] == 0:
not_first = True
cumsum[i + 1] = cumsum[i] + arr[i]
if cumsum[i + 1] == 0 and not not_first:
first_beauty += 1
beauty = 0
cnt = dict()
for i in range(len(arr) - 1, -1, -1):
if cumsum[i + 1] in cnt:
cnt[cumsum[i + 1]] += 1
else:
cnt[cumsum[i + 1]] = 1
if arr[i] == 0 and cnt:
max_v = 0
for k, v in cnt.items():
if v > max_v:
max_v = v
max_k = k
if max_v:
beauty += max_v
cnt.clear()
if cnt:
beauty += first_beauty
return beauty
for i in range(int(input())):
n = input()
arr = [int(i) for i in input().split()]
print(max_beauty(arr)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for _ in range(int(input())):
s = input()
l = len(s)
stack = []
d = {i: v for v, i in enumerate(s)}
st = set()
for i in range(l):
if s[i] not in st:
while stack and s[i] > stack[-1] and d[stack[-1]] > i:
st.discard(stack.pop())
stack.append(s[i])
st.add(s[i])
print("".join(stack)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for k in range(t):
a = input()
b = list(a)
new = []
povt = {}
for i in b:
if i in povt.keys():
povt[i] += 1
else:
povt[i] = 1
i = 0
while i < len(b) - 1:
if b[i] in b[:i]:
povt[b[i]] -= 1
b.pop(i)
if i < 0:
i -= 1
elif povt[b[i]] > 1:
if b[i + 1] in b[: i + 1]:
povt[b[i + 1]] -= 1
b.pop(i + 1)
elif ord(b[i + 1]) >= ord(b[i]):
povt[b[i]] -= 1
b.pop(i)
if i > 0:
i -= 1
continue
elif ord(b[i + 1]) < ord(b[i]):
i += 1
else:
i += 1
if povt[b[-1]] > 1:
b.pop(-1)
print(*b, sep="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | T = int(input())
r = 1
while r <= T:
s = input()
n = len(s)
lastappear = [0] * 26
for i in range(n):
lastappear[ord(s[i]) - 97] = i
stack = []
already = [False] * 26
for i in range(n):
if already[ord(s[i]) - 97]:
continue
while stack and ord(stack[-1]) <= ord(s[i]):
if i > lastappear[ord(stack[-1]) - 97]:
break
c = stack.pop()
already[ord(c) - 97] = False
if not already[ord(s[i]) - 97]:
stack.append(s[i])
already[ord(s[i]) - 97] = True
ans = "".join(stack)
print(ans)
r += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
f_inf = float("inf")
MOD = 10**9 + 7
def solve():
S = input().rstrip()
d = {i: v for v, i in enumerate(S)}
res = []
seen = set()
for i, s in enumerate(S):
if s not in seen:
while res and s > res[-1] and d[res[-1]] > i:
seen.discard(res.pop())
res.append(s)
seen.add(s)
print("".join(res))
t = int(input())
for _ in range(t):
solve() | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
s = input().strip()
n = len(s)
d = {}
for i in s:
d[i] = d.get(i, 0) + 1
l = len(d)
used = set()
places = []
last = 0
for p in range(l):
for j in range(25, -1, -1):
if j in used:
continue
f = 0
for i in range(last, n):
if ord(s[i]) - 97 == j:
used.add(j)
places.append(s[i])
last = i + 1
f = 1
for k in range(last, i):
d[s[k]] += 1
break
else:
d[s[i]] -= 1
if d[s[i]] == 0:
for k in range(last, i + 1):
d[s[k]] += 1
break
if f:
break
print("".join(places)) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for _ in range(int(input())):
ans = ""
s = input()
occ = {}
for c in s:
if c not in occ:
occ[c] = 0
occ[c] += 1
remove = set()
prev_i = -1
for i in range(len(s)):
occ[s[i]] -= 1
if s[i] in remove:
continue
if prev_i == -1 or s[i] > s[prev_i]:
prev_i = i
if occ[s[i]] == 0:
order = sorted(set(s[prev_i:i]), reverse=True)
order_i = 0
for j in range(prev_i, i):
while order_i < len(order) and order[order_i] in remove:
order_i += 1
if (
order_i < len(order)
and order[order_i] == s[j]
and s[j] >= s[i]
and s[j] not in remove
):
ans += s[j]
remove.add(s[j])
prev_i = j
order = sorted(set(s[prev_i:i]), reverse=True)
order_i = 0
if s[i] not in remove:
ans += s[i]
remove.add(s[i])
prev_i = i
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for _ in range(t):
S = input()
l = [[] for i in range(26)]
for i in range(len(S)):
l[ord(S[i]) - ord("a")].append(i)
last = [10**10] * 26
for i in range(26):
if l[i]:
l[i] = l[i][::-1]
last[i] = l[i][0]
ans = ""
now = -1
while True:
nex = 10**10
ind = -1
for i in range(26):
if nex > last[i]:
nex = last[i]
ind = i
nex = min(nex, last[i])
if ind == -1:
break
for i in range(26)[::-1]:
if last[i] == 10**10:
continue
while l[i] and l[i][-1] <= now:
l[i].pop()
if l[i] and l[i][-1] <= nex:
ans += chr(ord("a") + i)
now = l[i][-1]
last[i] = 10**10
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for _ in range(t):
s = str(input())
n = len(s)
dict1 = {}
for i in range(n):
dict1[ord(s[i])] = i
stack = []
vis = [0] * 26
for i in range(n):
if vis[ord(s[i]) - 97] == 0:
while stack and ord(s[i]) > stack[-1] and i < dict1[stack[-1]]:
vis[stack.pop() - 97] = 0
stack.append(ord(s[i]))
vis[ord(s[i]) - 97] = 1
for i in stack:
print(chr(i), end="")
print("") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for i in range(int(input())):
s = input()
if len(set(s[0:])) != len(s):
while s:
DEL = ""
S = set(s[0:])
abc = sorted(set(s[0:]))
for j in range(len(abc) - 1, -1, -1):
if len(set(s[s.index(abc[j]) :])) == len(S):
DEL = abc[j]
break
print(DEL, end="")
s = s[s.index(DEL) + 1 :]
s = s.replace(DEL, "")
print()
else:
print(s) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | def solve():
s = input()
exists = [[]] * 26
for i in range(len(s) - 1, -1, -1):
ch = ord(s[i]) - ord("a")
if exists[ch]:
exists[ch].append(i)
else:
exists[ch] = [i]
start = 0
while True:
something_happened = False
for use in range(25, -1, -1):
if not exists[use]:
continue
where = exists[use][-1]
suitable = True
for occur in exists:
if occur and occur[0] < where:
suitable = False
break
if suitable:
print(s[where], end="")
exists[use] = []
start = where + 1
for c in range(26):
while exists[c] and exists[c][-1] < start:
exists[c].pop()
something_happened = True
break
if not something_happened:
break
print()
def main():
t = int(input())
while t:
solve()
t -= 1
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for _ in range(t):
list_s = input()
lastPositions = {c: pos for pos, c in enumerate(list_s)}
assigned = {c: (False) for c in lastPositions}
new_string = []
for pos, c in enumerate(list_s):
if len(new_string) == len(assigned):
break
if not assigned[c]:
i = len(new_string) - 1
while i >= 0 and new_string[i] < c and pos < lastPositions[new_string[i]]:
assigned[new_string[i]] = False
i -= 1
new_string = new_string[: i + 1]
new_string.append(c)
assigned[c] = True
print("".join(new_string)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
from sys import stdin
tt = int(stdin.readline())
alp = "abcdefghijklmnopqrstuvwxyz"
ANS = []
for loop in range(tt):
s = list(stdin.readline()[:-1])
n = len(s)
alis = []
adic = {}
use = [False] * n
for i in s:
if i not in adic:
adic[i] = True
alis.append(i)
alis.sort()
anum = len(alis)
l = -1
for i in range(anum):
nmax = None
ntype = 0
dic = {}
for j in range(n - 1, l, -1):
if not adic[s[j]]:
continue
if s[j] not in dic:
dic[s[j]] = 1
ntype += 1
if ntype + i >= anum:
if nmax == None:
nmax = j
elif s[j] >= s[nmax]:
nmax = j
use[nmax] = True
l = nmax
adic[s[nmax]] = False
ans = []
for i in range(n):
if use[i]:
ans.append(s[i])
ANS.append("".join(ans))
print("\n".join(ANS)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
input = sys.stdin.readline
t = int(input().strip())
for __ in range(t):
s = input().strip()
cnt = [0] * 26
vis = [0] * 26
n = len(s)
for i in s:
cnt[ord(i) - ord("a")] += 1
res = []
for i, c in enumerate(s):
cnt[ord(c) - ord("a")] -= 1
if not vis[ord(c) - ord("a")]:
while len(res) > 0 and res[-1] < c and cnt[ord(res[-1]) - ord("a")] > 0:
vis[ord(res[-1]) - ord("a")] = 0
del res[-1]
res += [c]
vis[ord(c) - ord("a")] = 1
print("".join(res)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR LIST VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | def CountAparicions(s):
repeatedChars = {}
for i in range(len(s)):
char = s[i]
if char in repeatedChars:
repeatedChars[char] += 1
else:
repeatedChars[char] = 1
return repeatedChars
def MaxSubStr(word, NewWord=""):
if word == "":
return NewWord
possibleLetters = []
numberOfAparicions = {}
counted = CountAparicions(word)
for letter in word:
if letter in possibleLetters:
numberOfAparicions[letter] += 1
if numberOfAparicions[letter] == counted[letter]:
break
else:
numberOfAparicions[letter] = 1
possibleLetters.append(letter)
if counted[letter] == 1:
break
del numberOfAparicions
NewLetter = max(possibleLetters)
NewLetterIndex = word.find(NewLetter)
word = word[NewLetterIndex + 1 :]
NewWord += NewLetter
word = word.replace(NewLetter, "")
return MaxSubStr(word, NewWord)
numberIterations = int(input())
i = 0
while i < numberIterations:
s = input()
s = "".join(e for e in s if e.isalpha())
if s == "":
continue
s1 = MaxSubStr(s)
print(s1)
i += 1 | FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF STRING IF VAR STRING RETURN VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for _ in range(t):
a = list(input())
trans = lambda x: ord(x) - 97
d = {}
for i in a:
d[trans(i)] = 0
for i in range(len(a)):
d[trans(a[i])] = i
used = [False] * 26
ans = [a[0]] + [""] * len(a)
used[trans(a[0])] = True
p = 0
for i in range(1, len(a)):
if used[trans(a[i])]:
continue
while p >= 0 and ans[p] < a[i] and d[trans(ans[p])] > i:
used[trans(ans[p])] = False
ans[p] = ""
p -= 1
p += 1
ans[p] = a[i]
used[trans(ans[p])] = True
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for j in range(t):
s = input()
n = len(s)
d = {}
for i in range(n):
d[s[i]] = i
ans = []
st = set()
for i in range(n):
if s[i] not in st:
while ans and s[i] > ans[-1] and d[ans[-1]] > i:
st.remove(ans.pop())
ans.append(s[i])
st.add(s[i])
print(*ans, sep="") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
for _ in range(int(input())):
s = input()
d = {}
for i in s:
if i in d:
d[i] += 1
else:
d[i] = 1
v = [(False) for i in range(26)]
temp = []
for i in s:
d[i] -= 1
if v[ord(i) - ord("a")] == True:
continue
while temp and temp[-1] < i and d[temp[-1]] > 0:
now = temp.pop()
v[ord(now) - ord("a")] = False
v[ord(i) - ord("a")] = True
temp.append(i)
ans = ""
for i in temp:
ans += i
print(ans) | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER WHILE VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
def can(tc, s, A, chars):
t = tc[:-1]
c = tc[-1]
j = 0
for i in range(len(s)):
if not t:
break
if s[i] == t[j]:
j += 1
if j == len(t):
break
i = i + 1 if t else 0
found = False
while i < len(s):
if s[i] == c:
found = True
break
i += 1
i += 1
if not found:
return False
if A[i] | set(list(t)) | {c} == set(chars):
return True
return False
prev = None
for _ in range(int(input())):
s = input()
if s == prev:
print(t)
continue
A = [set() for _ in range(len(s) + 1)]
for i in range(len(s) - 1, -1, -1):
A[i] |= A[i + 1] | {s[i]}
chars = list(set(list(s)))
chars.sort(reverse=True)
t = ""
while len(t) < len(chars):
for char in chars:
if char not in t and can(t + char, s, A, chars):
t += char
break
prev = s
print(t) | IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | from sys import stdin
def solve(tc):
s = stdin.readline().strip()
n = len(s)
si = [(0) for i in range(n)]
remaining = [(0) for i in range(26)]
for i in range(n):
key = ord(s[i]) - ord("a")
si[i] = key
remaining[key] += 1
st = []
curUsed = [(0) for i in range(26)]
for i in range(n):
if curUsed[si[i]] > 0:
remaining[si[i]] -= 1
continue
while len(st) > 0 and st[-1] < si[i] and remaining[st[-1]] > 0:
pick = st.pop()
curUsed[pick] -= 1
st.append(si[i])
curUsed[si[i]] += 1
remaining[si[i]] -= 1
print(*list(map(lambda x: chr(x + ord("a")), st)), sep="")
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
solve(tc)
tc += 1 | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | def count(s, remove=None):
c = dict()
y = ""
o = ""
for x in s:
if x == y or x == remove:
continue
if x not in c:
c[x] = 0
c[x] += 1
o += x
y = x
return o, c
def one(s):
ptr = 0
best = ""
s, c = count(s)
while True:
leader = ""
i = 0
while i < len(s):
x = s[i]
if x in best:
i += 1
continue
c[x] -= 1
if x > leader:
leader = x
li = i
if c[x] == 0:
break
i += 1
best += leader
ptr = li + 1
if ptr > len(s):
break
s, c = count(s[li + 1 :], leader)
return best
for _ in range(int(input())):
s = input()
r = one(s)
print(f"{r}") | FUNC_DEF NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for nt in range(int(input())):
s = input()
n = len(s)
vis = [0] * 26
d = {}
for i in range(n):
d[s[i]] = i
ans = []
stack = []
for i in range(n):
if not vis[ord(s[i]) - 97]:
while stack and s[i] > stack[-1] and i < d[stack[-1]]:
vis[ord(stack.pop()) - 97] = 0
stack.append(s[i])
vis[ord(s[i]) - 97] = 1
print("".join(stack)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | n = int(input())
for _ in range(n):
s = input()
n = len(s)
ch_set = set(s)
t = ""
for i in range(len(ch_set)):
for ch in sorted(ch_set, reverse=True):
if set(s[s.find(ch) :]).issuperset(ch_set):
s = s[s.find(ch) :].replace(ch, "")
ch_set.remove(ch)
t += ch
break
print(t) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for _ in range(int(input())):
stroke = input()
arr = {b: a for a, b in enumerate(stroke)}
tec = []
tecs = set()
for num, j in enumerate(stroke):
if j not in tecs:
while len(tec) != 0 and tec[-1] < j and num < arr[tec[-1]]:
tecs.discard(tec.pop())
tec.append(j)
tecs.add(j)
print(*tec, sep="") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
s = input().strip()
n = len(s)
freq = {}
check = {}
for i in s:
freq[i] = freq.get(i, 0) + 1
check[i] = 0
stack = []
for i in range(n):
freq[s[i]] -= 1
if check[s[i]]:
continue
while len(stack) and s[i] > stack[-1] and freq[stack[-1]]:
x = stack.pop()
check[x] = 0
stack.append(s[i])
check[s[i]] = 1
print("".join(stack)) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
input = sys.stdin.readline
def maxthestring3():
for _ in range(int(input())):
s = input().rstrip()
lastpos = {v: i for i, v in enumerate(s)}
queue = []
his = set()
for i in range(len(s)):
nowl = s[i]
if nowl not in his:
if queue:
while queue[-1] < nowl and lastpos[queue[-1]] > i:
his.discard(queue.pop())
if not queue:
break
queue.append(nowl)
his.add(nowl)
else:
queue.append(nowl)
his.add(nowl)
print("".join(queue))
maxthestring3() | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR WHILE VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
max_int = 2147483648
min_int = -max_int
t = int(input())
for _t in range(t):
s = input()
d = {}
for ss in s:
if ss in d:
d[ss] += 1
else:
d[ss] = 1
st = []
was = set()
for ss in s:
if ss not in was:
while st and ss >= st[-1] and d[st[-1]]:
was.discard(st[-1])
st.pop()
st.append(ss)
was.add(ss)
d[ss] -= 1
print("".join(st)) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
input = sys.stdin.readline
for _ in range(int(input())):
S = list(map(lambda x: ord(x) - ord("a"), list(input())[:-1]))
n = len(S)
table = [0] * 26
res = []
for x in S:
table[x] += 1
last = 0
for _ in range(len(set(S))):
c = 0
start = last
for i in range(last, n):
x = S[i]
if table[x] > 1:
if c < x:
c = x
last = i + 1
table[x] -= 1
elif table[x] == 1:
if c < x:
c = x
last = i + 1
table[x] -= 1
break
res.append(c)
for j in range(last, i + 1):
table[S[j]] += 1
for x in res:
table[x] = 0
print("".join([chr(x + ord("a")) for x in res])) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | import sys
def charToInt(c):
return ord(c) - ord("a")
def intToChar(x):
return chr(ord("a") + x)
def main():
t = int(input())
allans = []
for _ in range(t):
s = input()
n = len(s)
arr = [charToInt(c) for c in s]
charIndexes = [[] for _ in range(26)]
for i, x in enumerate(arr):
charIndexes[x].append(i)
availCharsAfter = [(0) for _ in range(n)]
for i in range(n - 1, -1, -1):
b = 1 << arr[i]
if i + 1 < n:
availCharsAfter[i] = availCharsAfter[i + 1] | b
else:
availCharsAfter[i] = b
requiredChars = set(arr)
reqCharsBin = 0
for x in requiredChars:
reqCharsBin |= 1 << x
ans = []
currI = 0
for __ in range(26):
for char in range(25, -1, -1):
if reqCharsBin & 1 << char == 0:
continue
tempArr = charIndexes[char]
idx = -1
bb = len(tempArr)
while bb > 0:
while idx + bb < len(tempArr) and tempArr[idx + bb] < currI:
idx += bb
bb //= 2
nextI = tempArr[idx + 1]
if reqCharsBin == reqCharsBin & availCharsAfter[nextI]:
ans.append(intToChar(char))
reqCharsBin = reqCharsBin & ~(1 << char)
currI = nextI
break
allans.append("".join(ans))
multiLineArrayPrint(allans)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _aa in range(1):
main() | IMPORT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for _ in range(int(input())):
s = input()
stack = []
seen = set()
last_occurrence = {c: i for i, c in enumerate(s)}
for i, c in enumerate(s):
if c not in seen:
while stack and c > stack[-1] and i < last_occurrence[stack[-1]]:
seen.discard(stack.pop())
seen.add(c)
stack.append(c)
print("".join(stack)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | from sys import stdin, stdout
def maximize_the_remaining_string(s):
check_a = [False] * 26
c_a = [[] for _ in range(26)]
hs = set()
for i in range(len(s)):
c_a[ord(s[i]) - ord("a")].append(i)
check_a[ord(s[i]) - ord("a")] = True
hs.add(s[i])
r = ""
p = -1
for _ in range(len(hs)):
for i in range(25, -1, -1):
if not check_a[i]:
continue
l = 0
h = len(c_a[i])
while l < h:
m = (l + h) // 2
if c_a[i][m] <= p:
l = m + 1
else:
h = m
found = True
for j in range(i - 1, -1, -1):
if not check_a[j]:
continue
if c_a[i][h] > c_a[j][-1]:
found = False
break
if found:
check_a[i] = False
r += chr(i + ord("a"))
p = c_a[i][h]
break
return r
t = int(stdin.readline())
for _ in range(t):
s = stdin.readline().strip()
r = maximize_the_remaining_string(s)
stdout.write(str(r) + "\n") | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | t = int(input())
for test in range(1, t + 1):
s = list(input())
store = [(0) for _ in range(len(s))]
curr = 0
visited = set()
last_seen = {}
for idx, ch in enumerate(s[::-1]):
if ch not in visited:
curr += 1
visited.add(ch)
store[-idx - 1] = curr
if ch not in last_seen:
last_seen[ch] = len(s) - 1 - idx
res = []
sel_idx = 0
visited = set()
while curr > 0:
choose = None
choose_idx = None
for idx in range(sel_idx, len(s)):
item = store[idx]
if item == curr and s[idx] not in visited:
if choose is None or s[idx] > choose:
choose = s[idx]
choose_idx = idx
res.append(choose)
visited.add(choose)
sel_idx = choose_idx + 1
new_store = []
curr -= 1
for idx, item in enumerate(store):
if idx <= last_seen[choose]:
new_store.append(item - 1)
else:
new_store.append(item)
store = new_store
print("".join(res)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR NONE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string $s$, consisting of lowercase Latin letters. While there is at least one character in the string $s$ that is repeated at least twice, you perform the following operation:
you choose the index $i$ ($1 \le i \le |s|$) such that the character at position $i$ occurs at least two times in the string $s$, and delete the character at position $i$, that is, replace $s$ with $s_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n$.
For example, if $s=$"codeforces", then you can apply the following sequence of operations:
$i=6 \Rightarrow s=$"codefrces";
$i=1 \Rightarrow s=$"odefrces";
$i=7 \Rightarrow s=$"odefrcs";
Given a given string $s$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string $a$ of length $n$ is lexicographically less than a string $b$ of length $m$, if:
there is an index $i$ ($1 \le i \le \min(n, m)$) such that the first $i-1$ characters of the strings $a$ and $b$ are the same, and the $i$-th character of the string $a$ is less than $i$-th character of string $b$;
or the first $\min(n, m)$ characters in the strings $a$ and $b$ are the same and $n < m$.
For example, the string $a=$"aezakmi" is lexicographically less than the string $b=$"aezus".
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case is characterized by a string $s$, consisting of lowercase Latin letters ($1 \le |s| \le 2 \cdot 10^5$).
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
-----Examples-----
Input
6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz
Output
odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz
-----Note-----
None | for i in range(int(input())):
s = input()
if len(set(s[0:])) != len(s):
n = len(set(s[0:]))
a = sorted(set(s[0:]))
while s:
DEL = ""
for j in range(len(a) - 1, -1, -1):
if len(set(s[s.index(a[j]) :])) == n:
DEL = a[j]
break
print(DEL, end="")
s = s[s.index(DEL) + 1 :]
s = s.replace(DEL, "")
n -= 1
a.pop(a.index(DEL))
print()
else:
print(s) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
a = list(map(int, input().split()))
arr, x, ans = [], 0, 0
for i in range(n):
if a[i] < 0:
arr.append(a[i])
arr = sorted(arr, reverse=True)
x = x + a[i]
if x < 0:
x -= arr.pop()
else:
ans = ans + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
list1 = list(map(int, input().split()))
s = 0
c = 0
b = []
for i in list1:
s += i
c += 1
if i < 0:
b.append(i)
b.sort()
if s < 0:
s -= b[0]
b.pop(0)
c -= 1
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
l = list(map(int, input().split()))
s = 0
c = 0
a = []
k = 0
for i in range(n):
s += l[i]
if l[i] < 0:
a.append(l[i])
a.sort(reverse=True)
if s < 0:
x = a.pop()
s = s - x
k += 1
print(n - k) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
l1 = [int(a) for a in input().split()]
negs = []
ctr, sm = 0, 0
for i in range(n):
sm += l1[i]
ctr += 1
if l1[i] < 0:
negs.append(l1[i])
negs.sort(reverse=True)
while sm < 0:
sm -= negs[-1]
negs.pop()
ctr -= 1
print(ctr) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
drinkings = [int(item) for item in input().split(" ")]
health = 0
drinks = []
for i in range(n):
health += drinkings[i]
drinks.append(drinkings[i])
if health < 0:
Min = min(drinks)
health -= Min
drinks.remove(Min)
print(len(drinks)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
potions = tuple(map(int, input().split()))
health = 0
res = 0
considered_harmful_potions = {}
most_harmful_potion = 0
for potion in potions:
res += 1
if potion < 0:
if potion in considered_harmful_potions:
considered_harmful_potions[potion] += 1
else:
considered_harmful_potions[potion] = 1
most_harmful_potion = min(most_harmful_potion, potion)
health += potion
if health < 0:
considered_harmful_potions[most_harmful_potion] -= 1
health -= most_harmful_potion
res -= 1
if considered_harmful_potions[most_harmful_potion] == 0:
del considered_harmful_potions[most_harmful_potion]
most_harmful_potion = min(considered_harmful_potions.keys(), default=0)
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The only difference is that in this version $n \leq 2000$. You can make hacks only if both versions of the problem are solved.
There are $n$ potions in a line, with potion $1$ on the far left and potion $n$ on the far right. Each potion will increase your health by $a_i$ when drunk. $a_i$ can be negative, meaning that potion will decrease will health.
You start with $0$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 2000$) β the number of potions.
The next line contains $n$ integers $a_1$, $a_2$, ... ,$a_n$ ($-10^9 \leq a_i \leq 10^9$) which represent the change in health after drinking that potion.
-----Output-----
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
-----Examples-----
Input
6
4 -4 1 -3 1 -3
Output
5
-----Note-----
For the sample, you can drink $5$ potions by taking potions $1$, $3$, $4$, $5$ and $6$. It is not possible to drink all $6$ potions because your health will go negative at some point | n = int(input())
A = list(map(int, input().split()))
sm = 0
cnt = 0
Neg = []
for i in range(n):
if sm + A[i] >= 0:
cnt += 1
sm += A[i]
if A[i] < 0:
Neg.append(A[i])
elif Neg:
if A[i] > min(Neg):
sm += A[i] - min(Neg)
Neg.remove(min(Neg))
Neg.append(A[i])
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.